1 /**
2  * \file cmd-pickup.c
3  * \brief Pickup code
4  *
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke,
6  * Copyright (c) 2007 Leon Marrick
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 "cave.h"
22 #include "cmds.h"
23 #include "game-event.h"
24 #include "game-input.h"
25 #include "generate.h"
26 #include "init.h"
27 #include "mon-lore.h"
28 #include "mon-timed.h"
29 #include "mon-util.h"
30 #include "obj-desc.h"
31 #include "obj-gear.h"
32 #include "obj-ignore.h"
33 #include "obj-pile.h"
34 #include "obj-tval.h"
35 #include "obj-util.h"
36 #include "player-attack.h"
37 #include "player-calcs.h"
38 #include "player-history.h"
39 #include "player-util.h"
40 #include "trap.h"
41 
42 /**
43  * Pick up all gold at the player's current location.
44  */
player_pickup_gold(struct player * p)45 static void player_pickup_gold(struct player *p)
46 {
47 	s32b total_gold = 0L;
48 	char name[30] = "";
49 
50 	struct object *obj = square_object(cave, p->grid), *next;
51 
52 	int sound_msg;
53 	bool verbal = false;
54 	bool at_most_one = true;
55 
56 	/* Pick up all the ordinary gold objects */
57 	while (obj) {
58 		struct object_kind *kind = NULL;
59 
60 		/* Get next object */
61 		next = obj->next;
62 
63 		/* Ignore if not legal treasure */
64 		kind = lookup_kind(obj->tval, obj->sval);
65 		if (!tval_is_money(obj) || !kind) {
66 			obj = next;
67 			continue;
68 		}
69 
70 		/* Multiple types if we have a second name, otherwise record the name */
71 		if (total_gold && !streq(kind->name, name))
72 			at_most_one = false;
73 		else
74 			my_strcpy(name, kind->name, sizeof(name));
75 
76 		/* Remember whether feedback message is in order */
77 		if (!ignore_item_ok(obj))
78 			verbal = true;
79 
80 		/* Increment total value */
81 		total_gold += (s32b)obj->pval;
82 
83 		/* Delete the gold */
84 		if (obj->known) {
85 			square_delete_object(p->cave, p->grid, obj->known, false, false);
86 		}
87 		square_delete_object(cave, p->grid, obj, false, false);
88 		obj = next;
89 	}
90 
91 	/* Pick up the gold, if present */
92 	if (total_gold) {
93 		char buf[100];
94 
95 		/* Build a message */
96 		(void)strnfmt(buf, sizeof(buf),
97 					  "You have found %d gold pieces worth of ", total_gold);
98 
99 		/* One treasure type.. */
100 		if (at_most_one)
101 			my_strcat(buf, name, sizeof(buf));
102 		/* ... or more */
103 		else
104 			my_strcat(buf, "treasures", sizeof(buf));
105 		my_strcat(buf, ".", sizeof(buf));
106 
107 		/* Determine which sound to play */
108 		if      (total_gold < 200) sound_msg = MSG_MONEY1;
109 		else if (total_gold < 600) sound_msg = MSG_MONEY2;
110 		else                       sound_msg = MSG_MONEY3;
111 
112 		/* Display the message */
113 		if (verbal)
114 			msgt(sound_msg, "%s", buf);
115 
116 		/* Add gold to purse */
117 		p->au += total_gold;
118 
119 		/* Redraw gold */
120 		p->upkeep->redraw |= (PR_GOLD);
121 	}
122 }
123 
124 
125 /**
126  * Find the specified object in the inventory (not equipment)
127  */
find_stack_object_in_inventory(const struct object * obj,const struct object * start)128 static const struct object *find_stack_object_in_inventory(const struct object *obj, const struct object *start)
129 {
130 	const struct object *gear_obj;
131 	for (gear_obj = (start) ? start : player->gear; gear_obj; gear_obj = gear_obj->next) {
132 		if (!object_is_equipped(player->body, gear_obj) &&
133 				object_stackable(gear_obj, obj, OSTACK_PACK)) {
134 			/* We found the object */
135 			return gear_obj;
136 		}
137 	}
138 
139 	return NULL;
140 }
141 
142 
143 /**
144  * Determine if an object can be picked up automatically and return the
145  * number to pick up.
146  */
auto_pickup_okay(const struct object * obj)147 static int auto_pickup_okay(const struct object *obj)
148 {
149         /*
150 	 * Use the following inscriptions to guide pickup with the last one
151 	 * borrowed from Unangband:
152 	 *
153 	 * !g     don't pickup
154 	 * =g     pickup
155 	 * =g<n>  (i.e. =g5) pick up if have less than n
156 	 *
157 	 * !g takes precedence over any of the others if an object is
158 	 * inscribed with it and any of the others.  =g with no value takes
159 	 * precedence over =g<n> if an object is inscribed with both.  In
160 	 * general, inscriptions on the item on the floor are examined first
161 	 * and the ones on a matching item in the pack will only come into
162 	 * consideration if those on the item on the floor do not force or
163 	 * reject pickup.  When examining inscriptions in the pack, only
164 	 * use those on the first stack.
165 	 *
166 	 * The player option to always pick up overrides all of those
167 	 * inscriptions.  The player option to pickup if in the inventory
168 	 * honors those inscriptions.
169 	 */
170 	int num = inven_carry_num(obj, false);
171 	unsigned obj_has_auto, obj_has_maxauto;
172 	int obj_maxauto;
173 
174 	if (!num) return 0;
175 
176 	if (OPT(player, pickup_always)) return num;
177 	if (check_for_inscrip(obj, "!g")) return 0;
178 
179 	obj_has_auto = check_for_inscrip(obj, "=g");
180 	obj_maxauto = INT_MAX;
181 	obj_has_maxauto = check_for_inscrip_with_int(obj, "=g", &obj_maxauto);
182 	if (obj_has_auto > obj_has_maxauto) return num;
183 
184 	if (OPT(player, pickup_inven) || obj_has_maxauto) {
185 		const struct object *gear_obj = find_stack_object_in_inventory(obj, NULL);
186 		if (!gear_obj) {
187 			if (obj_has_maxauto) {
188 				return (num < obj_maxauto) ? num : obj_maxauto;
189 			}
190 			return 0;
191 		}
192 		if (!check_for_inscrip(gear_obj, "!g")) {
193 			unsigned int gear_has_auto = check_for_inscrip(gear_obj, "=g");
194 			unsigned int gear_has_maxauto;
195 			int gear_maxauto;
196 
197 			gear_has_maxauto = check_for_inscrip_with_int(gear_obj, "=g", &gear_maxauto);
198 			if (gear_has_auto > gear_has_maxauto) {
199 				return num;
200 			}
201 			if (obj_has_maxauto || gear_has_maxauto) {
202 				/* Use the pack inscription if have both. */
203 				int max_num = (gear_has_maxauto) ?
204 					gear_maxauto : obj_maxauto;
205 				/* Determine the total number in the pack. */
206 				int pack_num = gear_obj->number;
207 
208 				while (1) {
209 					if (!gear_obj->next) {
210 						break;
211 					}
212 					gear_obj = find_stack_object_in_inventory(obj, gear_obj->next);
213 					if (!gear_obj) {
214 						break;
215 					}
216 					pack_num += gear_obj->number;
217 				}
218 				if (pack_num >= max_num) {
219 					return 0;
220 				}
221 				return (num < max_num - pack_num) ?
222 					num : max_num - pack_num;
223 			}
224 			return num;
225 		}
226 	}
227 
228 	return 0;
229 }
230 
231 
232 /**
233  * Move an object from a floor pile to the player's gear, checking first
234  * whether partial pickup is needed
235  */
player_pickup_aux(struct player * p,struct object * obj,int auto_max,bool domsg)236 static void player_pickup_aux(struct player *p, struct object *obj,
237 							  int auto_max, bool domsg)
238 {
239 	int max = inven_carry_num(obj, false);
240 
241 	/* Confirm at least some of the object can be picked up */
242 	if (max == 0)
243 		quit_fmt("Failed pickup of %s", obj->kind->name);
244 
245 	/* Set ignore status */
246 	p->upkeep->notice |= PN_IGNORE;
247 
248 	/* Allow auto-pickup to limit the number if it wants to */
249 	if (auto_max && max > auto_max) {
250 		max = auto_max;
251 	}
252 
253 	/* Carry the object, prompting for number if necessary */
254 	if (max == obj->number) {
255 		if (obj->known) {
256 			square_excise_object(p->cave, p->grid, obj->known);
257 			delist_object(p->cave, obj->known);
258 		}
259 		square_excise_object(cave, p->grid, obj);
260 		delist_object(cave, obj);
261 		inven_carry(p, obj, true, domsg);
262 	} else {
263 		int num;
264 		bool dummy;
265 		struct object *picked_up;
266 
267 		if (auto_max)
268 			num = auto_max;
269 		else
270 			num = get_quantity(NULL, max);
271 		if (!num) return;
272 		picked_up = floor_object_for_use(obj, num, false, &dummy);
273 		inven_carry(p, picked_up, true, domsg);
274 	}
275 }
276 
277 /**
278  * Pick up objects and treasure on the floor.  -LM-
279  *
280  * Scan the list of objects in that floor grid. Pick up gold automatically.
281  * Pick up objects automatically until backpack space is full if
282  * auto-pickup option is on, otherwise, store objects on
283  * floor in an array, and tally both how many there are and can be picked up.
284  *
285  * If not picking up anything, indicate objects on the floor.  Do the same
286  * thing if we don't have room for anything.
287  *
288  * Pick up multiple objects using Tim Baker's menu system.   Recursively
289  * call this function (forcing menus for any number of objects) until
290  * objects are gone, backpack is full, or player is satisfied.
291  *
292  * We keep track of number of objects picked up to calculate time spent.
293  * This tally is incremented even for automatic pickup, so we are careful
294  * (in "dungeon.c" and elsewhere) to handle pickup as either a separate
295  * automated move or a no-cost part of the stay still or 'g'et command.
296  *
297  * Note the lack of chance for the character to be disturbed by unmarked
298  * objects.  They are truly "unknown".
299  *
300  * \param obj is the object to pick up.
301  * \param menu is whether to present a menu to the player
302  */
player_pickup_item(struct player * p,struct object * obj,bool menu)303 static byte player_pickup_item(struct player *p, struct object *obj, bool menu)
304 {
305 	struct object *current = NULL;
306 
307 	int floor_max = z_info->floor_size + 1;
308 	struct object **floor_list = mem_zalloc(floor_max * sizeof(*floor_list));
309 	int floor_num = 0;
310 
311 	int i;
312 	int can_pickup = 0;
313 	bool call_function_again = false;
314 
315 	bool domsg = true;
316 
317 	/* Objects picked up.  Used to determine time cost of command. */
318 	byte objs_picked_up = 0;
319 
320 	/* Always know what's on the floor */
321 	square_know_pile(cave, p->grid);
322 
323 	/* Always pickup gold, effortlessly */
324 	player_pickup_gold(p);
325 
326 	/* Nothing else to pick up -- return */
327 	if (!square_object(cave, p->grid)) {
328 		mem_free(floor_list);
329 		return objs_picked_up;
330 	}
331 
332 	/* We're given an object - pick it up */
333 	if (obj) {
334 		player_pickup_aux(p, obj, 0, domsg);
335 		objs_picked_up = 1;
336 		mem_free(floor_list);
337 		return objs_picked_up;
338 	}
339 
340 	/* Tally objects that can be at least partially picked up.*/
341 	floor_num = scan_floor(floor_list, floor_max, OFLOOR_VISIBLE, NULL);
342 	for (i = 0; i < floor_num; i++)
343 	    if (inven_carry_okay(floor_list[i]))
344 			can_pickup++;
345 
346 	if (!can_pickup) {
347 	    event_signal(EVENT_SEEFLOOR);
348 		mem_free(floor_list);
349 	    return objs_picked_up;
350 	}
351 
352 	/* Use a menu interface for multiple objects, or pickup single objects */
353 	if (!menu && !current) {
354 		if (floor_num > 1)
355 			menu = true;
356 		else
357 			current = floor_list[0];
358 	}
359 
360 	/* Display a list if requested. */
361 	if (menu && !current) {
362 		const char *q, *s;
363 		struct object *obj_local = NULL;
364 
365 		/* Get an object or exit. */
366 		q = "Get which item?";
367 		s = "You see nothing there.";
368 		if (!get_item(&obj_local, q, s, CMD_PICKUP, inven_carry_okay, USE_FLOOR)) {
369 			mem_free(floor_list);
370 			return (objs_picked_up);
371 		}
372 
373 		current = obj_local;
374 		call_function_again = true;
375 
376 		/* With a list, we do not need explicit pickup messages */
377 		domsg = true;
378 	}
379 
380 	/* Pick up object, if legal */
381 	if (current) {
382 		/* Pick up the object */
383 		player_pickup_aux(p, current, 0, domsg);
384 
385 		/* Indicate an object picked up. */
386 		objs_picked_up = 1;
387 	}
388 
389 	/*
390 	 * If requested, call this function recursively.  Count objects picked
391 	 * up.  Force the display of a menu in all cases.
392 	 */
393 	if (call_function_again)
394 		objs_picked_up += player_pickup_item(p, NULL, true);
395 
396 	mem_free(floor_list);
397 
398 	/* Indicate how many objects have been picked up. */
399 	return (objs_picked_up);
400 }
401 
402 /**
403  * Pick up everything on the floor that requires no player action
404  */
do_autopickup(struct player * p)405 int do_autopickup(struct player *p)
406 {
407 	struct object *obj, *next;
408 	byte objs_picked_up = 0;
409 
410 	/* Nothing to pick up -- return */
411 	if (!square_object(cave, p->grid))
412 		return 0;
413 
414 	/* Always pickup gold, effortlessly */
415 	player_pickup_gold(p);
416 
417 	/* Scan the remaining objects */
418 	obj = square_object(cave, p->grid);
419 	while (obj) {
420 		next = obj->next;
421 
422 		/* Ignore all hidden objects and non-objects */
423 		if (!ignore_item_ok(obj)) {
424 			int auto_num;
425 
426 			/* Hack -- disturb */
427 			disturb(p);
428 
429 			/* Automatically pick up items into the backpack */
430 			auto_num = auto_pickup_okay(obj);
431 			if (auto_num) {
432 				/* Pick up the object (as much as possible) with message */
433 				player_pickup_aux(p, obj, auto_num, true);
434 				objs_picked_up++;
435 			}
436 		}
437 		obj = next;
438 	}
439 
440 	return objs_picked_up;
441 }
442 
443 /**
444  * Pick up objects at the player's request
445  */
do_cmd_pickup(struct command * cmd)446 void do_cmd_pickup(struct command *cmd)
447 {
448 	int energy_cost = 0;
449 	struct object *obj = NULL;
450 
451 	/* See if we have an item already */
452 	(void) cmd_get_arg_item(cmd, "item", &obj);
453 
454 	/* Pick up floor objects with a menu for multiple objects */
455 	energy_cost += player_pickup_item(player, obj, false)
456 		* z_info->move_energy / 10;
457 
458 	/* Limit */
459 	if (energy_cost > z_info->move_energy) energy_cost = z_info->move_energy;
460 
461 	/* Charge this amount of energy. */
462 	player->upkeep->energy_use = energy_cost;
463 
464 	/* Redraw the object list using the upkeep flag so that the update can be
465 	 * somewhat coalesced. Use event_signal(EVENT_ITEMLIST to force update. */
466 	player->upkeep->redraw |= (PR_ITEMLIST);
467 }
468 
469 /**
470  * Pick up or look at objects on a square when the player steps onto it
471  */
do_cmd_autopickup(struct command * cmd)472 void do_cmd_autopickup(struct command *cmd)
473 {
474 	/* Get the obvious things */
475 	player->upkeep->energy_use = do_autopickup(player)
476 		* z_info->move_energy / 10;
477 	if (player->upkeep->energy_use > z_info->move_energy)
478 		player->upkeep->energy_use = z_info->move_energy;
479 
480 	/* Look at or feel what's left */
481 	event_signal(EVENT_SEEFLOOR);
482 
483 	/* Redraw the object list using the upkeep flag so that the update can be
484 	 * somewhat coalesced. Use event_signal(EVENT_ITEMLIST to force update. */
485 	player->upkeep->redraw |= (PR_ITEMLIST);
486 }
487