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