ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.24
Committed: Thu Jan 18 16:19:34 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.23: +22 -43 lines
Log Message:
- fix a horrendous bug that might have caused all the map corruption
- optimise/modernise some map-insert-related stuff
- fix debug_desc
- remove crypt configury
- minor adjustments/cleanups

File Contents

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