init.c revision 1.17 1 /* $NetBSD: init.c,v 1.17 2008/01/14 03:50:01 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Timothy C. Stoehr.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 5/31/93";
39 #else
40 __RCSID("$NetBSD: init.c,v 1.17 2008/01/14 03:50:01 dholland Exp $");
41 #endif
42 #endif /* not lint */
43
44 /*
45 * init.c
46 *
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
52 * gain or profit.
53 *
54 */
55
56 #include <stdlib.h>
57 #include <fcntl.h>
58
59 #include "rogue.h"
60
61 static void do_args(int, char **);
62 static void do_opts(void);
63 static void env_get_value(char **, char *, boolean);
64 static void init_str(char **, const char *);
65 static void player_init(void);
66
67 static char *rest_file = NULL;
68 static boolean init_curses = 0;
69
70 char login_name[MAX_OPT_LEN];
71 char *nick_name = NULL;
72 boolean cant_int = 0;
73 boolean did_int = 0;
74 boolean score_only;
75 boolean save_is_interactive = 1;
76 boolean ask_quit = 1;
77 boolean no_skull = 0;
78 boolean passgo = 0;
79 const char *error_file = "rogue.esave";
80 const char *byebye_string = "Okay, bye bye!";
81 gid_t gid, egid;
82
83 int
84 init(int argc, char *argv[])
85 {
86 const char *pn;
87 int seed;
88 int fd;
89
90 gid = getgid();
91 egid = getegid();
92 setegid(gid);
93 /* Check for dirty tricks with closed fds 0, 1, 2 */
94 fd = open("/dev/null", O_RDONLY);
95 if (fd < 3)
96 exit(1);
97 close(fd);
98
99 seed = 0;
100 pn = md_gln();
101 if ((!pn) || (strlen(pn) >= MAX_OPT_LEN)) {
102 clean_up("Hey! Who are you?");
103 }
104 /* LOGIN_NAME_SIZE == MAX_OPT_LEN now, but just in case... */
105 (void)strlcpy(login_name, pn, sizeof(login_name));
106
107 do_args(argc, argv);
108 do_opts();
109
110 if (!score_only && !rest_file) {
111 printf("Hello %s, just a moment while I dig the dungeon...",
112 nick_name);
113 fflush(stdout);
114 }
115
116 initscr();
117 if ((LINES < DROWS) || (COLS < DCOLS)) {
118 clean_up("must be played on at least 80 x 24 screen");
119 }
120 start_window();
121 init_curses = 1;
122
123 md_heed_signals();
124
125 if (score_only) {
126 put_scores(NULL, 0);
127 }
128 seed = md_gseed();
129 (void)srrandom(seed);
130 if (rest_file) {
131 restore(rest_file);
132 return(1);
133 }
134 mix_colors();
135 get_wand_and_ring_materials();
136 make_scroll_titles();
137
138 level_objects.next_object = NULL;
139 level_monsters.next_monster = NULL;
140 player_init();
141 ring_stats(0);
142 return(0);
143 }
144
145 static void
146 player_init(void)
147 {
148 object *obj;
149
150 rogue.pack.next_object = NULL;
151
152 obj = alloc_object();
153 get_food(obj, 1);
154 (void)add_to_pack(obj, &rogue.pack, 1);
155
156 obj = alloc_object(); /* initial armor */
157 obj->what_is = ARMOR;
158 obj->which_kind = RINGMAIL;
159 obj->class = RINGMAIL+2;
160 obj->is_protected = 0;
161 obj->d_enchant = 1;
162 (void)add_to_pack(obj, &rogue.pack, 1);
163 do_wear(obj);
164
165 obj = alloc_object(); /* initial weapons */
166 obj->what_is = WEAPON;
167 obj->which_kind = MACE;
168 obj->damage = "2d3";
169 obj->hit_enchant = obj->d_enchant = 1;
170 obj->identified = 1;
171 (void)add_to_pack(obj, &rogue.pack, 1);
172 do_wield(obj);
173
174 obj = alloc_object();
175 obj->what_is = WEAPON;
176 obj->which_kind = BOW;
177 obj->damage = "1d2";
178 obj->hit_enchant = 1;
179 obj->d_enchant = 0;
180 obj->identified = 1;
181 (void)add_to_pack(obj, &rogue.pack, 1);
182
183 obj = alloc_object();
184 obj->what_is = WEAPON;
185 obj->which_kind = ARROW;
186 obj->quantity = get_rand(25, 35);
187 obj->damage = "1d2";
188 obj->hit_enchant = 0;
189 obj->d_enchant = 0;
190 obj->identified = 1;
191 (void)add_to_pack(obj, &rogue.pack, 1);
192 }
193
194 void
195 clean_up(const char *estr)
196 {
197 if (save_is_interactive) {
198 if (init_curses) {
199 move(DROWS-1, 0);
200 refresh();
201 stop_window();
202 }
203 printf("\n%s\n", estr);
204 }
205 md_exit(0);
206 }
207
208 void
209 start_window(void)
210 {
211 cbreak();
212 noecho();
213 #ifndef BAD_NONL
214 nonl();
215 #endif
216 }
217
218 void
219 stop_window(void)
220 {
221 endwin();
222 }
223
224 void
225 byebye(int dummy __unused)
226 {
227 md_ignore_signals();
228 if (ask_quit) {
229 quit(1);
230 } else {
231 clean_up(byebye_string);
232 }
233 md_heed_signals();
234 }
235
236 void
237 onintr(int dummy __unused)
238 {
239 md_ignore_signals();
240 if (cant_int) {
241 did_int = 1;
242 } else {
243 check_message();
244 messagef(1, "interrupt");
245 }
246 md_heed_signals();
247 }
248
249 void
250 error_save(int dummy __unused)
251 {
252 save_is_interactive = 0;
253 save_into_file(error_file);
254 clean_up("");
255 }
256
257 static void
258 do_args(int argc, char *argv[])
259 {
260 int i, j;
261
262 for (i = 1; i < argc; i++) {
263 if (argv[i][0] == '-') {
264 for (j = 1; argv[i][j]; j++) {
265 switch(argv[i][j]) {
266 case 's':
267 score_only = 1;
268 break;
269 }
270 }
271 } else {
272 rest_file = argv[i];
273 }
274 }
275 }
276
277 static void
278 do_opts(void)
279 {
280 char *eptr;
281
282 if ((eptr = md_getenv("ROGUEOPTS")) != NULL) {
283 for (;;) {
284 while ((*eptr) == ' ') {
285 eptr++;
286 }
287 if (!(*eptr)) {
288 break;
289 }
290 if (!strncmp(eptr, "fruit=", 6)) {
291 eptr += 6;
292 env_get_value(&fruit, eptr, 1);
293 } else if (!strncmp(eptr, "file=", 5)) {
294 eptr += 5;
295 env_get_value(&save_file, eptr, 0);
296 } else if (!strncmp(eptr, "jump", 4)) {
297 jump = 1;
298 } else if (!strncmp(eptr, "name=", 5)) {
299 eptr += 5;
300 env_get_value(&nick_name, eptr, 0);
301 } else if (!strncmp(eptr, "noaskquit", 9)) {
302 ask_quit = 0;
303 } else if (!strncmp(eptr, "noskull", 5) ||
304 !strncmp(eptr,"notomb", 6)) {
305 no_skull = 1;
306 } else if (!strncmp(eptr, "passgo", 5)) {
307 passgo = 1;
308 }
309 while ((*eptr) && (*eptr != ',')) {
310 eptr++;
311 }
312 if (!(*(eptr++))) {
313 break;
314 }
315 }
316 }
317 /* If some strings have not been set through ROGUEOPTS, assign defaults
318 * to them so that the options editor has data to work with.
319 */
320 init_str(&nick_name, login_name);
321 init_str(&save_file, "rogue.save");
322 init_str(&fruit, "slime-mold");
323 }
324
325 static void
326 env_get_value(char **s, char *e, boolean add_blank)
327 {
328 short i = 0;
329 const char *t;
330
331 t = e;
332
333 while ((*e) && (*e != ',')) {
334 if (*e == ':') {
335 *e = ';'; /* ':' reserved for score file purposes */
336 }
337 e++;
338 if (++i >= MAX_OPT_LEN) {
339 break;
340 }
341 }
342 /* note: edit_opts() in room.c depends on this being the right size */
343 *s = md_malloc(MAX_OPT_LEN + 2);
344 if (*s == NULL)
345 clean_up("out of memory");
346 (void)strncpy(*s, t, i);
347 if (add_blank) {
348 (*s)[i++] = ' ';
349 }
350 (*s)[i] = '\0';
351 }
352
353 static void
354 init_str(char **str, const char *dflt)
355 {
356 if (!(*str)) {
357 /* note: edit_opts() in room.c depends on this size */
358 *str = md_malloc(MAX_OPT_LEN + 2);
359 if (*str == NULL)
360 clean_up("out of memory");
361 (void)strlcpy(*str, dflt, MAX_OPT_LEN + 2);
362 }
363 }
364