ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.14
Committed: Tue Dec 12 21:39:57 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +6 -6 lines
Log Message:
- more ooficiation
- removed now superfluous remove calls

File Contents

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