ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.5
Committed: Tue Sep 12 18:15:35 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +32 -17 lines
Log Message:
- introduce shstr_cmp for mass comparisons to shstr and make use of it
- introduce assign utility function to replace strncpy

File Contents

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