forked from Davidslv/rogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
398 lines (343 loc) · 6.68 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
/*
* Rogue: Exploring the Dungeons of Doom
* Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
* All rights reserved.
*
* See the file LICENSE.TXT for full copyright and licensing information.
*
* @(#)main.c 4.22 (Berkeley) 02/05/99
*/
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <curses.h>
#include <locale.h>
#include "rogue.h"
/*
* main:
* The main program, of course
*/
int
main(int argc, char **argv, char **envp)
{
char *env;
int lowtime;
setlocale(LC_CTYPE, "");
md_init();
#ifdef MASTER
/*
* Check to see if he is a wizard
*/
if (argc >= 2 && argv[1][0] == '\0')
if (strcmp(PASSWD, md_crypt(md_getpass("Shuıtoa po mí wızard kú hí móq? "), "mT")) == 0)
{
wizard = TRUE;
player.t_flags |= SEEMONST;
argv++;
argc--;
}
#endif
/*
* get home and options from environment
*/
strncpy(home, md_gethomedir(), MAXSTR);
strcpy(file_name, home);
strcat(file_name, "rogue.save");
if ((env = getenv("ROGUEOPTS")) != NULL)
parse_opts(env);
if (env == NULL || whoami[0] == '\0')
strucpy(whoami, md_getusername(), (int) strlen(md_getusername()));
lowtime = (int) time(NULL);
#ifdef MASTER
if (wizard && getenv("SEED") != NULL)
dnum = atoi(getenv("SEED"));
else
#endif
dnum = lowtime + md_getpid();
seed = dnum;
open_score();
/*
* Drop setuid/setgid after opening the scoreboard file.
*/
md_normaluser();
/*
* check for print-score option
*/
md_normaluser(); /* we drop any setgid/setuid priveldges here */
if (argc == 2)
{
if (strcmp(argv[1], "-s") == 0)
{
noscore = TRUE;
score(0, -1, 0);
exit(0);
}
else if (strcmp(argv[1], "-d") == 0)
{
dnum = rnd(100); /* throw away some rnd()s to break patterns */
while (--dnum)
rnd(100);
purse = rnd(100) + 1;
level = rnd(100) + 1;
initscr();
getltchars();
death(death_monst());
exit(0);
}
}
init_check(); /* check for legal startup */
if (argc == 2)
if (!restore(argv[1], envp)) /* Note: restore will never return */
my_exit(1);
#ifdef MASTER
if (wizard)
printf("Hello %s, welcome to dungeon #%d", whoami, dnum);
else
#endif
printf("Jadı, hóı mí %s, lao súq, ꝡä baı jí báq doqjoq ba...", whoami);
fflush(stdout);
initscr(); /* Start up cursor package */
init_probs(); /* Set up prob tables for objects */
init_player(); /* Set up initial player stats */
init_names(); /* Set up names of scrolls */
init_colors(); /* Set up colors of potions */
init_stones(); /* Set up stone settings of rings */
init_materials(); /* Set up materials of wands */
setup();
/*
* The screen must be at least NUMLINES x NUMCOLS
*/
if (LINES < NUMLINES || COLS < NUMCOLS)
{
printf("\nKushe, duaı hạsao jíoqchuao báq po %dx%d da.\n", NUMLINES, NUMCOLS);
endwin();
my_exit(1);
}
/*
* Set up windows
*/
hw = newwin(LINES, COLS, 0, 0);
idlok(stdscr, TRUE);
idlok(hw, TRUE);
#ifdef MASTER
noscore = wizard;
#endif
new_level(); /* Draw current level */
/*
* Start up daemons and fuses
*/
start_daemon(runners, 0, AFTER);
start_daemon(doctor, 0, AFTER);
fuse(swander, 0, WANDERTIME, AFTER);
start_daemon(stomach, 0, AFTER);
playit();
return(0);
}
/*
* endit:
* Exit the program abnormally.
*/
void
endit(int sig)
{
NOOP(sig);
fatal("Ina, baıbaı!\n");
}
/*
* fatal:
* Exit the program, printing a message.
*/
void
fatal(char *s)
{
mvaddstr(LINES - 2, 0, s);
refresh();
endwin();
my_exit(0);
}
/*
* rnd:
* Pick a very random number.
*/
int
rnd(int range)
{
return range == 0 ? 0 : abs((int) RN) % range;
}
/*
* roll:
* Roll a number of dice
*/
int
roll(int number, int sides)
{
int dtotal = 0;
while (number--)
dtotal += rnd(sides)+1;
return dtotal;
}
/*
* tstp:
* Handle stop and start signals
*/
void
tstp(int ignored)
{
int y, x;
int oy, ox;
NOOP(ignored);
/*
* leave nicely
*/
getyx(curscr, oy, ox);
mvcur(0, COLS - 1, LINES - 1, 0);
endwin();
resetltchars();
fflush(stdout);
md_tstpsignal();
/*
* start back up again
*/
md_tstpresume();
raw();
noecho();
keypad(stdscr,1);
playltchars();
clearok(curscr, TRUE);
wrefresh(curscr);
getyx(curscr, y, x);
mvcur(y, x, oy, ox);
fflush(stdout);
curscr->_cury = oy;
curscr->_curx = ox;
}
/*
* 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;
/*
* set up defaults for slow terminals
*/
/* if (baudrate() <= 1200)
{
terse = TRUE;
jump = TRUE;
see_floor = FALSE;
} */
if (md_hasclreol())
inv_type = INV_CLEAR;
/*
* parse environment declaration of options
*/
if ((opts = getenv("ROGUEOPTS")) != NULL)
parse_opts(opts);
oldpos = hero;
oldrp = roomin(&hero);
while (playing)
command(); /* Command execution */
endit(0);
}
/*
* quit:
* Have player make certain, then exit.
*/
void
quit(int sig)
{
int oy, ox;
NOOP(sig);
/*
* Reset the signal in case we got here via an interrupt
*/
if (!q_comm)
mpos = 0;
getyx(curscr, oy, ox);
msg("Ma shao shaı luaq súq moq? (n/z)");
if (readchar() == 'n')
{
signal(SIGINT, leave);
clear();
mvprintw(LINES - 2, 0, "Luı shaı súq hêaq sá nuaıhea %d da.", purse);
move(LINES - 1, 0);
refresh();
score(purse, 1, 0);
my_exit(0);
}
else
{
move(0, 0);
clrtoeol();
status();
move(oy, ox);
refresh();
mpos = 0;
count = 0;
to_death = FALSE;
}
}
/*
* leave:
* Leave quickly, but curteously
*/
void
leave(int sig)
{
static char buf[BUFSIZ];
NOOP(sig);
setbuf(stdout, buf); /* throw away pending output */
if (!isendwin())
{
mvcur(0, COLS - 1, LINES - 1, 0);
endwin();
}
putchar('\n');
my_exit(0);
}
/*
* shell:
* Let them escape for a while
*/
void
shell()
{
/*
* Set the terminal back to original mode
*/
move(LINES-1, 0);
refresh();
endwin();
resetltchars();
putchar('\n');
in_shell = TRUE;
after = FALSE;
fflush(stdout);
/*
* Fork and do a shell
*/
md_shellescape();
printf("\n[Dem súq níqgıucıoq doa]");
fflush(stdout);
noecho();
raw();
keypad(stdscr,1);
playltchars();
in_shell = FALSE;
wait_for('\n');
clearok(stdscr, TRUE);
}
/*
* my_exit:
* Leave the process properly
*/
void
my_exit(int st)
{
resetltchars();
exit(st);
}