ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/pets.C
Revision: 1.19
Committed: Wed Jan 3 20:32:13 2007 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.18: +19 -12 lines
Log Message:
set no_drop flag on all inv items of created objects (e.g. pets), experimental.

File Contents

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