xref: /openbsd/games/hack/hack.u_init.c (revision 898184e3)
1 /*	$OpenBSD: hack.u_init.c,v 1.9 2009/10/27 23:59:25 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5  * Amsterdam
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * - Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * - 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  *
19  * - Neither the name of the Stichting Centrum voor Wiskunde en
20  * Informatica, nor the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <ctype.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <signal.h>
68 #include "hack.h"
69 #define	UNDEF_TYP	0
70 #define	UNDEF_SPE	'\177'
71 extern char plname[];
72 
73 struct you zerou;
74 char pl_character[PL_CSIZ];
75 char *(roles[]) = {	/* must all have distinct first letter */
76 			/* roles[4] may be changed to -woman */
77 	"Tourist", "Speleologist", "Fighter", "Knight",
78 	"Cave-man", "Wizard"
79 };
80 #define	NR_OF_ROLES	SIZE(roles)
81 char rolesyms[NR_OF_ROLES + 1];		/* filled by u_init() */
82 
83 struct trobj {
84 	uchar trotyp;
85 	schar trspe;
86 	char trolet;
87 	Bitfield(trquan,6);
88 	Bitfield(trknown,1);
89 };
90 
91 #ifdef WIZARD
92 struct trobj Extra_objs[] = {
93 	{ 0, 0, 0, 0, 0 },
94 	{ 0, 0, 0, 0, 0 }
95 };
96 #endif /* WIZARD */
97 
98 struct trobj Cave_man[] = {
99 	{ MACE, 1, WEAPON_SYM, 1, 1 },
100 	{ BOW, 1, WEAPON_SYM, 1, 1 },
101 	{ ARROW, 0, WEAPON_SYM, 25, 1 },	/* quan is variable */
102 	{ LEATHER_ARMOR, 0, ARMOR_SYM, 1, 1 },
103 	{ 0, 0, 0, 0, 0}
104 };
105 
106 struct trobj Fighter[] = {
107 	{ TWO_HANDED_SWORD, 0, WEAPON_SYM, 1, 1 },
108 	{ RING_MAIL, 0, ARMOR_SYM, 1, 1 },
109 	{ 0, 0, 0, 0, 0 }
110 };
111 
112 struct trobj Knight[] = {
113 	{ LONG_SWORD, 0, WEAPON_SYM, 1, 1 },
114 	{ SPEAR, 2, WEAPON_SYM, 1, 1 },
115 	{ RING_MAIL, 1, ARMOR_SYM, 1, 1 },
116 	{ HELMET, 0, ARMOR_SYM, 1, 1 },
117 	{ SHIELD, 0, ARMOR_SYM, 1, 1 },
118 	{ PAIR_OF_GLOVES, 0, ARMOR_SYM, 1, 1 },
119 	{ 0, 0, 0, 0, 0 }
120 };
121 
122 struct trobj Speleologist[] = {
123 	{ STUDDED_LEATHER_ARMOR, 0, ARMOR_SYM, 1, 1 },
124 	{ UNDEF_TYP, 0, POTION_SYM, 2, 0 },
125 	{ FOOD_RATION, 0, FOOD_SYM, 3, 1 },
126 	{ PICK_AXE, UNDEF_SPE, TOOL_SYM, 1, 0 },
127 	{ ICE_BOX, 0, TOOL_SYM, 1, 0 },
128 	{ 0, 0, 0, 0, 0}
129 };
130 
131 struct trobj Tinopener[] = {
132 	{ CAN_OPENER, 0, TOOL_SYM, 1, 1 },
133 	{ 0, 0, 0, 0, 0 }
134 };
135 
136 struct trobj Tourist[] = {
137 	{ UNDEF_TYP, 0, FOOD_SYM, 10, 1 },
138 	{ POT_EXTRA_HEALING, 0, POTION_SYM, 2, 0 },
139 	{ EXPENSIVE_CAMERA, 0, TOOL_SYM, 1, 1 },
140 	{ DART, 2, WEAPON_SYM, 25, 1 },	/* quan is variable */
141 	{ 0, 0, 0, 0, 0 }
142 };
143 
144 struct trobj Wizard[] = {
145 	{ ELVEN_CLOAK, 0, ARMOR_SYM, 1, 1 },
146 	{ UNDEF_TYP, UNDEF_SPE, WAND_SYM, 2, 0 },
147 	{ UNDEF_TYP, UNDEF_SPE, RING_SYM, 2, 0 },
148 	{ UNDEF_TYP, UNDEF_SPE, POTION_SYM, 2, 0 },
149 	{ UNDEF_TYP, UNDEF_SPE, SCROLL_SYM, 3, 0 },
150 	{ 0, 0, 0, 0, 0 }
151 };
152 
153 static void ini_inv(struct trobj *);
154 static int  role_index(char);
155 #ifdef WIZARD
156 static void wiz_inv(void);
157 #endif
158 
159 void
160 u_init()
161 {
162 	int i;
163 	char exper = 'y', pc;
164 
165 	if(flags.female)	/* should have been set in HACKOPTIONS */
166 		roles[4] = "Cave-woman";
167 	for(i = 0; i < NR_OF_ROLES; i++)
168 		rolesyms[i] = roles[i][0];
169 	rolesyms[i] = 0;
170 
171 	if ((pc = pl_character[0])) {
172 		if (islower(pc)) pc = toupper(pc);
173 		if ((i = role_index(pc)) >= 0)
174 			goto got_suffix;	/* implies experienced */
175 		printf("\nUnknown role: %c\n", pc);
176 		pl_character[0] = pc = 0;
177 	}
178 
179 	printf("\nAre you an experienced player? [ny] ");
180 
181 	while(!strchr("ynYN \n\004", (exper = readchar())))
182 		hackbell();
183 	if(exper == '\004')		/* Give him an opportunity to get out */
184 		end_of_input();
185 	printf("%c\n", exper);		/* echo */
186 	if(strchr("Nn \n", exper)) {
187 		exper = 0;
188 		goto beginner;
189 	}
190 
191 	printf("\nTell me what kind of character you are:\n");
192 	printf("Are you");
193 	for(i = 0; i < NR_OF_ROLES; i++) {
194 		printf(" a %s", roles[i]);
195 		if(i == 2)			/* %% */
196 			printf(",\n\t");
197 		else if(i < NR_OF_ROLES - 2)
198 			printf(",");
199 		else if(i == NR_OF_ROLES - 2)
200 			printf(" or");
201 	}
202 	printf("? [%s] ", rolesyms);
203 
204 	while ((pc = readchar())) {
205 		if(islower(pc)) pc = toupper(pc);
206 		if((i = role_index(pc)) >= 0) {
207 			printf("%c\n", pc);	/* echo */
208 			(void) fflush(stdout);	/* should be seen */
209 			break;
210 		}
211 		if(pc == '\n')
212 			break;
213 		if(pc == '\004')    /* Give him the opportunity to get out */
214 			end_of_input();
215 		hackbell();
216 	}
217 	if(pc == '\n')
218 		pc = 0;
219 
220 beginner:
221 	if(!pc) {
222 		printf("\nI'll choose a character for you.\n");
223 		i = rn2(NR_OF_ROLES);
224 		pc = rolesyms[i];
225 		printf("This game you will be a%s %s.\n",
226 			exper ? "n experienced" : "",
227 			roles[i]);
228 		getret();
229 		/* give him some feedback in case mklev takes much time */
230 		(void) putchar('\n');
231 		(void) fflush(stdout);
232 	}
233 #if 0
234 	/* Given the above code, I can't see why this would ever change
235 	   anything; it does core pretty well, though.  - cmh 4/20/93 */
236 	if(exper) {
237 		roles[i][0] = pc;
238 	}
239 #endif
240 
241 got_suffix:
242 
243 	(void) strlcpy(pl_character, roles[i], sizeof pl_character);
244 	flags.beginner = 1;
245 	u = zerou;
246 	u.usym = '@';
247 	u.ulevel = 1;
248 	init_uhunger();
249 #ifdef QUEST
250 	u.uhorizon = 6;
251 #endif /* QUEST */
252 	uarm = uarm2 = uarmh = uarms = uarmg = uwep = uball = uchain =
253 	uleft = uright = 0;
254 
255 	switch(pc) {
256 	case 'c':
257 	case 'C':
258 		Cave_man[2].trquan = 12 + rnd(9)*rnd(9);
259 		u.uhp = u.uhpmax = 16;
260 		u.ustr = u.ustrmax = 18;
261 		ini_inv(Cave_man);
262 		break;
263 	case 't':
264 	case 'T':
265 		Tourist[3].trquan = 20 + rnd(20);
266 		u.ugold = u.ugold0 = rnd(1000);
267 		u.uhp = u.uhpmax = 10;
268 		u.ustr = u.ustrmax = 8;
269 		ini_inv(Tourist);
270 		if(!rn2(25)) ini_inv(Tinopener);
271 		break;
272 	case 'w':
273 	case 'W':
274 		for(i=1; i<=4; i++) if(!rn2(5))
275 			Wizard[i].trquan += rn2(3) - 1;
276 		u.uhp = u.uhpmax = 15;
277 		u.ustr = u.ustrmax = 16;
278 		ini_inv(Wizard);
279 		break;
280 	case 's':
281 	case 'S':
282 		Fast = INTRINSIC;
283 		Stealth = INTRINSIC;
284 		u.uhp = u.uhpmax = 12;
285 		u.ustr = u.ustrmax = 10;
286 		ini_inv(Speleologist);
287 		if(!rn2(10)) ini_inv(Tinopener);
288 		break;
289 	case 'k':
290 	case 'K':
291 		u.uhp = u.uhpmax = 12;
292 		u.ustr = u.ustrmax = 10;
293 		ini_inv(Knight);
294 		break;
295 	case 'f':
296 	case 'F':
297 		u.uhp = u.uhpmax = 14;
298 		u.ustr = u.ustrmax = 17;
299 		ini_inv(Fighter);
300 		break;
301 	default:	/* impossible */
302 		u.uhp = u.uhpmax = 12;
303 		u.ustr = u.ustrmax = 16;
304 	}
305 	find_ac();
306 	if(!rn2(20)) {
307 		int d = rn2(7) - 2;	/* biased variation */
308 		u.ustr += d;
309 		u.ustrmax += d;
310 	}
311 
312 #ifdef WIZARD
313 	if(wizard) wiz_inv();
314 #endif /* WIZARD */
315 
316 	/* make sure he can carry all he has - especially for T's */
317 	while(inv_weight() > 0 && u.ustr < 118)
318 		u.ustr++, u.ustrmax++;
319 }
320 
321 static void
322 ini_inv(struct trobj *trop)
323 {
324 	struct obj *obj;
325 
326 	while(trop->trolet) {
327 		obj = mkobj(trop->trolet);
328 		obj->known = trop->trknown;
329 		/* not obj->dknown = 1; - let him look at it at least once */
330 		obj->cursed = 0;
331 		if(obj->olet == WEAPON_SYM){
332 			obj->quan = trop->trquan;
333 			trop->trquan = 1;
334 		}
335 		if(trop->trspe != UNDEF_SPE)
336 			obj->spe = trop->trspe;
337 		if(trop->trotyp != UNDEF_TYP)
338 			obj->otyp = trop->trotyp;
339 		else
340 			if(obj->otyp == WAN_WISHING)	/* gitpyr!robert */
341 				obj->otyp = WAN_DEATH;
342 		obj->owt = weight(obj);	/* defined after setting otyp+quan */
343 		obj = addinv(obj);
344 		if(obj->olet == ARMOR_SYM){
345 			switch(obj->otyp){
346 			case SHIELD:
347 				if(!uarms) setworn(obj, W_ARMS);
348 				break;
349 			case HELMET:
350 				if(!uarmh) setworn(obj, W_ARMH);
351 				break;
352 			case PAIR_OF_GLOVES:
353 				if(!uarmg) setworn(obj, W_ARMG);
354 				break;
355 			case ELVEN_CLOAK:
356 				if(!uarm2)
357 					setworn(obj, W_ARM);
358 				break;
359 			default:
360 				if(!uarm) setworn(obj, W_ARM);
361 			}
362 		}
363 		if(obj->olet == WEAPON_SYM)
364 			if(!uwep) setuwep(obj);
365 #ifndef PYRAMID_BUG
366 		if(--trop->trquan) continue;	/* make a similar object */
367 #else
368 		if(trop->trquan) {		/* check if zero first */
369 			--trop->trquan;
370 			if(trop->trquan)
371 				continue;	/* make a similar object */
372 		}
373 #endif /* PYRAMID_BUG */
374 		trop++;
375 	}
376 }
377 
378 #ifdef WIZARD
379 static void
380 wiz_inv()
381 {
382 	struct trobj *trop = &Extra_objs[0];
383 	char *ep = getenv("INVENT");
384 	int type;
385 
386 	while(ep && *ep) {
387 		type = atoi(ep);
388 		ep = strchr(ep, ',');
389 		if(ep) while(*ep == ',' || *ep == ' ') ep++;
390 		if(type <= 0 || type > NROFOBJECTS) continue;
391 		trop->trotyp = type;
392 		trop->trolet = objects[type].oc_olet;
393 		trop->trspe = 4;
394 		trop->trknown = 1;
395 		trop->trquan = 1;
396 		ini_inv(trop);
397 	}
398 	/* give him a wand of wishing by default */
399 	trop->trotyp = WAN_WISHING;
400 	trop->trolet = WAND_SYM;
401 	trop->trspe = 20;
402 	trop->trknown = 1;
403 	trop->trquan = 1;
404 	ini_inv(trop);
405 }
406 #endif /* WIZARD */
407 
408 void
409 plnamesuffix()
410 {
411 	char *p;
412 
413 	if ((p = strrchr(plname, '-'))) {
414 		*p = 0;
415 		pl_character[0] = p[1];
416 		pl_character[1] = 0;
417 		if(!plname[0]) {
418 			askname();
419 			plnamesuffix();
420 		}
421 	}
422 }
423 
424 /* must be called only from u_init() */
425 /* so that rolesyms[] is defined */
426 static int
427 role_index(char pc)
428 {
429 	char *cp;
430 
431 	if ((cp = strchr(rolesyms, pc)))
432 		return(cp - rolesyms);
433 	return(-1);
434 }
435