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