-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
725 lines (650 loc) · 14.9 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
* main.c - setup code
*
* Advanced Rogue
* Copyright (C) 1984, 1985, 1986 Michael Morgan, Ken Dalka and AT&T
* 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 <signal.h>
#ifdef BSD
#include <sys/time.h>
#else
#include <time.h>
#endif
#include "mach_dep.h"
#include "network.h"
#include "rogue.h"
#ifdef PC7300
#include "sys/window.h"
#include <ctype.h>
extern struct uwdata wdata, oldwin;
extern char oldtext[WTXTNUM][WTXTLEN];
#endif
main(argc, argv, envp)
char **argv;
char **envp;
{
register char *env;
int lowtime;
time_t now;
#ifdef PC7300
int hardwindow; /* Do we have a hardware window? */
#endif
md_init();
/*
* get home and options from environment
*/
strncpy(home, md_gethomedir(), LINELEN);
/* Get default save file */
strcpy(file_name, home);
strcat(file_name, "arogue77.sav");
/* Get default score file */
strcpy(score_file, md_getroguedir());
if (*score_file)
strcat(score_file,"/");
strcat(score_file,"arogue77.scr");
if ((env = getenv("ROGUEOPTS")) != NULL)
parse_opts(env);
if (whoami[0] == '\0')
strucpy(whoami, md_getusername(), strlen(md_getusername()));
/*
* check for print-score option
*/
if (argc == 2 && strcmp(argv[1], "-s") == 0)
{
waswizard = TRUE;
score(0, SCOREIT, 0);
exit(0);
}
#ifdef NUMNET
/*
* Check for a network update
*/
if (argc == 2 && strcmp(argv[1], "-u") == 0) {
unsigned long netread();
int errcheck, errors = 0;
unsigned long amount;
short monster;
/* Read in the amount and monster values to pass to score */
amount = netread(&errcheck, sizeof(unsigned long), stdin);
if (errcheck) errors++;
monster = (short) netread(&errcheck, sizeof(short), stdin);
if (errcheck) errors++;
/* Now do the update if there were no errors */
if (errors) exit(1);
else {
score(amount, UPDATE, monster);
exit(0);
}
}
#endif
#ifdef WIZARD
/*
* Check to see if he is a wizard
*/
if (argc >= 2 && argv[1][0] == '\0')
if (strcmp(PASSWD, md_crypt(md_getpass("Wizard's password: "), "mT")) == 0)
{
wizard = TRUE;
argv++;
argc--;
}
#endif
if (!wizard && !author() && !holiday()) {
printf("Sorry, %s, but you can't play during working hours.\n", whoami);
printf("Try again later.\n");
exit(1);
}
if (!wizard && !author() && too_much()) {
printf("Sorry, %s, but the system is too loaded now.\n", whoami);
printf("Try again later.\n");
exit(1);
}
#if NICE
if (!wizard)
nice(19); /* nice the max amount */
#endif
if (argc == 2)
if (!restore(argv[1], envp)) /* Note: restore will never return */
exit(1);
lowtime = (int) time(&now);
dnum = (wizard && getenv("SEED") != NULL ?
atoi(getenv("SEED")) :
lowtime + getpid());
if (wizard)
printf("Hello %s, welcome to dungeon #%d", whoami, dnum);
else
printf("Hello %s, just a moment while I dig the dungeon...", whoami);
fflush(stdout);
seed = dnum;
md_srand(seed);
#ifdef PC7300
/* Store static window parameters */
hardwindow = ioctl(0, WIOCGETD, &wdata);
if (hardwindow >= 0) { /* We have a hardware window */
extern char **environ;
/* Make sure our window is the right size */
oldwin = wdata;
if ((wdata.uw_height / wdata.uw_vs) < 23 ||
(wdata.uw_width / wdata.uw_hs) < 75) {
wdata.uw_width = 80 * wdata.uw_hs;
wdata.uw_height = 24 * wdata.uw_vs;
wdata.uw_x = 0;
wdata.uw_y = wdata.uw_vs;
wdata.uw_uflags = NBORDER;
/* Make the change */
if (ioctl(1, WIOCSETD, &wdata) >= 0 && environ) {
char **eptr, *tptr, *nptr, *newenv, *lptr = 0, *cptr = 0;
int i, nlines = -1, ncols = -1, nlindig = 0, ncoldig = 0;
struct utdata labelbuf;
/* Save and change window-associated text */
for (i=0; i<WTXTNUM; i++) {
labelbuf.ut_num = i;
ioctl(1, WIOCGETTEXT, &labelbuf);
strncpy(oldtext[i], labelbuf.ut_text, WTXTLEN - 1);
if (*labelbuf.ut_text) {
*labelbuf.ut_text = '\0';
ioctl(1, WIOCSETTEXT, &labelbuf);
}
}
labelbuf.ut_num = WTXTLABEL;
strcpy(labelbuf.ut_text, "Advanced Rogue");
ioctl(1, WIOCSETTEXT, &labelbuf);
/* We have to change the TERMCAP entry */
eptr = environ;
while (*eptr) {
if (strncmp(*eptr, "TERMCAP=", 8) == 0) break;
else eptr++;
}
/* We found a TERMCAP entry */
if (*eptr) {
/* Search for li# and co# */
tptr = *eptr;
while (*tptr) {
switch (*tptr) {
case 'l':
if (nlines == -1 &&
strncmp(tptr, "li#", 3) == 0) {
tptr += 3;
lptr = tptr;
lines = atoi(tptr);
while (isdigit(*tptr)) {
nlindig++;;
tptr++;
}
}
else tptr++;
break;
case 'c':
if (ncols == -1 &&
strncmp(tptr, "co#", 3) == 0) {
tptr += 3;
cptr = tptr;
cols = atoi(tptr);
while (isdigit(*tptr)) {
ncoldig++;
tptr++;
}
}
else tptr++;
break;
default:
tptr++;
}
}
/* Change the entry */
if (ncoldig != 2 || nlindig != 2) {
int length;
/* Add in difference in num lengths plus NULL */
length = strlen(*eptr) - ncoldig - nlindig + 5;
if (ncoldig == 0) length += 4; /* For :co# */
if (nlindig == 0) length += 4; /* For :li# */
newenv = malloc(length);
tptr = *eptr;
nptr = newenv;
if (nlindig == 0 || ncoldig == 0) {
/* Copy up to the first : */
while (*tptr && *tptr != ':') *nptr++ = *tptr++;
/* Do we have to add a field? */
if (nlindig == 0) {
strcpy(nptr, ":li#24");
nptr += 6;
}
if (ncoldig == 0) {
strcpy(nptr, ":co#80");
nptr += 6;
}
}
while (*tptr) {
if (tptr == lptr) {
strcpy(nptr, "24");
nptr += 2;
tptr += nlindig;
}
else if (tptr == cptr) {
strcpy(nptr, "80");
nptr += 2;
tptr += ncoldig;
}
else *nptr++ = *tptr++;
}
*nptr = '\0';
/* Replace the old one */
free(*eptr);
*eptr = newenv;
}
else {
/* Just overwrite the old numbers */
*lptr++ = '2';
*lptr = '4';
*cptr++ = '8';
*cptr = '0';
}
}
}
}
}
#endif
init_things(); /* Set up probabilities of things */
init_colors(); /* Set up colors of potions */
init_stones(); /* Set up stone settings of rings */
init_materials(); /* Set up materials of wands */
initscr(); /* Start up cursor package */
init_names(); /* Set up names of scrolls */
init_misc(); /* Set up miscellaneous magic */
init_foods(); /* set up the food table */
cols = COLS;
lines = LINES;
if (cols > 85) cols = 85;
if (lines > 24) lines = 24;
if (lines < 23 || cols < 75) { /* give player a break if larger font used */
printf("\nERROR: screen size too small for rogue\n");
byebye(0);
}
/*
* Now that we have cols and lines, we can update our window
* structure for non-hardware windows.
*/
#ifdef PC7300
if (hardwindow < 0) {
wdata.uw_x = 0;
wdata.uw_y = 0;
wdata.uw_width = COLS;
wdata.uw_height = LINES;
wdata.uw_uflags = 0;
wdata.uw_hs = 1;
wdata.uw_vs = 1;
wdata.uw_baseline = 0;
}
#endif
setup();
/*
* Set up windows
*/
cw = newwin(lines, cols, 0, 0);
mw = newwin(lines, cols, 0, 0);
hw = newwin(lines, cols, 0, 0);
msgw = newwin(4, cols, 0, 0);
keypad(cw,TRUE);
keypad(msgw,TRUE);
init_player(); /* Roll up the rogue */
waswizard = wizard;
#ifdef WIZARD
/* A super wizard doesn't have to get equipped */
if (wizard && strcmp(getenv("SUPER"),"YES") == 0) {
level = 1;
new_level(NORMLEV);
}
else
#endif
new_level(STARTLEV); /* Draw current level */
/*
* Start up daemons and fuses
*/
daemon(doctor, &player, AFTER);
fuse(swander, 0, WANDERTIME, AFTER);
if (player.t_ctype == C_MAGICIAN || player.t_ctype == C_RANGER)
fuse(spell_recovery, 0, SPELLTIME, AFTER);
if (player.t_ctype == C_DRUID || player.t_ctype == C_RANGER)
fuse(chant_recovery, 0, SPELLTIME, AFTER);
if (player.t_ctype == C_CLERIC || player.t_ctype == C_PALADIN)
fuse(prayer_recovery, 0, SPELLTIME, AFTER);
daemon(stomach, 0, AFTER);
if (player.t_ctype == C_THIEF ||
player.t_ctype == C_ASSASIN ||
player.t_ctype == C_MONK)
daemon(trap_look, 0, AFTER);
/* Does this character have any special knowledge? */
switch (player.t_ctype) {
case C_ASSASIN:
/* Assassins automatically recognize poison */
p_know[P_POISON] = TRUE;
}
/* Choose a quest item */
quest_item = rnd(MAXRELIC);
draw(cw);
msg("You have been quested to retrieve the %s....",
rel_magic[quest_item].mi_name);
mpos = 0;
playit();
}
/*
* endit:
* Exit the program abnormally.
*/
void
endit(sig)
int sig;
{
fatal("Ok, if you want to exit that badly, I'll have to allow it\n");
}
/*
* fatal:
* Exit the program, printing a message.
*/
fatal(s)
char *s;
{
clear();
move(lines-2, 0);
printw("%s", s);
draw(stdscr);
endwin();
#ifdef PC7300
endhardwin();
#endif
printf("\n"); /* So the curser doesn't stop at the end of the line */
exit(0);
}
/*
* rnd:
* Pick a very random number.
*/
rnd(range)
register int range;
{
return(range <= 0 ? 0 : md_rand() % range);
}
/*
* roll:
* roll a number of dice
*/
roll(number, sides)
register int number, sides;
{
register int dtotal = 0;
while(number--)
dtotal += rnd(sides)+1;
return dtotal;
}
# ifdef SIGTSTP
/*
* handle stop and start signals
*/
void
tstp(sig)
int sig;
{
mvcur(0, cols - 1, lines - 1, 0);
endwin();
fflush(stdout);
kill(0, SIGTSTP);
signal(SIGTSTP, tstp);
raw();
noecho();
keypad(cw,1);
keypad(msgw,1);
clearok(curscr, TRUE);
touchwin(cw);
draw(cw);
md_flushinp();
}
# endif
setup()
{
#ifdef CHECKTIME
int checkout();
if (!author()) {
signal(SIGALRM, checkout);
alarm(CHECKTIME * 60);
}
#endif
/*
#ifndef DUMP
signal(SIGILL, bugkill);
#ifdef SIGTRAP
signal(SIGTRAP, bugkill);
#endif
#ifdef SIGIOT
signal(SIGIOT, bugkill);
#endif
#ifdef SIGEMT
signal(SIGEMT, bugkill);
#endif
signal(SIGFPE, bugkill);
#ifdef SIGBUS
signal(SIGBUS, bugkill);
#endif
signal(SIGSEGV, bugkill);
#ifdef SIGSYS
signal(SIGSYS, bugkill);
#endif
#ifdef SIGPIPE
signal(SIGPIPE, bugkill);
#endif
#endif
*/
#ifdef SIGTSTP
signal(SIGTSTP, tstp);
#endif
#ifdef SIGHUP
signal(SIGHUP, auto_save);
#endif
signal(SIGTERM, auto_save);
signal(SIGINT, quit);
#ifdef SIGQUIT
signal(SIGQUIT, endit);
#endif
raw(); /* Cbreak mode */
noecho(); /* Echo off */
}
/*
* playit:
* The main loop of the program. Loop until the game is over,
* refreshing things and looking at the proper times.
*/
playit()
{
register char *opts;
/*
* parse environment declaration of options
*/
if ((opts = getenv("ROGUEOPTS")) != NULL)
parse_opts(opts);
player.t_oldpos = hero;
oldrp = roomin(&hero);
after = TRUE;
command(); /* Command execution */
endit(0);
}
/*
* see if the system is being used too much for this game
*/
too_much()
{
#if MAXPROCESSES
if (loadav() > MAXPROCESSES)
return(TRUE);
#endif
#if MAXUSERS
if (ucount() > MAXUSERS)
return(TRUE);
#endif
return(FALSE);
}
/*
* author:
* See if a user is an author of the program
*/
author()
{
switch (md_getuid()) {
#if AUTHOR
case AUTHOR:
#endif
case 0: /* always OK for root to play */
return TRUE;
default:
return FALSE;
}
}
#if CHECKTIME
static int num_checks = 0; /* times we've gone over in checkout() */
checkout()
{
static char *msgs[] = {
"The system is too loaded for games. Please leave in %d minutes",
"Please save your game. You have %d minutes",
"This is your last chance. You had better leave in %d minutes",
};
int checktime;
signal(SIGALRM, checkout);
if (!holiday() && !author()) {
msg("Game time is over. Your game is being saved.\n\n");
auto_save(); /* NO RETURN */
}
if (too_much()) {
if (num_checks >= 3)
fatal("You didn't listen, so now you are DEAD !!\n");
checktime = CHECKTIME / (num_checks + 1);
chmsg(msgs[num_checks++], checktime);
alarm(checktime * 60);
}
else {
if (num_checks) {
chmsg("The load has dropped. You have a reprieve.");
num_checks = 0;
}
alarm(CHECKTIME * 60);
}
}
/*
* checkout()'s version of msg. If we are in the middle of a shell, do a
* printf instead of a msg to avoid the refresh.
*/
chmsg(fmt, arg)
char *fmt;
int arg;
{
if (in_shell) {
printf(fmt, arg);
putchar('\n');
fflush(stdout);
}
else
msg(fmt, arg);
}
#endif
#ifdef MAXPROCESSES
#include <fcntl.h>
loadav()
{
char *sarcmd = "sar -v | cut -c17-20 | tail -2";
char *gettycmd = "grep getty /etc/inittab | wc -l";
char sysbuffer[BUFSIZ];
char tempfile[50];
char inbuf[BUFSIZ];
int fd, nprocess, ngetty;
sprintf(tempfile, "/tmp/rg%d", getpid());
sprintf(sysbuffer, "%s > %s", sarcmd, tempfile);
if (system(sysbuffer) != 0) {
debug ("system() call failed");
return (MAXPROCESSES - 1);
}
if ((fd = open(tempfile, O_RDONLY)) == -1) {
debug ("open() call failed");
return (MAXPROCESSES - 1);
}
if (read(fd, inbuf, BUFSIZ) == -1) {
debug ("read() call failed");
return (MAXPROCESSES - 1);
}
close(fd);
sscanf(inbuf, "%d", &nprocess);
sprintf(sysbuffer, "%s > %s", gettycmd, tempfile);
if (system(sysbuffer) != 0) {
debug ("system() call failed");
return (MAXPROCESSES - 1);
}
if ((fd = open(tempfile, O_RDONLY)) == -1) {
debug ("open() call failed");
return (MAXPROCESSES - 1);
}
if (read(fd, inbuf, BUFSIZ) == -1) {
debug ("read() call failed");
return (MAXPROCESSES - 1);
}
close(fd);
sscanf(inbuf, "%d", &ngetty);
unlink(tempfile);
return(nprocess - ngetty);
}
#endif
#ifdef MAXUSERS
/*
* ucount:
* Count the number of people on the system
*/
#include <sys/types.h>
#include <utmp.h>
struct utmp buf;
ucount()
{
reg struct utmp *up;
reg FILE *utmp;
reg int count;
if ((utmp = fopen(UTMP, "r")) == NULL)
return 0;
up = &buf;
count = 0;
while (fread(up, 1, sizeof (*up), utmp) > 0)
#ifdef BSD
if (buf.ut_line[0] == 't') /* On a tty */
#else
if (buf.ut_type == USER_PROCESS)
#endif
count++;
fclose(utmp);
return count;
}
#endif
/*
* holiday:
* Returns TRUE when it is a good time to play rogue
*/
holiday()
{
#ifdef CHECKTIME
long now;
struct tm *localtime();
reg struct tm *ntime;
time(&now); /* get the current time */
ntime = localtime(&now);
if(ntime->tm_wday == 0 || ntime->tm_wday == 6)
return TRUE; /* OK on Sat & Sun */
if(ntime->tm_hour < 8 || ntime->tm_hour >= 18)
return TRUE; /* OK before 8AM & after 6PM */
if(ntime->tm_yday <= 7 || ntime->tm_yday >= 350)
return TRUE; /* OK during Christmas */
return FALSE; /* All other times are bad */
#else
return TRUE;
#endif
}