ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.16
Committed: Wed Dec 20 09:14:22 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.15: +4 -4 lines
Log Message:
- minor cleanups
- minor optimisations (in_player vs. is_player_inv)
- added P_PLAYER map flag
- some (dead) concept code

File Contents

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