Subversion Repositories HelenOS

Rev

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

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