xref: /csrg-svn/games/rogue/init.c (revision 60842)
132689Sbostic /*
2*60842Sbostic  * Copyright (c) 1988, 1993
3*60842Sbostic  *	The Regents of the University of California.  All rights reserved.
436704Sbostic  *
536704Sbostic  * This code is derived from software contributed to Berkeley by
636704Sbostic  * Timothy C. Stoehr.
736704Sbostic  *
842592Sbostic  * %sccs.include.redist.c%
936704Sbostic  */
1036704Sbostic 
1136704Sbostic #ifndef lint
12*60842Sbostic static char sccsid[] = "@(#)init.c	8.1 (Berkeley) 05/31/93";
1336704Sbostic #endif /* not lint */
1436704Sbostic 
1536704Sbostic /*
1632689Sbostic  * init.c
1732689Sbostic  *
1832689Sbostic  * This source herein may be modified and/or distributed by anybody who
1932689Sbostic  * so desires, with the following restrictions:
2032689Sbostic  *    1.)  No portion of this notice shall be removed.
2132689Sbostic  *    2.)  Credit shall not be taken for the creation of this source.
2232689Sbostic  *    3.)  This code is not to be traded, sold, or used for personal
2332689Sbostic  *         gain or profit.
2432689Sbostic  *
2532689Sbostic  */
2632689Sbostic 
2732689Sbostic #include <stdio.h>
2832689Sbostic #include "rogue.h"
2932689Sbostic 
3032689Sbostic char login_name[MAX_OPT_LEN];
3132689Sbostic char *nick_name = (char *) 0;
3232689Sbostic char *rest_file = 0;
3332689Sbostic boolean cant_int = 0;
3432689Sbostic boolean did_int = 0;
3532689Sbostic boolean score_only;
3632689Sbostic boolean init_curses = 0;
3732689Sbostic boolean save_is_interactive = 1;
3832689Sbostic boolean ask_quit = 1;
3932689Sbostic boolean no_skull = 0;
4032689Sbostic boolean passgo = 0;
4132689Sbostic char *error_file = "rogue.esave";
4232689Sbostic char *byebye_string = "Okay, bye bye!";
4332689Sbostic 
4432689Sbostic extern char *fruit;
4532689Sbostic extern char *save_file;
4632689Sbostic extern short party_room;
4732689Sbostic extern boolean jump;
4832689Sbostic 
init(argc,argv)4932689Sbostic init(argc, argv)
5032689Sbostic int argc;
5132689Sbostic char *argv[];
5232689Sbostic {
5332689Sbostic 	char *pn;
5432689Sbostic 	int seed;
5532689Sbostic 
5632689Sbostic 	pn = md_gln();
5732689Sbostic 	if ((!pn) || (strlen(pn) >= MAX_OPT_LEN)) {
5832689Sbostic 		clean_up("Hey!  Who are you?");
5932689Sbostic 	}
6032689Sbostic 	(void) strcpy(login_name, pn);
6132689Sbostic 
6232689Sbostic 	do_args(argc, argv);
6332689Sbostic 	do_opts();
6432689Sbostic 
6532689Sbostic 	if (!score_only && !rest_file) {
6632689Sbostic 		printf("Hello %s, just a moment while I dig the dungeon...",
6732689Sbostic 			nick_name);
6832689Sbostic 		fflush(stdout);
6932689Sbostic 	}
7032689Sbostic 
7132689Sbostic 	initscr();
7232689Sbostic 	if ((LINES < DROWS) || (COLS < DCOLS)) {
7332689Sbostic 		clean_up("must be played on 24 x 80 screen");
7432689Sbostic 	}
7532689Sbostic 	start_window();
7632689Sbostic 	init_curses = 1;
7732689Sbostic 
7832689Sbostic 	md_heed_signals();
7932689Sbostic 
8032689Sbostic 	if (score_only) {
8132689Sbostic 		put_scores((object *) 0, 0);
8232689Sbostic 	}
8332689Sbostic 	seed = md_gseed();
8432689Sbostic 	(void) srrandom(seed);
8532689Sbostic 	if (rest_file) {
8632689Sbostic 		restore(rest_file);
8732689Sbostic 		return(1);
8832689Sbostic 	}
8932689Sbostic 	mix_colors();
9032689Sbostic 	get_wand_and_ring_materials();
9132689Sbostic 	make_scroll_titles();
9232689Sbostic 
9332689Sbostic 	level_objects.next_object = (object *) 0;
9432689Sbostic 	level_monsters.next_monster = (object *) 0;
9532689Sbostic 	player_init();
9632689Sbostic 	ring_stats(0);
9732689Sbostic 	return(0);
9832689Sbostic }
9932689Sbostic 
player_init()10032689Sbostic player_init()
10132689Sbostic {
10232689Sbostic 	object *obj;
10332689Sbostic 
10432689Sbostic 	rogue.pack.next_object = (object *) 0;
10532689Sbostic 
10632689Sbostic 	obj = alloc_object();
10732689Sbostic 	get_food(obj, 1);
10832689Sbostic 	(void) add_to_pack(obj, &rogue.pack, 1);
10932689Sbostic 
11032689Sbostic 	obj = alloc_object();		/* initial armor */
11132689Sbostic 	obj->what_is = ARMOR;
11232689Sbostic 	obj->which_kind = RINGMAIL;
11332689Sbostic 	obj->class = RINGMAIL+2;
11432689Sbostic 	obj->is_protected = 0;
11532689Sbostic 	obj->d_enchant = 1;
11632689Sbostic 	(void) add_to_pack(obj, &rogue.pack, 1);
11732689Sbostic 	do_wear(obj);
11832689Sbostic 
11932689Sbostic 	obj = alloc_object();		/* initial weapons */
12032689Sbostic 	obj->what_is = WEAPON;
12132689Sbostic 	obj->which_kind = MACE;
12232689Sbostic 	obj->damage = "2d3";
12332689Sbostic 	obj->hit_enchant = obj->d_enchant = 1;
12432689Sbostic 	obj->identified = 1;
12532689Sbostic 	(void) add_to_pack(obj, &rogue.pack, 1);
12632689Sbostic 	do_wield(obj);
12732689Sbostic 
12832689Sbostic 	obj = alloc_object();
12932689Sbostic 	obj->what_is = WEAPON;
13032689Sbostic 	obj->which_kind = BOW;
13132689Sbostic 	obj->damage = "1d2";
13232689Sbostic 	obj->hit_enchant = 1;
13332689Sbostic 	obj->d_enchant = 0;
13432689Sbostic 	obj->identified = 1;
13532689Sbostic 	(void) add_to_pack(obj, &rogue.pack, 1);
13632689Sbostic 
13732689Sbostic 	obj = alloc_object();
13832689Sbostic 	obj->what_is = WEAPON;
13932689Sbostic 	obj->which_kind = ARROW;
14032689Sbostic 	obj->quantity = get_rand(25, 35);
14132689Sbostic 	obj->damage = "1d2";
14232689Sbostic 	obj->hit_enchant = 0;
14332689Sbostic 	obj->d_enchant = 0;
14432689Sbostic 	obj->identified = 1;
14532689Sbostic 	(void) add_to_pack(obj, &rogue.pack, 1);
14632689Sbostic }
14732689Sbostic 
clean_up(estr)14832689Sbostic clean_up(estr)
14932689Sbostic char *estr;
15032689Sbostic {
15132689Sbostic 	if (save_is_interactive) {
15232689Sbostic 		if (init_curses) {
15332689Sbostic 			move(DROWS-1, 0);
15432689Sbostic 			refresh();
15532689Sbostic 			stop_window();
15632689Sbostic 		}
15732689Sbostic 		printf("\n%s\n", estr);
15832689Sbostic 	}
15932689Sbostic 	md_exit(0);
16032689Sbostic }
16132689Sbostic 
start_window()16232689Sbostic start_window()
16332689Sbostic {
16432689Sbostic 	crmode();
16532689Sbostic 	noecho();
16632689Sbostic #ifndef BAD_NONL
16732689Sbostic 	nonl();
16832689Sbostic #endif
16932689Sbostic 	md_control_keybord(0);
17032689Sbostic }
17132689Sbostic 
stop_window()17232689Sbostic stop_window()
17332689Sbostic {
17432689Sbostic 	endwin();
17532689Sbostic 	md_control_keybord(1);
17632689Sbostic }
17732689Sbostic 
17846758Sbostic void
byebye()17932689Sbostic byebye()
18032689Sbostic {
18132689Sbostic 	md_ignore_signals();
18232689Sbostic 	if (ask_quit) {
18332689Sbostic 		quit(1);
18432689Sbostic 	} else {
18532689Sbostic 		clean_up(byebye_string);
18632689Sbostic 	}
18732689Sbostic 	md_heed_signals();
18832689Sbostic }
18932689Sbostic 
19046758Sbostic void
onintr()19132689Sbostic onintr()
19232689Sbostic {
19332689Sbostic 	md_ignore_signals();
19432689Sbostic 	if (cant_int) {
19532689Sbostic 		did_int = 1;
19632689Sbostic 	} else {
19732689Sbostic 		check_message();
19832689Sbostic 		message("interrupt", 1);
19932689Sbostic 	}
20032689Sbostic 	md_heed_signals();
20132689Sbostic }
20232689Sbostic 
20346758Sbostic void
error_save()20432689Sbostic error_save()
20532689Sbostic {
20632689Sbostic 	save_is_interactive = 0;
20732689Sbostic 	save_into_file(error_file);
20832689Sbostic 	clean_up("");
20932689Sbostic }
21032689Sbostic 
do_args(argc,argv)21132689Sbostic do_args(argc, argv)
21232689Sbostic int argc;
21332689Sbostic char *argv[];
21432689Sbostic {
21532689Sbostic 	short i, j;
21632689Sbostic 
21732689Sbostic 	for (i = 1; i < argc; i++) {
21832689Sbostic 		if (argv[i][0] == '-') {
21932689Sbostic 			for (j = 1; argv[i][j]; j++) {
22032689Sbostic 				switch(argv[i][j]) {
22132689Sbostic 				case 's':
22232689Sbostic 					score_only = 1;
22332689Sbostic 					break;
22432689Sbostic 				}
22532689Sbostic 			}
22632689Sbostic 		} else {
22732689Sbostic 			rest_file = argv[i];
22832689Sbostic 		}
22932689Sbostic 	}
23032689Sbostic }
23132689Sbostic 
do_opts()23232689Sbostic do_opts()
23332689Sbostic {
23432689Sbostic 	char *eptr;
23532689Sbostic 
23632689Sbostic 	if (eptr = md_getenv("ROGUEOPTS")) {
23732689Sbostic 		for (;;) {
23832689Sbostic 			while ((*eptr) == ' ') {
23932689Sbostic 				eptr++;
24032689Sbostic 			}
24132689Sbostic 			if (!(*eptr)) {
24232689Sbostic 				break;
24332689Sbostic 			}
24432689Sbostic 			if (!strncmp(eptr, "fruit=", 6)) {
24532689Sbostic 				eptr += 6;
24632689Sbostic 				env_get_value(&fruit, eptr, 1);
24732689Sbostic 			} else if (!strncmp(eptr, "file=", 5)) {
24832689Sbostic 				eptr += 5;
24932689Sbostic 				env_get_value(&save_file, eptr, 0);
25032689Sbostic 			} else if (!strncmp(eptr, "jump", 4)) {
25132689Sbostic 				jump = 1;
25232689Sbostic 			} else if (!strncmp(eptr, "name=", 5)) {
25332689Sbostic 				eptr += 5;
25432689Sbostic 				env_get_value(&nick_name, eptr, 0);
25532689Sbostic 			} else if (!strncmp(eptr, "noaskquit", 9)) {
25632689Sbostic 				ask_quit = 0;
25732689Sbostic 			} else if (!strncmp(eptr, "noskull", 5) ||
25832689Sbostic 					!strncmp(eptr,"notomb", 6)) {
25932689Sbostic 				no_skull = 1;
26032689Sbostic 			} else if (!strncmp(eptr, "passgo", 5)) {
26132689Sbostic 				passgo = 1;
26232689Sbostic 			}
26332689Sbostic 			while ((*eptr) && (*eptr != ',')) {
26432689Sbostic 				eptr++;
26532689Sbostic 			}
26632689Sbostic 			if (!(*(eptr++))) {
26732689Sbostic 				break;
26832689Sbostic 			}
26932689Sbostic 		}
27032689Sbostic 	}
27132689Sbostic 	/* If some strings have not been set through ROGUEOPTS, assign defaults
27232689Sbostic 	 * to them so that the options editor has data to work with.
27332689Sbostic 	 */
27432689Sbostic 	init_str(&nick_name, login_name);
27532689Sbostic 	init_str(&save_file, "rogue.save");
27632689Sbostic 	init_str(&fruit, "slime-mold");
27732689Sbostic }
27832689Sbostic 
env_get_value(s,e,add_blank)27932689Sbostic env_get_value(s, e, add_blank)
28032689Sbostic char **s, *e;
28132689Sbostic boolean add_blank;
28232689Sbostic {
28332689Sbostic 	short i = 0;
28432689Sbostic 	char *t;
28532689Sbostic 
28632689Sbostic 	t = e;
28732689Sbostic 
28832689Sbostic 	while ((*e) && (*e != ',')) {
28932689Sbostic 		if (*e == ':') {
29032689Sbostic 			*e = ';';		/* ':' reserved for score file purposes */
29132689Sbostic 		}
29232689Sbostic 		e++;
29332689Sbostic 		if (++i >= MAX_OPT_LEN) {
29432689Sbostic 			break;
29532689Sbostic 		}
29632689Sbostic 	}
29732689Sbostic 	*s = md_malloc(MAX_OPT_LEN + 2);
29832689Sbostic 	(void) strncpy(*s, t, i);
29932689Sbostic 	if (add_blank) {
30032689Sbostic 		(*s)[i++] = ' ';
30132689Sbostic 	}
30232689Sbostic 	(*s)[i] = '\0';
30332689Sbostic }
30432689Sbostic 
init_str(str,dflt)30532689Sbostic init_str(str, dflt)
30632689Sbostic char **str, *dflt;
30732689Sbostic {
30832689Sbostic 	if (!(*str)) {
30932689Sbostic 		*str = md_malloc(MAX_OPT_LEN + 2);
31032689Sbostic 		(void) strcpy(*str, dflt);
31132689Sbostic 	}
31232689Sbostic }
313