ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.20
Committed: Sat Jan 6 14:42:31 2007 UTC (17 years, 5 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.19: +1 -0 lines
Log Message:
added some copyrights

File Contents

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