1 /* File: spells3.c */
2 
3 /* Purpose: Spell code (part 3) */
4 
5 /*
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12 
13 #include "angband.h"
14 #include "script.h"
15 
16 /* Maximum number of tries for teleporting */
17 #define MAX_TRIES 100
18 
19 /*
20  * Teleport a monster, normally up to "dis" grids away.
21  *
22  * Attempt to move the monster at least "dis/2" grids away.
23  *
24  * But allow variation to prevent infinite loops.
25  */
teleport_away(int m_idx,int dis)26 bool teleport_away(int m_idx, int dis)
27 {
28 	int ny = 0, nx = 0, oy, ox, d, i, min;
29 	int tries = 0;
30 
31 	bool look = TRUE;
32 
33 	monster_type *m_ptr = &m_list[m_idx];
34 	monster_race *r_ptr = &r_info[m_ptr->r_idx];
35 	cave_type *c_ptr = NULL;
36 
37 	/* Paranoia */
38 	if (!m_ptr->r_idx) return (FALSE);
39 
40 	/* Save the old location */
41 	oy = m_ptr->fy;
42 	ox = m_ptr->fx;
43 
44 	/* Minimum distance */
45 	min = dis / 2;
46 
47 	if ((((p_ptr->chp * 10) / p_ptr->mhp) < 5) &&
48 		(randint1(5) > ((p_ptr->chp * 10) / p_ptr->mhp)))
49 	{
50 		chg_virtue(V_VALOUR, -1);
51 	}
52 
53 	/* Look until done */
54 	while (look)
55 	{
56 		tries++;
57 
58 		/* Verify max distance */
59 		if (dis > 200) dis = 200;
60 
61 		/* Try several locations */
62 		for (i = 0; i < 500; i++)
63 		{
64 			/* Pick a (possibly illegal) location */
65 			while (1)
66 			{
67 				ny = rand_spread(oy, dis);
68 				nx = rand_spread(ox, dis);
69 				d = distance(ox, oy, nx, ny);
70 				if ((d >= min) && (d <= dis)) break;
71 			}
72 
73 			/* Ignore illegal locations */
74 			if (!in_bounds2(nx, ny)) continue;
75 
76 			c_ptr = area(nx, ny);
77 
78 			/* Require "empty" floor space */
79 			if (!cave_empty_grid(c_ptr)) continue;
80 
81 			/* Not on player */
82 			if ((ny == p_ptr->py) && (nx == p_ptr->px)) continue;
83 
84 			/* Not on bad terrain */
85 			if (!test_monster_square(c_ptr, r_ptr)) continue;
86 
87 			/*
88 			 * Test for fields that will not allow monsters to
89 			 * be generated on them.  (i.e. Glyph of warding)
90 			 */
91 			if (fields_have_flags(c_ptr, FIELD_INFO_NO_MPLACE)) continue;
92 
93 			/* No teleporting into vaults and such */
94 			if (c_ptr->info & CAVE_ICKY) continue;
95 
96 			/* This grid looks good */
97 			look = FALSE;
98 
99 			/* Stop looking */
100 			break;
101 		}
102 
103 		/* Increase the maximum distance */
104 		dis = dis * 2;
105 
106 		/* Decrease the minimum distance */
107 		min = min / 2;
108 
109 		/* Stop after MAX_TRIES tries */
110 		if (tries > MAX_TRIES) return (FALSE);
111 	}
112 
113 	/* Sound */
114 	sound(SOUND_TPOTHER);
115 
116 	/* Update the new location */
117 	area(nx, ny)->m_idx = m_idx;
118 
119 	/* Update the old location */
120 	area(ox, oy)->m_idx = 0;
121 
122 	/* Move the monster */
123 	m_ptr->fy = ny;
124 	m_ptr->fx = nx;
125 
126 	/* Update the monster (new location) */
127 	update_mon(m_idx, TRUE);
128 
129 	/* Process fields under the monster. */
130 	field_script(c_ptr, FIELD_ACT_MONSTER_ENTER, "");
131 
132 	/* Redraw the old grid */
133 	lite_spot(ox, oy);
134 
135 	/* Redraw the new grid */
136 	lite_spot(nx, ny);
137 
138 	/* Notice changes in view */
139 	if (FLAG(r_ptr, RF_LITE_1) ||  FLAG(r_ptr, RF_LITE_2))
140 	{
141 		/* Update some things */
142 		p_ptr->update |= (PU_MON_LITE);
143 	}
144 
145 	return (TRUE);
146 }
147 
148 
149 
150 /*
151  * Teleport monster next to the player
152  */
teleport_to_player(int m_idx)153 void teleport_to_player(int m_idx)
154 {
155 	int ny, nx, oy, ox, px, py, d, i, min;
156 	int attempts = 500;
157 	int dis = 2;
158 	bool look = TRUE;
159 	monster_type *m_ptr = &m_list[m_idx];
160 	monster_race *r_ptr = &r_info[m_ptr->r_idx];
161 	cave_type *c_ptr = NULL;
162 
163 	/* Paranoia */
164 	if (!m_ptr->r_idx) return;
165 
166 	/* "Skill" test */
167 	if (randint1(100) > r_info[m_ptr->r_idx].level) return;
168 
169 	/* Initialize */
170 	ny = m_ptr->fy;
171 	nx = m_ptr->fx;
172 
173 	/* Initialize */
174 	py = p_ptr->py;
175 	px = p_ptr->px;
176 
177 	/* Save the old location */
178 	oy = m_ptr->fy;
179 	ox = m_ptr->fx;
180 
181 	/* Minimum distance */
182 	min = dis / 2;
183 
184 	/* Look until done */
185 	while (look && --attempts)
186 	{
187 		/* Verify max distance */
188 		if (dis > 200) dis = 200;
189 
190 		/* Try several locations */
191 		for (i = 0; i < 500; i++)
192 		{
193 			/* Pick a (possibly illegal) location */
194 			while (1)
195 			{
196 				ny = rand_spread(py, dis);
197 				nx = rand_spread(px, dis);
198 				d = distance(px, py, nx, ny);
199 				if ((d >= min) && (d <= dis)) break;
200 			}
201 
202 			/* Ignore illegal locations */
203 			if (!in_bounds2(nx, ny)) continue;
204 
205 			c_ptr = area(nx, ny);
206 
207 			/* Check for a field that blocks movement */
208 			if (fields_have_flags(c_ptr, FIELD_INFO_NO_ENTER))
209 			{
210 				continue;
211 			}
212 
213 			/*
214 			 * Test for fields that will not allow monsters to
215 			 * be generated on them.  (i.e. Glyph of warding)
216 			 */
217 			if (fields_have_flags(c_ptr, FIELD_INFO_NO_MPLACE)) continue;
218 
219 			/* Require "empty" floor space */
220 			if (!cave_empty_grid(c_ptr)) continue;
221 
222 			/* Not on player */
223 			if ((ny == py) && (nx == px)) continue;
224 
225 			/* ...nor onto the Pattern */
226 			if (cave_pattern_grid(c_ptr)) continue;
227 
228 			/* No teleporting into vaults and such */
229 			/* if (c_ptr->info & (CAVE_ICKY)) continue; */
230 
231 			/* This grid looks good */
232 			look = FALSE;
233 
234 			/* Stop looking */
235 			break;
236 		}
237 
238 		/* Increase the maximum distance */
239 		dis = dis * 2;
240 
241 		/* Decrease the minimum distance */
242 		min = min / 2;
243 	}
244 
245 	if (attempts < 1) return;
246 
247 	/* Sound */
248 	sound(SOUND_TPOTHER);
249 
250 	/* Update the new location */
251 	area(nx, ny)->m_idx = m_idx;
252 
253 	/* Update the old location */
254 	area(ox, oy)->m_idx = 0;
255 
256 	/* Move the monster */
257 	m_ptr->fy = ny;
258 	m_ptr->fx = nx;
259 
260 	/* Update the monster (new location) */
261 	update_mon(m_idx, TRUE);
262 
263 	/* Process fields under the monster. */
264 	field_script(c_ptr, FIELD_ACT_MONSTER_ENTER, "");
265 
266 	/* Redraw the old grid */
267 	lite_spot(ox, oy);
268 
269 	/* Redraw the new grid */
270 	lite_spot(nx, ny);
271 
272 	/* Notice changes in view */
273 	if (FLAG(r_ptr, RF_LITE_1) || FLAG(r_ptr, RF_LITE_2))
274 	{
275 		/* Update some things */
276 		p_ptr->update |= (PU_MON_LITE);
277 	}
278 }
279 
280 
281 /*
282  * Teleport the player to a location up to "dis" grids away.
283  *
284  * If no such spaces are readily available, the distance may increase.
285  * Try very hard to move the player at least a quarter that distance.
286  *
287  * When long-range teleport effects are considered, there is a nasty
288  * tendency to "bounce" the player between two or three different spots
289  * because these are the only spots that are "far enough" way to satisfy
290  * the algorithm.  Therefore, if the teleport distance is more than 50,
291  * we decrease the minimum acceptable distance to try to increase randomness.
292  * -GJW
293  */
teleport_player(int dis)294 void teleport_player(int dis)
295 {
296 	int px = p_ptr->px;
297 	int py = p_ptr->py;
298 
299 	int d, i, min, ox, oy;
300 	int tries = 0;
301 
302 	int xx, yy;
303 
304 	/* Initialize */
305 	int y = py;
306 	int x = px;
307 
308 	monster_type *m_ptr;
309 	u16b m_idx;
310 
311 	bool look = TRUE;
312 	cave_type *c_ptr;
313 
314 	if (FLAG(p_ptr, TR_NO_TELE))
315 	{
316 		msgf("A mysterious force prevents you from teleporting!");
317 		return;
318 	}
319 
320 	if (dis > 200) dis = 200;	/* To be on the safe side... */
321 
322 	/* Minimum distance */
323 	min = dis / (dis > 50 ? 3 : 2);
324 
325 	/* Look until done */
326 	while (look)
327 	{
328 		tries++;
329 
330 		/* Verify max distance */
331 		if (dis > 200) dis = 200;
332 
333 		/* Try several locations */
334 		for (i = 0; i < 500; i++)
335 		{
336 			/* Pick a (possibly illegal) location */
337 			while (1)
338 			{
339 				y = rand_spread(py, dis);
340 				x = rand_spread(px, dis);
341 				d = distance(px, py, x, y);
342 				if ((d >= min) && (d <= dis)) break;
343 			}
344 
345 			/* Ignore illegal locations */
346 			if (!in_bounds2(x, y)) continue;
347 
348 			c_ptr = area(x, y);
349 
350 			/* Require empty space */
351 			if (!cave_empty_grid(c_ptr)) continue;
352 
353 			/* No non-movement */
354 			if ((y == py) && (x == px)) continue;
355 
356 			/* Check for a field that blocks movement */
357 			if (fields_have_flags(c_ptr, FIELD_INFO_NO_ENTER))
358 			{
359 				continue;
360 			}
361 
362 			/* No teleporting into vaults and such */
363 			if (c_ptr->info & CAVE_ICKY) continue;
364 
365 			/* This grid looks good */
366 			look = FALSE;
367 
368 			/* Stop looking */
369 			break;
370 		}
371 
372 		/* Increase the maximum distance */
373 		dis = dis * 2;
374 
375 		/* Decrease the minimum distance */
376 		min = min / 2;
377 
378 		/* Stop after MAX_TRIES tries */
379 		if (tries > MAX_TRIES) return;
380 	}
381 
382 	/* Sound */
383 	sound(SOUND_TELEPORT);
384 
385 	/* Save old location */
386 	oy = py;
387 	ox = px;
388 
389 	/* Move the player */
390 	py = y;
391 	px = x;
392 
393 	/* Move the player */
394 	p_ptr->py = y;
395 	p_ptr->px = x;
396 
397 	/* Notice movement */
398 	Term_move_player();
399 
400 	if (!p_ptr->depth)
401 	{
402 		/* Scroll wilderness */
403 		p_ptr->wilderness_x = px;
404 		p_ptr->wilderness_y = py;
405 		move_wild();
406 	}
407 
408 	/* Redraw the old spot */
409 	lite_spot(ox, oy);
410 
411 	/* Redraw the new spot */
412 	lite_spot(px, py);
413 
414 	/* Process fields under the player. */
415 	field_script(area(px, py), FIELD_ACT_PLAYER_ENTER, "");
416 
417 	/* Monsters with teleport ability may follow the player */
418 	for (xx = -1; xx <= 1; xx++)
419 	{
420 		for (yy = -1; yy <= 1; yy++)
421 		{
422 			if ((xx == 0) && (yy == 0))
423 			{
424 				/* Do nothing */
425 			}
426 			else
427 			{
428 				x = ox + xx;
429 				y = oy + yy;
430 
431 				if (in_bounds2(x, y) && area(x, y)->m_idx)
432 				{
433 					m_idx = area(x, y)->m_idx;
434 					m_ptr = &m_list[m_idx];
435 
436 					if ((FLAG(&r_info[m_ptr->r_idx], RF_TPORT)) &&
437 						!(FLAG(&r_info[m_ptr->r_idx], RF_RES_TELE)) &&
438 						!(m_ptr->csleep))
439 						/*
440 						 * The RES_TELE limitation is to avoid
441 						 * totally unkillable suckers...
442 						 */
443 					{
444 						teleport_to_player(m_idx);
445 					}
446 				}
447 			}
448 		}
449 	}
450 
451 	/* Check for new panel (redraw map) */
452 	verify_panel();
453 
454 	/* Update stuff */
455 	p_ptr->update |= (PU_VIEW | PU_FLOW | PU_MON_LITE);
456 
457 	/* Update the monsters */
458 	p_ptr->update |= (PU_DISTANCE);
459 
460 	/* Window stuff */
461 	p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
462 
463 	/* Handle stuff XXX XXX XXX */
464 	handle_stuff();
465 }
466 
467 
468 
469 /*
470  * Teleport player to a grid near the given location
471  *
472  * This function is slightly obsessive about correctness.
473  * This function allows teleporting into vaults (!)
474  */
teleport_player_to(int nx,int ny)475 void teleport_player_to(int nx, int ny)
476 {
477 	int py = p_ptr->py;
478 	int px = p_ptr->px;
479 
480 	int y, x, oy, ox, dis = 0, ctr = 0;
481 
482 	cave_type *c_ptr;
483 
484 	/* No movement at all */
485 	if ((ny == py) && (nx == px)) return;
486 
487 	if (FLAG(p_ptr, TR_NO_TELE))
488 	{
489 		msgf("A mysterious force prevents you from teleporting!");
490 		return;
491 	}
492 
493 	/* Find a usable location */
494 	while (1)
495 	{
496 		/* Pick a nearby legal location */
497 		while (1)
498 		{
499 			y = rand_spread(ny, dis);
500 			x = rand_spread(nx, dis);
501 			if (in_bounds2(x, y)) break;
502 		}
503 
504 		/* Accept "naked" floor grids */
505 		c_ptr = area(x, y);
506 
507 		/* No non-movement */
508 		if ((y == py) && (x == px)) continue;
509 
510 		/* Can enter grid? */
511 		if (cave_empty_grid(c_ptr) && !(c_ptr->info & CAVE_ICKY) &&
512 				!(fields_have_flags(c_ptr, FIELD_INFO_NO_ENTER)))
513 			break;
514 
515 		/* Occasionally advance the distance */
516 		if (++ctr > (4 * dis * dis + 4 * dis + 1))
517 		{
518 			ctr = 0;
519 			dis++;
520 		}
521 	}
522 
523 	/* Sound */
524 	sound(SOUND_TELEPORT);
525 
526 	/* Save old location */
527 	oy = py;
528 	ox = px;
529 
530 	/* Move the player */
531 	py = y;
532 	px = x;
533 
534 	/* Move the player */
535 	p_ptr->py = y;
536 	p_ptr->px = x;
537 
538 	/* Notice movement */
539 	Term_move_player();
540 
541 	if (!p_ptr->depth)
542 	{
543 		/* Scroll wilderness */
544 		p_ptr->wilderness_x = px;
545 		p_ptr->wilderness_y = py;
546 		move_wild();
547 	}
548 
549 	/* Redraw the old spot */
550 	lite_spot(ox, oy);
551 
552 	/* Redraw the new spot */
553 	lite_spot(px, py);
554 
555 	/* Process fields under the player. */
556 	field_script(area(px, py), FIELD_ACT_PLAYER_ENTER, "");
557 
558 	/* Check for new panel (redraw map) */
559 	verify_panel();
560 
561 	/* Update stuff */
562 	p_ptr->update |= (PU_VIEW | PU_FLOW | PU_MON_LITE);
563 
564 	/* Update the monsters */
565 	p_ptr->update |= (PU_DISTANCE);
566 
567 	/* Window stuff */
568 	p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
569 
570 	/* Handle stuff XXX XXX XXX */
571 	handle_stuff();
572 }
573 
574 
575 /*
576  * Teleport the player one level up or down (random when legal)
577  */
teleport_player_level(void)578 void teleport_player_level(void)
579 {
580 	/* No effect in final quest */
581 	if (is_special_level(p_ptr->depth) && ironman_downward)
582 	{
583 		msgf("There is no effect.");
584 		return;
585 	}
586 
587 	if (!check_down_wild())
588 	{
589 		msgf("There is no effect.");
590 		return;
591 	}
592 
593 	if (FLAG(p_ptr, TR_NO_TELE))
594 	{
595 		msgf("A mysterious force prevents you from teleporting!");
596 		return;
597 	}
598 
599 	if (!p_ptr->depth || ironman_downward)
600 	{
601 		msgf(MSGT_TPLEVEL, "You sink through the floor.");
602 
603 		/* Go down */
604 		move_dun_level(1);
605 	}
606 	else if (is_special_level(p_ptr->depth))
607 	{
608 		msgf(MSGT_TPLEVEL, "You rise up through the ceiling.");
609 
610 		/* Go up */
611 		move_dun_level(-1);
612 	}
613 	else if (one_in_(2) || (p_ptr->depth >= dungeon()->max_level))
614 	{
615 		msgf(MSGT_TPLEVEL, "You rise up through the ceiling.");
616 
617 		/* Go down */
618 		move_dun_level(-1);
619 	}
620 	else
621 	{
622 		msgf(MSGT_TPLEVEL, "You sink through the floor.");
623 
624 		/* Go up */
625 	    move_dun_level(1);
626 	}
627 
628 	/* Sound */
629 	sound(SOUND_TPLEVEL);
630 }
631 
632 
check_down_wild(void)633 bool check_down_wild(void)
634 {
635 	place_type *pl_ptr;
636 
637 	/* Can always recall from dungeon */
638 	if (p_ptr->depth) return (TRUE);
639 
640 	/* Hack - no recalling in the middle of the wilderness */
641 	if (!p_ptr->place_num)
642 	{
643 		msgf("Nothing happens.");
644 		return (FALSE);
645 	}
646 
647 	/* Cannot recall in towns with no dungeon */
648 	if (!vanilla_town)
649 	{
650 		pl_ptr = &place[p_ptr->place_num];
651 
652 		/* Look for dungeon */
653 		if (!pl_ptr->dungeon)
654 		{
655 			msgf("Nothing happens.");
656 			return (FALSE);
657 		}
658 
659 		/* Else we must have been down before */
660 		if (!pl_ptr->dungeon->recall_depth)
661 		{
662 			msgf("You need to visit the dungeon first.");
663 			return (FALSE);
664 		}
665 	}
666 
667 	return (TRUE);
668 }
669 
670 
671 /*
672  * Recall the player to town or dungeon
673  */
recall_player(int turns)674 void recall_player(int turns)
675 {
676 	dun_type *d_ptr = dungeon();
677 
678 	/*
679 	 * TODO: Recall the player to the last
680 	 * visited town when in the wilderness
681 	 */
682 
683 	/* Ironman option */
684 	if (ironman_downward)
685 	{
686 		msgf("Nothing happens.");
687 		return;
688 	}
689 
690 	if (!check_down_wild()) return;
691 
692 	if (p_ptr->depth && (d_ptr->recall_depth > p_ptr->depth))
693 	{
694 		if (get_check("Reset recall depth? "))
695 			d_ptr->recall_depth = (char) p_ptr->depth;
696 
697 	}
698 	else if (p_ptr->depth > d_ptr->recall_depth)
699 	{
700 		d_ptr->recall_depth = (char) p_ptr->depth;
701 	}
702 
703 	if (!p_ptr->tim.word_recall)
704 	{
705 		p_ptr->tim.word_recall = turns;
706 		msgf("The air about you becomes charged...");
707 		p_ptr->redraw |= (PR_STATUS);
708 	}
709 	else
710 	{
711 		p_ptr->tim.word_recall = 0;
712 		msgf("A tension leaves the air around you...");
713 		p_ptr->redraw |= (PR_STATUS);
714 	}
715 }
716 
717 
word_of_recall(void)718 void word_of_recall(void)
719 {
720 	recall_player(rand_range(15, 35));
721 }
722 
723 
724 /*
725  * Apply disenchantment to the player's stuff
726  *
727  * XXX XXX XXX This function is also called from the "melee" code
728  *
729  * Return "TRUE" if the player notices anything
730  */
apply_disenchant(void)731 bool apply_disenchant(void)
732 {
733 	int t = 0;
734 	object_type *o_ptr;
735 
736 	/* Pick a random slot */
737 	switch (randint1(8))
738 	{
739 		case 1:
740 		{
741 			t = EQUIP_WIELD;
742 			break;
743 		}
744 		case 2:
745 		{
746 			t = EQUIP_BOW;
747 			break;
748 		}
749 		case 3:
750 		{
751 			t = EQUIP_BODY;
752 			break;
753 		}
754 		case 4:
755 		{
756 			t = EQUIP_OUTER;
757 			break;
758 		}
759 		case 5:
760 		{
761 			t = EQUIP_ARM;
762 			break;
763 		}
764 		case 6:
765 		{
766 			t = EQUIP_HEAD;
767 			break;
768 		}
769 		case 7:
770 		{
771 			t = EQUIP_HANDS;
772 			break;
773 		}
774 		case 8:
775 		{
776 			t = EQUIP_FEET;
777 			break;
778 		}
779 	}
780 
781 	/* Get the item */
782 	o_ptr = &p_ptr->equipment[t];
783 
784 	/* No item, nothing happens */
785 	if (!o_ptr->k_idx) return (FALSE);
786 
787 
788 	/* Nothing to disenchant */
789 	if ((o_ptr->to_h <= 0) && (o_ptr->to_d <= 0) && (o_ptr->to_a <= 0))
790 	{
791 		/* Nothing to notice */
792 		return (FALSE);
793 	}
794 
795 
796 	/* Artifacts have 71% chance to resist */
797 	if ((FLAG(o_ptr, TR_INSTA_ART)) && (randint0(100) < 71))
798 	{
799 		/* Message */
800 		msgf("Your %v (%c) resist%s disenchantment!",
801 				OBJECT_FMT(o_ptr, FALSE, 0), I2A(t),
802 				((o_ptr->number != 1) ? "" : "s"));
803 
804 		/* Notice */
805 		return (TRUE);
806 	}
807 
808 	/* Message */
809 	msgf("Your %v (%c) %s disenchanted!",
810 			OBJECT_FMT(o_ptr, FALSE, 0), I2A(t),
811 			((o_ptr->number != 1) ? "were" : "was"));
812 
813 	/* Disenchant tohit */
814 	if (o_ptr->to_h > 0) o_ptr->to_h--;
815 	if ((o_ptr->to_h > 10) && (randint0(100) < 20)) o_ptr->to_h--;
816 
817 	/* Disenchant todam */
818 	if (o_ptr->to_d > 0) o_ptr->to_d--;
819 	if ((o_ptr->to_d > 10) && (randint0(100) < 20)) o_ptr->to_d--;
820 
821 	/* Disenchant toac */
822 	if (o_ptr->to_a > 0) o_ptr->to_a--;
823 	if ((o_ptr->to_a > 10) && (randint0(100) < 20)) o_ptr->to_a--;
824 
825 	/* Trigger scripts */
826 	apply_object_trigger(TRIGGER_ALTER, o_ptr, "");
827 
828 
829 	chg_virtue(V_HARMONY, 1);
830 	chg_virtue(V_ENCHANT, -2);
831 
832 	/* Recalculate bonuses */
833 	p_ptr->update |= (PU_BONUS);
834 
835 	/* Notice changes */
836 	notice_equip();
837 
838 	/* Notice */
839 	return (TRUE);
840 }
841 
842 
mutate_player(void)843 void mutate_player(void)
844 {
845 	int max1, cur1, max2, cur2, ii, jj;
846 	int bonus1, bonus2;
847 
848 	/* Pick a pair of stats */
849 	ii = randint0(A_MAX);
850 	for (jj = ii; jj == ii; jj = randint0(A_MAX)) /* loop */ ;
851 
852 	max1 = p_ptr->stat[ii].max;
853 	cur1 = p_ptr->stat[ii].cur;
854 	max2 = p_ptr->stat[jj].max;
855 	cur2 = p_ptr->stat[jj].cur;
856 
857 	/* Adjust the swapped stats... */
858 	bonus1 = rp_ptr->r_adj[ii] + cp_ptr->c_adj[ii];
859 	bonus2 = rp_ptr->r_adj[jj] + cp_ptr->c_adj[jj];
860 
861 	max1 = adjust_stat(jj, max1, bonus2 - bonus1);
862 	max2 = adjust_stat(ii, max2, bonus1 - bonus2);
863 
864 	/* Hack - restore both stats rather than figure try to swap drainage */
865 	cur1 = max1;
866 	cur2 = max2;
867 
868 	p_ptr->stat[ii].max = max2;
869 	p_ptr->stat[ii].cur = cur2;
870 	p_ptr->stat[jj].max = max1;
871 	p_ptr->stat[jj].cur = cur1;
872 
873 	p_ptr->update |= (PU_BONUS);
874 }
875 
876 
877 /*
878  * Apply Nexus
879  */
apply_nexus(const monster_type * m_ptr)880 void apply_nexus(const monster_type *m_ptr)
881 {
882 	monster_race *r_ptr = &r_info[m_ptr->r_idx];
883 
884 	switch (randint1(7))
885 	{
886 		case 1:  case 2:  case 3:
887 		{
888 			teleport_player(200);
889 			break;
890 		}
891 
892 		case 4:  case 5:
893 		{
894 			teleport_player_to(m_ptr->fx, m_ptr->fy);
895 			break;
896 		}
897 
898 		case 6:
899 		{
900 			if (player_save(r_ptr->hdice * 2))
901 			{
902 				msgf("You resist the effects!");
903 				break;
904 			}
905 
906 			/* Teleport Level */
907 			teleport_player_level();
908 			break;
909 		}
910 
911 		case 7:
912 		{
913 			if (player_save(r_ptr->hdice * 2))
914 			{
915 				msgf("You resist the effects!");
916 				break;
917 			}
918 
919 			msgf("Your body starts to scramble...");
920 			mutate_player();
921 			break;
922 		}
923 	}
924 }
925 
926 
927 /*
928  * Charge a lite (torch or latern)
929  */
phlogiston(void)930 void phlogiston(void)
931 {
932 	int max_flog;
933 	object_type *o_ptr = &p_ptr->equipment[EQUIP_LITE];
934 	cptr lite_item = NULL;
935 
936 
937 	/* It's a lamp */
938 	if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_LANTERN))
939 	{
940 		max_flog = FUEL_LAMP;
941 
942 		/* Remember what the item is */
943 		lite_item = "lantern";
944 	}
945 
946 	/* It's a torch */
947 	else if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
948 	{
949 		max_flog = FUEL_TORCH;
950 
951 		/* Remember what the item is */
952 		lite_item = "torch";
953 	}
954 
955 	/* No torch to refill */
956 	else
957 	{
958 		msgf("You are not wielding anything which uses phlogiston.");
959 		return;
960 	}
961 
962 	if (o_ptr->timeout >= max_flog)
963 	{
964 		msgf("No more phlogiston can be put in this %s.", lite_item);
965 		return;
966 	}
967 
968 	/* Refuel */
969 	o_ptr->timeout += (max_flog / 2);
970 
971 	/* Message */
972 	msgf("You add phlogiston to your %s.", lite_item);
973 
974 	/* Comment */
975 	if (o_ptr->timeout >= max_flog)
976 	{
977 		o_ptr->timeout = max_flog;
978 		msgf("Your %s is full.", lite_item);
979 	}
980 
981 	/* Recalculate torch */
982 	p_ptr->update |= (PU_TORCH);
983 
984 	/* Window stuff */
985 	p_ptr->window |= (PW_EQUIP);
986 }
987 
988 
989 /*
990  * Brand the current weapon
991  */
brand_weapon(int brand_type)992 void brand_weapon(int brand_type)
993 {
994 	object_type *o_ptr = &p_ptr->equipment[EQUIP_WIELD];
995 
996 	byte ego = 0;
997 
998 	/* you can never modify artifacts / ego-items */
999 	/* you can never modify cursed items */
1000 	/* TY: You _can_ modify broken items (if you're silly enough) */
1001 	if (o_ptr->k_idx && !o_ptr->xtra_name && !cursed_p(o_ptr))
1002 	{
1003 		cptr act;
1004 
1005 		switch (brand_type)
1006 		{
1007 			case 1:
1008 			{
1009 				act = "is engulfed in raw Logrus!";
1010 				ego = EGO_CHAOTIC;
1011 				break;
1012 			}
1013 			case 2:
1014 			{
1015 				act = "is coated with poison.";
1016 				ego = EGO_BRAND_POIS;
1017 				break;
1018 			}
1019 			case 3:
1020 			{
1021 				act = "thirsts for blood!";
1022 				ego = EGO_VAMPIRIC;
1023 				break;
1024 			}
1025 			case 4:
1026 			{
1027 				act = "seems very unstable now.";
1028 				ego = EGO_TRUMP;
1029 				o_ptr->pval = randint1(2);
1030 				break;
1031 			}
1032 
1033 			default:
1034 			{
1035 				if (randint0(100) < 25)
1036 				{
1037 					act = "is covered in a fiery shield!";
1038 					ego = EGO_BRAND_FIRE;
1039 				}
1040 				else
1041 				{
1042 					act = "glows deep, icy blue!";
1043 					ego = EGO_BRAND_COLD;
1044 				}
1045 			}
1046 		}
1047 
1048 		msgf("Your %v %s", OBJECT_FMT(o_ptr, FALSE, 0), act);
1049 
1050 		(void)enchant(o_ptr, rand_range(4, 6), ENCH_TOHIT | ENCH_TODAM);
1051 	}
1052 	else
1053 	{
1054 		if (flush_failure) flush();
1055 
1056 		msgf("The Branding failed.");
1057 
1058 		chg_virtue(V_ENCHANT, -2);
1059 	}
1060 
1061 	if (ego)
1062 	{
1063 		/* Hack - save the price */
1064 		s32b cost = o_ptr->cost;
1065 
1066 		add_ego_flags(o_ptr, ego);
1067 
1068 		o_ptr->cost = cost;
1069 
1070 		/* Recalculate bonuses */
1071 		p_ptr->update |= (PU_BONUS);
1072 
1073 		/* Recalculate mana */
1074 		p_ptr->update |= (PU_MANA);
1075 
1076 		/* Window stuff */
1077 		p_ptr->window |= (PW_PLAYER);
1078 
1079 		/* Notice changes */
1080 		notice_item();
1081 	}
1082 }
1083 
1084 
call_the_(void)1085 void call_the_(void)
1086 {
1087 	int py = p_ptr->py;
1088 	int px = p_ptr->px;
1089 
1090 	int i;
1091 
1092 	if (in_bounds(px, py) &&
1093 		cave_floor_grid(area(px - 1, py - 1)) &&
1094 		cave_floor_grid(area(px - 1, py)) &&
1095 		cave_floor_grid(area(px - 1, py + 1)) &&
1096 		cave_floor_grid(area(px, py - 1)) &&
1097 		cave_floor_grid(area(px, py + 1)) &&
1098 		cave_floor_grid(area(px + 1, py - 1)) &&
1099 		cave_floor_grid(area(px + 1, py)) &&
1100 		cave_floor_grid(area(px + 1, py + 1)))
1101 	{
1102 		for (i = 1; i < 10; i++)
1103 		{
1104 			if (i != 5) (void)fire_ball(GF_ROCKET, i, 175, 2);
1105 		}
1106 
1107 		for (i = 1; i < 10; i++)
1108 		{
1109 			if (i != 5) (void)fire_ball(GF_MANA, i, 175, 3);
1110 		}
1111 
1112 		for (i = 1; i < 10; i++)
1113 		{
1114 			if (i != 5) (void)fire_ball(GF_NUKE, i, 175, 4);
1115 		}
1116 	}
1117 	else
1118 	{
1119 		msgf("You %s the %s too close to a wall!",
1120 				   ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "recite" : "cast"),
1121 				   ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "prayer" : "spell"));
1122 		msgf("There is a loud explosion!");
1123 
1124 		if (destroy_area(px, py, 20 + p_ptr->lev))
1125 			msgf("The dungeon collapses...");
1126 		else
1127 			msgf("The dungeon trembles.");
1128 
1129 		take_hit(rand_range(100, 250), "a suicidal Call the Void");
1130 	}
1131 }
1132 
1133 
1134 /*
1135  * Fetch an item (teleport it right underneath the caster)
1136  *
1137  * This is a massive hack.
1138  */
fetch(int dir,int wgt,bool require_los)1139 void fetch(int dir, int wgt, bool require_los)
1140 {
1141 	int py = p_ptr->py;
1142 	int px = p_ptr->px;
1143 
1144 	int tx, ty;
1145 	cave_type *c_ptr;
1146 	object_type *o_ptr;
1147 
1148 	/* Check to see if an object is already there */
1149 	if (area(px, py)->o_idx)
1150 	{
1151 		msgf("You can't fetch when you're already standing on something.");
1152 		return;
1153 	}
1154 
1155 	/* Use a target */
1156 	if ((dir == 5) && target_okay())
1157 	{
1158 		tx = p_ptr->target_col;
1159 		ty = p_ptr->target_row;
1160 
1161 		/* Paranoia */
1162 		if ((distance(px, py, tx, ty) > MAX_RANGE) || (!in_bounds2(tx, ty)))
1163 		{
1164 			msgf("You can't fetch something that far away!");
1165 			return;
1166 		}
1167 
1168 		c_ptr = area(tx, ty);
1169 
1170 		/* We need an item to fetch */
1171 		if (!c_ptr->o_idx)
1172 		{
1173 			msgf("There is no object at this place.");
1174 			return;
1175 		}
1176 
1177 		/* No fetching from vault */
1178 		if (c_ptr->info & CAVE_ICKY)
1179 		{
1180 			msgf("The item slips from your control.");
1181 			return;
1182 		}
1183 
1184 		/* We need to see the item */
1185 		if (require_los && !player_has_los_grid(parea(tx, ty)))
1186 		{
1187 			msgf("You have no direct line of sight to that location.");
1188 			return;
1189 		}
1190 	}
1191 	else
1192 	{
1193 		/* Use a direction */
1194 		ty = py;				/* Where to drop the item */
1195 		tx = px;
1196 
1197 		while (TRUE)
1198 		{
1199 			ty += ddy[dir];
1200 			tx += ddx[dir];
1201 
1202 			/* paranoia */
1203 			if (!in_bounds2(tx, ty)) continue;
1204 
1205 			c_ptr = area(tx, ty);
1206 
1207 			if ((distance(px, py, tx, ty) > MAX_RANGE) ||
1208 				cave_wall_grid(c_ptr)) return;
1209 
1210 			/* found a spot */
1211 			if (!c_ptr->o_idx) break;
1212 		}
1213 	}
1214 
1215 	o_ptr = &o_list[c_ptr->o_idx];
1216 
1217 	if (o_ptr->weight > wgt)
1218 	{
1219 		/* Too heavy to 'fetch' */
1220 		msgf("The object is too heavy.");
1221 		return;
1222 	}
1223 
1224 	/*
1225 	 * Hack - do not get artifacts.
1226 	 * This interacts badly with preserve mode.
1227 	 */
1228 	if (FLAG(o_ptr, TR_INSTA_ART))
1229 	{
1230 		msgf("The object seems to have a will of its own!");
1231 		return;
1232 	}
1233 
1234 	/* Move the object */
1235 	move_object(&area(px, py)->o_idx, &c_ptr->o_idx, o_ptr);
1236 
1237 	/* Record the new location */
1238 	o_ptr->ix = px;
1239 	o_ptr->iy = py;
1240 
1241 	msgf("%^v flies through the air to your feet.", OBJECT_FMT(o_ptr, TRUE, 0));
1242 
1243 	/* Notice the moved object (The player gets redrawn) */
1244 	note_spot(px, py);
1245 
1246 	/* Redraw the map???  Can we just use lite_spot() a few times? */
1247 	p_ptr->redraw |= PR_MAP;
1248 }
1249 
1250 
alter_reality(void)1251 void alter_reality(void)
1252 {
1253 	if (p_ptr->depth)
1254 	{
1255 		msgf("The world changes!");
1256 
1257 		/* Leaving */
1258 		p_ptr->state.leaving = TRUE;
1259 	}
1260 	else
1261 	{
1262 		msgf("The world seems to change for a moment!");
1263 	}
1264 }
1265 
1266 
1267 /*
1268  * Leave a "glyph of warding" which prevents monster movement
1269  */
warding_glyph(void)1270 bool warding_glyph(void)
1271 {
1272 	int py = p_ptr->py;
1273 	int px = p_ptr->px;
1274 
1275 	cave_type *c_ptr = area(px, py);
1276 
1277 	/* XXX XXX XXX */
1278 	if (!cave_naked_grid(c_ptr))
1279 	{
1280 		msgf("The object resists the spell.");
1281 		return FALSE;
1282 	}
1283 
1284 	/* Not in a wall */
1285 	if (cave_wall_grid(c_ptr))
1286 	{
1287 		msgf("You need open space to draw the rune.");
1288 		return FALSE;
1289 	}
1290 
1291 	/* Add the glyph here as a field */
1292 	(void)place_field(px, py, FT_GLYPH_WARDING);
1293 
1294 	/* Notice it */
1295 	note_spot(px, py);
1296 
1297 	return TRUE;
1298 }
1299 
1300 
1301 /*
1302  * Leave an "explosive rune" which prevents monster movement
1303  */
explosive_rune(void)1304 bool explosive_rune(void)
1305 {
1306 	int py = p_ptr->py;
1307 	int px = p_ptr->px;
1308 
1309 	cave_type *c_ptr = area(px, py);
1310 
1311 	/* XXX XXX XXX */
1312 	if (!cave_naked_grid(c_ptr))
1313 	{
1314 		msgf("The object resists the spell.");
1315 		return FALSE;
1316 	}
1317 
1318 	/* Not in a wall */
1319 	if (cave_wall_grid(c_ptr))
1320 	{
1321 		msgf("You need open space to draw the rune.");
1322 		return FALSE;
1323 	}
1324 
1325 	/* Add the glyph here as a field */
1326 	(void)place_field(px, py, FT_GLYPH_EXPLODE);
1327 
1328 	/* Notice it */
1329 	note_spot(px, py);
1330 
1331 	return TRUE;
1332 }
1333 
1334 
1335 /*
1336  * Identify everything being carried.
1337  * Done by a potion of "self knowledge".
1338  */
identify_pack(void)1339 void identify_pack(void)
1340 {
1341 	int i;
1342 	object_type *o_ptr;
1343 
1344 	/* Identify equipment */
1345 	for (i = 0; i < EQUIP_MAX; i++)
1346 	{
1347 		o_ptr = &p_ptr->equipment[i];
1348 
1349 		/* Skip non-objects */
1350 		if (!o_ptr->k_idx) continue;
1351 
1352 		/* Identify it */
1353 		identify_item(o_ptr);
1354 	}
1355 
1356 	/* Identify inventory */
1357 	OBJ_ITT_START (p_ptr->inventory, o_ptr)
1358 	{
1359 		/* Identify it */
1360 		identify_item(o_ptr);
1361 	}
1362 	OBJ_ITT_END;
1363 
1364 	/* Notice changes */
1365 	notice_inven();
1366 }
1367 
1368 /*
1369  * Try to remove a curse from an item
1370  *
1371  * Note that Items which are "Perma-Cursed" (The One Ring,
1372  * The Crown of Morgoth) can NEVER be uncursed.
1373  *
1374  * Note that if "all" is FALSE, then Items which are
1375  * "Heavy-Cursed" (Mormegil, Calris, and Weapons of Morgul)
1376  * will not be uncursed.
1377  *
1378  * If the item is heavily or permanently cursed, we add that flag
1379  * to the 'known' flags so the player can see that it is cursed on
1380  * the 'C'haracter screen. This may allow a player to learn that
1381  * an unidentified scroll is remove curse when it has no apparent
1382  * effect, in rare circumstances.
1383  */
uncurse_item(object_type * o_ptr,bool all)1384 static bool uncurse_item(object_type *o_ptr, bool all)
1385 {
1386 	bool heavy;
1387 
1388 	/* Uncursed already */
1389 	if (!cursed_p(o_ptr)) return (FALSE);
1390 
1391 	/* Heavily Cursed Items need a special spell */
1392 	if (!all && (FLAG(o_ptr, TR_HEAVY_CURSE)))
1393 	{
1394 		/* Let the player know */
1395 		o_ptr->kn_flags[2] |= TR2_HEAVY_CURSE;
1396 
1397 		/* Done */
1398 		return (FALSE);
1399 	}
1400 
1401 	/* Perma-Cursed Items can NEVER be uncursed */
1402 	if (FLAG(o_ptr, TR_PERMA_CURSE))
1403 	{
1404 		/* Let the player know */
1405 		o_ptr->kn_flags[2] |= TR2_PERMA_CURSE;
1406 
1407 		/* Done */
1408 		return (FALSE);
1409 	}
1410 
1411 	/* Uncurse the item */
1412 	o_ptr->flags[2] &= ~(TR2_CURSED | TR2_HEAVY_CURSE);
1413 	o_ptr->kn_flags[2] &= ~(TR2_CURSED | TR2_HEAVY_CURSE);
1414 
1415 	/* Heavy sensing? */
1416 	heavy = class_info[p_ptr->rp.pclass].heavy_sense;
1417 
1418 	/* Take away the feeling */
1419 	o_ptr->feeling = FEEL_NONE;
1420 
1421 	/* Strip awareness of feeling */
1422 	o_ptr->info &= ~(OB_SENSE);
1423 
1424 	/* Renew feeling */
1425 	sense_item(o_ptr, heavy, TRUE, FALSE);
1426 
1427 	/* Recalculate the bonuses */
1428 	p_ptr->update |= (PU_BONUS);
1429 
1430 	/* Notice changes */
1431 	notice_item();
1432 
1433 	return (TRUE);
1434 }
1435 
1436 /*
1437  * Removes curses from items in inventory
1438  */
remove_curse_aux(bool all)1439 static int remove_curse_aux(bool all)
1440 {
1441 	int i, cnt = 0;
1442 	object_type *o_ptr;
1443 
1444 	/* Attempt to uncurse equipment */
1445 	for (i = 0; i < EQUIP_MAX; i++)
1446 	{
1447 		o_ptr = &p_ptr->equipment[i];
1448 
1449 		/* Skip non-objects */
1450 		if (!o_ptr->k_idx) continue;
1451 
1452 		/* Count the uncursings */
1453 		if (uncurse_item(o_ptr, all)) cnt++;
1454 	}
1455 
1456 	/* Attempt to uncurse inventory */
1457 	OBJ_ITT_START (p_ptr->inventory, o_ptr)
1458 	{
1459 		/* Count the uncursings */
1460 		if (uncurse_item(o_ptr, all)) cnt++;
1461 	}
1462 	OBJ_ITT_END;
1463 
1464 	/* Return "something uncursed" */
1465 	return (cnt);
1466 }
1467 
1468 
1469 /*
1470  * Remove most curses
1471  */
remove_curse(void)1472 bool remove_curse(void)
1473 {
1474 	return (remove_curse_aux(FALSE));
1475 }
1476 
1477 /*
1478  * Remove all curses
1479  */
remove_all_curse(void)1480 bool remove_all_curse(void)
1481 {
1482 	return (remove_curse_aux(TRUE));
1483 }
1484 
1485 
1486 /*
1487  * Turns an object into gold, gain some of its value in a shop
1488  */
alchemy(void)1489 bool alchemy(void)
1490 {
1491 	int amt = 1;
1492 	int old_number;
1493 	long price;
1494 	bool force = FALSE;
1495 	object_type *o_ptr;
1496 	char o_name[256];
1497 
1498 	cptr q, s;
1499 
1500 	/* Hack -- force destruction */
1501 	if (p_ptr->cmd.arg > 0) force = TRUE;
1502 
1503 	/* Get an item */
1504 	q = "Turn which item to gold? ";
1505 	s = "You have nothing to turn to gold.";
1506 
1507 	o_ptr = get_item(q, s, (USE_INVEN | USE_FLOOR));
1508 
1509 	/* Not a valid item */
1510 	if (!o_ptr) return (FALSE);
1511 
1512 	/* See how many items */
1513 	if (o_ptr->number > 1)
1514 	{
1515 		/* Get a quantity */
1516 		amt = get_quantity(NULL, o_ptr->number);
1517 
1518 		/* Allow user abort */
1519 		if (amt <= 0) return FALSE;
1520 	}
1521 
1522 
1523 	/* Describe the object */
1524 	old_number = o_ptr->number;
1525 	o_ptr->number = amt;
1526 	object_desc(o_name, o_ptr, TRUE, 3, 256);
1527 	o_ptr->number = old_number;
1528 
1529 	/* Verify unless quantity given */
1530 	if (!force)
1531 	{
1532 		if (!(auto_destroy && (object_value(o_ptr) < 1)))
1533 		{
1534 			/* Make a verification */
1535 			if (!get_check("Really turn %s to gold? ", o_name)) return FALSE;
1536 		}
1537 	}
1538 
1539 	/* Check for artifacts */
1540 	if (!can_player_destroy_object(o_ptr))
1541 	{
1542 		/* Message */
1543 		msgf("You fail to turn %s to gold!", o_name);
1544 
1545 		/* Done */
1546 		return FALSE;
1547 	}
1548 
1549 	price = object_value_real(o_ptr);
1550 
1551 	if (price <= 0)
1552 	{
1553 		/* Message */
1554 		msgf("You turn %s to fool's gold.", o_name);
1555 	}
1556 	else
1557 	{
1558 		price /= 3;
1559 
1560 		if (amt > 1) price *= amt;
1561 
1562 		if (price > 30000) price = 30000;
1563 		msgf("You turn %s to %ld coins worth of gold.", o_name, price);
1564 		p_ptr->au += price;
1565 
1566 		/* Redraw gold */
1567 		p_ptr->redraw |= (PR_GOLD);
1568 
1569 		/* Window stuff */
1570 		p_ptr->window |= (PW_PLAYER);
1571 
1572 	}
1573 
1574 	/* Eliminate the item */
1575 	item_increase(o_ptr, -amt);
1576 
1577 	return TRUE;
1578 }
1579 
1580 
1581 /*
1582  * Create stairs at the player location
1583  */
stair_creation(void)1584 void stair_creation(void)
1585 {
1586 	int py = p_ptr->py;
1587 	int px = p_ptr->px;
1588 
1589 	cave_type *c_ptr = area(px, py);
1590 
1591 	/* XXX XXX XXX */
1592 	if (!cave_valid_grid(c_ptr))
1593 	{
1594 		msgf("The object resists the spell.");
1595 		return;
1596 	}
1597 
1598 	if (!check_down_wild()) return;
1599 
1600 	/* XXX XXX XXX */
1601 	delete_object(px, py);
1602 
1603 	if (!p_ptr->depth || ironman_downward)
1604 	{
1605 		/* Town/wilderness or Ironman */
1606 		cave_set_feat(px, py, FEAT_MORE);
1607 	}
1608 	else if (is_special_level(p_ptr->depth))
1609 	{
1610 		/* Quest level */
1611 		cave_set_feat(px, py, FEAT_LESS);
1612 	}
1613 	else if (one_in_(2) || (p_ptr->depth >= dungeon()->max_level))
1614 	{
1615 		cave_set_feat(px, py, FEAT_LESS);
1616 	}
1617 	else
1618 	{
1619 		cave_set_feat(px, py, FEAT_MORE);
1620 	}
1621 }
1622 
1623 
1624 
1625 
1626 /*
1627  * Break the curse of an item
1628  */
break_curse(object_type * o_ptr)1629 static void break_curse(object_type *o_ptr)
1630 {
1631 	if (cursed_p(o_ptr) && !(FLAG(o_ptr, TR_PERMA_CURSE))
1632 		 && (randint0(100) < 25))
1633 	{
1634 		msgf("The curse is broken!");
1635 
1636 		/* Uncurse it */
1637 		uncurse_item(o_ptr, TRUE);
1638 	}
1639 }
1640 
1641 
1642 #define ENCHANT_MAX_DAM 25
1643 #define ENCHANT_MAX 15
1644 
1645 /*
1646  * Used by the "enchant" function (chance of failure)
1647  *
1648  * Formula: 1000-0.064x^3
1649  */
1650 static int enchant_table_dam[ENCHANT_MAX_DAM + 1] =
1651 {
1652 	0, 115, 221, 319, 407,
1653 	488, 561, 627, 686, 738,
1654 	784, 824, 859, 889, 914,
1655 	936, 953, 967, 978, 986,
1656 	992, 996, 998, 999, 999,
1657 	1000
1658 };
1659 
1660 /*
1661  * Used by the "enchant" function (chance of failure)
1662  */
1663 static int enchant_table[ENCHANT_MAX + 1] =
1664 {
1665 	0, 10, 50, 100, 200,
1666 	300, 400, 500, 650, 800,
1667 	950, 987, 993, 995, 998,
1668 	1000
1669 };
1670 
1671 
1672 /*
1673  * Enchants a plus onto an item. -RAK-
1674  *
1675  * Revamped!  Now takes item pointer, number of times to try enchanting,
1676  * and a flag of what to try enchanting.  Artifacts resist enchantment
1677  * some of the time, and successful enchantment to at least +0 might
1678  * break a curse on the item. -CFT-
1679  *
1680  * Note that an item can technically be enchanted all the way to +15 if
1681  * you wait a very, very, long time.  Going from +9 to +10 only works
1682  * about 5% of the time, and from +10 to +11 only about 1% of the time.
1683  *
1684  * Note that this function can now be used on "piles" of items, and
1685  * the larger the pile, the lower the chance of success.
1686  */
enchant(object_type * o_ptr,int n,int eflag)1687 bool enchant(object_type *o_ptr, int n, int eflag)
1688 {
1689 	int i, chance, prob, change;
1690 	bool res = FALSE;
1691 	bool a = ((FLAG(o_ptr, TR_INSTA_ART)) ? TRUE : FALSE);
1692 	bool force = (eflag & ENCH_FORCE);
1693 
1694 
1695 	/* Large piles resist enchantment */
1696 	prob = o_ptr->number * 100;
1697 
1698 	/* Missiles are easy to enchant */
1699 	if ((o_ptr->tval == TV_BOLT) ||
1700 		(o_ptr->tval == TV_ARROW) || (o_ptr->tval == TV_SHOT))
1701 	{
1702 		prob = prob / 50;
1703 	}
1704 
1705 	/* Some items are easier to enchant */
1706 	if (FLAG(o_ptr, TR_EASY_ENCHANT))
1707 	{
1708 		/* Don't apply artifact failure chance */
1709 		a = FALSE;
1710 
1711 		/* Apply more enchantment attempts */
1712 		n *= 2;
1713 	}
1714 
1715 	/* Try "n" times */
1716 	for (i = 0; i < n; i++)
1717 	{
1718 		/* Hack -- Roll for pile resistance */
1719 		if (!force && randint0(prob) >= 100) continue;
1720 
1721 		/* Enchant to hit */
1722 		if (eflag & ENCH_TOHIT)
1723 		{
1724 			if (o_ptr->to_h < 0) chance = 0;
1725 			else if (o_ptr->to_h > ENCHANT_MAX) chance = 1000;
1726 			else
1727 				chance = enchant_table[o_ptr->to_h];
1728 
1729 			if (force || ((randint1(1000) > chance) && (!a || one_in_(2))))
1730 			{
1731 				/* The amount you enchant varys */
1732 				if ((o_ptr->to_h > 7) || force) change = 1;
1733 				else if (o_ptr->to_h > 4) change = randint1(2);
1734 				else
1735 					change = randint1(3);
1736 
1737 				o_ptr->to_h += change;
1738 				res = TRUE;
1739 
1740 				/* only when you get it above -1 -CFT */
1741 				if (o_ptr->to_h >= 0)
1742 					break_curse(o_ptr);
1743 			}
1744 		}
1745 
1746 		/* Enchant to damage */
1747 		if (eflag & ENCH_TODAM)
1748 		{
1749 			if (o_ptr->to_d < 0) chance = 0;
1750 			else if (o_ptr->to_d > ENCHANT_MAX_DAM) chance = 1000;
1751 			else
1752 				chance = enchant_table_dam[o_ptr->to_d];
1753 
1754 			if (force || ((randint1(1000) > chance) && (!a || one_in_(2))))
1755 			{
1756 				/* The amount you enchant varys */
1757 				if ((o_ptr->to_d > 7) || force) change = 1;
1758 				else if (o_ptr->to_d > 4) change = randint1(2);
1759 				else
1760 					change = randint1(3);
1761 
1762 				o_ptr->to_d += change;
1763 				res = TRUE;
1764 
1765 				/* only when you get it above -1 -CFT */
1766 				if (o_ptr->to_d >= 0)
1767 					break_curse(o_ptr);
1768 			}
1769 		}
1770 
1771 		/* Enchant to armor class */
1772 		if (eflag & ENCH_TOAC)
1773 		{
1774 			if (o_ptr->to_a < 0) chance = 0;
1775 			else if (o_ptr->to_a > ENCHANT_MAX) chance = 1000;
1776 			else
1777 				chance = enchant_table[o_ptr->to_a];
1778 
1779 			if (force || ((randint1(1000) > chance) && (!a || one_in_(2))))
1780 			{
1781 				/* The amount you enchant varys */
1782 				if ((o_ptr->to_a > 7) || force) change = 1;
1783 				else if (o_ptr->to_a > 4) change = randint1(2);
1784 				else
1785 					change = randint1(3);
1786 
1787 				o_ptr->to_a += change;
1788 				res = TRUE;
1789 
1790 				/* only when you get it above -1 -CFT */
1791 				if (o_ptr->to_a >= 0)
1792 					break_curse(o_ptr);
1793 			}
1794 		}
1795 	}
1796 
1797 	/* Failure */
1798 	if (!res) return (FALSE);
1799 
1800 	/* Apply trigger */
1801 	apply_object_trigger(TRIGGER_ALTER, o_ptr, "");
1802 
1803 	/* Recalculate bonuses */
1804 	p_ptr->update |= (PU_BONUS);
1805 
1806 	/* Window stuff */
1807 	p_ptr->window |= (PW_PLAYER);
1808 
1809 	/* Notice changes */
1810 	notice_item();
1811 
1812 	/* Success */
1813 	return (TRUE);
1814 }
1815 
1816 
1817 
1818 /*
1819  * Enchant an item (in the inventory or on the floor)
1820  * Note that "num_ac" requires armour, else weapon
1821  * Returns TRUE if attempted, FALSE if cancelled
1822  */
enchant_spell(int num_hit,int num_dam,int num_ac)1823 bool enchant_spell(int num_hit, int num_dam, int num_ac)
1824 {
1825 	bool okay = FALSE;
1826 	object_type *o_ptr;
1827 	cptr q, s;
1828 
1829 
1830 	/* Assume enchant weapon */
1831 	item_tester_hook = item_tester_hook_weapon;
1832 
1833 	/* Enchant armor if requested */
1834 	if (num_ac) item_tester_hook = item_tester_hook_armour;
1835 
1836 	/* Get an item */
1837 	q = "Enchant which item? ";
1838 	s = "You have nothing to enchant.";
1839 
1840 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
1841 
1842 	/* Not a valid item */
1843 	if (!o_ptr) return (FALSE);
1844 
1845 	/* Describe */
1846 	msgf("The %v glow%s brightly!", OBJECT_FMT(o_ptr, FALSE, 0),
1847 			   ((o_ptr->number > 1) ? "" : "s"));
1848 
1849 	/* Enchant */
1850 	if (enchant(o_ptr, num_hit, ENCH_TOHIT)) okay = TRUE;
1851 	if (enchant(o_ptr, num_dam, ENCH_TODAM)) okay = TRUE;
1852 	if (enchant(o_ptr, num_ac, ENCH_TOAC)) okay = TRUE;
1853 
1854 	/* Failure */
1855 	if (!okay)
1856 	{
1857 		/* Flush */
1858 		if (flush_failure) flush();
1859 
1860 		/* Message */
1861 		msgf("The enchantment failed.");
1862 
1863 		if (one_in_(3)) chg_virtue(V_ENCHANT, -1);
1864 	}
1865 	else
1866 		chg_virtue(V_ENCHANT, 1);
1867 
1868 	/* Something happened */
1869 	return (TRUE);
1870 }
1871 
1872 
artifact_scroll(void)1873 bool artifact_scroll(void)
1874 {
1875 	bool okay;
1876 	object_type *o_ptr;
1877 	char o_name[256];
1878 	cptr q, s;
1879 
1880 
1881 	/* Enchant weapon/armour */
1882 	item_tester_hook = item_tester_hook_weapon_armour;
1883 
1884 	/* Get an item */
1885 	q = "Enchant which item? ";
1886 	s = "You have nothing to enchant.";
1887 
1888 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
1889 
1890 	/* Not a valid item */
1891 	if (!o_ptr) return (FALSE);
1892 
1893 	/* Description */
1894 	object_desc(o_name, o_ptr, FALSE, 0, 256);
1895 
1896 	/* Describe */
1897 	msgf("The %s radiate%s a blinding light!", o_name,
1898 			   ((o_ptr->number > 1) ? "" : "s"));
1899 
1900 	/* No artifact creation of Dragon Scale Mail */
1901 	if (o_ptr->tval == TV_DRAG_ARMOR)
1902 	{
1903 		/* ToDo: Maybe allow some of the DSMs to be enchanted */
1904 		msgf("The %s %s already magical!",
1905 				   o_name, ((o_ptr->number > 1) ? "are" : "is"));
1906 
1907 		okay = FALSE;
1908 	}
1909 
1910 	else if (o_ptr->xtra_name)
1911 	{
1912 		msgf("The %s %s already %s!",
1913 				   o_name, ((o_ptr->number > 1) ? "are" : "is"),
1914 				   ((o_ptr->number >
1915 					 1) ? "powerful items" : "a powerful item"));
1916 		okay = FALSE;
1917 	}
1918 
1919 	else
1920 	{
1921 		if (o_ptr->number > 1)
1922 		{
1923 			msgf
1924 				("Not enough enough energy to enchant more than one object!");
1925 			msgf("%d of your %s %s destroyed!", (o_ptr->number) - 1,
1926 					   o_name, ((o_ptr->number > 2) ? "were" : "was"));
1927 
1928 			o_ptr->number = 1;
1929 
1930 			/* Notice weight changes */
1931 			p_ptr->update |= PU_WEIGHT;
1932 		}
1933 
1934 		/* The power of the generated artifact depends on player level */
1935 		okay = create_artifact(o_ptr, p_ptr->lev * 2, TRUE);
1936 	}
1937 
1938 	/* Failure */
1939 	if (!okay)
1940 	{
1941 		/* Flush */
1942 		if (flush_failure) flush();
1943 
1944 		/* Message */
1945 		msgf("The enchantment failed.");
1946 		if (one_in_(3)) chg_virtue(V_ENCHANT, -1);
1947 	}
1948 	else
1949 		chg_virtue(V_ENCHANT, 1);
1950 
1951 	/* Something happened */
1952 	return (TRUE);
1953 }
1954 
1955 
1956 /*
1957  * Apply bad luck to an object
1958  */
bad_luck(object_type * o_ptr)1959 static void bad_luck(object_type *o_ptr)
1960 {
1961 	bool is_art = ((FLAG(o_ptr, TR_INSTA_ART)) ? TRUE : FALSE);
1962 
1963 	object_type *q_ptr;
1964 
1965 	/* Do not curse unwieldable items */
1966 	if (wield_slot(o_ptr) == -1) return;
1967 
1968 	/* Objects become worse sometimes */
1969 	if (one_in_(13))
1970 	{
1971 		int number = o_ptr->number;
1972 
1973 		/* Non-artifacts get rerolled */
1974 		if (!is_art)
1975 		{
1976 			SET_FLAG(o_ptr, TR_CURSED);
1977 
1978 			/* Prepare it */
1979 			q_ptr = object_prep(o_ptr->k_idx);
1980 
1981 			/* Swap it */
1982 			swap_objects(o_ptr, q_ptr);
1983 
1984 			/* Restore the number */
1985 			o_ptr->number = number;
1986 
1987 			/* Apply bad magic */
1988 			apply_magic(o_ptr, p_ptr->depth, 0, OC_FORCE_BAD);
1989 		}
1990 
1991 		/* Now curse it */
1992 		SET_FLAG(o_ptr, TR_CURSED);
1993 	}
1994 
1995 	/* Objects are blasted sometimes */
1996 	if (one_in_(666) && (!is_art || one_in_(3)))
1997 	{
1998 		/* Blast it */
1999 		if (o_ptr->to_a) o_ptr->to_a = 0 - (s16b)rand_range(5, 10);
2000 		if (o_ptr->to_h) o_ptr->to_h = 0 - (s16b)rand_range(5, 10);
2001 		if (o_ptr->to_d) o_ptr->to_d = 0 - (s16b)rand_range(5, 10);
2002 		o_ptr->ac = 0;
2003 		o_ptr->dd = 1;
2004 		o_ptr->ds = 1;
2005 		o_ptr->flags[0] = 0;
2006 		o_ptr->flags[1] = 0;
2007 		o_ptr->flags[2] = 0;
2008 		o_ptr->flags[3] = 0;
2009 
2010 		add_ego_flags(o_ptr, EGO_BLASTED);
2011 
2012 		/* Recalculate bonuses */
2013 		p_ptr->update |= (PU_BONUS);
2014 
2015 		/* Recalculate mana */
2016 		p_ptr->update |= (PU_MANA);
2017 
2018 		/* Window stuff */
2019 		p_ptr->window |= (PW_PLAYER);
2020 
2021 		/* Notice changes */
2022 		notice_item();
2023 	}
2024 }
2025 
2026 
2027 /*
2028  * Identify an object
2029  */
identify_item(object_type * o_ptr)2030 void identify_item(object_type *o_ptr)
2031 {
2032 	if (p_ptr->muta3 & MUT3_BAD_LUCK)
2033 	{
2034 		bad_luck(o_ptr);
2035 	}
2036 
2037 	if (!object_known_full(o_ptr))
2038 	{
2039 		if (FLAG(o_ptr, TR_INSTA_ART))
2040 			chg_virtue(V_KNOWLEDGE, 3);
2041 		else
2042 			chg_virtue(V_KNOWLEDGE, 1);
2043 	}
2044 
2045 	/* Identify it fully */
2046 	object_aware(o_ptr);
2047 	object_known(o_ptr);
2048 
2049 	/* Save knowledge of artifact */
2050 	if (o_ptr->a_idx)
2051 	{
2052 		/* Have we seen it before? */
2053 		if (a_info[o_ptr->a_idx].cur_num != 2)
2054 		{
2055 			int artifact = o_ptr->a_idx;
2056 
2057 			/* Notice a quest for this artifact */
2058 			trigger_quest_complete(QX_KNOW_ARTIFACT, &artifact);
2059 
2060 			/*
2061 			 * If the item was an artifact, and if the
2062 			 * auto-note is selected, write a message.
2063 			 */
2064 			if (auto_notes && take_notes)
2065 			{
2066 				/* Write note */
2067 				add_note('A', "Found The %v", OBJECT_FMT(o_ptr, FALSE, 0));
2068 			}
2069 		}
2070 
2071 		a_info[o_ptr->a_idx].cur_num = 2;
2072 	}
2073 
2074 	/* Recalculate bonuses */
2075 	p_ptr->update |= (PU_BONUS);
2076 
2077 	/* Window stuff */
2078 	p_ptr->window |= (PW_PLAYER);
2079 
2080 	/* Notice changes */
2081 	notice_item();
2082 }
2083 
2084 
item_tester_unknown(const object_type * o_ptr)2085 static bool item_tester_unknown(const object_type *o_ptr)
2086 {
2087 	object_kind *k_ptr = &k_info[o_ptr->k_idx];
2088 
2089 	/* Check to see if we don't know the flavor */
2090 	if (k_ptr->flavor && !k_ptr->aware) return (TRUE);
2091 
2092 	/* Check to see if we have identified the item */
2093 	if (object_known_p(o_ptr)) return (FALSE);
2094 
2095 	return (TRUE);
2096 }
2097 
2098 
item_tester_unknown_star(const object_type * o_ptr)2099 static bool item_tester_unknown_star(const object_type *o_ptr)
2100 {
2101 	object_kind *k_ptr = &k_info[o_ptr->k_idx];
2102 
2103 	/* Check to see if we don't know the flavor */
2104 	if (k_ptr->flavor && !k_ptr->aware) return (TRUE);
2105 
2106 	/* Check to see if we have identified the item */
2107 	if (object_known_full(o_ptr)) return (FALSE);
2108 
2109 	return (TRUE);
2110 }
2111 
2112 
2113 /*
2114  * Identify an object in the inventory (or on the floor)
2115  * Returns TRUE if something was identified, else FALSE.
2116  * As a side effect it also sorts and combines the objects in the inventory.
2117  *
2118  * This has been rewritten so that when the identification was of an inventory
2119  * object the correct inv slot (after sorting and combining) is shown in the message.
2120  * To do this the routine combines and sorts the objects right after the
2121  * identification and before the message is generated.  This way the combined
2122  * and sorted object will have the right letter for the slot in the message.
2123  * Except in the case where the player had 1 scroll of identify and that scroll
2124  * disappeared after use.  So then the letter for the slot in the message is
2125  * one too high.  This is solved by determining that the identification was by
2126  * scroll, there was only one scroll and so the letter must be one lower.
2127  */
ident_spell_aux(int k_idx)2128 static bool ident_spell_aux(int k_idx)
2129 {
2130 	cptr q, s;
2131 	object_type *o_ptr, *j_ptr;
2132 	bool disappear = FALSE, back_step = FALSE, skip = FALSE;
2133 
2134 	/* Only un-id'ed items */
2135 	item_tester_hook = item_tester_unknown;
2136 
2137 	/* Get an item */
2138 	q = "Identify which item? ";
2139 	s = "You have nothing to identify.";
2140 
2141 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
2142 
2143 	/* Not a valid item */
2144 	if (!o_ptr) return (FALSE);
2145 
2146 	/* Identify it */
2147 	identify_item(o_ptr);
2148 
2149 	/* Hack.  Do the sorting now */
2150 	o_ptr = reorder_pack_watch(o_ptr);
2151 
2152 	/* Hack.  Do the combining now */
2153 	o_ptr = combine_pack_watch(o_ptr);
2154 
2155 	/* Find out if the id was by scroll */
2156 	OBJ_ITT_START (p_ptr->inventory, j_ptr)
2157 	{
2158 		/* No need to skip anything now */
2159 		skip = FALSE;
2160 
2161 		/* Was it exactly one scroll? */
2162 		if (j_ptr->k_idx == k_idx &&
2163 			j_ptr->number == 1)
2164 		{
2165 			/* That will disappear */
2166 			disappear = TRUE;
2167 
2168 			/* If you read the id scroll on itself, list it just once */
2169 			skip = TRUE;
2170 		}
2171 
2172 		/* Found the original */
2173 		if (o_ptr == j_ptr)
2174 		{
2175 			/* The id scroll is located before object */
2176 			if (disappear) back_step = TRUE;
2177 
2178 			/* We know enough */
2179 			break;
2180 		}
2181 	}
2182 	OBJ_ITT_END;
2183 
2184 	/* Do we need the hack for the right letter in the inventory? */
2185 	if (back_step)
2186 	{
2187 		/* Not quite the description */
2188 		if (!skip) item_describe_faux(o_ptr);
2189 	}
2190 	else
2191 		/* Description */
2192 		item_describe(o_ptr);
2193 
2194 	/* Something happened */
2195 	return (TRUE);
2196 }
2197 
2198 
2199 /* Identify an object by some non-scroll method. */
ident_spell(void)2200 bool ident_spell(void)
2201 {
2202 	return (ident_spell_aux(0));
2203 }
2204 
2205 /* Identify an object by reading an identify scroll */
ident_scroll(int k_idx)2206 bool ident_scroll(int k_idx)
2207 {
2208 	return (ident_spell_aux(k_idx));
2209 }
2210 
2211 
2212 /*
2213  * Mundanify an object in the inventory (or on the floor)
2214  * This routine does *not* automatically combine objects.
2215  * Returns TRUE if something was mundanified, else FALSE.
2216  */
mundane_spell(void)2217 bool mundane_spell(void)
2218 {
2219 	object_type *o_ptr;
2220 	object_kind *k_ptr;
2221 	cptr q, s;
2222 
2223 
2224 	/* Get an item */
2225 	q = "Use which item? ";
2226 	s = "You have nothing you can use.";
2227 
2228 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
2229 
2230 	/* Not a valid item */
2231 	if (!o_ptr) return (FALSE);
2232 
2233 	k_ptr = &k_info[o_ptr->k_idx];
2234 
2235 	/* Oops */
2236 	msgf("There is a bright flash of light!");
2237 
2238 	/* No discount */
2239 	o_ptr->discount = 0;
2240 
2241 	/* Not identified yet */
2242 	o_ptr->info &= ~(OB_SENSE | OB_KNOWN | OB_EMPTY | OB_STOREB);
2243 
2244     /* Erase the inscription */
2245     quark_remove(&o_ptr->inscription);
2246 
2247 	/* No longer a numbered artifact */
2248 	o_ptr->a_idx = 0;
2249 
2250 	/* Erase the "feeling" */
2251 	o_ptr->feeling = FEEL_NONE;
2252 
2253 	/* Default "pval" */
2254 	o_ptr->pval = k_ptr->pval;
2255 
2256 	/* Default weight */
2257 	o_ptr->weight = k_ptr->weight;
2258 
2259 	/* Default magic */
2260 	o_ptr->to_h = k_ptr->to_h;
2261 	o_ptr->to_d = k_ptr->to_d;
2262 	o_ptr->to_a = k_ptr->to_a;
2263 
2264     /* No longer artifact / ego item */
2265     quark_remove(&o_ptr->xtra_name);
2266 
2267 	/* Default power */
2268 	o_ptr->ac = k_ptr->ac;
2269 	o_ptr->dd = k_ptr->dd;
2270 	o_ptr->ds = k_ptr->ds;
2271 
2272 	/* No artifact powers */
2273 	o_ptr->flags[0] = k_ptr->flags[0];
2274 	o_ptr->flags[1] = k_ptr->flags[1];
2275 	o_ptr->flags[2] = k_ptr->flags[2];
2276 	o_ptr->flags[3] = k_ptr->flags[3];
2277 
2278 	/* For rod-stacking */
2279 	if (o_ptr->tval == TV_ROD)
2280 	{
2281 		o_ptr->timeout = o_ptr->pval * o_ptr->number;
2282 		o_ptr->pval = k_ptr->pval * o_ptr->number;
2283 	}
2284 
2285 	/* Initialise cost */
2286 	o_ptr->cost = k_ptr->cost;
2287 
2288 	/* Something happened */
2289 	return (TRUE);
2290 }
2291 
2292 
2293 
2294 /*
2295  * Fully "identify" an object in the inventory  -BEN-
2296  * This routine returns TRUE if an item was identified.
2297  */
identify_fully(void)2298 bool identify_fully(void)
2299 {
2300 	object_type *o_ptr;
2301 	cptr q, s;
2302 
2303 	/* Only un-*id*'ed items */
2304 	item_tester_hook = item_tester_unknown_star;
2305 
2306 	/* Get an item */
2307 	q = "Identify which item? ";
2308 	s = "You have nothing to *identify*.";
2309 
2310 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
2311 
2312 	/* Not a valid item */
2313 	if (!o_ptr) return (FALSE);
2314 
2315 	/* Identify it */
2316 	identify_item(o_ptr);
2317 	object_mental(o_ptr);
2318 
2319 	/* Save all the known flags */
2320 	o_ptr->kn_flags[0] = o_ptr->flags[0];
2321 	o_ptr->kn_flags[1] = o_ptr->flags[1];
2322 	o_ptr->kn_flags[2] = o_ptr->flags[2];
2323 	o_ptr->kn_flags[3] = o_ptr->flags[3];
2324 
2325 	/* Handle stuff */
2326 	handle_stuff();
2327 
2328 	/* Describe it fully */
2329 	identify_fully_aux(o_ptr);
2330 
2331 	/* Success */
2332 	return (TRUE);
2333 }
2334 
2335 /*
2336  * Recharge a wand/staff/rod from the pack or on the floor.
2337  * This function has been rewritten in Oangband and ZAngband.
2338  *
2339  * Sorcery/Arcane -- Recharge  --> recharge(plev * 4)
2340  * Chaos -- Arcane Binding     --> recharge(90)
2341  *
2342  * Scroll of recharging        --> recharge(130)
2343  * Artifact activation/Thingol --> recharge(130)
2344  *
2345  * It is harder to recharge high level, and highly charged wands,
2346  * staffs, and rods.  The more wands in a stack, the more easily and
2347  * strongly they recharge.  Staffs, however, each get fewer charges if
2348  * stacked.
2349  *
2350  * XXX XXX XXX Beware of "sliding index errors".
2351  */
recharge(int power)2352 bool recharge(int power)
2353 {
2354 	int lev;
2355 	int recharge_strength, recharge_amount;
2356 
2357 	object_type *o_ptr;
2358 	object_kind *k_ptr;
2359 
2360 	bool fail = FALSE;
2361 	byte fail_type = 1;
2362 
2363 	cptr q, s;
2364 	char o_name[256];
2365 
2366 
2367 	/* Only accept legal items */
2368 	item_tester_hook = item_tester_hook_recharge;
2369 
2370 	/* Get an item */
2371 	q = "Recharge which item? ";
2372 	s = "You have nothing to recharge.";
2373 
2374 	o_ptr = get_item(q, s, (USE_INVEN | USE_FLOOR));
2375 
2376 	/* Not a valid item */
2377 	if (!o_ptr) return (FALSE);
2378 
2379 	/* Get the object kind. */
2380 	k_ptr = &k_info[o_ptr->k_idx];
2381 
2382 	/*
2383 	 * Extract the object "level"
2384 	 * (Rescaled due to change in dungeon distribtuion)
2385 	 */
2386 	lev = k_info[o_ptr->k_idx].level / 2;
2387 
2388 	/* Recharge a rod */
2389 	if (o_ptr->tval == TV_ROD)
2390 	{
2391 		/* Extract a recharge strength by comparing object level to power. */
2392 		recharge_strength = ((power > lev) ? (power - lev) : 0) / 5;
2393 
2394 
2395 		/* Back-fire */
2396 		if (one_in_(recharge_strength))
2397 		{
2398 			/* Activate the failure code. */
2399 			fail = TRUE;
2400 		}
2401 
2402 		/* Recharge */
2403 		else
2404 		{
2405 			/* Recharge amount */
2406 			recharge_amount = (power * damroll(3, 2));
2407 
2408 			/* Recharge by that amount */
2409 			if (o_ptr->timeout > recharge_amount)
2410 				o_ptr->timeout -= recharge_amount;
2411 			else
2412 				o_ptr->timeout = 0;
2413 		}
2414 	}
2415 
2416 
2417 	/* Recharge wand/staff */
2418 	else
2419 	{
2420 		/* Extract a recharge strength by comparing object level to power.
2421 		 * Divide up a stack of wands' charges to calculate charge penalty.
2422 		 */
2423 		if ((o_ptr->tval == TV_WAND) && (o_ptr->number > 1))
2424 			recharge_strength = (100 + power - lev -
2425 								 (8 * o_ptr->pval / o_ptr->number)) / 15;
2426 
2427 		/* All staffs, unstacked wands. */
2428 		else
2429 			recharge_strength = (100 + power - lev - (8 * o_ptr->pval)) / 15;
2430 
2431 		/* Back-fire */
2432 		if ((recharge_strength < 0) || one_in_(recharge_strength))
2433 		{
2434 			/* Activate the failure code. */
2435 			fail = TRUE;
2436 		}
2437 
2438 		/* If the spell didn't backfire, recharge the wand or staff. */
2439 		else
2440 		{
2441 			/* Recharge based on the standard number of charges. */
2442 			recharge_amount = randint1(1 + k_ptr->pval);
2443 
2444 			/* Multiple wands in a stack increase recharging somewhat. */
2445 			if ((o_ptr->tval == TV_WAND) && (o_ptr->number > 1))
2446 			{
2447 				recharge_amount +=
2448 					(randint1(recharge_amount * (o_ptr->number - 1))) / 2;
2449 				if (recharge_amount < 1) recharge_amount = 1;
2450 				if (recharge_amount > 12) recharge_amount = 12;
2451 			}
2452 
2453 			/* But each staff in a stack gets fewer additional charges,
2454 			 * although always at least one.
2455 			 */
2456 			if ((o_ptr->tval == TV_STAFF) && (o_ptr->number > 1))
2457 			{
2458 				recharge_amount /= o_ptr->number;
2459 				if (recharge_amount < 1) recharge_amount = 1;
2460 			}
2461 
2462 			/* Recharge the wand or staff. */
2463 			o_ptr->pval += recharge_amount;
2464 
2465 			/* Reduce "used" charges */
2466 			if (o_ptr->tval == TV_WAND)
2467 			{
2468 				o_ptr->ac -= recharge_amount;
2469 
2470 				/* Never less than zero */
2471 				if (o_ptr->ac < 0) o_ptr->ac = 0;
2472 			}
2473 
2474 			/* Hack -- we no longer "memorize" the item */
2475 			o_ptr->info &= ~(OB_MENTAL);
2476 
2477 			/* Hack -- we no longer "know" the item */
2478 			o_ptr->info &= ~(OB_KNOWN);
2479 
2480 			/* Hack -- we no longer think the item is empty */
2481 			o_ptr->info &= ~(OB_EMPTY);
2482 		}
2483 	}
2484 
2485 
2486 	/* Inflict the penalties for failing a recharge. */
2487 	if (fail)
2488 	{
2489 		/* Artifacts are never destroyed. */
2490 		if (FLAG(o_ptr, TR_INSTA_ART))
2491 		{
2492 			msgf("The recharging backfires - %v is completely drained!",
2493 					   OBJECT_FMT(o_ptr, TRUE, 0));
2494 
2495 			/* Artifact rods. */
2496 			if ((o_ptr->tval == TV_ROD) && (o_ptr->timeout < 10000))
2497 				o_ptr->timeout = (o_ptr->timeout + 100) * 2;
2498 
2499 			/* Artifact wands and staffs. */
2500 			else
2501 			{
2502 				if (o_ptr->tval == TV_WAND)
2503 				{
2504 					o_ptr->ac += o_ptr->pval;
2505 				}
2506 
2507 				o_ptr->pval = 0;
2508 			}
2509 		}
2510 		else
2511 		{
2512 			/* Get the object description */
2513 			object_desc(o_name, o_ptr, FALSE, 0, 256);
2514 
2515 			/*** Determine Seriousness of Failure ***/
2516 
2517 			/* (High) Mages recharge objects more safely. */
2518 			if ((p_ptr->rp.pclass == CLASS_MAGE) ||
2519 				(p_ptr->rp.pclass == CLASS_HIGH_MAGE))
2520 			{
2521 				/* 10% chance to blow up one rod, otherwise draining. */
2522 				if (o_ptr->tval == TV_ROD)
2523 				{
2524 					if (one_in_(10)) fail_type = 2;
2525 					else
2526 						fail_type = 1;
2527 				}
2528 				/* 75% chance to blow up one wand, otherwise draining. */
2529 				else if (o_ptr->tval == TV_WAND)
2530 				{
2531 					if (!one_in_(3)) fail_type = 2;
2532 					else
2533 						fail_type = 1;
2534 				}
2535 				/* 50% chance to blow up one staff, otherwise no effect. */
2536 				else if (o_ptr->tval == TV_STAFF)
2537 				{
2538 					if (one_in_(2)) fail_type = 2;
2539 					else
2540 						fail_type = 0;
2541 				}
2542 			}
2543 
2544 			/* All other classes get no special favors. */
2545 			else
2546 			{
2547 				/* 33% chance to blow up one rod, otherwise draining. */
2548 				if (o_ptr->tval == TV_ROD)
2549 				{
2550 					if (one_in_(3)) fail_type = 2;
2551 					else
2552 						fail_type = 1;
2553 				}
2554 				/* 20% chance of the entire stack, else destroy one wand. */
2555 				else if (o_ptr->tval == TV_WAND)
2556 				{
2557 					if (one_in_(5)) fail_type = 3;
2558 					else
2559 						fail_type = 2;
2560 				}
2561 				/* Blow up one staff. */
2562 				else if (o_ptr->tval == TV_STAFF)
2563 				{
2564 					fail_type = 2;
2565 				}
2566 			}
2567 
2568 			/*** Apply draining and destruction. ***/
2569 
2570 			/* Drain object or stack of objects. */
2571 			if (fail_type == 1)
2572 			{
2573 				if (o_ptr->tval == TV_ROD)
2574 				{
2575 					msgf("The recharge backfires, draining the rod further!");
2576 					if (o_ptr->timeout < 10000)
2577 						o_ptr->timeout = (o_ptr->timeout + 100) * 2;
2578 				}
2579 				else if (o_ptr->tval == TV_WAND)
2580 				{
2581 					msgf("You save your %s from destruction, but all charges are lost.",
2582 						 o_name);
2583 					o_ptr->ac += o_ptr->pval;
2584 					o_ptr->pval = 0;
2585 				}
2586 				/* Staffs aren't drained. */
2587 			}
2588 
2589 			/* Destroy an object or one in a stack of objects. */
2590 			if (fail_type == 2)
2591 			{
2592 				if (o_ptr->number > 1)
2593 					msgf("Wild magic consumes one of your %s!", o_name);
2594 				else
2595 					msgf("Wild magic consumes your %s!", o_name);
2596 
2597 				/* Reduce rod stack maximum timeout, drain wands. */
2598 				if (o_ptr->tval == TV_ROD) o_ptr->pval -= k_ptr->pval;
2599 				if (o_ptr->tval == TV_WAND)
2600 				{
2601 					o_ptr->ac += o_ptr->pval;
2602 					o_ptr->pval = 0;
2603 
2604 					reduce_charges(o_ptr, 1);
2605 				}
2606 
2607 				/* Reduce and describe */
2608 				item_increase(o_ptr, -1);
2609 			}
2610 
2611 			/* Destroy all members of a stack of objects. */
2612 			if (fail_type == 3)
2613 			{
2614 				if (o_ptr->number > 1)
2615 					msgf("Wild magic consumes all your %s!", o_name);
2616 				else
2617 					msgf("Wild magic consumes your %s!", o_name);
2618 
2619 
2620 				/* Reduce and describe */
2621 				item_increase(o_ptr, -999);
2622 			}
2623 		}
2624 	}
2625 
2626 	/* Notice changes */
2627 	notice_inven();
2628 
2629 	/* Something was done */
2630 	return (TRUE);
2631 }
2632 
2633 
2634 /*
2635  * Bless a weapon
2636  */
bless_weapon(void)2637 bool bless_weapon(void)
2638 {
2639 	object_type *o_ptr;
2640 	char o_name[256];
2641 	cptr q, s;
2642 
2643 	/* Assume enchant weapon */
2644 	item_tester_hook = item_tester_hook_weapon;
2645 
2646 	/* Get an item */
2647 	q = "Bless which weapon? ";
2648 	s = "You have weapon to bless.";
2649 
2650 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
2651 
2652 	/* Not a valid item */
2653 	if (!o_ptr) return FALSE;
2654 
2655 	/* Description */
2656 	object_desc(o_name, o_ptr, FALSE, 0, 256);
2657 
2658 	if (cursed_p(o_ptr))
2659 	{
2660 		if (((FLAG(o_ptr, TR_HEAVY_CURSE)) && (randint1(100) < 33)) ||
2661 			(FLAG(o_ptr, TR_PERMA_CURSE)))
2662 		{
2663 			msgf("The black aura on the %s disrupts the blessing!",
2664 					   o_name);
2665 			return TRUE;
2666 		}
2667 
2668 		msgf("A malignant aura leaves the %s.", o_name);
2669 
2670 		/* Uncurse it */
2671 		uncurse_item(o_ptr, TRUE);
2672 	}
2673 
2674 	/*
2675 	 * Next, we try to bless it. Artifacts have a 1/3 chance of
2676 	 * being blessed, otherwise, the operation simply disenchants
2677 	 * them, godly power negating the magic. Ok, the explanation
2678 	 * is silly, but otherwise priests would always bless every
2679 	 * artifact weapon they find. Ego weapons and normal weapons
2680 	 * can be blessed automatically.
2681 	 */
2682 	if (FLAG(o_ptr, TR_BLESSED))
2683 	{
2684 		msgf("The %s %s blessed already.", o_name,
2685 				   ((o_ptr->number > 1) ? "were" : "was"));
2686 		return TRUE;
2687 	}
2688 
2689 	if (!(o_ptr->xtra_name) || one_in_(3))
2690 	{
2691 		/* Describe */
2692 		msgf("The %s shine%s!", o_name, ((o_ptr->number > 1) ? "" : "s"));
2693 		SET_FLAG(o_ptr, TR_BLESSED);
2694 		o_ptr->kn_flags[2] |= TR2_BLESSED;
2695 	}
2696 	else
2697 	{
2698 		bool dis_happened = FALSE;
2699 
2700 		msgf("The artifact resists your blessing!");
2701 
2702 		/* Disenchant tohit */
2703 		if (o_ptr->to_h > 0)
2704 		{
2705 			o_ptr->to_h--;
2706 			dis_happened = TRUE;
2707 		}
2708 
2709 		if ((o_ptr->to_h > 5) && (randint0(100) < 33)) o_ptr->to_h--;
2710 
2711 		/* Disenchant todam */
2712 		if (o_ptr->to_d > 0)
2713 		{
2714 			o_ptr->to_d--;
2715 			dis_happened = TRUE;
2716 		}
2717 
2718 		if ((o_ptr->to_d > 5) && (randint0(100) < 33)) o_ptr->to_d--;
2719 
2720 		/* Disenchant toac */
2721 		if (o_ptr->to_a > 0)
2722 		{
2723 			o_ptr->to_a--;
2724 			dis_happened = TRUE;
2725 		}
2726 
2727 		if ((o_ptr->to_a > 5) && (randint0(100) < 33)) o_ptr->to_a--;
2728 
2729 		if (dis_happened)
2730 		{
2731 			msgf("There is a static feeling in the air...");
2732 			msgf("The %s %s disenchanted!", o_name,
2733 					   ((o_ptr->number > 1) ? "were" : "was"));
2734 		}
2735 	}
2736 
2737 	/* Recalculate bonuses */
2738 	p_ptr->update |= (PU_BONUS);
2739 
2740 	/* Notice changes */
2741 	notice_item();
2742 
2743 	return TRUE;
2744 }
2745 
2746 
2747 /*
2748  * Potions "smash open" and cause an area effect when
2749  * (1) they are shattered while in the player's inventory,
2750  * due to cold (etc) attacks;
2751  * (2) they are thrown at a monster, or obstacle;
2752  * (3) they are shattered by a "cold ball" or other such spell
2753  * while lying on the floor.
2754  *
2755  * Arguments:
2756  *    who   ---  who caused the potion to shatter (0=player)
2757  *          potions that smash on the floor are assumed to
2758  *          be caused by no-one (who = 1), as are those that
2759  *          shatter inside the player inventory.
2760  *          (Not anymore -- I changed this; TY)
2761  *    y, x  --- coordinates of the potion (or player if
2762  *          the potion was in her inventory);
2763  *    k_idx --- type of object.
2764  */
potion_smash_effect(int who,int x,int y,object_type * o_ptr)2765 bool potion_smash_effect(int who, int x, int y, object_type *o_ptr)
2766 {
2767 	int k_idx = o_ptr->k_idx;
2768 
2769 	bool ident = FALSE;
2770 	bool angry = FALSE;
2771 
2772 	object_kind *k_ptr = &k_info[k_idx];
2773 
2774 	bool result = FALSE;
2775 	apply_object_trigger(TRIGGER_SMASH, o_ptr, "iii:bb",
2776 			LUA_VAR(who), LUA_VAR(x), LUA_VAR(y),
2777 			LUA_RETURN(result), LUA_RETURN(ident));
2778 	angry = result;
2779 
2780 	/* An identification was made */
2781 	if (ident && !(k_ptr->aware))
2782 	{
2783 		k_ptr->aware = TRUE;
2784 		gain_exp((k_ptr->level + p_ptr->lev / 2) / p_ptr->lev);
2785 	}
2786 
2787 	/* Notice changes */
2788 	notice_item();
2789 
2790 	return (angry);
2791 }
2792 
2793 
2794 /*
2795  * Hack -- Display all known spells in a window
2796  *
2797  * XXX XXX XXX Need more color coding.
2798  */
display_spell_list(void)2799 void display_spell_list(void)
2800 {
2801 	int i, j;
2802 	int y = 0, x = 0;
2803 	int use_realm[2];
2804 	const magic_type *s_ptr;
2805 	char name[80];
2806 	char out_val[160];
2807 	int row = 0, col = 0;
2808 	unsigned int max_wid = 0;
2809 
2810 	use_realm[0] = p_ptr->spell.r[0].realm - 1;
2811 	use_realm[1] = p_ptr->spell.r[1].realm - 1;
2812 
2813 	/* Erase window */
2814 	clear_from(0);
2815 
2816 	/* Warriors are illiterate */
2817 	if (!mp_ptr->spell_book) return;
2818 
2819 	/* Mindcrafter spell-list */
2820 	if (p_ptr->rp.pclass == CLASS_MINDCRAFTER)
2821 	{
2822 		int minfail;
2823 		int plev = p_ptr->lev;
2824 		int chance;
2825 		mindcraft_power spell;
2826 		char comment[80];
2827 
2828 		/* Display a list of spells */
2829 		put_fstr(x + 3, y, "Name");
2830 		put_fstr(x + 33, y, "Lv Mana Fail Info");
2831 
2832 		/* Dump the spells */
2833 		for (i = 0; (i < MINDCRAFT_MAX) && (i < Term->hgt - 1); i++)
2834 		{
2835 			cptr a = CLR_WHITE;
2836 
2837 			/* Access the available spell */
2838 			spell = mindcraft_powers[i];
2839 			if (spell.min_lev > plev) break;
2840 
2841 			/* Get the failure rate */
2842 			chance = spell.fail;
2843 
2844 			/* Reduce failure rate by "effective" level adjustment */
2845 			chance -= 3 * (p_ptr->lev - spell.min_lev);
2846 
2847 			/* Reduce failure rate by INT/WIS adjustment */
2848 			chance -= adj_mag_stat[p_ptr->stat[mp_ptr->spell_stat].ind] - 3;
2849 
2850 			/* Not enough mana to cast */
2851 			if (spell.mana_cost > p_ptr->csp)
2852 			{
2853 				chance += 5 * (spell.mana_cost - p_ptr->csp);
2854 				a = CLR_ORANGE;
2855 			}
2856 
2857 			/* Extract the minimum failure rate */
2858 			minfail = adj_mag_fail[p_ptr->stat[mp_ptr->spell_stat].ind];
2859 
2860 			/* Minimum failure rate */
2861 			if (chance < minfail) chance = minfail;
2862 
2863 			/* Stunning makes spells harder */
2864 			if (p_ptr->tim.stun > 50) chance += 25;
2865 			else if (p_ptr->tim.stun) chance += 15;
2866 
2867 			/* Always a 5 percent chance of working */
2868 			if (chance > 95) chance = 95;
2869 
2870 			/* Get info */
2871 			mindcraft_info(comment, i);
2872 
2873 			/* Dump the spell */
2874 			put_fstr(x, y + i + 1, "%s%c) %-30s%2d %4d %3d%%%s",
2875 					a, I2A(i), spell.name,
2876 					spell.min_lev, spell.mana_cost, chance, comment);
2877 		}
2878 
2879 		return;
2880 	}
2881 
2882 	/* Normal spellcaster with books */
2883 
2884 	/* Scan books */
2885 	for (j = 0; j < ((use_realm[1] > -1) ? 2 : 1); j++)
2886 	{
2887 		int n = 0;
2888 
2889 		/* Scan spells */
2890 		for (i = 0; i < 32; i++)
2891 		{
2892 			cptr a = CLR_WHITE;
2893 
2894 			/* Access the spell */
2895 			s_ptr = &mp_ptr->info[use_realm[j]][i % 32];
2896 
2897 			strcpy(name,
2898 				   spell_names[use_realm[j]][i % 32]);
2899 
2900 			/* Illegible */
2901 			if (s_ptr->slevel >= 99)
2902 			{
2903 				/* Illegible */
2904 				strcpy(name, "(illegible)");
2905 
2906 				/* Unusable */
2907 				a = CLR_L_DARK;
2908 			}
2909 
2910 			/* Forgotten */
2911 			else if (p_ptr->spell.r[j].forgotten & (1L << (i % 32)))
2912 			{
2913 				/* Forgotten */
2914 				a = CLR_ORANGE;
2915 			}
2916 
2917 			/* Unknown */
2918 			else if (!(p_ptr->spell.r[j].learned & (1L << (i % 32))))
2919 			{
2920 				/* Unknown */
2921 				a = CLR_RED;
2922 			}
2923 
2924 			/* Untried */
2925 			else if (!(p_ptr->spell.r[j].worked & (1L << (i % 32))))
2926 			{
2927 				/* Untried */
2928 				a = CLR_YELLOW;
2929 			}
2930 
2931 			/* Dump the spell --(-- */
2932 			strnfmt(out_val, 160, "%c/%c) %s", I2A(n / 8), I2A(n % 8), name);
2933 
2934 			max_wid = MAX(max_wid, strlen(out_val) + 1);
2935 
2936 			/* Dump onto the window */
2937 			put_fstr(col, row, "%s%s", a, out_val);
2938 
2939 			/* Next row */
2940 			row++;
2941 
2942 			if (row >= Term->hgt)
2943 			{
2944 				row = 0;
2945 				col += max_wid;
2946 				max_wid = 0;
2947 			}
2948 
2949 			/* Next */
2950 			n++;
2951 		}
2952 	}
2953 }
2954 
2955 
2956 
2957 /*
2958  * Returns spell chance of failure for spell -RAK-
2959  */
spell_chance(int spell,int realm)2960 s16b spell_chance(int spell, int realm)
2961 {
2962 	int chance, minfail;
2963 	const magic_type *s_ptr;
2964 	int smana;
2965 
2966 
2967 	/* Paranoia -- must be literate */
2968 	if (!mp_ptr->spell_book) return (100);
2969 
2970 	/* Access the spell */
2971 	s_ptr = &mp_ptr->info[realm][spell];
2972 
2973 	/* Extract the base spell failure rate */
2974 	if (realm == REALM_ARCANE-1)
2975 		chance = s_ptr->slevel + 20;
2976 	else
2977 		chance = s_ptr->slevel * 3 / 2 + 20;
2978 
2979 	/* Reduce failure rate by "effective" level adjustment */
2980 	chance -= 3 * (p_ptr->lev - s_ptr->slevel);
2981 
2982 	/* Reduce failure rate by INT/WIS adjustment */
2983 	chance -= adj_mag_stat[p_ptr->stat[mp_ptr->spell_stat].ind];
2984 
2985 	/* Get mana cost */
2986 	smana = spell_mana(spell, realm);
2987 
2988 	/* Not enough mana to cast */
2989 	if (smana > p_ptr->csp)
2990 	{
2991 		chance += 5 * (smana - p_ptr->csp);
2992 	}
2993 
2994 	/* Some mutations increase spell failure */
2995 	if ((p_ptr->muta3 & MUT3_MAGIC_RES) || (p_ptr->muta1 & MUT1_EAT_MAGIC))
2996 	{
2997 		chance += 5;
2998 	}
2999 
3000 	if (realm == REALM_DEATH-1 && (p_ptr->muta1 & MUT1_BANISH))
3001 	{
3002 		chance += 10;
3003 	}
3004 
3005 	if (p_ptr->muta3 & MUT3_SILLY_VOI)
3006 	{
3007 		chance += s_ptr->slevel;
3008 	}
3009 
3010 	/* Extract the minimum failure rate */
3011 	minfail = adj_mag_fail[p_ptr->stat[mp_ptr->spell_stat].ind];
3012 
3013 	/*
3014 	 * Non mage/priest characters never get too good
3015 	 * (added high mage, mindcrafter)
3016 	 */
3017 	if ((p_ptr->rp.pclass != CLASS_PRIEST) &&
3018 		(p_ptr->rp.pclass != CLASS_MAGE) &&
3019 		(p_ptr->rp.pclass != CLASS_MINDCRAFTER) &&
3020 		(p_ptr->rp.pclass != CLASS_HIGH_MAGE))
3021 	{
3022 		if (minfail < 5) minfail = 5;
3023 	}
3024 
3025 	/* Hack -- Priest prayer penalty for "edged" weapons  -DGK */
3026 	if ((p_ptr->rp.pclass == CLASS_PRIEST) && p_ptr->state.icky_wield) chance += 25;
3027 
3028 	/* Minimum failure rate */
3029 	if (chance < minfail) chance = minfail;
3030 
3031 	/* Stunning makes spells harder */
3032 	if (p_ptr->tim.stun > 50) chance += 25;
3033 	else if (p_ptr->tim.stun) chance += 15;
3034 
3035 	/* Always a 5 percent chance of working */
3036 	if (chance > 95) chance = 95;
3037 
3038 	/* Return the chance */
3039 	return (chance);
3040 }
3041 
3042 /*
3043  * Returns spell mana cost for spell
3044  */
spell_mana(int spell,int realm)3045 int spell_mana(int spell, int realm)
3046 {
3047 	const magic_type *s_ptr;
3048 	int smana;
3049 
3050 
3051 	/* Paranoia -- must be literate */
3052 	if (!mp_ptr->spell_book) return (100);
3053 
3054 	/* Access the spell */
3055 	s_ptr = &mp_ptr->info[realm][spell];
3056 
3057 	smana = s_ptr->smana;
3058 
3059 	/* Chaos patrons improve chaos magic */
3060 	if ((realm == REALM_CHAOS - 1) && (FLAG(p_ptr, TR_PATRON)))
3061 	{
3062 		smana = (smana * 2 + 2) / 3;
3063 	}
3064 
3065 	return (smana);
3066 }
3067 
3068 
3069 
3070 /*
3071  * Determine if a spell is "okay" for the player to cast or study
3072  * The spell must be legible, not forgotten, and also, to cast,
3073  * it must be known, and to study, it must not be known.
3074  */
spell_okay(int spell,bool known,int realm)3075 bool spell_okay(int spell, bool known, int realm)
3076 {
3077 	const magic_type *s_ptr;
3078 
3079 	/* Access the spell */
3080 	s_ptr = &mp_ptr->info[realm][spell];
3081 
3082 	/* Spell is illegal */
3083 	if (s_ptr->slevel > p_ptr->lev) return (FALSE);
3084 
3085 	/* Spell is forgotten */
3086 	if ((realm == p_ptr->spell.r[1].realm - 1) ?
3087 		(p_ptr->spell.r[1].forgotten & (1L << spell)) :
3088 		(p_ptr->spell.r[0].forgotten & (1L << spell)))
3089 	{
3090 		/* Never okay */
3091 		return (FALSE);
3092 	}
3093 
3094 	/* Spell is learned */
3095 	if ((realm == p_ptr->spell.r[1].realm - 1) ?
3096 		(p_ptr->spell.r[1].learned & (1L << spell)) :
3097 		(p_ptr->spell.r[0].learned & (1L << spell)))
3098 	{
3099 		/* Okay to cast, not to study */
3100 		return (known);
3101 	}
3102 
3103 	/* Okay to study, not to cast */
3104 	return (!known);
3105 }
3106 
3107 
3108 
3109 /*
3110  * Extra information on a spell -DRS-
3111  *
3112  * We can use up to 14 characters of the buffer 'p'
3113  *
3114  * The strings in this function were extracted from the code in the
3115  * functions "do_cmd_cast()" and "do_cmd_pray()" and may be dated.
3116  */
spell_info(char * p,int spell,int realm)3117 void spell_info(char *p, int spell, int realm)
3118 {
3119 	/* Default */
3120 	p[0] = 0;
3121 
3122 	{
3123 		int plev = p_ptr->lev;
3124 
3125 		/* See below */
3126 		int orb = (plev / ((p_ptr->rp.pclass == CLASS_PRIEST ||
3127 							p_ptr->rp.pclass == CLASS_HIGH_MAGE) ? 2 : 4));
3128 
3129 		/* Analyze the spell */
3130 		switch (realm)
3131 		{
3132 			case 0:
3133 			{
3134 				/* Life */
3135 				switch (spell)
3136 				{
3137 					case 1:
3138 					{
3139 						strcpy(p, " heal 2d10");
3140 						break;
3141 					}
3142 					case 2:
3143 					{
3144 						/* Actually rand_range(12,24) */
3145 						strcpy(p, " dur 12+d12 turns");
3146 						break;
3147 					}
3148 					case 4:
3149 					{
3150 						strnfmt(p, 80, " dam 2d%d", (plev / 2));
3151 						break;
3152 					}
3153 					case 6:
3154 					{
3155 						strcpy(p, " heal 4d10");
3156 						break;
3157 					}
3158 					case 10:
3159 					{
3160 						strcpy(p, " heal 8d10");
3161 						break;
3162 					}
3163 					case 11:
3164 					{
3165 						/* Actually rand_range(24,48) */
3166 						strcpy(p, " dur 24+d24");
3167 						break;
3168 					}
3169 					case 12:
3170 					{
3171 						strnfmt(p, 80, " dam %d+3d6", plev + orb);
3172 						break;
3173 					}
3174 					case 13:
3175 					{
3176 						strnfmt(p, 80, " dur %d+d25", 3 * plev);
3177 						break;
3178 					}
3179 					case 14:
3180 					{
3181 						strcpy(p, " heal 300");
3182 						break;
3183 					}
3184 					case 16:
3185 					{
3186 						strnfmt(p, 80, " dam %d+%d", plev, plev);
3187 						break;
3188 					}
3189 					case 18:
3190 					{
3191 						strnfmt(p, 80, " dam %d+%d", 3 * plev, 3 * plev);
3192 						break;
3193 					}
3194 					case 20:
3195 					{
3196 						strnfmt(p, 80, " dam %d", 4 * plev);
3197 						break;
3198 					}
3199 					case 22:
3200 					{
3201 						strnfmt(p, 80, " d %d/h 1000", 4 * plev);
3202 						break;
3203 					}
3204 					case 24:
3205 					{
3206 						/* Actually rand_range(25,50) */
3207 						strcpy(p, " dur 25+d25");
3208 						break;
3209 					}
3210 					case 25:
3211 					{
3212 						/* Actually rand_range(50,100) */
3213 						strcpy(p, " dur 50+d50");
3214 						break;
3215 					}
3216 					case 28:
3217 					{
3218 						strcpy(p, " heal 2000");
3219 						break;
3220 					}
3221 					case 30:
3222 					{
3223 						strnfmt(p, 80, " h300/d%d+388", plev * 4);
3224 						break;
3225 					}
3226 					case 31:
3227 					{
3228 						/* Actually rand_range(7,14) */
3229 						strcpy(p, " dur 7+d7");
3230 						break;
3231 					}
3232 				}
3233 				break;
3234 			}
3235 
3236 			case 1:
3237 			{
3238 				/* Sorcery */
3239 				switch (spell)
3240 				{
3241 					case 1:
3242 					{
3243 						strcpy(p, " range 10");
3244 						break;
3245 					}
3246 					case 3:
3247 					{
3248 						strnfmt(p, 80, " dam 2d%d", (plev / 2));
3249 						break;
3250 					}
3251 					case 5:
3252 					{
3253 						strnfmt(p, 80, " range %d", plev * 5);
3254 						break;
3255 					}
3256 					case 13:
3257 					{
3258 						strnfmt(p, 80, " dur %d+d%d", plev, plev + 20);
3259 						break;
3260 					}
3261 					case 19:
3262 					{
3263 						strnfmt(p, 80, " range %d", plev + 2);
3264 						break;
3265 					}
3266 					case 20:
3267 					{
3268 						/* Actually rand_range(25,55) */
3269 						strcpy(p, " dur 25+d30");
3270 						break;
3271 					}
3272 					case 23:
3273 					{
3274 						strcpy(p, " delay 15+d21");
3275 						break;
3276 					}
3277 					case 25:
3278 					{
3279 						strnfmt(p, 80, " max wgt %d", plev * 15 / 10);
3280 						break;
3281 					}
3282 					case 26:
3283 					{
3284 						strnfmt(p, 80, " dam %d+7d7", plev / 2);
3285 						break;
3286 					}
3287 					case 27:
3288 					{
3289 						/* Actually rand_range(25,55) */
3290 						strcpy(p, " dur 25+d30");
3291 						break;
3292 					}
3293 					case 31:
3294 					{
3295 						/* Actually rand_range(8,16) */
3296 						strcpy(p, " dur 8+d8");
3297 						break;
3298 					}
3299 				}
3300 				break;
3301 			}
3302 
3303 			case 2:
3304 			{
3305 				/* Nature */
3306 				switch (spell)
3307 				{
3308 					case 1:
3309 					{
3310 						strcpy(p, " heal 2d8");
3311 						break;
3312 					}
3313 					case 4:
3314 					{
3315 						strnfmt(p, 80, " dam 2d%d", (plev / 2));
3316 						break;
3317 					}
3318 					case 6:
3319 					{
3320 						/* Actually rand_range(20,40) */
3321 						strcpy(p, " dur 20+d20");
3322 						break;
3323 					}
3324 					case 9:
3325 					{
3326 						strnfmt(p, 80, " dam %dd8", (3 + ((plev - 5) / 4)));
3327 						break;
3328 					}
3329 					case 11:
3330 					{
3331 						strnfmt(p, 80, " dam %dd8", (5 + ((plev - 5) / 4)));
3332 						break;
3333 					}
3334 					case 12:
3335 					{
3336 						strcpy(p, " dam 6d8");
3337 						break;
3338 					}
3339 					case 15:
3340 					{
3341 						strcpy(p, " heal 1000");
3342 						break;
3343 					}
3344 					case 18:
3345 					{
3346 						/* Actually rand_range(30,50) */
3347 						strcpy(p, " dur 20+d30");
3348 						break;
3349 					}
3350 					case 19:
3351 					{
3352 						/* Actually rand_range(20,40) */
3353 						strcpy(p, " dur 20+d20");
3354 						break;
3355 					}
3356 					case 24:
3357 					{
3358 						strcpy(p, " rad 10");
3359 						break;
3360 					}
3361 					case 26:
3362 					{
3363 						strnfmt(p, 80, " dam %d", 70 + plev);
3364 						break;
3365 					}
3366 					case 27:
3367 					{
3368 						strnfmt(p, 80, " dam %d", 90 + plev);
3369 						break;
3370 					}
3371 					case 28:
3372 					{
3373 						strnfmt(p, 80, " dam %d", 100 + plev);
3374 						break;
3375 					}
3376 					case 29:
3377 					{
3378 						strcpy(p, " dam 75");
3379 						break;
3380 					}
3381 					case 31:
3382 					{
3383 						strnfmt(p, 80, " dam %d+%d", 4 * plev, (100 + plev) / 2);
3384 						break;
3385 					}
3386 				}
3387 				break;
3388 			}
3389 
3390 			case 3:
3391 			{
3392 				/* Chaos */
3393 				switch (spell)
3394 				{
3395 					case 0:
3396 					{
3397 						strnfmt(p, 80, " dam %dd4", 3 + ((plev - 1) / 5));
3398 						break;
3399 					}
3400 					case 2:
3401 					{
3402 						strnfmt(p, 80, " dam 2d%d", (plev / 2));
3403 						break;
3404 					}
3405 					case 4:
3406 					{
3407 						strnfmt(p, 80, " dam %d+3d5", plev + (plev /
3408 														  (((p_ptr->rp.pclass ==
3409 															 CLASS_MAGE)
3410 															|| (p_ptr->rp.pclass ==
3411 																CLASS_HIGH_MAGE))
3412 														   ? 2 : 4)));
3413 						break;
3414 					}
3415 					case 5:
3416 					{
3417 						strnfmt(p, 80, " dam %dd8", (8 + ((plev - 5) / 4)));
3418 						break;
3419 					}
3420 					case 6:
3421 					{
3422 						strnfmt(p, 80, " dam %dd8", (8 + ((plev - 5) / 4)));
3423 						break;
3424 					}
3425 					case 7:
3426 					{
3427 						strnfmt(p, 80, " range %d", plev * 5);
3428 						break;
3429 					}
3430 					case 8:
3431 					{
3432 						strcpy(p, " random");
3433 						break;
3434 					}
3435 					case 9:
3436 					{
3437 						strnfmt(p, 80, " dam %dd8", (10 + ((plev - 5) / 4)));
3438 						break;
3439 					}
3440 					case 10:
3441 					{
3442 						strnfmt(p, 80, " dam %d", (45 + plev) / 2);
3443 						break;
3444 					}
3445 					case 11:
3446 					{
3447 						strnfmt(p, 80, " dam %dd8", (11 + ((plev - 5) / 4)));
3448 						break;
3449 					}
3450 					case 12:
3451 					{
3452 						strnfmt(p, 80, " dam %d", 55 + plev);
3453 						break;
3454 					}
3455 					case 15:
3456 					{
3457 						strnfmt(p, 80, " dam %d", 66 + plev);
3458 						break;
3459 					}
3460 					case 17:
3461 					{
3462 						strnfmt(p, 80, " dam %dd8", (5 + (plev / 10)));
3463 						break;
3464 					}
3465 					case 19:
3466 					{
3467 						strnfmt(p, 80, " dam %d", 80 + plev);
3468 						break;
3469 					}
3470 					case 24:
3471 					{
3472 						strnfmt(p, 80, " dam %dd8", (9 + ((plev - 5) / 4)));
3473 						break;
3474 					}
3475 					case 25:
3476 					{
3477 						strnfmt(p, 80, " dam %d each", (3 * plev) / 2);
3478 						break;
3479 					}
3480 					case 26:
3481 					{
3482 						strnfmt(p, 80, " dam %d", 75 + plev);
3483 						break;
3484 					}
3485 					case 27:
3486 					{
3487 						strcpy(p, " dam 75 / 150");
3488 						break;
3489 					}
3490 					case 28:
3491 					{
3492 						strnfmt(p, 80, " dam %d", 120 + plev);
3493 						break;
3494 					}
3495 					case 29:
3496 					{
3497 						strnfmt(p, 80, " dam %d", 300 + (plev * 2));
3498 						break;
3499 					}
3500 					case 30:
3501 					{
3502 						strnfmt(p, 80, " dam %d", p_ptr->chp);
3503 						break;
3504 					}
3505 					case 31:
3506 					{
3507 						strcpy(p, " dam 3 * 175");
3508 						break;
3509 					}
3510 				}
3511 				break;
3512 			}
3513 
3514 			case 4:
3515 			{
3516 				/* Death */
3517 				switch (spell)
3518 				{
3519 					case 1:
3520 					{
3521 						strnfmt(p, 80, " dam %dd3", (3 + ((plev - 1) / 5)));
3522 						break;
3523 					}
3524 					case 3:
3525 					{
3526 						strnfmt(p, 80, " dam %d", 10 + (plev / 2));
3527 						break;
3528 					}
3529 					case 5:
3530 					{
3531 						/* Actually rand_range(20,40) */
3532 						strnfmt(p, 80, " dur 20+d20");
3533 						break;
3534 					}
3535 					case 8:
3536 					{
3537 						strnfmt(p, 80, " dam %d+3d6", plev +
3538 								(plev / (((p_ptr->rp.pclass == CLASS_MAGE) ||
3539 										  (p_ptr->rp.pclass ==
3540 										   CLASS_HIGH_MAGE)) ? 2 : 4)));
3541 						break;
3542 					}
3543 					case 9:
3544 					{
3545 						strnfmt(p, 80, " dam %dd8", (6 + ((plev - 5) / 4)));
3546 						break;
3547 					}
3548 					case 11:
3549 					{
3550 						strnfmt(p, 80, " dm %d+%d*d15", plev, MAX(1, plev / 10));
3551 						break;
3552 					}
3553 					case 13:
3554 					{
3555 						strnfmt(p, 80, " dam %d", 4 * plev);
3556 						break;
3557 					}
3558 					case 16:
3559 					{
3560 						/* Actually rand_range(25,50) */
3561 						strcpy(p, " dur 25+d25");
3562 						break;
3563 					}
3564 					case 17:
3565 					{
3566 						strcpy(p, " random");
3567 						break;
3568 					}
3569 					case 18:
3570 					{
3571 						strnfmt(p, 80, " dam %dd8", (4 + ((plev - 5) / 4)));
3572 						break;
3573 					}
3574 					case 19:
3575 					{
3576 						/* This is too complicated to give accurately */
3577 						strcpy(p, " max dur 50");
3578 						break;
3579 					}
3580 					case 20:
3581 					{
3582 						strcpy(p, " dam 3*100");
3583 						break;
3584 					}
3585 					case 22:
3586 					{
3587 						strcpy(p, " dam 120");
3588 						break;
3589 					}
3590 					case 27:
3591 					{
3592 						strnfmt(p, 80, " dam %d", plev * 3);
3593 						break;
3594 					}
3595 					case 28:
3596 					{
3597 						strnfmt(p, 80, " dam %d", plev * 4);
3598 						break;
3599 					}
3600 					case 29:
3601 					{
3602 						strcpy(p, " dam 666");
3603 						break;
3604 					}
3605 					case 31:
3606 					{
3607 						/* Actually rand_range(plev/2,plev) */
3608 						strnfmt(p, 80, " dur %d+d%d", (plev / 2), (plev / 2));
3609 						break;
3610 					}
3611 				}
3612 				break;
3613 			}
3614 
3615 			case 5:
3616 			{
3617 				/* Trump */
3618 				switch (spell)
3619 				{
3620 					case 0:
3621 					{
3622 						strcpy(p, " range 10");
3623 						break;
3624 					}
3625 					case 1:
3626 					{
3627 						strnfmt(p, 80, " dam %dd3", 3 + ((plev - 1) / 5));
3628 						break;
3629 					}
3630 					case 2:
3631 					{
3632 						strcpy(p, " random");
3633 						break;
3634 					}
3635 					case 4:
3636 					{
3637 						strnfmt(p, 80, " range %d", plev * 4);
3638 						break;
3639 					}
3640 					case 5:
3641 					{
3642 						strnfmt(p, 80, " range %d", plev + 2);
3643 						break;
3644 					}
3645 					case 6:
3646 					{
3647 						/* Actually rand_range(25,55) */
3648 						strcpy(p, " dur 25+d30");
3649 						break;
3650 					}
3651 					case 8:
3652 					{
3653 						strnfmt(p, 80, " max wgt %d", plev * 15 / 10);
3654 						break;
3655 					}
3656 					case 14:
3657 					{
3658 						strcpy(p, " delay 15+d21");
3659 						break;
3660 					}
3661 					case 22:
3662 					{
3663 						strnfmt(p, 80, " dam %d", plev * 3);
3664 						/* break; */
3665 					}
3666 				}
3667 				break;
3668 			}
3669 
3670 			case 6:
3671 			{
3672 				/* Arcane */
3673 				switch (spell)
3674 				{
3675 					case 0:
3676 					{
3677 						strnfmt(p, 80, " dam %dd3", 3 + ((plev - 1) / 5));
3678 						break;
3679 					}
3680 					case 4:
3681 					{
3682 						strcpy(p, " range 10");
3683 						break;
3684 					}
3685 					case 5:
3686 					{
3687 						strnfmt(p, 80, " dam 2d%d", plev / 2);
3688 						break;
3689 					}
3690 					case 7:
3691 					{
3692 						strcpy(p, " heal 2d8");
3693 						break;
3694 					}
3695 					case 14:
3696 					case 15:
3697 					case 16:
3698 					case 17:
3699 					{
3700 						/* Actually rand_range(20,40) */
3701 						strcpy(p, " dur 20+d20");
3702 						break;
3703 					}
3704 					case 18:
3705 					{
3706 						strcpy(p, " heal 4d8");
3707 						break;
3708 					}
3709 					case 19:
3710 					{
3711 						strnfmt(p, 80, " range %d", plev * 5);
3712 						break;
3713 					}
3714 					case 21:
3715 					{
3716 						strcpy(p, " dam 6d8");
3717 						break;
3718 					}
3719 					case 23:
3720 					{
3721 						/* Actually rand_range(24,48) */
3722 						strcpy(p, " dur 24+d24");
3723 						break;
3724 					}
3725 					case 28:
3726 					{
3727 						strnfmt(p, 80, " dam %d", 75 + plev);
3728 						break;
3729 					}
3730 					case 30:
3731 					{
3732 						strcpy(p, " delay 15+d21");
3733 						break;
3734 					}
3735 					case 31:
3736 					{
3737 						/* Actually rand_range(25,55) */
3738 						strcpy(p, " dur 25+d30");
3739 						break;
3740 					}
3741 				}
3742 				break;
3743 			}
3744 
3745 			default:
3746 			{
3747 				strnfmt(p, 80, "Unknown type: %d.", realm);
3748 			}
3749 		}
3750 	}
3751 }
3752 
3753 
3754 /*
3755  * Print a list of spells (for browsing or casting or viewing)
3756  */
print_spells(byte * spells,int num,int x,int y,int realm)3757 void print_spells(byte *spells, int num, int x, int y, int realm)
3758 {
3759 	int i, spell;
3760 	const magic_type *s_ptr;
3761 	cptr comment;
3762 	char info[80];
3763 
3764 	if (((realm < 0) || (realm >= MAX_REALM)) && p_ptr->state.wizard)
3765 		msgf("Warning! print_spells called with null realm");
3766 
3767 	/* Title the list */
3768 	prtf(x, y, "");
3769 	put_fstr(x + 5, y, "Name");
3770 	put_fstr(x + 35, y, "Lv Mana Fail Info");
3771 
3772 
3773 	/* Dump the spells */
3774 	for (i = 0; i < num; i++)
3775 	{
3776 		/* Access the spell */
3777 		spell = spells[i];
3778 
3779 		/* Access the spell */
3780 		s_ptr = &mp_ptr->info[realm][spell];
3781 
3782 		/* Skip illegible spells */
3783 		if (s_ptr->slevel >= 99)
3784 		{
3785 			prtf(x, y + i + 1, CLR_L_DARK "  %c) %-30s",
3786 					 I2A(i), "(illegible)");
3787 			continue;
3788 		}
3789 
3790 		/* XXX XXX Could label spells above the players level */
3791 
3792 		/* Get extra info */
3793 		spell_info(info, spell, realm);
3794 
3795 		/* Use that info */
3796 		comment = info;
3797 
3798 		/* Analyze the spell */
3799 		if ((realm + 1 != p_ptr->spell.r[0].realm) && (realm + 1 != p_ptr->spell.r[1].realm))
3800 		{
3801 			comment = CLR_SLATE " uncastable";
3802 		}
3803 
3804 		/* We know these books */
3805 		else if ((realm + 1 == p_ptr->spell.r[0].realm) ?
3806 				 ((p_ptr->spell.r[0].forgotten & (1L << spell))) :
3807 				 ((p_ptr->spell.r[1].forgotten & (1L << spell))))
3808 		{
3809 			comment = CLR_YELLOW " forgotten";
3810 		}
3811 		else if (!((realm + 1 == p_ptr->spell.r[0].realm) ?
3812 				   (p_ptr->spell.r[0].learned & (1L << spell)) :
3813 				   (p_ptr->spell.r[1].learned & (1L << spell))))
3814 		{
3815 			comment = CLR_L_BLUE " unknown";
3816 		}
3817 		else if (!((realm + 1 == p_ptr->spell.r[0].realm) ?
3818 				   (p_ptr->spell.r[0].worked & (1L << spell)) :
3819 				   (p_ptr->spell.r[1].worked & (1L << spell))))
3820 		{
3821 			comment = CLR_L_GREEN " untried";
3822 		}
3823 
3824 		/* Dump the spell --(-- */
3825 		prtf(x, y + i + 1, "  %c) %-30s%2d %4d %3d%%%s",
3826 				I2A(i), spell_names[realm][spell],
3827 				(int)s_ptr->slevel, spell_mana(spell, realm),
3828 				spell_chance(spell, realm), comment);
3829 	}
3830 
3831 	/* Clear the bottom line */
3832 	prtf(x, y + i + 1, "");
3833 }
3834 
3835 
3836 /*
3837  * Note that amulets, rods, and high-level spell books are immune
3838  * to "inventory damage" of any kind.  Also sling ammo and shovels.
3839  */
3840 
3841 
3842 /*
3843  * Does a given class of objects (usually) hate acid?
3844  * Note that acid can either melt or corrode something.
3845  */
hates_acid(const object_type * o_ptr)3846 bool hates_acid(const object_type *o_ptr)
3847 {
3848 	switch (o_ptr->tval)
3849 	{
3850 			/* Wearable items */
3851 		case TV_ARROW:
3852 		case TV_BOLT:
3853 		case TV_BOW:
3854 		case TV_SWORD:
3855 		case TV_HAFTED:
3856 		case TV_POLEARM:
3857 		case TV_HELM:
3858 		case TV_CROWN:
3859 		case TV_SHIELD:
3860 		case TV_BOOTS:
3861 		case TV_GLOVES:
3862 		case TV_CLOAK:
3863 		case TV_SOFT_ARMOR:
3864 		case TV_HARD_ARMOR:
3865 		case TV_DRAG_ARMOR:
3866 		{
3867 			/* Analyze the type */
3868 			return (TRUE);
3869 		}
3870 
3871 		case TV_STAFF:
3872 		case TV_SCROLL:
3873 		{
3874 			/* Staffs/Scrolls are wood/paper */
3875 			return (TRUE);
3876 		}
3877 
3878 		case TV_CHEST:
3879 		{
3880 			/* Ouch */
3881 			return (TRUE);
3882 		}
3883 
3884 		case TV_SKELETON:
3885 		case TV_BOTTLE:
3886 		case TV_JUNK:
3887 		{
3888 			/* Junk is useless */
3889 			return (TRUE);
3890 		}
3891 	}
3892 
3893 	return (FALSE);
3894 }
3895 
3896 
3897 /*
3898  * Does a given object (usually) hate electricity?
3899  */
hates_elec(const object_type * o_ptr)3900 bool hates_elec(const object_type *o_ptr)
3901 {
3902 	switch (o_ptr->tval)
3903 	{
3904 		case TV_RING:
3905 		case TV_WAND:
3906 		{
3907 			return (TRUE);
3908 		}
3909 	}
3910 
3911 	return (FALSE);
3912 }
3913 
3914 
3915 /*
3916  * Does a given object (usually) hate fire?
3917  * Hafted/Polearm weapons have wooden shafts.
3918  * Arrows/Bows are mostly wooden.
3919  */
hates_fire(const object_type * o_ptr)3920 bool hates_fire(const object_type *o_ptr)
3921 {
3922 	/* Analyze the type */
3923 	switch (o_ptr->tval)
3924 	{
3925 		case TV_LITE:
3926 		case TV_ARROW:
3927 		case TV_BOW:
3928 		case TV_HAFTED:
3929 		case TV_POLEARM:
3930 		case TV_BOOTS:
3931 		case TV_GLOVES:
3932 		case TV_CLOAK:
3933 		case TV_SOFT_ARMOR:
3934 		{
3935 			/* Wearable */
3936 			return (TRUE);
3937 		}
3938 
3939 		case TV_LIFE_BOOK:
3940 		case TV_SORCERY_BOOK:
3941 		case TV_NATURE_BOOK:
3942 		case TV_CHAOS_BOOK:
3943 		case TV_DEATH_BOOK:
3944 		case TV_TRUMP_BOOK:
3945 		case TV_ARCANE_BOOK:
3946 		{
3947 			/* Books */
3948 			return (TRUE);
3949 		}
3950 
3951 		case TV_CHEST:
3952 		{
3953 			/* Chests */
3954 			return (TRUE);
3955 		}
3956 
3957 		case TV_STAFF:
3958 		case TV_SCROLL:
3959 		{
3960 			/* Staffs/Scrolls burn */
3961 			return (TRUE);
3962 		}
3963 	}
3964 
3965 	return (FALSE);
3966 }
3967 
3968 
3969 /*
3970  * Does a given object (usually) hate cold?
3971  */
hates_cold(const object_type * o_ptr)3972 bool hates_cold(const object_type *o_ptr)
3973 {
3974 	switch (o_ptr->tval)
3975 	{
3976 		case TV_POTION:
3977 		case TV_FLASK:
3978 		case TV_BOTTLE:
3979 		{
3980 			return (TRUE);
3981 		}
3982 	}
3983 
3984 	return (FALSE);
3985 }
3986 
3987 
3988 /*
3989  * Melt something
3990  */
set_acid_destroy(object_type * o_ptr)3991 int set_acid_destroy(object_type *o_ptr)
3992 {
3993 	if (!hates_acid(o_ptr)) return (FALSE);
3994 	if (FLAG(o_ptr, TR_IGNORE_ACID)) return (FALSE);
3995 	return (TRUE);
3996 }
3997 
3998 
3999 /*
4000  * Electrical damage
4001  */
set_elec_destroy(object_type * o_ptr)4002 int set_elec_destroy(object_type *o_ptr)
4003 {
4004 	if (!hates_elec(o_ptr)) return (FALSE);
4005 	if (FLAG(o_ptr, TR_IGNORE_ELEC)) return (FALSE);
4006 	return (TRUE);
4007 }
4008 
4009 
4010 /*
4011  * Burn something
4012  */
set_fire_destroy(object_type * o_ptr)4013 int set_fire_destroy(object_type *o_ptr)
4014 {
4015 	if (!hates_fire(o_ptr)) return (FALSE);
4016 	if (FLAG(o_ptr, TR_IGNORE_FIRE)) return (FALSE);
4017 	return (TRUE);
4018 }
4019 
4020 
4021 /*
4022  * Freeze things
4023  */
set_cold_destroy(object_type * o_ptr)4024 int set_cold_destroy(object_type *o_ptr)
4025 {
4026 	if (!hates_cold(o_ptr)) return (FALSE);
4027 	if (FLAG(o_ptr, TR_IGNORE_COLD)) return (FALSE);
4028 	return (TRUE);
4029 }
4030 
4031 
4032 /*
4033  * Destroys a type of item on a given percent chance
4034  * Note that missiles are no longer necessarily all destroyed
4035  * Destruction taken from "melee.c" code for "stealing".
4036  * New-style wands and rods handled correctly. -LM-
4037  * Returns number of items destroyed.
4038  */
inven_damage(inven_func typ,int perc)4039 int inven_damage(inven_func typ, int perc)
4040 {
4041 	int j, k, amt;
4042 	object_type *o_ptr;
4043 
4044 	int slot;
4045 
4046 
4047 	/* Count the casualties */
4048 	k = 0;
4049 
4050 	/* Scan the inventory */
4051 	OBJ_ITT_START (p_ptr->inventory, o_ptr)
4052 	{
4053 		/* Hack -- for now, skip artifacts */
4054 		if (FLAG(o_ptr, TR_INSTA_ART)) continue;
4055 
4056 		/* Give this item slot a shot at death */
4057 		if ((*typ) (o_ptr))
4058 		{
4059 			/* Count the casualties */
4060 			for (amt = j = 0; j < o_ptr->number; ++j)
4061 			{
4062 				if (randint0(100) < perc) amt++;
4063 			}
4064 
4065 			/* Some casualities */
4066 			if (amt)
4067 			{
4068 				/* Get slot */
4069 				slot = get_item_position(p_ptr->inventory, o_ptr);
4070 
4071 				/* Message */
4072 				msgf("%sour %v (%c) %s destroyed!",
4073 						   ((o_ptr->number > 1) ?
4074 							((amt == o_ptr->number) ? "All of y" :
4075 							 (amt > 1 ? "Some of y" : "One of y")) : "Y"),
4076 						   OBJECT_FMT(o_ptr, FALSE, 3), I2A(slot),
4077 						    ((amt > 1) ? "were" : "was"));
4078 
4079 				/* Potions smash open */
4080 				if (object_is_potion(o_ptr))
4081 				{
4082 					int px = p_ptr->px;
4083 					int py = p_ptr->py;
4084 
4085 					(void)potion_smash_effect(0, px, py, o_ptr);
4086 				}
4087 
4088 				/* Reduce the charges of rods/wands */
4089 				reduce_charges(o_ptr, amt);
4090 
4091 				/* Destroy "amt" items */
4092 				item_increase(o_ptr, -amt);
4093 
4094 				/* Count the casualties */
4095 				k += amt;
4096 			}
4097 		}
4098 	}
4099 	OBJ_ITT_END;
4100 
4101 	/* Return the casualty count */
4102 	return (k);
4103 }
4104 
4105 
rustproof(void)4106 bool rustproof(void)
4107 {
4108 	object_type *o_ptr;
4109 	cptr q, s;
4110 
4111 	/* Select a piece of armour */
4112 	item_tester_hook = item_tester_hook_armour_no_acid;
4113 
4114 	/* Get an item */
4115 	q = "Rustproof which piece of armour? ";
4116 	s = "You have nothing to rustproof.";
4117 
4118 	o_ptr = get_item(q, s, (USE_EQUIP | USE_INVEN | USE_FLOOR));
4119 
4120 	/* Not a valid item */
4121 	if (!o_ptr) return (FALSE);
4122 
4123 	SET_FLAG(o_ptr, TR_IGNORE_ACID);
4124 	o_ptr->kn_flags[2] |= TR2_IGNORE_ACID;
4125 
4126 	if ((o_ptr->to_a < 0) && !(cursed_p(o_ptr)))
4127 	{
4128 		msgf("The %v look%s as good as new!", OBJECT_FMT(o_ptr, FALSE, 0),
4129 				   ((o_ptr->number > 1) ? "" : "s"));
4130 		o_ptr->to_a = 0;
4131 	}
4132 
4133 	msgf("The %v %s now protected against corrosion.", OBJECT_FMT(o_ptr, FALSE, 0),
4134 			   ((o_ptr->number > 1) ? "are" : "is"));
4135 
4136 	return TRUE;
4137 }
4138 
4139 
4140 /*
4141  * Curse the players armor
4142  */
curse_armor(void)4143 bool curse_armor(void)
4144 {
4145 	object_type *o_ptr;
4146 
4147 
4148 	/* Curse the body armor */
4149 	o_ptr = &p_ptr->equipment[EQUIP_BODY];
4150 
4151 	/* Nothing to curse */
4152 	if (!o_ptr->k_idx) return (FALSE);
4153 
4154 
4155 	/* Attempt a saving throw for artifacts */
4156 	if ((FLAG(o_ptr, TR_INSTA_ART)) && !one_in_(3))
4157 	{
4158 		/* Cool */
4159 		msgf("A terrible black aura tries to surround your armor, but your %v resists the effects!",
4160 			OBJECT_FMT(o_ptr, FALSE, 3));
4161 	}
4162 
4163 	/* not artifact or failed save... */
4164 	else
4165 	{
4166 		/* Oops */
4167 		msgf("A terrible black aura blasts your %v!", OBJECT_FMT(o_ptr, FALSE, 3));
4168 
4169 		chg_virtue(V_ENCHANT, -5);
4170 
4171 		/* Blast the armor */
4172 		o_ptr->to_a = 0 - (s16b)rand_range(5, 10);
4173 		o_ptr->to_h = 0;
4174 		o_ptr->to_d = 0;
4175 		o_ptr->ac = 0;
4176 		o_ptr->dd = 1;
4177 		o_ptr->ds = 1;
4178 		o_ptr->flags[0] = 0;
4179 		o_ptr->flags[1] = 0;
4180 		o_ptr->flags[2] = 0;
4181 		o_ptr->flags[3] = 0;
4182 
4183 		/* Lose your feeling */
4184 		o_ptr->feeling = FEEL_NONE;
4185 
4186 		add_ego_flags(o_ptr, EGO_BLASTED);
4187 
4188 		/* Recalculate bonuses */
4189 		p_ptr->update |= (PU_BONUS);
4190 
4191 		/* Recalculate mana */
4192 		p_ptr->update |= (PU_MANA);
4193 
4194 		/* Window stuff */
4195 		p_ptr->window |= (PW_PLAYER);
4196 
4197 		/* Notice changes */
4198 		notice_item();
4199 	}
4200 
4201 	return (TRUE);
4202 }
4203 
4204 
4205 /*
4206  * Curse the players weapon
4207  */
curse_weapon(void)4208 bool curse_weapon(void)
4209 {
4210 	object_type *o_ptr;
4211 
4212 	/* Curse the weapon */
4213 	o_ptr = &p_ptr->equipment[EQUIP_WIELD];
4214 
4215 	/* Nothing to curse */
4216 	if (!o_ptr->k_idx) return (FALSE);
4217 
4218 	/* Attempt a saving throw */
4219 	if ((FLAG(o_ptr, TR_INSTA_ART)) && !one_in_(3))
4220 	{
4221 		/* Cool */
4222 		msgf("A terrible black aura tries to surround your weapon, but your %v resists the effects!",
4223 				OBJECT_FMT(o_ptr, FALSE, 3));
4224 	}
4225 
4226 	/* not artifact or failed save... */
4227 	else
4228 	{
4229 		/* Oops */
4230 		msgf("A terrible black aura blasts your %v!", OBJECT_FMT(o_ptr, FALSE, 3));
4231 
4232 		chg_virtue(V_ENCHANT, -5);
4233 
4234 		/* Shatter the weapon */
4235 		o_ptr->to_h = 0 - (s16b)rand_range(5, 10);
4236 		o_ptr->to_d = 0 - (s16b)rand_range(5, 10);
4237 		o_ptr->to_a = 0;
4238 		o_ptr->ac = 0;
4239 		o_ptr->dd = 1;
4240 		o_ptr->ds = 1;
4241 		o_ptr->flags[0] = 0;
4242 		o_ptr->flags[1] = 0;
4243 		o_ptr->flags[2] = 0;
4244 		o_ptr->flags[3] = 0;
4245 
4246 		/* Lose your feeling */
4247 		o_ptr->feeling = FEEL_NONE;
4248 
4249 		add_ego_flags(o_ptr, EGO_SHATTERED);
4250 
4251 		/* Recalculate bonuses */
4252 		p_ptr->update |= (PU_BONUS);
4253 
4254 		/* Recalculate mana */
4255 		p_ptr->update |= (PU_MANA);
4256 
4257 		/* Window stuff */
4258 		p_ptr->window |= (PW_PLAYER);
4259 
4260 		/* Notice changes */
4261 		notice_item();
4262 	}
4263 
4264 	/* Notice */
4265 	return (TRUE);
4266 }
4267 
4268 
4269 /*
4270  * Enchant some bolts
4271  */
brand_bolts(void)4272 bool brand_bolts(void)
4273 {
4274 	object_type *o_ptr;
4275 
4276 	/* Use the first acceptable bolts */
4277 	OBJ_ITT_START (p_ptr->inventory, o_ptr)
4278 	{
4279 		/* Skip non-bolts */
4280 		if (o_ptr->tval != TV_BOLT) continue;
4281 
4282 		/* Skip artifacts and ego-items */
4283 		if (o_ptr->xtra_name) continue;
4284 
4285 		/* Skip cursed/broken items */
4286 		if (cursed_p(o_ptr) || !o_ptr->cost) continue;
4287 
4288 		/* Randomize */
4289 		if (randint0(100) < 75) continue;
4290 
4291 		/* Message */
4292 		msgf("Your bolts are covered in a fiery aura!");
4293 
4294 		/* Ego-item */
4295 		add_ego_flags(o_ptr, EGO_FLAME);
4296 
4297 		/* Enchant */
4298 		(void)enchant(o_ptr, rand_range(2, 6), ENCH_TOHIT | ENCH_TODAM);
4299 
4300 		/* Notice changes */
4301 		notice_inven();
4302 
4303 		/* Notice */
4304 		return (TRUE);
4305 	}
4306 	OBJ_ITT_END;
4307 
4308 	/* Flush */
4309 	if (flush_failure) flush();
4310 
4311 	/* Fail */
4312 	msgf("The fiery enchantment failed.");
4313 
4314 	/* Notice */
4315 	return (TRUE);
4316 }
4317 
4318 
4319 /*
4320  * Helper function -- return a "nearby" race for polymorphing
4321  *
4322  * Note that this function is one of the more "dangerous" ones...
4323  */
poly_r_idx(int r_idx)4324 static s16b poly_r_idx(int r_idx)
4325 {
4326 	monster_race *r_ptr = &r_info[r_idx];
4327 
4328 	int i, r, lev1, lev2;
4329 
4330 	/* Hack -- Uniques/Questors never polymorph */
4331 	if (FLAG(r_ptr, RF_UNIQUE) || FLAG(r_ptr, RF_QUESTOR))
4332 		return (r_idx);
4333 
4334 	/* Allowable range of "levels" for resulting monster */
4335 	lev1 = r_ptr->level - ((randint1(20) / randint1(9)) + 1);
4336 	lev2 = r_ptr->level + ((randint1(20) / randint1(9)) + 1);
4337 
4338 	/* Pick a (possibly new) non-unique race */
4339 	for (i = 0; i < 1000; i++)
4340 	{
4341 		/* Pick a new race, using a level calculation */
4342 		r = get_mon_num((p_ptr->depth + r_ptr->level) / 2 + 5);
4343 
4344 		/* Handle failure */
4345 		if (!r) break;
4346 
4347 		/* Obtain race */
4348 		r_ptr = &r_info[r];
4349 
4350 		/* Ignore unique monsters */
4351 		if (FLAG(r_ptr, RF_UNIQUE)) continue;
4352 
4353 		/* Ignore monsters with incompatible levels */
4354 		if ((r_ptr->level < lev1) || (r_ptr->level > lev2)) continue;
4355 
4356 		/* Use that index */
4357 		r_idx = r;
4358 
4359 		/* Done */
4360 		break;
4361 	}
4362 
4363 	/* Result */
4364 	return (r_idx);
4365 }
4366 
4367 
polymorph_monster(int x,int y)4368 bool polymorph_monster(int x, int y)
4369 {
4370 	cave_type *c_ptr = area(x, y);
4371 	monster_type *m_ptr = &m_list[c_ptr->m_idx];
4372 	bool friendly, pet;
4373 	bool polymorphed = FALSE;
4374 	int new_r_idx;
4375 	int old_r_idx = m_ptr->r_idx;
4376 
4377 
4378 	/* Get the monsters attitude */
4379 	friendly = is_friendly(m_ptr);
4380 	pet = is_pet(m_ptr);
4381 
4382 	/* Pick a "new" monster race */
4383 	new_r_idx = poly_r_idx(old_r_idx);
4384 
4385 	/* Handle polymorph */
4386 	if (new_r_idx != old_r_idx)
4387 	{
4388 		/* "Kill" the "old" monster */
4389 		delete_monster_idx(c_ptr->m_idx);
4390 
4391 		/* Create a new monster (no groups) */
4392 		if (place_monster_aux(x, y, new_r_idx, FALSE, FALSE, friendly, pet, TRUE))
4393 		{
4394 			/* Success */
4395 			polymorphed = TRUE;
4396 		}
4397 		else
4398 		{
4399 			/* Placing the new monster failed - use the old. */
4400 			(void)place_monster_aux(x, y, old_r_idx, FALSE, FALSE, friendly,
4401 									pet, TRUE);
4402 		}
4403 	}
4404 
4405 	/* Update some things */
4406 	p_ptr->update |= (PU_MON_LITE);
4407 
4408 	return (polymorphed);
4409 }
4410 
4411 
4412 /*
4413  * Dimension Door
4414  */
dimension_door(void)4415 bool dimension_door(void)
4416 {
4417 	int px = p_ptr->px;
4418 	int py = p_ptr->py;
4419 
4420 	int plev = p_ptr->lev;
4421 	int x = 0, y = 0;
4422 	cave_type *c_ptr;
4423 
4424 	if (!tgt_pt(&x, &y)) return FALSE;
4425 
4426 	p_ptr->energy -= 60 - plev;
4427 
4428 	/* paranoia */
4429 	if (!in_bounds2(x, y)) return FALSE;
4430 
4431 	c_ptr = area(x, y);
4432 
4433 	if (!cave_empty_grid(c_ptr) || (c_ptr->info & CAVE_ICKY) ||
4434 		(distance(x, y, px, py) > plev + 2) || (one_in_(plev * plev / 2)))
4435 	{
4436 		msgf("You fail to exit the astral plane correctly!");
4437 		p_ptr->energy -= 100;
4438 		teleport_player(10);
4439 	}
4440 	else
4441 		teleport_player_to(x, y);
4442 
4443 	return (TRUE);
4444 }
4445 
4446 
4447 /*
4448  * Map the wilderness
4449  */
map_wilderness(int radius,s32b x,s32b y)4450 void map_wilderness(int radius, s32b x, s32b y)
4451 {
4452 	int i, j;
4453 	int dist;
4454 
4455 	/* Map a rough circle around the target position in the wilderness */
4456 	for (i = x - radius; i < x + radius + 1; i++)
4457 	{
4458 		for (j = y - radius; j < y + radius + 1; j++)
4459 		{
4460 			/* In bounds? */
4461 			if ((i >= 0) && (i < max_wild - 1) && (j >= 0) && (j < max_wild - 1))
4462 			{
4463 				dist = distance(i, j, x, y);
4464 
4465 				if ((randint0(dist) < radius / 2) && (dist < radius))
4466 				{
4467 					/* Memorise the location */
4468 					wild[j][i].done.info |= WILD_INFO_SEEN;
4469 
4470 					/* Alert player to a new wilderness quest */
4471 					discover_wild_quest(place[wild[j][i].done.place].quest_num);
4472 				}
4473 			}
4474 		}
4475 	}
4476 }
4477 
4478 
sanity_blast(const monster_type * m_ptr)4479 void sanity_blast(const monster_type *m_ptr)
4480 {
4481 	int power = 100;
4482 
4483 	monster_race *r_ptr = &r_info[m_ptr->r_idx];
4484 
4485 	power = r_ptr->hdice * 2 + 10;
4486 
4487 	if (!FLAG(r_ptr, RF_UNIQUE))
4488 	{
4489 		if (FLAG(r_ptr, RF_FRIENDS))
4490 			power -= 50;
4491 	}
4492 	else
4493 	{
4494 		power += 50;
4495 	}
4496 
4497 	/* Can we see it? */
4498 	if (!m_ptr->ml) return;
4499 
4500 	/* Paranoia */
4501 	if (!FLAG(r_ptr, RF_ELDRITCH_HORROR)) return;
4502 
4503 	/* Pet eldritch horrors are safe most of the time */
4504 	if (is_pet(m_ptr) && !one_in_(8)) return;
4505 
4506 	/* Do we pass the saving throw? */
4507 	if (player_save(power)) return;
4508 
4509 	if (p_ptr->tim.image)
4510 	{
4511 		/* Something silly happens... */
4512 		msgf("You behold the %s visage of %v!",
4513 				   funny_desc[randint0(MAX_SAN_FUNNY)], MONSTER_FMT(m_ptr, 0));
4514 
4515 		if (one_in_(3))
4516 		{
4517 			msgf(funny_comments[randint0(MAX_SAN_COMMENT)]);
4518 			(void)inc_image(randint1(r_ptr->hdice * 2));
4519 		}
4520 
4521 		/* Never mind; we can't see it clearly enough */
4522 		return;
4523 	}
4524 
4525 	/* Something frightening happens... */
4526 	msgf("You behold the %s visage of %v!",
4527 			   horror_desc[randint0(MAX_SAN_HORROR)], MONSTER_FMT(m_ptr, 0));
4528 
4529 	/* Monster memory */
4530 	r_ptr->r_flags[3] |= RF3_ELDRITCH_HORROR;
4531 
4532 	/* Demon characters are unaffected */
4533 	if (p_ptr->rp.prace == RACE_IMP) return;
4534 
4535 	/* Undead characters are 50% likely to be unaffected */
4536 	if (((p_ptr->rp.prace == RACE_SKELETON) ||
4537 		 (p_ptr->rp.prace == RACE_ZOMBIE) ||
4538 		 (p_ptr->rp.prace == RACE_VAMPIRE) ||
4539 		 (p_ptr->rp.prace == RACE_SPECTRE) ||
4540 		 (p_ptr->rp.prace == RACE_GHOUL)) && saving_throw(25 + p_ptr->lev)) return;
4541 
4542 	/* Mind blast */
4543 	if (!player_save(power))
4544 	{
4545 		if ((!(FLAG(p_ptr, TR_RES_FEAR))) || one_in_(5))
4546 		{
4547 			/* Get afraid, even if have resist fear! */
4548 			(void)inc_afraid(rand_range(10, 20));
4549 		}
4550 		if (!(FLAG(p_ptr, TR_RES_CHAOS)))
4551 		{
4552 			(void)inc_image(rand_range(150, 400));
4553 		}
4554 		return;
4555 	}
4556 
4557 	if (lose_all_info())
4558 	{
4559 		msgf("You forget everything in your utmost terror!");
4560 	}
4561 
4562 	p_ptr->update |= PU_BONUS;
4563 	handle_stuff();
4564 }
4565