| 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 <stdbool.h> | 
|---|
| 62 | #include <stdio.h> | 
|---|
| 63 | #include <str.h> | 
|---|
| 64 | #include <io/console.h> | 
|---|
| 65 | #include <io/keycode.h> | 
|---|
| 66 | #include <vfs/vfs.h> | 
|---|
| 67 | #include <stdlib.h> | 
|---|
| 68 | #include <time.h> | 
|---|
| 69 | #include "screen.h" | 
|---|
| 70 | #include "tetris.h" | 
|---|
| 71 | #include "scores.h" | 
|---|
| 72 |  | 
|---|
| 73 | /* | 
|---|
| 74 | * Within this code, we can hang onto one extra "high score", leaving | 
|---|
| 75 | * room for our current score (whether or not it is high). | 
|---|
| 76 | * | 
|---|
| 77 | * We also sometimes keep tabs on the "highest" score on each level. | 
|---|
| 78 | * As long as the scores are kept sorted, this is simply the first one at | 
|---|
| 79 | * that level. | 
|---|
| 80 | */ | 
|---|
| 81 |  | 
|---|
| 82 | #define NUMSPOTS  (MAXHISCORES + 1) | 
|---|
| 83 | #define NLEVELS   (MAXLEVEL + 1) | 
|---|
| 84 |  | 
|---|
| 85 | static struct highscore scores[NUMSPOTS]; | 
|---|
| 86 |  | 
|---|
| 87 | /** Copy from hiscore table score with index src to dest | 
|---|
| 88 | * | 
|---|
| 89 | */ | 
|---|
| 90 | static void copyhiscore(int dest, int src) | 
|---|
| 91 | { | 
|---|
| 92 | str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, | 
|---|
| 93 | scores[src].hs_name); | 
|---|
| 94 | scores[dest].hs_score = scores[src].hs_score; | 
|---|
| 95 | scores[dest].hs_level = scores[src].hs_level; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void showscores(int firstgame) | 
|---|
| 99 | { | 
|---|
| 100 | int i; | 
|---|
| 101 |  | 
|---|
| 102 | clear_screen(); | 
|---|
| 103 | moveto(10, 0); | 
|---|
| 104 | printf("\tRank \tLevel \tName\t                     points\n"); | 
|---|
| 105 | printf("\t========================================================\n"); | 
|---|
| 106 |  | 
|---|
| 107 | for (i = 0; i < NUMSPOTS - 1; i++) | 
|---|
| 108 | printf("\t%6d %6d %-16s %20d\n", | 
|---|
| 109 | i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score); | 
|---|
| 110 |  | 
|---|
| 111 | if (!firstgame) { | 
|---|
| 112 | printf("\t========================================================\n"); | 
|---|
| 113 | printf("\t  Last %6d %-16s %20d\n", | 
|---|
| 114 | scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | printf("\n\n\n\n\tPress any key to return to main menu."); | 
|---|
| 118 | getchar(); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void insertscore(int score, int level) | 
|---|
| 122 | { | 
|---|
| 123 | int i; | 
|---|
| 124 | int j; | 
|---|
| 125 | size_t off; | 
|---|
| 126 | cons_event_t ev; | 
|---|
| 127 | kbd_event_t *kev; | 
|---|
| 128 |  | 
|---|
| 129 | clear_screen(); | 
|---|
| 130 | moveto(10, 10); | 
|---|
| 131 | fputs("Insert your name: ", stdout); | 
|---|
| 132 | str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, | 
|---|
| 133 | "Player"); | 
|---|
| 134 | i = 6; | 
|---|
| 135 | off = 6; | 
|---|
| 136 |  | 
|---|
| 137 | moveto(10, 28); | 
|---|
| 138 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i, | 
|---|
| 139 | "........................................"); | 
|---|
| 140 |  | 
|---|
| 141 | while (true) { | 
|---|
| 142 | console_flush(console); | 
|---|
| 143 | if (!console_get_event(console, &ev)) | 
|---|
| 144 | exit(1); | 
|---|
| 145 |  | 
|---|
| 146 | if (ev.type != CEV_KEY || ev.ev.key.type == KEY_RELEASE) | 
|---|
| 147 | continue; | 
|---|
| 148 |  | 
|---|
| 149 | kev = &ev.ev.key; | 
|---|
| 150 |  | 
|---|
| 151 | if (kev->key == KC_ENTER || kev->key == KC_NENTER) | 
|---|
| 152 | break; | 
|---|
| 153 |  | 
|---|
| 154 | if (kev->key == KC_BACKSPACE) { | 
|---|
| 155 | if (i > 0) { | 
|---|
| 156 | wchar_t uc; | 
|---|
| 157 |  | 
|---|
| 158 | --i; | 
|---|
| 159 | while (off > 0) { | 
|---|
| 160 | --off; | 
|---|
| 161 | size_t otmp = off; | 
|---|
| 162 | uc = str_decode(scores[NUMSPOTS - 1].hs_name, | 
|---|
| 163 | &otmp, STR_BOUNDS(MAXLOGNAME) + 1); | 
|---|
| 164 | if (uc != U_SPECIAL) | 
|---|
| 165 | break; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; | 
|---|
| 169 | } | 
|---|
| 170 | } else if (kev->c != '\0') { | 
|---|
| 171 | if (i < (MAXLOGNAME - 1)) { | 
|---|
| 172 | if (chr_encode(kev->c, scores[NUMSPOTS - 1].hs_name, | 
|---|
| 173 | &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) { | 
|---|
| 174 | ++i; | 
|---|
| 175 | } | 
|---|
| 176 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; | 
|---|
| 177 | } | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | moveto(10, 28); | 
|---|
| 181 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i, | 
|---|
| 182 | "........................................"); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | scores[NUMSPOTS - 1].hs_score = score; | 
|---|
| 186 | scores[NUMSPOTS - 1].hs_level = level; | 
|---|
| 187 |  | 
|---|
| 188 | i = NUMSPOTS - 1; | 
|---|
| 189 | while ((i > 0) && (scores[i - 1].hs_score < score)) | 
|---|
| 190 | i--; | 
|---|
| 191 |  | 
|---|
| 192 | for (j = NUMSPOTS - 2; j > i; j--) | 
|---|
| 193 | copyhiscore(j, j - 1); | 
|---|
| 194 |  | 
|---|
| 195 | copyhiscore(i, NUMSPOTS - 1); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | void initscores(void) | 
|---|
| 199 | { | 
|---|
| 200 | int i; | 
|---|
| 201 | for (i = 0; i < NUMSPOTS; i++) { | 
|---|
| 202 | str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team"); | 
|---|
| 203 | scores[i].hs_score = (NUMSPOTS - i) * 200; | 
|---|
| 204 | scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1); | 
|---|
| 205 | } | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | errno_t loadscores(void) | 
|---|
| 209 | { | 
|---|
| 210 | FILE *f; | 
|---|
| 211 | size_t cnt; | 
|---|
| 212 | int rc; | 
|---|
| 213 |  | 
|---|
| 214 | f = fopen("/data/tetris.sco", "rb"); | 
|---|
| 215 | if (f == NULL) | 
|---|
| 216 | return ENOENT; | 
|---|
| 217 |  | 
|---|
| 218 | cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f); | 
|---|
| 219 | rc = fclose(f); | 
|---|
| 220 |  | 
|---|
| 221 | if (cnt != NUMSPOTS || rc != 0) | 
|---|
| 222 | return EIO; | 
|---|
| 223 |  | 
|---|
| 224 | return EOK; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | void savescores(void) | 
|---|
| 228 | { | 
|---|
| 229 | FILE *f; | 
|---|
| 230 | size_t cnt; | 
|---|
| 231 | int rc; | 
|---|
| 232 |  | 
|---|
| 233 | f = fopen("/data/tetris.sco", "wb"); | 
|---|
| 234 | if (f == NULL) { | 
|---|
| 235 | printf("Error creating table\n"); | 
|---|
| 236 | return; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f); | 
|---|
| 240 | rc = fclose(f); | 
|---|
| 241 |  | 
|---|
| 242 | if (cnt != NUMSPOTS || rc != 0) | 
|---|
| 243 | printf("Error saving score table\n"); | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | /** @} | 
|---|
| 247 | */ | 
|---|