Annotation of early-roguelike/rogue5/main.c, Revision 1.1
1.1 ! rubenllo 1: /*
! 2: * Rogue: Exploring the Dungeons of Doom
! 3: * Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
! 4: * All rights reserved.
! 5: *
! 6: * See the file LICENSE.TXT for full copyright and licensing information.
! 7: *
! 8: * @(#)main.c 4.22 (Berkeley) 02/05/99
! 9: */
! 10:
! 11: #include <stdlib.h>
! 12: #include <string.h>
! 13: #include <signal.h>
! 14: #include <time.h>
! 15: #include <curses.h>
! 16: #include "rogue.h"
! 17:
! 18: /*
! 19: * main:
! 20: * The main program, of course
! 21: */
! 22: int
! 23: main(int argc, char **argv)
! 24: {
! 25: char *env;
! 26:
! 27: md_init();
! 28:
! 29: #ifdef MASTER
! 30: /*
! 31: * Check to see if he is a wizard
! 32: */
! 33: if (argc >= 2 && argv[1][0] == '\0')
! 34: if (strcmp(PASSWD, md_crypt(md_getpass("wizard's password: "), "mT")) == 0)
! 35: {
! 36: wizard = TRUE;
! 37: player.t_flags |= SEEMONST;
! 38: argv++;
! 39: argc--;
! 40: }
! 41:
! 42: #endif
! 43:
! 44: /* Check to see if savefiles should be stored in the system location */
! 45: #ifdef SAVEDIR
! 46: if (argc >= 3 && !strcmp(argv[1], "-n"))
! 47: {
! 48: use_savedir = TRUE;
! 49: strncpy(whoami, argv[2], MAXSTR);
! 50: whoami[MAXSTR-1] = '\0';
! 51: snprintf(file_name, MAXSTR, "%s/%d-%.80s.r5sav", SAVEDIR,
! 52: md_getuid(), whoami);
! 53: }
! 54: #endif
! 55:
! 56: open_score();
! 57: open_log();
! 58:
! 59: /*
! 60: * get home and options from environment
! 61: */
! 62:
! 63: strcpy(home, md_gethomedir());
! 64:
! 65: if (strlen(home) > MAXSTR - strlen("rogue5.save") - 1)
! 66: *home = 0;
! 67:
! 68: if (!use_savedir)
! 69: {
! 70: md_normaluser();
! 71: strcpy(file_name, home);
! 72: strcat(file_name, "rogue5.save");
! 73: }
! 74:
! 75: if ((env = getenv("ROGUEOPTS")) != NULL)
! 76: parse_opts(env);
! 77: if (!use_savedir && (env == NULL || whoami[0] == '\0'))
! 78: strucpy(whoami, md_getusername(), strlen(md_getusername()));
! 79: if (getenv("SEED") != NULL)
! 80: {
! 81: dnum = atoi(getenv("SEED"));
! 82: noscore = 1;
! 83: }
! 84: else
! 85: dnum = md_random_seed();
! 86: seed = dnum;
! 87:
! 88: /*
! 89: * check for print-score option
! 90: */
! 91:
! 92: if (argc == 2)
! 93: {
! 94: if (strcmp(argv[1], "-s") == 0)
! 95: {
! 96: noscore = TRUE;
! 97: score(0, -1, 0);
! 98: exit(0);
! 99: }
! 100: else if (strcmp(argv[1], "-d") == 0)
! 101: {
! 102: dnum = rnd(100); /* throw away some rnd()s to break patterns */
! 103: while (--dnum)
! 104: rnd(100);
! 105: purse = rnd(100) + 1;
! 106: level = rnd(100) + 1;
! 107: initscr();
! 108: getltchars();
! 109: death(death_monst());
! 110: exit(0);
! 111: }
! 112: }
! 113:
! 114: init_check(); /* check for legal startup */
! 115: if (use_savedir)
! 116: {
! 117: /* If there is a saved game, restore() will not return. If it
! 118: * returns 1, there isn't a game, so start one. If 0, there was
! 119: * an error. */
! 120: if (!restore(file_name))
! 121: my_exit(1);
! 122: }
! 123: else if (argc == 2)
! 124: if (!restore(argv[1])) /* Note: restore will never return */
! 125: my_exit(1);
! 126: #ifdef MASTER
! 127: if (wizard)
! 128: printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
! 129: else
! 130: #endif
! 131: printf("Hello %s, just a moment while I dig the dungeon...\n", whoami);
! 132: fflush(stdout);
! 133:
! 134: initscr(); /* Start up cursor package */
! 135: init_probs(); /* Set up prob tables for objects */
! 136: init_player(); /* Set up initial player stats */
! 137: init_names(); /* Set up names of scrolls */
! 138: init_colors(); /* Set up colors of potions */
! 139: init_stones(); /* Set up stone settings of rings */
! 140: init_materials(); /* Set up materials of wands */
! 141: setup();
! 142:
! 143: /*
! 144: * The screen must be at least NUMLINES x NUMCOLS
! 145: */
! 146: if (LINES < NUMLINES || COLS < NUMCOLS)
! 147: {
! 148: printf("\nSorry, the screen must be at least %dx%d\n", NUMLINES, NUMCOLS);
! 149: endwin();
! 150: my_exit(1);
! 151: }
! 152:
! 153: /*
! 154: * Set up windows
! 155: */
! 156: hw = newwin(LINES, COLS, 0, 0);
! 157: idlok(stdscr, TRUE);
! 158: idlok(hw, TRUE);
! 159: #ifdef MASTER
! 160: noscore = wizard;
! 161: #endif
! 162: new_level(); /* Draw current level */
! 163: /*
! 164: * Start up daemons and fuses
! 165: */
! 166: start_daemon(runners, 0, AFTER);
! 167: start_daemon(doctor, 0, AFTER);
! 168: fuse(swander, 0, WANDERTIME, AFTER);
! 169: start_daemon(stomach, 0, AFTER);
! 170: playit();
! 171: return(0);
! 172: }
! 173:
! 174: /*
! 175: * endit:
! 176: * Exit the program abnormally.
! 177: */
! 178:
! 179: void
! 180: endit(int sig)
! 181: {
! 182: NOOP(sig);
! 183: fatal("Okay, bye bye!\n");
! 184: }
! 185:
! 186: /*
! 187: * fatal:
! 188: * Exit the program, printing a message.
! 189: */
! 190:
! 191: void
! 192: fatal(const char *s)
! 193: {
! 194: mvaddstr(LINES - 2, 0, s);
! 195: refresh();
! 196: endwin();
! 197: my_exit(0);
! 198: }
! 199:
! 200: /*
! 201: * rnd:
! 202: * Pick a very random number.
! 203: */
! 204: int
! 205: rnd(int range)
! 206: {
! 207: return range == 0 ? 0 : abs((int) RN) % range;
! 208: }
! 209:
! 210: /*
! 211: * roll:
! 212: * Roll a number of dice
! 213: */
! 214: int
! 215: roll(int number, int sides)
! 216: {
! 217: int dtotal = 0;
! 218:
! 219: while (number--)
! 220: dtotal += rnd(sides)+1;
! 221: return dtotal;
! 222: }
! 223:
! 224: /*
! 225: * tstp:
! 226: * Handle stop and start signals
! 227: */
! 228:
! 229: void
! 230: tstp(int ignored)
! 231: {
! 232: int y, x;
! 233: int oy, ox;
! 234:
! 235: NOOP(ignored);
! 236:
! 237: /*
! 238: * leave nicely
! 239: */
! 240: getyx(curscr, oy, ox);
! 241: mvcur(0, COLS - 1, LINES - 1, 0);
! 242: endwin();
! 243: resetltchars();
! 244: fflush(stdout);
! 245: md_tstpsignal();
! 246:
! 247: /*
! 248: * start back up again
! 249: */
! 250: md_tstpresume();
! 251: raw();
! 252: noecho();
! 253: nonl();
! 254: keypad(stdscr,1);
! 255: playltchars();
! 256: clearok(curscr, TRUE);
! 257: wrefresh(curscr);
! 258: getyx(curscr, y, x);
! 259: mvcur(y, x, oy, ox);
! 260: fflush(stdout);
! 261: curscr->_cury = oy;
! 262: curscr->_curx = ox;
! 263: }
! 264:
! 265: /*
! 266: * playit:
! 267: * The main loop of the program. Loop until the game is over,
! 268: * refreshing things and looking at the proper times.
! 269: */
! 270:
! 271: void
! 272: playit(void)
! 273: {
! 274: char *opts;
! 275:
! 276: /*
! 277: * set up defaults for slow terminals
! 278: */
! 279:
! 280: if (baudrate() <= 1200)
! 281: {
! 282: terse = TRUE;
! 283: jump = TRUE;
! 284: see_floor = FALSE;
! 285: }
! 286:
! 287: if (md_hasclreol())
! 288: inv_type = INV_CLEAR;
! 289:
! 290: /*
! 291: * parse environment declaration of options
! 292: */
! 293: if ((opts = getenv("ROGUEOPTS")) != NULL)
! 294: parse_opts(opts);
! 295:
! 296:
! 297: oldpos = hero;
! 298: oldrp = roomin(&hero);
! 299: while (playing)
! 300: command(); /* Command execution */
! 301: endit(0);
! 302: }
! 303:
! 304: /*
! 305: * quit:
! 306: * Have player make certain, then exit.
! 307: */
! 308:
! 309: void
! 310: quit(int sig)
! 311: {
! 312: int oy, ox;
! 313:
! 314: NOOP(sig);
! 315:
! 316: /*
! 317: * Reset the signal in case we got here via an interrupt
! 318: */
! 319: if (!q_comm)
! 320: mpos = 0;
! 321: getyx(curscr, oy, ox);
! 322: msg("really quit?");
! 323: if (readchar() == 'y')
! 324: {
! 325: signal(SIGINT, leave);
! 326: clear();
! 327: mvprintw(LINES - 2, 0, "You quit with %d gold pieces", purse);
! 328: move(LINES - 1, 0);
! 329: refresh();
! 330: writelog(purse, 1, 0);
! 331: score(purse, 1, 0);
! 332: printf("[Press return to exit]\n");
! 333: fflush(NULL);
! 334: getchar();
! 335: my_exit(0);
! 336: }
! 337: else
! 338: {
! 339: move(0, 0);
! 340: clrtoeol();
! 341: status();
! 342: move(oy, ox);
! 343: refresh();
! 344: mpos = 0;
! 345: count = 0;
! 346: to_death = FALSE;
! 347: }
! 348: }
! 349:
! 350: /*
! 351: * leave:
! 352: * Leave quickly, but curteously
! 353: */
! 354:
! 355: void
! 356: leave(int sig)
! 357: {
! 358: static char buf[BUFSIZ];
! 359:
! 360: NOOP(sig);
! 361:
! 362: setbuf(stdout, buf); /* throw away pending output */
! 363:
! 364: if (!isendwin())
! 365: {
! 366: mvcur(0, COLS - 1, LINES - 1, 0);
! 367: endwin();
! 368: }
! 369:
! 370: putchar('\n');
! 371: my_exit(0);
! 372: }
! 373:
! 374: /*
! 375: * shell:
! 376: * Let them escape for a while
! 377: */
! 378:
! 379: void
! 380: shell(void)
! 381: {
! 382: /*
! 383: * Set the terminal back to original mode
! 384: */
! 385: move(LINES-1, 0);
! 386: refresh();
! 387: endwin();
! 388: resetltchars();
! 389: putchar('\n');
! 390: in_shell = TRUE;
! 391: after = FALSE;
! 392: fflush(stdout);
! 393: /*
! 394: * Fork and do a shell
! 395: */
! 396: md_shellescape();
! 397:
! 398: noecho();
! 399: raw();
! 400: nonl();
! 401: keypad(stdscr,1);
! 402: playltchars();
! 403: in_shell = FALSE;
! 404: clearok(stdscr, TRUE);
! 405: }
! 406:
! 407: /*
! 408: * my_exit:
! 409: * Leave the process properly
! 410: */
! 411:
! 412: void
! 413: my_exit(int st)
! 414: {
! 415: resetltchars();
! 416: exit(st);
! 417: }
! 418:
CVSweb