Rev 4565 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4565 | Rev 4573 | ||
---|---|---|---|
1 | /* $OpenBSD: scores.c,v 1.11 2006/04/20 03:25:36 ray Exp $ */ |
1 | /* $OpenBSD: scores.c,v 1.11 2006/04/20 03:25:36 ray Exp $ */ |
2 | /* $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $ */ |
2 | /* $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $ */ |
3 | 3 | ||
4 | /*- |
4 | /*- |
5 | * Copyright (c) 1992, 1993 |
5 | * Copyright (c) 1992, 1993 |
6 | * The Regents of the University of California. All rights reserved. |
6 | * The Regents of the University of California. All rights reserved. |
7 | * |
7 | * |
8 | * This code is derived from software contributed to Berkeley by |
8 | * This code is derived from software contributed to Berkeley by |
9 | * Chris Torek and Darren F. Provine. |
9 | * Chris Torek and Darren F. Provine. |
10 | * |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. Neither the name of the University nor the names of its contributors |
19 | * 3. Neither the name of the University nor the names of its contributors |
20 | * may be used to endorse or promote products derived from this software |
20 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. |
21 | * without specific prior written permission. |
22 | * |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | * SUCH DAMAGE. |
33 | * SUCH DAMAGE. |
34 | * |
34 | * |
35 | * @(#)scores.c 8.1 (Berkeley) 5/31/93 |
35 | * @(#)scores.c 8.1 (Berkeley) 5/31/93 |
36 | */ |
36 | */ |
37 | 37 | ||
38 | /** @addtogroup tetris |
38 | /** @addtogroup tetris |
39 | * @{ |
39 | * @{ |
40 | */ |
40 | */ |
41 | /** @file |
41 | /** @file |
42 | */ |
42 | */ |
43 | 43 | ||
44 | /* |
44 | /* |
45 | * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu) |
45 | * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu) |
46 | * modified 22 January 1992, to limit the number of entries any one |
46 | * modified 22 January 1992, to limit the number of entries any one |
47 | * person has. |
47 | * person has. |
48 | * |
48 | * |
49 | * Major whacks since then. |
49 | * Major whacks since then. |
50 | */ |
50 | */ |
51 | 51 | ||
52 | #include <errno.h> |
52 | #include <errno.h> |
53 | #include <stdio.h> |
53 | #include <stdio.h> |
54 | #include <string.h> |
54 | #include <string.h> |
55 | #include <io/console.h> |
55 | #include <io/console.h> |
56 | #include <io/keycode.h> |
56 | #include <io/keycode.h> |
57 | #include <vfs/vfs.h> |
57 | #include <vfs/vfs.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include <fcntl.h> |
59 | #include <fcntl.h> |
60 | #include <err.h> |
60 | #include <err.h> |
61 | #include <time.h> |
61 | #include <time.h> |
62 | 62 | ||
63 | #include "screen.h" |
63 | #include "screen.h" |
64 | #include "tetris.h" |
64 | #include "tetris.h" |
65 | #include "scores.h" |
65 | #include "scores.h" |
66 | 66 | ||
67 | /* |
67 | /* |
68 | * Within this code, we can hang onto one extra "high score", leaving |
68 | * Within this code, we can hang onto one extra "high score", leaving |
69 | * room for our current score (whether or not it is high). |
69 | * room for our current score (whether or not it is high). |
70 | * |
70 | * |
71 | * We also sometimes keep tabs on the "highest" score on each level. |
71 | * We also sometimes keep tabs on the "highest" score on each level. |
72 | * As long as the scores are kept sorted, this is simply the first one at |
72 | * As long as the scores are kept sorted, this is simply the first one at |
73 | * that level. |
73 | * that level. |
74 | */ |
74 | */ |
75 | 75 | ||
76 | #define NUMSPOTS (MAXHISCORES + 1) |
76 | #define NUMSPOTS (MAXHISCORES + 1) |
77 | #define NLEVELS (MAXLEVEL + 1) |
77 | #define NLEVELS (MAXLEVEL + 1) |
78 | 78 | ||
79 | static struct highscore scores[NUMSPOTS]; |
79 | static struct highscore scores[NUMSPOTS]; |
80 | 80 | ||
81 | /** Copy from hiscore table score with index src to dest |
81 | /** Copy from hiscore table score with index src to dest |
82 | * |
82 | * |
83 | */ |
83 | */ |
84 | static void copyhiscore(int dest, int src) |
84 | static void copyhiscore(int dest, int src) |
85 | { |
85 | { |
86 | str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, |
86 | str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, |
87 | scores[src].hs_name); |
87 | scores[src].hs_name); |
88 | scores[dest].hs_score = scores[src].hs_score; |
88 | scores[dest].hs_score = scores[src].hs_score; |
89 | scores[dest].hs_level = scores[src].hs_level; |
89 | scores[dest].hs_level = scores[src].hs_level; |
90 | } |
90 | } |
91 | 91 | ||
92 | void showscores(int firstgame) |
92 | void showscores(int firstgame) |
93 | { |
93 | { |
94 | int i; |
94 | int i; |
95 | 95 | ||
96 | clear_screen(); |
96 | clear_screen(); |
97 | moveto(10, 0); |
97 | moveto(10, 0); |
98 | printf("\tRank \tLevel \tName\t points\n"); |
98 | printf("\tRank \tLevel \tName\t points\n"); |
99 | printf("\t========================================================\n"); |
99 | printf("\t========================================================\n"); |
100 | 100 | ||
101 | for (i = 0; i < NUMSPOTS - 1; i++) |
101 | for (i = 0; i < NUMSPOTS - 1; i++) |
102 | printf("\t%6d %6d %-16s %20d\n", |
102 | printf("\t%6d %6d %-16s %20d\n", |
103 | i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score); |
103 | i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score); |
104 | 104 | ||
105 | if (!firstgame) { |
105 | if (!firstgame) { |
106 | printf("\t========================================================\n"); |
106 | printf("\t========================================================\n"); |
107 | printf("\t Last %6d %-16s %20d\n", |
107 | printf("\t Last %6d %-16s %20d\n", |
108 | scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score); |
108 | scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score); |
109 | } |
109 | } |
110 | 110 | ||
111 | printf("\n\n\n\n\tPress any key to return to main menu."); |
111 | printf("\n\n\n\n\tPress any key to return to main menu."); |
112 | getchar(); |
112 | getchar(); |
113 | } |
113 | } |
114 | 114 | ||
115 | void insertscore(int score, int level) |
115 | void insertscore(int score, int level) |
116 | { |
116 | { |
117 | int i; |
117 | int i; |
118 | int j; |
118 | int j; |
119 | size_t off; |
119 | size_t off; |
120 | console_event_t ev; |
120 | console_event_t ev; |
121 | 121 | ||
122 | clear_screen(); |
122 | clear_screen(); |
123 | moveto(10, 10); |
123 | moveto(10, 10); |
124 | puts("Insert your name: "); |
124 | puts("Insert your name: "); |
125 | str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, |
125 | str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, |
126 | "Player"); |
126 | "Player"); |
127 | i = 6; |
127 | i = 6; |
128 | off = 6; |
128 | off = 6; |
129 | 129 | ||
130 | moveto(10 , 28); |
130 | moveto(10 , 28); |
131 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i, |
131 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i, |
132 | "........................................"); |
132 | "........................................"); |
133 | 133 | ||
134 | while (1) { |
134 | while (1) { |
135 | fflush(stdout); |
135 | fflush(stdout); |
136 | if (!console_get_event(fphone(stdin), &ev)) |
136 | if (!console_get_event(fphone(stdin), &ev)) |
137 | exit(1); |
137 | exit(1); |
138 | 138 | ||
139 | if (ev.type == KEY_RELEASE) |
139 | if (ev.type == KEY_RELEASE) |
140 | continue; |
140 | continue; |
141 | 141 | ||
142 | if (ev.key == KC_ENTER || ev.key == KC_NENTER) |
142 | if (ev.key == KC_ENTER || ev.key == KC_NENTER) |
143 | break; |
143 | break; |
144 | 144 | ||
145 | if (ev.key == KC_BACKSPACE) { |
145 | if (ev.key == KC_BACKSPACE) { |
146 | if (i > 0) { |
146 | if (i > 0) { |
147 | wchar_t uc; |
147 | wchar_t uc; |
148 | 148 | ||
149 | --i; |
149 | --i; |
150 | while (off > 0) { |
150 | while (off > 0) { |
151 | --off; |
151 | --off; |
152 | size_t otmp = off; |
152 | size_t otmp = off; |
153 | uc = str_decode(scores[NUMSPOTS - 1].hs_name, |
153 | uc = str_decode(scores[NUMSPOTS - 1].hs_name, |
154 | &otmp, STR_BOUNDS(MAXLOGNAME) + 1); |
154 | &otmp, STR_BOUNDS(MAXLOGNAME) + 1); |
155 | if (uc != U_SPECIAL) |
155 | if (uc != U_SPECIAL) |
156 | break; |
156 | break; |
157 | } |
157 | } |
158 | 158 | ||
159 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; |
159 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; |
160 | } |
160 | } |
161 | } else if (ev.c != '\0') { |
161 | } else if (ev.c != '\0') { |
162 | if (i < (MAXLOGNAME - 1)) { |
162 | if (i < (MAXLOGNAME - 1)) { |
163 | if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name, |
163 | if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name, |
164 | &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) { |
164 | &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) { |
165 | ++i; |
165 | ++i; |
166 | } |
166 | } |
167 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; |
167 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; |
168 | } |
168 | } |
169 | } |
169 | } |
170 | 170 | ||
171 | moveto(10, 28); |
171 | moveto(10, 28); |
172 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i, |
172 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i, |
173 | "........................................"); |
173 | "........................................"); |
174 | } |
174 | } |
175 | 175 | ||
176 | scores[NUMSPOTS - 1].hs_score = score; |
176 | scores[NUMSPOTS - 1].hs_score = score; |
177 | scores[NUMSPOTS - 1].hs_level = level; |
177 | scores[NUMSPOTS - 1].hs_level = level; |
178 | 178 | ||
179 | i = NUMSPOTS - 1; |
179 | i = NUMSPOTS - 1; |
180 | while ((i > 0) && (scores[i - 1].hs_score < score)) |
180 | while ((i > 0) && (scores[i - 1].hs_score < score)) |
181 | i--; |
181 | i--; |
182 | 182 | ||
183 | for (j = NUMSPOTS - 2; j > i; j--) |
183 | for (j = NUMSPOTS - 2; j > i; j--) |
184 | copyhiscore(j, j-1); |
184 | copyhiscore(j, j-1); |
185 | 185 | ||
186 | copyhiscore(i, NUMSPOTS - 1); |
186 | copyhiscore(i, NUMSPOTS - 1); |
187 | } |
187 | } |
188 | 188 | ||
189 | void initscores(void) |
189 | void initscores(void) |
190 | { |
190 | { |
191 | int i; |
191 | int i; |
192 | for (i = 0; i < NUMSPOTS; i++) { |
192 | for (i = 0; i < NUMSPOTS; i++) { |
193 | str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team"); |
193 | str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team"); |
194 | scores[i].hs_score = (NUMSPOTS - i) * 200; |
194 | scores[i].hs_score = (NUMSPOTS - i) * 200; |
195 | scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1); |
195 | scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1); |
196 | } |
196 | } |
197 | } |
197 | } |
198 | 198 | ||
199 | int loadscores(void) |
199 | int loadscores(void) |
200 | { |
200 | { |
201 | FILE *f; |
201 | FILE *f; |
202 | size_t cnt; |
202 | size_t cnt; |
203 | int rc; |
203 | int rc; |
204 | 204 | ||
205 | f = fopen("/tetris.sco", "rb"); |
205 | f = fopen("/data/tetris.sco", "rb"); |
206 | if (f == NULL) |
206 | if (f == NULL) |
207 | return ENOENT; |
207 | return ENOENT; |
208 | 208 | ||
209 | cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f); |
209 | cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f); |
210 | rc = fclose(f); |
210 | rc = fclose(f); |
211 | 211 | ||
212 | if (cnt != NUMSPOTS || rc != 0) |
212 | if (cnt != NUMSPOTS || rc != 0) |
213 | return EIO; |
213 | return EIO; |
214 | 214 | ||
215 | return EOK; |
215 | return EOK; |
216 | } |
216 | } |
217 | 217 | ||
218 | void savescores(void) |
218 | void savescores(void) |
219 | { |
219 | { |
220 | FILE *f; |
220 | FILE *f; |
221 | size_t cnt; |
221 | size_t cnt; |
222 | int rc; |
222 | int rc; |
223 | 223 | ||
224 | f = fopen("/tetris.sco", "wb"); |
224 | f = fopen("/data/tetris.sco", "wb"); |
225 | cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f); |
225 | cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f); |
226 | rc = fclose(f); |
226 | rc = fclose(f); |
227 | 227 | ||
228 | if (cnt != NUMSPOTS || rc != 0) |
228 | if (cnt != NUMSPOTS || rc != 0) |
229 | printf("Error saving score table\n"); |
229 | printf("Error saving score table\n"); |
230 | } |
230 | } |
231 | 231 | ||
232 | /** @} |
232 | /** @} |
233 | */ |
233 | */ |
234 | 234 |