forked from RoguelikeRestorationProject/urogue1.03
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
282 lines (215 loc) · 6.03 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
/*
main.c - setup code
UltraRogue: The Ultimate Adventure in the Dungeons of Doom
Copyright (C) 1985, 1986, 1992, 1993, 1995 Herb Chong
All rights reserved.
Based on "Advanced Rogue"
Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka
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.
*/
#define _ALL_SOURCE
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include "rogue.h"
#define MIN(a,b) ((a) < (b) ? (a) : (b))
FILE *fd_score = NULL;
int ur_lines, ur_cols;
char home[LINELEN];
/* Command line options */
int prscore; /* Print scores */
int prversion; /* Print version info */
int
main(int argc, char *argv[])
{
int x;
char *env;
time_t lowtime;
time_t now;
int rflag = 0;
char *nm;
float scale;
/* get home from environment */
if ((env = getenv("HOME")) != NULL)
strcpy(home, env);
else
strcpy(home, ".");
strcat(home, "/");
for (x = 1; x < argc; x++)
{
if (argv[x][0] != '-')
break;
switch (argv[x][1])
{
case 's':
prscore = TRUE;
break;
case 'v':
prversion = TRUE;
break;
case 'r':
rflag = TRUE;
break;
default:
fprintf(stderr,"%s: Unknown option '%c'.\n",argv[0],argv[x][1]);
exit(1);
}
}
if (!rflag)
{
argc -= (x - 1);
argv += (x - 1);
}
/* Get default score file */
strcpy(score_file, "urogue.scr");
fd_score = fopen(score_file, "r+");
if (fd_score == NULL)
fd_score = fopen(score_file, "a+");
if ((env = getenv("OPTIONS")) != NULL)
parse_opts(env);
nm = getenv("USER");
if (nm != NULL)
strcpy(whoami,nm);
else
strcpy(whoami,"anonymous");
lowtime = time(&now);
dnum = (wizard && getenv("SEED") != NULL ? atoi( getenv("SEED")) : (int)lowtime);
ur_srandom(dnum);
if (env == NULL || fruit[0] == '\0')
{
static const char *funfruit[] =
{
"candleberry", "caprifig", "dewberry", "elderberry",
"gooseberry", "guanabana", "hagberry", "ilama", "imbu",
"jaboticaba", "jujube", "litchi", "mombin", "pitanga",
"prickly pear", "rambutan", "sapodilla", "soursop",
"sweetsop", "whortleberry"
};
strcpy(fruit, funfruit[rnd(sizeof(funfruit) / sizeof(funfruit[0]))]);
}
/* put a copy of fruit in the right place */
fd_data[1].mi_name = strdup(fruit);
/* print scores */
if (prscore)
{
waswizard = TRUE;
score(0L, 0, SCOREIT, 0);
exit(0);
}
/* check for version option */
if (prversion)
{
printf("UltraRogue Version %s.\n", release);
exit(0);
}
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);
init_things(); /* Set up probabilities of things */
init_fd(); /* Set up food probabilities */
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 */
ur_lines = MIN(LINES, 25);
ur_cols = MIN(COLS, 80);
refresh();
init_names(); /* Set up names of scrolls */
cbreak();
crmode(); /* Cbreak mode */
noecho(); /* Echo off */
nonl();
scale = (float) (ur_lines * ur_cols) / (80.0F * 25.0F); /* get food right for */
/* different screen sizes */
food_left = (int) (food_left * scale);
/* Set up windows */
cw = newwin(ur_lines, ur_cols, 0, 0);
mw = newwin(ur_lines, ur_cols, 0, 0);
hw = newwin(ur_lines, ur_cols, 0, 0);
if (argc == 2 && argv[1][0] != '\0' && !restore(argv[1]))
/* Note: restore returns on error only */
exit(1);
waswizard = wizard; /* set wizard flags */
init_player(); /* look up things and outfit pack */
resurrect = pstats.s_const;
init_exp(); /* set first experience level change */
init_flags(); /* set initial flags */
wclear(hw);
wrefresh(hw);
new_level(POSTLEV); /* Draw current level */
/* Start up daemons and fuses */
start_daemon(DAEMON_DOCTOR, &player, AFTER);
light_fuse(FUSE_SWANDER, 0, WANDERTIME, AFTER);
start_daemon(DAEMON_STOMACH, 0, AFTER);
start_daemon(DAEMON_RUNNERS, 0, AFTER);
char_type = player.t_ctype;
player.t_oldpos = hero;
oldrp = roomin(&hero);
after = TRUE;
signal(SIGINT, quit_handler);
while(playing)
{
command(); /* Command execution */
}
fatal("");
return(0);
}
/*
fatal()
Exit the program, printing a message.
*/
void
fatal(char *s)
{
clear();
move(ur_lines - 2, 0);
printw("%s", s);
wrefresh(stdscr);
endwin();
printf("\n"); /* So the cursor doesn't stop at the end of the line */
exit(100);
}
/*
rnd()
Pick a very random number.
*/
unsigned char
ucrnd(unsigned char range)
{
return (unsigned char)(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
}
short
srnd(short range)
{
return (short)(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
}
int
rnd(int range)
{
return (range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
}
unsigned long
ulrnd(unsigned long range)
{
return(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
}
/*
roll()
roll a number of dice
*/
int
roll(int number, int sides)
{
int dtotal = 0;
while (number--)
dtotal += rnd(sides) + 1;
return(dtotal);
}