scores.c

Go to the documentation of this file.
00001 /*      $OpenBSD: scores.c,v 1.11 2006/04/20 03:25:36 ray Exp $ */
00002 /*      $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $   */
00003 
00004 /*-
00005  * Copyright (c) 1992, 1993
00006  *      The Regents of the University of California.  All rights reserved.
00007  *
00008  * This code is derived from software contributed to Berkeley by
00009  * Chris Torek and Darren F. Provine.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the University nor the names of its contributors
00020  *    may be used to endorse or promote products derived from this software
00021  *    without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  *
00035  *      @(#)scores.c    8.1 (Berkeley) 5/31/93
00036  */
00037 
00044 /*
00045  * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu)
00046  * modified 22 January 1992, to limit the number of entries any one
00047  * person has.
00048  *
00049  * Major whacks since then.
00050  */
00051 
00052 #include <errno.h>
00053 #include <stdio.h>
00054 #include <str.h>
00055 #include <io/console.h>
00056 #include <io/keycode.h>
00057 #include <vfs/vfs.h>
00058 #include <stdlib.h>
00059 #include <fcntl.h>
00060 #include <err.h>
00061 #include <time.h>
00062 
00063 #include "screen.h"
00064 #include "tetris.h"
00065 #include "scores.h"
00066 
00067 /*
00068  * Within this code, we can hang onto one extra "high score", leaving
00069  * room for our current score (whether or not it is high).
00070  *
00071  * We also sometimes keep tabs on the "highest" score on each level.
00072  * As long as the scores are kept sorted, this is simply the first one at
00073  * that level.
00074  */
00075 
00076 #define NUMSPOTS  (MAXHISCORES + 1)
00077 #define NLEVELS   (MAXLEVEL + 1)
00078 
00079 static struct highscore scores[NUMSPOTS];
00080 
00084 static void copyhiscore(int dest, int src)
00085 {
00086         str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
00087             scores[src].hs_name);
00088         scores[dest].hs_score = scores[src].hs_score;
00089         scores[dest].hs_level = scores[src].hs_level;
00090 }
00091 
00092 void showscores(int firstgame)
00093 {
00094         int i;
00095         
00096         clear_screen();
00097         moveto(10, 0);
00098         printf("\tRank \tLevel \tName\t                     points\n");
00099         printf("\t========================================================\n");
00100         
00101         for (i = 0; i < NUMSPOTS - 1; i++)
00102                 printf("\t%6d %6d %-16s %20d\n",
00103                     i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
00104         
00105         if (!firstgame) {
00106                 printf("\t========================================================\n");
00107                 printf("\t  Last %6d %-16s %20d\n",
00108                     scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
00109         }
00110         
00111         printf("\n\n\n\n\tPress any key to return to main menu.");
00112         getchar();
00113 }
00114 
00115 void insertscore(int score, int level)
00116 {
00117         int i;
00118         int j;
00119         size_t off;
00120         console_event_t ev;
00121         
00122         clear_screen();
00123         moveto(10, 10);
00124         puts("Insert your name: ");
00125         str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
00126             "Player");
00127         i = 6;
00128         off = 6;
00129         
00130         moveto(10 , 28);
00131         printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
00132             "........................................");
00133         
00134         while (1) {
00135                 fflush(stdout);
00136                 if (!console_get_event(fphone(stdin), &ev))
00137                         exit(1);
00138                 
00139                 if (ev.type == KEY_RELEASE)
00140                         continue;
00141                 
00142                 if (ev.key == KC_ENTER || ev.key == KC_NENTER)
00143                         break;
00144                 
00145                 if (ev.key == KC_BACKSPACE) {
00146                         if (i > 0) {
00147                                 wchar_t uc;
00148                                 
00149                                 --i;
00150                                 while (off > 0) {
00151                                         --off;
00152                                         size_t otmp = off;
00153                                         uc = str_decode(scores[NUMSPOTS - 1].hs_name,
00154                                             &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
00155                                         if (uc != U_SPECIAL)
00156                                                 break;
00157                                 }
00158                                 
00159                                 scores[NUMSPOTS - 1].hs_name[off] = '\0';
00160                         }
00161                 } else if (ev.c != '\0') {
00162                         if (i < (MAXLOGNAME - 1)) {
00163                                 if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name,
00164                                     &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
00165                                         ++i;
00166                                 }
00167                                 scores[NUMSPOTS - 1].hs_name[off] = '\0';
00168                         }
00169                 }
00170                 
00171                 moveto(10, 28);
00172                 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
00173                     "........................................");
00174         }
00175         
00176         scores[NUMSPOTS - 1].hs_score = score;
00177         scores[NUMSPOTS - 1].hs_level = level;
00178         
00179         i = NUMSPOTS - 1;
00180         while ((i > 0) && (scores[i - 1].hs_score < score))
00181                 i--;
00182         
00183         for (j = NUMSPOTS - 2; j > i; j--)
00184                 copyhiscore(j, j-1);
00185         
00186         copyhiscore(i, NUMSPOTS - 1);
00187 }
00188 
00189 void initscores(void)
00190 {
00191         int i;
00192         for (i = 0; i < NUMSPOTS; i++) {
00193                 str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
00194                 scores[i].hs_score = (NUMSPOTS - i) * 200;
00195                 scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
00196         }
00197 }
00198 
00199 int loadscores(void)
00200 {
00201         FILE *f;
00202         size_t cnt;
00203         int rc;
00204 
00205         f = fopen("/data/tetris.sco", "rb");
00206         if (f == NULL)
00207                 return ENOENT;
00208 
00209         cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f);
00210         rc = fclose(f);
00211 
00212         if (cnt != NUMSPOTS || rc != 0)
00213                 return EIO;
00214 
00215         return EOK;
00216 }
00217 
00218 void savescores(void)
00219 {
00220         FILE *f;
00221         size_t cnt;
00222         int rc;
00223 
00224         f = fopen("/data/tetris.sco", "wb");
00225         cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f);
00226         rc = fclose(f);
00227 
00228         if (cnt != NUMSPOTS || rc != 0)
00229                 printf("Error saving score table\n");
00230 }
00231 

Generated on Thu Jun 2 07:45:43 2011 for HelenOS/USB by  doxygen 1.4.7