ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.28
Committed: Mon Apr 16 11:50:45 2007 UTC (17 years, 2 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.27: +92 -92 lines
Log Message:
finally fixed the inactive pet monsters and the disappearing pet monsters
of traps/runes and doors. yay!

File Contents

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