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