[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>
|
---|
[6a7f6b8] | 61 | #include <stdio.h>
|
---|
[19f857a] | 62 | #include <str.h>
|
---|
[0c25c10] | 63 | #include <io/console.h>
|
---|
| 64 | #include <io/keycode.h>
|
---|
| 65 | #include <vfs/vfs.h>
|
---|
[3a180ad] | 66 | #include <stdlib.h>
|
---|
[ebe70f1] | 67 | #include <fcntl.h>
|
---|
| 68 | #include <err.h>
|
---|
| 69 | #include <time.h>
|
---|
[e9a3c52] | 70 | #include "screen.h"
|
---|
| 71 | #include "tetris.h"
|
---|
[c0e674a] | 72 | #include "scores.h"
|
---|
[e9a3c52] | 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 |
|
---|
[ebe70f1] | 83 | #define NUMSPOTS (MAXHISCORES + 1)
|
---|
| 84 | #define NLEVELS (MAXLEVEL + 1)
|
---|
| 85 |
|
---|
[9996ed5] | 86 | static struct highscore scores[NUMSPOTS];
|
---|
[e9a3c52] | 87 |
|
---|
[ebe70f1] | 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 | }
|
---|
[e9a3c52] | 98 |
|
---|
[9996ed5] | 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");
|
---|
[ebe70f1] | 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 |
|
---|
[9996ed5] | 112 | if (!firstgame) {
|
---|
| 113 | printf("\t========================================================\n");
|
---|
[ebe70f1] | 114 | printf("\t Last %6d %-16s %20d\n",
|
---|
| 115 | scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
|
---|
[9996ed5] | 116 | }
|
---|
[ebe70f1] | 117 |
|
---|
[9996ed5] | 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 | {
|
---|
[ebe70f1] | 124 | int i;
|
---|
| 125 | int j;
|
---|
[3a180ad] | 126 | size_t off;
|
---|
[79ae36dd] | 127 | kbd_event_t ev;
|
---|
[9996ed5] | 128 |
|
---|
| 129 | clear_screen();
|
---|
[ebe70f1] | 130 | moveto(10, 10);
|
---|
[9996ed5] | 131 | puts("Insert your name: ");
|
---|
[6eb2e96] | 132 | str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
|
---|
| 133 | "Player");
|
---|
[ebe70f1] | 134 | i = 6;
|
---|
| 135 | off = 6;
|
---|
| 136 |
|
---|
[9996ed5] | 137 | moveto(10 , 28);
|
---|
[ebe70f1] | 138 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
|
---|
| 139 | "........................................");
|
---|
| 140 |
|
---|
[3a180ad] | 141 | while (1) {
|
---|
[79ae36dd] | 142 | console_flush(console);
|
---|
| 143 | if (!console_get_kbd_event(console, &ev))
|
---|
[3a180ad] | 144 | exit(1);
|
---|
[ebe70f1] | 145 |
|
---|
[0c25c10] | 146 | if (ev.type == KEY_RELEASE)
|
---|
[3a180ad] | 147 | continue;
|
---|
[ebe70f1] | 148 |
|
---|
[3a180ad] | 149 | if (ev.key == KC_ENTER || ev.key == KC_NENTER)
|
---|
| 150 | break;
|
---|
[ebe70f1] | 151 |
|
---|
[3a180ad] | 152 | if (ev.key == KC_BACKSPACE) {
|
---|
| 153 | if (i > 0) {
|
---|
| 154 | wchar_t uc;
|
---|
[ebe70f1] | 155 |
|
---|
[3a180ad] | 156 | --i;
|
---|
| 157 | while (off > 0) {
|
---|
| 158 | --off;
|
---|
| 159 | size_t otmp = off;
|
---|
| 160 | uc = str_decode(scores[NUMSPOTS - 1].hs_name,
|
---|
| 161 | &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
|
---|
| 162 | if (uc != U_SPECIAL)
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
[ebe70f1] | 165 |
|
---|
[3a180ad] | 166 | scores[NUMSPOTS - 1].hs_name[off] = '\0';
|
---|
| 167 | }
|
---|
| 168 | } else if (ev.c != '\0') {
|
---|
| 169 | if (i < (MAXLOGNAME - 1)) {
|
---|
| 170 | if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name,
|
---|
| 171 | &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
|
---|
| 172 | ++i;
|
---|
| 173 | }
|
---|
| 174 | scores[NUMSPOTS - 1].hs_name[off] = '\0';
|
---|
| 175 | }
|
---|
[9996ed5] | 176 | }
|
---|
| 177 |
|
---|
[ebe70f1] | 178 | moveto(10, 28);
|
---|
| 179 | printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
|
---|
| 180 | "........................................");
|
---|
[9996ed5] | 181 | }
|
---|
| 182 |
|
---|
[ebe70f1] | 183 | scores[NUMSPOTS - 1].hs_score = score;
|
---|
[9996ed5] | 184 | scores[NUMSPOTS - 1].hs_level = level;
|
---|
| 185 |
|
---|
[ebe70f1] | 186 | i = NUMSPOTS - 1;
|
---|
[9996ed5] | 187 | while ((i > 0) && (scores[i - 1].hs_score < score))
|
---|
| 188 | i--;
|
---|
[ebe70f1] | 189 |
|
---|
| 190 | for (j = NUMSPOTS - 2; j > i; j--)
|
---|
| 191 | copyhiscore(j, j-1);
|
---|
| 192 |
|
---|
| 193 | copyhiscore(i, NUMSPOTS - 1);
|
---|
[9996ed5] | 194 | }
|
---|
| 195 |
|
---|
| 196 | void initscores(void)
|
---|
| 197 | {
|
---|
| 198 | int i;
|
---|
[ebe70f1] | 199 | for (i = 0; i < NUMSPOTS; i++) {
|
---|
[6eb2e96] | 200 | str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
|
---|
[ebe70f1] | 201 | scores[i].hs_score = (NUMSPOTS - i) * 200;
|
---|
| 202 | scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
|
---|
[9996ed5] | 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[d4b9d28] | 206 | int loadscores(void)
|
---|
| 207 | {
|
---|
| 208 | FILE *f;
|
---|
| 209 | size_t cnt;
|
---|
| 210 | int rc;
|
---|
| 211 |
|
---|
[00fe6bb] | 212 | f = fopen("/data/tetris.sco", "rb");
|
---|
[d4b9d28] | 213 | if (f == NULL)
|
---|
| 214 | return ENOENT;
|
---|
| 215 |
|
---|
| 216 | cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f);
|
---|
| 217 | rc = fclose(f);
|
---|
| 218 |
|
---|
| 219 | if (cnt != NUMSPOTS || rc != 0)
|
---|
| 220 | return EIO;
|
---|
| 221 |
|
---|
| 222 | return EOK;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void savescores(void)
|
---|
| 226 | {
|
---|
| 227 | FILE *f;
|
---|
| 228 | size_t cnt;
|
---|
| 229 | int rc;
|
---|
| 230 |
|
---|
[00fe6bb] | 231 | f = fopen("/data/tetris.sco", "wb");
|
---|
[d4b9d28] | 232 | cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f);
|
---|
| 233 | rc = fclose(f);
|
---|
| 234 |
|
---|
| 235 | if (cnt != NUMSPOTS || rc != 0)
|
---|
| 236 | printf("Error saving score table\n");
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[b2951e2] | 239 | /** @}
|
---|
| 240 | */
|
---|