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