1 /* $NetBSD: init.c,v 1.10 1999/09/12 09:02:23 jsm 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.10 1999/09/12 09:02:23 jsm 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 <fcntl.h> 61 62 #include "rogue.h" 63 64 char login_name[MAX_OPT_LEN]; 65 char *nick_name = (char *) 0; 66 char *rest_file = 0; 67 boolean cant_int = 0; 68 boolean did_int = 0; 69 boolean score_only; 70 boolean init_curses = 0; 71 boolean save_is_interactive = 1; 72 boolean ask_quit = 1; 73 boolean no_skull = 0; 74 boolean passgo = 0; 75 const char *error_file = "rogue.esave"; 76 const char *byebye_string = "Okay, bye bye!"; 77 gid_t gid, egid; 78 79 int 80 init(argc, argv) 81 int argc; 82 char *argv[]; 83 { 84 const char *pn; 85 int seed; 86 int fd; 87 88 gid = getgid(); 89 egid = getegid(); 90 setegid(gid); 91 /* Check for dirty tricks with closed fds 0, 1, 2 */ 92 fd = open("/dev/null", O_RDONLY); 93 if (fd < 3) 94 exit(1); 95 close(fd); 96 97 seed = 0; 98 pn = md_gln(); 99 if ((!pn) || (strlen(pn) >= MAX_OPT_LEN)) { 100 clean_up("Hey! Who are you?"); 101 } 102 (void) strcpy(login_name, pn); 103 104 do_args(argc, argv); 105 do_opts(); 106 107 if (!score_only && !rest_file) { 108 printf("Hello %s, just a moment while I dig the dungeon...", 109 nick_name); 110 fflush(stdout); 111 } 112 113 initscr(); 114 if ((LINES < DROWS) || (COLS < DCOLS)) { 115 clean_up("must be played on 24 x 80 screen"); 116 } 117 start_window(); 118 init_curses = 1; 119 120 md_heed_signals(); 121 122 if (score_only) { 123 put_scores((object *) 0, 0); 124 } 125 seed = md_gseed(); 126 (void) srrandom(seed); 127 if (rest_file) { 128 restore(rest_file); 129 return(1); 130 } 131 mix_colors(); 132 get_wand_and_ring_materials(); 133 make_scroll_titles(); 134 135 level_objects.next_object = (object *) 0; 136 level_monsters.next_monster = (object *) 0; 137 player_init(); 138 ring_stats(0); 139 return(0); 140 } 141 142 void 143 player_init() 144 { 145 object *obj; 146 147 rogue.pack.next_object = (object *) 0; 148 149 obj = alloc_object(); 150 get_food(obj, 1); 151 (void) add_to_pack(obj, &rogue.pack, 1); 152 153 obj = alloc_object(); /* initial armor */ 154 obj->what_is = ARMOR; 155 obj->which_kind = RINGMAIL; 156 obj->class = RINGMAIL+2; 157 obj->is_protected = 0; 158 obj->d_enchant = 1; 159 (void) add_to_pack(obj, &rogue.pack, 1); 160 do_wear(obj); 161 162 obj = alloc_object(); /* initial weapons */ 163 obj->what_is = WEAPON; 164 obj->which_kind = MACE; 165 obj->damage = "2d3"; 166 obj->hit_enchant = obj->d_enchant = 1; 167 obj->identified = 1; 168 (void) add_to_pack(obj, &rogue.pack, 1); 169 do_wield(obj); 170 171 obj = alloc_object(); 172 obj->what_is = WEAPON; 173 obj->which_kind = BOW; 174 obj->damage = "1d2"; 175 obj->hit_enchant = 1; 176 obj->d_enchant = 0; 177 obj->identified = 1; 178 (void) add_to_pack(obj, &rogue.pack, 1); 179 180 obj = alloc_object(); 181 obj->what_is = WEAPON; 182 obj->which_kind = ARROW; 183 obj->quantity = get_rand(25, 35); 184 obj->damage = "1d2"; 185 obj->hit_enchant = 0; 186 obj->d_enchant = 0; 187 obj->identified = 1; 188 (void) add_to_pack(obj, &rogue.pack, 1); 189 } 190 191 void 192 clean_up(estr) 193 const char *estr; 194 { 195 if (save_is_interactive) { 196 if (init_curses) { 197 move(DROWS-1, 0); 198 refresh(); 199 stop_window(); 200 } 201 printf("\n%s\n", estr); 202 } 203 md_exit(0); 204 } 205 206 void 207 start_window() 208 { 209 crmode(); 210 noecho(); 211 #ifndef BAD_NONL 212 nonl(); 213 #endif 214 } 215 216 void 217 stop_window() 218 { 219 endwin(); 220 } 221 222 void 223 byebye(dummy) 224 int dummy __attribute__((__unused__)); 225 { 226 md_ignore_signals(); 227 if (ask_quit) { 228 quit(1); 229 } else { 230 clean_up(byebye_string); 231 } 232 md_heed_signals(); 233 } 234 235 void 236 onintr(dummy) 237 int dummy __attribute__((__unused__)); 238 { 239 md_ignore_signals(); 240 if (cant_int) { 241 did_int = 1; 242 } else { 243 check_message(); 244 message("interrupt", 1); 245 } 246 md_heed_signals(); 247 } 248 249 void 250 error_save(dummy) 251 int dummy __attribute__((__unused__)); 252 { 253 save_is_interactive = 0; 254 save_into_file(error_file); 255 clean_up(""); 256 } 257 258 void 259 do_args(argc, argv) 260 int argc; 261 char *argv[]; 262 { 263 short i, j; 264 265 for (i = 1; i < argc; i++) { 266 if (argv[i][0] == '-') { 267 for (j = 1; argv[i][j]; j++) { 268 switch(argv[i][j]) { 269 case 's': 270 score_only = 1; 271 break; 272 } 273 } 274 } else { 275 rest_file = argv[i]; 276 } 277 } 278 } 279 280 void 281 do_opts() 282 { 283 char *eptr; 284 285 if ((eptr = md_getenv("ROGUEOPTS")) != NULL) { 286 for (;;) { 287 while ((*eptr) == ' ') { 288 eptr++; 289 } 290 if (!(*eptr)) { 291 break; 292 } 293 if (!strncmp(eptr, "fruit=", 6)) { 294 eptr += 6; 295 env_get_value(&fruit, eptr, 1); 296 } else if (!strncmp(eptr, "file=", 5)) { 297 eptr += 5; 298 env_get_value(&save_file, eptr, 0); 299 } else if (!strncmp(eptr, "jump", 4)) { 300 jump = 1; 301 } else if (!strncmp(eptr, "name=", 5)) { 302 eptr += 5; 303 env_get_value(&nick_name, eptr, 0); 304 } else if (!strncmp(eptr, "noaskquit", 9)) { 305 ask_quit = 0; 306 } else if (!strncmp(eptr, "noskull", 5) || 307 !strncmp(eptr,"notomb", 6)) { 308 no_skull = 1; 309 } else if (!strncmp(eptr, "passgo", 5)) { 310 passgo = 1; 311 } 312 while ((*eptr) && (*eptr != ',')) { 313 eptr++; 314 } 315 if (!(*(eptr++))) { 316 break; 317 } 318 } 319 } 320 /* If some strings have not been set through ROGUEOPTS, assign defaults 321 * to them so that the options editor has data to work with. 322 */ 323 init_str(&nick_name, login_name); 324 init_str(&save_file, "rogue.save"); 325 init_str(&fruit, "slime-mold"); 326 } 327 328 void 329 env_get_value(s, e, add_blank) 330 char **s, *e; 331 boolean add_blank; 332 { 333 short i = 0; 334 const char *t; 335 336 t = e; 337 338 while ((*e) && (*e != ',')) { 339 if (*e == ':') { 340 *e = ';'; /* ':' reserved for score file purposes */ 341 } 342 e++; 343 if (++i >= MAX_OPT_LEN) { 344 break; 345 } 346 } 347 *s = md_malloc(MAX_OPT_LEN + 2); 348 if (*s == NULL) 349 clean_up("out of memory"); 350 (void) strncpy(*s, t, i); 351 if (add_blank) { 352 (*s)[i++] = ' '; 353 } 354 (*s)[i] = '\0'; 355 } 356 357 void 358 init_str(str, dflt) 359 char **str; 360 const char *dflt; 361 { 362 if (!(*str)) { 363 *str = md_malloc(MAX_OPT_LEN + 2); 364 if (*str == NULL) 365 clean_up("out of memory"); 366 (void) strcpy(*str, dflt); 367 } 368 } 369