ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.48
Committed: Mon Sep 29 10:31:32 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_72, rel-2_73
Changes since 1.47: +6 -14 lines
Log Message:
introduce drop_and_destroy and use it

File Contents

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