1 /**
2  * \file cmd-obj.c
3  * \brief Handle objects in various ways
4  *
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  * Copyright (c) 2007-9 Andi Sidwell, Chris Carr, Ed Graham, Erik Osheim
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 "cmd-core.h"
23 #include "cmds.h"
24 #include "effects.h"
25 #include "game-input.h"
26 #include "init.h"
27 #include "obj-desc.h"
28 #include "obj-gear.h"
29 #include "obj-ignore.h"
30 #include "obj-info.h"
31 #include "obj-knowledge.h"
32 #include "obj-make.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-spell.h"
39 #include "player-timed.h"
40 #include "player-util.h"
41 #include "target.h"
42 #include "trap.h"
43 
44 /**
45  * ------------------------------------------------------------------------
46  * Utility bits and bobs
47  * ------------------------------------------------------------------------
48  */
49 
50 /**
51  * Check to see if the player can use a rod/wand/staff/activatable object.
52  */
check_devices(struct object * obj)53 static int check_devices(struct object *obj)
54 {
55 	int fail;
56 	const char *action;
57 	const char *what = NULL;
58 	bool activated = false;
59 
60 	/* Get the right string */
61 	if (tval_is_rod(obj)) {
62 		action = "zap the rod";
63 	} else if (tval_is_wand(obj)) {
64 		action = "use the wand";
65 		what = "wand";
66 	} else if (tval_is_staff(obj)) {
67 		action = "use the staff";
68 		what = "staff";
69 	} else {
70 		action = "activate it";
71 		activated = true;
72 	}
73 
74 	/* Figure out how hard the item is to use */
75 	fail = get_use_device_chance(obj);
76 
77 	/* Roll for usage */
78 	if (randint1(1000) < fail) {
79 		event_signal(EVENT_INPUT_FLUSH);
80 		msg("You failed to %s properly.", action);
81 		return false;
82 	}
83 
84 	/* Notice empty staffs */
85 	if (what && obj->pval <= 0) {
86 		event_signal(EVENT_INPUT_FLUSH);
87 		msg("The %s has no charges left.", what);
88 		return false;
89 	}
90 
91 	/* Notice activations */
92 	if (activated) {
93 		if (obj->effect)
94 			obj->known->effect = obj->effect;
95 		else if (obj->activation)
96 			obj->known->activation = obj->activation;
97 	}
98 
99 	return true;
100 }
101 
102 
103 /**
104  * Return the chance of an effect beaming, given a tval.
105  */
beam_chance(int tval)106 static int beam_chance(int tval)
107 {
108 	switch (tval)
109 	{
110 		case TV_WAND: return 20;
111 		case TV_ROD:  return 10;
112 	}
113 
114 	return 0;
115 }
116 
117 
118 /**
119  * Print an artifact activation message.
120  */
activation_message(struct object * obj)121 static void activation_message(struct object *obj)
122 {
123 	const char *message;
124 
125 	/* See if we have a message, then print it */
126 	if (!obj->activation) return;
127 	if (!obj->activation->message) return;
128 	if (obj->artifact && obj->artifact->alt_msg) {
129 		message = obj->artifact->alt_msg;
130 	} else {
131 		message = obj->activation->message;
132 	}
133 	print_custom_message(obj, message, MSG_GENERIC);
134 }
135 
136 
137 
138 /**
139  * ------------------------------------------------------------------------
140  * Inscriptions
141  * ------------------------------------------------------------------------
142  */
143 
144 /**
145  * Remove inscription
146  */
do_cmd_uninscribe(struct command * cmd)147 void do_cmd_uninscribe(struct command *cmd)
148 {
149 	struct object *obj;
150 
151 	if (player_is_shapechanged(player)) {
152 		msg("You cannot do this while in %s form.",	player->shape->name);
153 		if (get_check("Do you want to change back? " )) {
154 			player_resume_normal_shape(player);
155 		} else {
156 			return;
157 		}
158 	}
159 
160 	/* Get arguments */
161 	if (cmd_get_item(cmd, "item", &obj,
162 			/* Prompt */ "Uninscribe which item?",
163 			/* Error  */ "You have nothing you can uninscribe.",
164 			/* Filter */ obj_has_inscrip,
165 			/* Choice */ USE_EQUIP | USE_INVEN | USE_QUIVER | USE_FLOOR) != CMD_OK)
166 		return;
167 
168 	obj->note = 0;
169 	msg("Inscription removed.");
170 
171 	player->upkeep->notice |= (PN_COMBINE | PN_IGNORE);
172 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP);
173 }
174 
175 /**
176  * Add inscription
177  */
do_cmd_inscribe(struct command * cmd)178 void do_cmd_inscribe(struct command *cmd)
179 {
180 	struct object *obj;
181 	const char *str;
182 
183 	char prompt[1024];
184 	char o_name[80];
185 
186 	if (player_is_shapechanged(player)) {
187 		msg("You cannot do this while in %s form.",	player->shape->name);
188 		if (get_check("Do you want to change back? " )) {
189 			player_resume_normal_shape(player);
190 		} else {
191 			return;
192 		}
193 	}
194 
195 	/* Get arguments */
196 	if (cmd_get_item(cmd, "item", &obj,
197 			/* Prompt */ "Inscribe which item?",
198 			/* Error  */ "You have nothing to inscribe.",
199 			/* Filter */ NULL,
200 			/* Choice */ USE_EQUIP | USE_INVEN | USE_QUIVER | USE_FLOOR | IS_HARMLESS) != CMD_OK)
201 		return;
202 
203 	/* Form prompt */
204 	object_desc(o_name, sizeof(o_name), obj, ODESC_PREFIX | ODESC_FULL);
205 	strnfmt(prompt, sizeof prompt, "Inscribing %s.", o_name);
206 
207 	if (cmd_get_string(cmd, "inscription", &str,
208 			quark_str(obj->note) /* Default */,
209 			prompt, "Inscribe with what? ") != CMD_OK)
210 		return;
211 
212 	obj->note = quark_add(str);
213 	string_free((char *)str);
214 
215 	player->upkeep->notice |= (PN_COMBINE | PN_IGNORE);
216 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP);
217 }
218 
219 
220 /**
221  * Autoinscribe all appropriate objects
222  */
do_cmd_autoinscribe(struct command * cmd)223 void do_cmd_autoinscribe(struct command *cmd)
224 {
225 	if (player_is_shapechanged(player)) return;
226 
227 	autoinscribe_ground();
228 	autoinscribe_pack();
229 
230 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP);
231 }
232 
233 
234 /**
235  * ------------------------------------------------------------------------
236  * Taking off/putting on
237  * ------------------------------------------------------------------------
238  */
239 
240 /**
241  * Take off an item
242  */
do_cmd_takeoff(struct command * cmd)243 void do_cmd_takeoff(struct command *cmd)
244 {
245 	struct object *obj;
246 
247 	if (player_is_shapechanged(player)) {
248 		msg("You cannot do this while in %s form.",	player->shape->name);
249 		if (get_check("Do you want to change back? " )) {
250 			player_resume_normal_shape(player);
251 		} else {
252 			return;
253 		}
254 	}
255 
256 	/* Get arguments */
257 	if (cmd_get_item(cmd, "item", &obj,
258 			/* Prompt */ "Take off or unwield which item?",
259 			/* Error  */ "You have nothing to take off or unwield.",
260 			/* Filter */ obj_can_takeoff,
261 			/* Choice */ USE_EQUIP) != CMD_OK)
262 		return;
263 
264 	inven_takeoff(obj);
265 	combine_pack();
266 	pack_overflow(obj);
267 	player->upkeep->energy_use = z_info->move_energy / 2;
268 }
269 
270 
271 /**
272  * Wield or wear an item
273  */
do_cmd_wield(struct command * cmd)274 void do_cmd_wield(struct command *cmd)
275 {
276 	struct object *equip_obj;
277 	char o_name[80];
278 	const char *act;
279 
280 	unsigned n;
281 
282 	int slot;
283 	struct object *obj;
284 
285 	if (player_is_shapechanged(player)) {
286 		msg("You cannot do this while in %s form.",	player->shape->name);
287 		if (get_check("Do you want to change back? " )) {
288 			player_resume_normal_shape(player);
289 		} else {
290 			return;
291 		}
292 	}
293 
294 	/* Get arguments */
295 	if (cmd_get_item(cmd, "item", &obj,
296 			/* Prompt */ "Wear or wield which item?",
297 			/* Error  */ "You have nothing to wear or wield.",
298 			/* Filter */ obj_can_wear,
299 			/* Choice */ USE_INVEN | USE_FLOOR) != CMD_OK)
300 		return;
301 
302 	/* Get the slot the object wants to go in, and the item currently there */
303 	slot = wield_slot(obj);
304 	equip_obj = slot_object(player, slot);
305 
306 	/* If the slot is open, wield and be done */
307 	if (!equip_obj) {
308 		inven_wield(obj, slot);
309 		return;
310 	}
311 
312 	/* Usually if the slot is taken we'll just replace the item in the slot,
313 	 * but for rings we need to ask the user which slot they actually
314 	 * want to replace */
315 	if (tval_is_ring(obj)) {
316 		if (cmd_get_item(cmd, "replace", &equip_obj,
317 						 /* Prompt */ "Replace which ring? ",
318 						 /* Error  */ "Error in do_cmd_wield(), please report.",
319 						 /* Filter */ tval_is_ring,
320 						 /* Choice */ USE_EQUIP) != CMD_OK)
321 			return;
322 
323 		/* Change slot if necessary */
324 		slot = equipped_item_slot(player->body, equip_obj);
325 	}
326 
327 	/* Prevent wielding into a stickied slot */
328 	if (!obj_can_takeoff(equip_obj)) {
329 		object_desc(o_name, sizeof(o_name), equip_obj, ODESC_BASE);
330 		msg("You cannot remove the %s you are %s.", o_name,
331 			equip_describe(player, slot));
332 		return;
333 	}
334 
335 	/* "!t" checks for taking off */
336 	n = check_for_inscrip(equip_obj, "!t");
337 	while (n--) {
338 		/* Prompt */
339 		object_desc(o_name, sizeof(o_name), equip_obj,
340 					ODESC_PREFIX | ODESC_FULL);
341 
342 		/* Forget it */
343 		if (!get_check(format("Really take off %s? ", o_name))) return;
344 	}
345 
346 	/* Describe the object */
347 	object_desc(o_name, sizeof(o_name), equip_obj, ODESC_PREFIX | ODESC_FULL);
348 
349 	/* Took off weapon */
350 	if (slot_type_is(slot, EQUIP_WEAPON))
351 		act = "You were wielding";
352 	/* Took off bow */
353 	else if (slot_type_is(slot, EQUIP_BOW))
354 		act = "You were holding";
355 	/* Took off light */
356 	else if (slot_type_is(slot, EQUIP_LIGHT))
357 		act = "You were holding";
358 	/* Took off something else */
359 	else
360 		act = "You were wearing";
361 
362 	inven_wield(obj, slot);
363 
364 	/* Message */
365 	msgt(MSG_WIELD, "%s %s (%c).", act, o_name, gear_to_label(equip_obj));
366 }
367 
368 /**
369  * Drop an item
370  */
do_cmd_drop(struct command * cmd)371 void do_cmd_drop(struct command *cmd)
372 {
373 	int amt;
374 	struct object *obj;
375 
376 	if (player_is_shapechanged(player)) {
377 		msg("You cannot do this while in %s form.",	player->shape->name);
378 		if (get_check("Do you want to change back? " )) {
379 			player_resume_normal_shape(player);
380 		} else {
381 			return;
382 		}
383 	}
384 
385 	/* Get arguments */
386 	if (cmd_get_item(cmd, "item", &obj,
387 			/* Prompt */ "Drop which item?",
388 			/* Error  */ "You have nothing to drop.",
389 			/* Filter */ NULL,
390 			/* Choice */ USE_EQUIP | USE_INVEN | USE_QUIVER) != CMD_OK)
391 		return;
392 
393 	if (cmd_get_quantity(cmd, "quantity", &amt, obj->number) != CMD_OK)
394 		return;
395 
396 	/* Cannot remove stickied items */
397 	if (object_is_equipped(player->body, obj) && !obj_can_takeoff(obj)) {
398 		msg("Hmmm, it seems to be stuck.");
399 		return;
400 	}
401 
402 	inven_drop(obj, amt);
403 	player->upkeep->energy_use = z_info->move_energy / 2;
404 }
405 
406 /**
407  * ------------------------------------------------------------------------
408  * Using items the traditional way
409  * ------------------------------------------------------------------------
410  */
411 
412 enum use {
413 	USE_TIMEOUT,
414 	USE_CHARGE,
415 	USE_SINGLE
416 };
417 
418 /**
419  * Use an object the right way.
420  */
use_aux(struct command * cmd,struct object * obj,enum use use,int snd)421 static void use_aux(struct command *cmd, struct object *obj, enum use use,
422 					int snd)
423 {
424 	struct effect *effect = object_effect(obj);
425 	bool can_use = true;
426 	bool was_aware, from_floor;
427 	bool known_aim = false;
428 	bool none_left = false;
429 	int dir = 5;
430 	char label = gear_to_label(obj);
431 	struct trap_kind *rune = lookup_trap("glyph of warding");
432 
433 	/* Get arguments */
434 	assert(cmd_get_arg_item(cmd, "item", &obj) == CMD_OK);
435 
436 	was_aware = object_flavor_is_aware(obj);
437 
438 	/* Determine whether we know an item needs to be be aimed */
439 	if (tval_is_wand(obj) || tval_is_rod(obj) || was_aware ||
440 		(obj->effect && (obj->known->effect == obj->effect)) ||
441 		(obj->activation && (obj->known->activation == obj->activation))) {
442 		known_aim = true;
443 	}
444 
445 	if (obj_needs_aim(obj)) {
446 		/* Unknown things with no obvious aim get a random direction */
447 		if (!known_aim) {
448 			dir = ddd[randint0(8)];
449 		} else if (cmd_get_target(cmd, "target", &dir) != CMD_OK) {
450 			return;
451 		}
452 
453 		/* Confusion wrecks aim */
454 		player_confuse_dir(player, &dir, false);
455 	}
456 
457 	/* track the object used */
458 	track_object(player->upkeep, obj);
459 
460 	/* Verify effect */
461 	assert(effect);
462 
463 	/* Check for use if necessary */
464 	if ((use == USE_CHARGE) || (use == USE_TIMEOUT)) {
465 		can_use = check_devices(obj);
466 	}
467 
468 	/* Execute the effect */
469 	if (can_use) {
470 		int beam = beam_chance(obj->tval);
471 		int boost, level, charges = 0;
472 		int number = 0;
473 		bool ident = false, used;
474 		struct object *work_obj;
475 
476 		/* Get the level */
477 		if (obj->artifact)
478 			level = obj->artifact->level;
479 		else
480 			level = obj->kind->level;
481 
482 		/* Sound and/or message */
483 		if (obj->activation) {
484 			msgt(snd, "You activate it.");
485 			activation_message(obj);
486 		} else if (obj->kind->effect_msg) {
487 			msgt(snd, obj->kind->effect_msg);
488 		} else if (obj->kind->vis_msg && !player->timed[TMD_BLIND]) {
489 			msgt(snd, obj->kind->vis_msg);
490 		} else {
491 			/* Make a noise! */
492 			sound(snd);
493 		}
494 
495 		/* Boost damage effects if skill > difficulty */
496 		boost = MAX((player->state.skills[SKILL_DEVICE] - level) / 2, 0);
497 
498 		/*
499 		 * Tentatively deduct the amount used - the effect could leave
500 		 * the object inaccessible making it difficult to do after a
501 		 * successful use.  For the same reason, get a copy of the
502 		 * object to use for propagating knowledge.
503 		 */
504 		if (use == USE_SINGLE) {
505 			if (object_is_carried(player, obj)) {
506 				work_obj = gear_object_for_use(obj, 1, false, &none_left);
507 				from_floor = false;
508 			} else {
509 				work_obj = floor_object_for_use(obj, 1, false, &none_left);
510 				from_floor = true;
511 			}
512 			/* Record number for messages after use */
513 			number = (none_left) ? 0 : obj->number;
514 		} else  {
515 			if (use == USE_CHARGE) {
516 				charges = obj->pval;
517 				/* Use a single charge */
518 				obj->pval--;
519 			} else if (use == USE_TIMEOUT) {
520 				charges = obj->timeout;
521 				obj->timeout += randcalc(obj->time, 0, RANDOMISE);
522 			}
523 			work_obj = object_new();
524 			object_copy(work_obj, obj);
525 			work_obj->oidx = 0;
526 			if (obj->known) {
527 				work_obj->known = object_new();
528 				object_copy(work_obj->known, obj->known);
529 				work_obj->known->oidx = 0;
530 			}
531 			from_floor = !object_is_carried(player, obj);
532 		}
533 
534 		/* Do effect; use original not copy (proj. effect handling) */
535 		target_fix();
536 		used = effect_do(effect,
537 							source_player(),
538 							obj,
539 							&ident,
540 							was_aware,
541 							dir,
542 							beam,
543 							boost,
544 							cmd);
545 		target_release();
546 
547 		if (!used) {
548 			/* Restore the tentative deduction. */
549 			if (use == USE_SINGLE) {
550 				/* Drop copy to simplify subsequent logic */
551 				struct object *dropped = object_new();
552 
553 				object_copy(dropped, work_obj);
554 				if (work_obj->known) {
555 					dropped->known = object_new();
556 					object_copy(dropped->known, work_obj->known);
557 				}
558 				if (from_floor) {
559 					drop_near(cave, &dropped, 0, player->grid, false, true);
560 				} else {
561 					inven_carry(player, dropped, true, false);
562 				}
563 			} else if (use == USE_CHARGE) {
564 				obj->pval = charges;
565 			} else if (use == USE_TIMEOUT) {
566 				obj->timeout = charges;
567 			}
568 
569 			/*
570 			 * Quit if the item wasn't used and no knowledge was
571 			 * gained
572 			 */
573 			if (was_aware || !ident) {
574 				if (work_obj->known) {
575 					object_delete(&work_obj->known);
576 				}
577 				object_delete(&work_obj);
578 				return;
579 			}
580 		}
581 
582 		/* Increase knowledge */
583 		if (use == USE_SINGLE) {
584 			char name[80];
585 			int old_num = work_obj->number;
586 
587 			/* Single use items are automatically learned */
588 			if (!was_aware) {
589 				object_learn_on_use(player, work_obj);
590 			}
591 			/* Get a description */
592 			work_obj->number = number + ((used) ? 0 : 1);
593 			object_desc(name, sizeof(name), work_obj, ODESC_PREFIX | ODESC_FULL);
594 			work_obj->number = old_num;
595 			if (from_floor) {
596 				/* Print a message */
597 				msg("You see %s.", name);
598 			} else {
599 				msg("You have %s (%c).", name, label);
600 			}
601 		} else {
602 			/* Wearables may need update, other things become known or tried */
603 			if (tval_is_wearable(work_obj)) {
604 				update_player_object_knowledge(player);
605 			} else if (!was_aware && ident) {
606 				object_learn_on_use(player, work_obj);
607 			} else {
608 				object_flavor_tried(work_obj);
609 			}
610 		}
611 
612 		if (used && use == USE_CHARGE) {
613 			/* Describe charges */
614 			if (from_floor)
615 				floor_item_charges(work_obj);
616 			else
617 				inven_item_charges(work_obj);
618 		}
619 
620 		/* Clean up created copy. */
621 		if (work_obj->known)
622 			object_delete(&work_obj->known);
623 		object_delete(&work_obj);
624 	} else {
625 		from_floor = !object_is_carried(player, obj);
626 	}
627 
628 	/* Use the turn */
629 	player->upkeep->energy_use = z_info->move_energy;
630 
631 	/* Autoinscribe if we are guaranteed to still have any */
632 	if (!none_left && !from_floor)
633 		apply_autoinscription(obj);
634 
635 	/* Mark as tried and redisplay */
636 	player->upkeep->notice |= (PN_COMBINE);
637 	player->upkeep->redraw |= (PR_INVEN | PR_EQUIP | PR_OBJECT);
638 
639 	/* Hack to make Glyph of Warding work properly */
640 	if (square_trap_specific(cave, player->grid, rune->tidx)) {
641 		/* Push objects off the grid */
642 		if (square_object(cave, player->grid))
643 			push_object(player->grid);
644 	}
645 }
646 
647 
648 /**
649  * Read a scroll
650  */
do_cmd_read_scroll(struct command * cmd)651 void do_cmd_read_scroll(struct command *cmd)
652 {
653 	struct object *obj;
654 
655 	if (player_is_shapechanged(player)) {
656 		msg("You cannot do this while in %s form.",	player->shape->name);
657 		if (get_check("Do you want to change back? " )) {
658 			player_resume_normal_shape(player);
659 		} else {
660 			return;
661 		}
662 	}
663 
664 	/* Check player can use scroll */
665 	if (!player_can_read(player, true))
666 		return;
667 
668 	/* Get the scroll */
669 	if (cmd_get_item(cmd, "item", &obj,
670 			"Read which scroll? ",
671 			"You have no scrolls to read.",
672 			tval_is_scroll,
673 			USE_INVEN | USE_FLOOR) != CMD_OK) return;
674 
675 	use_aux(cmd, obj, USE_SINGLE, MSG_GENERIC);
676 }
677 
678 /**
679  * Use a staff
680  */
do_cmd_use_staff(struct command * cmd)681 void do_cmd_use_staff(struct command *cmd)
682 {
683 	struct object *obj;
684 
685 	if (player_is_shapechanged(player)) {
686 		msg("You cannot do this while in %s form.",	player->shape->name);
687 		if (get_check("Do you want to change back? " )) {
688 			player_resume_normal_shape(player);
689 		} else {
690 			return;
691 		}
692 	}
693 
694 	/* Get an item */
695 	if (cmd_get_item(cmd, "item", &obj,
696 			"Use which staff? ",
697 			"You have no staves to use.",
698 			tval_is_staff,
699 			USE_INVEN | USE_FLOOR | SHOW_FAIL) != CMD_OK) return;
700 
701 	if (!obj_has_charges(obj)) {
702 		msg("That staff has no charges.");
703 		return;
704 	}
705 
706 	use_aux(cmd, obj, USE_CHARGE, MSG_USE_STAFF);
707 }
708 
709 /**
710  * Aim a wand
711  */
do_cmd_aim_wand(struct command * cmd)712 void do_cmd_aim_wand(struct command *cmd)
713 {
714 	struct object *obj;
715 
716 	if (player_is_shapechanged(player)) {
717 		msg("You cannot do this while in %s form.",	player->shape->name);
718 		if (get_check("Do you want to change back? " )) {
719 			player_resume_normal_shape(player);
720 		} else {
721 			return;
722 		}
723 	}
724 
725 	/* Get an item */
726 	if (cmd_get_item(cmd, "item", &obj,
727 			"Aim which wand? ",
728 			"You have no wands to aim.",
729 			tval_is_wand,
730 			USE_INVEN | USE_FLOOR | SHOW_FAIL) != CMD_OK) return;
731 
732 	if (!obj_has_charges(obj)) {
733 		msg("That wand has no charges.");
734 		return;
735 	}
736 
737 	use_aux(cmd, obj, USE_CHARGE, MSG_ZAP_ROD);
738 }
739 
740 /**
741  * Zap a rod
742  */
do_cmd_zap_rod(struct command * cmd)743 void do_cmd_zap_rod(struct command *cmd)
744 {
745 	struct object *obj;
746 
747 	if (player_is_shapechanged(player)) {
748 		msg("You cannot do this while in %s form.",	player->shape->name);
749 		if (get_check("Do you want to change back? " )) {
750 			player_resume_normal_shape(player);
751 		} else {
752 			return;
753 		}
754 	}
755 
756 	/* Get an item */
757 	if (cmd_get_item(cmd, "item", &obj,
758 			"Zap which rod? ",
759 			"You have no rods to zap.",
760 			tval_is_rod,
761 			USE_INVEN | USE_FLOOR | SHOW_FAIL) != CMD_OK) return;
762 
763 	if (!obj_can_zap(obj)) {
764 		msg("That rod is still charging.");
765 		return;
766 	}
767 
768 	use_aux(cmd, obj, USE_TIMEOUT, MSG_ZAP_ROD);
769 }
770 
771 /**
772  * Activate an object
773  */
do_cmd_activate(struct command * cmd)774 void do_cmd_activate(struct command *cmd)
775 {
776 	struct object *obj;
777 
778 	if (player_is_shapechanged(player)) {
779 		msg("You cannot do this while in %s form.",	player->shape->name);
780 		if (get_check("Do you want to change back? " )) {
781 			player_resume_normal_shape(player);
782 		} else {
783 			return;
784 		}
785 	}
786 
787 	/* Get an item */
788 	if (cmd_get_item(cmd, "item", &obj,
789 			"Activate which item? ",
790 			"You have no items to activate.",
791 			obj_is_activatable,
792 			USE_EQUIP | SHOW_FAIL) != CMD_OK) return;
793 
794 	if (!obj_can_activate(obj)) {
795 		msg("That item is still charging.");
796 		return;
797 	}
798 
799 	use_aux(cmd, obj, USE_TIMEOUT, MSG_ACT_ARTIFACT);
800 }
801 
802 /**
803  * Eat some food
804  */
do_cmd_eat_food(struct command * cmd)805 void do_cmd_eat_food(struct command *cmd)
806 {
807 	struct object *obj;
808 
809 	/* Get an item */
810 	if (cmd_get_item(cmd, "item", &obj,
811 			"Eat which food? ",
812 			"You have no food to eat.",
813 			tval_is_edible,
814 			USE_INVEN | USE_FLOOR) != CMD_OK) return;
815 
816 	use_aux(cmd, obj, USE_SINGLE, MSG_EAT);
817 }
818 
819 /**
820  * Quaff a potion
821  */
do_cmd_quaff_potion(struct command * cmd)822 void do_cmd_quaff_potion(struct command *cmd)
823 {
824 	struct object *obj;
825 
826 	if (player_is_shapechanged(player)) {
827 		msg("You cannot do this while in %s form.",	player->shape->name);
828 		if (get_check("Do you want to change back? " )) {
829 			player_resume_normal_shape(player);
830 		} else {
831 			return;
832 		}
833 	}
834 
835 	/* Get an item */
836 	if (cmd_get_item(cmd, "item", &obj,
837 			"Quaff which potion? ",
838 			"You have no potions from which to quaff.",
839 			tval_is_potion,
840 			USE_INVEN | USE_FLOOR) != CMD_OK) return;
841 
842 	use_aux(cmd, obj, USE_SINGLE, MSG_QUAFF);
843 }
844 
845 /**
846  * Use any usable item
847  */
do_cmd_use(struct command * cmd)848 void do_cmd_use(struct command *cmd)
849 {
850 	struct object *obj;
851 
852 	if (player_is_shapechanged(player)) {
853 		msg("You cannot do this while in %s form.",	player->shape->name);
854 		if (get_check("Do you want to change back? " )) {
855 			player_resume_normal_shape(player);
856 		} else {
857 			return;
858 		}
859 	}
860 
861 	/* Get an item */
862 	if (cmd_get_item(cmd, "item", &obj,
863 			"Use which item? ",
864 			"You have no items to use.",
865 			obj_is_useable,
866 			USE_EQUIP | USE_INVEN | USE_QUIVER | USE_FLOOR | SHOW_FAIL | QUIVER_TAGS | SHOW_FAIL) != CMD_OK)
867 		return;
868 
869 	if (tval_is_ammo(obj))				do_cmd_fire(cmd);
870 	else if (tval_is_potion(obj))		do_cmd_quaff_potion(cmd);
871 	else if (tval_is_edible(obj))		do_cmd_eat_food(cmd);
872 	else if (tval_is_rod(obj))			do_cmd_zap_rod(cmd);
873 	else if (tval_is_wand(obj))			do_cmd_aim_wand(cmd);
874 	else if (tval_is_staff(obj))		do_cmd_use_staff(cmd);
875 	else if (tval_is_scroll(obj))		do_cmd_read_scroll(cmd);
876 	else if (obj_can_refill(obj))		do_cmd_refill(cmd);
877 	else if (obj_is_activatable(obj)) {
878 		if (object_is_equipped(player->body, obj)) {
879 			do_cmd_activate(cmd);
880 		} else {
881 			msg("Equip the item to use it.");
882 		}
883 	} else
884 		msg("The item cannot be used at the moment");
885 }
886 
887 
888 /**
889  * ------------------------------------------------------------------------
890  * Refuelling
891  * ------------------------------------------------------------------------
892  */
893 
refill_lamp(struct object * lamp,struct object * obj)894 static void refill_lamp(struct object *lamp, struct object *obj)
895 {
896 	/* Refuel */
897 	lamp->timeout += obj->timeout ? obj->timeout : obj->pval;
898 
899 	/* Message */
900 	msg("You fuel your lamp.");
901 
902 	/* Comment */
903 	if (lamp->timeout >= z_info->fuel_lamp) {
904 		lamp->timeout = z_info->fuel_lamp;
905 		msg("Your lamp is full.");
906 	}
907 
908 	/* Refilled from a lantern */
909 	if (of_has(obj->flags, OF_TAKES_FUEL)) {
910 		/* Unstack if necessary */
911 		if (obj->number > 1) {
912 			/* Obtain a local object, split */
913 			struct object *used = object_split(obj, 1);
914 
915 			/* Remove fuel */
916 			used->timeout = 0;
917 
918 			/* Carry or drop */
919 			if (object_is_carried(player, obj) && inven_carry_okay(used))
920 				inven_carry(player, used, true, true);
921 			else
922 				drop_near(cave, &used, 0, player->grid, false, true);
923 		} else
924 			/* Empty a single lantern */
925 			obj->timeout = 0;
926 
927 		/* Combine the pack (later) */
928 		player->upkeep->notice |= (PN_COMBINE);
929 
930 		/* Redraw stuff */
931 		player->upkeep->redraw |= (PR_INVEN);
932 	} else { /* Refilled from a flask */
933 		struct object *used;
934 		bool none_left = false;
935 
936 		/* Decrease the item from the pack or the floor */
937 		if (object_is_carried(player, obj))
938 			used = gear_object_for_use(obj, 1, true, &none_left);
939 		else
940 			used = floor_object_for_use(obj, 1, true, &none_left);
941 		if (used->known)
942 			object_delete(&used->known);
943 		object_delete(&used);
944 	}
945 
946 	/* Recalculate torch */
947 	player->upkeep->update |= (PU_TORCH);
948 
949 	/* Redraw stuff */
950 	player->upkeep->redraw |= (PR_EQUIP);
951 }
952 
953 
do_cmd_refill(struct command * cmd)954 void do_cmd_refill(struct command *cmd)
955 {
956 	struct object *light = equipped_item_by_slot_name(player, "light");
957 	struct object *obj;
958 
959 	if (player_is_shapechanged(player)) {
960 		msg("You cannot do this while in %s form.",	player->shape->name);
961 		if (get_check("Do you want to change back? " )) {
962 			player_resume_normal_shape(player);
963 		} else {
964 			return;
965 		}
966 	}
967 
968 	/* Get an item */
969 	if (cmd_get_item(cmd, "item", &obj,
970 			"Refuel with with fuel source? ",
971 			"You have nothing you can refuel with.",
972 			obj_can_refill,
973 			USE_INVEN | USE_FLOOR) != CMD_OK) return;
974 
975 	/* Check what we're wielding. */
976 	if (!light || !tval_is_light(light)) {
977 		msg("You are not wielding a light.");
978 		return;
979 	} else if (of_has(light->flags, OF_NO_FUEL)) {
980 		msg("Your light cannot be refilled.");
981 		return;
982 	} else if (of_has(light->flags, OF_TAKES_FUEL)) {
983 		refill_lamp(light, obj);
984 	} else {
985 		msg("Your light cannot be refilled.");
986 		return;
987 	}
988 
989 	player->upkeep->energy_use = z_info->move_energy / 2;
990 }
991 
992 
993 
994 /**
995  * ------------------------------------------------------------------------
996  * Spell casting
997  * ------------------------------------------------------------------------
998  */
999 
1000 /**
1001  * Cast a spell from a book
1002  */
do_cmd_cast(struct command * cmd)1003 void do_cmd_cast(struct command *cmd)
1004 {
1005 	int spell_index, dir = 0;
1006 	const struct class_spell *spell;
1007 
1008 	if (player_is_shapechanged(player)) {
1009 		if (get_check("Change back to your original form? " )) {
1010 			player_resume_normal_shape(player);
1011 		}
1012 		return;
1013 	}
1014 
1015 	/* Check the player can cast spells at all */
1016 	if (!player_can_cast(player, true))
1017 		return;
1018 
1019 	/* Get arguments */
1020 	if (cmd_get_spell(cmd, "spell", &spell_index,
1021 			/* Verb */   "cast",
1022 			/* Book */   obj_can_cast_from,
1023 			/* Error */  "There are no spells you can cast.",
1024 			/* Filter */ spell_okay_to_cast) != CMD_OK)
1025 		return;
1026 
1027 	if (spell_needs_aim(spell_index)) {
1028 		if (cmd_get_target(cmd, "target", &dir) == CMD_OK)
1029 			player_confuse_dir(player, &dir, false);
1030 		else
1031 			return;
1032 	}
1033 
1034 	/* Get the spell */
1035 	spell = spell_by_index(spell_index);
1036 
1037 	/* Verify "dangerous" spells */
1038 	if (spell->smana > player->csp) {
1039 		const char *verb = spell->realm->verb;
1040 		const char *noun = spell->realm->spell_noun;
1041 
1042 		/* Warning */
1043 		msg("You do not have enough mana to %s this %s.", verb, noun);
1044 
1045 		/* Flush input */
1046 		event_signal(EVENT_INPUT_FLUSH);
1047 
1048 		/* Verify */
1049 		if (!get_check("Attempt it anyway? ")) return;
1050 	}
1051 
1052 	/* Cast a spell */
1053 	target_fix();
1054 	if (spell_cast(spell_index, dir, cmd)) {
1055 		if (player->timed[TMD_FASTCAST]) {
1056 			player->upkeep->energy_use = (z_info->move_energy * 3) / 4;
1057 		} else {
1058 			player->upkeep->energy_use = z_info->move_energy;
1059 		}
1060 	}
1061 	target_release();
1062 }
1063 
1064 
1065 /**
1066  * Gain a specific spell, specified by spell number (for mages).
1067  */
do_cmd_study_spell(struct command * cmd)1068 void do_cmd_study_spell(struct command *cmd)
1069 {
1070 	int spell_index;
1071 
1072 	/* Check the player can study at all atm */
1073 	if (!player_can_study(player, true))
1074 		return;
1075 
1076 	if (cmd_get_spell(cmd, "spell", &spell_index,
1077 			/* Verb */   "study",
1078 			/* Book */   obj_can_study,
1079 			/* Error  */ "You cannot learn any new spells from the books you have.",
1080 			/* Filter */ spell_okay_to_study) != CMD_OK)
1081 		return;
1082 
1083 	spell_learn(spell_index);
1084 	player->upkeep->energy_use = z_info->move_energy;
1085 }
1086 
1087 /**
1088  * Gain a random spell from the given book (for priests)
1089  */
do_cmd_study_book(struct command * cmd)1090 void do_cmd_study_book(struct command *cmd)
1091 {
1092 	struct object *book_obj;
1093 	const struct class_book *book;
1094 	int spell_index = -1;
1095 	struct class_spell *spell;
1096 	int i, k = 0;
1097 
1098 	if (cmd_get_item(cmd, "item", &book_obj,
1099 			/* Prompt */ "Study which book? ",
1100 			/* Error  */ "You cannot learn any new spells from the books you have.",
1101 			/* Filter */ obj_can_study,
1102 			/* Choice */ USE_INVEN | USE_FLOOR) != CMD_OK)
1103 		return;
1104 
1105 	book = player_object_to_book(player, book_obj);
1106 	track_object(player->upkeep, book_obj);
1107 	handle_stuff(player);
1108 
1109 	/* Check the player can study at all atm */
1110 	if (!player_can_study(player, true))
1111 		return;
1112 
1113 	for (i = 0; i < book->num_spells; i++) {
1114 		spell = &book->spells[i];
1115 		if (!spell_okay_to_study(spell->sidx))
1116 			continue;
1117 		if ((++k > 1) && (randint0(k) != 0))
1118 			continue;
1119 		spell_index = spell->sidx;
1120 	}
1121 
1122 	if (spell_index < 0) {
1123 		msg("You cannot learn any %ss in that book.", book->realm->spell_noun);
1124 	} else {
1125 		spell_learn(spell_index);
1126 		player->upkeep->energy_use = z_info->move_energy;
1127 	}
1128 }
1129 
1130 /**
1131  * Choose the way to study.  Choose life.  Choose a career.  Choose family.
1132  * Choose a fucking big monster, choose orc shamans, kobolds, dark elven
1133  * druids, and Mim, Betrayer of Turin.
1134  */
do_cmd_study(struct command * cmd)1135 void do_cmd_study(struct command *cmd)
1136 {
1137 	if (player_is_shapechanged(player)) {
1138 		msg("You cannot do this while in %s form.",	player->shape->name);
1139 		if (get_check("Do you want to change back? " )) {
1140 			player_resume_normal_shape(player);
1141 		} else {
1142 			return;
1143 		}
1144 	}
1145 
1146 	if (player_has(player, PF_CHOOSE_SPELLS))
1147 		do_cmd_study_spell(cmd);
1148 	else
1149 		do_cmd_study_book(cmd);
1150 }
1151