Annotation of early-roguelike/urogue/main.c, Revision 1.1
1.1 ! rubenllo 1: /*
! 2: main.c - setup code
! 3:
! 4: UltraRogue: The Ultimate Adventure in the Dungeons of Doom
! 5: Copyright (C) 1985, 1986, 1992, 1993, 1995 Herb Chong
! 6: All rights reserved.
! 7:
! 8: Based on "Advanced Rogue"
! 9: Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka
! 10: All rights reserved.
! 11:
! 12: Based on "Rogue: Exploring the Dungeons of Doom"
! 13: Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
! 14: All rights reserved.
! 15:
! 16: See the file LICENSE.TXT for full copyright and licensing information.
! 17: */
! 18:
! 19: #define _ALL_SOURCE
! 20:
! 21: #include <time.h>
! 22: #include <stdlib.h>
! 23: #include <string.h>
! 24: #include <signal.h>
! 25: #include <stdlib.h>
! 26: #include <errno.h>
! 27: #include "rogue.h"
! 28:
! 29: #ifdef HAVE_CONFIG_H
! 30: #include "config.h"
! 31: #endif
! 32:
! 33: FILE *fd_score = NULL;
! 34: FILE *file_log = NULL;
! 35:
! 36: /* Command line options */
! 37:
! 38: int prscore; /* Print scores */
! 39: int prversion; /* Print version info */
! 40:
! 41: int
! 42: main(int argc, char *argv[])
! 43: {
! 44: int x;
! 45: char *env;
! 46: time_t lowtime;
! 47: time_t now;
! 48: int rflag = 0;
! 49: char *nm;
! 50: float scale;
! 51:
! 52: for (x = 1; x < argc; x++)
! 53: {
! 54: if (argv[x][0] != '-')
! 55: break;
! 56:
! 57: switch (argv[x][1])
! 58: {
! 59: case 's':
! 60: prscore = TRUE;
! 61: break;
! 62:
! 63: case 'v':
! 64: prversion = TRUE;
! 65: break;
! 66:
! 67: case 'r':
! 68: rflag = TRUE;
! 69: break;
! 70:
! 71: #ifdef SAVEDIR
! 72: case 'n':
! 73: if (x + 1 < argc) {
! 74: use_savedir = TRUE;
! 75: x++;
! 76: // Set rogue's name to the next argument
! 77: strncpy(whoami, argv[x], 2*LINELEN);
! 78: whoami[2*LINELEN - 1] = '\0';
! 79: // And set up the savefile name
! 80: snprintf(file_name, 2*LINELEN, "%s/%d-%.80s.ursav", SAVEDIR,
! 81: md_getuid(), whoami);
! 82: }
! 83: break;
! 84: #endif
! 85:
! 86: default:
! 87: fprintf(stderr,"%s: Unknown option '%c'.\n",argv[0],argv[x][1]);
! 88: exit(1);
! 89: }
! 90: }
! 91:
! 92: if (!rflag)
! 93: {
! 94: argc -= (x - 1);
! 95: argv += (x - 1);
! 96: }
! 97:
! 98: /* Get default score file */
! 99:
! 100: #ifdef SCOREFILE
! 101: strncpy(score_file, SCOREFILE, 2*LINELEN);
! 102: score_file[2*LINELEN - 1] = '\0';
! 103: #else
! 104: strcpy(score_file, "urogue.scr");
! 105: #endif
! 106:
! 107: fd_score = fopen(score_file, "r+");
! 108:
! 109: if (fd_score == NULL)
! 110: fd_score = fopen(score_file, "a+");
! 111:
! 112: #ifdef LOGFILE
! 113: file_log = fopen(LOGFILE, "a+");
! 114: #endif
! 115:
! 116: if (!use_savedir)
! 117: md_normaluser();
! 118:
! 119: if ((env = getenv("OPTIONS")) != NULL)
! 120: parse_opts(env);
! 121:
! 122: if (!use_savedir) {
! 123: nm = getenv("USER");
! 124:
! 125: if (nm != NULL)
! 126: strcpy(whoami,nm);
! 127: else
! 128: strcpy(whoami,"anonymous");
! 129: }
! 130:
! 131: lowtime = time(&now);
! 132:
! 133: dnum = (wizard && getenv("SEED") != NULL ? atoi( getenv("SEED")) : (int)lowtime);
! 134:
! 135: ur_srandom(dnum);
! 136:
! 137: if (env == NULL || fruit[0] == '\0')
! 138: {
! 139: static const char *funfruit[] =
! 140: {
! 141: "candleberry", "caprifig", "dewberry", "elderberry",
! 142: "gooseberry", "guanabana", "hagberry", "ilama", "imbu",
! 143: "jaboticaba", "jujube", "litchi", "mombin", "pitanga",
! 144: "prickly pear", "rambutan", "sapodilla", "soursop",
! 145: "sweetsop", "whortleberry"
! 146: };
! 147:
! 148: strcpy(fruit, funfruit[rnd(sizeof(funfruit) / sizeof(funfruit[0]))]);
! 149: }
! 150:
! 151: /* put a copy of fruit in the right place */
! 152:
! 153: fd_data[1].mi_name = md_strdup(fruit);
! 154:
! 155: /* print scores */
! 156:
! 157: if (prscore)
! 158: {
! 159: waswizard = TRUE;
! 160: score(0L, 0, SCOREIT, 0);
! 161: exit(0);
! 162: }
! 163:
! 164: /* check for version option */
! 165:
! 166: if (prversion)
! 167: {
! 168: printf("UltraRogue Version %s.\n", release);
! 169: exit(0);
! 170: }
! 171:
! 172: if (wizard)
! 173: printf("Hello %s, welcome to dungeon #%d", whoami, dnum);
! 174: else
! 175: printf("Hello %s, just a moment while I dig the dungeon...", whoami);
! 176:
! 177: mem_debug(2);
! 178: mem_tracking(1);
! 179:
! 180: fflush(stdout);
! 181:
! 182: init_things(); /* Set up probabilities of things */
! 183: init_fd(); /* Set up food probabilities */
! 184: init_colors(); /* Set up colors of potions */
! 185: init_stones(); /* Set up stone settings of rings */
! 186: init_materials(); /* Set up materials of wands */
! 187: initscr(); /* Start up cursor package */
! 188: refresh();
! 189: init_names(); /* Set up names of scrolls */
! 190: cbreak();
! 191: crmode(); /* Cbreak mode */
! 192: noecho(); /* Echo off */
! 193: nonl();
! 194:
! 195: scale = (float) (LINES * COLS) / (80.0F * 25.0F); /* get food right for */
! 196: /* different screen sizes */
! 197:
! 198: food_left = (int) (food_left * scale);
! 199:
! 200: md_onsignal_autosave();
! 201:
! 202: /* Set up windows */
! 203:
! 204: cw = newwin(LINES, COLS, 0, 0);
! 205: mw = newwin(LINES, COLS, 0, 0);
! 206: hw = newwin(LINES, COLS, 0, 0);
! 207: keypad(cw, TRUE);
! 208:
! 209: if (use_savedir) {
! 210: if (!restore(file_name))
! 211: exit(1);
! 212: }
! 213: else if (argc == 2 && argv[1][0] != '\0' && !restore(argv[1]))
! 214: /* Note: restore returns on error only */
! 215: exit(1);
! 216:
! 217: waswizard = wizard; /* set wizard flags */
! 218:
! 219: init_player(); /* look up things and outfit pack */
! 220:
! 221: resurrect = pstats.s_const;
! 222: init_exp(); /* set first experience level change */
! 223: init_flags(); /* set initial flags */
! 224: wclear(hw);
! 225: wrefresh(hw);
! 226: new_level(POSTLEV,0); /* Draw current level */
! 227:
! 228: /* Start up daemons and fuses */
! 229:
! 230: start_daemon(DAEMON_DOCTOR, &player, AFTER);
! 231:
! 232: light_fuse(FUSE_SWANDER, 0, WANDERTIME, AFTER);
! 233:
! 234: start_daemon(DAEMON_STOMACH, 0, AFTER);
! 235: start_daemon(DAEMON_RUNNERS, 0, AFTER);
! 236:
! 237: char_type = player.t_ctype;
! 238: player.t_oldpos = hero;
! 239: oldrp = roomin(hero);
! 240: after = TRUE;
! 241:
! 242: signal(SIGINT, quit_handler);
! 243:
! 244: while(playing)
! 245: {
! 246: do_daemons(BEFORE);
! 247: do_fuses(BEFORE);
! 248:
! 249: command(); /* Command execution */
! 250:
! 251: if (after)
! 252: do_after_effects();
! 253: }
! 254:
! 255: fatal("");
! 256:
! 257: return(0);
! 258: }
! 259:
! 260: /*
! 261: fatal()
! 262: Exit the program, printing a message.
! 263: */
! 264:
! 265: void
! 266: fatal(char *s)
! 267: {
! 268: clear();
! 269: move(LINES - 2, 0);
! 270: printw("%s", s);
! 271: wrefresh(stdscr);
! 272: endwin();
! 273: printf("\n"); /* So the cursor doesn't stop at the end of the line */
! 274: exit(100);
! 275: }
! 276:
! 277: void
! 278: fatal_handler(int sig)
! 279: {
! 280: fatal("");
! 281: }
! 282:
! 283: /*
! 284: rnd()
! 285: Pick a very random number.
! 286: */
! 287:
! 288: unsigned char
! 289: ucrnd(unsigned char range)
! 290: {
! 291: return (unsigned char)(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
! 292: }
! 293:
! 294: short
! 295: srnd(short range)
! 296: {
! 297: return (short)(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
! 298: }
! 299:
! 300: int
! 301: rnd(int range)
! 302: {
! 303: return (range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
! 304: }
! 305:
! 306: unsigned long
! 307: ulrnd(unsigned long range)
! 308: {
! 309: return(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
! 310: }
! 311:
! 312: /*
! 313: roll()
! 314: roll a number of dice
! 315: */
! 316:
! 317: int
! 318: roll(int number, int sides)
! 319: {
! 320: int dtotal = 0;
! 321:
! 322: while (number--)
! 323: dtotal += rnd(sides) + 1;
! 324:
! 325: return(dtotal);
! 326: }
CVSweb