running: tweaks
[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
18 /*---------------------------------------------------------------------------*/
19
20 static int score_time_comp(const struct score *S, int i, int j)
21 {
22     if (S->timer[i] < S->timer[j])
23         return 1;
24
25     if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
26         return 1;
27
28     return 0;
29 }
30
31 static int score_coin_comp(const struct score *S, int i, int j)
32 {
33     if (S->coins[i] > S->coins[j])
34         return 1;
35
36     if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
37         return 1;
38
39     return 0;
40 }
41
42 static void score_swap(struct score *S, int i, int j)
43 {
44     char player[MAXNAM];
45     int  tmp;
46
47     strncpy(player,       S->player[i], MAXNAM);
48     strncpy(S->player[i], S->player[j], MAXNAM);
49     strncpy(S->player[j], player,       MAXNAM);
50
51     tmp         = S->timer[i];
52     S->timer[i] = S->timer[j];
53     S->timer[j] = tmp;
54
55     tmp         = S->coins[i];
56     S->coins[i] = S->coins[j];
57     S->coins[j] = tmp;
58 }
59
60 /*---------------------------------------------------------------------------*/
61
62 void score_init_hs(struct score *s, int timer, int coins)
63 {
64     int i;
65
66     strcpy(s->player[0], "Hard");
67     strcpy(s->player[1], "Medium");
68     strcpy(s->player[2], "Easy");
69     strcpy(s->player[3], "");
70
71     for (i = 0; i < NSCORE + 1; i++)
72     {
73         s->timer[i] = timer;
74         s->coins[i] = coins;
75     }
76 }
77
78 int score_time_insert(struct score *s, const char *player, int timer, int coins)
79 {
80     int i;
81
82     strncpy(s->player[3], player, MAXNAM);
83     s->timer[3] = timer;
84     s->coins[3] = coins;
85
86     for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
87         score_swap(s, i + 1, i);
88
89     return i + 1;
90 }
91
92 int score_coin_insert(struct score *s, const char *player, int timer, int coins)
93 {
94     int i;
95
96     strncpy(s->player[3], player, MAXNAM);
97     s->timer[3] = timer;
98     s->coins[3] = coins;
99
100     for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
101         score_swap(s, i + 1, i);
102
103     return i + 1;
104 }
105
106 /*---------------------------------------------------------------------------*/