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