xref: /original-bsd/games/rogue/init.c (revision 9532a02d)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)init.c	8.1 (Berkeley) 05/31/93";
13 #endif /* not lint */
14 
15 /*
16  * init.c
17  *
18  * This source herein may be modified and/or distributed by anybody who
19  * so desires, with the following restrictions:
20  *    1.)  No portion of this notice shall be removed.
21  *    2.)  Credit shall not be taken for the creation of this source.
22  *    3.)  This code is not to be traded, sold, or used for personal
23  *         gain or profit.
24  *
25  */
26 
27 #include <stdio.h>
28 #include "rogue.h"
29 
30 char login_name[MAX_OPT_LEN];
31 char *nick_name = (char *) 0;
32 char *rest_file = 0;
33 boolean cant_int = 0;
34 boolean did_int = 0;
35 boolean score_only;
36 boolean init_curses = 0;
37 boolean save_is_interactive = 1;
38 boolean ask_quit = 1;
39 boolean no_skull = 0;
40 boolean passgo = 0;
41 char *error_file = "rogue.esave";
42 char *byebye_string = "Okay, bye bye!";
43 
44 extern char *fruit;
45 extern char *save_file;
46 extern short party_room;
47 extern boolean jump;
48 
49 init(argc, argv)
50 int argc;
51 char *argv[];
52 {
53 	char *pn;
54 	int seed;
55 
56 	pn = md_gln();
57 	if ((!pn) || (strlen(pn) >= MAX_OPT_LEN)) {
58 		clean_up("Hey!  Who are you?");
59 	}
60 	(void) strcpy(login_name, pn);
61 
62 	do_args(argc, argv);
63 	do_opts();
64 
65 	if (!score_only && !rest_file) {
66 		printf("Hello %s, just a moment while I dig the dungeon...",
67 			nick_name);
68 		fflush(stdout);
69 	}
70 
71 	initscr();
72 	if ((LINES < DROWS) || (COLS < DCOLS)) {
73 		clean_up("must be played on 24 x 80 screen");
74 	}
75 	start_window();
76 	init_curses = 1;
77 
78 	md_heed_signals();
79 
80 	if (score_only) {
81 		put_scores((object *) 0, 0);
82 	}
83 	seed = md_gseed();
84 	(void) srrandom(seed);
85 	if (rest_file) {
86 		restore(rest_file);
87 		return(1);
88 	}
89 	mix_colors();
90 	get_wand_and_ring_materials();
91 	make_scroll_titles();
92 
93 	level_objects.next_object = (object *) 0;
94 	level_monsters.next_monster = (object *) 0;
95 	player_init();
96 	ring_stats(0);
97 	return(0);
98 }
99 
100 player_init()
101 {
102 	object *obj;
103 
104 	rogue.pack.next_object = (object *) 0;
105 
106 	obj = alloc_object();
107 	get_food(obj, 1);
108 	(void) add_to_pack(obj, &rogue.pack, 1);
109 
110 	obj = alloc_object();		/* initial armor */
111 	obj->what_is = ARMOR;
112 	obj->which_kind = RINGMAIL;
113 	obj->class = RINGMAIL+2;
114 	obj->is_protected = 0;
115 	obj->d_enchant = 1;
116 	(void) add_to_pack(obj, &rogue.pack, 1);
117 	do_wear(obj);
118 
119 	obj = alloc_object();		/* initial weapons */
120 	obj->what_is = WEAPON;
121 	obj->which_kind = MACE;
122 	obj->damage = "2d3";
123 	obj->hit_enchant = obj->d_enchant = 1;
124 	obj->identified = 1;
125 	(void) add_to_pack(obj, &rogue.pack, 1);
126 	do_wield(obj);
127 
128 	obj = alloc_object();
129 	obj->what_is = WEAPON;
130 	obj->which_kind = BOW;
131 	obj->damage = "1d2";
132 	obj->hit_enchant = 1;
133 	obj->d_enchant = 0;
134 	obj->identified = 1;
135 	(void) add_to_pack(obj, &rogue.pack, 1);
136 
137 	obj = alloc_object();
138 	obj->what_is = WEAPON;
139 	obj->which_kind = ARROW;
140 	obj->quantity = get_rand(25, 35);
141 	obj->damage = "1d2";
142 	obj->hit_enchant = 0;
143 	obj->d_enchant = 0;
144 	obj->identified = 1;
145 	(void) add_to_pack(obj, &rogue.pack, 1);
146 }
147 
148 clean_up(estr)
149 char *estr;
150 {
151 	if (save_is_interactive) {
152 		if (init_curses) {
153 			move(DROWS-1, 0);
154 			refresh();
155 			stop_window();
156 		}
157 		printf("\n%s\n", estr);
158 	}
159 	md_exit(0);
160 }
161 
162 start_window()
163 {
164 	crmode();
165 	noecho();
166 #ifndef BAD_NONL
167 	nonl();
168 #endif
169 	md_control_keybord(0);
170 }
171 
172 stop_window()
173 {
174 	endwin();
175 	md_control_keybord(1);
176 }
177 
178 void
179 byebye()
180 {
181 	md_ignore_signals();
182 	if (ask_quit) {
183 		quit(1);
184 	} else {
185 		clean_up(byebye_string);
186 	}
187 	md_heed_signals();
188 }
189 
190 void
191 onintr()
192 {
193 	md_ignore_signals();
194 	if (cant_int) {
195 		did_int = 1;
196 	} else {
197 		check_message();
198 		message("interrupt", 1);
199 	}
200 	md_heed_signals();
201 }
202 
203 void
204 error_save()
205 {
206 	save_is_interactive = 0;
207 	save_into_file(error_file);
208 	clean_up("");
209 }
210 
211 do_args(argc, argv)
212 int argc;
213 char *argv[];
214 {
215 	short i, j;
216 
217 	for (i = 1; i < argc; i++) {
218 		if (argv[i][0] == '-') {
219 			for (j = 1; argv[i][j]; j++) {
220 				switch(argv[i][j]) {
221 				case 's':
222 					score_only = 1;
223 					break;
224 				}
225 			}
226 		} else {
227 			rest_file = argv[i];
228 		}
229 	}
230 }
231 
232 do_opts()
233 {
234 	char *eptr;
235 
236 	if (eptr = md_getenv("ROGUEOPTS")) {
237 		for (;;) {
238 			while ((*eptr) == ' ') {
239 				eptr++;
240 			}
241 			if (!(*eptr)) {
242 				break;
243 			}
244 			if (!strncmp(eptr, "fruit=", 6)) {
245 				eptr += 6;
246 				env_get_value(&fruit, eptr, 1);
247 			} else if (!strncmp(eptr, "file=", 5)) {
248 				eptr += 5;
249 				env_get_value(&save_file, eptr, 0);
250 			} else if (!strncmp(eptr, "jump", 4)) {
251 				jump = 1;
252 			} else if (!strncmp(eptr, "name=", 5)) {
253 				eptr += 5;
254 				env_get_value(&nick_name, eptr, 0);
255 			} else if (!strncmp(eptr, "noaskquit", 9)) {
256 				ask_quit = 0;
257 			} else if (!strncmp(eptr, "noskull", 5) ||
258 					!strncmp(eptr,"notomb", 6)) {
259 				no_skull = 1;
260 			} else if (!strncmp(eptr, "passgo", 5)) {
261 				passgo = 1;
262 			}
263 			while ((*eptr) && (*eptr != ',')) {
264 				eptr++;
265 			}
266 			if (!(*(eptr++))) {
267 				break;
268 			}
269 		}
270 	}
271 	/* If some strings have not been set through ROGUEOPTS, assign defaults
272 	 * to them so that the options editor has data to work with.
273 	 */
274 	init_str(&nick_name, login_name);
275 	init_str(&save_file, "rogue.save");
276 	init_str(&fruit, "slime-mold");
277 }
278 
279 env_get_value(s, e, add_blank)
280 char **s, *e;
281 boolean add_blank;
282 {
283 	short i = 0;
284 	char *t;
285 
286 	t = e;
287 
288 	while ((*e) && (*e != ',')) {
289 		if (*e == ':') {
290 			*e = ';';		/* ':' reserved for score file purposes */
291 		}
292 		e++;
293 		if (++i >= MAX_OPT_LEN) {
294 			break;
295 		}
296 	}
297 	*s = md_malloc(MAX_OPT_LEN + 2);
298 	(void) strncpy(*s, t, i);
299 	if (add_blank) {
300 		(*s)[i++] = ' ';
301 	}
302 	(*s)[i] = '\0';
303 }
304 
305 init_str(str, dflt)
306 char **str, *dflt;
307 {
308 	if (!(*str)) {
309 		*str = md_malloc(MAX_OPT_LEN + 2);
310 		(void) strcpy(*str, dflt);
311 	}
312 }
313