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