Mercurial > hg > early-roguelike
comparison xrogue/main.c @ 142:6b5fbd7c3ece
Merge arogue7 and xrogue trees.
| author | John "Elwin" Edwards |
|---|---|
| date | Tue, 12 May 2015 21:39:39 -0400 |
| parents | 0d151573bdb0 |
| children | 7faf4568c295 |
comparison
equal
deleted
inserted
replaced
| 132:66b0263af424 | 142:6b5fbd7c3ece |
|---|---|
| 1 /* | |
| 2 main.c - setup code | |
| 3 | |
| 4 XRogue: Expeditions into the Dungeons of Doom | |
| 5 Copyright (C) 1991 Robert Pietkivitch | |
| 6 All rights reserved. | |
| 7 | |
| 8 Based on "Advanced Rogue" | |
| 9 Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T | |
| 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 #include <stdlib.h> | |
| 20 #include <string.h> | |
| 21 #include <curses.h> | |
| 22 #include <signal.h> | |
| 23 #include <time.h> | |
| 24 | |
| 25 #include "mach_dep.h" | |
| 26 #include "network.h" | |
| 27 #include "rogue.h" | |
| 28 | |
| 29 #define SCOREFILE "xrogue.scr" | |
| 30 #define SAVEDIR "." | |
| 31 | |
| 32 main(argc, argv, envp) | |
| 33 char **argv; | |
| 34 char **envp; | |
| 35 { | |
| 36 register char *env; | |
| 37 time_t now; | |
| 38 | |
| 39 md_init(); | |
| 40 | |
| 41 /* | |
| 42 * get home and options from environment | |
| 43 */ | |
| 44 | |
| 45 strncpy(home, md_gethomedir(), LINELEN); | |
| 46 | |
| 47 /* Get default save file */ | |
| 48 strcpy(file_name, home); | |
| 49 strcat(file_name, "xrogue.sav"); | |
| 50 | |
| 51 /* Get default score file */ | |
| 52 #ifdef SCOREFILE | |
| 53 strncpy(score_file, SCOREFILE, LINELEN); | |
| 54 score_file[LINELEN-1] = '\0'; | |
| 55 #else | |
| 56 strcpy(score_file, md_getroguedir()); | |
| 57 | |
| 58 if (*score_file) | |
| 59 strcat(score_file,"/"); | |
| 60 | |
| 61 strcat(score_file, "xrogue.scr"); | |
| 62 #endif | |
| 63 | |
| 64 #ifdef SAVEDIR | |
| 65 /* Check for common save location */ | |
| 66 if (argc >= 3 && strcmp(argv[1], "-n") == 0) | |
| 67 { | |
| 68 strncpy(whoami, argv[2], 79); | |
| 69 whoami[79] = '\0'; | |
| 70 use_savedir = TRUE; | |
| 71 if (LINELEN <= snprintf(file_name, LINELEN, "%s/%d-%s.xrsav", SAVEDIR, | |
| 72 md_getuid(), whoami)) | |
| 73 { | |
| 74 strcpy(file_name, "xrogue.sav"); | |
| 75 use_savedir = FALSE; | |
| 76 } | |
| 77 } | |
| 78 #endif | |
| 79 | |
| 80 if ((env = getenv("ROGUEOPTS")) != NULL) | |
| 81 parse_opts(env); | |
| 82 | |
| 83 if (whoami[0] == '\0') | |
| 84 strucpy(whoami, md_getusername(), strlen(md_getusername())); | |
| 85 | |
| 86 /* | |
| 87 * check for print-score option | |
| 88 */ | |
| 89 if (argc == 2 && strcmp(argv[1], "-s") == 0) | |
| 90 { | |
| 91 waswizard = TRUE; | |
| 92 score((long)0, SCOREIT, (short)0); | |
| 93 exit_game(0); | |
| 94 } | |
| 95 | |
| 96 /* | |
| 97 * Check for a network update | |
| 98 */ | |
| 99 if (argc == 2 && strcmp(argv[1], "-u") == 0) { | |
| 100 int errcheck, errors = 0; | |
| 101 unsigned long amount; | |
| 102 short monster; | |
| 103 | |
| 104 /* Read in the amount and monster values to pass to score */ | |
| 105 amount = netread(&errcheck, sizeof(unsigned long), stdin); | |
| 106 if (errcheck) errors++; | |
| 107 | |
| 108 monster = (short) netread(&errcheck, sizeof(short), stdin); | |
| 109 if (errcheck) errors++; | |
| 110 | |
| 111 /* Now do the update if there were no errors */ | |
| 112 if (errors) exit_game(0); | |
| 113 else { | |
| 114 score((long)amount, UPDATE, (short)monster); | |
| 115 exit_game(0); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 /* | |
| 120 * Check to see if he is a wizard | |
| 121 */ | |
| 122 #ifdef WIZARD | |
| 123 if (argc >= 2 && argv[1][0] == '\0') | |
| 124 if (strcmp(PASSWD, xcrypt(md_getpass("Wizard's password: "), "mT")) == 0) | |
| 125 { | |
| 126 wizard = TRUE; | |
| 127 argv++; | |
| 128 argc--; | |
| 129 } | |
| 130 #endif | |
| 131 | |
| 132 if (betaover()) | |
| 133 { | |
| 134 printf("Sorry, %s, but the test period of this prerelease version\n",whoami); | |
| 135 printf("of xrogue is over. Please acquire a new version. Sorry.\n"); | |
| 136 exit_game(0); | |
| 137 } | |
| 138 | |
| 139 if (!wizard && !author() && !playtime()) { | |
| 140 printf("Sorry, %s, but you can't play during working hours.\n", whoami); | |
| 141 printf("Try again later.\n"); | |
| 142 exit_game(0); | |
| 143 } | |
| 144 if (!wizard && !author() && too_much()) { | |
| 145 printf("Sorry, %s, but the system is too loaded now.\n", whoami); | |
| 146 printf("Try again later.\n"); | |
| 147 exit_game(0); | |
| 148 } | |
| 149 | |
| 150 if (use_savedir) | |
| 151 { | |
| 152 /* restore() will return TRUE if a new game should be started. */ | |
| 153 if (!restore(file_name, envp)) | |
| 154 exit_game(0); | |
| 155 } | |
| 156 if (argc == 2) | |
| 157 if (!restore(argv[1], envp)) /* Note: restore will never return */ | |
| 158 exit_game(0); | |
| 159 | |
| 160 if (wizard && getenv("SEED") != NULL) { | |
| 161 seed = atoi(getenv("SEED")); | |
| 162 } | |
| 163 else { | |
| 164 seed = (int) time(&now) + getpid(); | |
| 165 } | |
| 166 if (wizard) | |
| 167 printf("Hello %s, welcome to dungeon #%d", whoami, seed); | |
| 168 else | |
| 169 printf("Hello %s, just a moment while I dig the dungeon...", whoami); | |
| 170 fflush(stdout); | |
| 171 | |
| 172 md_srand(seed); | |
| 173 | |
| 174 init_things(); /* Set up probabilities of things */ | |
| 175 init_colors(); /* Set up colors of potions */ | |
| 176 init_stones(); /* Set up stone settings of rings */ | |
| 177 init_materials(); /* Set up materials of wands */ | |
| 178 init_names(); /* Set up names of scrolls */ | |
| 179 init_misc(); /* Set up miscellaneous magic */ | |
| 180 init_foods(); /* set up the food table */ | |
| 181 | |
| 182 initscr(); /* Start up cursor package */ | |
| 183 | |
| 184 typeahead(-1); /* turn off 3.2/4.0 curses feature */ | |
| 185 | |
| 186 if (COLS < MINCOLS) | |
| 187 { | |
| 188 printf("\n\nSorry, %s, but your terminal window has too few columns.\n", whoami); | |
| 189 printf("Your terminal has %d columns, needs 70.\n",COLS); | |
| 190 byebye(0); | |
| 191 } | |
| 192 if (LINES < MINLINES) | |
| 193 { | |
| 194 printf("\n\nSorry, %s, but your terminal window has too few lines.\n", whoami); | |
| 195 printf("Your terminal has %d lines, needs 22.\n",LINES); | |
| 196 byebye(0); | |
| 197 } | |
| 198 | |
| 199 cols = COLS; | |
| 200 lines = LINES; | |
| 201 | |
| 202 if ( cols % 2 != 0) cols -=1; /* must be even for maze code */ | |
| 203 if (lines % 2 != 0) lines -=1; /* must be even for maze code */ | |
| 204 | |
| 205 /* | |
| 206 * Now that we have cols and lines, we can update our window | |
| 207 * structure for non-hardware windows. | |
| 208 */ | |
| 209 setup(); | |
| 210 /* | |
| 211 * Set up windows | |
| 212 */ | |
| 213 cw = newwin(lines, cols, 0, 0); | |
| 214 mw = newwin(lines, cols, 0, 0); | |
| 215 hw = newwin(lines, cols, 0, 0); | |
| 216 msgw = newwin(4, cols, 0, 0); | |
| 217 if (cw == NULL || hw == NULL || mw == NULL || msgw == NULL) { | |
| 218 exit_game(EXIT_CLS | EXIT_ENDWIN); | |
| 219 } | |
| 220 | |
| 221 keypad(cw, TRUE); | |
| 222 keypad(hw, TRUE); | |
| 223 | |
| 224 init_player(); /* Roll up the rogue */ | |
| 225 waswizard = wizard; | |
| 226 | |
| 227 draw(cw); | |
| 228 /* A super wizard doesn't have to get equipped */ | |
| 229 /* Check if "" option is TRUE and get environment flag */ | |
| 230 if (wizard && strcmp(getenv("SUPER"),"YES") == 0 || | |
| 231 def_attr == TRUE) { | |
| 232 level = 1; | |
| 233 new_level(NORMLEV); | |
| 234 } | |
| 235 else | |
| 236 new_level(STARTLEV); /* Draw current level */ | |
| 237 | |
| 238 /* | |
| 239 * Start up daemons and fuses | |
| 240 */ | |
| 241 daemon(doctor, &player, AFTER); | |
| 242 fuse(swander, (VOID *)NULL, WANDERTIME, AFTER); | |
| 243 /* Give characters their innate abilities */ | |
| 244 if (player.t_ctype == C_MAGICIAN || player.t_ctype == C_RANGER) | |
| 245 fuse(spell_recovery, (VOID *)NULL, SPELLTIME, AFTER); | |
| 246 if (player.t_ctype == C_DRUID || player.t_ctype == C_MONK) | |
| 247 fuse(chant_recovery, (VOID *)NULL, SPELLTIME, AFTER); | |
| 248 if (player.t_ctype == C_CLERIC || player.t_ctype == C_PALADIN) | |
| 249 fuse(prayer_recovery, (VOID *)NULL, SPELLTIME, AFTER); | |
| 250 daemon(stomach, (VOID *)NULL, AFTER); | |
| 251 if (player.t_ctype == C_THIEF || | |
| 252 player.t_ctype == C_ASSASSIN || | |
| 253 player.t_ctype == C_MONK) | |
| 254 daemon(trap_look, (VOID *)NULL, AFTER); | |
| 255 | |
| 256 /* Does this character have any special knowledge? */ | |
| 257 switch (player.t_ctype) { | |
| 258 case C_ASSASSIN: | |
| 259 /* Assassins automatically recognize poison */ | |
| 260 p_know[P_POISON] = TRUE; | |
| 261 when C_FIGHTER: | |
| 262 /* Fighters automatically recognize skill */ | |
| 263 p_know[P_SKILL] = TRUE; | |
| 264 } | |
| 265 | |
| 266 /* Choose an initial quest item */ | |
| 267 if (!wizard) { | |
| 268 if (def_attr == FALSE) | |
| 269 quest_item = rnd(MAXRELIC); | |
| 270 } | |
| 271 mpos = 0; | |
| 272 draw(cw); | |
| 273 msg("You have been quested to retrieve the %s....", | |
| 274 rel_magic[quest_item].mi_name); | |
| 275 mpos = 0; | |
| 276 playit(); | |
| 277 } | |
| 278 | |
| 279 /* | |
| 280 * endit: | |
| 281 * Exit the program abnormally. | |
| 282 */ | |
| 283 | |
| 284 /*UNUSED*/ | |
| 285 void | |
| 286 endit(sig) | |
| 287 int sig; | |
| 288 { | |
| 289 NOOP(sig); | |
| 290 fatal("Ok, if you want to exit that badly, I'll have to allow it\n"); | |
| 291 } | |
| 292 | |
| 293 /* | |
| 294 * fatal: | |
| 295 * Exit the program, printing a message. | |
| 296 */ | |
| 297 | |
| 298 fatal(s) | |
| 299 char *s; | |
| 300 { | |
| 301 clear(); | |
| 302 move(lines-2, 0); | |
| 303 printw("%s", s); | |
| 304 draw(stdscr); | |
| 305 printf("\n"); /* So the curser doesn't stop at the end of the line */ | |
| 306 exit_game(EXIT_ENDWIN); | |
| 307 } | |
| 308 | |
| 309 /* | |
| 310 * rnd: | |
| 311 * Pick a very random number. | |
| 312 */ | |
| 313 | |
| 314 rnd(range) | |
| 315 register int range; | |
| 316 { | |
| 317 return( md_rand(range) ); | |
| 318 } | |
| 319 | |
| 320 /* | |
| 321 * roll: | |
| 322 * roll a number of dice | |
| 323 */ | |
| 324 | |
| 325 roll(number, sides) | |
| 326 register int number, sides; | |
| 327 { | |
| 328 register int dtotal = 0; | |
| 329 | |
| 330 while(number--) | |
| 331 dtotal += rnd(sides)+1; | |
| 332 return dtotal; | |
| 333 } | |
| 334 | |
| 335 setup() | |
| 336 { | |
| 337 md_setup(); | |
| 338 } | |
| 339 | |
| 340 /* | |
| 341 * playit: | |
| 342 * The main loop of the program. Loop until the game is over, | |
| 343 * refreshing things and looking at the proper times. | |
| 344 */ | |
| 345 | |
| 346 playit() | |
| 347 { | |
| 348 register char *opts; | |
| 349 | |
| 350 /* | |
| 351 * parse environment declaration of options | |
| 352 */ | |
| 353 if ((opts = getenv("ROGUEOPTS")) != NULL) | |
| 354 parse_opts(opts); | |
| 355 | |
| 356 player.t_oldpos = hero; | |
| 357 oldrp = roomin(&hero); | |
| 358 after = TRUE; | |
| 359 command(); /* Command execution */ | |
| 360 endit(-1); | |
| 361 } | |
| 362 | |
| 363 /* | |
| 364 * see if the system is being used too much for this game | |
| 365 */ | |
| 366 | |
| 367 too_much() | |
| 368 { | |
| 369 /* we no longer do load checking or user counts */ | |
| 370 return(FALSE); | |
| 371 } | |
| 372 | |
| 373 /* | |
| 374 * author: | |
| 375 * See if a user is an author of the program |
