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