Subversion Repositories HelenOS

Rev

Rev 4565 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  38. /** @addtogroup tetris
  39.  * @{
  40.  */
  41. /** @file
  42.  */
  43.  
  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.  */
  51.  
  52. #include <errno.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <io/console.h>
  56. #include <io/keycode.h>
  57. #include <vfs/vfs.h>
  58. #include <stdlib.h>
  59. #include <fcntl.h>
  60. #include <err.h>
  61. #include <time.h>
  62.  
  63. #include "screen.h"
  64. #include "tetris.h"
  65. #include "scores.h"
  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.  
  76. #define NUMSPOTS  (MAXHISCORES + 1)
  77. #define NLEVELS   (MAXLEVEL + 1)
  78.  
  79. static struct highscore scores[NUMSPOTS];
  80.  
  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. }
  91.  
  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");
  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.    
  105.     if (!firstgame) {
  106.         printf("\t========================================================\n");
  107.         printf("\t  Last %6d %-16s %20d\n",
  108.             scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
  109.     }
  110.    
  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. {
  117.     int i;
  118.     int j;
  119.     size_t off;
  120.     console_event_t ev;
  121.    
  122.     clear_screen();
  123.     moveto(10, 10);
  124.     puts("Insert your name: ");
  125.     str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
  126.         "Player");
  127.     i = 6;
  128.     off = 6;
  129.    
  130.     moveto(10 , 28);
  131.     printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
  132.         "........................................");
  133.    
  134.     while (1) {
  135.         fflush(stdout);
  136.         if (!console_get_event(fphone(stdin), &ev))
  137.             exit(1);
  138.        
  139.         if (ev.type == KEY_RELEASE)
  140.             continue;
  141.        
  142.         if (ev.key == KC_ENTER || ev.key == KC_NENTER)
  143.             break;
  144.        
  145.         if (ev.key == KC_BACKSPACE) {
  146.             if (i > 0) {
  147.                 wchar_t uc;
  148.                
  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.                 }
  158.                
  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.             }
  169.         }
  170.        
  171.         moveto(10, 28);
  172.         printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
  173.             "........................................");
  174.     }
  175.    
  176.     scores[NUMSPOTS - 1].hs_score = score;
  177.     scores[NUMSPOTS - 1].hs_level = level;
  178.    
  179.     i = NUMSPOTS - 1;
  180.     while ((i > 0) && (scores[i - 1].hs_score < score))
  181.         i--;
  182.    
  183.     for (j = NUMSPOTS - 2; j > i; j--)
  184.         copyhiscore(j, j-1);
  185.    
  186.     copyhiscore(i, NUMSPOTS - 1);
  187. }
  188.  
  189. void initscores(void)
  190. {
  191.     int i;
  192.     for (i = 0; i < NUMSPOTS; i++) {
  193.         str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
  194.         scores[i].hs_score = (NUMSPOTS - i) * 200;
  195.         scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
  196.     }
  197. }
  198.  
  199. int loadscores(void)
  200. {
  201.     FILE *f;
  202.     size_t cnt;
  203.     int rc;
  204.  
  205.     f = fopen("/data/tetris.sco", "rb");
  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.  
  224.     f = fopen("/data/tetris.sco", "wb");
  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.  
  232. /** @}
  233.  */
  234.