ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.40
Committed: Wed Sep 12 11:10:11 2007 UTC (16 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_3
Changes since 1.39: +3 -3 lines
Log Message:
- rewrite/cleanup ob_blocked a little. its more efficient now, probably
  has less bugs and more bugs.
- get rid of now unused P_NO_PASS.

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