JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Raised default high-scores. Alligned seconds for high scores < 1:00
[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 "config.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;
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                         fscanf(f, titles[j]);
74                         for(i = 0; i<N_SCORES; i++) {
75                                 fscanf(f, "%d %31[^\n]\n", &g_scores[j][i].score, g_scores[j][i].name);
76                         }
77                 }
78                 fclose(f);
79         }
80 }
81
82 void
83 write_high_score_table()
84 {
85         FILE *f;
86         int i, j;
87         
88         f = open_score_file("w");
89         if(f) {
90                 // If the file exists, write to it
91                 for(j=0; j<2; j++) {
92                         fprintf(f, titles[j]);
93                         for(i = 0; i<N_SCORES; i++) {
94                                 fprintf (f, "%d %.31s\n", g_scores[j][i].score, g_scores[j][i].name);
95                         }
96                 }
97                 fclose(f);
98         }
99 }
100
101 int
102 score_rank(int score)
103 {
104         int i;
105
106         for(i=0; i<N_SCORES; i++) {
107                 if(score > g_scores[g_easy][i].score) return i;
108         }
109         return -1;
110 }
111
112 int
113 new_high_score(int score)
114 {
115         cur_score = -1;  // assume not a new high score
116         if(score <= g_scores[g_easy][LOW_SCORE].score) return false;
117         read_high_score_table();
118         cur_score = score_rank(score);
119         return cur_score >= 0;
120 }
121
122 int
123 insert_score(int score)
124 {
125         int i;
126
127         // Move all lower scores down a notch, losing lowest score forever.
128         if(strcmp(g_scores[g_easy][cur_score].name, "-") != 0)
129                 for(i=LOW_SCORE; i>cur_score; i--)
130                         g_scores[g_easy][i] = g_scores[g_easy][i-1];
131
132         // initialize new score entry.
133         g_scores[g_easy][cur_score].score = score;
134         for(i=0; i<32; i++) g_scores[g_easy][cur_score].name[i] = 0;
135         return true;
136 }
137
138 int
139 snprintscore(char *s, size_t n, int score)
140 {
141         int min = score/60000;
142         int sec = score/1000%60;
143         int tenths = score%1000/100;
144         if(min) {
145                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
146         } else {
147                 return snprintf(s, n, "   %2d.%d", sec, tenths);
148         }
149 }
150
151 void
152 show_score(void)
153 {
154         char s[16];
155         int r = snprintf(s, 16, "Time: ");
156         snprintscore(s+r, 16-r, score);
157         font_write(XSIZE-250, 0, s);
158 }
159
160 void
161 display_scores(uint32_t x, uint32_t y)
162 {
163         char t[1024];
164         int i,h = font_height();
165         int display_cursor = (SDL_GetTicks() / CURSOR_BLINK_TIME) % 2;
166
167
168         font_write(x+30, y, "High scores");
169         y += h;
170         if(g_easy) font_write(x+75,y,"(easy)");
171         else font_write(x+60,y,"(normal)");
172         for(i = 0; i<N_SCORES; i++) {
173                 y += h;
174                 snprintf(t, 1024, "#%1d",i+1);
175                 font_write(x, y, t);
176                 snprintscore(t, 1024, g_scores[g_easy][i].score);
177                 font_write(x+50, y, t);
178                 if(display_cursor && i == cur_score) snprintf(t, 1024, "%s_", g_scores[g_easy][i].name);
179                 else snprintf(t, 1024, "%s", g_scores[g_easy][i].name);
180                 font_write(x+180, y, t);
181         }
182 }
183
184 int
185 process_score_input(SDL_keysym *key)
186 {
187         char *name;
188         int n;
189         
190         name = g_scores[g_easy][cur_score].name;
191         n = strlen(name);
192
193         if(key->sym == SDLK_BACKSPACE) {
194                 if(n > 0) name[--n]=0;
195         } else {
196                 if(key->sym == SDLK_RETURN || key->sym == SDLK_KP_ENTER) {
197                         SDL_EnableUNICODE(0);
198                         cur_score = -1;
199                         if(n == 0) {
200                                 name[0] = '-';
201                         }
202                         return false;
203                 } else if(n < 12) {
204                         if(key->unicode >= 32 && key->unicode <= 126) {
205                                 name[n++] = key->unicode;
206                         }
207                 } // else drop it
208         }
209         return true;
210 }