Source:NetHack 2.2a/u init.c

Below is the full text to u_init.c from the source code of NetHack 2.2a. To link to a particular line, write [[NetHack 2.2a/u_init.c#line123 ]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

1.   /*	SCCS Id: @(#)u_init.c	2.0	87/09/15 2.   /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3.     4.    #include   5.    #include   6.    #include "hack.h"  7.    #ifdef GENIX 8.   #define	void	int 9.   #endif 10.   11.   #define Strcpy	(void) strcpy 12.  #define	Strcat	(void) strcat 13.  #define	UNDEF_TYP	0 14.  #define	UNDEF_SPE	'\177' 15.  extern struct obj *addinv; 16.  extern char *eos; 17.  extern char plname[]; 18.  #define IS_MAGIC(x)	((x)->olet == WAND_SYM || (x)->olet == POTION_SYM || \  19.   			 (x)->olet == RING_SYM || (x)->olet == SCROLL_SYM || \  20.   			 (x)->olet == SPBOOK_SYM) 21.   22.   struct you zerou; 23.  char pl_character[PL_CSIZ]; 24.  char *(roles[]) = {	/* must all have distinct first letter */ 25.  			/* roles[4] & [7] may be changed for females */ 26.  	"Archeologist", "Tourist", "Barbarian", "Knight", "Cave-man", 27.  #ifdef NEWCLASS 28.  	"Samurai", "Ninja", "Priest", 29.  #endif 30.  #ifdef KAA 31.  	"Valkyrie", "Elf", "Healer", 32.  #endif 33.  	"Wizard" 34.  };  35.   #define	NR_OF_ROLES	SIZE(roles) 36.  char rolesyms[NR_OF_ROLES + 1];		/* filled by u_init */ 37.   38.   struct trobj { 39.  	unsigned short trotyp; 40.  	schar trspe; 41.  	char trolet; 42.  	Bitfield(trquan,6); 43.  	Bitfield(trknown,1); 44.  };  45.    46.   #ifdef WIZARD 47.  struct trobj Extra_objs[] = { 48.  	{ 0, 0, 0, 0, 0 },  49.   	{ 0, 0, 0, 0, 0 }  50.   };  51.   #endif 52.   53.   struct trobj Cave_man[] = { 54.  #define C_ARROWS	2 55.  	{ CLUB, 1, WEAPON_SYM, 1, 1 }, 56.  	{ BOW, 1, WEAPON_SYM, 1, 1 }, 57.  	{ ARROW, 0, WEAPON_SYM, 25, 1 },	/* quan is variable */ 58.  	{ LEATHER_ARMOR, 0, ARMOR_SYM, 1, 1 }, 59.  	{ 0, 0, 0, 0, 0}  60.   };  61.    62.   struct trobj Barbarian[] = { 63.  	{ TWO_HANDED_SWORD, 0, WEAPON_SYM, 1, 1 }, 64.  	{ RING_MAIL, 0, ARMOR_SYM, 1, 1 }, 65.  	{ 0, 0, 0, 0, 0 }  66.   };  67.    68.   struct trobj Knight[] = { 69.  	{ LONG_SWORD, 0, WEAPON_SYM, 1, 1 }, 70.  	{ SPEAR, 2, WEAPON_SYM, 1, 1 }, 71.  	{ RING_MAIL, 1, ARMOR_SYM, 1, 1 }, 72.  	{ HELMET, 0, ARMOR_SYM, 1, 1 }, 73.  	{ SHIELD, 0, ARMOR_SYM, 1, 1 }, 74.  	{ PAIR_OF_GLOVES, 0, ARMOR_SYM, 1, 1 }, 75.  	{ 0, 0, 0, 0, 0 }  76.   };  77.    78.   #ifdef KAA 79.  struct trobj Elf[] = { 80.  #define E_ARROWS	2 81.  #define E_ARMOR		3 82.  	{ SHORT_SWORD, 0, WEAPON_SYM, 1, 1 }, 83.  	{ BOW, 0, WEAPON_SYM, 1, 1 }, 84.  	{ ARROW, 0, WEAPON_SYM, 25, 1 }, 85.  	{ UNDEF_TYP, 0, ARMOR_SYM, 1, 1 }, 86.  	{ 0, 0, 0, 0, 0 }  87.   };  88.    89.   struct trobj Valkyrie[] = { 90.  	{ LONG_SWORD, 1, WEAPON_SYM, 1, 1 }, 91.  	{ SHIELD, 3, ARMOR_SYM, 1, 1 }, 92.  	{ FOOD_RATION, 0, FOOD_SYM, 1, 1 }, 93.  	{ 0, 0, 0, 0, 0 }  94.   };  95.    96.   struct trobj Healer[] = { 97.  	{ STETHOSCOPE, 0, TOOL_SYM, 1, 0 }, 98.  	{ POT_HEALING, 0, POTION_SYM, 4, 1 }, 99.  	{ POT_EXTRA_HEALING, 0, POTION_SYM, 4, 1 }, 100. 	{ APPLE, 0, FOOD_SYM, 5, 0 }, 101. 	{ 0, 0, 0, 0, 0}  102.  };  103.  #endif /* KAA /**/ 104.  105.  struct trobj Archeologist[] = { 106. 	{ STUDDED_LEATHER_ARMOR, 0, ARMOR_SYM, 1, 1 }, 107. 	{ UNDEF_TYP, 0, POTION_SYM, 2, 0 }, 108. 	{ FOOD_RATION, 0, FOOD_SYM, 3, 1 }, 109. 	{ PICK_AXE, UNDEF_SPE, TOOL_SYM, 1, 0 }, 110. 	{ ICE_BOX, 0, TOOL_SYM, 1, 0 }, 111. 	{ 0, 0, 0, 0, 0}  112.  };  113.   114.  struct trobj Tinopener[] = { 115. 	{ CAN_OPENER, 0, TOOL_SYM, 1, 1 }, 116. 	{ 0, 0, 0, 0, 0 }  117.  };  118.   119.  #ifdef MARKER 120. struct trobj Magicmarker[] = { 121. 	{ MAGIC_MARKER, 50, TOOL_SYM, 1, 0 }, 122. 	{ 0, 0, 0, 0, 0 }  123.  };  124.  #endif 125.  126.  #ifdef WALKIES 127. struct trobj Leash[] = { 128. 	{ LEASH, 0, TOOL_SYM, 1, 0 }, 129. 	{ 0, 0, 0, 0, 0 }  130.  };  131.  #endif 132.  133.  struct trobj Blindfold[] = { 134. 	{ BLINDFOLD, 0, TOOL_SYM, 1, 0 }, 135. 	{ 0, 0, 0, 0, 0 }  136.  };  137.   138.  struct trobj Tourist[] = { 139. #define	T_DARTS		3 140. 	{ UNDEF_TYP, 0, FOOD_SYM, 10, 1 }, 141. 	{ POT_EXTRA_HEALING, 0, POTION_SYM, 2, 0 }, 142. 	{ EXPENSIVE_CAMERA, 0, TOOL_SYM, 1, 1 }, 143. 	{ DART, 2, WEAPON_SYM, 25, 1 },	/* quan is variable */ 144. 	{ 0, 0, 0, 0, 0 }  145.  };  146.   147.  struct trobj Wizard[] = { 148. #define W_MULTSTART	2 149. #define W_MULTEND	6 150. 	{ ELVEN_CLOAK, 0, ARMOR_SYM, 1, 1 }, 151. 	{ DAGGER, 0, WEAPON_SYM, 1, 1 },	/* for dealing with ghosts */ 152. 	{ UNDEF_TYP, UNDEF_SPE, WAND_SYM, 2, 1 }, 153. 	{ UNDEF_TYP, UNDEF_SPE, RING_SYM, 2, 1 }, 154. 	{ UNDEF_TYP, UNDEF_SPE, POTION_SYM, 2, 1 }, 155. 	{ UNDEF_TYP, UNDEF_SPE, SCROLL_SYM, 3, 1 }, 156. #ifdef SPELLS 157. 	{ UNDEF_TYP, UNDEF_SPE, SPBOOK_SYM, 3, 1 }, 158. #endif 159. 	{ 0, 0, 0, 0, 0 }  160.  };  161.   162.  #ifdef NEWCLASS 163. struct	trobj	Samurai[] = { 164. #define S_ARROWS	3 165. 	{ KATANA, 0, WEAPON_SYM, 1, 1 }, 166. 	{ SHORT_SWORD, 0, WEAPON_SYM, 1, 1 },	/* the wakizashi */ 167. 	{ BOW,    1, WEAPON_SYM, 1, 1 }, 168. 	{ ARROW,  0, WEAPON_SYM, 25, 1 },	/* quan is variable */ 169. 	{ SPLINT_MAIL, 0, ARMOR_SYM, 1, 1}, 170. 	{ 0, 0, 0, 0, 0 }  171.  };  172.   173.  struct	trobj	Ninja[] = { 174. #define N_SHURIKEN	1 175. 	{ KATANA, 0, WEAPON_SYM, 1, 1 }, 176. 	{ SHURIKEN, 0, WEAPON_SYM, 25, 1 },	/* quan is variable */ 177. 	{ LEATHER_ARMOR, 1, ARMOR_SYM, 1, 1}, 178. 	{ 0, 0, 0, 0, 0 }  179.  };  180.   181.  struct	trobj	Priest[] = { 182. 	{ CHAIN_MAIL, 0, ARMOR_SYM, 1, 1 }, 183. 	{ SHIELD, 0, ARMOR_SYM, 1, 1 }, 184. 	{ MACE, 1, WEAPON_SYM, 1, 1 }, 185. 	{ POT_HOLY_WATER, 0, POTION_SYM, 4, 1 }, 186. #ifdef SPELLS 187. 	{ UNDEF_TYP, UNDEF_SPE, SPBOOK_SYM, 2, 0 }, 188. #endif 189. 	{ 0, 0, 0, 0, 0 }  190.  };  191.  #endif /* NEWCLASS /**/ 192.  193.  u_init{ 194. register int i;  195. char exper = 'y', pc; 196. extern char readchar; 197. 	if(flags.female)  {	/* should have been set in HACKOPTIONS */ 198. 		roles[4] = "Cave-woman"; 199. #ifdef NEWCLASS 200. 		roles[7] = "Priestess"; 201. #endif 202. 	}  203.  	for(i = 0; i < NR_OF_ROLES; i++) 204. 		rolesyms[i] = roles[i][0]; 205. 	rolesyms[i] = 0; 206.  207.  	if(pc = pl_character[0]) { 208. 		if('a' <= pc && pc <= 'z') pc += 'A'-'a'; 209. 		if((i = role_index(pc)) >= 0) 210. 			goto got_suffix;	/* implies experienced */ 211. 		printf("\nUnknown role: %c\n", pc); 212. 		pl_character[0] = pc = 0; 213. 	}  214.   215.  	printf("\nShall I pick a character for you (yes, no, or quit) ? [ynq] "); 216.  217.  	while(!index("yYnNqQ", (exper = readchar)))	bell; 218.  219.  	printf("%c\n", exper);		/* echo */ 220.  221.  	if (index("qQ", exper)) { 222. 		clearlocks; 223. 		settty((char *) 0); 224. 		exit(0); 225. 	}  226.   227.  	if(index("Yy", exper)) { 228. 		exper = 0; 229. 		goto beginner; 230. 	}  231.   232.  	printf("\n Tell me what kind of character you are:\n"); 233. 	printf(" Are you"); 234. 	for(i = 0; i < NR_OF_ROLES; i++) { 235. 		printf(" %s %s", index("AEIOU",roles[i][0]) ? "an" : "a", roles[i]); 236. 		if((((i + 1) % 4) == 0) && (i != NR_OF_ROLES -1)) printf(",\n        "); 237. 		else if(i < NR_OF_ROLES - 2)	printf(","); 238. 		if(i == NR_OF_ROLES - 2)	printf(" or"); 239. 	}  240.  	printf("? [%s or q(quit)] ", rolesyms); 241.  242.  	while(pc = readchar) { 243. 		if (pc == 'q' || pc == 'Q') { 244.  245.  			clearlocks; 246. 			settty((char *) 0); 247. 			exit(0); 248. 		}  249.  		if('a' <= pc && pc <= 'z') pc += 'A'-'a'; 250. 		if((i = role_index(pc)) >= 0) { 251. 			printf("%c\n", pc);	/* echo */ 252. 			(void) fflush(stdout);	/* should be seen */ 253. 			break; 254. 		}  255.  		if(pc == '\n') break; 256. 		bell; 257. 	}  258.  	if(pc == '\n')	pc = 0; 259.  260.  beginner: 261. 	if(!pc) { 262. 		i = rn2(NR_OF_ROLES); 263. 		pc = rolesyms[i]; 264. 		printf("\nThis game you will be %s %s%s.\n",  265.  			(exper || index("AEIOU", roles[i][0])) ? "an" : "a",  266.  			exper ? "experienced " : "", roles[i]); 267. 		getret; 268. 		/* give him some feedback in case mklev takes much time */ 269. 		(void) putchar('\n'); 270. 		(void) fflush(stdout); 271. 	}  272.  	if(exper) { 273. 		roles[i][0] = pc; 274. 	}  275.   276.  got_suffix: 277.  278.  	(void) strncpy(pl_character, roles[i], PL_CSIZ-1); 279. 	pl_character[PL_CSIZ-1] = 0; 280. 	flags.beginner = 1; 281. 	u = zerou; 282. 	u.usym = '@'; 283. 	u.ulevel = 1; 284. 	init_uhunger; 285. 	uarm = uarm2 = uarmh = uarms = uarmg = uwep = 286. 	uball = uchain = uleft = uright = 0; 287.  288.  #ifdef SPELLS 289. 	u.uen = u.uenmax = 1; 290. 	for (i = 0; i <= MAXSPELL; i++) spl_book[i].sp_id = NO_SPELL; 291. #endif 292. #ifdef PRAYERS 293. 	u.ublesscnt = 300;			/* no prayers just yet */ 294. 	u.ublessed = 0;				/* not worthy yet */ 295. 	u.ugangr   = 0;				/* gods not angry */ 296. #endif 297. #ifdef HARD 298. 	u.udemigod = u.udg_cnt = 0;		/* not a demi-god yet... */ 299.  #endif 300. #ifdef KAA 301. 	u.mh = u.mhmax = u.umonnum = u.mtimedone = 0; 302. #endif 303. #ifdef QUEST 304. 	u.uhorizon = 6; 305. #endif 306. 	switch(pc) { 307. 	case 'c': 308. 	case 'C': 309. 		Cave_man[C_ARROWS].trquan = 12 + rnd(9)*rnd(9); 310. 		u.uhp = u.uhpmax = 16; 311. 		u.ustr = u.ustrmax = 18; 312. 		ini_inv(Cave_man); 313. 		break; 314. 	case 't': 315. 	case 'T': 316. #ifdef KAA 317. 		objects[POT_EXTRA_HEALING].oc_name_known=1; 318. #endif 319. 		Tourist[T_DARTS].trquan = 20 + rnd(20); 320. 		u.ugold = u.ugold0 = rnd(1000); 321. 		u.uhp = u.uhpmax = 10; 322. 		u.ustr = u.ustrmax = 8; 323. 		ini_inv(Tourist); 324. 		if(!rn2(25)) ini_inv(Tinopener); 325. #ifdef MARKER 326. 		else if(!rn2(25)) ini_inv(Magicmarker); 327. #endif 328. #ifdef WALKIES 329. 		else if(!rn2(25)) ini_inv(Leash); 330. #endif 331. 		break; 332. 	case 'w': 333. 	case 'W': 334. 		for(i = W_MULTSTART; i <= W_MULTEND; i++) 335. 		    if(!rn2(5)) 336. 			Wizard[i].trquan += rn2(3) - 1; 337. 		u.uhp = u.uhpmax = 15; 338. 		u.ustr = u.ustrmax = 16; 339. #ifdef SPELLS 340. 		u.uen = u.uenmax += rn2(4); 341. #endif 342. 		ini_inv(Wizard); 343. #ifdef MARKER 344. 		if(!rn2(5)) ini_inv(Magicmarker); 345. #endif 346. 		if(!rn2(5)) ini_inv(Blindfold); 347. 		break; 348. 	case 'a': 349. 	case 'A': 350. 		Fast = INTRINSIC; 351. 		Stealth = INTRINSIC; 352. 		u.uhp = u.uhpmax = 12; 353. 		u.ustr = u.ustrmax = 10; 354. 		ini_inv(Archeologist); 355. 		if(!rn2(10)) ini_inv(Tinopener); 356. #ifdef MARKER 357. 		else if(!rn2(10)) ini_inv(Magicmarker); 358. #endif 359. 		break; 360. #ifdef KAA 361. 	case 'e': 362. 	case 'E': 363. 		Elf[E_ARROWS].trquan = 15+rnd(20); 364. 		Elf[E_ARMOR].trotyp = (rn2(2) ? ELFIN_CHAIN_MAIL : ELVEN_CLOAK); 365. 		Fast = INTRINSIC; 366. 		HSee_invisible = INTRINSIC; 367. 		u.uhp = u.uhpmax = 16; 368. 		u.ustr = u.ustrmax = 16; 369. 		ini_inv(Elf); 370. 		if(!rn2(5)) ini_inv(Blindfold); 371. 		break; 372. 	case 'v': 373. 	case 'V': 374. 		Stealth = INTRINSIC; 375. 		HCold_resistance = INTRINSIC; 376. 		flags.female = TRUE; 377. 		u.uhp = u.uhpmax = 16; 378. 		u.ustr = u.ustrmax = 17; 379. 		ini_inv(Valkyrie); 380. 		break; 381. 	case 'h': 382. 	case 'H': 383. 		objects[POT_HEALING].oc_name_known=1; 384. 		objects[POT_EXTRA_HEALING].oc_name_known=1; 385. 		HPoison_resistance = INTRINSIC; 386. 		u.uhp = u.uhpmax = 16; 387. 		u.ustr = u.ustrmax = 15; 388. 		ini_inv(Healer); 389. 		break; 390. #endif 391. 	case 'k': 392. 	case 'K': 393. 		u.uhp = u.uhpmax = 12; 394. 		u.ustr = u.ustrmax = 10; 395. 		ini_inv(Knight); 396. 		break; 397. 	case 'b': 398. 	case 'B': 399. 		u.uhp = u.uhpmax = 14; 400. 		u.ustr = u.ustrmax = 17; 401. 		ini_inv(Barbarian); 402. 		break; 403. #ifdef NEWCLASS 404. 	case 's': 405. 	case 'S': 406. 		Fast = INTRINSIC; 407. 		u.uhp = u.uhpmax = 16; 408. 		u.ustr = u.ustrmax = 16; 409. 		Samurai[S_ARROWS].trquan = 12 + rnd(9)*rnd(9); 410. 		ini_inv(Samurai); 411. 		break; 412. 	case 'n': 413. 	case 'N': 414. 		Fast = INTRINSIC; 415. 		Stealth = INTRINSIC; 416. 		u.uhp = u.uhpmax = 15; 417. 		u.ustr = u.ustrmax = 10; 418. 		Ninja[N_SHURIKEN].trquan = 12 + rnd(9)*rnd(9); 419. 		ini_inv(Ninja); 420. 		if(!rn2(5)) ini_inv(Blindfold); 421. 		break; 422. 	case 'p': 423. 	case 'P': 424. 		u.uhp = u.uhpmax = 13; 425. 		u.ustr = u.ustrmax = 15; 426. # ifdef SPELLS 427. 		u.uen = u.uenmax += rn2(4); 428. # endif 429. 		ini_inv(Priest); 430. # ifdef KAA 431. 		uwep->dknown = 1;	/* bless his primary weapon */ 432. # endif 433. # ifdef MARKER 434. 		if(!rn2(10)) ini_inv(Magicmarker); 435. # endif 436. 		objects[POT_HOLY_WATER].oc_name_known = 1; 437. 		break; 438. #endif /* NEWCLASS /**/ 439. 	default:	/* impossible */ 440. 		u.uhp = u.uhpmax = 12; 441. 		u.ustr = u.ustrmax = 16; 442. 	}  443.  	find_ac; 444. 	if(!rn2(20)) { 445. 		register int d = rn2(7) - 2;	/* biased variation */ 446. 		u.ustr += d;  447. u.ustrmax += d; 448. } 449.   450.  #ifdef WIZARD 451. 	wiz_inv; 452. #endif 453. 	/* make sure he can carry all he has - especially for T's */ 454. 	while(inv_weight > 0 && u.ustr < 118) 455. 		u.ustr++, u.ustrmax++; 456. }  457.   458.  ini_inv(trop) register struct trobj *trop; { 459. register struct obj *obj; 460. extern struct obj *mkobj; 461. 	while(trop->trolet) { 462. 		obj = mkobj(trop->trolet); 463. 		obj->known = trop->trknown; 464. 		/* not obj->dknown = 1; - let him look at it at least once */ 465. 		obj->cursed = 0; 466. 		if(obj->olet == WEAPON_SYM){ 467. 			obj->quan = trop->trquan; 468. 			trop->trquan = 1; 469. 		}  470.  		if(trop->trspe != UNDEF_SPE) 471. 			obj->spe = trop->trspe; 472. 		if(trop->trotyp != UNDEF_TYP) 473. 			obj->otyp = trop->trotyp; 474. 		else 475. 			if(obj->otyp == WAN_WISHING)	/* gitpyr!robert */ 476. 				obj->otyp = WAN_DEATH; 477. 		obj->owt = weight(obj);	/* defined after setting otyp+quan */ 478. 		obj = addinv(obj); 479.  480.  		/*  481.  		 * if a magic item's 'known' bit is on, set its name known 482. 		 */  483.  		if (IS_MAGIC(obj) && obj->known) 484. 		    objects[obj->otyp].oc_name_known=1; 485.  486.  		if(obj->olet == ARMOR_SYM){ 487. 			switch(obj->otyp){ 488. 			case SHIELD: 489. 				if(!uarms) setworn(obj, W_ARMS); 490. 				break; 491. 			case HELMET: 492. 				if(!uarmh) setworn(obj, W_ARMH); 493. 				break; 494. 			case PAIR_OF_GLOVES: 495. 				if(!uarmg) setworn(obj, W_ARMG); 496. 				break; 497. 			case ELVEN_CLOAK: 498. 				if(!uarm2) 499. 					setworn(obj, W_ARM); 500. 				break; 501. 			default: 502. 				if(!uarm) setworn(obj, W_ARM); 503. 			}  504.  		}  505.  		/* below changed by GAN 01/09/87 to allow wielding of  506. * pick-axe or can-opener if there is no weapon 507. 		 */  508.  		if(obj->olet == WEAPON_SYM || obj->otyp == PICK_AXE ||  509.  		   obj->otyp == CAN_OPENER) 510. 			if(!uwep) setuwep(obj); 511. #ifndef PYRAMID_BUG 512. 		if(--trop->trquan) continue;	/* make a similar object */ 513. #else 514. 		if(trop->trquan) {		/* check if zero first */ 515. 			--trop->trquan; 516. 			if(trop->trquan) 517. 				continue;	/* make a similar object */ 518. 		}  519.  #endif 520. 		trop++; 521. 	}  522.  }  523.   524.  #ifdef WIZARD 525. wiz_inv{ 526. register struct trobj *trop = &Extra_objs[0]; 527. extern char *getenv; 528. register char *ep = getenv("INVENT"); 529. register int type; 530. 	while(ep && *ep) { 531. 		type = atoi(ep); 532. 		ep = index(ep, ','); 533. 		if(ep) while(*ep == ',' || *ep == ' ') ep++; 534. 		if(type <= 0 || type > NROFOBJECTS) continue; 535. 		trop->trotyp = type; 536. 		trop->trolet = objects[type].oc_olet; 537. 		trop->trspe = 4; 538. 		trop->trknown = 1; 539. 		trop->trquan = 1; 540. 		ini_inv(trop); 541. 	}  542.  }  543.  #endif /* WIZARD /**/ 544.  545.  plnamesuffix { 546. register char *p; 547. 	if(p = rindex(plname, '-')) { 548. 		*p = 0; 549. 		pl_character[0] = p[1]; 550. 		pl_character[1] = 0; 551. 		if(!plname[0]) { 552. 			askname; 553. 			plnamesuffix; 554. 		}  555.  	}  556.  }  557.   558.  role_index(pc) 559. char pc; 560. {		/* must be called only from u_init */ 561. 		/* so that rolesyms[] is defined */ 562. 	register char *cp; 563.  564.  	if(cp = index(rolesyms, pc)) 565. 		return(cp - rolesyms); 566. 	return(-1); 567. }