xref: /openbsd/games/hack/hack.u_init.c (revision aed906e4)
1 /*	$OpenBSD: hack.u_init.c,v 1.11 2016/01/09 18:33:15 mestre 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 
68 #include "hack.h"
69 
70 #define	UNDEF_TYP	0
71 #define	UNDEF_SPE	'\177'
72 extern char plname[];
73 
74 struct you zerou;
75 char pl_character[PL_CSIZ];
76 char *(roles[]) = {	/* must all have distinct first letter */
77 			/* roles[4] may be changed to -woman */
78 	"Tourist", "Speleologist", "Fighter", "Knight",
79 	"Cave-man", "Wizard"
80 };
81 #define	NR_OF_ROLES	SIZE(roles)
82 char rolesyms[NR_OF_ROLES + 1];		/* filled by u_init() */
83 
84 struct trobj {
85 	uchar trotyp;
86 	schar trspe;
87 	char trolet;
88 	Bitfield(trquan,6);
89 	Bitfield(trknown,1);
90 };
91 
92 #ifdef WIZARD
93 struct trobj Extra_objs[] = {
94 	{ 0, 0, 0, 0, 0 },
95 	{ 0, 0, 0, 0, 0 }
96 };
97 #endif /* WIZARD */
98 
99 struct trobj Cave_man[] = {
100 	{ MACE, 1, WEAPON_SYM, 1, 1 },
101 	{ BOW, 1, WEAPON_SYM, 1, 1 },
102 	{ ARROW, 0, WEAPON_SYM, 25, 1 },	/* quan is variable */
103 	{ LEATHER_ARMOR, 0, ARMOR_SYM, 1, 1 },
104 	{ 0, 0, 0, 0, 0}
105 };
106 
107 struct trobj Fighter[] = {
108 	{ TWO_HANDED_SWORD, 0, WEAPON_SYM, 1, 1 },
109 	{ RING_MAIL, 0, ARMOR_SYM, 1, 1 },
110 	{ 0, 0, 0, 0, 0 }
111 };
112 
113 struct trobj Knight[] = {
114 	{ LONG_SWORD, 0, WEAPON_SYM, 1, 1 },
115 	{ SPEAR, 2, WEAPON_SYM, 1, 1 },
116 	{ RING_MAIL, 1, ARMOR_SYM, 1, 1 },
117 	{ HELMET, 0, ARMOR_SYM, 1, 1 },
118 	{ SHIELD, 0, ARMOR_SYM, 1, 1 },
119 	{ PAIR_OF_GLOVES, 0, ARMOR_SYM, 1, 1 },
120 	{ 0, 0, 0, 0, 0 }
121 };
122 
123 struct trobj Speleologist[] = {
124 	{ STUDDED_LEATHER_ARMOR, 0, ARMOR_SYM, 1, 1 },
125 	{ UNDEF_TYP, 0, POTION_SYM, 2, 0 },
126 	{ FOOD_RATION, 0, FOOD_SYM, 3, 1 },
127 	{ PICK_AXE, UNDEF_SPE, TOOL_SYM, 1, 0 },
128 	{ ICE_BOX, 0, TOOL_SYM, 1, 0 },
129 	{ 0, 0, 0, 0, 0}
130 };
131 
132 struct trobj Tinopener[] = {
133 	{ CAN_OPENER, 0, TOOL_SYM, 1, 1 },
134 	{ 0, 0, 0, 0, 0 }
135 };
136 
137 struct trobj Tourist[] = {
138 	{ UNDEF_TYP, 0, FOOD_SYM, 10, 1 },
139 	{ POT_EXTRA_HEALING, 0, POTION_SYM, 2, 0 },
140 	{ EXPENSIVE_CAMERA, 0, TOOL_SYM, 1, 1 },
141 	{ DART, 2, WEAPON_SYM, 25, 1 },	/* quan is variable */
142 	{ 0, 0, 0, 0, 0 }
143 };
144 
145 struct trobj Wizard[] = {
146 	{ ELVEN_CLOAK, 0, ARMOR_SYM, 1, 1 },
147 	{ UNDEF_TYP, UNDEF_SPE, WAND_SYM, 2, 0 },
148 	{ UNDEF_TYP, UNDEF_SPE, RING_SYM, 2, 0 },
149 	{ UNDEF_TYP, UNDEF_SPE, POTION_SYM, 2, 0 },
150 	{ UNDEF_TYP, UNDEF_SPE, SCROLL_SYM, 3, 0 },
151 	{ 0, 0, 0, 0, 0 }
152 };
153 
154 static void ini_inv(struct trobj *);
155 static int  role_index(char);
156 #ifdef WIZARD
157 static void wiz_inv(void);
158 #endif
159 
160 void
u_init(void)161 u_init(void)
162 {
163 	int i;
164 	char exper = 'y', pc;
165 
166 	if(flags.female)	/* should have been set in HACKOPTIONS */
167 		roles[4] = "Cave-woman";
168 	for(i = 0; i < NR_OF_ROLES; i++)
169 		rolesyms[i] = roles[i][0];
170 	rolesyms[i] = 0;
171 
172 	if ((pc = pl_character[0])) {
173 		if (islower((unsigned char)pc))
174 			pc = toupper((unsigned char)pc);
175 		if ((i = role_index(pc)) >= 0)
176 			goto got_suffix;	/* implies experienced */
177 		printf("\nUnknown role: %c\n", pc);
178 		pl_character[0] = pc = 0;
179 	}
180 
181 	printf("\nAre you an experienced player? [ny] ");
182 
183 	while(!strchr("ynYN \n\004", (exper = readchar())))
184 		hackbell();
185 	if(exper == '\004')		/* Give him an opportunity to get out */
186 		end_of_input();
187 	printf("%c\n", exper);		/* echo */
188 	if(strchr("Nn \n", exper)) {
189 		exper = 0;
190 		goto beginner;
191 	}
192 
193 	printf("\nTell me what kind of character you are:\n");
194 	printf("Are you");
195 	for(i = 0; i < NR_OF_ROLES; i++) {
196 		printf(" a %s", roles[i]);
197 		if(i == 2)			/* %% */
198 			printf(",\n\t");
199 		else if(i < NR_OF_ROLES - 2)
200 			printf(",");
201 		else if(i == NR_OF_ROLES - 2)
202 			printf(" or");
203 	}
204 	printf("? [%s] ", rolesyms);
205 
206 	while ((pc = readchar())) {
207 		if(islower((unsigned char)pc))
208 			pc = toupper((unsigned char)pc);
209 		if((i = role_index(pc)) >= 0) {
210 			printf("%c\n", pc);	/* echo */
211 			(void) fflush(stdout);	/* should be seen */
212 			break;
213 		}
214 		if(pc == '\n')
215 			break;
216 		if(pc == '\004')    /* Give him the opportunity to get out */
217 			end_of_input();
218 		hackbell();
219 	}
220 	if(pc == '\n')
221 		pc = 0;
222 
223 beginner:
224 	if(!pc) {
225 		printf("\nI'll choose a character for you.\n");
226 		i = rn2(NR_OF_ROLES);
227 		pc = rolesyms[i];
228 		printf("This game you will be a%s %s.\n",
229 			exper ? "n experienced" : "",
230 			roles[i]);
231 		getret();
232 		/* give him some feedback in case mklev takes much time */
233 		(void) putchar('\n');
234 		(void) fflush(stdout);
235 	}
236 #if 0
237 	/* Given the above code, I can't see why this would ever change
238 	   anything; it does core pretty well, though.  - cmh 4/20/93 */
239 	if(exper) {
240 		roles[i][0] = pc;
241 	}
242 #endif
243 
244 got_suffix:
245 
246 	(void) strlcpy(pl_character, roles[i], sizeof pl_character);
247 	flags.beginner = 1;
248 	u = zerou;
249 	u.usym = '@';
250 	u.ulevel = 1;
251 	init_uhunger();
252 #ifdef QUEST
253 	u.uhorizon = 6;
254 #endif /* QUEST */
255 	uarm = uarm2 = uarmh = uarms = uarmg = uwep = uball = uchain =
256 	uleft = uright = 0;
257 
258 	switch(pc) {
259 	case 'c':
260 	case 'C':
261 		Cave_man[2].trquan = 12 + rnd(9)*rnd(9);
262 		u.uhp = u.uhpmax = 16;
263 		u.ustr = u.ustrmax = 18;
264 		ini_inv(Cave_man);
265 		break;
266 	case 't':
267 	case 'T':
268 		Tourist[3].trquan = 20 + rnd(20);
269 		u.ugold = u.ugold0 = rnd(1000);
270 		u.uhp = u.uhpmax = 10;
271 		u.ustr = u.ustrmax = 8;
272 		ini_inv(Tourist);
273 		if(!rn2(25)) ini_inv(Tinopener);
274 		break;
275 	case 'w':
276 	case 'W':
277 		for(i=1; i<=4; i++) if(!rn2(5))
278 			Wizard[i].trquan += rn2(3) - 1;
279 		u.uhp = u.uhpmax = 15;
280 		u.ustr = u.ustrmax = 16;
281 		ini_inv(Wizard);
282 		break;
283 	case 's':
284 	case 'S':
285 		Fast = INTRINSIC;
286 		Stealth = INTRINSIC;
287 		u.uhp = u.uhpmax = 12;
288 		u.ustr = u.ustrmax = 10;
289 		ini_inv(Speleologist);
290 		if(!rn2(10)) ini_inv(Tinopener);
291 		break;
292 	case 'k':
293 	case 'K':
294 		u.uhp = u.uhpmax = 12;
295 		u.ustr = u.ustrmax = 10;
296 		ini_inv(Knight);
297 		break;
298 	case 'f':
299 	case 'F':
300 		u.uhp = u.uhpmax = 14;
301 		u.ustr = u.ustrmax = 17;
302 		ini_inv(Fighter);
303 		break;
304 	default:	/* impossible */
305 		u.uhp = u.uhpmax = 12;
306 		u.ustr = u.ustrmax = 16;
307 	}
308 	find_ac();
309 	if(!rn2(20)) {
310 		int d = rn2(7) - 2;	/* biased variation */
311 		u.ustr += d;
312 		u.ustrmax += d;
313 	}
314 
315 #ifdef WIZARD
316 	if(wizard) wiz_inv();
317 #endif /* WIZARD */
318 
319 	/* make sure he can carry all he has - especially for T's */
320 	while(inv_weight() > 0 && u.ustr < 118)
321 		u.ustr++, u.ustrmax++;
322 }
323 
324 static void
ini_inv(struct trobj * trop)325 ini_inv(struct trobj *trop)
326 {
327 	struct obj *obj;
328 
329 	while(trop->trolet) {
330 		obj = mkobj(trop->trolet);
331 		obj->known = trop->trknown;
332 		/* not obj->dknown = 1; - let him look at it at least once */
333 		obj->cursed = 0;
334 		if(obj->olet == WEAPON_SYM){
335 			obj->quan = trop->trquan;
336 			trop->trquan = 1;
337 		}
338 		if(trop->trspe != UNDEF_SPE)
339 			obj->spe = trop->trspe;
340 		if(trop->trotyp != UNDEF_TYP)
341 			obj->otyp = trop->trotyp;
342 		else
343 			if(obj->otyp == WAN_WISHING)	/* gitpyr!robert */
344 				obj->otyp = WAN_DEATH;
345 		obj->owt = weight(obj);	/* defined after setting otyp+quan */
346 		obj = addinv(obj);
347 		if(obj->olet == ARMOR_SYM){
348 			switch(obj->otyp){
349 			case SHIELD:
350 				if(!uarms) setworn(obj, W_ARMS);
351 				break;
352 			case HELMET:
353 				if(!uarmh) setworn(obj, W_ARMH);
354 				break;
355 			case PAIR_OF_GLOVES:
356 				if(!uarmg) setworn(obj, W_ARMG);
357 				break;
358 			case ELVEN_CLOAK:
359 				if(!uarm2)
360 					setworn(obj, W_ARM);
361 				break;
362 			default:
363 				if(!uarm) setworn(obj, W_ARM);
364 			}
365 		}
366 		if(obj->olet == WEAPON_SYM)
367 			if(!uwep) setuwep(obj);
368 #ifndef PYRAMID_BUG
369 		if(--trop->trquan) continue;	/* make a similar object */
370 #else
371 		if(trop->trquan) {		/* check if zero first */
372 			--trop->trquan;
373 			if(trop->trquan)
374 				continue;	/* make a similar object */
375 		}
376 #endif /* PYRAMID_BUG */
377 		trop++;
378 	}
379 }
380 
381 #ifdef WIZARD
382 static void
wiz_inv(void)383 wiz_inv(void)
384 {
385 	struct trobj *trop = &Extra_objs[0];
386 	char *ep = getenv("INVENT");
387 	int type;
388 
389 	while(ep && *ep) {
390 		type = atoi(ep);
391 		ep = strchr(ep, ',');
392 		if(ep) while(*ep == ',' || *ep == ' ') ep++;
393 		if(type <= 0 || type > NROFOBJECTS) continue;
394 		trop->trotyp = type;
395 		trop->trolet = objects[type].oc_olet;
396 		trop->trspe = 4;
397 		trop->trknown = 1;
398 		trop->trquan = 1;
399 		ini_inv(trop);
400 	}
401 	/* give him a wand of wishing by default */
402 	trop->trotyp = WAN_WISHING;
403 	trop->trolet = WAND_SYM;
404 	trop->trspe = 20;
405 	trop->trknown = 1;
406 	trop->trquan = 1;
407 	ini_inv(trop);
408 }
409 #endif /* WIZARD */
410 
411 void
plnamesuffix(void)412 plnamesuffix(void)
413 {
414 	char *p;
415 
416 	if ((p = strrchr(plname, '-'))) {
417 		*p = 0;
418 		pl_character[0] = p[1];
419 		pl_character[1] = 0;
420 		if(!plname[0]) {
421 			askname();
422 			plnamesuffix();
423 		}
424 	}
425 }
426 
427 /* must be called only from u_init() */
428 /* so that rolesyms[] is defined */
429 static int
role_index(char pc)430 role_index(char pc)
431 {
432 	char *cp;
433 
434 	if ((cp = strchr(rolesyms, pc)))
435 		return(cp - rolesyms);
436 	return(-1);
437 }
438