ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.74
Committed: Sat Nov 17 23:40:04 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.73: +1 -0 lines
Log Message:
copyright update 2018

File Contents

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