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