ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.36
Committed: Mon Jun 4 13:04:00 2007 UTC (16 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.35: +8 -8 lines
Log Message:
- archetype is now a subclass of object.
- store archetypes in an object_vector.
- use a different startegy for archetype loading
  (reloading is MOST CERTAINLY broken).

File Contents

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