rogue

Ken Arnold's Rogue
git clone https://git.woozle.org/neale/rogue.git

Neale Pickett  ·  2013-07-18

rmap.c

  1/*
  2 * Exploring the dungeons of doom
  3 * Copyright (C) 1981 by Michael Toy, Ken Arnold, and Glenn Wichman
  4 * All rights reserved
  5 *
  6 * @(#)rmap.c	4.22 (Berkeley) 02/05/99
  7 */
  8
  9#include <curses.h>
 10#ifdef	attron
 11#include <term.h>
 12#endif	attron
 13#include <signal.h>
 14#include <pwd.h>
 15#include "netprot.h"
 16
 17/*
 18 * main:
 19 *	The main program, of course
 20 */
 21int
 22main(int argc, char **argv, char **envp)
 23{
 24    char *env;
 25    struct passwd *pw;
 26    int lowtime;
 27
 28#ifndef DUMP
 29    signal(SIGQUIT, exit);
 30    signal(SIGILL, exit);
 31    signal(SIGTRAP, exit);
 32    signal(SIGIOT, exit);
 33    signal(SIGFPE, exit);
 34    signal(SIGBUS, exit);
 35    signal(SIGSEGV, exit);
 36    signal(SIGSYS, exit);
 37#endif
 38
 39#ifdef MASTER
 40    /*
 41     * Check to see if he is a wizard
 42     */
 43    if (argc >= 2 && argv[1][0] == '\0')
 44	if (strcmp(PASSWD, crypt(getpass("Wizard's password: "), "mT")) == 0)
 45	{
 46	    Wizard = TRUE;
 47	    Player.t_flags |= SEEMONST;
 48	    argv++;
 49	    argc--;
 50	}
 51#endif
 52
 53    /*
 54     * get Home and options from environment
 55     */
 56    if ((env = getenv("HOME")) != NULL)
 57	strcpy(Home, env);
 58    else if ((pw = getpwuid(getuid())) != NULL)
 59	strcpy(Home, pw->pw_dir);
 60    strcat(Home, "/");
 61
 62    strcpy(File_name, Home);
 63    strcat(File_name, "rogue.save");
 64
 65    if ((env = getenv("ROGUEOPTS")) != NULL)
 66	parse_opts(env);
 67    if (env == NULL || Whoami[0] == '\0')
 68	if ((pw = getpwuid(getuid())) == NULL)
 69	{
 70	    printf("Say, who the hell are you?\n");
 71	    exit(1);
 72	}
 73	else
 74	    strucpy(Whoami, pw->pw_name, strlen(pw->pw_name));
 75
 76#ifdef MASTER
 77    if (Wizard && getenv("SEED") != NULL)
 78	Dnum = atoi(getenv("SEED"));
 79    else
 80#endif
 81	Dnum = lowtime + getpid();
 82    Seed = Dnum;
 83
 84    /*
 85     * check for print-score option
 86     */
 87    open_score();
 88    if (argc == 2)
 89	if (strcmp(argv[1], "-s") == 0)
 90	{
 91	    Noscore = TRUE;
 92	    score(0, -1);
 93	    exit(0);
 94	}
 95	else if (strcmp(argv[1], "-d") == 0)
 96	{
 97	    Dnum = rnd(100);	/* throw away some rnd()s to break patterns */
 98	    while (--Dnum)
 99		rnd(100);
100	    Purse = rnd(100) + 1;
101	    Level = rnd(100) + 1;
102	    initscr();
103	    getltchars();
104	    death(death_monst());
105	    exit(0);
106	}
107
108    init_check();			/* check for legal startup */
109    if (argc == 2)
110	if (!restore(argv[1], envp))	/* Note: restore will never return */
111	    my_exit(1);
112    lowtime = (int) time(NULL);
113#ifdef MASTER
114    if (Wizard)
115	printf("Hello %s, welcome to dungeon #%d", Whoami, Dnum);
116    else
117#endif
118	printf("Hello %s, just a moment while I dig the dungeon...", Whoami);
119    fflush(stdout);
120
121    initscr();				/* Start up cursor package */
122    init_probs();			/* Set up prob tables for objects */
123    init_player();			/* Set up initial Player stats */
124    init_names();			/* Set up names of scrolls */
125    init_colors();			/* Set up colors of potions */
126    init_stones();			/* Set up stone settings of rings */
127    init_materials();			/* Set up materials of wands */
128    setup();
129
130    /*
131     * The screen must be at least NUMLINES x NUMCOLS
132     */
133    if (LINES < NUMLINES || COLS < NUMCOLS)
134    {
135	printf("\nSorry, the screen must be at least %dx%d\n", NUMLINES, NUMCOLS);
136	endwin();
137	my_exit(1);
138    }
139
140    /*
141     * Set up windows
142     */
143    Hw = newwin(LINES, COLS, 0, 0);
144#ifdef	attron
145    idlok(stdscr, TRUE);
146    idlok(Hw, TRUE);
147#endif	attron
148#ifdef MASTER
149    Noscore = Wizard;
150#endif
151    new_level();			/* Draw current level */
152    /*
153     * Start up daemons and fuses
154     */
155    start_daemon(runners, 0, AFTER);
156    start_daemon(doctor, 0, AFTER);
157    fuse(swander, 0, WANDERTIME, AFTER);
158    start_daemon(stomach, 0, AFTER);
159    playit();
160}
161
162/*
163 * endit:
164 *	Exit the program abnormally.
165 */
166void
167endit(int sig)
168{
169    fatal("Okay, bye bye!\n");
170}
171
172/*
173 * fatal:
174 *	Exit the program, printing a message.
175 */
176void
177fatal(char *s)
178{
179    mvaddstr(LINES - 2, 0, s);
180    refresh();
181    endwin();
182    my_exit(0);
183}
184
185/*
186 * rnd:
187 *	Pick a very random number.
188 */
189int
190rnd(int range)
191{
192    return range == 0 ? 0 : abs((int) RN) % range;
193}
194
195/*
196 * roll:
197 *	Roll a number of dice
198 */
199int 
200roll(int number, int sides)
201{
202    int dtotal = 0;
203
204    while (number--)
205	dtotal += rnd(sides)+1;
206    return dtotal;
207}
208
209#ifdef SIGTSTP
210/*
211 * tstp:
212 *	Handle stop and start signals
213 */
214void
215tstp(int ignored)
216{
217    int y, x;
218    int oy, ox;
219
220    /*
221     * leave nicely
222     */
223    getyx(curscr, oy, ox);
224    mvcur(0, COLS - 1, LINES - 1, 0);
225    endwin();
226    resetltchars();
227    fflush(stdout);
228    kill(0, SIGTSTP);		/* send actual signal and suspend process */
229
230    /*
231     * start back up again
232     */
233    signal(SIGTSTP, tstp);
234    crmode();
235    noecho();
236    playltchars();
237    clearok(curscr, TRUE);
238    wrefresh(curscr);
239    getyx(curscr, y, x);
240    mvcur(y, x, oy, ox);
241    fflush(stdout);
242    curscr->_cury = oy;
243    curscr->_curx = ox;
244}
245#endif
246
247/*
248 * playit:
249 *	The main loop of the program.  Loop until the game is over,
250 *	refreshing things and looking at the proper times.
251 */
252void
253playit(void)
254{
255    char *opts;
256
257    /*
258     * set up defaults for slow terminals
259     */
260
261#ifndef	attron
262    if (_tty.sg_ospeed <= B1200)
263#else	attron
264    if (baudrate() <= 1200)
265#endif	attron
266    {
267	Terse = TRUE;
268	Jump = TRUE;
269	See_floor = FALSE;
270    }
271#ifndef	attron
272    if (!CE)
273#else	attron
274    if (clr_eol)
275#endif	attron
276	Inv_type = INV_CLEAR;
277
278    /*
279     * parse environment declaration of options
280     */
281    if ((opts = getenv("ROGUEOPTS")) != NULL)
282	parse_opts(opts);
283
284
285    Oldpos = Hero;
286    Oldrp = roomin(&Hero);
287    while (Playing)
288	command();			/* Command execution */
289    endit(0);
290}
291
292/*
293 * quit:
294 *	Have player make certain, then exit.
295 */
296void
297quit(int sig)
298{
299    int oy, ox;
300
301    /*
302     * Reset the signal in case we got here via an interrupt
303     */
304    if (!Q_comm)
305	Mpos = 0;
306    getyx(curscr, oy, ox);
307    msg("really quit?");
308    if (readchar() == 'y')
309    {
310	signal(SIGINT, leave);
311	clear();
312	mvprintw(LINES - 2, 0, "You quit with %d gold pieces", Purse);
313	move(LINES - 1, 0);
314	refresh();
315	score(Purse, 1);
316	my_exit(0);
317    }
318    else
319    {
320	move(0, 0);
321	clrtoeol();
322	status();
323	move(oy, ox);
324	refresh();
325	Mpos = 0;
326	Count = 0;
327	To_death = FALSE;
328    }
329}
330
331/*
332 * leave:
333 *	Leave quickly, but curteously
334 */
335void
336leave(int sig)
337{
338    static char buf[BUFSIZ];
339
340    setbuf(stdout, buf);	/* throw away pending output */
341#ifndef	attron
342    if (!_endwin)
343    {
344	mvcur(0, COLS - 1, LINES - 1, 0);
345	endwin();
346    }
347#else	attron
348    endwin();
349#endif	attron
350    putchar('\n');
351    my_exit(0);
352}
353
354/*
355 * my_exit:
356 *	Leave the process properly
357 */
358void
359my_exit(int st)
360{
361    resetltchars();
362    exit(st);
363}