comparison rogue4/main.c @ 12:9535a08ddc39

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