[79ae36dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
[e9a3c52] | 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 | *
|
---|
[79ae36dd] | 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.
|
---|
[e9a3c52] | 43 | *
|
---|
| 44 | */
|
---|
| 45 |
|
---|
[b2951e2] | 46 | /** @addtogroup tetris
|
---|
[ebe70f1] | 47 | * @{
|
---|
[b2951e2] | 48 | */
|
---|
| 49 | /** @file
|
---|
| 50 | */
|
---|
| 51 |
|
---|
[e9a3c52] | 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 | */
|
---|
[ebe70f1] | 59 |
|
---|
[e9a3c52] | 60 | #include <errno.h>
|
---|
[76d0981d] | 61 | #include <stdbool.h>
|
---|
[6a7f6b8] | 62 | #include <stdio.h>
|
---|
[19f857a] | 63 | #include <str.h>
|
---|
[0c25c10] | 64 | #include <io/console.h>
|
---|
| 65 | #include <io/keycode.h>
|
---|
| 66 | #include <vfs/vfs.h>
|
---|
[3a180ad] | 67 | #include <stdlib.h>
|
---|
[ebe70f1] | 68 | #include <time.h>
|
---|
[e9a3c52] | 69 | #include "screen.h"
|
---|
| 70 | #include "tetris.h"
|
---|
[c0e674a] | 71 | #include "scores.h"
|
---|
[e9a3c52] | 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 |
|
---|
[ebe70f1] | 82 | #define NUMSPOTS (MAXHISCORES + 1)
|
---|
| 83 | #define NLEVELS (MAXLEVEL + 1)
|
---|
| 84 |
|
---|
[9996ed5] | 85 | static struct highscore scores[NUMSPOTS];
|
---|
[e9a3c52] | 86 |
|
---|
[ebe70f1] | 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 | }
|
---|
[e9a3c52] | 97 |
|
---|
[9996ed5] | 98 | void showscores(int firstgame)
|
---|
| 99 | {
|
---|
| 100 | int i;
|
---|
[a35b458] | 101 |
|
---|
[9996ed5] | 102 | clear_screen();
|
---|
| 103 | moveto(10, 0);
|
---|
| 104 | printf("\tRank \tLevel \tName\t points\n");
|
---|
| 105 | printf("\t========================================================\n");
|
---|
[a35b458] | 106 |
|
---|
[ebe70f1] | 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);
|
---|
[a35b458] | 110 |
|
---|
[9996ed5] | 111 | if (!firstgame) {
|
---|
| 112 | printf("\t========================================================\n");
|
---|
[ebe70f1] | 113 | printf("\t Last %6d %-16s %20d\n",
|
---|
| 114 | scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
|
---|
[9996ed5] | 115 | }
|
---|
[a35b458] | 116 |
|
---|
[9996ed5] | 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 | {
|
---|
[ebe70f1] | 123 | int i;
|
---|
| 124 | int j;
|
---|
[3a180ad] | 125 | size_t off;
|
---|
[07b7c48] | 126 | cons_event_t ev;
|
---|
| 127 | kbd_event_t *kev;
|
---|
[87822ce] | 128 | errno_t rc;
|
---|
[a35b458] | 129 |
|
---|
[9996ed5] | 130 | clear_screen();
|
---|
[ebe70f1] | 131 | moveto(10, 10);
|
---|
[1abcf1d] | 132 | fputs("Insert your name: ", stdout);
|
---|
[6eb2e96] | 133 | str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
|
---|
| 134 | "Player");
|
---|
[ebe70f1] | 135 | i = 6;
|
---|
| 136 | off = 6;
|
---|
[a35b458] | 137 |
|
---|
[1433ecda] | 138 | moveto(10, 28);
|
---|
| 139 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
|
---|
[ebe70f1] | 140 | "........................................");
|
---|
[a35b458] | 141 |
|
---|
[76d0981d] | 142 | while (true) {
|
---|
[79ae36dd] | 143 | console_flush(console);
|
---|
[87822ce] | 144 | rc = console_get_event(console, &ev);
|
---|
| 145 | if (rc != EOK)
|
---|
[3a180ad] | 146 | exit(1);
|
---|
[a35b458] | 147 |
|
---|
[07b7c48] | 148 | if (ev.type != CEV_KEY || ev.ev.key.type == KEY_RELEASE)
|
---|
[3a180ad] | 149 | continue;
|
---|
[a35b458] | 150 |
|
---|
[b987eb4] | 151 | if ((ev.ev.key.mods & (KM_CTRL | KM_ALT)) != 0)
|
---|
| 152 | continue;
|
---|
| 153 |
|
---|
[07b7c48] | 154 | kev = &ev.ev.key;
|
---|
[a35b458] | 155 |
|
---|
[07b7c48] | 156 | if (kev->key == KC_ENTER || kev->key == KC_NENTER)
|
---|
[3a180ad] | 157 | break;
|
---|
[a35b458] | 158 |
|
---|
[07b7c48] | 159 | if (kev->key == KC_BACKSPACE) {
|
---|
[3a180ad] | 160 | if (i > 0) {
|
---|
[28a5ebd] | 161 | char32_t uc;
|
---|
[a35b458] | 162 |
|
---|
[3a180ad] | 163 | --i;
|
---|
| 164 | while (off > 0) {
|
---|
| 165 | --off;
|
---|
| 166 | size_t otmp = off;
|
---|
| 167 | uc = str_decode(scores[NUMSPOTS - 1].hs_name,
|
---|
| 168 | &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
|
---|
| 169 | if (uc != U_SPECIAL)
|
---|
| 170 | break;
|
---|
| 171 | }
|
---|
[a35b458] | 172 |
|
---|
[3a180ad] | 173 | scores[NUMSPOTS - 1].hs_name[off] = '\0';
|
---|
| 174 | }
|
---|
[07b7c48] | 175 | } else if (kev->c != '\0') {
|
---|
[3a180ad] | 176 | if (i < (MAXLOGNAME - 1)) {
|
---|
[07b7c48] | 177 | if (chr_encode(kev->c, scores[NUMSPOTS - 1].hs_name,
|
---|
[3a180ad] | 178 | &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
|
---|
| 179 | ++i;
|
---|
| 180 | }
|
---|
| 181 | scores[NUMSPOTS - 1].hs_name[off] = '\0';
|
---|
| 182 | }
|
---|
[9996ed5] | 183 | }
|
---|
[a35b458] | 184 |
|
---|
[ebe70f1] | 185 | moveto(10, 28);
|
---|
| 186 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
|
---|
| 187 | "........................................");
|
---|
[9996ed5] | 188 | }
|
---|
[a35b458] | 189 |
|
---|
[ebe70f1] | 190 | scores[NUMSPOTS - 1].hs_score = score;
|
---|
[9996ed5] | 191 | scores[NUMSPOTS - 1].hs_level = level;
|
---|
[a35b458] | 192 |
|
---|
[ebe70f1] | 193 | i = NUMSPOTS - 1;
|
---|
[9996ed5] | 194 | while ((i > 0) && (scores[i - 1].hs_score < score))
|
---|
| 195 | i--;
|
---|
[a35b458] | 196 |
|
---|
[ebe70f1] | 197 | for (j = NUMSPOTS - 2; j > i; j--)
|
---|
[1433ecda] | 198 | copyhiscore(j, j - 1);
|
---|
[a35b458] | 199 |
|
---|
[ebe70f1] | 200 | copyhiscore(i, NUMSPOTS - 1);
|
---|
[9996ed5] | 201 | }
|
---|
| 202 |
|
---|
| 203 | void initscores(void)
|
---|
| 204 | {
|
---|
| 205 | int i;
|
---|
[ebe70f1] | 206 | for (i = 0; i < NUMSPOTS; i++) {
|
---|
[6eb2e96] | 207 | str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
|
---|
[ebe70f1] | 208 | scores[i].hs_score = (NUMSPOTS - i) * 200;
|
---|
| 209 | scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
|
---|
[9996ed5] | 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[b7fd2a0] | 213 | errno_t loadscores(void)
|
---|
[d4b9d28] | 214 | {
|
---|
| 215 | FILE *f;
|
---|
| 216 | size_t cnt;
|
---|
| 217 | int rc;
|
---|
| 218 |
|
---|
[63c1dd5] | 219 | f = fopen("/w/data/tetris.sco", "rb");
|
---|
[d4b9d28] | 220 | if (f == NULL)
|
---|
| 221 | return ENOENT;
|
---|
| 222 |
|
---|
| 223 | cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f);
|
---|
| 224 | rc = fclose(f);
|
---|
| 225 |
|
---|
| 226 | if (cnt != NUMSPOTS || rc != 0)
|
---|
| 227 | return EIO;
|
---|
| 228 |
|
---|
| 229 | return EOK;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | void savescores(void)
|
---|
| 233 | {
|
---|
| 234 | FILE *f;
|
---|
| 235 | size_t cnt;
|
---|
| 236 | int rc;
|
---|
| 237 |
|
---|
[63c1dd5] | 238 | f = fopen("/w/data/tetris.sco", "wb");
|
---|
[c354a08] | 239 | if (f == NULL) {
|
---|
| 240 | printf("Error creating table\n");
|
---|
| 241 | return;
|
---|
| 242 | }
|
---|
[a35b458] | 243 |
|
---|
[d4b9d28] | 244 | cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f);
|
---|
| 245 | rc = fclose(f);
|
---|
| 246 |
|
---|
| 247 | if (cnt != NUMSPOTS || rc != 0)
|
---|
| 248 | printf("Error saving score table\n");
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[b2951e2] | 251 | /** @}
|
---|
| 252 | */
|
---|