ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.11
Committed: Sat Sep 16 22:24:13 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +4 -4 lines
Log Message:
mapstruct => maptile
removed many ytypedefs in favor of structure tags

File Contents

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