-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
468 lines (375 loc) · 8.24 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Rogue
* Exploring the dungeons of doom
*
* @(#)main.c 9.0 (rdk) 7/17/84
*
* Super-Rogue
* Copyright (C) 1984 Robert D. Kindelberger
* All rights reserved.
*
* Based on "Rogue: Exploring the Dungeons of Doom"
* Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE.TXT for full copyright and licensing information.
*/
#include <curses.h>
#include <limits.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <sys/stat.h>
#include <termios.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "rogue.h"
#ifdef ATT
#include <time.h>
#endif
#ifdef BSD
#define srand48(seed) srandom(seed)
#define lrand48() random()
#include <sys/time.h>
#endif
#include "rogue.ext"
struct termios terminal;
int main(int argc,char const* argv[],char **envp)
{
char *env;
struct linked_list *item;
struct object *obj;
struct passwd *pw;
struct passwd *getpwuid();
char alldone, wpt;
char *getpass(), *xcrypt(), *strrchr();
int lowtime;
time_t now;
char *roguehome();
char *homedir = roguehome();
#ifdef __DJGPP__
_fmode = O_BINARY;
#endif
if (homedir == NULL)
homedir = "";
playuid = getuid();
if (setuid(playuid) < 0) {
printf("Cannot change to effective uid: %d\n", playuid);
exit(1);
}
playgid = getgid();
/* check for print-score option */
strcpy(scorefile, homedir);
if (*scorefile)
strcat(scorefile,"/");
strcat(scorefile, "srogue.scr");
if(argc >= 2 && strcmp(argv[1], "-s") == 0)
{
showtop(0);
exit(0);
}
if (argc >= 2 && author() && strcmp(argv[1],"-a") == 0)
{
wizard = TRUE;
argv++;
argc--;
}
/* Check to see if he is a wizard */
if (argc >= 2 && strcmp(argv[1],"-w") == 0)
{
if (strcmp(PASSWD, xcrypt(getpass(wizstr),"mT")) == 0)
{
wizard = TRUE;
argv++;
argc--;
}
}
time(&now);
lowtime = (int) now;
/* get home and options from environment */
if ((env = getenv("HOME")) != NULL)
strcpy(home, env);
else if ((pw = getpwuid(playuid)) != NULL)
strcpy(home, pw->pw_dir);
else
home[0] = '\0';
if (strcmp(home,"/") == 0)
home[0] = '\0';
if ((strlen(home) > 0) && (home[strlen(home)-1] != '/'))
strcat(home, "/");
strcpy(file_name, home);
strcat(file_name, "srogue.sav");
if ((env = getenv("ROGUEOPTS")) != NULL)
parse_opts(env);
if (env == NULL || whoami[0] == '\0')
{
if((pw = getpwuid(playuid)) == NULL)
{
printf("Say, who are you?\n");
exit(1);
}
else
strucpy(whoami, pw->pw_name, strlen(pw->pw_name));
}
if (env == NULL || fruit[0] == '\0')
strcpy(fruit, "juicy-fruit");
if (argc == 2)
if(!restore(argv[1], envp)) /* NOTE: NEVER RETURNS */
exit(1);
dnum = (wizard && getenv("SEED") != NULL ?
atoi(getenv("SEED")) : lowtime + getpid());
if(wizard)
printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
else
printf("Hello %s, One moment while I open the door to the dungeon...\n", whoami);
fflush(stdout);
seed = dnum;
srand48(seed); /* init rnd number gen */
signal(SIGINT, byebye); /* just in case */
signal(SIGQUIT ,byebye);
init_everything();
#ifdef __INTERIX
setenv("TERM","interix");
#endif
initscr(); /* Start up cursor package */
if (strcmp(termname(),"dumb") == 0)
{
endwin();
printf("ERROR in terminal parameters.\n");
printf("Check TERM in environment.\n");
byebye(1);
}
if (LINES < 24 || COLS < 80) {
endwin();
printf("ERROR: screen size too small\n");
byebye(1);
}
if ((whoami[0] == '\0') || (*whoami == '\0') || (strcmp(whoami,"dosuser")==0))
{
echo();
mvaddstr(23,2,"Rogue's Name? ");
wgetnstr(stdscr,whoami,MAXSTR);
noecho();
}
if ((whoami[0] == '\0') || (*whoami == '\0'))
strcpy(whoami,"Rodney");
setup();
/* Set up windows */
cw = newwin(0, 0, 0, 0);
mw = newwin(0, 0, 0, 0);
hw = newwin(0, 0, 0, 0);
waswizard = wizard;
/* Draw current level */
new_level(NORMLEV);
/* Start up daemons and fuses */
srdaemon(status, TRUE, BEFORE);
srdaemon(doctor, TRUE, BEFORE);
srdaemon(stomach, TRUE, BEFORE);
srdaemon(runners, TRUE, AFTER);
fuse(swander, TRUE, WANDERTIME);
/* Give the rogue his weaponry */
do {
wpt = pick_one(w_magic);
switch (wpt)
{
case MACE: case SWORD: case TWOSWORD:
case SPEAR: case TRIDENT: case SPETUM:
case BARDICHE: case PIKE: case BASWORD:
case HALBERD:
alldone = TRUE;
otherwise:
alldone = FALSE;
}
} while(!alldone);
item = new_thing(FALSE, WEAPON, wpt);
obj = OBJPTR(item);
obj->o_hplus = rnd(3);
obj->o_dplus = rnd(3);
obj->o_flags = ISKNOW;
add_pack(item, TRUE);
cur_weapon = obj;
/* Now a bow */
item = new_thing(FALSE, WEAPON, BOW);
obj = OBJPTR(item);
obj->o_hplus = rnd(3);
obj->o_dplus = rnd(3);
obj->o_flags = ISKNOW;
add_pack(item, TRUE);
/* Now some arrows */
item = new_thing(FALSE, WEAPON, ARROW);
obj = OBJPTR(item);
obj->o_count = 25 + rnd(15);
obj->o_hplus = rnd(2);
obj->o_dplus = rnd(2);
obj->o_flags = ISKNOW;
add_pack(item, TRUE);
/* And his suit of armor */
wpt = pick_one(a_magic);
item = new_thing(FALSE, ARMOR, wpt);
obj = OBJPTR(item);
obj->o_flags = ISKNOW;
obj->o_ac = armors[wpt].a_class - rnd(4);
cur_armor = obj;
add_pack(item, TRUE);
/* Give him some food */
item = new_thing(FALSE, FOOD, 0);
add_pack(item, TRUE);
playit();
return 0;
}
/*
* endit:
* Exit the program abnormally.
*/
void endit(int a)
{
fatal("Ok, if you want to exit that badly, I'll have to allow it");
}
/*
* fatal:
* Exit the program, printing a message.
*/
void fatal(char *s)
{
clear();
refresh();
endwin();
fprintf(stderr,"%s\n\r",s);
fflush(stderr);
byebye(2);
}
/*
* byebye:
* Exit here and reset the users terminal parameters
* to the way they were when he started
*/
void byebye(int how)
{
if (!isendwin())
endwin();
exit(how); /* exit like flag says */
}
/*
* rnd:
* Pick a very random number.
*/
int rnd(int range)
{
int wh;
if (range == 0)
wh = 0;
else {
wh = lrand48() % range;
wh &= 0x7FFFFFFF;
}
return wh;
}
/*
* roll:
* roll a number of dice
*/
int roll(int number,int sides)
{
int dtotal = 0;
while(number-- > 0)
dtotal += rnd(sides)+1;
return dtotal;
}
/*
** setup: Setup signal catching functions
*/
void setup()
{
signal(SIGHUP, auto_save);
signal(SIGINT, auto_save);
signal(SIGQUIT, byebye);
signal(SIGILL, game_err);
signal(SIGTRAP, game_err);
#ifdef SIGIOT
signal(SIGIOT, game_err);
#endif
#ifdef SIGEMT
signal(SIGEMT, game_err);
#endif
signal(SIGFPE, game_err);
#ifdef SIGBUS
signal(SIGBUS, game_err);
#endif
signal(SIGSEGV, game_err);
#ifdef SIGSYS
signal(SIGSYS, game_err);
#endif
signal(SIGPIPE, game_err);
signal(SIGTERM, game_err);
cbreak();
noecho();
}
/*
** playit: The main loop of the program. Loop until the game is over,
** refreshing things and looking at the proper times.
*/
void playit()
{
char *opts;
tcgetattr(0,&terminal);
/* parse environment declaration of options */
if ((opts = getenv("ROGUEOPTS")) != NULL)
parse_opts(opts);
player.t_oldpos = hero;
oldrp = roomin(&hero);
nochange = FALSE;
while (playing)
command(); /* Command execution */
endit(0);
}
/*
** author: See if a user is an author of the program
*/
bool author()
{
switch (playuid) {
case 100:
case 0:
return TRUE;
default:
return FALSE;
}
}
int directory_exists(char *dirname)
{
struct stat sb;
if (stat(dirname, &sb) == 0) /* path exists */
return (S_ISDIR (sb.st_mode));
return(0);
}
char *roguehome()
{
static char path[1024];
char *end,*home;
if ( (home = getenv("ROGUEHOME")) != NULL)
{
if (*home)
{
strncpy(path, home, PATH_MAX - 20);
end = &path[strlen(path)-1];
while( (end >= path) && ((*end == '/') || (*end == '\\')))
*end-- = '\0';
if (directory_exists(path))
return(path);
}
}
if (directory_exists("/var/games/roguelike"))
return("/var/games/roguelike");
if (directory_exists("/var/lib/roguelike"))
return("/var/lib/roguelike");
if (directory_exists("/var/roguelike"))
return("/var/roguelike");
if (directory_exists("/usr/games/lib"))
return("/usr/games/lib");
if (directory_exists("/games/roguelik"))
return("/games/roguelik");
return(NULL);
}