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