Crossfire Server, Trunk  R22047
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
102  objarray[0].prev = NULL,
103  objarray[0].next = &objarray[1],
106  for (int i = 1; i < STARTMAX-1; i++) {
107  objarray[i].next = &objarray[i+1];
108  objarray[i].prev = &objarray[i-1];
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 
199 int object_can_merge(object *ob1, object *ob2) {
200  /* A couple quicksanity checks */
201  if (ob1 == ob2 || ob1->type != ob2->type)
202  return 0;
203 
204  if (ob1->speed != ob2->speed)
205  return 0;
206  /* Note sure why the following is the case - either the object has to
207  * be animated or have a very low speed. Is this an attempted monster
208  * check?
209  */
210  /*TODO is this check really needed?*/
211  if (!QUERY_FLAG(ob1, FLAG_ANIMATE) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED)
212  return 0;
213 
214  /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that
215  * value could not be stored in a int32_t (which unfortunately sometimes is
216  * used to store nrof).
217  */
218  if (ob1->nrof+ob2->nrof >= 1UL<<31)
219  return 0;
220 
221  /* This is really a spellbook check - really, we should
222  * check all objects in the inventory.
223  */
224  /*TODO is this check really needed?*/
225  if (ob1->inv || ob2->inv) {
226  /* if one object has inventory but the other doesn't, not equiv */
227  if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv))
228  return 0;
229 
230  /* Now check to see if the two inventory objects could merge */
231  if (!object_can_merge(ob1->inv, ob2->inv))
232  return 0;
233 
234  /* inventory ok - still need to check rest of this object to see
235  * if it is valid.
236  */
237  }
238 
239  /* If the objects have been identified, set the BEEN_APPLIED flag.
240  * This is to the comparison of the flags below will be OK. We
241  * just can't ignore the been applied or identified flags, as they
242  * are not equal - just if it has been identified, the been_applied
243  * flags lose any meaning.
244  */
245 
246  /*TODO is this hack on BEEN_APPLIED really needed? */
247  if (QUERY_FLAG(ob1, FLAG_IDENTIFIED))
249 
250  if (QUERY_FLAG(ob2, FLAG_IDENTIFIED))
252 
253 
254  /* Note: FLAG_INV_LOCKED is ignored for merging purposes */
255  if ((ob1->arch != ob2->arch)
256  || (ob1->flags[0] != ob2->flags[0])
257  || (ob1->flags[1] != ob2->flags[1])
258  || (ob1->flags[2] != ob2->flags[2])
259  || ((ob1->flags[3]&~0x84) != (ob2->flags[3]&~0x84)) /* ignore CLIENT_SENT and FLAG_OBJ_ORIGINAL */
260  || (ob1->name != ob2->name)
261  || (ob1->title != ob2->title)
262  || (ob1->msg != ob2->msg)
263  || (ob1->weight != ob2->weight)
264  || (ob1->item_power != ob2->item_power)
265  || (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist)) != 0)
266  || (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats)) != 0)
267  || (ob1->attacktype != ob2->attacktype)
268  || (ob1->magic != ob2->magic)
269  || (ob1->slaying != ob2->slaying)
270  || (ob1->skill != ob2->skill)
271  || (ob1->value != ob2->value)
272  || (ob1->animation != ob2->animation)
273  || (ob1->client_type != ob2->client_type)
274  || (ob1->materialname != ob2->materialname)
275  || (ob1->lore != ob2->lore)
276  || (ob1->subtype != ob2->subtype)
277  || (ob1->move_type != ob2->move_type)
278  || (ob1->move_block != ob2->move_block)
279  || (ob1->move_allow != ob2->move_allow)
280  || (ob1->move_on != ob2->move_on)
281  || (ob1->move_off != ob2->move_off)
282  || (ob1->move_slow != ob2->move_slow)
283  || (ob1->move_slow_penalty != ob2->move_slow_penalty)
284  || (ob1->map_layer != ob2->map_layer))
285  return 0;
286 
287  /* Don't merge objects that are applied. With the new 'body' code,
288  * it is possible for most any character to have more than one of
289  * some items equipped, and we don't want those to merge.
290  */
291  if (QUERY_FLAG(ob1, FLAG_APPLIED) || QUERY_FLAG(ob2, FLAG_APPLIED))
292  return 0;
293 
294  if (ob1->key_values != NULL || ob2->key_values != NULL) {
295  /* At least one of these has key_values. */
296  if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) {
297  /* One has fields, but the other one doesn't. */
298  return 0;
299  } else {
300  if (!compare_ob_value_lists(ob1, ob2)) {
301  return 0;
302  }
303  }
304  }
305 
306  /*TODO should this really be limited to scrolls?*/
307  switch (ob1->type) {
308  case SCROLL:
309  if (ob1->level != ob2->level)
310  return 0;
311  break;
312  }
313 
314  /* Don't merge items with differing custom names. */
315  if (ob1->custom_name != ob2->custom_name)
316  return 0;
317 
318  /* Everything passes, must be OK. */
319  return 1;
320 }
321 
337 /* TODO should check call this this are made a place where we really need reevaluaton of whole tree */
338 signed long object_sum_weight(object *op) {
339  signed long sum;
340 
341  sum = 0;
342  FOR_INV_PREPARE(op, inv) {
343  if (inv->inv)
344  object_sum_weight(inv);
345  sum += inv->carrying+inv->weight*NROF(inv);
346  } FOR_INV_FINISH();
347  if (op->type == CONTAINER && op->stats.Str)
348  sum = (sum*(100-op->stats.Str))/100;
349  op->carrying = sum;
350  return sum;
351 }
352 
360 object *object_get_env_recursive(object *op) {
361  while (op->env != NULL)
362  op = op->env;
363  return op;
364 }
365 
377 object *object_get_player_container(object *op) {
378  for (; op != NULL && op->type != PLAYER; op = op->env)
379  /*TODO this is patching the structure on the flight as side effect. Shoudln't be needed in clean code */
380  if (op->env == op)
381  op->env = NULL;
382  return op;
383 }
384 
394 static const object *object_get_owner_const(const object *op) {
395  if (op->owner == NULL)
396  return NULL;
397 
398  if (!QUERY_FLAG(op->owner, FLAG_FREED)
399  && !QUERY_FLAG(op->owner, FLAG_REMOVED)
400  && op->owner->count == op->ownercount)
401  return op->owner;
402 
403  LOG(llevError, "Warning, no owner found\n");
404  return NULL;
405 }
406 
415 void object_dump(const object *op, StringBuffer *sb) {
416  if (op == NULL) {
417  stringbuffer_append_string(sb, "[NULL pointer]");
418  return;
419  }
420 
421  /* object *tmp;*/
422 
423  if (op->arch != NULL) {
424  const object *owner;
425 
426  stringbuffer_append_string(sb, "arch ");
427  stringbuffer_append_string(sb, op->arch->name ? op->arch->name : "(null)");
428  stringbuffer_append_string(sb, "\n");
429 
430  if (op->artifact != NULL) {
431  stringbuffer_append_string(sb, "artifact ");
432  stringbuffer_append_string(sb, op->artifact);
433  stringbuffer_append_string(sb, "\n");
434  }
435 
436  get_ob_diff(sb, op, &empty_archetype->clone);
437  if (op->more) {
438  stringbuffer_append_printf(sb, "more %u\n", op->more->count);
439  }
440  if (op->head) {
441  stringbuffer_append_printf(sb, "head %u\n", op->head->count);
442  }
443  if (op->env) {
444  stringbuffer_append_printf(sb, "env %u\n", op->env->count);
445  }
446  if (op->inv) {
447  stringbuffer_append_printf(sb, "inv %u\n", op->inv->count);
448  }
449  owner = object_get_owner_const(op);
450  if (owner != NULL) {
451  stringbuffer_append_printf(sb, "owner %u\n", owner->count);
452  }
453  stringbuffer_append_string(sb, "end\n");
454  } else {
455  stringbuffer_append_string(sb, "Object ");
456  stringbuffer_append_string(sb, op->name == NULL ? "(null)" : op->name);
457  stringbuffer_append_string(sb, "\nend\n");
458  }
459 }
460 
468 void object_dump_all(void) {
469  object *op;
470 
471  for (op = objects; op != NULL; op = op->next) {
472  StringBuffer *sb;
473  char *diff;
474 
475  sb = stringbuffer_new();
476  object_dump(op, sb);
477  diff = stringbuffer_finish(sb);
478  LOG(llevDebug, "Object %u\n:%s\n", op->count, diff);
479  free(diff);
480  }
481 }
482 
491 object *object_find_by_tag_global(tag_t i) {
492  object *op;
493 
494  for (op = objects; op != NULL; op = op->next)
495  if (op->count == i)
496  break;
497  return op;
498 }
499 
511 object *object_find_by_name_global(const char *str) {
512  const char *name = add_string(str);
513  object *op;
514 
515  for (op = objects; op != NULL; op = op->next)
516  if (op->name == name)
517  break;
518  free_string(name);
519  return op;
520 }
521 
532 #ifdef MEMORY_DEBUG
533  object *op, *next;
534 
535  for (op = free_objects; op != NULL; ) {
536  next = op->next;
537  free(op);
539  nroffreeobjects--;
540  op = next;
541  }
542  free_objects = NULL;
543 
544  for (op = objects; op != NULL; ) {
545  next = op->next;
546  if (!QUERY_FLAG(op, FLAG_FREED)) {
547  LOG(llevDebug, "non freed object: %s\n", op->name);
548  }
549  op = next;
550  }
551 #endif
552 
553  LOG(llevDebug, "%d allocated objects, %d free objects, STARMAX=%d\n", nrofallocobjects, nroffreeobjects, STARTMAX);
554 }
555 
568 object *object_get_owner(object *op) {
569  if (op->owner == NULL)
570  return NULL;
571 
572  if (!QUERY_FLAG(op->owner, FLAG_FREED)
573  && !QUERY_FLAG(op->owner, FLAG_REMOVED)
574  && op->owner->count == op->ownercount)
575  return op->owner;
576 
577  object_clear_owner(op);
578  return NULL;
579 }
580 
587 void object_clear_owner(object *op) {
588  if (!op)
589  return;
590 
591  op->owner = NULL;
592  op->ownercount = 0;
593 }
594 
604 void object_set_owner(object *op, object *owner) {
605  /* Assign temp to something, so it can't accidentally be NULL */
606  object *tmp = owner;
607  if (op == NULL)
608  return;
609  if (owner == NULL) {
610  object_clear_owner(op);
611  return;
612  }
613 
614  /* next line added to allow objects which own objects */
615  /* Add a check for ownercounts in here, as I got into an endless loop
616  * with the fireball owning a poison cloud which then owned the
617  * fireball. I believe that was caused by one of the objects getting
618  * freed and then another object replacing it. Since the ownercounts
619  * didn't match, this check is valid and I believe that cause is valid.
620  */
621  /*
622  * if owner is NULL, function will have already returned,
623  * so loop should still function as before.
624  */
625  while (tmp) {
626  tmp = object_get_owner(owner);
627  if (tmp)
628  owner = tmp;
629  }
630 
631  /* must not cause owner cycles */
632  assert(op != owner);
633 
634  if (op->owner != NULL)
635  object_clear_owner(op);
636 
637  op->owner = owner;
638  op->ownercount = owner->count;
639 }
640 
657 void object_copy_owner(object *op, object *clone) {
658  object *owner = object_get_owner(clone);
659  if (owner == NULL) {
660  /* players don't have owners - they own themselves. Update
661  * as appropriate.
662  */
663  /*TODO owner=self is dangerous and should be avoided*/
664  if (clone->type != PLAYER)
665  return;
666  owner = clone;
667  }
668  object_set_owner(op, owner);
669 }
670 
679 void object_set_enemy(object *op, object *enemy) {
680  if (op->enemy == enemy) {
681  return;
682  }
683 
684 #if 0
685  if (op->type != PLAYER) {
686  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);
687  }
688 #endif
689  op->enemy = enemy;
690 }
691 
698 void object_reset(object *op) {
699  op->name = NULL;
700  op->name_pl = NULL;
701  op->title = NULL;
702  op->race = NULL;
703  op->slaying = NULL;
704  op->skill = NULL;
705  op->msg = NULL;
706  op->materialname = NULL;
707  op->lore = NULL;
708  object_clear(op);
709 }
710 
718 void object_free_key_values(object *op) {
719  key_value *i;
720  key_value *next = NULL;
721 
722  if (op->key_values == NULL)
723  return;
724 
725  for (i = op->key_values; i != NULL; i = next) {
726  /* Store next *first*. */
727  next = i->next;
728 
729  if (i->key)
730  FREE_AND_CLEAR_STR(i->key);
731  if (i->value)
732  FREE_AND_CLEAR_STR(i->value);
733  i->next = NULL;
734  free(i);
735  }
736 
737  op->key_values = NULL;
738 }
739 
747 void object_clear(object *op) {
748  /*TODO this comment must be investigated*/
749  /* redo this to be simpler/more efficient. Was also seeing
750  * crashes in the old code. Move this to the top - am
751  * seeing periodic crashes in this code, and would like to have
752  * as much info available as possible (eg, object name).
753  */
756 
757  /* the memset will clear all these values for us, but we need
758  * to reduce the refcount on them.
759  */
760  if (op->name != NULL)
761  FREE_AND_CLEAR_STR(op->name);
762  if (op->name_pl != NULL)
763  FREE_AND_CLEAR_STR(op->name_pl);
764  if (op->title != NULL)
765  FREE_AND_CLEAR_STR(op->title);
766  if (op->race != NULL)
767  FREE_AND_CLEAR_STR(op->race);
768  if (op->slaying != NULL)
769  FREE_AND_CLEAR_STR(op->slaying);
770  if (op->skill != NULL)
771  FREE_AND_CLEAR_STR(op->skill);
772  if (op->msg != NULL)
773  FREE_AND_CLEAR_STR(op->msg);
774  if (op->lore != NULL)
775  FREE_AND_CLEAR_STR(op->lore);
776  if (op->materialname != NULL)
777  FREE_AND_CLEAR_STR(op->materialname);
778 
779  /* Remove object from friendly list if needed. */
780  if (QUERY_FLAG(op, FLAG_FRIENDLY))
782 
783  memset((void *)((char *)op+offsetof(object, name)), 0, sizeof(object)-offsetof(object, name));
784  /* Below here, we clear things that are not done by the memset,
785  * or set default values that are not zero.
786  */
787  /* This is more or less true */
788  SET_FLAG(op, FLAG_REMOVED);
789 
790 
791  op->contr = NULL;
792  op->below = NULL;
793  op->above = NULL;
794  op->inv = NULL;
795  op->container = NULL;
796  op->env = NULL;
797  op->more = NULL;
798  op->head = NULL;
799  op->map = NULL;
800  op->active_next = NULL;
801  op->active_prev = NULL;
802  /* What is not cleared is next, prev, and count */
803 
804  op->expmul = 1.0;
805  op->face = blank_face;
806  op->attacked_by_count = -1;
808  op->casting_time = -1;
809 }
810 
824 void object_copy(const object *src_ob, object *dest_ob) {
825  int is_freed = QUERY_FLAG(dest_ob, FLAG_FREED), is_removed = QUERY_FLAG(dest_ob, FLAG_REMOVED);
826 
827  /* Decrement the refcounts, but don't bother zeroing the fields;
828  they'll be overwritten by memcpy. */
829  if (dest_ob->artifact != NULL)
830  free_string(dest_ob->artifact);
831  if (dest_ob->name != NULL)
832  free_string(dest_ob->name);
833  if (dest_ob->name_pl != NULL)
834  free_string(dest_ob->name_pl);
835  if (dest_ob->anim_suffix != NULL)
836  free_string(dest_ob->anim_suffix);
837  if (dest_ob->title != NULL)
838  free_string(dest_ob->title);
839  if (dest_ob->race != NULL)
840  free_string(dest_ob->race);
841  if (dest_ob->slaying != NULL)
842  free_string(dest_ob->slaying);
843  if (dest_ob->skill != NULL)
844  free_string(dest_ob->skill);
845  if (dest_ob->msg != NULL)
846  free_string(dest_ob->msg);
847  if (dest_ob->lore != NULL)
848  free_string(dest_ob->lore);
849  if (dest_ob->materialname != NULL)
850  free_string(dest_ob->materialname);
851  if (dest_ob->custom_name != NULL)
852  free_string(dest_ob->custom_name);
853  if (dest_ob->spell_tags != NULL)
854  FREE_AND_CLEAR(dest_ob->spell_tags);
855 
856  /* Basically, same code as from object_clear() */
857 
858  object_free_key_values(dest_ob);
859  free_dialog_information(dest_ob);
860 
861  /* Copy all attributes below name (name included). */
862  (void)memcpy((void *)((char *)dest_ob+offsetof(object, name)),
863  (void *)((char *)src_ob+offsetof(object, name)),
864  sizeof(object)-offsetof(object, name));
865 
866  if (is_freed)
867  SET_FLAG(dest_ob, FLAG_FREED);
868  if (is_removed)
869  SET_FLAG(dest_ob, FLAG_REMOVED);
870  if (dest_ob->artifact != NULL)
871  add_refcount(dest_ob->artifact);
872  if (dest_ob->name != NULL)
873  add_refcount(dest_ob->name);
874  if (dest_ob->name_pl != NULL)
875  add_refcount(dest_ob->name_pl);
876  if (dest_ob->anim_suffix != NULL)
877  add_refcount(dest_ob->anim_suffix);
878  if (dest_ob->title != NULL)
879  add_refcount(dest_ob->title);
880  if (dest_ob->race != NULL)
881  add_refcount(dest_ob->race);
882  if (dest_ob->slaying != NULL)
883  add_refcount(dest_ob->slaying);
884  if (dest_ob->skill != NULL)
885  add_refcount(dest_ob->skill);
886  if (dest_ob->lore != NULL)
887  add_refcount(dest_ob->lore);
888  if (dest_ob->msg != NULL)
889  add_refcount(dest_ob->msg);
890  if (dest_ob->custom_name != NULL)
891  add_refcount(dest_ob->custom_name);
892  if (dest_ob->materialname != NULL)
893  add_refcount(dest_ob->materialname);
894 
895  if (dest_ob->spell_tags != NULL) {
896  dest_ob->spell_tags = malloc(sizeof(tag_t)*SPELL_TAG_SIZE);
897  memcpy(dest_ob->spell_tags, src_ob->spell_tags, sizeof(tag_t)*SPELL_TAG_SIZE);
898  }
899 
900  /* If archetype is a temporary one, we need to update reference count, because
901  * that archetype will be freed by object_free_drop_inventory() when the last object is removed.
902  */
903  if (dest_ob->arch->reference_count > 0)
904  dest_ob->arch->reference_count++;
905 
906  if (src_ob->speed < 0)
907  dest_ob->speed_left = src_ob->speed_left-RANDOM()%200/100.0;
908 
909  /* Copy over key_values, if any. */
910  if (src_ob->key_values != NULL) {
911  key_value *tail = NULL;
912  key_value *i;
913 
914  dest_ob->key_values = NULL;
915 
916  for (i = src_ob->key_values; i != NULL; i = i->next) {
917  key_value *new_link = malloc(sizeof(key_value));
918 
919  new_link->next = NULL;
920  new_link->key = add_refcount(i->key);
921  if (i->value)
922  new_link->value = add_refcount(i->value);
923  else
924  new_link->value = NULL;
925 
926  /* Try and be clever here, too. */
927  if (dest_ob->key_values == NULL) {
928  dest_ob->key_values = new_link;
929  tail = new_link;
930  } else {
931  tail->next = new_link;
932  tail = new_link;
933  }
934  }
935  }
936 
937  /* This way, dialog information will be parsed again when/if needed. */
938  CLEAR_FLAG(dest_ob, FLAG_DIALOG_PARSED);
939 
940  object_update_speed(dest_ob);
941 }
942 
952 void object_copy_with_inv(const object *src_ob, object *dest_ob) {
953  object_copy(src_ob, dest_ob);
954  FOR_INV_PREPARE(src_ob, walk) {
955  object *tmp;
956 
957  tmp = object_new();
958  object_copy_with_inv(walk, tmp);
959  object_insert_in_ob(tmp, dest_ob);
960  } FOR_INV_FINISH();
961 }
962 
970 static void expand_objects(void) {
971  int i;
972  object *new;
973 
974  new = (object *)CALLOC(OBJ_EXPAND, sizeof(object));
975 
976  if (new == NULL)
978  free_objects = new;
979  new[0].prev = NULL;
980  new[0].next = &new[1],
981  SET_FLAG(&new[0], FLAG_REMOVED);
982  SET_FLAG(&new[0], FLAG_FREED);
983 
984  for (i = 1; i < OBJ_EXPAND-1; i++) {
985  new[i].next = &new[i+1],
986  new[i].prev = &new[i-1],
987  SET_FLAG(&new[i], FLAG_REMOVED);
988  SET_FLAG(&new[i], FLAG_FREED);
989  }
990  new[OBJ_EXPAND-1].prev = &new[OBJ_EXPAND-2],
991  new[OBJ_EXPAND-1].next = NULL,
992  SET_FLAG(&new[OBJ_EXPAND-1], FLAG_REMOVED);
993  SET_FLAG(&new[OBJ_EXPAND-1], FLAG_FREED);
994 
997 }
998 
1011 object *object_new(void) {
1012  object *op;
1013 #ifdef MEMORY_DEBUG
1014  /* FIXME: However this doesn't work since object_free() sometimes add
1015  * objects back to the free_objects linked list, and some functions mess
1016  * with the object after return of object_free(). This is bad and should be
1017  * fixed. But it would need fairly extensive changes and a lot of debugging.
1018  */
1019  op = calloc(1, sizeof(object));
1020  if (op == NULL)
1022 #else
1023  if (free_objects == NULL) {
1024  expand_objects();
1025  }
1026  op = free_objects;
1027  if (!QUERY_FLAG(op, FLAG_FREED)) {
1028  LOG(llevError, "Fatal: Getting busy object.\n");
1029 #ifdef MANY_CORES
1030  abort();
1031 #endif
1032  }
1033  free_objects = op->next;
1034  if (free_objects != NULL)
1035  free_objects->prev = NULL;
1036  nroffreeobjects--;
1037 #endif
1038  op->count = ++ob_count;
1039  op->name = NULL;
1040  op->name_pl = NULL;
1041  op->title = NULL;
1042  op->race = NULL;
1043  op->slaying = NULL;
1044  op->skill = NULL;
1045  op->lore = NULL;
1046  op->msg = NULL;
1047  op->materialname = NULL;
1048  op->next = objects;
1049  op->prev = NULL;
1050  op->active_next = NULL;
1051  op->active_prev = NULL;
1052  op->spell_tags = NULL;
1053  if (objects != NULL)
1054  objects->prev = op;
1055  objects = op;
1056  object_clear(op);
1057  SET_FLAG(op, FLAG_REMOVED);
1058  return op;
1059 }
1060 
1069 void object_update_turn_face(object *op) {
1070  if (op->animation == 0 || !QUERY_FLAG(op, FLAG_IS_TURNABLE))
1071  return;
1072  animate_object(op, op->direction);
1073 }
1074 
1086 void object_update_speed(object *op) {
1087  /* FIXME what the hell is this crappy hack?*/
1088  extern int arch_init;
1089 
1090  /* No reason putting the archetypes objects on the speed list,
1091  * since they never really need to be updated.
1092  */
1093 
1094  if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
1095  LOG(llevError, "Object %s is freed but has speed.\n", op->name);
1096 #ifdef MANY_CORES
1097  abort();
1098 #else
1099  op->speed = 0;
1100 #endif
1101  }
1102  if (arch_init) {
1103  return;
1104  }
1105  if (FABS(op->speed) > MIN_ACTIVE_SPEED) {
1106  /* If already on active list, don't do anything */
1107  /* TODO this check can probably be simplified a lot */
1108  if (op->active_next || op->active_prev || op == active_objects)
1109  return;
1110 
1111  /* process_events() expects us to insert the object at the beginning
1112  * of the list. */
1113  op->active_next = active_objects;
1114  if (op->active_next != NULL)
1115  op->active_next->active_prev = op;
1116  active_objects = op;
1117  } else {
1118  /* If not on the active list, nothing needs to be done */
1119  if (!op->active_next && !op->active_prev && op != active_objects)
1120  return;
1121 
1122  if (op->active_prev == NULL) {
1123  active_objects = op->active_next;
1124  if (op->active_next != NULL)
1125  op->active_next->active_prev = NULL;
1126  } else {
1127  op->active_prev->active_next = op->active_next;
1128  if (op->active_next)
1129  op->active_next->active_prev = op->active_prev;
1130  }
1131  op->active_next = NULL;
1132  op->active_prev = NULL;
1133  }
1134 }
1135 
1149  /* If not on the active list, nothing needs to be done */
1150  if (!op->active_next && !op->active_prev && op != active_objects)
1151  return;
1152 
1153  if (op->active_prev == NULL) {
1154  active_objects = op->active_next;
1155  if (op->active_next != NULL)
1156  op->active_next->active_prev = NULL;
1157  } else {
1158  op->active_prev->active_next = op->active_next;
1159  if (op->active_next)
1160  op->active_next->active_prev = op->active_prev;
1161  }
1162  op->active_next = NULL;
1163  op->active_prev = NULL;
1164 }
1165 
1190 void object_update(object *op, int action) {
1191  int update_now = 0, flags;
1192  MoveType move_on, move_off, move_block, move_slow;
1193  object *pl;
1194 
1195  if (op == NULL) {
1196  /* this should never happen */
1197  LOG(llevDebug, "object_update() called for NULL object.\n");
1198  return;
1199  }
1200 
1201  if (op->env != NULL) {
1202  /* Animation is currently handled by client, so nothing
1203  * to do in this case.
1204  */
1205  return;
1206  }
1207 
1208  /* If the map is saving, don't do anything as everything is
1209  * going to get freed anyways.
1210  */
1211  if (!op->map || op->map->in_memory == MAP_SAVING)
1212  return;
1213 
1214  /* make sure the object is within map boundaries */
1215  if (op->x < 0 || op->x >= MAP_WIDTH(op->map)
1216  || op->y < 0 || op->y >= MAP_HEIGHT(op->map)) {
1217  LOG(llevError, "object_update() called for object out of map!\n");
1218 #ifdef MANY_CORES
1219  abort();
1220 #endif
1221  return;
1222  }
1223 
1224  flags = GET_MAP_FLAGS(op->map, op->x, op->y);
1225  SET_MAP_FLAGS(op->map, op->x, op->y, flags|P_NEED_UPDATE);
1226  move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
1227  move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
1228  move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
1229  move_off = GET_MAP_MOVE_OFF(op->map, op->x, op->y);
1230 
1231  if (action == UP_OBJ_INSERT) {
1233  update_now = 1;
1234 
1235  if (QUERY_FLAG(op, FLAG_NO_MAGIC) && !(flags&P_NO_MAGIC))
1236  update_now = 1;
1237 
1238  if (QUERY_FLAG(op, FLAG_DAMNED) && !(flags&P_NO_CLERIC))
1239  update_now = 1;
1240 
1241  if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags&P_IS_ALIVE))
1242  update_now = 1;
1243 
1244  if ((move_on|op->move_on) != move_on)
1245  update_now = 1;
1246  if ((move_off|op->move_off) != move_off)
1247  update_now = 1;
1248  /* This isn't perfect, but I don't expect a lot of objects to
1249  * to have move_allow right now.
1250  */
1251  if (((move_block|op->move_block)&~op->move_allow) != move_block)
1252  update_now = 1;
1253  if ((move_slow|op->move_slow) != move_slow)
1254  update_now = 1;
1255 
1256  if (op->type == PLAYER)
1257  update_now = 1;
1258  /* if the object is being removed, we can't make intelligent
1259  * decisions, because object_remove() can't really pass the object
1260  * that is being removed.
1261  */
1262  } else if (action == UP_OBJ_REMOVE) {
1263  update_now = 1;
1264  } else if (action == UP_OBJ_FACE || action == UP_OBJ_CHANGE) {
1265  /* In addition to sending info to client, need to update space
1266  * information.
1267  */
1268  if (action == UP_OBJ_CHANGE)
1269  update_now = 1;
1270 
1271  /* There is a player on this space - we may need to send an
1272  * update to the client.
1273  * If this object is supposed to be animated by the client,
1274  * nothing to do here - let the client animate it.
1275  * We can't use FLAG_ANIMATE, as that is basically set for
1276  * all objects with multiple faces, regardless if they are animated.
1277  * (levers have it set for example).
1278  */
1279  if (flags&P_PLAYER
1282  pl = GET_MAP_PLAYER(op->map, op->x, op->y);
1283 
1284  /* If update_look is set, we're going to send this entire space
1285  * to the client, so no reason to send face information now.
1286  */
1287  if (!pl->contr->socket.update_look) {
1288  esrv_update_item(UPD_FACE, pl, op);
1289  }
1290  }
1291  } else {
1292  LOG(llevError, "object_update called with invalid action: %d\n", action);
1293  }
1294 
1295  if (update_now) {
1296  SET_MAP_FLAGS(op->map, op->x, op->y, flags|P_NO_ERROR|P_NEED_UPDATE);
1297  update_position(op->map, op->x, op->y);
1298  }
1299 
1300  if (op->more != NULL)
1301  object_update(op->more, action);
1302 }
1303 
1316 void object_free_drop_inventory(object *ob) {
1317  object_free(ob, 0);
1318 }
1319 
1324 void object_free_inventory(object *ob) {
1325  while (ob->inv) {
1326  object *inv = ob->inv;
1327  object_remove(inv);
1328  object_free(inv, FREE_OBJ_NO_DESTROY_CALLBACK | FREE_OBJ_FREE_INVENTORY);
1329  }
1330 }
1331 
1348 void object_free(object *ob, int flags) {
1349  if (!QUERY_FLAG(ob, FLAG_REMOVED)) {
1350  StringBuffer *sb;
1351  char *diff;
1352 
1353  LOG(llevDebug, "Free object called with non removed object\n");
1354  sb = stringbuffer_new();
1355  object_dump(ob, sb);
1356  diff = stringbuffer_finish(sb);
1357  LOG(llevError, "%s", diff);
1358  free(diff);
1359 #ifdef MANY_CORES
1360  abort();
1361 #endif
1362  }
1363  if (QUERY_FLAG(ob, FLAG_FRIENDLY)) {
1364  LOG(llevMonster, "Warning: tried to free friendly object.\n");
1366  }
1367  if (QUERY_FLAG(ob, FLAG_FREED)) {
1368  StringBuffer *sb;
1369  char *diff;
1370 
1371  sb = stringbuffer_new();
1372  object_dump(ob, sb);
1373  diff = stringbuffer_finish(sb);
1374  LOG(llevError, "Trying to free freed object.\n%s\n", diff);
1375  free(diff);
1376  return;
1377  }
1378 
1379  if ((flags & FREE_OBJ_NO_DESTROY_CALLBACK) == 0) {
1381  }
1382 
1383  if (ob->inv) {
1384  /* Only if the space blocks everything do we not process -
1385  * if some form of movemnt is allowed, let objects
1386  * drop on that space.
1387  */
1388  if ((flags & FREE_OBJ_FREE_INVENTORY) != 0
1389  || ob->map == NULL
1390  || ob->map->in_memory != MAP_IN_MEMORY
1391  || (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) {
1392  FOR_INV_PREPARE(ob, op) {
1393  object_remove(op);
1394  object_free(op, flags);
1395  } FOR_INV_FINISH();
1396  } else { /* Put objects in inventory onto this space */
1397  FOR_INV_PREPARE(ob, op) {
1398  object_remove(op);
1399  /* No drop means no drop, including its inventory */
1400  if (QUERY_FLAG(op, FLAG_NO_DROP))
1401  object_free(op, FREE_OBJ_FREE_INVENTORY);
1402  else if (QUERY_FLAG(op, FLAG_STARTEQUIP)
1403  || QUERY_FLAG(op, FLAG_NO_DROP)
1404  || op->type == RUNE
1405  || op->type == TRAP
1408  else {
1409  object *part;
1410 
1411  /* If it's a multi-tile object, scatter dropped items randomly */
1412  if (ob->more) {
1413  int partcount = 0;
1414  /* Get the number of non-head parts */
1415  for (part = ob; part; part = part->more) {
1416  partcount++;
1417  }
1418  /* Select a random part */
1419  partcount = RANDOM()%partcount;
1420  for (part = ob; partcount > 0; partcount--) {
1421  part = part->more;
1422  }
1423  } else {
1424  part = ob;
1425  }
1426 
1427  if (QUERY_FLAG(op, FLAG_ALIVE)) {
1428  object_insert_to_free_spot_or_free(op, part->map, part->x, part->y, 0, SIZEOFFREE, NULL);
1429  } else {
1430  int f = 0;
1431  if (flags & FREE_OBJ_DROP_ABOVE_FLOOR)
1432  f = INS_ABOVE_FLOOR_ONLY;
1433  object_insert_in_map_at(op, part->map, NULL, f, part->x, part->y); /* Insert in same map as the envir */
1434  }
1435  }
1436  } FOR_INV_FINISH();
1437  }
1438  }
1439 
1440  if (ob->more != NULL) {
1441  object_free(ob->more, flags);
1442  ob->more = NULL;
1443  }
1444 
1445  /* Remove object from the active list */
1446  ob->speed = 0;
1447  object_update_speed(ob);
1448 
1449  SET_FLAG(ob, FLAG_FREED);
1450  ob->count = 0;
1451 
1452  /* Remove this object from the list of used objects */
1453  if (ob->prev == NULL) {
1454  objects = ob->next;
1455  if (objects != NULL)
1456  objects->prev = NULL;
1457  } else {
1458  ob->prev->next = ob->next;
1459  if (ob->next != NULL)
1460  ob->next->prev = ob->prev;
1461  }
1462 
1463  if (ob->artifact != NULL) FREE_AND_CLEAR_STR(ob->artifact);
1464  if (ob->name != NULL) FREE_AND_CLEAR_STR(ob->name);
1465  if (ob->name_pl != NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1466  if (ob->title != NULL) FREE_AND_CLEAR_STR(ob->title);
1467  if (ob->race != NULL) FREE_AND_CLEAR_STR(ob->race);
1468  if (ob->slaying != NULL) FREE_AND_CLEAR_STR(ob->slaying);
1469  if (ob->skill != NULL) FREE_AND_CLEAR_STR(ob->skill);
1470  if (ob->lore != NULL) FREE_AND_CLEAR_STR(ob->lore);
1471  if (ob->msg != NULL) FREE_AND_CLEAR_STR(ob->msg);
1472  if (ob->materialname != NULL) FREE_AND_CLEAR_STR(ob->materialname);
1473  if (ob->spell_tags) FREE_AND_CLEAR(ob->spell_tags);
1474 
1475  /* Why aren't events freed? */
1477 
1479 
1480  /* Test whether archetype is a temporary one, and if so look whether it should be trashed. */
1481  if (ob->arch && ob->arch->reference_count > 0) {
1482  if (--ob->arch->reference_count == 0) {
1483  free_arch(ob->arch);
1484  }
1485  }
1486 
1487 #ifdef MEMORY_DEBUG
1488  free(ob);
1489 #else
1490  /* Now link it with the free_objects list: */
1491  ob->prev = NULL;
1492  ob->next = free_objects;
1493  if (free_objects != NULL)
1494  free_objects->prev = ob;
1495  free_objects = ob;
1496  nroffreeobjects++;
1497 #endif
1498 }
1499 
1507  int i = 0;
1508  object *tmp = free_objects;
1509 
1510  while (tmp != NULL)
1511  tmp = tmp->next,
1512  i++;
1513  return i;
1514 }
1515 
1523  int i = 0;
1524  object *tmp = objects;
1525 
1526  while (tmp != NULL)
1527  tmp = tmp->next,
1528  i++;
1529  return i;
1530 }
1531 
1539  int i = 0;
1540  object *tmp = active_objects;
1541 
1542  while (tmp != NULL)
1543  tmp = tmp->active_next,
1544  i++;
1545  return i;
1546 }
1547 
1562 void object_sub_weight(object *op, signed long weight) {
1563  while (op != NULL) {
1564  if (op->type == CONTAINER) {
1565  weight = (signed long)(weight*(100-op->stats.Str)/100);
1566  }
1567  op->carrying -= weight;
1568  op = op->env;
1569  }
1570 }
1571 
1588 void object_remove(object *op) {
1589  object *last = NULL;
1590  object *otmp;
1591  tag_t tag;
1592  int check_walk_off;
1593  mapstruct *m;
1594  int16_t x, y;
1595 
1596  if (QUERY_FLAG(op, FLAG_REMOVED)) {
1597  StringBuffer *sb;
1598  char *diff;
1599 
1600  sb = stringbuffer_new();
1601  object_dump(op, sb);
1602  diff = stringbuffer_finish(sb);
1603  LOG(llevError, "Trying to remove removed object.\n%s\n", diff);
1604  free(diff);
1605  abort();
1606  }
1607  if (op->more != NULL)
1608  object_remove(op->more);
1609 
1610  SET_FLAG(op, FLAG_REMOVED);
1611 
1612  /*
1613  * In this case, the object to be removed is in someones
1614  * inventory.
1615  */
1616  /* TODO try to call a generic inventory weight adjusting function like object_sub_weight */
1617  if (op->env != NULL) {
1618  player *pl = NULL;
1619 
1620  if (op->nrof)
1621  object_sub_weight(op->env, op->weight*op->nrof);
1622  else
1623  object_sub_weight(op->env, op->weight+op->carrying);
1624 
1625  /* Update in two cases: item is in a player, or in a container the player is looking into. */
1626  if (op->env->contr != NULL && op->head == NULL) {
1627  pl = op->env->contr;
1628  } else if (op->env->type == CONTAINER && QUERY_FLAG(op->env, FLAG_APPLIED)) {
1629 
1630  if (op->env->env && op->env->env->contr)
1631  /* Container is in player's inventory. */
1632  pl = op->env->env->contr;
1633  else if (op->env->map) {
1634  /* Container on map, look above for player. */
1635  object *above = op->env->above;
1636 
1637  while (above && !above->contr)
1638  above = above->above;
1639  if (above)
1640  pl = above->contr;
1641  }
1642  }
1643 
1644  /* NO_FIX_PLAYER is set when a great many changes are being
1645  * made to players inventory. If set, avoiding the call
1646  * to save cpu time.
1647  */
1648  otmp = object_get_player_container(op->env);
1649  if (otmp != NULL
1650  && otmp->contr
1651  && !QUERY_FLAG(otmp, FLAG_NO_FIX_PLAYER))
1652  fix_object(otmp);
1653 
1654  if (op->above != NULL)
1655  op->above->below = op->below;
1656  else
1657  op->env->inv = op->below;
1658 
1659  if (op->below != NULL)
1660  op->below->above = op->above;
1661 
1662  /* we set up values so that it could be inserted into
1663  * the map, but we don't actually do that - it is up
1664  * to the caller to decide what we want to do.
1665  */
1666  op->x = op->env->x;
1667  op->y = op->env->y;
1668  op->ox = op->x;
1669  op->oy = op->y;
1670  op->map = op->env->map;
1671  op->above = NULL;
1672  op->below = NULL;
1673  /* send the delitem before resetting env, so container's contents be may
1674  * refreshed */
1675  if (LOOK_OBJ(op) && pl != NULL)
1676  esrv_del_item(pl, op);
1677  op->env = NULL;
1678  return;
1679  }
1680 
1681  /* If we get here, we are removing it from a map */
1682  if (op->map == NULL)
1683  return;
1684 
1685  if (op->contr != NULL && !op->contr->hidden)
1686  op->map->players--;
1687 
1688  x = op->x;
1689  y = op->y;
1690  m = get_map_from_coord(op->map, &x, &y);
1691 
1692  if (!m) {
1693  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);
1694  abort();
1695  }
1696  if (op->map != m) {
1697  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);
1698  }
1699 
1700  /* link the object above us */
1701  if (op->above)
1702  op->above->below = op->below;
1703  else
1704  SET_MAP_TOP(m, x, y, op->below); /* we were top, set new top */
1705 
1706  /* Relink the object below us, if there is one */
1707  if (op->below) {
1708  op->below->above = op->above;
1709  } else {
1710  /* Nothing below, which means we need to relink map object for this space
1711  * use translated coordinates in case some oddness with map tiling is
1712  * evident
1713  */
1714  /*TODO is this check really needed?*/
1715  if (GET_MAP_OB(m, x, y) != op) {
1716  StringBuffer *sb;
1717  char *diff;
1718 
1719  sb = stringbuffer_new();
1720  object_dump(op, sb);
1721  diff = stringbuffer_finish(sb);
1722  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);
1723  free(diff);
1724 
1725  sb = stringbuffer_new();
1726  object_dump(GET_MAP_OB(m, x, y), sb);
1727  diff = stringbuffer_finish(sb);
1728  LOG(llevError, "%s\n", diff);
1729  free(diff);
1730  }
1731  SET_MAP_OB(m, x, y, op->above); /* goes on above it. */
1732  }
1733  op->above = NULL;
1734  op->below = NULL;
1735 
1736  if (op->map->in_memory == MAP_SAVING)
1737  return;
1738 
1739  tag = op->count;
1740  check_walk_off = !QUERY_FLAG(op, FLAG_NO_APPLY);
1741  FOR_MAP_PREPARE(m, x, y, tmp) {
1742  /* No point updating the players look faces if he is the object
1743  * being removed.
1744  */
1745 
1746  if (tmp->type == PLAYER && tmp != op) {
1747  /* If a container that the player is currently using somehow gets
1748  * removed (most likely destroyed), update the player view
1749  * appropriately.
1750  */
1751  if (tmp->container == op) {
1752  CLEAR_FLAG(op, FLAG_APPLIED);
1753  tmp->container = NULL;
1754  }
1755  tmp->contr->socket.update_look = 1;
1756  }
1757  /* See if player moving off should effect something */
1758  if (check_walk_off
1759  && ((op->move_type&tmp->move_off) && (op->move_type&~tmp->move_off&~tmp->move_block) == 0)) {
1760  ob_move_on(tmp, op, NULL);
1761  if (object_was_destroyed(op, tag)) {
1762  LOG(llevError, "BUG: object_remove(): name %s, archname %s destroyed leaving object\n", tmp->name, tmp->arch->name);
1763  }
1764  }
1765 
1766  /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1767  if (tmp->above == tmp)
1768  tmp->above = NULL;
1769  last = tmp;
1770  } FOR_MAP_FINISH();
1771  /* last == NULL or there are no objects on this space */
1772  if (last == NULL) {
1773  /* set P_NEED_UPDATE, otherwise update_position will complain. In theory,
1774  * we could preserve the flags (GET_MAP_FLAGS), but update_position figures
1775  * those out anyways, and if there are any flags set right now, they won't
1776  * be correct anyways.
1777  */
1778  SET_MAP_FLAGS(op->map, op->x, op->y, P_NEED_UPDATE);
1779  update_position(op->map, op->x, op->y);
1780  } else
1781  object_update(last, UP_OBJ_REMOVE);
1782 
1783  if (QUERY_FLAG(op, FLAG_BLOCKSVIEW) || (op->glow_radius != 0))
1784  update_all_los(op->map, op->x, op->y);
1785 }
1786 
1800 object *object_merge(object *op, object *top) {
1801  if (!op->nrof)
1802  return NULL;
1803 
1804  if (top == NULL)
1805  for (top = op; top != NULL && top->above != NULL; top = top->above)
1806  ;
1808  if (top == op)
1809  continue;
1810  if (object_can_merge(op, top)) {
1811  object_increase_nrof(top, op->nrof);
1812  /*
1813  * Previous behavior set weight to zero here.
1814  * This, however, caused the object_sub_weight
1815  * call in object_remove to subtract zero weight
1816  * when removing the object. Thus, until inventory
1817  * weight is next recalculated, the object merged
1818  * into another pile added weight in object_increase_nrof
1819  * but did not remove the weight from the original
1820  * instance of itself in object_remove, essentially
1821  * counting for double weight for several minutes.
1822  *
1823  * SilverNexus 2014-05-27
1824  */
1825  object_remove(op);
1826  object_free(op, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK);
1827  return top;
1828  }
1830  return NULL;
1831 }
1832 
1849 object *object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y) {
1850  object *tmp;
1851 
1852  op = HEAD(op);
1853  for (tmp = op; tmp; tmp = tmp->more) {
1854  tmp->x = x+tmp->arch->clone.x;
1855  tmp->y = y+tmp->arch->clone.y;
1856  tmp->map = m;
1857  }
1858  return object_insert_in_map(op, m, originator, flag);
1859 }
1860 
1878 void object_merge_spell(object *op, int16_t x, int16_t y) {
1879  int i;
1880 
1881  /* We try to do some merging of spell objects - if something has same owner,
1882  * is same type of spell, and going in the same direction, it is somewhat
1883  * mergable.
1884  *
1885  * If the spell object has an other_arch, don't merge - when the spell
1886  * does something, like explodes, it will use this other_arch, and
1887  * if we merge, there is no easy way to make the correct values be
1888  * set on this new object (values should be doubled, tripled, etc.)
1889  *
1890  * We also care about speed - only process objects that will not be
1891  * active this tick. Without this, the results are incorrect - think
1892  * of a case where tmp would normally get processed this tick, but
1893  * get merges with op, which does not get processed.
1894  */
1895  FOR_MAP_PREPARE(op->map, x, y, tmp) {
1896  if (op->type == tmp->type
1897  && op->subtype == tmp->subtype
1898  && op->direction == tmp->direction
1899  && op->owner == tmp->owner && op->ownercount == tmp->ownercount
1900  && op->range == tmp->range
1901  && op->stats.wc == tmp->stats.wc
1902  && op->level == tmp->level
1903  && op->attacktype == tmp->attacktype
1904  && op->speed == tmp->speed
1905  && !tmp->other_arch
1906  && (tmp->speed_left+tmp->speed) < 0.0
1907  && op != tmp) {
1908  /* Quick test - if one or the other objects already have hash tables
1909  * set up, and that hash bucket contains a value that doesn't
1910  * match what we want to set it up, we won't be able to merge.
1911  * Note that these two if statements are the same, except
1912  * for which object they are checking against. They could
1913  * be merged, but the line wrapping would be large enough
1914  * that IMO it would become difficult to read the different clauses
1915  * so its cleaner just to do 2 statements - MSW
1916  */
1917  if (op->spell_tags
1918  && !OB_SPELL_TAG_MATCH(op, (tag_t)tmp->stats.maxhp)
1919  && OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) != 0)
1920  continue;
1921 
1922  if (tmp->spell_tags
1923  && !OB_SPELL_TAG_MATCH(tmp, (tag_t)op->stats.maxhp)
1924  && OB_SPELL_TAG_HASH(tmp, op->stats.maxhp) != 0)
1925  continue;
1926 
1927  /* If we merge, the data from tmp->spell_tags gets copied into op.
1928  * so we need to make sure that slot isn't filled up.
1929  */
1930  if (tmp->spell_tags
1931  && !OB_SPELL_TAG_MATCH(tmp, (tag_t)tmp->stats.maxhp)
1932  && OB_SPELL_TAG_HASH(tmp, tmp->stats.maxhp) != 0)
1933  continue;
1934 
1935  /* If both objects have spell_tags, we need to see if there are conflicting
1936  * values - if there are, we won't be able to merge then.
1937  */
1938  if (tmp->spell_tags && op->spell_tags) {
1939  int need_copy = 0;
1940 
1941  for (i = 0; i < SPELL_TAG_SIZE; i++) {
1942  /* If the two tag values in the hash are set, but are
1943  * not set to the same value, then these objects
1944  * can not be merged.
1945  */
1946  if (op->spell_tags[i] && tmp->spell_tags[i]
1947  && op->spell_tags[i] != tmp->spell_tags[i]) {
1949  break;
1950  }
1951  /* If one tag is set and the other is not, that is
1952  * fine, but we have to note that we need to copy
1953  * the data in that case.
1954  */
1955  if ((!op->spell_tags[i] && tmp->spell_tags[i])
1956  || (op->spell_tags[i] && !tmp->spell_tags[i])) {
1957  need_copy = 1;
1958  }
1959  }
1960  /* If we did not get through entire array, it means
1961  * we got a conflicting hash, and so we won't be
1962  * able to merge these - just continue processing
1963  * object on this space.
1964  */
1965  if (i <= SPELL_TAG_SIZE)
1966  continue;
1967 
1968  /* Ok - everything checked out - we should be able to
1969  * merge tmp in op. So lets copy the tag data if
1970  * needed. Note that this is a selective copy, as
1971  * we don't want to clear values that may be set in op.
1972  */
1973  if (need_copy) {
1974  for (i = 0; i < SPELL_TAG_SIZE; i++)
1975  if (!op->spell_tags[i]
1976  && tmp->spell_tags[i]
1977  && tmp->spell_tags[i] != (tag_t)op->stats.maxhp)
1978  op->spell_tags[i] = tmp->spell_tags[i];
1979  }
1980  FREE_AND_CLEAR(tmp->spell_tags);
1981  }
1982 
1983  /* if tmp has a spell_tags table, copy it to op and free tmps */
1984  if (tmp->spell_tags && !op->spell_tags) {
1985  op->spell_tags = tmp->spell_tags;
1986  tmp->spell_tags = NULL;
1987 
1988  /* We don't need to keep a copy of our maxhp value
1989  * in the copied over value
1990  */
1991  if (OB_SPELL_TAG_MATCH(op, (tag_t)op->stats.maxhp))
1992  OB_SPELL_TAG_HASH(op, op->stats.maxhp) = 0;
1993  }
1994 
1995  /* For spells to work correctly, we need to record what spell
1996  * tags we've merged in with this effect. This is used
1997  * in ok_to_put_more() to see if a spell effect is already on
1998  * the space.
1999  */
2000  if (op->stats.maxhp != tmp->stats.maxhp) {
2001 #ifdef OBJECT_DEBUG
2002  /* This if statement should never happen - the logic above should
2003  * have prevented it. It is a problem, because by now its possible
2004  * we've destroyed the spell_tags in tmp, so we can't really
2005  * just bail out.
2006  */
2007 
2008  if (op->spell_tags
2009  && OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) != 0
2010  && !OB_SPELL_TAG_MATCH(op, tmp->stats.maxhp)) {
2011  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);
2012  }
2013 #endif
2014  if (!op->spell_tags)
2015  op->spell_tags = calloc(SPELL_TAG_SIZE, sizeof(tag_t));
2016 
2017  OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) = tmp->stats.maxhp;
2018  }
2019 
2021  op->speed_left = MAX(op->speed_left, tmp->speed_left);
2022 
2023  if (tmp->duration != op->duration) {
2024  /* We need to use tmp_dam here because otherwise the
2025  * calculations can overflow the size of stats.dam.
2026  */
2027  int tmp_dam = tmp->stats.dam*(tmp->duration+1)+
2028  op->stats.dam*(op->duration+1);
2029 
2030  op->duration = MAX(op->duration, tmp->duration);
2031  tmp_dam /= op->duration+1;
2032  op->stats.dam = tmp_dam+1;
2033  } else {
2034  /* in this case, duration is the same, so simply adding
2035  * up damage works.
2036  */
2037  op->stats.dam += tmp->stats.dam;
2038  }
2039 
2040  object_remove(tmp);
2042  }
2043  } FOR_MAP_FINISH();
2044 }
2045 
2046 static object *find_insert_pos(object *op, const int flag) {
2047  object *floor = NULL;
2048  /*
2049  * If there are multiple objects on this space, we do some trickier handling.
2050  * We've already dealt with merging if appropriate.
2051  * Generally, we want to put the new object on top. But if
2052  * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last
2053  * floor, we want to insert above that and no further.
2054  * Also, if there are spell objects on this space, we stop processing
2055  * once we get to them. This reduces the need to traverse over all of
2056  * them when adding another one - this saves quite a bit of cpu time
2057  * when lots of spells are cast in one area. Currently, it is presumed
2058  * that flying non pickable objects are spell objects.
2059  */
2060  if (flag&INS_ON_TOP) {
2061  return GET_MAP_TOP(op->map, op->x, op->y);
2062  }
2063  object *last = NULL;
2064  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
2065  if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)
2066  || QUERY_FLAG(tmp, FLAG_OVERLAY_FLOOR))
2067  floor = tmp;
2068 
2069  if (QUERY_FLAG(tmp, FLAG_NO_PICK)
2070  && (tmp->move_type&(MOVE_FLY_LOW|MOVE_FLY_HIGH))
2071  && !QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2072  /* We insert above tmp, so we want this object below this */
2073  break;
2074  }
2075  last = tmp;
2076  } FOR_MAP_FINISH();
2077  if (flag&INS_ABOVE_FLOOR_ONLY)
2078  return floor;
2079  return last;
2080 }
2081 
2110 object *object_insert_in_map(object *op, mapstruct *m, object *originator, int flag) {
2111  object *tmp, *top, *floor = NULL;
2112  int16_t x, y;
2113 
2114  if (QUERY_FLAG(op, FLAG_FREED)) {
2115  LOG(llevError, "Trying to insert freed object!\n");
2116  return NULL;
2117  }
2118  if (m == NULL) {
2119  StringBuffer *sb;
2120  char *diff;
2121 
2122  sb = stringbuffer_new();
2123  object_dump(op, sb);
2124  diff = stringbuffer_finish(sb);
2125  LOG(llevError, "Trying to insert in null-map!\n%s\n", diff);
2126  free(diff);
2127  return op;
2128  }
2129  if (out_of_map(m, op->x, op->y)) {
2130  StringBuffer *sb;
2131  char *diff;
2132 
2133  sb = stringbuffer_new();
2134  object_dump(op, sb);
2135  diff = stringbuffer_finish(sb);
2136  LOG(llevError, "Trying to insert object outside the map.\n%s\n", diff);
2137  free(diff);
2138 #ifdef MANY_CORES
2139  /* Better to catch this here, as otherwise the next use of this object
2140  * is likely to cause a crash. Better to find out where it is getting
2141  * improperly inserted.
2142  */
2143  abort();
2144 #endif
2145  return op;
2146  }
2147  if (!QUERY_FLAG(op, FLAG_REMOVED)) {
2148  StringBuffer *sb;
2149  char *diff;
2150 
2151  sb = stringbuffer_new();
2152  object_dump(op, sb);
2153  diff = stringbuffer_finish(sb);
2154  LOG(llevError, "Trying to insert (map) inserted object.\n%s\n", diff);
2155  free(diff);
2156  return op;
2157  }
2158  if (op->more != NULL) {
2159  /* The part may be on a different map. */
2160 
2161  object *more = op->more;
2162 
2163  /* We really need the caller to normalize coordinates - if
2164  * we set the map, that doesn't work if the location is within
2165  * a map and this is straddling an edge. So only if coordinate
2166  * is clear wrong do we normalize it.
2167  */
2168  if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
2169  /* Debugging information so you can see the last coordinates this object had */
2170  more->ox = more->x;
2171  more->oy = more->y;
2172  more->map = get_map_from_coord(more->map, &more->x, &more->y);
2173  } else if (!more->map) {
2174  /* For backwards compatibility - when not dealing with tiled maps,
2175  * more->map should always point to the parent.
2176  */
2177  more->map = m;
2178  }
2179 
2180  if (object_insert_in_map(more, more->map, originator, flag) == NULL) {
2181  if (!op->head)
2182  LOG(llevError, "BUG: object_insert_in_map(): inserting op->more killed op\n");
2183  return NULL;
2184  }
2185  }
2186  CLEAR_FLAG(op, FLAG_REMOVED);
2187 
2188  /* Debugging information so you can see the last coordinates this object had */
2189  op->ox = op->x;
2190  op->oy = op->y;
2191  x = op->x;
2192  y = op->y;
2193  op->map = get_map_from_coord(m, &x, &y);
2194 
2195  /* this has to be done after we translate the coordinates. */
2196  if (op->nrof
2197  && !(flag&INS_NO_MERGE)
2198  && op->type != SPELL_EFFECT) {
2199  FOR_MAP_PREPARE(op->map, x, y, tmp) {
2200  if (object_can_merge(op, tmp)) {
2201  op->nrof += tmp->nrof;
2202  object_remove(tmp);
2203  object_free(tmp, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK);
2204  }
2205  } FOR_MAP_FINISH();
2206  } else if (op->type == SPELL_EFFECT
2207  && !op->range
2208  && !op->other_arch
2209  && (op->speed_left+op->speed) < 0.0) {
2210  object_merge_spell(op, x, y);
2211  }
2212 
2213  /* Ideally, the caller figures this out. However, it complicates a lot
2214  * of areas of callers (eg, anything that uses object_find_free_spot() would now
2215  * need extra work
2216  */
2217  if (op->map != m) {
2218  /* coordinates should not change unless map also changes */
2219  op->x = x;
2220  op->y = y;
2221  }
2222 
2223  if (op->type != LAMP)
2224  /* lamps use the FLAG_APPLIED to keep the light/unlit status, so don't reset it.
2225  Other objects just get unapplied, since the container "drops" them. */
2226  CLEAR_FLAG(op, FLAG_APPLIED);
2228  if (!QUERY_FLAG(op, FLAG_ALIVE))
2230 
2231  /* In many places, a player is passed as the originator, which
2232  * is fine. However, if the player is on a transport, they are not
2233  * actually on the map, so we can't use them for the linked pointers,
2234  * nor should the walk on function below use them either.
2235  */
2236  if (originator && originator->contr && originator->contr->transport)
2237  originator = originator->contr->transport;
2238 
2239  if (flag&INS_BELOW_ORIGINATOR) {
2240  if (originator->map != op->map
2241  || originator->x != op->x
2242  || originator->y != op->y) {
2243  LOG(llevError, "object_insert_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n");
2244  abort();
2245  }
2246  op->above = originator;
2247  op->below = originator->below;
2248  if (op->below)
2249  op->below->above = op;
2250  else
2251  SET_MAP_OB(op->map, op->x, op->y, op);
2252  /* since *below *originator, no need to update top */
2253  originator->below = op;
2254  } else {
2255  /* Top is the object that our object (op) is going to get inserted above. */
2256  top = find_insert_pos(op, flag);
2257 
2258  /* First object on this space */
2259  if (!top) {
2260  op->above = GET_MAP_OB(op->map, op->x, op->y);
2261  if (op->above)
2262  op->above->below = op;
2263  op->below = NULL;
2264  SET_MAP_OB(op->map, op->x, op->y, op);
2265  } else { /* get inserted into the stack above top */
2266  op->above = top->above;
2267  if (op->above)
2268  op->above->below = op;
2269  op->below = top;
2270  top->above = op;
2271  }
2272  if (op->above == NULL)
2273  SET_MAP_TOP(op->map, op->x, op->y, op);
2274  } /* else not INS_BELOW_ORIGINATOR */
2275 
2276  if (!(flag&INS_MAP_LOAD)) {
2277  if (op->type == PLAYER)
2278  op->contr->do_los = 1;
2279 
2280  /* If we have a floor, we know the player, if any, will be above
2281  * it, so save a few ticks and start from there.
2282  */
2283  tmp = floor ? floor : GET_MAP_OB(op->map, op->x, op->y);
2285  if (tmp->type == PLAYER)
2286  tmp->contr->socket.update_look = 1;
2288 
2289  /* If this object glows, it may affect lighting conditions that are
2290  * visible to others on this map. But update_all_los is really
2291  * an inefficient way to do this, as it means los for all players
2292  * on the map will get recalculated. The players could very well
2293  * be far away from this change and not affected in any way -
2294  * this should get redone to only look for players within range,
2295  * or just updating the P_NEED_UPDATE for spaces within this area
2296  * of effect may be sufficient.
2297  */
2298  if (MAP_DARKNESS(op->map) && (op->glow_radius != 0))
2299  update_all_los(op->map, op->x, op->y);
2300 
2301  if (op->contr && !op->contr->hidden)
2302  op->map->players++;
2303  }
2304 
2305  /* updates flags (blocked, alive, no magic, etc) for this map space */
2306  object_update(op, UP_OBJ_INSERT);
2307 
2308  /* Don't know if moving this to the end will break anything. However,
2309  * we want to have update_look set above before calling this.
2310  *
2311  * object_check_move_on() must be after this because code called from
2312  * object_check_move_on() depends on correct map flags (so functions like
2313  * blocked() and wall() work properly), and these flags are updated by
2314  * object_update().
2315  */
2316 
2317  /* if this is not the head or flag has been passed, don't check walk on status */
2318 
2319  if (!(flag&INS_NO_WALK_ON) && !op->head) {
2320  if (object_check_move_on(op, originator))
2321  return NULL;
2322 
2323  /* If we are a multi part object, lets work our way through the check
2324  * walk on's.
2325  */
2326  for (tmp = op->more; tmp != NULL; tmp = tmp->more)
2327  if (object_check_move_on(tmp, originator))
2328  return NULL;
2329  }
2330  return op;
2331 }
2332 
2342 void object_replace_insert_in_map(const char *arch_string, object *op) {
2343  object *tmp1;
2344  archetype *at;
2345 
2346  /* first search for itself and remove any old instances */
2347  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
2348  if (!strcmp(tmp->arch->name, arch_string)) { /* same archetype */
2349  object_remove(tmp);
2351  }
2352  } FOR_MAP_FINISH();
2353 
2354  at = find_archetype(arch_string);
2355  if (at == NULL) {
2356  return;
2357  }
2358  tmp1 = arch_to_object(at);
2359  object_insert_in_map_at(tmp1, op->map, op, INS_BELOW_ORIGINATOR, op->x, op->y);
2360 }
2361 
2382 object *object_split(object *orig_ob, uint32_t nr, char *err, size_t size) {
2383  object *newob;
2384 
2385  if (MAX(1, orig_ob->nrof) < nr) {
2386  /* If err is set, the caller knows that nr can be wrong (player trying to drop items), thus don't log that. */
2387  if (err)
2388  snprintf(err, size, "There are only %u %ss.", NROF(orig_ob), orig_ob->name);
2389  else
2390  LOG(llevDebug, "There are only %u %ss.\n", NROF(orig_ob), orig_ob->name);
2391  return NULL;
2392  }
2393  newob = object_create_clone(orig_ob);
2394  if (orig_ob->nrof == 0) {
2395  if (!QUERY_FLAG(orig_ob, FLAG_REMOVED)) {
2396  object_remove(orig_ob);
2397  }
2398  object_free(orig_ob, FREE_OBJ_FREE_INVENTORY);
2399  } else {
2400  newob->nrof = nr;
2401  object_decrease_nrof(orig_ob, nr);
2402  }
2403 
2404  return newob;
2405 }
2406 
2421 object *object_decrease_nrof(object *op, uint32_t i) {
2422  object *tmp;
2423 
2424  if (i == 0) /* objects with op->nrof require this check */
2425  return op;
2426 
2427  if (i > op->nrof)
2428  i = op->nrof;
2429 
2430  if (QUERY_FLAG(op, FLAG_REMOVED)) {
2431  op->nrof -= i;
2432  } else if (op->env != NULL) {
2433  if (i < op->nrof) {
2434  player *pl;
2435  /* is this object in the players inventory, or sub container
2436  * therein?
2437  */
2438  tmp = object_get_player_container(op->env);
2439  /* nope. Is this a container the player has opened?
2440  * If so, set tmp to that player.
2441  * IMO, searching through all the players will mostly
2442  * likely be quicker than following op->env to the map,
2443  * and then searching the map for a player.
2444  */
2445  if (!tmp) {
2446  for (pl = first_player; pl; pl = pl->next)
2447  if (pl->ob->container == op->env)
2448  break;
2449  if (pl)
2450  tmp = pl->ob;
2451  else
2452  tmp = NULL;
2453  }
2454 
2455  /* Because of weight reduction by container and integer arithmetic,
2456  * there is no guarantee the rounded weight of combined items will be
2457  * the same as the sum of rounded weights.
2458  * Therefore just remove the current weight, and add the new.
2459  * Same adjustment done in object_increase_nrof().
2460  */
2461  object_sub_weight(op->env, op->weight * op->nrof);
2462  op->nrof -= i;
2463  object_add_weight(op->env, op->weight * op->nrof);
2464  if (tmp) {
2465  esrv_update_item(UPD_NROF, tmp, op);
2466  esrv_update_item(UPD_WEIGHT, tmp, op->env);
2467  fix_object(tmp);
2468  }
2469  } else {
2470  object_remove(op);
2471  op->nrof = 0;
2472  }
2473  } else {
2474  /* On a map. */
2475  if (i < op->nrof) {
2476  op->nrof -= i;
2477 
2478  FOR_MAP_PREPARE(op->map, op->x, op->y, pl)
2479  if (pl->contr) {
2480  pl->contr->socket.update_look = 1;
2481  break;
2482  }
2483  FOR_MAP_FINISH();
2484  } else {
2485  object_remove(op);
2486  op->nrof = 0;
2487  }
2488  }
2489 
2490  if (op->nrof) {
2491  return op;
2492  } else {
2494  return NULL;
2495  }
2496 }
2497 
2508 static void object_increase_nrof(object *op, uint32_t i) {
2509  object *tmp;
2510 
2511  if (i == 0) /* objects with op->nrof require this check */
2512  return;
2513 
2514  if (QUERY_FLAG(op, FLAG_REMOVED)) {
2515  op->nrof += i;
2516  } else if (op->env != NULL) {
2517  player *pl;
2518  /* is this object in the players inventory, or sub container
2519  * therein?
2520  */
2521  tmp = object_get_player_container(op->env);
2522  /* nope. Is this a container the player has opened?
2523  * If so, set tmp to that player.
2524  * IMO, searching through all the players will mostly
2525  * likely be quicker than following op->env to the map,
2526  * and then searching the map for a player.
2527  */
2528  if (!tmp) {
2529  for (pl = first_player; pl; pl = pl->next)
2530  if (pl->ob->container == op->env)
2531  break;
2532  if (pl)
2533  tmp = pl->ob;
2534  else
2535  tmp = NULL;
2536  }
2537 
2538  /* Because of weight reduction by container and integer arithmetic,
2539  * there is no guarantee the rounded weight of combined items will be
2540  * the same as the sum of rounded weights.
2541  * Therefore just remove the current weight, and add the new.
2542  * Same adjustment done in object_decrease_nrof().
2543  */
2544  object_sub_weight(op->env, op->weight * op->nrof);
2545  op->nrof += i;
2546  object_add_weight(op->env, op->weight * op->nrof);
2547  if (tmp) {
2548  esrv_update_item(UPD_NROF, tmp, op);
2549  // Why don't we need to update weight of op->env here?
2550  }
2551  } else {
2552  /* On a map. */
2553  op->nrof += i;
2554 
2555  FOR_MAP_PREPARE(op->map, op->x, op->y, pl)
2556  if (pl->contr) {
2557  pl->contr->socket.update_look = 1;
2558  break;
2559  }
2560  FOR_MAP_FINISH();
2561  }
2562 }
2563 
2578 void object_add_weight(object *op, signed long weight) {
2579  while (op != NULL) {
2580  if (op->type == CONTAINER) {
2581  weight = (signed long)(weight*(100-op->stats.Str)/100);
2582  }
2583  op->carrying += weight;
2584  op = op->env;
2585  }
2586 }
2587 
2602 object *object_insert_in_ob(object *op, object *where) {
2603  object *otmp;
2604 
2605  if (!QUERY_FLAG(op, FLAG_REMOVED)) {
2606  StringBuffer *sb;
2607  char *diff;
2608 
2609  sb = stringbuffer_new();
2610  object_dump(op, sb);
2611  diff = stringbuffer_finish(sb);
2612  LOG(llevError, "Trying to insert (ob) inserted object.\n%s\n", diff);
2613  free(diff);
2614  return op;
2615  }
2616 
2617  if (where == NULL) {
2618  StringBuffer *sb;
2619  char *diff;
2620 
2621  sb = stringbuffer_new();
2622  object_dump(op, sb);
2623  diff = stringbuffer_finish(sb);
2624  LOG(llevError, "Trying to put object in NULL.\n%s\n", diff);
2625  free(diff);
2626  return op;
2627  }
2628  if (where->head) {
2629  LOG(llevDebug, "Warning: Tried to insert object wrong part of multipart object.\n");
2630  }
2631  where = HEAD(where);
2632  if (op->more) {
2633  LOG(llevError, "Tried to insert multipart object %s (%u)\n", op->name, op->count);
2634  return op;
2635  }
2637  CLEAR_FLAG(op, FLAG_REMOVED);
2638  if (op->nrof) {
2639  FOR_INV_PREPARE(where, tmp)
2640  if (object_can_merge(tmp, op)) {
2641  /* return the original object and remove inserted object
2642  * (client needs the original object) */
2643  object_increase_nrof(tmp, op->nrof);
2644  SET_FLAG(op, FLAG_REMOVED);
2645  object_free(op, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK); /* free the inserted object */
2646  return tmp;
2647  }
2648  FOR_INV_FINISH();
2649 
2650  /* the item couldn't merge. */
2651  object_add_weight(where, op->weight*op->nrof);
2652  } else
2653  object_add_weight(where, op->weight+op->carrying);
2654 
2655  op->map = NULL;
2656  op->env = where;
2657  op->above = NULL;
2658  op->below = NULL;
2659  op->x = 0,
2660  op->y = 0;
2661  op->ox = 0,
2662  op->oy = 0;
2663 
2664  /* Client has no idea of ordering so lets not bother ordering it here.
2665  * It sure simplifies this function...
2666  */
2667  if (where->inv == NULL)
2668  where->inv = op;
2669  else {
2670  op->below = where->inv;
2671  op->below->above = op;
2672  where->inv = op;
2673  }
2674 
2675  /* Update in 2 cases: object goes into player's inventory, or object goes into container the player
2676  * is looking into. */
2677  if (where->contr != NULL)
2678  esrv_send_item(where, op);
2679  else if (where->type == CONTAINER && QUERY_FLAG(where, FLAG_APPLIED)) {
2680  object *pl = NULL;
2681 
2682  if (op->env->env && op->env->env->contr)
2683  /* Container is in player's inventory. */
2684  pl = op->env->env;
2685  else if (op->env->map) {
2686  /* Container on map, look above for player. */
2687  FOR_ABOVE_PREPARE(op->env, above)
2688  if (above->contr) {
2689  pl = above;
2690  break;
2691  }
2692  FOR_ABOVE_FINISH();
2693  }
2694  if (pl)
2695  esrv_send_item(pl, op);
2696  }
2697 
2698  otmp = object_get_player_container(where);
2699  if (otmp && otmp->contr != NULL) {
2700  if (!QUERY_FLAG(otmp, FLAG_NO_FIX_PLAYER)
2701  && (QUERY_FLAG(op, FLAG_APPLIED) || op->type == SKILL || op->glow_radius != 0))
2702  /* fix_object will only consider applied items, or skills, or items with a glow radius.
2703  thus no need to call it if our object hasn't that. */
2704  fix_object(otmp);
2705  }
2706 
2707  /* reset the light list and los of the players on the map */
2708  if (op->glow_radius != 0 && where->map) {
2709 #ifdef DEBUG_LIGHTS
2710  LOG(llevDebug, " object_insert_in_ob(): got %s to insert in map/op\n", op->name);
2711 #endif /* DEBUG_LIGHTS */
2712  if (MAP_DARKNESS(where->map)) {
2713  SET_MAP_FLAGS(where->map, where->x, where->y, P_NEED_UPDATE);
2714  update_position(where->map, where->x, where->y);
2715  update_all_los(where->map, where->x, where->y);
2716  }
2717  }
2718 
2719  return op;
2720 }
2721 
2744 int object_check_move_on(object *op, object *originator) {
2745  object *tmp;
2746  tag_t tag;
2747  mapstruct *m = op->map;
2748  int x = op->x, y = op->y;
2749  MoveType move_on, move_slow, move_block;
2750 
2751  if (QUERY_FLAG(op, FLAG_NO_APPLY))
2752  return 0;
2753 
2754  tag = op->count;
2755 
2756  move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
2757  move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
2758  move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
2759 
2760  /* if nothing on this space will slow op down or be applied,
2761  * no need to do checking below. have to make sure move_type
2762  * is set, as lots of objects don't have it set - we treat that
2763  * as walking.
2764  */
2765  if (op->move_type
2766  && !(op->move_type&move_on)
2767  && !(op->move_type&move_slow))
2768  return 0;
2769 
2770  /* This is basically inverse logic of that below - basically,
2771  * if the object can avoid the move on or slow move, they do so,
2772  * but can't do it if the alternate movement they are using is
2773  * blocked. Logic on this seems confusing, but does seem correct.
2774  */
2775  if ((op->move_type&~move_on&~move_block) != 0
2776  && (op->move_type&~move_slow&~move_block) != 0)
2777  return 0;
2778 
2779  /* The objects have to be checked from top to bottom.
2780  * Hence, we first go to the top:
2781  */
2782 
2783  tmp = GET_MAP_OB(op->map, op->x, op->y);
2785  if (tmp->above == NULL)
2786  break;
2787  /* Trim the search when we find the first other spell effect
2788  * this helps performance so that if a space has 50 spell objects,
2789  * we don't need to check all of them.
2790  */
2791  if ((tmp->move_type&MOVE_FLY_LOW) && QUERY_FLAG(tmp, FLAG_NO_PICK))
2792  break;
2795  if (tmp == op)
2796  continue; /* Can't apply yourself */
2797 
2798  /* Check to see if one of the movement types should be slowed down.
2799  * Second check makes sure that the movement types not being slowed
2800  * (~slow_move) is not blocked on this space - just because the
2801  * space doesn't slow down swimming (for example), if you can't actually
2802  * swim on that space, can't use it to avoid the penalty.
2803  */
2804  if (!QUERY_FLAG(op, FLAG_WIZPASS)) {
2805  if ((!op->move_type && tmp->move_slow&MOVE_WALK)
2806  || ((op->move_type&tmp->move_slow) && (op->move_type&~tmp->move_slow&~tmp->move_block) == 0)) {
2807  float diff;
2808 
2809  diff = tmp->move_slow_penalty*FABS(op->speed);
2810  if (op->type == PLAYER) {
2813  diff /= 4.0;
2814  }
2815  }
2816  op->speed_left -= diff;
2817  }
2818  }
2819 
2820  /* Basically same logic as above, except now for actual apply. */
2821  if ((!op->move_type && tmp->move_on&MOVE_WALK)
2822  || ((op->move_type&tmp->move_on) && (op->move_type&~tmp->move_on&~tmp->move_block) == 0)) {
2823  ob_move_on(tmp, op, originator);
2824  if (object_was_destroyed(op, tag))
2825  return 1;
2826 
2827  /* what the person/creature stepped onto has moved the object
2828  * someplace new. Don't process any further - if we did,
2829  * have a feeling strange problems would result.
2830  */
2831  if (op->map != m || op->x != x || op->y != y)
2832  return 0;
2833  }
2835  return 0;
2836 }
2837 
2850 object *map_find_by_archetype(mapstruct *m, int x, int y, const archetype *at) {
2851  if (m == NULL || OUT_OF_REAL_MAP(m, x, y)) {
2852  LOG(llevError, "Present_arch called outside map.\n");
2853  return NULL;
2854  }
2855 
2856  FOR_MAP_PREPARE(m, x, y, tmp)
2857  if (tmp->arch == at)
2858  return tmp;
2859  FOR_MAP_FINISH();
2860 
2861  return NULL;
2862 }
2863 
2877 object *map_find_by_type(mapstruct *m, int x, int y, uint8_t type) {
2878  if (OUT_OF_REAL_MAP(m, x, y)) {
2879  return NULL;
2880  }
2881 
2882  FOR_MAP_PREPARE(m, x, y, tmp)
2883  if (tmp->type == type)
2884  return tmp;
2885  FOR_MAP_FINISH();
2886 
2887  return NULL;
2888 }
2889 
2900 object *object_present_in_ob(uint8_t type, const object *op) {
2901  object *tmp;
2902 
2903  for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
2904  if (tmp->type == type)
2905  return tmp;
2906 
2907  return NULL;
2908 }
2909 
2935 object *object_present_in_ob_by_name(int type, const char *str, const object *op) {
2936  object *tmp;
2937 
2938  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
2939  if ((type == -1 || tmp->type == type) && !strcmp(str, tmp->name))
2940  return tmp;
2941  }
2942  return NULL;
2943 }
2944 
2954 object *arch_present_in_ob(const archetype *at, const object *op) {
2955  object *tmp;
2956 
2957  for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
2958  if (tmp->arch == at)
2959  return tmp;
2960  return NULL;
2961 }
2962 
2971 void object_set_flag_inv(object*op, int flag) {
2972  object *tmp;
2973 
2974  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
2975  SET_FLAG(tmp, flag);
2976  object_set_flag_inv(tmp, flag);
2977  }
2978 }
2979 
2988 void object_unset_flag_inv(object*op, int flag) {
2989  object *tmp;
2990 
2991  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
2992  CLEAR_FLAG(tmp, flag);
2993  object_unset_flag_inv(tmp, flag);
2994  }
2995 }
2996 
3006 void object_set_cheat(object *op) {
3007  SET_FLAG(op, FLAG_WAS_WIZ);
3009 }
3010 
3028 int object_find_multi_free_spot_around(const object *ob, const object *gen, int16_t *hx, int16_t *hy) {
3029  int genx, geny, genx2, geny2, sx, sy, sx2, sy2, ix, iy, nx, ny, i, flag;
3030  int freecount = 0;
3031 
3032  ob = HEAD(ob);
3033 
3034  object_get_multi_size(ob, &sx, &sy, &sx2, &sy2);
3035  object_get_multi_size(gen, &genx, &geny, &genx2, &geny2);
3036  /*
3037  * sx and sy are now the coords of the bottom right corner of ob relative to the head.
3038  * genx and geny are now the coords of the bottom right corner of gen relative to the head.
3039  * sx2 and sy2 are now the coords of the head of ob relative to the top left corner.
3040  * genx2 and geny2 are now the coords of the head of gen relative to the top left corner.
3041  */
3042 
3043  sx++;
3044  sy++;
3045  genx++;
3046  geny++;
3047  /*
3048  * sx, sy, genx, and geny, are now the size of the object, excluding parts left and above
3049  * the head.
3050  */
3051 
3052  ix = gen->x-sx-genx2;
3053  iy = gen->y-sy-geny2;
3054  sx += genx+sx2;
3055  sy += geny+sy2;
3056  /*
3057  * ix and iy are the map coords of the top left square where the head of ob could possibly
3058  * be placed. sx and sy are now the size of the square to search for placement of the head
3059  * relative to ix and iy.
3060  */
3061 
3062  /*
3063  * Loop around the square of possible positions for the head of ob object:
3064  */
3065  for (i = 0; i < (sx+sx+sy+sy); i++) {
3066  if (i <= sx) {
3067  nx = i+ix;
3068  ny = iy;
3069  } else if (i <= sx+sy) {
3070  nx = ix+sx;
3071  ny = iy+i-sx;
3072  } else if (i <= sx+sy+sx) {
3073  nx = ix+sx-(i-(sx+sy));
3074  ny = iy+sy;
3075  } else {
3076  nx = ix;
3077  ny = iy+sy-(i-(sx+sy+sx));
3078  }
3079  /* Check if the spot is free. */
3080  flag = ob_blocked(ob, gen->map, nx, ny);
3081  if (!flag) {
3082  freecount++;
3083  }
3084  }
3085  /* If no free spaces, return. */
3086  if (!freecount)
3087  return -1;
3088 
3089  /* Choose a random valid position */
3090  freecount = RANDOM()%freecount;
3091  for (i = 0; i < sx+sx+sy+sy; i++) {
3092  if (i <= sx) {
3093  nx = i+ix;
3094  ny = iy;
3095  } else if (i <= sx+sy) {
3096  nx = ix+sx;
3097  ny = iy+i-sx;
3098  } else if (i <= sx+sy+sx) {
3099  nx = ix+sx-(i-(sx+sy));
3100  ny = iy+sy;
3101  } else {
3102  nx = ix;
3103  ny = iy+sy-(i-(sx+sy+sx));
3104  }
3105 
3106  /* Make sure it's within map. */
3107  if (nx < 0 || nx >= MAP_WIDTH(gen->map)
3108  || ny < 0 || ny >= MAP_HEIGHT(gen->map))
3109  continue;
3110 
3111  /* Check if the spot is free.*/
3112  flag = ob_blocked(ob, gen->map, nx, ny);
3113  if (!flag) {
3114  freecount--;
3115  if (freecount <= 0) {
3116  *hx = nx;
3117  *hy = ny;
3118  return 0;
3119  }
3120  }
3121  }
3122  return -1;
3123 }
3124 
3144 int object_find_multi_free_spot_within_radius(const object *ob, const object *gen, int *hx, int *hy) {
3145  int genx, geny, genx2, geny2, sx, sy, sx2, sy2, ix, iy, nx, ny, i, flag;
3146  int8_t x, y, radius;
3147  int freecount = 0, freecountstop = 0;
3148  const char *value;
3149  int8_t *x_array;
3150  int8_t *y_array;
3151 
3152  /* If radius is not set, default to 1 */
3153  value = object_get_value(gen, "generator_radius");
3154  if (value) {
3155  radius = (int8_t)strtol(value, NULL, 10);
3156  if (radius < 1) {
3157  radius = 1;
3158  }
3159  } else {
3160  radius = 1;
3161  }
3162 
3163  ob = HEAD(ob);
3164 
3165  object_get_multi_size(ob, &sx, &sy, &sx2, &sy2);
3166  object_get_multi_size(gen, &genx, &geny, &genx2, &geny2);
3167  /*
3168  * sx and sy are now the coords of the bottom right corner
3169  * of ob relative to the head.
3170  * genx and geny are now the coords of the bottom right corner
3171  * of gen relative to the head.
3172  * sx2 and sy2 are now the coords of the head of ob relative
3173  * to the top left corner.
3174  * genx2 and geny2 are now the coords of the head of gen relative
3175  * to the top left corner.
3176  */
3177 
3178  sx++;
3179  sy++;
3180  genx++;
3181  geny++;
3182  /*
3183  * sx, sy, genx, and geny, are now the size of the object,
3184  * excluding parts left and above the head.
3185  */
3186 
3187  ix = gen->x-sx-genx2-radius+1;
3188  iy = gen->y-sy-geny2-radius+1;
3189  sx += genx+sx2+radius*2-1;
3190  sy += geny+sy2+radius*2-1;
3191 
3192  /*
3193  * ix and iy are the map coords of the top left square where
3194  * the head of ob could possibly be placed. sx and sy are now
3195  * the size of the square to search for placement of the head
3196  * relative to ix and iy.
3197  */
3198 
3199  /* Create arrays large enough to hold free space coordinates */
3200  x_array = malloc(sx*sy*sizeof(int8_t));
3201  y_array = malloc(sx*sy*sizeof(int8_t));
3202 
3203  /*
3204  * Loop through the area of possible positions for the head of ob object:
3205  */
3206  for (x = 0; x < sx; x++) {
3207  for (y = 0; y < sy; y++) {
3208  nx = ix+x;
3209  ny = iy+y;
3210 
3211 
3212  /* Make sure it's within map. */
3213  if (get_map_flags(gen->map, NULL, nx, ny, NULL, NULL)&P_OUT_OF_MAP) {
3214  continue;
3215  }
3216 
3217  /* Check if the spot is free. */
3218  flag = ob_blocked(ob, gen->map, nx, ny);
3219  if (!flag) {
3220  x_array[freecount] = nx;
3221  y_array[freecount] = ny;
3222  freecount++;
3223  }
3224  }
3225  }
3226  /* If no free spaces, return. */
3227  if (!freecount) {
3228  free(x_array);
3229  free(y_array);
3230  return -1;
3231  }
3232 
3233  /* Choose a random valid position */
3234  freecountstop = RANDOM()%freecount;
3235  for (i = 0; i < freecount; i++) {
3236  nx = x_array[i];
3237  ny = y_array[i];
3238 
3239  /* Check if the spot is free.*/
3240  flag = ob_blocked(ob, gen->map, nx, ny);
3241  if (!flag) {
3242  freecountstop--;
3243  if (freecountstop <= 0) {
3244  *hx = nx;
3245  *hy = ny;
3246  free(x_array);
3247  free(y_array);
3248  return 0;
3249  }
3250  }
3251  }
3252  free(x_array);
3253  free(y_array);
3254  return -1;
3255 }
3256 
3291 int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop) {
3292  int i, index = 0, flag;
3293  static int altern[SIZEOFFREE];
3294 
3295  for (i = start; i < stop; i++) {
3296  flag = ob_blocked(ob, m, x+freearr_x[i], y+freearr_y[i]);
3297  if (!flag)
3298  altern[index++] = i;
3299 
3300  /* Basically, if we find a wall on a space, we cut down the search size.
3301  * In this way, we won't return spaces that are on another side of a wall.
3302  * This mostly work, but it cuts down the search size in all directions -
3303  * if the space being examined only has a wall to the north and empty
3304  * spaces in all the other directions, this will reduce the search space
3305  * to only the spaces immediately surrounding the target area, and
3306  * won't look 2 spaces south of the target space.
3307  */
3308  else if ((flag&AB_NO_PASS) && maxfree[i] < stop)
3309  stop = maxfree[i];
3310  }
3311  if (!index)
3312  return -1;
3313  return altern[RANDOM()%index];
3314 }
3315 
3331 int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y) {
3332  int i;
3333 
3334  for (i = 0; i < SIZEOFFREE; i++) {
3335  if (!ob_blocked(ob, m, x+freearr_x[i], y+freearr_y[i]))
3336  return i;
3337  }
3338  return -1;
3339 }
3340 
3350 static void permute(int *arr, int begin, int end) {
3351  int i, j, tmp, len;
3352 
3353  len = end-begin;
3354  for (i = begin; i < end; i++) {
3355  j = begin+RANDOM()%len;
3356 
3357  tmp = arr[i];
3358  arr[i] = arr[j];
3359  arr[j] = tmp;
3360  }
3361 }
3362 
3374 void get_search_arr(int *search_arr) {
3375  int i;
3376 
3377  for (i = 0; i < SIZEOFFREE; i++) {
3378  search_arr[i] = i;
3379  }
3380 
3381  permute(search_arr, 1, SIZEOFFREE1+1);
3382  permute(search_arr, SIZEOFFREE1+1, SIZEOFFREE2+1);
3383  permute(search_arr, SIZEOFFREE2+1, SIZEOFFREE);
3384 }
3385 
3404 int map_find_dir(mapstruct *m, int x, int y, object *exclude) {
3405  int i, max = SIZEOFFREE, mflags;
3406  int16_t nx, ny;
3407  mapstruct *mp;
3408  MoveType blocked, move_type;
3409 
3410  if (exclude && exclude->head) {
3411  exclude = exclude->head;
3412  move_type = exclude->move_type;
3413  } else {
3414  /* If we don't have anything, presume it can use all movement types. */
3415  move_type = MOVE_ALL;
3416  }
3417 
3418  for (i = 1; i < max; i++) {
3419  mp = m;
3420  nx = x+freearr_x[i];
3421  ny = y+freearr_y[i];
3422 
3423  mflags = get_map_flags(m, &mp, nx, ny, &nx, &ny);
3424  if (mflags&P_OUT_OF_MAP) {
3425  max = maxfree[i];
3426  } else {
3427  blocked = GET_MAP_MOVE_BLOCK(mp, nx, ny);
3428 
3429  if ((move_type&blocked) == move_type) {
3430  max = maxfree[i];
3431  } else if (mflags&P_IS_ALIVE) {
3432  FOR_MAP_PREPARE(mp, nx, ny, tmp) {
3433  if ((QUERY_FLAG(tmp, FLAG_MONSTER) || tmp->type == PLAYER)
3434  && (tmp != exclude || (tmp->head && tmp->head != exclude))) {
3435  return freedir[i];
3436  }
3437  } FOR_MAP_FINISH();
3438  }
3439  }
3440  }
3441  return 0;
3442 }
3443 
3451 int object_distance(const object *ob1, const object *ob2) {
3452  int i;
3453 
3454  i = (ob1->x-ob2->x)*(ob1->x-ob2->x)+
3455  (ob1->y-ob2->y)*(ob1->y-ob2->y);
3456  return i;
3457 }
3458 
3467 int find_dir_2(int x, int y) {
3468  int q;
3469 
3470  if (!y)
3471  q = -300*x;
3472  else
3473  q = x*100/y;
3474  if (y > 0) {
3475  if (q < -242)
3476  return 3;
3477  if (q < -41)
3478  return 2;
3479  if (q < 41)
3480  return 1;
3481  if (q < 242)
3482  return 8;
3483  return 7;
3484  }
3485  if (q < -242)
3486  return 7;
3487  if (q < -41)
3488  return 6;
3489  if (q < 41)
3490  return 5;
3491  if (q < 242)
3492  return 4;
3493  return 3;
3494 }
3495 
3504 int absdir(int d) {
3505  // Shortcut for modulus that work becuase we have a power of 2
3506  d &= 7;
3507  // 0 needs to be 8
3508  if (!d)
3509  d = 8;
3510  return d;
3511 }
3512 
3522 int dirdiff(int dir1, int dir2) {
3523  int d;
3524 
3525  d = abs(dir1-dir2);
3526  if (d > 4)
3527  d = 8-d;
3528  return d;
3529 }
3530 
3542 static const int reduction_dir[SIZEOFFREE][3] = {
3543  { 0, 0, 0 }, /* 0 */
3544  { 0, 0, 0 }, /* 1 */
3545  { 0, 0, 0 }, /* 2 */
3546  { 0, 0, 0 }, /* 3 */
3547  { 0, 0, 0 }, /* 4 */
3548  { 0, 0, 0 }, /* 5 */
3549  { 0, 0, 0 }, /* 6 */
3550  { 0, 0, 0 }, /* 7 */
3551  { 0, 0, 0 }, /* 8 */
3552  { 8, 1, 2 }, /* 9 */
3553  { 1, 2, -1 }, /* 10 */
3554  { 2, 10, 12 }, /* 11 */
3555  { 2, 3, -1 }, /* 12 */
3556  { 2, 3, 4 }, /* 13 */
3557  { 3, 4, -1 }, /* 14 */
3558  { 4, 14, 16 }, /* 15 */
3559  { 5, 4, -1 }, /* 16 */
3560  { 4, 5, 6 }, /* 17 */
3561  { 6, 5, -1 }, /* 18 */
3562  { 6, 20, 18 }, /* 19 */
3563  { 7, 6, -1 }, /* 20 */
3564  { 6, 7, 8 }, /* 21 */
3565  { 7, 8, -1 }, /* 22 */
3566  { 8, 22, 24 }, /* 23 */
3567  { 8, 1, -1 }, /* 24 */
3568  { 24, 9, 10 }, /* 25 */
3569  { 9, 10, -1 }, /* 26 */
3570  { 10, 11, -1 }, /* 27 */
3571  { 27, 11, 29 }, /* 28 */
3572  { 11, 12, -1 }, /* 29 */
3573  { 12, 13, -1 }, /* 30 */
3574  { 12, 13, 14 }, /* 31 */
3575  { 13, 14, -1 }, /* 32 */
3576  { 14, 15, -1 }, /* 33 */
3577  { 33, 15, 35 }, /* 34 */
3578  { 16, 15, -1 }, /* 35 */
3579  { 17, 16, -1 }, /* 36 */
3580  { 18, 17, 16 }, /* 37 */
3581  { 18, 17, -1 }, /* 38 */
3582  { 18, 19, -1 }, /* 39 */
3583  { 41, 19, 39 }, /* 40 */
3584  { 19, 20, -1 }, /* 41 */
3585  { 20, 21, -1 }, /* 42 */
3586  { 20, 21, 22 }, /* 43 */
3587  { 21, 22, -1 }, /* 44 */
3588  { 23, 22, -1 }, /* 45 */
3589  { 45, 47, 23 }, /* 46 */
3590  { 23, 24, -1 }, /* 47 */
3591  { 24, 9, -1 } /* 48 */
3592 };
3593 
3612 int can_see_monsterP(mapstruct *m, int x, int y, int dir) {
3613  int16_t dx, dy;
3614  int mflags;
3615 
3616  if (dir < 0)
3617  return 0; /* exit condition: invalid direction */
3618 
3619  dx = x+freearr_x[dir];
3620  dy = y+freearr_y[dir];
3621 
3622  mflags = get_map_flags(m, &m, dx, dy, &dx, &dy);
3623 
3624  /* This functional arguably was incorrect before - it was
3625  * checking for P_WALL - that was basically seeing if
3626  * we could move to the monster - this is being more
3627  * literal on if we can see it. To know if we can actually
3628  * move to the monster, we'd need the monster passed in or
3629  * at least its move type.
3630  */
3631  if (mflags&(P_OUT_OF_MAP|P_BLOCKSVIEW))
3632  return 0;
3633 
3634  /* yes, can see. */
3635  if (dir < 9)
3636  return 1;
3637  return can_see_monsterP(m, x, y, reduction_dir[dir][0])|
3638  can_see_monsterP(m, x, y, reduction_dir[dir][1])|
3639  can_see_monsterP(m, x, y, reduction_dir[dir][2]);
3640 }
3641 
3657 int object_can_pick(const object *who, const object *item) {
3658  /* I re-wrote this as a series of if statements
3659  * instead of a nested return (foo & bar && yaz)
3660  * - I think this is much more readable,
3661  * and likely compiler effectively optimizes it the
3662  * same.
3663  */
3664  if (item->weight <= 0)
3665  return 0;
3666  if (QUERY_FLAG(item, FLAG_NO_PICK))
3667  return 0;
3668  if (QUERY_FLAG(item, FLAG_ALIVE))
3669  return 0;
3670  if (item->invisible)
3671  return 0;
3672  if (item->type == TRANSPORT && item->contr != NULL) {
3673  return 0;
3674  }
3675 
3676  /* Weight limit for monsters */
3677  if (who->type != PLAYER && ((uint32_t)(who->weight+who->carrying+item->weight)) > get_weight_limit(who->stats.Str))
3678  return 0;
3679 
3680  /* Can not pick up multipart objects */
3681  if (item->head || item->more)
3682  return 0;
3683 
3684  /* Everything passes, so OK to pick up */
3685  return 1;
3686 }
3687 
3699 object *object_create_clone(object *asrc) {
3700  object *dst = NULL, *tmp, *src, *part, *prev;
3701 
3702  if (!asrc)
3703  return NULL;
3704  src = HEAD(asrc);
3705 
3706  prev = NULL;
3707  for (part = src; part; part = part->more) {
3708  tmp = object_new();
3709  object_copy(part, tmp);
3710  /*
3711  * Need to reset the weight, since object_insert_in_ob() later will
3712  * recompute this field.
3713  */
3714  tmp->carrying = tmp->arch->clone.carrying;
3715  tmp->x -= src->x;
3716  tmp->y -= src->y;
3717  if (!part->head) {
3718  dst = tmp;
3719  tmp->head = NULL;
3720  } else {
3721  tmp->head = dst;
3722  }
3723  tmp->more = NULL;
3724  if (prev)
3725  prev->more = tmp;
3726  prev = tmp;
3727  }
3728  /*** copy inventory ***/
3729  FOR_INV_PREPARE(src, item)
3730  (void)object_insert_in_ob(object_create_clone(item), dst);
3731  FOR_INV_FINISH();
3732 
3733  return dst;
3734 }
3735 
3746 object *object_find_by_name(const object *who, const char *name) {
3747  const char *name_shared = add_string(name);
3748  object *tmp;
3749 
3750  for (tmp = who->inv; tmp; tmp = tmp->below)
3751  if (tmp->name == name_shared)
3752  break;
3753  free_string(name_shared);
3754  return tmp;
3755 }
3756 
3770 object *object_find_by_type(const object *who, int type) {
3771  object *tmp;
3772 
3773  for (tmp = who->inv; tmp; tmp = tmp->below)
3774  if (tmp->type == type)
3775  return tmp;
3776 
3777  return NULL;
3778 }
3779 
3794 object *object_find_by_type_without_flags(const object *who, int type, int *flags, int num_flags) {
3795  int flag_okay;
3796  for (object *tmp = who->inv; tmp; tmp = tmp->below)
3797  if (tmp->type == type) {
3798  flag_okay = 1;
3799  for (int i = 0; i < num_flags; ++i) {
3800  if (QUERY_FLAG(tmp, flags[i])) {
3801  flag_okay = 0; // A flag we didn't want set was set. Skip this item.
3802  break;
3803  }
3804  }
3805  if (flag_okay) // If flag_okay == 1, then the flags specified were not set
3806  return tmp; // If we reach here, none of the flags specified were set. Just like we wanted.
3807  }
3808 
3809  return NULL;
3810 }
3811 
3827 object *object_find_by_type2(const object *who, int type1, int type2) {
3828  object *tmp;
3829 
3830  for (tmp = who->inv; tmp; tmp = tmp->below)
3831  if (tmp->type == type1 || tmp->type == type2)
3832  return tmp;
3833 
3834  return NULL;
3835 }
3836 
3850 object *object_find_by_tag(const object *who, tag_t tag) {
3851  object *tmp;
3852 
3853  for (tmp = who->inv; tmp; tmp = tmp->below)
3854  if (tmp->count == tag)
3855  return tmp;
3856 
3857  return NULL;
3858 }
3859 
3873 object *object_find_by_type_applied(const object *who, int type) {
3874  object *tmp;
3875 
3876  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
3877  if (tmp->type == type && QUERY_FLAG(tmp, FLAG_APPLIED))
3878  return tmp;
3879 
3880  return NULL;
3881 }
3882 
3898 object *object_find_by_type_and_name(const object *who, int type, const char *name) {
3899  object *tmp;
3900 
3901  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
3902  if (tmp->type == type && strcmp(tmp->name, name) == 0)
3903  return tmp;
3904 
3905  return NULL;
3906 }
3907 
3923 object *object_find_by_type_and_race(const object *who, int type, const char *race) {
3924  object *tmp;
3925 
3926  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
3927  if (tmp->type == type && strcmp(tmp->race, race) == 0)
3928  return tmp;
3929 
3930  return NULL;
3931 }
3932 
3948 object *object_find_by_type_and_slaying(const object *who, int type, const char *slaying) {
3949  object *tmp;
3950 
3951  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
3952  if (tmp->type == type && tmp->slaying != NULL && strcmp(tmp->slaying, slaying) == 0)
3953  return tmp;
3954 
3955  return NULL;
3956 }
3957 
3973 object *object_find_by_type_and_skill(const object *who, int type, const char *skill) {
3974  object *tmp;
3975 
3976  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
3977  if (tmp->type == type && tmp->skill != NULL && strcmp(tmp->skill, skill) == 0)
3978  return tmp;
3979 
3980  return NULL;
3981 }
3982 
3996 object *object_find_by_flag(const object *who, int flag) {
3997  object *tmp;
3998 
3999  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4000  if (QUERY_FLAG(tmp, flag))
4001  return tmp;
4002 
4003  return NULL;
4004 }
4005 
4019 object *object_find_by_flag_applied(const object *who, int flag) {
4020  object *tmp;
4021 
4022  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4023  if (QUERY_FLAG(tmp, FLAG_APPLIED) && QUERY_FLAG(tmp, flag))
4024  return tmp;
4025 
4026  return NULL;
4027 }
4028 
4042 object *object_find_by_arch_name(const object *who, const char *name) {
4043  object *tmp;
4044 
4045  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4046  if (strcmp(tmp->arch->name, name) == 0)
4047  return tmp;
4048 
4049  return NULL;
4050 }
4051 
4067 object *object_find_by_type_and_arch_name(const object *who, int type, const char *name) {
4068  object *tmp;
4069 
4070  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4071  if (tmp->type == type && strcmp(tmp->arch->name, name) == 0)
4072  return tmp;
4073 
4074  return NULL;
4075 }
4076 
4091 object *object_find_by_type_subtype(const object *who, int type, int subtype) {
4092  object *tmp;
4093 
4094  for (tmp = who->inv; tmp; tmp = tmp->below)
4095  if (tmp->type == type && tmp->subtype == subtype)
4096  return tmp;
4097 
4098  return NULL;
4099 }
4100 
4111 key_value *object_get_key_value(const object *ob, const char *key) {
4112  key_value *link;
4113 
4114  for (link = ob->key_values; link != NULL; link = link->next) {
4115  if (link->key == key) {
4116  return link;
4117  }
4118  }
4119 
4120  return NULL;
4121 }
4122 
4136 const char *object_get_value(const object *op, const char *const key) {
4137  key_value *link;
4138  const char *canonical_key;
4139 
4140  canonical_key = find_string(key);
4141 
4142  if (canonical_key == NULL) {
4143  /* 1. There being a field named key on any object
4144  * implies there'd be a shared string to find.
4145  * 2. Since there isn't, no object has this field.
4146  * 3. Therefore, *this *object doesn't have this field.
4147  */
4148  return NULL;
4149  }
4150 
4151  /* This is copied from object_get_key_value() above -
4152  * only 4 lines, and saves the function call overhead.
4153  */
4154  for (link = op->key_values; link != NULL; link = link->next) {
4155  if (link->key == canonical_key) {
4156  return link->value;
4157  }
4158  }
4159  return NULL;
4160 }
4161 
4176 static int object_set_value_s(object *op, const char *canonical_key, const char *value, int add_key) {
4177  key_value *field = NULL, *last = NULL;
4178 
4179  for (field = op->key_values; field != NULL; field = field->next) {
4180  if (field->key != canonical_key) {
4181  last = field;
4182  continue;
4183  }
4184 
4185  if (field->value)
4186  FREE_AND_CLEAR_STR(field->value);
4187  if (value)
4188  field->value = add_string(value);
4189  else {
4190  /* Basically, if the archetype has this key set,
4191  * we need to store the null value so when we save
4192  * it, we save the empty value so that when we load,
4193  * we get this value back again.
4194  */
4195  if (object_get_key_value(&op->arch->clone, canonical_key))
4196  field->value = NULL;
4197  else {
4198  /* Delete this link */
4199  if (field->key)
4200  FREE_AND_CLEAR_STR(field->key);
4201  if (field->value)
4202  FREE_AND_CLEAR_STR(field->value);
4203  if (last)
4204  last->next = field->next;
4205  else
4206  op->key_values = field->next;
4207  free(field);
4208  }
4209  }
4210  return TRUE;
4211  }
4212  /* IF we get here, key doesn't exist */
4213 
4214  /* No field, we'll have to add it. */
4215 
4216  if (!add_key) {
4217  return FALSE;
4218  }
4219  /* There isn't any good reason to store a null
4220  * value in the key/value list. If the archetype has
4221  * this key, then we should also have it, so shouldn't
4222  * be here. If user wants to store empty strings,
4223  * should pass in ""
4224  */
4225  if (value == NULL)
4226  return TRUE;
4227 
4228  field = malloc(sizeof(key_value));
4229 
4230  field->key = add_refcount(canonical_key);
4231  field->value = add_string(value);
4232  /* Usual prepend-addition. */
4233  field->next = op->key_values;
4234  op->key_values = field;
4235 
4236  return TRUE;
4237 }
4238 
4259 int object_set_value(object *op, const char *key, const char *value, int add_key) {
4260  const char *canonical_key = NULL;
4261  int floating_ref = FALSE;
4262  int ret;
4263 
4264  /* HACK This mess is to make sure set_ob_value() passes a shared string
4265  * to object_get_key_value(), without leaving a leaked refcount.
4266  */
4267 
4268  canonical_key = find_string(key);
4269  if (canonical_key == NULL) {
4270  canonical_key = add_string(key);
4271  floating_ref = TRUE;
4272  }
4273 
4274  ret = object_set_value_s(op, canonical_key, value, add_key);
4275 
4276  if (floating_ref) {
4277  free_string(canonical_key);
4278  }
4279 
4280  return ret;
4281 }
4282 
4334 int object_matches_string(object *pl, object *op, const char *name) {
4335  char *cp, local_name[MAX_BUF], name_op[MAX_BUF], name_short[HUGE_BUF], bname_s[MAX_BUF], bname_p[MAX_BUF];
4336  int count, retval = 0;
4337  /* strtok is destructive to name */
4338  safe_strncpy(local_name, name, sizeof(local_name));
4339 
4340  for (cp = strtok(local_name, ","); cp; cp = strtok(NULL, ",")) {
4341  while (cp[0] == ' ')
4342  ++cp; /* get rid of spaces */
4343 
4344  /* LOG(llevDebug, "Trying to match %s\n", cp);*/
4345  /* All is a very generic match - low match value */
4346  if (!strcmp(cp, "all"))
4347  return 1;
4348 
4349  /* unpaid is a little more specific */
4350  if (!strcmp(cp, "unpaid") && QUERY_FLAG(op, FLAG_UNPAID))
4351  return 2;
4352  if (!strcmp(cp, "cursed")
4354  && (QUERY_FLAG(op, FLAG_CURSED) || QUERY_FLAG(op, FLAG_DAMNED)))
4355  return 2;
4356 
4357  if (!strcmp(cp, "unlocked") && !QUERY_FLAG(op, FLAG_INV_LOCKED))
4358  return 2;
4359 
4360  /* Allow for things like '100 arrows' */
4361  count = atoi(cp);
4362  if (count != 0) {
4363  cp = strchr(cp, ' ');
4364  while (cp && cp[0] == ' ')
4365  ++cp; /* get rid of spaces */
4366  } else {
4367  if (pl->type == PLAYER)
4368  count = pl->contr->count;
4369  else
4370  count = 0;
4371  }
4372 
4373  if (!cp || cp[0] == '\0' || count < 0)
4374  return 0;
4375 
4376  /* The code here should go from highest retval to lowest. That
4377  * is because of the 'else' handling - we don't want to match on
4378  * something and set a low retval, even though it may match a higher retcal
4379  * later. So keep it in descending order here, so we try for the best
4380  * match first, and work downward.
4381  */
4382  query_name(op, name_op, MAX_BUF);
4383  query_short_name(op, name_short, HUGE_BUF);
4384  query_base_name(op, 0, bname_s, MAX_BUF);
4385  query_base_name(op, 1, bname_p, MAX_BUF);
4386 
4387  if (!strcasecmp(cp, name_op))
4388  retval = 20;
4389  else if (!strcasecmp(cp, name_short))
4390  retval = 18;
4391  else if (!strcasecmp(cp, bname_s))
4392  retval = 16;
4393  else if (!strcasecmp(cp, bname_p))
4394  retval = 16;
4395  else if (op->custom_name && !strcasecmp(cp, op->custom_name))
4396  retval = 15;
4397  else if (!strncasecmp(cp, bname_s, strlen(cp)))
4398  retval = 14;
4399  else if (!strncasecmp(cp, bname_p, strlen(cp)))
4400  retval = 14;
4401  /* Do substring checks, so things like 'Str+1' will match.
4402  * retval of these should perhaps be lower - they are lower
4403  * then the specific strcasecmp aboves, but still higher than
4404  * some other match criteria.
4405  */
4406  else if (strstr(bname_p, cp))
4407  retval = 12;
4408  else if (strstr(bname_s, cp))
4409  retval = 12;
4410  else if (strstr(name_short, cp))
4411  retval = 12;
4412  /* Check against plural/non plural based on count. */
4413  else if (count > 1 && !strcasecmp(cp, op->name_pl)) {
4414  retval = 6;
4415  } else if (count == 1 && !strcasecmp(op->name, cp)) {
4416  retval = 6;
4417  }
4418  /* base name matched - not bad */
4419  else if (strcasecmp(cp, op->name) == 0 && !count)
4420  retval = 4;
4421  /* Check for partial custom name, but give a real low priority */
4422  else if (op->custom_name && strstr(op->custom_name, cp))
4423  retval = 3;
4424 
4425  if (retval) {
4426  if (pl->type == PLAYER)
4427  pl->contr->count = count;
4428  return retval;
4429  }
4430  }
4431  return 0;
4432 }
4433 
4442 void object_fix_multipart(object *tmp) {
4443  archetype *at;
4444  object *op, *last;
4445 
4446  if (!tmp->map) {
4447  LOG(llevError, "object_fix_multipart: not on a map!\n");
4448  return;
4449  }
4450 
4451  /* already multipart - don't do anything more */
4452  if (tmp->head || tmp->more)
4453  return;
4454 
4455  /* If there is nothing more to this object, this for loop
4456  * won't do anything.
4457  */
4458  for (at = tmp->arch->more, last = tmp; at != NULL; at = at->more, last = op) {
4459  op = arch_to_object(at);
4460 
4461  /* update x,y coordinates */
4462  op->x += tmp->x;
4463  op->y += tmp->y;
4464  op->head = tmp;
4465  op->map = tmp->map;
4466  last->more = op;
4467  if (tmp->name != op->name) {
4468  if (op->name)
4469  free_string(op->name);
4470  op->name = add_string(tmp->name);
4471  }
4472  if (tmp->title != op->title) {
4473  if (op->title)
4474  free_string(op->title);
4475  op->title = add_string(tmp->title);
4476  }
4477  /* we could link all the parts onto tmp, and then just
4478  * call object_insert_in_map once, but the effect is the same,
4479  * as object_insert_in_map will call itself with each part, and
4480  * the coding is simpler to just to it here with each part.
4481  */
4482  object_insert_in_map(op, op->map, tmp, INS_NO_MERGE|INS_ABOVE_FLOOR_ONLY|INS_NO_WALK_ON);
4483  } /* for at = tmp->arch->more */
4484 }
4485 
4501 void object_get_multi_size(const object *ob, int *sx, int *sy, int *hx, int *hy) {
4502  archetype *part;
4503  int maxx = 0, maxy = 0, minx = 0, miny = 0;
4504 
4505  ob = HEAD(ob);
4506  *sx = 1;
4507  *sy = 1;
4508  if (ob->arch->more) {
4509  for (part = ob->arch; part; part = part->more) {
4510  if (part->clone.x > maxx)
4511  maxx = part->clone.x;
4512  if (part->clone.y > maxy)
4513  maxy = part->clone.y;
4514  if (part->clone.x < minx)
4515  minx = part->clone.x;
4516  if (part->clone.y < miny)
4517  miny = part->clone.y;
4518  }
4519  }
4520  if (sx)
4521  *sx = maxx;
4522  if (sy)
4523  *sy = maxy;
4524  if (hx)
4525  *hx = -minx;
4526  if (hy)
4527  *hy = -miny;
4528 }
4529 
4550 void object_insert_to_free_spot_or_free(object *op, mapstruct *map, int x, int y, int start, int stop, object *originator) {
4551  int pos;
4552 
4553  pos = object_find_free_spot(op, map, x, y, start, stop);
4554  if (pos == -1) {
4556  return;
4557  }
4558 
4559  object_insert_in_map_at(op, map, originator, 0, x+freearr_x[pos], y+freearr_y[pos]);
4560 }
4561 
4570 void object_set_msg(object *op, const char *msg) {
4571  if (op->msg != NULL) {
4572  free_string(op->msg);
4573  }
4574 
4575  if (msg != NULL) {
4576  // If the message does not have a trailing newline, add one.
4577  if (*msg != '\0' && strchr(msg, '\0')[-1] != '\n') {
4579  stringbuffer_append_string(sb, msg);
4580  stringbuffer_append_string(sb, "\n");
4581  op->msg = stringbuffer_finish_shared(sb);
4582  } else {
4583  op->msg = add_string(msg);
4584  }
4585  } else {
4586  op->msg = NULL;
4587  }
4588 }
4589 
4591 const char *const move_name[] = {
4592  "walk",
4593  "fly_low",
4594  "fly_high",
4595  "swim",
4596  "boat",
4597  NULL
4598 };
4599 
4600 /* This array equates the FLAG_ values with the V_ values. Use -1 to
4601  * put gaps in the array that should not be processed.
4602  * The order matches the order of the define values in 'define.h'.
4603  */
4610 static const char *const flag_names[NUM_FLAGS+1] = {
4611  "alive", "wiz", NULL, NULL, "was_wiz", "applied", "unpaid",
4612  "can_use_shield", "no_pick", "client_anim_sync", "client_anim_random", /* 10 */
4613  "is_animated", NULL /* FLAG_DIALOG_PARSED, not saved */,
4614  NULL /* flying */, "monster", "friendly", "generator",
4615  "is_thrown", "auto_apply", "treasure", "player sold", /* 20 */
4616  "see_invisible", "can_roll", "overlay_floor",
4617  "is_turnable", NULL /* walk_off */, NULL /* fly_on */,
4618  NULL /*fly_off*/, "is_used_up", "identified", "reflecting", /* 30 */
4619  "changing", "splitting", "hitback", "startequip",
4620  "blocksview", "undead", "scared", "unaggressive",
4621  "reflect_missile", "reflect_spell", /* 40 */
4622  "no_magic", "no_fix_player", "is_lightable", "tear_down",
4623  "run_away", NULL /*pass_thru */, NULL /*can_pass_thru*/,
4624  NULL /*"pick_up"*/, "unique", "no_drop", /* 50 */
4625  NULL /* wizcast*/, "can_cast_spell", "can_use_scroll", "can_use_range",
4626  "can_use_bow", "can_use_armour", "can_use_weapon",
4627  "can_use_ring", "has_ready_range", "has_ready_bow", /* 60 */
4628  "xrays", NULL, "is_floor", "lifesave", "no_strength", "sleep",
4629  "stand_still", "random_movement", "only_attack", "confused", /* 70 */
4630  "stealth", NULL, NULL, "cursed", "damned",
4631  "see_anywhere", "known_magical", "known_cursed",
4632  "can_use_skill", "been_applied", /* 80 */
4633  "has_ready_scroll", NULL, NULL,
4634  NULL, "make_invisible", "inv_locked", "is_wooded",
4635  "is_hilly", "has_ready_skill", "has_ready_weapon", /* 90 */
4636  "no_skill_ident", "is_blind", "can_see_in_dark", "is_cauldron",
4637  NULL, "no_steal", "one_hit", NULL, "berserk", "neutral", /* 100 */
4638  "no_attack", "no_damage", NULL, NULL, "activate_on_push",
4639  "activate_on_release", "is_water", "use_content_on_gen", NULL, "is_buildable", /* 110 */
4640  NULL, "blessed", "known_blessed"
4641 };
4642 
4652 {
4653  static char retbuf[MAX_BUF], retbuf_all[MAX_BUF];
4654  int i, all_count = 0, count;
4655 
4656  strcpy(retbuf, "");
4657  strcpy(retbuf_all, " all");
4658 
4659  /* Quick check, and probably fairly common */
4660  if (mt == MOVE_ALL) {
4661  stringbuffer_append_string(sb, "all");
4662  return;
4663  }
4664  if (mt == 0) {
4665  stringbuffer_append_string(sb, "0");
4666  return;
4667  }
4668 
4669  /* We basically slide the bits down. Why look at MOVE_ALL?
4670  * because we may want to return a string like 'all -swim',
4671  * and if we just looked at mt, we couldn't get that.
4672  */
4673  for (i = MOVE_ALL, count = 0; i != 0; i >>= 1, count++) {
4674  if (mt&(1<<count)) {
4675  strcat(retbuf, " ");
4676  strcat(retbuf, move_name[count]);
4677  } else {
4678  strcat(retbuf_all, " -");
4679  strcat(retbuf_all, move_name[count]);
4680  all_count++;
4681  }
4682  }
4683  /* Basically, if there is a single negation, return it, eg
4684  * 'all -swim'. But more than that, just return the
4685  * enumerated values. It doesn't make sense to return
4686  * 'all -walk -fly_low' - it is shorter to return 'fly_high swim'
4687  */
4688  if (all_count <= 1)
4689  stringbuffer_append_string(sb, retbuf_all+1);
4690  else
4691  stringbuffer_append_string(sb, retbuf+1);
4692 }
4693 
4695 static inline void ADD_STRINGLINE_ENTRY(StringBuffer *sb, const char *name, const char *value) {
4696  stringbuffer_append_string(sb, name);
4697  stringbuffer_append_string(sb, value);
4698  stringbuffer_append_string(sb, "\n");
4699 }
4700 
4702 static inline void FAST_SAVE_LONG(StringBuffer *sb, const char *name, const long value) {
4703  stringbuffer_append_printf(sb, "%s%ld\n", name, (long int)value);
4704 }
4705 
4707 static inline void FAST_SAVE_DOUBLE(StringBuffer *sb, const char *name, const double value) {
4708  stringbuffer_append_printf(sb, "%s%f\n", name, value);
4709 }
4710 
4724 void get_ob_diff(StringBuffer *sb, const object *op, const object *op2) {
4725  static char buf2[64];
4726  int tmp;
4727  int i;
4728  key_value *my_field;
4729  key_value *arch_field;
4730 
4731  /* This saves the key/value lists. We do it first so that any
4732  * keys that match field names will be overwritten by the loader.
4733  */
4734  for (my_field = op->key_values; my_field != NULL; my_field = my_field->next) {
4735  /* Find the field in the opposing member. */
4736  arch_field = object_get_key_value(op2, my_field->key);
4737 
4738  /* If there's no partnering field, or it's got a different value, save our field. */
4739  if (arch_field == NULL || my_field->value != arch_field->value) {
4740  stringbuffer_append_string(sb, my_field->key);
4741  stringbuffer_append_string(sb, " ");
4742  /* If this is null, then saving it as a space should
4743  * cause it to be null again.
4744  */
4745  if (my_field->value)
4746  stringbuffer_append_string(sb, my_field->value);
4747  stringbuffer_append_string(sb, "\n");
4748  }
4749  }
4750  /* We don't need to worry about the arch's extra fields - they
4751  * will get taken care of the object_copy() function.
4752  */
4753 
4754  if (op->name && op->name != op2->name) {
4755  ADD_STRINGLINE_ENTRY(sb, "name ", op->name);
4756  }
4757  if (op->name_pl && op->name_pl != op2->name_pl) {
4758  ADD_STRINGLINE_ENTRY(sb, "name_pl ", op->name_pl);
4759  }
4760  if (op->anim_suffix && op->anim_suffix != op2->anim_suffix) {
4761  ADD_STRINGLINE_ENTRY(sb, "anim_suffix ", op->anim_suffix);
4762  }
4763  if (op->custom_name && op->custom_name != op2->custom_name) {
4764  ADD_STRINGLINE_ENTRY(sb, "custom_name ", op->custom_name);
4765  }
4766  if (op->title && op->title != op2->title) {
4767  ADD_STRINGLINE_ENTRY(sb, "title ", op->title);
4768  }
4769  if (op->race && op->race != op2->race) {
4770  ADD_STRINGLINE_ENTRY(sb, "race ", op->race);
4771  }
4772  if (op->slaying && op->slaying != op2->slaying) {
4773  ADD_STRINGLINE_ENTRY(sb, "slaying ", op->slaying);
4774  }
4775  if (op->skill && op->skill != op2->skill) {
4776  ADD_STRINGLINE_ENTRY(sb, "skill ", op->skill);
4777  }
4778  if (op->msg && op->msg != op2->msg) {
4779  stringbuffer_append_string(sb, "msg\n");
4780  stringbuffer_append_string(sb, op->msg);
4781  stringbuffer_append_string(sb, "endmsg\n");
4782  }
4783  if (op->lore && op->lore != op2->lore) {
4784  stringbuffer_append_string(sb, "lore\n");
4785  stringbuffer_append_string(sb, op->lore);
4786  stringbuffer_append_string(sb, "endlore\n");
4787  }
4788  if (op->other_arch != op2->other_arch && op->other_arch != NULL && op->other_arch->name) {
4789  ADD_STRINGLINE_ENTRY(sb, "other_arch ", op->other_arch->name);
4790  }
4791  if (op->face != op2->face) {
4792  ADD_STRINGLINE_ENTRY(sb, "face ", op->face->name);
4793  }
4794 
4795  if (op->animation != op2->animation) {
4796  if (op->animation) {
4797  ADD_STRINGLINE_ENTRY(sb, "animation ", op->animation->name);
4798  if (!QUERY_FLAG (op, FLAG_ANIMATE)) {
4799  stringbuffer_append_string(sb, "is_animated 0\n");
4800  }
4801  } else {
4802  stringbuffer_append_string(sb, "animation NONE\n");
4803  }
4804  }
4805  if (op->stats.Str != op2->stats.Str)
4806  FAST_SAVE_LONG(sb, "Str ", op->stats.Str);
4807  if (op->stats.Dex != op2->stats.Dex)
4808  FAST_SAVE_LONG(sb, "Dex ", op->stats.Dex);
4809  if (op->stats.Con != op2->stats.Con)
4810  FAST_SAVE_LONG(sb, "Con ", op->stats.Con);
4811  if (op->stats.Wis != op2->stats.Wis)
4812  FAST_SAVE_LONG(sb, "Wis ", op->stats.Wis);
4813  if (op->stats.Pow != op2->stats.Pow)
4814  FAST_SAVE_LONG(sb, "Pow ", op->stats.Pow);
4815  if (op->stats.Cha != op2->stats.Cha)
4816  FAST_SAVE_LONG(sb, "Cha ", op->stats.Cha);
4817  if (op->stats.Int != op2->stats.Int)
4818  FAST_SAVE_LONG(sb, "Int ", op->stats.Int);
4819  if (op->stats.hp != op2->stats.hp)
4820  FAST_SAVE_LONG(sb, "hp ", op->stats.hp);
4821  if (op->stats.maxhp != op2->stats.maxhp)
4822  FAST_SAVE_LONG(sb, "maxhp ", op->stats.maxhp);
4823  if (op->stats.sp != op2->stats.sp)
4824  FAST_SAVE_LONG(sb, "sp ", op->stats.sp);
4825  if (op->stats.maxsp != op2->stats.maxsp)
4826  FAST_SAVE_LONG(sb, "maxsp ", op->stats.maxsp);
4827  if (op->stats.grace != op2->stats.grace)
4828  FAST_SAVE_LONG(sb, "grace ", op->stats.grace);
4829  if (op->stats.maxgrace != op2->stats.maxgrace)
4830  FAST_SAVE_LONG(sb, "maxgrace ", op->stats.maxgrace);
4831 
4832  if (op->stats.exp != op2->stats.exp) {
4833  snprintf(buf2, sizeof(buf2), "%"FMT64, op->stats.exp);
4834  ADD_STRINGLINE_ENTRY(sb, "exp ", buf2);
4835  }
4836 
4837  if (op->total_exp != op2->total_exp) {
4838  snprintf(buf2, sizeof(buf2), "%"FMT64, op->total_exp);
4839  ADD_STRINGLINE_ENTRY(sb, "total_exp ", buf2);
4840  }
4841 
4842  if (op->expmul != op2->expmul)
4843  FAST_SAVE_DOUBLE(sb, "expmul ", op->expmul);
4844  if (op->stats.food != op2->stats.food)
4845  FAST_SAVE_LONG(sb, "food ", op->stats.food);
4846  if (op->stats.dam != op2->stats.dam)
4847  FAST_SAVE_LONG(sb, "dam ", op->stats.dam);
4848  if (op->stats.luck != op2->stats.luck)
4849  FAST_SAVE_LONG(sb, "luck ", op->stats.luck);
4850  if (op->stats.wc != op2->stats.wc)
4851  FAST_SAVE_LONG(sb, "wc ", op->stats.wc);
4852  if (op->stats.ac != op2->stats.ac)
4853  FAST_SAVE_LONG(sb, "ac ", op->stats.ac);
4854  if (op->x != op2->x)
4855  FAST_SAVE_LONG(sb, "x ", op->x);
4856  if (op->y != op2->y)
4857  FAST_SAVE_LONG(sb, "y ", op->y);
4858  if (op->speed != op2->speed) {
4859  FAST_SAVE_DOUBLE(sb, "speed ", op->speed);
4860  }
4861  if (op->speed > 0 && op->speed_left != op2->speed_left) {
4862  FAST_SAVE_DOUBLE(sb, "speed_left ", op->speed_left);
4863  }
4864  if (op->weapon_speed != op2->weapon_speed) {
4865  FAST_SAVE_DOUBLE(sb, "weapon_speed ", op->weapon_speed);
4866  }
4867  if (op->weapon_speed > 0 && op->weapon_speed_left != op2->weapon_speed_left) {
4868  FAST_SAVE_DOUBLE(sb, "weapon_speed_left ", op->weapon_speed_left);
4869  }
4870  if (op->move_status != op2->move_status)
4871  FAST_SAVE_LONG(sb, "move_state ", op->move_status);
4872  if (op->attack_movement != op2->attack_movement)
4873  FAST_SAVE_LONG(sb, "attack_movement ", op->attack_movement);
4874  if (op->nrof != op2->nrof)
4875  FAST_SAVE_LONG(sb, "nrof ", op->nrof);
4876  if (op->level != op2->level)
4877  FAST_SAVE_LONG(sb, "level ", op->level);
4878  if (op->direction != op2->direction)
4879  FAST_SAVE_LONG(sb, "direction ", op->direction);
4880  if (op->type != op2->type)
4881  FAST_SAVE_LONG(sb, "type ", op->type);
4882  if (op->subtype != op2->subtype)
4883  FAST_SAVE_LONG(sb, "subtype ", op->subtype);
4884  if (op->attacktype != op2->attacktype)
4885  FAST_SAVE_LONG(sb, "attacktype ", op->attacktype);
4886 
4887  for (tmp = 0; tmp < NROFATTACKS; tmp++) {
4888  if (op->resist[tmp] != op2->resist[tmp]) {
4889  stringbuffer_append_string(sb, "resist_");
4890  FAST_SAVE_LONG(sb, resist_save[tmp], op->resist[tmp]);
4891  }
4892  }
4893 
4894  if (op->path_attuned != op2->path_attuned)
4895  FAST_SAVE_LONG(sb, "path_attuned ", op->path_attuned);
4896  if (op->path_repelled != op2->path_repelled)
4897  FAST_SAVE_LONG(sb, "path_repelled ", op->path_repelled);
4898  if (op->path_denied != op2->path_denied)
4899  FAST_SAVE_LONG(sb, "path_denied ", op->path_denied);
4900  if (op->material != op2->material)
4901  FAST_SAVE_LONG(sb, "material ", op->material);
4902  if (op->materialname && op->materialname != op2->materialname) {
4903  ADD_STRINGLINE_ENTRY(sb, "materialname ", op->materialname);
4904  }
4905  if (op->value != op2->value)
4906  FAST_SAVE_LONG(sb, "value ", op->value);
4907  if (op->carrying != op2->carrying)
4908  FAST_SAVE_LONG(sb, "carrying ", op->carrying);
4909  if (op->weight != op2->weight)
4910  FAST_SAVE_LONG(sb, "weight ", op->weight);
4911  if (op->invisible != op2->invisible)
4912  FAST_SAVE_LONG(sb, "invisible ", op->invisible);
4913  if (op->state != op2->state)
4914  FAST_SAVE_LONG(sb, "state ", op->state);
4915  if (op->magic != op2->magic)
4916  FAST_SAVE_LONG(sb, "magic ", op->magic);
4917  if (op->last_heal != op2->last_heal)
4918  FAST_SAVE_LONG(sb, "last_heal ", op->last_heal);
4919  if (op->last_sp != op2->last_sp)
4920  FAST_SAVE_LONG(sb, "last_sp ", op->last_sp);
4921  if (op->last_grace != op2->last_grace)
4922  FAST_SAVE_LONG(sb, "last_grace ", op->last_grace);
4923  if (op->last_eat != op2->last_eat)
4924  FAST_SAVE_LONG(sb, "last_eat ", op->last_eat);
4925  if (QUERY_FLAG(op, FLAG_IS_LINKED) && (tmp = get_button_value(op)))
4926  FAST_SAVE_LONG(sb, "connected ", tmp);
4927  if (op->glow_radius != op2->glow_radius)
4928  FAST_SAVE_LONG(sb, "glow_radius ", op->glow_radius);
4929  if (op->randomitems != op2->randomitems) {
4930  ADD_STRINGLINE_ENTRY(sb, "randomitems ", op->randomitems ? op->randomitems->name : "none");
4931  }
4932 
4933  if (op->run_away != op2->run_away)
4934  FAST_SAVE_LONG(sb, "run_away ", op->run_away);
4935  if (op->pick_up != op2->pick_up)
4936  FAST_SAVE_LONG(sb, "pick_up ", op->pick_up);
4937  if (op->weight_limit != op2->weight_limit)
4938  FAST_SAVE_LONG(sb, "container ", op->weight_limit);
4939  if (op->will_apply != op2->will_apply)
4940  FAST_SAVE_LONG(sb, "will_apply ", op->will_apply);
4941  if (op->smoothlevel != op2->smoothlevel)
4942  FAST_SAVE_LONG(sb, "smoothlevel ", op->smoothlevel);
4943 
4944  if (op->map_layer != op2->map_layer)
4945  ADD_STRINGLINE_ENTRY(sb, "map_layer ", map_layer_name[op->map_layer]);
4946 
4947  if (op->weapontype && op->weapontype != op2->weapontype) {
4948  FAST_SAVE_LONG(sb, "weapontype ", op->weapontype);
4949  }
4950  if (op->client_type && op->client_type != op2->client_type) {
4951  FAST_SAVE_LONG(sb, "client_type ", op->client_type);
4952  }
4953 
4954  if (op->item_power != op2->item_power) {
4955  FAST_SAVE_LONG(sb, "item_power ", op->item_power);
4956  }
4957 
4958  if (op->duration != op2->duration)
4959  FAST_SAVE_LONG(sb, "duration ", op->duration);
4960 
4961  if (op->range != op2->range)
4962  FAST_SAVE_LONG(sb, "range ", op->range);
4963 
4964  if (op->range_modifier != op2->range_modifier)
4965  FAST_SAVE_LONG(sb, "range_modifier ", op->range_modifier);
4966 
4967  if (op->duration_modifier != op2->duration_modifier)
4968  FAST_SAVE_LONG(sb, "duration_modifier ", op->duration_modifier);
4969 
4970  if (op->dam_modifier != op2->dam_modifier)
4971  FAST_SAVE_LONG(sb, "dam_modifier ", op->dam_modifier);
4972 
4973  if (op->gen_sp_armour != op2->gen_sp_armour) {
4974  FAST_SAVE_LONG(sb, "gen_sp_armour ", op->gen_sp_armour);
4975  }
4976 
4977  /* I've kept the old int move type saving code commented out.
4978  * In an ideal world, we'd know if we want to do a quick
4979  * save (say to a temp map, where we don't care about strings),
4980  * or a slower save/dm dump, where printing out strings is handy.
4981  */
4982  if (op->move_type != op2->move_type) {
4983  /*FAST_SAVE_LONG(sb, "move_type ", op->move_type)*/
4984  stringbuffer_append_string(sb, "move_type ");
4985  get_string_move_type(sb, op->move_type);
4986  stringbuffer_append_string(sb, "\n");
4987  }
4988  if (op->move_block != op2->move_block) {
4989  /*FAST_SAVE_LONG(sb, "move_block ", op->move_block)*/
4990  stringbuffer_append_string(sb, "move_block ");
4991  get_string_move_type(sb, op->move_block);
4992  stringbuffer_append_string(sb, "\n");
4993  }
4994  if (op->move_allow != op2->move_allow) {
4995  /*FAST_SAVE_LONG(sb, "move_allow ", op->move_allow);*/
4996  stringbuffer_append_string(sb, "move_allow ");
4997  get_string_move_type(sb, op->move_allow);
4998  stringbuffer_append_string(sb, "\n");
4999  }
5000  if (op->move_on != op2->move_on) {
5001  /*FAST_SAVE_LONG(sb, "move_on ", op->move_on);*/
5002  stringbuffer_append_string(sb, "move_on ");
5003  get_string_move_type(sb, op->move_on);
5004  stringbuffer_append_string(sb, "\n");
5005  }
5006  if (op->move_off != op2->move_off) {
5007  /*FAST_SAVE_LONG(sb, "move_off ", op->move_off);*/
5008  stringbuffer_append_string(sb, "move_off ");
5009  get_string_move_type(sb, op->move_off);
5010  stringbuffer_append_string(sb, "\n");
5011  }
5012  if (op->move_slow != op2->move_slow) {
5013  /*FAST_SAVE_LONG(sb, "move_slow ", op->move_slow);*/
5014  stringbuffer_append_string(sb, "move_slow ");
5015  get_string_move_type(sb, op->move_slow);
5016  stringbuffer_append_string(sb, "\n");
5017  }
5018 
5019  if (op->move_slow_penalty != op2->move_slow_penalty) {
5020  FAST_SAVE_DOUBLE(sb, "move_slow_penalty ", op->move_slow_penalty);
5021  }
5022 
5023  if (!COMPARE_FLAGS(op, op2)) {
5024  for (tmp = 0; tmp <= NUM_FLAGS; tmp++) {
5025  if (flag_names[tmp] && (QUERY_FLAG(op, tmp) != QUERY_FLAG(op2, tmp))) {
5026  ADD_STRINGLINE_ENTRY(sb, flag_names[tmp], QUERY_FLAG(op, tmp) ? " 1" : " 0");
5027  }
5028  }
5029  }
5030 
5031  /* Save body locations */
5032  for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
5033  if (op->body_info[i] != op2->body_info[i]) {
5034  stringbuffer_append_string(sb, body_locations[i].save_name);
5035  FAST_SAVE_LONG(sb, " ", op->body_info[i]);
5036  }
5037  }
5038 
5039 }
5040 
5045 void save_object_in_sb(StringBuffer *sb, const object *op, const int flag) {
5046  archetype *at = op->arch;
5047  if (at == NULL)
5048  at = empty_archetype;
5049 
5050  stringbuffer_append_printf(sb, "arch %s\n", at->name);
5051 
5052  if (op->arch->reference_count > 0) {
5053  /* The object is a custom item/monster, so we handle its save differently.
5054  * We compare the custom archetype to the "original" one, then only save hp/gr/sp
5055  * which are the only values we can't recompute later - all others are modified by items in inventory.
5056  * Note that hp/gr/sp will appear twice in save, but last value will take precedence.
5057  */
5058  archetype *original = find_archetype(op->arch->name);
5059  if (!original) {
5060  LOG(llevError, "could not find original archetype %s for custom monster!\n", op->arch->name);
5061  abort();
5062  }
5063  get_ob_diff(sb, &op->arch->clone, &original->clone);
5064  if (op->stats.hp != op->arch->clone.stats.hp)
5065  FAST_SAVE_LONG(sb, "hp ", op->stats.hp);
5066  if (op->stats.sp != op->arch->clone.stats.sp)
5067  FAST_SAVE_LONG(sb, "sp ", op->stats.sp);
5068  if (op->stats.grace != op->arch->clone.stats.grace)
5069  FAST_SAVE_LONG(sb, "grace ", op->stats.grace);
5070  if (op->x != op->arch->clone.x)
5071  FAST_SAVE_LONG(sb, "x ", op->x);
5072  if (op->y != op->arch->clone.y)
5073  FAST_SAVE_LONG(sb, "y ", op->y);
5074  } else if (op->artifact != NULL) {
5075  /* if op is an artifact, then find the "standard" artifact to use that for the diff */
5076  object *base;
5077  const artifact *artifact;
5078 
5079  artifact = find_artifact(op, op->artifact);
5080  if (artifact == NULL) {
5081  LOG(llevError, "could not find artifact %s [%d] to save data\n", op->artifact, op->type);
5082  get_ob_diff(sb, op, &at->clone);
5083  } else {
5084  stringbuffer_append_printf(sb, "artifact %s\n", op->artifact);
5085  base = arch_to_object(at);
5086  give_artifact_abilities(base, artifact->item);
5087  get_ob_diff(sb, op, base);
5088  object_free(base, FREE_OBJ_NO_DESTROY_CALLBACK | FREE_OBJ_FREE_INVENTORY);
5089  }
5090  } else {
5091  get_ob_diff(sb, op, &at->clone);
5092  }
5093 
5094  /* Eneq(@csd.uu.se): Added this to allow containers being saved with contents*/
5095  FOR_INV_PREPARE(op, tmp)
5096  save_object_in_sb(sb, tmp, flag);
5097  FOR_INV_FINISH();
5098 
5099  stringbuffer_append_string(sb, "end\n");
5100 }
5101 
5114 int save_object(FILE *fp, object *op, int flag) {
5115  /* Even if the object does have an owner, it would seem that we should
5116  * still save it.
5117  */
5118  if (object_get_owner(op) != NULL || fp == NULL)
5119  return SAVE_ERROR_OK;
5120 
5121  /* If it is unpaid and we don't want to save those, just return. */
5122  if (!(flag&SAVE_FLAG_SAVE_UNPAID) && (QUERY_FLAG(op, FLAG_UNPAID))) {
5123  return SAVE_ERROR_OK;
5124  }
5125 
5126  /* If the object has no_save set, just return */
5127  if (QUERY_FLAG(op, FLAG_NO_SAVE))
5128  return SAVE_ERROR_OK;
5129 
5131  save_object_in_sb(sb, op, flag);
5132  char *cp = stringbuffer_finish(sb);
5133  if (fputs(cp, fp) == EOF) {
5134  free(cp);
5135  return SAVE_ERROR_WRITE;
5136  } else {
5137  free(cp);
5138  return SAVE_ERROR_OK;
5139  }
5140 }
5141 
5143  if (op->map) {
5144  sstring death_animation = object_get_value(op, "death_animation");
5145  if (death_animation != NULL) {
5146  object *death = create_archetype(death_animation);
5147 
5148  if (death != NULL) {
5149  object_insert_in_map_at(death, op->map, op, 0, op->x, op->y);
5150  }
5151  }
5152  }
5153 }
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:3451
object * object_find_by_type_and_name(const object *who, int type, const char *name)
Definition: object.c:3898
#define FLAG_NO_MAGIC
Definition: define.h:276
char path[HUGE_BUF]
Definition: map.h:366
int find_dir_2(int x, int y)
Definition: object.c:3467
#define SET_MAP_OB(M, X, Y, tmp)
Definition: map.h:178
#define FLAG_NO_DROP
Definition: define.h:289
Definition: player.h:92
#define FLAG_DAMNED
Definition: define.h:318
#define FLAG_IS_FLOOR
Definition: define.h:303
#define FLAG_UNPAID
Definition: define.h:236
#define MOVE_WALK
Definition: define.h:407
#define FLAG_IS_LINKED
Definition: define.h:316
#define GET_MAP_MOVE_OFF(M, X, Y)
Definition: map.h:208
void object_free(object *ob, int flags)
Definition: object.c:1348
EXTERN long ob_count
Definition: global.h:148
#define P_PLAYER
Definition: map.h:237
#define SAVE_ERROR_OK
Definition: map.h:144
struct Statistics statistics
Definition: init.c:101
#define P_NEED_UPDATE
Definition: map.h:240
#define FOR_OB_AND_ABOVE_PREPARE(op_)
Definition: define.h:776
#define OBJ_EXPAND
Definition: config.h:525
void esrv_send_item(object *pl, object *op)
Definition: main.c:337
#define SET_FLAG(xyz, p)
Definition: define.h:223
int arch_init
sstring add_refcount(sstring str)
Definition: shstr.c:210
void object_clear_owner(object *op)
Definition: object.c:587
void object_sub_weight(object *op, signed long weight)
Definition: object.c:1562
object * object_find_by_type_applied(const object *who, int type)
Definition: object.c:3873
#define FABS(x)
Definition: define.h:22
unsigned char uint8_t
Definition: win32.h:161
void object_free_all_data(void)
Definition: object.c:531
void object_copy_owner(object *op, object *clone)
Definition: object.c:657
object * object_find_by_type(const object *who, int type)
Definition: object.c:3770
uint64_t spell_merges
Definition: global.h:355
object * object_get_env_recursive(object *op)
Definition: object.c:360
const Face * blank_face
Definition: image.c:35
#define FLAG_FRIENDLY
Definition: define.h:246
archetype * find_archetype(const char *name)
Definition: assets.cpp:253
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:952
sstring stringbuffer_finish_shared(StringBuffer *sb)
Definition: stringbuffer.c:85
#define P_NO_MAGIC
Definition: map.h:228
void fatal(enum fatal_error err)
Definition: utils.c:597
static object * find_insert_pos(object *op, const int flag)
Definition: object.c:2046
static void get_string_move_type(StringBuffer *sb, MoveType mt)
Definition: object.c:4651
void init_objects(void)
Definition: object.c:93
#define FLAG_NO_FIX_PLAYER
Definition: define.h:277
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
void get_search_arr(int *search_arr)
Definition: object.c:3374
unsigned char MoveType
Definition: define.h:432
void free_string(sstring str)
Definition: shstr.c:280
signed long object_sum_weight(object *op)
Definition: object.c:338
static void FAST_SAVE_LONG(StringBuffer *sb, const char *name, const long value)
Definition: object.c:4702
#define HUGE_BUF
Definition: define.h:37
void object_fix_multipart(object *tmp)
Definition: object.c:4442
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.c:2394
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.c:340
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4136
void query_base_name(const object *op, int plural, char *buf, size_t size)
Definition: item.c:680
#define MAP_HEIGHT(m)
Definition: map.h:80
socket_struct socket
Definition: player.h:94
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:3144
int object_count_active(void)
Definition: object.c:1538
object * object_find_by_flag(const object *who, int flag)
Definition: object.c:3996
int object_check_move_on(object *op, object *originator)
Definition: object.c:2744
object * object_find_by_type_without_flags(const object *who, int type, int *flags, int num_flags)
Definition: object.c:3794
void object_dump_all(void)
Definition: object.c:468
object * object_find_by_type_and_slaying(const object *who, int type, const char *slaying)
Definition: object.c:3948
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:218
#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:2954
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
#define MAX(x, y)
Definition: compat.h:20
void object_update(object *op, int action)
Definition: object.c:1190
uint32_t get_weight_limit(int stat)
Definition: living.c:2360
int object_can_merge(object *ob1, object *ob2)
Definition: object.c:199
#define safe_strncpy
Definition: compat.h:23
void free_dialog_information(object *op)
Definition: dialog.c:32
int absdir(int d)
Definition: object.c:3504
object * object_find_by_name(const object *who, const char *name)
Definition: object.c:3746
struct artifactstruct artifact
uint8_t casting_time
Definition: global.h:269
uint64_t spell_hash_full
Definition: global.h:356
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:791
#define FLAG_REMOVED
Definition: define.h:232
#define EVENT_DESTROY
Definition: events.h:25
#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:3542
#define SAVE_ERROR_WRITE
Definition: map.h:147
void object_set_flag_inv(object *op, int flag)
Definition: object.c:2971
#define MOVE_ALL
Definition: define.h:413
void esrv_del_item(player *pl, object *ob)
Definition: main.c:353
int object_find_multi_free_spot_around(const object *ob, const object *gen, int16_t *hx, int16_t *hy)
Definition: object.c:3028
void object_set_owner(object *op, object *owner)
Definition: object.c:604
#define SAVE_FLAG_SAVE_UNPAID
Definition: map.h:111
#define MAP_IN_MEMORY
Definition: map.h:131
int object_set_value(object *op, const char *key, const char *value, int add_key)
Definition: object.c:4259
uint32_t update_look
Definition: newserver.h:104
void object_free_drop_inventory(object *ob)
Definition: object.c:1316
key_value * object_get_key_value(const object *ob, const char *key)
Definition: object.c:4111
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1849
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:2342
int dirdiff(int dir1, int dir2)
Definition: object.c:3522
void object_update_turn_face(object *op)
Definition: object.c:1069
void object_dump(const object *op, StringBuffer *sb)
Definition: object.c:415
#define FLAG_CLIENT_ANIM_RANDOM
Definition: define.h:241
EXTERN archetype * empty_archetype
Definition: global.h:143
int maxfree[SIZEOFFREE]
Definition: object.c:77
#define STARTMAX
Definition: config.h:524
#define FLAG_ALIVE
Definition: define.h:230
object * object_new(void)
Definition: object.c:1011
#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:491
#define FOR_OB_AND_ABOVE_FINISH()
Definition: define.h:780
int object_can_pick(const object *who, const object *item)
Definition: object.c:3657
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2602
#define MAP_DARKNESS(m)
Definition: map.h:75
#define FLAG_NO_STEAL
Definition: define.h:343
signed short int16_t
Definition: win32.h:160
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
#define MOVE_FLY_LOW
Definition: define.h:408
#define FOR_ABOVE_FINISH()
Definition: define.h:731
void update_all_los(const mapstruct *map, int x, int y)
Definition: los.c:536
object * create_archetype(const char *name)
Definition: arch.cpp:281
#define snprintf
Definition: win32.h:46
#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:498
const char *const move_name[]
Definition: object.c:4591
const artifact * find_artifact(const object *op, const char *name)
Definition: artifact.c:652
int strcasecmp(const char *s1, const char *s2)
#define FLAG_CLIENT_ANIM_SYNC
Definition: define.h:240
#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:4019
void object_remove_from_active_list(object *op)
Definition: object.c:1148
#define FMT64
Definition: compat.h:12
void update_position(mapstruct *m, int x, int y)
Definition: map.c:2142
int object_count_free(void)
Definition: object.c:1506
object * objects
Definition: object.c:60
#define FLAG_BLOCKSVIEW
Definition: define.h:269
static const char *const flag_names[NUM_FLAGS+1]
Definition: object.c:4610
static void FAST_SAVE_DOUBLE(StringBuffer *sb, const char *name, const double value)
Definition: object.c:4707
#define FOR_ABOVE_PREPARE(op_, it_)
Definition: define.h:724
int save_object(FILE *fp, object *op, int flag)
Definition: object.c:5114
object * object_present_in_ob(uint8_t type, const object *op)
Definition: object.c:2900
#define offsetof(type, member)
Definition: shstr.h:37
#define FLAG_IS_A_TEMPLATE
Definition: define.h:375
void object_reset(object *op)
Definition: object.c:698
#define GET_MAP_TOP(M, X, Y)
Definition: map.h:175
object * object_create_clone(object *asrc)
Definition: object.c:3699
#define NUM_FLAGS
Definition: define.h:385
#define GET_MAP_MOVE_ON(M, X, Y)
Definition: map.h:203
#define FLAG_OVERLAY_FLOOR
Definition: define.h:255
#define SIZEOFFREE
Definition: define.h:154
#define P_OUT_OF_MAP
Definition: map.h:252
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:193
object * object_find_by_name_global(const char *str)
Definition: object.c:511
object * object_insert_in_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:2110
void object_clear(object *op)
Definition: object.c:747
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:198
#define MAP_SAVING
Definition: map.h:134
#define UPD_WEIGHT
Definition: newclient.h:291
static void ADD_STRINGLINE_ENTRY(StringBuffer *sb, const char *name, const char *value)
Definition: object.c:4695
object * object_find_by_type_and_race(const object *who, int type, const char *race)
Definition: object.c:3923
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#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:4501
#define MAX_BUF
Definition: define.h:35
static object * free_objects
Definition: object.c:61
static void expand_objects(void)
Definition: object.c:970
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:111
int strncasecmp(const char *s1, const char *s2, int n)
const char *const map_layer_name[MAP_LAYERS]
Definition: map.c:55
int nroffreeobjects
Definition: object.c:56
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
Definition: object.c:3331
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:4550
static const flag_definition flags[]
#define FOR_MAP_FINISH()
Definition: define.h:767
#define COMPARE_FLAGS(p, q)
Definition: define.h:226
void animate_object(object *op, int dir)
Definition: anim.c:43
#define FLAG_NO_APPLY
Definition: define.h:302
#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:3850
const char * sstring
Definition: global.h:40
#define GET_MAP_FLAGS(M, X, Y)
Definition: map.h:162
#define FLAG_IS_HILLY
Definition: define.h:333
#define FLAG_CURSED
Definition: define.h:317
unsigned int uint32_t
Definition: win32.h:162
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.c:4091
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:164
#define FLAG_ANIMATE
Definition: define.h:242
object * object_find_by_type_and_skill(const object *who, int type, const char *skill)
Definition: object.c:3973
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
Definition: object.c:2382
object * object_decrease_nrof(object *op, uint32_t i)
Definition: object.c:2421
void save_object_in_sb(StringBuffer *sb, const object *op, const int flag)
Definition: object.c:5045
#define RANDOM()
Definition: define.h:681
#define UPD_NROF
Definition: newclient.h:296
signed char int8_t
Definition: win32.h:158
#define GET_MAP_PLAYER(M, X, Y)
Definition: map.h:169
int get_button_value(const object *button)
Definition: button.c:749
#define AB_NO_PASS
Definition: map.h:236
#define UPD_FACE
Definition: newclient.h:292
#define MAP_WIDTH(m)
Definition: map.h:78
struct Settings settings
Definition: init.c:39
void object_set_enemy(object *op, object *enemy)
Definition: object.c:679
#define FLAG_APPLIED
Definition: define.h:235
#define NROFATTACKS
Definition: attack.h:17
object * object_merge(object *op, object *top)
Definition: object.c:1800
void query_short_name(const object *op, char *buf, size_t size)
Definition: item.c:508
object * object_get_player_container(object *op)
Definition: object.c:377
int out_of_map(mapstruct *m, int x, int y)
Definition: map.c:2321
static object objarray[STARTMAX]
Definition: object.c:55
#define SIZEOFFREE2
Definition: define.h:153
int object_count_used(void)
Definition: object.c:1522
static const object * object_get_owner_const(const object *op)
Definition: object.c:394
static void permute(int *, int, int)
Definition: object.c:3350
void object_handle_death_animation(object *op)
Definition: object.c:5142
void object_unset_flag_inv(object *op, int flag)
Definition: object.c:2988
int object_matches_string(object *pl, object *op, const char *name)
Definition: object.c:4334
#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:1878
int map_find_dir(mapstruct *m, int x, int y, object *exclude)
Definition: object.c:3404
void free_arch(archetype *at)
Definition: arch.cpp:172
sstring add_string(const char *str)
Definition: shstr.c:124
EXTERN player * first_player
Definition: global.h:115
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:2578
#define GET_MAP_OB(M, X, Y)
Definition: map.h:173
#define FLAG_MONSTER
Definition: define.h:245
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.c:824
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: events.cpp:259
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.c:311
void object_set_cheat(object *op)
Definition: object.c:3006
#define SIZEOFFREE1
Definition: define.h:152
#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:2508
EXTERN const char *const resist_save[NROFATTACKS]
Definition: attack.h:139
#define SET_MAP_TOP(M, X, Y, tmp)
Definition: map.h:180
#define FLAG_WAS_WIZ
Definition: define.h:234
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:760
#define GET_MAP_MOVE_SLOW(M, X, Y)
Definition: map.h:198
#define P_BLOCKSVIEW
Definition: map.h:227
void object_set_msg(object *op, const char *msg)
Definition: object.c:4570
int can_see_monsterP(mapstruct *m, int x, int y, int dir)
Definition: object.c:3612
int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop)
Definition: object.c:3291
#define FLAG_DIALOG_PARSED
Definition: define.h:243
#define P_NO_ERROR
Definition: map.h:241
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:583
object * item
Definition: artifact.h:15
object * find_skill_by_number(object *who, int skillno)
Definition: main.c:349
object * map_find_by_archetype(mapstruct *m, int x, int y, const archetype *at)
Definition: object.c:2850
Definition: map.h:326
#define P_IS_ALIVE
Definition: map.h:238
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.c:4724
#define FREE_AND_CLEAR(xyz)
Definition: global.h:193
#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:4067
#define FLAG_WIZPASS
Definition: define.h:315
void object_free_key_values(object *op)
Definition: object.c:718
#define FLAG_INV_LOCKED
Definition: define.h:330
void fix_object(object *op)
Definition: living.c:1124
void object_update_speed(object *op)
Definition: object.c:1086
#define SCRIPT_FIX_NOTHING
Definition: global.h:371
object * object_get_owner(object *op)
Definition: object.c:568
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
#define P_NO_CLERIC
Definition: map.h:239
object * object_find_by_arch_name(const object *who, const char *name)
Definition: object.c:4042
object * object_present_in_ob_by_name(int type, const char *str, const object *op)
Definition: object.c:2935
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:707
void object_remove(object *op)
Definition: object.c:1588
object * map_find_by_type(mapstruct *m, int x, int y, uint8_t type)
Definition: object.c:2877
static int compare_ob_value_lists_one(const object *, const object *)
Definition: object.c:129
object * object_find_by_type2(const object *who, int type1, int type2)
Definition: object.c:3827
#define FLAG_FREED
Definition: define.h:233
static int object_set_value_s(object *, const char *, const char *, int)
Definition: object.c:4176
void object_free_inventory(object *ob)
Definition: object.c:1324
#define FLAG_IS_TURNABLE
Definition: define.h:256