1 /**
2  * \file obj-gear.c
3  * \brief management of inventory, equipment and quiver
4  *
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  * Copyright (c) 2014 Nick McConnell
7  *
8  * This work is free software; you can redistribute it and/or modify it
9  * under the terms of either:
10  *
11  * a) the GNU General Public License as published by the Free Software
12  *    Foundation, version 2, or
13  *
14  * b) the "Angband licence":
15  *    This software may be copied and distributed for educational, research,
16  *    and not for profit purposes provided that this copyright and statement
17  *    are included in all such copies.  Other copyrights may also apply.
18  */
19 
20 #include "angband.h"
21 #include "cmd-core.h"
22 #include "game-event.h"
23 #include "init.h"
24 #include "obj-desc.h"
25 #include "obj-gear.h"
26 #include "obj-ignore.h"
27 #include "obj-knowledge.h"
28 #include "obj-pile.h"
29 #include "obj-tval.h"
30 #include "obj-util.h"
31 #include "player-calcs.h"
32 #include "player-util.h"
33 
34 static const struct slot_info {
35 	int index;
36 	bool acid_vuln;
37 	bool name_in_desc;
38 	const char *mention;
39 	const char *heavy_describe;
40 	const char *describe;
41 } slot_table[] = {
42 	#define EQUIP(a, b, c, d, e, f) { EQUIP_##a, b, c, d, e, f },
43 	#include "list-equip-slots.h"
44 	#undef EQUIP
45 	{ EQUIP_MAX, false, false, NULL, NULL, NULL }
46 };
47 
slot_by_name(struct player * p,const char * name)48 int slot_by_name(struct player *p, const char *name)
49 {
50 	int i;
51 
52 	/* Look for the correctly named slot */
53 	for (i = 0; i < p->body.count; i++) {
54 		if (streq(name, p->body.slots[i].name)) {
55 			break;
56 		}
57 	}
58 
59 	/* Index for that slot */
60 	return i;
61 }
62 
63 /**
64  * Gets a slot of the given type, preferentially empty unless full is true
65  */
slot_by_type(struct player * p,int type,bool full)66 int slot_by_type(struct player *p, int type, bool full)
67 {
68 	int i, fallback = p->body.count;
69 
70 	/* Look for a correct slot type */
71 	for (i = 0; i < p->body.count; i++) {
72 		if (type == p->body.slots[i].type) {
73 			if (full) {
74 				/* Found a full slot */
75 				if (p->body.slots[i].obj != NULL) break;
76 			} else {
77 				/* Found an empty slot */
78 				if (p->body.slots[i].obj == NULL) break;
79 			}
80 			/* Not right for full/empty, but still the right type */
81 			if (fallback == p->body.count)
82 				fallback = i;
83 		}
84 	}
85 
86 	/* Index for the best slot we found, or p->body.count if none found  */
87 	return (i != p->body.count) ? i : fallback;
88 }
89 
slot_type_is(int slot,int type)90 bool slot_type_is(int slot, int type)
91 {
92 	/* Assume default body if no player */
93 	struct player_body body = player ? player->body : bodies[0];
94 
95 	return body.slots[slot].type == type ? true : false;
96 }
97 
slot_object(struct player * p,int slot)98 struct object *slot_object(struct player *p, int slot)
99 {
100 	/* Ensure a valid body */
101 	if (p->body.slots && p->body.slots[slot].obj) {
102 		return p->body.slots[slot].obj;
103 	}
104 
105 	return NULL;
106 }
107 
equipped_item_by_slot_name(struct player * p,const char * name)108 struct object *equipped_item_by_slot_name(struct player *p, const char *name)
109 {
110 	/* Ensure a valid body */
111 	if (p->body.slots) {
112 		return slot_object(p, slot_by_name(p, name));
113 	}
114 
115 	return NULL;
116 }
117 
object_slot(struct player_body body,const struct object * obj)118 int object_slot(struct player_body body, const struct object *obj)
119 {
120 	int i;
121 
122 	for (i = 0; i < body.count; i++) {
123 		if (obj == body.slots[i].obj) {
124 			break;
125 		}
126 	}
127 
128 	return i;
129 }
130 
object_is_equipped(struct player_body body,const struct object * obj)131 bool object_is_equipped(struct player_body body, const struct object *obj)
132 {
133 	return object_slot(body, obj) < body.count;
134 }
135 
object_is_carried(struct player * p,const struct object * obj)136 bool object_is_carried(struct player *p, const struct object *obj)
137 {
138 	return pile_contains(p->gear, obj);
139 }
140 
141 /**
142  * Check if an object is in the quiver
143  */
object_is_in_quiver(struct player * p,const struct object * obj)144 static bool object_is_in_quiver(struct player *p, const struct object *obj)
145 {
146 	int i;
147 
148 	for (i = 0; i < z_info->quiver_size; i++) {
149 		if (obj == p->upkeep->quiver[i]) {
150 			return true;
151 		}
152 	}
153 
154 	return false;
155 }
156 
157 /**
158  * Calculate the number of pack slots used by the current gear.
159  *
160  * Note that this function does not check that there are adequate slots in the
161  * quiver, just the total quantity of missiles.
162  */
pack_slots_used(struct player * p)163 int pack_slots_used(struct player *p)
164 {
165 	struct object *obj;
166 	int i, pack_slots = 0;
167 	int quiver_ammo = 0;
168 
169 	for (obj = p->gear; obj; obj = obj->next) {
170 		bool found = false;
171 		/* Equipment doesn't count */
172 		if (!object_is_equipped(p->body, obj)) {
173 			/* Check if it is in the quiver */
174 			if (tval_is_ammo(obj) ||
175 					of_has(obj->flags, OF_THROWING)) {
176 				for (i = 0; i < z_info->quiver_size; i++) {
177 					if (p->upkeep->quiver[i] == obj) {
178 						quiver_ammo += obj->number *
179 							(tval_is_ammo(obj) ? 1 : 5);
180 						found = true;
181 						break;
182 					}
183 				}
184 			}
185 			if (!found) {
186 				/* Count regular slots */
187 				pack_slots++;
188 			}
189 		}
190 	}
191 
192 	/* Full slots */
193 	pack_slots += quiver_ammo / z_info->quiver_slot_size;
194 
195 	/* Plus one for any remainder */
196 	if (quiver_ammo % z_info->quiver_slot_size) {
197 		pack_slots++;
198 	}
199 
200 	return pack_slots;
201 }
202 
203 /*
204  * Return a string mentioning how a given item is carried
205  */
equip_mention(struct player * p,int slot)206 const char *equip_mention(struct player *p, int slot)
207 {
208 	int type = p->body.slots[slot].type;
209 
210 	/* Heavy */
211 	if ((type == EQUIP_WEAPON && p->state.heavy_wield) ||
212 			(type == EQUIP_WEAPON && p->state.heavy_shoot))
213 		return slot_table[type].heavy_describe;
214 	else if (slot_table[type].name_in_desc)
215 		return format(slot_table[type].mention, p->body.slots[slot].name);
216 	else
217 		return slot_table[type].mention;
218 }
219 
220 
221 /*
222  * Return a string describing how a given item is being worn.
223  * Currently, only used for items in the equipment, not inventory.
224  */
equip_describe(struct player * p,int slot)225 const char *equip_describe(struct player *p, int slot)
226 {
227 	int type = p->body.slots[slot].type;
228 
229 	/* Heavy */
230 	if ((type == EQUIP_WEAPON && p->state.heavy_wield) ||
231 			(type == EQUIP_WEAPON && p->state.heavy_shoot))
232 		return slot_table[type].heavy_describe;
233 	else if (slot_table[type].name_in_desc)
234 		return format(slot_table[type].describe, p->body.slots[slot].name);
235 	else
236 		return slot_table[type].describe;
237 }
238 
239 /**
240  * Determine which equipment slot (if any) an item likes. The slot might (or
241  * might not) be open, but it is a slot which the object could be equipped in.
242  *
243  * For items where multiple slots could work (e.g. rings), the function
244  * will try to return an open slot if possible.
245  */
wield_slot(const struct object * obj)246 int wield_slot(const struct object *obj)
247 {
248 	/* Slot for equipment */
249 	switch (obj->tval)
250 	{
251 		case TV_BOW: return slot_by_type(player, EQUIP_BOW, false);
252 		case TV_AMULET: return slot_by_type(player, EQUIP_AMULET, false);
253 		case TV_CLOAK: return slot_by_type(player, EQUIP_CLOAK, false);
254 		case TV_SHIELD: return slot_by_type(player, EQUIP_SHIELD, false);
255 		case TV_GLOVES: return slot_by_type(player, EQUIP_GLOVES, false);
256 		case TV_BOOTS: return slot_by_type(player, EQUIP_BOOTS, false);
257 	}
258 
259 	if (tval_is_melee_weapon(obj))
260 		return slot_by_type(player, EQUIP_WEAPON, false);
261 	else if (tval_is_ring(obj))
262 		return slot_by_type(player, EQUIP_RING, false);
263 	else if (tval_is_light(obj))
264 		return slot_by_type(player, EQUIP_LIGHT, false);
265 	else if (tval_is_body_armor(obj))
266 		return slot_by_type(player, EQUIP_BODY_ARMOR, false);
267 	else if (tval_is_head_armor(obj))
268 		return slot_by_type(player, EQUIP_HAT, false);
269 
270 	/* No slot available */
271 	return -1;
272 }
273 
274 
275 /**
276  * Acid has hit the player, attempt to affect some armor.
277  *
278  * Note that the "base armor" of an object never changes.
279  * If any armor is damaged (or resists), the player takes less damage.
280  */
minus_ac(struct player * p)281 bool minus_ac(struct player *p)
282 {
283 	int i, count = 0;
284 	struct object *obj = NULL;
285 
286 	/* Avoid crash during monster power calculations */
287 	if (!p->gear) return false;
288 
289 	/* Count the armor slots */
290 	for (i = 0; i < player->body.count; i++) {
291 		/* Ignore non-armor */
292 		if (slot_type_is(i, EQUIP_WEAPON)) continue;
293 		if (slot_type_is(i, EQUIP_BOW)) continue;
294 		if (slot_type_is(i, EQUIP_RING)) continue;
295 		if (slot_type_is(i, EQUIP_AMULET)) continue;
296 		if (slot_type_is(i, EQUIP_LIGHT)) continue;
297 
298 		/* Add */
299 		count++;
300 	}
301 
302 	/* Pick one at random */
303 	for (i = player->body.count - 1; i >= 0; i--) {
304 		/* Ignore non-armor */
305 		if (slot_type_is(i, EQUIP_WEAPON)) continue;
306 		if (slot_type_is(i, EQUIP_BOW)) continue;
307 		if (slot_type_is(i, EQUIP_RING)) continue;
308 		if (slot_type_is(i, EQUIP_AMULET)) continue;
309 		if (slot_type_is(i, EQUIP_LIGHT)) continue;
310 
311 		if (one_in_(count--)) break;
312 	}
313 
314 	/* Get the item */
315 	obj = slot_object(player, i);
316 
317 	/* If we can still damage the item */
318 	if (obj && (obj->ac + obj->to_a > 0)) {
319 		char o_name[80];
320 		object_desc(o_name, sizeof(o_name), obj, ODESC_BASE);
321 
322 		/* Object resists */
323 		if (obj->el_info[ELEM_ACID].flags & EL_INFO_IGNORE) {
324 			msg("Your %s is unaffected!", o_name);
325 		} else {
326 			msg("Your %s is damaged!", o_name);
327 
328 			/* Damage the item */
329 			obj->to_a--;
330 			if (p->obj_k->to_a)
331 				obj->known->to_a = obj->to_a;
332 
333 			p->upkeep->update |= (PU_BONUS);
334 			p->upkeep->redraw |= (PR_EQUIP);
335 		}
336 
337 		/* There was an effect */
338 		return true;
339 	} else {
340 		/* No damage or effect */
341 		return false;
342 	}
343 }
344 
345 /**
346  * Convert a gear object into a one character label.
347  */
gear_to_label(struct object * obj)348 char gear_to_label(struct object *obj)
349 {
350 	int i;
351 
352 	/* Equipment is easy */
353 	if (object_is_equipped(player->body, obj)) {
354 		return I2A(equipped_item_slot(player->body, obj));
355 	}
356 
357 	/* Check the quiver */
358 	for (i = 0; i < z_info->quiver_size; i++) {
359 		if (player->upkeep->quiver[i] == obj) {
360 			return I2D(i);
361 		}
362 	}
363 
364 	/* Check the inventory */
365 	for (i = 0; i < z_info->pack_size; i++) {
366 		if (player->upkeep->inven[i] == obj) {
367 			return I2A(i);
368 		}
369 	}
370 
371 	return '\0';
372 }
373 
374 /**
375  * Remove an object from the gear list, leaving it unattached
376  * \param obj the object being tested
377  * \return whether an object was removed
378  */
gear_excise_object(struct object * obj)379 static bool gear_excise_object(struct object *obj)
380 {
381 	int i;
382 
383 	pile_excise(&player->gear_k, obj->known);
384 	pile_excise(&player->gear, obj);
385 
386 	/* Change the weight */
387 	player->upkeep->total_weight -= (obj->number * obj->weight);
388 
389 	/* Make sure it isn't still equipped */
390 	for (i = 0; i < player->body.count; i++) {
391 		if (slot_object(player, i) == obj) {
392 			player->body.slots[i].obj = NULL;
393 			player->upkeep->equip_cnt--;
394 		}
395 	}
396 
397 	/* Update the gear */
398 	calc_inventory(player->upkeep, player->gear, player->body);
399 
400 	/* Housekeeping */
401 	player->upkeep->update |= (PU_BONUS);
402 	player->upkeep->notice |= (PN_COMBINE);
403 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP);
404 
405 	return true;
406 }
407 
gear_last_item(void)408 struct object *gear_last_item(void)
409 {
410 	return pile_last_item(player->gear);
411 }
412 
gear_insert_end(struct object * obj)413 void gear_insert_end(struct object *obj)
414 {
415 	pile_insert_end(&player->gear, obj);
416 	pile_insert_end(&player->gear_k, obj->known);
417 }
418 
419 /**
420  * Remove an amount of an object from the inventory or quiver, returning
421  * a detached object which can be used.
422  *
423  * Optionally describe what remains.
424  */
gear_object_for_use(struct object * obj,int num,bool message,bool * none_left)425 struct object *gear_object_for_use(struct object *obj, int num, bool message,
426 								   bool *none_left)
427 {
428 	struct object *usable;
429 	char name[80];
430 	char label = gear_to_label(obj);
431 	bool artifact = (obj->known->artifact != NULL);
432 
433 	/* Bounds check */
434 	num = MIN(num, obj->number);
435 
436 	/* Split off a usable object if necessary */
437 	if (obj->number > num) {
438 		usable = object_split(obj, num);
439 
440 		/* Change the weight */
441 		player->upkeep->total_weight -= (num * obj->weight);
442 
443 		if (message) {
444 			object_desc(name, sizeof(name), obj, ODESC_PREFIX | ODESC_FULL);
445 		}
446 	} else {
447 		if (message) {
448 			if (artifact) {
449 				object_desc(name, sizeof(name), obj, ODESC_FULL | ODESC_SINGULAR);
450 			} else {
451 				/* Describe zero amount */
452 				obj->number = 0;
453 				object_desc(name, sizeof(name), obj, ODESC_PREFIX | ODESC_FULL);
454 				obj->number = num;
455 			}
456 		}
457 
458 		/* We're using the entire stack */
459 		usable = obj;
460 		gear_excise_object(usable);
461 		*none_left = true;
462 
463 		/* Stop tracking item */
464 		if (tracked_object_is(player->upkeep, obj))
465 			track_object(player->upkeep, NULL);
466 
467 		/* Inventory has changed, so disable repeat command */
468 		cmd_disable_repeat();
469 	}
470 
471 	/* Housekeeping */
472 	player->upkeep->update |= (PU_BONUS);
473 	player->upkeep->notice |= (PN_COMBINE);
474 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP);
475 
476 	/* Print a message if desired */
477 	if (message) {
478 		if (artifact)
479 			msg("You no longer have the %s (%c).", name, label);
480 		else
481 			msg("You have %s (%c).", name, label);
482 	}
483 
484 	return usable;
485 }
486 
487 /**
488  * Check how many missiles can be put in the quiver without increasing the
489  * number of pack slots used.
490  *
491  * Returns the quantity from a given stack of missiles that can be added.
492  */
quiver_absorb_num(const struct object * obj)493 static int quiver_absorb_num(const struct object *obj)
494 {
495 	bool ammo = tval_is_ammo(obj);
496 	bool throwing = of_has(obj->flags, OF_THROWING);
497 
498 	/* Must be ammo or good for throwing */
499 	if (ammo || throwing) {
500 		int i, quiver_count = 0, space_free = 0;
501 
502 		/* Count the current space this object could go into */
503 		for (i = 0; i < z_info->quiver_size; i++) {
504 			struct object *quiver_obj = player->upkeep->quiver[i];
505 			if (quiver_obj) {
506 				int mult = tval_is_ammo(quiver_obj) ? 1 : 5;
507 
508 				quiver_count += quiver_obj->number * mult;
509 				if (object_stackable(quiver_obj, obj, OSTACK_PACK))
510 					space_free += z_info->quiver_slot_size
511 						- quiver_obj->number * mult;
512 			} else if (ammo) {
513 				space_free += z_info->quiver_slot_size;
514 			} else if (obj->note) {
515 				/*
516 				 * Per calc_inventory(), throwing weapons
517 				 * which aren't also ammo will be added to the
518 				 * quiver if inscribed to go into an unoccupied
519 				 * slot.  The inscription test should match what
520 				 * calc_inventory() uses.
521 				 */
522 				const char *s = strchr(quark_str(obj->note), '@');
523 
524 				if (s && (s[1] == 'f' || s[1] == 'v') &&
525 						s[2] - '0' == i) {
526 					space_free += z_info->quiver_slot_size;
527 				}
528 			}
529 		}
530 
531 		if (space_free) {
532 			/* Check we won't need another pack slot */
533 			quiver_count += z_info->quiver_slot_size;
534 			while (quiver_count > z_info->quiver_slot_size)
535 				quiver_count -= z_info->quiver_slot_size;
536 
537 			/* Return the number, or the number that will fit */
538 			space_free = MIN(space_free, z_info->quiver_slot_size - quiver_count);
539 			return MIN(obj->number,
540 				space_free / (tval_is_ammo(obj) ? 1 : 5));
541 		}
542 	}
543 
544 	/* No ammo or no space */
545 	return 0;
546 }
547 
548 /**
549  * Calculate how much of an item is can be carried in the inventory or quiver.
550  *
551  * Optionally only return a positive value if there is already a similar object.
552  */
inven_carry_num(const struct object * obj,bool stack)553 int inven_carry_num(const struct object *obj, bool stack)
554 {
555 	/* Check for similarity */
556 	if (stack) {
557 		struct object *gear_obj;
558 
559 		for (gear_obj = player->gear; gear_obj; gear_obj = gear_obj->next) {
560 			if (!object_is_equipped(player->body, gear_obj) &&
561 					object_stackable(gear_obj, obj, OSTACK_PACK)) {
562 				break;
563 			}
564 		}
565 
566 		/* No similar object, so no stacking */
567 		if (!gear_obj) {
568 			return 0;
569 		}
570 	}
571 
572 	/* Free inventory slots, so there is definitely room */
573 	if (pack_slots_used(player) < z_info->pack_size) {
574 		return obj->number;
575 	} else {
576 		int i;
577 
578 		/* Absorb as many as we can in the quiver */
579 		int num_left = obj->number - quiver_absorb_num(obj);
580 
581 		/* See if we can add to a part full inventory slot */
582 		for (i = 0; i < z_info->pack_size; i++) {
583 			struct object *inven_obj = player->upkeep->inven[i];
584 			if (inven_obj && object_stackable(inven_obj, obj, OSTACK_PACK)) {
585 				num_left -= inven_obj->kind->base->max_stack - inven_obj->number;
586 			}
587 		}
588 
589 		/* Return the number we can absorb */
590 		return obj->number - MAX(num_left, 0);
591 	}
592 }
593 
594 /**
595  * Check if we have space for some of an item in the pack, optionally requiring
596  * stacking
597  */
inven_carry_okay(const struct object * obj)598 bool inven_carry_okay(const struct object *obj)
599 {
600 	return inven_carry_num(obj, false) > 0;
601 }
602 
603 /**
604  * Describe the charges on an item in the inventory.
605  */
inven_item_charges(struct object * obj)606 void inven_item_charges(struct object *obj)
607 {
608 	/* Require staff/wand */
609 	if (tval_can_have_charges(obj) && object_flavor_is_aware(obj)) {
610 		msg("You have %d charge%s remaining.",
611 				obj->pval,
612 				PLURAL(obj->pval));
613 	}
614 }
615 
616 /**
617  * Add an item to the players inventory.
618  *
619  * If the new item can combine with an existing item in the inventory,
620  * it will do so, using object_similar() and object_absorb(), else,
621  * the item will be placed into the first available gear array index.
622  *
623  * This function can be used to "over-fill" the player's pack, but only
624  * once, and such an action must trigger the "overflow" code immediately.
625  * Note that when the pack is being "over-filled", the new item must be
626  * placed into the "overflow" slot, and the "overflow" must take place
627  * before the pack is reordered, but (optionally) after the pack is
628  * combined.  This may be tricky.  See "dungeon.c" for info.
629  *
630  * Note that this code removes any location information from the object once
631  * it is placed into the inventory, but takes no responsibility for removing
632  * the object from any other pile it was in.
633  */
inven_carry(struct player * p,struct object * obj,bool absorb,bool message)634 void inven_carry(struct player *p, struct object *obj, bool absorb,
635 				 bool message)
636 {
637 	bool combining = false;
638 
639 	/* Check for combining, if appropriate */
640 	if (absorb) {
641 		struct object *combine_item = NULL;
642 
643 		struct object *gear_obj = p->gear;
644 		while ((combine_item == NULL) && (gear_obj != NULL)) {
645 			if (!object_is_equipped(p->body, gear_obj) &&
646 					object_similar(gear_obj, obj, OSTACK_PACK)) {
647 				combine_item = gear_obj;
648 			}
649 
650 			gear_obj = gear_obj->next;
651 		}
652 
653 		if (combine_item) {
654 			/* Increase the weight */
655 			p->upkeep->total_weight += (obj->number * obj->weight);
656 
657 			/* Combine the items, and their known versions */
658 			object_absorb(combine_item->known, obj->known);
659 			obj->known = NULL;
660 			object_absorb(combine_item, obj);
661 
662 			/* Ensure numbers are aligned (should not be necessary, but safe) */
663 			combine_item->known->number = combine_item->number;
664 
665 			obj = combine_item;
666 			combining = true;
667 		}
668 	}
669 
670 	/* We didn't manage the find an object to combine with */
671 	if (!combining) {
672 		/* Paranoia */
673 		assert(pack_slots_used(p) <= z_info->pack_size);
674 
675 		gear_insert_end(obj);
676 		apply_autoinscription(obj);
677 
678 		/* Remove cave object details */
679 		obj->held_m_idx = 0;
680 		obj->grid = loc(0, 0);
681 		obj->known->grid = loc(0, 0);
682 
683 		/* Update the inventory */
684 		p->upkeep->total_weight += (obj->number * obj->weight);
685 		p->upkeep->notice |= (PN_COMBINE);
686 
687 		/* Hobbits ID mushrooms on pickup, gnomes ID wands and staffs on pickup */
688 		if (!object_flavor_is_aware(obj)) {
689 			if (player_has(player, PF_KNOW_MUSHROOM) && tval_is_mushroom(obj)) {
690 				object_flavor_aware(obj);
691 				msg("Mushrooms for breakfast!");
692 			} else if (player_has(player, PF_KNOW_ZAPPER) && tval_is_zapper(obj))
693 				object_flavor_aware(obj);
694 		}
695 	}
696 
697 	p->upkeep->update |= (PU_BONUS | PU_INVEN);
698 	p->upkeep->redraw |= (PR_INVEN);
699 	update_stuff(player);
700 
701 	if (message) {
702 		char o_name[80];
703 		object_desc(o_name, sizeof(o_name), obj, ODESC_PREFIX | ODESC_FULL);
704 		msg("You have %s (%c).", o_name, gear_to_label(obj));
705 	}
706 
707 	if (object_is_in_quiver(p, obj))
708 		sound(MSG_QUIVER);
709 }
710 
711 
712 /**
713  * Wield or wear a single item from the pack or floor
714  */
inven_wield(struct object * obj,int slot)715 void inven_wield(struct object *obj, int slot)
716 {
717 	struct object *wielded, *old = player->body.slots[slot].obj;
718 
719 	const char *fmt;
720 	char o_name[80];
721 	bool dummy = false;
722 
723 	/* Increase equipment counter if empty slot */
724 	if (old == NULL)
725 		player->upkeep->equip_cnt++;
726 
727 	/* Take a turn */
728 	player->upkeep->energy_use = z_info->move_energy;
729 
730 	/* It's either a gear object or a floor object */
731 	if (object_is_carried(player, obj)) {
732 		/* Split off a new object if necessary */
733 		if (obj->number > 1) {
734 			wielded = gear_object_for_use(obj, 1, false, &dummy);
735 
736 			/* The new item needs new gear and known gear entries */
737 			wielded->next = obj->next;
738 			obj->next = wielded;
739 			wielded->prev = obj;
740 			if (wielded->next)
741 				(wielded->next)->prev = wielded;
742 			wielded->known->next = obj->known->next;
743 			obj->known->next = wielded->known;
744 			wielded->known->prev = obj->known;
745 			if (wielded->known->next)
746 				(wielded->known->next)->prev = wielded->known;
747 		} else {
748 			/* Just use the object directly */
749 			wielded = obj;
750 		}
751 	} else {
752 		/* Get a floor item and carry it */
753 		wielded = floor_object_for_use(obj, 1, false, &dummy);
754 		inven_carry(player, wielded, false, false);
755 	}
756 
757 	/* Wear the new stuff */
758 	player->body.slots[slot].obj = wielded;
759 
760 	/* Do any ID-on-wield */
761 	object_learn_on_wield(player, wielded);
762 
763 	/* Where is the item now */
764 	if (tval_is_melee_weapon(wielded))
765 		fmt = "You are wielding %s (%c).";
766 	else if (wielded->tval == TV_BOW)
767 		fmt = "You are shooting with %s (%c).";
768 	else if (tval_is_light(wielded))
769 		fmt = "Your light source is %s (%c).";
770 	else
771 		fmt = "You are wearing %s (%c).";
772 
773 	/* Describe the result */
774 	object_desc(o_name, sizeof(o_name), wielded, ODESC_PREFIX | ODESC_FULL);
775 
776 	/* Message */
777 	msgt(MSG_WIELD, fmt, o_name, I2A(slot));
778 
779 	/* Sticky flag geats a special mention */
780 	if (of_has(wielded->flags, OF_STICKY)) {
781 		/* Warn the player */
782 		msgt(MSG_CURSED, "Oops! It feels deathly cold!");
783 	}
784 
785 	/* See if we have to overflow the pack */
786 	combine_pack();
787 	pack_overflow(old);
788 
789 	/* Recalculate bonuses, torch, mana, gear */
790 	player->upkeep->notice |= (PN_IGNORE);
791 	player->upkeep->update |= (PU_BONUS | PU_INVEN | PU_UPDATE_VIEW);
792 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP | PR_ARMOR);
793 	player->upkeep->redraw |= (PR_STATS | PR_HP | PR_MANA | PR_SPEED);
794 	update_stuff(player);
795 
796 	/* Disable repeats */
797 	cmd_disable_repeat();
798 }
799 
800 
801 /**
802  * Take off a non-cursed equipment item
803  *
804  * Note that taking off an item when "full" may cause that item
805  * to fall to the ground.
806  *
807  * Note also that this function does not try to combine the taken off item
808  * with other inventory items - that must be done by the calling function.
809  */
inven_takeoff(struct object * obj)810 void inven_takeoff(struct object *obj)
811 {
812 	int slot = equipped_item_slot(player->body, obj);
813 	const char *act;
814 	char o_name[80];
815 
816 	/* Paranoia */
817 	if (slot == player->body.count) return;
818 
819 	/* Describe the object */
820 	object_desc(o_name, sizeof(o_name), obj, ODESC_PREFIX | ODESC_FULL);
821 
822 	/* Describe removal by slot */
823 	if (slot_type_is(slot, EQUIP_WEAPON))
824 		act = "You were wielding";
825 	else if (slot_type_is(slot, EQUIP_BOW))
826 		act = "You were holding";
827 	else if (slot_type_is(slot, EQUIP_LIGHT))
828 		act = "You were holding";
829 	else
830 		act = "You were wearing";
831 
832 	/* De-equip the object */
833 	player->body.slots[slot].obj = NULL;
834 	player->upkeep->equip_cnt--;
835 
836 	player->upkeep->update |= (PU_BONUS | PU_INVEN | PU_UPDATE_VIEW);
837 	player->upkeep->notice |= (PN_IGNORE);
838 	update_stuff(player);
839 
840 	/* Message */
841 	msgt(MSG_WIELD, "%s %s (%c).", act, o_name, gear_to_label(obj));
842 
843 	return;
844 }
845 
846 
847 /**
848  * Drop (some of) a non-cursed inventory/equipment item "near" the current
849  * location
850  *
851  * There are two cases here - a single object or entire stack is being dropped,
852  * or part of a stack is being split off and dropped
853  */
inven_drop(struct object * obj,int amt)854 void inven_drop(struct object *obj, int amt)
855 {
856 	struct object *dropped;
857 	bool none_left = false;
858 	bool quiver = false;
859 
860 	char name[80];
861 	char label;
862 
863 	/* Error check */
864 	if (amt <= 0)
865 		return;
866 
867 	/* Check it is still held, in case there were two drop commands queued
868 	 * for this item.  This is in theory not ideal, but in practice should
869 	 * be safe. */
870 	if (!object_is_carried(player, obj))
871 		return;
872 
873 	/* Get where the object is now */
874 	label = gear_to_label(obj);
875 
876 	/* Is it in the quiver? */
877 	if (object_is_in_quiver(player, obj))
878 		quiver = true;
879 
880 	/* Not too many */
881 	if (amt > obj->number) amt = obj->number;
882 
883 	/* Take off equipment, don't combine */
884 	if (object_is_equipped(player->body, obj))
885 		inven_takeoff(obj);
886 
887 	/* Get the object */
888 	dropped = gear_object_for_use(obj, amt, false, &none_left);
889 
890 	/* Describe the dropped object */
891 	object_desc(name, sizeof(name), dropped, ODESC_PREFIX | ODESC_FULL);
892 
893 	/* Message */
894 	msg("You drop %s (%c).", name, label);
895 
896 	/* Describe what's left */
897 	if (dropped->artifact) {
898 		object_desc(name, sizeof(name), dropped,
899 					ODESC_FULL | ODESC_SINGULAR);
900 		msg("You no longer have the %s (%c).", name, label);
901 	} else if (none_left) {
902 		/* Play silly games to get the right description */
903 		int number = dropped->number;
904 		dropped->number = 0;
905 		object_desc(name, sizeof(name), dropped, ODESC_PREFIX | ODESC_FULL);
906 		msg("You have %s (%c).", name, label);
907 		dropped->number = number;
908 	} else {
909 		object_desc(name, sizeof(name), obj, ODESC_PREFIX | ODESC_FULL);
910 		msg("You have %s (%c).", name, label);
911 	}
912 
913 	/* Drop it near the player */
914 	drop_near(cave, &dropped, 0, player->grid, false, true);
915 
916 	/* Sound for quiver objects */
917 	if (quiver)
918 		sound(MSG_QUIVER);
919 
920 	event_signal(EVENT_INVENTORY);
921 	event_signal(EVENT_EQUIPMENT);
922 }
923 
924 
925 /**
926  * Return whether each stack of objects can be merged into two uneven stacks.
927  */
inven_can_stack_partial(const struct object * obj1,const struct object * obj2,object_stack_t mode)928 static bool inven_can_stack_partial(const struct object *obj1,
929 									const struct object *obj2,
930 									object_stack_t mode)
931 {
932 	if (!(mode & OSTACK_STORE)) {
933 		int total = obj1->number + obj2->number;
934 		int remainder = total - obj1->kind->base->max_stack;
935 
936 		if (remainder > obj1->kind->base->max_stack)
937 			return false;
938 	}
939 
940 	return object_stackable(obj1, obj2, mode);
941 }
942 
943 
944 /**
945  * Combine items in the pack, confirming no blank objects or gold
946  */
combine_pack(void)947 void combine_pack(void)
948 {
949 	struct object *obj1, *obj2, *prev;
950 	bool display_message = false;
951 
952 	/* Combine the pack (backwards) */
953 	obj1 = gear_last_item();
954 	while (obj1) {
955 		assert(obj1->kind);
956 		assert(!tval_is_money(obj1));
957 		prev = obj1->prev;
958 
959 		/* Scan the items above that item */
960 		for (obj2 = player->gear; obj2 && obj2 != obj1; obj2 = obj2->next) {
961 			assert(obj2->kind);
962 
963 			/* Can we drop "obj1" onto "obj2"? */
964 			if (object_similar(obj2, obj1, OSTACK_PACK)) {
965 				display_message = true;
966 				object_absorb(obj2->known, obj1->known);
967 				obj1->known = NULL;
968 				object_absorb(obj2, obj1);
969 
970 				/* Ensure numbers align (should not be necessary, but safer) */
971 				obj2->known->number = obj2->number;
972 
973 				break;
974 			} else if (inven_can_stack_partial(obj2, obj1, OSTACK_PACK)) {
975 				/* Setting this to true spams the combine message. */
976 				display_message = false;
977 				object_absorb_partial(obj2->known, obj1->known);
978 				object_absorb_partial(obj2, obj1);
979 
980 				/* Ensure numbers align (should not be necessary, but safer) */
981 				obj2->known->number = obj2->number;
982 				obj1->known->number = obj1->number;
983 
984 				break;
985 			}
986 		}
987 		obj1 = prev;
988 	}
989 
990 	calc_inventory(player->upkeep, player->gear, player->body);
991 
992 	/* Redraw gear */
993 	event_signal(EVENT_INVENTORY);
994 	event_signal(EVENT_EQUIPMENT);
995 
996 	/* Message */
997 	if (display_message) {
998 		msg("You combine some items in your pack.");
999 
1000 		/* Stop "repeat last command" from working. */
1001 		cmd_disable_repeat();
1002 	}
1003 }
1004 
1005 /**
1006  * Returns whether the pack is holding the maximum number of items.
1007  */
pack_is_full(void)1008 bool pack_is_full(void)
1009 {
1010 	return pack_slots_used(player) == z_info->pack_size;
1011 }
1012 
1013 /**
1014  * Returns whether the pack is holding the more than the maximum number of
1015  * items. If this is true, calling pack_overflow() will trigger a pack overflow.
1016  */
pack_is_overfull(void)1017 bool pack_is_overfull(void)
1018 {
1019 	return pack_slots_used(player) > z_info->pack_size;
1020 }
1021 
1022 /**
1023  * Overflow an item from the pack, if it is overfull.
1024  */
pack_overflow(struct object * obj)1025 void pack_overflow(struct object *obj)
1026 {
1027 	int i;
1028 	char o_name[80];
1029 	bool artifact = false;
1030 
1031 	if (!pack_is_overfull()) return;
1032 
1033 	/* Disturbing */
1034 	disturb(player);
1035 
1036 	/* Warning */
1037 	msg("Your pack overflows!");
1038 
1039 	/* Get the last proper item */
1040 	for (i = 1; i <= z_info->pack_size; i++)
1041 		if (!player->upkeep->inven[i])
1042 			break;
1043 
1044 	/* Drop the last inventory item unless requested otherwise */
1045 	if (!obj) {
1046 		obj = player->upkeep->inven[i - 1];
1047 	}
1048 
1049 	/* Rule out weirdness (like pack full, but inventory empty) */
1050 	assert(obj != NULL);
1051 
1052 	/* Describe */
1053 	object_desc(o_name, sizeof(o_name), obj, ODESC_PREFIX | ODESC_FULL);
1054 	if (obj->artifact) {
1055 		artifact = true;
1056 	}
1057 
1058 	/* Message */
1059 	msg("You drop %s.", o_name);
1060 
1061 	/* Excise the object and drop it (carefully) near the player */
1062 	gear_excise_object(obj);
1063 	drop_near(cave, &obj, 0, player->grid, false, true);
1064 
1065 	/* Describe */
1066 	if (artifact)
1067 		msg("You no longer have the %s.", o_name);
1068 	else
1069 		msg("You no longer have %s.", o_name);
1070 
1071 	/* Notice, update, redraw */
1072 	if (player->upkeep->notice) notice_stuff(player);
1073 	if (player->upkeep->update) update_stuff(player);
1074 	if (player->upkeep->redraw) redraw_stuff(player);
1075 }
1076