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