1 /* File: object2.c */
2 
3 /* Purpose: Object code, part 2 */
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 /*
17  * Temp object used to return an object not allocated via o_pop().
18  *
19  * This is used by object_dup() and by object_prep() to
20  * return "new" objects.  Note that these routines delete
21  * any object stored here before they use it.  This means
22  * that object (quark) refcounting will work.  This should
23  * be the only "static" object used, unless you make
24  * absolutely sure the memory management is correct.
25  *
26  * Be very careful - object_copy() does not duplicate
27  * references, where as object_dup() does.  Make sure any
28  * allocated object will be wiped eventually.  temp_object
29  * is wiped when ever it is used so it is safe.
30  *
31  * Note - because several routines use this variable, any
32  * statically allocated object returned must be used asap.
33  */
34 static object_type temp_object;
35 
36 
37 /*
38  * Prepare an object based on an existing object
39  */
object_copy(object_type * o_ptr,const object_type * j_ptr)40 static void object_copy(object_type *o_ptr, const object_type *j_ptr)
41 {
42 	/* Copy the structure */
43 	COPY(o_ptr, j_ptr, object_type);
44 }
45 
46 /*
47  * Excise a dungeon object from any stacks
48  */
excise_object_idx(s16b * o_idx_ptr,object_type * o_ptr)49 static void excise_object_idx(s16b *o_idx_ptr, object_type *o_ptr)
50 {
51 	object_type *j_ptr = NULL;
52 
53 	object_type *q_ptr;
54 
55 	/* Scan all objects in the list */
56 	OBJ_ITT_START (*o_idx_ptr, q_ptr)
57 	{
58 		/* Hack - Done? */
59 		if (q_ptr == o_ptr)
60 		{
61 			/* No previous */
62 			if (!j_ptr)
63 			{
64 				/* Remove from list */
65 				*o_idx_ptr = q_ptr->next_o_idx;
66 			}
67 
68 			/* Real previous */
69 			else
70 			{
71 				/* Remove from list */
72 				j_ptr->next_o_idx = q_ptr->next_o_idx;
73 			}
74 
75 			/* Forget next pointer */
76 			q_ptr->next_o_idx = 0;
77 
78 			/* Done */
79 			break;
80 		}
81 
82 		/* Save previous object */
83 		j_ptr = q_ptr;
84 	}
85 	OBJ_ITT_END;
86 }
87 
88 
89 /*
90  * Delete a dungeon object
91  *
92  * Handle "stacks" of objects correctly.
93  */
delete_held_object(s16b * o_idx_ptr,object_type * o_ptr)94 void delete_held_object(s16b *o_idx_ptr, object_type *o_ptr)
95 {
96 	/* Excise */
97 	excise_object_idx(o_idx_ptr, o_ptr);
98 
99 	/* Wipe the object */
100 	object_wipe(o_ptr);
101 
102 	/* Count objects */
103 	o_cnt--;
104 }
105 
106 /*
107  * Delete an object we know is lying on the dungeon floor.
108  */
delete_dungeon_object(object_type * o_ptr)109 void delete_dungeon_object(object_type *o_ptr)
110 {
111 	int x, y;
112 
113 	cave_type *c_ptr;
114 
115 	/* Location */
116 	x = o_ptr->ix;
117 	y = o_ptr->iy;
118 
119 	c_ptr = area(x, y);
120 
121 	/* Excise */
122 	excise_object_idx(&c_ptr->o_idx, o_ptr);
123 
124 	/* Visual update */
125 	lite_spot(x, y);
126 
127 	/* Wipe the object */
128 	object_wipe(o_ptr);
129 
130 	/* Count objects */
131 	o_cnt--;
132 }
133 
134 
135 /*
136  * Deletes all objects at given location
137  */
delete_object(int x,int y)138 void delete_object(int x, int y)
139 {
140 	cave_type *c_ptr;
141 
142 	/* Refuse "illegal" locations */
143 	if (!in_bounds2(x, y)) return;
144 
145 	/* Grid */
146 	c_ptr = area(x, y);
147 
148 	/* Delete the objects */
149 	delete_object_list(&c_ptr->o_idx);
150 
151 	/* Visual update */
152 	lite_spot(x, y);
153 }
154 
155 
156 /*
157  * Delete a statically-allocated object
158  */
delete_static_object(object_type * o_ptr)159 static void delete_static_object(object_type *o_ptr)
160 {
161 	int i;
162 
163 	/* Deallocate quarks */
164 	quark_remove(&o_ptr->xtra_name);
165 	quark_remove(&o_ptr->inscription);
166 
167 	for (i = 0; i < MAX_TRIGGER; i++)
168 		quark_remove(&o_ptr->trigger[i]);
169 
170 	/* Do nothing */
171 	return;
172 }
173 
174 /*
175  * Deletes all objects at given location
176  */
delete_object_list(s16b * o_idx_ptr)177 void delete_object_list(s16b *o_idx_ptr)
178 {
179 	object_type *o_ptr;
180 
181 	/*
182 	 * Special exception: if the first object is (nothing),
183 	 * just zero the index.
184 	 * This happens when loading savefiles.
185 	 */
186 	if (!o_list[*o_idx_ptr].k_idx)
187 		*o_idx_ptr = 0;
188 
189 	/* Scan all objects in the grid */
190 	OBJ_ITT_START (*o_idx_ptr, o_ptr)
191 	{
192 		/* Wipe the object */
193 		object_wipe(o_ptr);
194 
195 		/* Count objects */
196 		if (o_cnt) o_cnt--;
197 	}
198 	OBJ_ITT_END;
199 
200 	/* Objects are gone */
201 	*o_idx_ptr = 0;
202 }
203 
204 /*
205  * Drop all the items in the list near the location (x, y).
206  */
drop_object_list(s16b * o_idx_ptr,int x,int y)207 void drop_object_list(s16b *o_idx_ptr, int x, int y)
208 {
209 	object_type *o_ptr;
210 	object_type *q_ptr;
211 
212 	/* Drop objects being carried */
213 	OBJ_ITT_START (*o_idx_ptr, o_ptr)
214 	{
215 		/* Duplicate object */
216 		q_ptr = object_dup(o_ptr);
217 
218 		/* Delete held object */
219 		delete_held_object(o_idx_ptr, o_ptr);
220 
221 		/* Drop object */
222 		drop_near(q_ptr, -1, x, y);
223 	}
224 	OBJ_ITT_END;
225 
226 	/* No more objects in the list */
227 	*o_idx_ptr = 0;
228 }
229 
230 
231 /*
232  * Compact and Reorder the object list
233  *
234  * This function can be very dangerous, use with caution!
235  *
236  * When actually "compacting" objects, we base the saving throw on a
237  * combination of object level, distance from player, and current
238  * "desperation".
239  *
240  * After "compacting" (if needed), we "reorder" the objects into a more
241  * compact order, and we reset the allocation info, and the "live" array.
242  */
compact_objects(int size)243 void compact_objects(int size)
244 {
245 	int i, y, x, num, cnt;
246 
247 	int cur_lev, cur_dis, chance;
248 
249 	object_type *o_ptr;
250 
251 	monster_type *m_ptr;
252 
253 
254 	/* Compact */
255 	if (size)
256 	{
257 		/* Message */
258 		msgf("Compacting objects...");
259 
260 		/* Redraw map */
261 		p_ptr->redraw |= (PR_MAP);
262 
263 		/* Window stuff */
264 		p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
265 	}
266 
267 
268 	/* Compact at least 'size' objects */
269 	for (num = 0, cnt = 1; num < size; cnt++)
270 	{
271 		/* Get more vicious each iteration */
272 		cur_lev = 5 * cnt;
273 
274 		/* Get closer each iteration */
275 		cur_dis = 5 * (20 - cnt);
276 
277 		/* Examine the objects */
278 		for (i = 1; i < o_max; i++)
279 		{
280 			o_ptr = &o_list[i];
281 
282 			/* Skip dead objects */
283 			if (!o_ptr->k_idx) continue;
284 
285 			/* Hack -- High level objects start out "immune" */
286 			if (get_object_level(o_ptr) > cur_lev) continue;
287 
288 			/* Only objects in the dungeon */
289 			if (!((o_ptr->ix) && (o_ptr->iy))) continue;
290 
291 			/* Get the location */
292 			x = o_ptr->ix;
293 			y = o_ptr->iy;
294 
295 			/* Nearby objects start out "immune" */
296 			if (distance(p_ptr->px, p_ptr->py, x, y) < cur_dis) continue;
297 
298 			/* Saving throw */
299 			chance = 90;
300 
301 			/* Hack -- only compact artifacts in emergencies */
302 			if ((FLAG(o_ptr, TR_INSTA_ART)) && (cnt < 1000)) chance = 100;
303 
304 			/* Apply the saving throw */
305 			if (randint0(100) < chance) continue;
306 
307 			/* Delete the object */
308 			delete_dungeon_object(o_ptr);
309 
310 			/* Count it */
311 			num++;
312 		}
313 
314 		/* Scan monster inventories */
315 		for (i = 1; i < m_max; i++)
316 		{
317 			m_ptr = &m_list[i];
318 
319 			/* Ignore dead monsters */
320 			if (!m_ptr->r_idx) continue;
321 
322 			/* We need to hold some objects */
323 			if (!m_ptr->hold_o_idx) continue;
324 
325 			/* Get the location */
326 			x = m_ptr->fx;
327 			y = m_ptr->fy;
328 
329 			/* Nearby objects start out "immune" */
330 			if (distance(p_ptr->px, p_ptr->py, x, y) < cur_dis) continue;
331 
332 			/* Saving throw (higher than dungeon objects) */
333 			chance = 99;
334 
335 			OBJ_ITT_START (m_ptr->hold_o_idx, o_ptr)
336 			{
337 				/* Hack -- High level objects start out "immune" */
338 				if (get_object_level(o_ptr) > cur_lev) continue;
339 
340 				/* Hack -- only compact artifacts in emergencies */
341 				if ((FLAG(o_ptr, TR_INSTA_ART)) &&
342 					(cnt < 1000)) chance = 100;
343 
344 				/* Apply the saving throw */
345 				if (randint0(100) < chance) continue;
346 
347 				/* Delete the object */
348 				delete_held_object(&m_ptr->hold_o_idx, o_ptr);
349 
350 				/* Count it */
351 				num++;
352 			}
353 			OBJ_ITT_END;
354 		}
355 
356 	}
357 
358 
359 	/* Excise dead objects (backwards!) */
360 	while (o_max > 1)
361 	{
362 		object_type *o_ptr = &o_list[o_max - 1];
363 
364 		/* Stop when we get to an object */
365 		if (o_ptr->k_idx) break;
366 
367 		/* Compress "o_max" */
368 		o_max--;
369 	}
370 }
371 
372 
373 /*
374  * Delete all the non-held items.
375  * Items in the players inventory are not touched.
376  * Items in monster inventories are wiped by
377  * calling wipe_m_list() before this function.
378  *
379  * Hack -- we clear the "c_ptr->o_idx" field for every grid
380  * since we know we are clearing every object.  Technically, we
381  * only clear those fields for grids containing objects,
382  * and we clear it once for every such object.
383  */
wipe_o_list(void)384 void wipe_o_list(void)
385 {
386 	int i;
387 
388 	int x, y;
389 
390 	cave_type *c_ptr;
391 
392 	object_type *o_ptr;
393 
394 	/* Set all objects to be unallocated */
395 	for (i = 1; i < o_max; i++)
396 	{
397 		o_ptr = &o_list[i];
398 
399 		o_ptr->allocated = FALSE;
400 	}
401 
402 	/* Only if inventory exists */
403 	if (o_list[p_ptr->inventory].k_idx)
404 	{
405 		/* Save players inventory (only objects in a list to save) */
406 		OBJ_ITT_START (p_ptr->inventory, o_ptr)
407 		{
408 			o_ptr->allocated = TRUE;
409 		}
410 		OBJ_ITT_END;
411 	}
412 
413 	/* Delete the existing objects */
414 	for (i = 1; i < o_max; i++)
415 	{
416 		o_ptr = &o_list[i];
417 
418 		/* Skip dead objects */
419 		if (!o_ptr->k_idx) continue;
420 
421 		/* Skip allocated objects */
422 		if (o_ptr->allocated) continue;
423 
424 		/* Preserve artifacts */
425 		if (preserve_mode && (FLAG(o_ptr, TR_INSTA_ART)) &&
426 			o_ptr->a_idx &&
427 			(a_info[o_ptr->a_idx].cur_num == 1))
428 		{
429 			a_info[o_ptr->a_idx].cur_num = 0;
430 		}
431 
432 		/* Access location */
433 		y = o_ptr->iy;
434 		x = o_ptr->ix;
435 
436 		/* Store item? */
437 		if (!x && !y)
438 		{
439 			/* Hack - just kill it */
440 			object_wipe(o_ptr);
441 
442 			/* Count objects */
443 			o_cnt--;
444 
445 			continue;
446 		}
447 
448 		/* Access grid */
449 		c_ptr = area(x, y);
450 
451 		/* Hack -- see above */
452 		c_ptr->o_idx = 0;
453 
454 		/* Delete the object */
455 		delete_dungeon_object(o_ptr);
456 	}
457 
458 	/* Compress the object list */
459 	compact_objects(0);
460 }
461 
462 
463 /*
464  * Wipe objects in region
465  */
wipe_objects(int rg_idx)466 void wipe_objects(int rg_idx)
467 {
468 	int i;
469 
470 	object_type *o_ptr;
471 
472 	/* Delete the existing objects */
473 	for (i = 1; i < o_max; i++)
474 	{
475 		o_ptr = &o_list[i];
476 
477 		/* Skip dead objects */
478 		if (!o_ptr->k_idx) continue;
479 
480 		/* Enforce region */
481 		if (o_ptr->region != rg_idx) continue;
482 
483 		/* Preserve artifacts */
484 		if (preserve_mode && (FLAG(o_ptr, TR_INSTA_ART)) &&
485 			o_ptr->a_idx &&
486 			(a_info[o_ptr->a_idx].cur_num == 1))
487 		{
488 			a_info[o_ptr->a_idx].cur_num = 0;
489 		}
490 
491 		/* Delete the object */
492 		delete_dungeon_object(o_ptr);
493 	}
494 }
495 
496 
497 /* Current object counter */
498 static s16b o_cur = 1;
499 
500 /*
501  * Acquires and returns the index of a "free" object.
502  *
503  * This routine should almost never fail, but in case it does,
504  * we must be sure to handle "failure" of this routine.
505  *
506  *
507  * We have a choice... scan from o_cur onwards, or use o_max.
508  *
509  * Using o_max is fast, but it leads to fragmentation.
510  * Using o_cur is slower (and might not even give a better
511  * result than using o_max), but it leads to a more compact
512  * object distribution.
513  */
o_pop(void)514 static s16b o_pop(void)
515 {
516 	bool wrapped = FALSE;
517 
518 	/* Wrap counter */
519 	if (o_cur >= o_max) o_cur = 1;
520 
521 	/*
522 	 * If the number remaining is less than one third of the
523 	 * total number of allocated objects, then add a new object
524 	 * to the end of the list.
525 	 *
526 	 * We add 1 to o_cnt because object 0 is unusable.
527 	 *
528 	 * Feel free to tune this parameter.
529 	 */
530 	if ((o_max - (o_cnt + 1)) * 3 < o_max)
531 	{
532 		/* Initial allocation */
533 		if (o_max < z_info->o_max)
534 		{
535 			/* Expand object array */
536 			o_max++;
537 
538 			/* Count objects */
539 			o_cnt++;
540 
541 			/* Use this object */
542 			return (o_max - 1);
543 		}
544 	}
545 
546 	/* Recycle dead objects */
547 	while (TRUE)
548 	{
549 		object_type *o_ptr;
550 
551 		/* Acquire object */
552 		o_ptr = &o_list[o_cur];
553 
554 		/* Skip live objects */
555 		if (o_ptr->k_idx)
556 		{
557 			/* Increment counter */
558 			o_cur++;
559 
560 			/* Wrap counter */
561 			if (o_cur >= o_max)
562 			{
563 				/* Infinite loop protection */
564 				if (wrapped) break;
565 
566 				o_cur = 1;
567 				wrapped = TRUE;
568 			}
569 			continue;
570 		}
571 
572 		/* Count objects */
573 		o_cnt++;
574 
575 		/* Use this object */
576 		return (o_cur);
577 	}
578 
579 
580 	/* Warn the player (except during dungeon creation) */
581 	if (character_dungeon) msgf("Too many objects!");
582 
583 	/* Oops */
584 	return (0);
585 }
586 
587 /*
588  * Add an object to a list.  o_ptr is a pointer
589  * to a statically declared object that is assumed
590  * not to be in the standard object array.
591  *
592  * We do not adjust position, held or region information.
593  *
594  * The old object is wiped.
595  */
add_object_list(s16b * o_idx_ptr,object_type * o_ptr)596 object_type *add_object_list(s16b *o_idx_ptr, object_type *o_ptr)
597 {
598 	s16b o_idx;
599 
600 	object_type *j_ptr;
601 
602 	/* Get a new object */
603 	o_idx = o_pop();
604 
605 	/* Hack - bail out */
606 	if (!o_idx) return (NULL);
607 
608 	/* Point to the object */
609 	j_ptr = &o_list[o_idx];
610 
611 	/* Move to the list */
612 	swap_objects(j_ptr, o_ptr);
613 
614 	/* Add to the list */
615 	j_ptr->next_o_idx = *o_idx_ptr;
616 	*o_idx_ptr = o_idx;
617 
618 	/* Now held */
619 	j_ptr->allocated = TRUE;
620 
621 	/* Return the new item */
622 	return (j_ptr);
623 }
624 
625 /*
626  * Move an object from one list to another
627  */
move_object(s16b * tgt_list_ptr,s16b * cur_list_ptr,object_type * o_ptr)628 void move_object(s16b *tgt_list_ptr, s16b *cur_list_ptr, object_type *o_ptr)
629 {
630 	/* Excise object from the first list */
631 	excise_object_idx(cur_list_ptr, o_ptr);
632 
633 	/* Add to the new list */
634 	o_ptr->next_o_idx = *tgt_list_ptr;
635 
636 	/* *tgt_list_ptr = o_idx; */
637 	*tgt_list_ptr = GET_ARRAY_INDEX(o_list, o_ptr);
638 }
639 
640 /*
641  * Swap objects
642  */
swap_objects(object_type * o1_ptr,object_type * o2_ptr)643 void swap_objects(object_type *o1_ptr, object_type *o2_ptr)
644 {
645 	object_type temp;
646 
647 	/* Copy the object */
648 	object_copy(&temp, o2_ptr);
649 
650 	/* Copy the object */
651 	object_copy(o2_ptr, o1_ptr);
652 
653 	/* Get correct next-object fields */
654 	o2_ptr->next_o_idx = temp.next_o_idx;
655 	temp.next_o_idx = o1_ptr->next_o_idx;
656 
657 	/* Get correct position fields */
658 	o2_ptr->ix = temp.ix;
659 	temp.ix = o1_ptr->ix;
660 	o2_ptr->iy = temp.iy;
661 	temp.iy = o1_ptr->iy;
662 
663 	/* Get correct region */
664 	o2_ptr->region = temp.region;
665 	temp.region = o1_ptr->region;
666 
667 	/* Get correct allocated value */
668 	o2_ptr->allocated = temp.allocated;
669 	temp.allocated = o1_ptr->allocated;
670 
671 	/* Copy the object */
672 	object_copy(o1_ptr, &temp);
673 }
674 
675 /*
676  * Apply a "object restriction function" to the "object allocation table"
677  */
get_obj_num_prep(object_hook_type object_hook)678 void get_obj_num_prep(object_hook_type object_hook)
679 {
680 	int i;
681 
682 	byte prob;
683 
684 	/* Get the entry */
685 	alloc_entry *table = alloc_kind_table;
686 
687 	/* Scan the allocation table */
688 	for (i = 0; i < alloc_kind_size; i++)
689 	{
690 		/* Accept objects which pass the restriction, if any */
691 		if (object_hook)
692 		{
693 			/* Get probability */
694 			prob = (*object_hook) (table[i].index);
695 
696 			/* Paranoia */
697 			if (prob > 100) prob = 100;
698 
699 			/* Accept this object */
700 			table[i].prob2 = (table[i].prob1 * prob) / 100;
701 		}
702 		else
703 		{
704 			/* Accept this object */
705 			table[i].prob2 = table[i].prob1;
706 		}
707 	}
708 }
709 
710 
711 /*
712  * Choose an object kind that seems "appropriate" to the given level
713  *
714  * This function uses the "prob2" field of the "object allocation table",
715  * and various local information, to calculate the "prob3" field of the
716  * same table, which is then used to choose an "appropriate" object, in
717  * a relatively efficient manner.
718  *
719  * It is more likely to acquire an object of the given level
720  * than one of a lower level.  This is done by choosing three objects
721  * appropriate to the given level and keeping the "hardest" one.
722  *
723  * Note that if no objects are "appropriate", then this function will
724  * fail, and return zero, but this should *almost* never happen.
725  */
get_obj_num(int level,int min_level)726 s16b get_obj_num(int level, int min_level)
727 {
728 	int i;
729 	long value1, value2, total;
730 	alloc_entry *table = alloc_kind_table;
731 
732 	/* Luck gives occasional out-of-depth items */
733 	if ((FLAG(p_ptr, TR_STRANGE_LUCK)) && one_in_(13))
734 	{
735 		level += randint1(one_in_(7) ? 40 : 10);
736 	}
737 
738 	/* Occasional "boost" */
739 	if (one_in_(GREAT_OBJ))
740 	{
741 		/* What a bizarre calculation */
742 		level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
743 	}
744 
745 	/* Reset total */
746 	total = 0L;
747 
748 	/* Process probabilities */
749 	for (i = 0; i < alloc_kind_size; i++)
750 	{
751 		/* Objects are sorted by depth */
752 		if (table[i].level > level) break;
753 
754 		/* What John West rejects, makes John West the best. */
755 		if (table[i].level < min_level) continue;
756 
757 		/* Total */
758 		total += table[i].prob2;
759 	}
760 
761 	/* No legal objects */
762 	if (total <= 0) return (0);
763 
764 
765 	/* Pick an object */
766 	value1 = randint0(total);
767 
768 	for (i = 0; i < 3; i++)
769 	{
770 		/* Try for a "better" object once */
771 		value2 = randint0(total);
772 
773 		/* Is it better? */
774 		if (value2 > value1)
775 		{
776 			/* This hack works because the object table is sorted by depth */
777 			value1 = value2;
778 		}
779 	}
780 
781 	/* Find the object */
782 	for (i = 0; i < alloc_kind_size; i++)
783 	{
784 		/* What John West rejects, makes John West the best. */
785 		if (table[i].level < min_level) continue;
786 
787 		/* Decrement */
788 		value1 -= table[i].prob2;
789 
790 		/* A match? */
791 		if (value1 < 0L) break;
792 	}
793 
794 	/* Result */
795 	return (table[i].index);
796 }
797 
798 
799 /*
800  * Known is true when the "attributes" of an object are "known".
801  * These include tohit, todam, toac, cost, and pval (charges).
802  *
803  * Note that "knowing" an object gives you everything that an "awareness"
804  * gives you, and much more.  In fact, the player is always "aware" of any
805  * item of which he has full "knowledge".
806  *
807  * But having full knowledge of, say, one "wand of wonder", does not, by
808  * itself, give you knowledge, or even awareness, of other "wands of wonder".
809  * It happens that most "identify" routines (including "buying from a shop")
810  * will make the player "aware" of the object as well as fully "know" it.
811  *
812  * This routine also removes any inscriptions generated by "feelings".
813  */
object_known(object_type * o_ptr)814 void object_known(object_type *o_ptr)
815 {
816 	/* Remove "default inscriptions" */
817 	o_ptr->feeling = FEEL_NONE;
818 
819 	/* Clear the "Felt" info */
820 	o_ptr->info &= ~(OB_SENSE);
821 
822 	/* Clear the "Empty" info */
823 	o_ptr->info &= ~(OB_EMPTY);
824 
825 	/* Now we know about the item */
826 	o_ptr->info |= (OB_KNOWN);
827 }
828 
829 
830 /*
831  * The player is now aware of the effects of the given object.
832  */
object_aware(object_type * o_ptr)833 void object_aware(object_type *o_ptr)
834 {
835 	/* Fully aware of the effects */
836 	k_info[o_ptr->k_idx].aware = TRUE;
837 }
838 
839 
840 /*
841  * Something has been "sampled"
842  */
object_tried(object_type * o_ptr)843 void object_tried(object_type *o_ptr)
844 {
845 	/* Mark it as tried (even if "aware") */
846 	k_info[o_ptr->k_idx].tried = TRUE;
847 }
848 
849 /*
850  * The player has now *identified* the item
851  */
object_mental(object_type * o_ptr)852 void object_mental(object_type *o_ptr)
853 {
854 	o_ptr->info |= (OB_MENTAL);
855 }
856 
857 
858 /*
859  * Return the "value" of an "unknown" item
860  * Make a guess at the value of non-aware items
861  */
object_value_base(const object_type * o_ptr)862 static s32b object_value_base(const object_type *o_ptr)
863 {
864 	/* Aware item -- use template cost */
865 	if (object_aware_p(o_ptr)) return (k_info[o_ptr->k_idx].cost);
866 
867 	/* Analyze the type */
868 	switch (o_ptr->tval)
869 	{
870 		case TV_FOOD:
871 		{
872 			/* Un-aware Food */
873 			return (5L);
874 		}
875 
876 		case TV_POTION:
877 		{
878 			/* Un-aware Potions */
879 			return (20L);
880 		}
881 
882 		case TV_SCROLL:
883 		{
884 			/* Un-aware Scrolls */
885 			return (20L);
886 		}
887 
888 		case TV_STAFF:
889 		{
890 			/* Un-aware Staffs */
891 			return (70L);
892 		}
893 
894 		case TV_WAND:
895 		{
896 			/* Un-aware Wands */
897 			return (50L);
898 		}
899 
900 		case TV_ROD:
901 		{
902 			/* Un-aware Rods */
903 			return (90L);
904 		}
905 
906 		case TV_RING:
907 		{
908 			/* Un-aware Rings */
909 			return (45L);
910 		}
911 
912 		case TV_AMULET:
913 		{
914 			/* Un-aware Amulets */
915 			return (45L);
916 		}
917 	}
918 
919 	/* Paranoia -- Oops */
920 	return (0L);
921 }
922 
923 /* Return the sign of the argument * x * x. */
sqvalue(s32b x)924 static s32b sqvalue(s32b x)
925 {
926 	if (x < 0) return (-x * x);
927 	return (x * x);
928 }
929 
930 /* Return a number dependent on the argument */
bonus_value(s32b x)931 static s32b bonus_value(s32b x)
932 {
933 	if (x <= 0)
934 		return 5 * x;
935 	if (x < 10)
936 		return x + 5;
937 	if (x < 20)
938 		return 2 * (x - 10) + 15;
939 	return 5 * (x - 20) + 35;
940 }
941 
942 
943 /* Return the value of the flags the object has... */
flag_cost(const object_type * o_ptr,int plusses)944 s32b flag_cost(const object_type *o_ptr, int plusses)
945 {
946 	s32b total = 0;
947 
948 	if (FLAG(o_ptr, TR_STR)) total += (500 * plusses);
949 	if (FLAG(o_ptr, TR_INT)) total += (500 * plusses);
950 	if (FLAG(o_ptr, TR_WIS)) total += (500 * plusses);
951 	if (FLAG(o_ptr, TR_DEX)) total += (500 * plusses);
952 	if (FLAG(o_ptr, TR_CON)) total += (500 * plusses);
953 	if (FLAG(o_ptr, TR_CHR)) total += (250 * plusses);
954 
955 	if ((FLAG(o_ptr, TR_SP)) && (plusses > 0)) total += (2500 * plusses);
956 	if (FLAG(o_ptr, TR_CHAOTIC)) total += 500;
957 	if (FLAG(o_ptr, TR_VAMPIRIC)) total += 5000;
958 	if (FLAG(o_ptr, TR_STEALTH)) total += (50 * plusses);
959 	if (FLAG(o_ptr, TR_SEARCH)) total += (50 * plusses);
960 	if (FLAG(o_ptr, TR_INFRA)) total += (30 * plusses);
961 	if (FLAG(o_ptr, TR_TUNNEL)) total += (20 * plusses);
962 	if ((FLAG(o_ptr, TR_SPEED)) && (plusses > 0)) total += (500 * sqvalue(plusses));
963 	if ((FLAG(o_ptr, TR_BLOWS)) && (plusses > 0)) total += (2500 * sqvalue(plusses));
964 
965 	if (FLAG(o_ptr, TR_SLAY_ANIMAL)) total += 750;
966 	if (FLAG(o_ptr, TR_SLAY_EVIL))   total += 750;
967 	if (FLAG(o_ptr, TR_SLAY_UNDEAD)) total += 750;
968 	if (FLAG(o_ptr, TR_SLAY_DEMON))  total += 750;
969 	if (FLAG(o_ptr, TR_SLAY_ORC))    total += 750;
970 	if (FLAG(o_ptr, TR_SLAY_TROLL))  total += 750;
971 	if (FLAG(o_ptr, TR_SLAY_GIANT))  total += 750;
972 	if (FLAG(o_ptr, TR_SLAY_DRAGON)) total += 750;
973 	if (FLAG(o_ptr, TR_KILL_DRAGON)) total += 1500;
974 
975 	if (FLAG(o_ptr, TR_VORPAL)) total += 1500;
976 	if (FLAG(o_ptr, TR_IMPACT)) total += 1500;
977 
978 	if (FLAG(o_ptr, TR_BRAND_POIS)) total += 750;
979 	if (FLAG(o_ptr, TR_BRAND_ACID)) total += 750;
980 	if (FLAG(o_ptr, TR_BRAND_ELEC)) total += 750;
981 	if (FLAG(o_ptr, TR_BRAND_FIRE)) total += 750;
982 	if (FLAG(o_ptr, TR_BRAND_COLD)) total += 750;
983 
984 	if (FLAG(o_ptr, TR_SUST_STR)) total += 200;
985 	if (FLAG(o_ptr, TR_SUST_INT)) total += 200;
986 	if (FLAG(o_ptr, TR_SUST_WIS)) total += 200;
987 	if (FLAG(o_ptr, TR_SUST_DEX)) total += 200;
988 	if (FLAG(o_ptr, TR_SUST_CON)) total += 200;
989 	if (FLAG(o_ptr, TR_SUST_CHR)) total += 200;
990 
991 	if (FLAG(o_ptr, TR_IM_POIS)) total += 10000;
992 	if (FLAG(o_ptr, TR_IM_ACID)) total += 10000;
993 	if (FLAG(o_ptr, TR_IM_ELEC)) total += 10000;
994 	if (FLAG(o_ptr, TR_IM_FIRE)) total += 10000;
995 	if (FLAG(o_ptr, TR_IM_COLD)) total += 10000;
996 	if (FLAG(o_ptr, TR_IM_LITE)) total += 10000;
997 	if (FLAG(o_ptr, TR_IM_DARK)) total += 10000;
998 
999 	if (FLAG(o_ptr, TR_THROW)) total += 2000;
1000 	if (FLAG(o_ptr, TR_REFLECT)) total += 5000;
1001 
1002 	if (FLAG(o_ptr, TR_RES_ACID))   total += 500;
1003 	if (FLAG(o_ptr, TR_RES_ELEC))   total += 500;
1004 	if (FLAG(o_ptr, TR_RES_FIRE))   total += 500;
1005 	if (FLAG(o_ptr, TR_RES_COLD))   total += 500;
1006 	if (FLAG(o_ptr, TR_RES_POIS))   total += 1500;
1007 	if (FLAG(o_ptr, TR_RES_FEAR))   total += 1500;
1008 	if (FLAG(o_ptr, TR_RES_LITE))   total += 1500;
1009 	if (FLAG(o_ptr, TR_RES_DARK))   total += 1500;
1010 	if (FLAG(o_ptr, TR_RES_BLIND))  total += 1500;
1011 	if (FLAG(o_ptr, TR_RES_CONF))   total += 1500;
1012 	if (FLAG(o_ptr, TR_RES_SOUND))  total += 1500;
1013 	if (FLAG(o_ptr, TR_RES_SHARDS)) total += 1500;
1014 	if (FLAG(o_ptr, TR_RES_NETHER)) total += 1500;
1015 	if (FLAG(o_ptr, TR_RES_NEXUS))  total += 1500;
1016 	if (FLAG(o_ptr, TR_RES_CHAOS))  total += 1500;
1017 	if (FLAG(o_ptr, TR_RES_DISEN))  total += 1500;
1018 
1019 	if (FLAG(o_ptr, TR_SH_FIRE)) total += 1000;
1020 	if (FLAG(o_ptr, TR_SH_ELEC)) total += 1000;
1021 	if (FLAG(o_ptr, TR_SH_ACID)) total += 1000;
1022 	if (FLAG(o_ptr, TR_SH_COLD)) total += 1000;
1023 
1024 	if (FLAG(o_ptr, TR_QUESTITEM)) total += 0;
1025 	if (FLAG(o_ptr, TR_XXX4)) total += 0;
1026 	if (FLAG(o_ptr, TR_NO_TELE)) total += 2500;
1027 	if (FLAG(o_ptr, TR_NO_MAGIC)) total += 2500;
1028 	if (FLAG(o_ptr, TR_TY_CURSE)) total -= 15000;
1029 	if (FLAG(o_ptr, TR_EASY_KNOW)) total += 0;
1030 	if (FLAG(o_ptr, TR_HIDE_TYPE)) total += 0;
1031 	if (FLAG(o_ptr, TR_SHOW_MODS)) total += 0;
1032 	if (FLAG(o_ptr, TR_INSTA_ART)) total += 0;
1033 
1034 	/* EGO_XTRA_ABILITY */
1035 	if (FLAG(o_ptr, TR_SLOW_DIGEST)) total += 250;
1036 	if (FLAG(o_ptr, TR_FEATHER))     total += 250;
1037 	if (FLAG(o_ptr, TR_LITE))        total += 500;
1038 	if (FLAG(o_ptr, TR_SEE_INVIS))   total += 500;
1039 	if (FLAG(o_ptr, TR_REGEN))       total += 1000;
1040 	if (FLAG(o_ptr, TR_FREE_ACT))    total += 1000;
1041 	if (FLAG(o_ptr, TR_HOLD_LIFE))   total += 2000;
1042 	if (FLAG(o_ptr, TR_TELEPATHY))   total += 2000;
1043 
1044 	if (FLAG(o_ptr, TR_XTRA_MIGHT)) total += 1000;
1045 	if (FLAG(o_ptr, TR_XTRA_SHOTS)) total += 1000;
1046 
1047 	if (FLAG(o_ptr, TR_IGNORE_ACID)) total += 50;
1048 	if (FLAG(o_ptr, TR_IGNORE_ELEC)) total += 50;
1049 	if (FLAG(o_ptr, TR_IGNORE_FIRE)) total += 50;
1050 	if (FLAG(o_ptr, TR_IGNORE_COLD)) total += 50;
1051 
1052 	if (FLAG(o_ptr, TR_ACTIVATE)) total += 0;
1053 	if (FLAG(o_ptr, TR_DRAIN_EXP)) total -= 12500;
1054 	if (FLAG(o_ptr, TR_TELEPORT))
1055 	{
1056 		if (cursed_p(o_ptr))
1057 			total -= 7500;
1058 		else
1059 			total += 250;
1060 	}
1061 	if (FLAG(o_ptr, TR_AGGRAVATE)) total -= 5000;
1062 	if (FLAG(o_ptr, TR_BLESSED)) total += 200;
1063 	if (FLAG(o_ptr, TR_CURSED)) total -= 5000;
1064 	if (FLAG(o_ptr, TR_HEAVY_CURSE)) total -= 12500;
1065 	if (FLAG(o_ptr, TR_PERMA_CURSE)) total -= 15000;
1066 	if (FLAG(o_ptr, TR_LUCK_10)) total += 3000;
1067 	if (FLAG(o_ptr, TR_WILD_SHOT)) total += 50;
1068 	if (FLAG(o_ptr, TR_WILD_WALK)) total += 200;
1069 	if (FLAG(o_ptr, TR_MUTATE)) total += 500;
1070 	if (FLAG(o_ptr, TR_PATRON)) total += 500;
1071 	if (FLAG(o_ptr, TR_STRANGE_LUCK)) total += 2000;
1072 	if (FLAG(o_ptr, TR_PASS_WALL)) total += 25000;
1073 	if (FLAG(o_ptr, TR_GHOUL_TOUCH)) total += 750;
1074 	if (FLAG(o_ptr, TR_PSI_CRIT)) total += 1500;
1075 	if (FLAG(o_ptr, TR_RETURN)) total += 500;
1076 	if (FLAG(o_ptr, TR_EXPLODE)) total += 500;
1077 	if (FLAG(o_ptr, TR_HURT_ACID)) total -= 5000;
1078 	if (FLAG(o_ptr, TR_HURT_ELEC)) total -= 5000;
1079 	if (FLAG(o_ptr, TR_HURT_FIRE)) total -= 5000;
1080 	if (FLAG(o_ptr, TR_HURT_COLD)) total -= 5000;
1081 	if (FLAG(o_ptr, TR_HURT_LITE)) total -= 2500;
1082 	if (FLAG(o_ptr, TR_HURT_DARK)) total -= 2500;
1083 	if (FLAG(o_ptr, TR_XXX27)) total += 0;
1084 	if (FLAG(o_ptr, TR_XXX28)) total += 0;
1085 	if (FLAG(o_ptr, TR_AUTO_CURSE)) total -= 2500;
1086 	if (FLAG(o_ptr, TR_DRAIN_STATS)) total -= 5000;
1087 	if (FLAG(o_ptr, TR_CANT_EAT)) total -= 500;
1088 	if (FLAG(o_ptr, TR_SLOW_HEAL)) total -= 2000;
1089 
1090 
1091 	return total;
1092 }
1093 
1094 
1095 /*
1096  * Return the "real" price of a "known" item, not including discounts
1097  *
1098  * Wand and staffs get cost for each charge
1099  *
1100  * Armor is worth an extra 5 gold per bonus point to armor class^2.
1101  *
1102  * Weapons are worth an extra 5 gold per bonus point^2 (AC,TH,TD).
1103  *
1104  * Note: the bonuses are proportional to the points squared.
1105  *
1106  * Missiles are only worth 5 gold per bonus point, since they
1107  * usually appear in groups of 20, and we want the player to get
1108  * the same amount of cash for any "equivalent" item.  Note that
1109  * missiles never have any of the "pval" flags, and in fact, they
1110  * only have a few of the available flags, primarily of the "slay"
1111  * and "brand" and "ignore" variety.
1112  *
1113  * Armor with a negative armor bonus is worthless.
1114  *
1115  * Every wearable item with a "pval" bonus is worth extra (see below).
1116  */
object_value_real(const object_type * o_ptr)1117 s32b object_value_real(const object_type *o_ptr)
1118 {
1119 	s32b value;
1120 
1121 	object_kind *k_ptr = &k_info[o_ptr->k_idx];
1122 	object_type dummy;
1123 
1124 	/* Base cost */
1125 	value = o_ptr->cost;
1126 
1127 	/* Hack -- "worthless" items */
1128 	if (!value) return (0L);
1129 
1130 	/*
1131 	 * For non-artifact items, count the flag value
1132 	 */
1133 	if (!o_ptr->a_idx)
1134 	{
1135 		int divisor = 1;
1136 
1137 		/* Ammo gets less value from flags */
1138 		if (o_ptr->tval == TV_SHOT ||
1139 		    o_ptr->tval == TV_ARROW ||
1140 		    o_ptr->tval == TV_BOLT)
1141 		{
1142 			divisor = 20;
1143 		}
1144 
1145 		/* Initialize the dummy object */
1146 		memcpy(&dummy, o_ptr, sizeof(object_type));
1147 
1148 		/* Copy the flags from the type to the dummy object */
1149 		memcpy(dummy.flags, k_ptr->flags, sizeof(dummy.flags));
1150 
1151 		/* Price the object's flags vs the default flags */
1152 		value += flag_cost(o_ptr, o_ptr->pval) / divisor;
1153 		value -= flag_cost(&dummy, 1) / divisor;
1154 	}
1155 
1156 
1157 	/* Analyze the item */
1158 	switch (o_ptr->tval)
1159 	{
1160 		case TV_WAND:
1161 		{
1162 			/* Wands */
1163 
1164 			/* Pay extra for charges, depending on standard number of
1165 			 * charges.  Handle new-style wands correctly. -LM-
1166 			 */
1167 			value += (value * o_ptr->pval / o_ptr->number / (k_ptr->pval * 2));
1168 
1169 			/* Done */
1170 			break;
1171 		}
1172 		case TV_STAFF:
1173 		{
1174 			/* Staffs */
1175 
1176 			/* Pay extra for charges, depending on standard number of
1177 			 * charges.  -LM-
1178 			 */
1179 			value += (value * o_ptr->pval / (k_ptr->pval * 2));
1180 
1181 			/* Done */
1182 			break;
1183 		}
1184 
1185 		case TV_RING:
1186 		case TV_AMULET:
1187 		{
1188             /* Rings/Amulets */
1189 
1190             /* Hack -- special handling for amulets of Berserk Strength */
1191             if (o_ptr->sval == SV_AMULET_BERSERK)
1192             {
1193                 value += (bonus_value(o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 20);
1194 
1195                 /* Done */
1196                 break;
1197             }
1198 
1199 			/* Hack -- negative bonuses are bad */
1200 			if (o_ptr->to_a < 0) return (0L);
1201 			if (o_ptr->to_h < 0) return (0L);
1202 			if (o_ptr->to_d < 0) return (0L);
1203 
1204 			/* Factor in the bonuses */
1205 			/*
1206 			 * Note that combat bonuses on a ring/amulet are worth
1207 			 * twice what they are on a weapon/armor
1208 			 */
1209 			value += (bonus_value(o_ptr->to_h + o_ptr->to_d) * 40 +
1210 					  bonus_value(o_ptr->to_a * 2) * 20);
1211 
1212 			/* Done */
1213 			break;
1214 		}
1215 
1216 		case TV_BOOTS:
1217 		case TV_GLOVES:
1218 		case TV_CLOAK:
1219 		case TV_CROWN:
1220 		case TV_HELM:
1221 		case TV_SHIELD:
1222 		case TV_SOFT_ARMOR:
1223 		case TV_HARD_ARMOR:
1224 		case TV_DRAG_ARMOR:
1225 		{
1226 			/* Armour */
1227 
1228 			/* Factor in the bonuses */
1229 			value += (bonus_value(o_ptr->to_h - k_ptr->to_h +
1230 								  o_ptr->to_d - k_ptr->to_d) * 20);
1231 			value += (bonus_value((o_ptr->to_a - k_ptr->to_a) * 2) * 10);
1232 
1233 			/* Done */
1234 			break;
1235 		}
1236 
1237 		case TV_BOW:
1238 		case TV_DIGGING:
1239 		case TV_HAFTED:
1240 		case TV_SWORD:
1241 		case TV_POLEARM:
1242 		{
1243 			/* Bows/Weapons */
1244 
1245 			/* Factor in the bonuses */
1246 			value += (bonus_value(o_ptr->to_h + o_ptr->to_d) * 20 +
1247 					  bonus_value(o_ptr->to_a * 2) * 10);
1248 
1249 			/* Hack -- Factor in extra damage dice */
1250 			if (k_ptr->dd * k_ptr->ds)
1251 			{
1252 				value = value * o_ptr->dd * o_ptr->ds / (k_ptr->dd * k_ptr->ds);
1253 			}
1254 
1255 			/* Done */
1256 			break;
1257 		}
1258 
1259 		case TV_SHOT:
1260 		case TV_ARROW:
1261 		case TV_BOLT:
1262 		{
1263 			/* Ammo */
1264 
1265 			/* Factor in the bonuses */
1266 			value += (bonus_value(o_ptr->to_h + o_ptr->to_d) * 2);
1267 
1268 			/* Hack -- Factor in extra damage dice */
1269 			if (k_ptr->dd * k_ptr->ds)
1270 			{
1271 				value = value * o_ptr->dd * o_ptr->ds / (k_ptr->dd * k_ptr->ds);
1272 			}
1273 
1274 			/* Done */
1275 			break;
1276 		}
1277 
1278 		case TV_FIGURINE:
1279 		{
1280 			/* Figurines, relative to monster level */
1281 			value = (r_info[o_ptr->pval].level *
1282 					 r_info[o_ptr->pval].level * 5L);
1283 			break;
1284 		}
1285 	}
1286 
1287 	/* No negative value */
1288 	if (value < 0) value = 0;
1289 
1290 	/* Return the value */
1291 	return (value);
1292 }
1293 
1294 
1295 /*
1296  * Return the price of an item including plusses (and charges)
1297  *
1298  * This function returns the "value" of the given item (qty one)
1299  *
1300  * Never notice "unknown" bonuses or properties, including "curses",
1301  * since that would give the player information he did not have.
1302  *
1303  * Note that discounted items stay discounted forever, even if
1304  * the discount is "forgotten" by the player via memory loss.
1305  */
object_value(const object_type * o_ptr)1306 s32b object_value(const object_type *o_ptr)
1307 {
1308 	s32b value;
1309 
1310 	/* Known items -- acquire the actual value */
1311 	if (object_known_p(o_ptr))
1312 	{
1313 		/* Broken items -- worthless */
1314 		if (!o_ptr->cost) return (0L);
1315 
1316 		/* Cursed items -- worthless */
1317 		if (cursed_p(o_ptr)) return (0L);
1318 
1319 		/* Real value (see above) */
1320 		value = object_value_real(o_ptr);
1321 	}
1322 
1323 	/* Unknown items -- acquire a base value */
1324 	else
1325 	{
1326 		/* Hack -- Felt broken items */
1327 		if ((o_ptr->info & (OB_SENSE)) && !o_ptr->cost) return (0L);
1328 
1329 		/* Hack -- Felt cursed items */
1330 		if ((o_ptr->info & (OB_SENSE)) && cursed_p(o_ptr)) return (0L);
1331 
1332 		/* Base value (see above) */
1333 		value = object_value_base(o_ptr);
1334 	}
1335 
1336 
1337 	/* Apply discount (if any) */
1338 	if (o_ptr->discount) value -= (value * o_ptr->discount / 100L);
1339 
1340 
1341 	/* Return the final value */
1342 	return (value);
1343 }
1344 
1345 
1346 /*
1347  * Distribute charges of rods or wands.
1348  *
1349  * o_ptr = source item
1350  * q_ptr = target item, must be of the same type as o_ptr
1351  * amt   = number of items that are transfered
1352  */
distribute_charges(object_type * o_ptr,object_type * q_ptr,int amt)1353 void distribute_charges(object_type *o_ptr, object_type *q_ptr, int amt)
1354 {
1355 	int new_charges;
1356 
1357 	/* Paranoia */
1358 	if (!o_ptr->number) return;
1359 
1360 	/*
1361 	 * Hack -- If rods or wands are dropped, the total maximum timeout or
1362 	 * charges needs to be allocated between the two stacks.  If all the items
1363 	 * are being dropped, it makes for a neater message to leave the original
1364 	 * stack's pval alone. -LM-
1365 	 */
1366 	if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_ROD))
1367 	{
1368 		new_charges = (o_ptr->pval + o_ptr->ac) * amt / o_ptr->number;
1369 
1370 		/* Hack - AC is a count of the "used" charges */
1371 		if (o_ptr->tval == TV_WAND)
1372 		{
1373 			/* Used more changes than a single wand has */
1374 			if (o_ptr->ac > new_charges)
1375 			{
1376 				/* Drop an empty wand */
1377 				q_ptr->pval = 0;
1378 
1379 				o_ptr->ac -= new_charges;
1380 				q_ptr->ac = new_charges;
1381 			}
1382 			else
1383 			{
1384 				/* Split the charges evenly - then move the "used ones" */
1385 				q_ptr->pval = new_charges - o_ptr->ac;
1386 
1387 				q_ptr->ac = o_ptr->ac;
1388 				o_ptr->ac = 0;
1389 			}
1390 		}
1391 		else
1392 		{
1393 			/* Rods are simple - just split them geometrically */
1394 			q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
1395 		}
1396 
1397 		/* Subtract moved charges */
1398 		if (amt < o_ptr->number) o_ptr->pval -= q_ptr->pval;
1399 
1400 		/* Hack -- Rods also need to have their timeouts distributed.  The
1401 		 * dropped stack will accept all time remaining to charge up to its
1402 		 * maximum.
1403 		 */
1404 		if ((o_ptr->tval == TV_ROD) && (o_ptr->timeout))
1405 		{
1406 			if (q_ptr->pval > o_ptr->timeout)
1407 				q_ptr->timeout = o_ptr->timeout;
1408 			else
1409 				q_ptr->timeout = q_ptr->pval;
1410 
1411 			if (amt < o_ptr->number) o_ptr->timeout -= q_ptr->timeout;
1412 		}
1413 	}
1414 }
1415 
1416 
reduce_charges(object_type * o_ptr,int amt)1417 void reduce_charges(object_type *o_ptr, int amt)
1418 {
1419 	/*
1420 	 * Hack -- If rods or wand are destroyed, the total maximum timeout or
1421 	 * charges of the stack needs to be reduced, unless all the items are
1422 	 * being destroyed. -LM-
1423 	 */
1424 	if (((o_ptr->tval == TV_WAND) ||
1425 		 (o_ptr->tval == TV_ROD)) && (amt < o_ptr->number))
1426 	{
1427 		o_ptr->pval -= o_ptr->pval * amt / o_ptr->number;
1428 		o_ptr->ac -= o_ptr->ac * amt / o_ptr->number;
1429 	}
1430 }
1431 
1432 
1433 /*
1434  * Determine if an item can "absorb" a second item
1435  *
1436  * See "object_absorb()" for the actual "absorption" code.
1437  *
1438  * If permitted, we allow staffs (if they are known to have equal charges
1439  * and both are either known or confirmed empty) and rods (in all cases)
1440  * to combine.
1441  * Staffs will unstack (if necessary) when they are used, but wands and
1442  * rods will only unstack if one is dropped. -LM-
1443  *
1444  * If permitted, we allow weapons/armor to stack, if fully "known".
1445  *
1446  * Missiles will combine if both stacks have the same "known" status.
1447  * This is done to make unidentified stacks of missiles useful.
1448  *
1449  * Food, potions, scrolls, and "easy know" items always stack.
1450  *
1451  * Chests, and activatable items, never stack (for various reasons).
1452  */
object_similar(const object_type * o_ptr,const object_type * j_ptr)1453 bool object_similar(const object_type *o_ptr, const object_type *j_ptr)
1454 {
1455 	int i;
1456 
1457 	/* Require identical object types */
1458 	if (o_ptr->k_idx != j_ptr->k_idx) return (FALSE);
1459 
1460 	/* Analyze the items */
1461 	switch (o_ptr->tval)
1462 	{
1463 		case TV_CHEST:
1464 		{
1465 			/* Chests */
1466 
1467 			/* Never okay */
1468 			return (FALSE);
1469 		}
1470 
1471 		case TV_FIGURINE:
1472 		case TV_STATUE:
1473 		{
1474 			/* Figurines and Statues */
1475 
1476 			/* Never okay */
1477 			return (FALSE);
1478 		}
1479 
1480 		case TV_FOOD:
1481 		case TV_POTION:
1482 		case TV_SCROLL:
1483 		{
1484 			/* Food and Potions and Scrolls */
1485 
1486 			/* Assume okay */
1487 			break;
1488 		}
1489 
1490 		case TV_STAFF:
1491 		{
1492 			/* Staffs */
1493 
1494 			/* Require either knowledge or known empty for both staffs. */
1495 			if ((!(o_ptr->info & (OB_EMPTY)) &&
1496 				 !object_known_p(o_ptr)) ||
1497 				(!(j_ptr->info & (OB_EMPTY)) &&
1498 				 !object_known_p(j_ptr))) return (FALSE);
1499 
1500 			/* Require identical charges, since staffs are bulky. */
1501 			if (o_ptr->pval != j_ptr->pval) return (FALSE);
1502 
1503 			/* Assume okay */
1504 			break;
1505 		}
1506 
1507 		case TV_WAND:
1508 		{
1509 			/* Wands */
1510 
1511 			/* Wand charges combine in O&ZAngband. */
1512 			if (object_known_p(o_ptr) != object_known_p(j_ptr)) return (FALSE);
1513 
1514 			/* Assume okay */
1515 			break;
1516 		}
1517 
1518 		case TV_ROD:
1519 		{
1520 			/* Rods */
1521 
1522 			/* Assume okay */
1523 			break;
1524 		}
1525 
1526 		case TV_BOW:
1527 		case TV_DIGGING:
1528 		case TV_HAFTED:
1529 		case TV_POLEARM:
1530 		case TV_SWORD:
1531 		case TV_BOOTS:
1532 		case TV_GLOVES:
1533 		case TV_HELM:
1534 		case TV_CROWN:
1535 		case TV_SHIELD:
1536 		case TV_CLOAK:
1537 		case TV_SOFT_ARMOR:
1538 		case TV_HARD_ARMOR:
1539 		case TV_DRAG_ARMOR:
1540 		{
1541 			/* Weapons and Armor stack if throwable */
1542 			if (!(FLAG(o_ptr, TR_THROW))) return (FALSE);
1543 
1544 			/* Fall through */
1545 		}
1546 
1547 		case TV_LITE:
1548 		{
1549 			/* Hack - Require identical fuel levels / timeouts */
1550 			if (o_ptr->timeout != j_ptr->timeout) return (FALSE);
1551 
1552 			/* Fall through */
1553 		}
1554 
1555 		case TV_RING:
1556 		case TV_AMULET:
1557 		{
1558 			/* Rings, Amulets, Lites */
1559 
1560 			/* Require full knowledge of both items */
1561 			if (!object_known_p(o_ptr)
1562 				|| !object_known_p(j_ptr)) return (FALSE);
1563 			/* Fall through */
1564 		}
1565 
1566 		case TV_BOLT:
1567 		case TV_ARROW:
1568 		case TV_SHOT:
1569 		{
1570 			/* Missiles */
1571 
1572 			/* Require identical knowledge of both items */
1573 			if (object_known_p(o_ptr) != object_known_p(j_ptr)) return (FALSE);
1574 
1575 			/* Require identical "bonuses" */
1576 			if (o_ptr->to_h != j_ptr->to_h) return (FALSE);
1577 			if (o_ptr->to_d != j_ptr->to_d) return (FALSE);
1578 			if (o_ptr->to_a != j_ptr->to_a) return (FALSE);
1579 
1580 			/* Require identical "pval" code */
1581 			if (o_ptr->pval != j_ptr->pval) return (FALSE);
1582 
1583 			/* Require identical "values" */
1584 			if (o_ptr->ac != j_ptr->ac) return (FALSE);
1585 			if (o_ptr->dd != j_ptr->dd) return (FALSE);
1586 			if (o_ptr->ds != j_ptr->ds) return (FALSE);
1587 
1588 			/* Probably okay */
1589 			break;
1590 		}
1591 
1592 		default:
1593 		{
1594 			/* Various */
1595 
1596 			/* Require knowledge */
1597 			if (!object_known_p(o_ptr)
1598 				|| !object_known_p(j_ptr)) return (FALSE);
1599 
1600 			/* Probably okay */
1601 			break;
1602 		}
1603 	}
1604 
1605 	/* Hack -- Identical flags! */
1606 	if ((o_ptr->flags[0] != j_ptr->flags[0]) ||
1607 		(o_ptr->flags[1] != j_ptr->flags[1]) ||
1608 		(o_ptr->flags[2] != j_ptr->flags[2]) ||
1609 		(o_ptr->flags[3] != j_ptr->flags[3]))
1610 		return (FALSE);
1611 
1612 	/* Hack -- Require identical "cursed" status */
1613 	if (cursed_p(o_ptr) != cursed_p(j_ptr)) return (FALSE);
1614 
1615 	/* Hack -- Require identical "broken" status */
1616 	if ((!o_ptr->cost) != (!j_ptr->cost)) return (FALSE);
1617 
1618 	/* Need to be identical ego items or artifacts */
1619 	if (o_ptr->xtra_name != j_ptr->xtra_name) return (FALSE);
1620 
1621 	/* Hack -- require semi-matching "inscriptions" */
1622 	if (o_ptr->inscription && j_ptr->inscription &&
1623 		(o_ptr->inscription != j_ptr->inscription))
1624 		return (FALSE);
1625 
1626 	/* Require matching scripts */
1627 	for (i = 0; i < MAX_TRIGGER; i++)
1628 		if (o_ptr->trigger[i] != j_ptr->trigger[i])
1629 			return (FALSE);
1630 
1631 	/* Maximal "stacking" limit */
1632 	if (o_ptr->number + j_ptr->number >= MAX_STACK_SIZE) return (FALSE);
1633 
1634 	/* They match, so they must be similar */
1635 	return (TRUE);
1636 }
1637 
1638 
1639 /*
1640  * Allow one item to "absorb" another, assuming they are similar
1641  */
object_absorb(object_type * o_ptr,const object_type * j_ptr)1642 void object_absorb(object_type *o_ptr, const object_type *j_ptr)
1643 {
1644 	int total = o_ptr->number + j_ptr->number;
1645 
1646 	/* Add together the item counts */
1647 	o_ptr->number = ((total < MAX_STACK_SIZE) ? total : (MAX_STACK_SIZE - 1));
1648 
1649 	/* Hack -- blend "known" status */
1650 	if (object_known_p(j_ptr)) object_known(o_ptr);
1651 
1652 	/* If the extra object was fully known */
1653 	if (object_known_full(j_ptr))
1654 	{
1655 		/* Then the stack is fully known too */
1656 		object_mental(o_ptr);
1657 	}
1658 
1659 	/* Hack -- blend "mental" status */
1660 	o_ptr->kn_flags[0] |= j_ptr->kn_flags[0];
1661 	o_ptr->kn_flags[1] |= j_ptr->kn_flags[1];
1662 	o_ptr->kn_flags[2] |= j_ptr->kn_flags[2];
1663 	o_ptr->kn_flags[3] |= j_ptr->kn_flags[3];
1664 
1665 	/* Hack -- blend "inscriptions" */
1666     if (j_ptr->inscription)
1667     {
1668         quark_remove(&o_ptr->inscription);
1669         o_ptr->inscription = j_ptr->inscription;
1670         quark_dup(j_ptr->inscription);
1671     }
1672 
1673 	/* Hack -- blend "feelings" */
1674 	if (j_ptr->feeling) o_ptr->feeling = j_ptr->feeling;
1675 
1676 	/* Hack -- could average discounts XXX XXX XXX */
1677 	/* Hack -- save largest discount XXX XXX XXX */
1678 	if (o_ptr->discount < j_ptr->discount) o_ptr->discount = j_ptr->discount;
1679 
1680 	/*
1681 	 * Hack -- if rods are stacking, add the pvals
1682 	 * (maximum timeouts) and current timeouts together. -LM-
1683 	 */
1684 	if (o_ptr->tval == TV_ROD)
1685 	{
1686 		o_ptr->pval += j_ptr->pval;
1687 		o_ptr->timeout += j_ptr->timeout;
1688 	}
1689 
1690 	/* Hack -- if wands are stacking, combine the charges. -LM- */
1691 	if (o_ptr->tval == TV_WAND)
1692 	{
1693 		o_ptr->pval += j_ptr->pval;
1694 		o_ptr->ac += j_ptr->ac;
1695 
1696 		/* Hack XXX XXX - remove {empty} inscription. */
1697 		if (o_ptr->pval) o_ptr->info &= ~(OB_EMPTY);
1698 	}
1699 }
1700 
1701 
1702 /*
1703  * Are these objects the same except for next_o_idx?  Originally meant for
1704  * matching object in the inventory so there may be some redundancy here.
1705  */
object_equal(const object_type * o_ptr,const object_type * j_ptr)1706 bool object_equal(const object_type *o_ptr, const object_type *j_ptr)
1707 {
1708 	int i;
1709 
1710 	/* Require identical object types */
1711 	if (o_ptr->k_idx != j_ptr->k_idx) return (FALSE);
1712 
1713 	/* Identical position */
1714 	if (o_ptr->ix != j_ptr->ix ||
1715 		o_ptr->iy != j_ptr->iy) return (FALSE);
1716 
1717 	/* Require identical weights */
1718 	if (o_ptr->weight != j_ptr->weight) return (FALSE);
1719 
1720 	/* Require identical tval, sval, pval */
1721 	if (o_ptr->tval != j_ptr->tval ||
1722 		o_ptr->sval != j_ptr->sval ||
1723 		o_ptr->pval != j_ptr->pval) return (FALSE);
1724 
1725 	/* Require identical discounts */
1726 	if (o_ptr->discount != j_ptr->discount) return (FALSE);
1727 
1728 	/* There is an equal number in the pile */
1729 	if (o_ptr->number != j_ptr->number) return (FALSE);
1730 
1731 	/* Require identical to_h, to_d, to_a, ac */
1732 	if (o_ptr->to_h != j_ptr->to_h ||
1733 		o_ptr->to_d != j_ptr->to_d ||
1734 		o_ptr->to_a != j_ptr->to_a ||
1735 		o_ptr->ac != j_ptr->ac) return (FALSE);
1736 
1737 	/* The timeout is the same */
1738 	if (o_ptr->timeout != j_ptr->timeout) return (FALSE);
1739 
1740 	/* Identical dice */
1741 	if (o_ptr->dd != j_ptr->dd ||
1742 		o_ptr->ds != j_ptr->ds) return (FALSE);
1743 
1744 	/* Hack -- require semi-matching "inscriptions" */
1745 	if (o_ptr->inscription && j_ptr->inscription &&
1746 		(o_ptr->inscription != j_ptr->inscription))
1747 		return (FALSE);
1748 
1749 	/* Need to be identical ego items or artifacts */
1750 	if (o_ptr->xtra_name != j_ptr->xtra_name) return (FALSE);
1751 
1752 	/* Identical flags! */
1753 	if ((o_ptr->flags[0] != j_ptr->flags[0]) ||
1754 		(o_ptr->flags[1] != j_ptr->flags[1]) ||
1755 		(o_ptr->flags[2] != j_ptr->flags[2]) ||
1756 		(o_ptr->flags[3] != j_ptr->flags[3]))
1757 		return (FALSE);
1758 
1759 	/* Identical kn_flags! */
1760 	if ((o_ptr->kn_flags[0] != j_ptr->kn_flags[0]) ||
1761 		(o_ptr->kn_flags[1] != j_ptr->kn_flags[1]) ||
1762 		(o_ptr->kn_flags[2] != j_ptr->kn_flags[2]) ||
1763 		(o_ptr->kn_flags[3] != j_ptr->kn_flags[3]))
1764 		return (FALSE);
1765 
1766 	/* Require identical "broken" status */
1767 	if ((!o_ptr->cost) != (!j_ptr->cost)) return (FALSE);
1768 
1769 	/* The feeling is the same */
1770 	if (o_ptr->feeling != j_ptr->feeling) return (FALSE);
1771 
1772 	/* The a_idx is the same */
1773 	if (o_ptr->a_idx != j_ptr->a_idx) return (FALSE);
1774 
1775 	/* The info is the same */
1776 	if (o_ptr->info != j_ptr->info) return (FALSE);
1777 
1778 	/* Require matching scripts */
1779 	for (i = 0; i < MAX_TRIGGER; i++)
1780 		if (o_ptr->trigger[i] != j_ptr->trigger[i])
1781 			return (FALSE);
1782 
1783 	/* The objects are the same */
1784 	return (TRUE);
1785 }
1786 
1787 
1788 /*
1789  * Find the index of the object_kind with the given tval and sval
1790  */
lookup_kind(int tval,int sval)1791 s16b lookup_kind(int tval, int sval)
1792 {
1793 	int k;
1794 	int num = 0;
1795 	int bk = 0;
1796 
1797 	/* Look for it */
1798 	for (k = 1; k < z_info->k_max; k++)
1799 	{
1800 		object_kind *k_ptr = &k_info[k];
1801 
1802 		/* Require correct tval */
1803 		if (k_ptr->tval != tval) continue;
1804 
1805 		/* Found a match */
1806 		if (k_ptr->sval == sval) return (k);
1807 
1808 		/* Ignore illegal items */
1809 		if (sval != SV_ANY) continue;
1810 
1811 		/* Apply the randomizer */
1812 		if (!one_in_(++num)) continue;
1813 
1814 		/* Use this value */
1815 		bk = k;
1816 	}
1817 
1818 	/* Return this choice */
1819 	if (sval == SV_ANY)
1820 	{
1821 		return bk;
1822 	}
1823 
1824 	/* Oops */
1825 	msgf("No object (%d,%d)", tval, sval);
1826 
1827 	/* Oops */
1828 	return (0);
1829 }
1830 
1831 
1832 /*
1833  * Wipe an object clean.
1834  */
object_wipe(object_type * o_ptr)1835 void object_wipe(object_type *o_ptr)
1836 {
1837 	/* Delete the references */
1838 	delete_static_object(o_ptr);
1839 
1840 	/* Wipe the structure */
1841 	(void)WIPE(o_ptr, object_type);
1842 }
1843 
1844 /*
1845  * Duplicate an object, and have the copy stored in the
1846  * standard static temp object.
1847  */
object_dup(const object_type * o_ptr)1848 object_type *object_dup(const object_type *o_ptr)
1849 {
1850 	int i;
1851 
1852 	object_type *q_ptr = &temp_object;
1853 
1854 	/* Delete old static object */
1855 	delete_static_object(q_ptr);
1856 
1857 	/* Copy it */
1858 	object_copy(q_ptr, o_ptr);
1859 
1860 	/* Allocate quarks */
1861 	quark_dup(o_ptr->xtra_name);
1862 	quark_dup(o_ptr->inscription);
1863 
1864 	for (i = 0; i < MAX_TRIGGER; i++)
1865 		quark_dup(o_ptr->trigger[i]);
1866 
1867 	/* Return a pointer to the static object */
1868 	return (q_ptr);
1869 }
1870 
1871 
1872 /*
1873  * Prepare an object based on an object kind.
1874  */
object_prep(int k_idx)1875 object_type *object_prep(int k_idx)
1876 {
1877 	object_type *o_ptr = &temp_object;
1878 
1879 	object_kind *k_ptr = &k_info[k_idx];
1880 
1881 	/* Clear the record */
1882 	object_wipe(o_ptr);
1883 
1884 	/* Save the kind index */
1885 	o_ptr->k_idx = k_idx;
1886 
1887 	/* Efficiency -- tval/sval */
1888 	o_ptr->tval = k_ptr->tval;
1889 	o_ptr->sval = k_ptr->sval;
1890 
1891 	/* Default "pval" */
1892 	o_ptr->pval = k_ptr->pval;
1893 
1894 	/* Default number */
1895 	o_ptr->number = 1;
1896 
1897 	/* Default weight */
1898 	o_ptr->weight = k_ptr->weight;
1899 
1900 	/* Default magic */
1901 	o_ptr->to_h = k_ptr->to_h;
1902 	o_ptr->to_d = k_ptr->to_d;
1903 	o_ptr->to_a = k_ptr->to_a;
1904 
1905 	/* Default power */
1906 	o_ptr->ac = k_ptr->ac;
1907 	o_ptr->dd = k_ptr->dd;
1908 	o_ptr->ds = k_ptr->ds;
1909 
1910 	/* Set cost */
1911 	o_ptr->cost = k_ptr->cost;
1912 
1913 	/* Save the flags */
1914 	o_ptr->flags[0] = k_ptr->flags[0];
1915 	o_ptr->flags[1] = k_ptr->flags[1];
1916 	o_ptr->flags[2] = k_ptr->flags[2];
1917 	o_ptr->flags[3] = k_ptr->flags[3];
1918 
1919 #if 0
1920 	/*
1921 	 * Don't add the triggers - apply_object_trigger looks up the object
1922 	 * kind's trigger if the specific object has none
1923 	 */
1924 	for (i = 0; i < MAX_TRIGGER; i++)
1925 	{
1926 		if (k_ptr->trigger[i])
1927 		{
1928 			o_ptr->trigger[i] =
1929 				quark_add(k_text + k_ptr->trigger[i]);
1930 		}
1931 	}
1932 #endif
1933 
1934 	return (o_ptr);
1935 }
1936 
1937 
1938 /*
1939  * Help determine an "enchantment bonus" for an object.
1940  *
1941  * To avoid floating point but still provide a smooth distribution of bonuses,
1942  * we simply round the results of division in such a way as to "average" the
1943  * correct floating point value.
1944  *
1945  * This function has been changed.  It uses "Rand_mormal()" to choose values from
1946  * a normal distribution, whose mean moves from zero towards the max as the
1947  * level increases, and whose standard deviation is equal to 1/4 of the max,
1948  * and whose values are forced to lie between zero and the max, inclusive.
1949  *
1950  * Since the "level" rarely passes 100 before Morgoth is dead, it is very
1951  * rare to get the "full" enchantment on an object, even at deep levels.
1952  *
1953  * It is always possible (albeit unlikely) to get the "full" enchantment.
1954  *
1955  * A sample distribution of values from "m_bonus(10, N)" is shown below:
1956  *
1957  *   N       0     1     2     3     4     5     6     7     8     9    10
1958  * ---    ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----
1959  *   0   66.37 13.01  9.73  5.47  2.89  1.31  0.72  0.26  0.12  0.09  0.03
1960  *   8   46.85 24.66 12.13  8.13  4.20  2.30  1.05  0.36  0.19  0.08  0.05
1961  *  16   30.12 27.62 18.52 10.52  6.34  3.52  1.95  0.90  0.31  0.15  0.05
1962  *  24   22.44 15.62 30.14 12.92  8.55  5.30  2.39  1.63  0.62  0.28  0.11
1963  *  32   16.23 11.43 23.01 22.31 11.19  7.18  4.46  2.13  1.20  0.45  0.41
1964  *  40   10.76  8.91 12.80 29.51 16.00  9.69  5.90  3.43  1.47  0.88  0.65
1965  *  48    7.28  6.81 10.51 18.27 27.57 11.76  7.85  4.99  2.80  1.22  0.94
1966  *  56    4.41  4.73  8.52 11.96 24.94 19.78 11.06  7.18  3.68  1.96  1.78
1967  *  64    2.81  3.07  5.65  9.17 13.01 31.57 13.70  9.30  6.04  3.04  2.64
1968  *  72    1.87  1.99  3.68  7.15 10.56 20.24 25.78 12.17  7.52  4.42  4.62
1969  *  80    1.02  1.23  2.78  4.75  8.37 12.04 27.61 18.07 10.28  6.52  7.33
1970  *  88    0.70  0.57  1.56  3.12  6.34 10.06 15.76 30.46 12.58  8.47 10.38
1971  *  96    0.27  0.60  1.25  2.28  4.30  7.60 10.77 22.52 22.51 11.37 16.53
1972  * 104    0.22  0.42  0.77  1.36  2.62  5.33  8.93 13.05 29.54 15.23 22.53
1973  * 112    0.15  0.20  0.56  0.87  2.00  3.83  6.86 10.06 17.89 27.31 30.27
1974  * 120    0.03  0.11  0.31  0.46  1.31  2.48  4.60  7.78 11.67 25.53 45.72
1975  * 128    0.02  0.01  0.13  0.33  0.83  1.41  3.24  6.17  9.57 14.22 64.07
1976  */
m_bonus(int max,int level)1977 s16b m_bonus(int max, int level)
1978 {
1979 	int bonus, stand, extra, value;
1980 
1981 
1982 	/* Paranoia -- enforce maximal "level" */
1983 	if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
1984 
1985 
1986 	/* The "bonus" moves towards the max */
1987 	bonus = ((max * level) / MAX_DEPTH);
1988 
1989 	/* Hack -- determine fraction of error */
1990 	extra = ((max * level) % MAX_DEPTH);
1991 
1992 	/* Hack -- simulate floating point computations */
1993 	if (randint0(MAX_DEPTH) < extra) bonus++;
1994 
1995 
1996 	/* The "stand" is equal to one quarter of the max */
1997 	stand = (max / 4);
1998 
1999 	/* Hack -- determine fraction of error */
2000 	extra = (max % 4);
2001 
2002 	/* Hack -- simulate floating point computations */
2003 	if (randint0(4) < extra) stand++;
2004 
2005 
2006 	/* Choose an "interesting" value */
2007 	value = Rand_normal(bonus, stand);
2008 
2009 	/* Enforce the minimum value */
2010 	if (value < 0) return (0);
2011 
2012 	/* Enforce the maximum value */
2013 	if (value > max) return (max);
2014 
2015 	/* Result */
2016 	return (value);
2017 }
2018 
2019 
2020 /*
2021  * Weighted bonus
2022  *
2023  * This function creates a "wieghted bonus" that
2024  * depends on the difference between the "normal"
2025  * level of an object, and the current level.
2026  * [O] based combat makes the extra plusses not
2027  * quite as important as in the old combat system.
2028  *
2029  * A 4d4 weapon with negative multipliers often is
2030  * much better than a 1d4 weapon with high positive
2031  * modifiers.
2032  *
2033  * This function attempts to balance that effect by
2034  * making things that are normally "junk" more powerful,
2035  * and things that are too powerful, weak.
2036  */
w_bonus(int max,int lev_dif)2037 static s16b w_bonus(int max, int lev_dif)
2038 {
2039 	/* Paranoia - max must be greater than 5 */
2040 	if (max < 6) return (0);
2041 
2042 	/* Level difference is too small? */
2043 	if (ABS(lev_dif) < 10) return (0);
2044 
2045 	if (lev_dif < 0)
2046 	{
2047 		/* Negative bonus */
2048 		return (-m_bonus(max - 5, lev_dif));
2049 
2050 	}
2051 	else
2052 	{
2053 		/* Positive bonus */
2054 		return (randint1(5) + m_bonus(max - 5, lev_dif * 3));
2055 	}
2056 }
2057 
2058 
2059 /*
2060  * Cheat -- describe a created object for the user
2061  */
object_mention(object_type * o_ptr)2062 static void object_mention(object_type *o_ptr)
2063 {
2064 	cptr type;
2065 
2066 	/* Artifact */
2067 	if (FLAG(o_ptr, TR_INSTA_ART))
2068 	{
2069 		if (o_ptr->a_idx)
2070 		{
2071 			/* Silly message */
2072 			type = "Artifact (";
2073 		}
2074 		else
2075 		{
2076 			/* Silly message */
2077 			type = "Random artifact (";
2078 		}
2079 	}
2080 
2081 	/* Ego-item */
2082 	else if (ego_item_p(o_ptr))
2083 	{
2084 		/* Silly message */
2085 		type = "Ego-item (";
2086 	}
2087 
2088 	/* Normal item */
2089 	else
2090 	{
2091 		/* Silly message */
2092 		type = "Object (";
2093 	}
2094 
2095 	msgf("%s%v)", type, OBJECT_STORE_FMT(o_ptr, FALSE, 0));
2096 }
2097 
2098 
2099 /* Select ego items for the required slot */
get_ego_prep(byte slot,bool good)2100 static bool get_ego_prep(byte slot, bool good)
2101 {
2102 	int i;
2103 	ego_item_type *e_ptr;
2104 
2105 	bool match = FALSE;
2106 
2107 	alloc_entry *table = alloc_ego_table;
2108 
2109 	/* Scan the allocation table */
2110 	for (i = 0; i < alloc_ego_size; i++)
2111 	{
2112 		/* Get pointer to ego item type */
2113 		e_ptr = &e_info[table[i].index];
2114 
2115 		/* Keep matching items */
2116 		if ((e_ptr->slot == slot) &&
2117 			((good && e_ptr->rating) || (!good && !e_ptr->rating)))
2118 		{
2119 			/* Accept this ego item */
2120 			table[i].prob2 = table[i].prob1;
2121 
2122 			/* There is a matching type */
2123 			match = TRUE;
2124 		}
2125 		else
2126 		{
2127 			/* Reject this ego item */
2128 			table[i].prob2 = 0;
2129 		}
2130 	}
2131 
2132 	/* Was there a matching item? */
2133 	return (match);
2134 }
2135 
2136 
2137 /*
2138  * Get an ego item appropriate to a level.
2139  *
2140  * The "standard" method of using an allocation table is used to
2141  * make the selection.
2142  */
get_ego_num(int level)2143 static byte get_ego_num(int level)
2144 {
2145 	int i;
2146 
2147 	long total, value;
2148 
2149 	alloc_entry *table = alloc_ego_table;
2150 
2151 
2152 	/* Boost the level from time to time */
2153 	if (one_in_(EGO_INFLATE))
2154 	{
2155 		/* What a bizzare calculation */
2156 		level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
2157 	}
2158 
2159 	total = 0L;
2160 
2161 	/* Process the probabilities */
2162 	for (i = 0; i < alloc_ego_size; i++)
2163 	{
2164 		/* Ego items are _not_ sorted by depth (yet) */
2165 		if (table[i].level <= level)
2166 		{
2167 			total += table[i].prob2;
2168 		}
2169 	}
2170 
2171 	/* No legal ego items? */
2172 	if (total <= 0L) return (0);
2173 
2174 
2175 	/* Pick an ego item */
2176 	value = randint1(total);
2177 
2178 	/* Find the ego item */
2179 	for (i = 0; i < alloc_ego_size; i++)
2180 	{
2181 		if (table[i].level <= level)
2182 		{
2183 			value -= table[i].prob2;
2184 
2185 			/* Match? */
2186 			if (value <= 0L) break;
2187 		}
2188 	}
2189 
2190 	/* Result */
2191 	return ((byte)(table[i].index));
2192 }
2193 
2194 
init_ego_item(object_type * o_ptr,byte ego)2195 static void init_ego_item(object_type *o_ptr, byte ego)
2196 {
2197 	ego_item_type *e_ptr = &e_info[ego];
2198 
2199 	/* Hack -- apply extra penalties if needed */
2200 	if (cursed_p(o_ptr) || !o_ptr->cost)
2201 	{
2202 		/* Hack -- obtain bonuses */
2203 		if (e_ptr->max_to_h) o_ptr->to_h -= randint1(abs(e_ptr->max_to_h));
2204 		if (e_ptr->max_to_d) o_ptr->to_d -= randint1(abs(e_ptr->max_to_d));
2205 		if (e_ptr->max_to_a) o_ptr->to_a -= randint1(abs(e_ptr->max_to_a));
2206 
2207 		/* Hack -- obtain pval */
2208 		if (e_ptr->max_pval) o_ptr->pval -= randint1(abs(e_ptr->max_pval));
2209 	}
2210 
2211 	/* Hack -- apply extra bonuses if needed */
2212 	else
2213 	{
2214 		/* Hack -- obtain bonuses */
2215 		if (e_ptr->max_to_h > 0)
2216 			o_ptr->to_h += randint1(e_ptr->max_to_h);
2217 		else if (e_ptr->max_to_h < 0)
2218 			o_ptr->to_h -= randint1(-e_ptr->max_to_h);
2219 
2220 		if (e_ptr->max_to_d > 0)
2221 			o_ptr->to_d += randint1(e_ptr->max_to_d);
2222 		else if (e_ptr->max_to_d < 0)
2223 			o_ptr->to_d -= randint1(-e_ptr->max_to_d);
2224 
2225 		if (e_ptr->max_to_a > 0)
2226 			o_ptr->to_a += randint1(e_ptr->max_to_a);
2227 		else if (e_ptr->max_to_a < 0)
2228 			o_ptr->to_a -= randint1(-e_ptr->max_to_a);
2229 
2230 		/* Hack -- obtain pval */
2231 		if ((e_ptr->max_pval) && ((!o_ptr->pval) || k_info[o_ptr->k_idx].pval))
2232 		{
2233 			/*
2234 			 * Add the ego pval only if object has no pval, or normally
2235 			 * has a pval - in which case the bonus should be added.
2236 			 * (Eg with diggers)
2237 			 */
2238 			if (e_ptr->max_pval > 0)
2239 				o_ptr->pval += randint1(e_ptr->max_pval);
2240 			else
2241 				o_ptr->pval -= randint1(-e_ptr->max_pval);
2242 		}
2243 	}
2244 
2245 	/* Hack -- apply rating bonus */
2246 	inc_rating(e_ptr->rating);
2247 
2248 	/* Cheat -- describe the item */
2249 	if (cheat_peek) object_mention(o_ptr);
2250 }
2251 
2252 
2253 /*
2254  * Turn an item into an ego item
2255  */
add_ego_flags(object_type * o_ptr,byte ego)2256 void add_ego_flags(object_type *o_ptr, byte ego)
2257 {
2258 	int i;
2259 
2260 	ego_item_type *e_ptr = &e_info[ego];
2261 
2262 	/* Set the flags */
2263 	o_ptr->flags[0] |= e_ptr->flags[0];
2264 	o_ptr->flags[1] |= e_ptr->flags[1];
2265 	o_ptr->flags[2] |= e_ptr->flags[2];
2266 	o_ptr->flags[3] |= e_ptr->flags[3];
2267 
2268 	/* Save all the known ego flags */
2269 	o_ptr->kn_flags[0] = e_ptr->flags[0];
2270 	o_ptr->kn_flags[1] = e_ptr->flags[1];
2271 	o_ptr->kn_flags[2] = e_ptr->flags[2];
2272 	o_ptr->kn_flags[3] = e_ptr->flags[3];
2273 
2274 	/* Save the inscription */
2275 	o_ptr->xtra_name = quark_add(e_name + e_ptr->name);
2276 
2277 	/* Add any special scripts */
2278 	for (i = 0; i < MAX_TRIGGER; i++)
2279 	{
2280 		if (e_ptr->trigger[i])
2281 			o_ptr->trigger[i] = quark_add(e_text + e_ptr->trigger[i]);
2282 	}
2283 
2284 	/* Add in cost of ego item */
2285 	o_ptr->cost = k_info[o_ptr->k_idx].cost + e_ptr->cost;
2286 
2287 	/* Lose information */
2288 	o_ptr->info &= ~(OB_MENTAL | OB_KNOWN);
2289 }
2290 
2291 
2292 /*
2293  * Mega-Hack -- Attempt to create an artifact.
2294  */
make_artifact(void)2295 static object_type *make_artifact(void)
2296 {
2297 	int i;
2298 	int k_idx = 0;
2299 
2300 	object_type *o_ptr;
2301 
2302 	/* Check the artifact list */
2303 	for (i = 1; i < z_info->a_max; i++)
2304 	{
2305 		artifact_type *a_ptr = &a_info[i];
2306 
2307 		/* Skip "empty" artifacts */
2308 		if (!a_ptr->name) continue;
2309 
2310 		/* Cannot make an artifact twice */
2311 		if (a_ptr->cur_num) continue;
2312 
2313 		/* No quest items */
2314 		if (FLAG(a_ptr, TR_QUESTITEM)) continue;
2315 
2316 		/* XXX XXX Enforce minimum "depth" (loosely) */
2317 		if (a_ptr->level > p_ptr->depth)
2318 		{
2319 			/* Acquire the "out-of-depth factor" */
2320 			int d = (a_ptr->level - p_ptr->depth) * 2;
2321 
2322 			/* Roll for out-of-depth creation */
2323 			if (!one_in_(d)) continue;
2324 		}
2325 
2326 		/* Artifact "rarity roll" */
2327 		if (!one_in_(a_ptr->rarity)) continue;
2328 
2329 		/* Find the base object */
2330 		k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
2331 
2332 		/* XXX XXX Enforce minimum "object" level (loosely) */
2333 		if (!(FLAG(&k_info[k_idx], TR_INSTA_ART)) &&
2334 				k_info[k_idx].level > base_level())
2335 		{
2336 			/* Acquire the "out-of-depth factor" */
2337 			int d = (k_info[k_idx].level - base_level()) * 5;
2338 
2339 			/* Roll for out-of-depth creation */
2340 			if (!one_in_(d)) continue;
2341 		}
2342 
2343 		/* Assign the template */
2344 		o_ptr = object_prep(k_idx);
2345 
2346 		/* Save the artifact flags */
2347 		o_ptr->flags[0] |= a_ptr->flags[0];
2348 		o_ptr->flags[1] |= a_ptr->flags[1];
2349 		o_ptr->flags[2] |= a_ptr->flags[2];
2350 		o_ptr->flags[3] |= a_ptr->flags[3];
2351 
2352 		/* Set the fields */
2353 		o_ptr->pval = a_ptr->pval;
2354 		o_ptr->ac = a_ptr->ac;
2355 		o_ptr->dd = a_ptr->dd;
2356 		o_ptr->ds = a_ptr->ds;
2357 		o_ptr->to_a = a_ptr->to_a;
2358 		o_ptr->to_h = a_ptr->to_h;
2359 		o_ptr->to_d = a_ptr->to_d;
2360 		o_ptr->weight = a_ptr->weight;
2361 
2362 		/* Mega-Hack XXX XXX -- set activation */
2363 		o_ptr->a_idx = i;
2364 
2365 		/* Add any special scripts */
2366 		for (i = 0; i < MAX_TRIGGER; i++)
2367 		{
2368 			if (a_ptr->trigger[i])
2369 				o_ptr->trigger[i] = quark_add(a_text + a_ptr->trigger[i]);
2370 		}
2371 
2372 		/* Do not make another one */
2373 		a_ptr->cur_num = 1;
2374 
2375 		/* Save the inscription */
2376 		o_ptr->xtra_name = quark_add(a_name + a_ptr->name);
2377 
2378 		/* Apply special scripts */
2379 		apply_object_trigger(TRIGGER_MAKE, o_ptr, "i", "lev",
2380 				a_ptr->level);
2381 
2382 		/* Hack - increase the level rating */
2383 		inc_rating(30);
2384 
2385 		if (!a_ptr->cost)
2386 		{
2387 			/* Hack -- "worthless" artifacts */
2388 			o_ptr->cost = 0L;
2389 		}
2390 		else
2391 		{
2392 			/* Hack - use the artifact price */
2393 			o_ptr->cost = k_info[o_ptr->k_idx].cost + a_ptr->cost;
2394 		}
2395 
2396 		/* Cheat -- peek at the item */
2397 		if (cheat_peek) object_mention(o_ptr);
2398 
2399 		/* Set the good item flag */
2400 		set_special();
2401 
2402 		/* Success */
2403 		return (o_ptr);
2404 	}
2405 
2406 	/* Failure */
2407 	return (NULL);
2408 }
2409 
2410 /*
2411  * Mega-Hack -- Attempt to create a "special" randart.
2412  */
make_special_randart(void)2413 static object_type *make_special_randart(void)
2414 {
2415 	int i;
2416 	int k_idx = 0;
2417 
2418 	object_type *o_ptr;
2419 
2420 	/* Check the artifact list */
2421 	for (i = 1; i <= MAX_ART_SPECIAL; i++)
2422 	{
2423 		int lev;
2424 
2425 		artifact_type *a_ptr = &a_info[i];
2426 
2427 		/* Skip "empty" artifacts */
2428 		if (!a_ptr->name) continue;
2429 
2430 		/* Find the base object */
2431 		k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
2432 
2433 		/* Skip "forbidden" artifacts */
2434 		if (!k_info[k_idx].extra) continue;
2435 
2436 		/* Enforce minimum "object" level (loosely) */
2437 		if (k_info[k_idx].level > base_level())
2438 		{
2439 			/* Acquire the "out-of-depth factor" */
2440 			int d = (k_info[k_idx].level - base_level()) * 2;
2441 
2442 			/* Roll for out-of-depth creation */
2443 			if (!one_in_(d)) continue;
2444 		}
2445 
2446 		/* Enforce "object rarity" */
2447 		if (!one_in_(k_info[k_idx].extra)) continue;
2448 
2449 		/* Assign the template */
2450 		o_ptr = object_prep(k_idx);
2451 
2452 		/* Use the object kind's depth */
2453 		lev = k_info[k_idx].level;
2454 
2455 		/* Create using the object kind's depth */
2456 		create_artifact(o_ptr, lev, FALSE);
2457 
2458 		/* Run special scripts */
2459 		apply_object_trigger(TRIGGER_MAKE, o_ptr, "i", LUA_VAR(lev));
2460 
2461 		return (o_ptr);
2462 	}
2463 
2464 	return (NULL);
2465 }
2466 
2467 /*
2468  * Apply magic to an item known to be a "weapon"
2469  *
2470  * Hack -- note special base damage dice boosting
2471  * Hack -- note special processing for weapon/digger
2472  * Hack -- note special rating boost for dragon scale mail
2473  */
a_m_aux_1(object_type * o_ptr,int level,int lev_dif,byte flags)2474 static void a_m_aux_1(object_type *o_ptr, int level, int lev_dif, byte flags)
2475 {
2476 	int tohit1 = w_bonus(10, lev_dif);
2477 	int todam1 = w_bonus(10, lev_dif);
2478 
2479 	int tohit2 = m_bonus(10, level);
2480 	int todam2 = m_bonus(10, level);
2481 
2482 	byte ego = 0;
2483 
2484 	/* Enchant */
2485 	o_ptr->to_h += tohit1;
2486 	o_ptr->to_d += todam1;
2487 
2488 
2489 	/* Good */
2490 	if (flags & OC_FORCE_GOOD)
2491 	{
2492 		/* Enchant again */
2493 		o_ptr->to_h += tohit2;
2494 		o_ptr->to_d += todam2;
2495 	}
2496 
2497 	/* Bad */
2498 	else if (flags & OC_FORCE_BAD)
2499 	{
2500 		/* Penalize again */
2501 		o_ptr->to_h -= tohit2 * 2;
2502 		o_ptr->to_d -= todam2 * 2;
2503 	}
2504 
2505 
2506 	/* Analyze type */
2507 	switch (o_ptr->tval)
2508 	{
2509 		case TV_DIGGING:
2510 		{
2511 			/* Very good */
2512 			if (flags & OC_FORCE_GOOD)
2513 			{
2514 				/* Roll for ego item */
2515 				if (get_ego_prep(ES_DIG, TRUE))
2516 				{
2517 					ego = get_ego_num(level);
2518 				}
2519 			}
2520 
2521 			/* Very bad */
2522 			else if (flags & OC_FORCE_BAD)
2523 			{
2524 				/* Hack -- Horrible digging bonus */
2525 				o_ptr->pval = 0 - (s16b)(rand_range(2, 7));
2526 			}
2527 
2528 			break;
2529 		}
2530 
2531 
2532 		case TV_HAFTED:
2533 		case TV_POLEARM:
2534 		case TV_SWORD:
2535 		{
2536 			/* Elfblades are always special */
2537 			if (o_ptr->sval == SV_ELFBLADE)
2538 			{
2539 				char new_name[1024];
2540 
2541 				(void)create_artifact(o_ptr, level, FALSE);
2542 
2543 				/* Hack - always use name made of random syllables */
2544 				quark_remove(&o_ptr->xtra_name);
2545 				get_table_name(new_name, TRUE);
2546 				o_ptr->xtra_name = quark_add(new_name);
2547 
2548 				break;
2549 			}
2550 
2551 			/* Very Good */
2552 			else if (flags & OC_FORCE_GOOD)
2553 			{
2554 				/* Roll for a random artifact */
2555 				if (one_in_(40))
2556 				{
2557 					(void)create_artifact(o_ptr, level, FALSE);
2558 
2559 					break;
2560 				}
2561 
2562 				/* Roll for an ego-item */
2563 				if (get_ego_prep(ES_WIELD, TRUE))
2564 				{
2565 					ego = get_ego_num(level);
2566 				}
2567 
2568 				/* Only sharp weapons can have sharpness */
2569 				if (ego == EGO_SHARPNESS && o_ptr->tval != TV_SWORD)
2570 					ego = 0;
2571 
2572 				/* Only hafted weapons can have earthquakes */
2573 				if (ego == EGO_EARTHQUAKES && o_ptr->tval != TV_HAFTED)
2574 					ego = 0;
2575 
2576 				/* Hack -- Super-charge the damage dice */
2577 				if (ego)
2578 				{
2579 					if (one_in_(10L * o_ptr->dd * o_ptr->ds))
2580 					{
2581 						o_ptr->ds += (o_ptr->ds * randint1(5)) / 5;
2582 					}
2583 				}
2584 			}
2585 
2586 			/* Very cursed */
2587 			else if (flags & OC_FORCE_BAD)
2588 			{
2589 				/* Roll for an ego-item */
2590 				if (get_ego_prep(ES_WIELD, FALSE))
2591 				{
2592 					ego = get_ego_num(level);
2593 
2594 					/* Extra powers */
2595 					if (ego == EGO_MORGUL)
2596 					{
2597 						SET_FLAG(o_ptr, TR_TY_CURSE);
2598 					}
2599 				}
2600 			}
2601 
2602 			break;
2603 		}
2604 
2605 
2606 		case TV_BOW:
2607 		{
2608 			/* Very good */
2609 			if (flags & OC_FORCE_GOOD)
2610 			{
2611 				/* Roll for a random artifact */
2612 				if (one_in_(21))
2613 				{
2614 					(void)create_artifact(o_ptr, level, FALSE);
2615 
2616 					break;
2617 				}
2618 
2619 				/* Roll for ego-item */
2620 				if (get_ego_prep(ES_BOW, TRUE))
2621 				{
2622 					ego = get_ego_num(level);
2623 				}
2624 			}
2625 
2626 			break;
2627 		}
2628 
2629 
2630 		case TV_BOLT:
2631 		case TV_ARROW:
2632 		case TV_SHOT:
2633 		{
2634 			/* Very good */
2635 			if (flags & OC_FORCE_GOOD)
2636 			{
2637 				/* Roll for ego-item */
2638 				if (get_ego_prep(ES_AMMO, TRUE))
2639 				{
2640 					ego = get_ego_num(level);
2641 				}
2642 
2643 				/* Hack -- super-charge the damage dice */
2644 				if (one_in_(10L * o_ptr->dd * o_ptr->ds))
2645 				{
2646 					o_ptr->ds += (o_ptr->ds * randint1(5)) / 5;
2647 				}
2648 			}
2649 
2650 			/* Very cursed */
2651 			else if (flags & OC_FORCE_BAD)
2652 			{
2653 				/* Roll for ego-item */
2654 				if (get_ego_prep(ES_AMMO, FALSE))
2655 				{
2656 					ego = get_ego_num(level);
2657 				}
2658 			}
2659 
2660 			break;
2661 		}
2662 	}
2663 
2664 	/* Add ego item powers */
2665 	if (ego)
2666 	{
2667 		add_ego_flags(o_ptr, ego);
2668 		init_ego_item(o_ptr, ego);
2669 	}
2670 
2671 
2672 	/* Cursed some of the time (only wearable items) */
2673 	if ((randint0(100) < 15) && (flags & OC_NORMAL))
2674 	{
2675 		SET_FLAG(o_ptr, TR_CURSED);
2676 	}
2677 
2678 	/* Run any special scripts */
2679 	apply_object_trigger(TRIGGER_MAKE, o_ptr, "i", LUA_VAR(level));
2680 }
2681 
2682 
dragon_resist(object_type * o_ptr)2683 static void dragon_resist(object_type *o_ptr)
2684 {
2685 	do
2686 	{
2687 		add_ego_power(EGO_XTRA_ANY_RESIST, o_ptr);
2688 	}
2689 	while (one_in_(2));
2690 }
2691 
2692 
2693 /*
2694  * Apply magic to an item known to be "armor"
2695  *
2696  * Hack -- note special processing for crown/helm
2697  * Hack -- note special processing for robe of permanence
2698  */
a_m_aux_2(object_type * o_ptr,int level,int lev_dif,byte flags)2699 static void a_m_aux_2(object_type *o_ptr, int level, int lev_dif, byte flags)
2700 {
2701 	int toac1 = w_bonus(10, lev_dif);
2702 
2703 	int toac2 = m_bonus(10, level);
2704 
2705 	byte ego = 0;
2706 
2707 	/* Hack - some items have an increased chance of being great */
2708 	if ((((o_ptr->tval == TV_HELM) && (o_ptr->sval == SV_DRAGON_HELM)) ||
2709 		 ((o_ptr->tval == TV_SHIELD) && (o_ptr->sval == SV_DRAGON_SHIELD)) ||
2710 		 ((o_ptr->tval == TV_CLOAK) && (o_ptr->sval == SV_ELVEN_CLOAK))) &&
2711 		lev_dif > 30)
2712 	{
2713 		/* Not cursed */
2714 		flags |= OC_FORCE_GOOD;
2715 		flags &= ~OC_FORCE_BAD;
2716 	}
2717 
2718 
2719 	/* Enchant */
2720 	o_ptr->to_a += toac1;
2721 
2722 	/* Good */
2723 	if (flags & OC_FORCE_GOOD)
2724 	{
2725 		/* Enchant again */
2726 		o_ptr->to_a += toac2;
2727 	}
2728 
2729 	/* Bad */
2730 	else if (flags & OC_FORCE_BAD)
2731 	{
2732 		/* Penalize again */
2733 		o_ptr->to_a -= toac2 * 2;
2734 	}
2735 
2736 	/* Analyze type */
2737 	switch (o_ptr->tval)
2738 	{
2739 		case TV_DRAG_ARMOR:
2740 		{
2741 			/* Rating boost */
2742 			inc_rating(30);
2743 
2744 			/* Mention the item */
2745 			if (cheat_peek) object_mention(o_ptr);
2746 
2747 			break;
2748 		}
2749 
2750 		case TV_HARD_ARMOR:
2751 		case TV_SOFT_ARMOR:
2752 		{
2753 			/* Very good */
2754 			if (flags & OC_FORCE_GOOD)
2755 			{
2756 				/* Roll for a random artifact */
2757 				if (one_in_(21))
2758 				{
2759 					(void)create_artifact(o_ptr, level, FALSE);
2760 
2761 					break;
2762 				}
2763 
2764 				/* Roll for an ego item */
2765 				if (get_ego_prep(ES_BODY, TRUE))
2766 				{
2767 					ego = get_ego_num(level);
2768 				}
2769 
2770 				/* Only robes can have permanence */
2771 				if (ego == EGO_PERMANENCE && (o_ptr->tval != TV_SOFT_ARMOR ||
2772 							o_ptr->sval != SV_ROBE))
2773 					ego = 0;
2774 			}
2775 
2776 			break;
2777 		}
2778 
2779 		case TV_SHIELD:
2780 		{
2781 
2782 			/* Dragon shields are already good */
2783 			if (o_ptr->sval == SV_DRAGON_SHIELD)
2784 			{
2785 				/* Rating boost */
2786 				inc_rating(5);
2787 
2788 				/* Mention the item */
2789 				if (cheat_peek) object_mention(o_ptr);
2790 				dragon_resist(o_ptr);
2791 			}
2792 			else
2793 			{
2794 				/* Very good */
2795 				if (flags & OC_FORCE_GOOD)
2796 				{
2797 					/* Roll for random artifact */
2798 					if (one_in_(21))
2799 					{
2800 						(void)create_artifact(o_ptr, level, FALSE);
2801 
2802 						break;
2803 					}
2804 
2805 					/* Roll for ego-item */
2806 					if (get_ego_prep(ES_ARM, TRUE))
2807 					{
2808 						ego = get_ego_num(level);
2809 					}
2810 				}
2811 			}
2812 			break;
2813 		}
2814 
2815 		case TV_GLOVES:
2816 		{
2817 			/* Very good */
2818 			if (flags & OC_FORCE_GOOD)
2819 			{
2820 				/* Roll for a random artifact */
2821 				if (one_in_(20))
2822 				{
2823 					(void)create_artifact(o_ptr, level, FALSE);
2824 
2825 					break;
2826 				}
2827 
2828 				/* Roll for ego-item */
2829 				if (get_ego_prep(ES_HANDS, TRUE))
2830 				{
2831 					ego = get_ego_num(level);
2832 				}
2833 			}
2834 
2835 			/* Very cursed */
2836 			else if (flags & OC_FORCE_BAD)
2837 			{
2838 				/* Roll for ego-item */
2839 				if (get_ego_prep(ES_HANDS, FALSE))
2840 				{
2841 					ego = get_ego_num(level);
2842 				}
2843 			}
2844 
2845 			break;
2846 		}
2847 
2848 		case TV_BOOTS:
2849 		{
2850 			/* Very good */
2851 			if (flags & OC_FORCE_GOOD)
2852 			{
2853 				/* Roll for a random artifact */
2854 				if (one_in_(20))
2855 				{
2856 					(void)create_artifact(o_ptr, level, FALSE);
2857 
2858 					break;
2859 				}
2860 
2861 				/* Roll for ego-item */
2862 				if (get_ego_prep(ES_FEET, TRUE))
2863 				{
2864 					ego = get_ego_num(level);
2865 				}
2866 			}
2867 
2868 			/* Very cursed */
2869 			else if (flags & OC_FORCE_BAD)
2870 			{
2871 				/* Roll for ego-item */
2872 				if (get_ego_prep(ES_FEET, FALSE))
2873 				{
2874 					ego = get_ego_num(level);
2875 				}
2876 			}
2877 
2878 			break;
2879 		}
2880 
2881 		case TV_CROWN:
2882 		{
2883 			/* Very good */
2884 			if (flags & OC_FORCE_GOOD)
2885 			{
2886 				/* Roll for a random artifact */
2887 				if (one_in_(20))
2888 				{
2889 					(void)create_artifact(o_ptr, level, FALSE);
2890 
2891 					break;
2892 				}
2893 
2894 				/* Roll for ego-item */
2895 				if (get_ego_prep(ES_CROWN, TRUE))
2896 				{
2897 					ego = get_ego_num(level);
2898 				}
2899 			}
2900 
2901 			/* Very cursed */
2902 			else if (flags & OC_FORCE_BAD)
2903 			{
2904 				/* Roll for ego-item */
2905 				if (get_ego_prep(ES_HEAD, FALSE))
2906 				{
2907 					ego = get_ego_num(level);
2908 				}
2909 			}
2910 
2911 			break;
2912 		}
2913 
2914 		case TV_HELM:
2915 		{
2916 			if (o_ptr->sval == SV_DRAGON_HELM)
2917 			{
2918 				/* Rating boost */
2919 				inc_rating(5);
2920 
2921 				/* Mention the item */
2922 				if (cheat_peek) object_mention(o_ptr);
2923 				dragon_resist(o_ptr);
2924 			}
2925 			else
2926 			{
2927 				/* Very good */
2928 				if (flags & OC_FORCE_GOOD)
2929 				{
2930 					/* Roll for a random artifacts */
2931 					if (one_in_(20))
2932 					{
2933 						(void)create_artifact(o_ptr, level, FALSE);
2934 
2935 						break;
2936 					}
2937 
2938 					/* Roll for ego-item */
2939 					if (get_ego_prep(ES_HEAD, TRUE))
2940 					{
2941 						ego = get_ego_num(level);
2942 					}
2943 				}
2944 
2945 				/* Very cursed */
2946 				else if (flags & OC_FORCE_BAD)
2947 				{
2948 					/* Roll for ego-item */
2949 					if (get_ego_prep(ES_HEAD, FALSE))
2950 					{
2951 						ego = get_ego_num(level);
2952 					}
2953 				}
2954 			}
2955 			break;
2956 		}
2957 
2958 		case TV_CLOAK:
2959 		{
2960 			if (o_ptr->sval == SV_ELVEN_CLOAK)
2961 			{
2962 				/* No cursed elven cloaks */
2963 				o_ptr->pval = randint1(4);
2964 			}
2965 
2966 			/* Very good */
2967 			if (flags & OC_FORCE_GOOD)
2968 			{
2969 				/* Roll for a random artifact */
2970 				if (one_in_(20))
2971 				{
2972 					(void)create_artifact(o_ptr, level, FALSE);
2973 
2974 					break;
2975 				}
2976 
2977 				/* Roll for ego-item */
2978 				if (get_ego_prep(ES_OUTER, TRUE))
2979 				{
2980 					ego = get_ego_num(level);
2981 				}
2982 			}
2983 
2984 			/* Very cursed */
2985 			else if (flags & OC_FORCE_BAD)
2986 			{
2987 
2988 				/* Roll for ego-item */
2989 				if (get_ego_prep(ES_OUTER, TRUE))
2990 				{
2991 					ego = get_ego_num(level);
2992 				}
2993 			}
2994 
2995 			break;
2996 		}
2997 	}
2998 
2999 	/* Add ego item powers */
3000 	if (ego)
3001 	{
3002 		add_ego_flags(o_ptr, ego);
3003 		init_ego_item(o_ptr, ego);
3004 	}
3005 
3006 	/* Cursed some of the time */
3007 	if ((randint0(100) < 15) && (flags & OC_NORMAL))
3008 	{
3009 		SET_FLAG(o_ptr, TR_CURSED);
3010 	}
3011 
3012 	/* Run any special scripts */
3013 	apply_object_trigger(TRIGGER_MAKE, o_ptr, "i", LUA_VAR(level));
3014 }
3015 
3016 
3017 /*
3018  * Apply magic to an item known to be a "ring" or "amulet"
3019  */
a_m_aux_3(object_type * o_ptr,int level,byte flags)3020 static void a_m_aux_3(object_type *o_ptr, int level, byte flags)
3021 {
3022 	bool allow_curse = FALSE;
3023 	int rating_boost = 0;
3024 
3025 	if (!(flags & OC_FORCE_GOOD) && one_in_(3))
3026 	{
3027 		/* Sometimes, the stuff can be bad */
3028 
3029 		flags |= OC_FORCE_BAD;
3030 	}
3031 
3032 	/* Apply magic according to type */
3033 	apply_object_trigger(TRIGGER_MAKE, o_ptr, "i:bi", LUA_VAR(level),
3034 			LUA_RETURN(allow_curse), LUA_RETURN(rating_boost));
3035 
3036 	/* Cursed? */
3037 	if (allow_curse && (flags & OC_FORCE_BAD))
3038 	{
3039 		/* Broken */
3040 		o_ptr->cost = 0;
3041 
3042 		/* Cursed */
3043 		SET_FLAG(o_ptr, TR_CURSED);
3044 
3045 		/* Reverse bonuses */
3046 		o_ptr->pval = 0 - o_ptr->pval;
3047 		o_ptr->to_a = 0 - o_ptr->to_a;
3048 		o_ptr->to_h = 0 - o_ptr->to_h;
3049 		o_ptr->to_d = 0 - o_ptr->to_d;
3050 	}
3051 	else if (rating_boost)
3052 	{
3053 		/* Boost the rating */
3054 		inc_rating(rating_boost);
3055 
3056 		/* Mention the item */
3057 		if (cheat_peek && rating_boost >= 10) object_mention(o_ptr);
3058 	}
3059 }
3060 
3061 
3062 /*
3063  * Apply magic to an item known to be "boring"
3064  *
3065  * Hack -- note the special code for various items
3066  */
a_m_aux_4(object_type * o_ptr,int level,byte flags)3067 static void a_m_aux_4(object_type *o_ptr, int level, byte flags)
3068 {
3069 	object_kind *k_ptr = &k_info[o_ptr->k_idx];
3070 
3071 	byte ego = 0;
3072 
3073 	/* Apply magic (good or bad) according to type */
3074 	switch (o_ptr->tval)
3075 	{
3076 		case TV_LITE:
3077 		{
3078 			/* Hack -- Torches -- random fuel */
3079 			if (o_ptr->sval == SV_LITE_TORCH)
3080 			{
3081 				if (o_ptr->pval > 0) o_ptr->timeout = randint1(o_ptr->pval);
3082 			}
3083 
3084 			/* Hack -- Lanterns -- random fuel */
3085 			if (o_ptr->sval == SV_LITE_LANTERN)
3086 			{
3087 				if (o_ptr->pval > 0) o_ptr->timeout = randint1(o_ptr->pval);
3088 			}
3089 
3090 			/* Hack - remove pval */
3091 			o_ptr->pval = 0;
3092 
3093 			if (flags & OC_FORCE_GOOD)
3094 			{
3095 				/* Roll for a random ego */
3096 				if (get_ego_prep(ES_LITE, TRUE))
3097 				{
3098 					ego = get_ego_num(level);
3099 
3100 					/* Initialise the ego item, if one is picked */
3101 					if (ego)
3102 					{
3103 						add_ego_flags(o_ptr, ego);
3104 						init_ego_item(o_ptr, ego);
3105 					}
3106 				}
3107 			}
3108 
3109 
3110 			break;
3111 		}
3112 
3113 		case TV_WAND:
3114 		{
3115 			/*
3116 			 * The wand or staff gets a number of initial charges equal
3117 			 * to between 1/2 (+1) and the full object kind's pval. -LM-
3118 			 */
3119 			o_ptr->pval = k_ptr->pval / 2 + randint1((k_ptr->pval + 1) / 2);
3120 
3121 			/* The number of "used" charges starts out as zero */
3122 			o_ptr->ac = 0;
3123 			break;
3124 		}
3125 
3126 		case TV_STAFF:
3127 		{
3128 			/*
3129 			 * The wand or staff gets a number of initial charges equal
3130 			 * to between 1/2 (+1) and the full object kind's pval. -LM-
3131 			 */
3132 			o_ptr->pval = k_ptr->pval / 2 + randint1((k_ptr->pval + 1) / 2);
3133 			break;
3134 		}
3135 
3136 		case TV_ROD:
3137 		{
3138 			/* Transfer the pval. -LM- */
3139 			o_ptr->pval = k_ptr->pval;
3140 			break;
3141 		}
3142 
3143 		case TV_FIGURINE:
3144 		{
3145 			int i = 1;
3146 
3147 			monster_race *r_ptr;
3148 
3149 			/* Pick a random non-unique monster race */
3150 			while (1)
3151 			{
3152 				i = randint1(z_info->r_max - 1);
3153 
3154 				r_ptr = &r_info[i];
3155 
3156 				/* Prefer less out-of-depth monsters */
3157 				if ((level < r_ptr->level) &&
3158 					!one_in_(r_ptr->level - level)) continue;
3159 
3160 				/* Ignore dead monsters */
3161 				if (!r_ptr->rarity) continue;
3162 
3163 				/* No uniques */
3164 				if (FLAG(r_ptr, RF_UNIQUE)) continue;
3165 
3166 				break;
3167 			}
3168 
3169 			o_ptr->pval = i;
3170 
3171 			if (cheat_peek)
3172 			{
3173 				msgf("Figurine of %s", mon_race_name(r_ptr));
3174 			}
3175 
3176 			break;
3177 		}
3178 
3179 		case TV_STATUE:
3180 		{
3181 			int i = 1;
3182 
3183 			monster_race *r_ptr;
3184 
3185 			/* Pick a random monster race */
3186 			while (1)
3187 			{
3188 				i = randint1(z_info->r_max - 1);
3189 
3190 				r_ptr = &r_info[i];
3191 
3192 				/* Ignore dead monsters */
3193 				if (!r_ptr->rarity) continue;
3194 
3195 				break;
3196 			}
3197 
3198 			o_ptr->pval = i;
3199 
3200 			if (cheat_peek)
3201 			{
3202 				msgf("Statue of %s", mon_race_name(r_ptr));
3203 			}
3204 
3205 			break;
3206 		}
3207 
3208 		case TV_CHEST:
3209 		{
3210 			byte obj_level = get_object_level(o_ptr);
3211 
3212 			/* Hack -- skip ruined chests */
3213 			if (obj_level <= 0) break;
3214 
3215 			/* Hack -- pick a "difficulty" */
3216 			o_ptr->pval = randint1(obj_level);
3217 
3218 			/* Never exceed "difficulty" of 55 to 59 */
3219 			if (o_ptr->pval > 55) o_ptr->pval = (byte)rand_range(55, 60);
3220 
3221 			break;
3222 		}
3223 	}
3224 
3225 	/* Run any special scripts */
3226 	apply_object_trigger(TRIGGER_MAKE, o_ptr, "i", LUA_VAR(level));
3227 }
3228 
3229 
add_ego_power(int power,object_type * o_ptr)3230 void add_ego_power(int power, object_type *o_ptr)
3231 {
3232 	if (power == EGO_XTRA_ANY_RESIST)
3233 	{
3234 		if (one_in_(4))
3235 			power = EGO_XTRA_LO_RESIST;
3236 		else
3237 			power = EGO_XTRA_HI_RESIST;
3238 	}
3239 
3240 	if (power == EGO_XTRA_POWER)
3241 	{
3242 		if (one_in_(2))
3243 			power = EGO_XTRA_ABILITY;
3244 		else
3245 			power = EGO_XTRA_HI_RESIST;
3246 	}
3247 
3248 	switch (power)
3249 	{
3250 		case EGO_XTRA_ABILITY:
3251 		{
3252 			/* Choose an ability */
3253 			switch (randint0(8))
3254 			{
3255 				case 0:
3256 				{
3257 					(o_ptr->flags[2]) |= (TR2_FEATHER);
3258 					break;
3259 				}
3260 				case 1:
3261 				{
3262 					(o_ptr->flags[2]) |= (TR2_LITE);
3263 					break;
3264 				}
3265 				case 2:
3266 				{
3267 					(o_ptr->flags[2]) |= (TR2_SEE_INVIS);
3268 					break;
3269 				}
3270 				case 3:
3271 				{
3272 					(o_ptr->flags[2]) |= (TR2_TELEPATHY);
3273 					break;
3274 				}
3275 				case 4:
3276 				{
3277 					(o_ptr->flags[2]) |= (TR2_SLOW_DIGEST);
3278 					break;
3279 				}
3280 				case 5:
3281 				{
3282 					(o_ptr->flags[2]) |= (TR2_REGEN);
3283 					break;
3284 				}
3285 				case 6:
3286 				{
3287 					(o_ptr->flags[1]) |= (TR1_FREE_ACT);
3288 					break;
3289 				}
3290 				case 7:
3291 				{
3292 					(o_ptr->flags[1]) |= (TR1_HOLD_LIFE);
3293 					break;
3294 				}
3295 			}
3296 
3297 			break;
3298 		}
3299 		case EGO_XTRA_SUSTAIN:
3300 		{
3301 			/* Choose a sustain */
3302 			switch (randint0(6))
3303 			{
3304 				case 0:
3305 				{
3306 					(o_ptr->flags[1]) |= (TR1_SUST_STR);
3307 					break;
3308 				}
3309 				case 1:
3310 				{
3311 					(o_ptr->flags[1]) |= (TR1_SUST_INT);
3312 					break;
3313 				}
3314 				case 2:
3315 				{
3316 					(o_ptr->flags[1]) |= (TR1_SUST_WIS);
3317 					break;
3318 				}
3319 				case 3:
3320 				{
3321 					(o_ptr->flags[1]) |= (TR1_SUST_DEX);
3322 					break;
3323 				}
3324 				case 4:
3325 				{
3326 					(o_ptr->flags[1]) |= (TR1_SUST_CON);
3327 					break;
3328 				}
3329 				case 5:
3330 				{
3331 					(o_ptr->flags[1]) |= (TR1_SUST_CHR);
3332 					break;
3333 				}
3334 			}
3335 
3336 			break;
3337 		}
3338 
3339 		case EGO_XTRA_LO_RESIST:
3340 		{
3341 			/* Choose a low resistance */
3342 			switch (randint0(4))
3343 			{
3344 				case 0:
3345 				{
3346 					(o_ptr->flags[1]) |= (TR1_RES_ACID);
3347 					break;
3348 				}
3349 				case 1:
3350 				{
3351 					(o_ptr->flags[1]) |= (TR1_RES_ELEC);
3352 					break;
3353 				}
3354 				case 2:
3355 				{
3356 					(o_ptr->flags[1]) |= (TR1_RES_FIRE);
3357 					break;
3358 				}
3359 				case 3:
3360 				{
3361 					(o_ptr->flags[1]) |= (TR1_RES_COLD);
3362 					break;
3363 				}
3364 			}
3365 
3366 			break;
3367 		}
3368 
3369 		case EGO_XTRA_HI_RESIST:
3370 		{
3371 			/* Choose a power */
3372 			switch (randint0(12))
3373 			{
3374 				case 0:
3375 				{
3376 					(o_ptr->flags[1]) |= (TR1_RES_BLIND);
3377 					break;
3378 				}
3379 				case 1:
3380 				{
3381 					(o_ptr->flags[1]) |= (TR1_RES_CONF);
3382 					break;
3383 				}
3384 				case 2:
3385 				{
3386 					(o_ptr->flags[1]) |= (TR1_RES_SOUND);
3387 					break;
3388 				}
3389 				case 3:
3390 				{
3391 					(o_ptr->flags[1]) |= (TR1_RES_SHARDS);
3392 					break;
3393 				}
3394 				case 4:
3395 				{
3396 					(o_ptr->flags[1]) |= (TR1_RES_NETHER);
3397 					break;
3398 				}
3399 				case 5:
3400 				{
3401 					(o_ptr->flags[1]) |= (TR1_RES_NEXUS);
3402 					break;
3403 				}
3404 				case 6:
3405 				{
3406 					(o_ptr->flags[1]) |= (TR1_RES_CHAOS);
3407 					break;
3408 				}
3409 				case 7:
3410 				{
3411 					(o_ptr->flags[1]) |= (TR1_RES_DISEN);
3412 					break;
3413 				}
3414 				case 8:
3415 				{
3416 					(o_ptr->flags[1]) |= (TR1_RES_POIS);
3417 					break;
3418 				}
3419 				case 9:
3420 				{
3421 					(o_ptr->flags[1]) |= (TR1_RES_DARK);
3422 					break;
3423 				}
3424 				case 10:
3425 				{
3426 					(o_ptr->flags[1]) |= (TR1_RES_LITE);
3427 					break;
3428 				}
3429 				case 11:
3430 				{
3431 					(o_ptr->flags[1]) |= (TR1_RES_FEAR);
3432 					break;
3433 				}
3434 			}
3435 
3436 			break;
3437 		}
3438 	}
3439 }
3440 
3441 
3442 /*
3443  * Complete the "creation" of an object by applying "magic" to the item
3444  *
3445  * This includes not only rolling for random bonuses, but also putting the
3446  * finishing touches on ego-items and artifacts, giving charges to wands and
3447  * staffs, giving fuel to lites, and placing traps on chests.
3448  *
3449  * The base "chance" of the item being "good" increases with the "level"
3450  * parameter, which is usually derived from the dungeon level, being equal
3451  * to the level plus 10, up to a maximum of 75.  If "good" is true, then
3452  * the object is guaranteed to be "good".  If an object is "good", then
3453  * the chance that the object will be "great" (ego-item or artifact), also
3454  * increases with the "level", being equal to half the level, plus 5, up to
3455  * a maximum of 20.  If "great" is true, then the object is guaranteed to be
3456  * "great".  At dungeon level 65 and below, 15/100 objects are "great".
3457  *
3458  * If the object is not "good", there is a chance it will be "cursed", and
3459  * if it is "cursed", there is a chance it will be "broken".  These chances
3460  * are related to the "good" / "great" chances above.
3461  *
3462  * Otherwise "normal" rings and amulets will be "good" half the time and
3463  * "cursed" half the time, unless the ring/amulet is always good or cursed.
3464  *
3465  * If "okay" is true, and the object is going to be "great", then there is
3466  * a chance that an artifact will be created.  This is true even if both the
3467  * "good" and "great" arguments are false.  As a total hack, if "great" is
3468  * true, then the item gets 3 extra "attempts" to become an artifact.
3469  */
apply_magic(object_type * o_ptr,int lev,int lev_dif,byte flags)3470 void apply_magic(object_type *o_ptr, int lev, int lev_dif, byte flags)
3471 {
3472 	int f;
3473 
3474 	/* Maximum "level" for various things */
3475 	if (lev > MAX_DEPTH - 1) lev = MAX_DEPTH - 1;
3476 
3477 	/* Base chance of being "good" */
3478 	f = (lev * 3) / 5 + 10;
3479 
3480 	/* Maximal chance of being "good" */
3481 	if (f > 42) f = 42;
3482 
3483 	if (FLAG(p_ptr, TR_STRANGE_LUCK))
3484 		f = f * 3 / 2;
3485 
3486 	/* Lights are more likely to be ego-items */
3487 	if (o_ptr->tval == TV_LITE) f *= 2;
3488 
3489 	/* Roll for ego items */
3490 	if ((flags & OC_NORMAL) && (randint0(100) < f))
3491 	{
3492 		if (randint0(100) < f) flags |= OC_FORCE_GOOD;
3493 		else if (randint0(100) < f) flags |= OC_FORCE_BAD;
3494 	}
3495 
3496 	if (FLAG(o_ptr, TR_INSTA_ART))
3497 	{
3498 		/* Paranoia - we have an artifact!!! */
3499 		msgf("Error Condition - artifact passed to apply_magic");
3500 		msgf("Object sval:%d Object flags3:%d",
3501 				   o_ptr->sval, o_ptr->flags[2]);
3502 		msgf("Submit a bugreport please. :-)");
3503 		return;
3504 	}
3505 
3506 	/* Apply magic */
3507 	switch (o_ptr->tval)
3508 	{
3509 		case TV_DIGGING:
3510 		case TV_HAFTED:
3511 		case TV_POLEARM:
3512 		case TV_SWORD:
3513 		case TV_BOW:
3514 		case TV_SHOT:
3515 		case TV_ARROW:
3516 		case TV_BOLT:
3517 		{
3518 			a_m_aux_1(o_ptr, lev, lev_dif, flags);
3519 			break;
3520 		}
3521 
3522 		case TV_DRAG_ARMOR:
3523 		case TV_HARD_ARMOR:
3524 		case TV_SOFT_ARMOR:
3525 		case TV_SHIELD:
3526 		case TV_HELM:
3527 		case TV_CROWN:
3528 		case TV_CLOAK:
3529 		case TV_GLOVES:
3530 		case TV_BOOTS:
3531 		{
3532 			a_m_aux_2(o_ptr, lev, lev_dif, flags);
3533 			break;
3534 		}
3535 
3536 		case TV_RING:
3537 		case TV_AMULET:
3538 		{
3539 			a_m_aux_3(o_ptr, lev, flags);
3540 			break;
3541 		}
3542 
3543 		default:
3544 		{
3545 			a_m_aux_4(o_ptr, lev, flags);
3546 			break;
3547 		}
3548 	}
3549 
3550 	/* Change level feeling for random artifacts */
3551 	if (FLAG(o_ptr, TR_INSTA_ART)) inc_rating(30);
3552 
3553 	/* Examine real objects */
3554 	if (o_ptr->k_idx)
3555 	{
3556 		object_kind *k_ptr = &k_info[o_ptr->k_idx];
3557 
3558 		/* Mega Hack - reset cost if not a powerful item */
3559 		if (!o_ptr->xtra_name)
3560 		{
3561 			o_ptr->cost = k_ptr->cost;
3562 		}
3563 	}
3564 }
3565 
3566 /* The tval / sval pair to match */
3567 static byte match_tv;
3568 static byte match_sv;
3569 
3570 
init_match_hook(byte tval,byte sval)3571 void init_match_hook(byte tval, byte sval)
3572 {
3573 	/* Save the tval/ sval pair to match */
3574 	match_tv = tval;
3575 	match_sv = sval;
3576 }
3577 
3578 
3579 /*
3580  * Hack -- match certain types of object only.
3581  *
3582  * Return 0% or 100% of matching based on tval and sval.
3583  */
kind_is_match(int k_idx)3584 byte kind_is_match(int k_idx)
3585 {
3586 	object_kind *k_ptr = &k_info[k_idx];
3587 
3588 	/* Does the tval match? */
3589 	if ((match_tv != TV_ANY) && (k_ptr->tval != match_tv)) return (0);
3590 
3591 	/* Does the sval match? */
3592 	if ((match_sv == SV_ANY) || (k_ptr->sval == match_sv)) return (100);
3593 
3594 	/* Not a match */
3595 	return (0);
3596 }
3597 
3598 
3599 /* The themed objects to use */
3600 static obj_theme match_theme;
3601 
init_match_theme(obj_theme theme)3602 void init_match_theme(obj_theme theme)
3603 {
3604 	/* Save the theme */
3605 	match_theme = theme;
3606 }
3607 
3608 /*
3609  * Hack -- match certain types of object only.
3610  *
3611  * Return percentage probability of match.
3612  */
kind_is_theme(int k_idx)3613 byte kind_is_theme(int k_idx)
3614 {
3615 	object_kind *k_ptr = &k_info[k_idx];
3616 
3617 	/* Pick probability to use */
3618 	switch (k_ptr->tval)
3619 	{
3620 		case TV_SKELETON:
3621 		case TV_BOTTLE:
3622 		case TV_JUNK:
3623 		{
3624 			/* Degree of junk is defined in terms of the other 4 quantities */
3625 			return (100 - (match_theme.treasure + match_theme.combat +
3626 						   match_theme.magic + match_theme.tools));
3627 		}
3628 		case TV_SPIKE: return (match_theme.tools);
3629 		case TV_CHEST: return (match_theme.treasure);
3630 		case TV_FIGURINE: return (match_theme.treasure);
3631 		case TV_STATUE: return (match_theme.treasure);
3632 		case TV_SHOT: return (match_theme.combat);
3633 		case TV_ARROW: return (match_theme.combat);
3634 		case TV_BOLT: return (match_theme.combat);
3635 		case TV_BOW: return (match_theme.combat);
3636 		case TV_DIGGING: return (match_theme.tools);
3637 		case TV_HAFTED: return (match_theme.combat);
3638 		case TV_POLEARM: return (match_theme.combat);
3639 		case TV_SWORD: return (match_theme.combat);
3640 		case TV_BOOTS: return (match_theme.combat);
3641 		case TV_GLOVES: return (match_theme.combat);
3642 		case TV_HELM: return (match_theme.combat);
3643 		case TV_CROWN: return (match_theme.treasure);
3644 		case TV_SHIELD: return (match_theme.combat);
3645 		case TV_CLOAK: return (match_theme.combat);
3646 		case TV_SOFT_ARMOR: return (match_theme.combat);
3647 		case TV_HARD_ARMOR: return (match_theme.combat);
3648 		case TV_DRAG_ARMOR: return (match_theme.treasure +
3649 					 match_theme.combat);
3650 		case TV_LITE: return (match_theme.tools);
3651 		case TV_AMULET: return (match_theme.treasure);
3652 		case TV_RING: return (match_theme.treasure);
3653 		case TV_STAFF: return (match_theme.magic);
3654 		case TV_WAND: return (match_theme.magic);
3655 		case TV_ROD: return (match_theme.magic);
3656 		case TV_SCROLL: return (match_theme.magic);
3657 		case TV_POTION: return (match_theme.magic);
3658 		case TV_FLASK: return (match_theme.tools);
3659 		case TV_FOOD: return (match_theme.tools);
3660 		case TV_LIFE_BOOK: return (match_theme.magic);
3661 		case TV_SORCERY_BOOK: return (match_theme.magic);
3662 		case TV_NATURE_BOOK: return (match_theme.magic);
3663 		case TV_CHAOS_BOOK: return (match_theme.magic);
3664 		case TV_DEATH_BOOK: return (match_theme.magic);
3665 		case TV_TRUMP_BOOK: return (match_theme.magic);
3666 		case TV_ARCANE_BOOK: return (match_theme.magic);
3667 
3668 			/* Paranoia */
3669 		default: return (0);
3670 	}
3671 }
3672 
3673 
3674 /*
3675  * Attempt to make an object (normal or good/great)
3676  *
3677  * This routine plays nasty games to generate the "special artifacts".
3678  *
3679  * This routine uses "level" for the "generation level".
3680  *
3681  * We assume that the given object has been "wiped".
3682  */
make_object(int level,int delta_level,obj_theme * theme)3683 object_type *make_object(int level, int delta_level, obj_theme *theme)
3684 {
3685 	int prob, base, min_level;
3686 	byte obj_level;
3687 	byte flags;
3688 	int k_idx = 0, count = 5;
3689 
3690 	object_type *o_ptr;
3691 
3692 	/* Chance of "special object" */
3693 	if (delta_level > 0)
3694 	{
3695 		prob = 800 / delta_level;
3696 
3697 		/* bounds checking */
3698 		if (prob < 10) prob = 10;
3699 	}
3700 	else
3701 	{
3702 		/* No divide by zero */
3703 		prob = 800;
3704 	}
3705 
3706 	/* "Good Luck" mutation */
3707 	if ((p_ptr->muta3 & MUT3_GOOD_LUCK) && one_in_(13))
3708 	{
3709 		/* The player is lucky - the item is better than normal */
3710 		delta_level += 20;
3711 	}
3712 
3713 	/* Renormalise the change in level */
3714 	delta_level = randint0(delta_level);
3715 
3716 	/* Base level for the object */
3717 	base = level + delta_level;
3718 
3719 	/* Paranoia - don't let this get too high */
3720 	if (base > 100) base = 100;
3721 
3722 	/* Hack - Set flags based on delta_level */
3723 	if (delta_level > 15)
3724 	{
3725 		flags = OC_FORCE_GOOD;
3726 
3727 		min_level = level + delta_level / 2;
3728 	}
3729 	else
3730 	{
3731 		flags = OC_NORMAL;
3732 		min_level = 0;
3733 	}
3734 
3735 	/* Make an artifact */
3736 	if (one_in_(prob))
3737 	{
3738 		/* Try for a fixed art */
3739 		o_ptr = make_artifact();
3740 
3741 		if (o_ptr) return (o_ptr);
3742 	}
3743 
3744 	if (one_in_(prob * 2))
3745 	{
3746 		/* Try for a special randart */
3747 		o_ptr = make_special_randart();
3748 
3749 		if (o_ptr) return (o_ptr);
3750 	}
3751 
3752 	/* Default to themed objects? */
3753 	if (theme)
3754 	{
3755 		while (!k_idx && (count > 0))
3756 		{
3757 			/* No infinite loops */
3758 			count--;
3759 
3760 			/* Select items based on "theme" */
3761 			init_match_theme(*theme);
3762 
3763 			/* Prepare allocation table */
3764 			get_obj_num_prep(kind_is_theme);
3765 
3766 			/* Pick a random object */
3767 			k_idx = get_obj_num(base, min_level);
3768 
3769 			/* Paranoia - try less hard to get something */
3770 			if (!k_idx) min_level /= 2;
3771 		}
3772 	}
3773 	else
3774 	{
3775 		/* We already have a restriction */
3776 
3777 		/* Pick a random object using the current restriction */
3778 		k_idx = get_obj_num(base, 0);
3779 	}
3780 
3781 	/* Handle failure */
3782 	if (!k_idx) return (NULL);
3783 
3784 	/* Prepare the object */
3785 	o_ptr = object_prep(k_idx);
3786 
3787 	/* Apply magic (allow artifacts) */
3788 	apply_magic(o_ptr, base, base - k_info[k_idx].level, flags);
3789 
3790 	/* Hack -- generate multiple spikes/missiles/ mushrooms */
3791 	switch (o_ptr->tval)
3792 	{
3793 		case TV_SPIKE:
3794 		case TV_SHOT:
3795 		case TV_ARROW:
3796 		case TV_BOLT:
3797 		{
3798 			o_ptr->number = (byte)damroll(6, 7);
3799 			break;
3800 		}
3801 
3802 		case TV_FOOD:
3803 		{
3804 			if (o_ptr->sval < SV_FOOD_BISCUIT)
3805 			{
3806 				/* mushrooms appear in clumps */
3807 				o_ptr->number = (byte)randint1(6);
3808 			}
3809 			break;
3810 		}
3811 	}
3812 
3813 	obj_level = get_object_level(o_ptr);
3814 
3815 	/* Notice "okay" out-of-depth objects */
3816 	if (!cursed_p(o_ptr) && o_ptr->cost && (obj_level > p_ptr->depth))
3817 	{
3818 		/* Rating increase */
3819 		inc_rating(obj_level - p_ptr->depth);
3820 
3821 		/* Cheat -- peek at items */
3822 		if (cheat_peek) object_mention(o_ptr);
3823 	}
3824 
3825 	/* Success */
3826 	return (o_ptr);
3827 }
3828 
3829 
3830 /*
3831  * Put an object on the ground.
3832  * We assume the grid is in bounds.
3833  */
put_object(object_type * o_ptr,int x,int y)3834 static bool put_object(object_type *o_ptr, int x, int y)
3835 {
3836 	/* Acquire grid */
3837 	cave_type *c_ptr = area(x, y);
3838 
3839 	object_type *j_ptr;
3840 
3841 	/* Require nice floor space */
3842 	if (!cave_nice_grid(c_ptr)) return (FALSE);
3843 
3844 	/* Paranoia */
3845 	if (!o_ptr) return (FALSE);
3846 
3847 	/* Add the object to the ground */
3848 	j_ptr = add_object_list(&c_ptr->o_idx, o_ptr);
3849 
3850 	/* Success */
3851 	if (j_ptr)
3852 	{
3853 		/* Location */
3854 		j_ptr->iy = y;
3855 		j_ptr->ix = x;
3856 
3857 		/* Region */
3858 		j_ptr->region = cur_region;
3859 
3860 		/* Notice + Redraw */
3861 		note_spot(x, y);
3862 
3863 		/* Debug - scan player list for this item and complain if we find it */
3864 		look_up_list(j_ptr);
3865 
3866 		return (TRUE);
3867 	}
3868 
3869 	/* Warn the player */
3870 	msgf("Failed to place object!");
3871 
3872 	/* Paranoia - preserve artifacts */
3873 	if ((preserve_mode) && FLAG(o_ptr, TR_INSTA_ART) &&
3874 		o_ptr->a_idx)
3875 	{
3876 		a_info[o_ptr->a_idx].cur_num = 0;
3877 	}
3878 
3879 	/* Failure */
3880 	return (FALSE);
3881 }
3882 
3883 /*
3884  * Put an object of the requested type on the location given.
3885  *
3886  * This is mostly used for creating items in quests.
3887  */
place_specific_object(int x,int y,int level,int k_idx)3888 void place_specific_object(int x, int y, int level, int k_idx)
3889 {
3890 	object_type *o_ptr;
3891 	object_kind *k_ptr;
3892 
3893 	int i;
3894 
3895 	/* Paranoia */
3896 	if (!k_idx) return;
3897 
3898 	k_ptr = &k_info[k_idx];
3899 
3900 	/* Instant artifacts are special */
3901 	if (FLAG(k_ptr, TR_INSTA_ART))
3902 	{
3903 		/* Find the "special" artifact this object belongs to */
3904 		for (i = 1; i < z_info->a_max; i++)
3905 		{
3906 			artifact_type *a_ptr = &a_info[i];
3907 
3908 			/* Skip "empty" artifacts */
3909 			if (!a_ptr->name) continue;
3910 
3911 			if ((a_ptr->tval == k_ptr->tval) && (a_ptr->sval == k_ptr->sval))
3912 			{
3913 				/* Found it */
3914 				create_named_art(i, x, y);
3915 				return;
3916 			}
3917 		}
3918 
3919 		/* Exit */
3920 		return;
3921 	}
3922 	else
3923 	{
3924 		/* Create the item */
3925 		o_ptr = object_prep(k_idx);
3926 
3927 		/* Apply magic */
3928 		apply_magic(o_ptr, level, 0, OC_NORMAL);
3929 
3930 		/* Hack -- generate multiple spikes/missiles/ mushrooms */
3931 		switch (o_ptr->tval)
3932 		{
3933 			case TV_SPIKE:
3934 			case TV_SHOT:
3935 			case TV_ARROW:
3936 			case TV_BOLT:
3937 			{
3938 				o_ptr->number = (byte)damroll(6, 7);
3939 				break;
3940 			}
3941 
3942 			case TV_FOOD:
3943 			{
3944 				if (o_ptr->sval < SV_FOOD_BISCUIT)
3945 				{
3946 					/* Mushrooms appear in clumps */
3947 					o_ptr->number = (byte)randint1(6);
3948 				}
3949 
3950 				break;
3951 			}
3952 		}
3953 	}
3954 
3955 	/* Add the object to the ground */
3956 	drop_near(o_ptr, -1, x, y);
3957 }
3958 
3959 
3960 /*
3961  * Attempt to place an object (normal or good/great) at the given location.
3962  *
3963  * This routine plays nasty games to generate the "special artifacts".
3964  *
3965  * This routine uses "base_level()" + "delta_level" for the "generation level".
3966  *
3967  * This routine requires a clean floor grid destination.
3968  */
place_object(int x,int y,bool good,bool great,int delta_level)3969 void place_object(int x, int y, bool good, bool great, int delta_level)
3970 {
3971 	cave_type *c_ptr;
3972 
3973 	object_type *o_ptr;
3974 
3975 	place_type *pl_ptr = &place[p_ptr->place_num];
3976 
3977 	obj_theme *o_theme = &pl_ptr->dungeon->theme;
3978 
3979 	/* Paranoia -- check bounds */
3980 	if (!in_bounds2(x, y)) return;
3981 
3982 	/* Acquire grid */
3983 	c_ptr = area(x, y);
3984 
3985 	/* Do not generate items on "nasty" terrain */
3986 	if ((c_ptr->feat == FEAT_SHAL_LAVA) ||
3987 		(c_ptr->feat == FEAT_SHAL_WATER) || (c_ptr->feat == FEAT_SHAL_ACID))
3988 	{
3989 		return;
3990 	}
3991 
3992 	/* Make an object (if possible) */
3993 	o_ptr = make_object(base_level() + delta_level,
3994 						(good ? 15 : 0) + (great ? 15 : 0), o_theme);
3995 
3996 	/* Put it on the ground */
3997 	(void)put_object(o_ptr, x, y);
3998 }
3999 
4000 
4001 /*
4002  * Make a treasure object
4003  */
make_gold(int level,int coin_type)4004 object_type *make_gold(int level, int coin_type)
4005 {
4006 	s16b i;
4007 
4008 	s32b base;
4009 
4010 	object_type *o_ptr;
4011 
4012 	if (coin_type)
4013 	{
4014 		/* Hack -- Creeping Coins only generate "themselves" */
4015 		i = coin_type;
4016 	}
4017 	else
4018 	{
4019 		/* Hack -- Pick a Treasure variety */
4020 		i = ((randint1(level + 2) + 2) / 2) - 1;
4021 
4022 		/* Apply "extra" magic */
4023 		if (one_in_(GREAT_OBJ))
4024 		{
4025 			i += randint1(level + 1);
4026 		}
4027 	}
4028 
4029 	/* Do not create "illegal" Treasure Types */
4030 	if (i >= MAX_GOLD) i = MAX_GOLD - 1;
4031 
4032 	/* Prepare a gold object */
4033 	o_ptr = object_prep(OBJ_GOLD_LIST + i);
4034 
4035 	/* Hack -- Base coin cost */
4036 	base = k_info[OBJ_GOLD_LIST + i].cost;
4037 
4038 	/* Determine how much the treasure is "worth" */
4039 	o_ptr->pval = (base + (8L * randint1(base)) + randint1(8));
4040 
4041 	/* return pointer to gold */
4042 	return (o_ptr);
4043 }
4044 
4045 
4046 /*
4047  * Places a treasure (Gold or Gems) at given location
4048  *
4049  * The location must be a legal, clean, floor grid.
4050  */
place_gold(int x,int y)4051 void place_gold(int x, int y)
4052 {
4053 	object_type *o_ptr;
4054 
4055 	/* Make some gold */
4056 	o_ptr = make_gold(base_level(), 0);
4057 
4058 	/* Put it on the ground */
4059 	(void)put_object(o_ptr, x, y);
4060 }
4061 
4062 
4063 /*
4064  * Let an object fall to the ground at or near a location.
4065  *
4066  * The initial location is assumed to be "in_bounds2()".
4067  *
4068  * This function takes a parameter "chance".  This is the percentage
4069  * chance that the item will "disappear" instead of drop.  If the object
4070  * has been thrown, then this is the chance of disappearance on contact.
4071  *
4072  * Hack -- this function uses "chance" to determine if it should produce
4073  * some form of "description" of the drop event (under the player).
4074  *
4075  * We check several locations to see if we can find a location at which
4076  * the object can combine, stack, or be placed.  Artifacts will try very
4077  * hard to be placed, including "teleporting" to a useful grid if needed.
4078  */
drop_near(object_type * j_ptr,int chance,int x,int y)4079 void drop_near(object_type *j_ptr, int chance, int x, int y)
4080 {
4081 	int i, k, d, s;
4082 
4083 	int bs, bn;
4084 	int by, bx;
4085 	int dy, dx;
4086 	int ty, tx;
4087 
4088 	cave_type *c_ptr;
4089 	object_type *o_ptr = NULL;
4090 
4091 	char o_name[256];
4092 
4093 	bool flag = FALSE;
4094 	bool done = FALSE;
4095 
4096 	bool plural = FALSE;
4097 
4098 
4099 	/* Extract plural */
4100 	if (j_ptr->number != 1) plural = TRUE;
4101 
4102 	/* Describe object */
4103 	object_desc(o_name, j_ptr, FALSE, 0, 256);
4104 
4105 
4106 	/* Handle normal "breakage" */
4107 	if (!(FLAG(j_ptr, TR_INSTA_ART)) && (randint0(100) < chance))
4108 	{
4109 		/* Message */
4110 		msgf("The %s disappear%s.", o_name, (plural ? "" : "s"));
4111 
4112 		/* Debug */
4113 		if (p_ptr->state.wizard) msgf("(breakage)");
4114 
4115 		/* Failure */
4116 		return;
4117 	}
4118 
4119 
4120 	/* Score */
4121 	bs = -1;
4122 
4123 	/* Picker */
4124 	bn = 0;
4125 
4126 	/* Default */
4127 	by = y;
4128 	bx = x;
4129 
4130 	/* Scan local grids */
4131 	for (dy = -3; dy <= 3; dy++)
4132 	{
4133 		/* Scan local grids */
4134 		for (dx = -3; dx <= 3; dx++)
4135 		{
4136 			bool comb = FALSE;
4137 
4138 			/* Calculate actual distance */
4139 			d = (dy * dy) + (dx * dx);
4140 
4141 			/* Ignore distant grids */
4142 			if (d > 10) continue;
4143 
4144 			/* Location */
4145 			ty = y + dy;
4146 			tx = x + dx;
4147 
4148 			/* Skip illegal grids */
4149 			if (!in_bounds2(tx, ty)) continue;
4150 
4151 			/* Require line of sight */
4152 			if (!los(x, y, tx, ty)) continue;
4153 
4154 			/* Obtain grid */
4155 			c_ptr = area(tx, ty);
4156 
4157 			/* Require floor space */
4158 			if (!cave_nice_grid(c_ptr)) continue;
4159 
4160 			/* Not on "nasty" terrains */
4161 			if ((c_ptr->feat == FEAT_SHAL_LAVA) ||
4162 				(c_ptr->feat == FEAT_SHAL_ACID) ||
4163 				(c_ptr->feat == FEAT_SHAL_WATER)) continue;
4164 
4165 			/* Check to see if fields dissallow placement */
4166 			if (fields_have_flags(c_ptr, FIELD_INFO_NO_OBJCT))
4167 			{
4168 				continue;
4169 			}
4170 
4171 			/* No objects */
4172 			k = 0;
4173 
4174 			/* Scan objects in that grid */
4175 			OBJ_ITT_START (c_ptr->o_idx, o_ptr)
4176 			{
4177 				/* Check for possible combination */
4178 				if (object_similar(o_ptr, j_ptr)) comb = TRUE;
4179 
4180 				/* Count objects */
4181 				k++;
4182 			}
4183 			OBJ_ITT_END;
4184 
4185 			/* Add new object */
4186 			if (!comb) k++;
4187 
4188 			/* No stacking (allow combining) [Optional for Topi] */
4189 			if (!testing_stack && (k > 1)) continue;
4190 
4191 			/* Paranoia */
4192 			if (k > 99) continue;
4193 
4194 			/* Calculate score */
4195 			s = 1000 - (d + k * 5);
4196 
4197 			/* Skip bad values */
4198 			if (s < bs) continue;
4199 
4200 			/* New best value */
4201 			if (s > bs) bn = 0;
4202 
4203 			/* Apply the randomizer to equivalent values */
4204 			if ((++bn >= 2) && !one_in_(bn)) continue;
4205 
4206 			/* Keep score */
4207 			bs = s;
4208 
4209 			/* Track it */
4210 			by = ty;
4211 			bx = tx;
4212 
4213 			/* Okay */
4214 			flag = TRUE;
4215 		}
4216 	}
4217 
4218 
4219 	/* Handle lack of space */
4220 	if (!flag && !(FLAG(j_ptr, TR_INSTA_ART)))
4221 	{
4222 		/* Message */
4223 		msgf("The %s disappear%s.", o_name, (plural ? "" : "s"));
4224 
4225 		/* Debug */
4226 		if (p_ptr->state.wizard) msgf("(no floor space)");
4227 
4228 		/* Failure */
4229 		return;
4230 	}
4231 
4232 	/* Find a grid */
4233 	for (i = 0; !flag; i++)
4234 	{
4235 		/* Bounce around */
4236 		if (i < 1000)
4237 		{
4238 			ty = rand_spread(by, 1);
4239 			tx = rand_spread(bx, 1);
4240 
4241 			/* Do some bound checking */
4242 			ty = MAX(ty, p_ptr->min_hgt);
4243 			ty = MIN(ty, p_ptr->max_hgt - 1);
4244 			tx = MAX(tx, p_ptr->min_wid);
4245 			tx = MIN(tx, p_ptr->max_wid - 1);
4246 		}
4247 
4248 		/* Random locations */
4249 		else
4250 		{
4251 			/* Pick a location */
4252 			ty = rand_range(p_ptr->min_hgt, p_ptr->max_hgt - 1);
4253 			tx = rand_range(p_ptr->min_wid, p_ptr->max_wid - 1);
4254 		}
4255 
4256 		/* Grid */
4257 		c_ptr = area(tx, ty);
4258 
4259 		/* Check to see if fields dissallow placement */
4260 		if (fields_have_flags(c_ptr, FIELD_INFO_NO_OBJCT)) continue;
4261 
4262 		/* Bounce to that location */
4263 		by = ty;
4264 		bx = tx;
4265 
4266 		/* Require floor space */
4267 		if (!cave_nice_grid(c_ptr)) continue;
4268 
4269 		/* Not on "nasty" terrains */
4270 		if ((c_ptr->feat == FEAT_SHAL_LAVA) ||
4271 			(c_ptr->feat == FEAT_SHAL_ACID) ||
4272 			(c_ptr->feat == FEAT_SHAL_WATER)) continue;
4273 
4274 		/* Okay */
4275 		flag = TRUE;
4276 	}
4277 
4278 	/* Grid */
4279 	c_ptr = area(bx, by);
4280 
4281 	/* Hack - artifacts will not be affected by terrain */
4282 	if (!(FLAG(j_ptr, TR_INSTA_ART)))
4283 	{
4284 		/* Check to see if the object will burn on contact with lava. */
4285 		if ((c_ptr->feat == FEAT_SHAL_LAVA) &&
4286 			((j_ptr->tval == TV_STAFF) ||
4287 			 (j_ptr->tval == TV_SCROLL) || (j_ptr->tval == TV_WAND)))
4288 		{
4289 			/* only display messages if player throws */
4290 			if (!chance)
4291 			{
4292 				/* Message */
4293 				msgf("The %s%s burns in the lava.",
4294 						   o_name, (plural ? "" : "s"));
4295 			}
4296 
4297 			/* Debug */
4298 			if (p_ptr->state.wizard) msgf("(contact with lava)");
4299 
4300 			/* Failure */
4301 			return;
4302 		}
4303 
4304 		/* Check to see if the object will disappear in water. */
4305 		if ((c_ptr->feat == FEAT_SHAL_WATER) && (j_ptr->tval == TV_RING))
4306 		{
4307 			/* only display messages if player throws */
4308 			if (!chance)
4309 			{
4310 				/* Message */
4311 				msgf("The %s disappear%s.", o_name, (plural ? "" : "s"));
4312 			}
4313 
4314 			/* Debug */
4315 			if (p_ptr->state.wizard) msgf("(contact with water)");
4316 
4317 			/* Failure */
4318 			return;
4319 		}
4320 	}
4321 
4322 	/* Scan objects in that grid for combination */
4323 	OBJ_ITT_START (c_ptr->o_idx, o_ptr)
4324 	{
4325 		/* Check for combination */
4326 		if (object_similar(o_ptr, j_ptr))
4327 		{
4328 			/* Combine the items */
4329 			object_absorb(o_ptr, j_ptr);
4330 
4331 			/* Success */
4332 			done = TRUE;
4333 
4334 			/* Done */
4335 			break;
4336 		}
4337 	}
4338 	OBJ_ITT_END;
4339 
4340 	/* Get new object */
4341 	if (!done)
4342 	{
4343 		/* Put it on the ground */
4344 		if (!put_object(j_ptr, bx, by))
4345 		{
4346 			/* Message */
4347 			msgf("The %s disappear%s.", o_name, (plural ? "" : "s"));
4348 
4349 			/* Debug */
4350 			if (p_ptr->state.wizard) msgf("(too many objects)");
4351 
4352 			/* Failure */
4353 			return;
4354 		}
4355 	}
4356 
4357 	/* Sound */
4358 	sound(SOUND_DROP);
4359 
4360 	/* Mega-Hack -- no message if "dropped" by player */
4361 	/* Message when an object falls under the player */
4362 	if (chance && (by == p_ptr->py) && (bx == p_ptr->px))
4363 	{
4364 		msgf("You feel something roll beneath your feet.");
4365 	}
4366 
4367 	/* Fields may interact with an object in some way */
4368 	field_script(area(bx, by), FIELD_ACT_OBJECT_DROP, "p",
4369 		 LUA_OBJECT(o_ptr));
4370 }
4371 
4372 
4373 /*
4374  * Scatter some "great" objects near the player
4375  */
acquirement(int x1,int y1,int num,bool great,bool known)4376 void acquirement(int x1, int y1, int num, bool great, bool known)
4377 {
4378 	object_type *o_ptr = NULL;
4379 
4380 	int i;
4381 
4382 	obj_theme theme;
4383 
4384 	/* Set theme - more weapons than normal */
4385 	theme.treasure = 10;
4386 	theme.combat = 80;
4387 	theme.magic = 10;
4388 	theme.tools = 0;
4389 
4390 	/* Acquirement */
4391 	while (num--)
4392 	{
4393 		/* We want a good object */
4394 		for (i = 0; i < 1000; i++)
4395 		{
4396 			if (great)
4397 			{
4398 				/* Make a great object (if possible) */
4399 				o_ptr = make_object(base_level(), 40, &theme);
4400 
4401 				/* Paranoia */
4402 				if (!o_ptr) continue;
4403 			}
4404 			else
4405 			{
4406 				/* Make a good object (if possible) */
4407 				o_ptr = make_object(base_level(), 20, &theme);
4408 
4409 				/* Paranoia */
4410 				if (!o_ptr) continue;
4411             }
4412 
4413             /* Skip cursed items */
4414             if (cursed_p(o_ptr)) continue;
4415 
4416 			/* Check to see if the object is worth anything */
4417 			if (object_value_real(o_ptr) > 0) break;
4418 		}
4419 
4420 		/* Paranoia */
4421 		if (i >= 1000) return;
4422 
4423 		if (known)
4424 		{
4425 			object_aware(o_ptr);
4426 			object_known(o_ptr);
4427 		}
4428 
4429 		/* Drop the object */
4430 		drop_near(o_ptr, -1, x1, y1);
4431 	}
4432 }
4433 
4434 /*
4435  * Look up the list that corresponds to a given
4436  * object returned by the get_item() function.
4437  *
4438  * We know the item is in our equipment, our
4439  * inventory, or is on the floor underneith us.
4440  */
look_up_list(object_type * o_ptr)4441 s16b *look_up_list(object_type *o_ptr)
4442 {
4443 	object_type *j_ptr;
4444 
4445 	cave_type *c_ptr;
4446 
4447 	place_type *pl_ptr = &place[p_ptr->place_num];
4448 
4449 	int i;
4450 
4451 	/* Some objects have no list */
4452 	if (!o_ptr->allocated) return (NULL);
4453 
4454 	/* Scan player inventory */
4455 	OBJ_ITT_START (p_ptr->inventory, j_ptr)
4456 	{
4457 		if (o_ptr == j_ptr) return (&p_ptr->inventory);
4458 
4459 		/* Debug - make sure we don't have a corrupted inventory */
4460 		if (j_ptr->ix || j_ptr->iy) quit("Corrupted inventory contains dungeon objects!");
4461 
4462 		/* Debug - test array bounds */
4463 		if (GET_ARRAY_INDEX(o_list, j_ptr) >= o_max) quit("Inven outside bounds!");
4464 	}
4465 	OBJ_ITT_END;
4466 
4467 	/* Scan dungeon */
4468 	if (o_ptr->ix || o_ptr->iy)
4469 	{
4470 		c_ptr = area(o_ptr->ix, o_ptr->iy);
4471 
4472 		/* Scan square */
4473 		OBJ_ITT_START (c_ptr->o_idx, j_ptr)
4474 		{
4475 			if (o_ptr == j_ptr) return (&c_ptr->o_idx);
4476 		}
4477 		OBJ_ITT_END;
4478 	}
4479 
4480 	/* Scan stores */
4481 	for (i = 0; i < pl_ptr->numstores; i++)
4482 	{
4483 		/* Scan store stock */
4484 		OBJ_ITT_START (pl_ptr->store[i].stock, j_ptr)
4485 		{
4486 			if (o_ptr == j_ptr) return (&pl_ptr->store[i].stock);
4487 		}
4488 		OBJ_ITT_END;
4489 	}
4490 
4491 	/* Failure - the object is inconsistant */
4492 	quit("Failed to look up object.");
4493 	return (NULL);
4494 }
4495 
4496 /*
4497  * Get the nth item in a list
4498  */
get_list_item(s16b list_start,int number)4499 object_type *get_list_item(s16b list_start, int number)
4500 {
4501 	object_type *o_ptr;
4502 
4503 	/* Paranoia */
4504 	if (number < 0) return (NULL);
4505 
4506 	OBJ_ITT_START (list_start, o_ptr)
4507 	{
4508 		if (!number) return (o_ptr);
4509 
4510 		number--;
4511 	}
4512 	OBJ_ITT_END;
4513 
4514 	/* We didn't find it */
4515 	return (NULL);
4516 }
4517 
4518 /*
4519  * The inverse function of get_list_item()
4520  *
4521  * Find the position in the list of a given item
4522  */
get_item_position(s16b list_start,object_type * o_ptr)4523 int get_item_position(s16b list_start, object_type *o_ptr)
4524 {
4525 	int i = 0;
4526 
4527 	object_type *j_ptr;
4528 
4529 	OBJ_ITT_START (list_start, j_ptr)
4530 	{
4531 		if (j_ptr == o_ptr) return (i);
4532 
4533 		i++;
4534 	}
4535 	OBJ_ITT_END;
4536 
4537 	/* Failure */
4538 	return (-1);
4539 }
4540 
4541 /*
4542  * How many items are in the list?
4543  */
get_list_length(s16b list_start)4544 int get_list_length(s16b list_start)
4545 {
4546 	int i = 0;
4547 
4548 	object_type *j_ptr;
4549 
4550 	OBJ_ITT_START (list_start, j_ptr)
4551 	{
4552 		/* Count items */
4553 		i++;
4554 	}
4555 	OBJ_ITT_END;
4556 
4557 	return (i);
4558 }
4559 
4560 
4561 /* Is the item on the floor? */
floor_item(object_type * o_ptr)4562 bool floor_item(object_type *o_ptr)
4563 {
4564 	s16b *o_list = look_up_list(o_ptr);
4565 
4566 	cave_type *c_ptr = area(p_ptr->px, p_ptr->py);
4567 
4568 	/* On floor? */
4569 	if (o_list == &c_ptr->o_idx) return (TRUE);
4570 
4571 	/* Elsewhere */
4572 	return (FALSE);
4573 }
4574 
4575 /* Is the item in the players inventory or equipment? */
player_item(object_type * o_ptr)4576 bool player_item(object_type *o_ptr)
4577 {
4578 	s16b *o_list = look_up_list(o_ptr);
4579 
4580 	/* Equipment? */
4581 	if (!o_list) return (TRUE);
4582 
4583 	/* Inventory */
4584 	if (o_list == &p_ptr->inventory) return (TRUE);
4585 
4586 	/* Elsewhere */
4587 	return (TRUE);
4588 }
4589 
4590 
4591 
4592 /*
4593  * Describe the charges on an item in the inventory.
4594  */
item_charges(object_type * o_ptr)4595 void item_charges(object_type *o_ptr)
4596 {
4597 	/* Require staff/wand */
4598 	if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
4599 
4600 	/* Require known item */
4601 	if (!object_known_p(o_ptr)) return;
4602 
4603 	/* Print a message */
4604 	msgf("%s %d charge%s remaining.",
4605 			   floor_item(o_ptr) ? "It has" : "You have", o_ptr->pval,
4606 			   (o_ptr->pval != 1) ? "s" : "");
4607 }
4608 
4609 
4610 /*
4611  * Describe an item in the inventory.
4612  */
item_describe_aux(object_type * o_ptr,bool back_step)4613 static cptr item_describe_aux(object_type *o_ptr, bool back_step)
4614 {
4615 	char o_name[256];
4616 	char lab[40] = "";
4617 
4618 	int item;
4619 
4620 	/* Get list */
4621 	s16b *list = look_up_list(o_ptr);
4622 
4623 	cave_type *c_ptr = area(p_ptr->px, p_ptr->py);
4624 
4625 	/* Get a description */
4626 	object_desc(o_name, o_ptr, TRUE, 3, 256);
4627 
4628 	if (o_ptr->number <= 0)
4629 	{
4630 		if (!list)
4631 		{
4632 			/* Hack XXX XXX pretend there is one item */
4633 			int num = o_ptr->number;
4634 			o_ptr->number = 1;
4635 
4636 			/* Get a (new) description */
4637 			object_desc(o_name, o_ptr, TRUE, 3, 256);
4638 
4639 			/* Item is in the equipment */
4640 			item = GET_ARRAY_INDEX(p_ptr->equipment, o_ptr);
4641 
4642 			/* No more items? */
4643 			return (format("You were %s: %s (%c).", describe_use(item), o_name, I2A(item)));
4644 
4645 			/* Restore old number of items */
4646 			o_ptr->number = num;
4647 		}
4648 		else if (list == &p_ptr->inventory)
4649 		{
4650 			/* No more items? */
4651 			return (format("There are %s.", o_name));
4652 		}
4653 		else if (list == &c_ptr->o_idx)
4654 		{
4655 			return (format("On the ground: %s.", o_name));
4656 		}
4657 	}
4658 	else
4659 	{
4660 		if (!list)
4661 		{
4662 			/* Item is in the equipment */
4663 			item = GET_ARRAY_INDEX(p_ptr->equipment, o_ptr);
4664 
4665 			if (show_labels) strnfmt(lab, 40, "%^s: ", describe_use(item));
4666 
4667 			return (format("%s%s (%c).", lab, o_name, I2A(item)));
4668 		}
4669 		else if (list == &p_ptr->inventory)
4670 		{
4671 			/* Get number of item in inventory */
4672 			item = get_item_position(p_ptr->inventory, o_ptr);
4673 
4674 			if (show_labels) strnfmt(lab, 40, "In your pack: ");
4675 
4676 			/* Hack to get that letter correct in case a scroll disappears */
4677 			if (back_step)
4678 				return (format("%s%s (%c).", lab, o_name, I2A(item - 1)));
4679 			else
4680 				return (format("%s%s (%c).", lab, o_name, I2A(item)));
4681 		}
4682 		else if (list == &c_ptr->o_idx)
4683 		{
4684 			return (format("On the ground: %s.", o_name));
4685 		}
4686 		/* Then it is in the shop */
4687 		else
4688 		{
4689 			if (show_labels) strnfmt(lab, 40, "In the shop: ");
4690 
4691 			return (format("%s%s", lab, o_name));
4692 		}
4693 	}
4694 
4695 	/* Missed it all */
4696 	return (NULL);
4697 }
4698 
4699 
4700 /*
4701  * Describe an item in the inventory.
4702  */
item_describe(object_type * o_ptr)4703 void item_describe(object_type *o_ptr)
4704 {
4705 	msgf("%s", item_describe_aux(o_ptr, FALSE));
4706 }
4707 
4708 
4709 /*
4710  * Describe an item in the inventory.
4711  */
item_describe_roff(object_type * o_ptr)4712 void item_describe_roff(object_type *o_ptr)
4713 {
4714 	roff("%s", item_describe_aux(o_ptr, FALSE));
4715 }
4716 
4717 
4718 /*
4719  * Describe an item in the inventory and pretend it is one slot lower than it
4720  * seems to be.  This is usefull when a the item is being identified by a scroll
4721  * of idenitfy and it was the last scroll of identify so there is some shuffling
4722  * in the inventory.
4723  *
4724  * Faux is for faux pas as this is a hack.
4725  */
item_describe_faux(object_type * o_ptr)4726 void item_describe_faux(object_type *o_ptr)
4727 {
4728 	msgf("%s", item_describe_aux(o_ptr, TRUE));
4729 }
4730 
4731 
4732 /*
4733  * Erase an inventory slot if it has no more items
4734  */
item_optimize(object_type * o_ptr)4735 static void item_optimize(object_type *o_ptr)
4736 {
4737 	s16b *list;
4738 
4739 	/* Default to looking under the player */
4740 	cave_type *c_ptr = area(p_ptr->px, p_ptr->py);
4741 
4742 	/* The player could have moved due to a phase door scroll */
4743 	if (in_bounds2(o_ptr->ix, o_ptr->iy))
4744 	{
4745 		c_ptr = area(o_ptr->ix, o_ptr->iy);
4746 	}
4747 
4748 	/* Only optimize real items */
4749 	if (!o_ptr->k_idx) return;
4750 
4751 	/* Only optimize empty items */
4752 	if (o_ptr->number) return;
4753 
4754 	/* Get list */
4755 	list = look_up_list(o_ptr);
4756 
4757 	/* The item is being wielded */
4758 	if (!list)
4759 	{
4760 		/* Erase the empty slot */
4761 		object_wipe(o_ptr);
4762 
4763 		/* Recalculate bonuses */
4764 		p_ptr->update |= (PU_BONUS);
4765 
4766 		/* Recalculate torch */
4767 		p_ptr->update |= (PU_TORCH);
4768 
4769 		/* Recalculate mana */
4770 		p_ptr->update |= (PU_MANA);
4771 
4772 		/* Window stuff */
4773 		p_ptr->window |= (PW_EQUIP);
4774 	}
4775 	else
4776 	{
4777 		/* Delete the object */
4778 		if (list == &p_ptr->inventory)
4779 		{
4780 			/* Inventory item */
4781 			delete_held_object(list, o_ptr);
4782 
4783 			/* Window stuff */
4784 			p_ptr->window |= (PW_INVEN);
4785 		}
4786 		else if (list == &c_ptr->o_idx)
4787 		{
4788 			/* Floor item */
4789 			delete_dungeon_object(o_ptr);
4790 		}
4791 		else
4792 		{
4793 			/* Store item */
4794 			delete_held_object(list, o_ptr);
4795 		}
4796 	}
4797 
4798 	/* Window stuff */
4799 	p_ptr->window |= (PW_SPELL);
4800 }
4801 
4802 
4803 /*
4804  * Split a pile into two bits.  Return a pointer to
4805  * the split off piece.  This piece will not be in the
4806  * original pile's list, but will be in the static
4807  * temp_object defined above.
4808  */
item_split(object_type * o_ptr,int num)4809 object_type *item_split(object_type *o_ptr, int num)
4810 {
4811 	object_type *q_ptr;
4812 
4813 	/* Paranoia */
4814 	if (o_ptr->number < num) num = o_ptr->number;
4815 
4816 	/* Duplicate the object */
4817 	q_ptr = object_dup(o_ptr);
4818 
4819 	/* Distribute charges of wands or rods */
4820 	distribute_charges(o_ptr, q_ptr, num);
4821 
4822 	/* Update item totals */
4823 	o_ptr->number -= num;
4824 	q_ptr->number = num;
4825 
4826 	/* Notice the change */
4827 	if (num && player_item(o_ptr))
4828 	{
4829 		/* Recalculate bonuses and weight */
4830 		p_ptr->update |= (PU_BONUS | PU_WEIGHT);
4831 
4832 		/* Recalculate mana */
4833 		p_ptr->update |= (PU_MANA);
4834 
4835 		/* Notice changes */
4836 		notice_item();
4837 	}
4838 
4839 	/* Fill in holes... */
4840 	item_optimize(o_ptr);
4841 
4842 	/* Done - return new item */
4843 	return (q_ptr);
4844 }
4845 
4846 
4847 /*
4848  * Increase the "number" of an item in the inventory
4849  */
item_increase_aux(object_type * o_ptr,int num,bool silent)4850 static void item_increase_aux(object_type *o_ptr, int num, bool silent)
4851 {
4852 	/* Apply */
4853 	num += o_ptr->number;
4854 
4855 	/* Bounds check */
4856 	if (num > 255) num = 255;
4857 	else if (num < 0) num = 0;
4858 
4859 	/* Un-apply */
4860 	num -= o_ptr->number;
4861 
4862 	/* Add the number */
4863 	o_ptr->number += num;
4864 
4865 	/* Notice the change */
4866 	if (num && player_item(o_ptr))
4867 	{
4868 		/* Recalculate bonuses and weight */
4869 		p_ptr->update |= (PU_BONUS | PU_WEIGHT);
4870 
4871 		/* Recalculate mana */
4872 		p_ptr->update |= (PU_MANA);
4873 
4874 		/* Notice changes */
4875 		notice_item();
4876 	}
4877 
4878 	/* The shop upkeeping shouldn't be mentioned */
4879 	if (!silent) item_describe(o_ptr);
4880 
4881 	item_optimize(o_ptr);
4882 }
4883 
4884 /*
4885  * Increase the "number" of an item in the inventory
4886  */
item_increase(object_type * o_ptr,int num)4887 void item_increase(object_type *o_ptr, int num)
4888 {
4889 	item_increase_aux(o_ptr, num, FALSE);
4890 }
4891 
4892 /*
4893  * Increase the "number" of an item in the inventory without a message
4894  */
item_increase_silent(object_type * o_ptr,int num)4895 void item_increase_silent(object_type *o_ptr, int num)
4896 {
4897 	item_increase_aux(o_ptr, num, TRUE);
4898 }
4899 
4900 /*
4901  * Check if we have space for an item in the pack without overflow
4902  */
inven_carry_okay(const object_type * o_ptr)4903 bool inven_carry_okay(const object_type *o_ptr)
4904 {
4905 	object_type *j_ptr;
4906 
4907 	/* Empty slot? */
4908 	if (get_list_length(p_ptr->inventory) < INVEN_PACK) return (TRUE);
4909 
4910 	/* Similar slot? */
4911 	OBJ_ITT_START (p_ptr->inventory, j_ptr)
4912 	{
4913 		/* Check if the two items can be combined */
4914 		if (object_similar(j_ptr, o_ptr)) return (TRUE);
4915 	}
4916 	OBJ_ITT_END;
4917 
4918 	/* Nope */
4919 	return (FALSE);
4920 }
4921 
4922 /*
4923  * Compare two items to see if they are in pack-order.
4924  */
reorder_pack_comp(const object_type * o1_ptr,const object_type * o2_ptr)4925 static bool reorder_pack_comp(const object_type *o1_ptr,
4926                               const object_type *o2_ptr)
4927 {
4928 	/* Hack -- readable books always come first */
4929 	if ((o1_ptr->tval == REALM1_BOOK) &&
4930 		(o2_ptr->tval != REALM1_BOOK)) return (TRUE);
4931 	if ((o2_ptr->tval == REALM1_BOOK) &&
4932 		(o1_ptr->tval != REALM1_BOOK)) return (FALSE);
4933 
4934 
4935 	if ((o1_ptr->tval == REALM2_BOOK) &&
4936 		(o2_ptr->tval != REALM2_BOOK)) return (TRUE);
4937 	if ((o2_ptr->tval == REALM2_BOOK) &&
4938 		(o1_ptr->tval != REALM2_BOOK)) return (FALSE);
4939 
4940 
4941 	/* Objects sort by decreasing type */
4942 	if (o1_ptr->tval > o2_ptr->tval) return (TRUE);
4943 	if (o1_ptr->tval < o2_ptr->tval) return (FALSE);
4944 
4945 	/* Non-aware (flavored) items always come last */
4946 	if (!object_aware_p(o2_ptr)) return (TRUE);
4947 	if (!object_aware_p(o1_ptr)) return (FALSE);
4948 
4949 	/* Objects sort by increasing sval */
4950 	if (o1_ptr->sval < o2_ptr->sval) return (TRUE);
4951 	if (o1_ptr->sval > o2_ptr->sval) return (FALSE);
4952 
4953 	/* Unidentified objects always come last */
4954 	if (!object_known_p(o2_ptr)) return (TRUE);
4955 	if (!object_known_p(o1_ptr)) return (FALSE);
4956 
4957 	/* Lites sort by increasing timeout */
4958 	if (o1_ptr->tval == TV_LITE)
4959 	{
4960 		if (o1_ptr->sval < o2_ptr->sval) return (TRUE);
4961 		if (o1_ptr->sval > o2_ptr->sval) return (FALSE);
4962 	}
4963 
4964 	/*
4965 	 * Hack:  otherwise identical rods sort by
4966 	 * increasing recharge time -DSB-
4967 	 */
4968 	if (o1_ptr->tval == TV_ROD)
4969 	{
4970 		if (o1_ptr->pval < o2_ptr->pval) return (TRUE);
4971 		if (o1_ptr->pval > o2_ptr->pval) return (FALSE);
4972 	}
4973 
4974 	/* Objects sort by decreasing value */
4975 	if (object_value(o1_ptr) >= object_value(o2_ptr)) return (TRUE);
4976 
4977 	return (FALSE);
4978 }
4979 
4980 /*
4981  * Actually reorder the objects
4982  *
4983  * This uses a simple bubble sort.
4984  *
4985  * Usually we only need to make a few swaps.
4986  */
reorder_objects_aux(object_type * q_ptr,object_comp comp_func,u16b o_idx)4987 object_type *reorder_objects_aux(object_type *q_ptr, object_comp comp_func,
4988                                  u16b o_idx)
4989 {
4990 	object_type *o_ptr, *j_ptr;
4991 
4992 	int i;
4993 
4994 	/*
4995 	 * Hack - Do this twice because we invert the order
4996 	 * of 'similar' objects on the first pass
4997 	 */
4998 	for (i = 0; i < 2; i++)
4999 	{
5000 		/* Re-order the pack */
5001 		OBJ_ITT_START (o_idx, o_ptr)
5002 		{
5003 			OBJ_ITT_START (o_ptr->next_o_idx, j_ptr)
5004 			{
5005 				/* Are they in the right order? */
5006 				if (comp_func(j_ptr, o_ptr))
5007 				{
5008 					/* Are we moving the watched item? */
5009 					if (q_ptr)
5010 					{
5011 						if (q_ptr == o_ptr) q_ptr = j_ptr;
5012 						else if (q_ptr == j_ptr) q_ptr = o_ptr;
5013 					}
5014 
5015 					/* Swap the objects */
5016 					swap_objects(o_ptr, j_ptr);
5017 				}
5018 			}
5019 			OBJ_ITT_END;
5020 		}
5021 		OBJ_ITT_END;
5022 	}
5023 
5024 	return (q_ptr);
5025 }
5026 
5027 
5028 /*
5029  * Add an item to the players inventory, and return the slot used.
5030  *
5031  * If the new item can combine with an existing item in the inventory,
5032  * it will do so, using "object_similar()" and "object_absorb()", else,
5033  * the item will be placed into the "proper" location in the inventory.
5034  *
5035  * This function can be used to "over-fill" the player's pack, but only
5036  * once, and such an action must trigger the "overflow" code immediately.
5037  * Note that when the pack is being "over-filled", the new item must be
5038  * placed into the "overflow" slot, and the "overflow" must take place
5039  * before the pack is reordered, but (optionally) after the pack is
5040  * combined.  This may be tricky.  See "dungeon.c" for info.
5041  *
5042  * Note that this code must remove any location/stack information
5043  * from the object once it is placed into the inventory.
5044  */
inven_carry(object_type * o_ptr)5045 object_type *inven_carry(object_type *o_ptr)
5046 {
5047 	object_type *j_ptr;
5048 
5049 	/* Check for combining */
5050 	OBJ_ITT_START (p_ptr->inventory, j_ptr)
5051 	{
5052 		/* Check if the two items can be combined */
5053 		if (object_similar(j_ptr, o_ptr))
5054 		{
5055 			/* Combine the items */
5056 			object_absorb(j_ptr, o_ptr);
5057 
5058 			/* Recalculate bonuses and weight */
5059 			p_ptr->update |= (PU_BONUS | PU_WEIGHT);
5060 
5061 			/* Notice changes */
5062 			notice_inven();
5063 
5064 			/* Wipe old object */
5065 			object_wipe(o_ptr);
5066 
5067 			/* Success */
5068 			return (j_ptr);
5069 		}
5070 	}
5071 	OBJ_ITT_END;
5072 
5073 	/* Add the item to the pack */
5074 	o_ptr = add_object_list(&p_ptr->inventory, o_ptr);
5075 
5076 	/* Paranoia */
5077 	if (!o_ptr) return (NULL);
5078 
5079 	/* Forget location */
5080 	o_ptr->iy = o_ptr->ix = 0;
5081 
5082 	/* Forget Region */
5083 	o_ptr->region = 0;
5084 
5085 	/* No longer marked */
5086 	o_ptr->info &= ~(OB_SEEN);
5087 
5088 	/* Reorder the pack */
5089 	o_ptr = reorder_objects_aux(o_ptr, reorder_pack_comp, p_ptr->inventory);
5090 
5091 	/* Recalculate bonuses and weight */
5092 	p_ptr->update |= (PU_BONUS | PU_WEIGHT);
5093 
5094 	/* Notice changes */
5095 	notice_inven();
5096 
5097 	/* Return the new item */
5098 	return (o_ptr);
5099 }
5100 
5101 
5102 /*
5103  * Take off (some of) a non-cursed equipment item
5104  *
5105  * Note that only one item at a time can be wielded per slot.
5106  *
5107  * Note that taking off an item when "full" may cause that item
5108  * to fall to the ground.
5109  *
5110  * Return the inventory slot into which the item is placed.
5111  */
inven_takeoff(object_type * o_ptr)5112 object_type *inven_takeoff(object_type *o_ptr)
5113 {
5114 	int item;
5115 
5116 	object_type *q_ptr;
5117 
5118 	cptr act;
5119 
5120 	/* Look up item number */
5121 	item = GET_ARRAY_INDEX(p_ptr->equipment, o_ptr);
5122 
5123 	/* Took off weapon */
5124 	if (item == EQUIP_WIELD)
5125 	{
5126 		act = "You were wielding";
5127 	}
5128 
5129 	/* Took off bow */
5130 	else if (item == EQUIP_BOW)
5131 	{
5132 		act = "You were holding";
5133 	}
5134 
5135 	/* Took off light */
5136 	else if (item == EQUIP_LITE)
5137 	{
5138 		act = "You were holding";
5139 	}
5140 
5141 	/* Took off something */
5142 	else
5143 	{
5144 		act = "You were wearing";
5145 	}
5146 
5147 	/* Message */
5148 	msgf("%s %v (%c).", act, OBJECT_FMT(o_ptr, TRUE, 3), I2A(item));
5149 
5150 	/* Carry the object */
5151 	q_ptr = inven_carry(o_ptr);
5152 
5153 	/* Paranoia */
5154 	if (!q_ptr)
5155 	{
5156 		msgf("You cannot take off the item - too many dungeon objects!");
5157 		return (NULL);
5158 	}
5159 
5160 	/* Recalculate torch */
5161 	p_ptr->update |= (PU_TORCH);
5162 
5163 	/* Recalculate mana */
5164 	p_ptr->update |= (PU_MANA);
5165 
5166 	p_ptr->redraw |= (PR_EQUIPPY);
5167 
5168 	/* Window stuff */
5169 	p_ptr->window |= (PW_PLAYER | PW_EQUIP);
5170 
5171 	/* Return the item */
5172 	return (q_ptr);
5173 }
5174 
5175 
5176 /*
5177  * Drop (some of) a non-cursed inventory/equipment item
5178  *
5179  * The object will be dropped "near" the current location
5180  */
inven_drop(object_type * o_ptr,int amt)5181 void inven_drop(object_type *o_ptr, int amt)
5182 {
5183 	object_type *q_ptr;
5184 
5185 	int slot;
5186 
5187 	s16b *list;
5188 
5189 	/* Error check */
5190 	if (amt <= 0) return;
5191 
5192 	/* Describe item */
5193 	item_describe(o_ptr);
5194 
5195 	/* Get list */
5196 	list = look_up_list(o_ptr);
5197 
5198 	/* Take off equipment */
5199 	if (!list)
5200 	{
5201 		/* Take off first */
5202 		o_ptr = inven_takeoff(o_ptr);
5203 
5204 		/* Paranoia */
5205 		if (!o_ptr) return;
5206 	}
5207 
5208 	/* Get item slot */
5209 	slot = get_item_position(p_ptr->inventory, o_ptr);
5210 
5211 	/* Get local object */
5212 	q_ptr = item_split(o_ptr, amt);
5213 
5214 	/* Message */
5215 	msgf("You drop %v (%c).", OBJECT_FMT(q_ptr, TRUE, 3), I2A(slot));
5216 
5217 	/* Drop it near the player */
5218 	drop_near(q_ptr, 0, p_ptr->px, p_ptr->py);
5219 
5220 	/* Update total weight */
5221 	p_ptr->update |= PU_WEIGHT;
5222 }
5223 
5224 
5225 /*
5226  * Combine items in the pack
5227  */
combine_pack_aux(object_type * q_ptr)5228 static object_type *combine_pack_aux(object_type *q_ptr)
5229 {
5230 	object_type *o_ptr;
5231 	object_type *j_ptr;
5232 	bool flag = FALSE;
5233 
5234 	/* Combine the pack */
5235 	OBJ_ITT_START (p_ptr->inventory, o_ptr)
5236 	{
5237 		/* Scan the items above that item */
5238 		OBJ_ITT_START (o_ptr->next_o_idx, j_ptr)
5239 		{
5240 			/* Can we drop "o_ptr" onto "j_ptr"? */
5241 			if (object_similar(o_ptr, j_ptr))
5242 			{
5243 				/* Take note */
5244 				flag = TRUE;
5245 
5246 				/* The original is about to disappear so assign to the new */
5247 				if (o_ptr == q_ptr) q_ptr = j_ptr;
5248 
5249 				/* Add together the item counts */
5250 				object_absorb(j_ptr, o_ptr);
5251 
5252 				/* Delete the item */
5253 				delete_held_object(&p_ptr->inventory, o_ptr);
5254 
5255 				/* Window stuff */
5256 				p_ptr->window |= (PW_INVEN);
5257 
5258 				/* Done */
5259 				break;
5260 			}
5261 		}
5262 		OBJ_ITT_END;
5263 	}
5264 	OBJ_ITT_END;
5265 
5266 	/* Message */
5267 	if (flag) msgf("You combine some items in your pack.");
5268 
5269 	return (q_ptr);
5270 }
5271 
5272 
5273 /*
5274  * Combine items in the pack
5275  */
combine_pack(void)5276 void combine_pack(void)
5277 {
5278 	(void)combine_pack_aux(NULL);
5279 }
5280 
5281 /*
5282  * Combine items in the pack and keep track of an object
5283  */
combine_pack_watch(object_type * q_ptr)5284 object_type *combine_pack_watch(object_type *q_ptr)
5285 {
5286 	return (combine_pack_aux(q_ptr));
5287 }
5288 
5289 /*
5290  * Reorder items in the pack
5291  */
reorder_pack(void)5292 void reorder_pack(void)
5293 {
5294 	(void)reorder_objects_aux(NULL, reorder_pack_comp, p_ptr->inventory);
5295 
5296 	/* Window stuff */
5297 	p_ptr->window |= (PW_INVEN);
5298 }
5299 
5300 
5301 /*
5302  * Reorder items in the pack and return the watched object.
5303  */
reorder_pack_watch(object_type * o_ptr)5304 object_type *reorder_pack_watch(object_type *o_ptr)
5305 {
5306 	/* Window stuff */
5307 	p_ptr->window |= (PW_INVEN);
5308 
5309 	return (reorder_objects_aux(o_ptr, reorder_pack_comp, p_ptr->inventory));
5310 }
5311 
5312 
can_player_destroy_object(object_type * o_ptr)5313 bool can_player_destroy_object(object_type *o_ptr)
5314 {
5315 	/*
5316 	 * Artifacts cannot be destroyed
5317 	 * However only notice if we have not *id'ed* it.
5318 	 */
5319 	if (FLAG(o_ptr, TR_INSTA_ART))
5320 	{
5321 		if (!(o_ptr->info & OB_MENTAL))
5322 		{
5323 			byte feel = FEEL_SPECIAL;
5324 
5325 			/* Hack -- Handle icky artifacts */
5326 			if (cursed_p(o_ptr) || !o_ptr->cost) feel = FEEL_TERRIBLE;
5327 
5328 			/* Hack -- inscribe the artifact */
5329 			o_ptr->feeling = feel;
5330 
5331 			/* We have "felt" it (again) */
5332 			o_ptr->info |= (OB_SENSE);
5333 
5334 			/* Redraw equippy chars */
5335 			p_ptr->redraw |= (PR_EQUIPPY);
5336 
5337 			/* Notice changes */
5338 			notice_item();
5339 		}
5340 
5341 		/* Done */
5342 		return FALSE;
5343 	}
5344 
5345 	return TRUE;
5346 }
5347 
5348 
5349 /*
5350  * Hack -- display an object kind in the current window
5351  *
5352  * Include list of usable spells for readible books
5353  */
display_koff(int k_idx)5354 void display_koff(int k_idx)
5355 {
5356 	/* Get local object */
5357 	object_type *q_ptr;
5358 
5359 	/* Erase the window */
5360     clear_from(0);
5361 
5362 	/* No info */
5363 	if (!k_idx) return;
5364 
5365 	/* Prepare the object */
5366 	q_ptr = object_prep(k_idx);
5367 
5368 	/* Mention the object name */
5369 	prtf(0, 0, "%v", OBJECT_STORE_FMT(q_ptr, FALSE, 0));
5370 
5371 	/* Warriors are illiterate */
5372 	if (!(p_ptr->spell.r[0].realm || p_ptr->spell.r[1].realm)) return;
5373 
5374 	/* Display spells in readible books */
5375 	if ((q_ptr->tval == REALM1_BOOK) || (q_ptr->tval == REALM2_BOOK))
5376 	{
5377 		int sval;
5378 		int spell = -1;
5379 		int num = 0;
5380 		byte spells[PY_MAX_SPELLS];
5381 
5382 
5383 		/* Access the item's sval */
5384 		sval = q_ptr->sval;
5385 
5386 		/* Extract spells */
5387 		for (spell = 0; spell < 32; spell++)
5388 		{
5389 			/* Check for this spell */
5390 			if (fake_spell_flags[sval] & (1L << spell))
5391 			{
5392 				/* Collect this spell */
5393 				spells[num++] = spell;
5394 			}
5395 		}
5396 
5397 		/* Print spells */
5398 		print_spells(spells, num, 0, 2,
5399 					 ((q_ptr->tval == REALM1_BOOK) ?
5400 					 	 p_ptr->spell.r[0].realm - 1 : p_ptr->spell.r[1].realm - 1));
5401 	}
5402 }
5403