Annotation of early-roguelike/rogue5/scedit.c, Revision 1.1
1.1 ! rubenllo 1: /*
! 2: * score editor
! 3: *
! 4: * @(#)score.c 4.6 (Berkeley) 02/05/99
! 5: *
! 6: * Rogue: Exploring the Dungeons of Doom
! 7: * Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
! 8: * All rights reserved.
! 9: *
! 10: * See the file LICENSE.TXT for full copyright and licensing information.
! 11: */
! 12:
! 13: # include <curses.h>
! 14: # include <stdio.h>
! 15: # include <signal.h>
! 16: # include <ctype.h>
! 17:
! 18: #ifndef TRUE
! 19: # define TRUE 1
! 20: #endif
! 21: # define FALSE 0
! 22: # define RN (((seed = seed*11109+13849) >> 16) & 0xffff)
! 23:
! 24: # define MAXSTR 80
! 25:
! 26: # include "score.h"
! 27:
! 28: SCORE top_ten[10];
! 29:
! 30: char buf[BUFSIZ],
! 31: *reason[] = {
! 32: "killed",
! 33: "quit",
! 34: "A total winner",
! 35: "killed with amulet",
! 36: };
! 37:
! 38: int seed;
! 39: FILE *inf;
! 40:
! 41: struct passwd *getpwnam();
! 42: int do_comm();
! 43: int pr_score(SCORE *, int);
! 44:
! 45: int
! 46: main(int ac, char *av[])
! 47: {
! 48: char *scorefile;
! 49: FILE *outf;
! 50:
! 51: if (ac == 1)
! 52: scorefile = "rogue54.scr";
! 53: else
! 54: scorefile = av[1];
! 55: seed = md_random_seed();
! 56:
! 57: if ((inf = fopen(scorefile, "r+")) < 0) {
! 58: perror(scorefile);
! 59: exit(1);
! 60: }
! 61: s_encread((char *) top_ten, sizeof top_ten, inf);
! 62:
! 63: while (do_comm())
! 64: continue;
! 65:
! 66: exit(0);
! 67: }
! 68:
! 69: /*
! 70: * do_comm:
! 71: * Get and execute a command
! 72: */
! 73: int
! 74: do_comm(void)
! 75: {
! 76: char *sp;
! 77: SCORE *scp;
! 78: struct passwd *pp;
! 79: static FILE *outf = NULL;
! 80: static int written = TRUE;
! 81:
! 82: printf("\nCommand: ");
! 83: while (isspace(buf[0] = getchar()) || buf[0] == '\n')
! 84: continue;
! 85: (void) fget(s&buf[1], BUFSIZ, stdin);
! 86: buf[strlen(buf) - 1] = '\0';
! 87: switch (buf[0]) {
! 88: case 'w':
! 89: if (strncmp(buf, "write", strlen(buf)))
! 90: goto def;
! 91: lseek(inf, 0L, 0);
! 92: if (outf == NULL && (outf = fdopen(inf, "w")) == NULL) {
! 93: perror("fdopen");
! 94: exit(1);
! 95: }
! 96: fseek(outf, 0L, 0);
! 97: if (s_lock_sc())
! 98: {
! 99: void (*fp)(int);
! 100:
! 101: fp = signal(SIGINT, SIG_IGN);
! 102: s_encwrite((char *) top_ten, sizeof top_ten, outf);
! 103: s_unlock_sc();
! 104: signal(SIGINT, fp);
! 105: written = TRUE;
! 106: }
! 107: break;
! 108:
! 109: case 'a':
! 110: if (strncmp(buf, "add", strlen(buf)))
! 111: goto def;
! 112: add_score();
! 113: written = FALSE;
! 114: break;
! 115:
! 116: case 'd':
! 117: if (strncmp(buf, "delete", strlen(buf)))
! 118: goto def;
! 119: del_score();
! 120: written = FALSE;
! 121: break;
! 122:
! 123: case 'p':
! 124: if (strncmp(buf, "print", strlen(buf)))
! 125: goto def;
! 126: printf("\nTop Ten Rogueists:\nRank\tScore\tName\n");
! 127: for (scp = top_ten; scp < &top_ten[10]; scp++)
! 128: if (!pr_score(scp, TRUE))
! 129: break;
! 130: break;
! 131:
! 132: case 'q':
! 133: if (strncmp(buf, "quit", strlen(buf)))
! 134: goto def;
! 135: if (!written) {
! 136: printf("No write\n");
! 137: written = TRUE;
! 138: }
! 139: else
! 140: return FALSE;
! 141: break;
! 142:
! 143: default:
! 144: def:
! 145: printf("Unkown command: \"%s\"\n", buf);
! 146: }
! 147: return TRUE;
! 148: }
! 149:
! 150: /*
! 151: * add_score:
! 152: * Add a score to the score file
! 153: */
! 154:
! 155: void
! 156: add_score(void)
! 157: {
! 158: SCORE *scp;
! 159: uid_t id = 0;
! 160: int i;
! 161: SCORE new;
! 162:
! 163: printf("Name: ");
! 164: (void) fgets(new.sc_name, MAXSTR, stdin);
! 165: new.sc_name[strlen(new.sc_name) - 1] = '\0';
! 166: do {
! 167: printf("reason: ");
! 168: if ((new.sc_flags = getchar()) < '0' || new.sc_flags > '2') {
! 169: for (i = 0; i < 3; i++)
! 170: printf("%d: %s\n", i, reason[i]);
! 171: ungetc(new.sc_flags, stdin);
! 172: }
! 173: while (getchar() != '\n')
! 174: continue;
! 175: } while (new.sc_flags < '0' || new.sc_flags > '2');
! 176: new.sc_flags -= '0';
! 177: do {
! 178: printf("User Id: ");
! 179: (void) fgets(buf, BUFSIZ, stdin);
! 180: buf[strlen(buf) - 1] = '\0';
! 181: id = atoi(buf);
! 182: } while (id == -1);
! 183: new.sc_uid = id;
! 184: do {
! 185: printf("Monster: ");
! 186: if (!isalpha(new.sc_monster = getchar())) {
! 187: printf("type A-Za-z [%s]\n", unctrl(new.sc_monster));
! 188: ungetc(new.sc_monster, stdin);
! 189: }
! 190: while (getchar() != '\n')
! 191: continue;
! 192: } while (!isalpha(new.sc_monster));
! 193: printf("Score: ");
! 194: scanf("%d", &new.sc_score);
! 195: printf("level: ");
! 196: scanf("%d", &new.sc_level);
! 197: while (getchar() != '\n')
! 198: continue;
! 199: pr_score(&new, FALSE);
! 200: printf("Really add it? ");
! 201: if (getchar() != 'y')
! 202: return;
! 203: while (getchar() != '\n')
! 204: continue;
! 205: insert_score(&new);
! 206: }
! 207:
! 208: /*
! 209: * pr_score:
! 210: * Print out a score entry. Return FALSE if last entry.
! 211: */
! 212:
! 213: pr_score(SCORE *scp, int num)
! 214: {
! 215: if (scp->sc_score) {
! 216: if (num)
! 217: printf("%d", scp - top_ten + 1);
! 218: printf("\t%d\t%s: %s on level %d", scp->sc_score, scp->sc_name,
! 219: reason[scp->sc_flags], scp->sc_level);
! 220: if (scp->sc_flags == 0)
! 221: printf(" by %s", s_killname(scp->sc_monster, TRUE));
! 222:
! 223: printf(" (%s)", md_getrealname(scp->sc_uid));
! 224: putchar('\n');
! 225: }
! 226: return scp->sc_score;
! 227: }
! 228:
! 229: /*
! 230: * insert_score:
! 231: * Insert a score into the top ten list
! 232: */
! 233:
! 234: insert_score(SCORE *new)
! 235: {
! 236: SCORE *scp, *sc2;
! 237: int flags, amount;
! 238: uid_t uid;
! 239:
! 240: flags = new->sc_flags;
! 241: uid = new->sc_uid;
! 242: amount = new->sc_score;
! 243:
! 244: for (scp = top_ten; scp < &top_ten[10]; scp++)
! 245: if (amount > scp->sc_score)
! 246: break;
! 247: else if (flags != 2 && scp->sc_uid == uid && scp->sc_flags != 2)
! 248: scp = &top_ten[10];
! 249: if (scp < &top_ten[10]) {
! 250: if (flags != 2)
! 251: for (sc2 = scp; sc2 < &top_ten[10]; sc2++) {
! 252: if (sc2->sc_uid == uid && sc2->sc_flags != 2)
! 253: break;
! 254: }
! 255: else
! 256: sc2 = &top_ten[9];
! 257: while (sc2 > scp) {
! 258: *sc2 = sc2[-1];
! 259: sc2--;
! 260: }
! 261: *scp = *new;
! 262: sc2 = scp;
! 263: }
! 264: }
! 265:
! 266: /*
! 267: * del_score:
! 268: * Delete a score from the score list.
! 269: */
! 270: void
! 271: del_score(void)
! 272: {
! 273: SCORE *scp;
! 274: int i;
! 275: int num;
! 276:
! 277: for (;;) {
! 278: printf("Which score? ");
! 279: (void) fgets(buf, BUFSIZ, stdin);
! 280: if (buf[0] == '\n')
! 281: return;
! 282: sscanf(buf, "%d", &num);
! 283: if (num < 1 || num > 10)
! 284: printf("range is 1-10\n");
! 285: else
! 286: break;
! 287: }
! 288: num--;
! 289: for (scp = &top_ten[num]; scp < &top_ten[9]; scp++)
! 290: *scp = scp[1];
! 291: top_ten[9].sc_score = 0;
! 292: for (i = 0; i < MAXSTR; i++)
! 293: top_ten[9].sc_name[i] = RN;
! 294: top_ten[9].sc_flags = RN;
! 295: top_ten[9].sc_level = RN;
! 296: top_ten[9].sc_monster = RN;
! 297: top_ten[9].sc_uid = RN;
! 298: }
CVSweb