ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.8
Committed: Thu Sep 14 17:10:25 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +10 -13 lines
Log Message:
removed now obsolete generation counter checks - further slimmed down sizeof(objetc) to 616 bytes

File Contents

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