ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.74
Committed: Sat Nov 17 23:40:04 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.73: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * Deliantra is free software: you can redistribute it and/or modify it under
10 * the terms of the Affero GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the Affero GNU General Public License
20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 #include <global.h>
27 #include <sproto.h>
28
29 /* given that 'pet' is a friendly object, this function returns a
30 * monster the pet should attack, NULL if nothing appropriate is
31 * found. it basically looks for nasty things around the owner
32 * of the pet to attack.
33 * this is now tilemap aware.
34 */
35 object *
36 get_pet_enemy (object *pet, rv_vector * rv)
37 {
38 object *tmp, *attacker, *tmp3;
39 int i;
40 sint16 x, y;
41 maptile *nm;
42 int search_arr[SIZEOFFREE];
43 int mflags;
44
45 attacker = pet->attacked_by; /*pointer to attacking enemy */
46 pet->attacked_by = NULL; /*clear this, since we are dealing with it */
47
48 object *owner = pet->owner;
49
50 if (!owner)
51 {
52 /* the owner is no longer around, so the
53 * pet no longer needs to be friendly.
54 */
55 remove_friendly_object (pet);
56 pet->attack_movement &= ~PETMOVE;
57 return 0;
58 }
59
60 /* If the owner has turned on the pet, make the pet
61 * unfriendly.
62 */
63 if (check_enemy (owner, rv) == pet)
64 {
65 remove_friendly_object (pet);
66 pet->attack_movement &= ~PETMOVE;
67 return owner;
68 }
69
70 /* If they are not on the same map, the pet won't be agressive */
71 //if (!on_same_map (pet, owner))
72 // return 0;
73
74 /* See if the pet has an existing enemy. If so, don't start a new one */
75 if (tmp = check_enemy (pet, rv))
76 {
77 if (tmp == owner && !pet->flag [FLAG_CONFUSED] && pet->flag [FLAG_FRIENDLY])
78 /* without this check, you can actually get pets with
79 * enemy set to owner!
80 */
81 pet->enemy = 0;
82 else
83 return tmp;
84 }
85
86 get_search_arr (search_arr);
87
88 if (owner->type == PLAYER && owner->contr->petmode > pet_normal)
89 {
90 if (owner->contr->petmode == pet_sad)
91 {
92 tmp = find_nearest_living_creature (pet);
93 if (tmp != 0)
94 {
95 get_rangevector (pet, tmp, rv, 0);
96 if (check_enemy (pet, rv) != 0)
97 return tmp;
98 else
99 pet->enemy = 0;
100 }
101 /* if we got here we have no enemy */
102 /* we return 0 to avoid heading back to the owner */
103 pet->enemy = 0;
104 return 0;
105 }
106 }
107
108 /* Since the pet has no existing enemy, look for anything nasty
109 * around the owner that it should go and attack. (if the owner is
110 * still on a map)
111 */
112 // owners sometimes are not on a map but in the inventory of somehting else
113 if (!owner->flag [FLAG_REMOVED] && owner->map)
114 {
115 tmp3 = 0;
116
117 for (i = 0; i < SIZEOFFREE; i++)
118 {
119 x = owner->x + DIRX (search_arr[i]);
120 y = owner->y + DIRY (search_arr[i]);
121 nm = owner->map;
122 /* Only look on the space if there is something alive there. */
123 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
124
125 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
126 {
127 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
128 {
129 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
130
131 if (tmp2->flag [FLAG_ALIVE] && ((!tmp2->flag [FLAG_FRIENDLY] &&
132 (tmp2->type != PLAYER)) ||
133 should_arena_attack (pet, owner, tmp2))
134 && !tmp2->flag [FLAG_UNAGGRESSIVE] && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
135 {
136
137 if (!can_see_enemy (pet, tmp2))
138 {
139 if (tmp3 != 0)
140 tmp3 = tmp2;
141 }
142 else
143 {
144 pet->enemy = tmp2;
145 if (check_enemy (pet, rv) != 0)
146 return tmp2;
147 else
148 pet->enemy = 0;
149 }
150 } /* if this is a valid enemy */
151 } /* for objects on this space */
152 } /* if there is something living on this space */
153 } /* for loop of spaces around the owner */
154
155 /* fine, we went through the whole loop and didn't find one we could
156 see, take what we have */
157 if (tmp3 != 0)
158 {
159 pet->enemy = tmp3;
160 if (check_enemy (pet, rv) != 0)
161 return tmp3;
162 else
163 pet->enemy = 0;
164 }
165 }
166
167 /* No threat to owner, check to see if the pet has an attacker */
168 if (attacker)
169 {
170 /* also need to check to make sure it is not freindly */
171 /* or otherwise non-hostile, and is an appropriate target */
172 if (!attacker->flag [FLAG_FRIENDLY] && on_same_map (pet, attacker))
173 {
174 pet->enemy = attacker;
175
176 if (check_enemy (pet, rv) != 0)
177 return attacker;
178 else
179 pet->enemy = 0;
180 }
181 }
182
183 /* Don't have an attacker or legal enemy, so look for a new one!.
184 * This looks for one around where the pet is. Thus, you could lead
185 * a pet to danger, then take a few steps back. This code is basically
186 * the same as the code that looks around the owner.
187 */
188 if (owner->type == PLAYER && owner->contr->petmode != pet_defend)
189 {
190 tmp3 = 0;
191 for (i = 0; i < SIZEOFFREE; i++)
192 {
193 x = pet->x + DIRX (search_arr[i]);
194 y = pet->y + DIRY (search_arr[i]);
195 nm = pet->map;
196 /* Only look on the space if there is something alive there. */
197 mflags = get_map_flags (nm, &nm, x, y, &x, &y);
198 if (!(mflags & P_OUT_OF_MAP) && mflags & P_IS_ALIVE)
199 {
200 for (tmp = GET_MAP_OB (nm, x, y); tmp != 0; tmp = tmp->above)
201 {
202 object *tmp2 = tmp->head == 0 ? tmp : tmp->head;
203
204 if (tmp2->flag [FLAG_ALIVE] && ((!tmp2->flag [FLAG_FRIENDLY] &&
205 (tmp2->type != PLAYER)) ||
206 should_arena_attack (pet, owner, tmp2))
207 && !tmp2->flag [FLAG_UNAGGRESSIVE] && tmp2 != pet && tmp2 != owner && can_detect_enemy (pet, tmp2, rv))
208 {
209
210 if (!can_see_enemy (pet, tmp2))
211 {
212 if (tmp3 != 0)
213 tmp3 = tmp2;
214 }
215 else
216 {
217 pet->enemy = tmp2;
218 if (check_enemy (pet, rv) != 0)
219 return tmp2;
220 else
221 pet->enemy = 0;
222 }
223 } /* make sure we can get to the bugger */
224 } /* for objects on this space */
225 } /* if there is something living on this space */
226 } /* for loop of spaces around the pet */
227
228 /* fine, we went through the whole loop and didn't find one we could
229 see, take what we have */
230 if (tmp3 != 0)
231 {
232 pet->enemy = tmp3;
233 if (check_enemy (pet, rv) != 0)
234 return tmp3;
235 else
236 pet->enemy = 0;
237 }
238 } /* pet in defence mode */
239
240 object *enemy = check_enemy (pet, rv);
241 // we have a summoned pet here and the owners enemy isn't set or can't
242 // be reached => search for a player around us and set it as our new enemy!!
243 if (!enemy && pet->owner && pet->owner->type != PLAYER)
244 enemy = get_nearest_player (pet);
245
246 /* Didn't find anything - return the owner's enemy or 0 */
247 return enemy;
248 }
249
250 void
251 terminate_all_pets (object *owner)
252 {
253 objectlink *obl, *next;
254
255 for (obl = first_friendly_object; obl; obl = next)
256 {
257 object *ob = obl->ob;
258 next = obl->next;
259
260 if (ob->owner == owner)
261 ob->drop_and_destroy ();
262 }
263 }
264
265 /*
266 * Unfortunately, sometimes, the owner of a pet is in the
267 * process of entering a new map when this is called.
268 * Interesting enough, we don't use the passed map structure in
269 * this function.
270 */
271 void
272 move_all_pets ()
273 {
274 objectlink *obl, *next;
275 object *owner;
276
277 for (obl = first_friendly_object; obl; obl = next)
278 {
279 next = obl->next;
280
281 if (obl->ob->type != PLAYER
282 && obl->ob->flag [FLAG_FRIENDLY]
283 && (owner = obl->ob->owner)
284 && !on_same_map (owner, obl->ob))
285 {
286 /* follow owner checks map status for us */
287 follow_owner (obl->ob, owner);
288 }
289 }
290 }
291
292 int
293 follow_owner (object *ob, object *owner)
294 {
295 if (owner->flag [FLAG_REMOVED])
296 return 0; // do nothing if the owner is removed
297 else if (!owner->map || owner->map->state != MAP_ACTIVE)
298 LOG (llevError, "owner '%s' of the pet '%s' not on a map in memory!?\n", &owner->name, &ob->name);
299 else
300 {
301 int dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE);
302
303 if (dir >= 0)
304 {
305 owner->map->insert (ob, owner->x + DIRX (dir), owner->y + DIRY (dir));
306
307 if (owner->type == PLAYER) /* Uh, I hope this is always true... */
308 new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you");
309
310 return 0;
311 }
312 }
313
314 ob->drop_and_destroy ();
315
316 return 1;
317 }
318
319 void
320 pet_move (object *ob)
321 {
322 int dir = 0, i;
323 sint16 dx, dy;
324 object *ob2, *owner;
325 maptile *m;
326
327 /* Check to see if player pulled out */
328 if ((owner = ob->owner) == NULL)
329 {
330 ob->drop_and_destroy ();
331 LOG (llevTrace, "Pet: no owner, leaving.\n");
332 return;
333 }
334
335 /* move monster into the owners map if not in the same map
336 * except when the owner is removed.
337 */
338 if (!on_same_map (ob, owner) && !owner->flag [FLAG_REMOVED])
339 {
340 follow_owner (ob, owner);
341 return;
342 }
343
344 /* Calculate Direction */
345 if (owner->type == PLAYER && owner->contr->petmode == pet_sad)
346 {
347 /* in S&D mode, if we have no enemy, run randomly about. */
348 for (i = 0; i < 15; i++)
349 {
350 dir = rndm (1, 8);
351 dx = ob->x + DIRX (dir);
352 dy = ob->y + DIRY (dir);
353 m = ob->map;
354 if (get_map_flags (ob->map, &m, dx, dy, &dx, &dy) & P_OUT_OF_MAP)
355 continue;
356 else if (OB_TYPE_MOVE_BLOCK (ob, GET_MAP_MOVE_BLOCK (m, dx, dy)))
357 continue;
358 else
359 break;
360 }
361 }
362 else if (!owner->flag [FLAG_REMOVED]) // only get vector to owner when owner not removed
363 {
364 struct rv_vector rv;
365
366 get_rangevector (ob, ob->owner, &rv, 0);
367 dir = rv.direction;
368 }
369
370 ob->direction = dir;
371
372 /* move_ob returns 0 if the object couldn't move. If that is the
373 * case, lets do some other work.
374 */
375 if (!(ob->move (dir)))
376 {
377 object *part;
378
379 /* the failed move_ob above may destroy the pet, so check here */
380 if (ob->destroyed ())
381 return;
382
383 for (part = ob; part != NULL; part = part->more)
384 {
385 dx = part->x + DIRX (dir);
386 dy = part->y + DIRY (dir);
387 m = get_map_from_coord (part->map, &dx, &dy);
388 if (!m)
389 continue;
390
391 for (ob2 = GET_MAP_OB (m, dx, dy); ob2 != NULL; ob2 = ob2->above)
392 {
393 object *new_ob;
394
395 new_ob = ob2->head ? ob2->head : ob2;
396
397 if (new_ob == ob)
398 break;
399
400 if (new_ob == ob->owner)
401 return;
402
403 if (new_ob->owner == ob->owner)
404 break;
405
406 /* Hmm. Did we try to move into an enemy monster? If so,
407 * make it our enemy.
408 */
409 if (new_ob->flag [FLAG_ALIVE] && !ob->flag [FLAG_UNAGGRESSIVE]
410 && !new_ob->flag [FLAG_UNAGGRESSIVE] && !new_ob->flag [FLAG_FRIENDLY])
411 {
412
413 ob->enemy = new_ob;
414 if (new_ob->enemy == NULL)
415 new_ob->enemy = ob;
416 return;
417 }
418 else if (new_ob->type == PLAYER)
419 {
420 new_draw_info (NDI_UNIQUE, 0, new_ob, "You stand in the way of someones pet.");
421 return;
422 }
423 }
424 }
425 /* Try a different course */
426 dir = absdir (dir + 4 - (rndm (5)) - (rndm (5)));
427 ob->move (dir);
428 }
429 }
430
431 /****************************************************************************
432 *
433 * GOLEM SPELL CODE
434 *
435 ****************************************************************************/
436
437 /* fix_summon_pet() - this makes multisquare/single square monsters
438 * proper for map insertion.
439 * at is the archetype, op is the caster of the spell, dir is the
440 * direction the monster should be placed in.
441 * is_golem is to note that this is a golem spell.
442 */
443 static object *
444 fix_summon_pet (archetype *at, object *op, int dir, int is_golem)
445 {
446 object *tmp = NULL, *prev = NULL, *head = NULL;
447
448 for (archetype *atmp = at; atmp; atmp = (archetype *)atmp->more)
449 {
450 tmp = atmp->instance ();
451
452 if (atmp == at)
453 {
454 if (!is_golem)
455 tmp->set_flag (FLAG_MONSTER);
456
457 tmp->set_owner (op);
458
459 if (op->type == PLAYER)
460 {
461 tmp->stats.exp = 0;
462 add_friendly_object (tmp);
463 if (is_golem)
464 tmp->clr_flag (FLAG_MONSTER);
465 }
466 else
467 {
468 if (op->flag [FLAG_FRIENDLY])
469 {
470 object *owner = op->owner;
471
472 if (owner)
473 { /* For now, we transfer ownership */
474 tmp->set_owner (owner);
475 tmp->attack_movement = PETMOVE;
476 add_friendly_object (tmp);
477 }
478 }
479 }
480
481 if (op->type != PLAYER || !is_golem)
482 {
483 tmp->attack_movement = PETMOVE;
484 tmp->speed_left = -1;
485 tmp->type = 0;
486 tmp->enemy = op->enemy;
487 }
488 else
489 tmp->type = GOLEM;
490
491 }
492
493 if (!head)
494 head = tmp;
495
496 tmp->x = op->x + DIRX (dir) + tmp->arch->x;
497 tmp->y = op->y + DIRY (dir) + tmp->arch->y;
498 tmp->map = op->map;
499
500 if (tmp->invisible)
501 tmp->invisible = 0;
502
503 if (head != tmp)
504 tmp->head = head, prev->more = tmp;
505
506 prev = tmp;
507 }
508
509 head->direction = dir;
510
511 /* need to change some monster attr to prevent problems/crashing */
512 head->last_heal = 0;
513 head->last_eat = 0;
514 head->last_grace = 0;
515 head->last_sp = 0;
516 head->other_arch = NULL;
517 head->stats.exp = 0;
518 head->clr_flag (FLAG_CHANGING);
519 head->clr_flag (FLAG_STAND_STILL);
520 head->clr_flag (FLAG_GENERATOR);
521 head->clr_flag (FLAG_SPLITTING);
522 if (head->attacktype & AT_GHOSTHIT)
523 head->attacktype = (AT_PHYSICAL | AT_DRAIN);
524
525 return head;
526 }
527
528 /* updated this to allow more than the golem 'head' to attack */
529 /* op is the golem to be moved. */
530 void
531 move_golem (object *op)
532 {
533 int made_attack = 0;
534 object *tmp;
535
536 if (op->flag [FLAG_MONSTER])
537 return; /* Has already been moved */
538
539 if (!op->owner)
540 {
541 LOG (llevDebug, "Golem without owner destructed.\n");
542 op->drop_and_destroy ();
543 return;
544 }
545
546 /* It would be nice to have a cleaner way of what message to print
547 * when the golem expires than these hard coded entries.
548 * Note it is intentional that a golems duration is based on its
549 * hp, and not duration
550 */
551 if (--op->stats.hp < 0)
552 {
553 if (op->msg)
554 new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg);
555
556 op->drop_and_destroy ();
557 return;
558 }
559
560 /* Do golem attacks/movement for single & multisq golems.
561 * Assuming here that op is the 'head' object. Pass only op to
562 * ->move (makes recursive calls to other parts)
563 * move_ob returns 0 if the creature was not able to move.
564 */
565 if (op->move (op->direction, op))
566 return;
567
568 if (op->destroyed ())
569 return;
570
571 for (tmp = op; tmp; tmp = tmp->more)
572 {
573 sint16 x = tmp->x + DIRX (op->direction), y = tmp->y + DIRY (op->direction);
574 object *victim;
575 maptile *m;
576 int mflags;
577
578 m = op->map;
579 mflags = get_map_flags (m, &m, x, y, &x, &y);
580
581 if (mflags & P_OUT_OF_MAP)
582 continue;
583
584 for (victim = GET_MAP_OB (m, x, y); victim; victim = victim->above)
585 if (victim->flag [FLAG_ALIVE])
586 break;
587
588 /* We used to call will_hit_self to make sure we don't
589 * hit ourselves, but that didn't work, and I don't really
590 * know if that was more efficient anyways than this.
591 * This at least works. Note that victim->head can be NULL,
592 * but since we are not trying to dereferance that pointer,
593 * that isn't a problem.
594 */
595 if (victim && victim != op && victim->head != op)
596 {
597 /* for golems with race fields, we don't attack
598 * aligned races
599 */
600
601 if (victim->race && op->race && op->race.contains (victim->race))
602 {
603 if (op->owner)
604 new_draw_info_format (NDI_UNIQUE, 0, op->owner, "%s avoids damaging %s.", &op->name, &victim->name);
605 }
606 else if (victim == op->owner)
607 {
608 if (op->owner)
609 new_draw_info_format (NDI_UNIQUE, 0, op->owner, "%s avoids damaging you.", &op->name);
610 }
611 else
612 {
613 attack_ob (victim, op);
614 made_attack = 1;
615 }
616 } /* If victim */
617 }
618
619 if (made_attack)
620 update_object (op, UP_OBJ_FACE);
621 }
622
623 /* this is a really stupid function when you get down and
624 * look at it. Keep it here for the time being - makes life
625 * easier if we ever decide to do more interesting thing with
626 * controlled golems.
627 */
628 void
629 control_golem (object *op, int dir)
630 {
631 op->direction = dir;
632 }
633
634 /* summon golem: summons a monster for 'op'. caster is the object
635 * casting the spell, dir is the direction to place the monster,
636 * at is the archetype of the monster, and spob is the spell
637 * object. At this stage, all spob is really used for is to
638 * adjust some values in the monster.
639 */
640 int
641 summon_golem (object *op, object *caster, int dir, object *spob)
642 {
643 object *tmp, *god = NULL;
644 archetype *at;
645
646 /* Because there can be different golem spells, player may want to
647 * 'lose' their old golem.
648 */
649 if (op->type == PLAYER && op->contr->golem)
650 {
651 new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem.");
652 op->contr->golem->drop_and_destroy ();
653 op->contr->golem = 0;
654 }
655
656 if (spob->other_arch)
657 at = spob->other_arch;
658 else if (spob->race)
659 {
660 god = find_god (determine_god (caster));
661
662 if (!god)
663 {
664 new_draw_info_format (NDI_UNIQUE, 0, op, "You must worship a god to cast %s.", &spob->name);
665 return 0;
666 }
667
668 at = determine_holy_arch (god, spob->race);
669
670 if (!at)
671 {
672 new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no %s for you to call.", &god->name, &spob->race);
673 return 0;
674 }
675 }
676 else
677 {
678 LOG (llevError, "Spell %s lacks other_arch\n", &spob->name);
679 return 0;
680 }
681
682 if (!dir)
683 dir = find_free_spot (at, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
684
685 if (dir < 0 || at->blocked (op->map, op->x + DIRX (dir), op->y + DIRY (dir)))
686 {
687 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
688 return 0;
689 }
690
691 /* basically want to get proper map/coordinates for this object */
692
693 if (!(tmp = fix_summon_pet (at, op, dir, GOLEM)))
694 {
695 new_draw_info (NDI_UNIQUE, 0, op, "Your spell fails.");
696 return 0;
697 }
698
699 if (op->type == PLAYER)
700 {
701 tmp->type = GOLEM;
702 op->contr->golem = tmp;
703 /* give the player control of the golem */
704 set_spell_skill (op, caster, spob, tmp);
705 }
706
707 /* This sets the level dependencies on dam and hp for monsters */
708 /* players can't cope with too strong summonings. */
709 /* but monsters can. reserve these for players. */
710 if (op->type == PLAYER)
711 {
712 tmp->stats.hp += spob->duration + SP_level_duration_adjust (caster, spob);
713
714 if (!spob->stats.dam)
715 tmp->stats.dam += SP_level_dam_adjust (caster, spob);
716 else
717 tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob);
718
719 tmp->set_speed (min (1.f, tmp->speed + .02f * SP_level_range_adjust (caster, spob)));
720
721 if (spob->attacktype)
722 tmp->attacktype = spob->attacktype;
723 }
724
725 tmp->stats.wc -= SP_level_range_adjust (caster, spob);
726
727 /* limit the speed to 0.3 for non-players, 1 for players. */
728
729 /* make experience increase in proportion to the strength.
730 * this is a bit simplistic - we are basically just looking at how
731 * often the sp doubles and use that as the ratio.
732 */
733 tmp->stats.exp *= 1 + max (spob->stats.maxgrace, spob->stats.sp) / casting_level (caster, spob);
734 tmp->speed_left = 0;
735 tmp->direction = dir;
736
737 /* Holy spell - some additional tailoring */
738 if (god)
739 {
740 char *buf = format ("%s of %s", &spob->name, &god->name);
741 buf[0] = toupper (buf[0]);
742
743 for (object *tmp2 = tmp; tmp2; tmp2 = tmp2->more)
744 tmp2->name = buf;
745
746 tmp->attacktype |= god->attacktype;
747 memcpy (tmp->resist, god->resist, sizeof (tmp->resist));
748 tmp->race = god->race;
749 tmp->slaying = god->slaying;
750
751 /* safety, we must allow a god's servants some reasonable attack */
752 if (!(tmp->attacktype & AT_PHYSICAL))
753 tmp->attacktype |= AT_PHYSICAL;
754 }
755
756 insert_ob_in_map (tmp, tmp->map, op, 0);
757
758 return 1;
759 }
760
761
762 /***************************************************************************
763 *
764 * Summon monster/pet/other object code
765 *
766 ***************************************************************************/
767
768 /* Returns a monster (chosen at random) that this particular player (and his
769 * god) find acceptable. This checks level, races allowed by god, etc
770 * to determine what is acceptable.
771 * This returns NULL if no match was found.
772 */
773 static object *
774 choose_cult_monster (object *pl, object *god, int summon_level)
775 {
776 char buf[MAX_BUF];
777 const char *race;
778 int racenr, mon_nr, i;
779 racelink *list;
780 objectlink *tobl;
781 object *otmp;
782
783 /* Determine the number of races available */
784 racenr = 0;
785 strcpy (buf, god->race);
786 race = strtok (buf, ",");
787 while (race)
788 {
789 racenr++;
790 race = strtok (NULL, ",");
791 }
792
793 /* next, randomly select a race from the aligned_races string */
794 if (racenr > 1)
795 {
796 racenr = rndm (0, racenr - 1);
797 strcpy (buf, god->race);
798 race = strtok (buf, ",");
799 for (i = 0; i < racenr; i++)
800 race = strtok (NULL, ",");
801 }
802 else
803 race = god->race;
804
805
806 /* see if a we can match a race list of monsters. This should not
807 * happen, so graceful recovery isn't really needed, but this sanity
808 * checking is good for cases where the god archetypes mismatch the
809 * race file
810 */
811 if ((list = find_racelink (race)) == NULL)
812 {
813 new_draw_info_format (NDI_UNIQUE, 0, pl, "The spell fails! %s's creatures are beyond the range of your summons", &god->name);
814 LOG (llevDebug, "choose_cult_monster() requested non-existent aligned race!\n");
815 return 0;
816 }
817
818 /* search for an apprplritate monster on this race list */
819 mon_nr = 0;
820 for (tobl = list->member; tobl; tobl = tobl->next)
821 {
822 otmp = tobl->ob;
823 if (!otmp || !otmp->flag [FLAG_MONSTER])
824 continue;
825 if (otmp->level <= summon_level)
826 mon_nr++;
827 }
828
829 /* If this god has multiple race entries, we should really choose another.
830 * But then we either need to track which ones we have tried, or just
831 * make so many calls to this function, and if we get so many without
832 * a valid entry, assuming nothing is available and quit.
833 */
834 if (!mon_nr)
835 return NULL;
836
837 mon_nr = rndm (mon_nr - 1);
838 for (tobl = list->member; tobl; tobl = tobl->next)
839 {
840 otmp = tobl->ob;
841 if (!otmp || !otmp->flag [FLAG_MONSTER])
842 continue;
843
844 if (otmp->level <= summon_level && !mon_nr--)
845 return otmp;
846 }
847
848 /* This should not happen */
849 LOG (llevDebug, "choose_cult_monster() mon_nr was set, but did not find a monster\n");
850 return NULL;
851 }
852
853 int
854 summon_object (object *op, object *caster, object *spell_ob, int dir, const char *stringarg)
855 {
856 int nrof = 1;
857 archetype *summon_arch;
858 int ndir;
859
860 if (spell_ob->other_arch)
861 summon_arch = spell_ob->other_arch;
862 else if (spell_ob->randomitems)
863 {
864 int level = casting_level (caster, spell_ob);
865 treasure *tr, *lasttr = NULL;
866
867 shstr_cmp sparam (stringarg);
868
869 /* In old code, this was a very convoluted for statement,
870 * with all the checks in the 'for' portion itself. Much
871 * more readable to break some of the conditions out.
872 */
873 for (tr = spell_ob->randomitems->items; tr; tr = tr->next)
874 {
875 if (!tr->item)
876 continue;
877
878 if (level < tr->magic)
879 break;
880
881 lasttr = tr;
882
883 if (tr->item->archname == sparam)
884 break;
885 }
886
887 if (!lasttr)
888 {
889 LOG (llevError, "Treasurelist %s did not generate a valid entry in summon_object\n", &spell_ob->randomitems->name);
890 new_draw_info (NDI_UNIQUE, 0, op, "The spell fails to summon any monsters.");
891 return 0;
892 }
893
894 summon_arch = lasttr->item;
895 nrof = lasttr->nrof;
896 }
897 else if (spell_ob->race == shstr_GODCULTMON)
898 {
899 object *god = find_god (determine_god (op)), *mon, *owner;
900 int summon_level, tries;
901
902 if (!god && ((owner = op->owner) != NULL))
903 god = find_god (determine_god (owner));
904
905 /* If we can't find a god, can't get what monster to summon */
906 if (!god)
907 return 0;
908
909 if (!god->race)
910 {
911 new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no creatures that you may summon!", &god->name);
912 return 0;
913 }
914
915 /* the summon level */
916 summon_level = casting_level (caster, spell_ob);
917 if (summon_level == 0)
918 summon_level = 1;
919
920 tries = 0;
921 do
922 {
923 mon = choose_cult_monster (op, god, summon_level);
924 if (!mon)
925 {
926 new_draw_info_format (NDI_UNIQUE, 0, op, "%s fails to send anything.", &god->name);
927 return 0;
928 }
929
930 ndir = dir;
931
932 if (!ndir)
933 ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE);
934
935 if (ndir < 0 || mon->blocked (op->map, op->x + DIRX (ndir), op->y + DIRY (ndir)))
936 {
937 ndir = -1;
938 if (++tries == 5)
939 {
940 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
941 return 0;
942 }
943 }
944 }
945 while (ndir == -1);
946
947 if (mon->level > (summon_level / 2))
948 nrof = random_roll (1, 2, op, PREFER_HIGH);
949 else
950 nrof = die_roll (2, 2, op, PREFER_HIGH);
951
952 summon_arch = mon->arch;
953 }
954 else
955 summon_arch = 0;
956
957 if (spell_ob->stats.dam)
958 nrof += spell_ob->stats.dam + SP_level_dam_adjust (caster, spell_ob);
959
960 if (!summon_arch)
961 {
962 new_draw_info (NDI_UNIQUE, 0, op, "There is no monsters available for summoning.");
963 return 0;
964 }
965
966 for (int i = 1; i <= nrof; i++)
967 {
968 object *prev = NULL, *head = NULL, *tmp;
969
970 if (dir)
971 {
972 ndir = dir;
973 dir = absdir (dir + 1);
974 }
975 else
976 ndir = find_free_spot (summon_arch, op->map, op->x, op->y, 1, SIZEOFFREE);
977
978 sint16 x = DIRX (ndir);
979 sint16 y = DIRY (ndir);
980
981 if (ndir < 0 || summon_arch->blocked (op->map, op->x + x, op->y + y))
982 {
983 new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
984 if (nrof > 1)
985 new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting.");
986
987 return nrof > 1;
988 }
989
990 for (archetype *atmp = summon_arch; atmp; atmp = (archetype *)atmp->more)
991 {
992 tmp = atmp->instance ();
993 if (atmp == summon_arch)
994 {
995 if (tmp->flag [FLAG_MONSTER])
996 {
997 tmp->set_owner (op);
998 set_spell_skill (op, caster, spell_ob, tmp);
999 tmp->enemy = op->enemy;
1000 tmp->type = 0;
1001 tmp->clr_flag (FLAG_SLEEP);
1002
1003 if (op->type == PLAYER || op->flag [FLAG_FRIENDLY])
1004 {
1005 /* If this is not set, we make it friendly */
1006 if (!spell_ob->flag [FLAG_MONSTER])
1007 {
1008 add_friendly_object (tmp);
1009 tmp->stats.exp = 0;
1010
1011 if (spell_ob->attack_movement)
1012 tmp->attack_movement = spell_ob->attack_movement;
1013
1014 if (op->owner)
1015 tmp->set_owner (op->owner);
1016 }
1017 }
1018 }
1019
1020 if (tmp->has_active_speed ())
1021 tmp->speed_left = -1;
1022 }
1023
1024 if (head == NULL)
1025 head = tmp;
1026 else
1027 {
1028 tmp->head = head;
1029 prev->more = tmp;
1030 }
1031
1032 prev = tmp;
1033 tmp->x = op->x + x + tmp->arch->x;
1034 tmp->y = op->y + y + tmp->arch->y;
1035 tmp->map = op->map;
1036 }
1037
1038 head->direction = freedir[ndir];
1039 head->stats.exp = 0;
1040 head = insert_ob_in_map (head, head->map, op, 0);
1041
1042 if (head && head->randomitems)
1043 {
1044 create_treasure (head->randomitems, head, GT_APPLY | GT_STARTEQUIP, 6, 0);
1045
1046 for (object *tmp = head->inv; tmp; tmp = tmp->below)
1047 tmp->set_flag (FLAG_DESTROY_ON_DEATH);
1048 }
1049 } /* for i < nrof */
1050
1051 return 1;
1052 }
1053
1054 /* determines if checks so pets don't attack players or other pets should be
1055 overruled by the arena petmode */
1056 int
1057 should_arena_attack (object *pet, object *owner, object *target)
1058 {
1059 object *rowner, *towner;
1060
1061 /* exit if the target, pet, or owner is null. */
1062 if ((target == NULL) || (pet == NULL) || (owner == NULL))
1063 return 0;
1064
1065 /* get the owners of itself and the target, this is to deal with pets of
1066 pets */
1067 rowner = owner->outer_owner ();
1068 towner = target->is_player () ? 0 : target->outer_owner ();
1069
1070 /* if the pet has now owner, exit with error */
1071 if (!rowner)
1072 {
1073 LOG (llevError, "Pet has no owner.\n");
1074 return 0;
1075 }
1076
1077 /* if the target is not a player, and has no owner, we shouldn't be here
1078 */
1079 if (!towner && !target->is_player ())
1080 {
1081 LOG (llevError, "Target is not a player but has no owner. We should not be here.\n");
1082 return 0;
1083 }
1084
1085 /* make sure that the owner is a player */
1086 if (!rowner->is_player ())
1087 return 0;
1088
1089 /* abort if the petmode is not arena */
1090 if (rowner->contr->petmode != pet_arena)
1091 return 0;
1092
1093 /* abort if the pet, it's owner, or the target is not on battleground */
1094 if (!(op_on_battleground (pet, NULL, NULL) && op_on_battleground (owner, NULL, NULL) && op_on_battleground (target, NULL, NULL)))
1095 return 0;
1096
1097 /* if the target is a monster, make sure it's owner is not the same */
1098 if (!target->is_player () && rowner == towner)
1099 return 0;
1100
1101 /* check if the target is a player which affects how it will handle
1102 parties */
1103 if (!target->is_player ())
1104 {
1105 /* if the target is owned by a player make sure than make sure
1106 it's not in the same party */
1107 if (towner->is_player () && rowner->contr->party)
1108 if (rowner->contr->party == towner->contr->party)
1109 return 0;
1110 }
1111 else
1112 {
1113 /* if the target is a player make sure than make sure it's not
1114 in the same party */
1115 if (rowner->contr->party)
1116 if (rowner->contr->party == target->contr->party)
1117 return 0;
1118 }
1119
1120 return 1;
1121 }
1122