ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.9
Committed: Thu Sep 14 22:34:04 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +1 -7 lines
Log Message:
indent

File Contents

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