ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.47
Committed: Mon Sep 29 10:20:49 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.46: +15 -8 lines
Log Message:
do the same everywhere else

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.41 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.38 * 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 root 1.41 * The authors can be reached via e-mail to <support@deliantra.net>
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 root 1.43 object *tmp, *attacker, *tmp3;
37 root 1.4 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 root 1.43 object *owner = pet->owner;
47    
48     if (!owner)
49 root 1.4 {
50 root 1.43 /* the owner is no longer around, so the
51     * pet no longer needs to be friendly.
52 root 1.4 */
53 root 1.43 remove_friendly_object (pet);
54     pet->attack_movement &= ~PETMOVE;
55     return 0;
56 root 1.4 }
57 root 1.43
58     /* If the owner has turned on the pet, make the pet
59     * unfriendly.
60     */
61     if (check_enemy (owner, rv) == pet)
62 root 1.4 {
63     remove_friendly_object (pet);
64     pet->attack_movement &= ~PETMOVE;
65 root 1.43 return owner;
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.47 {
260     op->destroy_inv (true); // be explicit about dropping
261     ob->destroy (true);
262     }
263 elmex 1.1 }
264     }
265    
266     /*
267     * Unfortunately, sometimes, the owner of a pet is in the
268     * process of entering a new map when this is called.
269     * Thus the map isn't loaded yet, and we have to remove
270     * the pet...
271     * Interesting enough, we don't use the passed map structure in
272     * this function.
273     */
274 root 1.4 void
275 root 1.11 remove_all_pets (maptile *map)
276 root 1.4 {
277 elmex 1.1 objectlink *obl, *next;
278     object *owner;
279    
280 root 1.24 for (obl = first_friendly_object; obl; obl = next)
281 elmex 1.1 {
282 root 1.4 next = obl->next;
283 root 1.24
284     if (obl->ob->type != PLAYER
285     && QUERY_FLAG (obl->ob, FLAG_FRIENDLY)
286     && (owner = obl->ob->owner) != 0
287     && !on_same_map (owner, obl->ob))
288 root 1.4 {
289     /* follow owner checks map status for us */
290     follow_owner (obl->ob, owner);
291     }
292 elmex 1.1 }
293     }
294    
295 root 1.4 int
296     follow_owner (object *ob, object *owner)
297     {
298 elmex 1.28 if (owner->flag [FLAG_REMOVED])
299     return 0; // do nothing if the owner is removed
300 root 1.44 else if (!owner->map || owner->map->in_memory != MAP_ACTIVE)
301 root 1.33 LOG (llevError, "owner '%s' of the pet '%s' not on a map in memory!?\n", &owner->name, &ob->name);
302 root 1.24 else
303     {
304     int dir = find_free_spot (ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE);
305 elmex 1.1
306 root 1.24 if (dir >= 0)
307     {
308     owner->map->insert (ob, owner->x + freearr_x[dir], owner->y + freearr_y[dir]);
309 elmex 1.1
310 root 1.24 if (owner->type == PLAYER) /* Uh, I hope this is always true... */
311     new_draw_info (NDI_UNIQUE, 0, owner, "Your pet magically appears next to you");
312 elmex 1.1
313 root 1.24 return 0;
314 root 1.2 }
315 elmex 1.1 }
316    
317 root 1.47 ob->destroy_inv (true); // be explicit about dropping
318     ob->destroy (true);
319    
320 root 1.4 return 1;
321 elmex 1.1 }
322    
323 root 1.4 void
324     pet_move (object *ob)
325 elmex 1.1 {
326 elmex 1.28 int dir = 0, i;
327 root 1.4 sint16 dx, dy;
328     object *ob2, *owner;
329 root 1.11 maptile *m;
330 root 1.4
331     /* Check to see if player pulled out */
332 root 1.15 if ((owner = ob->owner) == NULL)
333 root 1.4 {
334 root 1.47 op->destroy_inv (true); // be explicit about dropping
335     ob->destroy (true);
336 root 1.4 LOG (llevMonster, "Pet: no owner, leaving.\n");
337     return;
338     }
339    
340 elmex 1.28 /* move monster into the owners map if not in the same map
341     * except when the owner is removed.
342     */
343     if (!on_same_map (ob, owner) && !owner->flag [FLAG_REMOVED])
344 root 1.4 {
345     follow_owner (ob, owner);
346     return;
347     }
348     /* Calculate Direction */
349     if (owner->type == PLAYER && owner->contr->petmode == pet_sad)
350     {
351     /* in S&D mode, if we have no enemy, run randomly about. */
352     for (i = 0; i < 15; i++)
353     {
354     dir = rndm (1, 8);
355     dx = ob->x + freearr_x[dir];
356     dy = ob->y + freearr_y[dir];
357     m = ob->map;
358     if (get_map_flags (ob->map, &m, dx, dy, &dx, &dy) & P_OUT_OF_MAP)
359     continue;
360     else if (OB_TYPE_MOVE_BLOCK (ob, GET_MAP_MOVE_BLOCK (m, dx, dy)))
361     continue;
362     else
363     break;
364     }
365     }
366 elmex 1.28 else if (!owner->flag [FLAG_REMOVED]) // only get vector to owner when owner not removed
367 root 1.4 {
368     struct rv_vector rv;
369 elmex 1.1
370 root 1.4 get_rangevector (ob, ob->owner, &rv, 0);
371     dir = rv.direction;
372 elmex 1.1 }
373 root 1.4 ob->direction = dir;
374 elmex 1.1
375 root 1.4 /* move_ob returns 0 if the object couldn't move. If that is the
376     * case, lets do some other work.
377     */
378     if (!(move_ob (ob, dir, ob)))
379     {
380     object *part;
381    
382     /* the failed move_ob above may destroy the pet, so check here */
383 root 1.10 if (ob->destroyed ())
384 root 1.2 return;
385 root 1.4
386     for (part = ob; part != NULL; part = part->more)
387     {
388     dx = part->x + freearr_x[dir];
389     dy = part->y + freearr_y[dir];
390     m = get_map_from_coord (part->map, &dx, &dy);
391     if (!m)
392     continue;
393    
394 root 1.16 for (ob2 = GET_MAP_OB (m, dx, dy); ob2 != NULL; ob2 = ob2->above)
395 root 1.4 {
396     object *new_ob;
397    
398     new_ob = ob2->head ? ob2->head : ob2;
399     if (new_ob == ob)
400     break;
401     if (new_ob == ob->owner)
402     return;
403 root 1.15 if (new_ob->owner == ob->owner)
404 root 1.2 break;
405 root 1.4
406     /* Hmm. Did we try to move into an enemy monster? If so,
407     * make it our enemy.
408     */
409     if (QUERY_FLAG (new_ob, FLAG_ALIVE) && !QUERY_FLAG (ob, FLAG_UNAGGRESSIVE)
410     && !QUERY_FLAG (new_ob, FLAG_UNAGGRESSIVE) && !QUERY_FLAG (new_ob, FLAG_FRIENDLY))
411     {
412    
413     ob->enemy = new_ob;
414     if (new_ob->enemy == NULL)
415     new_ob->enemy = ob;
416     return;
417     }
418     else if (new_ob->type == PLAYER)
419     {
420     new_draw_info (NDI_UNIQUE, 0, new_ob, "You stand in the way of someones pet.");
421     return;
422 root 1.2 }
423     }
424     }
425 root 1.4 /* Try a different course */
426 root 1.25 dir = absdir (dir + 4 - (rndm (5)) - (rndm (5)));
427 root 1.4 (void) move_ob (ob, dir, ob);
428 elmex 1.1 }
429 root 1.4 return;
430 elmex 1.1 }
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.47 op->destroy_inv (true); // be explicit about dropping
544     op->destroy (true);
545 root 1.4 return;
546     }
547 root 1.10
548 root 1.4 /* It would be nice to have a cleaner way of what message to print
549     * when the golem expires than these hard coded entries.
550     * Note it is intentional that a golems duration is based on its
551     * hp, and not duration
552     */
553     if (--op->stats.hp < 0)
554     {
555     if (op->msg)
556     new_draw_info (NDI_UNIQUE, 0, op->owner, op->msg);
557 root 1.12
558 root 1.47 op->destroy_inv (true); // be explicit about dropping
559     op->destroy (true);
560 root 1.4 return;
561     }
562    
563     /* Do golem attacks/movement for single & multisq golems.
564     * Assuming here that op is the 'head' object. Pass only op to
565     * move_ob (makes recursive calls to other parts)
566     * move_ob returns 0 if the creature was not able to move.
567     */
568     if (move_ob (op, op->direction, op))
569     return;
570 root 1.10
571     if (op->destroyed ())
572 root 1.4 return;
573 elmex 1.1
574 root 1.4 for (tmp = op; tmp; tmp = tmp->more)
575     {
576     sint16 x = tmp->x + freearr_x[op->direction], y = tmp->y + freearr_y[op->direction];
577     object *victim;
578 root 1.11 maptile *m;
579 root 1.4 int mflags;
580    
581     m = op->map;
582     mflags = get_map_flags (m, &m, x, y, &x, &y);
583    
584     if (mflags & P_OUT_OF_MAP)
585     continue;
586    
587 root 1.42 for (victim = GET_MAP_OB (m, x, y); victim; victim = victim->above)
588 root 1.4 if (QUERY_FLAG (victim, FLAG_ALIVE))
589     break;
590    
591     /* We used to call will_hit_self to make sure we don't
592     * hit ourselves, but that didn't work, and I don't really
593     * know if that was more efficient anyways than this.
594     * This at least works. Note that victim->head can be NULL,
595     * but since we are not trying to dereferance that pointer,
596     * that isn't a problem.
597     */
598     if (victim && victim != op && victim->head != op)
599     {
600     /* for golems with race fields, we don't attack
601     * aligned races
602     */
603 root 1.2
604 root 1.4 if (victim->race && op->race && strstr (op->race, victim->race))
605     {
606     if (op->owner)
607     new_draw_info_format (NDI_UNIQUE, 0, op->owner, "%s avoids damaging %s.", &op->name, &victim->name);
608     }
609     else if (victim == op->owner)
610     {
611     if (op->owner)
612     new_draw_info_format (NDI_UNIQUE, 0, op->owner, "%s avoids damaging you.", &op->name);
613 root 1.2 }
614 root 1.4 else
615     {
616     attack_ob (victim, op);
617     made_attack = 1;
618     }
619     } /* If victim */
620 elmex 1.1 }
621 root 1.26
622 root 1.4 if (made_attack)
623     update_object (op, UP_OBJ_FACE);
624 elmex 1.1 }
625    
626     /* this is a really stupid function when you get down and
627     * look at it. Keep it here for the time being - makes life
628     * easier if we ever decide to do more interesting thing with
629     * controlled golems.
630     */
631 root 1.4 void
632     control_golem (object *op, int dir)
633     {
634     op->direction = dir;
635 elmex 1.1 }
636    
637     /* summon golem: summons a monster for 'op'. caster is the object
638     * casting the spell, dir is the direction to place the monster,
639     * at is the archetype of the monster, and spob is the spell
640     * object. At this stage, all spob is really used for is to
641     * adjust some values in the monster.
642     */
643 root 1.4 int
644     summon_golem (object *op, object *caster, int dir, object *spob)
645     {
646     object *tmp, *god = NULL;
647     archetype *at;
648     char buf[MAX_BUF];
649    
650     /* Because there can be different golem spells, player may want to
651     * 'lose' their old golem.
652     */
653 root 1.30 if (op->type == PLAYER && op->contr->golem)
654 root 1.4 {
655     new_draw_info (NDI_UNIQUE, 0, op, "You dismiss your existing golem.");
656 root 1.47 op->contr->holem->destroy_inv (true); // be explicit about dropping
657     op->contr->golem->destroy (true);
658 root 1.30 op->contr->golem = 0;
659 root 1.4 }
660    
661     if (spob->other_arch)
662     at = spob->other_arch;
663     else if (spob->race)
664     {
665     god = find_god (determine_god (caster));
666 elmex 1.1
667 root 1.4 if (!god)
668     {
669     new_draw_info_format (NDI_UNIQUE, 0, op, "You must worship a god to cast %s.", &spob->name);
670     return 0;
671 root 1.2 }
672 root 1.6
673 root 1.4 at = determine_holy_arch (god, spob->race);
674 root 1.6
675 root 1.4 if (!at)
676     {
677     new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no %s for you to call.", &god->name, &spob->race);
678     return 0;
679 root 1.2 }
680 elmex 1.1 }
681 root 1.4 else
682     {
683     LOG (llevError, "Spell %s lacks other_arch\n", &spob->name);
684     return 0;
685     }
686    
687     if (!dir)
688 root 1.36 dir = find_free_spot (at, op->map, op->x, op->y, 1, SIZEOFFREE1 + 1);
689 root 1.4
690 root 1.40 if (dir < 0 || at->blocked (op->map, op->x + freearr_x[dir], op->y + freearr_y[dir]))
691 root 1.4 {
692     new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
693     return 0;
694     }
695 root 1.19
696 root 1.4 /* basically want to get proper map/coordinates for this object */
697 elmex 1.1
698 root 1.4 if (!(tmp = fix_summon_pet (at, op, dir, GOLEM)))
699     {
700     new_draw_info (NDI_UNIQUE, 0, op, "Your spell fails.");
701     return 0;
702     }
703 elmex 1.1
704 root 1.4 if (op->type == PLAYER)
705     {
706     tmp->type = GOLEM;
707 root 1.30 op->contr->golem = tmp;
708 root 1.4 /* give the player control of the golem */
709 root 1.29 set_spell_skill (op, caster, spob, tmp);
710 elmex 1.1 }
711    
712 root 1.4 /* make the speed positive. */
713 root 1.46 tmp->speed = fabs (tmp->speed);
714 root 1.4
715     /* This sets the level dependencies on dam and hp for monsters */
716     /* players can't cope with too strong summonings. */
717     /* but monsters can. reserve these for players. */
718     if (op->type == PLAYER)
719     {
720     tmp->stats.hp += spob->duration + SP_level_duration_adjust (caster, spob);
721 root 1.6
722 root 1.4 if (!spob->stats.dam)
723     tmp->stats.dam += SP_level_dam_adjust (caster, spob);
724     else
725     tmp->stats.dam = spob->stats.dam + SP_level_dam_adjust (caster, spob);
726 root 1.6
727 root 1.4 tmp->speed += .02 * SP_level_range_adjust (caster, spob);
728 root 1.46 tmp->speed = min (tmp->speed, 1.0);
729 root 1.6
730 root 1.4 if (spob->attacktype)
731     tmp->attacktype = spob->attacktype;
732     }
733 root 1.6
734 root 1.4 tmp->stats.wc -= SP_level_range_adjust (caster, spob);
735    
736     /* limit the speed to 0.3 for non-players, 1 for players. */
737    
738     /* make experience increase in proportion to the strength.
739     * this is a bit simplistic - we are basically just looking at how
740     * often the sp doubles and use that as the ratio.
741     */
742 root 1.46 tmp->stats.exp *= 1 + max (spob->stats.maxgrace, spob->stats.sp) / casting_level (caster, spob);
743 root 1.4 tmp->speed_left = 0;
744     tmp->direction = dir;
745    
746     /* Holy spell - some additional tailoring */
747     if (god)
748     {
749     object *tmp2;
750    
751     sprintf (buf, "%s of %s", &spob->name, &god->name);
752     buf[0] = toupper (buf[0]);
753    
754     for (tmp2 = tmp; tmp2; tmp2 = tmp2->more)
755     tmp2->name = buf;
756    
757     tmp->attacktype |= god->attacktype;
758     memcpy (tmp->resist, god->resist, sizeof (tmp->resist));
759     tmp->race = god->race;
760     tmp->slaying = god->slaying;
761 root 1.6
762 root 1.4 /* safety, we must allow a god's servants some reasonable attack */
763     if (!(tmp->attacktype & AT_PHYSICAL))
764     tmp->attacktype |= AT_PHYSICAL;
765 elmex 1.1 }
766    
767 root 1.4 insert_ob_in_map (tmp, tmp->map, op, 0);
768     return 1;
769 elmex 1.1 }
770    
771    
772     /***************************************************************************
773     *
774     * Summon monster/pet/other object code
775     *
776     ***************************************************************************/
777    
778     /* Returns a monster (chosen at random) that this particular player (and his
779     * god) find acceptable. This checks level, races allowed by god, etc
780     * to determine what is acceptable.
781     * This returns NULL if no match was found.
782     */
783    
784 root 1.4 object *
785     choose_cult_monster (object *pl, object *god, int summon_level)
786     {
787     char buf[MAX_BUF];
788     const char *race;
789     int racenr, mon_nr, i;
790     racelink *list;
791     objectlink *tobl;
792     object *otmp;
793    
794     /* Determine the number of races available */
795     racenr = 0;
796     strcpy (buf, god->race);
797     race = strtok (buf, ",");
798     while (race)
799     {
800     racenr++;
801     race = strtok (NULL, ",");
802     }
803    
804     /* next, randomly select a race from the aligned_races string */
805     if (racenr > 1)
806     {
807     racenr = rndm (0, racenr - 1);
808     strcpy (buf, god->race);
809     race = strtok (buf, ",");
810     for (i = 0; i < racenr; i++)
811     race = strtok (NULL, ",");
812     }
813     else
814     race = god->race;
815    
816    
817     /* see if a we can match a race list of monsters. This should not
818     * happen, so graceful recovery isn't really needed, but this sanity
819     * checking is good for cases where the god archetypes mismatch the
820     * race file
821     */
822     if ((list = find_racelink (race)) == NULL)
823     {
824     new_draw_info_format (NDI_UNIQUE, 0, pl, "The spell fails! %s's creatures are beyond the range of your summons", &god->name);
825     LOG (llevDebug, "choose_cult_monster() requested non-existent aligned race!\n");
826     return 0;
827     }
828    
829     /* search for an apprplritate monster on this race list */
830     mon_nr = 0;
831     for (tobl = list->member; tobl; tobl = tobl->next)
832     {
833     otmp = tobl->ob;
834     if (!otmp || !QUERY_FLAG (otmp, FLAG_MONSTER))
835     continue;
836     if (otmp->level <= summon_level)
837     mon_nr++;
838 elmex 1.1 }
839 root 1.4
840     /* If this god has multiple race entries, we should really choose another.
841     * But then we either need to track which ones we have tried, or just
842     * make so many calls to this function, and if we get so many without
843     * a valid entry, assuming nothing is available and quit.
844     */
845     if (!mon_nr)
846 elmex 1.1 return NULL;
847 root 1.4
848     mon_nr = rndm (0, mon_nr - 1);
849     for (tobl = list->member; tobl; tobl = tobl->next)
850     {
851     otmp = tobl->ob;
852     if (!otmp || !QUERY_FLAG (otmp, FLAG_MONSTER))
853     continue;
854     if (otmp->level <= summon_level && !mon_nr--)
855     return otmp;
856     }
857     /* This should not happen */
858     LOG (llevDebug, "choose_cult_monster() mon_nr was set, but did not find a monster\n");
859     return NULL;
860 elmex 1.1 }
861    
862 root 1.4 int
863     summon_object (object *op, object *caster, object *spell_ob, int dir, const char *stringarg)
864 elmex 1.1 {
865 root 1.4 sint16 x, y, nrof = 1, i;
866     archetype *summon_arch;
867     int ndir;
868    
869     if (spell_ob->other_arch)
870 root 1.5 summon_arch = spell_ob->other_arch;
871 root 1.4 else if (spell_ob->randomitems)
872     {
873 root 1.46 int level = casting_level (caster, spell_ob);
874 root 1.5 treasure *tr, *lasttr = NULL;
875 root 1.4
876 root 1.5 shstr_cmp sparam (stringarg);
877    
878     /* In old code, this was a very convoluted for statement,
879 root 1.4 * with all the checks in the 'for' portion itself. Much
880     * more readable to break some of the conditions out.
881     */
882     for (tr = spell_ob->randomitems->items; tr; tr = tr->next)
883     {
884 elmex 1.18 if (!tr->item)
885     continue;
886    
887 root 1.4 if (level < tr->magic)
888     break;
889 root 1.5
890 root 1.4 lasttr = tr;
891 root 1.5
892 root 1.35 if (tr->item->archname == sparam)
893 root 1.4 break;
894     }
895 root 1.5
896 root 1.4 if (!lasttr)
897     {
898     LOG (llevError, "Treasurelist %s did not generate a valid entry in summon_object\n", &spell_ob->randomitems->name);
899     new_draw_info (NDI_UNIQUE, 0, op, "The spell fails to summon any monsters.");
900     return 0;
901     }
902 root 1.5
903 root 1.4 summon_arch = lasttr->item;
904 elmex 1.18 nrof = lasttr->nrof;
905 root 1.4 }
906     else if (spell_ob->race && !strcmp (spell_ob->race, "GODCULTMON"))
907     {
908     object *god = find_god (determine_god (op)), *mon, *owner;
909     int summon_level, tries;
910    
911 root 1.15 if (!god && ((owner = op->owner) != NULL))
912 root 1.5 god = find_god (determine_god (owner));
913    
914 root 1.4 /* If we can't find a god, can't get what monster to summon */
915     if (!god)
916     return 0;
917 elmex 1.1
918 root 1.4 if (!god->race)
919     {
920     new_draw_info_format (NDI_UNIQUE, 0, op, "%s has no creatures that you may summon!", &god->name);
921     return 0;
922 root 1.2 }
923 root 1.5
924 root 1.4 /* the summon level */
925 root 1.46 summon_level = casting_level (caster, spell_ob);
926 root 1.4 if (summon_level == 0)
927     summon_level = 1;
928 root 1.5
929 root 1.4 tries = 0;
930     do
931     {
932     mon = choose_cult_monster (op, god, summon_level);
933     if (!mon)
934     {
935     new_draw_info_format (NDI_UNIQUE, 0, op, "%s fails to send anything.", &god->name);
936     return 0;
937     }
938 root 1.5
939 root 1.4 ndir = dir;
940 root 1.5
941 root 1.4 if (!ndir)
942     ndir = find_free_spot (mon, op->map, op->x, op->y, 1, SIZEOFFREE);
943 root 1.5
944 root 1.40 if (ndir < 0 || mon->blocked (op->map, op->x + freearr_x[ndir], op->y + freearr_y[ndir]))
945 root 1.4 {
946     ndir = -1;
947     if (++tries == 5)
948     {
949     new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
950     return 0;
951 root 1.2 }
952     }
953 root 1.4 }
954     while (ndir == -1);
955 root 1.5
956 root 1.4 if (mon->level > (summon_level / 2))
957     nrof = random_roll (1, 2, op, PREFER_HIGH);
958     else
959     nrof = die_roll (2, 2, op, PREFER_HIGH);
960 root 1.5
961 root 1.4 summon_arch = mon->arch;
962     }
963     else
964 root 1.5 summon_arch = 0;
965 elmex 1.1
966 root 1.4 if (spell_ob->stats.dam)
967     nrof += spell_ob->stats.dam + SP_level_dam_adjust (caster, spell_ob);
968 elmex 1.1
969 root 1.4 if (!summon_arch)
970     {
971     new_draw_info (NDI_UNIQUE, 0, op, "There is no monsters available for summoning.");
972     return 0;
973 elmex 1.1 }
974    
975 root 1.4 for (i = 1; i <= nrof; i++)
976     {
977     object *prev = NULL, *head = NULL, *tmp;
978    
979     if (dir)
980     {
981     ndir = dir;
982     dir = absdir (dir + 1);
983     }
984     else
985 root 1.36 ndir = find_free_spot (summon_arch, op->map, op->x, op->y, 1, SIZEOFFREE);
986 root 1.4
987     if (ndir > 0)
988     {
989     x = freearr_x[ndir];
990     y = freearr_y[ndir];
991     }
992    
993 root 1.40 if (ndir < 0 || summon_arch->blocked (op->map, op->x + x, op->y + y))
994 root 1.4 {
995     new_draw_info (NDI_UNIQUE, 0, op, "There is something in the way.");
996     if (nrof > 1)
997     new_draw_info (NDI_UNIQUE, 0, op, "No more pets for this casting.");
998    
999     return nrof > 1;
1000     }
1001    
1002 root 1.37 for (archetype *atmp = summon_arch; atmp != NULL; atmp = (archetype *)atmp->more)
1003 root 1.4 {
1004     tmp = arch_to_object (atmp);
1005     if (atmp == summon_arch)
1006     {
1007     if (QUERY_FLAG (tmp, FLAG_MONSTER))
1008     {
1009 root 1.15 tmp->set_owner (op);
1010 root 1.4 set_spell_skill (op, caster, spell_ob, tmp);
1011     tmp->enemy = op->enemy;
1012     tmp->type = 0;
1013     CLEAR_FLAG (tmp, FLAG_SLEEP);
1014 root 1.5
1015 root 1.4 if (op->type == PLAYER || QUERY_FLAG (op, FLAG_FRIENDLY))
1016     {
1017     /* If this is not set, we make it friendly */
1018     if (!QUERY_FLAG (spell_ob, FLAG_MONSTER))
1019     {
1020     add_friendly_object (tmp);
1021     tmp->stats.exp = 0;
1022 root 1.5
1023 root 1.4 if (spell_ob->attack_movement)
1024     tmp->attack_movement = spell_ob->attack_movement;
1025 root 1.5
1026 root 1.15 if (op->owner)
1027     tmp->set_owner (op->owner);
1028 root 1.2 }
1029     }
1030     }
1031 root 1.5
1032 root 1.4 if (tmp->speed > MIN_ACTIVE_SPEED)
1033     tmp->speed_left = -1;
1034     }
1035 root 1.5
1036 root 1.4 if (head == NULL)
1037     head = tmp;
1038     else
1039     {
1040     tmp->head = head;
1041     prev->more = tmp;
1042 root 1.2 }
1043 root 1.5
1044 root 1.4 prev = tmp;
1045 root 1.36 tmp->x = op->x + x + tmp->arch->x;
1046     tmp->y = op->y + y + tmp->arch->y;
1047 root 1.4 tmp->map = op->map;
1048     }
1049 root 1.5
1050 root 1.4 head->direction = freedir[ndir];
1051     head->stats.exp = 0;
1052     head = insert_ob_in_map (head, head->map, op, 0);
1053 root 1.5
1054 root 1.4 if (head && head->randomitems)
1055     {
1056 root 1.19 create_treasure (head->randomitems, head, GT_APPLY | GT_STARTEQUIP, 6, 0);
1057 root 1.4
1058 root 1.19 for (object *tmp = head->inv; tmp; tmp = tmp->below)
1059 root 1.21 SET_FLAG (tmp, FLAG_DESTROY_ON_DEATH);
1060 root 1.2 }
1061 root 1.4 } /* for i < nrof */
1062 root 1.5
1063 root 1.4 return 1;
1064 elmex 1.1 }
1065    
1066     /* recursively look through the owner property of objects until the real owner
1067     is found */
1068 root 1.4 object *
1069     get_real_owner (object *ob)
1070     {
1071     object *realowner = ob;
1072    
1073     if (realowner == NULL)
1074     return NULL;
1075    
1076 root 1.15 while (realowner->owner != NULL)
1077 root 1.4 {
1078 root 1.15 realowner = realowner->owner;
1079 root 1.4 }
1080     return realowner;
1081 elmex 1.1 }
1082 root 1.4
1083 elmex 1.1 /* determines if checks so pets don't attack players or other pets should be
1084     overruled by the arena petmode */
1085 root 1.4 int
1086     should_arena_attack (object *pet, object *owner, object *target)
1087     {
1088     object *rowner, *towner;
1089    
1090     /* exit if the target, pet, or owner is null. */
1091     if ((target == NULL) || (pet == NULL) || (owner == NULL))
1092     return 0;
1093    
1094     /* get the owners of itself and the target, this is to deal with pets of
1095     pets */
1096     rowner = get_real_owner (owner);
1097 root 1.29 towner = target->type == PLAYER ? 0 : get_real_owner (target);
1098 root 1.4
1099     /* if the pet has now owner, exit with error */
1100 root 1.29 if (!rowner)
1101 root 1.4 {
1102     LOG (llevError, "Pet has no owner.\n");
1103     return 0;
1104     }
1105    
1106     /* if the target is not a player, and has no owner, we shouldn't be here
1107     */
1108     if ((towner == NULL) && (target->type != PLAYER))
1109     {
1110     LOG (llevError, "Target is not a player but has no owner. We should not be here.\n");
1111     return 0;
1112     }
1113    
1114     /* make sure that the owner is a player */
1115     if (rowner->type != PLAYER)
1116     return 0;
1117    
1118     /* abort if the petmode is not arena */
1119     if (rowner->contr->petmode != pet_arena)
1120     return 0;
1121    
1122     /* abort if the pet, it's owner, or the target is not on battleground */
1123     if (!(op_on_battleground (pet, NULL, NULL) && op_on_battleground (owner, NULL, NULL) && op_on_battleground (target, NULL, NULL)))
1124     return 0;
1125    
1126     /* if the target is a monster, make sure it's owner is not the same */
1127     if ((target->type != PLAYER) && (rowner == towner))
1128     return 0;
1129    
1130     /* check if the target is a player which affects how it will handle
1131     parties */
1132     if (target->type != PLAYER)
1133     {
1134     /* if the target is owned by a player make sure than make sure
1135     it's not in the same party */
1136     if ((towner->type == PLAYER) && (rowner->contr->party != NULL))
1137     {
1138     if (rowner->contr->party == towner->contr->party)
1139     return 0;
1140     }
1141     }
1142     else
1143     {
1144     /* if the target is a player make sure than make sure it's not
1145     in the same party */
1146     if (rowner->contr->party != NULL)
1147     {
1148     if (rowner->contr->party == target->contr->party)
1149     return 0;
1150 root 1.2 }
1151 root 1.4 }
1152    
1153     return 1;
1154 elmex 1.1 }