ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/apply.C
Revision: 1.247
Committed: Tue Apr 6 03:59:19 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.246: +3 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.137 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.61 *
4 root 1.228 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.227 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992 Frank Tore Johansen
7 pippijn 1.61 *
8 root 1.201 * Deliantra is free software: you can redistribute it and/or modify it under
9     * the terms of the Affero GNU General Public License as published by the
10     * Free Software Foundation, either version 3 of the License, or (at your
11     * option) any later version.
12 pippijn 1.61 *
13 root 1.111 * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17 root 1.107 *
18 root 1.201 * You should have received a copy of the Affero GNU General Public License
19     * and the GNU General Public License along with this program. If not, see
20     * <http://www.gnu.org/licenses/>.
21     *
22 root 1.137 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.61 */
24 elmex 1.1
25 root 1.79 #include <cmath>
26    
27 elmex 1.1 #include <global.h>
28     #include <living.h>
29     #include <spells.h>
30     #include <skills.h>
31     #include <tod.h>
32    
33 root 1.51 #include <sproto.h>
34 elmex 1.1
35 root 1.245 typedef bitset<NUM_TYPES> types_t;
36    
37     // these must be in the inventory before they can be applied
38    
39     static const struct may_attempt_inv : types_t
40     {
41     may_attempt_inv ()
42     {
43     set (WEAPON);
44     set (ARMOUR);
45     set (BOOTS);
46     set (GLOVES);
47     set (AMULET);
48     set (GIRDLE);
49     set (BRACERS);
50     set (SHIELD);
51     set (HELMET);
52     set (RING);
53     set (CLOAK);
54     set (WAND);
55     set (ROD);
56     set (HORN);
57     set (SKILL);
58     set (SPELL);
59     set (BOW);
60     set (RANGED);
61     set (BUILDER);
62     set (SKILL_TOOL);
63     }
64     } may_attempt_inv;
65    
66     // these only make sense for the player
67    
68     static const struct may_attempt_player : types_t
69     {
70     may_attempt_player ()
71     {
72     set (EXIT);
73     set (BOOK);
74     set (SKILLSCROLL);
75     set (SPELLBOOK);
76     set (TREASURE);
77     set (SAVEBED);
78     set (ARMOUR_IMPROVER);
79     set (WEAPON_IMPROVER);
80     set (CLOCK);
81     set (MENU);
82     set (LIGHTER); /* for lighting torches/lanterns/etc */
83     set (INSCRIBABLE);
84     set (SIGN);
85     set (BOOK);
86     }
87     } may_attempt_player;
88    
89     // applying these _can_ be attempted, others cannot
90     // be applied at all. used by e.g. apply below.
91    
92     static const struct may_attempt : types_t
93     {
94     may_attempt ()
95     : types_t ((types_t)may_attempt_player | (types_t)may_attempt_inv)
96     {
97     set (T_HANDLE);
98     set (TRIGGER);
99     set (SCROLL);
100     set (POTION);
101     set (CLOSE_CON);
102     set (CONTAINER);
103     set (LAMP);
104     set (TORCH);
105     set (DRINK);
106     set (FOOD);
107     set (FLESH);
108     set (POISON);
109     set (POWER_CRYSTAL);
110     set (ITEM_TRANSFORMER);
111     }
112     } may_attempt;
113    
114     /****************************************************************************
115     * Weapon improvement code follows
116     ****************************************************************************/
117    
118 elmex 1.1 /**
119 root 1.245 * This function just checks whether who can handle equipping an item
120     * with item_power.
121 elmex 1.1 */
122 root 1.245 static bool
123     check_item_power (object *who, int item_power)
124 elmex 1.1 {
125 root 1.245 if (who->type == PLAYER
126     && item_power
127     && item_power + who->contr->item_power > settings.item_power_factor * who->level)
128     return false;
129 root 1.22 else
130 root 1.245 return true;
131 elmex 1.1 }
132    
133     /**
134 root 1.245 * This returns the sum of nrof of item (arch name).
135 elmex 1.1 */
136 root 1.22 static int
137 root 1.245 check_item (object *op, shstr_cmp item)
138 elmex 1.1 {
139 root 1.245 int count = 0;
140 root 1.123
141 root 1.245 if (!item)
142 root 1.22 return 0;
143 elmex 1.1
144 root 1.245 for (op = op->below; op; op = op->below)
145     if (op->arch->archname == item)
146     if (!QUERY_FLAG (op, FLAG_CURSED) && !QUERY_FLAG (op, FLAG_DAMNED)
147     && /* Loophole bug? -FD- */ !QUERY_FLAG (op, FLAG_UNPAID))
148     count += op->number_of ();
149 elmex 1.1
150 root 1.245 return count;
151     }
152 root 1.123
153 root 1.245 /**
154     * This removes 'nrof' of what item->slaying says to remove.
155     * op is typically the player, which is only
156     * really used to determine what space to look at.
157     * Modified to only eat 'nrof' of objects.
158     */
159     static void
160     eat_item (object *op, shstr_cmp item, uint32 nrof)
161     {
162     object *prev;
163 root 1.123
164 root 1.245 prev = op;
165     op = op->below;
166 elmex 1.1
167 root 1.245 while (op)
168 root 1.22 {
169 root 1.245 if (op->arch->archname == item)
170 root 1.22 {
171 root 1.245 if (op->nrof >= nrof)
172 root 1.22 {
173 root 1.245 op->decrease (nrof);
174     return;
175 root 1.22 }
176     else
177     {
178 root 1.245 op->decrease (nrof);
179     nrof -= op->nrof;
180 root 1.22 }
181 root 1.245
182     op = prev;
183 root 1.14 }
184 root 1.245
185     prev = op;
186     op = op->below;
187 elmex 1.1 }
188 root 1.245 }
189    
190     /**
191     * Returns how many items of type improver->slaying there are under op.
192     * Will display a message if none found, and 1 if improver->slaying is NULL.
193     */
194     static int
195     check_sacrifice (object *op, const object *improver)
196     {
197     int count = 0;
198 root 1.123
199 root 1.245 if (improver->slaying)
200     {
201     count = check_item (op, improver->slaying);
202     if (count < 1)
203     {
204     op->failmsg (format ("The gods want more %ss", &improver->slaying));
205     return 0;
206     }
207     }
208 root 1.129 else
209 root 1.245 count = 1;
210 root 1.123
211 root 1.245 return count;
212 elmex 1.1 }
213    
214     /**
215 root 1.245 * Actually improves the weapon, and tells user.
216     */
217     static int
218     improve_weapon_stat (object *op, object *improver, object *weapon, sint8 &stat, int sacrifice_count, const char *statname)
219 elmex 1.1 {
220 root 1.245 stat += sacrifice_count;
221     weapon->last_eat++;
222     improver->decrease ();
223    
224     /* So it updates the players stats and the window */
225     op->update_stats ();
226    
227     op->statusmsg (format (
228     "Your sacrifice was accepted.\n"
229     "Weapon's bonus to %s improved by %d.",
230     statname, sacrifice_count
231     ));
232    
233     return 1;
234 elmex 1.1 }
235    
236 root 1.245 /* Types of improvements, hidden in the sp field. */
237     #define IMPROVE_PREPARE 1
238     #define IMPROVE_DAMAGE 2
239     #define IMPROVE_WEIGHT 3
240     #define IMPROVE_ENCHANT 4
241     #define IMPROVE_STR 5
242     #define IMPROVE_DEX 6
243     #define IMPROVE_CON 7
244     #define IMPROVE_WIS 8
245     #define IMPROVE_CHA 9
246     #define IMPROVE_INT 10
247     #define IMPROVE_POW 11
248    
249 elmex 1.1 /**
250 root 1.245 * This does the prepare weapon scroll.
251     * Checks for sacrifice, and so on.
252 elmex 1.1 */
253 root 1.245 static int
254     prepare_weapon (object *op, object *improver, object *weapon)
255 elmex 1.1 {
256 root 1.245 int sacrifice_count, i;
257     char buf[MAX_BUF];
258    
259     if (weapon->level != 0)
260     {
261     op->failmsg ("Weapon is already prepared!");
262     return 0;
263     }
264 elmex 1.3
265 root 1.245 for (i = 0; i < NROFATTACKS; i++)
266     if (weapon->resist[i])
267     break;
268 elmex 1.3
269 root 1.245 /* If we break out, i will be less than nrofattacks, preventing
270     * improvement of items that already have protections.
271     */
272     if (i < NROFATTACKS || weapon->stats.hp || /* regeneration */
273     (weapon->stats.sp && weapon->type == WEAPON) || /* sp regeneration */
274     weapon->stats.exp || /* speed */
275     weapon->stats.ac) /* AC - only taifu's I think */
276 elmex 1.3 {
277 root 1.245 op->failmsg ("You cannot prepare magic weapons. "
278     "H<A weapon is considered magical if it changes regeneration, "
279     "speed or ac, or has other protections.>");
280 elmex 1.4 return 0;
281     }
282 elmex 1.3
283 root 1.245 sacrifice_count = check_sacrifice (op, improver);
284     if (sacrifice_count <= 0)
285     return 0;
286 elmex 1.3
287 root 1.245 weapon->level = isqrt (sacrifice_count);
288     eat_item (op, improver->slaying, sacrifice_count);
289 elmex 1.1
290 root 1.245 op->statusmsg (format (
291     "Your sacrifice was accepted."
292     "Your *%s may be improved %d times.",
293     &weapon->name, weapon->level
294     ));
295 elmex 1.1
296 root 1.245 sprintf (buf, "%s's %s", &op->name, &weapon->name);
297     weapon->name = weapon->name_pl = buf;
298     weapon->nrof = 0; /* prevents preparing n weapons in the same
299     slot at once! */
300     improver->decrease ();
301     weapon->last_eat = 0;
302     return 1;
303     }
304 root 1.37
305 root 1.245 /**
306     * Does the dirty job for 'improve weapon' scroll, prepare or add something.
307     * This is the new improve weapon code.
308     * Returns 0 if it was not able to work for some reason.
309     *
310     * Checks if weapon was prepared, if enough potions on the floor, ...
311     *
312     * We are hiding extra information about the weapon in the level and
313     * last_eat numbers for an object. Hopefully this won't break anything ??
314     * level == max improve last_eat == current improve
315     */
316     static int
317     improve_weapon (object *op, object *improver, object *weapon)
318     {
319     int sacrifice_count, sacrifice_needed = 0;
320 root 1.146
321 root 1.245 if (improver->stats.sp == IMPROVE_PREPARE)
322     return prepare_weapon (op, improver, weapon);
323 root 1.37
324 root 1.245 if (weapon->level == 0)
325     {
326     op->failmsg (
327     "This weapon has not been prepared."
328     " H<You first have to prepare a weapon with a prepare weapon scroll.>");
329     return 0;
330     }
331 root 1.37
332 root 1.245 if (weapon->last_eat >= weapon->level // improvements used up
333     || weapon->item_power >= 100) // or item_power >= arbitrary limit of 100
334     {
335     op->failmsg ("This weapon cannot be improved any more.");
336     return 0;
337     }
338 elmex 1.1
339 root 1.245 if (QUERY_FLAG (weapon, FLAG_APPLIED)
340     && !check_item_power (op, weapon->item_power + 1))
341     {
342     op->failmsg ("Improving the weapon will make it too "
343     "powerful for you to use. Unready it if you "
344     "really want to improve it.");
345     return 0;
346 elmex 1.4 }
347    
348 root 1.245 /* This just increases damage by 5 points, no matter what. No sacrifice
349     * is needed. Since stats.dam is now a 16 bit value and not 8 bit,
350     * don't put any maximum value on damage - the limit is how much the
351     * weapon can be improved.
352     */
353     if (improver->stats.sp == IMPROVE_DAMAGE)
354 elmex 1.4 {
355 root 1.245 weapon->stats.dam += 5;
356     weapon->weight += 5000; /* 5 KG's */
357     op->statusmsg (format ("Damage has been increased by 5 to %d.", weapon->stats.dam));
358     weapon->last_eat++;
359 root 1.123
360 root 1.245 weapon->item_power++;
361     improver->decrease ();
362     return 1;
363     }
364 root 1.123
365 root 1.245 if (improver->stats.sp == IMPROVE_WEIGHT)
366     {
367     /* Reduce weight by 20% */
368     weapon->weight = (weapon->weight * 8) / 10;
369     if (weapon->weight < 1)
370     weapon->weight = 1;
371 root 1.123
372 root 1.245 op->statusmsg (format ("Weapon weight reduced to %6.1fkg.", (float) weapon->weight / 1000.0));
373     weapon->last_eat++;
374     weapon->item_power++;
375     improver->decrease ();
376     return 1;
377     }
378 root 1.123
379 root 1.245 if (improver->stats.sp == IMPROVE_ENCHANT)
380     {
381     weapon->magic++;
382     weapon->last_eat++;
383     op->statusmsg (format ("Weapon magic increased to %d.", weapon->magic));
384     improver->decrease ();
385     weapon->item_power++;
386     return 1;
387     }
388 root 1.37
389 root 1.245 sacrifice_needed = weapon->stats.Str + weapon->stats.Int + weapon->stats.Dex +
390     weapon->stats.Pow + weapon->stats.Con + weapon->stats.Cha + weapon->stats.Wis;
391 root 1.37
392 root 1.245 if (sacrifice_needed < 1)
393     sacrifice_needed = 1;
394     sacrifice_needed *= 2;
395 root 1.37
396 root 1.245 sacrifice_count = check_sacrifice (op, improver);
397     if (sacrifice_count < sacrifice_needed)
398     {
399     op->failmsg (format ("You need at least %d %s.", sacrifice_needed, &improver->slaying));
400     return 0;
401 elmex 1.4 }
402    
403 root 1.245 eat_item (op, improver->slaying, sacrifice_needed);
404     weapon->item_power++;
405 elmex 1.4
406 root 1.245 switch (improver->stats.sp)
407 elmex 1.4 {
408 root 1.245 case IMPROVE_STR: return improve_weapon_stat (op, improver, weapon, weapon->stats.Str, 1, "strength");
409     case IMPROVE_DEX: return improve_weapon_stat (op, improver, weapon, weapon->stats.Dex, 1, "dexterity");
410     case IMPROVE_CON: return improve_weapon_stat (op, improver, weapon, weapon->stats.Con, 1, "constitution");
411     case IMPROVE_WIS: return improve_weapon_stat (op, improver, weapon, weapon->stats.Wis, 1, "wisdom");
412     case IMPROVE_CHA: return improve_weapon_stat (op, improver, weapon, weapon->stats.Cha, 1, "charisma");
413     case IMPROVE_INT: return improve_weapon_stat (op, improver, weapon, weapon->stats.Int, 1, "intelligence");
414     case IMPROVE_POW: return improve_weapon_stat (op, improver, weapon, weapon->stats.Pow, 1, "power");
415     default:
416     op->failmsg ("Unknown improvement type.");
417     }
418    
419     LOG (llevError, "improve_weapon: Got to end of function\n");
420     return 0;
421     }
422 elmex 1.1
423 root 1.245 /**
424     * Handles the applying of improve/prepare/enchant weapon scroll.
425     * Checks a few things (not on a non-magic square, marked weapon, ...),
426     * then calls improve_weapon to do the dirty work.
427     */
428     static int
429     check_improve_weapon (object *op, object *tmp)
430     {
431     object *otmp;
432 root 1.123
433 root 1.245 if (op->type != PLAYER)
434     return 0;
435 root 1.123
436 root 1.245 if (!QUERY_FLAG (op, FLAG_WIZCAST) && (get_map_flags (op->map, NULL, op->x, op->y, NULL, NULL) & P_NO_MAGIC))
437     {
438     op->failmsg ("Something blocks the magic of the scroll!");
439     return 0;
440 elmex 1.4 }
441    
442 root 1.245 otmp = find_marked_object (op);
443    
444     if (!otmp)
445 elmex 1.4 {
446 root 1.245 op->failmsg ("You need to mark a weapon object. H<Use the mark command or the mark option from the item popup menu.>");
447     return 0;
448     }
449 root 1.123
450 root 1.245 if (otmp->type != WEAPON && otmp->type != BOW)
451     {
452     op->failmsg ("Marked item is not a weapon or bow!");
453     return 0;
454 elmex 1.1 }
455 root 1.123
456 root 1.245 op->statusmsg ("Applied weapon builder.");
457    
458     improve_weapon (op, tmp, otmp);
459     esrv_send_item (op, otmp);
460 elmex 1.3 return 1;
461 elmex 1.1 }
462    
463     /**
464 root 1.245 * This code deals with the armour improvment scrolls.
465     * Change limits on improvement - let players go up to
466     * +5 no matter what level, but they are limited by item
467     * power.
468     * Try to use same improvement code as in the common/treasure.c
469     * file, so that if you make a +2 full helm, it will be just
470     * the same as one you find in a shop.
471     *
472     * deprecated comment:
473     * this code is by b.t. (thomas@nomad.astro.psu.edu) -
474     * only 'enchantment' of armour is possible - improving
475     * the stats of a player w/ armour as well as a weapon
476     * will probably horribly unbalance the game. Magic enchanting
477     * depends on the level of the character - ie the plus
478     * value (magic) of the armour can never be increased beyond
479     * the level of the character / 10 -- rounding upish, nor may
480     * the armour value of the piece of equipment exceed either
481     * the users level or 90)
482     * Modified by MSW for partial resistance. Only support
483     * changing of physical area right now.
484 elmex 1.1 */
485 root 1.22 static int
486 root 1.245 improve_armour (object *op, object *improver, object *armour)
487 elmex 1.1 {
488 root 1.245 object *tmp;
489 elmex 1.1
490 root 1.245 if (armour->magic >= settings.armor_max_enchant)
491     {
492     op->failmsg ("This armour can not be enchanted any further!");
493     return 0;
494     }
495 root 1.66
496 root 1.245 /* Dealing with random artifact armor is a lot trickier (in terms of value, weight,
497     * etc), so take the easy way out and don't worry about it.
498     * Note - maybe add scrolls which make the random artifact versions (eg, armour
499     * of gnarg and what not?)
500     */
501     if (armour->title)
502     {
503     op->failmsg ("This armour will not accept further enchantment.");
504     return 0;
505     }
506 root 1.66
507 root 1.245 /* Split objects if needed. Can't insert tmp until the
508     * end of this function - otherwise it will just re-merge.
509     */
510     tmp = armour->nrof > 1 ? armour->split (armour->nrof - 1) : 0;
511 elmex 1.1
512 root 1.245 armour->magic++;
513 elmex 1.1
514 root 1.245 if (!settings.armor_speed_linear)
515     {
516     int base = 100;
517     int pow = 0;
518 elmex 1.1
519 root 1.245 while (pow < armour->magic)
520 root 1.22 {
521 root 1.245 base = base - (base * settings.armor_speed_improvement) / 100;
522     pow++;
523 root 1.14 }
524 root 1.123
525 root 1.245 ARMOUR_SPEED (armour) = (ARMOUR_SPEED (armour->arch) * base) / 100;
526 elmex 1.1 }
527 root 1.245 else
528     ARMOUR_SPEED (armour) = (ARMOUR_SPEED (armour->arch) * (100 + armour->magic * settings.armor_speed_improvement)) / 100;
529 elmex 1.1
530 root 1.245 if (!settings.armor_weight_linear)
531     {
532     int base = 100;
533     int pow = 0;
534 root 1.22
535 root 1.245 while (pow < armour->magic)
536 root 1.22 {
537 root 1.245 base = base - (base * settings.armor_weight_reduction) / 100;
538     pow++;
539 root 1.14 }
540 root 1.245
541     armour->weight = (armour->arch->weight * base) / 100;
542 elmex 1.1 }
543 root 1.22 else
544 root 1.245 armour->weight = (armour->arch->weight * (100 - armour->magic * settings.armor_weight_reduction)) / 100;
545    
546     if (armour->weight <= 0)
547     {
548     LOG (llevInfo, "Warning: enchanted armours can have negative weight\n.");
549     armour->weight = 1;
550     }
551    
552     armour->item_power = get_power_from_ench (armour->arch->item_power + armour->magic);
553    
554     if (op->type == PLAYER)
555     {
556     esrv_send_item (op, armour);
557 elmex 1.1
558 root 1.245 if (QUERY_FLAG (armour, FLAG_APPLIED))
559     op->update_stats ();
560     }
561 elmex 1.1
562 root 1.144 improver->decrease ();
563 elmex 1.1
564 root 1.245 if (tmp)
565     op->insert (tmp);
566 root 1.123
567 elmex 1.1 return 1;
568     }
569    
570 root 1.245 /*
571     * convert_item() returns 1 if anything was converted, 0 if the item was not
572     * what the converter wants, -1 if the converter is broken.
573     *
574     * Takes one type of items and makes another.
575     * converter is the object that is doing the conversion.
576     * item is the object that triggered the converter - if it is not
577     * what the converter wants, this will not do anything.
578 elmex 1.1 */
579 root 1.245 int
580     convert_item (object *item, object *converter)
581 elmex 1.1 {
582 root 1.245 sint64 nr = 0, price_in;
583 elmex 1.1
584 root 1.245 if (item->flag [FLAG_UNPAID])
585     return 0;
586 root 1.115
587 root 1.245 shstr conv_from = converter->slaying;
588     archetype *conv_to = converter->other_arch;
589     sint64 need = converter->stats.food;
590     sint64 give = converter->stats.sp;
591 elmex 1.1
592 root 1.245 /* We make some assumptions - we assume if it takes money as it type,
593     * it wants some amount. We don't make change (ie, if something costs
594     * 3 gp and player drops a platinum, tough luck)
595 root 1.22 */
596 root 1.245 if (conv_from == shstr_money)
597 elmex 1.1 {
598 root 1.245 if (item->type != MONEY)
599     return 0;
600 root 1.115
601 root 1.245 nr = sint64 (item->nrof) * item->value / need;
602     if (!nr)
603     return 0;
604 root 1.115
605 root 1.245 converter->play_sound (sound_find ("shop_buy"));
606 root 1.22
607 root 1.245 sint64 cost = (nr * need + item->value - 1) / item->value;
608 root 1.22
609 root 1.245 item->decrease (cost);
610 elmex 1.1
611 root 1.245 price_in = cost * item->value;
612     }
613     else
614     {
615     if (item->type == PLAYER
616     || conv_from != item->arch->archname
617     || (need && need > (uint16) item->nrof))
618     return 0;
619 elmex 1.1
620 root 1.245 converter->play_sound (sound_find ("convert_item"));
621 root 1.123
622 root 1.245 if (need)
623     {
624     nr = sint64 (item->nrof) / need;
625     item->decrease (nr * need);
626     price_in = nr * need * item->value;
627     }
628     else
629     {
630     price_in = item->value;
631     item->destroy ();
632     }
633 root 1.22 }
634 root 1.123
635 root 1.245 if (converter->inv)
636 root 1.22 {
637 root 1.245 object *ob;
638     int i;
639     object *ob_to_copy;
640 root 1.123
641 root 1.245 /* select random object from inventory to copy */
642     ob_to_copy = converter->inv;
643     for (ob = converter->inv->below, i = 1; ob; ob = ob->below, i++)
644     if (rndm (0, i) == 0)
645     ob_to_copy = ob;
646 root 1.123
647 root 1.245 item = ob_to_copy->deep_clone ();
648     CLEAR_FLAG (item, FLAG_IS_A_TEMPLATE);
649     unflag_inv (item, FLAG_IS_A_TEMPLATE);
650 root 1.22 }
651 root 1.245 else
652 root 1.22 {
653 root 1.245 if (!conv_to)
654     {
655     LOG (llevError, "move_creator: Converter doesn't have other arch set: %s (%s, %d, %d)\n",
656     &converter->name, &converter->map->path, converter->x, converter->y);
657     return -1;
658     }
659 root 1.123
660 root 1.245 item = object_create_arch (conv_to);
661     fix_generated_item (item, converter, 0, 0, GT_MINIMAL);
662 root 1.22 }
663 root 1.162
664 root 1.245 if (give)
665     item->nrof = give;
666 root 1.14
667 root 1.245 if (nr)
668     item->nrof *= nr;
669 root 1.22
670 root 1.245 if (converter->flag [FLAG_PRECIOUS])
671     SET_FLAG (item, FLAG_UNPAID);
672 elmex 1.1
673 root 1.245 if (converter->is_in_shop ())
674 root 1.22 {
675 root 1.245 // converters on shop floors don't work anymore, bug lets check for it
676     // and report in case someone still does it.
677     LOG (llevDebug, "ITEMBUG: broken converter, converters on shop floor don't work: %s\n",
678     converter->debug_desc ());
679     SET_FLAG (item, FLAG_UNPAID);
680 root 1.22 }
681 root 1.245 else if (price_in < sint64 (item->nrof) * item->value)
682 root 1.22 {
683 root 1.245 LOG (llevDebug, "converter output price higher than input: %s at %s (%d, %d) in value %d, out value %d for %s\n",
684     &converter->name, &converter->map->path, converter->x, converter->y, price_in, item->nrof * item->value, &item->name);
685     /**
686     * elmex: we are going to let the game continue, as the mapcreator
687     * hopefully had something in mind when doing this.
688     */
689 root 1.22 }
690 root 1.123
691 root 1.245 // elmex: only identify if we need to, for example so that generated money doesn't
692     // get an 'identified' flag so easily.
693     if (need_identify (item))
694     identify (item);
695    
696     insert_ob_in_map_at (item, converter->map, converter, 0, converter->x, converter->y);
697     return 1;
698 elmex 1.1 }
699    
700     /**
701 root 1.245 * Handle apply on containers.
702     * By Eneq(@csd.uu.se).
703     * Moved to own function and added many features [Tero.Haatanen@lut.fi]
704     * added the alchemical cauldron to the code -b.t.
705 elmex 1.1 */
706 root 1.209 static int
707 root 1.245 apply_container (object *op, object *sack)
708 elmex 1.1 {
709 root 1.245 if (op->type != PLAYER || !op->contr->ns)
710     return 0; /* This might change */
711 elmex 1.1
712 root 1.245 if (!sack || sack->type != CONTAINER)
713 root 1.22 {
714 root 1.245 LOG (llevError, "apply_container: %s is not container!\n", sack ? &sack->name : "[nullobject]");
715 elmex 1.1 return 0;
716     }
717 root 1.77
718 root 1.245 op->contr->last_used = 0;
719 root 1.162
720 root 1.245 if (sack->env && sack->env != op)
721 root 1.22 {
722 root 1.245 op->failmsg ("You must put it onto the floor or into your inventory first.");
723     return 1;
724 elmex 1.1 }
725 root 1.77
726 root 1.245 // already applied == open on ground, or open in inv, or active in inv
727     if (sack->flag [FLAG_APPLIED])
728 root 1.22 {
729 root 1.245 if (op->container_ () == sack)
730     {
731     // open on ground or inv, so close
732     op->close_container ();
733     return 1;
734     }
735     else if (!sack->env)
736     {
737     // active, but not ours: some other player has opened it
738     op->failmsg (format ("Somebody else is using the %s already.", query_name (sack)));
739     return 1;
740     }
741    
742     // fall through to opening it (active in inv)
743     }
744     else if (sack->env)
745     {
746     // it is in our env, so activate it, do not open yet
747     op->close_container ();
748     sack->flag [FLAG_APPLIED] = 1;
749     esrv_update_item (UPD_FLAGS, op, sack);
750     op->statusmsg (format ("You ready %s.", query_name (sack)));
751     return 1;
752     }
753    
754     // it's locked?
755     if (sack->slaying)
756     {
757     if (object *tmp = find_key (op, op, sack))
758     op->statusmsg (format ("You unlock %s with %s.", query_name (sack), query_name (tmp)));
759     else
760     {
761     op->statusmsg (format ("You don't have the key to unlock %s.", query_name (sack)));
762     return 1;
763     }
764 elmex 1.1 }
765 root 1.77
766 root 1.245 op->open_container (sack);
767 root 1.123
768 root 1.22 return 1;
769 elmex 1.1 }
770    
771     /**
772 root 1.245 * Handles dropping things on altar.
773     * Returns true if sacrifice was accepted.
774 elmex 1.1 */
775 root 1.210 static int
776 root 1.245 apply_altar (object *altar, object *sacrifice, object *originator)
777 elmex 1.1 {
778 root 1.245 /* Only players can make sacrifices on spell casting altars. */
779     if (altar->inv && (!originator || originator->type != PLAYER))
780     return 0;
781 elmex 1.1
782 root 1.245 if (operate_altar (altar, &sacrifice, originator))
783 root 1.22 {
784 root 1.245 /* Simple check. Unfortunately, it means you can't cast magic bullet
785     * with an altar. We call it a Potion - altars are stationary - it
786     * is up to map designers to use them properly.
787     */
788     if (altar->inv && altar->inv->type == SPELL)
789     {
790     originator->statusmsg (format ("The altar casts %s.", &altar->inv->name));
791     cast_spell (originator, altar, 0, altar->inv, NULL);
792     /* If it is connected, push the button. Fixes some problems with
793     * old maps.
794     */
795 root 1.139
796 root 1.245 /* push_button (altar);*/
797 elmex 1.1 }
798 root 1.245 else
799 elmex 1.1 {
800 root 1.245 altar->value = 1; /* works only once */
801     push_button (altar, originator);
802 elmex 1.1 }
803    
804 root 1.245 return !sacrifice;
805 root 1.22 }
806     else
807 root 1.245 return 0;
808 elmex 1.1 }
809    
810 root 1.245 /**
811     * Handles 'movement' of shop mats.
812     * Returns 1 if 'op' was destroyed, 0 if not.
813     * Largely re-written to not use nearly as many gotos, plus
814     * some of this code just looked plain out of date.
815     * MSW 2001-08-29
816 elmex 1.1 */
817 elmex 1.195 int
818 root 1.245 apply_shop_mat (object *shop_mat, object *op)
819 root 1.22 {
820 root 1.245 int rv = 0;
821     double opinion;
822     object *tmp, *next;
823    
824     SET_FLAG (op, FLAG_NO_APPLY); /* prevent loops */
825 root 1.22
826 root 1.245 bool has_unpaid = false;
827 elmex 1.213
828 root 1.245 // quite inefficient to do this here twice, but the api doesn't lend itself to
829     // a quick and small change :(
830     for (object::depth_iterator item = op->begin (); item != op->end (); ++item)
831     if (item->flag [FLAG_UNPAID])
832     {
833     has_unpaid = true;
834     break;
835     }
836 root 1.142
837 root 1.245 if (!op->is_player ())
838 root 1.22 {
839 root 1.245 /* Remove all the unpaid objects that may be carried here.
840     * This could be pets or monsters that are somehow in
841     * the shop.
842     */
843     for (tmp = op->inv; tmp; tmp = next)
844     {
845     next = tmp->below;
846    
847     if (QUERY_FLAG (tmp, FLAG_UNPAID))
848     {
849     int i = find_free_spot (tmp, op->map, op->x, op->y, 1, 9);
850    
851     if (i >= 0)
852     tmp->move (i);
853     }
854     }
855 root 1.22
856 root 1.245 /* Don't teleport things like spell effects */
857     if (QUERY_FLAG (op, FLAG_NO_PICK))
858 root 1.22 return 0;
859 root 1.121
860 root 1.245 /* unpaid objects, or non living objects, can't transfer by
861     * shop mats. Instead, put it on a nearby space.
862     */
863     if (QUERY_FLAG (op, FLAG_UNPAID) || !QUERY_FLAG (op, FLAG_ALIVE))
864     {
865     /* Somebody dropped an unpaid item, just move to an adjacent place. */
866     int i = find_free_spot (op, op->map, op->x, op->y, 1, 9);
867 root 1.121
868 root 1.245 if (i != -1)
869     rv = transfer_ob (op, op->x + freearr_x[i], op->y + freearr_y[i], 0, shop_mat);
870 root 1.121
871 root 1.245 return 0;
872     }
873 elmex 1.1
874 root 1.245 /* Removed code that checked for multipart objects - it appears that
875     * the teleport function should be able to handle this just fine.
876     */
877     rv = teleport (shop_mat, SHOP_MAT, op);
878 root 1.22 }
879 root 1.245 else if (can_pay (op) && get_payment (op))
880 root 1.22 {
881 root 1.245 /* this is only used for players */
882     rv = teleport (shop_mat, SHOP_MAT, op);
883 elmex 1.1
884 root 1.245 if (has_unpaid)
885     op->contr->play_sound (sound_find ("shop_buy"));
886     else if (op->is_in_shop ())
887     op->contr->play_sound (sound_find ("shop_enter"));
888     else
889     op->contr->play_sound (sound_find ("shop_leave"));
890 root 1.121
891 root 1.245 if (shop_mat->msg)
892     op->statusmsg (shop_mat->msg);
893     /* This check below is a bit simplistic - generally it should be correct,
894     * but there is never a guarantee that the bottom space on the map is
895     * actually the shop floor.
896     */
897     else if (!rv && !op->is_in_shop ())
898 root 1.22 {
899 root 1.245 opinion = shopkeeper_approval (op->map, op);
900    
901     op->statusmsg (
902     opinion >= 0.90 ? "The shopkeeper gives you a friendly wave."
903     : opinion >= 0.75 ? "The shopkeeper waves to you."
904     : opinion >= 0.50 ? "The shopkeeper ignores you."
905     : "The shopkeeper glares at you with contempt."
906     );
907 root 1.22 }
908     }
909 root 1.245 else
910 root 1.22 {
911 root 1.245 /* if we get here, a player tried to leave a shop but was not able
912     * to afford the items he has. We try to move the player so that
913     * they are not on the mat anymore
914     */
915     int i = find_free_spot (op, op->map, op->x, op->y, 1, 9);
916 root 1.22
917 root 1.245 if (i == -1)
918     LOG (llevError, "Internal shop-mat problem.\n");
919     else
920 root 1.22 {
921 root 1.245 op->remove ();
922     op->x += freearr_x[i];
923     op->y += freearr_y[i];
924     rv = insert_ob_in_map (op, op->map, shop_mat, 0) == NULL;
925 root 1.22 }
926     }
927    
928 root 1.245 CLEAR_FLAG (op, FLAG_NO_APPLY);
929     return rv;
930 elmex 1.1 }
931 root 1.22
932 elmex 1.1 /**
933 root 1.245 * Handles applying a sign.
934 elmex 1.1 */
935 root 1.245 static void
936     apply_sign (object *op, object *sign, int autoapply)
937 elmex 1.1 {
938 root 1.245 if (!op->is_player())
939     return;
940 elmex 1.1
941 root 1.245 if (sign->has_dialogue ())
942 root 1.22 {
943 root 1.245 op->statusmsg (format ("Maybe you should I<talk> to the %s instead?", &sign->name));
944     return;
945 elmex 1.1 }
946 root 1.40
947 root 1.245 if (!sign->msg)
948 root 1.22 {
949 root 1.245 op->contr->infobox (MSG_CHANNEL ("examine"),
950     format ("T<%s>\n\n Nothing %sis written on it.",
951     &sign->name,
952     sign->name == sign->arch->name ? "" : "else "));
953     return;
954 elmex 1.1 }
955    
956 root 1.245 if (sign->stats.food)
957 root 1.22 {
958 root 1.245 if (sign->last_eat >= sign->stats.food)
959 root 1.22 {
960 root 1.245 if (!sign->move_on)
961     op->failmsg ("You cannot read it anymore.");
962    
963     return;
964 root 1.14 }
965 root 1.22
966 root 1.245 if (!QUERY_FLAG (op, FLAG_WIZPASS))
967     sign->last_eat++;
968 elmex 1.1 }
969    
970 root 1.245 /* Sign or magic mouth? Do we need to see it, or does it talk to us?
971     * No way to know for sure. The presumption is basically that if
972     * move_on is zero, it needs to be manually applied (doesn't talk
973     * to us).
974     */
975     if (QUERY_FLAG (op, FLAG_BLIND) && !QUERY_FLAG (op, FLAG_WIZ) && !sign->move_on)
976 root 1.68 {
977 root 1.245 op->failmsg ("You are unable to read while blind!");
978     return;
979 elmex 1.1 }
980    
981 root 1.245 if (op->contr)
982     if (client *ns = op->contr->ns)
983     {
984     if (sign->sound)
985     ns->play_sound (sign->sound);
986     else if (autoapply)
987     ns->play_sound (sound_find ("msg_voice"));
988 elmex 1.1
989 root 1.245 op->contr->infobox (MSG_CHANNEL ("examine"), format ("T<%s>\n\n%s", &sign->name, &sign->msg));
990     }
991 elmex 1.1 }
992    
993 root 1.245 static void
994     move_apply_hole (object *trap, object *victim)
995 elmex 1.1 {
996 root 1.245 /* Hole not open? */
997     if (trap->stats.wc > 0)
998     return;
999    
1000     /* Is this a multipart monster and not the head? If so, return.
1001     * Processing will happen if the head runs into the pit
1002     */
1003     if (victim->head)
1004     return;
1005    
1006     // now find all possible locations and randomly pick one
1007     int dir = find_free_spot (victim, trap->map, EXIT_X (trap), EXIT_Y (trap), 0,
1008     trap->range >= 3 ? SIZEOFFREE3 + 1
1009     : trap->range >= 2 ? SIZEOFFREE2 + 1
1010     : trap->range >= 1 ? SIZEOFFREE1 + 1
1011     : SIZEOFFREE0 + 1);
1012 root 1.22
1013 root 1.245 if (dir < 0)
1014     return;
1015 elmex 1.1
1016 root 1.245 victim->play_sound (trap->sound ? trap->sound : sound_find ("fall_hole"));
1017     victim->statusmsg ("You fall through the hole!", NDI_RED);
1018 root 1.26
1019 root 1.245 transfer_ob (victim,
1020     EXIT_X (trap) + freearr_x[dir],
1021     EXIT_Y (trap) + freearr_y[dir],
1022     0, victim);
1023 elmex 1.1 }
1024    
1025     /**
1026 root 1.245 * Unapplies specified item.
1027     * No check done on cursed/damned.
1028     * Break this out of apply_special - this is just done
1029     * to keep the size of apply_special to a more managable size.
1030 elmex 1.1 */
1031 root 1.245 static bool
1032     unapply_special (object *who, object *op, int aflags)
1033 elmex 1.1 {
1034 root 1.245 if (INVOKE_OBJECT (BE_UNREADY, op, ARG_OBJECT (who), ARG_INT (aflags))
1035     || INVOKE_OBJECT (UNREADY, who, ARG_OBJECT (op), ARG_INT (aflags)))
1036     return RESULT_INT (0);
1037 elmex 1.1
1038 root 1.245 if (who->current_weapon == op)
1039     who->current_weapon = 0;
1040 elmex 1.1
1041 root 1.245 op->flag [FLAG_APPLIED] = false;
1042 root 1.121
1043 root 1.245 switch (op->type)
1044     {
1045     case SKILL:
1046     if (player *pl = who->contr)
1047     if (op->invisible)
1048     pl->statusmsg (format ("You can no longer use the %s skill.", &op->skill));
1049     else
1050     pl->statusmsg (format ("You stop using the %s.", query_name (op)));
1051    
1052     change_abil (who, op);
1053     who->flag [FLAG_READY_SKILL] = false;
1054 root 1.121 break;
1055    
1056 root 1.245 case WEAPON:
1057     who->statusmsg (format ("You unwield %s.", query_name (op)));
1058     change_abil (who, op);
1059     who->flag [FLAG_READY_WEAPON] = false;
1060    
1061     // unapplying a weapon or skill tool should also unapply the skill it governs
1062     // but this is hard, as it shouldn't do so when the skill can
1063     // be used for other reasons
1064     if (who->chosen_skill)
1065     if (!who->chosen_skill->flag [FLAG_CAN_USE_SKILL])
1066     unapply_special (who, op, 0);
1067 root 1.25
1068 root 1.245 break;
1069 root 1.14
1070 root 1.245 case BOW:
1071     case WAND:
1072     case ROD:
1073     case HORN:
1074     case RANGED:
1075     if (player *pl = who->contr)
1076     {
1077     who->statusmsg (format ("You unready %s.", query_name (op)));
1078     change_abil (who, op);
1079     }
1080     else
1081     {
1082     who->change_skill (0);
1083 root 1.14
1084 root 1.245 if (op->type == BOW)
1085     op->flag [FLAG_READY_BOW ] = false;
1086     else
1087     op->flag [FLAG_READY_RANGE] = false;
1088     }
1089 root 1.14
1090 root 1.245 break;
1091 root 1.22
1092 root 1.245 case ARMOUR:
1093     case HELMET:
1094     case SHIELD:
1095     case RING:
1096     case BOOTS:
1097     case GLOVES:
1098     case AMULET:
1099     case GIRDLE:
1100     case BRACERS:
1101     case CLOAK:
1102     who->statusmsg (format ("You unwear %s.", query_name (op)));
1103     change_abil (who, op);
1104     break;
1105 root 1.24
1106 root 1.245 case SPELL:
1107     case BUILDER:
1108     who->statusmsg (format ("You unready %s.", query_name (op)));
1109     break;
1110 root 1.178
1111 root 1.245 //case SKILL_TOOL://TODO
1112     default:
1113     who->statusmsg (format ("You unapply %s.", query_name (op)));
1114     break;
1115 elmex 1.1 }
1116 elmex 1.15
1117 root 1.245 if (aflags & AP_NO_MERGE || !merge_ob (op, 0))
1118     if (object *pl = op->visible_to ())
1119     esrv_send_item (pl, op);
1120 root 1.121
1121 root 1.245 who->update_stats ();
1122 root 1.24
1123 root 1.245 return 1;
1124     }
1125 root 1.22
1126 root 1.245 /**
1127     * Returns the object that is using location 'loc'.
1128     * Note that 'start' is the first object to start examing - we
1129     * then go through the below of this. In this way, you can do
1130     * something like:
1131     * tmp = get_next_item_from_body_location(who->inv, 1);
1132     * if (tmp) tmp1 = get_next_item_from_body_location(tmp->below, 1);
1133     * to find the second object that may use this location, etc.
1134     * Returns NULL if no match is found.
1135     * loc is the index into the array we are looking for a match.
1136     * don't return invisible objects unless they are skill objects
1137     * invisible other objects that use
1138     * up body locations can be used as restrictions.
1139     */
1140     static object *
1141     get_next_item_from_body_location (int loc, object *start)
1142     {
1143     for (object *tmp = start; tmp; tmp = tmp->below)
1144     if (tmp->flag [FLAG_APPLIED]
1145     && tmp->slot [loc].info
1146     && (!tmp->invisible || tmp->type == SKILL || tmp->type == SPELL))
1147     return tmp;
1148 root 1.24
1149 root 1.245 return 0;
1150 elmex 1.1 }
1151    
1152     /**
1153 root 1.245 * 'op' wants to apply an object, but can't because of other equipment.
1154     * This should only be called when it is known
1155     * that there are objects to unapply. This makes pretty heavy
1156     * use of get_item_from_body_location. It makes no intelligent choice
1157     * on objects - rather, the first that is matched is used.
1158     * Returns 0 on success, returns 1 if there is some problem.
1159     * if aflags is AP_PRINT, we instead print out waht to unapply
1160     * instead of doing it. This is a lot less code than having
1161     * another function that does just that.
1162 elmex 1.1 */
1163 root 1.179
1164 root 1.245 #define CANNOT_REMOVE_CURSED \
1165     "H<You cannot remove cursed or damned items, you first have to remove the curse. " \
1166     "Praying over an altar, scrolls of remove curse/damnation, " \
1167     "priests or even other players might help.>"
1168 root 1.22
1169 root 1.245 static bool
1170     unapply_for_ob (object *who, object *op, int aflags)
1171     {
1172     if (op->is_range ())
1173     for (object *tmp = who->inv; tmp; tmp = tmp->below)
1174     if (QUERY_FLAG (tmp, FLAG_APPLIED) && tmp->is_range ())
1175     if ((aflags & AP_IGNORE_CURSE) || (aflags & AP_PRINT) || (!QUERY_FLAG (tmp, FLAG_CURSED) && !QUERY_FLAG (tmp, FLAG_DAMNED)))
1176     {
1177     if (aflags & AP_PRINT)
1178     who->failmsg (query_name (tmp));
1179     else
1180     unapply_special (who, tmp, aflags);
1181     }
1182     else
1183     {
1184     /* In this case, we want to try and remove a cursed item.
1185     * While we know it won't work, we want unapply_special to
1186     * at least generate the message.
1187     */
1188     who->failmsg (format ("No matter how hard you try, you just can't remove the %s." CANNOT_REMOVE_CURSED, query_name (tmp)));
1189     return 1;
1190     }
1191 elmex 1.1
1192 root 1.245 for (int i = 0; i < NUM_BODY_LOCATIONS; i++)
1193 root 1.22 {
1194 root 1.245 /* this used up a slot that we need to free */
1195     if (op->slot[i].info)
1196 root 1.22 {
1197 root 1.245 object *last = who->inv;
1198    
1199     /* We do a while loop - may need to remove several items in order
1200     * to free up enough slots.
1201     */
1202     while ((who->slot[i].used + op->slot[i].info) < 0)
1203     {
1204     object *tmp = get_next_item_from_body_location (i, last);
1205 root 1.120
1206 root 1.245 if (!tmp)
1207     {
1208     #if 0
1209     /* Not a bug - we'll get this if the player has cursed items
1210     * equipped.
1211     */
1212     LOG (llevError, "Can't find object using location %d (%s) on %s\n", i, body_locations[i].save_name, who->name);
1213     #endif
1214     return 1;
1215     }
1216 elmex 1.1
1217 root 1.245 /* If we are just printing, we don't care about cursed status */
1218     if ((aflags & AP_IGNORE_CURSE) || (aflags & AP_PRINT) || (!(QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))))
1219     {
1220     if (aflags & AP_PRINT)
1221     who->failmsg (query_name (tmp));
1222     else
1223     unapply_special (who, tmp, aflags);
1224     }
1225     else
1226     {
1227     /* Cursed item that we can't unequip - tell the player.
1228     * Note this could be annoying if this is just one of a few,
1229     * so it may not be critical (eg, putting on a ring and you have
1230     * one cursed ring.)
1231     */
1232     who->failmsg (format ("The %s just won't come off." CANNOT_REMOVE_CURSED, query_name (tmp)));
1233     }
1234    
1235     last = tmp->below;
1236     }
1237     /* if we got here, this slot is freed up - otherwise, if it wasn't freed up, the
1238     * return in the !tmp would have kicked in.
1239     */
1240     } /* if op is using this body location */
1241     } /* for body lcoations */
1242    
1243     return 0;
1244     }
1245    
1246     /**
1247     * Checks to see if 'who' can apply object 'op'.
1248     * Returns 0 if apply can be done without anything special.
1249     * Otherwise returns a bitmask - potentially several of these may be
1250     * set, but largely depends on circumstance - in the future, processing
1251     * may be pruned once we know some status (eg, once CAN_APPLY_NEVER
1252     * is set, do we really care what the other flags may be?)
1253     *
1254     * See include/define.h for detailed description of the meaning of
1255     * these return values.
1256     */
1257     int
1258     can_apply_object (object *who, object *op)
1259     {
1260     if (INVOKE_OBJECT (CAN_BE_APPLIED, op, ARG_OBJECT (who)) || INVOKE_OBJECT (CAN_APPLY, who, ARG_OBJECT (op)))
1261     return RESULT_INT (0);
1262    
1263     int retval = 0;
1264     object *tmp = 0, *ws = 0;
1265 elmex 1.1
1266 root 1.245 for (int i = 0; i < NUM_BODY_LOCATIONS; i++)
1267 root 1.22 {
1268 root 1.245 if (op->slot[i].info)
1269     {
1270     /* Item uses more slots than we have */
1271     if (who->slot[i].info + op->slot [i].info < 0)
1272     {
1273     /* Could return now for efficiency - rest of info below isn't
1274     * really needed.
1275     */
1276     retval |= CAN_APPLY_NEVER;
1277     }
1278     else if (who->slot[i].used + op->slot[i].info < 0)
1279     {
1280     /* in this case, equipping this would use more free spots than
1281     * we have.
1282     */
1283    
1284     /* if we have an applied weapon/shield, and unapply it would free
1285     * enough slots to equip the new item, then just set "can
1286     * apply unapply". We don't care about the logic below - if you have a
1287     * shield equipped and try to equip another shield, there is only
1288     * one choice. However, the check for the number of body locations
1289     * does take into the account cases where what is being applied
1290     * may be two handed for example.
1291     */
1292     if (ws)
1293     if ((who->slot[i].used - ws->slot[i].info + op->slot[i].info) >= 0)
1294     {
1295     retval |= CAN_APPLY_UNAPPLY;
1296     continue;
1297     }
1298    
1299     object *tmp1 = get_next_item_from_body_location (i, who->inv);
1300     if (!tmp1)
1301     retval |= CAN_APPLY_NEVER;
1302     else
1303     {
1304     /* need to unapply something. However, if this something
1305     * is different than we had found before, it means they need
1306     * to apply multiple objects
1307     */
1308     retval |= CAN_APPLY_UNAPPLY;
1309 root 1.110
1310 root 1.245 if (!tmp)
1311     tmp = tmp1;
1312     else if (tmp != tmp1)
1313     retval |= CAN_APPLY_UNAPPLY_MULT;
1314 root 1.110
1315 root 1.245 /* This object isn't using up all the slots, so there must
1316     * be another. If so, and it the new item doesn't need all
1317     * the slots, the player then has a choice.
1318     */
1319     if ((who->slot[i].used - tmp1->slot[i].info != who->slot[i].info)
1320     && abs (op->slot[i].info) < who->slot[i].info)
1321     retval |= CAN_APPLY_UNAPPLY_CHOICE;
1322 elmex 1.1
1323 root 1.245 /* Does unequippint 'tmp1' free up enough slots for this to be
1324     * equipped? If not, there must be something else to unapply.
1325     */
1326     if (who->slot[i].used + op->slot[i].info < tmp1->slot[i].info)
1327     retval |= CAN_APPLY_UNAPPLY_MULT;
1328     }
1329     } /* if not enough free slots */
1330     } /* if this object uses location i */
1331     } /* for i -> num_body_locations loop */
1332 root 1.167
1333 root 1.245 /* Note that we don't check for FLAG_USE_ARMOUR - that should
1334     * really be controlled by use of body locations. We do have
1335     * the weapon/shield checks, and the range checks for monsters,
1336     * because you can't control those just by body location - bows, shields,
1337     * and weapons all use the same slot. Similar for horn/rod/wand - they
1338     * all use the same location.
1339 root 1.167 */
1340 root 1.245 if (op->type == WEAPON && !QUERY_FLAG (who, FLAG_USE_WEAPON))
1341     retval |= CAN_APPLY_RESTRICTION;
1342    
1343     if (op->type == SHIELD && !QUERY_FLAG (who, FLAG_USE_SHIELD))
1344     retval |= CAN_APPLY_RESTRICTION;
1345    
1346     if (who->type != PLAYER)
1347     {
1348     if ((op->type == WAND || op->type == HORN || op->type == ROD) && !QUERY_FLAG (who, FLAG_USE_RANGE))
1349     retval |= CAN_APPLY_RESTRICTION;
1350 root 1.167
1351 root 1.245 if (op->type == BOW && !QUERY_FLAG (who, FLAG_USE_BOW))
1352     retval |= CAN_APPLY_RESTRICTION;
1353 root 1.167
1354 root 1.245 if (op->type == RING && !QUERY_FLAG (who, FLAG_USE_RING))
1355     retval |= CAN_APPLY_RESTRICTION;
1356 root 1.167
1357 root 1.245 if (op->type == BOW && !QUERY_FLAG (who, FLAG_USE_BOW))
1358     retval |= CAN_APPLY_RESTRICTION;
1359     }
1360 root 1.167
1361 root 1.245 return retval;
1362 root 1.167 }
1363    
1364 elmex 1.1 /**
1365 root 1.245 * who is the object using the object. It can be a monster.
1366     * op is the object they are using. op is an equipment type item,
1367     * eg, one which you put on and keep on for a while, and not something
1368     * like a potion or scroll.
1369     *
1370     * function returns 1 if the action could not be completed, 0 on
1371     * success. However, success is a matter of meaning - if the
1372     * user passes the 'apply' flag to an object already applied,
1373     * nothing is done, and 0 is returned.
1374     *
1375     * aflags is special flags (0 - normal/toggle, AP_APPLY=always apply,
1376     * AP_UNAPPLY=always unapply).
1377     *
1378     * Optional flags:
1379     * AP_NO_MERGE: don't merge an unapplied object with other objects
1380     * AP_IGNORE_CURSE: unapply cursed items
1381     * AP_NO_READY: do not ready skills when applying skill tools
1382     *
1383     * Usage example: apply_special (who, op, AP_UNAPPLY | AP_IGNORE_CURSE)
1384 elmex 1.1 *
1385 root 1.245 * apply_special() doesn't check for unpaid items.
1386 elmex 1.1 */
1387 root 1.245
1388     #define LACK_ITEM_POWER \
1389     " H<You lack enough unused item power to use this weapon, see the skills command.>"
1390    
1391     static bool
1392     apply_special (object *who, object *op, int aflags)
1393 elmex 1.1 {
1394 root 1.245 int basic_flag = aflags & AP_MODE;
1395     object *tmp, *tmp2, *skop = NULL;
1396    
1397     if (who == NULL)
1398     {
1399     LOG (llevError, "apply_special() from object without environment.\n");
1400     return 1;
1401     }
1402 elmex 1.1
1403 root 1.245 //TODO: remove these when apply_special is no longer exposed
1404     if (op->env != who)
1405     return 1; /* op is not in inventory */
1406 elmex 1.1
1407 root 1.245 /* trying to unequip op */
1408     if (QUERY_FLAG (op, FLAG_APPLIED))
1409 root 1.22 {
1410 root 1.245 /* always apply, so no reason to unapply */
1411     if (basic_flag == AP_APPLY)
1412     return 0;
1413    
1414     if (!(aflags & AP_IGNORE_CURSE) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED)))
1415     {
1416     who->failmsg (format ("No matter how hard you try, you just can't remove %s." CANNOT_REMOVE_CURSED, query_name (op)));
1417     return 1;
1418     }
1419    
1420     return unapply_special (who, op, aflags);
1421 elmex 1.1 }
1422 root 1.245 else if (basic_flag == AP_UNAPPLY)
1423     return 0;
1424    
1425     splay (op);
1426 root 1.110
1427 root 1.245 /* Can't just apply this object. Lets see what not and what to do */
1428     if (int i = can_apply_object (who, op))
1429     {
1430     if (i & CAN_APPLY_NEVER)
1431     {
1432     who->failmsg (format ("You don't have the body to use a %s. H<You can never apply this item.>", query_name (op)));
1433     return 1;
1434     }
1435     else if (i & CAN_APPLY_RESTRICTION)
1436     {
1437     who->failmsg (format (
1438     "You have a prohibition against using a %s. "
1439     "H<Your belief, profession or class prevents you from applying this item.>",
1440     query_name (op)
1441     ));
1442     return 1;
1443     }
1444 elmex 1.1
1445 root 1.245 if (who->type != PLAYER)
1446     {
1447     /* Some error, so don't try to equip something more */
1448     if (unapply_for_ob (who, op, aflags))
1449     return 1;
1450     }
1451     else
1452     {
1453     if (who->contr->unapply == unapply_never || (i & CAN_APPLY_UNAPPLY_CHOICE && who->contr->unapply == unapply_nochoice))
1454 root 1.198 {
1455 root 1.245 who->failmsg ("You need to unapply some of the following item(s) or change your applymode:");
1456     unapply_for_ob (who, op, AP_PRINT);
1457     return 1;
1458     }
1459     else if (who->contr->unapply == unapply_always || !(i & CAN_APPLY_UNAPPLY_CHOICE))
1460     if (unapply_for_ob (who, op, aflags))
1461     return 1;
1462     }
1463     }
1464    
1465     if (op->skill && op->type != SKILL && op->type != SKILL_TOOL)
1466     {
1467     // try to ready attached skill first
1468     skop = find_skill_by_name (who, op->skill);
1469    
1470     if (!skop)
1471     {
1472     who->failmsg (format ("You need the %s skill to use this item!", &op->skill));
1473     return 1;
1474     }
1475     else if (!who->apply (skop, AP_APPLY | AP_NO_SLOT))
1476     {
1477     who->failmsg (format ("You can't use the required %s skill!", &op->skill));
1478     return 1;
1479     }
1480     }
1481    
1482     if (!check_item_power (who, op->item_power))
1483     {
1484     who->failmsg ("Equipping that combined with other items would consume your soul!" LACK_ITEM_POWER);
1485     return 1;
1486     }
1487    
1488     /* Ok. We are now at the state where we can apply the new object.
1489     * Note that we don't have the checks for can_use_...
1490     * below - that is already taken care of by can_apply_object.
1491     */
1492    
1493     // split away all the other items from the stack, so only one item is left
1494     tmp = op->nrof > 1 ? op->split (op->nrof - 1) : 0;
1495    
1496     if (INVOKE_OBJECT (BE_READY, op, ARG_OBJECT (who)) || INVOKE_OBJECT (READY, who, ARG_OBJECT (op)))
1497     return RESULT_INT (0);
1498    
1499     switch (op->type)
1500     {
1501     case WEAPON:
1502     if (op->level && (!op->name.starts_with (who->name) || op->name [who->name.length ()] != '\''))
1503     {
1504     /* if the weapon does not have the name as the character, can't use it. */
1505     /* (Ragnarok's sword attempted to be used by Foo: won't work) */
1506     who->failmsg ("The weapon does not recognize you as its owner. "
1507     "H<Its name indicates that it belongs to somebody else.>");
1508     if (tmp) who->insert (tmp);
1509     return 1;
1510     }
1511    
1512     op->flag [FLAG_APPLIED] = true;
1513    
1514     if (player *pl = who->contr)
1515     {
1516     who->statusmsg (format ("You wield %s.", query_name (op)));
1517     change_abil (who, op);
1518     }
1519     else
1520     who->change_skill (skop);
1521    
1522     op->flag [FLAG_READY_WEAPON] = true;
1523     break;
1524    
1525     case ARMOUR:
1526     case HELMET:
1527     case SHIELD:
1528     case BOOTS:
1529     case GLOVES:
1530     case GIRDLE:
1531     case BRACERS:
1532     case CLOAK:
1533     case RING:
1534     case AMULET:
1535     SET_FLAG (op, FLAG_APPLIED);
1536     who->statusmsg (format ("You wear %s.", query_name (op)));
1537     change_abil (who, op);
1538     break;
1539    
1540     case SKILL_TOOL:
1541     // applying a skill tool does not ready the skill
1542     // if something needs the skill, it has to ready it itself
1543     //TODO: unapplying should unapply the skill, though
1544     SET_FLAG (op, FLAG_APPLIED);
1545     break;
1546    
1547     case SKILL:
1548     if (!(aflags & AP_NO_SLOT))
1549     {
1550     // skill is used on it's own, as opposed to being a chosen_skill
1551    
1552     if (skill_flags [op->subtype] & (SF_NEED_ITEM | SF_MANA))
1553     {
1554     who->failmsg (format (
1555     "You feel as if you wanted to do something funny, but you can't remember what. "
1556     "H<The %s skill needs something else to function, for example a tool, weapon, rod, or spell. "
1557     "It cannot be used on its own.>",
1558     &op->skill
1559     ));
1560     if (tmp) who->insert (tmp);
1561     return 1;
1562     }
1563    
1564     if (skill_flags [op->subtype] & SF_AUTARK
1565     || !(skill_flags [op->subtype] & (SF_COMBAT | SF_RANGED)))
1566     {
1567     if (skill_flags [op->subtype] & SF_USE)
1568     who->failmsg (format (
1569     "You feel as if you wanted to do something funny, but you can't remember what. "
1570     "H<The %s skill cannot be readied, instead, try C<use_skill %s>.>",
1571     &op->skill, &op->skill
1572     ));
1573     else
1574     who->failmsg (format (
1575     "You feel as if you wanted to do something funny, but you can't remember what. "
1576     "H<The %s skill cannot be readied or used, it is always active.>",
1577     &op->skill
1578     ));
1579    
1580     if (tmp) who->insert (tmp);
1581    
1582     return 1;
1583     }
1584    
1585     if (who->contr)
1586     if (op->invisible)
1587     who->statusmsg (format ("You can now use the %s skill.", &op->skill));
1588     else
1589     who->statusmsg (format ("You ready %s.", query_name (op)));
1590     }
1591    
1592     SET_FLAG (who, FLAG_READY_SKILL);
1593     SET_FLAG (op, FLAG_APPLIED);
1594     change_abil (who, op);
1595     break;
1596    
1597     case BOW:
1598     if (op->level && (!op->name.starts_with (who->name) || op->name [who->name.length ()] != '\''))
1599     {
1600     who->failmsg ("The weapon does not recognize you as its owner. "
1601     "H<Its name indicates that it belongs to somebody else.>");
1602     if (tmp) who->insert (tmp);
1603     return 1;
1604     }
1605    
1606     if (player *pl = who->contr)
1607     {
1608     op->flag [FLAG_APPLIED] = true;
1609     who->statusmsg (format ("You wield the %s.", query_name (op)));
1610     change_abil (who, op);
1611     }
1612     break;
1613    
1614     case RANGED:
1615     if (player *pl = who->contr)
1616     {
1617     op->flag [FLAG_APPLIED] = true;
1618     who->statusmsg (format ("You applied the %s.", query_name (op)));
1619     }
1620     break;
1621    
1622     case SPELL:
1623     if (player *pl = who->contr)
1624     {
1625     op->flag [FLAG_APPLIED] = true;
1626     who->statusmsg (format ("You ready the spell %s.", query_name (op)));
1627     }
1628     break;
1629    
1630     /*FALLTHROUGH*/
1631     case WAND:
1632     case ROD:
1633     case HORN:
1634     /* check for skill, alter player status */
1635 root 1.198
1636 root 1.245 if (!skop)
1637     {
1638     who->failmsg (format ("The %s is broken, please report this to the dungeon master!", query_name (op)));//TODO
1639     if (tmp) who->insert (tmp);
1640     return 1;
1641     }
1642 root 1.198
1643 root 1.245 op->flag [FLAG_APPLIED] = true;
1644 elmex 1.1
1645 root 1.245 if (player *pl = who->contr)
1646     {
1647     who->statusmsg (format ("You ready %s.", query_name (op)));
1648 root 1.14
1649 root 1.245 if (op->type == BOW)
1650     who->statusmsg (format ("You will now fire %s with %s.", op->race ? &op->race : "nothing", query_name (op)));
1651 root 1.14
1652 root 1.245 change_abil (who, op);
1653     }
1654     else
1655     {
1656     who->change_skill (skop);
1657 root 1.14
1658 root 1.245 if (op->type == BOW)
1659     op->flag [FLAG_READY_BOW ] = true;
1660     else
1661     op->flag [FLAG_READY_RANGE] = true;
1662     }
1663 root 1.14
1664 root 1.245 break;
1665 root 1.198
1666 root 1.245 case BUILDER:
1667     if (player *pl = who->contr)
1668     {
1669     who->statusmsg (format ("You ready your %s.", query_name (op)));
1670     //TODO: change_abil?
1671     }
1672     break;
1673 root 1.14
1674 root 1.245 default:
1675     who->statusmsg (format ("You apply %s.", query_name (op)));
1676     }
1677 root 1.23
1678 root 1.245 SET_FLAG (op, FLAG_APPLIED);
1679 root 1.14
1680 root 1.245 if (tmp) who->insert (tmp);
1681 root 1.14
1682 root 1.245 who->update_stats ();
1683 root 1.14
1684 root 1.245 /* We exclude spell casting objects. The fire code will set the
1685     * been applied flag when they are used - until that point,
1686     * you don't know anything about them.
1687     */
1688     if (who->type == PLAYER && op->type != WAND && op->type != HORN && op->type != ROD)
1689     SET_FLAG (op, FLAG_BEEN_APPLIED);
1690 root 1.14
1691 root 1.245 if (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))
1692     if (who->type == PLAYER)
1693     {
1694     who->failmsg (
1695     "Oops, it feels deadly cold! "
1696     "H<Maybe it wasn't such a bright idea to apply this cursed or damned item.>"
1697     );
1698     SET_FLAG (op, FLAG_KNOWN_CURSED);
1699     }
1700 root 1.22
1701 root 1.245 if (object *pl = op->visible_to ())
1702     esrv_send_item (pl, op);
1703 root 1.14
1704 root 1.245 return 0;
1705     }
1706 root 1.14
1707 root 1.245 /**
1708     * Check if op should abort moving victim because of it's race or slaying.
1709     * Returns 1 if it should abort, returns 0 if it should continue.
1710     */
1711     int
1712     should_director_abort (object *op, object *victim)
1713     {
1714     int arch_flag, name_flag, race_flag;
1715 root 1.22
1716 root 1.245 /* Get flags to determine what of arch, name, and race should be checked.
1717     * This is stored in subtype, and is a bitmask, the LSB is the arch flag,
1718     * the next is the name flag, and the last is the race flag. Also note,
1719     * if subtype is set to zero, that also goes to defaults of all affecting
1720     * it. Examples:
1721     * subtype 1: only arch
1722     * subtype 3: arch or name
1723     * subtype 5: arch or race
1724     * subtype 7: all three
1725     */
1726     if (op->subtype)
1727     {
1728     arch_flag = op->subtype & 1;
1729     name_flag = op->subtype & 2;
1730     race_flag = op->subtype & 4;
1731     }
1732     else
1733     {
1734     arch_flag = 1;
1735     name_flag = 1;
1736     race_flag = 1;
1737     }
1738 root 1.14
1739 root 1.245 /* If the director has race set, only affect objects with a arch,
1740     * name or race that matches.
1741     */
1742     if ((op->race) &&
1743     ((!(victim->arch && arch_flag && victim->arch->archname) || op->race != victim->arch->archname)) &&
1744     ((!(victim->name && name_flag) || op->race != victim->name)) &&
1745     ((!(victim->race && race_flag) || op->race != victim->race)))
1746     return 1;
1747 root 1.14
1748 root 1.245 /* If the director has slaying set, only affect objects where none
1749     * of arch, name, or race match.
1750     */
1751     if ((op->slaying) && (((victim->arch && arch_flag && victim->arch->archname && op->slaying == victim->arch->archname))) ||
1752     ((victim->name && name_flag && op->slaying == victim->name)) ||
1753     ((victim->race && race_flag && op->slaying == victim->race)))
1754     return 1;
1755 root 1.118
1756 root 1.245 return 0;
1757     }
1758 root 1.14
1759 root 1.245 /**
1760     * This handles a player dropping money on an altar to identify stuff.
1761     * It'll identify marked item, if none all items up to dropped money.
1762     * Return value: 1 if money was destroyed, 0 if not.
1763     */
1764     static int
1765     apply_id_altar (object *money, object *altar, object *pl)
1766     {
1767     dynbuf_text &buf = msg_dynbuf; buf.clear ();
1768 root 1.14
1769 root 1.245 if (!pl || pl->type != PLAYER)
1770     return 0;
1771 root 1.14
1772 root 1.245 /* Check for MONEY type is a special hack - it prevents 'nothing needs
1773     * identifying' from being printed out more than it needs to be.
1774     */
1775     if (!check_altar_sacrifice (altar, money, pl) || money->type != MONEY)
1776     return 0;
1777 root 1.23
1778 root 1.245 /* if the player has a marked item, identify that if it needs to be
1779     * identified. If it doesn't, then go through the player inventory.
1780     */
1781     if (object *marked = find_marked_object (pl))
1782     if (!QUERY_FLAG (marked, FLAG_IDENTIFIED) && need_identify (marked))
1783     {
1784     if (operate_altar (altar, &money, pl))
1785     {
1786     identify (marked);
1787 root 1.23
1788 root 1.245 buf.printf ("You have %s.\r", long_desc (marked, pl));
1789     if (marked->msg)
1790     buf << "The item has a story:\r" << marked->msg << "\n\n";
1791 root 1.23
1792 root 1.245 return !money;
1793     }
1794     }
1795 root 1.14
1796 root 1.245 for (object *id = pl->inv; id; id = id->below)
1797     {
1798     if (!QUERY_FLAG (id, FLAG_IDENTIFIED) && !id->invisible && need_identify (id))
1799     {
1800     if (operate_altar (altar, &money, pl))
1801     {
1802     identify (id);
1803 root 1.55
1804 root 1.245 buf.printf ("You have %s.\r", long_desc (id, pl));
1805     if (id->msg)
1806     buf << "The item has a story:\r" << id->msg << "\n\n";
1807 root 1.14
1808 root 1.245 /* If no more money, might as well quit now */
1809     if (!money || !check_altar_sacrifice (altar, money))
1810     break;
1811     }
1812     else
1813     {
1814     LOG (llevError, "check_id_altar: Couldn't do sacrifice when we should have been able to\n");
1815     break;
1816     }
1817     }
1818     }
1819 root 1.23
1820 root 1.245 if (buf.empty ())
1821     pl->failmsg ("You have nothing that needs identifying");
1822     else
1823     pl->contr->infobox (MSG_CHANNEL ("identify"), buf);
1824 root 1.23
1825 root 1.245 return !money;
1826     }
1827 root 1.23
1828 root 1.245 /**
1829     * This checks whether the object has a "on_use_yield" field, and if so generated and drops
1830     * matching item.
1831     **/
1832     void
1833     handle_apply_yield (object *tmp)
1834     {
1835     if (shstr_tmp yield = tmp->kv (shstr_on_use_yield))
1836     archetype::get (yield)->insert_at (tmp, tmp, INS_BELOW_ORIGINATOR);
1837 elmex 1.1 }
1838    
1839     /**
1840 root 1.245 * Handles applying a potion.
1841 elmex 1.1 */
1842 root 1.245 int
1843     apply_potion (object *op, object *tmp)
1844 elmex 1.1 {
1845 root 1.245 int got_one = 0, i;
1846     object *force = 0;
1847 root 1.22
1848 root 1.245 object *floor = GET_MAP_OB (op->map, op->x, op->y);
1849 root 1.112
1850 root 1.245 if (get_map_flags (op->map, NULL, op->x, op->y, NULL, NULL) & P_SAFE)
1851 root 1.22 {
1852 root 1.245 op->failmsg ("Gods prevent you from using this here, it's sacred ground!");
1853 elmex 1.1
1854 root 1.245 CLEAR_FLAG (tmp, FLAG_APPLIED);
1855     return 0;
1856 elmex 1.1 }
1857 root 1.112
1858 root 1.245 if (op->type == PLAYER)
1859     if (!QUERY_FLAG (tmp, FLAG_IDENTIFIED))
1860     identify (tmp);
1861 elmex 1.1
1862 root 1.245 handle_apply_yield (tmp);
1863 elmex 1.223
1864 root 1.245 /* Potion of restoration - only for players */
1865     if (op->type == PLAYER && (tmp->attacktype & AT_DEPLETE))
1866     {
1867     object *depl;
1868     archetype *at;
1869 root 1.22
1870 root 1.245 if (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))
1871     {
1872     op->drain_stat ();
1873     op->update_stats ();
1874     tmp->decrease ();
1875     return 1;
1876     }
1877 root 1.22
1878 root 1.245 if (!(at = archetype::find (shstr_depletion)))
1879 root 1.22 {
1880 root 1.245 LOG (llevError, "Could not find archetype depletion\n");
1881     return 0;
1882 root 1.22 }
1883 root 1.41
1884 root 1.245 depl = present_arch_in_ob (at, op);
1885 elmex 1.1
1886 root 1.245 if (depl)
1887     {
1888     for (i = 0; i < NUM_STATS; i++)
1889     if (depl->stats.stat (i))
1890     op->statusmsg (restore_msg[i]);
1891 root 1.208
1892 root 1.245 depl->destroy ();
1893     op->update_stats ();
1894 root 1.208 }
1895 root 1.245 else
1896     op->statusmsg ("Your potion had no effect.");
1897 root 1.208
1898 root 1.245 tmp->decrease ();
1899     return 1;
1900 elmex 1.1 }
1901    
1902 root 1.245 /* improvement potion - only for players */
1903     if (op->type == PLAYER && (tmp->attacktype & AT_GODPOWER))
1904 root 1.22 {
1905 root 1.245 for (i = 1; i < min (11, op->level); i++)
1906     {
1907     if (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))
1908     {
1909     if (op->contr->levhp[i] != 1)
1910     {
1911     op->contr->levhp[i] = 1;
1912     break;
1913     }
1914 elmex 1.1
1915 root 1.245 if (op->contr->levsp[i] != 1)
1916     {
1917     op->contr->levsp[i] = 1;
1918     break;
1919     }
1920 elmex 1.1
1921 root 1.245 if (op->contr->levgrace[i] != 1)
1922     {
1923     op->contr->levgrace[i] = 1;
1924     break;
1925     }
1926     }
1927     else
1928     {
1929     if (op->contr->levhp[i] < 9)
1930     {
1931     op->contr->levhp[i] = 9;
1932     break;
1933     }
1934 root 1.118
1935 root 1.245 if (op->contr->levsp[i] < 6)
1936     {
1937     op->contr->levsp[i] = 6;
1938     break;
1939     }
1940 root 1.22
1941 root 1.245 if (op->contr->levgrace[i] < 3)
1942     {
1943     op->contr->levgrace[i] = 3;
1944     break;
1945     }
1946     }
1947     }
1948 elmex 1.1
1949 root 1.245 /* Just makes checking easier */
1950     if (i < min (11, op->level))
1951     got_one = 1;
1952 elmex 1.1
1953 root 1.245 if (!QUERY_FLAG (tmp, FLAG_CURSED) && !QUERY_FLAG (tmp, FLAG_DAMNED))
1954     {
1955     if (got_one)
1956     {
1957     op->update_stats ();
1958     op->statusmsg ("The Gods smile upon you and remake you "
1959     "a little more in their image. "
1960     "You feel a little more perfect.", NDI_GREEN);
1961     }
1962     else
1963     op->statusmsg ("The potion had no effect - you are already perfect.");
1964     }
1965     else
1966     { /* cursed potion */
1967     if (got_one)
1968     {
1969     op->update_stats ();
1970     op->failmsg ("The Gods are angry and punish you.");
1971     }
1972     else
1973     op->statusmsg ("You are fortunate that you are so pathetic.", NDI_DK_ORANGE);
1974     }
1975 root 1.22
1976 root 1.245 tmp->decrease ();
1977     return 1;
1978 root 1.22 }
1979 elmex 1.1
1980 root 1.22
1981 root 1.245 /* A potion that casts a spell. Healing, restore spellpoint (power potion)
1982     * and heroism all fit into this category. Given the spell object code,
1983     * there is no limit to the number of spells that potions can be cast,
1984     * but direction is problematic to try and imbue fireball potions for example.
1985 root 1.22 */
1986 root 1.245 if (tmp->inv)
1987 root 1.22 {
1988 root 1.245 if (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))
1989 root 1.22 {
1990 root 1.245 op->failmsg ("Yech! Your lungs are on fire!");
1991     create_exploding_ball_at (op, op->level);
1992 root 1.14 }
1993 root 1.22 else
1994 root 1.245 cast_spell (op, tmp, op->facing, tmp->inv, NULL);
1995 root 1.114
1996 root 1.245 tmp->decrease ();
1997 root 1.22
1998 root 1.245 /* if youre dead, no point in doing this... */
1999     if (!QUERY_FLAG (op, FLAG_REMOVED))
2000     op->update_stats ();
2001 root 1.22
2002 root 1.245 return 1;
2003 root 1.22 }
2004    
2005 root 1.245 /* Deal with protection potions */
2006     force = NULL;
2007     for (i = 0; i < NROFATTACKS; i++)
2008     {
2009     if (tmp->resist[i])
2010     {
2011     if (!force)
2012     force = get_archetype (FORCE_NAME);
2013 root 1.32
2014 root 1.245 memcpy (force->resist, tmp->resist, sizeof (tmp->resist));
2015     force->type = POTION_EFFECT;
2016     break; /* Only need to find one protection since we copy entire batch */
2017     }
2018 root 1.22 }
2019 root 1.31
2020 root 1.245 /* This is a protection potion */
2021     if (force)
2022 root 1.22 {
2023 root 1.245 /* cursed items last longer */
2024     if (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))
2025     {
2026     force->stats.food *= 10;
2027     for (i = 0; i < NROFATTACKS; i++)
2028     if (force->resist[i] > 0)
2029     force->resist[i] = -force->resist[i]; /* prot => vuln */
2030     }
2031 root 1.22
2032 root 1.245 force->speed_left = -1;
2033     force = insert_ob_in_ob (force, op);
2034     CLEAR_FLAG (tmp, FLAG_APPLIED);
2035     SET_FLAG (force, FLAG_APPLIED);
2036     change_abil (op, force);
2037     tmp->decrease ();
2038     return 1;
2039 elmex 1.1 }
2040 root 1.22
2041 root 1.245 /* Only thing left are the stat potions */
2042     if (op->type == PLAYER)
2043     { /* only for players */
2044     if ((QUERY_FLAG (tmp, FLAG_CURSED)
2045     || QUERY_FLAG (tmp, FLAG_DAMNED))
2046     && tmp->value != 0)
2047     CLEAR_FLAG (tmp, FLAG_APPLIED);
2048     else
2049     SET_FLAG (tmp, FLAG_APPLIED);
2050 root 1.25
2051 root 1.245 if (!change_abil (op, tmp))
2052     op->statusmsg ("Nothing happened.");
2053 elmex 1.1 }
2054    
2055 root 1.245 /* CLEAR_FLAG is so that if the character has other potions
2056     * that were grouped with the one consumed, his
2057     * stat will not be raised by them. fix_player just clears
2058     * up all the stats.
2059 root 1.22 */
2060 root 1.245 CLEAR_FLAG (tmp, FLAG_APPLIED);
2061     op->update_stats ();
2062     tmp->decrease ();
2063     return 1;
2064     }
2065 root 1.22
2066 root 1.245 /**
2067     * 'victim' moves onto 'trap'
2068     * 'victim' leaves 'trap'
2069     * effect is determined by move_on/move_off of trap and move_type of victime.
2070     *
2071     * originator: Player, monster or other object that caused 'victim' to move
2072     * onto 'trap'. Will receive messages caused by this action. May be NULL.
2073     * However, some types of traps require an originator to function.
2074 elmex 1.1 */
2075 root 1.22 void
2076 root 1.245 move_apply (object *trap, object *victim, object *originator)
2077 elmex 1.1 {
2078 root 1.245 static int recursion_depth = 0;
2079    
2080     /* Only exits affect DMs. */
2081     if (QUERY_FLAG (victim, FLAG_WIZPASS) && trap->type != EXIT && trap->type != SIGN)
2082     return;
2083 elmex 1.1
2084 root 1.245 /* move_apply() is the most likely candidate for causing unwanted and
2085     * possibly unlimited recursion.
2086     */
2087     /* The following was changed because it was causing perfectly correct
2088     * maps to fail. 1) it's not an error to recurse:
2089     * rune detonates, summoning monster. monster lands on nearby rune.
2090     * nearby rune detonates. This sort of recursion is expected and
2091     * proper. This code was causing needless crashes.
2092     */
2093     if (recursion_depth >= 500)
2094 root 1.22 {
2095 root 1.245 LOG (llevDebug, "WARNING: move_apply(): aborting recursion "
2096     "[trap arch %s, name %s; victim arch %s, name %s]\n", &trap->arch->archname, &trap->name, &victim->arch->archname, &victim->name);
2097 root 1.22 return;
2098 elmex 1.1 }
2099    
2100 root 1.245 recursion_depth++;
2101     if (trap->head)
2102     trap = trap->head;
2103    
2104     if (!INVOKE_OBJECT (MOVE_TRIGGER, trap, ARG_OBJECT (victim), ARG_OBJECT (originator)))
2105     switch (trap->type)
2106     {
2107     case PLAYERMOVER:
2108     if (trap->attacktype && (trap->level || victim->type != PLAYER) && !should_director_abort (trap, victim))
2109     {
2110     if (!trap->stats.maxsp)
2111     trap->stats.maxsp = 2;
2112 elmex 1.1
2113 root 1.245 /* Is this correct? From the docs, it doesn't look like it
2114     * should be divided by trap->speed
2115     */
2116     victim->speed_left = -trap->stats.maxsp * victim->speed / trap->speed;
2117 elmex 1.1
2118 root 1.245 /* Just put in some sanity check. I think there is a bug in the
2119     * above with some objects have zero speed, and thus the player
2120     * getting permanently paralyzed.
2121     */
2122     if (victim->speed_left < -50.f)
2123     victim->speed_left = -50.f;
2124     /* LOG(llevDebug, "apply, playermove, player speed_left=%f\n", victim->speed_left); */
2125     }
2126     break;
2127 elmex 1.1
2128 root 1.245 case SPINNER:
2129     if (victim->direction)
2130     {
2131     victim->direction = absdir (victim->direction - trap->stats.sp);
2132     update_turn_face (victim);
2133     }
2134     break;
2135 elmex 1.1
2136 root 1.245 case DIRECTOR:
2137     if (victim->direction && !should_director_abort (trap, victim))
2138     {
2139     victim->direction = trap->stats.sp;
2140     update_turn_face (victim);
2141     }
2142     break;
2143 elmex 1.1
2144 root 1.245 case BUTTON:
2145     case PEDESTAL:
2146     case T_MATCH:
2147     update_button (trap, originator);
2148     break;
2149 elmex 1.1
2150 root 1.245 case ALTAR:
2151     /* sacrifice victim on trap */
2152     apply_altar (trap, victim, originator);
2153     break;
2154 elmex 1.1
2155 root 1.245 case THROWN_OBJ:
2156     if (trap->inv == NULL)
2157     break;
2158     /* fallthrough */
2159 elmex 1.1
2160 root 1.245 case ARROW:
2161     /* bad bug: monster throw a object, make a step forwards, step on object ,
2162     * trigger this here and get hit by own missile - and will be own enemy.
2163     * Victim then is his own enemy and will start to kill herself (this is
2164     * removed) but we have not synced victim and his missile. To avoid senseless
2165     * action, we avoid hits here
2166     */
2167     if ((QUERY_FLAG (victim, FLAG_ALIVE) && trap->has_active_speed ())
2168     && trap->owner != victim)
2169     hit_with_arrow (trap, victim);
2170     break;
2171 root 1.22
2172 root 1.245 case SPELL_EFFECT:
2173     apply_spell_effect (trap, victim);
2174     break;
2175 root 1.99
2176 root 1.245 case TRAPDOOR:
2177     {
2178     int max, sound_was_played;
2179     object *ab, *ab_next;
2180 elmex 1.1
2181 root 1.245 if (!trap->value)
2182     {
2183     int tot;
2184 root 1.22
2185 root 1.245 for (ab = trap->above, tot = 0; ab; ab = ab->above)
2186     if ((ab->move_type && trap->move_on) || ab->move_type == 0)
2187     tot += ab->head_ ()->total_weight ();
2188 root 1.37
2189 root 1.245 if (!(trap->value = (tot > trap->weight) ? 1 : 0))
2190     break;
2191 elmex 1.1
2192 root 1.245 SET_ANIMATION (trap, trap->value);
2193     update_object (trap, UP_OBJ_FACE);
2194     }
2195 elmex 1.1
2196 root 1.245 for (ab = trap->above, max = 100, sound_was_played = 0; --max && ab; ab = ab_next)
2197     {
2198     /* need to set this up, since if we do transfer the object,
2199     * ab->above would be bogus
2200     */
2201     ab_next = ab->above;
2202 root 1.22
2203 root 1.245 if ((ab->move_type && trap->move_on) || ab->move_type == 0)
2204     {
2205     if (!sound_was_played)
2206     {
2207     trap->play_sound (trap->sound ? trap->sound : sound_find ("fall_hole"));
2208     sound_was_played = 1;
2209     }
2210 root 1.22
2211 root 1.245 ab->statusmsg ("You fall into a trapdoor!", NDI_RED);
2212     transfer_ob (ab, EXIT_X (trap), EXIT_Y (trap), 0, ab);
2213     }
2214     }
2215     break;
2216     }
2217 root 1.22
2218 root 1.245 case CONVERTER:
2219     if (convert_item (victim, trap) < 0)
2220     {
2221     originator->failmsg (format ("The %s seems to be broken!", query_name (trap)));
2222     archetype::get (shstr_burnout)->insert_at (trap, trap);
2223     }
2224 root 1.22
2225 root 1.245 break;
2226 root 1.22
2227 root 1.245 case TRIGGER_BUTTON:
2228     case TRIGGER_PEDESTAL:
2229     case TRIGGER_ALTAR:
2230     check_trigger (trap, victim, originator);
2231     break;
2232 root 1.124
2233 root 1.245 case DEEP_SWAMP:
2234     walk_on_deep_swamp (trap, victim);
2235     break;
2236 root 1.22
2237 root 1.245 case CHECK_INV:
2238     check_inv (victim, trap);
2239     break;
2240 root 1.22
2241 root 1.245 case HOLE:
2242     move_apply_hole (trap, victim);
2243     break;
2244 root 1.22
2245 root 1.245 case EXIT:
2246     if (victim->type == PLAYER && EXIT_PATH (trap))
2247     {
2248     /* Basically, don't show exits leading to random maps the
2249     * players output.
2250     */
2251     if (trap->msg && EXIT_PATH (trap) != shstr_random_map_exit)
2252     victim->statusmsg (trap->msg, NDI_NAVY);
2253 root 1.22
2254 root 1.245 trap->play_sound (trap->sound);
2255     victim->enter_exit (trap);
2256     }
2257     break;
2258 root 1.22
2259 root 1.245 case ENCOUNTER:
2260     /* may be some leftovers on this */
2261     break;
2262 root 1.22
2263 root 1.245 case SHOP_MAT:
2264     apply_shop_mat (trap, victim);
2265     break;
2266 root 1.22
2267 root 1.245 /* Drop a certain amount of gold, and have one item identified */
2268     case IDENTIFY_ALTAR:
2269     apply_id_altar (victim, trap, originator);
2270     break;
2271 root 1.22
2272 root 1.245 case SIGN:
2273     if (victim->type != PLAYER && trap->stats.food > 0)
2274     break; /* monsters musn't apply magic_mouths with counters */
2275 root 1.22
2276 root 1.245 apply_sign (victim, trap, 1);
2277     break;
2278 root 1.22
2279 root 1.245 case CONTAINER:
2280     apply_container (victim, trap);
2281     break;
2282 root 1.22
2283 root 1.245 case RUNE:
2284     case TRAP:
2285     if (trap->level && QUERY_FLAG (victim, FLAG_ALIVE))
2286     spring_trap (trap, victim);
2287     break;
2288 root 1.22
2289 root 1.245 default:
2290     LOG (llevDebug, "name %s, arch %s, type %d with fly/walk on/off not "
2291     "handled in move_apply()\n", &trap->name, &trap->arch->archname, trap->type);
2292     break;
2293     }
2294 root 1.124
2295 root 1.245 recursion_depth--;
2296     }
2297 root 1.124
2298 root 1.245 /**
2299     * Handles reading a regular (ie not containing a spell) book.
2300     */
2301     static void
2302     apply_book (object *op, object *tmp)
2303     {
2304     int lev_diff;
2305     object *skill_ob;
2306 root 1.22
2307 root 1.245 if (QUERY_FLAG (op, FLAG_BLIND) && !QUERY_FLAG (op, FLAG_WIZ))
2308     {
2309     op->failmsg ("You are unable to read while blind!");
2310     return;
2311     }
2312 root 1.22
2313 root 1.245 if (!tmp->msg)
2314 root 1.22 {
2315 root 1.245 op->failmsg (format ("The %s contains nothing but meaningless gibberish. H<There is nothing interesting to read here.>", &tmp->name));
2316     return;
2317     }
2318 root 1.22
2319 root 1.245 /* need a literacy skill to read stuff! */
2320     skill_ob = find_skill_by_name (op, tmp->skill);
2321     if (!skill_ob)
2322     {
2323     op->failmsg (format ("You are unable to decipher the strange symbols. H<You lack the %s skill to read this.>", &tmp->skill));
2324     return;
2325 root 1.22 }
2326    
2327 root 1.245 lev_diff = tmp->level - (skill_ob->level + 5);
2328     if (!QUERY_FLAG (op, FLAG_WIZ) && lev_diff > 0)
2329 root 1.22 {
2330 root 1.245 op->failmsg (lev_diff < 2 ? "This book is just barely beyond your comprehension."
2331     : lev_diff < 3 ? "This book is slightly beyond your comprehension."
2332     : lev_diff < 5 ? "This book is beyond your comprehension."
2333     : lev_diff < 8 ? "This book is quite a bit beyond your comprehension."
2334     : lev_diff < 15 ? "This book is way beyond your comprehension."
2335     : "This book is totally beyond your comprehension.");
2336     return;
2337     }
2338    
2339     // we currently don't use the message types for anything.
2340     // readable_message_type *msgType = get_readable_message_type (tmp);
2341    
2342     tmp->play_sound (tmp->sound ? tmp->sound : sound_find ("apply_book"));
2343    
2344     if (player *pl = op->contr)
2345     if (client *ns = pl->ns)
2346     pl->infobox (MSG_CHANNEL ("book"), format ("T<%s>\n\n%s", (char *)long_desc (tmp, op), &tmp->msg));
2347    
2348     /* gain xp from reading */
2349     if (!QUERY_FLAG (tmp, FLAG_NO_SKILL_IDENT))
2350     { /* only if not read before */
2351     int exp_gain = calc_skill_exp (op, tmp, skill_ob);
2352 root 1.22
2353 root 1.245 if (!QUERY_FLAG (tmp, FLAG_IDENTIFIED))
2354 root 1.22 {
2355 root 1.245 /*exp_gain *= 2; because they just identified it too */
2356     SET_FLAG (tmp, FLAG_IDENTIFIED);
2357    
2358     if (object *pl = tmp->visible_to ())
2359     esrv_update_item (UPD_FLAGS | UPD_NAME, pl, tmp);
2360 root 1.22 }
2361 root 1.245
2362     change_exp (op, exp_gain, skill_ob->skill, 0);
2363     SET_FLAG (tmp, FLAG_NO_SKILL_IDENT); /* so no more xp gained from this book */
2364 elmex 1.1 }
2365     }
2366    
2367     /**
2368 root 1.245 * op made some mistake with a scroll, this takes care of punishment.
2369     * scroll_failure()- hacked directly from spell_failure
2370 root 1.208 */
2371     static void
2372 root 1.245 scroll_failure (object *op, int failure, int power)
2373 root 1.208 {
2374 root 1.245 if (abs (failure / 4) > power)
2375     power = abs (failure / 4); /* set minimum effect */
2376    
2377     if (failure <= -1 && failure > -15)
2378     { /* wonder */
2379     object *tmp;
2380 root 1.208
2381 root 1.245 op->failmsg ("Your spell warps!");
2382     tmp = get_archetype (SPELL_WONDER);
2383     cast_wonder (op, op, 0, tmp);
2384     tmp->destroy ();
2385     }
2386     else if (failure <= -15 && failure > -35)
2387     { /* drain mana */
2388     op->failmsg ("Your mana is drained!");
2389     op->stats.sp -= random_roll (0, power - 1, op, PREFER_LOW);
2390     if (op->stats.sp < 0)
2391     op->stats.sp = 0;
2392     }
2393     else if (settings.spell_failure_effects == TRUE)
2394 root 1.208 {
2395 root 1.245 if (failure <= -35 && failure > -60)
2396     { /* confusion */
2397     op->failmsg ("The magic recoils on you!");
2398     confuse_player (op, op, power);
2399     }
2400     else if (failure <= -60 && failure > -70)
2401     { /* paralysis */
2402     op->failmsg ("The magic recoils and paralyzes you!");
2403     paralyze_player (op, op, power);
2404     }
2405     else if (failure <= -70 && failure > -80)
2406     { /* blind */
2407     op->failmsg ("The magic recoils on you!");
2408     blind_player (op, op, power);
2409     }
2410     else if (failure <= -80)
2411     { /* blast the immediate area */
2412     object *tmp = get_archetype (LOOSE_MANA);
2413     cast_magic_storm (op, tmp, power);
2414     op->failmsg ("You unleash uncontrolled mana!");
2415     tmp->destroy ();
2416 root 1.208 }
2417     }
2418     }
2419    
2420     /**
2421 root 1.245 * Handles the applying of a skill scroll, calling learn_skill straight.
2422     * op is the person learning the skill, tmp is the skill scroll object
2423 elmex 1.1 */
2424 root 1.22 static void
2425 root 1.245 apply_skillscroll (object *op, object *tmp)
2426 elmex 1.1 {
2427 root 1.245 switch (learn_skill (op, tmp))
2428     {
2429     case 0:
2430     op->play_sound (sound_find ("generic_fail"));
2431     op->failmsg (format ("You already possess the knowledge held within the %s.", query_name (tmp)));
2432     break;
2433    
2434     case 1:
2435     tmp->decrease ();
2436     op->play_sound (sound_find ("skill_learn"));
2437     op->statusmsg (format ("You succeed in learning %s", &tmp->skill));
2438     break;
2439 elmex 1.1
2440 root 1.245 default:
2441     tmp->decrease ();
2442     op->play_sound (sound_find ("generic_fail"));
2443     op->failmsg (format ("You fail to learn the knowledge of the %s.\n", query_name (tmp)));
2444     break;
2445 elmex 1.1 }
2446 root 1.245 }
2447 root 1.51
2448 root 1.245 /**
2449     * Actually makes op learn spell.
2450     * Informs player of what happens.
2451     */
2452     void
2453     do_learn_spell (object *op, object *spell, int special_prayer)
2454     {
2455     object *tmp;
2456 root 1.51
2457 root 1.245 if (op->type != PLAYER)
2458 root 1.22 {
2459 root 1.245 LOG (llevError, "BUG: do_learn_spell(): not a player\n");
2460 elmex 1.1 return;
2461     }
2462 root 1.51
2463 root 1.245 /* Upgrade special prayers to normal prayers */
2464     if ((tmp = check_spell_known (op, spell->name)) != NULL)
2465 elmex 1.1 {
2466 root 1.245 if (special_prayer && !QUERY_FLAG (tmp, FLAG_STARTEQUIP))
2467     {
2468     LOG (llevError, "BUG: do_learn_spell(): spell already known, but not marked as startequip\n");
2469     return;
2470     }
2471 root 1.22 return;
2472 elmex 1.1 }
2473    
2474 root 1.245 op->contr->play_sound (sound_find ("learn_spell"));
2475    
2476     tmp = spell->clone ();
2477     insert_ob_in_ob (tmp, op);
2478    
2479     if (special_prayer)
2480     SET_FLAG (tmp, FLAG_STARTEQUIP);
2481    
2482     esrv_add_spells (op->contr, tmp);
2483 elmex 1.1 }
2484    
2485 root 1.245 /**
2486     * Erases spell from player's inventory.
2487     */
2488 root 1.151 void
2489 root 1.245 do_forget_spell (object *op, const char *spell)
2490 elmex 1.1 {
2491 root 1.245 object *spob;
2492 root 1.158
2493 root 1.245 if (op->type != PLAYER)
2494 root 1.22 {
2495 root 1.245 LOG (llevError, "BUG: do_forget_spell(): not a player\n");
2496     return;
2497 elmex 1.1 }
2498 root 1.245 if ((spob = check_spell_known (op, spell)) == NULL)
2499 root 1.22 {
2500 root 1.245 LOG (llevError, "BUG: do_forget_spell(): spell not known\n");
2501     return;
2502 elmex 1.1 }
2503 root 1.114
2504 root 1.245 op->failmsg (format ("You lose knowledge of %s.", spell));
2505     esrv_remove_spell (op->contr, spob);
2506     spob->destroy ();
2507 elmex 1.1 }
2508    
2509     /**
2510 root 1.245 * Handles player applying a spellbook.
2511     * Checks whether player has knowledge of required skill, doesn't already know the spell,
2512     * stuff like that. Random learning failure too.
2513 elmex 1.184 */
2514 root 1.245 static void
2515     apply_spellbook (object *op, object *tmp)
2516 elmex 1.184 {
2517 root 1.245 object *skop, *spell, *spell_skill;
2518    
2519     if (QUERY_FLAG (op, FLAG_BLIND) && !QUERY_FLAG (op, FLAG_WIZ))
2520     {
2521     op->failmsg ("You are unable to read while blind.");
2522     return;
2523     }
2524    
2525     /* artifact_spellbooks have 'slaying' field point to a spell name,
2526     * instead of having their spell stored in stats.sp. These are
2527     * legacy spellbooks
2528     */
2529     if (tmp->slaying)
2530 elmex 1.184 {
2531 root 1.245 spell = find_archetype_by_object_name (tmp->slaying)->instance ();
2532    
2533     if (!spell)
2534 elmex 1.184 {
2535 root 1.245 op->failmsg (format ("The book's formula for %s is incomplete.", &tmp->slaying));
2536     return;
2537 elmex 1.184 }
2538 root 1.245 else
2539     insert_ob_in_ob (spell, tmp);
2540 elmex 1.184
2541 root 1.245 tmp->slaying = 0;
2542 elmex 1.184 }
2543    
2544 root 1.245 skop = find_skill_by_name (op, tmp->skill);
2545    
2546     /* need a literacy skill to learn spells. Also, having a literacy level
2547     * lower than the spell will make learning the spell more difficult */
2548     if (!skop)
2549 elmex 1.184 {
2550 root 1.245 op->failmsg (format ("You can't read! Your attempt fails. H<You lack the %s skill.>", &tmp->skill));
2551     return;
2552 elmex 1.184 }
2553    
2554 root 1.245 spell = tmp->inv;
2555 elmex 1.184
2556 root 1.245 if (!spell)
2557 elmex 1.184 {
2558 root 1.245 LOG (llevError, "apply_spellbook: Book %s has no spell in it!\n", &tmp->name);
2559     op->failmsg ("The spellbook symbols make no sense. This is a bug, please report!");
2560     return;
2561 elmex 1.184 }
2562    
2563 root 1.245 int learn_level = sqrtf (spell->level) * 1.5f;
2564     if (skop->level < learn_level)
2565 elmex 1.187 {
2566 root 1.245 op->failmsg (format ("You are unable to decipher the strange symbols. H<Your %s level is too low, it must be at least %d.>",
2567     &tmp->skill, learn_level));
2568     return;
2569 elmex 1.187 }
2570    
2571 root 1.245 op->statusmsg (format ("The spellbook contains the %s level spell %s.", ordinal (spell->level), &spell->name));
2572 elmex 1.187
2573 root 1.245 if (!QUERY_FLAG (tmp, FLAG_IDENTIFIED))
2574     identify (tmp);
2575 elmex 1.184
2576 root 1.245 /* I removed the check for special_prayer_mark here - it didn't make
2577     * a lot of sense - special prayers are not found in spellbooks, and
2578     * if the player doesn't know the spell, doesn't make a lot of sense that
2579     * they would have a special prayer mark.
2580     */
2581     if (check_spell_known (op, spell->name))
2582 elmex 1.184 {
2583 root 1.245 op->statusmsg ("You already know that spell. H<It makes no sense to learn spells twice, and would only waste the spellbook.>\n");
2584     return;
2585     }
2586 elmex 1.184
2587 root 1.245 if (spell->skill)
2588     {
2589     spell_skill = find_skill_by_name (op, spell->skill);
2590 elmex 1.184
2591 root 1.245 if (!spell_skill)
2592 elmex 1.184 {
2593 root 1.245 op->failmsg (format ("You lack the %s skill to use this spell.", &spell->skill));
2594 elmex 1.184 return;
2595     }
2596    
2597 root 1.245 if (spell_skill->level < spell->level)
2598 elmex 1.187 {
2599 root 1.245 op->failmsg (format ("You need to be level %d in %s to learn this spell.", spell->level, &spell->skill));
2600 elmex 1.187 return;
2601     }
2602 root 1.245 }
2603 elmex 1.187
2604 root 1.245 /* Logic as follows
2605     *
2606     * 1- MU spells use Int to learn, Cleric spells use Wisdom
2607     *
2608     * 2- The learner's skill level in literacy adjusts the chance to learn
2609     * a spell.
2610     *
2611     * 3 -Automatically fail to learn if you read while confused
2612     *
2613     * Overall, chances are the same but a player will find having a high
2614     * literacy rate very useful! -b.t.
2615     */
2616     if (QUERY_FLAG (op, FLAG_CONFUSED))
2617     {
2618     op->failmsg ("In your confused state you flub the wording of the text!");
2619     scroll_failure (op, 0 - random_roll (0, spell->level, op, PREFER_LOW), max (spell->stats.sp, spell->stats.grace));
2620 elmex 1.184 }
2621 root 1.245 else if (QUERY_FLAG (tmp, FLAG_STARTEQUIP) ||
2622     (random_roll (0, 100, op, PREFER_LOW) - (5 * skop->level)) < learn_spell[spell->stats.grace ? op->stats.Wis : op->stats.Int])
2623 elmex 1.184 {
2624 root 1.245 op->statusmsg ("You succeed in learning the spell!", NDI_GREEN);
2625     do_learn_spell (op, spell, 0);
2626 elmex 1.187
2627 root 1.245 /* xp gain to literacy for spell learning */
2628     if (!QUERY_FLAG (tmp, FLAG_STARTEQUIP))
2629     change_exp (op, calc_skill_exp (op, tmp, skop), skop->skill, 0);
2630 elmex 1.184 }
2631     else
2632     {
2633 root 1.245 op->contr->play_sound (sound_find ("fumble_spell"));
2634     op->failmsg ("You fail to learn the spell. H<Wis (priests) or Int (wizards) governs the chance of learning a prayer or spell.>\n");
2635 elmex 1.184 }
2636    
2637 root 1.245 tmp->decrease ();
2638 elmex 1.184 }
2639    
2640     /**
2641 root 1.245 * Handles applying a spell scroll.
2642 root 1.208 */
2643 root 1.245 void
2644     apply_scroll (object *op, object *tmp, int dir)
2645 root 1.208 {
2646 root 1.245 object *skop;
2647 root 1.208
2648 root 1.245 if (QUERY_FLAG (op, FLAG_BLIND) && !QUERY_FLAG (op, FLAG_WIZ))
2649 root 1.208 {
2650 root 1.245 op->failmsg ("You are unable to read while blind.");
2651 root 1.208 return;
2652     }
2653    
2654 root 1.245 if (!tmp->inv || tmp->inv->type != SPELL)
2655 root 1.208 {
2656 root 1.245 op->failmsg ("The scroll just doesn't make sense! H<...and never will make sense.>");
2657 root 1.208 return;
2658     }
2659    
2660 root 1.245 if (op->type == PLAYER)
2661 root 1.208 {
2662 root 1.245 /* players need a literacy skill to read stuff! */
2663     int exp_gain = 0;
2664    
2665     /* hard code literacy - tmp->skill points to where the exp
2666     * should go for anything killed by the spell.
2667     */
2668     skop = find_skill_by_name (op, skill_names[SK_LITERACY]);
2669 root 1.208
2670 root 1.245 if (!skop)
2671 root 1.208 {
2672 root 1.245 op->failmsg (format ("You are unable to decipher the strange symbols. H<You lack the %s skill.>", &skill_names[SK_LITERACY]));
2673     return;
2674 root 1.208 }
2675 root 1.245
2676     if ((exp_gain = calc_skill_exp (op, tmp, skop)))
2677     change_exp (op, exp_gain, skop->skill, 0);
2678 root 1.208 }
2679    
2680 root 1.245 if (!QUERY_FLAG (tmp, FLAG_IDENTIFIED))
2681     identify (tmp);
2682    
2683     op->statusmsg (format ("The scroll of %s turns to dust.", &tmp->inv->name));
2684    
2685     cast_spell (op, tmp, dir, tmp->inv, NULL);
2686     tmp->decrease ();
2687     }
2688    
2689     /**
2690     * Applies a treasure object - by default, chest. op
2691     * is the person doing the applying, tmp is the treasure
2692     * chest.
2693     */
2694     static void
2695     apply_treasure (object *op, object *tmp)
2696     {
2697     /* Nice side effect of this treasure creation method is that the treasure
2698     * for the chest is done when the chest is created, and put into the chest
2699     * inventory. So that when the chest burns up, the items still exist. Also
2700     * prevents people from moving chests to more difficult maps to get better
2701     * treasure
2702     */
2703     object *treas = tmp->inv;
2704 root 1.208
2705 root 1.245 if (!treas)
2706 root 1.208 {
2707 root 1.245 op->statusmsg ("The chest was empty.");
2708     tmp->decrease ();
2709 root 1.208 return;
2710     }
2711    
2712 root 1.245 while (tmp->inv)
2713     {
2714     treas = tmp->inv;
2715     treas->remove ();
2716    
2717     treas->x = op->x;
2718     treas->y = op->y;
2719     treas = insert_ob_in_map (treas, op->map, op, INS_BELOW_ORIGINATOR);
2720 root 1.208
2721 root 1.245 if (treas && (treas->type == RUNE || treas->type == TRAP) && treas->level && QUERY_FLAG (op, FLAG_ALIVE))
2722     spring_trap (treas, op);
2723 root 1.208
2724 root 1.245 /* If either player or container was destroyed, no need to do
2725     * further processing. I think this should be enclused with
2726     * spring trap above, as I don't think there is otherwise
2727     * any way for the treasure chest or player to get killed.
2728     */
2729     if (op->destroyed () || tmp->destroyed ())
2730     break;
2731     }
2732 root 1.208
2733 root 1.245 if (!tmp->destroyed () && !tmp->inv)
2734     tmp->decrease (true);
2735 root 1.208 }
2736    
2737     /**
2738 root 1.245 * A dragon is eating some flesh. If the flesh contains resistances,
2739     * there is a chance for the dragon's skin to get improved.
2740 elmex 1.1 *
2741 root 1.245 * attributes:
2742     * object *op the object (dragon player) eating the flesh
2743     * object *meal the flesh item, getting chewed in dragon's mouth
2744     * return:
2745     * int 1 if eating successful, 0 if it doesn't work
2746 elmex 1.1 */
2747 root 1.235 static int
2748 root 1.245 dragon_eat_flesh (object *op, object *meal)
2749 elmex 1.1 {
2750 root 1.245 object *skin = NULL; /* pointer to dragon skin force */
2751     object *abil = NULL; /* pointer to dragon ability force */
2752     object *tmp = NULL; /* tmp. object */
2753    
2754     double chance; /* improvement-chance of one resistance type */
2755     double totalchance = 1; /* total chance of gaining one resistance */
2756     double bonus = 0; /* level bonus (improvement is easier at lowlevel) */
2757     double mbonus = 0; /* monster bonus */
2758     int atnr_winner[NROFATTACKS]; /* winning candidates for resistance improvement */
2759     int winners = 0; /* number of winners */
2760     int i; /* index */
2761    
2762     /* let's make sure and doublecheck the parameters */
2763     if (meal->type != FLESH || !op->is_dragon ())
2764     return 0;
2765    
2766     /* now grab the 'dragon_skin'- and 'dragon_ability'-forces
2767     from the player's inventory */
2768     for (tmp = op->inv; tmp; tmp = tmp->below)
2769     if (tmp->type == FORCE)
2770     if (tmp->arch->archname == shstr_dragon_skin_force)
2771     skin = tmp;
2772     else if (tmp->arch->archname == shstr_dragon_ability_force)
2773     abil = tmp;
2774    
2775     /* if either skin or ability are missing, this is an old player
2776     which is not to be considered a dragon -> bail out */
2777     if (skin == NULL || abil == NULL)
2778     return 0;
2779    
2780     /* now start by filling stomache and health, according to food-value */
2781     if ((MAX_FOOD - op->stats.food) < meal->stats.food)
2782     op->stats.hp += (MAX_FOOD - op->stats.food) / 50;
2783     else
2784     op->stats.hp += meal->stats.food / 50;
2785    
2786     min_it (op->stats.hp, op->stats.maxhp);
2787     op->stats.food = min (MAX_FOOD, op->stats.food + meal->stats.food);
2788    
2789     /*LOG(llevDebug, "-> player: %d, flesh: %d\n", op->level, meal->level); */
2790 elmex 1.1
2791 root 1.245 /* on to the interesting part: chances for adding resistance */
2792     for (i = 0; i < NROFATTACKS; i++)
2793 root 1.22 {
2794 root 1.245 if (meal->resist[i] > 0 && atnr_is_dragon_enabled (i))
2795 root 1.22 {
2796 root 1.245 /* got positive resistance, now calculate improvement chance (0-100) */
2797 elmex 1.1
2798 root 1.245 /* this bonus makes resistance increase easier at lower levels */
2799     bonus = (settings.max_level - op->level) * 30. / ((double) settings.max_level);
2800     if (i == abil->stats.exp)
2801     bonus += 5; /* additional bonus for resistance of ability-focus */
2802 root 1.8
2803 root 1.245 /* monster bonus increases with level, because high-level
2804     flesh is too rare */
2805     mbonus = op->level * 20. / ((double) settings.max_level);
2806 root 1.14
2807 root 1.245 chance = (((double)min (op->level + bonus, meal->level + bonus + mbonus)) * 100. /
2808     ((double)settings.max_level)) - skin->resist[i];
2809 root 1.57
2810 root 1.245 if (chance >= 0.)
2811     chance += 1.;
2812     else
2813     chance = (chance < -12) ? 0. : 1. / pow (2., -chance);
2814 root 1.14
2815 root 1.245 /* chance is proportional to amount of resistance (max. 50) */
2816     chance *= ((double)(min (meal->resist[i], 50))) / 50.;
2817 root 1.57
2818 root 1.245 /* doubled chance for resistance of ability-focus */
2819     if (i == abil->stats.exp)
2820     chance = min (100., chance * 2.);
2821 root 1.57
2822 root 1.245 /* now make the throw and save all winners (Don't insert luck bonus here!) */
2823     if (rndm (10000) < (unsigned int)(chance * 100))
2824     {
2825     atnr_winner[winners] = i;
2826     winners++;
2827     }
2828 root 1.85
2829 root 1.245 if (chance >= 0.01)
2830     totalchance *= 1 - chance / 100;
2831 root 1.14
2832 root 1.245 /*LOG(llevDebug, " %s: bonus %.1f, chance %.1f\n", attacks[i], bonus, chance); */
2833     }
2834     }
2835 root 1.122
2836 root 1.245 /* inverse totalchance as until now we have the failure-chance */
2837     totalchance = 100 - totalchance * 100;
2838 root 1.14
2839 root 1.245 /* print message according to totalchance */
2840     const char *buf;
2841     if (totalchance > 50.)
2842     buf = format ("Hmm! The %s tasted delicious!", &meal->name);
2843     else if (totalchance > 10.)
2844     buf = format ("The %s tasted very good.", &meal->name);
2845     else if (totalchance > 1.)
2846     buf = format ("The %s tasted good.", &meal->name);
2847     else if (totalchance > 0.1)
2848     buf = format ("The %s tasted bland.", &meal->name);
2849     else if (totalchance >= 0.01)
2850     buf = format ("The %s had a boring taste.", &meal->name);
2851     else if (meal->last_eat > 0 && atnr_is_dragon_enabled (meal->last_eat))
2852     buf = format ("The %s tasted strange.", &meal->name);
2853     else
2854     buf = format ("The %s had no taste.", &meal->name);
2855 root 1.14
2856 root 1.245 op->statusmsg (buf);
2857 root 1.14
2858 root 1.245 /* now choose a winner if we have any */
2859     i = -1;
2860     if (winners > 0)
2861     i = atnr_winner [rndm (winners)];
2862 root 1.14
2863 root 1.245 if (i >= 0 && i < NROFATTACKS && skin->resist[i] < 95)
2864     {
2865     /* resistance increased! */
2866     skin->resist[i]++;
2867     op->update_stats ();
2868 root 1.14
2869 root 1.245 op->statusmsg (format ("Your skin is now more resistant to %s!", change_resist_msg[i]));
2870     }
2871 root 1.14
2872 root 1.245 /* if this flesh contains a new ability focus, we mark it
2873     into the ability_force and it will take effect on next level */
2874     if (meal->last_eat > 0 && atnr_is_dragon_enabled (meal->last_eat) && meal->last_eat != abil->last_eat)
2875     {
2876     abil->last_eat = meal->last_eat; /* write: last_eat <new attnr focus> */
2877 elmex 1.184
2878 root 1.245 if (meal->last_eat != abil->stats.exp)
2879     op->statusmsg (format (
2880     "Your metabolism prepares to focus on %s!\n"
2881     "The change will happen at level %d.",
2882     change_resist_msg[meal->last_eat],
2883     abil->level + 1
2884     ));
2885     else
2886     {
2887     op->statusmsg (format ("Your metabolism will continue to focus on %s.", change_resist_msg[meal->last_eat]));
2888     abil->last_eat = 0;
2889     }
2890     }
2891 root 1.79
2892 root 1.245 return 1;
2893     }
2894 root 1.14
2895 root 1.245 /**
2896     * op eats food.
2897     * If player, takes care of messages and dragon special food.
2898     */
2899     static void
2900     apply_food (object *op, object *tmp)
2901     {
2902     int capacity_remaining;
2903 root 1.14
2904 root 1.245 if (op->type != PLAYER)
2905     op->stats.hp = op->stats.maxhp;
2906     else
2907     {
2908     /* check if this is a dragon (player), eating some flesh */
2909     if (tmp->type == FLESH && op->is_dragon () && dragon_eat_flesh (op, tmp))
2910     ;
2911     else
2912     {
2913     /* usual case - no dragon meal: */
2914     if (op->stats.food + tmp->stats.food > MAX_FOOD)
2915     {
2916     if (tmp->type == FOOD || tmp->type == FLESH)
2917     op->failmsg ("You feel full, but what a waste of food!");
2918     else
2919     op->statusmsg ("Most of the drink goes down your face not your throat!");
2920     }
2921 root 1.14
2922 root 1.245 tmp->play_sound (
2923     tmp->sound
2924     ? tmp->sound
2925     : tmp->type == DRINK
2926     ? sound_find ("eat_drink")
2927     : sound_find ("eat_food")
2928     );
2929 root 1.14
2930 root 1.245 if (!QUERY_FLAG (tmp, FLAG_CURSED))
2931     {
2932     const char *buf;
2933 root 1.14
2934 root 1.245 if (!op->is_dragon ())
2935     {
2936     /* eating message for normal players */
2937     if (tmp->type == DRINK)
2938     buf = format ("Ahhh...that %s tasted good.", &tmp->name);
2939     else
2940     buf = format ("The %s tasted %s", &tmp->name, tmp->type == FLESH ? "terrible!" : "good.");
2941     }
2942     else
2943     /* eating message for dragon players */
2944     buf = format ("The %s tasted terrible!", &tmp->name);
2945 root 1.14
2946 root 1.245 op->statusmsg (buf);
2947 root 1.22
2948 root 1.245 capacity_remaining = MAX_FOOD - op->stats.food;
2949     op->stats.food += tmp->stats.food;
2950     if (capacity_remaining < tmp->stats.food)
2951     op->stats.hp += capacity_remaining / 50;
2952     else
2953     op->stats.hp += tmp->stats.food / 50;
2954 root 1.14
2955 root 1.245 min_it (op->stats.hp, op->stats.maxhp);
2956     min_it (op->stats.food, MAX_FOOD);
2957     }
2958 elmex 1.1
2959 root 1.245 /* special food hack -b.t. */
2960     if (tmp->title || QUERY_FLAG (tmp, FLAG_CURSED))
2961     eat_special_food (op, tmp);
2962     }
2963     }
2964 root 1.14
2965 root 1.245 handle_apply_yield (tmp);
2966     tmp->decrease ();
2967 elmex 1.1 }
2968    
2969     /**
2970 root 1.245 * Handles applying an improve armor scroll.
2971     * Does some sanity checks, then calls improve_armour.
2972 elmex 1.1 */
2973 root 1.245 static void
2974     apply_armour_improver (object *op, object *tmp)
2975 elmex 1.1 {
2976 root 1.245 object *armor;
2977 root 1.22
2978 root 1.245 if (!QUERY_FLAG (op, FLAG_WIZCAST) && (get_map_flags (op->map, 0, op->x, op->y, 0, 0) & P_NO_MAGIC))
2979 root 1.22 {
2980 root 1.245 op->failmsg ("Something blocks the magic of the scroll. H<This area prevents magic effects.>");
2981     return;
2982     }
2983 root 1.68
2984 root 1.245 armor = find_marked_object (op);
2985    
2986     if (!armor)
2987     {
2988     op->failmsg ("You need to mark an armor object. Use the right mouse button popup or the mark command to do this.");
2989     return;
2990     }
2991 root 1.241
2992 root 1.245 if (armor->type != ARMOUR
2993     && armor->type != CLOAK
2994     && armor->type != BOOTS && armor->type != GLOVES && armor->type != BRACERS && armor->type != SHIELD && armor->type != HELMET)
2995     {
2996     op->failmsg ("Your marked item is not armour!\n");
2997     return;
2998 elmex 1.1 }
2999 root 1.245
3000     op->statusmsg ("Applying armour enchantment.");
3001     improve_armour (op, tmp, armor);
3002 elmex 1.1 }
3003    
3004 root 1.245 void
3005     apply_poison (object *op, object *tmp)
3006 elmex 1.1 {
3007 root 1.245 // need to do it now when it is still on the map
3008     handle_apply_yield (tmp);
3009 root 1.12
3010 root 1.245 object *poison = tmp->split (1);
3011 root 1.233
3012 root 1.245 if (op->type == PLAYER)
3013     {
3014     op->contr->play_sound (sound_find ("drink_poison"));
3015     op->failmsg ("Yech! That tasted poisonous!");
3016     op->contr->killer = poison;
3017     }
3018 root 1.76
3019 root 1.245 if (poison->stats.hp > 0)
3020 root 1.22 {
3021 root 1.245 LOG (llevDebug, "Trying to poison player/monster for %d hp\n", poison->stats.hp);
3022     hit_player (op, poison->stats.hp, tmp, AT_POISON, 1);
3023     }
3024    
3025     op->stats.food -= op->stats.food / 4;
3026     poison->destroy ();
3027     }
3028 root 1.233
3029 root 1.245 /**
3030     * This function will try to apply a lighter and in case no lighter
3031     * is specified it will try to find a lighter in the players inventory,
3032     * and inform him about this requirement.
3033     *
3034     * who - the player
3035     * op - the item we want to light
3036     * lighter - the lighter or 0 if a lighter has yet to be found
3037     */
3038     static object *
3039     auto_apply_lighter (object *who, object *op, object *lighter)
3040     {
3041     if (lighter == 0)
3042     {
3043     for (object *tmp = who->inv; tmp; tmp = tmp->below)
3044     {
3045     if (tmp->type == LIGHTER)
3046     {
3047     lighter = tmp;
3048     break;
3049     }
3050     }
3051 root 1.233
3052 root 1.245 if (!lighter)
3053     {
3054     who->failmsg (format (
3055     "You can't light up the %s with your bare hands! "
3056     "H<You need a lighter in your inventory, for example a flint and steel.>",
3057     &op->name));
3058     return 0;
3059     }
3060     }
3061 root 1.233
3062 root 1.245 // last_eat == 0 means the lighter is not being used up!
3063     if (lighter->last_eat && lighter->stats.food)
3064     {
3065     /* lighter gets used up */
3066     lighter = lighter->split ();
3067     lighter->stats.food--;
3068     who->insert (lighter);
3069     }
3070     else if (lighter->last_eat)
3071     {
3072     /* no charges left in lighter */
3073     who->failmsg (format (
3074     "You attempt to light the %s with a used up %s.",
3075     &op->name, &lighter->name));
3076     return 0;
3077     }
3078 root 1.233
3079 root 1.245 return lighter;
3080     }
3081 root 1.96
3082 root 1.245 /**
3083     * Designed primarily to light torches/lanterns/etc.
3084     * Also burns up burnable material too. First object in the inventory is
3085     * the selected object to "burn". -b.t.
3086     */
3087     static void
3088     apply_lighter (object *who, object *lighter)
3089     {
3090     object *item;
3091     int is_player_env = 0;
3092 root 1.232
3093 root 1.245 item = find_marked_object (who);
3094     if (item)
3095     {
3096     if (!auto_apply_lighter (who, item, lighter))
3097     return;
3098 root 1.76
3099 root 1.245 /* Perhaps we should split what we are trying to light on fire?
3100     * I can't see many times when you would want to light multiple
3101     * objects at once.
3102     */
3103 root 1.14
3104 root 1.245 save_throw_object (item, AT_FIRE, who);
3105 root 1.79
3106 root 1.245 if (item->destroyed ()
3107     || ((item->type == LAMP || item->type == TORCH)
3108     && item->glow_radius > 0))
3109     who->statusmsg (format (
3110     "You light the %s with the %s.",
3111     &item->name, &lighter->name));
3112     else
3113     who->failmsg (format (
3114     "You attempt to light the %s with the %s and fail.",
3115     &item->name, &lighter->name));
3116 elmex 1.1 }
3117 root 1.245 else
3118     who->failmsg ("You need to mark a lightable object.");
3119 elmex 1.1 }
3120    
3121     /**
3122 root 1.245 * This function generates a cursed effect for cursed lamps and torches.
3123 elmex 1.1 */
3124 root 1.245 static void
3125     player_apply_lamp_cursed_effect (object *who, object *op)
3126 elmex 1.1 {
3127 root 1.245 if (op->level)
3128     {
3129     who->failmsg (format (
3130     "The %s was cursed, it explodes in a big fireball!",
3131     &op->name));
3132     create_exploding_ball_at (who, op->level);
3133     }
3134     else
3135     {
3136     who->failmsg (format (
3137     "The %s was cursed, it crumbles to dust, at least it didn't explode.!",
3138     &op->name));
3139     }
3140 elmex 1.1
3141 root 1.245 op->destroy ();
3142 elmex 1.1 }
3143    
3144     /**
3145 root 1.245 * Apply for players and lamps
3146     *
3147     * who - the player
3148     * op - the lamp
3149 elmex 1.1 */
3150 root 1.245 static void
3151     player_apply_lamp (object *who, object *op)
3152     {
3153     bool switch_on = op->glow_radius ? false : true;
3154 root 1.114
3155 root 1.245 if (switch_on)
3156     {
3157     object *lighter = 0;
3158    
3159     if (op->flag [FLAG_IS_LIGHTABLE]
3160     && !(lighter = auto_apply_lighter (who, op, 0)))
3161     return;
3162    
3163     if (op->stats.food < 1)
3164     {
3165     if (op->type == LAMP)
3166     who->failmsg (format (
3167     "The %s is out of fuel! "
3168     "H<Lamps and similar items need fuel. They cannot be refilled.>",
3169     &op->name));
3170     else
3171     who->failmsg (format (
3172     "The %s is burnt out! "
3173     "H<Torches and similar items burn out and become worthless.>",
3174     &op->name));
3175     return;
3176     }
3177 root 1.114
3178 root 1.245 if (op->flag [FLAG_CURSED])
3179     {
3180     player_apply_lamp_cursed_effect (who, op);
3181     return;
3182     }
3183 elmex 1.1
3184 root 1.245 if (lighter)
3185     who->statusmsg (format (
3186     "You light up the %s with the %s.", &op->name, &lighter->name));
3187     else
3188     who->statusmsg (format ("You light up the %s.", &op->name));
3189     }
3190     else
3191 root 1.22 {
3192 root 1.245 if (op->flag [FLAG_CURSED])
3193 root 1.22 {
3194 root 1.245 player_apply_lamp_cursed_effect (who, op);
3195     return;
3196     }
3197 root 1.14
3198 root 1.245 if (op->type == TORCH)
3199     {
3200     if (!op->flag [FLAG_IS_LIGHTABLE])
3201 root 1.22 {
3202 root 1.245 who->statusmsg (format (
3203     "You put out the %s. "
3204     "H<The %s can't be used anymore, as it can't be lighted up again.>",
3205     &op->name, &op->name));
3206     }
3207     else
3208     who->statusmsg (format (
3209     "You put out the %s."
3210     "H<Torches wear out if you put them out.>",
3211     &op->name));
3212     }
3213     else
3214     who->statusmsg (format ("You turn off the %s.", &op->name));
3215     }
3216 root 1.81
3217 root 1.245 apply_lamp (op, switch_on);
3218     }
3219 root 1.81
3220 root 1.245 void get_animation_from_arch (object *op, arch_ptr a)
3221     {
3222     op->animation_id = a->animation_id;
3223     op->flag [FLAG_IS_TURNABLE] = a->flag [FLAG_IS_TURNABLE];
3224     op->flag [FLAG_ANIMATE] = a->flag [FLAG_ANIMATE];
3225     op->anim_speed = a->anim_speed;
3226     op->last_anim = 0;
3227     op->state = 0;
3228     op->face = a->face;
3229 root 1.81
3230 root 1.245 if (NUM_ANIMATIONS(op) > 1)
3231     {
3232     SET_ANIMATION(op, 0);
3233     animate_object (op, op->direction);
3234     }
3235     else
3236     update_object (op, UP_OBJ_FACE);
3237 elmex 1.1 }
3238    
3239     /**
3240 root 1.245 * Apply for LAMPs and TORCHes.
3241 elmex 1.1 *
3242 root 1.245 * op - the lamp
3243     * switch_on - a flag which says whether the lamp should be switched on or off
3244 elmex 1.1 */
3245 root 1.245 void apply_lamp (object *op, bool switch_on)
3246 elmex 1.1 {
3247 root 1.245 op->set_glow_radius (switch_on ? op->range : 0);
3248     op->set_speed (switch_on ? op->arch->speed : 0);
3249 root 1.22
3250 root 1.245 // torches wear out if you put them out
3251     if (op->type == TORCH && !switch_on)
3252 root 1.22 {
3253 root 1.245 if (op->flag [FLAG_IS_LIGHTABLE])
3254 root 1.22 {
3255 root 1.245 op->stats.food -= (double) op->arch->stats.food / 15;
3256     if (op->stats.food < 0)
3257     op->stats.food = 0;
3258     }
3259     else
3260     op->stats.food = 0;
3261     }
3262 elmex 1.1
3263 root 1.245 // lamps and torched get worthless when used up
3264     if (op->stats.food <= 0)
3265     op->value = 0;
3266 root 1.22
3267 root 1.245 // FIXME: This is a hack to make the more sane torches and lamps
3268     // still animated ;-/
3269     if (op->other_arch)
3270     get_animation_from_arch (op, switch_on ? op->other_arch : op->arch);
3271 root 1.96
3272 root 1.245 if (object *pl = op->visible_to ())
3273     esrv_update_item (UPD_ANIM | UPD_FACE | UPD_NAME, pl, op);
3274     }
3275 root 1.78
3276 root 1.245 /**
3277     * This handles items of type 'transformer'.
3278     * Basically those items, used with a marked item, transform both items into something
3279     * else.
3280     * "Transformer" item has food decreased by 1, removed if 0 (0 at start means illimited)..
3281     * Change information is contained in the 'slaying' field of the marked item.
3282     * The format is as follow: transformer:[number ]yield[;transformer:...].
3283     * This way an item can be transformed in many things, and/or many objects.
3284     * The 'slaying' field for transformer is used as verb for the action.
3285     */
3286     static void
3287     apply_item_transformer (object *pl, object *transformer)
3288     {
3289     object *marked;
3290     object *new_item;
3291     const char *find;
3292     char *separator;
3293     int yield;
3294     char got[MAX_BUF];
3295     int len;
3296 root 1.22
3297 root 1.245 if (!pl || !transformer)
3298     return;
3299 root 1.22
3300 root 1.245 marked = find_marked_object (pl);
3301 root 1.76
3302 root 1.245 if (!marked)
3303     {
3304     pl->failmsg (format ("Use the %s with what item?", query_name (transformer)));
3305     return;
3306     }
3307 root 1.22
3308 root 1.245 if (!marked->slaying)
3309 root 1.22 {
3310 root 1.245 pl->failmsg (format ("You can't use the %s with your %s!", query_name (transformer), query_name (marked)));
3311     return;
3312     }
3313 root 1.78
3314 root 1.245 /* check whether they are compatible or not */
3315     find = strstr (&marked->slaying, transformer->arch->archname);
3316     if (!find || (*(find + strlen (transformer->arch->archname)) != ':'))
3317     {
3318     pl->failmsg (format ("You can't use the %s with your %s!", query_name (transformer), query_name (marked)));
3319     return;
3320 elmex 1.1 }
3321 root 1.76
3322 root 1.245 find += strlen (transformer->arch->archname) + 1;
3323     /* Item can be used, now find how many and what it yields */
3324     if (isdigit (*(find)))
3325 root 1.243 {
3326 root 1.245 yield = atoi (find);
3327     if (yield < 1)
3328 root 1.243 {
3329 root 1.245 LOG (llevDebug, "apply_item_transformer: item %s has slaying-yield %d.", query_base_name (marked, 0), yield);
3330     yield = 1;
3331 root 1.243 }
3332     }
3333 root 1.245 else
3334     yield = 1;
3335 root 1.243
3336 root 1.245 while (isdigit (*find))
3337     find++;
3338 root 1.234
3339 root 1.245 while (*find == ' ')
3340     find++;
3341 root 1.233
3342 root 1.245 memset (got, 0, MAX_BUF);
3343 root 1.235
3344 root 1.245 if ((separator = (char *) strchr (find, ';')))
3345     len = separator - find;
3346     else
3347     len = strlen (find);
3348 root 1.234
3349 root 1.245 min_it (len, MAX_BUF - 1);
3350 root 1.235
3351 root 1.245 strcpy (got, find);
3352     got[len] = '\0';
3353 root 1.244
3354 root 1.245 /* Now create new item, remove used ones when required. */
3355     new_item = get_archetype (got);
3356     if (!new_item)
3357     {
3358     pl->failmsg (format ("This %s is strange, better to not use it.", query_base_name (marked, 0)));
3359     return;
3360 root 1.234 }
3361    
3362 root 1.245 new_item->nrof = yield;
3363 root 1.234
3364 root 1.245 pl->statusmsg (format ("You %s the %s.", &transformer->slaying, query_base_name (marked, 0)));
3365 root 1.233
3366 root 1.245 pl->insert (new_item);
3367     /* Eat up one item */
3368     marked->decrease ();
3369 root 1.233
3370 root 1.245 /* Eat one transformer if needed */
3371     if (transformer->stats.food)
3372     if (--transformer->stats.food == 0)
3373     transformer->decrease ();
3374 root 1.233 }
3375    
3376 elmex 1.1 /**
3377 root 1.245 * Main apply handler.
3378 elmex 1.1 *
3379 root 1.245 * Checks for unpaid items before applying.
3380 elmex 1.1 *
3381 root 1.245 * Return value is currently not used
3382 elmex 1.1 *
3383 root 1.245 * who is the object that is causing object to be applied, op is the object
3384     * being applied.
3385 elmex 1.1 *
3386 root 1.245 * aflag is special (always apply/unapply) flags. Nothing is done with
3387     * them in this function - they are passed to apply_special
3388 elmex 1.1 */
3389 root 1.245 static bool
3390     manual_apply (object *who, object *op, int aflag)
3391 elmex 1.1 {
3392 root 1.245 op = op->head_ ();
3393 elmex 1.1
3394 root 1.245 if (op->flag [FLAG_UNPAID] && !op->flag [FLAG_APPLIED])
3395 root 1.22 {
3396 root 1.245 if (who->contr)
3397     {
3398     examine (who, op);
3399     //who->failmsg ("You should pay for it first! H<You cannot use items marked as unpaid.>");//TODO remove
3400     return 1;
3401     }
3402     else
3403     return 0; /* monsters just skip unpaid items */
3404 elmex 1.1 }
3405    
3406 root 1.245 if (INVOKE_OBJECT (APPLY, op, ARG_OBJECT (who)))
3407     return RESULT_INT (0);
3408     else if (may_attempt_inv [op->type])
3409     {
3410     // special item, using slot system, needs to be in inv
3411     if (op->env == who)
3412     return apply_special (who, op, aflag);
3413 elmex 1.1
3414 root 1.245 who->failmsg (format ("You must get it first! H<You can only apply the %s if it is in your inventory.>\n", query_name (op)));
3415     }
3416     else if (!who->contr && may_attempt_player [op->type])
3417     return 0; // monsters shouldn't try to apply player-only stuff
3418     else if (may_attempt [op->type])
3419 root 1.22 {
3420 root 1.245 // ordinary stuff, may be on the floor
3421     switch (op->type)
3422     {
3423     case T_HANDLE:
3424     who->play_sound (sound_find ("turn_handle"));
3425     who->statusmsg ("You turn the handle.");
3426     op->value = op->value ? 0 : 1;
3427     SET_ANIMATION (op, op->value);
3428     update_object (op, UP_OBJ_FACE);
3429     push_button (op, who);
3430     break;
3431    
3432     case TRIGGER:
3433     if (check_trigger (op, who, who))
3434     {
3435     who->statusmsg ("You turn the handle.");
3436     who->play_sound (sound_find ("turn_handle"));
3437     }
3438     else
3439     who->failmsg ("The handle doesn't move.");
3440    
3441     break;
3442    
3443     case EXIT:
3444     if (!EXIT_PATH (op))
3445     who->failmsg (format ("The %s is closed. H<And will stay closed, until somebody fires up the map editor and adds it.>", query_name (op)));
3446     else
3447     {
3448     /* Don't display messages for random maps. */
3449     if (op->msg && EXIT_PATH (op) != shstr_random_map_exit)
3450     who->statusmsg (op->msg, NDI_NAVY);
3451    
3452     who->enter_exit (op);
3453     }
3454    
3455     break;
3456    
3457     case INSCRIBABLE:
3458     who->statusmsg (op->msg);
3459     // maybe show a spell menu to chose from or something like that
3460     break;
3461    
3462     case SIGN:
3463     apply_sign (who, op, 0);
3464     break;
3465    
3466     case BOOK:
3467     apply_book (who, op);
3468     break;
3469    
3470     case SKILLSCROLL:
3471     apply_skillscroll (who, op);
3472     break;
3473 root 1.22
3474 root 1.245 case SPELLBOOK:
3475     apply_spellbook (who, op);
3476     break;
3477 root 1.78
3478 root 1.245 case SCROLL:
3479     apply_scroll (who, op, 0);
3480     break;
3481 elmex 1.1
3482 root 1.245 case POTION:
3483     apply_potion (who, op);
3484     break;
3485 root 1.104
3486 root 1.245 /* Eneq(@csd.uu.se): Handle apply on containers. */
3487     //TODO: remove, as it is unsed?
3488     case CLOSE_CON:
3489     apply_container (who, op->env);
3490     break;
3491 root 1.78
3492 root 1.245 case CONTAINER:
3493     apply_container (who, op);
3494     break;
3495 root 1.55
3496 root 1.245 case TREASURE:
3497     apply_treasure (who, op);
3498     break;
3499 root 1.78
3500 root 1.245 case LAMP:
3501     case TORCH:
3502     player_apply_lamp (who, op);
3503     break;
3504 root 1.22
3505 root 1.245 case DRINK:
3506     case FOOD:
3507     case FLESH:
3508     apply_food (who, op);
3509     break;
3510 elmex 1.1
3511 root 1.245 case POISON:
3512     apply_poison (who, op);
3513     break;
3514 root 1.233
3515 root 1.245 case SAVEBED:
3516     break;
3517 root 1.22
3518 root 1.245 case ARMOUR_IMPROVER:
3519     apply_armour_improver (who, op);
3520     break;
3521 root 1.22
3522 root 1.245 case WEAPON_IMPROVER:
3523     check_improve_weapon (who, op);
3524     break;
3525 root 1.65
3526 root 1.245 case CLOCK:
3527     {
3528     char buf[MAX_BUF];
3529     timeofday_t tod;
3530 root 1.233
3531 root 1.245 get_tod (&tod);
3532     who->play_sound (sound_find ("sound_clock"));
3533     who->statusmsg (format (
3534     "It is %d minute%s past %d o'clock %s",
3535     tod.minute + 1, ((tod.minute + 1 < 2) ? "" : "s"),
3536     ((tod.hour % 14 == 0) ? 14 : ((tod.hour) % 14)), ((tod.hour >= 14) ? "pm" : "am")
3537     ));
3538     }
3539     break;
3540 root 1.78
3541 root 1.245 case MENU:
3542     shop_listing (op, who);
3543     break;
3544 root 1.14
3545 root 1.245 case POWER_CRYSTAL:
3546     apply_power_crystal (who, op); /* see egoitem.c */
3547     break;
3548 root 1.76
3549 root 1.245 case LIGHTER: /* for lighting torches/lanterns/etc */
3550     apply_lighter (who, op);
3551     break;
3552 root 1.96
3553 root 1.245 case ITEM_TRANSFORMER:
3554     apply_item_transformer (who, op);
3555     break;
3556     }
3557 root 1.238
3558 root 1.245 return 1;
3559     }
3560     else
3561     {
3562     who->statusmsg (format ("I don't know how to apply the %s.", query_name (op)));
3563     return 0;
3564     }
3565     }
3566 root 1.238
3567 root 1.245 /**
3568     * player_apply_below attempts to apply the object 'below' the player.
3569     * If the player has an open container, we use that for below, otherwise
3570     * we use the ground.
3571     */
3572     void
3573     player_apply_below (object *pl)
3574     {
3575     object *top = pl->container_ () ? pl->container_ ()->inv : pl->below;
3576 root 1.238
3577 root 1.245 /* If using a container, set the starting item to be the top
3578     * item in the container. Otherwise, use the map.
3579     */
3580 root 1.241
3581 root 1.245 // first try to apply "applyables"
3582     for (object *tmp = top; tmp; tmp = tmp->below)
3583     if (!tmp->invisible && may_attempt [tmp->type])
3584     {
3585     // If it is visible, player can apply it.
3586     pl->apply (tmp);
3587     return;
3588     }
3589 root 1.76
3590 root 1.245 while (top && top->invisible)
3591     top = top->below;
3592 root 1.76
3593 root 1.245 if (!top || top->flag [FLAG_IS_FLOOR])
3594     pl->failmsg ("You inspect this space, but find no way to apply anything. "
3595     "H<There is nothing here that you can apply.>");
3596     else
3597     // next, try to explain the topmost object
3598     switch (top->type)
3599     {
3600     // TODO: all this should move to examine
3601     case ALTAR:
3602     case IDENTIFY_ALTAR:
3603 root 1.247 case TRIGGER_ALTAR:
3604 root 1.245 case CONVERTER:
3605 root 1.247 //case TRIGGER_PEDESTAL:
3606 root 1.245 pl->failmsg (format (
3607     "You see no obvious mechanism on the %s."
3608     "H<You have to drop one or more specific items here.>",
3609     query_short_name (top)
3610     ));
3611     break;
3612 root 1.22
3613 root 1.245 case BUTTON:
3614 root 1.247 case TRIGGER_BUTTON:
3615 root 1.245 pl->failmsg (format (
3616     "The %s looks as if you could activate it with somehting heavy. "
3617     "H<You must put enough items here to activate it.>",
3618     query_short_name (top)
3619     ));
3620     break;
3621 root 1.233
3622 root 1.245 default:
3623     examine (pl, top);
3624     break;
3625     }
3626     }
3627 root 1.76
3628 root 1.245 // saner interface, returns successful status
3629     bool
3630     object::apply (object *ob, int aflags)
3631     {
3632     if (!ob) // simplifies a lot of callers
3633     return true;
3634 root 1.233
3635 root 1.245 if (contr)
3636     {
3637     if (!ob->env && (move_type & MOVE_FLYING))
3638     {
3639     /* player is flying and applying object not in inventory */
3640     if (!flag [FLAG_WIZ] && !(ob->move_type & MOVE_FLYING))
3641     {
3642     failmsg ("But you are floating high above the ground! "
3643     "H<You have to stop levitating first, if you can, either by using your levitation skill, "
3644     "or waiting till the levitation effect wears off.>");
3645     return 0;
3646     }
3647     }
3648 root 1.76
3649 root 1.245 contr->last_used = ob;
3650     }
3651 root 1.78
3652 root 1.245 bool want_apply =
3653     aflags & AP_APPLY ? true
3654     : aflags & AP_UNAPPLY ? false
3655     : !ob->flag [FLAG_APPLIED]; // AP_TOGGLE
3656 root 1.80
3657 root 1.245 object_ptr *slot = 0;
3658 root 1.22
3659 root 1.245 // detect the slot, if this is a player
3660     if (contr && !(aflags & AP_NO_SLOT))
3661     {
3662     object *oslot;
3663 root 1.76
3664 root 1.245 switch (ob->type)
3665     {
3666     case WEAPON:
3667     slot = &contr->combat_ob;
3668     oslot = contr->ranged_ob;
3669     break;
3670 root 1.233
3671 root 1.245 case BOW:
3672     case RANGED:
3673     case SPELL:
3674     case WAND:
3675     case ROD:
3676     case HORN:
3677     case BUILDER:
3678     slot = &contr->ranged_ob;
3679     oslot = contr->combat_ob;
3680     break;
3681 root 1.232
3682 root 1.245 // oh, the humanity
3683     case SKILL:
3684     if (aflags & AP_NO_SLOT)
3685     break;
3686 root 1.76
3687 root 1.245 if (skill_flags [ob->subtype] & SF_NEED_ITEM)
3688     break;
3689 elmex 1.1
3690 root 1.245 if (skill_flags [ob->subtype] & SF_COMBAT)
3691     {
3692     slot = &contr->combat_ob;
3693     oslot = contr->ranged_ob;
3694     }
3695     else if (skill_flags [ob->subtype] & SF_RANGED)
3696     {
3697     slot = &contr->ranged_ob;
3698     oslot = contr->combat_ob;
3699     }
3700 elmex 1.1
3701 root 1.245 break;
3702     }
3703 root 1.22
3704 root 1.245 // now handle slot exclusions
3705     if (slot)
3706     {
3707     // only one slot can be active
3708     if (want_apply)
3709     {
3710     // clear slot unless we are in it already
3711     if (*slot != ob)
3712     apply (*slot, AP_UNAPPLY);
3713 elmex 1.1
3714 root 1.245 // unapply other slot, because we want to become active
3715     apply (oslot, AP_UNAPPLY);
3716     }
3717 elmex 1.1
3718 root 1.245 // clear item from slot if applied
3719     if (!want_apply && current_weapon == ob)
3720     current_weapon = 0;
3721     }
3722     }
3723 elmex 1.1
3724 root 1.245 if (ob->flag [FLAG_APPLIED] != want_apply)
3725     manual_apply (this, ob, aflags);
3726 root 1.22
3727 root 1.245 if (ob->flag [FLAG_APPLIED] != want_apply)
3728     return false;
3729 root 1.76
3730 root 1.245 if (slot && want_apply)
3731     current_weapon = *slot = ob;
3732 root 1.76
3733 root 1.245 return true;
3734 elmex 1.1 }
3735    
3736     /**
3737     * Map was just loaded, handle op's initialisation.
3738     *
3739     * Generates shop floor's item, and treasures.
3740     */
3741 root 1.22 int
3742     auto_apply (object *op)
3743     {
3744     object *tmp = NULL, *tmp2;
3745     int i;
3746 elmex 1.1
3747 root 1.164 CLEAR_FLAG (op, FLAG_AUTO_APPLY);
3748 elmex 1.163
3749 root 1.22 switch (op->type)
3750     {
3751 root 1.24 case SHOP_FLOOR:
3752 root 1.42 if (!op->has_random_items ())
3753 root 1.24 return 0;
3754 root 1.38
3755 root 1.24 do
3756     {
3757     i = 10; /* let's give it 10 tries */
3758     while ((tmp = generate_treasure (op->randomitems,
3759 root 1.164 op->stats.exp
3760     ? (int) op->stats.exp
3761     : max (op->map->difficulty, 5)))
3762     == NULL && --i);
3763    
3764 root 1.24 if (tmp == NULL)
3765     return 0;
3766 root 1.164
3767 root 1.24 if (QUERY_FLAG (tmp, FLAG_CURSED) || QUERY_FLAG (tmp, FLAG_DAMNED))
3768     {
3769 root 1.172 tmp->destroy ();
3770 root 1.24 tmp = NULL;
3771     }
3772     }
3773     while (!tmp);
3774 root 1.38
3775 root 1.24 tmp->x = op->x;
3776     tmp->y = op->y;
3777     SET_FLAG (tmp, FLAG_UNPAID);
3778     insert_ob_in_map (tmp, op->map, NULL, 0);
3779     identify (tmp);
3780     break;
3781 root 1.14
3782 root 1.24 case TREASURE:
3783     if (QUERY_FLAG (op, FLAG_IS_A_TEMPLATE))
3784     return 0;
3785 root 1.37
3786 root 1.67 while (op->stats.hp-- > 0)
3787 root 1.24 create_treasure (op->randomitems, op, op->map ? GT_ENVIRONMENT : 0,
3788     op->stats.exp ? (int) op->stats.exp : op->map == NULL ? 14 : op->map->difficulty, 0);
3789    
3790     /* If we generated an object and put it in this object inventory,
3791     * move it to the parent object as the current object is about
3792     * to disappear. An example of this item is the random_* stuff
3793     * that is put inside other objects.
3794     */
3795 root 1.171 if (op->env)
3796     while (op->inv)
3797     op->env->insert (op->inv);
3798 root 1.37
3799 root 1.172 op->destroy ();
3800 root 1.24 break;
3801 elmex 1.1 }
3802 root 1.164
3803     return !!tmp;
3804 elmex 1.1 }
3805    
3806     /**
3807 root 1.68 * fix_auto_apply goes through the entire map every time a map
3808     * is loaded or swapped in and performs special actions for
3809 elmex 1.1 * certain objects (most initialization of chests and creation of
3810     * treasures and stuff). Calls auto_apply if appropriate.
3811     */
3812 root 1.20 void
3813 root 1.55 maptile::fix_auto_apply ()
3814 root 1.20 {
3815 root 1.55 if (!spaces)
3816 root 1.20 return;
3817    
3818 root 1.55 for (mapspace *ms = spaces + size (); ms-- > spaces; )
3819     for (object *tmp = ms->bot; tmp; )
3820     {
3821     object *above = tmp->above;
3822 root 1.20
3823 root 1.55 if (tmp->inv)
3824     {
3825     object *invtmp, *invnext;
3826 elmex 1.1
3827 root 1.164 for (invtmp = tmp->inv; invtmp; invtmp = invnext)
3828 root 1.55 {
3829     invnext = invtmp->below;
3830 root 1.14
3831 root 1.55 if (QUERY_FLAG (invtmp, FLAG_AUTO_APPLY))
3832     auto_apply (invtmp);
3833     else if (invtmp->type == TREASURE && invtmp->has_random_items ())
3834     {
3835 root 1.164 while (invtmp->stats.hp-- > 0)
3836 root 1.55 create_treasure (invtmp->randomitems, invtmp, 0, difficulty, 0);
3837    
3838     invtmp->randomitems = NULL;
3839     }
3840     else if (invtmp && invtmp->arch
3841     && invtmp->type != TREASURE && invtmp->type != SPELL && invtmp->type != CLASS && invtmp->has_random_items ())
3842     {
3843     create_treasure (invtmp->randomitems, invtmp, 0, difficulty, 0);
3844     /* Need to clear this so that we never try to create
3845     * treasure again for this object
3846     */
3847     invtmp->randomitems = NULL;
3848     }
3849     }
3850 root 1.214
3851 root 1.55 /* This is really temporary - the code at the bottom will
3852     * also set randomitems to null. The problem is there are bunches
3853     * of maps/players already out there with items that have spells
3854     * which haven't had the randomitems set to null yet.
3855     * MSW 2004-05-13
3856     *
3857     * And if it's a spellbook, it's better to set randomitems to NULL too,
3858     * else you get two spells in the book ^_-
3859     * Ryo 2004-08-16
3860     */
3861     if (tmp->type == WAND || tmp->type == ROD || tmp->type == SCROLL
3862     || tmp->type == HORN || tmp->type == FIREWALL || tmp->type == POTION || tmp->type == ALTAR || tmp->type == SPELLBOOK)
3863     tmp->randomitems = NULL;
3864     }
3865 root 1.20
3866 root 1.55 if (QUERY_FLAG (tmp, FLAG_AUTO_APPLY))
3867     auto_apply (tmp);
3868     else if ((tmp->type == TREASURE || (tmp->type == CONTAINER)) && tmp->has_random_items ())
3869     {
3870     while ((tmp->stats.hp--) > 0)
3871     create_treasure (tmp->randomitems, tmp, 0, difficulty, 0);
3872     tmp->randomitems = NULL;
3873     }
3874     else if (tmp->type == TIMED_GATE)
3875     {
3876     object *head = tmp->head != NULL ? tmp->head : tmp;
3877    
3878     if (QUERY_FLAG (head, FLAG_IS_LINKED))
3879     tmp->set_speed (0);
3880     }
3881     /* This function can be called everytime a map is loaded, even when
3882     * swapping back in. As such, we don't want to create the treasure
3883     * over and ove again, so after we generate the treasure, blank out
3884     * randomitems so if it is swapped in again, it won't make anything.
3885     * This is a problem for the above objects, because they have counters
3886     * which say how many times to make the treasure.
3887     */
3888     else if (tmp && tmp->arch && tmp->type != PLAYER
3889     && tmp->type != TREASURE && tmp->type != SPELL
3890     && tmp->type != PLAYER_CHANGER && tmp->type != CLASS && tmp->has_random_items ())
3891     {
3892     create_treasure (tmp->randomitems, tmp, GT_APPLY, difficulty, 0);
3893     tmp->randomitems = NULL;
3894     }
3895 root 1.78
3896 root 1.68 // close all containers
3897     else if (tmp->type == CONTAINER)
3898     tmp->flag [FLAG_APPLIED] = 0;
3899 root 1.22
3900 root 1.55 tmp = above;
3901     }
3902 elmex 1.1
3903 root 1.55 for (mapspace *ms = spaces + size (); ms-- > spaces; )
3904     for (object *tmp = ms->bot; tmp; tmp = tmp->above)
3905     if (tmp->above && (tmp->type == TRIGGER_BUTTON || tmp->type == TRIGGER_PEDESTAL))
3906 root 1.200 check_trigger (tmp, tmp->above, tmp->above);
3907 elmex 1.1 }
3908    
3909     /**
3910     * Handles player eating food that temporarily changes status (resistances, stats).
3911     * This used to call cast_change_attr(), but
3912     * that doesn't work with the new spell code. Since we know what
3913     * the food changes, just grab a force and use that instead.
3914     */
3915 root 1.22 void
3916     eat_special_food (object *who, object *food)
3917     {
3918     object *force;
3919     int i, did_one = 0;
3920    
3921 sf-marcmagus 1.199 char buf[64];
3922     snprintf (buf, sizeof(buf), "sp_food_%s", &food->title);
3923     shstr key (buf);
3924 root 1.22
3925 sf-marcmagus 1.199 /* bigger morsel of food = longer effect time */
3926     int duration = TIME2TICK (food->stats.food);
3927 elmex 1.1
3928 sf-marcmagus 1.199 if (force = who->force_find (key))
3929 root 1.22 {
3930 root 1.215 if (duration > fabs (force->speed_left / force->speed))
3931 root 1.22 {
3932 sf-marcmagus 1.199 new_draw_info_format (NDI_UNIQUE, 0, who, "More magical force spreads through you. H<The effect will last for about %.10g more seconds.>", TICK2TIME (duration));
3933     force->force_set_timer (duration);
3934 root 1.22 }
3935 sf-marcmagus 1.199 else
3936     new_draw_info (NDI_UNIQUE, 0, who, "It had no additional effect.");
3937    
3938     return;
3939 root 1.22 }
3940 sf-marcmagus 1.199 else
3941     {
3942     force = who->force_add (key, duration);
3943     force->name = key;
3944 elmex 1.47
3945 sf-marcmagus 1.199 /* check if the food affects a stat */
3946     for (i = 0; i < NUM_STATS; i++)
3947     if (sint8 k = food->stats.stat (i))
3948     {
3949     force->stats.stat (i) = k;
3950     did_one = 1;
3951     }
3952    
3953     /* check if we can protect the eater */
3954     for (i = 0; i < NROFATTACKS; i++)
3955     {
3956     if (food->resist[i] > 0)
3957     {
3958     force->resist[i] = food->resist[i];
3959     did_one = 1;
3960     }
3961     }
3962    
3963     if (did_one)
3964     {
3965     new_draw_info_format (NDI_UNIQUE, 0, who, "You are suffused with magical force. H<The effect will last for about %.10g seconds.>", TICK2TIME (duration));
3966    
3967     /* make the force take effect and report effects to user */
3968     change_abil (who, force);
3969     }
3970     else
3971     force->destroy ();
3972 root 1.22 }
3973    
3974     /* check for hp, sp change */
3975     if (food->stats.hp != 0)
3976     {
3977     if (QUERY_FLAG (food, FLAG_CURSED))
3978     {
3979 root 1.151 who->contr->killer = food;
3980 root 1.22 hit_player (who, food->stats.hp, food, AT_POISON, 1);
3981 root 1.125 who->failmsg ("Eck!...that was poisonous!");
3982 root 1.22 }
3983     else
3984     {
3985     if (food->stats.hp > 0)
3986 root 1.125 who->statusmsg ("You begin to feel better.");
3987 root 1.22 else
3988 root 1.125 who->failmsg ("Eck!...that was poisonous!");
3989    
3990 root 1.22 who->stats.hp += food->stats.hp;
3991     }
3992     }
3993 root 1.151
3994 root 1.22 if (food->stats.sp != 0)
3995     {
3996     if (QUERY_FLAG (food, FLAG_CURSED))
3997     {
3998 root 1.125 who->failmsg ("You are drained of mana!");
3999 root 1.22 who->stats.sp -= food->stats.sp;
4000     if (who->stats.sp < 0)
4001     who->stats.sp = 0;
4002     }
4003     else
4004     {
4005 root 1.125 who->statusmsg ("You feel a rush of magical energy!");
4006 root 1.22 who->stats.sp += food->stats.sp;
4007     /* place limit on max sp from food? */
4008     }
4009 elmex 1.1 }
4010 root 1.125
4011 root 1.51 who->update_stats ();
4012 elmex 1.1 }
4013    
4014 root 1.22 void
4015     apply_changes_to_player (object *pl, object *change)
4016     {
4017     int excess_stat = 0; /* if the stat goes over the maximum
4018     for the race, put the excess stat some
4019     where else. */
4020 elmex 1.1
4021 root 1.22 switch (change->type)
4022     {
4023 root 1.24 case CLASS:
4024     {
4025     living *stats = &(pl->contr->orig_stats);
4026     living *ns = &(change->stats);
4027     object *walk;
4028     int flag_change_face = 1;
4029    
4030     /* the following code assigns stats up to the stat max
4031     * for the race, and if the stat max is exceeded,
4032     * tries to randomly reassign the excess stat
4033     */
4034     int i, j;
4035 root 1.22
4036 root 1.24 for (i = 0; i < NUM_STATS; i++)
4037     {
4038 root 1.109 int race_bonus = pl->arch->stats.stat (i);
4039 root 1.91 sint8 stat = stats->stat (i) + ns->stat (i);
4040 root 1.22
4041 root 1.24 if (stat > 20 + race_bonus)
4042     {
4043     excess_stat++;
4044     stat = 20 + race_bonus;
4045     }
4046 root 1.91
4047     stats->stat (i) = stat;
4048 root 1.24 }
4049 elmex 1.1
4050 root 1.24 for (j = 0; excess_stat > 0 && j < 100; j++)
4051     { /* try 100 times to assign excess stats */
4052     int i = rndm (0, 6);
4053    
4054     if (i == CHA)
4055     continue; /* exclude cha from this */
4056 root 1.91
4057     int stat = stats->stat (i);
4058 root 1.109 int race_bonus = pl->arch->stats.stat (i);
4059 root 1.24 if (stat < 20 + race_bonus)
4060     {
4061     change_attr_value (stats, i, 1);
4062     excess_stat--;
4063     }
4064     }
4065 root 1.14
4066 root 1.24 /* insert the randomitems from the change's treasurelist into
4067     * the player ref: player.c
4068     */
4069 root 1.182 if (change->randomitems)
4070 root 1.24 give_initial_items (pl, change->randomitems);
4071 root 1.14
4072 root 1.24 /* set up the face, for some races. */
4073 root 1.14
4074 root 1.24 /* first, look for the force object banning
4075     * changing the face. Certain races never change face with class.
4076     */
4077 root 1.182 for (walk = pl->inv; walk; walk = walk->below)
4078     if (walk->name == shstr_NOCLASSFACECHANGE)
4079 root 1.24 flag_change_face = 0;
4080 root 1.14
4081 root 1.24 if (flag_change_face)
4082     {
4083 root 1.157 pl->face = change->face;
4084     pl->animation_id = change->animation_id;
4085     pl->flag [FLAG_ANIMATE] = change->flag [FLAG_ANIMATE];
4086 root 1.24 }
4087 root 1.14
4088 root 1.24 /* check the special case of can't use weapons */
4089     /*if(QUERY_FLAG(change,FLAG_USE_WEAPON)) CLEAR_FLAG(pl,FLAG_USE_WEAPON); */
4090 root 1.182 if (change->name == shstr_monk)
4091 root 1.24 CLEAR_FLAG (pl, FLAG_USE_WEAPON);
4092 root 1.14
4093 root 1.24 break;
4094     }
4095 elmex 1.1 }
4096     }
4097