Debian Codegraph @

server/apply.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Handles objects being applied, and their effect.
17 */
18
19 #include "global.h"
20
21 /* need math lib for double-precision and pow() in dragon_eat_flesh() */
22 #include <math.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "living.h"
27 #include "skills.h"
28 #include "sounds.h"
29 #include "spells.h"
30 #include "sproto.h"
31 #include "tod.h"
32
33 static int apply_check_apply_restrictions(object *who, object *op, int aflags);
34 static int apply_check_personalized_blessings(object *who, const object *op);
35 static int apply_check_item_power(const object *who, const object *op, int aflags);
36 static int apply_check_owner(const object *who, const object *op, int aflags);
37 static void apply_update_ranged_skill(const object *who, object *op, int aflags);
38
39 /**
40 * Can transport hold object op?
41 * This is a pretty trivial function,
42 * but in the future, possible transport may have more restrictions
43 * or weight reduction like containers
44 *
45 * @param transport
46 * transport to check.
47 * @param op
48 * object we're trying to insert.
49 * @param nrof
50 * number of op.
51 * @return
52 * 1 if can hold, 0 else.
53 */
54 int transport_can_hold(const object *transport, const object *op, int nrof) {
55 return op->weight*nrof+transport->carrying <= transport->weight_limit;
56 }
57
58 /**
59 * Check if op should abort moving victim because of it's race or slaying.
60 *
61 * @param op
62 * detector or equivalent we're testing. Note that its type is not checked.
63 * @param victim
64 * object trying to move on op.
65 * @return
66 * 1 if it should abort, 0 if it should continue.
67 */
68 int should_director_abort(const object *op, const object *victim) {
69 int arch_flag, name_flag, race_flag;
70
71 /* Never move doors, it messes things. */
72 if (victim->type == DOOR)
73 return 1;
74
75 /* Get flags to determine what of arch, name, and race should be
76 * checked. This is stored in subtype, and is a bitmask, the LSB
77 * is the arch flag, the next is the name flag, and the last is
78 * the race flag. Also note, if subtype is set to zero, that also
79 * goes to defaults of all affecting it. Examples:
80 * subtype 1: only arch
81 * subtype 3: arch or name
82 * subtype 5: arch or race
83 * subtype 7: all three
84 */
85 if (op->subtype) {
86 arch_flag = op->subtype&1;
87 name_flag = op->subtype&2;
88 race_flag = op->subtype&4;
89 } else {
90 arch_flag = 1;
91 name_flag = 1;
92 race_flag = 1;
93 }
94 /* If the director has race set, only affect objects with a arch,
95 * name or race that matches.
96 */
97 if (op->race
98 && (!(victim->arch && arch_flag && victim->arch->name) || strcmp(op->race, victim->arch->name))
99 && (!(victim->name && name_flag) || strcmp(op->race, victim->name))
100 && (!(victim->race && race_flag) || strcmp(op->race, victim->race)))
101 return 1;
102
103 /* If the director has slaying set, only affect objects where none
104 * of arch, name, or race match.
105 */
106 if (op->slaying
107 && ((victim->arch && arch_flag && victim->arch->name && !strcmp(op->slaying, victim->arch->name))
108 || (victim->name && name_flag && !strcmp(op->slaying, victim->name))
109 || (victim->race && race_flag && !strcmp(op->slaying, victim->race))))
110 return 1;
111
112 return 0;
113 }
114
115 /**
116 * This checks whether the object has a "on_use_yield" field,
117 * and if so generated and drops matching item.
118 *
119 * @param tmp
120 * item that was applied.
121 */
122 void apply_handle_yield(object *tmp) {
123 const char *yield;
124
125 yield = object_get_value(tmp, "on_use_yield");
126 if (yield != NULL) {
127 object *drop = create_archetype(yield);
128 if (tmp->env)
129 drop = object_insert_in_ob(drop, tmp->env);
130 else
131 object_insert_in_map_at(drop, tmp->map, tmp, INS_BELOW_ORIGINATOR, tmp->x, tmp->y);
132 }
133 }
134
135 /**
136 * Makes an object's face the main face, which is supposed to be the "closed" one.
137 *
138 * Sets an object's face to the 'face' in the archetype.
139 * Meant for showing containers opening and closing.
140 *
141 * @param op
142 * Object to set face on
143 *
144 * @return TRUE if face changed
145 */
146 int set_object_face_main(object *op) {
147 const Face *newface = op->arch->clone.face;
148 sstring saved = object_get_value(op, "face_closed");
149
150 if (op->more)
151 set_object_face_main(op->more);
152
153 if (saved)
154 newface = find_face(saved, newface);
155 if (newface && op->face != newface) {
156 op->face = newface;
157 return TRUE;
158 }
159 return FALSE;
160 }
161
162 /**
163 * Makes an object's face the other_arch face, supposed to be the "opened" one.
164 *
165 * Sets an object's face to the other_arch 'face'.
166 * Meant for showing containers opening and closing.
167 *
168 * @param op
169 * Object to set face on
170 *
171 * @return TRUE if face changed
172 */
173 static int set_object_face_other(object *op) {
174 sstring custom;
175 const Face *newface = NULL;
176 object *head = op->head ? op->head : op;
177
178 if (op->more)
179 set_object_face_other(op->more);
180
181 if (head->face && head->other_arch && head->other_arch->clone.face)
182 newface = head->other_arch->clone.face;
183
184 if (op->face != op->arch->clone.face) {
185 /* object has a custom face, save it so it gets correctly restored later. */
186 object_set_value(op, "face_closed", op->face->name, 1);
187 }
188
189 custom = object_get_value(head, "face_opened");
190 if (custom)
191 newface = find_face(custom, newface);
192 if (newface && op->face != newface) {
193 op->face = newface;
194 return TRUE;
195 }
196 return FALSE;
197 }
198
199 /**
200 * Handle apply on containers. This is for
201 * containers that are applied by a player, whether in inventory or
202 * on the ground: eg, sacks, luggage, etc.
203 *
204 * Moved to own function and added many features [Tero.Haatanen(at)lut.fi]
205 * This version is for client/server mode.
206 *
207 * Reminder - there are three states for any container - closed (non applied),
208 * applied (not open, but objects that match get tossed into it), and open
209 * (applied flag set, and op->container points to the open container)
210 *
211 * @param op
212 * player.
213 * @param sack
214 * container the player is opening or closing.
215 * @param aflags
216 * combination of @ref AP_xxx flags.
217 * @return
218 * 1 if an object is applied somehow or another, 0 if error/no apply
219 *
220 * @author Eneq(at)(csd.uu.se)
221 */
222 int apply_container(object *op, object *sack, int aflags) {
223 char name_sack[MAX_BUF], name_tmp[MAX_BUF];
224 object *tmp = op->container;
225
226 if (op->type != PLAYER)
227 return 0; /* This might change */
228
229 if (sack == NULL || sack->type != CONTAINER) {
230 LOG(llevError, "apply_container: '%s' tried to apply %s, which is not a container\n", op->name, sack ? sack->name : "(null)");
231 return 0;
232 }
233
234 if (sack->head)
235 sack = sack->head;
236
237 query_name(sack, name_sack, MAX_BUF);
238
239 if ( aflags == AP_APPLY || aflags == AP_UNAPPLY || aflags == AP_OPEN ) {
240 // What if the container isn't in the player's inventory?
241 if ( sack->env != op ) {
242 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
243 "Not in your inventory: %s",
244 name_sack);
245 return 0;
246 }
247 }
248 if ( aflags == AP_APPLY ) {
249 // What if the container is open? Make it just ready!
250 if ( op->container == sack ) {
251 op->container = NULL;
252 if (op->contr != NULL)
253 op->contr->socket.container_position = 0;
254 CLEAR_FLAG(sack, FLAG_APPLIED);
255 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
256 "You readied %s.",
257 name_sack);
258 SET_FLAG(sack, FLAG_APPLIED);
259 // FIXME: This is not flipping the face!
260 if (set_object_face_main(sack)) // change image to closed
261 esrv_update_item(UPD_FLAGS|UPD_FACE, op, sack);
262 else
263 esrv_update_item(UPD_FLAGS, op, sack);
264 return 0;
265 }
266 // What if the container is already applied? Do nothing!
267 if (QUERY_FLAG(sack, FLAG_APPLIED)) {
268 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
269 "Already readied %s.",
270 name_sack);
271 return 0;
272 }
273 // What if the container is closed? Same as no aflags.
274 aflags = AP_NULL;
275 }
276
277 if ( aflags == AP_OPEN ) {
278 // What if the container is already open?
279 if ( op->container == sack ) {
280 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
281 "Already opened %s.",
282 name_sack);
283 return 0;
284 }
285 // Set the container as applied and then proceed as if no special flags
286 SET_FLAG(sack, FLAG_APPLIED);
287 aflags = AP_NULL;
288 }
289
290 if ( aflags == AP_UNAPPLY ) {
291 // If not open, two cases:
292 if ( op->container != sack ) {
293 if (QUERY_FLAG(sack, FLAG_APPLIED)) {
294 CLEAR_FLAG(sack, FLAG_APPLIED);
295 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
296 "You closed %s.",
297 name_sack);
298 esrv_update_item(UPD_FLAGS, op, sack);
299 return 0;
300 }
301 else {
302 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
303 "Already closed %s.",
304 name_sack);
305 return 0;
306 }
307 }
308 // open; same as no special flags
309 aflags = AP_NULL;
310 }
311
312 /* If we have a currently open container, then it needs
313 * to be closed in all cases if we are opening this one up.
314 * We then fall through if appropriate for opening the new
315 * container.
316 */
317 if (op->container && QUERY_FLAG(op->container, FLAG_APPLIED) &&
318 (QUERY_FLAG(sack, FLAG_APPLIED) || sack->env != op) )
319 {
320 tag_t tmp_tag = op->container->count;
321
322 if (op->container->env != op) { /* if container is on the ground */
323 object *part = op->container->head ? op->container->head : op->container;
324 while (part) {
325 part->move_off = 0;
326 part = part->more;
327 }
328 }
329
330 /* Query name before the close event, as the container could be destroyed. */
331 query_name(op->container, name_tmp, MAX_BUF);
332
333 /* Lauwenmark: Handle for plugin close event */
334 if (execute_event(tmp, EVENT_CLOSE, op, NULL, NULL, SCRIPT_FIX_ALL) != 0)
335 return 1;
336
337 draw_ext_info_format(NDI_UNIQUE, 0, op,
338 MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
339 "You close %s.",
340 name_tmp);
341
342 op->container = NULL;
343 if (op->contr != NULL)
344 op->contr->socket.container_position = 0;
345
346 /* The container may have been destroyed by the event handler. */
347 if (!object_was_destroyed(tmp, tmp_tag)) {
348 CLEAR_FLAG(tmp, FLAG_APPLIED);
349 if (set_object_face_main(tmp))
350 esrv_update_item(UPD_FLAGS|UPD_FACE, op, tmp);
351 else
352 esrv_update_item(UPD_FLAGS, op, tmp);
353 }
354 if (tmp == sack)
355 return 1;
356 }
357
358 /* If the player is trying to open it (which he must be doing
359 * if we got here), and it is locked, check to see if player
360 * has the equipment to open it.
361 */
362
363 if (sack->slaying) { /* it's locked */
364 tmp = find_key(op, op, sack);
365 if (tmp) {
366 query_name(tmp, name_tmp, MAX_BUF);
367 draw_ext_info_format(NDI_UNIQUE, 0, op,
368 MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
369 "You unlock %s with %s.",
370 name_sack, name_tmp);
371 } else {
372 draw_ext_info_format(NDI_UNIQUE, 0, op,
373 MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
374 "You don't have the key to unlock %s.",
375 name_sack);
376 return 0;
377 }
378 }
379
380 /* By the time we get here, we have made sure any other container
381 * has been closed and if this is a locked container, the player
382 * has the key to open it.
383 */
384
385 /* There are really two cases - the sack is either on the ground,
386 * or the sack is part of the player's inventory. If on the ground,
387 * we assume that the player is opening it, since if it was being
388 * closed, that would have been taken care of above.
389 */
390
391
392 if (sack->env != op) {
393 /* Hypothetical case - the player is trying to open a sack
394 * that belongs to someone else. This normally should not
395 * happen, but a misbehaving client/player could
396 * try to do it, so let's handle it gracefully.
397 */
398 if (sack->env) {
399 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
400 "You can't open %s",
401 name_sack);
402 return 0;
403 }
404
405 if (sack->nrof > 1) {
406 object *left = object_split(sack, sack->nrof-1, NULL, 0);
407
408 object_insert_in_map_at(left, sack->map, NULL, INS_NO_MERGE, sack->x, sack->y);
409 /* recompute the name so it's nice */
410 query_name(sack, name_sack, MAX_BUF);
411 }
412
413 /* set it so when the player walks off, we can unapply the sack */
414 {
415 object *part = sack->head ? sack->head : sack;
416 while (part) {
417 part->move_off = MOVE_ALL;
418 part = part->more;
419 }
420 }
421
422 CLEAR_FLAG(sack, FLAG_APPLIED);
423 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
424 "You open %s.",
425 name_sack);
426 SET_FLAG(sack, FLAG_APPLIED);
427 op->container = sack;
428 if (op->contr != NULL)
429 op->contr->socket.container_position = 0;
430
431 if (set_object_face_other(sack))
432 esrv_update_item(UPD_FLAGS|UPD_FACE, op, sack);
433 else
434 esrv_update_item(UPD_FLAGS, op, sack);
435 esrv_send_inventory(op, sack);
436 } else { /* sack is in players inventory */
437 if (QUERY_FLAG(sack, FLAG_APPLIED)) { /* readied sack becoming open */
438 CLEAR_FLAG(sack, FLAG_APPLIED);
439 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
440 "You open %s.",
441 name_sack);
442 SET_FLAG(sack, FLAG_APPLIED);
443 op->container = sack;
444 if (op->contr != NULL)
445 op->contr->socket.container_position = 0;
446
447 if (set_object_face_other(sack))
448 esrv_update_item(UPD_FLAGS|UPD_FACE, op, sack);
449 else
450 esrv_update_item(UPD_FLAGS, op, sack);
451 esrv_send_inventory(op, sack);
452 } else {
453 object *left = NULL;
454
455 if (sack->nrof > 1)
456 left = object_split(sack, sack->nrof-1, NULL, 1);
457
458 CLEAR_FLAG(sack, FLAG_APPLIED);
459 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
460 "You readied %s.",
461 name_sack);
462 SET_FLAG(sack, FLAG_APPLIED);
463 esrv_update_item(UPD_FLAGS, op, sack);
464
465 if (left) {
466 object_insert_in_ob(left, sack->env);
467 esrv_send_item(op, left);
468 }
469 }
470 }
471 return 1;
472 }
473
474 /**
475 * Actually makes op learn spell.
476 * Informs player of new spell and binding.
477 *
478 * @param op
479 * player who'll learn the spell.
480 * @param spell
481 * spell to learn.
482 * @param special_prayer
483 * 1 for god-given prayer, 0 else.
484 */
485 void do_learn_spell(object *op, object *spell, int special_prayer) {
486 object *tmp;
487
488 if (op->type != PLAYER) {
489 LOG(llevError, "BUG: do_learn_spell(): not a player\n");
490 return;
491 }
492
493 /* Upgrade special prayers to normal prayers */
494 tmp = check_spell_known(op, spell->name);
495 if (tmp != NULL) {
496 if (special_prayer && !QUERY_FLAG(tmp, FLAG_STARTEQUIP)) {
497 LOG(llevError, "BUG: do_learn_spell(): spell already known, but not marked as startequip\n");
498 return;
499 }
500 return;
501 }
502
503 play_sound_player_only(op->contr, SOUND_TYPE_SPELL, spell, 0, "learn");
504 tmp = object_new();
505 object_copy(spell, tmp);
506 object_insert_in_ob(tmp, op);
507
508 if (special_prayer)
509 SET_FLAG(tmp, FLAG_STARTEQUIP);
510
511 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
512 "Type 'bind cast %s to store the spell in a key.",
513 spell->name);
514
515 esrv_add_spells(op->contr, tmp);
516 }
517
518 /**
519 * Erases spell from player's inventory. Will inform player of loss.
520 *
521 * @param op
522 * player.
523 * @param spell
524 * spell name to forget.
525 */
526 void do_forget_spell(object *op, const char *spell) {
527 object *spob;
528
529 if (op->type != PLAYER) {
530 LOG(llevError, "BUG: do_forget_spell(): not a player\n");
531 return;
532 }
533 spob = check_spell_known(op, spell);
534 if (spob == NULL) {
535 LOG(llevError, "BUG: do_forget_spell(): spell not known\n");
536 return;
537 }
538
539 draw_ext_info_format(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_CURSED,
540 "You lose knowledge of %s.",
541 spell);
542 player_unready_range_ob(op->contr, spob);
543 esrv_remove_spell(op->contr, spob);
544 object_remove(spob);
545 object_free(spob, 0);
546 }
547
548 /**
549 * Checks if an item is restricted to a race. Non players and DMs can always apply.
550 *
551 * @param who
552 * living thing trying to apply an item.
553 * @param item
554 * item being applied.
555 * @return
556 * 0 if item can't be applied, 1 else.
557 */
558 static int apply_check_race_restrictions(object *who, object *item) {
559 char buf[MAX_BUF];
560 sstring restriction;
561
562 if (who->type != PLAYER || QUERY_FLAG(who, FLAG_WIZ))
563 return 1;
564
565 restriction = object_get_value(item, "race_restriction");
566 if (!restriction)
567 return 1;
568
569 snprintf(buf, sizeof(buf), ":%s:", who->race);
570 buf[sizeof(buf)-1] = '\0';
571
572 if (strstr(restriction, buf) != NULL)
573 return 1;
574
575 query_name(item, buf, sizeof(buf));
576 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_PROHIBITION, "Somehow you can't seem to use the %s.", buf);
577
578 return 0;
579 }
580
581 /**
582 * Main apply handler.
583 *
584 * Checks for unpaid items before applying.
585 *
586 * @param op
587 * ::object causing tmp to be applied.
588 * @param tmp
589 * ::object being applied.
590 * @param aflag
591 * special (always apply/unapply) flags. Nothing is done with
592 * them in this function - they are passed to apply_special().
593 * @return
594 * - 0: player or monster can't apply objects of that type
595 * - 1: has been applied, or there was an error applying the object
596 * - 2: objects of that type can't be applied if not in inventory
597 */
598 int apply_manual(object *op, object *tmp, int aflag) {
599 tmp = HEAD(tmp);
600
601 if (QUERY_FLAG(tmp, FLAG_UNPAID) && !QUERY_FLAG(tmp, FLAG_APPLIED)) {
602 if (op->type == PLAYER) {
603 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
604 "You should pay for it first.");
605 return METHOD_SILENT_ERROR;
606 }
607 return 0; /* monsters just skip unpaid items */
608 }
609
610 if (!apply_check_race_restrictions(op, tmp))
611 return METHOD_SILENT_ERROR;
612
613 if (op->contr)
614 play_sound_player_only(op->contr, SOUND_TYPE_ITEM, tmp, 0, "apply");
615
616 return ob_apply(tmp, op, aflag);
617 }
618
619 /**
620 * Living thing is applying an object.
621 *
622 * @param pl
623 * ::object causing op to be applied.
624 * @param op
625 * ::object being applied.
626 * @param aflag
627 * special (always apply/unapply) flags. Nothing is done with
628 * them in this function - they are passed to apply_special().
629 * @param quiet
630 * if 1, suppresses the "don't know how to apply" and "you must get it first"
631 * messages as needed by apply_by_living_below(). There can still be
632 * "but you are floating high above the ground" messages.
633 * @return
634 * - 0: player or monster can't apply objects of that type
635 * - 1: has been applied, or there was an error applying the object
636 * - 2: objects of that type can't be applied if not in inventory
637 */
638 int apply_by_living(object *pl, object *op, int aflag, int quiet) {
639 int tmp;
640
641 if (op->env == NULL && (pl->move_type&MOVE_FLYING)) {
642 /* player is flying and applying object not in inventory */
643 if (!QUERY_FLAG(pl, FLAG_WIZ) && !(op->move_type&MOVE_FLYING)) {
644 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
645 "But you are floating high above the ground!");
646 return 0;
647 }
648 }
649
650 /* Check for PLAYER to avoid a DM to disappear in a puff of smoke if
651 * applied.
652 */
653 if (op->type != PLAYER
654 && QUERY_FLAG(op, FLAG_WAS_WIZ)
655 && !QUERY_FLAG(pl, FLAG_WAS_WIZ)) {
656 play_sound_map(SOUND_TYPE_ITEM, op, 0, "evaporate");
657 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
658 "The object disappears in a puff of smoke!");
659 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
660 "It must have been an illusion.");
661 object_remove(op);
662 object_free(op, 0);
663 return 1;
664 }
665
666 tmp = apply_manual(pl, op, aflag);
667 if (!quiet) {
668 if (tmp == METHOD_UNHANDLED) {
669 char name[MAX_BUF];
670
671 query_name(op, name, MAX_BUF);
672 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
673 "I don't know how to apply the %s.",
674 name);
675 } else if (tmp == METHOD_ERROR)
676 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
677 "You must get it first!\n");
678 else if (tmp == METHOD_SILENT_ERROR)
679 return tmp;
680 }
681 if (tmp == METHOD_OK) {
682 if (op->anim_suffix != NULL)
683 apply_anim_suffix(pl, op->anim_suffix);
684 }
685 return tmp;
686 }
687
688 /**
689 * Attempt to apply the object 'below' the player.
690 * If the player has an open container, we use that for below, otherwise
691 * we use the ground.
692 *
693 * @param pl
694 * player.
695 */
696 void apply_by_living_below(object *pl) {
697 object *tmp;
698 int floors;
699
700 if (pl->contr->transport && pl->contr->transport->type == TRANSPORT) {
701 ob_apply(pl->contr->transport, pl, 0);
702 return;
703 }
704
705 /* If using a container, set the starting item to be the top
706 * item in the container. Otherwise, use the map.
707 */
708 tmp = pl->container != NULL ? pl->container->inv : pl->below;
709
710 /* This is perhaps more complicated. However, I want to make sure that
711 * we don't use a corrupt pointer for the next object, so we get the
712 * next object in the stack before applying. This is can only be a
713 * problem if apply_by_living() has a bug in that it uses the object but
714 * does not return a proper value.
715 */
716 floors = 0;
717 FOR_OB_AND_BELOW_PREPARE(tmp) {
718 if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
719 floors++;
720 else if (floors > 0)
721 return; /* process only floor objects after first floor object */
722
723 /* If it is visible, player can apply it. If it is applied by
724 * person moving on it, also activate. Added code to make it
725 * so that at least one of players movement types be that which
726 * the item needs.
727 */
728 if (!tmp->invisible || (tmp->move_on&pl->move_type)) {
729 if (apply_by_living(pl, tmp, 0, 1) == METHOD_OK)
730 return;
731 }
732 if (floors >= 2)
733 return; /* process at most two floor objects */
734 } FOR_OB_AND_BELOW_FINISH();
735 }
736
737 /**
738 * Unapplies specified item.
739 * No check done on cursed/damned.
740 * Break this out of apply_special() - this is just done
741 * to keep the size of apply_special() to a more manageable size.
742 *
743 * @param who
744 * living that has op removed.
745 * @param op
746 * ::object.
747 * @param aflags
748 * combination of @ref AP_xxx "AP_xxx" flags.
749 * @return
750 * 0.
751 */
752 static int unapply_special(object *who, object *op, int aflags) {
753 char name[MAX_BUF];
754
755 if (op->type != LAMP)
756 CLEAR_FLAG(op, FLAG_APPLIED);
757 query_name(op, name, MAX_BUF);
758 switch (op->type) {
759 case WEAPON:
760 if (!(aflags&AP_NOPRINT))
761 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
762 "You unwield %s.",
763 name);
764 (void)change_abil(who, op);
765 CLEAR_FLAG(who, FLAG_READY_WEAPON);
766 who->current_weapon = NULL;
767 clear_skill(who);
768 break;
769
770 case SKILL: /* allows objects to impart skills */
771 case SKILL_TOOL:
772 if (op != who->chosen_skill)
773 LOG(llevError, "BUG: unapply_special(): applied skill is not a chosen skill\n");
774 if (who->type == PLAYER) {
775 if (who->contr->shoottype == range_skill)
776 who->contr->shoottype = range_none;
777 if (!op->invisible) {
778 if (!(aflags&AP_NOPRINT))
779 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
780 "You stop using the %s.",
781 name);
782 } else {
783 if (!(aflags&AP_NOPRINT))
784 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
785 "You can no longer use the skill: %s.",
786 op->skill);
787 }
788 }
789 (void)change_abil(who, op);
790 who->chosen_skill = NULL;
791 CLEAR_FLAG(who, FLAG_READY_SKILL);
792 break;
793
794 case ARMOUR:
795 case HELMET:
796 case SHIELD:
797 case RING:
798 case BOOTS:
799 case GLOVES:
800 case AMULET:
801 case GIRDLE:
802 case BRACERS:
803 case CLOAK:
804 if (!(aflags&AP_NOPRINT))
805 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
806 "You unwear %s.",
807 name);
808 (void)change_abil(who, op);
809 break;
810
811 case BOW:
812 case WAND:
813 case ROD:
814 clear_skill(who);
815 if (!(aflags&AP_NOPRINT))
816 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
817 "You unready %s.",
818 name);
819 if (who->type == PLAYER)
820 who->contr->shoottype = range_none;
821 else if (op->type == BOW)
822 CLEAR_FLAG(who, FLAG_READY_BOW);
823 else
824 CLEAR_FLAG(who, FLAG_READY_RANGE);
825 break;
826
827 case BUILDER:
828 if (!(aflags&AP_NOPRINT))
829 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
830 "You unready %s.",
831 name);
832 who->contr->shoottype = range_none;
833 who->contr->ranges[range_builder] = NULL;
834 break;
835
836 default:
837 if (!(aflags&AP_NOPRINT))
838 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
839 "You unapply %s.",
840 name);
841 break;
842 }
843
844 fix_object(who);
845
846 if (!(aflags&AP_NO_MERGE)) {
847 object *tmp;
848
849 tmp = object_merge(op, NULL);
850 if (who->type == PLAYER) {
851 if (tmp) { /* it was merged */
852 op = tmp;
853 }
854 esrv_update_item(UPD_FLAGS, who, op);
855 }
856 }
857 return 0;
858 }
859
860 /**
861 * Returns the object that is using body location 'loc'.
862 * Note that 'start' is the first object to start examine - we
863 * then go through the below of this. In this way, you can do
864 * something like:
865 * tmp = get_item_from_body_location(who->inv, 1);
866 * if (tmp) tmp1 = get_item_from_body_location(tmp->below, 1);
867 * to find the second object that may use this location, etc.
868 *
869 * Don't return invisible objects unless they are skill objects.
870 * Invisible other objects that use up body locations can be used as restrictions.
871 *
872 * @param start
873 * object to start from.
874 * @param loc
875 * body position to search. Must be between 0 and NUM_BODY_LOCATIONS-1.
876 * @return
877 * object at position, NULL if none.
878 */
879 static object *get_item_from_body_location(object *start, int loc) {
880 object *tmp;
881
882 if (!start)
883 return NULL;
884
885 tmp = start;
886 FOR_OB_AND_BELOW_PREPARE(tmp)
887 if (QUERY_FLAG(tmp, FLAG_APPLIED)
888 && tmp->body_info[loc]
889 && (!tmp->invisible || tmp->type == SKILL))
890 return tmp;
891 FOR_OB_AND_BELOW_FINISH();
892 return NULL;
893 }
894
895 /**
896 * Remove equipment so an object can be applied.
897 *
898 * This should only be called when it is known
899 * that there are objects to unapply. This makes pretty heavy
900 * use of get_item_from_body_location(). It makes no intelligent choice
901 * on objects - rather, the first that matched is used.
902 *
903 * if aflags has AP_PRINT set, we instead print out what to unapply
904 * instead of doing it. This is a lot less code than having
905 * another function that does just that.
906 *
907 * @param who
908 * living trying to apply op.
909 * @param op
910 * ::object being applied.
911 * @param aflags
912 * combination of @ref AP_xxx "AP_xxx" flags.
913 * @return
914 * 0 on success, 1 if there is some problem.
915 */
916 static int unapply_for_ob(object *who, object *op, int aflags) {
917 int i;
918 object *tmp = NULL, *last;
919 char name[MAX_BUF];
920
921 /* If we are applying a shield or weapon, unapply any equipped shield
922 * or weapons first - only allowed to use one weapon/shield at a time.
923 */
924 if (op->type == WEAPON || op->type == SHIELD) {
925 FOR_INV_PREPARE(who, tmp) {
926 if (QUERY_FLAG(tmp, FLAG_APPLIED) && tmp->type == op->type) {
927 if (!(aflags&AP_IGNORE_CURSE)
928 && !(aflags&AP_PRINT)
929 && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))) {
930 /* In this case, we want to try and remove a
931 * cursed item. While we know it won't work, we
932 * want unapply_special to at least generate the
933 * message.
934 */
935 if (!(aflags&AP_NOPRINT)) {
936 query_name(tmp, name, MAX_BUF);
937 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
938 "No matter how hard you try, you just can't remove %s.",
939 name);
940 }
941 return 1;
942 }
943
944 if (aflags&AP_PRINT) {
945 query_name(tmp, name, MAX_BUF);
946 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
947 name);
948 } else
949 unapply_special(who, tmp, aflags);
950 }
951 } FOR_INV_FINISH();
952 }
953
954 for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
955 /* this used up a slot that we need to free */
956 if (op->body_info[i]) {
957 last = who->inv;
958
959 /* We do a while loop - may need to remove several items
960 * in order to free up enough slots.
961 */
962 while (who->body_used[i]+op->body_info[i] < 0) {
963 tmp = get_item_from_body_location(last, i);
964 if (!tmp)
965 return 1;
966
967 /* If just printing, we don't care about cursed status */
968 if ((aflags&AP_IGNORE_CURSE)
969 || (aflags&AP_PRINT)
970 || (!(QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)))) {
971 if (aflags&AP_PRINT) {
972 query_name(tmp, name, MAX_BUF);
973 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
974 name);
975 } else
976 unapply_special(who, tmp, aflags);
977 } else {
978 /* Cursed item that we can't unequip - tell the player.
979 * Note this could be annoying if this is just one of a
980 * few, so it may not be critical (eg, putting on a
981 * ring and you have one cursed ring.)
982 */
983 if (!(aflags&AP_NOPRINT)) {
984 query_name(tmp, name, MAX_BUF);
985 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
986 "The %s just won't come off",
987 name);
988 }
989 }
990 last = tmp->below;
991 }
992 /* if we got here, this slot is freed up - otherwise, if it
993 * wasn't freed up, the return in the !tmp would have
994 * kicked in.
995 */
996 } /* if op is using this body location */
997 } /* for body locations */
998 return 0;
999 }
1000
1001 /**
1002 * Checks to see if 'who' can apply object 'op'.
1003 *
1004 * @param who
1005 * living thing trying to apply op.
1006 * @param op
1007 * object applied.
1008 * @return
1009 * 0 if apply can be done without anything special.
1010 * Otherwise returns a bitmask of @ref CAN_APPLY_xxx "CAN_APPLY_xxx" - potentially several of these may be
1011 * set, but largely depends on circumstance - in the future, processing
1012 * may be pruned once we know some status (eg, once CAN_APPLY_NEVER
1013 * is set, do we really are what the other flags may be?)
1014 * See include/define.h for detailed description of the meaning of
1015 * these return values.
1016 */
1017 int apply_can_apply_object(const object *who, const object *op) {
1018 int i, retval = 0;
1019 object *tmp = NULL, *ws = NULL;
1020
1021 /* Players have 2 'arm's, so they could in theory equip 2 shields or
1022 * 2 weapons, but we don't want to let them do that. So if they are
1023 * trying to equip a weapon or shield, see if they already have one
1024 * in place and store that way.
1025 */
1026 if (op->type == WEAPON || op->type == SHIELD) {
1027 tmp = object_find_by_type_applied(who, op->type);
1028 if (tmp != NULL) {
1029 retval = CAN_APPLY_UNAPPLY;
1030 ws = tmp;
1031 }
1032 }
1033
1034 for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
1035 if (op->body_info[i]) {
1036 /* Item uses more slots than we have */
1037 if (FABS(op->body_info[i]) > who->body_info[i]) {
1038 /* Could return now for efficiently - rest of info
1039 * below isn't really needed.
1040 */
1041 retval |= CAN_APPLY_NEVER;
1042 } else if (who->body_used[i]+op->body_info[i] < 0) {
1043 /* in this case, equipping this would use more free
1044 * spots than we have.
1045 */
1046 object *tmp1;
1047
1048 /* if we have an applied weapon/shield, and unapply
1049 * it would free enough slots to equip the new item,
1050 * then just set this can continue. We don't care
1051 * about the logic below - if you have shield equipped
1052 * and try to equip another shield, there is only one
1053 * choice. However, the check for the number of body
1054 * locations does take into the account cases where what
1055 * is being applied may be two handed for example.
1056 */
1057 if (ws) {
1058 if (who->body_used[i]-ws->body_info[i]+op->body_info[i] >= 0) {
1059 retval |= CAN_APPLY_UNAPPLY;
1060 continue;
1061 }
1062 }
1063
1064 tmp1 = get_item_from_body_location(who->inv, i);
1065 if (!tmp1)
1066 retval |= CAN_APPLY_NEVER;
1067 else {
1068 /* need to unapply something. However, if this
1069 * something is different than we had found before,
1070 * it means they need to apply multiple objects
1071 */
1072 retval |= CAN_APPLY_UNAPPLY;
1073 if (!tmp)
1074 tmp = tmp1;
1075 else if (tmp != tmp1)
1076 retval |= CAN_APPLY_UNAPPLY_MULT;
1077 /* This object isn't using up all the slots, so
1078 * there must be another. If so, and if the new
1079 * item doesn't need all the slots, the player
1080 * then has a choice.
1081 */
1082 if (who->body_used[i]-tmp1->body_info[i] != who->body_info[i]
1083 && FABS(op->body_info[i]) < who->body_info[i])
1084 retval |= CAN_APPLY_UNAPPLY_CHOICE;
1085
1086 /* Does unequipping 'tmp1' free up enough slots
1087 * for this to be equipped? If not, there must
1088 * be something else to unapply.
1089 */
1090 if (who->body_used[i]+op->body_info[i]-tmp1->body_info[i] < 0)
1091 retval |= CAN_APPLY_UNAPPLY_MULT;
1092 }
1093 } /* if not enough free slots */
1094 } /* if this object uses location i */
1095 } /* for i -> num_body_locations loop */
1096
1097 /* Do checks for can_use_weapon/shield/armour. */
1098 if (IS_WEAPON(op) && !QUERY_FLAG(who, FLAG_USE_WEAPON))
1099 retval |= CAN_APPLY_RESTRICTION;
1100 if (IS_SHIELD(op) && !QUERY_FLAG(who, FLAG_USE_SHIELD))
1101 retval |= CAN_APPLY_RESTRICTION;
1102 if (IS_ARMOR(op) && !QUERY_FLAG(who, FLAG_USE_ARMOUR))
1103 retval |= CAN_APPLY_RESTRICTION;
1104
1105 if (who->type != PLAYER) {
1106 if ((op->type == WAND || op->type == ROD)
1107 && !QUERY_FLAG(who, FLAG_USE_RANGE))
1108 retval |= CAN_APPLY_RESTRICTION;
1109 if (op->type == BOW && !QUERY_FLAG(who, FLAG_USE_BOW))
1110 retval |= CAN_APPLY_RESTRICTION;
1111 if (op->type == RING && !QUERY_FLAG(who, FLAG_USE_RING))
1112 retval |= CAN_APPLY_RESTRICTION;
1113 }
1114 return retval;
1115 }
1116
1117 /**
1118 * This checks to see of the player (who) is sufficient level to use a weapon
1119 * with improves improvements (typically last_eat). We take an int here
1120 * instead of the object so that the improvement code can pass along the
1121 * increased value to see if the object is usable.
1122 * we return 1 (true) if the player can use the weapon.
1123 * See ../types/weapon_improver/weapon_improver.c
1124 *
1125 * @param who
1126 * living to check
1127 * @param improves
1128 * improvement level.
1129 * @return
1130 * 1 if who can use the item, 0 else.
1131 */
1132 int apply_check_weapon_power(const object *who, int improves) {
1133 return (who->level/5)+5 >= improves;
1134 }
1135
1136 /**
1137 * Apply an object.
1138 *
1139 * This function doesn't check for unpaid items, but check other restrictions.
1140 *
1141 * Usage example: apply_special (who, op, AP_UNAPPLY | AP_IGNORE_CURSE)
1142 *
1143 * @param who
1144 * object using op. It can be a monster.
1145 * @param op
1146 * object being used. Should be an equipment type item,
1147 * eg, one which you put on and keep on for a while, and not something
1148 * like a potion or scroll.
1149 * @param aflags
1150 * combination of @ref AP_xxx "AP_xxx" flags.
1151 * @return
1152 * 1 if the action could not be completed, 0 on success.
1153 * However, success is a matter of meaning - if the
1154 * user passes the 'apply' flag to an object already applied,
1155 * nothing is done, and 0 is returned.
1156 */
1157 int apply_special(object *who, object *op, int aflags) {
1158 int basic_flag = aflags&AP_BASIC_FLAGS;
1159 object *tmp, *skop;
1160 char name_op[MAX_BUF];
1161
1162 if (who == NULL) {
1163 LOG(llevError, "apply_special() from object without environment.\n");
1164 return 1;
1165 }
1166
1167 if (op->env != who)
1168 return 1; /* op is not in inventory */
1169
1170 /* trying to unequip op */
1171 if (QUERY_FLAG(op, FLAG_APPLIED)) {
1172 /* always apply, so no reason to unapply */
1173 if (basic_flag == AP_APPLY)
1174 return 0;
1175
1176 if (!(aflags&AP_IGNORE_CURSE)
1177 && (QUERY_FLAG(op, FLAG_CURSED) || QUERY_FLAG(op, FLAG_DAMNED))) {
1178 if (!(aflags&AP_NOPRINT)) {
1179 query_name(op, name_op, MAX_BUF);
1180 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
1181 "No matter how hard you try, you just can't remove %s.",
1182 name_op);
1183 }
1184 return 1;
1185 }
1186 return unapply_special(who, op, aflags);
1187 }
1188
1189 if (basic_flag == AP_UNAPPLY)
1190 return 0;
1191
1192 if (!apply_check_apply_restrictions(who, op, aflags))
1193 return 1;
1194
1195 if (op->skill && op->type != SKILL && op->type != SKILL_TOOL) {
1196 skop = find_skill_by_name(who, op->skill);
1197 if (!skop) {
1198 if (!(aflags&AP_NOPRINT))
1199 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
1200 "You need the %s skill to use this item!",
1201 op->skill);
1202 if (who->type == PLAYER)
1203 return 1;
1204
1205 /* monsters do not care about missing skills */
1206 } else
1207 /* While experience will be credited properly, we want to
1208 * change the skill so that the dam and wc get updated
1209 */
1210 change_skill(who, skop, (aflags&AP_NOPRINT));
1211 } else
1212 skop = NULL;
1213
1214 if (!apply_check_item_power(who, op, aflags))
1215 return 1;
1216
1217 if (!apply_check_personalized_blessings(who, op))
1218 return 1;
1219
1220 /* Ok. We are now at the state where we can apply the new object.
1221 * Note that we don't have the checks for can_use_...
1222 * below - that is already taken care of by apply_can_apply_object().
1223 */
1224
1225 tmp = op->nrof <= 1 ? NULL : object_split(op, op->nrof-1, NULL, 0);
1226
1227 switch (op->type) {
1228 case WEAPON:
1229 if (!apply_check_weapon_power(who, op->last_eat)) {
1230 if (!(aflags&AP_NOPRINT))
1231 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
1232 "That weapon is too powerful for you to use. It would consume your soul!");
1233 if (tmp != NULL)
1234 (void)object_insert_in_ob(tmp, who);
1235 return 1;
1236 }
1237
1238 if (!apply_check_owner(who, op, aflags)) {
1239 if (tmp != NULL)
1240 (void)object_insert_in_ob(tmp, who);
1241 return 1;
1242 }
1243
1244 SET_FLAG(op, FLAG_APPLIED);
1245
1246 if (skop)
1247 change_skill(who, skop, 1);
1248 SET_FLAG(who, FLAG_READY_WEAPON);
1249
1250 if (!(aflags&AP_NOPRINT)) {
1251 query_name(op, name_op, MAX_BUF);
1252 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1253 "You wield %s.",
1254 name_op);
1255 }
1256
1257 (void)change_abil(who, op);
1258 break;
1259
1260 case ARMOUR:
1261 case HELMET:
1262 case SHIELD:
1263 case BOOTS:
1264 case GLOVES:
1265 case GIRDLE:
1266 case BRACERS:
1267 case CLOAK:
1268 case RING:
1269 case AMULET:
1270 SET_FLAG(op, FLAG_APPLIED);
1271 if (!(aflags&AP_NOPRINT)) {
1272 query_name(op, name_op, MAX_BUF);
1273 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1274 "You wear %s.",
1275 name_op);
1276 }
1277 (void)change_abil(who, op);
1278 break;
1279
1280 /* this part is needed for skill-tools */
1281 case SKILL:
1282 case SKILL_TOOL:
1283 if (who->chosen_skill) {
1284 LOG(llevError, "BUG: apply_special(): can't apply two skills\n");
1285 return 1;
1286 }
1287
1288 apply_update_ranged_skill(who, op, aflags);
1289 SET_FLAG(op, FLAG_APPLIED);
1290 (void)change_abil(who, op);
1291 who->chosen_skill = op;
1292 SET_FLAG(who, FLAG_READY_SKILL);
1293 break;
1294
1295 case BOW:
1296 if (!apply_check_weapon_power(who, op->last_eat)) {
1297 if (!(aflags&AP_NOPRINT))
1298 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
1299 "That weapon is too powerful for you to use. It would consume your soul!");
1300 if (tmp != NULL)
1301 (void)object_insert_in_ob(tmp, who);
1302 return 1;
1303 }
1304
1305 if (!apply_check_owner(who, op, aflags)) {
1306 if (tmp != NULL)
1307 (void)object_insert_in_ob(tmp, who);
1308 return 1;
1309 }
1310 /*FALLTHROUGH*/
1311 case WAND:
1312 case ROD:
1313 /* check for skill, alter player status */
1314 SET_FLAG(op, FLAG_APPLIED);
1315 if (skop)
1316 change_skill(who, skop, 0);
1317 if (!(aflags&AP_NOPRINT)) {
1318 query_name(op, name_op, MAX_BUF);
1319 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1320 "You ready %s.",
1321 name_op);
1322 }
1323 if (who->type == PLAYER) {
1324 if (op->type == BOW) {
1325 (void)change_abil(who, op);
1326 if (!(aflags&AP_NOPRINT)) {
1327 query_name(op, name_op, MAX_BUF);
1328 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1329 "You will now fire %s with %s.",
1330 op->race ? op->race : "nothing",
1331 name_op);
1332 }
1333 who->contr->shoottype = range_bow;
1334 } else
1335 who->contr->shoottype = range_misc;
1336 } else {
1337 if (op->type == BOW)
1338 SET_FLAG(who, FLAG_READY_BOW);
1339 else
1340 SET_FLAG(who, FLAG_READY_RANGE);
1341 }
1342 break;
1343
1344 case BUILDER:
1345 if (who->contr->ranges[range_builder])
1346 unapply_special(who, who->contr->ranges[range_builder], 0);
1347 who->contr->shoottype = range_builder;
1348 who->contr->ranges[range_builder] = op;
1349 if (!(aflags&AP_NOPRINT)) {
1350 query_name(op, name_op, MAX_BUF);
1351 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1352 "You ready your %s.",
1353 name_op);
1354 }
1355 break;
1356
1357 default:
1358 query_name(op, name_op, MAX_BUF);
1359 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1360 "You apply %s.",
1361 name_op);
1362 break;
1363 } /* end of switch op->type */
1364
1365 SET_FLAG(op, FLAG_APPLIED);
1366
1367 if (tmp != NULL)
1368 tmp = object_insert_in_ob(tmp, who);
1369
1370 fix_object(who);
1371
1372 /* We exclude spell casting objects. The fire code will set the
1373 * been applied flag when they are used - until that point,
1374 * you don't know anything about them.
1375 */
1376 if (who->type == PLAYER && op->type != WAND && op->type != ROD)
1377 SET_FLAG(op, FLAG_BEEN_APPLIED);
1378
1379 if (QUERY_FLAG(op, FLAG_CURSED) || QUERY_FLAG(op, FLAG_DAMNED)) {
1380 if (who->type == PLAYER) {
1381 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_CURSED,
1382 "Oops, it feels deadly cold!");
1383 SET_FLAG(op, FLAG_KNOWN_CURSED);
1384 }
1385 }
1386 if (who->type == PLAYER)
1387 esrv_update_item(UPD_NAME|UPD_NROF|UPD_FLAGS|UPD_WEIGHT, who, op);
1388 return 0;
1389 }
1390
1391 /**
1392 * Map was just loaded, handle op's initialization.
1393 *
1394 * Generates shop floor's item, and treasures.
1395 *
1396 * @param op
1397 * object to initialize.
1398 * @return
1399 * 1 if object was initialized, 0 else.
1400 */
1401 int apply_auto(object *op) {
1402 object *tmp;
1403
1404 switch (op->type) {
1405 case SHOP_FLOOR:
1406 if (!HAS_RANDOM_ITEMS(op))
1407 return 0;
1408 do {
1409 int i;
1410
1411 i = 10; /* let's give it 10 tries */
1412 while ((tmp = generate_treasure(op->randomitems, op->stats.exp ? (int)op->stats.exp : MAX(op->map->difficulty, 5))) == NULL
1413 && --i)
1414 ;
1415 if (tmp == NULL)
1416 return 0;
1417 if (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED)) {
1418 object_free(tmp, FREE_OBJ_NO_DESTROY_CALLBACK);
1419 tmp = NULL;
1420 }
1421 } while (!tmp);
1422 SET_FLAG(tmp, FLAG_UNPAID);
1423 object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
1424 CLEAR_FLAG(op, FLAG_AUTO_APPLY);
1425 identify(tmp);
1426 return 1;
1427 break;
1428
1429 case TREASURE:
1430 if (QUERY_FLAG(op, FLAG_IS_A_TEMPLATE))
1431 return 0;
1432 while (op->stats.hp-- > 0)
1433 create_treasure(op->randomitems, op, (op->map ? GT_ENVIRONMENT : 0) | (QUERY_FLAG(op, FLAG_BLESSED) ? GT_ONLY_GOOD : 0), op->stats.exp ? (int)op->stats.exp : op->map == NULL ? 14 : op->map->difficulty, 0);
1434
1435 /* If we generated an object and put it in this object's
1436 * inventory, move it to the parent object as the current
1437 * object is about to disappear. An example of this item
1438 * is the random_ *stuff that is put inside other objects.
1439 */
1440 FOR_INV_PREPARE(op, tmp) {
1441 object_remove(tmp);
1442 if (op->env)
1443 object_insert_in_ob(tmp, op->env);
1444 else
1445 object_free_drop_inventory(tmp);
1446 } FOR_INV_FINISH();
1447 object_remove(op);
1448 object_free(op, FREE_OBJ_NO_DESTROY_CALLBACK);
1449 break;
1450 }
1451 return 0;
1452 }
1453
1454 /**
1455 * Go through the entire map (only the first time
1456 * when an original map is loaded) and performs special actions for
1457 * certain objects (most initialization of chests and creation of
1458 * treasures and stuff). Calls apply_auto() if appropriate.
1459 *
1460 * @param m
1461 * map to fix.
1462 */
1463
1464 void apply_auto_fix(mapstruct *m) {
1465 int x, y;
1466
1467 if (m == NULL)
1468 return;
1469
1470 for (x = 0; x < MAP_WIDTH(m); x++)
1471 for (y = 0; y < MAP_HEIGHT(m); y++)
1472 FOR_MAP_PREPARE(m, x, y, tmp) {
1473 if (tmp->inv) {
1474 FOR_INV_PREPARE(tmp, invtmp) {
1475 if (QUERY_FLAG(invtmp, FLAG_AUTO_APPLY))
1476 apply_auto(invtmp);
1477 else if (invtmp->type == TREASURE && HAS_RANDOM_ITEMS(invtmp)) {
1478 while (invtmp->stats.hp-- > 0)
1479 create_treasure(invtmp->randomitems, invtmp, 0, m->difficulty, 0);
1480 invtmp->randomitems = NULL;
1481 } else if (invtmp && invtmp->arch
1482 && invtmp->type != TREASURE
1483 && invtmp->type != SPELL
1484 && invtmp->type != CLASS
1485 && HAS_RANDOM_ITEMS(invtmp)) {
1486 create_treasure(invtmp->randomitems, invtmp, 0, m->difficulty, 0);
1487 /* Need to clear this so that we never try to
1488 * create treasure again for this object
1489 */
1490 invtmp->randomitems = NULL;
1491 }
1492 } FOR_INV_FINISH();
1493 /* This is really temporary - the code at the
1494 * bottom will also set randomitems to null.
1495 * The problem is there are bunches of maps/players
1496 * already out there with items that have spells
1497 * which haven't had the randomitems set
1498 * to null yet.
1499 * MSW 2004-05-13
1500 *
1501 * And if it's a spellbook, it's better to set
1502 * randomitems to NULL too, else you get two spells
1503 * in the book ^_-
1504 * Ryo 2004-08-16
1505 */
1506 if (tmp->type == WAND
1507 || tmp->type == ROD
1508 || tmp->type == SCROLL
1509 || tmp->type == FIREWALL
1510 || tmp->type == POTION
1511 || tmp->type == ALTAR
1512 || tmp->type == SPELLBOOK)
1513 tmp->randomitems = NULL;
1514 }
1515
1516 if (QUERY_FLAG(tmp, FLAG_AUTO_APPLY))
1517 apply_auto(tmp);
1518 else if ((tmp->type == TREASURE || tmp->type == CONTAINER)
1519 && HAS_RANDOM_ITEMS(tmp)) {
1520 while (tmp->stats.hp-- > 0)
1521 create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
1522 tmp->randomitems = NULL;
1523 } else if (tmp->type == TIMED_GATE) {
1524 object *head = HEAD(tmp);
1525
1526 if (QUERY_FLAG(head, FLAG_IS_LINKED)) {
1527 tmp->speed = 0;
1528 object_update_speed(tmp);
1529 }
1530 }
1531 /* This function can be called every time a map is loaded,
1532 * even when swapping back in. As such, we don't want to
1533 * create the treasure over and over again, so after we
1534 * generate the treasure, blank out randomitems so if it
1535 * is swapped in again, it won't make anything. This is a
1536 * problem for the above objects, because they have
1537 * counters which say how many times to make the treasure.
1538 */
1539 else if (tmp
1540 && tmp->arch
1541 && tmp->type != PLAYER
1542 && tmp->type != TREASURE
1543 && tmp->type != SPELL
1544 && tmp->type != PLAYER_CHANGER
1545 && tmp->type != CLASS
1546 && HAS_RANDOM_ITEMS(tmp)) {
1547 create_treasure(tmp->randomitems, tmp, 0, m->difficulty, 0);
1548 tmp->randomitems = NULL;
1549 }
1550
1551 if (QUERY_FLAG(tmp, FLAG_MONSTER))
1552 monster_check_apply_all(tmp);
1553 } FOR_MAP_FINISH();
1554
1555 for (x = 0; x < MAP_WIDTH(m); x++)
1556 for (y = 0; y < MAP_HEIGHT(m); y++)
1557 FOR_MAP_PREPARE(m, x, y, tmp) {
1558 if (tmp->above
1559 && (tmp->type == TRIGGER_BUTTON || tmp->type == TRIGGER_PEDESTAL))
1560 check_trigger(tmp, tmp->above);
1561 } FOR_MAP_FINISH();
1562 }
1563
1564 /**
1565 * op made some mistake with a scroll, this takes care of punishment.
1566 * scroll_failure()- hacked directly from spell_failure
1567 *
1568 * If settings.spell_failure_effects is FALSE, the only nasty things
1569 * that can happen are weird spell cast, or mana drain.
1570 *
1571 * @param op
1572 * who failed.
1573 * @param failure
1574 * what kind of nasty things happen.
1575 * @param power
1576 * the higher the value, the worse the thing that happens.
1577 */
1578 void scroll_failure(object *op, int failure, int power) {
1579 if (abs(failure/4) > power)
1580 power = abs(failure/4); /* set minimum effect */
1581
1582 if (failure <= -1 && failure > -15) {/* wonder */
1583 object *tmp;
1584
1585 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
1586 "Your spell warps!");
1587 tmp = create_archetype(SPELL_WONDER);
1588 cast_wonder(op, op, 0, tmp);
1589 if (op->stats.sp < 0)
1590 /* For some reason the sp can become negative here. */
1591 op->stats.sp = 0;
1592 object_free(tmp, FREE_OBJ_NO_DESTROY_CALLBACK);
1593 return;
1594 }
1595
1596 if (settings.spell_failure_effects == TRUE) {
1597 if (failure <= -35 && failure > -60) { /* confusion */
1598 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
1599 "The magic recoils on you!");
1600 confuse_living(op, op, power);
1601 return;
1602 }
1603
1604 if (failure <= -60 && failure > -70) {/* paralysis */
1605 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
1606 "The magic recoils and paralyzes you!");
1607 paralyze_living(op, power);
1608 return;
1609 }
1610
1611 if (failure <= -70 && failure > -80) {/* blind */
1612 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
1613 "The magic recoils on you!");
1614 blind_living(op, op, power);
1615 return;
1616 }
1617
1618 if (failure <= -80) {/* blast the immediate area */
1619 object *tmp;
1620
1621 tmp = create_archetype(LOOSE_MANA);
1622 cast_magic_storm(op, tmp, power);
1623 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
1624 "You unlease uncontrolled mana!");
1625 object_free(tmp, FREE_OBJ_NO_DESTROY_CALLBACK);
1626 return;
1627 }
1628 }
1629 /* Either no spell failure on this server, or wrong values,
1630 * in any case let's punish.
1631 */
1632 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
1633 "Your mana is drained!");
1634 op->stats.sp -= random_roll(0, power-1, op, PREFER_LOW);
1635 if (op->stats.sp < 0)
1636 op->stats.sp = 0;
1637 }
1638
1639 /**
1640 * Applies (race) changes to a player.
1641 * @param pl
1642 * object to change.
1643 * @param change
1644 * what kind of changes to apply. Should be of type CLASS.
1645 * @param limit_stats
1646 * uses the AC_PLAYER_STAT defines from define.h:
1647 * AC_PLAYER_STAT_LIMIT: Limit stats to racial maximum
1648 * AC_PLAYER_STAT_NO_CHANGE: Do not make any stat adjustments
1649 */
1650 void apply_changes_to_player(object *pl, object *change, int limit_stats) {
1651 int i, j;
1652 int excess_stat = 0; /* if the stat goes over the maximum
1653 * for the race, put the excess stat some
1654 * where else.
1655 */
1656
1657 if (change->type != CLASS) return;
1658
1659 /* the following code assigns stats up to the stat max
1660 * for the race, and if the stat max is exceeded,
1661 * tries to randomly reassign the excess stat
1662 */
1663 if (! (limit_stats & AC_PLAYER_STAT_NO_CHANGE)) {
1664 for (i = 0; i < NUM_STATS; i++) {
1665 int8_t stat = get_attr_value(&pl->contr->orig_stats, i);
1666 int race_bonus = get_attr_value(&pl->arch->clone.stats, i);
1667
1668 stat += get_attr_value(&change->stats, i);
1669 if (limit_stats & AC_PLAYER_STAT_LIMIT) {
1670 if (stat > 20+race_bonus) {
1671 excess_stat++;
1672 stat = 20+race_bonus;
1673 } else if (stat < 1) {
1674 /* I didn't see any code here before to make sure minimum
1675 * stats were enforced - maybe it was just dumb
1676 * luck that one would not have a stat low enough that then
1677 * has a stat penalty for class that would bring it negative?
1678 * I imagine a negative stat would crash the server pretty
1679 * quickly - MSW, Sept 2010
1680 */
1681 excess_stat += stat;
1682 stat = 1;
1683 }
1684 }
1685 set_attr_value(&pl->contr->orig_stats, i, stat);
1686 }
1687
1688 /* Maybe we should randomly deduct stats in this case?
1689 * It's will go away sometime soon in any case.
1690 */
1691 if (excess_stat < 0) excess_stat = 0;
1692
1693 /* We don't put an explicit check for limit_stats here -
1694 * excess stat will never be 0 if limit_stats is not
1695 * true.
1696 */
1697 for (j = 0; excess_stat > 0 && j < 100; j++) {
1698 /* try 100 times to assign excess stats */
1699 int i = rndm(0, NUM_STATS-1);
1700 int stat = get_attr_value(&pl->contr->orig_stats, i);
1701 int race_bonus = get_attr_value(&pl->arch->clone.stats, i);
1702
1703 if (i == CHARISMA) {
1704 continue; /* exclude cha from this */
1705 }
1706
1707 if (stat < 20+race_bonus) {
1708 change_attr_value(&pl->contr->orig_stats, i, 1);
1709 excess_stat--;
1710 }
1711 }
1712 }
1713 /* Done with stat processing */
1714
1715 /* insert the randomitems from the change's treasurelist into
1716 * the player ref: player.c
1717 */
1718 if (change->randomitems != NULL)
1719 give_initial_items(pl, change->randomitems);
1720
1721
1722 /* set up the face, for some races. */
1723
1724 /* first, look for the force object banning changing the
1725 * face. Certain races never change face with class.
1726 */
1727 int has_noclassfacechange = (object_find_by_name(pl, "NOCLASSFACECHANGE") != NULL);
1728 const Animations *anim = NULL;
1729
1730 if (change->anim_suffix) {
1731 char buf[MAX_BUF];
1732
1733 snprintf(buf, MAX_BUF, "%s_%s", pl->animation->name, change->anim_suffix);
1734 anim = try_find_animation(buf);
1735 if (anim) {
1736 pl->animation = anim;
1737 pl->anim_speed = -1;
1738 CLEAR_FLAG(pl, FLAG_ANIMATE);
1739 animate_object(pl, pl->facing);
1740 }
1741 }
1742 /* Check for anim == -1 so that we can override specific class faces for races.
1743 * This allows us to have custom class faces on the races that lack noclassfacechange
1744 *
1745 * Daniel Hawkins 2020-09-08
1746 */
1747 if ((!has_noclassfacechange) && anim == 0) {
1748 pl->animation = GET_ANIM(change);
1749 pl->face = change->face;
1750
1751 if (QUERY_FLAG(change, FLAG_ANIMATE))
1752 SET_FLAG(pl, FLAG_ANIMATE);
1753 else
1754 CLEAR_FLAG(pl, FLAG_ANIMATE);
1755 }
1756
1757 /* Hard coding in class name is a horrible idea - lets
1758 * use the supported mechanism for this
1759 */
1760 if (object_present_in_ob_by_name(FORCE, "no weapon force", pl))
1761 CLEAR_FLAG(pl, FLAG_USE_WEAPON);
1762
1763 }
1764
1765 /**
1766 * Checks for general apply restrictions (no body, prohibited by god, conflicts
1767 * with other items, etc.)
1768 *
1769 * @param who
1770 * the object applying the item
1771 * @param op
1772 * the item being applied
1773 * @param aflags
1774 * combinaison of @ref AP_xxx "AP_xxx" flags
1775 * @return
1776 * whether applying is possible
1777 */
1778 static int apply_check_apply_restrictions(object *who, object *op, int aflags) {
1779 int i;
1780
1781 i = apply_can_apply_object(who, op);
1782 if (i == 0)
1783 return 1;
1784
1785 /* Can't just apply this object. Lets see why not and what to do */
1786
1787 if (i&CAN_APPLY_NEVER) {
1788 if (!(aflags&AP_NOPRINT)) {
1789 char name_op[MAX_BUF];
1790
1791 query_name(op, name_op, MAX_BUF);
1792 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BADBODY,
1793 "You don't have the body to use a %s",
1794 name_op);
1795 }
1796 return 0;
1797 }
1798
1799 if (i&CAN_APPLY_RESTRICTION) {
1800 if (!(aflags&AP_NOPRINT)) {
1801 char name_op[MAX_BUF];
1802
1803 query_name(op, name_op, MAX_BUF);
1804 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_PROHIBITION,
1805 "You have a prohibition against using a %s",
1806 name_op);
1807 }
1808 return 0;
1809 }
1810
1811 if (who->type != PLAYER) {
1812 /* Some error, so don't try to equip something more */
1813 return !unapply_for_ob(who, op, aflags);
1814 }
1815
1816 if (who->contr->unapply == unapply_never
1817 || (i&CAN_APPLY_UNAPPLY_CHOICE && who->contr->unapply == unapply_nochoice)) {
1818 if (!(aflags&AP_NOPRINT))
1819 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
1820 "You need to unapply some item(s):");
1821 unapply_for_ob(who, op, AP_PRINT);
1822 return 0;
1823 }
1824
1825 if (who->contr->unapply == unapply_always
1826 || !(i&CAN_APPLY_UNAPPLY_CHOICE)) {
1827 return !unapply_for_ob(who, op, aflags);
1828 }
1829
1830 return 1;
1831 }
1832
1833 /**
1834 * Checks for item power restrictions when applying an item.
1835 *
1836 * @param who
1837 * the object applying the item
1838 * @param op
1839 * the item being applied
1840 * @param aflags
1841 * combinaison of @ref AP_xxx "AP_xxx" flags
1842 * @return
1843 * whether applying is possible
1844 */
1845 static int apply_check_item_power(const object *who, const object *op, int aflags) {
1846 if (who->type != PLAYER)
1847 return 1;
1848
1849 if (op->item_power == 0
1850 || op->item_power+who->contr->item_power <= settings.item_power_factor*who->level)
1851 return 1;
1852
1853 if (!(aflags&AP_NOPRINT))
1854 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
1855 "Equipping that combined with other items would consume your soul!");
1856 return 0;
1857 }
1858
1859 /**
1860 * If personalized blessings are activated, the weapon can bite
1861 * the wielder if he/she is not the one who initially blessed it.
1862 * Chances of being hurt depend on the experience amount
1863 * ("willpower") the object has, compared to the experience
1864 * amount of the wielder.
1865 *
1866 * @param who
1867 * the object applying the item
1868 * @param op
1869 * the item being applied
1870 * @return
1871 * whether applying is possible
1872 */
1873 static int apply_check_personalized_blessings(object *who, const object *op) {
1874 const char *owner;
1875 const char *will;
1876 long item_will;
1877 long margin;
1878 const char *msg;
1879 int random_effect;
1880 int damage_percentile;
1881
1882 if (!settings.personalized_blessings) {
1883 return 1;
1884 }
1885
1886 owner = object_get_value(op, "item_owner");
1887 if (owner == NULL || strcmp(owner, who->name) == 0)
1888 return 1;
1889
1890 will = object_get_value(op, "item_willpower");
1891 item_will = will != NULL ? atol(will) : 0;
1892 if (item_will > who->stats.exp) {
1893 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
1894 "This %s refuses to serve you - it keeps evading your hand !",
1895 op->name);
1896 return 0;
1897 }
1898
1899 margin = item_will != 0 ? who->stats.exp/item_will : who->stats.exp;
1900 random_effect = random_roll(0, 100, who, 1)-margin*20;
1901 if (random_effect > 80) {
1902 msg = "You don't know why, but you have the feeling that the %s is angry at you !";
1903 damage_percentile = 60;
1904 } else if (random_effect > 60) {
1905 msg = "The %s seems to look at you nastily !";
1906 damage_percentile = 45;
1907 } else if (random_effect > 40) {
1908 msg = "You have the strange feeling that the %s is annoyed...";
1909 damage_percentile = 30;
1910 } else if (random_effect > 20) {
1911 msg = "The %s seems tired, or bored, in a way. Very strange !";
1912 damage_percentile = 15;
1913 } else if (random_effect > 0) {
1914 msg = "You hear the %s sighing !";
1915 damage_percentile = 0;
1916 } else {
1917 msg = NULL;
1918 damage_percentile = 0;
1919 }
1920 if (msg != NULL)
1921 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1922 msg, op->name);
1923 if (damage_percentile > 0) {
1924 int weapon_bite = (who->stats.hp*damage_percentile)/100;
1925 if (weapon_bite < 1)
1926 weapon_bite = 1;
1927 who->stats.hp -= weapon_bite;
1928 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
1929 "You get a nasty bite in the hand !");
1930 }
1931
1932 return 1;
1933 }
1934
1935 /**
1936 * Checks that the item's owner matches the applier. If the weapon does not
1937 * have the name as the character, can't use it. (Ragnarok's sword attempted to
1938 * be used by Foo: won't work).
1939 *
1940 * @param who
1941 * the object applying the item
1942 * @param op
1943 * the item being applied
1944 * @param aflags
1945 * combination of @ref AP_xxx "AP_xxx" flags
1946 * @return
1947 * whether applying is possible
1948 */
1949 static int apply_check_owner(const object *who, const object *op, int aflags) {
1950 const char *quotepos;
1951
1952 if (op->level == 0)
1953 return 1;
1954
1955 quotepos = strstr(op->name, "'");
1956 if (quotepos == NULL || strncmp(op->name, who->name, quotepos-op->name) == 0)
1957 return 1;
1958
1959 if (!(aflags&AP_NOPRINT))
1960 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
1961 "The weapon does not recognize you as its owner.");
1962 return 0;
1963 }
1964
1965 /**
1966 * Updates ranged skill information.
1967 *
1968 * @param who
1969 * the object applying the item
1970 * @param op
1971 * the item being applied
1972 * @param aflags
1973 * combination of @ref AP_xxx "AP_xxx" flags
1974 */
1975 static void apply_update_ranged_skill(const object *who, object *op, int aflags) {
1976 if (who->type != PLAYER) {
1977 return;
1978 }
1979
1980 who->contr->shoottype = range_skill;
1981 who->contr->ranges[range_skill] = op;
1982 if (op->invisible) {
1983 if (!(aflags&AP_NOPRINT))
1984 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1985 "Readied skill: %s.",
1986 op->skill ? op->skill : op->name);
1987 } else {
1988 if (!(aflags&AP_NOPRINT)) {
1989 char name_op[MAX_BUF];
1990
1991 query_name(op, name_op, MAX_BUF);
1992 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1993 "You ready %s.",
1994 name_op);
1995 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
1996 "You can now use the skill: %s.",
1997 op->skill);
1998 }
1999 }
2000 }