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