Debian Codegraph @

server/spell_effect.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Various spell effects, non attacks.
17 * @todo use the same parameter names/orders.
18 */
19 #define _GNU_SOURCE // strcasestr() is a GNU extension in string.h
20
21 #include "global.h"
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "living.h"
29 #include "object.h"
30 #include "shop.h"
31 #include "sounds.h"
32 #include "spells.h"
33 #include "sproto.h"
34
35 /**
36 * This is really used mostly for spell fumbles and the like.
37 *
38 * @param op
39 * what is casting this.
40 * @param tmp
41 * object to propagate.
42 * @param lvl
43 * how nasty should the propagation be.
44 */
45 void cast_magic_storm(object *op, object *tmp, int lvl) {
46 if (!tmp)
47 return; /* error */
48 tmp->level = op->level;
49 tmp->range += lvl/5; /* increase the area of destruction */
50 tmp->duration += lvl/5;
51
52 /* Put a cap on duration for this - if the player fails in their
53 * apartment, don't want it to go on so long that it kills them
54 * multiple times. Also, damage already increases with level,
55 * so don't really need to increase the duration as much either.
56 */
57 if (tmp->duration >= 40)
58 tmp->duration = 40;
59 tmp->stats.dam = lvl; /* nasty recoils! */
60 tmp->stats.maxhp = tmp->count; /* tract single parent */
61 if (tmp->stats.maxhp == 0)
62 tmp->stats.maxhp = 1;
63 object_insert_in_map_at(tmp, op->map, op, 0, op->x, op->y);
64 }
65
66 /**
67 * Recharge wands.
68 *
69 * @param op
70 * who is casting.
71 * @param caster
72 * what is casting.
73 * @param spell_ob
74 * spell object.
75 * @retval 0
76 * nothing happened.
77 * @retval 1
78 * wand was recharged, or destroyed.
79 */
80 int recharge(object *op, object *caster, object *spell_ob) {
81 object *wand, *tmp;
82 int ncharges;
83 char name[MAX_BUF];
84
85 wand = find_marked_object(op);
86 if (wand == NULL || wand->type != WAND) {
87 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
88 "You need to mark the wand you want to recharge.");
89 return 0;
90 }
91 if (!(random_roll(0, 3, op, PREFER_HIGH))) {
92 query_name(wand, name, MAX_BUF);
93 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
94 "The %s vibrates violently, then explodes!",
95 name);
96 play_sound_map(SOUND_TYPE_ITEM, wand, 0, "explode");
97 object_remove(wand);
98 object_free(wand, 0);
99 tmp = create_archetype("fireball");
100 tmp->stats.dam = (spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob))/10;
101 if (!tmp->stats.dam)
102 tmp->stats.dam = 1;
103 tmp->stats.hp = tmp->stats.dam/2;
104 if (tmp->stats.hp < 2)
105 tmp->stats.hp = 2;
106 object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
107 return 1;
108 }
109
110 ncharges = (spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob));
111 if (wand->inv && wand->inv->level)
112 ncharges /= wand->inv->level;
113 else {
114 query_name(wand, name, MAX_BUF);
115 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
116 "Your %s is broken.",
117 name);
118 return 0;
119 }
120 if (!ncharges)
121 ncharges = 1;
122
123 wand->stats.food += ncharges;
124 query_name(wand, name, MAX_BUF);
125 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
126 "The %s glows with power.",
127 name);
128
129 if (wand->arch && QUERY_FLAG(&wand->arch->clone, FLAG_ANIMATE)) {
130 SET_FLAG(wand, FLAG_ANIMATE);
131 wand->speed = wand->arch->clone.speed;
132 object_update_speed(wand);
133 }
134 return 1;
135 }
136
137 /******************************************************************************
138 * Start of polymorph related functions.
139 *
140 * Changed around for 0.94.3 - it will now look through and use all the
141 * possible choices for objects/monsters (before it was the first 80 -
142 * arbitrary hardcoded limit in this file.) Doing this will be a bit
143 * slower however - while before, it traversed the archetypes once and
144 * stored them into an array, it will now potentially traverse it
145 * an average of 1.5 times. This is probably more costly on the polymorph
146 * item function, since it is possible a couple lookups might be needed before
147 * an item of proper value is generated.
148 */
149
150 /**
151 * Takes a living object (op) and turns it into
152 * another monster of some sort.
153 * @param op
154 * object to try to polymorph. Should be monster.
155 * @param level
156 * level of the polymorph spell.
157 */
158 static void polymorph_living(object *op, int level) {
159 archetype *at;
160 int x = op->x, y = op->y, numat = 0, choice, friendly;
161 mapstruct *map = op->map;
162 object *owner;
163
164 op = HEAD(op);
165
166 /* High level creatures are immune, as are creatures immune to magic. Otherwise,
167 * give the creature a saving throw.
168 */
169 if (op->level >= level*2
170 || did_make_save(op, op->level, op->resist[ATNR_MAGIC]/10)
171 || (op->resist[ATNR_MAGIC] == 100))
172 return;
173
174 object_remove(op);
175
176 /* First, count up the number of legal matches */
177 for (at = first_archetype; at != NULL; at = at->next)
178 if ((QUERY_FLAG((&at->clone), FLAG_MONSTER) == QUERY_FLAG(op, FLAG_MONSTER))
179 && (object_find_free_spot(&at->clone, map, x, y, 0, SIZEOFFREE) != -1)) {
180 numat++;
181 }
182 if (!numat) {
183 object_insert_in_map_at(op, map, NULL, 0, x, y);
184 return; /* no valid matches? if so, return */
185 }
186
187 /* Next make a choice, and loop through until we get to it */
188 choice = rndm(0, numat-1);
189 for (at = first_archetype; at != NULL; at = at->next)
190 if ((QUERY_FLAG((&at->clone), FLAG_MONSTER) == QUERY_FLAG(op, FLAG_MONSTER)) && (object_find_free_spot(&at->clone, map, x, y, 0, SIZEOFFREE) != -1)) {
191 if (!choice)
192 break;
193 else
194 choice--;
195 }
196
197 /* Look through the monster. Unapply anything they have applied,
198 * and remove any spells. Note that if this is extended
199 * to players, that would need to get fixed somehow.
200 */
201 FOR_INV_PREPARE(op, tmp) {
202 if (QUERY_FLAG(tmp, FLAG_APPLIED))
203 apply_manual(op, tmp, 0);
204 if (tmp->type == SPELL) {
205 object_remove(tmp);
206 object_free(tmp, 0);
207 }
208 } FOR_INV_FINISH();
209
210 /* Preserve some values for the new object */
211 owner = object_get_owner(op);
212 friendly = QUERY_FLAG(op, FLAG_FRIENDLY);
213 if (friendly)
214 remove_friendly_object(op);
215
216 object_copy(&(at->clone), op);
217 if (owner != NULL)
218 object_set_owner(op, owner);
219 if (friendly) {
220 SET_FLAG(op, FLAG_FRIENDLY);
221 op->attack_movement = PETMOVE;
222 add_friendly_object(op);
223 } else
224 CLEAR_FLAG(op, FLAG_FRIENDLY);
225
226 /* Put the new creature on the map */
227 if ((op = object_insert_in_map_at(op, map, owner, 0, x, y)) == NULL)
228 return;
229
230 if (HAS_RANDOM_ITEMS(op))
231 create_treasure(op->randomitems, op, GT_INVISIBLE, map->difficulty, 0);
232
233 /* Apply any objects. */
234 monster_check_apply_all(op);
235 }
236
237
238 /**
239 * Destroys item from polymorph failure
240 * @note
241 * We should probably do something
242 * more clever ala nethack - create an iron golem or
243 * something.
244 * @param who
245 * who cast the spell.
246 * @param op
247 * spell victim.
248 */
249 static void polymorph_melt(object *who, object *op) {
250 /* Not unique */
251 char name[MAX_BUF];
252
253 query_name(op, name, MAX_BUF);
254 if (op->nrof > 1)
255 draw_ext_info_format(NDI_GREY, 0, who, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
256 "The %s glow red, melt and evaporate!",
257 name);
258 else
259 draw_ext_info_format(NDI_GREY, 0, who, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
260 "The %s glows red, melts and evaporates!",
261 name);
262 play_sound_map(SOUND_TYPE_ITEM, op, 0, "evaporate");
263 object_remove(op);
264 object_free(op, 0);
265 return;
266 }
267
268 /**
269 * Changes an item to another item of similar type.
270 * @param who
271 * spell caster.
272 * @param op
273 * object being polymorphed. Should be a non living thing.
274 * @param level
275 * spell level, required for monster resistance.
276 */
277 static void polymorph_item(object *who, object *op, int level) {
278 archetype *at;
279 int max_value, difficulty, tries = 0, choice, charges = op->stats.food, numat = 0;
280 object *new_ob;
281 mapstruct *m;
282 int16_t x, y;
283
284 /* We try and limit the maximum value of the changed object. */
285 max_value = op->value*2;
286 if (max_value > 2000*(level/10))
287 max_value = 2000*(level/10)+(max_value-2000*(level/10))/3;
288
289 /* Look through and try to find matching items. Can't turn into something
290 * invisible. Also, if the value is too high now, it would almost
291 * certainly be too high below.
292 */
293 for (at = first_archetype; at != NULL; at = at->next) {
294 if (at->clone.type == op->type
295 && !at->clone.invisible
296 && at->clone.value > 0
297 && at->clone.value < max_value
298 && !QUERY_FLAG(&at->clone, FLAG_NO_DROP)
299 && !QUERY_FLAG(&at->clone, FLAG_STARTEQUIP))
300 numat++;
301 }
302
303 if (!numat)
304 return;
305
306 difficulty = op->magic*5;
307 if (difficulty < 0)
308 difficulty = 0;
309 new_ob = object_new();
310 do {
311 choice = rndm(0, numat-1);
312 for (at = first_archetype; at != NULL; at = at->next) {
313 if (at->clone.type == op->type
314 && !at->clone.invisible
315 && at->clone.value > 0
316 && at->clone.value < max_value
317 && !QUERY_FLAG(&at->clone, FLAG_NO_DROP)
318 && !QUERY_FLAG(&at->clone, FLAG_STARTEQUIP)) {
319 if (!choice)
320 break;
321 else
322 choice--;
323 }
324 }
325 object_copy(&(at->clone), new_ob);
326 fix_generated_item(new_ob, op, difficulty, FABS(op->magic), GT_ENVIRONMENT);
327 ++tries;
328 } while (new_ob->value > max_value && tries < 10);
329 if (new_ob->invisible) {
330 LOG(llevError, "polymorph_item: fix_generated_object made %s invisible?!\n", new_ob->name);
331 object_free(new_ob, FREE_OBJ_NO_DESTROY_CALLBACK);
332 return;
333 }
334
335 /* Unable to generate an acceptable item? Melt it */
336 if (tries == 10) {
337 polymorph_melt(who, op);
338 object_free(new_ob, FREE_OBJ_NO_DESTROY_CALLBACK);
339 return;
340 }
341
342 if (op->nrof && new_ob->nrof) {
343 new_ob->nrof = op->nrof;
344 /* decrease the number of items */
345 if (new_ob->nrof > 2)
346 new_ob->nrof -= rndm(0, op->nrof/2-1);
347 }
348
349 /* We don't want rings to keep sustenance/hungry status. There are probably
350 * other cases too that should be checked.
351 */
352 if (charges && op->type != RING && op->type != FOOD)
353 op->stats.food = charges;
354
355 x = op->x;
356 y = op->y;
357 m = op->map;
358 object_remove(op);
359 object_free(op, FREE_OBJ_NO_DESTROY_CALLBACK);
360 /*
361 * Don't want objects merged or re-arranged, as it then messes up the
362 * order
363 */
364 object_insert_in_map_at(new_ob, m, new_ob, INS_NO_MERGE|INS_NO_WALK_ON, x, y);
365 }
366
367 /**
368 * Handles polymorphing an object, living or not.
369 * Will avoid some specific items (flying arrows and such).
370 * @param op
371 * object being polymorphed.
372 * @param who
373 * spell caster.
374 * @param level
375 * spell level.
376 */
377 void polymorph(object *op, object *who, int level) {
378 int tmp;
379
380 /* Can't polymorph players right now */
381 /* polymorphing generators opens up all sorts of abuses */
382 if (op->type == PLAYER || QUERY_FLAG(op, FLAG_GENERATOR))
383 return;
384
385 if (QUERY_FLAG(op, FLAG_MONSTER)) {
386 polymorph_living(op, level);
387 return;
388 }
389 /* If it is a living object of some other type, don't handle
390 * it now.
391 */
392 if (QUERY_FLAG(op, FLAG_ALIVE))
393 return;
394
395 /* Don't want to morph flying arrows, etc... */
396 if (FABS(op->speed) > 0.001 && !QUERY_FLAG(op, FLAG_ANIMATE))
397 return;
398
399 /* Do some sanity checking here. type=0 is unknown, objects
400 * without archetypes are not good. As are a few other
401 * cases.
402 */
403 if (op->type == 0
404 || op->arch == NULL
405 || QUERY_FLAG(op, FLAG_NO_PICK)
406 || op->move_block
407 || op->type == TREASURE)
408 return;
409
410 tmp = rndm(0, 7);
411 if (tmp)
412 polymorph_item(who, op, level);
413 else
414 polymorph_melt(who, op);
415 }
416
417
418 /**
419 * Polymorph spell casting.
420 * @param op
421 * who is casting the spell.
422 * @param caster
423 * object used to cast spell.
424 * @param spell_ob
425 * spell itself.
426 * @param dir
427 * casting direction. 0 won't have any effect.
428 * @return
429 * Returns 0 on illegal cast, otherwise 1.
430 */
431 int cast_polymorph(object *op, object *caster, object *spell_ob, int dir) {
432 object *tmp;
433 int range, mflags, maxrange, level;
434 mapstruct *m;
435
436 if (dir == 0)
437 return 0;
438
439 maxrange = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
440 level = caster_level(caster, spell_ob);
441 for (range = 1; range < maxrange; range++) {
442 int16_t x = op->x+freearr_x[dir]*range, y = op->y+freearr_y[dir]*range;
443 object *image;
444
445 m = op->map;
446 mflags = get_map_flags(m, &m, x, y, &x, &y);
447
448 if (mflags&(P_NO_MAGIC|P_OUT_OF_MAP))
449 break;
450
451 if (GET_MAP_MOVE_BLOCK(m, x, y)&MOVE_FLY_LOW)
452 break;
453
454 /* Get the top most object */
455 for (tmp = GET_MAP_OB(m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above)
456 ;
457
458 /* Now start polymorphing the objects, top down */
459 FOR_OB_AND_BELOW_PREPARE(tmp) {
460 /* Once we find the floor, no need to go further */
461 if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
462 break;
463 polymorph(tmp, op, level);
464 } FOR_OB_AND_BELOW_FINISH();
465 image = arch_to_object(spell_ob->other_arch);
466 image->stats.food = 5;
467 image->speed_left = 0.1;
468 object_insert_in_map_at(image, m, op, 0, x, y);
469 }
470 return 1;
471 }
472
473 /**
474 * Create a missile (nonmagic - magic +4). Will either create bolts or arrows
475 * based on whether a crossbow or bow is equiped. If neither, it defaults to
476 * arrows.
477 * Sets the plus based on the casters level. It is also settable with the
478 * invoke command. If the caster attempts to create missiles with too
479 * great a plus, the default is used.
480 * The # of arrows created also goes up with level, so if a 30th level mage
481 * wants LOTS of arrows, and doesn't care what the plus is he could
482 * create nonnmagic arrows, or even -1, etc...
483 *
484 * @param op
485 * who is casting.
486 * @param caster
487 * what is casting.
488 * @param spell
489 * actual spell object.
490 * @param dir
491 * casting direction.
492 * @param stringarg
493 * optional parameter specifying what kind of items to create.
494 * @retval 0
495 * no missile created.
496 * @retval
497 * missiles were created.
498 */
499 int cast_create_missile(object *op, object *caster, object *spell, int dir, const char *stringarg) {
500 int missile_plus = 0, bonus_plus = 0;
501 const char *missile_name;
502 object *tmp, *missile;
503 tag_t tag;
504
505 tmp = object_find_by_type_applied(op, BOW);
506 missile_name = tmp != NULL ? tmp->race : "arrow";
507
508 missile_plus = spell->stats.dam+SP_level_dam_adjust(caster, spell);
509
510 if (!strcmp(missile_name, "arrows"))
511 missile_name = "arrow";
512 else if (!strcmp(missile_name, "crossbow bolts"))
513 missile_name = "bolt";
514
515 /* Check if the type (bolt/arrow) was specified with the spell */
516 if ( stringarg && strncmp(stringarg,"of ",3)==0 ) stringarg +=3;
517 if ( stringarg && strncmp(stringarg,"bolt",4)==0 ) {
518 missile_name = "bolt";
519 stringarg += 4;
520 while ( isalpha(*stringarg) ) ++stringarg;
521 while ( *stringarg==' ' ) ++stringarg;
522 }
523 if ( stringarg && strncmp(stringarg,"arrow",5)==0 ) {
524 missile_name = "arrow";
525 stringarg += 5;
526 while ( isalpha(*stringarg) ) ++stringarg;
527 while ( *stringarg==' ' ) ++stringarg;
528 }
529
530 if (find_archetype(missile_name) == NULL) {
531 LOG(llevDebug, "Cast create_missile: could not find archetype %s\n", missile_name);
532 return 0;
533 }
534 missile = create_archetype(missile_name);
535
536 if ( stringarg && strncmp(stringarg,"of ",3)==0 ) stringarg +=3;
537 if (stringarg) {
538 /* If it starts with a letter, presume it is a description */
539 if (isalpha(*stringarg)) {
540 artifact *al = find_artifactlist(missile->type)->items;
541
542 for (; al != NULL; al = al->next)
543 if (!strcasecmp(al->item->name, stringarg))
544 break;
545
546 if (!al) {
547 object_free(missile, FREE_OBJ_NO_DESTROY_CALLBACK);
548 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
549 "No such object %ss of %s",
550 missile_name, stringarg);
551 return 0;
552 }
553 if (al->item->slaying) {
554 object_free(missile, FREE_OBJ_NO_DESTROY_CALLBACK);
555 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
556 "You are not allowed to create %ss of %s",
557 missile_name, stringarg);
558 return 0;
559 }
560 give_artifact_abilities(missile, al->item);
561 /* These special arrows cost something extra. Don't have them also be
562 * magical - otherwise, in most cases, not enough will be created.
563 * I don't want to get into the parsing both plus and type.
564 */
565 bonus_plus = 1+(al->item->value/5);
566 missile_plus = 0;
567 } else if (atoi(stringarg) < missile_plus)
568 missile_plus = atoi(stringarg);
569 }
570 if (missile_plus > 4)
571 missile_plus = 4;
572 else if (missile_plus < -4)
573 missile_plus = -4;
574
575 missile->nrof = spell->duration+SP_level_duration_adjust(caster, spell);
576 if (missile->nrof <= (uint32_t)3 * (missile_plus + bonus_plus)) {
577 object_free(missile, FREE_OBJ_NO_DESTROY_CALLBACK);
578 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
579 "This item is too powerful for you to create!");
580 return 0;
581 }
582 missile->nrof -= 3*(missile_plus+bonus_plus);
583 if (missile->nrof < 1)
584 missile->nrof = 1;
585
586 missile->magic = missile_plus;
587 /* Can't get any money for these objects */
588 missile->value = 0;
589
590 SET_FLAG(missile, FLAG_IDENTIFIED);
591 tag = missile->count;
592
593 if (!cast_create_obj(op, missile, dir)
594 && op->type == PLAYER
595 && !object_was_destroyed(missile, tag)) {
596 pick_up(op, missile);
597 }
598 return 1;
599 }
600
601
602 /**
603 * Create food.
604 * Allows the choice of what sort of food object to make.
605 * If stringarg is NULL, it will create food dependent on level --PeterM
606 *
607 * @param op
608 * who is casting.
609 * @param caster
610 * what is casting.
611 * @param spell_ob
612 * actual spell object.
613 * @param dir
614 * casting direction.
615 * @param stringarg
616 * optional parameter specifying what kind of items to create.
617 * @retval 0
618 * no food created.
619 * @retval
620 * food was created.
621 */
622 int cast_create_food(object *op, object *caster, object *spell_ob, int dir, const char *stringarg) {
623 int food_value;
624 archetype *at = NULL;
625 object *new_op;
626
627 food_value = spell_ob->stats.food+50*SP_level_duration_adjust(caster, spell_ob);
628
629 if (stringarg) {
630 at = find_archetype_by_object_type_name(FOOD, stringarg);
631 if (at == NULL)
632 at = find_archetype_by_object_type_name(DRINK, stringarg);
633 if (at == NULL || at->clone.stats.food > food_value)
634 stringarg = NULL;
635 }
636
637 if (!stringarg) {
638 archetype *at_tmp;
639
640 /* We try to find the archetype with the maximum food value.
641 * This removes the dependency of hard coded food values in this
642 * function, and addition of new food types is automatically added.
643 * We don't use flesh types because the weight values of those need
644 * to be altered from the donor.
645 */
646
647 /* We assume the food items don't have multiple parts */
648 for (at_tmp = first_archetype; at_tmp != NULL; at_tmp = at_tmp->next) {
649 if (at_tmp->clone.type == FOOD || at_tmp->clone.type == DRINK) {
650 /* Basically, if the food value is something that is creatable
651 * under the limits of the spell and it is higher than
652 * the item we have now, take it instead.
653 */
654 if (at_tmp->clone.stats.food <= food_value
655 && (!at || at_tmp->clone.stats.food > at->clone.stats.food))
656 at = at_tmp;
657 }
658 }
659 }
660 /* Pretty unlikely (there are some very low food items), but you never
661 * know
662 */
663 if (!at) {
664 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
665 "You don't have enough experience to create any food.");
666 return 0;
667 }
668
669 food_value /= at->clone.stats.food;
670 new_op = object_new();
671 object_copy(&at->clone, new_op);
672 new_op->nrof = food_value;
673
674 new_op->value = 0;
675 if (new_op->nrof < 1)
676 new_op->nrof = 1;
677
678 cast_create_obj(op, new_op, dir);
679 return 1;
680 }
681
682 /**
683 * Try to get information about a living thing.
684 *
685 * @param op
686 * who is casting.
687 * @param caster
688 * what is casting.
689 * @param spell_ob
690 * spell object being cast.
691 * @param dir
692 * cast direction.
693 * @param level
694 * probe level.
695 * @retval 0
696 * nothing probed.
697 * @retval 1
698 * something was probed.
699 */
700 int probe(object *op, object *caster, object *spell_ob, int dir, int level) {
701 int r, mflags, maxrange;
702 mapstruct *m;
703
704 if (!dir) {
705 examine_monster(op, op, level);
706 return 1;
707 }
708 maxrange = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
709 for (r = 1; r < maxrange; r++) {
710 int16_t x = op->x+r*freearr_x[dir], y = op->y+r*freearr_y[dir];
711
712 m = op->map;
713 mflags = get_map_flags(m, &m, x, y, &x, &y);
714
715 if (mflags&P_OUT_OF_MAP)
716 break;
717
718 if (!QUERY_FLAG(op, FLAG_WIZCAST) && (mflags&P_NO_MAGIC)) {
719 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
720 "Something blocks your magic.");
721 return 0;
722 }
723 if (mflags&P_IS_ALIVE) {
724 FOR_MAP_PREPARE(m, x, y, tmp)
725 if (CAN_PROBE(tmp)) {
726 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
727 "You detect something.");
728 examine_monster(op, HEAD(tmp), level);
729 return 1;
730 }
731 FOR_MAP_FINISH();
732 }
733 }
734 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
735 "You detect nothing.");
736 return 1;
737 }
738
739
740 /**
741 * This checks to see if 'pl' is invisible to 'mon'.
742 * Does race check, undead check, etc
743 * Returns TRUE if mon can't see pl, false
744 * otherwise. This doesn't check range, walls, etc. It
745 * only checks the racial adjustments, and in fact that
746 * pl is invisible.
747 *
748 * @param pl
749 * potentially invisible object.
750 * @param mon
751 * who may see pl.
752 * @retval 0
753 * mon can see pl.
754 * @retval 1
755 * mon can't see pl.
756 */
757 int makes_invisible_to(object *pl, object *mon) {
758 if (!pl->invisible)
759 return 0;
760 if (pl->type == PLAYER) {
761 /* If race isn't set, then invisible unless it is undead */
762 if (!pl->contr->invis_race) {
763 if (QUERY_FLAG(mon, FLAG_UNDEAD))
764 return 0;
765 return 1;
766 }
767 /* invis_race is set if we get here */
768 if (!strcmp(pl->contr->invis_race, "undead") && is_true_undead(mon))
769 return 1;
770 /* No race, can't be invisible to it */
771 if (!mon->race)
772 return 0;
773 if (strstr(mon->race, pl->contr->invis_race))
774 return 1;
775 /* Nothing matched above, return 0 */
776 return 0;
777 } else {
778 /* monsters are invisible to everything */
779 return 1;
780 }
781 }
782
783 /**
784 * Makes the player or character invisible.
785 * Note the spells to 'stack', but perhaps in odd ways.
786 * the duration for all is cumulative.
787 * In terms of invis undead/normal invis, it is the last one cast that
788 * will determine if you are invisible to undead or normal monsters.
789 * For improved invis, if you cast it with a one of the others, you
790 * lose the improved part of it, and the above statement about undead/
791 * normal applies.
792 *
793 * @param op
794 * who is casting.
795 * @param caster
796 * what is casting.
797 * @param spell_ob
798 * actual spell object.
799 * @retval 0
800 * invisibility was already in action.
801 * @retval 1
802 * op is now invisible.
803 */
804 int cast_invisible(object *op, object *caster, object *spell_ob) {
805 object *tmp;
806
807 if (op->invisible > 1000) {
808 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
809 "You can not extend the duration of your invisibility any further");
810 return 0;
811 }
812
813 /* Remove the switch with 90% duplicate code - just handle the differences with
814 * and if statement or two.
815 */
816 op->invisible += spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
817 /* max duration */
818 if (op->invisible > 1000)
819 op->invisible = 1000;
820
821 if (op->type == PLAYER) {
822 if (op->contr->invis_race)
823 FREE_AND_CLEAR_STR(op->contr->invis_race);
824 if (spell_ob->race)
825 op->contr->invis_race = add_refcount(spell_ob->race);
826 if (QUERY_FLAG(spell_ob, FLAG_MAKE_INVIS))
827 op->contr->tmp_invis = 0;
828 else
829 op->contr->tmp_invis = 1;
830
831 op->contr->hidden = 0;
832 }
833 if (makes_invisible_to(op, op))
834 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
835 "You can't see your hands!");
836 else
837 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
838 "You feel more transparent!");
839
840 object_update(op, UP_OBJ_FACE);
841
842 /* Only search the active objects - only these should actually do
843 * harm to the player.
844 */
845 for (tmp = active_objects; tmp != NULL; tmp = tmp->active_next)
846 if (tmp->enemy == op)
847 object_set_enemy(tmp, NULL);
848 return 1;
849 }
850
851 /**
852 * Basically destroys earthwalls in the area.
853 *
854 * @param op
855 * who is casting.
856 * @param caster
857 * what is casting.
858 * @param spell_ob
859 * actual spell object.
860 * @retval 0
861 * op isn't a player.
862 * @retval 1
863 * op is a player.
864 */
865 int cast_earth_to_dust(object *op, object *caster, object *spell_ob) {
866 int range, i, j, mflags;
867 int16_t sx, sy;
868 mapstruct *m;
869
870 if (op->type != PLAYER)
871 return 0;
872
873 range = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
874
875 for (i = -range; i < range; i++)
876 for (j = -range; j < range; j++) {
877 sx = op->x+i;
878 sy = op->y+j;
879 m = op->map;
880 mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);
881
882 if (mflags&P_OUT_OF_MAP)
883 continue;
884
885 /* If the space doesn't block, no wall here to remove
886 * Don't care too much what it blocks - this allows for
887 * any sort of earthwall/airwall/waterwall, etc
888 * type effects.
889 */
890 if (GET_MAP_MOVE_BLOCK(m, sx, sy)) {
891 FOR_MAP_PREPARE(m, sx, sy, tmp)
892 if (tmp && QUERY_FLAG(tmp, FLAG_TEAR_DOWN))
893 hit_player(tmp, 9998, op, AT_PHYSICAL, 0);
894 FOR_MAP_FINISH();
895 }
896 }
897 return 1;
898 }
899
900 /**
901 * Word of recall causes the player to return 'home'.
902 * we put a force into the player object, so that there is a
903 * time delay effect.
904 *
905 * @param op
906 * who is casting.
907 * @param caster
908 * what is casting.
909 * @param spell_ob
910 * actual spell object.
911 * @retval 0
912 * op isn't a player.
913 * @retval 1
914 * word of recall initiated.
915 */
916 int cast_word_of_recall(object *op, object *caster, object *spell_ob) {
917 object *dummy;
918 int time;
919
920 if (op->type != PLAYER)
921 return 0;
922
923 if (object_find_by_type_subtype(op, SPELL_EFFECT, SP_WORD_OF_RECALL)) {
924 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
925 "You feel a force starting to build up inside you.");
926 return 1;
927 }
928
929 dummy = create_archetype(FORCE_NAME);
930 if (dummy == NULL) {
931 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
932 "Oops, program error!");
933 LOG(llevError, "cast_word_of_recall: create_archetype(force) failed!\n");
934 return 0;
935 }
936 time = spell_ob->duration-SP_level_duration_adjust(caster, spell_ob);
937 if (time < 1)
938 time = 1;
939
940 /* value of speed really doesn't make much difference, as long as it is
941 * positive. Lower value may be useful so that the problem doesn't
942 * do anything really odd if it say a -1000 or something.
943 */
944 dummy->speed = 0.002;
945 object_update_speed(dummy);
946 dummy->speed_left = -dummy->speed*time;
947 dummy->type = SPELL_EFFECT;
948 dummy->subtype = SP_WORD_OF_RECALL;
949
950 /* If we could take advantage of enter_player_savebed() here, it would be
951 * nice, but until the map load fails, we can't.
952 */
953 EXIT_PATH(dummy) = add_string(op->contr->savebed_map);
954 EXIT_X(dummy) = op->contr->bed_x;
955 EXIT_Y(dummy) = op->contr->bed_y;
956
957 (void)object_insert_in_ob(dummy, op);
958 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
959 "You feel a force starting to build up inside you.");
960 return 1;
961 }
962
963 /**
964 * wonder is really just a spell that will likely cast another
965 * spell.
966 *
967 * @param op
968 * who is casting.
969 * @param caster
970 * what is casting.
971 * @param dir
972 * casting direction.
973 * @param spell_ob
974 * actual spell object.
975 * @todo
976 * doesn't it decrease sp without checking?
977 */
978 int cast_wonder(object *op, object *caster, int dir, object *spell_ob) {
979 object *newspell;
980
981 if (!rndm(0, 3))
982 return cast_cone(op, caster, dir, spell_ob);
983
984 if (spell_ob->randomitems) {
985 newspell = generate_treasure(spell_ob->randomitems, caster->level);
986 if (!newspell) {
987 LOG(llevError, "cast_wonder: Unable to get a spell!\n");
988 return 0;
989 }
990 if (newspell->type != SPELL) {
991 LOG(llevError, "cast_wonder: spell returned is not a spell (%d, %s)!\n", newspell->type, newspell->name);
992 return 0;
993 }
994 /* Prevent inifinite recursion */
995 if (newspell->subtype == SP_WONDER) {
996 LOG(llevError, "cast_wonder: spell returned is another wonder spell!\n");
997 return 0;
998 }
999 return cast_spell(op, caster, dir, newspell, NULL);
1000 }
1001 return 1;
1002 }
1003
1004 /**
1005 * Living thing wants to know information.
1006 *
1007 * @param op
1008 * who wants information.
1009 * @return
1010 * 1.
1011 */
1012 int perceive_self(object *op) {
1013 char *cp, buf[MAX_BUF];
1014 archetype *at = find_archetype(ARCH_DEPLETION);
1015 if (at == NULL) {
1016 return 1;
1017 }
1018 object *tmp;
1019 const object *god;
1020 int i;
1021 StringBuffer *immunity;
1022
1023 god = find_god(determine_god(op));
1024 if (god)
1025 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1026 "You worship %s",
1027 god->name);
1028 else
1029 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1030 "You worship no god");
1031
1032 tmp = arch_present_in_ob(at, op);
1033
1034 cp = stringbuffer_finish(describe_item(op, op, 0, NULL));
1035
1036 if (*cp == '\0' && tmp == NULL)
1037 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1038 "You feel very mundane");
1039 else {
1040 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1041 "You have:");
1042 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1043 cp);
1044 if (tmp != NULL) {
1045 for (i = 0; i < NUM_STATS; i++) {
1046 if (get_attr_value(&tmp->stats, i) < 0) {
1047 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1048 "Your %s is depleted by %d",
1049 statname[i], -(get_attr_value(&tmp->stats, i)));
1050 }
1051 }
1052 }
1053 }
1054 free(cp);
1055
1056 if (op->glow_radius > 0)
1057 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1058 "You glow in the dark.");
1059
1060 immunity = NULL;
1061 for (tmp = op->inv; tmp; tmp = tmp->below) {
1062 if (tmp->type == SIGN) {
1063 if (immunity == NULL) {
1064 immunity = stringbuffer_new();
1065 stringbuffer_append_string(immunity, "You have been exposed to: ");
1066 } else {
1067 stringbuffer_append_string(immunity, ", ");
1068 }
1069 stringbuffer_append_string(immunity, tmp->name);
1070 if (tmp->level > 100)
1071 stringbuffer_append_string(immunity, " (full immunity)");
1072 else if (tmp->level > 70)
1073 stringbuffer_append_string(immunity, " (high immunity)");
1074 else if (tmp->level > 20)
1075 stringbuffer_append_string(immunity, " (partial immunity)");
1076 }
1077 }
1078
1079 if (immunity != NULL) {
1080 cp = stringbuffer_finish(immunity);
1081 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF, cp);
1082 free(cp);
1083 }
1084
1085 if (is_dragon_pl(op)) {
1086 /* now grab the 'dragon_ability'-force from the player's inventory */
1087 tmp = object_find_by_type_and_arch_name(op, FORCE, "dragon_ability_force");
1088 if (tmp != NULL) {
1089 StringBuffer *levels = NULL;
1090 int i;
1091
1092 if (tmp->stats.exp == 0) {
1093 snprintf(buf, sizeof(buf), "Your metabolism isn't focused on anything.");
1094 } else {
1095 snprintf(buf, sizeof(buf), "Your metabolism is focused on %s.", change_resist_msg[tmp->stats.exp]);
1096 }
1097 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
1098 buf);
1099
1100 for (i = 0; i < NROFATTACKS; i++) {
1101 if (atnr_is_dragon_enabled(i) && tmp->resist[i] > 0) {
1102 if (levels == NULL) {
1103 levels = stringbuffer_new();
1104 stringbuffer_append_string(levels, "Ability levels:\n");
1105 }
1106 stringbuffer_append_printf(levels, "- %s: %d\n", change_resist_msg[i], tmp->resist[i]);
1107 }
1108 }
1109
1110 if (levels != NULL) {
1111 cp = stringbuffer_finish(levels);
1112 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF, cp);
1113 free(cp);
1114 }
1115 }
1116 }
1117 return 1;
1118 }
1119
1120 static object* town_portal_find_force(object* op, object* spell) {
1121 object *dummy, *force;
1122
1123 /* The first thing to do is to check if we have a marked destination
1124 * dummy is used to make a check inventory for the force
1125 */
1126 dummy = arch_to_object(spell->other_arch);
1127 if (dummy == NULL) {
1128 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1129 "Oops, program error!");
1130 LOG(llevError, "object_new failed (force in cast_create_town_portal for %s!\n", op->name);
1131 return NULL;
1132 }
1133
1134 force = check_inv_recursive(op, dummy);
1135 if (force == NULL) {
1136 /* Here we know there is no destination marked up.
1137 * We have 2 things to do:
1138 * 1. Mark the destination in the player inventory.
1139 * 2. Let the player know it worked.
1140 */
1141 free_string(dummy->name);
1142 dummy->name = add_string(op->map->path);
1143 EXIT_X(dummy) = op->x;
1144 EXIT_Y(dummy) = op->y;
1145 dummy->weapontype = op->map->last_reset_time;
1146 object_insert_in_ob(dummy, op);
1147 draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
1148 "You fix this place in your mind and feel that you "
1149 "can come here from anywhere.");
1150 } else {
1151 object_free(dummy, FREE_OBJ_NO_DESTROY_CALLBACK);
1152 }
1153 return force;
1154 }
1155
1156 /**
1157 * Remove op's existing town portals. Returns 0 on success.
1158 */
1159 static int town_portal_destroy_existing(object* op, object* spell) {
1160 object* dummy = create_archetype(spell->race);
1161 object* old_force;
1162 if (dummy == NULL) {
1163 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1164 "Oops, program error!");
1165 LOG(llevError, "object_new failed (force) in cast_create_town_portal for %s!\n", op->name);
1166 return 0;
1167 }
1168 archetype *perm_portal;
1169 perm_portal = find_archetype(spell->slaying);
1170 if (perm_portal == NULL) {
1171 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1172 "Oops, program error!");
1173 return 0;
1174 }
1175
1176 /* To kill a town portal, we go trough the player's inventory,
1177 * for each marked portal in player's inventory,
1178 * -We try load the associated map (if impossible, consider the portal destructed)
1179 * -We find any portal in the specified location.
1180 * If it has the good name, we destruct it.
1181 * -We destruct the force indicating that portal.
1182 */
1183 while ((old_force = check_inv_recursive(op, dummy))) {
1184 int16_t exitx = EXIT_X(old_force);
1185 int16_t exity = EXIT_Y(old_force);
1186 LOG(llevDebug, "Trying to kill a portal in %s (%d,%d)\n", old_force->race, exitx, exity);
1187
1188 mapstruct *exitmap;
1189 if (!strncmp(old_force->race, settings.localdir, strlen(settings.localdir)))
1190 exitmap = ready_map_name(old_force->race, MAP_PLAYER_UNIQUE);
1191 else
1192 exitmap = ready_map_name(old_force->race, 0);
1193
1194 if (exitmap) {
1195 object* tmp = map_find_by_archetype(exitmap, exitx, exity, perm_portal);
1196 FOR_OB_AND_ABOVE_PREPARE(tmp)
1197 if (tmp->name == old_force->name) {
1198 object_remove(tmp);
1199 object_free(tmp, 0);
1200 break;
1201 }
1202 FOR_OB_AND_ABOVE_FINISH();
1203
1204 /* kill any opening animation there is */
1205 archetype *arch = find_archetype("town_portal_open");
1206 if (arch != NULL) {
1207 tmp = map_find_by_archetype(exitmap, exitx, exity, arch);
1208 FOR_OB_AND_ABOVE_PREPARE(tmp)
1209 if (tmp->name == old_force->name) {
1210 object_remove(tmp);
1211 object_free(tmp, FREE_OBJ_FREE_INVENTORY);
1212 break;
1213 }
1214 FOR_OB_AND_ABOVE_FINISH();
1215 }
1216 }
1217 object_remove(old_force);
1218 object_free(old_force, 0);
1219 }
1220 object_free(dummy, FREE_OBJ_NO_DESTROY_CALLBACK);
1221 return 0;
1222 }
1223
1224 /**
1225 * This function cast the spell of town portal for op.
1226 *
1227 * The spell operates in two passes. During the first one a place
1228 * is marked as a destination for the portal. During the second one,
1229 * 2 portals are created, one in the position the player cast it and
1230 * one in the destination place. The portal are synchronized and 2 forces
1231 * are inserted in the player to destruct the portal next time player
1232 * creates a new portal pair.
1233 * This spell has a side effect that it allows people to meet each other
1234 * in a permanent, private, apartments by making a town portal from it
1235 * to the town or another public place. So, check if the map is unique and if
1236 * so return an error
1237 *
1238 * Code by Tchize ([email protected])
1239 *
1240 * @param op
1241 * who is casting.
1242 * @param caster
1243 * what is casting.
1244 * @param spell
1245 * actual spell object.
1246 * @param dir
1247 * casting direction.
1248 * @retval 0
1249 * spell was insuccessful for some reason.
1250 * @retval 1
1251 * spell worked.
1252 */
1253 int cast_create_town_portal(object *op, object *caster, object *spell, int dir) {
1254 object *dummy, *force, *tmp;
1255 char portal_name [1024], portal_message [1024];
1256 mapstruct *exitmap;
1257 int op_level, x, y;
1258
1259 /* Check to see if the map the player is currently on is a per player unique
1260 * map. This can be determined in that per player unique maps have the
1261 * full pathname listed. Ignore if settings.create_home_portals is true.
1262 */
1263 if (!settings.create_home_portals) {
1264 if (!strncmp(op->map->path, settings.localdir, strlen(settings.localdir))) {
1265 draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1266 "You can't cast that here.");
1267 return 0;
1268 }
1269 }
1270
1271 /* Check to see if the player is on a transport */
1272 if (op->contr && op->contr->transport) {
1273 draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1274 "You need to exit the transport to cast that.");
1275 return 0;
1276 }
1277
1278 force = town_portal_find_force(op, spell);
1279 if (force == NULL) {
1280 return 1;
1281 }
1282
1283 /* Here we know where the town portal should go to
1284 * We should kill any existing portal associated with the player.
1285 * Than we should create the 2 portals.
1286 * For each of them, we need:
1287 * - To create the portal with the name of the player+destination map
1288 * - set the owner of the town portal
1289 * - To mark the position of the portal in the player's inventory
1290 * for easier destruction.
1291 *
1292 * The mark works has follow:
1293 * slaying: Existing town portal
1294 * hp, sp : x & y of the associated portal
1295 * name : name of the portal
1296 * race : map the portal is in
1297 */
1298 if (town_portal_destroy_existing(op, spell) != 0) {
1299 return 1;
1300 }
1301
1302 /* Creating the portals.
1303 * The very first thing to do is to ensure
1304 * access to the destination map.
1305 * If we can't, don't fizzle. Simply warn player.
1306 * This ensure player pays his mana for the spell
1307 * because HE is responsible for forgetting.
1308 * 'force' is the destination of the town portal, which we got
1309 * from the players inventory above.
1310 */
1311
1312 /* Ensure exit map is loaded*/
1313 if (!strncmp(force->name, settings.localdir, strlen(settings.localdir)))
1314 exitmap = ready_map_name(force->name, MAP_PLAYER_UNIQUE);
1315 else
1316 exitmap = ready_map_name(force->name, 0);
1317
1318 /* If we were unable to load (ex. random map deleted), warn player*/
1319 if (exitmap == NULL) {
1320 draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1321 "Something strange happens. You can't remember where to go!?");
1322 object_remove(force);
1323 object_free(force, 0);
1324 cast_create_obj(op, create_archetype(ARCH_PORTAL_FAILED), 0);
1325 return 1;
1326 } else if (exitmap->last_reset_time != force->weapontype) {
1327 draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1328 "The spell effect has expired.");
1329 object_remove(force);
1330 object_free(force, 0);
1331 cast_create_obj(op, create_archetype(ARCH_PORTAL_FAILED), 0);
1332 return 1;
1333 }
1334
1335 op_level = caster_level(caster, spell);
1336 if (op_level < 15)
1337 snprintf(portal_message, 1024, "The air moves around you and a huge smell of ammonia surrounds you as you pass through %s's tiny portal. Pouah!", op->name);
1338 else if (op_level < 30)
1339 snprintf(portal_message, 1024, "%s's portal smells of ozone. You do a lot of movements and finally pass through the small hole in the air.", op->name);
1340 else if (op_level < 60)
1341 snprintf(portal_message, 1024, "A shining door opens in the air in front of you, showing you the path to another place.");
1342 else
1343 snprintf(portal_message, 1024, "As you walk through %s's portal, flowers come out from the ground around you. You feel awed.", op->name);
1344
1345 /* Create a portal in front of player
1346 * dummy contain the portal and
1347 * force contain the track to kill it later
1348 */
1349
1350 snprintf(portal_name, 1024, "%s's portal to %s", op->name, force->name);
1351 dummy = create_archetype(spell->slaying); /*The portal*/
1352 if (dummy == NULL) {
1353 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1354 "Oops, program error!");
1355 LOG(llevError, "object_new failed (perm_magic_portal) in cast_create_town_portal for %s!\n", op->name);
1356 return 0;
1357 }
1358 EXIT_PATH(dummy) = add_string(force->name);
1359 EXIT_X(dummy) = EXIT_X(force);
1360 EXIT_Y(dummy) = EXIT_Y(force);
1361 FREE_AND_COPY(dummy->name, portal_name);
1362 FREE_AND_COPY(dummy->name_pl, portal_name);
1363 object_set_msg(dummy, portal_message);
1364 dummy->race = add_string(op->name); /*Save the owner of the portal*/
1365
1366 /* create a nice animation */
1367 tmp = create_archetype("town_portal_open");
1368 FREE_AND_COPY(tmp->name, portal_name);
1369 FREE_AND_COPY(tmp->name_pl, portal_name);
1370 object_insert_in_ob(dummy, tmp);
1371 /* and put it on the floor, when it ends the portal will be on the ground */
1372 cast_create_obj(op, tmp, 0);
1373 x = tmp->x;
1374 y = tmp->y;
1375
1376 /* Now we need to to create a town portal marker inside the player
1377 * object, so on future castings, we can know that he has an active
1378 * town portal.
1379 */
1380 tmp = create_archetype(spell->race);
1381 if (tmp == NULL) {
1382 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1383 "Oops, program error!");
1384 LOG(llevError, "object_new failed (force) in cast_create_town_portal for %s!\n", op->name);
1385 return 0;
1386 }
1387 tmp->race = add_string(op->map->path);
1388 FREE_AND_COPY(tmp->name, portal_name);
1389 EXIT_X(tmp) = x;
1390 EXIT_Y(tmp) = y;
1391 object_insert_in_ob(tmp, op);
1392
1393 /* Create a portal in the destination map
1394 * dummy contain the portal and
1395 * force the track to kill it later
1396 * the 'force' variable still contains the 'reminder' of
1397 * where this portal goes to.
1398 */
1399 snprintf(portal_name, 1024, "%s's portal to %s", op->name, op->map->path);
1400 dummy = create_archetype(spell->slaying); /*The portal*/
1401 if (dummy == NULL) {
1402 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1403 "Oops, program error!");
1404 LOG(llevError, "object_new failed (perm_magic_portal) in cast_create_town_portal for %s!\n", op->name);
1405 return 0;
1406 }
1407 EXIT_PATH(dummy) = add_string(op->map->path);
1408 EXIT_X(dummy) = op->x;
1409 EXIT_Y(dummy) = op->y;
1410 FREE_AND_COPY(dummy->name, portal_name);
1411 FREE_AND_COPY(dummy->name_pl, portal_name);
1412 object_set_msg(dummy, portal_message);
1413 dummy->race = add_string(op->name); /*Save the owner of the portal*/
1414
1415 /* animation here too */
1416 tmp = create_archetype("town_portal_open");
1417 FREE_AND_COPY(tmp->name, portal_name);
1418 FREE_AND_COPY(tmp->name_pl, portal_name);
1419 object_insert_in_ob(dummy, tmp);
1420 /* and put it on the floor, when it ends the portal will be on the ground */
1421 object_insert_in_map_at(tmp, exitmap, op, 0, EXIT_X(force), EXIT_Y(force));
1422 x = tmp->x;
1423 y = tmp->y;
1424
1425 /* Now we create another town portal marker that
1426 * points back to the one we just made
1427 */
1428 tmp = create_archetype(spell->race);
1429 if (tmp == NULL) {
1430 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1431 "Oops, program error!");
1432 LOG(llevError, "object_new failed (force) in cast_create_town_portal for %s!\n", op->name);
1433 return 0;
1434 }
1435 tmp->race = add_string(force->name);
1436 FREE_AND_COPY(tmp->name, portal_name);
1437 EXIT_X(tmp) = x;
1438 EXIT_Y(tmp) = y;
1439 object_insert_in_ob(tmp, op);
1440
1441 /* Describe the player what happened
1442 */
1443 draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
1444 "You see air moving and showing you the way home.");
1445 object_remove(force); /* Delete the force inside the player*/
1446 object_free(force, 0);
1447 return 1;
1448 }
1449
1450
1451 /**
1452 * This creates magic walls. Really, it can create most any object,
1453 * within some reason.
1454 *
1455 * @param op
1456 * who is casting.
1457 * @param caster
1458 * what is casting.
1459 * @param dir
1460 * casting direction.
1461 * @param spell_ob
1462 * actual spell object.
1463 * @retval 0
1464 * spell failed.
1465 * @retval 1
1466 * spell was successful.
1467 */
1468 int magic_wall(object *op, object *caster, int dir, object *spell_ob) {
1469 object *tmp, *tmp2;
1470 int i, posblocked, negblocked, maxrange;
1471 int16_t x, y;
1472 mapstruct *m;
1473 const char *name;
1474 archetype *at;
1475
1476 if (!dir) {
1477 dir = op->facing;
1478 x = op->x;
1479 y = op->y;
1480 } else {
1481 x = op->x+freearr_x[dir];
1482 y = op->y+freearr_y[dir];
1483 }
1484 m = op->map;
1485
1486 if ((spell_ob->move_block || x != op->x || y != op->y)
1487 && (get_map_flags(m, &m, x, y, &x, &y)&(P_OUT_OF_MAP|P_IS_ALIVE)
1488 || ((spell_ob->move_block&GET_MAP_MOVE_BLOCK(m, x, y)) == spell_ob->move_block))) {
1489 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1490 "Something is in the way.");
1491 return 0;
1492 }
1493 if (spell_ob->other_arch) {
1494 tmp = arch_to_object(spell_ob->other_arch);
1495 } else if (spell_ob->race) {
1496 char buf1[MAX_BUF];
1497
1498 snprintf(buf1, sizeof(buf1), spell_ob->race, dir);
1499 at = find_archetype(buf1);
1500 if (!at) {
1501 LOG(llevError, "summon_wall: Unable to find archetype %s\n", buf1);
1502 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1503 "This spell is broken.");
1504 return 0;
1505 }
1506 tmp = arch_to_object(at);
1507 } else {
1508 LOG(llevError, "magic_wall: spell %s lacks other_arch\n", spell_ob->name);
1509 return 0;
1510 }
1511
1512 if (tmp->type == SPELL_EFFECT) {
1513 tmp->attacktype = spell_ob->attacktype;
1514 tmp->duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
1515 tmp->stats.dam = spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob);
1516 tmp->range = 0;
1517 } else if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
1518 tmp->stats.hp = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
1519 tmp->stats.maxhp = tmp->stats.hp;
1520 object_set_owner(tmp, op);
1521 set_spell_skill(op, caster, spell_ob, tmp);
1522 }
1523 if (QUERY_FLAG(spell_ob, FLAG_IS_USED_UP) || QUERY_FLAG(tmp, FLAG_IS_USED_UP)) {
1524 tmp->stats.food = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
1525 SET_FLAG(tmp, FLAG_IS_USED_UP);
1526 }
1527 if (QUERY_FLAG(spell_ob, FLAG_TEAR_DOWN)) {
1528 tmp->stats.hp = spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob);
1529 tmp->stats.maxhp = tmp->stats.hp;
1530 SET_FLAG(tmp, FLAG_TEAR_DOWN);
1531 SET_FLAG(tmp, FLAG_ALIVE);
1532 }
1533
1534 /* This can't really hurt - if the object doesn't kill anything,
1535 * these fields just won't be used.
1536 */
1537 object_set_owner(tmp, op);
1538 set_spell_skill(op, caster, spell_ob, tmp);
1539 tmp->level = caster_level(caster, spell_ob)/2;
1540
1541 name = tmp->name;
1542 if ((tmp = object_insert_in_map_at(tmp, m, op, 0, x, y)) == NULL) {
1543 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1544 "Something destroys your %s",
1545 name);
1546 return 0;
1547 }
1548 /* If this is a spellcasting wall, need to insert the spell object */
1549 if (tmp->other_arch && tmp->other_arch->clone.type == SPELL)
1550 object_insert_in_ob(arch_to_object(tmp->other_arch), tmp);
1551
1552 /* This code causes the wall to extend some distance in
1553 * each direction, or until an obstruction is encountered.
1554 * posblocked and negblocked help determine how far the
1555 * created wall can extend, it won't go extend through
1556 * blocked spaces.
1557 */
1558 maxrange = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
1559 posblocked = 0;
1560 negblocked = 0;
1561
1562 for (i = 1; i <= maxrange; i++) {
1563 int dir2;
1564
1565 dir2 = (dir < 4) ? (dir+2) : dir-2;
1566
1567 x = tmp->x+i*freearr_x[dir2];
1568 y = tmp->y+i*freearr_y[dir2];
1569 m = tmp->map;
1570
1571 if (!(get_map_flags(m, &m, x, y, &x, &y)&(P_OUT_OF_MAP|P_IS_ALIVE))
1572 && ((spell_ob->move_block&GET_MAP_MOVE_BLOCK(m, x, y)) != spell_ob->move_block)
1573 && !posblocked) {
1574 tmp2 = object_new();
1575 object_copy(tmp, tmp2);
1576 object_insert_in_map_at(tmp2, m, op, 0, x, y);
1577 /* If this is a spellcasting wall, need to insert the spell object */
1578 if (tmp2->other_arch && tmp2->other_arch->clone.type == SPELL)
1579 object_insert_in_ob(arch_to_object(tmp2->other_arch), tmp2);
1580 } else
1581 posblocked = 1;
1582
1583 x = tmp->x-i*freearr_x[dir2];
1584 y = tmp->y-i*freearr_y[dir2];
1585 m = tmp->map;
1586
1587 if (!(get_map_flags(m, &m, x, y, &x, &y)&(P_OUT_OF_MAP|P_IS_ALIVE))
1588 && ((spell_ob->move_block&GET_MAP_MOVE_BLOCK(m, x, y)) != spell_ob->move_block)
1589 && !negblocked) {
1590 tmp2 = object_new();
1591 object_copy(tmp, tmp2);
1592 object_insert_in_map_at(tmp2, m, op, 0, x, y);
1593 if (tmp2->other_arch && tmp2->other_arch->clone.type == SPELL)
1594 object_insert_in_ob(arch_to_object(tmp2->other_arch), tmp2);
1595 } else
1596 negblocked = 1;
1597 }
1598
1599 if (QUERY_FLAG(tmp, FLAG_BLOCKSVIEW))
1600 update_all_los(op->map, op->x, op->y);
1601
1602 return 1;
1603 }
1604
1605 /**
1606 * Teleport through some doors and space.
1607 *
1608 * @param op
1609 * who is casting.
1610 * @param caster
1611 * what is casting.
1612 * @param spob
1613 * actual spell object.
1614 * @param dir
1615 * casting direction.
1616 * @retval 0
1617 * spell failure.
1618 * @retval 1
1619 * spell was successful.
1620 */
1621 int dimension_door(object *op, object *caster, object *spob, int dir) {
1622 uint32_t dist, maxdist;
1623 int mflags;
1624 mapstruct *m;
1625 int16_t sx, sy;
1626
1627 if (op->type != PLAYER)
1628 return 0;
1629
1630 if (!dir) {
1631 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1632 "In what direction?");
1633 return 0;
1634 }
1635
1636 /* Given the new outdoor maps, can't let players dimension door for
1637 * ever, so put limits in.
1638 */
1639 maxdist = spob->range+SP_level_range_adjust(caster, spob);
1640
1641 if (op->contr->count) {
1642 if (op->contr->count > maxdist) {
1643 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1644 "You can't dimension door that far!");
1645 return 0;
1646 }
1647
1648 for (dist = 0; dist < op->contr->count; dist++) {
1649 mflags = get_map_flags(op->map, &m,
1650 op->x+freearr_x[dir]*(dist+1),
1651 op->y+freearr_y[dir]*(dist+1),
1652 &sx, &sy);
1653
1654 if (mflags&(P_NO_MAGIC|P_OUT_OF_MAP))
1655 break;
1656
1657 if ((mflags&P_BLOCKSVIEW)
1658 && OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy)))
1659 break;
1660 }
1661
1662 if (dist < op->contr->count) {
1663 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1664 "Something blocks the magic of the spell.");
1665 op->contr->count = 0;
1666 return 0;
1667 }
1668 op->contr->count = 0;
1669
1670 /* Remove code that puts player on random space on maps. IMO,
1671 * a lot of maps probably have areas the player should not get to,
1672 * but may not be marked as NO_MAGIC (as they may be bounded
1673 * by such squares). Also, there are probably treasure rooms and
1674 * lots of other maps that protect areas with no magic, but the
1675 * areas themselves don't contain no magic spaces.
1676 */
1677 /* This call here is really just to normalize the coordinates */
1678 mflags = get_map_flags(op->map, &m, op->x+freearr_x[dir]*dist, op->y+freearr_y[dir]*dist,
1679 &sx, &sy);
1680 if (mflags&P_IS_ALIVE || OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy))) {
1681 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1682 "You cast your spell, but nothing happens.");
1683 return 1; /* Maybe the penalty should be more severe... */
1684 }
1685 } else {
1686 /* Player didn't specify a distance, so lets see how far
1687 * we can move the player. Don't know why this stopped on
1688 * spaces that blocked the players view.
1689 */
1690
1691 for (dist = 0; dist < maxdist; dist++) {
1692 mflags = get_map_flags(op->map, &m,
1693 op->x+freearr_x[dir]*(dist+1),
1694 op->y+freearr_y[dir]*(dist+1),
1695 &sx, &sy);
1696
1697 if (mflags&(P_NO_MAGIC|P_OUT_OF_MAP))
1698 break;
1699
1700 if ((mflags&P_BLOCKSVIEW)
1701 && OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy)))
1702 break;
1703 }
1704
1705 /* If the destination is blocked, keep backing up until we
1706 * find a place for the player.
1707 */
1708 for (; dist > 0; dist--) {
1709 if (get_map_flags(op->map, &m, op->x+freearr_x[dir]*dist, op->y+freearr_y[dir]*dist, &sx, &sy)&(P_OUT_OF_MAP|P_IS_ALIVE))
1710 continue;
1711
1712 if (!OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy)))
1713 break;
1714 }
1715 if (!dist) {
1716 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1717 "Your spell failed!");
1718 return 0;
1719 }
1720 }
1721
1722 if (op->contr->transport && op->contr->transport->type == TRANSPORT) {
1723 ob_apply(op->contr->transport, op, 0);
1724 if (op->contr->transport) {
1725 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1726 "Your spell failed!");
1727 return 0;
1728 }
1729 }
1730
1731 /* Actually move the player now */
1732 object_remove(op);
1733 if ((op = object_insert_in_map_at(op, op->map, op, 0, op->x+freearr_x[dir]*dist, op->y+freearr_y[dir]*dist)) == NULL)
1734 return 1;
1735
1736 if (op->type == PLAYER) {
1737 map_newmap_cmd(&op->contr->socket);
1738 player_update_bg_music(op);
1739 }
1740 op->speed_left = -FABS(op->speed)*5; /* Freeze them for a short while */
1741 return 1;
1742 }
1743
1744
1745 /**
1746 * Heals something.
1747 * @param op
1748 * who is casting.
1749 * @param caster
1750 * what is casting.
1751 * @param spell
1752 * actual spell object.
1753 * @param dir
1754 * casting direction.
1755 * @todo check spurious cure_disease call (shouldn't the spell's level be sent?) and return check value (always 1).
1756 */
1757 int cast_heal(object *op, object *caster, object *spell, int dir) {
1758 object *target;
1759 archetype *at;
1760 object *poison;
1761 int heal = 0, success = 0;
1762
1763 target = find_target_for_friendly_spell(op, dir);
1764
1765 if (target == NULL)
1766 return 0;
1767
1768 /* Figure out how many hp this spell might cure.
1769 * could be zero if this spell heals effects, not damage.
1770 */
1771 heal = spell->stats.dam;
1772 if (spell->stats.hp)
1773 heal += random_roll(spell->stats.hp, 6, op, PREFER_HIGH)+spell->stats.hp;
1774
1775 if (heal) {
1776 if (target->stats.hp >= target->stats.maxhp) {
1777 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1778 "You are already fully healed.");
1779 } else {
1780 /* See how many points we actually heal. Instead of messages
1781 * based on type of spell, we instead do messages based
1782 * on amount of damage healed.
1783 */
1784 if (heal > (target->stats.maxhp-target->stats.hp))
1785 heal = target->stats.maxhp-target->stats.hp;
1786 target->stats.hp += heal;
1787
1788 if (target->stats.hp >= target->stats.maxhp) {
1789 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1790 "You feel just fine!");
1791 } else if (heal > 50) {
1792 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1793 "Your wounds close!");
1794 } else if (heal > 25) {
1795 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1796 "Your wounds mostly close.");
1797 } else if (heal > 10) {
1798 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1799 "Your wounds start to fade.");
1800 } else {
1801 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1802 "Your wounds start to close.");
1803 }
1804 success = 1;
1805 }
1806 }
1807 if (spell->attacktype&AT_DISEASE)
1808 if (cure_disease(target, op, caster && caster->type != PLAYER ? caster->skill : spell->skill))
1809 success = 1;
1810
1811 if (spell->attacktype&AT_POISON) {
1812 at = find_archetype("poisoning");
1813 if (at != NULL) {
1814 poison = arch_present_in_ob(at, target);
1815 if (poison) {
1816 success = 1;
1817 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1818 "Your body feels cleansed");
1819 poison->stats.food = 1;
1820 }
1821 }
1822 }
1823 if (spell->attacktype&AT_CONFUSION) {
1824 poison = object_present_in_ob_by_name(FORCE, "confusion", target);
1825 if (poison) {
1826 success = 1;
1827 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1828 "Your mind feels clearer");
1829 poison->duration = 1;
1830 }
1831 }
1832 if (spell->attacktype&AT_BLIND) {
1833 at = find_archetype("blindness");
1834 if (at != NULL) {
1835 poison = arch_present_in_ob(at, target);
1836 if (poison) {
1837 success = 1;
1838 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1839 "Your vision begins to return.");
1840 poison->stats.food = 1;
1841 }
1842 }
1843 }
1844 if (spell->last_sp && target->stats.sp < target->stats.maxsp) {
1845 target->stats.sp += spell->last_sp;
1846 if (target->stats.sp > target->stats.maxsp)
1847 target->stats.sp = target->stats.maxsp;
1848 success = 1;
1849 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1850 "Magical energies surge through your body!");
1851 }
1852 if (spell->last_grace && target->stats.grace < target->stats.maxgrace) {
1853 target->stats.grace += spell->last_grace;
1854 if (target->stats.grace > target->stats.maxgrace)
1855 target->stats.grace = target->stats.maxgrace;
1856 success = 1;
1857 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1858 "You feel redeemed with you god!");
1859 }
1860 if (spell->stats.food && target->stats.food < MAX_FOOD) {
1861 target->stats.food += spell->stats.food;
1862 if (target->stats.food > MAX_FOOD)
1863 target->stats.food = MAX_FOOD;
1864 success = 1;
1865 /* We could do something a bit better like the messages for healing above */
1866 draw_ext_info(NDI_UNIQUE, 0, target, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
1867 "You feel your belly fill with food");
1868 }
1869
1870 if (spell->other_arch != NULL && target->map != NULL) {
1871 object_insert_in_map_at(arch_to_object(spell->other_arch), target->map, NULL, INS_ON_TOP, target->x, target->y);
1872 }
1873
1874 return success;
1875 }
1876
1877
1878 /**
1879 * This is used for the spells that gain stats. There are no spells
1880 * right now that icnrease wis/int/pow on a temp basis, so no
1881 * good comments for those.
1882 */
1883 static const char *const no_gain_msgs[NUM_STATS] = {
1884 "You grow no stronger.",
1885 "You grow no more agile.",
1886 "You don't feel any healthier.",
1887 "no wis",
1888 "You are no easier to look at.",
1889 "no int",
1890 "no pow"
1891 };
1892
1893 /**
1894 * Cast some stat-improving spell.
1895 *
1896 * @param op
1897 * who is casting.
1898 * @param caster
1899 * what is casting.
1900 * @param spell_ob
1901 * actual spell object.
1902 * @param dir
1903 * casting direction.
1904 * @param silent
1905 * if non zero, don't say when the spell is already is effect.
1906 * @retval 0
1907 * spell failed.
1908 * @retval 1
1909 * spell was successful.
1910 * @todo weird check on duration? since you'll never get there since a force would have been found?
1911 */
1912 int cast_change_ability(object *op, object *caster, object *spell_ob, int dir, int silent) {
1913 object *tmp;
1914 object *force = NULL;
1915 int i;
1916
1917 /* if dir = 99 op defaults to tmp, eat_special_food() requires this. */
1918 if (dir != 0) {
1919 tmp = find_target_for_friendly_spell(op, dir);
1920 } else {
1921 tmp = op;
1922 }
1923
1924 if (tmp == NULL)
1925 return 0;
1926
1927 /* If we've already got a force of this type, don't add a new one. */
1928 FOR_INV_PREPARE(tmp, tmp2)
1929 if (tmp2->type == FORCE && tmp2->subtype == FORCE_CHANGE_ABILITY) {
1930 if (tmp2->name == spell_ob->name) {
1931 force = tmp2; /* the old effect will be "refreshed" */
1932 break;
1933 } else if (spell_ob->race && spell_ob->race == tmp2->name) {
1934 if (!silent)
1935 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
1936 "You can not cast %s while %s is in effect",
1937 spell_ob->name, tmp2->name_pl);
1938 return 0;
1939 }
1940 }
1941 FOR_INV_FINISH();
1942 if (force == NULL) {
1943 force = create_archetype(FORCE_NAME);
1944 force->subtype = FORCE_CHANGE_ABILITY;
1945 free_string(force->name);
1946 if (spell_ob->race)
1947 force->name = add_refcount(spell_ob->race);
1948 else
1949 force->name = add_refcount(spell_ob->name);
1950 free_string(force->name_pl);
1951 force->name_pl = add_refcount(spell_ob->name);
1952 } else {
1953 int duration;
1954
1955 duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
1956 if (duration > force->duration) {
1957 force->duration = duration;
1958 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
1959 "You recast the spell while in effect.");
1960
1961 if (spell_ob->other_arch != NULL && tmp->map != NULL) {
1962 object_insert_in_map_at(arch_to_object(spell_ob->other_arch), tmp->map, NULL, INS_ON_TOP, tmp->x, tmp->y);
1963 }
1964
1965 } else {
1966 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
1967 "Recasting the spell had no effect.");
1968 }
1969 return 1;
1970 }
1971 force->duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
1972 if (op->type == PLAYER)
1973 store_spell_expiry(force);
1974 force->speed = 1.0;
1975 force->speed_left = -1.0;
1976 SET_FLAG(force, FLAG_APPLIED);
1977
1978 /* Now start processing the effects. First, protections */
1979 for (i = 0; i < NROFATTACKS; i++) {
1980 if (spell_ob->resist[i]) {
1981 force->resist[i] = spell_ob->resist[i]+SP_level_dam_adjust(caster, spell_ob);
1982 if (force->resist[i] > 100)
1983 force->resist[i] = 100;
1984 }
1985 }
1986 if (spell_ob->stats.hp)
1987 force->stats.hp = spell_ob->stats.hp+SP_level_dam_adjust(caster, spell_ob);
1988
1989 if (tmp->type == PLAYER) {
1990 /* Stat adjustment spells */
1991 for (i = 0; i < NUM_STATS; i++) {
1992 int8_t stat = get_attr_value(&spell_ob->stats, i), k, sm;
1993
1994 if (stat) {
1995 sm = 0;
1996 for (k = 0; k < stat; k++)
1997 sm += rndm(1, 3);
1998
1999 if ((get_attr_value(&tmp->stats, i)+sm) > (15+5*stat)) {
2000 sm = (15+5*stat)-get_attr_value(&tmp->stats, i);
2001 if (sm < 0)
2002 sm = 0;
2003 }
2004 set_attr_value(&force->stats, i, sm);
2005 if (!sm)
2006 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2007 no_gain_msgs[i]);
2008 }
2009 }
2010 }
2011
2012 force->move_type = spell_ob->move_type;
2013
2014 if (QUERY_FLAG(spell_ob, FLAG_SEE_IN_DARK))
2015 SET_FLAG(force, FLAG_SEE_IN_DARK);
2016
2017 if (QUERY_FLAG(spell_ob, FLAG_XRAYS))
2018 SET_FLAG(force, FLAG_XRAYS);
2019
2020 /* Haste/bonus speed */
2021 if (spell_ob->stats.exp) {
2022 if (op->speed > 0.5)
2023 force->stats.exp = (float)spell_ob->stats.exp/(op->speed+0.5);
2024 else
2025 force->stats.exp = spell_ob->stats.exp;
2026 }
2027
2028 force->stats.wc = spell_ob->stats.wc;
2029 force->stats.ac = spell_ob->stats.ac;
2030 force->attacktype = spell_ob->attacktype;
2031
2032 SET_FLAG(tmp, FLAG_NO_FIX_PLAYER); /* we don't want object_insert_in_ob() to call fix_object. */
2033 object_insert_in_ob(force, tmp);
2034 CLEAR_FLAG(tmp, FLAG_NO_FIX_PLAYER);
2035 change_abil(tmp, force); /* Display any relevant messages, and call fix_object to update the player */
2036
2037 if (spell_ob->other_arch != NULL && tmp->map != NULL) {
2038 object_insert_in_map_at(arch_to_object(spell_ob->other_arch), tmp->map, NULL, INS_ON_TOP, tmp->x, tmp->y);
2039 }
2040
2041 return 1;
2042 }
2043
2044 /**
2045 * Improve statistics of some living object.
2046 *
2047 * @param op
2048 * who is casting.
2049 * @param caster
2050 * what is casting.
2051 * @param spell_ob
2052 * actual spell object.
2053 * @param dir
2054 * casting direction.
2055 * @retval 0
2056 * spell failed.
2057 * @retval 1
2058 * spell was successful.
2059 */
2060 int cast_bless(object *op, object *caster, object *spell_ob, int dir) {
2061 int i;
2062 const object *god = find_god(determine_god(op));
2063 object *force = NULL, *tmp;
2064
2065 /* if dir = 99 op defaults to tmp, eat_special_food() requires this. */
2066 if (dir != 0) {
2067 tmp = find_target_for_friendly_spell(op, dir);
2068 } else {
2069 tmp = op;
2070 }
2071
2072 /* If we've already got a force of this type, don't add a new one. */
2073 FOR_INV_PREPARE(tmp, tmp2)
2074 if (tmp2->type == FORCE && tmp2->subtype == FORCE_CHANGE_ABILITY) {
2075 if (tmp2->name == spell_ob->name) {
2076 force = tmp2; /* the old effect will be "refreshed" */
2077 break;
2078 } else if (spell_ob->race && spell_ob->race == tmp2->name) {
2079 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
2080 "You can not cast %s while %s is in effect",
2081 spell_ob->name, tmp2->name_pl);
2082 return 0;
2083 }
2084 }
2085 FOR_INV_FINISH();
2086 if (force == NULL) {
2087 force = create_archetype(FORCE_NAME);
2088 force->subtype = FORCE_CHANGE_ABILITY;
2089 free_string(force->name);
2090 if (spell_ob->race)
2091 force->name = add_refcount(spell_ob->race);
2092 else
2093 force->name = add_refcount(spell_ob->name);
2094 free_string(force->name_pl);
2095 force->name_pl = add_refcount(spell_ob->name);
2096 } else {
2097 int duration;
2098
2099 duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
2100 if (duration > force->duration) {
2101 force->duration = duration;
2102 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
2103 "You recast the spell while in effect.");
2104 } else {
2105 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2106 "Recasting the spell had no effect.");
2107 }
2108 return 0;
2109 }
2110 force->duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
2111 force->speed = 1.0;
2112 force->speed_left = -1.0;
2113
2114 if (!god) {
2115 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2116 "Your blessing seems empty.");
2117 } else {
2118 /* Only give out good benefits, and put a max on it */
2119 const int16_t resist_max = spell_ob->resist[ATNR_GODPOWER];
2120 for (i = 0; i < NROFATTACKS; i++) {
2121 if (god->resist[i] > 0) {
2122 force->resist[i] = MIN(god->resist[i], resist_max);
2123 }
2124 }
2125 // Also grant appropriate amount of godpower resistance.
2126 force->resist[ATNR_GODPOWER] = spell_ob->resist[ATNR_GODPOWER];
2127
2128 force->path_attuned |= god->path_attuned;
2129 if (spell_ob->attacktype) {
2130 force->attacktype |= god->attacktype|AT_PHYSICAL;
2131 if (god->slaying)
2132 force->slaying = add_string(god->slaying);
2133 }
2134 if (tmp != op) {
2135 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
2136 "You bless %s.",
2137 tmp->name);
2138 draw_ext_info_format(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2139 "%s blessed you.",
2140 op->name);
2141 } else {
2142 draw_ext_info_format(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2143 "You are blessed by %s!",
2144 god->name);
2145 }
2146 }
2147 force->stats.wc = spell_ob->stats.wc;
2148 force->stats.ac = spell_ob->stats.ac;
2149
2150 store_spell_expiry(force);
2151 object_insert_in_ob(force, tmp);
2152 SET_FLAG(force, FLAG_APPLIED);
2153 change_abil(tmp, force); /* To display any messages, will call fix_object() */
2154
2155 if (spell_ob->other_arch != NULL && tmp->map != NULL) {
2156 object_insert_in_map_at(arch_to_object(spell_ob->other_arch), tmp->map, NULL, INS_ON_TOP, tmp->x, tmp->y);
2157 }
2158 return 1;
2159 }
2160
2161
2162
2163 /*
2164 * Alchemy code by Mark Wedel
2165 *
2166 * This code adds a new spell, called alchemy. Alchemy will turn
2167 * objects to gold nuggets, the value of the gold nuggets being
2168 * from 5% to 40% of that of the item itself depending on casting level.
2169 * It uses the value of the object before charisma adjustments, because
2170 * the nuggets themselves will be will be adjusted by charisma when sold.
2171 *
2172 * Large nuggets are worth 25 gp each (base). You will always get
2173 * the maximum number of large nuggets you could get.
2174 * Small nuggets are worth 1 gp each (base). You will get from 0
2175 * to the max amount of small nuggets as you could get.
2176 *
2177 * For example, if an item is worth 110 gold, you will get
2178 * 4 large nuggets, and from 0-10 small nuggets.
2179 *
2180 * There is also a chance (1:30) that you will get nothing at all
2181 * for the object. There is also a maximum weight that will be
2182 * alchemied.
2183 */
2184
2185 #define SMALL_NUGGET "smallnugget"
2186 #define LARGE_NUGGET "largenugget"
2187
2188 /**
2189 * Compute how many nuggets an object is worth, and remove it.
2190 *
2191 * @param value_adj
2192 * how much to adjust the cost of obj.
2193 * @param obj
2194 * object to convert.
2195 * @param[out] small_nuggets
2196 * how many small nuggets obj gives.
2197 * @param[out] large_nuggets
2198 * how many large nuggets obj gives.
2199 * @param[out] weight
2200 * the weight of the object.
2201 */
2202 static void alchemy_object(float value_adj, object *obj, int *small_nuggets, int *large_nuggets, int *weight) {
2203 uint64_t value = price_base(obj);
2204 uint64_t small_value, large_value; /**< Value of nuggets. */
2205
2206 /* Multiply the value of the object by value_adj, which should range
2207 * from 0.05 to 0.40. Set value to 0 instead if unpaid.
2208 */
2209 if (QUERY_FLAG(obj, FLAG_UNPAID))
2210 value = 0;
2211 else
2212 value *= value_adj;
2213
2214 archetype *small_nugget_arch = find_archetype(SMALL_NUGGET);
2215 if (small_nugget_arch == NULL) {
2216 return;
2217 }
2218 small_value = price_base(&small_nugget_arch->clone);
2219 archetype *large_nugget_arch = find_archetype(LARGE_NUGGET);
2220 if (large_nugget_arch == NULL) {
2221 return;
2222 }
2223 large_value = price_base(&large_nugget_arch->clone);
2224
2225 /* Give half of what value_adj says when we alchemy money (This should
2226 * hopefully make it so that it isn't worth it to alchemy money, sell
2227 * the nuggets, alchemy the gold from that, etc.
2228 */
2229 if (value && (obj->type == MONEY || obj->type == GEM))
2230 value /= 2;
2231
2232 if ((obj->value > 0) && rndm(0, 29)) {
2233 int count;
2234
2235 assert(large_value != 0 && small_value != 0);
2236 count = value/large_value;
2237 *large_nuggets += count;
2238 value -= (uint64_t)count*large_value;
2239 count = value/small_value;
2240 *small_nuggets += count;
2241 }
2242
2243 /* Turn 25 small nuggets into 1 large nugget. If the value
2244 * of large nuggets is not evenly divisable by the small nugget
2245 * value, take off an extra small_nugget (Assuming small_nuggets!=0)
2246 */
2247 if (*small_nuggets*small_value >= large_value) {
2248 assert(small_value != 0);
2249 (*large_nuggets)++;
2250 *small_nuggets -= large_value/small_value;
2251 if (*small_nuggets && large_value%small_value)
2252 (*small_nuggets)--;
2253 }
2254
2255 if (weight != NULL) {
2256 *weight += obj->weight;
2257 }
2258
2259 object_remove(obj);
2260 object_free(obj, FREE_OBJ_NO_DESTROY_CALLBACK);
2261 }
2262
2263 /**
2264 * Place gold nuggets on the map.
2265 * @param op
2266 * player who is casting the spell. Just used so nuggets are inserted below her.
2267 * @param m
2268 * map to insert to.
2269 * @param small_nuggets
2270 * @param large_nuggets
2271 * how many nuggets to place.
2272 * @param x
2273 * @param y
2274 * where to place the nuggets.
2275 */
2276 static void place_alchemy_objects(object *op, mapstruct *m, int small_nuggets, int large_nuggets, int x, int y) {
2277 object *tmp;
2278 int flag = 0;
2279
2280 /* Put any nuggets below the player, but we can only pass this
2281 * flag if we are on the same space as the player
2282 */
2283 if (x == op->x && y == op->y && op->map == m)
2284 flag = INS_BELOW_ORIGINATOR;
2285
2286 if (small_nuggets) {
2287 tmp = create_archetype(SMALL_NUGGET);
2288 tmp-> nrof = small_nuggets;
2289 object_insert_in_map_at(tmp, m, op, flag, x, y);
2290 }
2291 if (large_nuggets) {
2292 tmp = create_archetype(LARGE_NUGGET);
2293 tmp-> nrof = large_nuggets;
2294 object_insert_in_map_at(tmp, m, op, flag, x, y);
2295 }
2296 }
2297
2298 /**
2299 * Change items to gold nuggets. Only works for players.
2300 *
2301 * @param op
2302 * who is casting.
2303 * @param caster
2304 * what is casting.
2305 * @param spell_ob
2306 * actual spell object.
2307 * @retval 0
2308 * op isn't a player.
2309 * @retval 1
2310 * op is a player.
2311 */
2312 int alchemy(object *op, object *caster, object *spell_ob) {
2313 int x, y, weight = 0, weight_max, large_nuggets, small_nuggets, mflags;
2314 int16_t nx, ny;
2315 float value_adj;
2316 mapstruct *mp;
2317
2318 if (op->type != PLAYER)
2319 return 0;
2320
2321 /* Put a maximum weight of items that can be alchemied. Limits the power
2322 * some, and also prevents people from alcheming every table/chair/clock
2323 * in sight
2324 */
2325 weight_max = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
2326 weight_max *= 1000;
2327
2328 /* Set value_adj to be a multiplier for how much of the original value
2329 * will be in the nuggets. Starts at 0.05, increasing by 0.01 per casting
2330 * level, maxing out at 0.40.
2331 */
2332 value_adj = (SP_level_dam_adjust(caster, spell_ob)/100.00)+0.05;
2333
2334 if (value_adj > 0.40)
2335 value_adj = 0.40;
2336
2337 for (y = op->y-1; y <= op->y+1; y++) {
2338 for (x = op->x-1; x <= op->x+1; x++) {
2339 nx = x;
2340 ny = y;
2341
2342 mp = op->map;
2343
2344 mflags = get_map_flags(mp, &mp, nx, ny, &nx, &ny);
2345
2346 if (mflags&(P_OUT_OF_MAP|P_NO_MAGIC))
2347 continue;
2348
2349 /* Treat alchemy a little differently - most spell effects
2350 * use fly as the movement type - for alchemy, consider it
2351 * ground level effect.
2352 */
2353 if (GET_MAP_MOVE_BLOCK(mp, nx, ny)&MOVE_WALK)
2354 continue;
2355
2356 small_nuggets = 0;
2357 large_nuggets = 0;
2358
2359 FOR_MAP_PREPARE(mp, nx, ny, tmp) {
2360 if (tmp->weight > 0 && !QUERY_FLAG(tmp, FLAG_NO_PICK)
2361 && !QUERY_FLAG(tmp, FLAG_ALIVE)
2362 && !QUERY_FLAG(tmp, FLAG_IS_CAULDRON)) {
2363 if (tmp->inv) {
2364 FOR_INV_PREPARE(tmp, tmp1)
2365 if (tmp1->weight > 0 && !QUERY_FLAG(tmp1, FLAG_NO_PICK)
2366 && !QUERY_FLAG(tmp1, FLAG_ALIVE)
2367 && !QUERY_FLAG(tmp1, FLAG_IS_CAULDRON))
2368 alchemy_object(value_adj, tmp1, &small_nuggets, &large_nuggets, &weight);
2369 FOR_INV_FINISH();
2370 }
2371 alchemy_object(value_adj, tmp, &small_nuggets, &large_nuggets, &weight);
2372
2373 if (weight > weight_max) {
2374 place_alchemy_objects(op, mp, small_nuggets, large_nuggets, nx, ny);
2375 return 1;
2376 }
2377 } /* is alchemable object */
2378 } FOR_MAP_FINISH(); /* process all objects on this space */
2379
2380 /* Insert all the nuggets at one time. This probably saves time, but
2381 * it also prevents us from alcheming nuggets that were just created
2382 * with this spell.
2383 */
2384 place_alchemy_objects(op, mp, small_nuggets, large_nuggets, nx, ny);
2385 }
2386 }
2387
2388 /* reset this so that if player standing on a big pile of stuff,
2389 * it is redrawn properly.
2390 */
2391 op->contr->socket.look_position = 0;
2392 return 1;
2393 }
2394
2395
2396 /**
2397 * This function removes the cursed/damned status on equipped
2398 * items.
2399 *
2400 * @param op
2401 * who is casting.
2402 * @param caster
2403 * what is casting.
2404 * @param spell
2405 * actual spell object.
2406 * @return
2407 * how many items were affected.
2408 * @todo why is the value set to 0?
2409 */
2410 int remove_curse(object *op, object *caster, object *spell) {
2411 int success = 0, was_one = 0;
2412
2413 FOR_INV_PREPARE(op, tmp)
2414 if (QUERY_FLAG(tmp, FLAG_APPLIED)
2415 && ((QUERY_FLAG(tmp, FLAG_CURSED) && QUERY_FLAG(spell, FLAG_CURSED))
2416 || (QUERY_FLAG(tmp, FLAG_DAMNED) && QUERY_FLAG(spell, FLAG_DAMNED)))) {
2417 was_one++;
2418 if (tmp->level <= caster_level(caster, spell)) {
2419 success++;
2420 if (QUERY_FLAG(spell, FLAG_DAMNED))
2421 CLEAR_FLAG(tmp, FLAG_DAMNED);
2422
2423 CLEAR_FLAG(tmp, FLAG_CURSED);
2424 CLEAR_FLAG(tmp, FLAG_KNOWN_CURSED);
2425 tmp->value = 0; /* Still can't sell it */
2426 if (op->type == PLAYER)
2427 esrv_update_item(UPD_FLAGS, op, tmp);
2428 }
2429 }
2430 FOR_INV_FINISH();
2431
2432 if (op->type == PLAYER) {
2433 if (success) {
2434 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
2435 "You feel like some of your items are looser now.");
2436 } else {
2437 if (was_one)
2438 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2439 "You failed to remove the curse.");
2440 else
2441 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2442 "You are not using any cursed items.");
2443 }
2444 }
2445 return success;
2446 }
2447
2448 /**
2449 * This alters player's marked item's cursed or blessed status, based on the spell_ob's fields.
2450 *
2451 * @param op
2452 * player casting the spell.
2453 * @param caster
2454 * what object was used to cast the spell.
2455 * @param spell_ob
2456 * spell itself.
2457 * @return
2458 * 1 if item was changed, 0 else.
2459 */
2460 int cast_item_curse_or_curse(object *op, object *caster, object *spell_ob) {
2461 object *marked = find_marked_object(op);
2462 char name[HUGE_BUF];
2463
2464 if (!marked) {
2465 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2466 "You need to mark an item first!");
2467 return 0;
2468 }
2469
2470 if ((QUERY_FLAG(marked, FLAG_CURSED) && QUERY_FLAG(spell_ob, FLAG_CURSED))
2471 || (QUERY_FLAG(marked, FLAG_BLESSED) && QUERY_FLAG(spell_ob, FLAG_BLESSED))) {
2472 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2473 "The spell has no effect");
2474 return 0;
2475 }
2476
2477 query_short_name(marked, name, HUGE_BUF);
2478 if (QUERY_FLAG(spell_ob, FLAG_CURSED)) {
2479 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
2480 "Your %s emits a dark light for a few seconds.", name);
2481 SET_FLAG(marked, FLAG_CURSED);
2482 CLEAR_FLAG(marked, FLAG_KNOWN_CURSED);
2483 CLEAR_FLAG(marked, FLAG_IDENTIFIED);
2484 esrv_update_item(UPD_FLAGS, op, marked);
2485 return 1;
2486 }
2487
2488 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
2489 "Your %s glows blue for a few seconds.", name);
2490 SET_FLAG(marked, FLAG_BLESSED);
2491 SET_FLAG(marked, FLAG_KNOWN_BLESSED);
2492 SET_FLAG(marked, FLAG_STARTEQUIP);
2493 esrv_update_item(UPD_FLAGS, op, marked);
2494 return 1;
2495 }
2496
2497 /**
2498 * Identifies objects in the players inventory/on the ground.
2499 *
2500 * @param op
2501 * who is casting.
2502 * @param caster
2503 * what is casting.
2504 * @param spell
2505 * actual spell object.
2506 * @retval 0
2507 * nothing was identified.
2508 * @retval 1
2509 * at least one object was identified.
2510 */
2511 int cast_identify(object *op, object *caster, object *spell) {
2512 int success = 0, num_ident;
2513 char desc[MAX_BUF];
2514
2515 num_ident = spell->stats.dam+SP_level_dam_adjust(caster, spell);
2516
2517 if (num_ident < 1)
2518 num_ident = 1;
2519
2520 FOR_INV_PREPARE(op, tmp)
2521 if (!tmp->invisible && !is_identified(tmp)) {
2522 tmp = identify(tmp);
2523 if (op->type == PLAYER) {
2524 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
2525 "You have %s.",
2526 ob_describe(tmp, op, 1, desc, sizeof(desc)));
2527 if (tmp->msg) {
2528 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
2529 "The item has a story:\n%s",
2530 tmp->msg);
2531 }
2532 }
2533 num_ident--;
2534 success = 1;
2535 if (!num_ident)
2536 break;
2537 }
2538 FOR_INV_FINISH();
2539 /* If all the power of the spell has been used up, don't go and identify
2540 * stuff on the floor. Only identify stuff on the floor if the spell
2541 * was not fully used.
2542 */
2543 if (num_ident) {
2544 FOR_MAP_PREPARE(op->map, op->x, op->y, tmp)
2545 if (!tmp->invisible && !is_identified(tmp)) {
2546 tmp = identify(tmp);
2547 if (op->type == PLAYER) {
2548 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
2549 "On the ground is %s.",
2550 ob_describe(tmp, op, 1, desc, sizeof(desc)));
2551 if (tmp->msg) {
2552 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
2553 "The item has a story:\n%s",
2554 tmp->msg);
2555 }
2556 esrv_update_item(UPD_FLAGS|UPD_NAME, op, tmp);
2557 }
2558 num_ident--;
2559 success = 1;
2560 if (!num_ident)
2561 break;
2562 }
2563 FOR_MAP_FINISH();
2564 }
2565 if (!success)
2566 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2567 "You can't reach anything unidentified.");
2568 else {
2569 spell_effect(spell, op->x, op->y, op->map, op);
2570 }
2571 return success;
2572 }
2573
2574 /**
2575 * Detect magic or invisible items.
2576 *
2577 * @param op
2578 * who is casting.
2579 * @param caster
2580 * what is casting.
2581 * @param spell
2582 * actual spell object.
2583 * @return
2584 * 1.
2585 */
2586 int cast_detection(object *op, object *caster, object *spell) {
2587 object *tmp, *last, *detect;
2588 const object *god;
2589 int done_one, range, mflags, floor, level;
2590 int16_t x, y, nx, ny;
2591 mapstruct *m;
2592
2593 /* We precompute some values here so that we don't have to keep
2594 * doing it over and over again.
2595 */
2596 god = find_god(determine_god(op));
2597 level = caster_level(caster, spell);
2598 range = spell->range+SP_level_range_adjust(caster, spell);
2599
2600 for (x = op->x-range; x <= op->x+range; x++)
2601 for (y = op->y-range; y <= op->y+range; y++) {
2602 m = op->map;
2603 mflags = get_map_flags(m, &m, x, y, &nx, &ny);
2604 if (mflags&P_OUT_OF_MAP)
2605 continue;
2606
2607 /* For most of the detections, we only detect objects above the
2608 * floor. But this is not true for show invisible.
2609 * Basically, we just go and find the top object and work
2610 * down - that is easier than working up.
2611 */
2612
2613 last = NULL;
2614 FOR_MAP_PREPARE(m, nx, ny, tmp)
2615 last = tmp;
2616 FOR_MAP_FINISH();
2617 /* Shouldn't happen, but if there are no objects on a space, this
2618 * would happen.
2619 */
2620 if (!last)
2621 continue;
2622
2623 done_one = 0;
2624 floor = 0;
2625 detect = NULL;
2626 tmp = last;
2627 FOR_OB_AND_BELOW_PREPARE(tmp) {
2628 /* show invisible */
2629 if (QUERY_FLAG(spell, FLAG_MAKE_INVIS)
2630 /* Might there be other objects that we can make visibile? */
2631 && (tmp->invisible && (QUERY_FLAG(tmp, FLAG_MONSTER) ||
2632 (tmp->type == PLAYER && !QUERY_FLAG(tmp, FLAG_WIZ)) ||
2633 tmp->type == CF_HANDLE ||
2634 tmp->type == TRAPDOOR || tmp->type == EXIT || tmp->type == HOLE ||
2635 tmp->type == BUTTON || tmp->type == TELEPORTER ||
2636 tmp->type == GATE || tmp->type == LOCKED_DOOR ||
2637 tmp->type == WEAPON || tmp->type == ALTAR || tmp->type == SIGN ||
2638 tmp->type == TRIGGER_PEDESTAL || tmp->type == SPECIAL_KEY ||
2639 tmp->type == TREASURE || tmp->type == BOOK ||
2640 tmp->type == HOLY_ALTAR))) {
2641 if (random_roll(0, level-1, op, PREFER_HIGH) > tmp->level) {
2642 tmp->invisible = 0;
2643 done_one = 1;
2644 }
2645 }
2646 if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
2647 floor = 1;
2648
2649 /* All detections below this point don't descend beneath the floor,
2650 * so just continue on. We could be clever and look at the type of
2651 * detection to completely break out if we don't care about objects beneath
2652 * the floor, but once we get to the floor, not likely a very big issue anyways.
2653 */
2654 if (floor)
2655 continue;
2656
2657 /* I had thought about making detect magic and detect curse
2658 * show the flash the magic item like it does for detect monster.
2659 * however, if the object is within sight, this would then make it
2660 * difficult to see what object is magical/cursed, so the
2661 * effect wouldn't be as apparent.
2662 */
2663
2664 /* detect magic */
2665 if (QUERY_FLAG(spell, FLAG_KNOWN_MAGICAL)
2666 && !QUERY_FLAG(tmp, FLAG_KNOWN_MAGICAL)
2667 && !QUERY_FLAG(tmp, FLAG_IDENTIFIED)
2668 && is_magical(tmp)) {
2669 SET_FLAG(tmp, FLAG_KNOWN_MAGICAL);
2670 /* make runes more visible */
2671 if (tmp->type == RUNE && tmp->attacktype&AT_MAGIC)
2672 tmp->stats.Cha /= 4;
2673 done_one = 1;
2674 }
2675 /* detect monster */
2676 if (QUERY_FLAG(spell, FLAG_MONSTER)
2677 && (QUERY_FLAG(tmp, FLAG_MONSTER) || (tmp->type == PLAYER && !QUERY_FLAG(tmp, FLAG_WIZ)))) {
2678 done_one = 2;
2679 if (!detect)
2680 detect = tmp;
2681 }
2682 /* Basically, if race is set in the spell, then the creatures race must
2683 * match that. if the spell race is set to GOD, then the gods opposing
2684 * race must match.
2685 */
2686 if (spell->race
2687 && QUERY_FLAG(tmp, FLAG_MONSTER)
2688 && tmp->race
2689 && ((!strcmp(spell->race, "GOD") && god && god->slaying && strstr(god->slaying, tmp->race)) || (strstr(spell->race, tmp->race)))) {
2690 done_one = 2;
2691 if (!detect)
2692 detect = tmp;
2693 }
2694 if (QUERY_FLAG(spell, FLAG_KNOWN_CURSED)
2695 && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED)
2696 && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))) {
2697 SET_FLAG(tmp, FLAG_KNOWN_CURSED);
2698 done_one = 1;
2699 }
2700 } FOR_OB_AND_BELOW_FINISH(); /* for stack of objects on this space */
2701
2702 /* Code here puts an effect of the spell on the space, so you can see
2703 * where the magic is.
2704 */
2705 if (done_one) {
2706 object *detect_ob;
2707 int dx = nx, dy = ny;
2708
2709 /* if this is set, we want to copy the face */
2710 if (done_one == 2 && detect) {
2711 /*
2712 * We can't simply copy the face to a single item, because
2713 * multipart objects need to have multipart glows.
2714 * So copy the initial item, erase some properties, and use that.
2715 */
2716
2717 object *part;
2718 int flag;
2719
2720 dx = HEAD(detect)->x;
2721 dy = HEAD(detect)->y;
2722
2723 detect_ob = object_create_arch(HEAD(detect)->arch);
2724 for (part = detect_ob; part != NULL; part = part->more) {
2725 if (part->arch->reference_count > 0)
2726 part->arch->reference_count++;
2727 part->last_anim = 0;
2728 part->type = spell->other_arch->clone.type;
2729 for (flag = 0; flag < 4; flag++) {
2730 part->flags[flag] = spell->other_arch->clone.flags[flag];
2731 }
2732 part->stats.food = spell->other_arch->clone.stats.food;
2733 part->last_anim = 0;
2734 part->speed = spell->other_arch->clone.speed;
2735 part->speed_left = spell->other_arch->clone.speed_left;
2736 part->move_allow = spell->other_arch->clone.move_allow;
2737 part->move_block = spell->other_arch->clone.move_block;
2738 part->move_type = spell->other_arch->clone.move_type;
2739 part->glow_radius = spell->other_arch->clone.glow_radius;
2740 part->invisible = spell->other_arch->clone.invisible;
2741 part->weight = spell->other_arch->clone.weight;
2742 part->map_layer = spell->other_arch->clone.map_layer;
2743 FREE_AND_COPY(part->name, spell->other_arch->clone.name);
2744
2745 /* by default, the detect_ob is already animated */
2746 if (!QUERY_FLAG(detect, FLAG_ANIMATE))
2747 CLEAR_FLAG(part, FLAG_ANIMATE);
2748 }
2749 object_update_speed(detect_ob);
2750 } else
2751 detect_ob = arch_to_object(spell->other_arch);
2752
2753 object_insert_in_map_at(detect_ob, m, op, 0, dx, dy);
2754 }
2755 } /* for processing the surrounding spaces */
2756
2757
2758 /* Now process objects in the players inventory if detect curse or magic */
2759 if (QUERY_FLAG(spell, FLAG_KNOWN_CURSED) || QUERY_FLAG(spell, FLAG_KNOWN_MAGICAL)) {
2760 done_one = 0;
2761 FOR_INV_PREPARE(op, tmp) {
2762 if (!tmp->invisible && !QUERY_FLAG(tmp, FLAG_IDENTIFIED)) {
2763 if (QUERY_FLAG(spell, FLAG_KNOWN_MAGICAL)
2764 && is_magical(tmp)
2765 && !QUERY_FLAG(tmp, FLAG_KNOWN_MAGICAL)) {
2766 SET_FLAG(tmp, FLAG_KNOWN_MAGICAL);
2767 if (op->type == PLAYER)
2768 esrv_send_item(op, tmp);
2769 }
2770 if (QUERY_FLAG(spell, FLAG_KNOWN_CURSED)
2771 && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED)
2772 && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))) {
2773 SET_FLAG(tmp, FLAG_KNOWN_CURSED);
2774 if (op->type == PLAYER)
2775 esrv_send_item(op, tmp);
2776 }
2777 } /* if item is not identified */
2778 } FOR_INV_FINISH(); /* for the players inventory */
2779 } /* if detect magic/curse and object is a player */
2780 return 1;
2781 }
2782
2783 /**
2784 * Checks if victim has overcharged mana, and if so does some fireball.
2785 * @param victim
2786 * who may have overcharged.
2787 * @param caster_level
2788 * caster's (skill) level whose spell did cause the overcharge.
2789 */
2790 static void charge_mana_effect(object *victim, int caster_level) {
2791 /* Prevent explosions for objects without mana. Without this check, doors
2792 * will explode, too.
2793 */
2794 if (victim->stats.maxsp <= 0)
2795 return;
2796
2797 draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2798 "You feel energy course through you.");
2799
2800 if (victim->stats.sp >= victim->stats.maxsp*2) {
2801 object *tmp;
2802
2803 draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_SPELL,
2804 "Your head explodes!");
2805
2806 /* Explodes a fireball centered at player */
2807 tmp = create_archetype(EXPLODING_FIREBALL);
2808 tmp->dam_modifier = random_roll(1, caster_level, victim, PREFER_LOW)/5+1;
2809 tmp->stats.maxhp = random_roll(1, caster_level, victim, PREFER_LOW)/10+2;
2810 object_insert_in_map_at(tmp, victim->map, NULL, 0, victim->x, victim->y);
2811 victim->stats.sp = 2*victim->stats.maxsp;
2812 } else if (victim->stats.sp >= victim->stats.maxsp*1.88) {
2813 draw_ext_info(NDI_UNIQUE, NDI_ORANGE, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2814 "You feel like your head is going to explode.");
2815 } else if (victim->stats.sp >= victim->stats.maxsp*1.66) {
2816 draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2817 "You get a splitting headache!");
2818 } else if (victim->stats.sp >= victim->stats.maxsp*1.5) {
2819 draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2820 "Chaos fills your world.");
2821 confuse_living(victim, victim, 99);
2822 } else if (victim->stats.sp >= victim->stats.maxsp*1.25) {
2823 draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
2824 "You start hearing voices.");
2825 }
2826 }
2827
2828 /**
2829 * This spell transfers sp from the player to another person.
2830 * We let the target go above their normal maximum SP.
2831 *
2832 * @param op
2833 * who is casting.
2834 * @param caster
2835 * what is casting.
2836 * @param spell
2837 * actual spell object.
2838 * @param dir
2839 * casting direction.
2840 * @retval 0
2841 * no transfer happened.
2842 * @retval 1
2843 * transfer happened.
2844 */
2845 int cast_transfer(object *op, object *caster, object *spell, int dir) {
2846 object *plyr = NULL;
2847 int16_t x, y;
2848 mapstruct *m;
2849 int mflags;
2850
2851 m = op->map;
2852 x = op->x+freearr_x[dir];
2853 y = op->y+freearr_y[dir];
2854
2855 mflags = get_map_flags(m, &m, x, y, &x, &y);
2856 if (!(mflags&P_OUT_OF_MAP) && mflags&P_IS_ALIVE) {
2857 FOR_MAP_PREPARE(m, x, y, tmp)
2858 plyr = tmp;
2859 if (plyr != op && QUERY_FLAG(plyr, FLAG_ALIVE))
2860 break;
2861 FOR_MAP_FINISH();
2862 }
2863
2864 /* If we did not find a player in the specified direction, transfer
2865 * to anyone on top of us. This is used for the rune of transference mostly.
2866 */
2867 if (plyr == NULL)
2868 FOR_MAP_PREPARE(op->map, op->x, op->y, tmp)
2869 plyr = tmp;
2870 if (plyr != op && QUERY_FLAG(plyr, FLAG_ALIVE))
2871 break;
2872 FOR_MAP_FINISH();
2873
2874 if (!plyr) {
2875 draw_ext_info(NDI_BLACK, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
2876 "There is no one there.");
2877 return 0;
2878 }
2879
2880 /* give sp */
2881 if (spell->stats.dam > 0) {
2882 plyr->stats.sp += spell->stats.dam+SP_level_dam_adjust(caster, spell);
2883 charge_mana_effect(plyr, caster_level(caster, spell));
2884 return 1;
2885 /* suck sp away. Can't suck sp from yourself */
2886 } else if (op != plyr) {
2887 /* old dragin magic used floats. easier to just use ints and divide by 100 */
2888
2889 int rate = -spell->stats.dam+SP_level_dam_adjust(caster, spell), sucked;
2890
2891 if (rate > 95)
2892 rate = 95;
2893
2894 sucked = (plyr->stats.sp*rate)/100;
2895 plyr->stats.sp -= sucked;
2896 if (QUERY_FLAG(op, FLAG_ALIVE)) {
2897 /* Player doesn't get full credit */
2898 sucked = (sucked*rate)/100;
2899 op->stats.sp += sucked;
2900 if (sucked > 0) {
2901 charge_mana_effect(op, caster_level(caster, spell));
2902 }
2903 }
2904 return 1;
2905 }
2906 return 0;
2907 }
2908
2909 /**
2910 * Nullifies spell effects.
2911 * Basically, if the object has a magic attacktype, this may nullify it.
2912 *
2913 * @param op
2914 * counterspell object.
2915 * @param dir
2916 * direction it was cast in.
2917 */
2918 void counterspell(object *op, int dir) {
2919 object *head;
2920 int mflags;
2921 mapstruct *m;
2922 int16_t sx, sy;
2923
2924 sx = op->x+freearr_x[dir];
2925 sy = op->y+freearr_y[dir];
2926 m = op->map;
2927 mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);
2928 if (mflags&P_OUT_OF_MAP)
2929 return;
2930
2931 FOR_MAP_PREPARE(m, sx, sy, tmp) {
2932 object *owner;
2933
2934 /* Need to look at the head object - otherwise, if tmp
2935 * points to a monster, we don't have all the necessary
2936 * info for it.
2937 */
2938 head = HEAD(tmp);
2939
2940 /* don't attack our own spells */
2941 owner = object_get_owner(tmp);
2942 if (owner != NULL && owner == object_get_owner(op))
2943 continue;
2944
2945 /* Basically, if the object is magical and not counterspell,
2946 * we will more or less remove the object. Don't counterspell
2947 * monsters either.
2948 */
2949
2950 if (head->attacktype&AT_MAGIC
2951 && !(head->attacktype&AT_COUNTERSPELL)
2952 && !QUERY_FLAG(head, FLAG_MONSTER)
2953 && (op->level > head->level)) {
2954 object_remove(head);
2955 object_free(head, 0);
2956 } else switch (head->type) {
2957 case SPELL_EFFECT:
2958 if ((op->level > head->level) && !op->stats.food && !op->speed_left) {
2959 object_remove(head);
2960 object_free(head, 0);
2961 }
2962 break;
2963
2964 /* I really don't get this rune code that much - that
2965 * random chance seems really low.
2966 */
2967 case RUNE:
2968 if (rndm(0, 149) == 0) {
2969 head->stats.hp--; /* weaken the rune */
2970 if (!head->stats.hp) {
2971 object_remove(head);
2972 object_free(head, 0);
2973 }
2974 }
2975 break;
2976 }
2977 } FOR_MAP_FINISH();
2978 }
2979
2980 /**
2981 * A spell to make an altar your god's.
2982 *
2983 * @param op
2984 * who is casting.
2985 * @param caster
2986 * what is casting.
2987 * @param spell
2988 * actual spell object.
2989 * @retval 0
2990 * no consecration happened.
2991 * @retval 1
2992 * an altar waas consecrated.
2993 */
2994 int cast_consecrate(object *op, object *caster, object *spell) {
2995 char buf[MAX_BUF];
2996 const object *god = find_god(determine_god(op));
2997
2998 if (!god) {
2999 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3000 "You can't consecrate anything if you don't worship a god!");
3001 return 0;
3002 }
3003
3004 FOR_BELOW_PREPARE(op, tmp) {
3005 if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
3006 break;
3007 if (tmp->type == HOLY_ALTAR) {
3008 if (tmp->level > caster_level(caster, spell)) {
3009 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
3010 "You are not powerful enough to reconsecrate the %s",
3011 tmp->name);
3012 return 0;
3013 } else {
3014 /* If we got here, we are consecrating an altar */
3015 object *new_altar;
3016 size_t letter;
3017 archetype *altar_arch;
3018
3019 snprintf(buf, MAX_BUF, "altar_");
3020 letter = strlen(buf);
3021 strncpy(buf+letter, god->name, MAX_BUF-letter);
3022 for (; letter < strlen(buf); letter++)
3023 buf[letter] = tolower(buf[letter]);
3024 altar_arch = find_archetype(buf);
3025 if (!altar_arch) {
3026 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
3027 "You fail to consecrate the altar.");
3028 LOG(llevError, "cast_consecrate: can't find altar %s for god %s\n", buf, god->name);
3029 return 0;
3030 }
3031 new_altar = arch_to_object(altar_arch);
3032 new_altar->level = tmp->level;
3033 if (QUERY_FLAG(tmp, FLAG_IS_BUILDABLE))
3034 SET_FLAG(new_altar, FLAG_IS_BUILDABLE);
3035 object_insert_in_map_at(new_altar, tmp->map, tmp, INS_BELOW_ORIGINATOR, tmp->x, tmp->y);
3036 object_remove(tmp);
3037 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
3038 "You consecrated the altar to %s!",
3039 god->name);
3040 return 1;
3041 }
3042 }
3043 } FOR_BELOW_FINISH();
3044 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
3045 "You are not standing over an altar!");
3046 return 0;
3047 }
3048
3049 /**
3050 * Generalization of staff_to_snake(). Makes a golem out of the caster's weapon.
3051 * The golem is based on the archetype specified, modified by the caster's level
3052 * and the attributes of the weapon. The weapon is inserted in the golem's
3053 * inventory so that it falls to the ground when the golem dies.
3054 * This code was very odd - code early on would only let players use the spell,
3055 * yet the code was full of player checks. I've presumed that the code
3056 * that only let players use it was correct, and removed all the other
3057 * player checks. MSW 2003-01-06
3058 *
3059 * @param op
3060 * who is casting.
3061 * @param caster
3062 * what is casting.
3063 * @param spell
3064 * actual spell object.
3065 * @param dir
3066 * casting direction.
3067 * @retval 0
3068 * spell failure.
3069 * @retval 1
3070 * spell was successful.
3071 */
3072 int animate_weapon(object *op, object *caster, object *spell, int dir) {
3073 object *weapon, *tmp;
3074 char buf[MAX_BUF];
3075 int a, i;
3076 int16_t x, y;
3077 mapstruct *m;
3078 materialtype_t *mt;
3079
3080 if (!spell->other_arch) {
3081 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3082 "Oops, program error!");
3083 LOG(llevError, "animate_weapon failed: spell %s missing other_arch!\n", spell->name);
3084 return 0;
3085 }
3086
3087 /* exit if it's not a player using this spell. */
3088 if (op->type != PLAYER)
3089 return 0;
3090
3091 /* if player already has a golem, abort */
3092 if (op->contr->ranges[range_golem] != NULL && op->contr->golem_count == op->contr->ranges[range_golem]->count) {
3093 pets_control_golem(op->contr->ranges[range_golem], dir);
3094 return 0;
3095 }
3096
3097 /* if no direction specified, pick one */
3098 if (!dir) {
3099 dir = object_find_free_spot(NULL, op->map, op->x, op->y, 1, 9);
3100 if ( dir == -1 ) {
3101 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3102 "There is something in the way.");
3103 return 0;
3104 }
3105 }
3106
3107 m = op->map;
3108 x = op->x+freearr_x[dir];
3109 y = op->y+freearr_y[dir];
3110
3111 /* if there's no place to put the golem, abort */
3112 if ((dir == -1)
3113 || (get_map_flags(m, &m, x, y, &x, &y)&P_OUT_OF_MAP)
3114 || ((spell->other_arch->clone.move_type&GET_MAP_MOVE_BLOCK(m, x, y)) == spell->other_arch->clone.move_type)) {
3115 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3116 "There is something in the way.");
3117 return 0;
3118 }
3119
3120 /* Use the weapon marked by the player. */
3121 weapon = find_marked_object(op);
3122
3123 if (!weapon) {
3124 draw_ext_info(NDI_BLACK, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3125 "You must mark a weapon to use with this spell!");
3126 return 0;
3127 }
3128 if (spell->race && strcmp(weapon->arch->name, spell->race)) {
3129 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3130 "The spell fails to transform your weapon.");
3131 return 0;
3132 }
3133 if (weapon->type != WEAPON) {
3134 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3135 "You need to mark a weapon to animate it.");
3136 return 0;
3137 }
3138 if (QUERY_FLAG(weapon, FLAG_UNPAID)) {
3139 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3140 "You need to pay for the weapon to animate it.");
3141 return 0;
3142 }
3143 if (QUERY_FLAG(weapon, FLAG_APPLIED)) {
3144 char wn[MAX_BUF];
3145
3146 query_name(weapon, wn, MAX_BUF);
3147 draw_ext_info_format(NDI_BLACK, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3148 "You need to unequip %s before using it in this spell",
3149 wn);
3150 return 0;
3151 }
3152
3153 if (weapon->nrof > 1) {
3154 tmp = object_split(weapon, 1, NULL, 0);
3155 esrv_update_item(UPD_NROF, op, weapon);
3156 weapon = tmp;
3157 }
3158
3159 /* create the golem object */
3160 tmp = arch_to_object(spell->other_arch);
3161
3162 /* if animated by a player, give the player control of the golem */
3163 CLEAR_FLAG(tmp, FLAG_MONSTER);
3164 SET_FLAG(tmp, FLAG_FRIENDLY);
3165 tmp->stats.exp = 0;
3166 add_friendly_object(tmp);
3167 tmp->type = GOLEM;
3168 object_set_owner(tmp, op);
3169 set_spell_skill(op, caster, spell, tmp);
3170 op->contr->ranges[range_golem] = tmp;
3171 op->contr->shoottype = range_golem;
3172 op->contr->golem_count = tmp->count;
3173
3174 /* Give the weapon to the golem now. A bit of a hack to check the
3175 * removed flag - it should only be set if object_split() was
3176 * used above.
3177 */
3178 if (!QUERY_FLAG(weapon, FLAG_REMOVED))
3179 object_remove(weapon);
3180 object_insert_in_ob(weapon, tmp);
3181
3182 /* To do everything necessary to let a golem use the weapon is a pain,
3183 * so instead, just set it as equipped (otherwise, we need to update
3184 * body_info, skills, etc)
3185 */
3186 SET_FLAG(tmp, FLAG_USE_WEAPON);
3187 SET_FLAG(weapon, FLAG_APPLIED);
3188 fix_object(tmp);
3189
3190 /* There used to be 'odd' code that basically seemed to take the absolute
3191 * value of the weapon->magic an use that. IMO, that doesn't make sense -
3192 * if you're using a crappy weapon, it shouldn't be as good.
3193 */
3194
3195 /* modify weapon's animated wc */
3196 tmp->stats.wc = tmp->stats.wc
3197 -SP_level_range_adjust(caster, spell)
3198 -5*weapon->stats.Dex
3199 -2*weapon->stats.Str
3200 -weapon->magic;
3201 if (tmp->stats.wc < -127)
3202 tmp->stats.wc = -127;
3203
3204 /* Modify hit points for weapon */
3205 tmp->stats.maxhp = tmp->stats.maxhp
3206 +spell->duration
3207 +SP_level_duration_adjust(caster, spell)
3208 +8*weapon->magic
3209 +12*weapon->stats.Con;
3210 if (tmp->stats.maxhp < 0)
3211 tmp->stats.maxhp = 10;
3212 tmp->stats.hp = tmp->stats.maxhp;
3213
3214 /* Modify weapon's damage */
3215 tmp->stats.dam = spell->stats.dam
3216 +SP_level_dam_adjust(caster, spell)
3217 +weapon->stats.dam
3218 +weapon->magic
3219 +5*weapon->stats.Str;
3220 if (tmp->stats.dam < 0)
3221 tmp->stats.dam = 127;
3222
3223 /* attacktype */
3224 if (!tmp->attacktype)
3225 tmp->attacktype = AT_PHYSICAL;
3226
3227 mt = NULL;
3228 if (op->materialname != NULL)
3229 mt = name_to_material(op->materialname);
3230 if (mt != NULL) {
3231 for (i = 0; i < NROFATTACKS; i++)
3232 tmp->resist[i] = 50-(mt->save[i]*5);
3233 a = mt->save[0];
3234 } else {
3235 for (i = 0; i < NROFATTACKS; i++)
3236 tmp->resist[i] = 5;
3237 a = 10;
3238 }
3239 /* Set weapon's immunity */
3240 tmp->resist[ATNR_CONFUSION] = 100;
3241 tmp->resist[ATNR_POISON] = 100;
3242 tmp->resist[ATNR_SLOW] = 100;
3243 tmp->resist[ATNR_PARALYZE] = 100;
3244 tmp->resist[ATNR_TURN_UNDEAD] = 100;
3245 tmp->resist[ATNR_FEAR] = 100;
3246 tmp->resist[ATNR_DEPLETE] = 100;
3247 tmp->resist[ATNR_DEATH] = 100;
3248 tmp->resist[ATNR_BLIND] = 100;
3249
3250 /* Improve weapon's armour value according to best save vs. physical of its material */
3251
3252 if (a > 14)
3253 a = 14;
3254 tmp->resist[ATNR_PHYSICAL] = 100-(int)((100.0-(float)tmp->resist[ATNR_PHYSICAL])/(30.0-2.0*a));
3255
3256 /* Determine golem's speed */
3257 tmp->speed = 0.4+0.1*SP_level_range_adjust(caster, spell);
3258
3259 if (tmp->speed > 3.33)
3260 tmp->speed = 3.33;
3261
3262 if (!spell->race) {
3263 snprintf(buf, sizeof(buf), "animated %s", weapon->name);
3264 if (tmp->name)
3265 free_string(tmp->name);
3266 tmp->name = add_string(buf);
3267
3268 tmp->face = weapon->face;
3269 tmp->animation = weapon->animation;
3270 tmp->anim_speed = weapon->anim_speed;
3271 tmp->last_anim = weapon->last_anim;
3272 tmp->state = weapon->state;
3273 if (QUERY_FLAG(weapon, FLAG_ANIMATE)) {
3274 SET_FLAG(tmp, FLAG_ANIMATE);
3275 } else {
3276 CLEAR_FLAG(tmp, FLAG_ANIMATE);
3277 }
3278 object_update_speed(tmp);
3279 }
3280
3281 /* make experience increase in proportion to the strength of the summoned creature. */
3282 tmp->stats.exp *= 1+(MAX(spell->stats.maxgrace, spell->stats.sp)/caster_level(caster, spell));
3283
3284 tmp->speed_left = -1;
3285 tmp->direction = dir;
3286 object_insert_in_map_at(tmp, m, op, 0, x, y);
3287 return 1;
3288 }
3289
3290 /**
3291 * This changes the light level for the entire map.
3292 *
3293 * @param op
3294 * who is casting.
3295 * @param caster
3296 * what is casting.
3297 * @param spell
3298 * actual spell object.
3299 * @retval 0
3300 * light not affected.
3301 * @retval 1
3302 * light changed.
3303 */
3304 int cast_change_map_lightlevel(object *op, object *caster, object *spell) {
3305 int success;
3306
3307 if (!op->map)
3308 return 0; /* shouldnt happen */
3309
3310 success = change_map_light(op->map, spell->stats.dam);
3311 if (!success) {
3312 if (spell->stats.dam < 0)
3313 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
3314 "It can be no brighter here.");
3315 else
3316 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
3317 "It can be no darker here.");
3318 }
3319 return success;
3320 }
3321
3322 /**
3323 * Create an aura spell object and put it in the player's inventory.
3324 * This is also used for elemental shields - the creation is the same
3325 * just the 'move' code is different.
3326 *
3327 * @param op
3328 * who is casting.
3329 * @param caster
3330 * what is casting.
3331 * @param spell
3332 * actual spell object.
3333 * @return
3334 * 1.
3335 */
3336 int create_aura(object *op, object *caster, object *spell) {
3337 int refresh = 0, i;
3338 object *new_aura;
3339
3340 new_aura = arch_present_in_ob(spell->other_arch, op);
3341 if (new_aura)
3342 refresh = 1;
3343 else
3344 new_aura = arch_to_object(spell->other_arch);
3345
3346 new_aura->duration = spell->duration+10*SP_level_duration_adjust(caster, spell);
3347 if (op->type == PLAYER)
3348 store_spell_expiry(new_aura);
3349
3350 new_aura->stats.dam = spell->stats.dam+SP_level_dam_adjust(caster, spell);
3351
3352 object_set_owner(new_aura, op);
3353 set_spell_skill(op, caster, spell, new_aura);
3354 new_aura->attacktype = spell->attacktype;
3355
3356 new_aura->level = caster_level(caster, spell);
3357
3358 /* Code below is so that auras can also provide resistances. For
3359 * example, fire shield both does damage to nearby enemies and also
3360 * provides some protection to fire. We need to use a different
3361 * FORCE object for this, as putting it in with the aura object
3362 * just puts too many meanings into that one object. Because
3363 * the duration of this force object is the same, we don't need
3364 * to set up spell expiry on it - this second object is really
3365 * an internal mechanic that should be invisible to the player.
3366 */
3367 for (i = 0; i < NROFATTACKS; i++) {
3368 if (spell->resist[i]) {
3369 int refresh1=1;
3370 object *force;
3371
3372 force = object_present_in_ob_by_name(FORCE, spell->name, op);
3373 if (!force) {
3374 force=create_archetype(FORCE_NAME);
3375 force->subtype = FORCE_CHANGE_ABILITY;
3376 free_string(force->name);
3377 force->name = add_refcount(spell->name);
3378 free_string(force->name_pl);
3379 force->name_pl = add_refcount(spell->name);
3380 refresh1=0;
3381 }
3382 force->duration = new_aura->duration;
3383 force->speed = new_aura->speed;
3384 memcpy(&force->resist, spell->resist, sizeof(spell->resist));
3385 SET_FLAG(force, FLAG_APPLIED);
3386
3387 if (!refresh1)
3388 object_insert_in_ob(force, op);
3389 change_abil(op, new_aura);
3390 fix_object(op);
3391 break;
3392 }
3393 }
3394
3395 if (refresh)
3396 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
3397 "You recast the spell while in effect.");
3398 else
3399 object_insert_in_ob(new_aura, op);
3400 return 1;
3401 }
3402
3403 /**
3404 * This writes a rune that contains the appropriate message.
3405 * There really aren't any adjustments we make.
3406 *
3407 * @param op
3408 * who is casting.
3409 * @param spell
3410 * actual spell cast.
3411 * @param msg
3412 * message to write.
3413 * @retval 0
3414 * failure.
3415 * @retval 1
3416 * success.
3417 */
3418 int write_mark(object *op, object *spell, const char *msg) {
3419 char rune[HUGE_BUF];
3420 object *tmp;
3421
3422 if (!msg || msg[0] == 0) {
3423 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3424 "Write what?");
3425 return 0;
3426 }
3427
3428 if (strcasestr_local(msg, "endmsg")) {
3429 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
3430 "Trying to cheat are we?");
3431 LOG(llevInfo, "write_rune: player %s tried to write bogus rune %s\n", op->name, msg);
3432 return 0;
3433 }
3434
3435 if (!spell->other_arch)
3436 return 0;
3437 tmp = arch_to_object(spell->other_arch);
3438 strncpy(rune, msg, HUGE_BUF-2);
3439 rune[HUGE_BUF-2] = 0;
3440 strcat(rune, "\n");
3441 tmp->race = add_string(op->name); /*Save the owner of the rune*/
3442 object_set_msg(tmp, rune);
3443 object_insert_in_map_at(tmp, op->map, op, INS_BELOW_ORIGINATOR, op->x, op->y);
3444 return 1;
3445 }