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