Merge from vendor branch OPENSSH:
[dragonfly.git] / games / rogue / init.c
CommitLineData
984263bc
MD
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.
1de703da
MD
35 *
36 * @(#)init.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/rogue/init.c,v 1.4 1999/11/30 03:49:22 billf Exp $
38 * $DragonFly: src/games/rogue/init.c,v 1.2 2003/06/17 04:25:24 dillon Exp $
984263bc
MD
39 */
40
984263bc
MD
41/*
42 * init.c
43 *
44 * This source herein may be modified and/or distributed by anybody who
45 * so desires, with the following restrictions:
46 * 1.) No portion of this notice shall be removed.
47 * 2.) Credit shall not be taken for the creation of this source.
48 * 3.) This code is not to be traded, sold, or used for personal
49 * gain or profit.
50 *
51 */
52
53#include <stdio.h>
54#include "rogue.h"
55
56char login_name[MAX_OPT_LEN];
57char *nick_name = (char *) 0;
58char *rest_file = 0;
59boolean cant_int = 0;
60boolean did_int = 0;
61boolean score_only;
62boolean init_curses = 0;
63boolean save_is_interactive = 1;
64boolean ask_quit = 1;
65boolean no_skull = 0;
66boolean passgo = 0;
67boolean flush = 1;
68const char *error_file = "rogue.esave";
69const char *byebye_string = "Okay, bye bye!";
70
71extern char *fruit;
72extern char *save_file;
73extern short party_room;
74extern boolean jump;
75
76init(argc, argv)
77int argc;
78char *argv[];
79{
80 const char *pn;
81 int seed;
82
83 pn = md_gln();
84 if ((!pn) || (strlen(pn) >= MAX_OPT_LEN)) {
85 clean_up("Hey! Who are you?");
86 }
87 (void) strcpy(login_name, pn);
88
89 do_args(argc, argv);
90 do_opts();
91
92 if (!score_only && !rest_file) {
93 printf("Hello %s, just a moment while I dig the dungeon...",
94 nick_name);
95 fflush(stdout);
96 }
97
98 initscr();
99 if ((LINES < DROWS) || (COLS < DCOLS)) {
100 clean_up("must be played on 24 x 80 screen");
101 }
102 start_window();
103 init_curses = 1;
104
105 md_heed_signals();
106
107 if (score_only) {
108 put_scores((object *) 0, 0);
109 }
110 seed = md_gseed();
111 (void) srrandom(seed);
112 if (rest_file) {
113 restore(rest_file);
114 return(1);
115 }
116 mix_colors();
117 get_wand_and_ring_materials();
118 make_scroll_titles();
119
120 level_objects.next_object = (object *) 0;
121 level_monsters.next_monster = (object *) 0;
122 player_init();
123 ring_stats(0);
124 return(0);
125}
126
127player_init()
128{
129 object *obj;
130
131 rogue.pack.next_object = (object *) 0;
132
133 obj = alloc_object();
134 get_food(obj, 1);
135 (void) add_to_pack(obj, &rogue.pack, 1);
136
137 obj = alloc_object(); /* initial armor */
138 obj->what_is = ARMOR;
139 obj->which_kind = RINGMAIL;
140 obj->class = RINGMAIL+2;
141 obj->is_protected = 0;
142 obj->d_enchant = 1;
143 (void) add_to_pack(obj, &rogue.pack, 1);
144 do_wear(obj);
145
146 obj = alloc_object(); /* initial weapons */
147 obj->what_is = WEAPON;
148 obj->which_kind = MACE;
149 obj->damage = "2d3";
150 obj->hit_enchant = obj->d_enchant = 1;
151 obj->identified = 1;
152 (void) add_to_pack(obj, &rogue.pack, 1);
153 do_wield(obj);
154
155 obj = alloc_object();
156 obj->what_is = WEAPON;
157 obj->which_kind = BOW;
158 obj->damage = "1d2";
159 obj->hit_enchant = 1;
160 obj->d_enchant = 0;
161 obj->identified = 1;
162 (void) add_to_pack(obj, &rogue.pack, 1);
163
164 obj = alloc_object();
165 obj->what_is = WEAPON;
166 obj->which_kind = ARROW;
167 obj->quantity = get_rand(25, 35);
168 obj->damage = "1d2";
169 obj->hit_enchant = 0;
170 obj->d_enchant = 0;
171 obj->identified = 1;
172 (void) add_to_pack(obj, &rogue.pack, 1);
173}
174
175clean_up(estr)
176const char *estr;
177{
178 if (save_is_interactive) {
179 if (init_curses) {
180 move(DROWS-1, 0);
181 refresh();
182 stop_window();
183 }
184 printf("\n%s\n", estr);
185 }
186 md_exit(0);
187}
188
189start_window()
190{
191 crmode();
192 noecho();
193#ifndef BAD_NONL
194 nonl();
195#endif
196 md_control_keybord(0);
197}
198
199stop_window()
200{
201 endwin();
202 md_control_keybord(1);
203}
204
205void
206byebye()
207{
208 md_ignore_signals();
209 if (ask_quit) {
210 quit(1);
211 } else {
212 clean_up(byebye_string);
213 }
214 md_heed_signals();
215}
216
217void
218onintr()
219{
220 md_ignore_signals();
221 if (cant_int) {
222 did_int = 1;
223 } else {
224 check_message();
225 message("interrupt", 1);
226 }
227 md_heed_signals();
228}
229
230void
231error_save()
232{
233 save_is_interactive = 0;
234 save_into_file(error_file);
235 clean_up("");
236}
237
238do_args(argc, argv)
239int argc;
240char *argv[];
241{
242 short i, j;
243
244 for (i = 1; i < argc; i++) {
245 if (argv[i][0] == '-') {
246 for (j = 1; argv[i][j]; j++) {
247 switch(argv[i][j]) {
248 case 's':
249 score_only = 1;
250 break;
251 }
252 }
253 } else {
254 rest_file = argv[i];
255 }
256 }
257}
258
259do_opts()
260{
261 char *eptr;
262
263 if (eptr = md_getenv("ROGUEOPTS")) {
264 for (;;) {
265 while ((*eptr) == ' ') {
266 eptr++;
267 }
268 if (!(*eptr)) {
269 break;
270 }
271 if (!strncmp(eptr, "fruit=", 6)) {
272 eptr += 6;
273 env_get_value(&fruit, eptr, 1);
274 } else if (!strncmp(eptr, "file=", 5)) {
275 eptr += 5;
276 env_get_value(&save_file, eptr, 0);
277 } else if (!strncmp(eptr, "jump", 4)) {
278 jump = 1;
279 } else if (!strncmp(eptr, "name=", 5)) {
280 eptr += 5;
281 env_get_value(&nick_name, eptr, 0);
282 } else if (!strncmp(eptr, "noaskquit", 9)) {
283 ask_quit = 0;
284 } else if (!strncmp(eptr, "noskull", 7) ||
285 !strncmp(eptr,"notomb", 6)) {
286 no_skull = 1;
287 } else if (!strncmp(eptr, "passgo", 6)) {
288 passgo = 1;
289 } else if (!strncmp(eptr, "noflush", 7)) {
290 flush = 0;
291 }
292 while ((*eptr) && (*eptr != ',')) {
293 eptr++;
294 }
295 if (!(*(eptr++))) {
296 break;
297 }
298 }
299 }
300 /* If some strings have not been set through ROGUEOPTS, assign defaults
301 * to them so that the options editor has data to work with.
302 */
303 init_str(&nick_name, login_name);
304 init_str(&save_file, "rogue.save");
305 init_str(&fruit, "slime-mold");
306}
307
308env_get_value(s, e, add_blank)
309char **s, *e;
310boolean add_blank;
311{
312 short i = 0;
313 const char *t;
314
315 t = e;
316
317 while ((*e) && (*e != ',')) {
318 if (*e == ':') {
319 *e = ';'; /* ':' reserved for score file purposes */
320 }
321 e++;
322 if (++i >= MAX_OPT_LEN) {
323 break;
324 }
325 }
326 *s = md_malloc(MAX_OPT_LEN + 2);
327 (void) strncpy(*s, t, i);
328 if (add_blank) {
329 (*s)[i++] = ' ';
330 }
331 (*s)[i] = '\0';
332}
333
334init_str(str, dflt)
335char **str;
336const char *dflt;
337{
338 if (!(*str)) {
339 *str = md_malloc(MAX_OPT_LEN + 2);
340 (void) strcpy(*str, dflt);
341 }
342}