ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.54
Committed: Fri Oct 16 00:30:19 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82
Changes since 1.53: +2 -5 lines
Log Message:
-MAX_BUF

File Contents

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