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