comparison urogue/main.c @ 256:c495a4f288c6

Import UltraRogue from the Roguelike Restoration Project (r1490)
author John "Elwin" Edwards
date Tue, 31 Jan 2017 19:56:04 -0500
parents
children 096d3cfd9afd
comparison
equal deleted inserted replaced
253:d9badb9c0179 256:c495a4f288c6
1 /*
2 main.c - setup code
3
4 UltraRogue: The Ultimate Adventure in the Dungeons of Doom
5 Copyright (C) 1985, 1986, 1992, 1993, 1995 Herb Chong
6 All rights reserved.
7
8 Based on "Advanced Rogue"
9 Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka
10 All rights reserved.
11
12 Based on "Rogue: Exploring the Dungeons of Doom"
13 Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
14 All rights reserved.
15
16 See the file LICENSE.TXT for full copyright and licensing information.
17 */
18
19 #define _ALL_SOURCE
20
21 #include <time.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include "rogue.h"
27
28 FILE *fd_score = NULL;
29
30 /* Command line options */
31
32 int prscore; /* Print scores */
33 int prversion; /* Print version info */
34
35 int
36 main(int argc, char *argv[])
37 {
38 int x;
39 char *env;
40 time_t lowtime;
41 time_t now;
42 int rflag = 0;
43 char *nm;
44 float scale;
45
46 for (x = 1; x < argc; x++)
47 {
48 if (argv[x][0] != '-')
49 break;
50
51 switch (argv[x][1])
52 {
53 case 's':
54 prscore = TRUE;
55 break;
56
57 case 'v':
58 prversion = TRUE;
59 break;
60
61 case 'r':
62 rflag = TRUE;
63 break;
64
65 default:
66 fprintf(stderr,"%s: Unknown option '%c'.\n",argv[0],argv[x][1]);
67 exit(1);
68 }
69 }
70
71 if (!rflag)
72 {
73 argc -= (x - 1);
74 argv += (x - 1);
75 }
76
77 /* Get default score file */
78
79 strcpy(score_file, "urogue.scr");
80
81 fd_score = fopen(score_file, "r+");
82
83 if (fd_score == NULL)
84 fd_score = fopen(score_file, "a+");
85
86 if ((env = getenv("OPTIONS")) != NULL)
87 parse_opts(env);
88
89 nm = getenv("USER");
90
91 if (nm != NULL)
92 strcpy(whoami,nm);
93 else
94 strcpy(whoami,"anonymous");
95
96 lowtime = time(&now);
97
98 dnum = (wizard && getenv("SEED") != NULL ? atoi( getenv("SEED")) : (int)lowtime);
99
100 ur_srandom(dnum);
101
102 if (env == NULL || fruit[0] == '\0')
103 {
104 static const char *funfruit[] =
105 {
106 "candleberry", "caprifig", "dewberry", "elderberry",
107 "gooseberry", "guanabana", "hagberry", "ilama", "imbu",
108 "jaboticaba", "jujube", "litchi", "mombin", "pitanga",
109 "prickly pear", "rambutan", "sapodilla", "soursop",
110 "sweetsop", "whortleberry"
111 };
112
113 strcpy(fruit, funfruit[rnd(sizeof(funfruit) / sizeof(funfruit[0]))]);
114 }
115
116 /* put a copy of fruit in the right place */
117
118 fd_data[1].mi_name = md_strdup(fruit);
119
120 /* print scores */
121
122 if (prscore)
123 {
124 waswizard = TRUE;
125 score(0L, 0, SCOREIT, 0);
126 exit(0);
127 }
128
129 /* check for version option */
130
131 if (prversion)
132 {
133 printf("UltraRogue Version %s.\n", release);
134 exit(0);
135 }
136
137 if (wizard)
138 printf("Hello %s, welcome to dungeon #%d", whoami, dnum);
139 else
140 printf("Hello %s, just a moment while I dig the dungeon...", whoami);
141
142 mem_debug(2);
143 mem_tracking(1);
144
145 fflush(stdout);
146
147 init_things(); /* Set up probabilities of things */
148 init_fd(); /* Set up food probabilities */
149 init_colors(); /* Set up colors of potions */
150 init_stones(); /* Set up stone settings of rings */
151 init_materials(); /* Set up materials of wands */
152 initscr(); /* Start up cursor package */
153 refresh();
154 init_names(); /* Set up names of scrolls */
155 cbreak();
156 crmode(); /* Cbreak mode */
157 noecho(); /* Echo off */
158 nonl();
159
160 scale = (float) (LINES * COLS) / (80.0F * 25.0F); /* get food right for */
161 /* different screen sizes */
162
163 food_left = (int) (food_left * scale);
164
165 /* Set up windows */
166
167 cw = newwin(LINES, COLS, 0, 0);
168 mw = newwin(LINES, COLS, 0, 0);
169 hw = newwin(LINES, COLS, 0, 0);
170
171 if (argc == 2 && argv[1][0] != '\0' && !restore(argv[1]))
172 /* Note: restore returns on error only */
173 exit(1);
174
175 waswizard = wizard; /* set wizard flags */
176
177 init_player(); /* look up things and outfit pack */
178
179 resurrect = pstats.s_const;
180 init_exp(); /* set first experience level change */
181 init_flags(); /* set initial flags */
182 wclear(hw);
183 wrefresh(hw);
184 new_level(POSTLEV,0); /* Draw current level */
185
186 /* Start up daemons and fuses */
187
188 start_daemon(DAEMON_DOCTOR, &player, AFTER);
189
190 light_fuse(FUSE_SWANDER, 0, WANDERTIME, AFTER);
191
192 start_daemon(DAEMON_STOMACH, 0, AFTER);
193 start_daemon(DAEMON_RUNNERS, 0, AFTER);
194
195 char_type = player.t_ctype;
196 player.t_oldpos = hero;
197 oldrp = roomin(hero);
198 after = TRUE;
199
200 signal(SIGINT, quit_handler);
201
202 while(playing)
203 {
204 do_daemons(BEFORE);
205 do_fuses(BEFORE);
206
207 command(); /* Command execution */
208
209 if (after)
210 do_after_effects();
211 }
212
213 fatal("");
214
215 return(0);
216 }
217
218 /*
219 fatal()
220 Exit the program, printing a message.
221 */
222
223 void
224 fatal(char *s)
225 {
226 clear();
227 move(LINES - 2, 0);
228 printw("%s", s);
229 wrefresh(stdscr);
230 endwin();
231 printf("\n"); /* So the cursor doesn't stop at the end of the line */
232 exit(100);
233 }
234
235 /*
236 rnd()
237 Pick a very random number.
238 */
239
240 unsigned char
241 ucrnd(unsigned char range)
242 {
243 return (unsigned char)(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
244 }
245
246 short
247 srnd(short range)
248 {
249 return (short)(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
250 }
251
252 int
253 rnd(int range)
254 {
255 return (range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
256 }
257
258 unsigned long
259 ulrnd(unsigned long range)
260 {
261 return(range <= 0 ? 0 : (ur_random() & 0x7fffffffL) % range);
262 }
263
264 /*
265 roll()
266 roll a number of dice
267 */
268
269 int
270 roll(int number, int sides)
271 {
272 int dtotal = 0;
273
274 while (number--)
275 dtotal += rnd(sides) + 1;
276
277 return(dtotal);
278 }