ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.54
Committed: Fri Oct 16 00:30:19 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82
Changes since 1.53: +2 -5 lines
Log Message:
-MAX_BUF

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