Crossfire Server, Trunk  R21041
object.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 /* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects.
20  object_sub/add_weight will transcend the environment updating the carrying
21  variable. */
22 
23 #include "global.h"
24 
25 #include <assert.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #ifndef WIN32 /* ---win32 exclude headers */
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #endif /* win32 */
35 
36 #include "loader.h"
37 #include "object.h"
38 #include "skills.h"
39 #include "sproto.h"
40 #include "stringbuffer.h"
41 
42 static int compare_ob_value_lists_one(const object *, const object *);
43 static int compare_ob_value_lists(const object *, const object *);
44 static void expand_objects(void);
45 static void permute(int *, int, int);
46 static int object_set_value_s(object *, const char *, const char *, int);
47 static void object_increase_nrof(object *op, uint32_t i);
48 
49 #ifdef MEMORY_DEBUG
50 int nroffreeobjects = 0;
51 int nrofallocobjects = 0;
52 #undef OBJ_EXPAND
53 #define OBJ_EXPAND 1
54 #else
55 static object objarray[STARTMAX];
58 #endif
59 
60 object *objects;
61 static object *free_objects;
62 object *active_objects;
66  0, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1,
67  0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
68 };
69 
72  0, -1, -1, 0, 1, 1, 1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2,
73  -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3
74 };
75 
78  0, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45,
79  48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49
80 };
81 
84  0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8,
85  1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
86 };
87 
93 void init_objects(void) {
94  /* Initialize all objects: */
95  objects = NULL;
96  active_objects = NULL;
97 
98 #ifdef MEMORY_DEBUG
99  free_objects = NULL;
100 #else
101  free_objects = objarray;
102  objarray[0].prev = NULL,
103  objarray[0].next = &objarray[1],
104  SET_FLAG(&objarray[0], FLAG_REMOVED);
105  SET_FLAG(&objarray[0], FLAG_FREED);
106  for (int i = 1; i < STARTMAX-1; i++) {
107  objarray[i].next = &objarray[i+1];
108  objarray[i].prev = &objarray[i-1];
109  SET_FLAG(&objarray[i], FLAG_REMOVED);
110  SET_FLAG(&objarray[i], FLAG_FREED);
111  }
112  objarray[STARTMAX-1].next = NULL;
113  objarray[STARTMAX-1].prev = &objarray[STARTMAX-2];
114  SET_FLAG(&objarray[STARTMAX-1], FLAG_REMOVED);
115  SET_FLAG(&objarray[STARTMAX-1], FLAG_FREED);
116 #endif
117 }
118 
129 static int compare_ob_value_lists_one(const object *wants, const object *has) {
130  key_value *wants_field;
131 
132  /* n-squared behaviour (see object_get_key_value()), but I'm hoping both
133  * objects with lists are rare, and lists stay short. If not, use a
134  * different structure or at least keep the lists sorted...
135  */
136 
137  /* For each field in wants, */
138  for (wants_field = wants->key_values; wants_field != NULL; wants_field = wants_field->next) {
139  key_value *has_field;
140 
141  /* Look for a field in has with the same key. */
142  has_field = object_get_key_value(has, wants_field->key);
143 
144  if (has_field == NULL) {
145  /* No field with that name. */
146  return FALSE;
147  }
148 
149  /* Found the matching field. */
150  if (has_field->value != wants_field->value) {
151  /* Values don't match, so this half of the comparison is false. */
152  return FALSE;
153  }
154 
155  /* If we get here, we found a match. Now for the next field in wants. */
156  }
157 
158  /* If we get here, every field in wants has a matching field in has. */
159  return TRUE;
160 }
161 
170 static int compare_ob_value_lists(const object *ob1, const object *ob2) {
171  /* However, there may be fields in has which aren't partnered in wants,
172  * so we need to run the comparison *twice*. :(
173  */
174  return compare_ob_value_lists_one(ob1, ob2) && compare_ob_value_lists_one(ob2, ob1);
175 }
176 
202 int object_can_merge(object *ob1, object *ob2) {
203  /* A couple quicksanity checks */
204  if (ob1 == ob2 || ob1->type != ob2->type)
205  return 0;
206 
207  if (ob1->speed != ob2->speed)
208  return 0;
209  /* Note sure why the following is the case - either the object has to
210  * be animated or have a very low speed. Is this an attempted monster
211  * check?
212  */
213  /*TODO is this check really needed?*/
214  if (!QUERY_FLAG(ob1, FLAG_ANIMATE) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED)
215  return 0;
216 
217  /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that
218  * value could not be stored in a int32_t (which unfortunately sometimes is
219  * used to store nrof).
220  */
221  if (ob1->nrof+ob2->nrof >= 1UL<<31)
222  return 0;
223 
224  /* This is really a spellbook check - really, we should
225  * check all objects in the inventory.
226  */
227  /*TODO is this check really needed?*/
228  if (ob1->inv || ob2->inv) {
229  /* if one object has inventory but the other doesn't, not equiv */
230  if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv))
231  return 0;
232 
233  /* Now check to see if the two inventory objects could merge */
234  if (!object_can_merge(ob1->inv, ob2->inv))
235  return 0;
236 
237  /* inventory ok - still need to check rest of this object to see
238  * if it is valid.
239  */
240  }
241 
242  /* If the objects have been identified, set the BEEN_APPLIED flag.
243  * This is to the comparison of the flags below will be OK. We
244  * just can't ignore the been applied or identified flags, as they
245  * are not equal - just if it has been identified, the been_applied
246  * flags lose any meaning.
247  */
248 
249  /*TODO is this hack on BEEN_APPLIED really needed? */
250  if (QUERY_FLAG(ob1, FLAG_IDENTIFIED))
252 
253  if (QUERY_FLAG(ob2, FLAG_IDENTIFIED))
255 
256 
257  /* Note: FLAG_INV_LOCKED is ignored for merging purposes */
258  if ((ob1->arch != ob2->arch)
259  || (ob1->flags[0] != ob2->flags[0])
260  || (ob1->flags[1] != ob2->flags[1])
261  || (ob1->flags[2] != ob2->flags[2])
262  || ((ob1->flags[3]&~0x84) != (ob2->flags[3]&~0x84)) /* ignore CLIENT_SENT and FLAG_OBJ_ORIGINAL */
263  || (ob1->name != ob2->name)
264  || (ob1->title != ob2->title)
265  || (ob1->msg != ob2->msg)
266  || (ob1->weight != ob2->weight)
267  || (ob1->item_power != ob2->item_power)
268  || (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist)) != 0)
269  || (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats)) != 0)
270  || (ob1->attacktype != ob2->attacktype)
271  || (ob1->magic != ob2->magic)
272  || (ob1->slaying != ob2->slaying)
273  || (ob1->skill != ob2->skill)
274  || (ob1->value != ob2->value)
275  || (ob1->animation_id != ob2->animation_id)
276  || (ob1->client_type != ob2->client_type)
277  || (ob1->materialname != ob2->materialname)
278  || (ob1->lore != ob2->lore)
279  || (ob1->subtype != ob2->subtype)
280  || (ob1->move_type != ob2->move_type)
281  || (ob1->move_block != ob2->move_block)
282  || (ob1->move_allow != ob2->move_allow)
283  || (ob1->move_on != ob2->move_on)
284  || (ob1->move_off != ob2->move_off)
285  || (ob1->move_slow != ob2->move_slow)
286  || (ob1->move_slow_penalty != ob2->move_slow_penalty)
287  || (ob1->map_layer != ob2->map_layer))
288  return 0;
289 
290  /* Don't merge objects that are applied. With the new 'body' code,
291  * it is possible for most any character to have more than one of
292  * some items equipped, and we don't want those to merge.
293  */
294  if (QUERY_FLAG(ob1, FLAG_APPLIED) || QUERY_FLAG(ob2, FLAG_APPLIED))
295  return 0;
296 
297  if (ob1->key_values != NULL || ob2->key_values != NULL) {
298  /* At least one of these has key_values. */
299  if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) {
300  /* One has fields, but the other one doesn't. */
301  return 0;
302  } else {
303  return compare_ob_value_lists(ob1, ob2);
304  }
305  }
306 
307  /*TODO should this really be limited to scrolls?*/
308  switch (ob1->type) {
309  case SCROLL:
310  if (ob1->level != ob2->level)
311  return 0;
312  break;
313  }
314 
315  /* Don't merge items with differing custom names. */
316  if (ob1->custom_name != ob2->custom_name)
317  return 0;
318 
319  /* Everything passes, must be OK. */
320  return 1;
321 }
322 
341 /* TODO should check call this this are made a place where we really need reevaluaton of whole tree */
342 signed long object_sum_weight(object *op) {
343  signed long sum;
344 
345  sum = 0;
346  FOR_INV_PREPARE(op, inv) {
347  if (inv->inv)
348  object_sum_weight(inv);
349  sum += inv->carrying+inv->weight*(inv->nrof ? inv->nrof : 1);
350  } FOR_INV_FINISH();
351  if (op->type == CONTAINER && op->stats.Str)
352  sum = (sum*(100-op->stats.Str))/100;
353  op->carrying = sum;
354  return sum;
355 }
356 
364 object *object_get_env_recursive(object *op) {
365  while (op->env != NULL)
366  op = op->env;
367  return op;
368 }
369 
384 object *object_get_player_container(object *op) {
385  for (; op != NULL && op->type != PLAYER; op = op->env)
386  /*TODO this is patching the structure on the flight as side effect. Shoudln't be needed in clean code */
387  if (op->env == op)
388  op->env = NULL;
389  return op;
390 }
391 
401 static const object *object_get_owner_const(const object *op) {
402  if (op->owner == NULL)
403  return NULL;
404 
405  if (!QUERY_FLAG(op->owner, FLAG_FREED)
406  && !QUERY_FLAG(op->owner, FLAG_REMOVED)
407  && op->owner->count == op->ownercount)
408  return op->owner;
409 
410  LOG(llevError, "Warning, no owner found\n");
411  return NULL;
412 }
413 
425 void object_dump(const object *op, StringBuffer *sb) {
426  if (op == NULL) {
427  stringbuffer_append_string(sb, "[NULL pointer]");
428  return;
429  }
430 
431  /* object *tmp;*/
432 
433  if (op->arch != NULL) {
434  const object *owner;
435 
436  stringbuffer_append_string(sb, "arch ");
437  stringbuffer_append_string(sb, op->arch->name ? op->arch->name : "(null)");
438  stringbuffer_append_string(sb, "\n");
439 
440  if (op->artifact != NULL) {
441  stringbuffer_append_string(sb, "artifact ");
443  stringbuffer_append_string(sb, "\n");
444  }
445 
446  get_ob_diff(sb, op, &empty_archetype->clone);
447  if (op->more) {
448  stringbuffer_append_printf(sb, "more %u\n", op->more->count);
449  }
450  if (op->head) {
451  stringbuffer_append_printf(sb, "head %u\n", op->head->count);
452  }
453  if (op->env) {
454  stringbuffer_append_printf(sb, "env %u\n", op->env->count);
455  }
456  if (op->inv) {
457  stringbuffer_append_printf(sb, "inv %u\n", op->inv->count);
458  }
459  owner = object_get_owner_const(op);
460  if (owner != NULL) {
461  stringbuffer_append_printf(sb, "owner %u\n", owner->count);
462  }
463  stringbuffer_append_string(sb, "end\n");
464  } else {
465  stringbuffer_append_string(sb, "Object ");
466  stringbuffer_append_string(sb, op->name == NULL ? "(null)" : op->name);
467  stringbuffer_append_string(sb, "\nend\n");
468  }
469 }
470 
481 void object_dump_all(void) {
482  object *op;
483 
484  for (op = objects; op != NULL; op = op->next) {
485  StringBuffer *sb;
486  char *diff;
487 
488  sb = stringbuffer_new();
489  object_dump(op, sb);
490  diff = stringbuffer_finish(sb);
491  LOG(llevDebug, "Object %u\n:%s\n", op->count, diff);
492  free(diff);
493  }
494 }
495 
508  object *op;
509 
510  for (op = objects; op != NULL; op = op->next)
511  if (op->count == i)
512  break;
513  return op;
514 }
515 
530 object *object_find_by_name_global(const char *str) {
531  const char *name = add_string(str);
532  object *op;
533 
534  for (op = objects; op != NULL; op = op->next)
535  if (op->name == name)
536  break;
537  free_string(name);
538  return op;
539 }
540 
554 #ifdef MEMORY_DEBUG
555  object *op, *next;
556 
557  for (op = free_objects; op != NULL; ) {
558  next = op->next;
559  free(op);
561  nroffreeobjects--;
562  op = next;
563  }
564  free_objects = NULL;
565 
566  for (op = objects; op != NULL; ) {
567  next = op->next;
568  if (!QUERY_FLAG(op, FLAG_FREED)) {
569  LOG(llevDebug, "non freed object: %s\n", op->name);
570  }
571  op = next;
572  }
573 #endif
574 
575  LOG(llevDebug, "%d allocated objects, %d free objects, STARMAX=%d\n", nrofallocobjects, nroffreeobjects, STARTMAX);
576 }
577 
590 object *object_get_owner(object *op) {
591  if (op->owner == NULL)
592  return NULL;
593 
594  if (!QUERY_FLAG(op->owner, FLAG_FREED)
595  && !QUERY_FLAG(op->owner, FLAG_REMOVED)
596  && op->owner->count == op->ownercount)
597  return op->owner;
598 
599  object_clear_owner(op);
600  return NULL;
601 }
602 
612 void object_clear_owner(object *op) {
613  if (!op)
614  return;
615 
616  op->owner = NULL;
617  op->ownercount = 0;
618 }
619 
632 void object_set_owner(object *op, object *owner) {
633  /* Assign temp to something, so it can't accidentally be NULL */
634  object *tmp = owner;
635  if (op == NULL)
636  return;
637  if (owner == NULL) {
638  object_clear_owner(op);
639  return;
640  }
641 
642  /* next line added to allow objects which own objects */
643  /* Add a check for ownercounts in here, as I got into an endless loop
644  * with the fireball owning a poison cloud which then owned the
645  * fireball. I believe that was caused by one of the objects getting
646  * freed and then another object replacing it. Since the ownercounts
647  * didn't match, this check is valid and I believe that cause is valid.
648  */
649  /*
650  * if owner is NULL, function will have already returned,
651  * so loop should still function as before.
652  */
653  while (tmp) {
654  tmp = object_get_owner(owner);
655  if (tmp)
656  owner = tmp;
657  }
658 
659  /* must not cause owner cycles */
660  assert(op != owner);
661 
662  if (op->owner != NULL)
663  object_clear_owner(op);
664 
665  op->owner = owner;
666  op->ownercount = owner->count;
667 }
668 
688 void object_copy_owner(object *op, object *clone) {
689  object *owner = object_get_owner(clone);
690  if (owner == NULL) {
691  /* players don't have owners - they own themselves. Update
692  * as appropriate.
693  */
694  /*TODO owner=self is dangerous and should be avoided*/
695  if (clone->type != PLAYER)
696  return;
697  owner = clone;
698  }
699  object_set_owner(op, owner);
700 }
701 
710 void object_set_enemy(object *op, object *enemy) {
711  if (op->enemy == enemy) {
712  return;
713  }
714 
715 #if 0
716  if (op->type != PLAYER) {
717  LOG(llevDebug, "object_set_enemy: %s(%lu)->enemy=%s(%lu)\n", op->name, op->count, enemy == NULL ? "NONE" : enemy->name, enemy == NULL ? 0 : enemy->count);
718  }
719 #endif
720  op->enemy = enemy;
721 }
722 
735 void object_reset(object *op) {
736  op->name = NULL;
737  op->name_pl = NULL;
738  op->title = NULL;
739  op->race = NULL;
740  op->slaying = NULL;
741  op->skill = NULL;
742  op->msg = NULL;
743  op->materialname = NULL;
744  op->lore = NULL;
745  object_clear(op);
746 }
747 
758 void object_free_key_values(object *op) {
759  key_value *i;
760  key_value *next = NULL;
761 
762  if (op->key_values == NULL)
763  return;
764 
765  for (i = op->key_values; i != NULL; i = next) {
766  /* Store next *first*. */
767  next = i->next;
768 
769  if (i->key)
771  if (i->value)
773  i->next = NULL;
774  free(i);
775  }
776 
777  op->key_values = NULL;
778 }
779 
790 void object_clear(object *op) {
791  /*TODO this comment must be investigated*/
792  /* redo this to be simpler/more efficient. Was also seeing
793  * crashes in the old code. Move this to the top - am
794  * seeing periodic crashes in this code, and would like to have
795  * as much info available as possible (eg, object name).
796  */
799 
800  /* the memset will clear all these values for us, but we need
801  * to reduce the refcount on them.
802  */
803  if (op->name != NULL)
805  if (op->name_pl != NULL)
807  if (op->title != NULL)
809  if (op->race != NULL)
811  if (op->slaying != NULL)
813  if (op->skill != NULL)
815  if (op->msg != NULL)
816  FREE_AND_CLEAR_STR(op->msg);
817  if (op->lore != NULL)
819  if (op->materialname != NULL)
821  if (op->discrete_damage != NULL)
823 
824  /* Remove object from friendly list if needed. */
825  if (QUERY_FLAG(op, FLAG_FRIENDLY))
827 
828  memset((void *)((char *)op+offsetof(object, name)), 0, sizeof(object)-offsetof(object, name));
829  /* Below here, we clear things that are not done by the memset,
830  * or set default values that are not zero.
831  */
832  /* This is more or less true */
833  SET_FLAG(op, FLAG_REMOVED);
834 
835 
836  op->contr = NULL;
837  op->below = NULL;
838  op->above = NULL;
839  op->inv = NULL;
840  op->container = NULL;
841  op->env = NULL;
842  op->more = NULL;
843  op->head = NULL;
844  op->map = NULL;
845  op->active_next = NULL;
846  op->active_prev = NULL;
847  /* What is not cleared is next, prev, and count */
848 
849  op->expmul = 1.0;
850  op->face = blank_face;
851  op->attacked_by_count = -1;
853  op->casting_time = -1;
854 }
855 
869 void object_copy(const object *src_ob, object *dest_ob) {
870  int is_freed = QUERY_FLAG(dest_ob, FLAG_FREED), is_removed = QUERY_FLAG(dest_ob, FLAG_REMOVED);
871 
872  /* Decrement the refcounts, but don't bother zeroing the fields;
873  they'll be overwritten by memcpy. */
874  if (dest_ob->artifact != NULL)
875  free_string(dest_ob->artifact);
876  if (dest_ob->name != NULL)
877  free_string(dest_ob->name);
878  if (dest_ob->name_pl != NULL)
879  free_string(dest_ob->name_pl);
880  if (dest_ob->anim_suffix != NULL)
881  free_string(dest_ob->anim_suffix);
882  if (dest_ob->title != NULL)
883  free_string(dest_ob->title);
884  if (dest_ob->race != NULL)
885  free_string(dest_ob->race);
886  if (dest_ob->slaying != NULL)
887  free_string(dest_ob->slaying);
888  if (dest_ob->skill != NULL)
889  free_string(dest_ob->skill);
890  if (dest_ob->msg != NULL)
891  free_string(dest_ob->msg);
892  if (dest_ob->lore != NULL)
893  free_string(dest_ob->lore);
894  if (dest_ob->materialname != NULL)
895  free_string(dest_ob->materialname);
896  if (dest_ob->custom_name != NULL)
897  free_string(dest_ob->custom_name);
898  if (dest_ob->discrete_damage != NULL)
900  if (dest_ob->spell_tags != NULL)
901  FREE_AND_CLEAR(dest_ob->spell_tags);
902 
903  /* Basically, same code as from object_clear() */
904 
905  object_free_key_values(dest_ob);
906  free_dialog_information(dest_ob);
907 
908  /* Copy all attributes below name (name included). */
909  (void)memcpy((void *)((char *)dest_ob+offsetof(object, name)),
910  (void *)((char *)src_ob+offsetof(object, name)),
911  sizeof(object)-offsetof(object, name));
912 
913  if (is_freed)
914  SET_FLAG(dest_ob, FLAG_FREED);
915  if (is_removed)
916  SET_FLAG(dest_ob, FLAG_REMOVED);
917  if (dest_ob->artifact != NULL)
918  add_refcount(dest_ob->artifact);
919  if (dest_ob->name != NULL)
920  add_refcount(dest_ob->name);
921  if (dest_ob->name_pl != NULL)
922  add_refcount(dest_ob->name_pl);
923  if (dest_ob->anim_suffix != NULL)
924  add_refcount(dest_ob->anim_suffix);
925  if (dest_ob->title != NULL)
926  add_refcount(dest_ob->title);
927  if (dest_ob->race != NULL)
928  add_refcount(dest_ob->race);
929  if (dest_ob->slaying != NULL)
930  add_refcount(dest_ob->slaying);
931  if (dest_ob->skill != NULL)
932  add_refcount(dest_ob->skill);
933  if (dest_ob->lore != NULL)
934  add_refcount(dest_ob->lore);
935  if (dest_ob->msg != NULL)
936  add_refcount(dest_ob->msg);
937  if (dest_ob->custom_name != NULL)
938  add_refcount(dest_ob->custom_name);
939  if (dest_ob->materialname != NULL)
940  add_refcount(dest_ob->materialname);
941  if (dest_ob->discrete_damage != NULL) {
942  dest_ob->discrete_damage = malloc(sizeof(int16_t)*NROFATTACKS);
943  memcpy(dest_ob->discrete_damage, src_ob->discrete_damage, sizeof(int16_t)*NROFATTACKS);
944  }
945 
946  if (dest_ob->spell_tags != NULL) {
947  dest_ob->spell_tags = malloc(sizeof(tag_t)*SPELL_TAG_SIZE);
948  memcpy(dest_ob->spell_tags, src_ob->spell_tags, sizeof(tag_t)*SPELL_TAG_SIZE);
949  }
950 
951  /* If archetype is a temporary one, we need to update reference count, because
952  * that archetype will be freed by object_free_drop_inventory() when the last object is removed.
953  */
954  if (dest_ob->arch->reference_count > 0)
955  dest_ob->arch->reference_count++;
956 
957  if (src_ob->speed < 0)
958  dest_ob->speed_left = src_ob->speed_left-RANDOM()%200/100.0;
959 
960  /* Copy over key_values, if any. */
961  if (src_ob->key_values != NULL) {
962  key_value *tail = NULL;
963  key_value *i;
964 
965  dest_ob->key_values = NULL;
966 
967  for (i = src_ob->key_values; i != NULL; i = i->next) {
968  key_value *new_link = malloc(sizeof(key_value));
969 
970  new_link->next = NULL;
971  new_link->key = add_refcount(i->key);
972  if (i->value)
973  new_link->value = add_refcount(i->value);
974  else
975  new_link->value = NULL;
976 
977  /* Try and be clever here, too. */
978  if (dest_ob->key_values == NULL) {
979  dest_ob->key_values = new_link;
980  tail = new_link;
981  } else {
982  tail->next = new_link;
983  tail = new_link;
984  }
985  }
986  }
987 
988  /* This way, dialog information will be parsed again when/if needed. */
989  CLEAR_FLAG(dest_ob, FLAG_DIALOG_PARSED);
990 
991  object_update_speed(dest_ob);
992 }
993 
1006 void object_copy_with_inv(const object *src_ob, object *dest_ob) {
1007  object_copy(src_ob, dest_ob);
1008  FOR_INV_PREPARE(src_ob, walk) {
1009  object *tmp;
1010 
1011  tmp = object_new();
1012  object_copy_with_inv(walk, tmp);
1013  object_insert_in_ob(tmp, dest_ob);
1014  } FOR_INV_FINISH();
1015 }
1016 
1024 static void expand_objects(void) {
1025  int i;
1026  object *new;
1027 
1028  new = (object *)CALLOC(OBJ_EXPAND, sizeof(object));
1029 
1030  if (new == NULL)
1032  free_objects = new;
1033  new[0].prev = NULL;
1034  new[0].next = &new[1],
1035  SET_FLAG(&new[0], FLAG_REMOVED);
1036  SET_FLAG(&new[0], FLAG_FREED);
1037 
1038  for (i = 1; i < OBJ_EXPAND-1; i++) {
1039  new[i].next = &new[i+1],
1040  new[i].prev = &new[i-1],
1041  SET_FLAG(&new[i], FLAG_REMOVED);
1042  SET_FLAG(&new[i], FLAG_FREED);
1043  }
1044  new[OBJ_EXPAND-1].prev = &new[OBJ_EXPAND-2],
1045  new[OBJ_EXPAND-1].next = NULL,
1046  SET_FLAG(&new[OBJ_EXPAND-1], FLAG_REMOVED);
1047  SET_FLAG(&new[OBJ_EXPAND-1], FLAG_FREED);
1048 
1051 }
1052 
1068 object *object_new(void) {
1069  object *op;
1070 #ifdef MEMORY_DEBUG
1071  /* FIXME: However this doesn't work since object_free2() sometimes add
1072  * objects back to the free_objects linked list, and some functions mess
1073  * with the object after return of object_free2(). This is bad and should be
1074  * fixed. But it would need fairly extensive changes and a lot of debugging.
1075  */
1076  op = calloc(1, sizeof(object));
1077  if (op == NULL)
1079 #else
1080  if (free_objects == NULL) {
1081  expand_objects();
1082  }
1083  op = free_objects;
1084  if (!QUERY_FLAG(op, FLAG_FREED)) {
1085  LOG(llevError, "Fatal: Getting busy object.\n");
1086 #ifdef MANY_CORES
1087  abort();
1088 #endif
1089  }
1090  free_objects = op->next;
1091  if (free_objects != NULL)
1092  free_objects->prev = NULL;
1093  nroffreeobjects--;
1094 #endif
1095  op->count = ++ob_count;
1096  op->name = NULL;
1097  op->name_pl = NULL;
1098  op->title = NULL;
1099  op->race = NULL;
1100  op->slaying = NULL;
1101  op->skill = NULL;
1102  op->lore = NULL;
1103  op->msg = NULL;
1104  op->materialname = NULL;
1105  op->next = objects;
1106  op->prev = NULL;
1107  op->active_next = NULL;
1108  op->active_prev = NULL;
1109  op->discrete_damage = NULL;
1110  op->spell_tags = NULL;
1111  if (objects != NULL)
1112  objects->prev = op;
1113  objects = op;
1114  object_clear(op);
1115  SET_FLAG(op, FLAG_REMOVED);
1116  return op;
1117 }
1118 
1130 void object_update_turn_face(object *op) {
1131  if (op->animation_id == 0 || !QUERY_FLAG(op, FLAG_IS_TURNABLE))
1132  return;
1133  animate_object(op, op->direction);
1134 }
1135 
1150 void object_update_speed(object *op) {
1151  /* FIXME what the hell is this crappy hack?*/
1152  extern int arch_init;
1153 
1154  /* No reason putting the archetypes objects on the speed list,
1155  * since they never really need to be updated.
1156  */
1157 
1158  if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
1159  LOG(llevError, "Object %s is freed but has speed.\n", op->name);
1160 #ifdef MANY_CORES
1161  abort();
1162 #else
1163  op->speed = 0;
1164 #endif
1165  }
1166  if (arch_init) {
1167  return;
1168  }
1169  if (FABS(op->speed) > MIN_ACTIVE_SPEED) {
1170  /* If already on active list, don't do anything */
1171  /* TODO this check can probably be simplified a lot */
1172  if (op->active_next || op->active_prev || op == active_objects)
1173  return;
1174 
1175  /* process_events() expects us to insert the object at the beginning
1176  * of the list. */
1178  if (op->active_next != NULL)
1179  op->active_next->active_prev = op;
1180  active_objects = op;
1181  } else {
1182  /* If not on the active list, nothing needs to be done */
1183  if (!op->active_next && !op->active_prev && op != active_objects)
1184  return;
1185 
1186  if (op->active_prev == NULL) {
1187  active_objects = op->active_next;
1188  if (op->active_next != NULL)
1189  op->active_next->active_prev = NULL;
1190  } else {
1191  op->active_prev->active_next = op->active_next;
1192  if (op->active_next)
1193  op->active_next->active_prev = op->active_prev;
1194  }
1195  op->active_next = NULL;
1196  op->active_prev = NULL;
1197  }
1198 }
1199 
1216  /* If not on the active list, nothing needs to be done */
1217  if (!op->active_next && !op->active_prev && op != active_objects)
1218  return;
1219 
1220  if (op->active_prev == NULL) {
1221  active_objects = op->active_next;
1222  if (op->active_next != NULL)
1223  op->active_next->active_prev = NULL;
1224  } else {
1225  op->active_prev->active_next = op->active_next;
1226  if (op->active_next)
1227  op->active_next->active_prev = op->active_prev;
1228  }
1229  op->active_next = NULL;
1230  op->active_prev = NULL;
1231 }
1232 
1260 void object_update(object *op, int action) {
1261  int update_now = 0, flags;
1262  MoveType move_on, move_off, move_block, move_slow;
1263  object *pl;
1264 
1265  if (op == NULL) {
1266  /* this should never happen */
1267  LOG(llevDebug, "object_update() called for NULL object.\n");
1268  return;
1269  }
1270 
1271  if (op->env != NULL) {
1272  /* Animation is currently handled by client, so nothing
1273  * to do in this case.
1274  */
1275  return;
1276  }
1277 
1278  /* If the map is saving, don't do anything as everything is
1279  * going to get freed anyways.
1280  */
1281  if (!op->map || op->map->in_memory == MAP_SAVING)
1282  return;
1283 
1284  /* make sure the object is within map boundaries */
1285  if (op->x < 0 || op->x >= MAP_WIDTH(op->map)
1286  || op->y < 0 || op->y >= MAP_HEIGHT(op->map)) {
1287  LOG(llevError, "object_update() called for object out of map!\n");
1288 #ifdef MANY_CORES
1289  abort();
1290 #endif
1291  return;
1292  }
1293 
1294  flags = GET_MAP_FLAGS(op->map, op->x, op->y);
1295  SET_MAP_FLAGS(op->map, op->x, op->y, flags|P_NEED_UPDATE);
1296  move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
1297  move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
1298  move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
1299  move_off = GET_MAP_MOVE_OFF(op->map, op->x, op->y);
1300 
1301  if (action == UP_OBJ_INSERT) {
1303  update_now = 1;
1304 
1305  if (QUERY_FLAG(op, FLAG_NO_MAGIC) && !(flags&P_NO_MAGIC))
1306  update_now = 1;
1307 
1308  if (QUERY_FLAG(op, FLAG_DAMNED) && !(flags&P_NO_CLERIC))
1309  update_now = 1;
1310 
1311  if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags&P_IS_ALIVE))
1312  update_now = 1;
1313 
1314  if ((move_on|op->move_on) != move_on)
1315  update_now = 1;
1316  if ((move_off|op->move_off) != move_off)
1317  update_now = 1;
1318  /* This isn't perfect, but I don't expect a lot of objects to
1319  * to have move_allow right now.
1320  */
1321  if (((move_block|op->move_block)&~op->move_allow) != move_block)
1322  update_now = 1;
1323  if ((move_slow|op->move_slow) != move_slow)
1324  update_now = 1;
1325 
1326  if (op->type == PLAYER)
1327  update_now = 1;
1328  /* if the object is being removed, we can't make intelligent
1329  * decisions, because object_remove() can't really pass the object
1330  * that is being removed.
1331  */
1332  } else if (action == UP_OBJ_REMOVE) {
1333  update_now = 1;
1334  } else if (action == UP_OBJ_FACE || action == UP_OBJ_CHANGE) {
1335  /* In addition to sending info to client, need to update space
1336  * information.
1337  */
1338  if (action == UP_OBJ_CHANGE)
1339  update_now = 1;
1340 
1341  /* There is a player on this space - we may need to send an
1342  * update to the client.
1343  * If this object is supposed to be animated by the client,
1344  * nothing to do here - let the client animate it.
1345  * We can't use FLAG_ANIMATE, as that is basically set for
1346  * all objects with multiple faces, regardless if they are animated.
1347  * (levers have it set for example).
1348  */
1349  if (flags&P_PLAYER
1352  pl = GET_MAP_PLAYER(op->map, op->x, op->y);
1353 
1354  /* If update_look is set, we're going to send this entire space
1355  * to the client, so no reason to send face information now.
1356  */
1357  if (!pl->contr->socket.update_look) {
1358  esrv_update_item(UPD_FACE, pl, op);
1359  }
1360  }
1361  } else {
1362  LOG(llevError, "object_update called with invalid action: %d\n", action);
1363  }
1364 
1365  if (update_now) {
1366  SET_MAP_FLAGS(op->map, op->x, op->y, flags|P_NO_ERROR|P_NEED_UPDATE);
1367  update_position(op->map, op->x, op->y);
1368  }
1369 
1370  if (op->more != NULL)
1371  object_update(op->more, action);
1372 }
1373 
1389 void object_free_drop_inventory(object *ob) {
1390  object_free2(ob, 0);
1391 }
1392 
1412 void object_free2(object *ob, int flags) {
1413  if (!QUERY_FLAG(ob, FLAG_REMOVED)) {
1414  StringBuffer *sb;
1415  char *diff;
1416 
1417  LOG(llevDebug, "Free object called with non removed object\n");
1418  sb = stringbuffer_new();
1419  object_dump(ob, sb);
1420  diff = stringbuffer_finish(sb);
1421  LOG(llevError, "%s", diff);
1422  free(diff);
1423 #ifdef MANY_CORES
1424  abort();
1425 #endif
1426  }
1427  if (QUERY_FLAG(ob, FLAG_FRIENDLY)) {
1428  LOG(llevMonster, "Warning: tried to free friendly object.\n");
1430  }
1431  if (QUERY_FLAG(ob, FLAG_FREED)) {
1432  StringBuffer *sb;
1433  char *diff;
1434 
1435  sb = stringbuffer_new();
1436  object_dump(ob, sb);
1437  diff = stringbuffer_finish(sb);
1438  LOG(llevError, "Trying to free freed object.\n%s\n", diff);
1439  free(diff);
1440  return;
1441  }
1442 
1443  if ((flags & FREE_OBJ_NO_DESTROY_CALLBACK) == 0) {
1444  /* Handle for plugin destroy event */
1445  execute_event(ob, EVENT_DESTROY, NULL, NULL, NULL, SCRIPT_FIX_NOTHING);
1446  }
1447 
1448  if (ob->inv) {
1449  /* Only if the space blocks everything do we not process -
1450  * if some form of movemnt is allowed, let objects
1451  * drop on that space.
1452  */
1453  if ((flags & FREE_OBJ_FREE_INVENTORY) != 0
1454  || ob->map == NULL
1455  || ob->map->in_memory != MAP_IN_MEMORY
1456  || (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) {
1457  FOR_INV_PREPARE(ob, op) {
1458  object_remove(op);
1459  object_free2(op, flags);
1460  } FOR_INV_FINISH();
1461  } else { /* Put objects in inventory onto this space */
1462  FOR_INV_PREPARE(ob, op) {
1463  object_remove(op);
1464  /* No drop means no drop, including its inventory */
1465  if (QUERY_FLAG(op, FLAG_NO_DROP))
1466  object_free2(op, FREE_OBJ_FREE_INVENTORY);
1467  else if (QUERY_FLAG(op, FLAG_STARTEQUIP)
1468  || QUERY_FLAG(op, FLAG_NO_DROP)
1469  || op->type == RUNE
1470  || op->type == TRAP
1473  else {
1474  object *part;
1475 
1476  /* If it's a multi-tile object, scatter dropped items randomly */
1477  if (ob->more) {
1478  int partcount = 0;
1479  /* Get the number of non-head parts */
1480  for (part = ob; part; part = part->more) {
1481  partcount++;
1482  }
1483  /* Select a random part */
1484  partcount = RANDOM()%partcount;
1485  for (part = ob; partcount > 0; partcount--) {
1486  part = part->more;
1487  }
1488  } else {
1489  part = ob;
1490  }
1491 
1492  if (QUERY_FLAG(op, FLAG_ALIVE)) {
1493  object_insert_to_free_spot_or_free(op, part->map, part->x, part->y, 0, SIZEOFFREE, NULL);
1494  } else {
1495  int f = 0;
1496  if (flags & FREE_OBJ_DROP_ABOVE_FLOOR)
1498  object_insert_in_map_at(op, part->map, NULL, f, part->x, part->y); /* Insert in same map as the envir */
1499  }
1500  }
1501  } FOR_INV_FINISH();
1502  }
1503  }
1504 
1505  if (ob->more != NULL) {
1506  object_free2(ob->more, flags);
1507  ob->more = NULL;
1508  }
1509 
1510  /* Remove object from the active list */
1511  ob->speed = 0;
1512  object_update_speed(ob);
1513 
1514  SET_FLAG(ob, FLAG_FREED);
1515  ob->count = 0;
1516 
1517  /* Remove this object from the list of used objects */
1518  if (ob->prev == NULL) {
1519  objects = ob->next;
1520  if (objects != NULL)
1521  objects->prev = NULL;
1522  } else {
1523  ob->prev->next = ob->next;
1524  if (ob->next != NULL)
1525  ob->next->prev = ob->prev;
1526  }
1527 
1528  if (ob->artifact != NULL) FREE_AND_CLEAR_STR(ob->artifact);
1529  if (ob->name != NULL) FREE_AND_CLEAR_STR(ob->name);
1530  if (ob->name_pl != NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1531  if (ob->title != NULL) FREE_AND_CLEAR_STR(ob->title);
1532  if (ob->race != NULL) FREE_AND_CLEAR_STR(ob->race);
1533  if (ob->slaying != NULL) FREE_AND_CLEAR_STR(ob->slaying);
1534  if (ob->skill != NULL) FREE_AND_CLEAR_STR(ob->skill);
1535  if (ob->lore != NULL) FREE_AND_CLEAR_STR(ob->lore);
1536  if (ob->msg != NULL) FREE_AND_CLEAR_STR(ob->msg);
1537  if (ob->materialname != NULL) FREE_AND_CLEAR_STR(ob->materialname);
1538  if (ob->discrete_damage != NULL) FREE_AND_CLEAR(ob->discrete_damage);
1539  if (ob->spell_tags) FREE_AND_CLEAR(ob->spell_tags);
1540 
1541  /* Why aren't events freed? */
1543 
1545 
1546  /* Test whether archetype is a temporary one, and if so look whether it should be trashed. */
1547  if (ob->arch && ob->arch->reference_count > 0) {
1548  if (--ob->arch->reference_count == 0) {
1549  free_arch(ob->arch);
1550  }
1551  }
1552 
1553 #ifdef MEMORY_DEBUG
1554  free(ob);
1555 #else
1556  /* Now link it with the free_objects list: */
1557  ob->prev = NULL;
1558  ob->next = free_objects;
1559  if (free_objects != NULL)
1560  free_objects->prev = ob;
1561  free_objects = ob;
1562  nroffreeobjects++;
1563 #endif
1564 }
1565 
1576  int i = 0;
1577  object *tmp = free_objects;
1578 
1579  while (tmp != NULL)
1580  tmp = tmp->next,
1581  i++;
1582  return i;
1583 }
1584 
1595  int i = 0;
1596  object *tmp = objects;
1597 
1598  while (tmp != NULL)
1599  tmp = tmp->next,
1600  i++;
1601  return i;
1602 }
1603 
1614  int i = 0;
1615  object *tmp = active_objects;
1616 
1617  while (tmp != NULL)
1618  tmp = tmp->active_next,
1619  i++;
1620  return i;
1621 }
1622 
1640 void object_sub_weight(object *op, signed long weight) {
1641  while (op != NULL) {
1642  if (op->type == CONTAINER) {
1643  weight = (signed long)(weight*(100-op->stats.Str)/100);
1644  }
1645  op->carrying -= weight;
1646  op = op->env;
1647  }
1648 }
1649 
1669 void object_remove(object *op) {
1670  object *last = NULL;
1671  object *otmp;
1672  tag_t tag;
1673  int check_walk_off;
1674  mapstruct *m;
1675  int16_t x, y;
1676 
1677  if (QUERY_FLAG(op, FLAG_REMOVED)) {
1678  StringBuffer *sb;
1679  char *diff;
1680 
1681  sb = stringbuffer_new();
1682  object_dump(op, sb);
1683  diff = stringbuffer_finish(sb);
1684  LOG(llevError, "Trying to remove removed object.\n%s\n", diff);
1685  free(diff);
1686  abort();
1687  }
1688  if (op->more != NULL)
1689  object_remove(op->more);
1690 
1691  SET_FLAG(op, FLAG_REMOVED);
1692 
1693  /*
1694  * In this case, the object to be removed is in someones
1695  * inventory.
1696  */
1697  /* TODO try to call a generic inventory weight adjusting function like object_sub_weight */
1698  if (op->env != NULL) {
1699  player *pl = NULL;
1700 
1701  if (op->nrof)
1702  object_sub_weight(op->env, op->weight*op->nrof);
1703  else
1704  object_sub_weight(op->env, op->weight+op->carrying);
1705 
1706  /* Update in two cases: item is in a player, or in a container the player is looking into. */
1707  if (op->env->contr != NULL && op->head == NULL) {
1708  pl = op->env->contr;
1709  } else if (op->env->type == CONTAINER && QUERY_FLAG(op->env, FLAG_APPLIED)) {
1710 
1711  if (op->env->env && op->env->env->contr)
1712  /* Container is in player's inventory. */
1713  pl = op->env->env->contr;
1714  else if (op->env->map) {
1715  /* Container on map, look above for player. */
1716  object *above = op->env->above;
1717 
1718  while (above && !above->contr)
1719  above = above->above;
1720  if (above)
1721  pl = above->contr;
1722  }
1723  }
1724 
1725  /* NO_FIX_PLAYER is set when a great many changes are being
1726  * made to players inventory. If set, avoiding the call
1727  * to save cpu time.
1728  */
1729  otmp = object_get_player_container(op->env);
1730  if (otmp != NULL
1731  && otmp->contr
1732  && !QUERY_FLAG(otmp, FLAG_NO_FIX_PLAYER))
1733  fix_object(otmp);
1734 
1735  if (op->above != NULL)
1736  op->above->below = op->below;
1737  else
1738  op->env->inv = op->below;
1739 
1740  if (op->below != NULL)
1741  op->below->above = op->above;
1742 
1743  /* we set up values so that it could be inserted into
1744  * the map, but we don't actually do that - it is up
1745  * to the caller to decide what we want to do.
1746  */
1747  op->x = op->env->x;
1748  op->y = op->env->y;
1749  op->ox = op->x;
1750  op->oy = op->y;
1751  op->map = op->env->map;
1752  op->above = NULL;
1753  op->below = NULL;
1754  /* send the delitem before resetting env, so container's contents be may
1755  * refreshed */
1756  if (LOOK_OBJ(op) && pl != NULL)
1757  esrv_del_item(pl, op);
1758  op->env = NULL;
1759  return;
1760  }
1761 
1762  /* If we get here, we are removing it from a map */
1763  if (op->map == NULL)
1764  return;
1765 
1766  if (op->contr != NULL && !op->contr->hidden)
1767  op->map->players--;
1768 
1769  x = op->x;
1770  y = op->y;
1771  m = get_map_from_coord(op->map, &x, &y);
1772 
1773  if (!m) {
1774  LOG(llevError, "object_remove called when object was on map but appears to not be within valid coordinates? %s (%d,%d)\n", op->map->path, op->x, op->y);
1775  abort();
1776  }
1777  if (op->map != m) {
1778  LOG(llevError, "object_remove: Object not really on map it claimed to be on? %s != %s, %d,%d != %d,%d\n", op->map->path, m->path, op->x, op->y, x, y);
1779  }
1780 
1781  /* link the object above us */
1782  if (op->above)
1783  op->above->below = op->below;
1784  else
1785  SET_MAP_TOP(m, x, y, op->below); /* we were top, set new top */
1786 
1787  /* Relink the object below us, if there is one */
1788  if (op->below) {
1789  op->below->above = op->above;
1790  } else {
1791  /* Nothing below, which means we need to relink map object for this space
1792  * use translated coordinates in case some oddness with map tiling is
1793  * evident
1794  */
1795  /*TODO is this check really needed?*/
1796  if (GET_MAP_OB(m, x, y) != op) {
1797  StringBuffer *sb;
1798  char *diff;
1799 
1800  sb = stringbuffer_new();
1801  object_dump(op, sb);
1802  diff = stringbuffer_finish(sb);
1803  LOG(llevError, "object_remove: GET_MAP_OB on %s does not return object to be removed even though it appears to be on the bottom?\n%s\n", m->path, diff);
1804  free(diff);
1805 
1806  sb = stringbuffer_new();
1807  object_dump(GET_MAP_OB(m, x, y), sb);
1808  diff = stringbuffer_finish(sb);
1809  LOG(llevError, "%s\n", diff);
1810  free(diff);
1811  }
1812  SET_MAP_OB(m, x, y, op->above); /* goes on above it. */
1813  }
1814  op->above = NULL;
1815  op->below = NULL;
1816 
1817  if (op->map->in_memory == MAP_SAVING)
1818  return;
1819 
1820  tag = op->count;
1821  check_walk_off = !QUERY_FLAG(op, FLAG_NO_APPLY);
1822  FOR_MAP_PREPARE(m, x, y, tmp) {
1823  /* No point updating the players look faces if he is the object
1824  * being removed.
1825  */
1826 
1827  if (tmp->type == PLAYER && tmp != op) {
1828  /* If a container that the player is currently using somehow gets
1829  * removed (most likely destroyed), update the player view
1830  * appropriately.
1831  */
1832  if (tmp->container == op) {
1833  CLEAR_FLAG(op, FLAG_APPLIED);
1834  tmp->container = NULL;
1835  }
1836  tmp->contr->socket.update_look = 1;
1837  }
1838  /* See if player moving off should effect something */
1839  if (check_walk_off
1840  && ((op->move_type&tmp->move_off) && (op->move_type&~tmp->move_off&~tmp->move_block) == 0)) {
1841  ob_move_on(tmp, op, NULL);
1842  if (object_was_destroyed(op, tag)) {
1843  LOG(llevError, "BUG: object_remove(): name %s, archname %s destroyed leaving object\n", tmp->name, tmp->arch->name);
1844  }
1845  }
1846 
1847  /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1848  if (tmp->above == tmp)
1849  tmp->above = NULL;
1850  last = tmp;
1851  } FOR_MAP_FINISH();
1852  /* last == NULL or there are no objects on this space */
1853  if (last == NULL) {
1854  /* set P_NEED_UPDATE, otherwise update_position will complain. In theory,
1855  * we could preserve the flags (GET_MAP_FLAGS), but update_position figures
1856  * those out anyways, and if there are any flags set right now, they won't
1857  * be correct anyways.
1858  */
1859  SET_MAP_FLAGS(op->map, op->x, op->y, P_NEED_UPDATE);
1860  update_position(op->map, op->x, op->y);
1861  } else
1863 
1864  if (QUERY_FLAG(op, FLAG_BLOCKSVIEW) || (op->glow_radius != 0))
1865  update_all_los(op->map, op->x, op->y);
1866 }
1867 
1884 object *object_merge(object *op, object *top) {
1885  if (!op->nrof)
1886  return NULL;
1887 
1888  if (top == NULL)
1889  for (top = op; top != NULL && top->above != NULL; top = top->above)
1890  ;
1892  if (top == op)
1893  continue;
1894  if (object_can_merge(op, top)) {
1895  object_increase_nrof(top, op->nrof);
1896  /*
1897  * Previous behavior set weight to zero here.
1898  * This, however, caused the object_sub_weight
1899  * call in object_remove to subtract zero weight
1900  * when removing the object. Thus, until inventory
1901  * weight is next recalculated, the object merged
1902  * into another pile added weight in object_increase_nrof
1903  * but did not remove the weight from the original
1904  * instance of itself in object_remove, essentially
1905  * counting for double weight for several minutes.
1906  *
1907  * SilverNexus 2014-05-27
1908  */
1909  object_remove(op);
1911  return top;
1912  }
1914  return NULL;
1915 }
1916 
1936 object *object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y) {
1937  object *tmp;
1938 
1939  op = HEAD(op);
1940  for (tmp = op; tmp; tmp = tmp->more) {
1941  tmp->x = x+tmp->arch->clone.x;
1942  tmp->y = y+tmp->arch->clone.y;
1943  tmp->map = m;
1944  }
1945  return object_insert_in_map(op, m, originator, flag);
1946 }
1947 
1968 void object_merge_spell(object *op, int16_t x, int16_t y) {
1969  int i;
1970 
1971  /* We try to do some merging of spell objects - if something has same owner,
1972  * is same type of spell, and going in the same direction, it is somewhat
1973  * mergable.
1974  *
1975  * If the spell object has an other_arch, don't merge - when the spell
1976  * does something, like explodes, it will use this other_arch, and
1977  * if we merge, there is no easy way to make the correct values be
1978  * set on this new object (values should be doubled, tripled, etc.)
1979  *
1980  * We also care about speed - only process objects that will not be
1981  * active this tick. Without this, the results are incorrect - think
1982  * of a case where tmp would normally get processed this tick, but
1983  * get merges with op, which does not get processed.
1984  */
1985  FOR_MAP_PREPARE(op->map, x, y, tmp) {
1986  if (op->type == tmp->type
1987  && op->subtype == tmp->subtype
1988  && op->direction == tmp->direction
1989  && op->owner == tmp->owner && op->ownercount == tmp->ownercount
1990  && op->range == tmp->range
1991  && op->stats.wc == tmp->stats.wc
1992  && op->level == tmp->level
1993  && op->attacktype == tmp->attacktype
1994  && op->speed == tmp->speed
1995  && !tmp->other_arch
1996  && (tmp->speed_left+tmp->speed) < 0.0
1997  && op != tmp) {
1998  /* Quick test - if one or the other objects already have hash tables
1999  * set up, and that hash bucket contains a value that doesn't
2000  * match what we want to set it up, we won't be able to merge.
2001  * Note that these two if statements are the same, except
2002  * for which object they are checking against. They could
2003  * be merged, but the line wrapping would be large enough
2004  * that IMO it would become difficult to read the different clauses
2005  * so its cleaner just to do 2 statements - MSW
2006  */
2007  if (op->spell_tags
2008  && !OB_SPELL_TAG_MATCH(op, (tag_t)tmp->stats.maxhp)
2009  && OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) != 0)
2010  continue;
2011 
2012  if (tmp->spell_tags
2013  && !OB_SPELL_TAG_MATCH(tmp, (tag_t)op->stats.maxhp)
2014  && OB_SPELL_TAG_HASH(tmp, op->stats.maxhp) != 0)
2015  continue;
2016 
2017  /* If we merge, the data from tmp->spell_tags gets copied into op.
2018  * so we need to make sure that slot isn't filled up.
2019  */
2020  if (tmp->spell_tags
2021  && !OB_SPELL_TAG_MATCH(tmp, (tag_t)tmp->stats.maxhp)
2022  && OB_SPELL_TAG_HASH(tmp, tmp->stats.maxhp) != 0)
2023  continue;
2024 
2025  /* If both objects have spell_tags, we need to see if there are conflicting
2026  * values - if there are, we won't be able to merge then.
2027  */
2028  if (tmp->spell_tags && op->spell_tags) {
2029  int need_copy = 0;
2030 
2031  for (i = 0; i < SPELL_TAG_SIZE; i++) {
2032  /* If the two tag values in the hash are set, but are
2033  * not set to the same value, then these objects
2034  * can not be merged.
2035  */
2036  if (op->spell_tags[i] && tmp->spell_tags[i]
2037  && op->spell_tags[i] != tmp->spell_tags[i]) {
2039  break;
2040  }
2041  /* If one tag is set and the other is not, that is
2042  * fine, but we have to note that we need to copy
2043  * the data in that case.
2044  */
2045  if ((!op->spell_tags[i] && tmp->spell_tags[i])
2046  || (op->spell_tags[i] && !tmp->spell_tags[i])) {
2047  need_copy = 1;
2048  }
2049  }
2050  /* If we did not get through entire array, it means
2051  * we got a conflicting hash, and so we won't be
2052  * able to merge these - just continue processing
2053  * object on this space.
2054  */
2055  if (i <= SPELL_TAG_SIZE)
2056  continue;
2057 
2058  /* Ok - everything checked out - we should be able to
2059  * merge tmp in op. So lets copy the tag data if
2060  * needed. Note that this is a selective copy, as
2061  * we don't want to clear values that may be set in op.
2062  */
2063  if (need_copy) {
2064  for (i = 0; i < SPELL_TAG_SIZE; i++)
2065  if (!op->spell_tags[i]
2066  && tmp->spell_tags[i]
2067  && tmp->spell_tags[i] != (tag_t)op->stats.maxhp)
2068  op->spell_tags[i] = tmp->spell_tags[i];
2069  }
2070  FREE_AND_CLEAR(tmp->spell_tags);
2071  }
2072 
2073  /* if tmp has a spell_tags table, copy it to op and free tmps */
2074  if (tmp->spell_tags && !op->spell_tags) {
2075  op->spell_tags = tmp->spell_tags;
2076  tmp->spell_tags = NULL;
2077 
2078  /* We don't need to keep a copy of our maxhp value
2079  * in the copied over value
2080  */
2081  if (OB_SPELL_TAG_MATCH(op, (tag_t)op->stats.maxhp))
2082  OB_SPELL_TAG_HASH(op, op->stats.maxhp) = 0;
2083  }
2084 
2085  /* For spells to work correctly, we need to record what spell
2086  * tags we've merged in with this effect. This is used
2087  * in ok_to_put_more() to see if a spell effect is already on
2088  * the space.
2089  */
2090  if (op->stats.maxhp != tmp->stats.maxhp) {
2091 #ifdef OBJECT_DEBUG
2092  /* This if statement should never happen - the logic above should
2093  * have prevented it. It is a problem, because by now its possible
2094  * we've destroyed the spell_tags in tmp, so we can't really
2095  * just bail out.
2096  */
2097 
2098  if (op->spell_tags
2099  && OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) != 0
2100  && !OB_SPELL_TAG_MATCH(op, tmp->stats.maxhp)) {
2101  LOG(llevError, "object_insert_in_map: Got non matching spell tags: %d != %d\n", OB_SPELL_TAG_HASH(op, tmp->stats.maxhp), tmp->stats.maxhp);
2102  }
2103 #endif
2104  if (!op->spell_tags)
2105  op->spell_tags = calloc(SPELL_TAG_SIZE, sizeof(tag_t));
2106 
2107  OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) = tmp->stats.maxhp;
2108  }
2109 
2111  op->speed_left = MAX(op->speed_left, tmp->speed_left);
2112 
2113  if (tmp->duration != op->duration) {
2114  /* We need to use tmp_dam here because otherwise the
2115  * calculations can overflow the size of stats.dam.
2116  */
2117  int tmp_dam = tmp->stats.dam*(tmp->duration+1)+
2118  op->stats.dam*(op->duration+1);
2119 
2120  op->duration = MAX(op->duration, tmp->duration);
2121  tmp_dam /= op->duration+1;
2122  op->stats.dam = tmp_dam+1;
2123  } else {
2124  /* in this case, duration is the same, so simply adding
2125  * up damage works.
2126  */
2127  op->stats.dam += tmp->stats.dam;
2128  }
2129 
2130  object_remove(tmp);
2132  }
2133  } FOR_MAP_FINISH();
2134 }
2135 
2167 object *object_insert_in_map(object *op, mapstruct *m, object *originator, int flag) {
2168  object *tmp, *top, *floor = NULL;
2169  int16_t x, y;
2170 
2171  if (QUERY_FLAG(op, FLAG_FREED)) {
2172  LOG(llevError, "Trying to insert freed object!\n");
2173  return NULL;
2174  }
2175  if (m == NULL) {
2176  StringBuffer *sb;
2177  char *diff;
2178 
2179  sb = stringbuffer_new();
2180  object_dump(op, sb);
2181  diff = stringbuffer_finish(sb);
2182  LOG(llevError, "Trying to insert in null-map!\n%s\n", diff);
2183  free(diff);
2184  return op;
2185  }
2186  if (out_of_map(m, op->x, op->y)) {
2187  StringBuffer *sb;
2188  char *diff;
2189 
2190  sb = stringbuffer_new();
2191  object_dump(op, sb);
2192  diff = stringbuffer_finish(sb);
2193  LOG(llevError, "Trying to insert object outside the map.\n%s\n", diff);
2194  free(diff);
2195 #ifdef MANY_CORES
2196  /* Better to catch this here, as otherwise the next use of this object
2197  * is likely to cause a crash. Better to find out where it is getting
2198  * improperly inserted.
2199  */
2200  abort();
2201 #endif
2202  return op;
2203  }
2204  if (!QUERY_FLAG(op, FLAG_REMOVED)) {
2205  StringBuffer *sb;
2206  char *diff;
2207 
2208  sb = stringbuffer_new();
2209  object_dump(op, sb);
2210  diff = stringbuffer_finish(sb);
2211  LOG(llevError, "Trying to insert (map) inserted object.\n%s\n", diff);
2212  free(diff);
2213  return op;
2214  }
2215  if (op->more != NULL) {
2216  /* The part may be on a different map. */
2217 
2218  object *more = op->more;
2219 
2220  /* We really need the caller to normalize coordinates - if
2221  * we set the map, that doesn't work if the location is within
2222  * a map and this is straddling an edge. So only if coordinate
2223  * is clear wrong do we normalize it.
2224  */
2225  if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
2226  /* Debugging information so you can see the last coordinates this object had */
2227  more->ox = more->x;
2228  more->oy = more->y;
2229  more->map = get_map_from_coord(more->map, &more->x, &more->y);
2230  } else if (!more->map) {
2231  /* For backwards compatibility - when not dealing with tiled maps,
2232  * more->map should always point to the parent.
2233  */
2234  more->map = m;
2235  }
2236 
2237  if (object_insert_in_map(more, more->map, originator, flag) == NULL) {
2238  if (!op->head)
2239  LOG(llevError, "BUG: object_insert_in_map(): inserting op->more killed op\n");
2240  return NULL;
2241  }
2242  }
2243  CLEAR_FLAG(op, FLAG_REMOVED);
2244 
2245  /* Debugging information so you can see the last coordinates this object had */
2246  op->ox = op->x;
2247  op->oy = op->y;
2248  x = op->x;
2249  y = op->y;
2250  op->map = get_map_from_coord(m, &x, &y);
2251 
2252  /* this has to be done after we translate the coordinates. */
2253  if (op->nrof
2254  && !(flag&INS_NO_MERGE)
2255  && op->type != SPELL_EFFECT) {
2256  FOR_MAP_PREPARE(op->map, x, y, tmp) {
2257  if (object_can_merge(op, tmp)) {
2258  op->nrof += tmp->nrof;
2259  object_remove(tmp);
2261  }
2262  } FOR_MAP_FINISH();
2263  } else if (op->type == SPELL_EFFECT
2264  && !op->range
2265  && !op->other_arch
2266  && (op->speed_left+op->speed) < 0.0) {
2267  object_merge_spell(op, x, y);
2268  }
2269 
2270  /* Ideally, the caller figures this out. However, it complicates a lot
2271  * of areas of callers (eg, anything that uses object_find_free_spot() would now
2272  * need extra work
2273  */
2274  if (op->map != m) {
2275  /* coordinates should not change unless map also changes */
2276  op->x = x;
2277  op->y = y;
2278  }
2279 
2280  if (op->type != LAMP)
2281  /* lamps use the FLAG_APPLIED to keep the light/unlit status, so don't reset it.
2282  Other objects just get unapplied, since the container "drops" them. */
2283  CLEAR_FLAG(op, FLAG_APPLIED);
2285  if (!QUERY_FLAG(op, FLAG_ALIVE))
2287 
2288  /* In many places, a player is passed as the originator, which
2289  * is fine. However, if the player is on a transport, they are not
2290  * actually on the map, so we can't use them for the linked pointers,
2291  * nor should the walk on function below use them either.
2292  */
2293  if (originator && originator->contr && originator->contr->transport)
2294  originator = originator->contr->transport;
2295 
2296  if (flag&INS_BELOW_ORIGINATOR) {
2297  if (originator->map != op->map
2298  || originator->x != op->x
2299  || originator->y != op->y) {
2300  LOG(llevError, "object_insert_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n");
2301  abort();
2302  }
2303  op->above = originator;
2304  op->below = originator->below;
2305  if (op->below)
2306  op->below->above = op;
2307  else
2308  SET_MAP_OB(op->map, op->x, op->y, op);
2309  /* since *below *originator, no need to update top */
2310  originator->below = op;
2311  } else {
2312  /* If there are other objects, then */
2313  /* This test is incorrect i think, as ins_above_floor_only needs the floor variable
2314  if ((!(flag&INS_MAP_LOAD)) && ((top = GET_MAP_OB(op->map, op->x, op->y)) != NULL)) {
2315  */
2316  object *last;
2317 
2318  /*
2319  * If there are multiple objects on this space, we do some trickier handling.
2320  * We've already dealt with merging if appropriate.
2321  * Generally, we want to put the new object on top. But if
2322  * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last
2323  * floor, we want to insert above that and no further.
2324  * Also, if there are spell objects on this space, we stop processing
2325  * once we get to them. This reduces the need to traverse over all of
2326  * them when adding another one - this saves quite a bit of cpu time
2327  * when lots of spells are cast in one area. Currently, it is presumed
2328  * that flying non pickable objects are spell objects.
2329  */
2330  last = NULL;
2331  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
2332  if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)
2333  || QUERY_FLAG(tmp, FLAG_OVERLAY_FLOOR))
2334  floor = tmp;
2335 
2336  if (QUERY_FLAG(tmp, FLAG_NO_PICK)
2338  && !QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2339  /* We insert above tmp, so we want this object below this */
2340  break;
2341  }
2342  last = tmp;
2343  } FOR_MAP_FINISH();
2344  top = last;
2345 
2346  if (flag&INS_MAP_LOAD)
2347  top = GET_MAP_TOP(op->map, op->x, op->y);
2348  if (flag&INS_ABOVE_FLOOR_ONLY)
2349  top = floor;
2350 
2351  /* Top is the object that our object (op) is going to get inserted above. */
2352 
2353  /* First object on this space */
2354  if (!top) {
2355  op->above = GET_MAP_OB(op->map, op->x, op->y);
2356  if (op->above)
2357  op->above->below = op;
2358  op->below = NULL;
2359  SET_MAP_OB(op->map, op->x, op->y, op);
2360  } else { /* get inserted into the stack above top */
2361  op->above = top->above;
2362  if (op->above)
2363  op->above->below = op;
2364  op->below = top;
2365  top->above = op;
2366  }
2367  if (op->above == NULL)
2368  SET_MAP_TOP(op->map, op->x, op->y, op);
2369  } /* else not INS_BELOW_ORIGINATOR */
2370 
2371  if (op->type == PLAYER)
2372  op->contr->do_los = 1;
2373 
2374  /* If we have a floor, we know the player, if any, will be above
2375  * it, so save a few ticks and start from there.
2376  */
2377  if (!(flag&INS_MAP_LOAD)) {
2378  tmp = floor ? floor : GET_MAP_OB(op->map, op->x, op->y);
2380  if (tmp->type == PLAYER)
2381  tmp->contr->socket.update_look = 1;
2383  }
2384 
2385  /* If this object glows, it may affect lighting conditions that are
2386  * visible to others on this map. But update_all_los is really
2387  * an inefficient way to do this, as it means los for all players
2388  * on the map will get recalculated. The players could very well
2389  * be far away from this change and not affected in any way -
2390  * this should get redone to only look for players within range,
2391  * or just updating the P_NEED_UPDATE for spaces within this area
2392  * of effect may be sufficient.
2393  */
2394  if (MAP_DARKNESS(op->map) && (op->glow_radius != 0))
2395  update_all_los(op->map, op->x, op->y);
2396 
2397  /* updates flags (blocked, alive, no magic, etc) for this map space */
2399 
2400  if (op->contr && !op->contr->hidden)
2401  op->map->players++;
2402 
2403  /* Don't know if moving this to the end will break anything. However,
2404  * we want to have update_look set above before calling this.
2405  *
2406  * object_check_move_on() must be after this because code called from
2407  * object_check_move_on() depends on correct map flags (so functions like
2408  * blocked() and wall() work properly), and these flags are updated by
2409  * object_update().
2410  */
2411 
2412  /* if this is not the head or flag has been passed, don't check walk on status */
2413 
2414  if (!(flag&INS_NO_WALK_ON) && !op->head) {
2415  if (object_check_move_on(op, originator))
2416  return NULL;
2417 
2418  /* If we are a multi part object, lets work our way through the check
2419  * walk on's.
2420  */
2421  for (tmp = op->more; tmp != NULL; tmp = tmp->more)
2422  if (object_check_move_on(tmp, originator))
2423  return NULL;
2424  }
2425  return op;
2426 }
2427 
2440 void object_replace_insert_in_map(const char *arch_string, object *op) {
2441  object *tmp1;
2442 
2443  /* first search for itself and remove any old instances */
2444  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
2445  if (!strcmp(tmp->arch->name, arch_string)) { /* same archetype */
2446  object_remove(tmp);
2448  }
2449  } FOR_MAP_FINISH();
2450 
2451  tmp1 = arch_to_object(find_archetype(arch_string));
2452  object_insert_in_map_at(tmp1, op->map, op, INS_BELOW_ORIGINATOR, op->x, op->y);
2453 }
2454 
2478 object *object_split(object *orig_ob, uint32_t nr, char *err, size_t size) {
2479  object *newob;
2480 
2481  if (MAX(1, orig_ob->nrof) < nr) {
2482  /* If err is set, the caller knows that nr can be wrong (player trying to drop items), thus don't log that. */
2483  if (err)
2484  snprintf(err, size, "There are only %u %ss.", orig_ob->nrof ? orig_ob->nrof : 1, orig_ob->name);
2485  else
2486  LOG(llevDebug, "There are only %u %ss.\n", orig_ob->nrof ? orig_ob->nrof : 1, orig_ob->name);
2487  return NULL;
2488  }
2489  newob = object_create_clone(orig_ob);
2490  if (orig_ob->nrof == 0) {
2491  if (!QUERY_FLAG(orig_ob, FLAG_REMOVED)) {
2492  object_remove(orig_ob);
2493  }
2495  } else {
2496  newob->nrof = nr;
2497  object_decrease_nrof(orig_ob, nr);
2498  }
2499 
2500  return newob;
2501 }
2502 
2520 object *object_decrease_nrof(object *op, uint32_t i) {
2521  object *tmp;
2522 
2523  if (i == 0) /* objects with op->nrof require this check */
2524  return op;
2525 
2526  if (i > op->nrof)
2527  i = op->nrof;
2528 
2529  if (QUERY_FLAG(op, FLAG_REMOVED)) {
2530  op->nrof -= i;
2531  } else if (op->env != NULL) {
2532  if (i < op->nrof) {
2533  player *pl;
2534  /* is this object in the players inventory, or sub container
2535  * therein?
2536  */
2537  tmp = object_get_player_container(op->env);
2538  /* nope. Is this a container the player has opened?
2539  * If so, set tmp to that player.
2540  * IMO, searching through all the players will mostly
2541  * likely be quicker than following op->env to the map,
2542  * and then searching the map for a player.
2543  */
2544  if (!tmp) {
2545  for (pl = first_player; pl; pl = pl->next)
2546  if (pl->ob->container == op->env)
2547  break;
2548  if (pl)
2549  tmp = pl->ob;
2550  else
2551  tmp = NULL;
2552  }
2553 
2554  /* Because of weight reduction by container and integer arithmetic,
2555  * there is no guarantee the rounded weight of combined items will be
2556  * the same as the sum of rounded weights.
2557  * Therefore just remove the current weight, and add the new.
2558  * Same adjustment done in object_increase_nrof().
2559  */
2560  object_sub_weight(op->env, op->weight * op->nrof);
2561  op->nrof -= i;
2562  object_add_weight(op->env, op->weight * op->nrof);
2563  if (tmp) {
2564  esrv_update_item(UPD_NROF, tmp, op);
2565  fix_object(tmp);
2566  }
2567  } else {
2568  object_remove(op);
2569  op->nrof = 0;
2570  }
2571  } else {
2572  /* On a map. */
2573  if (i < op->nrof) {
2574  op->nrof -= i;
2575 
2576  FOR_MAP_PREPARE(op->map, op->x, op->y, pl)
2577  if (pl->contr) {
2578  pl->contr->socket.update_look = 1;
2579  break;
2580  }
2581  FOR_MAP_FINISH();
2582  } else {
2583  object_remove(op);
2584  op->nrof = 0;
2585  }
2586  }
2587 
2588  if (op->nrof) {
2589  return op;
2590  } else {
2592  return NULL;
2593  }
2594 }
2595 
2606 static void object_increase_nrof(object *op, uint32_t i) {
2607  object *tmp;
2608 
2609  if (i == 0) /* objects with op->nrof require this check */
2610  return;
2611 
2612  if (QUERY_FLAG(op, FLAG_REMOVED)) {
2613  op->nrof += i;
2614  } else if (op->env != NULL) {
2615  player *pl;
2616  /* is this object in the players inventory, or sub container
2617  * therein?
2618  */
2619  tmp = object_get_player_container(op->env);
2620  /* nope. Is this a container the player has opened?
2621  * If so, set tmp to that player.
2622  * IMO, searching through all the players will mostly
2623  * likely be quicker than following op->env to the map,
2624  * and then searching the map for a player.
2625  */
2626  if (!tmp) {
2627  for (pl = first_player; pl; pl = pl->next)
2628  if (pl->ob->container == op->env)
2629  break;
2630  if (pl)
2631  tmp = pl->ob;
2632  else
2633  tmp = NULL;
2634  }
2635 
2636  /* Because of weight reduction by container and integer arithmetic,
2637  * there is no guarantee the rounded weight of combined items will be
2638  * the same as the sum of rounded weights.
2639  * Therefore just remove the current weight, and add the new.
2640  * Same adjustment done in object_decrease_nrof().
2641  */
2642  object_sub_weight(op->env, op->weight * op->nrof);
2643  op->nrof += i;
2644  object_add_weight(op->env, op->weight * op->nrof);
2645  if (tmp) {
2646  esrv_update_item(UPD_NROF, tmp, op);
2647  }
2648  } else {
2649  /* On a map. */
2650  op->nrof += i;
2651 
2652  FOR_MAP_PREPARE(op->map, op->x, op->y, pl)
2653  if (pl->contr) {
2654  pl->contr->socket.update_look = 1;
2655  break;
2656  }
2657  FOR_MAP_FINISH();
2658  }
2659 }
2660 
2678 void object_add_weight(object *op, signed long weight) {
2679  while (op != NULL) {
2680  if (op->type == CONTAINER) {
2681  weight = (signed long)(weight*(100-op->stats.Str)/100);
2682  }
2683  op->carrying += weight;
2684  op = op->env;
2685  }
2686 }
2687 
2705 object *object_insert_in_ob(object *op, object *where) {
2706  object *otmp;
2707 
2708  if (!QUERY_FLAG(op, FLAG_REMOVED)) {
2709  StringBuffer *sb;
2710  char *diff;
2711 
2712  sb = stringbuffer_new();
2713  object_dump(op, sb);
2714  diff = stringbuffer_finish(sb);
2715  LOG(llevError, "Trying to insert (ob) inserted object.\n%s\n", diff);
2716  free(diff);
2717  return op;
2718  }
2719 
2720  if (where == NULL) {
2721  StringBuffer *sb;
2722  char *diff;
2723 
2724  sb = stringbuffer_new();
2725  object_dump(op, sb);
2726  diff = stringbuffer_finish(sb);
2727  LOG(llevError, "Trying to put object in NULL.\n%s\n", diff);
2728  free(diff);
2729  return op;
2730  }
2731  if (where->head) {
2732  LOG(llevDebug, "Warning: Tried to insert object wrong part of multipart object.\n");
2733  }
2734  where = HEAD(where);
2735  if (op->more) {
2736  LOG(llevError, "Tried to insert multipart object %s (%u)\n", op->name, op->count);
2737  return op;
2738  }
2740  CLEAR_FLAG(op, FLAG_REMOVED);
2741  if (op->nrof) {
2742  FOR_INV_PREPARE(where, tmp)
2743  if (object_can_merge(tmp, op)) {
2744  /* return the original object and remove inserted object
2745  * (client needs the original object) */
2746  object_increase_nrof(tmp, op->nrof);
2747  SET_FLAG(op, FLAG_REMOVED);
2748  object_free2(op, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK); /* free the inserted object */
2749  return tmp;
2750  }
2751  FOR_INV_FINISH();
2752 
2753  /* the item couldn't merge. */
2754  object_add_weight(where, op->weight*op->nrof);
2755  } else
2756  object_add_weight(where, op->weight+op->carrying);
2757 
2758  op->map = NULL;
2759  op->env = where;
2760  op->above = NULL;
2761  op->below = NULL;
2762  op->x = 0,
2763  op->y = 0;
2764  op->ox = 0,
2765  op->oy = 0;
2766 
2767  /* Client has no idea of ordering so lets not bother ordering it here.
2768  * It sure simplifies this function...
2769  */
2770  if (where->inv == NULL)
2771  where->inv = op;
2772  else {
2773  op->below = where->inv;
2774  op->below->above = op;
2775  where->inv = op;
2776  }
2777 
2778  /* Update in 2 cases: object goes into player's inventory, or object goes into container the player
2779  * is looking into. */
2780  if (where->contr != NULL)
2781  esrv_send_item(where, op);
2782  else if (where->type == CONTAINER && QUERY_FLAG(where, FLAG_APPLIED)) {
2783  object *pl = NULL;
2784 
2785  if (op->env->env && op->env->env->contr)
2786  /* Container is in player's inventory. */
2787  pl = op->env->env;
2788  else if (op->env->map) {
2789  /* Container on map, look above for player. */
2790  FOR_ABOVE_PREPARE(op->env, above)
2791  if (above->contr) {
2792  pl = above;
2793  break;
2794  }
2795  FOR_ABOVE_FINISH();
2796  }
2797  if (pl)
2798  esrv_send_item(pl, op);
2799  }
2800 
2801  otmp = object_get_player_container(where);
2802  if (otmp && otmp->contr != NULL) {
2803  if (!QUERY_FLAG(otmp, FLAG_NO_FIX_PLAYER)
2804  && (QUERY_FLAG(op, FLAG_APPLIED) || op->type == SKILL || op->glow_radius != 0))
2805  /* fix_object will only consider applied items, or skills, or items with a glow radius.
2806  thus no need to call it if our object hasn't that. */
2807  fix_object(otmp);
2808  }
2809 
2810  /* reset the light list and los of the players on the map */
2811  if (op->glow_radius != 0 && where->map) {
2812 #ifdef DEBUG_LIGHTS
2813  LOG(llevDebug, " object_insert_in_ob(): got %s to insert in map/op\n", op->name);
2814 #endif /* DEBUG_LIGHTS */
2815  if (MAP_DARKNESS(where->map)) {
2816  SET_MAP_FLAGS(where->map, where->x, where->y, P_NEED_UPDATE);
2817  update_position(where->map, where->x, where->y);
2818  update_all_los(where->map, where->x, where->y);
2819  }
2820  }
2821 
2822  return op;
2823 }
2824 
2850 int object_check_move_on(object *op, object *originator) {
2851  object *tmp;
2852  tag_t tag;
2853  mapstruct *m = op->map;
2854  int x = op->x, y = op->y;
2855  MoveType move_on, move_slow, move_block;
2856 
2857  if (QUERY_FLAG(op, FLAG_NO_APPLY))
2858  return 0;
2859 
2860  tag = op->count;
2861 
2862  move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
2863  move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
2864  move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
2865 
2866  /* if nothing on this space will slow op down or be applied,
2867  * no need to do checking below. have to make sure move_type
2868  * is set, as lots of objects don't have it set - we treat that
2869  * as walking.
2870  */
2871  if (op->move_type
2872  && !(op->move_type&move_on)
2873  && !(op->move_type&move_slow))
2874  return 0;
2875 
2876  /* This is basically inverse logic of that below - basically,
2877  * if the object can avoid the move on or slow move, they do so,
2878  * but can't do it if the alternate movement they are using is
2879  * blocked. Logic on this seems confusing, but does seem correct.
2880  */
2881  if ((op->move_type&~move_on&~move_block) != 0
2882  && (op->move_type&~move_slow&~move_block) != 0)
2883  return 0;
2884 
2885  /* The objects have to be checked from top to bottom.
2886  * Hence, we first go to the top:
2887  */
2888 
2889  tmp = GET_MAP_OB(op->map, op->x, op->y);
2891  if (tmp->above == NULL)
2892  break;
2893  /* Trim the search when we find the first other spell effect
2894  * this helps performance so that if a space has 50 spell objects,
2895  * we don't need to check all of them.
2896  */
2897  if ((tmp->move_type&MOVE_FLY_LOW) && QUERY_FLAG(tmp, FLAG_NO_PICK))
2898  break;
2901  if (tmp == op)
2902  continue; /* Can't apply yourself */
2903 
2904  /* Check to see if one of the movement types should be slowed down.
2905  * Second check makes sure that the movement types not being slowed
2906  * (~slow_move) is not blocked on this space - just because the
2907  * space doesn't slow down swimming (for example), if you can't actually
2908  * swim on that space, can't use it to avoid the penalty.
2909  */
2910  if (!QUERY_FLAG(op, FLAG_WIZPASS)) {
2911  if ((!op->move_type && tmp->move_slow&MOVE_WALK)
2912  || ((op->move_type&tmp->move_slow) && (op->move_type&~tmp->move_slow&~tmp->move_block) == 0)) {
2913  float diff;
2914 
2915  diff = tmp->move_slow_penalty*FABS(op->speed);
2916  if (op->type == PLAYER) {
2919  diff /= 4.0;
2920  }
2921  }
2922  op->speed_left -= diff;
2923  }
2924  }
2925 
2926  /* Basically same logic as above, except now for actual apply. */
2927  if ((!op->move_type && tmp->move_on&MOVE_WALK)
2928  || ((op->move_type&tmp->move_on) && (op->move_type&~tmp->move_on&~tmp->move_block) == 0)) {
2929  ob_move_on(tmp, op, originator);
2930  if (object_was_destroyed(op, tag))
2931  return 1;
2932 
2933  /* what the person/creature stepped onto has moved the object
2934  * someplace new. Don't process any further - if we did,
2935  * have a feeling strange problems would result.
2936  */
2937  if (op->map != m || op->x != x || op->y != y)
2938  return 0;
2939  }
2941  return 0;
2942 }
2943 
2959 object *map_find_by_archetype(mapstruct *m, int x, int y, const archetype *at) {
2960  if (m == NULL || out_of_map(m, x, y)) {
2961  LOG(llevError, "Present_arch called outside map.\n");
2962  return NULL;
2963  }
2964 
2965  FOR_MAP_PREPARE(m, x, y, tmp)
2966  if (tmp->arch == at)
2967  return tmp;
2968  FOR_MAP_FINISH();
2969 
2970  return NULL;
2971 }
2972 
2989 object *map_find_by_type(mapstruct *m, int x, int y, uint8_t type) {
2990  if (out_of_map(m, x, y)) {
2991  LOG(llevError, "Present called outside map.\n");
2992  return NULL;
2993  }
2994 
2995  FOR_MAP_PREPARE(m, x, y, tmp)
2996  if (tmp->type == type)
2997  return tmp;
2998  FOR_MAP_FINISH();
2999 
3000  return NULL;
3001 }
3002 
3016 object *object_present_in_ob(uint8_t type, const object *op) {
3017  object *tmp;
3018 
3019  for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
3020  if (tmp->type == type)
3021  return tmp;
3022 
3023  return NULL;
3024 }
3025 
3054 object *object_present_in_ob_by_name(int type, const char *str, const object *op) {
3055  object *tmp;
3056 
3057  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
3058  if ((type == -1 || tmp->type == type) && !strcmp(str, tmp->name))
3059  return tmp;
3060  }
3061  return NULL;
3062 }
3063 
3076 object *arch_present_in_ob(const archetype *at, const object *op) {
3077  object *tmp;
3078 
3079  for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
3080  if (tmp->arch == at)
3081  return tmp;
3082  return NULL;
3083 }
3084 
3096 void object_set_flag_inv(object*op, int flag) {
3097  object *tmp;
3098 
3099  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
3100  SET_FLAG(tmp, flag);
3101  object_set_flag_inv(tmp, flag);
3102  }
3103 }
3104 
3116 void object_unset_flag_inv(object*op, int flag) {
3117  object *tmp;
3118 
3119  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
3120  CLEAR_FLAG(tmp, flag);
3121  object_unset_flag_inv(tmp, flag);
3122  }
3123 }
3124 
3137 void object_set_cheat(object *op) {
3138  SET_FLAG(op, FLAG_WAS_WIZ);
3140 }
3141 
3162 int object_find_multi_free_spot_around(const object *ob, const object *gen, int16_t *hx, int16_t *hy) {
3163  int genx, geny, genx2, geny2, sx, sy, sx2, sy2, ix, iy, nx, ny, i, flag;
3164  int freecount = 0;
3165 
3166  ob = HEAD(ob);
3167 
3168  object_get_multi_size(ob, &sx, &sy, &sx2, &sy2);
3169  object_get_multi_size(gen, &genx, &geny, &genx2, &geny2);
3170  /*
3171  * sx and sy are now the coords of the bottom right corner of ob relative to the head.
3172  * genx and geny are now the coords of the bottom right corner of gen relative to the head.
3173  * sx2 and sy2 are now the coords of the head of ob relative to the top left corner.
3174  * genx2 and geny2 are now the coords of the head of gen relative to the top left corner.
3175  */
3176 
3177  sx++;
3178  sy++;
3179  genx++;
3180  geny++;
3181  /*
3182  * sx, sy, genx, and geny, are now the size of the object, excluding parts left and above
3183  * the head.
3184  */
3185 
3186  ix = gen->x-sx-genx2;
3187  iy = gen->y-sy-geny2;
3188  sx += genx+sx2;
3189  sy += geny+sy2;
3190  /*
3191  * ix and iy are the map coords of the top left square where the head of ob could possibly
3192  * be placed. sx and sy are now the size of the square to search for placement of the head
3193  * relative to ix and iy.
3194  */
3195 
3196  /*
3197  * Loop around the square of possible positions for the head of ob object:
3198  */
3199  for (i = 0; i < (sx+sx+sy+sy); i++) {
3200  if (i <= sx) {
3201  nx = i+ix;
3202  ny = iy;
3203  } else if (i <= sx+sy) {
3204  nx = ix+sx;
3205  ny = iy+i-sx;
3206  } else if (i <= sx+sy+sx) {
3207  nx = ix+sx-(i-(sx+sy));
3208  ny = iy+sy;
3209  } else {
3210  nx = ix;
3211  ny = iy+sy-(i-(sx+sy+sx));
3212  }
3213  /* Check if the spot is free. */
3214  flag = ob_blocked(ob, gen->map, nx, ny);
3215  if (!flag) {
3216  freecount++;
3217  }
3218  }
3219  /* If no free spaces, return. */
3220  if (!freecount)
3221  return -1;
3222 
3223  /* Choose a random valid position */
3224  freecount = RANDOM()%freecount;
3225  for (i = 0; i < sx+sx+sy+sy; i++) {
3226  if (i <= sx) {
3227  nx = i+ix;
3228  ny = iy;
3229  } else if (i <= sx+sy) {
3230  nx = ix+sx;
3231  ny = iy+i-sx;
3232  } else if (i <= sx+sy+sx) {
3233  nx = ix+sx-(i-(sx+sy));
3234  ny = iy+sy;
3235  } else {
3236  nx = ix;
3237  ny = iy+sy-(i-(sx+sy+sx));
3238  }
3239 
3240  /* Make sure it's within map. */
3241  if (nx < 0 || nx >= MAP_WIDTH(gen->map)
3242  || ny < 0 || ny >= MAP_HEIGHT(gen->map))
3243  continue;
3244 
3245  /* Check if the spot is free.*/
3246  flag = ob_blocked(ob, gen->map, nx, ny);
3247  if (!flag) {
3248  freecount--;
3249  if (freecount <= 0) {
3250  *hx = nx;
3251  *hy = ny;
3252  return 0;
3253  }
3254  }
3255  }
3256  return -1;
3257 }
3258 
3281 int object_find_multi_free_spot_within_radius(const object *ob, const object *gen, int *hx, int *hy) {
3282  int genx, geny, genx2, geny2, sx, sy, sx2, sy2, ix, iy, nx, ny, i, flag;
3283  int8_t x, y, radius;
3284  int freecount = 0, freecountstop = 0;
3285  const char *value;
3286  int8_t *x_array;
3287  int8_t *y_array;
3288 
3289  /* If radius is not set, default to 1 */
3290  value = object_get_value(gen, "generator_radius");
3291  if (value) {
3292  radius = (int8_t)strtol(value, NULL, 10);
3293  if (radius < 1) {
3294  radius = 1;
3295  }
3296  } else {
3297  radius = 1;
3298  }
3299 
3300  ob = HEAD(ob);
3301 
3302  object_get_multi_size(ob, &sx, &sy, &sx2, &sy2);
3303  object_get_multi_size(gen, &genx, &geny, &genx2, &geny2);
3304  /*
3305  * sx and sy are now the coords of the bottom right corner
3306  * of ob relative to the head.
3307  * genx and geny are now the coords of the bottom right corner
3308  * of gen relative to the head.
3309  * sx2 and sy2 are now the coords of the head of ob relative
3310  * to the top left corner.
3311  * genx2 and geny2 are now the coords of the head of gen relative
3312  * to the top left corner.
3313  */
3314 
3315  sx++;
3316  sy++;
3317  genx++;
3318  geny++;
3319  /*
3320  * sx, sy, genx, and geny, are now the size of the object,
3321  * excluding parts left and above the head.
3322  */
3323 
3324  ix = gen->x-sx-genx2-radius+1;
3325  iy = gen->y-sy-geny2-radius+1;
3326  sx += genx+sx2+radius*2-1;
3327  sy += geny+sy2+radius*2-1;
3328 
3329  /*
3330  * ix and iy are the map coords of the top left square where
3331  * the head of ob could possibly be placed. sx and sy are now
3332  * the size of the square to search for placement of the head
3333  * relative to ix and iy.
3334  */
3335 
3336  /* Create arrays large enough to hold free space coordinates */
3337  x_array = malloc(sx*sy*sizeof(int8_t));
3338  y_array = malloc(sx*sy*sizeof(int8_t));
3339 
3340  /*
3341  * Loop through the area of possible positions for the head of ob object:
3342  */
3343  for (x = 0; x < sx; x++) {
3344  for (y = 0; y < sy; y++) {
3345  nx = ix+x;
3346  ny = iy+y;
3347 
3348 
3349  /* Make sure it's within map. */
3350  if (get_map_flags(gen->map, NULL, nx, ny, NULL, NULL)&P_OUT_OF_MAP) {
3351  continue;
3352  }
3353 
3354  /* Check if the spot is free. */
3355  flag = ob_blocked(ob, gen->map, nx, ny);
3356  if (!flag) {
3357  x_array[freecount] = nx;
3358  y_array[freecount] = ny;
3359  freecount++;
3360  }
3361  }
3362  }
3363  /* If no free spaces, return. */
3364  if (!freecount) {
3365  free(x_array);
3366  free(y_array);
3367  return -1;
3368  }
3369 
3370  /* Choose a random valid position */
3371  freecountstop = RANDOM()%freecount;
3372  for (i = 0; i < freecount; i++) {
3373  nx = x_array[i];
3374  ny = y_array[i];
3375 
3376  /* Check if the spot is free.*/
3377  flag = ob_blocked(ob, gen->map, nx, ny);
3378  if (!flag) {
3379  freecountstop--;
3380  if (freecountstop <= 0) {
3381  *hx = nx;
3382  *hy = ny;
3383  free(x_array);
3384  free(y_array);
3385  return 0;
3386  }
3387  }
3388  }
3389  free(x_array);
3390  free(y_array);
3391  return -1;
3392 }
3393 
3430 int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop) {
3431  int i, index = 0, flag;
3432  static int altern[SIZEOFFREE];
3433 
3434  for (i = start; i < stop; i++) {
3435  flag = ob_blocked(ob, m, x+freearr_x[i], y+freearr_y[i]);
3436  if (!flag)
3437  altern[index++] = i;
3438 
3439  /* Basically, if we find a wall on a space, we cut down the search size.
3440  * In this way, we won't return spaces that are on another side of a wall.
3441  * This mostly work, but it cuts down the search size in all directions -
3442  * if the space being examined only has a wall to the north and empty
3443  * spaces in all the other directions, this will reduce the search space
3444  * to only the spaces immediately surrounding the target area, and
3445  * won't look 2 spaces south of the target space.
3446  */
3447  else if ((flag&AB_NO_PASS) && maxfree[i] < stop)
3448  stop = maxfree[i];
3449  }
3450  if (!index)
3451  return -1;
3452  return altern[RANDOM()%index];
3453 }
3454 
3473 int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y) {
3474  int i;
3475 
3476  for (i = 0; i < SIZEOFFREE; i++) {
3477  if (!ob_blocked(ob, m, x+freearr_x[i], y+freearr_y[i]))
3478  return i;
3479  }
3480  return -1;
3481 }
3482 
3492 static void permute(int *arr, int begin, int end) {
3493  int i, j, tmp, len;
3494 
3495  len = end-begin;
3496  for (i = begin; i < end; i++) {
3497  j = begin+RANDOM()%len;
3498 
3499  tmp = arr[i];
3500  arr[i] = arr[j];
3501  arr[j] = tmp;
3502  }
3503 }
3504 
3516 void get_search_arr(int *search_arr) {
3517  int i;
3518 
3519  for (i = 0; i < SIZEOFFREE; i++) {
3520  search_arr[i] = i;
3521  }
3522 
3523  permute(search_arr, 1, SIZEOFFREE1+1);
3524  permute(search_arr, SIZEOFFREE1+1, SIZEOFFREE2+1);
3525  permute(search_arr, SIZEOFFREE2+1, SIZEOFFREE);
3526 }
3527 
3549 int map_find_dir(mapstruct *m, int x, int y, object *exclude) {
3550  int i, max = SIZEOFFREE, mflags;
3551  int16_t nx, ny;
3552  mapstruct *mp;
3553  MoveType blocked, move_type;
3554 
3555  if (exclude && exclude->head) {
3556  exclude = exclude->head;
3557  move_type = exclude->move_type;
3558  } else {
3559  /* If we don't have anything, presume it can use all movement types. */
3560  move_type = MOVE_ALL;
3561  }
3562 
3563  for (i = 1; i < max; i++) {
3564  mp = m;
3565  nx = x+freearr_x[i];
3566  ny = y+freearr_y[i];
3567 
3568  mflags = get_map_flags(m, &mp, nx, ny, &nx, &ny);
3569  if (mflags&P_OUT_OF_MAP) {
3570  max = maxfree[i];
3571  } else {
3572  blocked = GET_MAP_MOVE_BLOCK(mp, nx, ny);
3573 
3574  if ((move_type&blocked) == move_type) {
3575  max = maxfree[i];
3576  } else if (mflags&P_IS_ALIVE) {
3577  FOR_MAP_PREPARE(mp, nx, ny, tmp) {
3578  if ((QUERY_FLAG(tmp, FLAG_MONSTER) || tmp->type == PLAYER)
3579  && (tmp != exclude || (tmp->head && tmp->head != exclude))) {
3580  return freedir[i];
3581  }
3582  } FOR_MAP_FINISH();
3583  }
3584  }
3585  }
3586  return 0;
3587 }
3588 
3599 int object_distance(const object *ob1, const object *ob2) {
3600  int i;
3601 
3602  i = (ob1->x-ob2->x)*(ob1->x-ob2->x)+
3603  (ob1->y-ob2->y)*(ob1->y-ob2->y);
3604  return i;
3605 }
3606 
3615 int find_dir_2(int x, int y) {
3616  int q;
3617 
3618  if (!y)
3619  q = -300*x;
3620  else
3621  q = x*100/y;
3622  if (y > 0) {
3623  if (q < -242)
3624  return 3;
3625  if (q < -41)
3626  return 2;
3627  if (q < 41)
3628  return 1;
3629  if (q < 242)
3630  return 8;
3631  return 7;
3632  }
3633  if (q < -242)
3634  return 7;
3635  if (q < -41)
3636  return 6;
3637  if (q < 41)
3638  return 5;
3639  if (q < 242)
3640  return 4;
3641  return 3;
3642 }
3643 
3652 int absdir(int d) {
3653  // Shortcut for modulus that work becuase we have a power of 2
3654  d &= 7;
3655  // 0 needs to be 8
3656  if (!d)
3657  d = 8;
3658  return d;
3659 }
3660 
3670 int dirdiff(int dir1, int dir2) {
3671  int d;
3672 
3673  d = abs(dir1-dir2);
3674  if (d > 4)
3675  d = 8-d;
3676  return d;
3677 }
3678 
3690 static const int reduction_dir[SIZEOFFREE][3] = {
3691  { 0, 0, 0 }, /* 0 */
3692  { 0, 0, 0 }, /* 1 */
3693  { 0, 0, 0 }, /* 2 */
3694  { 0, 0, 0 }, /* 3 */
3695  { 0, 0, 0 }, /* 4 */
3696  { 0, 0, 0 }, /* 5 */
3697  { 0, 0, 0 }, /* 6 */
3698  { 0, 0, 0 }, /* 7 */
3699  { 0, 0, 0 }, /* 8 */
3700  { 8, 1, 2 }, /* 9 */
3701  { 1, 2, -1 }, /* 10 */
3702  { 2, 10, 12 }, /* 11 */
3703  { 2, 3, -1 }, /* 12 */
3704  { 2, 3, 4 }, /* 13 */
3705  { 3, 4, -1 }, /* 14 */
3706  { 4, 14, 16 }, /* 15 */
3707  { 5, 4, -1 }, /* 16 */
3708  { 4, 5, 6 }, /* 17 */
3709  { 6, 5, -1 }, /* 18 */
3710  { 6, 20, 18 }, /* 19 */
3711  { 7, 6, -1 }, /* 20 */
3712  { 6, 7, 8 }, /* 21 */
3713  { 7, 8, -1 }, /* 22 */
3714  { 8, 22, 24 }, /* 23 */
3715  { 8, 1, -1 }, /* 24 */
3716  { 24, 9, 10 }, /* 25 */
3717  { 9, 10, -1 }, /* 26 */
3718  { 10, 11, -1 }, /* 27 */
3719  { 27, 11, 29 }, /* 28 */
3720  { 11, 12, -1 }, /* 29 */
3721  { 12, 13, -1 }, /* 30 */
3722  { 12, 13, 14 }, /* 31 */
3723  { 13, 14, -1 }, /* 32 */
3724  { 14, 15, -1 }, /* 33 */
3725  { 33, 15, 35 }, /* 34 */
3726  { 16, 15, -1 }, /* 35 */
3727  { 17, 16, -1 }, /* 36 */
3728  { 18, 17, 16 }, /* 37 */
3729  { 18, 17, -1 }, /* 38 */
3730  { 18, 19, -1 }, /* 39 */
3731  { 41, 19, 39 }, /* 40 */
3732  { 19, 20, -1 }, /* 41 */
3733  { 20, 21, -1 }, /* 42 */
3734  { 20, 21, 22 }, /* 43 */
3735  { 21, 22, -1 }, /* 44 */
3736  { 23, 22, -1 }, /* 45 */
3737  { 45, 47, 23 }, /* 46 */
3738  { 23, 24, -1 }, /* 47 */
3739  { 24, 9, -1 } /* 48 */
3740 };
3741 
3760 int can_see_monsterP(mapstruct *m, int x, int y, int dir) {
3761  int16_t dx, dy;
3762  int mflags;
3763 
3764  if (dir < 0)
3765  return 0; /* exit condition: invalid direction */
3766 
3767  dx = x+freearr_x[dir];
3768  dy = y+freearr_y[dir];
3769 
3770  mflags = get_map_flags(m, &m, dx, dy, &dx, &dy);
3771 
3772  /* This functional arguably was incorrect before - it was
3773  * checking for P_WALL - that was basically seeing if
3774  * we could move to the monster - this is being more
3775  * literal on if we can see it. To know if we can actually
3776  * move to the monster, we'd need the monster passed in or
3777  * at least its move type.
3778  */
3779  if (mflags&(P_OUT_OF_MAP|P_BLOCKSVIEW))
3780  return 0;
3781 
3782  /* yes, can see. */
3783  if (dir < 9)
3784  return 1;
3785  return can_see_monsterP(m, x, y, reduction_dir[dir][0])|
3786  can_see_monsterP(m, x, y, reduction_dir[dir][1])|
3787  can_see_monsterP(m, x, y, reduction_dir[dir][2]);
3788 }
3789 
3808 int object_can_pick(const object *who, const object *item) {
3809  /* I re-wrote this as a series of if statements
3810  * instead of a nested return (foo & bar && yaz)
3811  * - I think this is much more readable,
3812  * and likely compiler effectively optimizes it the
3813  * same.
3814  */
3815  if (item->weight <= 0)
3816  return 0;
3817  if (QUERY_FLAG(item, FLAG_NO_PICK))
3818  return 0;
3819  if (QUERY_FLAG(item, FLAG_ALIVE))
3820  return 0;
3821  if (item->invisible)
3822  return 0;
3823  if (item->type == TRANSPORT && item->contr != NULL) {
3824  return 0;
3825  }
3826 
3827  /* Weight limit for monsters */
3828  if (who->type != PLAYER && ((uint32_t)(who->weight+who->carrying+item->weight)) > get_weight_limit(who->stats.Str))
3829  return 0;
3830 
3831  /* Can not pick up multipart objects */
3832  if (item->head || item->more)
3833  return 0;
3834 
3835  /* Everything passes, so OK to pick up */
3836  return 1;
3837 }
3838 
3850 object *object_create_clone(object *asrc) {
3851  object *dst = NULL, *tmp, *src, *part, *prev;
3852 
3853  if (!asrc)
3854  return NULL;
3855  src = HEAD(asrc);
3856 
3857  prev = NULL;
3858  for (part = src; part; part = part->more) {
3859  tmp = object_new();
3860  object_copy(part, tmp);
3861  /*
3862  * Need to reset the weight, since object_insert_in_ob() later will
3863  * recompute this field.
3864  */
3865  tmp->carrying = tmp->arch->clone.carrying;
3866  tmp->x -= src->x;
3867  tmp->y -= src->y;
3868  if (!part->head) {
3869  dst = tmp;
3870  tmp->head = NULL;
3871  } else {
3872  tmp->head = dst;
3873  }
3874  tmp->more = NULL;
3875  if (prev)
3876  prev->more = tmp;
3877  prev = tmp;
3878  }
3879  /*** copy inventory ***/
3880  FOR_INV_PREPARE(src, item)
3881  (void)object_insert_in_ob(object_create_clone(item), dst);
3882  FOR_INV_FINISH();
3883 
3884  return dst;
3885 }
3886 
3897 object *object_find_by_name(const object *who, const char *name) {
3898  const char *name_shared = add_string(name);
3899  object *tmp;
3900 
3901  for (tmp = who->inv; tmp; tmp = tmp->below)
3902  if (tmp->name == name_shared)
3903  break;
3904  free_string(name_shared);
3905  return tmp;
3906 }
3907 
3921 object *object_find_by_type(const object *who, int type) {
3922  object *tmp;
3923 
3924  for (tmp = who->inv; tmp; tmp = tmp->below)
3925  if (tmp->type == type)
3926  return tmp;
3927 
3928  return NULL;
3929 }
3930 
3946 object *object_find_by_type2(const object *who, int type1, int type2) {
3947  object *tmp;
3948 
3949  for (tmp = who->inv; tmp; tmp = tmp->below)
3950  if (tmp->type == type1 || tmp->type == type2)
3951  return tmp;
3952 
3953  return NULL;
3954 }
3955 
3969 object *object_find_by_tag(const object *who, tag_t tag) {
3970  object *tmp;
3971 
3972  for (tmp = who->inv; tmp; tmp = tmp->below)
3973  if (tmp->count == tag)
3974  return tmp;
3975 
3976  return NULL;
3977 }
3978 
3992 object *object_find_by_type_applied(const object *who, int type) {
3993  object *tmp;
3994 
3995  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
3996  if (tmp->type == type && QUERY_FLAG(tmp, FLAG_APPLIED))
3997  return tmp;
3998 
3999  return NULL;
4000 }
4001 
4017 object *object_find_by_type_and_name(const object *who, int type, const char *name) {
4018  object *tmp;
4019 
4020  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4021  if (tmp->type == type && strcmp(tmp->name, name) == 0)
4022  return tmp;
4023 
4024  return NULL;
4025 }
4026 
4042 object *object_find_by_type_and_race(const object *who, int type, const char *race) {
4043  object *tmp;
4044 
4045  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4046  if (tmp->type == type && strcmp(tmp->race, race) == 0)
4047  return tmp;
4048 
4049  return NULL;
4050 }
4051 
4067 object *object_find_by_type_and_slaying(const object *who, int type, const char *slaying) {
4068  object *tmp;
4069 
4070  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4071  if (tmp->type == type && tmp->slaying != NULL && strcmp(tmp->slaying, slaying) == 0)
4072  return tmp;
4073 
4074  return NULL;
4075 }
4076 
4092 object *object_find_by_type_and_skill(const object *who, int type, const char *skill) {
4093  object *tmp;
4094 
4095  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4096  if (tmp->type == SKILL && tmp->skill != NULL && strcmp(tmp->skill, skill) == 0)
4097  return tmp;
4098 
4099  return NULL;
4100 }
4101 
4115 object *object_find_by_flag(const object *who, int flag) {
4116  object *tmp;
4117 
4118  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4119  if (QUERY_FLAG(tmp, flag))
4120  return tmp;
4121 
4122  return NULL;
4123 }
4124 
4138 object *object_find_by_flag_applied(const object *who, int flag) {
4139  object *tmp;
4140 
4141  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4142  if (QUERY_FLAG(tmp, FLAG_APPLIED) && QUERY_FLAG(tmp, flag))
4143  return tmp;
4144 
4145  return NULL;
4146 }
4147 
4161 object *object_find_by_arch_name(const object *who, const char *name) {
4162  object *tmp;
4163 
4164  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4165  if (strcmp(tmp->arch->name, name) == 0)
4166  return tmp;
4167 
4168  return NULL;
4169 }
4170 
4186 object *object_find_by_type_and_arch_name(const object *who, int type, const char *name) {
4187  object *tmp;
4188 
4189  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4190  if (tmp->type == type && strcmp(tmp->arch->name, name) == 0)
4191  return tmp;
4192 
4193  return NULL;
4194 }
4195 
4213 object *object_find_by_type_subtype(const object *who, int type, int subtype) {
4214  object *tmp;
4215 
4216  for (tmp = who->inv; tmp; tmp = tmp->below)
4217  if (tmp->type == type && tmp->subtype == subtype)
4218  return tmp;
4219 
4220  return NULL;
4221 }
4222 
4236 key_value *object_get_key_value(const object *ob, const char *key) {
4237  key_value *link;
4238 
4239  for (link = ob->key_values; link != NULL; link = link->next) {
4240  if (link->key == key) {
4241  return link;
4242  }
4243  }
4244 
4245  return NULL;
4246 }
4247 
4264 const char *object_get_value(const object *op, const char *const key) {
4265  key_value *link;
4266  const char *canonical_key;
4267 
4268  canonical_key = find_string(key);
4269 
4270  if (canonical_key == NULL) {
4271  /* 1. There being a field named key on any object
4272  * implies there'd be a shared string to find.
4273  * 2. Since there isn't, no object has this field.
4274  * 3. Therefore, *this *object doesn't have this field.
4275  */
4276  return NULL;
4277  }
4278 
4279  /* This is copied from object_get_key_value() above -
4280  * only 4 lines, and saves the function call overhead.
4281  */
4282  for (link = op->key_values; link != NULL; link = link->next) {
4283  if (link->key == canonical_key) {
4284  return link->value;
4285  }
4286  }
4287  return NULL;
4288 }
4289 
4307 static int object_set_value_s(object *op, const char *canonical_key, const char *value, int add_key) {
4308  key_value *field = NULL, *last = NULL;
4309 
4310  for (field = op->key_values; field != NULL; field = field->next) {
4311  if (field->key != canonical_key) {
4312  last = field;
4313  continue;
4314  }
4315 
4316  if (field->value)
4317  FREE_AND_CLEAR_STR(field->value);
4318  if (value)
4319  field->value = add_string(value);
4320  else {
4321  /* Basically, if the archetype has this key set,
4322  * we need to store the null value so when we save
4323  * it, we save the empty value so that when we load,
4324  * we get this value back again.
4325  */
4326  if (object_get_key_value(&op->arch->clone, canonical_key))
4327  field->value = NULL;
4328  else {
4329  /* Delete this link */
4330  if (field->key)
4331  FREE_AND_CLEAR_STR(field->key);
4332  if (field->value)
4333  FREE_AND_CLEAR_STR(field->value);
4334  if (last)
4335  last->next = field->next;
4336  else
4337  op->key_values = field->next;
4338  free(field);
4339  }
4340  }
4341  return TRUE;
4342  }
4343  /* IF we get here, key doesn't exist */
4344 
4345  /* No field, we'll have to add it. */
4346 
4347  if (!add_key) {
4348  return FALSE;
4349  }
4350  /* There isn't any good reason to store a null
4351  * value in the key/value list. If the archetype has
4352  * this key, then we should also have it, so shouldn't
4353  * be here. If user wants to store empty strings,
4354  * should pass in ""
4355  */
4356  if (value == NULL)
4357  return TRUE;
4358 
4359  field = malloc(sizeof(key_value));
4360 
4361  field->key = add_refcount(canonical_key);
4362  field->value = add_string(value);
4363  /* Usual prepend-addition. */
4364  field->next = op->key_values;
4365  op->key_values = field;
4366 
4367  return TRUE;
4368 }
4369 
4393 int object_set_value(object *op, const char *key, const char *value, int add_key) {
4394  const char *canonical_key = NULL;
4395  int floating_ref = FALSE;
4396  int ret;
4397 
4398  /* HACK This mess is to make sure set_ob_value() passes a shared string
4399  * to object_get_key_value(), without leaving a leaked refcount.
4400  */
4401 
4402  canonical_key = find_string(key);
4403  if (canonical_key == NULL) {
4404  canonical_key = add_string(key);
4405  floating_ref = TRUE;
4406  }
4407 
4408  ret = object_set_value_s(op, canonical_key, value, add_key);
4409 
4410  if (floating_ref) {
4411  free_string(canonical_key);
4412  }
4413 
4414  return ret;
4415 }
4416 
4471 int object_matches_string(object *pl, object *op, const char *name) {
4472  char *cp, local_name[MAX_BUF], name_op[MAX_BUF], name_short[HUGE_BUF], bname_s[MAX_BUF], bname_p[MAX_BUF];
4473  int count, retval = 0;
4474  /* strtok is destructive to name */
4475  safe_strncpy(local_name, name, sizeof(local_name));
4476 
4477  for (cp = strtok(local_name, ","); cp; cp = strtok(NULL, ",")) {
4478  while (cp[0] == ' ')
4479  ++cp; /* get rid of spaces */
4480 
4481  /* LOG(llevDebug, "Trying to match %s\n", cp);*/
4482  /* All is a very generic match - low match value */
4483  if (!strcmp(cp, "all"))
4484  return 1;
4485 
4486  /* unpaid is a little more specific */
4487  if (!strcmp(cp, "unpaid") && QUERY_FLAG(op, FLAG_UNPAID))
4488  return 2;
4489  if (!strcmp(cp, "cursed")
4491  && (QUERY_FLAG(op, FLAG_CURSED) || QUERY_FLAG(op, FLAG_DAMNED)))
4492  return 2;
4493 
4494  if (!strcmp(cp, "unlocked") && !QUERY_FLAG(op, FLAG_INV_LOCKED))
4495  return 2;
4496 
4497  /* Allow for things like '100 arrows' */
4498  count = atoi(cp);
4499  if (count != 0) {
4500  cp = strchr(cp, ' ');
4501  while (cp && cp[0] == ' ')
4502  ++cp; /* get rid of spaces */
4503  } else {
4504  if (pl->type == PLAYER)
4505  count = pl->contr->count;
4506  else
4507  count = 0;
4508  }
4509 
4510  if (!cp || cp[0] == '\0' || count < 0)
4511  return 0;
4512 
4513  /* The code here should go from highest retval to lowest. That
4514  * is because of the 'else' handling - we don't want to match on
4515  * something and set a low retval, even though it may match a higher retcal
4516  * later. So keep it in descending order here, so we try for the best
4517  * match first, and work downward.
4518  */
4519  query_name(op, name_op, MAX_BUF);
4520  query_short_name(op, name_short, HUGE_BUF);
4521  query_base_name(op, 0, bname_s, MAX_BUF);
4522  query_base_name(op, 1, bname_p, MAX_BUF);
4523 
4524  if (!strcasecmp(cp, name_op))
4525  retval = 20;
4526  else if (!strcasecmp(cp, name_short))
4527  retval = 18;
4528  else if (!strcasecmp(cp, bname_s))
4529  retval = 16;
4530  else if (!strcasecmp(cp, bname_p))
4531  retval = 16;
4532  else if (op->custom_name && !strcasecmp(cp, op->custom_name))
4533  retval = 15;
4534  else if (!strncasecmp(cp, bname_s, strlen(cp)))
4535  retval = 14;
4536  else if (!strncasecmp(cp, bname_p, strlen(cp)))
4537  retval = 14;
4538  /* Do substring checks, so things like 'Str+1' will match.
4539  * retval of these should perhaps be lower - they are lower
4540  * then the specific strcasecmp aboves, but still higher than
4541  * some other match criteria.
4542  */
4543  else if (strstr(bname_p, cp))
4544  retval = 12;
4545  else if (strstr(bname_s, cp))
4546  retval = 12;
4547  else if (strstr(name_short, cp))
4548  retval = 12;
4549  /* Check against plural/non plural based on count. */
4550  else if (count > 1 && !strcasecmp(cp, op->name_pl)) {
4551  retval = 6;
4552  } else if (count == 1 && !strcasecmp(op->name, cp)) {
4553  retval = 6;
4554  }
4555  /* base name matched - not bad */
4556  else if (strcasecmp(cp, op->name) == 0 && !count)
4557  retval = 4;
4558  /* Check for partial custom name, but give a real low priority */
4559  else if (op->custom_name && strstr(op->custom_name, cp))
4560  retval = 3;
4561 
4562  if (retval) {
4563  if (pl->type == PLAYER)
4564  pl->contr->count = count;
4565  return retval;
4566  }
4567  }
4568  return 0;
4569 }
4570 
4582 void object_fix_multipart(object *tmp) {
4583  archetype *at;
4584  object *op, *last;
4585 
4586  if (!tmp->map) {
4587  LOG(llevError, "object_fix_multipart: not on a map!\n");
4588  return;
4589  }
4590 
4591  /* already multipart - don't do anything more */
4592  if (tmp->head || tmp->more)
4593  return;
4594 
4595  /* If there is nothing more to this object, this for loop
4596  * won't do anything.
4597  */
4598  for (at = tmp->arch->more, last = tmp; at != NULL; at = at->more, last = op) {
4599  op = arch_to_object(at);
4600 
4601  /* update x,y coordinates */
4602  op->x += tmp->x;
4603  op->y += tmp->y;
4604  op->head = tmp;
4605  op->map = tmp->map;
4606  last->more = op;
4607  if (tmp->name != op->name) {
4608  if (op->name)
4609  free_string(op->name);
4610  op->name = add_string(tmp->name);
4611  }
4612  if (tmp->title != op->title) {
4613  if (op->title)
4614  free_string(op->title);
4615  op->title = add_string(tmp->title);
4616  }
4617  /* we could link all the parts onto tmp, and then just
4618  * call object_insert_in_map once, but the effect is the same,
4619  * as object_insert_in_map will call itself with each part, and
4620  * the coding is simpler to just to it here with each part.
4621  */
4623  } /* for at = tmp->arch->more */
4624 }
4625 
4644 void object_get_multi_size(const object *ob, int *sx, int *sy, int *hx, int *hy) {
4645  archetype *part;
4646  int maxx = 0, maxy = 0, minx = 0, miny = 0;
4647 
4648  ob = HEAD(ob);
4649  *sx = 1;
4650  *sy = 1;
4651  if (ob->arch->more) {
4652  for (part = ob->arch; part; part = part->more) {
4653  if (part->clone.x > maxx)
4654  maxx = part->clone.x;
4655  if (part->clone.y > maxy)
4656  maxy = part->clone.y;
4657  if (part->clone.x < minx)
4658  minx = part->clone.x;
4659  if (part->clone.y < miny)
4660  miny = part->clone.y;
4661  }
4662  }
4663  if (sx)
4664  *sx = maxx;
4665  if (sy)
4666  *sy = maxy;
4667  if (hx)
4668  *hx = -minx;
4669  if (hy)
4670  *hy = -miny;
4671 }
4672 
4693 void object_insert_to_free_spot_or_free(object *op, mapstruct *map, int x, int y, int start, int stop, object *originator) {
4694  int pos;
4695 
4696  pos = object_find_free_spot(op, map, x, y, start, stop);
4697  if (pos == -1) {
4699  return;
4700  }
4701 
4702  object_insert_in_map_at(op, map, originator, 0, x+freearr_x[pos], y+freearr_y[pos]);
4703 }
4704 
4713 void object_set_msg(object *op, const char *msg) {
4714  if (op->msg != NULL) {
4715  free_string(op->msg);
4716  }
4717 
4718  if (msg != NULL) {
4719  // If the message does not have a trailing newline, add one.
4720  if (*msg != '\0' && strchr(msg, '\0')[-1] != '\n') {
4722  stringbuffer_append_string(sb, msg);
4723  stringbuffer_append_string(sb, "\n");
4724  op->msg = stringbuffer_finish_shared(sb);
4725  } else {
4726  op->msg = add_string(msg);
4727  }
4728  } else {
4729  op->msg = NULL;
4730  }
4731 }
4732 
4734 const char *const move_name[] = {
4735  "walk",
4736  "fly_low",
4737  "fly_high",
4738  "swim",
4739  "boat",
4740  NULL
4741 };
4742 
4743 /* This array equates the FLAG_ values with the V_ values. Use -1 to
4744  * put gaps in the array that should not be processed.
4745  * The order matches the order of the define values in 'define.h'.
4746  */
4753 static const char *const flag_names[NUM_FLAGS+1] = {
4754  "alive", "wiz", NULL, NULL, "was_wiz", "applied", "unpaid",
4755  "can_use_shield", "no_pick", "client_anim_sync", "client_anim_random", /* 10 */
4756  "is_animated", NULL /* FLAG_DIALOG_PARSED, not saved */,
4757  NULL /* flying */, "monster", "friendly", "generator",
4758  "is_thrown", "auto_apply", "treasure", "player sold", /* 20 */
4759  "see_invisible", "can_roll", "overlay_floor",
4760  "is_turnable", NULL /* walk_off */, NULL /* fly_on */,
4761  NULL /*fly_off*/, "is_used_up", "identified", "reflecting", /* 30 */
4762  "changing", "splitting", "hitback", "startequip",
4763  "blocksview", "undead", "scared", "unaggressive",
4764  "reflect_missile", "reflect_spell", /* 40 */
4765  "no_magic", "no_fix_player", "is_lightable", "tear_down",
4766  "run_away", NULL /*pass_thru */, NULL /*can_pass_thru*/,
4767  NULL /*"pick_up"*/, "unique", "no_drop", /* 50 */
4768  NULL /* wizcast*/, "can_cast_spell", "can_use_scroll", "can_use_range",
4769  "can_use_bow", "can_use_armour", "can_use_weapon",
4770  "can_use_ring", "has_ready_range", "has_ready_bow", /* 60 */
4771  "xrays", NULL, "is_floor", "lifesave", "no_strength", "sleep",
4772  "stand_still", "random_movement", "only_attack", "confused", /* 70 */
4773  "stealth", NULL, NULL, "cursed", "damned",
4774  "see_anywhere", "known_magical", "known_cursed",
4775  "can_use_skill", "been_applied", /* 80 */
4776  "has_ready_scroll", NULL, NULL,
4777  NULL, "make_invisible", "inv_locked", "is_wooded",
4778  "is_hilly", "has_ready_skill", "has_ready_weapon", /* 90 */
4779  "no_skill_ident", "is_blind", "can_see_in_dark", "is_cauldron",
4780  NULL, "no_steal", "one_hit", NULL, "berserk", "neutral", /* 100 */
4781  "no_attack", "no_damage", NULL, NULL, "activate_on_push",
4782  "activate_on_release", "is_water", "use_content_on_gen", NULL, "is_buildable", /* 110 */
4783  NULL, "blessed", "known_blessed"
4784 };
4785 
4795 {
4796  static char retbuf[MAX_BUF], retbuf_all[MAX_BUF];
4797  int i, all_count = 0, count;
4798 
4799  strcpy(retbuf, "");
4800  strcpy(retbuf_all, " all");
4801 
4802  /* Quick check, and probably fairly common */
4803  if (mt == MOVE_ALL) {
4804  stringbuffer_append_string(sb, "all");
4805  return;
4806  }
4807  if (mt == 0) {
4808  stringbuffer_append_string(sb, "0");
4809  return;
4810  }
4811 
4812  /* We basically slide the bits down. Why look at MOVE_ALL?
4813  * because we may want to return a string like 'all -swim',
4814  * and if we just looked at mt, we couldn't get that.
4815  */
4816  for (i = MOVE_ALL, count = 0; i != 0; i >>= 1, count++) {
4817  if (mt&(1<<count)) {
4818  strcat(retbuf, " ");
4819  strcat(retbuf, move_name[count]);
4820  } else {
4821  strcat(retbuf_all, " -");
4822  strcat(retbuf_all, move_name[count]);
4823  all_count++;
4824  }
4825  }
4826  /* Basically, if there is a single negation, return it, eg
4827  * 'all -swim'. But more than that, just return the
4828  * enumerated values. It doesn't make sense to return
4829  * 'all -walk -fly_low' - it is shorter to return 'fly_high swim'
4830  */
4831  if (all_count <= 1)
4832  stringbuffer_append_string(sb, retbuf_all+1);
4833  else
4834  stringbuffer_append_string(sb, retbuf+1);
4835 }
4836 
4838 #define ADD_STRINGLINE_ENTRY(sb__, entryname__, entryvalue__) do {\
4839  stringbuffer_append_string(sb__, entryname__);\
4840  stringbuffer_append_string(sb__, entryvalue__);\
4841  stringbuffer_append_string(sb__, "\n");\
4842  } while (0)
4843 
4844 #define FAST_SAVE_LONG(sb__, entryname__, entryvalue__) \
4845  stringbuffer_append_printf(sb__, "%s%ld\n", entryname__, (long int)entryvalue__)
4846 
4847 #define FAST_SAVE_DOUBLE(sb__, entryname__, entryvalue__) \
4848  stringbuffer_append_printf(sb__, "%s%f\n", entryname__, entryvalue__)
4849 
4863 void get_ob_diff(StringBuffer *sb, const object *op, const object *op2) {
4864  static char buf2[64];
4865  int tmp;
4866  int i;
4867  key_value *my_field;
4868  key_value *arch_field;
4869 
4870  /* This saves the key/value lists. We do it first so that any
4871  * keys that match field names will be overwritten by the loader.
4872  */
4873  for (my_field = op->key_values; my_field != NULL; my_field = my_field->next) {
4874  /* Find the field in the opposing member. */
4875  arch_field = object_get_key_value(op2, my_field->key);
4876 
4877  /* If there's no partnering field, or it's got a different value, save our field. */
4878  if (arch_field == NULL || my_field->value != arch_field->value) {
4879  stringbuffer_append_string(sb, my_field->key);
4880  stringbuffer_append_string(sb, " ");
4881  /* If this is null, then saving it as a space should
4882  * cause it to be null again.
4883  */
4884  if (my_field->value)
4885  stringbuffer_append_string(sb, my_field->value);
4886  stringbuffer_append_string(sb, "\n");
4887  }
4888  }
4889  /* We don't need to worry about the arch's extra fields - they
4890  * will get taken care of the object_copy() function.
4891  */
4892 
4893  if (op->name && op->name != op2->name) {
4894  ADD_STRINGLINE_ENTRY(sb, "name ", op->name);
4895  }
4896  if (op->name_pl && op->name_pl != op2->name_pl) {
4897  ADD_STRINGLINE_ENTRY(sb, "name_pl ", op->name_pl);
4898  }
4899  if (op->anim_suffix && op->anim_suffix != op2->anim_suffix) {
4900  ADD_STRINGLINE_ENTRY(sb, "anim_suffix ", op->anim_suffix);
4901  }
4902  if (op->custom_name && op->custom_name != op2->custom_name) {
4903  ADD_STRINGLINE_ENTRY(sb, "custom_name ", op->custom_name);
4904  }
4905  if (op->title && op->title != op2->title) {
4906  ADD_STRINGLINE_ENTRY(sb, "title ", op->title);
4907  }
4908  if (op->race && op->race != op2->race) {
4909  ADD_STRINGLINE_ENTRY(sb, "race ", op->race);
4910  }
4911  if (op->slaying && op->slaying != op2->slaying) {
4912  ADD_STRINGLINE_ENTRY(sb, "slaying ", op->slaying);
4913  }
4914  if (op->skill && op->skill != op2->skill) {
4915  ADD_STRINGLINE_ENTRY(sb, "skill ", op->skill);
4916  }
4917  if (op->msg && op->msg != op2->msg) {
4918  stringbuffer_append_string(sb, "msg\n");
4920  stringbuffer_append_string(sb, "endmsg\n");
4921  }
4922  if (op->lore && op->lore != op2->lore) {
4923  stringbuffer_append_string(sb, "lore\n");
4925  stringbuffer_append_string(sb, "endlore\n");
4926  }
4927  if (op->other_arch != op2->other_arch && op->other_arch != NULL && op->other_arch->name) {
4928  ADD_STRINGLINE_ENTRY(sb, "other_arch ", op->other_arch->name);
4929  }
4930  if (op->face != op2->face) {
4931  ADD_STRINGLINE_ENTRY(sb, "face ", op->face->name);
4932  }
4933 
4934  if (op->animation_id != op2->animation_id) {
4935  if (op->animation_id) {
4936  ADD_STRINGLINE_ENTRY(sb, "animation ", animations[GET_ANIM_ID(op)].name);
4937  if (!QUERY_FLAG (op, FLAG_ANIMATE)) {
4938  stringbuffer_append_string(sb, "is_animated 0\n");
4939  }
4940  } else {
4941  stringbuffer_append_string(sb, "animation NONE\n");
4942  }
4943  }
4944  if (op->stats.Str != op2->stats.Str)
4945  FAST_SAVE_LONG(sb, "Str ", op->stats.Str);
4946  if (op->stats.Dex != op2->stats.Dex)
4947  FAST_SAVE_LONG(sb, "Dex ", op->stats.Dex);
4948  if (op->stats.Con != op2->stats.Con)
4949  FAST_SAVE_LONG(sb, "Con ", op->stats.Con);
4950  if (op->stats.Wis != op2->stats.Wis)
4951  FAST_SAVE_LONG(sb, "Wis ", op->stats.Wis);
4952  if (op->stats.Pow != op2->stats.Pow)
4953  FAST_SAVE_LONG(sb, "Pow ", op->stats.Pow);
4954  if (op->stats.Cha != op2->stats.Cha)
4955  FAST_SAVE_LONG(sb, "Cha ", op->stats.Cha);
4956  if (op->stats.Int != op2->stats.Int)
4957  FAST_SAVE_LONG(sb, "Int ", op->stats.Int);
4958  if (op->stats.hp != op2->stats.hp)
4959  FAST_SAVE_LONG(sb, "hp ", op->stats.hp);
4960  if (op->stats.maxhp != op2->stats.maxhp)
4961  FAST_SAVE_LONG(sb, "maxhp ", op->stats.maxhp);
4962  if (op->stats.sp != op2->stats.sp)
4963  FAST_SAVE_LONG(sb, "sp ", op->stats.sp);
4964  if (op->stats.maxsp != op2->stats.maxsp)
4965  FAST_SAVE_LONG(sb, "maxsp ", op->stats.maxsp);
4966  if (op->stats.grace != op2->stats.grace)
4967  FAST_SAVE_LONG(sb, "grace ", op->stats.grace);
4968  if (op->stats.maxgrace != op2->stats.maxgrace)
4969  FAST_SAVE_LONG(sb, "maxgrace ", op->stats.maxgrace);
4970 
4971  if (op->stats.exp != op2->stats.exp) {
4972  snprintf(buf2, sizeof(buf2), "%"FMT64, op->stats.exp);
4973  ADD_STRINGLINE_ENTRY(sb, "exp ", buf2);
4974  }
4975 
4976  if (op->perm_exp != op2->perm_exp) {
4977  snprintf(buf2, sizeof(buf2), "%"FMT64, op->perm_exp);
4978  ADD_STRINGLINE_ENTRY(sb, "perm_exp ", buf2);
4979  }
4980 
4981  if (op->expmul != op2->expmul)
4982  FAST_SAVE_DOUBLE(sb, "expmul ", op->expmul);
4983  if (op->stats.food != op2->stats.food)
4984  FAST_SAVE_LONG(sb, "food ", op->stats.food);
4985  if (op->stats.dam != op2->stats.dam)
4986  FAST_SAVE_LONG(sb, "dam ", op->stats.dam);
4987  if (op->stats.luck != op2->stats.luck)
4988  FAST_SAVE_LONG(sb, "luck ", op->stats.luck);
4989  if (op->stats.wc != op2->stats.wc)
4990  FAST_SAVE_LONG(sb, "wc ", op->stats.wc);
4991  if (op->stats.ac != op2->stats.ac)
4992  FAST_SAVE_LONG(sb, "ac ", op->stats.ac);
4993  if (op->x != op2->x)
4994  FAST_SAVE_LONG(sb, "x ", op->x);
4995  if (op->y != op2->y)
4996  FAST_SAVE_LONG(sb, "y ", op->y);
4997  if (op->speed != op2->speed) {
4998  FAST_SAVE_DOUBLE(sb, "speed ", op->speed);
4999  }
5000  if (op->speed > 0 && op->speed_left != op2->speed_left) {
5001  FAST_SAVE_DOUBLE(sb, "speed_left ", op->speed_left);
5002  }
5003  if (op->weapon_speed != op2->weapon_speed) {
5004  FAST_SAVE_DOUBLE(sb, "weapon_speed ", op->weapon_speed);
5005  }
5006  if (op->weapon_speed > 0 && op->weapon_speed_left != op2->weapon_speed_left) {
5007  FAST_SAVE_DOUBLE(sb, "weapon_speed_left ", op->weapon_speed_left);
5008  }
5009  if (op->move_status != op2->move_status)
5010  FAST_SAVE_LONG(sb, "move_state ", op->move_status);
5011  if (op->attack_movement != op2->attack_movement)
5012  FAST_SAVE_LONG(sb, "attack_movement ", op->attack_movement);
5013  if (op->nrof != op2->nrof)
5014  FAST_SAVE_LONG(sb, "nrof ", op->nrof);
5015  if (op->level != op2->level)
5016  FAST_SAVE_LONG(sb, "level ", op->level);
5017  if (op->direction != op2->direction)
5018  FAST_SAVE_LONG(sb, "direction ", op->direction);
5019  if (op->type != op2->type)
5020  FAST_SAVE_LONG(sb, "type ", op->type);
5021  if (op->subtype != op2->subtype)
5022  FAST_SAVE_LONG(sb, "subtype ", op->subtype);
5023  if (op->attacktype != op2->attacktype)
5024  FAST_SAVE_LONG(sb, "attacktype ", op->attacktype);
5025 
5026  for (tmp = 0; tmp < NROFATTACKS; tmp++) {
5027  if (op->resist[tmp] != op2->resist[tmp]) {
5028  stringbuffer_append_string(sb, "resist_");
5029  FAST_SAVE_LONG(sb, resist_save[tmp], op->resist[tmp]);
5030  }
5031  }
5032 
5033  if (op->path_attuned != op2->path_attuned)
5034  FAST_SAVE_LONG(sb, "path_attuned ", op->path_attuned);
5035  if (op->path_repelled != op2->path_repelled)
5036  FAST_SAVE_LONG(sb, "path_repelled ", op->path_repelled);
5037  if (op->path_denied != op2->path_denied)
5038  FAST_SAVE_LONG(sb, "path_denied ", op->path_denied);
5039  if (op->material != op2->material)
5040  FAST_SAVE_LONG(sb, "material ", op->material);
5041  if (op->materialname && op->materialname != op2->materialname) {
5042  ADD_STRINGLINE_ENTRY(sb, "materialname ", op->materialname);
5043  }
5044  if (op->value != op2->value)
5045  FAST_SAVE_LONG(sb, "value ", op->value);
5046  if (op->carrying != op2->carrying)
5047  FAST_SAVE_LONG(sb, "carrying ", op->carrying);
5048  if (op->weight != op2->weight)
5049  FAST_SAVE_LONG(sb, "weight ", op->weight);
5050  if (op->invisible != op2->invisible)
5051  FAST_SAVE_LONG(sb, "invisible ", op->invisible);
5052  if (op->state != op2->state)
5053  FAST_SAVE_LONG(sb, "state ", op->state);
5054  if (op->magic != op2->magic)
5055  FAST_SAVE_LONG(sb, "magic ", op->magic);
5056  if (op->last_heal != op2->last_heal)
5057  FAST_SAVE_LONG(sb, "last_heal ", op->last_heal);
5058  if (op->last_sp != op2->last_sp)
5059  FAST_SAVE_LONG(sb, "last_sp ", op->last_sp);
5060  if (op->last_grace != op2->last_grace)
5061  FAST_SAVE_LONG(sb, "last_grace ", op->last_grace);
5062  if (op->last_eat != op2->last_eat)
5063  FAST_SAVE_LONG(sb, "last_eat ", op->last_eat);
5064  if (QUERY_FLAG(op, FLAG_IS_LINKED) && (tmp = get_button_value(op)))
5065  FAST_SAVE_LONG(sb, "connected ", tmp);
5066  if (op->glow_radius != op2->glow_radius)
5067  FAST_SAVE_LONG(sb, "glow_radius ", op->glow_radius);
5068  if (op->randomitems != op2->randomitems) {
5069  ADD_STRINGLINE_ENTRY(sb, "randomitems ", op->randomitems ? op->randomitems->name : "none");
5070  }
5071 
5072  if (op->run_away != op2->run_away)
5073  FAST_SAVE_LONG(sb, "run_away ", op->run_away);
5074  if (op->pick_up != op2->pick_up)
5075  FAST_SAVE_LONG(sb, "pick_up ", op->pick_up);
5076  if (op->weight_limit != op2->weight_limit)
5077  FAST_SAVE_LONG(sb, "container ", op->weight_limit);
5078  if (op->will_apply != op2->will_apply)
5079  FAST_SAVE_LONG(sb, "will_apply ", op->will_apply);
5080  if (op->smoothlevel != op2->smoothlevel)
5081  FAST_SAVE_LONG(sb, "smoothlevel ", op->smoothlevel);
5082 
5083  if (op->map_layer != op2->map_layer)
5084  ADD_STRINGLINE_ENTRY(sb, "map_layer ", map_layer_name[op->map_layer]);
5085 
5086  if (op->weapontype && op->weapontype != op2->weapontype) {
5087  FAST_SAVE_LONG(sb, "weapontype ", op->weapontype);
5088  }
5089  if (op->client_type && op->client_type != op2->client_type) {
5090  FAST_SAVE_LONG(sb, "client_type ", op->client_type);
5091  }
5092 
5093  if (op->item_power != op2->item_power) {
5094  FAST_SAVE_LONG(sb, "item_power ", op->item_power);
5095  }
5096 
5097  if (op->duration != op2->duration)
5098  FAST_SAVE_LONG(sb, "duration ", op->duration);
5099 
5100  if (op->range != op2->range)
5101  FAST_SAVE_LONG(sb, "range ", op->range);
5102 
5103  if (op->range_modifier != op2->range_modifier)
5104  FAST_SAVE_LONG(sb, "range_modifier ", op->range_modifier);
5105 
5106  if (op->duration_modifier != op2->duration_modifier)
5107  FAST_SAVE_LONG(sb, "duration_modifier ", op->duration_modifier);
5108 
5109  if (op->dam_modifier != op2->dam_modifier)
5110  FAST_SAVE_LONG(sb, "dam_modifier ", op->dam_modifier);
5111 
5112  if (op->gen_sp_armour != op2->gen_sp_armour) {
5113  FAST_SAVE_LONG(sb, "gen_sp_armour ", op->gen_sp_armour);
5114  }
5115 
5116  /* I've kept the old int move type saving code commented out.
5117  * In an ideal world, we'd know if we want to do a quick
5118  * save (say to a temp map, where we don't care about strings),
5119  * or a slower save/dm dump, where printing out strings is handy.
5120  */
5121  if (op->move_type != op2->move_type) {
5122  /*FAST_SAVE_LONG(sb, "move_type ", op->move_type)*/
5123  stringbuffer_append_string(sb, "move_type ");
5125  stringbuffer_append_string(sb, "\n");
5126  }
5127  if (op->move_block != op2->move_block) {
5128  /*FAST_SAVE_LONG(sb, "move_block ", op->move_block)*/
5129  stringbuffer_append_string(sb, "move_block ");
5131  stringbuffer_append_string(sb, "\n");
5132  }
5133  if (op->move_allow != op2->move_allow) {
5134  /*FAST_SAVE_LONG(sb, "move_allow ", op->move_allow);*/
5135  stringbuffer_append_string(sb, "move_allow ");
5137  stringbuffer_append_string(sb, "\n");
5138  }
5139  if (op->move_on != op2->move_on) {
5140  /*FAST_SAVE_LONG(sb, "move_on ", op->move_on);*/
5141  stringbuffer_append_string(sb, "move_on ");
5142  get_string_move_type(sb, op->move_on);
5143  stringbuffer_append_string(sb, "\n");
5144  }
5145  if (op->move_off != op2->move_off) {
5146  /*FAST_SAVE_LONG(sb, "move_off ", op->move_off);*/
5147  stringbuffer_append_string(sb, "move_off ");
5148  get_string_move_type(sb, op->move_off);
5149  stringbuffer_append_string(sb, "\n");
5150  }
5151  if (op->move_slow != op2->move_slow) {
5152  /*FAST_SAVE_LONG(sb, "move_slow ", op->move_slow);*/
5153  stringbuffer_append_string(sb, "move_slow ");
5155  stringbuffer_append_string(sb, "\n");
5156  }
5157 
5158  if (op->move_slow_penalty != op2->move_slow_penalty) {
5159  FAST_SAVE_DOUBLE(sb, "move_slow_penalty ", op->move_slow_penalty);
5160  }
5161 
5162  if (!COMPARE_FLAGS(op, op2)) {
5163  for (tmp = 0; tmp <= NUM_FLAGS; tmp++) {
5164  if (flag_names[tmp] && (QUERY_FLAG(op, tmp) != QUERY_FLAG(op2, tmp))) {
5165  ADD_STRINGLINE_ENTRY(sb, flag_names[tmp], QUERY_FLAG(op, tmp) ? " 1" : " 0");
5166  }
5167  }
5168  }
5169 
5170  /* Save body locations */
5171  for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
5172  if (op->body_info[i] != op2->body_info[i]) {
5173  stringbuffer_append_string(sb, body_locations[i].save_name);
5174  FAST_SAVE_LONG(sb, " ", op->body_info[i]);
5175  }
5176  }
5177 
5178  /* Save discrete damage if applicable
5179  * Note that given how the discrete_damage allocation is done, there can't be any case where
5180  * op->discrete_damage == NULL && op2->discrete_damage != NULL.
5181  */
5182  if (op->discrete_damage) {
5183  for (i = 0; i < NROFATTACKS; i++) {
5184  if (op2->discrete_damage == NULL || op2->discrete_damage[i] != op->discrete_damage[i]) {
5185  stringbuffer_append_string(sb, "damage_");
5186  FAST_SAVE_LONG(sb, resist_save[i], op->discrete_damage[i]);
5187  }
5188  }
5189  }
5190 }
5191 
5196 void save_object_in_sb(StringBuffer *sb, const object *op, const int flag) {
5197  archetype *at = op->arch;
5198  if (at == NULL)
5199  at = empty_archetype;
5200 
5201  stringbuffer_append_printf(sb, "arch %s\n", at->name);
5202 
5203  if (op->arch->reference_count > 0) {
5204  /* The object is a custom item/monster, so we handle its save differently.
5205  * We compare the custom archetype to the "original" one, then only save hp/gr/sp
5206  * which are the only values we can't recompute later - all others are modified by items in inventory.
5207  * Note that hp/gr/sp will appear twice in save, but last value will take precedence.
5208  */
5209  archetype *original = find_archetype(op->arch->name);
5210  if (!original) {
5211  LOG(llevError, "could not find original archetype %s for custom monster!\n", op->arch->name);
5212  abort();
5213  }
5214  get_ob_diff(sb, &op->arch->clone, &original->clone);
5215  if (op->stats.hp != op->arch->clone.stats.hp)
5216  FAST_SAVE_LONG(sb, "hp ", op->stats.hp);
5217  if (op->stats.sp != op->arch->clone.stats.sp)
5218  FAST_SAVE_LONG(sb, "sp ", op->stats.sp);
5219  if (op->stats.grace != op->arch->clone.stats.grace)
5220  FAST_SAVE_LONG(sb, "grace ", op->stats.grace);
5221  if (op->x != op->arch->clone.x)
5222  FAST_SAVE_LONG(sb, "x ", op->x);
5223  if (op->y != op->arch->clone.y)
5224  FAST_SAVE_LONG(sb, "y ", op->y);
5225  }
5226  else {
5227  /* if op is an artifact, then find the "standard" artifact to use that for the diff */
5228  if (op->artifact != NULL) {
5229  object *base;
5230  const artifact *artifact;
5231 
5232  artifact = find_artifact(op, op->artifact);
5233  if (artifact == NULL) {
5234  LOG(llevError, "could not find artifact %s [%d] to save data\n", op->artifact, op->type);
5235  get_ob_diff(sb, op, &at->clone);
5236  } else {
5237  stringbuffer_append_printf(sb, "artifact %s\n", op->artifact);
5238  base = arch_to_object(at);
5239  give_artifact_abilities(base, artifact->item);
5240  get_ob_diff(sb, op, base);
5242  }
5243  } else {
5244  get_ob_diff(sb, op, &at->clone);
5245  }
5246  }
5247 
5248  /* Eneq(@csd.uu.se): Added this to allow containers being saved with contents*/
5249  FOR_INV_PREPARE(op, tmp)
5250  save_object_in_sb(sb, tmp, flag);
5251  FOR_INV_FINISH();
5252 
5253  stringbuffer_append_string(sb, "end\n");
5254 }
5255 
5268 int save_object(FILE *fp, object *op, int flag) {
5269  /* Even if the object does have an owner, it would seem that we should
5270  * still save it.
5271  */
5272  if (object_get_owner(op) != NULL || fp == NULL)
5273  return SAVE_ERROR_OK;
5274 
5275  /* If it is unpaid and we don't want to save those, just return. */
5276  if (!(flag&SAVE_FLAG_SAVE_UNPAID) && (QUERY_FLAG(op, FLAG_UNPAID))) {
5277  return SAVE_ERROR_OK;
5278  }
5279 
5280  /* If the object has no_save set, just return */
5281  if (QUERY_FLAG(op, FLAG_NO_SAVE))
5282  return SAVE_ERROR_OK;
5283 
5285  save_object_in_sb(sb, op, flag);
5286  char *cp = stringbuffer_finish(sb);
5287  if (fputs(cp, fp) == EOF) {
5288  free(cp);
5289  return SAVE_ERROR_WRITE;
5290  } else {
5291  free(cp);
5292  return SAVE_ERROR_OK;
5293  }
5294 }
float weapon_speed_left
Definition: object.h:331
static int compare_ob_value_lists(const object *, const object *)
Definition: object.c:170
int object_distance(const object *ob1, const object *ob2)
Definition: object.c:3599
object * object_find_by_type_and_name(const object *who, int type, const char *name)
Definition: object.c:4017
#define FLAG_NO_MAGIC
Definition: define.h:276
char path[HUGE_BUF]
Definition: map.h:365
int find_dir_2(int x, int y)
Definition: object.c:3615
#define SET_MAP_OB(M, X, Y, tmp)
Definition: map.h:177
int reference_count
Definition: object.h:473
#define FLAG_NO_DROP
Definition: define.h:289
int8_t Int
Definition: living.h:36
#define NUM_BODY_LOCATIONS
Definition: object.h:13
Definition: player.h:92
archetype * find_archetype(const char *name)
Definition: arch.c:692
#define ADD_STRINGLINE_ENTRY(sb__, entryname__, entryvalue__)
Definition: object.c:4838
#define FLAG_DAMNED
Definition: define.h:318
#define FLAG_IS_FLOOR
Definition: define.h:303
#define FLAG_UNPAID
Definition: define.h:236
int8_t ac
Definition: living.h:38
struct _key_value * next
Definition: object.h:43
#define MOVE_WALK
Definition: define.h:407
int32_t weight_limit
Definition: object.h:366
#define UP_OBJ_FACE
Definition: object.h:517
tag_t ownercount
Definition: object.h:380
#define FLAG_IS_LINKED
Definition: define.h:316
MoveType move_type
Definition: object.h:424
#define INS_BELOW_ORIGINATOR
Definition: object.h:570
#define GET_MAP_MOVE_OFF(M, X, Y)
Definition: map.h:207
uint8_t dam_modifier
Definition: object.h:407
MoveType move_on
Definition: object.h:427
EXTERN long ob_count
Definition: global.h:154
#define P_PLAYER
Definition: map.h:236
#define SAVE_ERROR_OK
Definition: map.h:143
struct Statistics statistics
Definition: init.c:115
const char * race
Definition: object.h:318
#define P_NEED_UPDATE
Definition: map.h:239
#define FOR_OB_AND_ABOVE_PREPARE(op_)
Definition: define.h:776
uint16_t attack_movement
Definition: object.h:391
#define OBJ_EXPAND
Definition: config.h:565
void esrv_send_item(object *pl, object *op)
Definition: main.c:339
tag_t attacked_by_count
Definition: object.h:383
#define SET_FLAG(xyz, p)
Definition: define.h:223
sstring add_refcount(sstring str)
Definition: shstr.c:210
void object_clear_owner(object *op)
Definition: object.c:612
MoveType move_allow
Definition: object.h:426
void object_sub_weight(object *op, signed long weight)
Definition: object.c:1640
object * object_find_by_type_applied(const object *who, int type)
Definition: object.c:3992
#define FABS(x)
Definition: define.h:22
unsigned char uint8_t
Definition: win32.h:161
Definition: object.h:221
void object_free_all_data(void)
Definition: object.c:553
void object_copy_owner(object *op, object *clone)
Definition: object.c:688
uint16_t animation_id
Definition: object.h:416
uint16_t material
Definition: object.h:347
tag_t * spell_tags
Definition: object.h:436
sstring artifact
Definition: object.h:314
object * object_find_by_type(const object *who, int type)
Definition: object.c:3921
uint64_t spell_merges
Definition: global.h:346
#define EVENT_DESTROY
Definition: plugin.h:77
object * object_get_env_recursive(object *op)
Definition: object.c:364
struct obj * container
Definition: object.h:291
#define FLAG_FRIENDLY
Definition: define.h:246
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:1006
sstring stringbuffer_finish_shared(StringBuffer *sb)
Definition: stringbuffer.c:85
#define P_NO_MAGIC
Definition: map.h:227
int16_t players
Definition: map.h:344
void fatal(enum fatal_error err)
Definition: utils.c:597
static void get_string_move_type(StringBuffer *sb, MoveType mt)
Definition: object.c:4794
int8_t range
Definition: object.h:405
void init_objects(void)
Definition: object.c:93
#define FLAG_NO_FIX_PLAYER
Definition: define.h:277
#define LOOK_OBJ(ob)
Definition: object.h:505
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
uint32_t in_memory
Definition: map.h:345
void get_search_arr(int *search_arr)
Definition: object.c:3516
unsigned char MoveType
Definition: define.h:432
int32_t last_heal
Definition: object.h:357
int16_t maxgrace
Definition: living.h:45
void free_string(sstring str)
Definition: shstr.c:280
#define UP_OBJ_INSERT
Definition: object.h:514
signed long object_sum_weight(object *op)
Definition: object.c:342
#define HUGE_BUF
Definition: define.h:37
void object_fix_multipart(object *tmp)
Definition: object.c:4582
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.c:2380
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:342
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4264
struct treasureliststruct * randomitems
Definition: object.h:385
void query_base_name(const object *op, int plural, char *buf, size_t size)
Definition: item.c:723
#define MAP_HEIGHT(m)
Definition: map.h:80
object clone
Definition: object.h:470
int16_t duration
Definition: object.h:403
socket_struct socket
Definition: player.h:94
int16_t invisible
Definition: object.h:360
short freearr_x[SIZEOFFREE]
Definition: object.c:65
int object_find_multi_free_spot_within_radius(const object *ob, const object *gen, int *hx, int *hy)
Definition: object.c:3281
uint8_t duration_modifier
Definition: object.h:404
Definition: object.h:240
int object_count_active(void)
Definition: object.c:1613
const char * key
Definition: object.h:41
struct obj * prev
Definition: object.h:278
const char * slaying
Definition: object.h:319
object * object_find_by_flag(const object *who, int flag)
Definition: object.c:4115
int32_t last_sp
Definition: object.h:358
int object_check_move_on(object *op, object *originator)
Definition: object.c:2850
uint8_t subtype
Definition: object.h:339
int64_t exp
Definition: living.h:47
#define FREE_OBJ_NO_DESTROY_CALLBACK
Definition: object.h:531
int arch_init
Definition: arch.c:41
struct obj * above
Definition: object.h:288
void object_dump_all(void)
Definition: object.c:481
double expmul
Definition: object.h:395
object * object_find_by_type_and_slaying(const object *who, int type, const char *slaying)
Definition: object.c:4067
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:217
#define object_was_destroyed(op, old_tag)
Definition: object.h:68
#define TRUE
Definition: compat.h:10
int freedir[SIZEOFFREE]
Definition: object.c:83
object * arch_present_in_ob(const archetype *at, const object *op)
Definition: object.c:3076
body_locations_struct body_locations[NUM_BODY_LOCATIONS]
Definition: item.c:54
#define FALSE
Definition: compat.h:11
void remove_friendly_object(object *op)
Definition: friend.c:56
uint32_t path_attuned
Definition: object.h:343
#define MAX(x, y)
Definition: compat.h:20
void object_update(object *op, int action)
Definition: object.c:1260
int16_t sp
Definition: living.h:42
uint32_t get_weight_limit(int stat)
Definition: living.c:2281
int object_can_merge(object *ob1, object *ob2)
Definition: object.c:202
uint8_t smoothlevel
Definition: object.h:421
uint32_t path_repelled
Definition: object.h:344
#define safe_strncpy
Definition: compat.h:23
uint32_t hidden
Definition: player.h:132
void free_dialog_information(object *op)
Definition: dialog.c:32
struct obj * enemy
Definition: object.h:381
struct archt * other_arch
Definition: object.h:413
#define FAST_SAVE_LONG(sb__, entryname__, entryvalue__)
Definition: object.c:4844
int absdir(int d)
Definition: object.c:3652
object * object_find_by_name(const object *who, const char *name)
Definition: object.c:3897
Definition: object.h:465
uint8_t casting_time
Definition: global.h:266
uint64_t spell_hash_full
Definition: global.h:347
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:791
int16_t maxsp
Definition: living.h:43
int8_t Con
Definition: living.h:36
#define FLAG_REMOVED
Definition: define.h:232
int16_t hp
Definition: living.h:40
#define CALLOC(x, y)
Definition: compat.h:27
int nrofallocobjects
Definition: object.c:57
short freearr_y[SIZEOFFREE]
Definition: object.c:71
object * active_objects
Definition: object.c:62
static const int reduction_dir[SIZEOFFREE][3]
Definition: object.c:3690
#define SAVE_ERROR_WRITE
Definition: map.h:146
void object_set_flag_inv(object *op, int flag)
Definition: object.c:3096
#define MOVE_ALL
Definition: define.h:413
void esrv_del_item(player *pl, object *ob)
Definition: main.c:355
int object_find_multi_free_spot_around(const object *ob, const object *gen, int16_t *hx, int16_t *hy)
Definition: object.c:3162
void object_set_owner(object *op, object *owner)
Definition: object.c:632
int16_t * discrete_damage
Definition: object.h:435
#define SAVE_FLAG_SAVE_UNPAID
Definition: map.h:110
Definition: object.h:241
const char * lore
Definition: object.h:323
#define MAP_IN_MEMORY
Definition: map.h:130
int object_set_value(object *op, const char *key, const char *value, int add_key)
Definition: object.c:4393
uint32_t update_look
Definition: newserver.h:104
void object_free_drop_inventory(object *ob)
Definition: object.c:1389
const char * name
Definition: face.h:20
const char * title
Definition: object.h:317
int16_t y
Definition: object.h:326
key_value * object_get_key_value(const object *ob, const char *key)
Definition: object.c:4236
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1936
int16_t maxhp
Definition: living.h:41
sstring find_string(const char *str)
Definition: shstr.c:236
void object_replace_insert_in_map(const char *arch_string, object *op)
Definition: object.c:2440
int dirdiff(int dir1, int dir2)
Definition: object.c:3670
void object_update_turn_face(object *op)
Definition: object.c:1130
#define FREE_OBJ_FREE_INVENTORY
Definition: object.h:530
void object_dump(const object *op, StringBuffer *sb)
Definition: object.c:425
#define FLAG_CLIENT_ANIM_RANDOM
Definition: define.h:241
EXTERN archetype * empty_archetype
Definition: global.h:149
uint32_t path_denied
Definition: object.h:345
int maxfree[SIZEOFFREE]
Definition: object.c:77
#define STARTMAX
Definition: config.h:564
#define FLAG_ALIVE
Definition: define.h:230
object * object_new(void)
Definition: object.c:1068
const char * name_pl
Definition: object.h:315
#define FLAG_OBJ_ORIGINAL
Definition: define.h:365
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Definition: stringbuffer.c:95
object * object_find_by_tag_global(tag_t i)
Definition: object.c:507
#define FOR_OB_AND_ABOVE_FINISH()
Definition: define.h:780
int object_can_pick(const object *who, const object *item)
Definition: object.c:3808
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2705
float speed_left
Definition: object.h:329
#define MAP_DARKNESS(m)
Definition: map.h:75
#define FLAG_NO_STEAL
Definition: define.h:343
signed short int16_t
Definition: win32.h:160
int16_t ox
Definition: object.h:327
const char * materialname
Definition: object.h:346
int32_t weight
Definition: object.h:365
#define MOVE_FLY_LOW
Definition: define.h:408
const char * anim_suffix
Definition: object.h:316
#define FOR_ABOVE_FINISH()
Definition: define.h:731
void update_all_los(const mapstruct *map, int x, int y)
Definition: los.c:532
uint8_t range_modifier
Definition: object.h:406
int8_t Wis
Definition: living.h:36
uint32_t flags[4]
Definition: object.h:415
struct mapdef * map
Definition: object.h:297
void free_arch(archetype *at)
Definition: arch.c:263
#define snprintf
Definition: win32.h:46
struct obj * active_prev
Definition: object.h:283
#define FLAG_NO_SAVE
Definition: define.h:244
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Definition: map.c:487
const char *const move_name[]
Definition: object.c:4734
const artifact * find_artifact(const object *op, const char *name)
Definition: artifact.c:657
#define FLAG_CLIENT_ANIM_SYNC
Definition: define.h:240
object * transport
Definition: player.h:195
#define FLAG_IDENTIFIED
Definition: define.h:261
#define FOR_INV_FINISH()
Definition: define.h:714
object * object_find_by_flag_applied(const object *who, int flag)
Definition: object.c:4138
void object_remove_from_active_list(object *op)
Definition: object.c:1215
#define FMT64
Definition: compat.h:12
void update_position(mapstruct *m, int x, int y)
Definition: map.c:2131
int16_t dam
Definition: living.h:46
int object_count_free(void)
Definition: object.c:1575
int32_t carrying
Definition: object.h:367
object * objects
Definition: object.c:60
#define FLAG_BLOCKSVIEW
Definition: define.h:269
struct artifactstruct artifact
static const char *const flag_names[NUM_FLAGS+1]
Definition: object.c:4753
const char * name
Definition: object.h:311
struct obj * env
Definition: object.h:293
#define FOR_ABOVE_PREPARE(op_, it_)
Definition: define.h:724
int64_t perm_exp
Definition: object.h:369
int8_t gen_sp_armour
Definition: object.h:363
#define OB_SPELL_TAG_MATCH(op, count)
Definition: object.h:93
int save_object(FILE *fp, object *op, int flag)
Definition: object.c:5268
uint8_t state
Definition: object.h:349
struct obj * below
Definition: object.h:287
struct archt * more
Definition: object.h:469
#define INS_NO_WALK_ON
Definition: object.h:568
object * object_present_in_ob(uint8_t type, const object *op)
Definition: object.c:3016
#define offsetof(type, member)
Definition: shstr.h:37
#define FLAG_IS_A_TEMPLATE
Definition: define.h:375
int16_t last_grace
Definition: object.h:359
void object_reset(object *op)
Definition: object.c:735
#define GET_MAP_TOP(M, X, Y)
Definition: map.h:174
int8_t direction
Definition: object.h:334
object * object_create_clone(object *asrc)
Definition: object.c:3850
#define NUM_FLAGS
Definition: define.h:385
uint32_t nrof
Definition: object.h:333
int8_t Cha
Definition: living.h:36
#define GET_MAP_MOVE_ON(M, X, Y)
Definition: map.h:202
#define FLAG_OVERLAY_FLOOR
Definition: define.h:255
#define SIZEOFFREE
Definition: define.h:154
#define P_OUT_OF_MAP
Definition: map.h:251
MoveType move_off
Definition: object.h:428
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:192
object * object_find_by_name_global(const char *str)
Definition: object.c:530
EXTERN Animations * animations
Definition: global.h:163
struct pl * contr
Definition: object.h:276
int8_t item_power
Definition: object.h:362
object * object_insert_in_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:2167
void object_clear(object *op)
Definition: object.c:790
int16_t oy
Definition: object.h:327
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:206
#define MAP_SAVING
Definition: map.h:133
uint32_t tag_t
Definition: object.h:12
object * object_find_by_type_and_race(const object *who, int type, const char *race)
Definition: object.c:4042
int8_t luck
Definition: living.h:39
float speed
Definition: object.h:328
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#define HEAD(op)
Definition: object.h:592
#define INS_ABOVE_FLOOR_ONLY
Definition: object.h:567
#define FLAG_BEEN_APPLIED
Definition: define.h:324
void object_get_multi_size(const object *ob, int *sx, int *sy, int *hx, int *hy)
Definition: object.c:4644
#define MAX_BUF
Definition: define.h:35
static object * free_objects
Definition: object.c:61
struct obj * active_next
Definition: object.h:279
static void expand_objects(void)
Definition: object.c:1024
void give_artifact_abilities(object *op, const object *artifact)
Definition: artifact.c:203
method_ret ob_move_on(object *op, object *victim, object *originator)
Definition: ob_methods.c:105
int16_t x
Definition: object.h:326
int strncasecmp(const char *s1, const char *s2, int n)
Definition: porting.c:224
const char *const map_layer_name[MAP_LAYERS]
Definition: map.c:44
int nroffreeobjects
Definition: object.c:56
MoveType move_slow
Definition: object.h:429
const char * skill
Definition: object.h:321
int32_t last_eat
Definition: object.h:356
Definition: object.h:201
int8_t wc
Definition: living.h:37
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
Definition: object.c:3473
void object_insert_to_free_spot_or_free(object *op, mapstruct *map, int x, int y, int start, int stop, object *originator)
Definition: object.c:4693
static const flag_definition flags[]
#define FOR_MAP_FINISH()
Definition: define.h:767
key_value * key_values
Definition: object.h:433
struct obj * next
Definition: object.h:277
int8_t Str
Definition: living.h:36
#define COMPARE_FLAGS(p, q)
Definition: define.h:226
void animate_object(object *op, int dir)
Definition: anim.c:212
#define FLAG_NO_APPLY
Definition: define.h:302
int16_t resist[NROFATTACKS]
Definition: object.h:341
#define FLAG_KNOWN_CURSED
Definition: define.h:321
object * ob
Definition: player.h:158
object * object_find_by_tag(const object *who, tag_t tag)
Definition: object.c:3969
#define GET_MAP_FLAGS(M, X, Y)
Definition: map.h:161
#define FLAG_IS_HILLY
Definition: define.h:333
#define SPELL_TAG_SIZE
Definition: object.h:81
#define FLAG_CURSED
Definition: define.h:317
Definition: object.h:107
unsigned int uint32_t
Definition: win32.h:162
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.c:4213
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:163
#define FLAG_ANIMATE
Definition: define.h:242
int8_t body_info[NUM_BODY_LOCATIONS]
Definition: object.h:372
const char * name
Definition: treasure.h:83
object * object_find_by_type_and_skill(const object *who, int type, const char *skill)
Definition: object.c:4092
uint32_t attacktype
Definition: object.h:342
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
Definition: object.c:2478
uint8_t map_layer
Definition: object.h:422
#define INS_NO_MERGE
Definition: object.h:566
object * object_decrease_nrof(object *op, uint32_t i)
Definition: object.c:2520
void save_object_in_sb(StringBuffer *sb, const object *op, const int flag)
Definition: object.c:5196
#define UP_OBJ_CHANGE
Definition: object.h:516
struct obj * owner
Definition: object.h:377
#define RANDOM()
Definition: define.h:681
int16_t grace
Definition: living.h:44
#define UPD_NROF
Definition: newclient.h:296
signed char int8_t
Definition: win32.h:158
Definition: object.h:143
#define GET_MAP_PLAYER(M, X, Y)
Definition: map.h:168
New_Face * blank_face
Definition: image.c:39
const char * custom_name
Definition: object.h:432
#define UP_OBJ_REMOVE
Definition: object.h:515
int get_button_value(const object *button)
Definition: button.c:754
tag_t count
Definition: object.h:299
living stats
Definition: object.h:368
uint16_t client_type
Definition: object.h:340
int8_t Dex
Definition: living.h:36
struct archt * arch
Definition: object.h:412
#define AB_NO_PASS
Definition: map.h:235
#define UPD_FACE
Definition: newclient.h:292
#define MAP_WIDTH(m)
Definition: map.h:78
uint8_t will_apply
Definition: object.h:392
uint8_t type
Definition: object.h:338
uint32_t do_los
Definition: player.h:126
struct Settings settings
Definition: init.c:40
void object_set_enemy(object *op, object *enemy)
Definition: object.c:710
void object_free2(object *ob, int flags)
Definition: object.c:1412
uint32_t weapontype
Definition: object.h:371
#define FLAG_APPLIED
Definition: define.h:235
#define NROFATTACKS
Definition: attack.h:17
object * object_merge(object *op, object *top)
Definition: object.c:1884
void query_short_name(const object *op, char *buf, size_t size)
Definition: item.c:548
object * object_get_player_container(object *op)
Definition: object.c:384
int out_of_map(mapstruct *m, int x, int y)
Definition: map.c:2306
int execute_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: main.c:364
static object objarray[STARTMAX]
Definition: object.c:55
#define SIZEOFFREE2
Definition: define.h:153
#define OB_SPELL_TAG_HASH(op, count)
Definition: object.h:87
int object_count_used(void)
Definition: object.c:1594
static const object * object_get_owner_const(const object *op)
Definition: object.c:401
const char * msg
Definition: object.h:322
const char * value
Definition: object.h:42
static void permute(int *, int, int)
Definition: object.c:3492
int16_t casting_time
Definition: object.h:402
void object_unset_flag_inv(object *op, int flag)
Definition: object.c:3116
int object_matches_string(object *pl, object *op, const char *name)
Definition: object.c:4471
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:787
#define FLAG_STARTEQUIP
Definition: define.h:268
void object_merge_spell(object *op, int16_t x, int16_t y)
Definition: object.c:1968
int map_find_dir(mapstruct *m, int x, int y, object *exclude)
Definition: object.c:3549
#define GET_ANIM_ID(ob)
Definition: global.h:171
sstring add_string(const char *str)
Definition: shstr.c:124
EXTERN player * first_player
Definition: global.h:117
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.c:104
struct pl * next
Definition: player.h:93
#define MIN_ACTIVE_SPEED
Definition: define.h:676
void object_add_weight(object *op, signed long weight)
Definition: object.c:2678
#define GET_MAP_OB(M, X, Y)
Definition: map.h:172
int strcasecmp(const char *s1, const char *s2)
Definition: porting.c:256
int8_t glow_radius
Definition: object.h:364
#define FLAG_MONSTER
Definition: define.h:245
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:869
int8_t Pow
Definition: living.h:36
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:300
struct obj * inv
Definition: object.h:290
void object_set_cheat(object *op)
Definition: object.c:3137
#define SIZEOFFREE1
Definition: define.h:152
struct obj * head
Definition: object.h:296
#define MOVE_FLY_HIGH
Definition: define.h:409
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
static void object_increase_nrof(object *op, uint32_t i)
Definition: object.c:2606
EXTERN const char *const resist_save[NROFATTACKS]
Definition: attack.h:139
#define SET_MAP_TOP(M, X, Y, tmp)
Definition: map.h:179
#define FLAG_WAS_WIZ
Definition: define.h:234
uint8_t pick_up
Definition: object.h:361
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
#define FREE_OBJ_DROP_ABOVE_FLOOR
Definition: object.h:532
MoveType move_block
Definition: object.h:425
uint8_t run_away
Definition: object.h:384
#define GET_MAP_MOVE_SLOW(M, X, Y)
Definition: map.h:197
#define P_BLOCKSVIEW
Definition: map.h:226
void object_set_msg(object *op, const char *msg)
Definition: object.c:4713
int can_see_monsterP(mapstruct *m, int x, int y, int dir)
Definition: object.c:3760
int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop)
Definition: object.c:3430
#define FLAG_DIALOG_PARSED
Definition: define.h:243
#define FAST_SAVE_DOUBLE(sb__, entryname__, entryvalue__)
Definition: object.c:4847
#define P_NO_ERROR
Definition: map.h:240
float move_slow_penalty
Definition: object.h:430
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:626
object * item
Definition: artifact.h:15
object * find_skill_by_number(object *who, int skillno)
Definition: main.c:351
object * map_find_by_archetype(mapstruct *m, int x, int y, const archetype *at)
Definition: object.c:2959
Definition: map.h:325
#define P_IS_ALIVE
Definition: map.h:237
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.c:4863
const New_Face * face
Definition: object.h:332
#define FREE_AND_CLEAR(xyz)
Definition: global.h:201
#define FLAG_IS_WOODED
Definition: define.h:331
#define FLAG_NO_PICK
Definition: define.h:239
object * object_find_by_type_and_arch_name(const object *who, int type, const char *name)
Definition: object.c:4186
#define FLAG_WIZPASS
Definition: define.h:315
int32_t move_status
Definition: object.h:390
void object_free_key_values(object *op)
Definition: object.c:758
int16_t level
Definition: object.h:351
#define FLAG_INV_LOCKED
Definition: define.h:330
void fix_object(object *op)
Definition: living.c:1119
float weapon_speed
Definition: object.h:330
struct obj * more
Definition: object.h:295
object * arch_to_object(archetype *at)
Definition: arch.c:569
void object_update_speed(object *op)
Definition: object.c:1150
#define SCRIPT_FIX_NOTHING
Definition: global.h:362
int32_t value
Definition: object.h:350
object * object_get_owner(object *op)
Definition: object.c:590
int8_t magic
Definition: object.h:348
const char * name
Definition: object.h:466
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
#define P_NO_CLERIC
Definition: map.h:238
object * object_find_by_arch_name(const object *who, const char *name)
Definition: object.c:4161
object * object_present_in_ob_by_name(int type, const char *str, const object *op)
Definition: object.c:3054
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:707
void object_remove(object *op)
Definition: object.c:1669
object * map_find_by_type(mapstruct *m, int x, int y, uint8_t type)
Definition: object.c:2989
static int compare_ob_value_lists_one(const object *, const object *)
Definition: object.c:129
#define INS_MAP_LOAD
Definition: object.h:571
object * object_find_by_type2(const object *who, int type1, int type2)
Definition: object.c:3946
int32_t food
Definition: living.h:48
#define FLAG_FREED
Definition: define.h:233
uint32_t count
Definition: player.h:109
static int object_set_value_s(object *, const char *, const char *, int)
Definition: object.c:4307
#define FLAG_IS_TURNABLE
Definition: define.h:256