ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.6
Committed: Tue Sep 12 20:55:18 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +10 -2 lines
Log Message:
indent

File Contents

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