Crossfire Server, Trunk
pets.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
19 #include "global.h"
20 
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "sproto.h"
27 
34 static void mark_inventory_as_no_drop(object *ob) {
38 }
39 
54 object *pets_get_enemy(object *pet, rv_vector *rv) {
55  object *owner, *tmp, *attacker, *tmp3;
56  int i;
57  int16_t x, y;
58  mapstruct *nm;
59  int search_arr[SIZEOFFREE];
60  int mflags;
61 
62  attacker = pet->attacked_by; /*pointer to attacking enemy*/
63  pet->attacked_by = NULL; /*clear this, since we are dealing with it*/
64 
65  owner = object_get_owner(pet);
66  if (owner != NULL) {
67  /* If the owner has turned on the pet, make the pet
68  * unfriendly.
69  */
70  if (monster_check_enemy(owner, rv) == pet) {
73  pet->attack_movement &= ~PETMOVE;
74  return owner;
75  }
76  } else {
77  /* else the owner is no longer around, so the
78  * pet no longer needs to be friendly.
79  */
82  pet->attack_movement &= ~PETMOVE;
83  return NULL;
84  }
85  /* If they are not on the same map, the pet won't be agressive */
86  if (!on_same_map(pet, owner))
87  return NULL;
88 
89  /* See if the pet has an existing enemy. If so, don't start a new one*/
90  tmp = monster_check_enemy(pet, rv);
91  if (tmp != NULL) {
92  if (tmp != owner || QUERY_FLAG(pet, FLAG_CONFUSED) || !QUERY_FLAG(pet, FLAG_FRIENDLY))
93  return tmp;
94 
95  /* without this check, you can actually get pets with
96  * enemy set to owner!
97  */
98  object_set_enemy(pet, NULL);
99  }
100  get_search_arr(search_arr);
101 
102  if (owner->type == PLAYER && owner->contr->petmode == pet_sad) {
104  if (tmp != NULL && get_rangevector(pet, tmp, rv, 0))
105  return tmp;
106  /* if we got here we still have no enemy */
107  /* we return NULL to avoid heading back to the owner */
108  object_set_enemy(pet, NULL);
109  return NULL;
110  }
111 
112  /* Since the pet has no existing enemy, look for anything nasty
113  * around the owner that it should go and attack.
114  */
115  tmp3 = NULL;
116  for (i = 0; i < SIZEOFFREE; i++) {
117  x = owner->x+freearr_x[search_arr[i]];
118  y = owner->y+freearr_y[search_arr[i]];
119  nm = owner->map;
120  /* Only look on the space if there is something alive there. */
121  mflags = get_map_flags(nm, &nm, x, y, &x, &y);
122  if (!(mflags&P_OUT_OF_MAP) && mflags&P_IS_ALIVE) {
123  FOR_MAP_PREPARE(nm, x, y, tmp) {
124  object *tmp2 = HEAD(tmp);
125 
126  if (QUERY_FLAG(tmp2, FLAG_ALIVE)
127  && ((!QUERY_FLAG(tmp2, FLAG_FRIENDLY) && tmp2->type != PLAYER) || pets_should_arena_attack(pet, owner, tmp2))
128  && !QUERY_FLAG(tmp2, FLAG_UNAGGRESSIVE)
129  && tmp2 != pet
130  && tmp2 != owner
131  && monster_can_detect_enemy(pet, tmp2, rv)) {
132  if (!monster_can_see_enemy(pet, tmp2)) {
133  if (tmp3 == NULL)
134  tmp3 = tmp2;
135  } else {
136  object_set_enemy(pet, tmp2);
137  if (monster_check_enemy(pet, rv) != NULL)
138  return tmp2;
139  object_set_enemy(pet, NULL);
140  }
141  }/* if this is a valid enemy */
142  } FOR_MAP_FINISH();/* for objects on this space */
143  }/* if there is something living on this space */
144  } /* for loop of spaces around the owner */
145 
146  /* fine, we went through the whole loop and didn't find one we could
147  see, take what we have */
148  if (tmp3 != NULL) {
149  object_set_enemy(pet, tmp3);
150  if (monster_check_enemy(pet, rv) != NULL)
151  return tmp3;
152  object_set_enemy(pet, NULL);
153  }
154 
155  /* No threat to owner, check to see if the pet has an attacker*/
156  if (attacker != NULL) {
157  /* need to be sure this is the right one! */
158  if (attacker->count == pet->attacked_by_count) {
159  /* also need to check to make sure it is not freindly */
160  /* or otherwise non-hostile, and is an appropriate target */
161  if (!QUERY_FLAG(attacker, FLAG_FRIENDLY) && on_same_map(pet, attacker)) {
162  object_set_enemy(pet, attacker);
163  if (monster_check_enemy(pet, rv) != NULL)
164  return attacker;
165  object_set_enemy(pet, NULL);
166  }
167  }
168  }
169 
170  /* Don't have an attacker or legal enemy, so look for a new one!.
171  * This looks for one around where the pet is. Thus, you could lead
172  * a pet to danger, then take a few steps back. This code is basically
173  * the same as the code that looks around the owner.
174  */
175  if (owner->type == PLAYER && owner->contr->petmode != pet_defend) {
176  tmp3 = NULL;
177  for (i = 0; i < SIZEOFFREE; i++) {
178  x = pet->x+freearr_x[search_arr[i]];
179  y = pet->y+freearr_y[search_arr[i]];
180  nm = pet->map;
181  /* Only look on the space if there is something alive there. */
182  mflags = get_map_flags(nm, &nm, x, y, &x, &y);
183  if (!(mflags&P_OUT_OF_MAP) && mflags&P_IS_ALIVE) {
184  FOR_MAP_PREPARE(nm, x, y, tmp) {
185  object *tmp2 = HEAD(tmp);
186  if (QUERY_FLAG(tmp2, FLAG_ALIVE)
187  && ((!QUERY_FLAG(tmp2, FLAG_FRIENDLY) && tmp2->type != PLAYER) || pets_should_arena_attack(pet, owner, tmp2))
188  && !QUERY_FLAG(tmp2, FLAG_UNAGGRESSIVE)
189  && tmp2 != pet
190  && tmp2 != owner
191  && monster_can_detect_enemy(pet, tmp2, rv)) {
192  if (!monster_can_see_enemy(pet, tmp2)) {
193  assert(tmp3 == NULL);
194  } else {
195  object_set_enemy(pet, tmp2);
196  if (monster_check_enemy(pet, rv) != NULL)
197  return tmp2;
198  object_set_enemy(pet, NULL);
199  }
200  } /* make sure we can get to the bugger */
201  } FOR_MAP_FINISH();/* for objects on this space */
202  } /* if there is something living on this space */
203  } /* for loop of spaces around the pet */
204  } /* pet in defence mode */
205 
206  /* fine, we went through the whole loop and didn't find one we could
207  see, take what we have */
208  if (tmp3 != NULL) {
209  object_set_enemy(pet, tmp3);
210  if (monster_check_enemy(pet, rv) != NULL)
211  return tmp3;
212  object_set_enemy(pet, NULL);
213  }
214 
215  /* Didn't find anything - return the owner's enemy or NULL */
216  return monster_check_enemy(pet, rv);
217 }
218 
225 void pets_terminate_all(object *owner) {
226  objectlink *obl, *next;
227 
228  for (obl = first_friendly_object; obl != NULL; obl = next) {
229  object *ob = obl->ob;
230  next = obl->next;
231  if (object_get_owner(ob) == owner) {
232  if (!QUERY_FLAG(ob, FLAG_REMOVED))
233  object_remove(ob);
236  }
237  }
238 }
239 
248 void pets_attempt_follow(object *for_owner, int force) {
249  objectlink *obl, *next;
250  object *owner;
251 
252  for (obl = first_friendly_object; obl != NULL; obl = next) {
253  next = obl->next;
254  if (obl->ob->type != PLAYER
255  && QUERY_FLAG(obl->ob, FLAG_FRIENDLY)
256  && (owner = object_get_owner(obl->ob)) != NULL
257  && (for_owner == NULL || for_owner == owner)
258  && (force || !on_same_map(owner, obl->ob))) {
259  /* follow owner checks map status for us. Note that pet can
260  * die in pets_follow_owner(), so check for obl->ob existence
261  */
262  pets_follow_owner(obl->ob, owner);
263  if (obl->ob && QUERY_FLAG(obl->ob, FLAG_REMOVED) && FABS(obl->ob->speed) > MIN_ACTIVE_SPEED) {
264  object *ob = obl->ob;
265 
266  LOG(llevMonster, "(pet failed to follow)\n");
269  }
270  }
271  }
272 }
273 
282 void pets_follow_owner(object *ob, object *owner) {
283  int dir;
284 
285  if (!QUERY_FLAG(ob, FLAG_REMOVED))
286  object_remove(ob);
287 
288  if (owner->map == NULL) {
289  LOG(llevError, "Can't follow owner: no map.\n");
290  return;
291  }
292  if (owner->map->in_memory != MAP_IN_MEMORY) {
293  LOG(llevError, "Owner of the pet not on a map in memory!?\n");
294  return;
295  }
296  dir = object_find_free_spot(ob, owner->map, owner->x, owner->y, 1, SIZEOFFREE);
297 
298  if (dir == -1) {
299  LOG(llevMonster, "No space for pet to follow, freeing %s.\n", ob->name);
300  return; /* Will be freed since it's removed */
301  }
302  object_insert_in_map_at(ob, owner->map, NULL, 0, owner->x+freearr_x[dir], owner->y+freearr_y[dir]);
303  if (owner->type == PLAYER) /* Uh, I hope this is always true... */
305  "Your pet magically appears next to you");
306  return;
307 }
308 
315 void pets_move(object *ob) {
316  int dir, i;
317  tag_t tag;
318  int16_t dx, dy;
319  object *owner;
320  mapstruct *m;
321 
322  /* Check to see if player pulled out */
323  owner = object_get_owner(ob);
324  if (owner == NULL) {
325  object_remove(ob); /* Will be freed when returning */
328  LOG(llevMonster, "Pet: no owner, leaving.\n");
329  return;
330  }
331 
332  /* move monster into the owners map if not in the same map */
333  if (!on_same_map(ob, owner)) {
334  pets_follow_owner(ob, owner);
335  return;
336  }
337  /* Calculate Direction */
338  if (owner->type == PLAYER && owner->contr->petmode == pet_sad) {
339  /* in S&D mode, if we have no enemy, run randomly about. */
340  for (i = 0; i < 15; i++) {
341  dir = get_random_dir();
342  dx = ob->x+freearr_x[dir];
343  dy = ob->y+freearr_y[dir];
344  m = ob->map;
345  if (!(get_map_flags(ob->map, &m, dx, dy, &dx, &dy)&P_OUT_OF_MAP)
346  && !OB_TYPE_MOVE_BLOCK(ob, GET_MAP_MOVE_BLOCK(m, dx, dy)))
347  break;
348  }
349  } else {
350  rv_vector rv;
351 
352  if (get_rangevector(ob, owner, &rv, 0))
353  dir = rv.direction;
354  else
355  dir = get_random_dir();
356  }
357  ob->direction = dir;
358 
359  tag = ob->count;
360  /* move_ob returns 0 if the object couldn't move. If that is the
361  * case, lets do some other work.
362  */
363  if (!move_ob(ob, dir, ob)) {
364  object *part;
365 
366  /* the failed move_ob above may destroy the pet, so check here */
368  return;
369 
370  for (part = ob; part != NULL; part = part->more) {
371  dx = part->x+freearr_x[dir];
372  dy = part->y+freearr_y[dir];
373  m = get_map_from_coord(part->map, &dx, &dy);
374  if (m == NULL)
375  continue;
376 
377  FOR_MAP_PREPARE(m, dx, dy, ob2) {
378  object *new_ob;
379 
380  new_ob = HEAD(ob2);
381  if (new_ob == ob)
382  break;
383  if (new_ob == owner)
384  return;
385  if (object_get_owner(new_ob) == owner)
386  break;
387 
388  /* Hmm. Did we try to move into an enemy monster? If so,
389  * make it our enemy.
390  */
391  if (QUERY_FLAG(new_ob, FLAG_ALIVE)
393  && !QUERY_FLAG(new_ob, FLAG_UNAGGRESSIVE)
394  && !QUERY_FLAG(new_ob, FLAG_FRIENDLY)) {
395  object_set_enemy(ob, new_ob);
396  if (new_ob->enemy == NULL)
397  object_set_enemy(new_ob, ob);
398  return;
399  } else if (new_ob->type == PLAYER) {
400  draw_ext_info(NDI_UNIQUE, 0, new_ob,
402  "You stand in the way of someones pet.");
403  return;
404  }
405  } FOR_MAP_FINISH();
406  }
407  /* Try a different course */
408  dir = absdir(dir+4-(RANDOM()%5)-(RANDOM()%5));
409  (void)move_ob(ob, dir, ob);
410  }
411  return;
412 }
413 
414 /****************************************************************************
415  *
416  * GOLEM SPELL CODE
417  *
418  ****************************************************************************/
419 
432 static object *fix_summon_pet(archetype *at, object *op, int dir) {
433  archetype *atmp;
434  object *tmp = NULL, *prev = NULL, *head = NULL;
435 
436  for (atmp = at; atmp != NULL; atmp = atmp->more) {
437  tmp = arch_to_object(atmp);
438  if (atmp == at) {
439 
440  /* Ensure the golem can actually move if no move_type defined.
441  * This check is redundant since this is checked at server startup. */
442  if (tmp->move_type == 0) {
443  LOG(llevError, "summoned %s [%s] is without move_type!\n", tmp->name, atmp->name);
444  tmp->move_type = MOVE_WALK;
445  }
446 
448  if (op->type == PLAYER) {
449  tmp->stats.exp = 0;
453  } else if (QUERY_FLAG(op, FLAG_FRIENDLY)) {
454  object *owner = object_get_owner(op);
455 
456  if (owner != NULL) {/* For now, we transfer ownership */
457  object_set_owner(tmp, owner);
458  tmp->attack_movement = PETMOVE;
461  }
462  }
463  if (op->type != PLAYER) {
464  tmp->attack_movement = PETMOVE;
465  tmp->speed_left = -1;
466  tmp->type = 0;
467  object_set_enemy(tmp, op->enemy);
468  } else
469  tmp->type = GOLEM;
470  }
471  if (head == NULL)
472  head = tmp;
473  tmp->x = op->x+freearr_x[dir]+tmp->arch->clone.x;
474  tmp->y = op->y+freearr_y[dir]+tmp->arch->clone.y;
475  tmp->map = op->map;
476  if (tmp->invisible)
477  tmp->invisible = 0;
478  if (head != tmp)
479  tmp->head = head,
480  prev->more = tmp;
481  prev = tmp;
482  }
483  head->direction = dir;
484 
485  if (head->randomitems) {
486  create_treasure(head->randomitems, head, GT_STARTEQUIP, 6, 0);
487  if (QUERY_FLAG(head, FLAG_MONSTER)) {
489  }
490  }
492 
493  /* need to change some monster attr to prevent problems/crashing */
494  head->last_heal = 0;
495  head->last_eat = 0;
496  head->last_grace = 0;
497  head->last_sp = 0;
498  head->other_arch = NULL;
499  head->stats.exp = 0;
500  CLEAR_FLAG(head, FLAG_CHANGING);
502  CLEAR_FLAG(head, FLAG_GENERATOR);
503  CLEAR_FLAG(head, FLAG_SPLITTING);
504  if (head->attacktype&AT_GHOSTHIT)
505  head->attacktype = (AT_PHYSICAL|AT_DRAIN);
506 
507  return head;
508 }
509 
517 void pets_move_golem(object *op) {
518  int made_attack = 0;
519  object *tmp;
520  tag_t tag;
521  object *owner;
522 
523  if (QUERY_FLAG(op, FLAG_MONSTER))
524  return; /* Has already been moved */
525 
526  owner = object_get_owner(op);
527  if (owner == NULL) {
528  LOG(llevDebug, "Golem without owner destructed.\n");
529  object_remove(op);
531  return;
532  }
533  /* It would be nice to have a cleaner way of what message to print
534  * when the golem expires than these hard coded entries.
535  * Note it is intentional that a golems duration is based on its
536  * hp, and not duration
537  */
538  if (--op->stats.hp < 0) {
539  if (op->msg != NULL)
541  op->msg);
542  owner->contr->ranges[range_golem] = NULL;
543  owner->contr->golem_count = 0;
545  object_remove(op);
547  return;
548  }
549 
550  /* Do golem attacks/movement for single & multisq golems.
551  * Assuming here that op is the 'head' object. Pass only op to
552  * move_ob (makes recursive calls to other parts)
553  * move_ob returns 0 if the creature was not able to move.
554  */
555  tag = op->count;
556  if (move_ob(op, op->direction, op))
557  return;
559  return;
560 
561  for (tmp = op; tmp; tmp = tmp->more) {
562  int16_t x = tmp->x+DIRX(op), y = tmp->y+DIRY(op);
563  object *victim;
564  mapstruct *m;
565  int mflags;
566 
567  m = op->map;
568  mflags = get_map_flags(m, &m, x, y, &x, &y);
569 
570  if (mflags&P_OUT_OF_MAP)
571  continue;
572 
573  victim = NULL;
574  FOR_MAP_PREPARE(op->map, x, y, tmp)
575  if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
576  victim = tmp;
577  break;
578  }
579  FOR_MAP_FINISH();
580 
581  /* We used to call will_hit_self to make sure we don't
582  * hit ourselves, but that didn't work, and I don't really
583  * know if that was more efficient anyways than this.
584  * This at least works. Note that victim->head can be NULL,
585  * but since we are not trying to dereferance that pointer,
586  * that isn't a problem.
587  */
588  if (victim != NULL && victim != op && victim->head != op) {
589  /* for golems with race fields, we don't attack
590  * aligned races
591  */
592 
593  if (victim->race != NULL && op->race != NULL && strstr(op->race, victim->race)) {
594  if (owner != NULL)
597  "%s avoids damaging %s.",
598  op->name, victim->name);
599  } else if (victim == owner) {
600  if (owner != NULL)
603  "%s avoids damaging you.",
604  op->name);
605  } else {
606  attack_ob(victim, op);
607  made_attack = 1;
608  }
609  } /* If victim */
610  }
611  if (made_attack)
613 }
614 
628 void pets_control_golem(object *op, int dir) {
629  op->direction = dir;
630 }
631 
649 int pets_summon_golem(object *op, object *caster, int dir, object *spob) {
650  object *tmp;
651  const object *god = NULL;
652  archetype *at;
653  char buf[MAX_BUF];
654 
655  /* Because there can be different golem spells, player may want to
656  * 'lose' their old golem.
657  */
658  if (op->type == PLAYER
659  && op->contr->ranges[range_golem] != NULL
660  && op->contr->golem_count == op->contr->ranges[range_golem]->count) {
663  "You dismiss your existing golem.");
664  object_remove(op->contr->ranges[range_golem]);
665  object_free_drop_inventory(op->contr->ranges[range_golem]);
666  op->contr->ranges[range_golem] = NULL;
667  op->contr->golem_count = -1;
668  }
669 
670  if (spob->other_arch != NULL)
671  at = spob->other_arch;
672  else if (spob->race != NULL) {
673  god = find_god(determine_god(caster));
674  if (god == NULL) {
677  "You must worship a god to cast %s.",
678  spob->name);
679  return 0;
680  }
681 
682  at = determine_holy_arch(god, spob->race);
683  if (at == NULL) {
686  "%s has no %s for you to call.",
687  god->name, spob->race);
688  return 0;
689  }
690  } else {
691  LOG(llevError, "Spell %s lacks other_arch\n", spob->name);
692  return 0;
693  }
694 
695  if (!dir)
696  dir = object_find_free_spot(NULL, op->map, op->x, op->y, 1, SIZEOFFREE1+1);
697 
698  if (dir == -1
699  || ob_blocked(&at->clone, op->map, op->x+freearr_x[dir], op->y+freearr_y[dir])) {
702  "There is something in the way.");
703  return 0;
704  }
705  /* basically want to get proper map/coordinates for this object */
706  tmp = fix_summon_pet(at, op, dir);
707  if (tmp == NULL) {
710  "Your spell fails.");
711  return 0;
712  }
713 
714  if (op->type == PLAYER) {
715  tmp->type = GOLEM;
717  set_spell_skill(op, caster, spob, tmp);
718  op->contr->ranges[range_golem] = tmp;
719  op->contr->golem_count = tmp->count;
720  /* give the player control of the golem */
721  op->contr->shoottype = range_golem;
722  } else {
723  if (QUERY_FLAG(op, FLAG_FRIENDLY)) {
724  object *owner = object_get_owner(op);
725 
726  if (owner != NULL) { /* For now, we transfer ownership */
727  object_set_owner(tmp, owner);
728  tmp->attack_movement = PETMOVE;
730  set_spell_skill(op, caster, spob, tmp);
732  }
733  }
735  }
736 
737  /* make the speed positive.*/
738  tmp->speed = FABS(tmp->speed);
739 
740  /* This sets the level dependencies on dam and hp for monsters */
741  /* players can't cope with too strong summonings. */
742  /* but monsters can. reserve these for players. */
743  if (op->type == PLAYER) {
744  tmp->stats.hp += spob->duration+SP_level_duration_adjust(caster, spob);
745  tmp->stats.maxhp = tmp->stats.hp;
746  if (!spob->stats.dam)
747  tmp->stats.dam += SP_level_dam_adjust(caster, spob);
748  else
749  tmp->stats.dam = spob->stats.dam+SP_level_dam_adjust(caster, spob);
750  tmp->speed += .02*SP_level_range_adjust(caster, spob);
751  tmp->speed = MIN(tmp->speed, 1.0);
752  if (spob->attacktype)
753  tmp->attacktype = spob->attacktype;
754  }
755  tmp->stats.wc -= SP_level_wc_adjust(caster, spob);
756 
757  /* limit the speed to 0.3 for non-players, 1 for players. */
758 
759  /* make experience increase in proportion to the strength.
760  * this is a bit simplistic - we are basically just looking at how
761  * often the sp doubles and use that as the ratio.
762  */
763  tmp->stats.exp *= 1+(MAX(spob->stats.maxgrace, spob->stats.sp)/caster_level(caster, spob));
764  tmp->speed_left = 0;
765  tmp->direction = dir;
766 
767  /* Holy spell - some additional tailoring */
768  if (god != NULL) {
769  object *tmp2;
770 
771  snprintf(buf, sizeof(buf), "%s of %s", spob->name, god->name);
772  buf[0] = toupper(buf[0]);
773  for (tmp2 = tmp; tmp2; tmp2 = tmp2->more) {
774  if (tmp2->name != NULL)
775  free_string(tmp2->name);
776  tmp2->name = add_string(buf);
777  }
778  tmp->attacktype |= god->attacktype;
779  memcpy(tmp->resist, god->resist, sizeof(tmp->resist));
780  if (tmp->race != NULL)
781  FREE_AND_CLEAR_STR(tmp->race);
782  if (god->race != NULL)
783  tmp->race = add_string(god->race);
784  if (tmp->slaying != NULL)
785  FREE_AND_CLEAR_STR(tmp->slaying);
786  if (god->slaying != NULL)
787  tmp->slaying = add_string(god->slaying);
788  /* safety, we must allow a god's servants some reasonable attack */
789  if (!(tmp->attacktype&AT_PHYSICAL))
790  tmp->attacktype |= AT_PHYSICAL;
791  }
792 
793  object_insert_in_map_at(tmp, tmp->map, op, 0, tmp->x, tmp->y);
794  return 1;
795 }
796 
797 /***************************************************************************
798  *
799  * Summon monster/pet/other object code
800  *
801  ***************************************************************************/
802 
817 static object *choose_cult_monster(object *pl, const object *god, int summon_level) {
818  char buf[MAX_BUF];
819  const char *race;
820  int racenr, i;
821  object *otmp;
822 
823  /* Determine the number of races available */
824  racenr = 0;
825  safe_strncpy(buf, god->race, sizeof(buf));
826  race = strtok(buf, ",");
827  while (race) {
828  racenr++;
829  race = strtok(NULL, ",");
830  }
831 
832  /* next, randomly select a race from the aligned_races string */
833  if (racenr > 1) {
834  racenr = rndm(0, racenr-1);
835  safe_strncpy(buf, god->race, sizeof(buf));
836  race = strtok(buf, ",");
837  for (i = 0; i < racenr; i++)
838  race = strtok(NULL, ",");
839  } else
840  race = god->race;
841 
842  otmp = races_get_random_monster(race, summon_level);
843  if (!otmp) {
846  "The spell fails! %s's creatures are beyond the range of your summons",
847  god->name);
848  }
849  return otmp;
850 }
851 
870 int pets_summon_object(object *op, object *caster, object *spell_ob, int dir, const char *stringarg) {
871  int16_t x, y, nrof = 1, i;
872  archetype *summon_arch;
873  int ndir, mult;
874 
875  if (spell_ob->other_arch != NULL) {
876  summon_arch = spell_ob->other_arch;
877  } else if (spell_ob->randomitems != NULL) {
878  int level = caster_level(caster, spell_ob);
879  treasure *tr, *lasttr = NULL;
880 
881  /* In old code, this was a very convuluted for statement,
882  * with all the checks in the 'for' portion itself. Much
883  * more readable to break some of the conditions out.
884  */
885  for (tr = spell_ob->randomitems->items; tr; tr = tr->next) {
886  if (level < tr->magic)
887  break;
888  lasttr = tr;
889  if (stringarg != NULL && !strcmp(tr->item->name, stringarg))
890  break;
891  if (tr->next == NULL || tr->next->item == NULL)
892  break;
893  }
894  if (lasttr == NULL) {
895  LOG(llevError, "Treasurelist %s did not generate a valid entry in pets_summon_object\n", spell_ob->randomitems->name);
898  "The spell fails to summon any monsters.");
899  return 0;
900  }
901  summon_arch = lasttr->item;
902  nrof = lasttr->nrof;
903  } else if (spell_ob->race != NULL && !strcmp(spell_ob->race, "GODCULTMON")) {
904  const object *god = find_god(determine_god(op));
905  object *mon, *owner;
906  int summon_level, tries;
907 
908  if (god == NULL) {
909  owner = object_get_owner(op);
910  if (owner != NULL) {
911  god = find_god(determine_god(owner));
912  }
913  }
914  /* If we can't find a god, can't get what monster to summon */
915  if (god == NULL)
916  return 0;
917 
918  if (god->race == NULL) {
921  "%s has no creatures that you may summon!",
922  god->name);
923  return 0;
924  }
925  /* the summon level */
926  summon_level = caster_level(caster, spell_ob);
927  if (summon_level == 0)
928  summon_level = 1;
929  tries = 0;
930  do {
931  mon = choose_cult_monster(op, god, summon_level);
932  if (mon == NULL) {
935  "%s fails to send anything.",
936  god->name);
937  return 0;
938  }
939  ndir = dir;
940  if (!ndir)
941  ndir = object_find_free_spot(mon, op->map, op->x, op->y, 1, SIZEOFFREE);
942  if (ndir == -1
943  || ob_blocked(mon, op->map, op->x+freearr_x[ndir], op->y+freearr_y[ndir])) {
944  ndir = -1;
945  if (++tries == 5) {
948  "There is something in the way.");
949  return 0;
950  }
951  }
952  } while (ndir == -1);
953  if (mon->level > summon_level/2)
954  nrof = random_roll(1, 2, op, PREFER_HIGH);
955  else
956  nrof = die_roll(2, 2, op, PREFER_HIGH);
957  summon_arch = mon->arch;
958  } else {
959  summon_arch = NULL;
960  }
961 
962  if (spell_ob->stats.dam)
963  nrof += spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob);
964 
965  if (summon_arch == NULL) {
967  "There is no monsters available for summoning.");
968  return 0;
969  }
970 
971  if (dir) {
972  /* Only fail if caster specified a blocked direction. */
973  x = freearr_x[dir];
974  y = freearr_y[dir];
975  if (ob_blocked(&summon_arch->clone, op->map, op->x+x, op->y+y)) {
977  "There is something in the way.");
978  return 0;
979  }
980  }
981 
982  mult = (RANDOM()%2 ? -1 : 1);
983 
984  for (i = 1; i <= nrof; i++) {
985  archetype *atmp;
986  object *prev = NULL, *head = NULL, *tmp;
987 
988  if (dir) {
989  ndir = absdir(dir+(i/2)*mult);
990  mult = -mult;
991  } else
992  ndir = object_find_free_spot(&summon_arch->clone, op->map, op->x, op->y, 1, SIZEOFFREE);
993 
994  x = ndir > 0 ? freearr_x[ndir] : 0;
995  y = ndir > 0 ? freearr_y[ndir] : 0;
996  if (ndir == -1 || ob_blocked(&summon_arch->clone, op->map, op->x+x, op->y+y))
997  continue;
998 
999  for (atmp = summon_arch; atmp != NULL; atmp = atmp->more) {
1000  tmp = arch_to_object(atmp);
1001  if (atmp == summon_arch) {
1002  if (QUERY_FLAG(tmp, FLAG_MONSTER)) {
1004  set_spell_skill(op, caster, spell_ob, tmp);
1005  object_set_enemy(tmp, op->enemy);
1006  tmp->type = 0;
1008  if (op->type == PLAYER || QUERY_FLAG(op, FLAG_FRIENDLY)) {
1009  /* If this is not set, we make it friendly */
1010  if (!QUERY_FLAG(spell_ob, FLAG_MONSTER)) {
1013  tmp->stats.exp = 0;
1014  if (spell_ob->attack_movement)
1015  tmp->attack_movement = spell_ob->attack_movement;
1016  if (object_get_owner(op) != NULL)
1018  }
1019  }
1020  }
1021  if (tmp->speed > MIN_ACTIVE_SPEED)
1022  tmp->speed_left = -1;
1023  }
1024  if (head == NULL)
1025  head = tmp;
1026  else {
1027  tmp->head = head;
1028  prev->more = tmp;
1029  }
1030  prev = tmp;
1031  }
1032  head->direction = freedir[ndir];
1033  head->stats.exp = 0;
1034  head = object_insert_in_map_at(head, op->map, op, 0, op->x+x, op->y+y);
1035  if (head != NULL && head->randomitems) {
1036  create_treasure(head->randomitems, head, GT_STARTEQUIP, 6, 0);
1037  if (QUERY_FLAG(head, FLAG_MONSTER)) {
1039  }
1040  }
1041  if (head != NULL) {
1043  }
1044  } /* for i < nrof */
1045  return 1;
1046 }
1047 
1057 static object *get_real_owner(object *ob) {
1058  object *realowner = ob;
1059 
1060  if (realowner == NULL)
1061  return NULL;
1062 
1063  while (object_get_owner(realowner) != NULL) {
1064  realowner = object_get_owner(realowner);
1065  }
1066  return realowner;
1067 }
1068 
1084 int pets_should_arena_attack(object *pet, object *owner, object *target) {
1085  object *rowner, *towner;
1086 
1087  /* exit if the target, pet, or owner is null. */
1088  if (target == NULL || pet == NULL || owner == NULL)
1089  return 0;
1090 
1091  /* get the owners of itself and the target, this is to deal with pets of
1092  pets */
1093  rowner = get_real_owner(owner);
1094  if (target->type != PLAYER) {
1095  towner = get_real_owner(target);
1096  } else {
1097  towner = NULL;
1098  }
1099 
1100  /* if the pet has no owner, exit with error */
1101  if (rowner == NULL) {
1102  LOG(llevError, "Pet has no owner.\n");
1103  return 0;
1104  }
1105 
1106  /* if the target is not a player, and has no owner, we shouldn't be here
1107  */
1108  if (towner == NULL && target->type != PLAYER) {
1109  LOG(llevError, "Target is not a player but has no owner. We should not be here.\n");
1110  return 0;
1111  }
1112 
1113  /* make sure that the owner is a player */
1114  if (rowner->type != PLAYER)
1115  return 0;
1116 
1117  /* abort if the petmode is not arena */
1118  if (rowner->contr->petmode != pet_arena)
1119  return 0;
1120 
1121  /* abort if the pet, it's owner, or the target is not on battleground*/
1122  if (!(op_on_battleground(pet, NULL, NULL, NULL)
1123  && op_on_battleground(owner, NULL, NULL, NULL)
1124  && op_on_battleground(target, NULL, NULL, NULL)))
1125  return 0;
1126 
1127  /* if the target is a monster, make sure it's owner is not the same */
1128  if (target->type != PLAYER && rowner == towner)
1129  return 0;
1130 
1131  /* check if the target is a player which affects how it will handle
1132  parties */
1133  if (target->type != PLAYER) {
1134  /* if the target is owned by a player make sure than make sure
1135  it's not in the same party */
1136  if (towner->type == PLAYER && rowner->contr->party != NULL) {
1137  if (rowner->contr->party == towner->contr->party)
1138  return 0;
1139  }
1140  } else {
1141  /* if the target is a player make sure than make sure it's not
1142  in the same party */
1143  if (rowner->contr->party != NULL) {
1144  if (rowner->contr->party == target->contr->party)
1145  return 0;
1146  }
1147  }
1148 
1149  return 1;
1150 }
SP_level_range_adjust
int SP_level_range_adjust(const object *caster, const object *spob)
Definition: spell_util.c:337
object_was_destroyed
#define object_was_destroyed(op, old_tag)
Definition: object.h:68
treasurestruct::item
struct archt * item
Definition: treasure.h:64
give.next
def next
Definition: give.py:44
UP_OBJ_FACE
#define UP_OBJ_FACE
Definition: object.h:520
PLAYER
@ PLAYER
Definition: object.h:107
obj::attack_movement
uint16_t attack_movement
Definition: object.h:395
set_spell_skill
void set_spell_skill(object *op, object *caster, object *spob, object *dest)
Definition: spell_util.c:92
freedir
int freedir[SIZEOFFREE]
Definition: object.c:306
global.h
pets_terminate_all
void pets_terminate_all(object *owner)
Definition: pets.c:225
liv::dam
int16_t dam
Definition: living.h:46
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
object_remove
void object_remove(object *op)
Definition: object.c:1806
safe_strncpy
#define safe_strncpy
Definition: compat.h:27
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Definition: define.h:728
pl::golem_count
uint32_t golem_count
Definition: player.h:106
object_set_enemy
void object_set_enemy(object *op, object *enemy)
Definition: object.c:908
FLAG_STAND_STILL
#define FLAG_STAND_STILL
Definition: define.h:308
FLAG_CONFUSED
#define FLAG_CONFUSED
Definition: define.h:311
llevError
@ llevError
Definition: logger.h:11
FABS
#define FABS(x)
Definition: define.h:22
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
monster_can_detect_enemy
int monster_can_detect_enemy(object *op, object *enemy, rv_vector *rv)
Definition: monster.c:2522
FLAG_GENERATOR
#define FLAG_GENERATOR
Definition: define.h:248
object_update
void object_update(object *op, int action)
Definition: object.c:1407
diamondslots.x
x
Definition: diamondslots.py:15
obj::count
tag_t count
Definition: object.h:300
obj::map
struct mapdef * map
Definition: object.h:298
SIZEOFFREE1
#define SIZEOFFREE1
Definition: define.h:153
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
obj::race
sstring race
Definition: object.h:319
liv::maxgrace
int16_t maxgrace
Definition: living.h:45
op_on_battleground
int op_on_battleground(object *op, int *x, int *y, archetype **trophy)
Definition: player.c:4202
AT_PHYSICAL
#define AT_PHYSICAL
Definition: attack.h:76
obj::attacked_by_count
tag_t attacked_by_count
Definition: object.h:387
MSG_TYPE_SPELL_PET
#define MSG_TYPE_SPELL_PET
Definition: newclient.h:632
pl
Definition: player.h:92
guildjoin.ob
ob
Definition: guildjoin.py:42
mark_inventory_as_no_drop
static void mark_inventory_as_no_drop(object *ob)
Definition: pets.c:34
mapdef::in_memory
uint32_t in_memory
Definition: map.h:344
MIN
#define MIN(x, y)
Definition: compat.h:21
races_get_random_monster
object * races_get_random_monster(const char *race, int level)
Definition: races.cpp:25
SP_level_duration_adjust
int SP_level_duration_adjust(const object *caster, const object *spob)
Definition: spell_util.c:311
ob_blocked
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Definition: map.c:498
DIRX
#define DIRX(xyz)
Definition: define.h:463
Ice.tmp
int tmp
Definition: Ice.py:207
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.c:56
llevMonster
@ llevMonster
Definition: logger.h:14
get_rangevector
int get_rangevector(object *op1, const object *op2, rv_vector *retval, int flags)
Definition: map.c:2535
range_golem
@ range_golem
Definition: player.h:21
create_treasure
void create_treasure(treasurelist *t, object *op, int flag, int difficulty, int tries)
Definition: treasure.c:242
P_IS_ALIVE
#define P_IS_ALIVE
Definition: map.h:238
MSG_TYPE_MISC
#define MSG_TYPE_MISC
Definition: newclient.h:413
first_friendly_object
EXTERN objectlink * first_friendly_object
Definition: global.h:119
GT_STARTEQUIP
@ GT_STARTEQUIP
Definition: treasure.h:33
obj::randomitems
struct treasureliststruct * randomitems
Definition: object.h:389
treasurestruct
Definition: treasure.h:63
MAX
#define MAX(x, y)
Definition: compat.h:24
freearr_x
short freearr_x[SIZEOFFREE]
Definition: object.c:288
freearr_y
short freearr_y[SIZEOFFREE]
Definition: object.c:294
obj::duration
int16_t duration
Definition: object.h:407
archt
Definition: object.h:468
FLAG_ALIVE
#define FLAG_ALIVE
Definition: define.h:230
obj::slaying
sstring slaying
Definition: object.h:320
free_string
void free_string(sstring str)
Definition: shstr.c:280
m
static event_registration m
Definition: citylife.cpp:425
obj::attacked_by
struct obj * attacked_by
Definition: object.h:386
MAP_IN_MEMORY
#define MAP_IN_MEMORY
Definition: map.h:131
PREFER_HIGH
#define PREFER_HIGH
Definition: define.h:563
oblnk::next
struct oblnk * next
Definition: object.h:447
obj::name
sstring name
Definition: object.h:312
determine_god
const char * determine_god(object *op)
Definition: gods.c:106
get_real_owner
static object * get_real_owner(object *ob)
Definition: pets.c:1057
move_ob
int move_ob(object *op, int dir, object *originator)
Definition: move.c:58
mon
object * mon
Definition: comet_perf.c:75
MOVE_WALK
#define MOVE_WALK
Definition: define.h:392
pets_attempt_follow
void pets_attempt_follow(object *for_owner, int force)
Definition: pets.c:248
pl::petmode
petmode_t petmode
Definition: player.h:102
pets_follow_owner
void pets_follow_owner(object *ob, object *owner)
Definition: pets.c:282
HEAD
#define HEAD(op)
Definition: object.h:594
pet_defend
@ pet_defend
Definition: player.h:47
monster_find_nearest_living_creature
object * monster_find_nearest_living_creature(object *npc, object *exclude)
Definition: monster.c:155
pets_should_arena_attack
int pets_should_arena_attack(object *pet, object *owner, object *target)
Definition: pets.c:1084
absdir
int absdir(int d)
Definition: object.c:3722
pets_control_golem
void pets_control_golem(object *op, int dir)
Definition: pets.c:628
choose_cult_monster
static object * choose_cult_monster(object *pl, const object *god, int summon_level)
Definition: pets.c:817
obj::x
int16_t x
Definition: object.h:328
CFweardisguise.tag
tag
Definition: CFweardisguise.py:25
treasureliststruct::name
const char * name
Definition: treasure.h:83
GET_MAP_MOVE_BLOCK
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:193
obj::other_arch
struct archt * other_arch
Definition: object.h:417
FOR_INV_FINISH
#define FOR_INV_FINISH()
Definition: define.h:675
archt::more
struct archt * more
Definition: object.h:471
GOLEM
@ GOLEM
Definition: object.h:145
caster_level
int caster_level(const object *caster, const object *spell)
Definition: spell_util.c:193
rndm
int rndm(int min, int max)
Definition: utils.c:162
FLAG_UNAGGRESSIVE
#define FLAG_UNAGGRESSIVE
Definition: define.h:272
obj::speed
float speed
Definition: object.h:330
tag_t
uint32_t tag_t
Definition: object.h:12
sproto.h
FLAG_NO_DROP
#define FLAG_NO_DROP
Definition: define.h:288
obj::enemy
struct obj * enemy
Definition: object.h:385
mapdef
Definition: map.h:324
MSG_TYPE_SPELL
#define MSG_TYPE_SPELL
Definition: newclient.h:411
MSG_SUBTYPE_NONE
#define MSG_SUBTYPE_NONE
Definition: newclient.h:420
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
FLAG_SPLITTING
#define FLAG_SPLITTING
Definition: define.h:266
FLAG_MONSTER
#define FLAG_MONSTER
Definition: define.h:245
object_set_owner
void object_set_owner(object *op, object *owner)
Definition: object.c:833
SIZEOFFREE
#define SIZEOFFREE
Definition: define.h:155
P_OUT_OF_MAP
#define P_OUT_OF_MAP
Definition: map.h:250
MAX_BUF
#define MAX_BUF
Definition: define.h:35
SP_level_wc_adjust
int SP_level_wc_adjust(const object *caster, const object *spob)
Definition: spell_util.c:361
monster_check_apply_all
void monster_check_apply_all(object *monster)
Definition: monster.c:1959
RANDOM
#define RANDOM()
Definition: define.h:642
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:196
random_roll
int random_roll(int min, int max, const object *op, int goodbad)
Definition: utils.c:42
guildbuy.ob2
ob2
Definition: guildbuy.py:23
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:721
obj::y
int16_t y
Definition: object.h:328
obj::arch
struct archt * arch
Definition: object.h:416
treasureliststruct::items
struct treasurestruct * items
Definition: treasure.h:89
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
monster_can_see_enemy
int monster_can_see_enemy(object *op, object *enemy)
Definition: monster.c:2705
obj::type
uint8_t type
Definition: object.h:341
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:262
fix_summon_pet
static object * fix_summon_pet(archetype *at, object *op, int dir)
Definition: pets.c:432
FLAG_FRIENDLY
#define FLAG_FRIENDLY
Definition: define.h:246
die_roll
int die_roll(int num, int size, const object *op, int goodbad)
Definition: utils.c:122
obj::stats
living stats
Definition: object.h:371
SP_level_dam_adjust
int SP_level_dam_adjust(const object *caster, const object *spob)
Definition: spell_util.c:286
archt::clone
object clone
Definition: object.h:472
MSG_TYPE_SPELL_FAILURE
#define MSG_TYPE_SPELL_FAILURE
Definition: newclient.h:633
obj::contr
struct pl * contr
Definition: object.h:277
reputation.victim
victim
Definition: reputation.py:14
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
pets_move
void pets_move(object *ob)
Definition: pets.c:315
give.op
op
Definition: give.py:33
object_find_free_spot
int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop)
Definition: object.c:3509
pet_sad
@ pet_sad
Definition: player.h:46
pet_arena
@ pet_arena
Definition: player.h:48
pets_get_enemy
object * pets_get_enemy(object *pet, rv_vector *rv)
Definition: pets.c:54
treasurestruct::nrof
uint16_t nrof
Definition: treasure.h:76
rv_vector
Definition: map.h:379
monster_check_enemy
object * monster_check_enemy(object *npc, rv_vector *rv)
Definition: monster.c:72
diamondslots.y
y
Definition: diamondslots.py:16
oblnk::ob
object * ob
Definition: object.h:446
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
buf
StringBuffer * buf
Definition: readable.c:1606
obj::more
struct obj * more
Definition: object.h:296
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
pl::ranges
object * ranges[range_size]
Definition: player.h:103
MIN_ACTIVE_SPEED
#define MIN_ACTIVE_SPEED
Definition: define.h:637
treasurestruct::next
struct treasurestruct * next
Definition: treasure.h:66
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:2067
draw_ext_info
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:309
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.c:1533
pets_move_golem
void pets_move_golem(object *op)
Definition: pets.c:517
pets_summon_object
int pets_summon_object(object *op, object *caster, object *spell_ob, int dir, const char *stringarg)
Definition: pets.c:870
add_friendly_object
void add_friendly_object(object *op)
Definition: friend.c:30
OB_TYPE_MOVE_BLOCK
#define OB_TYPE_MOVE_BLOCK(ob1, type)
Definition: define.h:432
archt::name
sstring name
Definition: object.h:469
get_random_dir
int get_random_dir(void)
Definition: utils.c:427
PETMOVE
#define PETMOVE
Definition: define.h:501
obj::attacktype
uint32_t attacktype
Definition: object.h:345
attack_ob
int attack_ob(object *op, object *hitter)
Definition: attack.c:912
oblnk
Definition: object.h:445
pl::party
partylist * party
Definition: player.h:188
FLAG_CHANGING
#define FLAG_CHANGING
Definition: define.h:263
get_map_flags
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:311
get_search_arr
void get_search_arr(int *search_arr)
Definition: object.c:3592
AT_GHOSTHIT
#define AT_GHOSTHIT
Definition: attack.h:85
find_god
const object * find_god(const char *name)
Definition: gods.c:80
liv::sp
int16_t sp
Definition: living.h:42
rv_vector::direction
int direction
Definition: map.h:383
on_same_map
int on_same_map(const object *op1, const object *op2)
Definition: map.c:2647
obj::resist
int16_t resist[NROFATTACKS]
Definition: object.h:344
get_map_from_coord
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.c:2375
FLAG_SLEEP
#define FLAG_SLEEP
Definition: define.h:307
FOR_INV_PREPARE
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:668
AT_DRAIN
#define AT_DRAIN
Definition: attack.h:83
draw_ext_info_format
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:319
obj::level
int16_t level
Definition: object.h:354
llevDebug
@ llevDebug
Definition: logger.h:13
DIRY
#define DIRY(xyz)
Definition: define.h:464
pets_summon_golem
int pets_summon_golem(object *op, object *caster, int dir, object *spob)
Definition: pets.c:649
determine_holy_arch
archetype * determine_holy_arch(const object *god, const char *type)
Definition: gods.c:739
dragon_attune.force
force
Definition: dragon_attune.py:45
object_get_owner
object * object_get_owner(object *op)
Definition: object.c:797
level
Definition: level.py:1