xref: /openbsd-src/games/hack/hack.u_init.c (revision 8a97e63eab912d5b18f18e99c3b87ded87ffc48b)
1 /*	$OpenBSD: hack.u_init.c,v 1.10 2015/10/24 17:40:38 mmcc 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((unsigned char)pc))
173 			pc = toupper((unsigned char)pc);
174 		if ((i = role_index(pc)) >= 0)
175 			goto got_suffix;	/* implies experienced */
176 		printf("\nUnknown role: %c\n", pc);
177 		pl_character[0] = pc = 0;
178 	}
179 
180 	printf("\nAre you an experienced player? [ny] ");
181 
182 	while(!strchr("ynYN \n\004", (exper = readchar())))
183 		hackbell();
184 	if(exper == '\004')		/* Give him an opportunity to get out */
185 		end_of_input();
186 	printf("%c\n", exper);		/* echo */
187 	if(strchr("Nn \n", exper)) {
188 		exper = 0;
189 		goto beginner;
190 	}
191 
192 	printf("\nTell me what kind of character you are:\n");
193 	printf("Are you");
194 	for(i = 0; i < NR_OF_ROLES; i++) {
195 		printf(" a %s", roles[i]);
196 		if(i == 2)			/* %% */
197 			printf(",\n\t");
198 		else if(i < NR_OF_ROLES - 2)
199 			printf(",");
200 		else if(i == NR_OF_ROLES - 2)
201 			printf(" or");
202 	}
203 	printf("? [%s] ", rolesyms);
204 
205 	while ((pc = readchar())) {
206 		if(islower((unsigned char)pc))
207 			pc = toupper((unsigned char)pc);
208 		if((i = role_index(pc)) >= 0) {
209 			printf("%c\n", pc);	/* echo */
210 			(void) fflush(stdout);	/* should be seen */
211 			break;
212 		}
213 		if(pc == '\n')
214 			break;
215 		if(pc == '\004')    /* Give him the opportunity to get out */
216 			end_of_input();
217 		hackbell();
218 	}
219 	if(pc == '\n')
220 		pc = 0;
221 
222 beginner:
223 	if(!pc) {
224 		printf("\nI'll choose a character for you.\n");
225 		i = rn2(NR_OF_ROLES);
226 		pc = rolesyms[i];
227 		printf("This game you will be a%s %s.\n",
228 			exper ? "n experienced" : "",
229 			roles[i]);
230 		getret();
231 		/* give him some feedback in case mklev takes much time */
232 		(void) putchar('\n');
233 		(void) fflush(stdout);
234 	}
235 #if 0
236 	/* Given the above code, I can't see why this would ever change
237 	   anything; it does core pretty well, though.  - cmh 4/20/93 */
238 	if(exper) {
239 		roles[i][0] = pc;
240 	}
241 #endif
242 
243 got_suffix:
244 
245 	(void) strlcpy(pl_character, roles[i], sizeof pl_character);
246 	flags.beginner = 1;
247 	u = zerou;
248 	u.usym = '@';
249 	u.ulevel = 1;
250 	init_uhunger();
251 #ifdef QUEST
252 	u.uhorizon = 6;
253 #endif /* QUEST */
254 	uarm = uarm2 = uarmh = uarms = uarmg = uwep = uball = uchain =
255 	uleft = uright = 0;
256 
257 	switch(pc) {
258 	case 'c':
259 	case 'C':
260 		Cave_man[2].trquan = 12 + rnd(9)*rnd(9);
261 		u.uhp = u.uhpmax = 16;
262 		u.ustr = u.ustrmax = 18;
263 		ini_inv(Cave_man);
264 		break;
265 	case 't':
266 	case 'T':
267 		Tourist[3].trquan = 20 + rnd(20);
268 		u.ugold = u.ugold0 = rnd(1000);
269 		u.uhp = u.uhpmax = 10;
270 		u.ustr = u.ustrmax = 8;
271 		ini_inv(Tourist);
272 		if(!rn2(25)) ini_inv(Tinopener);
273 		break;
274 	case 'w':
275 	case 'W':
276 		for(i=1; i<=4; i++) if(!rn2(5))
277 			Wizard[i].trquan += rn2(3) - 1;
278 		u.uhp = u.uhpmax = 15;
279 		u.ustr = u.ustrmax = 16;
280 		ini_inv(Wizard);
281 		break;
282 	case 's':
283 	case 'S':
284 		Fast = INTRINSIC;
285 		Stealth = INTRINSIC;
286 		u.uhp = u.uhpmax = 12;
287 		u.ustr = u.ustrmax = 10;
288 		ini_inv(Speleologist);
289 		if(!rn2(10)) ini_inv(Tinopener);
290 		break;
291 	case 'k':
292 	case 'K':
293 		u.uhp = u.uhpmax = 12;
294 		u.ustr = u.ustrmax = 10;
295 		ini_inv(Knight);
296 		break;
297 	case 'f':
298 	case 'F':
299 		u.uhp = u.uhpmax = 14;
300 		u.ustr = u.ustrmax = 17;
301 		ini_inv(Fighter);
302 		break;
303 	default:	/* impossible */
304 		u.uhp = u.uhpmax = 12;
305 		u.ustr = u.ustrmax = 16;
306 	}
307 	find_ac();
308 	if(!rn2(20)) {
309 		int d = rn2(7) - 2;	/* biased variation */
310 		u.ustr += d;
311 		u.ustrmax += d;
312 	}
313 
314 #ifdef WIZARD
315 	if(wizard) wiz_inv();
316 #endif /* WIZARD */
317 
318 	/* make sure he can carry all he has - especially for T's */
319 	while(inv_weight() > 0 && u.ustr < 118)
320 		u.ustr++, u.ustrmax++;
321 }
322 
323 static void
324 ini_inv(struct trobj *trop)
325 {
326 	struct obj *obj;
327 
328 	while(trop->trolet) {
329 		obj = mkobj(trop->trolet);
330 		obj->known = trop->trknown;
331 		/* not obj->dknown = 1; - let him look at it at least once */
332 		obj->cursed = 0;
333 		if(obj->olet == WEAPON_SYM){
334 			obj->quan = trop->trquan;
335 			trop->trquan = 1;
336 		}
337 		if(trop->trspe != UNDEF_SPE)
338 			obj->spe = trop->trspe;
339 		if(trop->trotyp != UNDEF_TYP)
340 			obj->otyp = trop->trotyp;
341 		else
342 			if(obj->otyp == WAN_WISHING)	/* gitpyr!robert */
343 				obj->otyp = WAN_DEATH;
344 		obj->owt = weight(obj);	/* defined after setting otyp+quan */
345 		obj = addinv(obj);
346 		if(obj->olet == ARMOR_SYM){
347 			switch(obj->otyp){
348 			case SHIELD:
349 				if(!uarms) setworn(obj, W_ARMS);
350 				break;
351 			case HELMET:
352 				if(!uarmh) setworn(obj, W_ARMH);
353 				break;
354 			case PAIR_OF_GLOVES:
355 				if(!uarmg) setworn(obj, W_ARMG);
356 				break;
357 			case ELVEN_CLOAK:
358 				if(!uarm2)
359 					setworn(obj, W_ARM);
360 				break;
361 			default:
362 				if(!uarm) setworn(obj, W_ARM);
363 			}
364 		}
365 		if(obj->olet == WEAPON_SYM)
366 			if(!uwep) setuwep(obj);
367 #ifndef PYRAMID_BUG
368 		if(--trop->trquan) continue;	/* make a similar object */
369 #else
370 		if(trop->trquan) {		/* check if zero first */
371 			--trop->trquan;
372 			if(trop->trquan)
373 				continue;	/* make a similar object */
374 		}
375 #endif /* PYRAMID_BUG */
376 		trop++;
377 	}
378 }
379 
380 #ifdef WIZARD
381 static void
382 wiz_inv()
383 {
384 	struct trobj *trop = &Extra_objs[0];
385 	char *ep = getenv("INVENT");
386 	int type;
387 
388 	while(ep && *ep) {
389 		type = atoi(ep);
390 		ep = strchr(ep, ',');
391 		if(ep) while(*ep == ',' || *ep == ' ') ep++;
392 		if(type <= 0 || type > NROFOBJECTS) continue;
393 		trop->trotyp = type;
394 		trop->trolet = objects[type].oc_olet;
395 		trop->trspe = 4;
396 		trop->trknown = 1;
397 		trop->trquan = 1;
398 		ini_inv(trop);
399 	}
400 	/* give him a wand of wishing by default */
401 	trop->trotyp = WAN_WISHING;
402 	trop->trolet = WAND_SYM;
403 	trop->trspe = 20;
404 	trop->trknown = 1;
405 	trop->trquan = 1;
406 	ini_inv(trop);
407 }
408 #endif /* WIZARD */
409 
410 void
411 plnamesuffix()
412 {
413 	char *p;
414 
415 	if ((p = strrchr(plname, '-'))) {
416 		*p = 0;
417 		pl_character[0] = p[1];
418 		pl_character[1] = 0;
419 		if(!plname[0]) {
420 			askname();
421 			plnamesuffix();
422 		}
423 	}
424 }
425 
426 /* must be called only from u_init() */
427 /* so that rolesyms[] is defined */
428 static int
429 role_index(char pc)
430 {
431 	char *cp;
432 
433 	if ((cp = strchr(rolesyms, pc)))
434 		return(cp - rolesyms);
435 	return(-1);
436 }
437