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