JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
bump version to 0.5.8
[vor.git] / score.c
1 /* Variations on RockDodger
2  * Copyright (C) 2004 Joshua Grams <josh@qualdan.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18
19 #include <SDL.h>
20 #include <SDL_keysym.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "font.h"
26
27 #include "common.h"
28 #include "vorconfig.h"
29 #include "file.h"
30 #include "globals.h"
31 #include "score.h"
32
33 // High score table
34 // below are the defaults (when there's no high score file) in miliseconds
35 struct highscore g_scores[2][N_SCORES] = {
36         {
37                 {120000,"-"},
38                 {105000,"-"},
39                 { 90000,"-"},
40                 { 75000,"-"},
41                 { 60000,"-"},
42                 { 50000,"-"},
43                 { 40000,"-"},
44                 { 30000,"-"}
45         },
46         {
47                 {120000,"-"},
48                 {105000,"-"},
49                 { 90000,"-"},
50                 { 75000,"-"},
51                 { 60000,"-"},
52                 { 50000,"-"},
53                 { 40000,"-"},
54                 { 30000,"-"}
55         }
56 };
57
58 static char *titles[2] = { "Normal\n", "Easy\n" };
59
60 int g_easy = 0;
61 int cur_score = -1; // which score we're currently entering.
62
63 void
64 read_high_score_table()
65 {
66         FILE *f;
67         int i, j, ret;
68         
69         f = open_score_file("r");
70         if(f) {
71                 // If the file exists, read from it
72                 for(j=0; j<2; j++) {
73                         ret = fseek(f, strlen(titles[j]), SEEK_CUR);
74                         if (ret != 0) {
75                                 break;
76                         }
77                         for(i = 0; i<N_SCORES; i++) {
78                                 ret = fscanf(f, "%d %31[^\n]\n", &g_scores[j][i].score, g_scores[j][i].name);
79                                 if (ret != 2) {
80                                         break;
81                                 }
82                         }
83                 }
84                 fclose(f);
85         }
86 }
87
88 void
89 write_high_score_table()
90 {
91         FILE *f;
92         int i, j;
93         
94         f = open_score_file("w");
95         if(f) {
96                 // If the file exists, write to it
97                 for(j=0; j<2; j++) {
98                         fprintf(f, "%s", titles[j]);
99                         for(i = 0; i<N_SCORES; i++) {
100                                 fprintf (f, "%d %.31s\n", g_scores[j][i].score, g_scores[j][i].name);
101                         }
102                 }
103                 fclose(f);
104         }
105 }
106
107 int
108 score_rank(int score)
109 {
110         int i;
111
112         for(i=0; i<N_SCORES; i++) {
113                 if(score > g_scores[g_easy][i].score) return i;
114         }
115         return -1;
116 }
117
118 int
119 new_high_score(int score)
120 {
121         cur_score = -1;  // assume not a new high score
122         if(score <= g_scores[g_easy][LOW_SCORE].score) return false;
123         read_high_score_table();
124         cur_score = score_rank(score);
125         return cur_score >= 0;
126 }
127
128 int
129 insert_score(int score)
130 {
131         int i;
132
133         // Move all lower scores down a notch, losing lowest score forever.
134         if(strcmp(g_scores[g_easy][cur_score].name, "-") != 0)
135                 for(i=LOW_SCORE; i>cur_score; i--)
136                         g_scores[g_easy][i] = g_scores[g_easy][i-1];
137
138         // initialize new score entry.
139         g_scores[g_easy][cur_score].score = score;
140         for(i=0; i<32; i++) g_scores[g_easy][cur_score].name[i] = 0;
141         return true;
142 }
143
144 int
145 snprintscore(char *s, size_t n, int score)
146 {
147         int min = score/60000;
148         int sec = score/1000%60;
149         int tenths = score%1000/100;
150         if(min) {
151                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
152         } else {
153                 return snprintf(s, n, "   %2d.%d", sec, tenths);
154         }
155 }
156
157 void
158 show_score(void)
159 {
160         char s[16];
161         int r = snprintf(s, 16, "Time: ");
162         snprintscore(s+r, 16-r, score);
163         font_write(XSIZE-250, 0, s);
164 }
165
166 void
167 display_scores(uint32_t x, uint32_t y)
168 {
169         char t[1024];
170         int i,h = font_height();
171         int display_cursor = (SDL_GetTicks() / CURSOR_BLINK_TIME) % 2;
172
173
174         font_write(x+30, y, "High scores");
175         y += h;
176         if(g_easy) font_write(x+75,y,"(easy)");
177         else font_write(x+60,y,"(normal)");
178         for(i = 0; i<N_SCORES; i++) {
179                 y += h;
180                 snprintf(t, 1024, "#%1d",i+1);
181                 font_write(x, y, t);
182                 snprintscore(t, 1024, g_scores[g_easy][i].score);
183                 font_write(x+50, y, t);
184                 if(display_cursor && i == cur_score) snprintf(t, 1024, "%s_", g_scores[g_easy][i].name);
185                 else snprintf(t, 1024, "%s", g_scores[g_easy][i].name);
186                 font_write(x+180, y, t);
187         }
188 }
189
190 int
191 process_score_input(SDL_keysym *key)
192 {
193         char *name;
194         int n;
195         
196         name = g_scores[g_easy][cur_score].name;
197         n = strlen(name);
198
199         if(key->sym == SDLK_BACKSPACE) {
200                 if(n > 0) name[--n]=0;
201         } else {
202                 if(key->sym == SDLK_RETURN || key->sym == SDLK_KP_ENTER) {
203                         SDL_EnableUNICODE(0);
204                         cur_score = -1;
205                         if(n == 0) {
206                                 name[0] = '-';
207                         }
208                         return false;
209                 } else if(n < 12) {
210                         if(key->unicode >= 32 && key->unicode <= 126) {
211                                 name[n++] = key->unicode;
212                         }
213                 } // else drop it
214         }
215         return true;
216 }