ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.67
Committed: Sat Apr 23 04:56:56 2011 UTC (13 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.66: +1 -1 lines
Log Message:
update copyright to 2011

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