ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.22
Committed: Mon Jan 8 01:19:04 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.21: +0 -4 lines
Log Message:
more preperations for player eviction

File Contents

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