]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/scores.c
1 /* $NetBSD: scores.c,v 1.6 1999/09/12 09:02:23 jsm Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)scores.c 8.1 (Berkeley) 5/31/93
42 * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu)
43 * modified 22 January 1992, to limit the number of entries any one
46 * Major whacks since then.
59 #include "pathnames.h"
65 * Within this code, we can hang onto one extra "high score", leaving
66 * room for our current score (whether or not it is high).
68 * We also sometimes keep tabs on the "highest" score on each level.
69 * As long as the scores are kept sorted, this is simply the first one at
72 #define NUMSPOTS (MAXHISCORES + 1)
73 #define NLEVELS (MAXLEVEL + 1)
78 static struct highscore scores
[NUMSPOTS
];
80 static int checkscores
__P((struct highscore
*, int));
81 static int cmpscores
__P((const void *, const void *));
82 static void getscores
__P((FILE **));
83 static void printem
__P((int, int, struct highscore
*, int, const char *));
84 static char *thisuser
__P((void));
87 * Read the score file. Can be called from savescore (before showscores)
88 * or showscores (if savescore will not be called). If the given pointer
89 * is not NULL, sets *fpp to an open file pointer that corresponds to a
90 * read/write score file that is locked with LOCK_EX. Otherwise, the
91 * file is locked with LOCK_SH for the read and closed before return.
93 * Note, we assume closing the stdio file releases the lock.
101 const char *mstr
, *human
;
105 mint
= O_RDWR
| O_CREAT
;
107 human
= "read/write";
116 mask
= umask(S_IWOTH
);
117 sd
= open(_PATH_SCOREFILE
, mint
, 0666);
125 (void)fprintf(stderr
, "tetris: cannot open %s for %s: %s\n",
126 _PATH_SCOREFILE
, human
, strerror(errno
));
129 if ((sf
= fdopen(sd
, mstr
)) == NULL
) {
130 (void)fprintf(stderr
, "tetris: cannot fdopen %s for %s: %s\n",
131 _PATH_SCOREFILE
, human
, strerror(errno
));
140 (void)fprintf(stderr
,
141 "tetris: warning: score file %s cannot be locked: %s\n",
142 _PATH_SCOREFILE
, strerror(errno
));
144 nscores
= fread(scores
, sizeof(scores
[0]), MAXHISCORES
, sf
);
146 (void)fprintf(stderr
, "tetris: error reading %s: %s\n",
147 _PATH_SCOREFILE
, strerror(errno
));
161 register struct highscore
*sp
;
172 * Allow at most one score per person per level -- see if we
173 * can replace an existing score, or (easiest) do nothing.
174 * Otherwise add new score at end (there is always room).
178 for (i
= 0, sp
= &scores
[0]; i
< nscores
; i
++, sp
++) {
179 if (sp
->hs_level
!= level
|| strcmp(sp
->hs_name
, me
) != 0)
181 if (score
> sp
->hs_score
) {
182 (void)printf("%s bettered %s %d score of %d!\n",
183 "\nYou", "your old level", level
,
184 sp
->hs_score
* sp
->hs_level
);
185 sp
->hs_score
= score
; /* new score */
186 sp
->hs_time
= now
; /* and time */
188 } else if (score
== sp
->hs_score
) {
189 (void)printf("%s tied %s %d high score.\n",
190 "\nYou", "your old level", level
);
191 sp
->hs_time
= now
; /* renew it */
192 change
= 1; /* gotta rewrite, sigh */
193 } /* else new score < old score: do nothing */
197 strcpy(sp
->hs_name
, me
);
198 sp
->hs_level
= level
;
199 sp
->hs_score
= score
;
207 * Sort & clean the scores, then rewrite.
209 nscores
= checkscores(scores
, nscores
);
211 if (fwrite(scores
, sizeof(*sp
), nscores
, sf
) != nscores
||
213 (void)fprintf(stderr
,
214 "tetris: error writing %s: %s -- %s\n",
215 _PATH_SCOREFILE
, strerror(errno
),
216 "high scores may be damaged");
218 (void)fclose(sf
); /* releases lock */
222 * Get login name, or if that fails, get something suitable.
223 * The result is always trimmed to fit in a score.
228 register const char *p
;
229 register struct passwd
*pw
;
231 static char u
[sizeof(scores
[0].hs_name
)];
236 if (p
== NULL
|| *p
== '\0') {
237 pw
= getpwuid(getuid());
252 * Score comparison function for qsort.
254 * If two scores are equal, the person who had the score first is
255 * listed first in the highscore file.
261 register const struct highscore
*a
, *b
;
266 l
= (long)b
->hs_level
* b
->hs_score
- (long)a
->hs_level
* a
->hs_score
;
271 if (a
->hs_time
< b
->hs_time
)
273 if (a
->hs_time
> b
->hs_time
)
279 * If we've added a score to the file, we need to check the file and ensure
280 * that this player has only a few entries. The number of entries is
281 * controlled by MAXSCORES, and is to ensure that the highscore file is not
282 * monopolised by just a few people. People who no longer have accounts are
283 * only allowed the highest score. Scores older than EXPIRATION seconds are
284 * removed, unless they are someone's personal best.
285 * Caveat: the highest score on each level is always kept.
289 register struct highscore
*hs
;
292 register struct highscore
*sp
;
293 register int i
, j
, k
, numnames
;
294 int levelfound
[NLEVELS
];
299 register struct peruser
*pu
;
302 * Sort so that highest totals come first.
304 * levelfound[i] becomes set when the first high score for that
305 * level is encountered. By definition this is the highest score.
307 qsort((void *)hs
, nscores
, sizeof(*hs
), cmpscores
);
308 for (i
= MINLEVEL
; i
< NLEVELS
; i
++)
311 for (i
= 0, sp
= hs
; i
< num
;) {
313 * This is O(n^2), but do you think we care?
315 for (j
= 0, pu
= count
; j
< numnames
; j
++, pu
++)
316 if (strcmp(sp
->hs_name
, pu
->name
) == 0)
320 * Add new user, set per-user count to 1.
322 pu
->name
= sp
->hs_name
;
327 * Two ways to keep this score:
328 * - Not too many (per user), still has acct, &
329 * score not dated; or
330 * - High score on this level.
332 if ((pu
->times
< MAXSCORES
&&
333 getpwnam(sp
->hs_name
) != NULL
&&
334 sp
->hs_time
+ EXPIRATION
>= now
) ||
335 levelfound
[sp
->hs_level
] == 0)
339 * Delete this score, do not count it,
340 * do not pass go, do not collect $200.
343 for (k
= i
; k
< num
; k
++)
348 levelfound
[sp
->hs_level
] = 1;
351 return (num
> MAXHISCORES
? MAXHISCORES
: num
);
355 * Show current scores. This must be called after savescore, if
356 * savescore is called at all, for two reasons:
357 * - Showscores munches the time field.
358 * - Even if that were not the case, a new score must be recorded
359 * before it can be shown anyway.
365 register struct highscore
*sp
;
366 register int i
, n
, c
;
368 int levelfound
[NLEVELS
];
371 getscores((FILE **)NULL
);
372 (void)printf("\n\t\t\t Tetris High Scores\n");
375 * If level == 0, the person has not played a game but just asked for
376 * the high scores; we do not need to check for printing in highlight
377 * mode. If SOstr is null, we can't do highlighting anyway.
379 me
= level
&& SOstr
? thisuser() : NULL
;
382 * Set times to 0 except for high score on each level.
384 for (i
= MINLEVEL
; i
< NLEVELS
; i
++)
386 for (i
= 0, sp
= scores
; i
< nscores
; i
++, sp
++) {
387 if (levelfound
[sp
->hs_level
])
391 levelfound
[sp
->hs_level
] = 1;
396 * Page each screenful of scores.
398 for (i
= 0, sp
= scores
; i
< nscores
; sp
+= n
) {
402 printem(level
, i
+ 1, sp
, n
, me
);
403 if ((i
+= n
) < nscores
) {
404 (void)printf("\nHit RETURN to continue.");
405 (void)fflush(stdout
);
406 while ((c
= getchar()) != '\n')
415 printem(level
, offset
, hs
, n
, me
)
417 register struct highscore
*hs
;
421 register struct highscore
*sp
;
422 int nrows
, row
, col
, item
, i
, highlight
;
424 #define TITLE "Rank Score Name (points/level)"
427 * This makes a nice two-column sort with headers, but it's a bit
430 printf("%s %s\n", TITLE
, n
> 1 ? TITLE
: "");
435 for (row
= 0; row
< nrows
; row
++) {
436 for (col
= 0; col
< 2; col
++) {
437 item
= col
* nrows
+ row
;
440 * Can only occur on trailing columns.
445 (void)printf(item
+ offset
< 10 ? " " : " ");
448 "%d%c %6d %-11s (%d on %d)",
449 item
+ offset
, sp
->hs_time
? '*' : ' ',
450 sp
->hs_score
* sp
->hs_level
,
451 sp
->hs_name
, sp
->hs_score
, sp
->hs_level
);
453 * Highlight if appropriate. This works because
454 * we only get one score per level.
457 sp
->hs_level
== level
&&
458 sp
->hs_score
== score
&&
459 strcmp(sp
->hs_name
, me
) == 0) {
463 (void)printf("%s", buf
);
469 /* fill in spaces so column 1 lines up */
471 for (i
= 40 - strlen(buf
); --i
>= 0;)