tennis.map: Texture tweak
[neverball] / ball / score.c
1 /*
2  * Copyright (C) 2007 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <string.h>
16 #include "score.h"
17 #include "common.h"
18
19 /*---------------------------------------------------------------------------*/
20
21 static int score_time_comp(const struct score *S, int i, int j)
22 {
23     if (S->timer[i] < S->timer[j])
24         return 1;
25
26     if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
27         return 1;
28
29     return 0;
30 }
31
32 static int score_coin_comp(const struct score *S, int i, int j)
33 {
34     if (S->coins[i] > S->coins[j])
35         return 1;
36
37     if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
38         return 1;
39
40     return 0;
41 }
42
43 static void score_swap(struct score *S, int i, int j)
44 {
45     char player[MAXNAM];
46     int  tmp;
47
48     SAFECPY(player,       S->player[i]);
49     SAFECPY(S->player[i], S->player[j]);
50     SAFECPY(S->player[j], player);
51
52     tmp         = S->timer[i];
53     S->timer[i] = S->timer[j];
54     S->timer[j] = tmp;
55
56     tmp         = S->coins[i];
57     S->coins[i] = S->coins[j];
58     S->coins[j] = tmp;
59 }
60
61 /*---------------------------------------------------------------------------*/
62
63 static void score_insert(struct score *s, int i,
64                          const char *player, int timer, int coins)
65 {
66     SAFECPY(s->player[i], player);
67
68     s->timer[i] = timer;
69     s->coins[i] = coins;
70 }
71
72 void score_init_hs(struct score *s, int timer, int coins)
73 {
74     score_insert(s, RANK_HARD, "Hard",   timer, coins);
75     score_insert(s, RANK_MEDM, "Medium", timer, coins);
76     score_insert(s, RANK_EASY, "Easy",   timer, coins);
77     score_insert(s, RANK_LAST, "",       timer, coins);
78 }
79
80 void score_time_insert(struct score *s, int *rank,
81                        const char *player, int timer, int coins)
82 {
83     int i;
84
85     score_insert(s, RANK_LAST, player, timer, coins);
86
87     if (rank)
88     {
89         for (i = RANK_EASY; i >= RANK_HARD && score_time_comp(s, i + 1, i); i--)
90             score_swap(s, i + 1, i);
91
92         *rank = i + 1;
93     }
94 }
95
96 void score_coin_insert(struct score *s, int *rank,
97                        const char *player, int timer, int coins)
98 {
99     int i;
100
101     score_insert(s, RANK_LAST, player, timer, coins);
102
103     if (rank)
104     {
105         for (i = RANK_EASY; i >= RANK_HARD && score_coin_comp(s, i + 1, i); i--)
106             score_swap(s, i + 1, i);
107
108         *rank = i + 1;
109     }
110 }
111
112 /*---------------------------------------------------------------------------*/