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