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