Crossfire Server, Trunk
object.cpp
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 #ifdef CF_MXE_CROSS_COMPILE
43 # define ffs(word) (__builtin_constant_p (word) \
44  ? __builtin_ffs (word) \
45  : ({ int __cnt, __tmp; \
46  __asm__ __volatile__ \
47  ("bsfl %2,%0\n\t" \
48  "cmovel %1,%0" \
49  : "=&r" (__cnt), "=r" (__tmp) \
50  : "rm" (word), "1" (-1)); \
51  __cnt + 1; }))
52 #endif
53 
54 static int compare_ob_value_lists_one(const object *, const object *);
55 static int compare_ob_value_lists(const object *, const object *);
56 static void permute(int *, int, int);
57 static int object_set_value_s(object *, const char *, const char *, int);
58 static void object_increase_nrof(object *op, uint32_t i);
59 
74 const char *const spell_mapping[SPELL_MAPPINGS] = {
75  "spell_magic_bullet", /* 0 */
76  "spell_small_fireball", /* 1 */
77  "spell_medium_fireball", /* 2 */
78  "spell_large_fireball", /* 3 */
79  "spell_burning_hands", /* 4 */
80  "spell_sm_lightning", /* 5 */
81  "spell_large_lightning", /* 6 */
82  "spell_magic_missile", /* 7 */
83  "spell_create_bomb", /* 8 */
84  "spell_summon_golem", /* 9 */
85  "spell_summon_fire_elemental", /* 10 */
86  "spell_summon_earth_elemental", /* 11 */
87  "spell_summon_water_elemental", /* 12 */
88  "spell_summon_air_elemental", /* 13 */
89  "spell_dimension_door", /* 14 */
90  "spell_create_earth_wall", /* 15 */
91  "spell_paralyze", /* 16 */
92  "spell_icestorm", /* 17 */
93  "spell_magic_mapping", /* 18 */
94  "spell_turn_undead", /* 19 */
95  "spell_fear", /* 20 */
96  "spell_poison_cloud", /* 21 */
97  "spell_wonder", /* 22 */
98  "spell_destruction", /* 23 */
99  "spell_perceive_self", /* 24 */
100  "spell_word_of_recall", /* 25 */
101  "spell_invisible", /* 26 */
102  "spell_invisible_to_undead", /* 27 */
103  "spell_probe", /* 28 */
104  "spell_lg_magic_bullet", /* 29 */
105  "spell_improved_invisibility", /* 30 */
106  "spell_holy_word", /* 31 */
107  "spell_minor_healing", /* 32 */
108  "spell_medium_healing", /* 33 */
109  "spell_major_healing", /* 34 */
110  "spell_heal", /* 35 */
111  "spell_create_food", /* 36 */
112  "spell_earth_to_dust", /* 37 */
113  "spell_armour", /* 38 */
114  "spell_strength", /* 39 */
115  "spell_dexterity", /* 40 */
116  "spell_constitution", /* 41 */
117  "spell_charisma", /* 42 */
118  "spell_create_fire_wall", /* 43 */
119  "spell_create_frost_wall", /* 44 */
120  "spell_protection_from_cold", /* 45 */
121  "spell_protection_from_electricity", /* 46 */
122  "spell_protection_from_fire", /* 47 */
123  "spell_protection_from_poison", /* 48 */
124  "spell_protection_from_slow", /* 49 */
125  "spell_protection_from_paralysis", /* 50 */
126  "spell_protection_from_draining", /* 51 */
127  "spell_protection_from_magic", /* 52 */
128  "spell_protection_from_attack", /* 53 */
129  "spell_levitate", /* 54 */
130  "spell_small_speedball", /* 55 */
131  "spell_large_speedball", /* 56 */
132  "spell_hellfire", /* 57 */
133  "spell_dragonbreath", /* 58 */
134  "spell_large_icestorm", /* 59 */
135  "spell_charging", /* 60 */
136  "spell_polymorph", /* 61 */
137  "spell_cancellation", /* 62 */
138  "spell_confusion", /* 63 */
139  "spell_mass_confusion", /* 64 */
140  "spell_summon_pet_monster", /* 65 */
141  "spell_slow", /* 66 */
142  "spell_regenerate_spellpoints", /* 67 */
143  "spell_cure_poison", /* 68 */
144  "spell_protection_from_confusion", /* 69 */
145  "spell_protection_from_cancellation", /* 70 */
146  "spell_protection_from_depletion", /* 71 */
147  "spell_alchemy", /* 72 */
148  "spell_remove_curse", /* 73 */
149  "spell_remove_damnation", /* 74 */
150  "spell_identify", /* 75 */
151  "spell_detect_magic", /* 76 */
152  "spell_detect_monster", /* 77 */
153  "spell_detect_evil", /* 78 */
154  "spell_detect_curse", /* 79 */
155  "spell_heroism", /* 80 */
156  "spell_aggravation", /* 81 */
157  "spell_firebolt", /* 82 */
158  "spell_frostbolt", /* 83 */
159  "spell_shockwave", /* 84 */
160  "spell_color_spray", /* 85 */
161  "spell_haste", /* 86 */
162  "spell_face_of_death", /* 87 */
163  "spell_ball_lightning", /* 88 */
164  "spell_meteor_swarm", /* 89 */
165  "spell_comet", /* 90 */
166  "spell_mystic_fist", /* 91 */
167  "spell_raise_dead", /* 92 */
168  "spell_resurrection", /* 93 */
169  "spell_reincarnation", /* 94 */
170  NULL, /* 95 spell_immunity_to_cold */
171  NULL, /* 96 spell_immunity_to_electricity */
172  NULL, /* 97 spell_immunity_to_fire */
173  NULL, /* 98 spell_immunity_to_poison */
174  NULL, /* 99 spell_immunity_to_slow */
175  NULL, /* 100 spell_immunity_to_paralysis */
176  NULL, /* 101 spell_immunity_to_draining */
177  NULL, /* 102 spell_immunity_to_magic */
178  NULL, /* 103 spell_immunity_to_attack */
179  "spell_invulnerability", /* 104 */
180  "spell_defense", /* 105 */
181  "spell_rune_of_fire", /* 106 */
182  "spell_rune_of_frost", /* 107 */
183  "spell_rune_of_shocking", /* 108 */
184  "spell_rune_of_blasting", /* 109 */
185  "spell_rune_of_death", /* 110 */
186  "spell_marking_rune", /* 111 */
187  "spell_build_director", /* 112 */
188  "spell_create_pool_of_chaos", /* 113 */
189  "spell_build_bullet_wall", /* 114 */
190  "spell_build_lightning_wall", /* 115 */
191  "spell_build_fireball_wall", /* 116 */
192  "spell_magic_rune", /* 117 */
193  "spell_rune_of_magic_drain", /* 118 */
194  "spell_antimagic_rune", /* 119 */
195  "spell_rune_of_transference", /* 120 */
196  "spell_transference", /* 121 */
197  "spell_magic_drain", /* 122 */
198  "spell_counterspell", /* 123 */
199  "spell_disarm", /* 124 */
200  "spell_cure_confusion", /* 125 */
201  "spell_restoration", /* 126 */
202  NULL, /* 127 */ /* Was summon evil monster */
203  "spell_counterwall", /* 128 */
204  "spell_cause_light_wounds", /* 129 */
205  "spell_cause_medium_wounds", /* 130 */
206  "spell_cause_heavy_wounds", /* 131 */
207  "spell_charm_monsters", /* 132 */
208  "spell_banishment", /* 133 */
209  "spell_create_missile", /* 134 */
210  "spell_show_invisible", /* 135 */
211  "spell_xray", /* 136 */
212  "spell_pacify", /* 137 */
213  "spell_summon_fog", /* 138 */
214  "spell_steambolt", /* 139 */
215  "spell_command_undead", /* 140 */
216  "spell_holy_orb", /* 141 */
217  "spell_summon_avatar", /* 142 */
218  "spell_holy_possession", /* 143 */
219  "spell_bless", /* 144 */
220  "spell_curse", /* 145 */
221  "spell_regeneration", /* 146 */
222  "spell_consecrate", /* 147 */
223  "spell_summon_cult_monsters", /* 148 */
224  "spell_cause_critical_wounds", /* 149 */
225  "spell_holy_wrath", /* 150 */
226  "spell_retributive_strike", /* 151 */
227  "spell_finger_of_death", /* 152 */
228  "spell_insect_plague", /* 153 */
229  "spell_call_holy_servant", /* 154 */
230  "spell_wall_of_thorns", /* 155 */
231  "spell_staff_to_snake", /* 156 */
232  "spell_light", /* 157 */
233  "spell_darkness", /* 158 */
234  "spell_nightfall", /* 159 */
235  "spell_daylight", /* 160 */
236  "spell_sunspear", /* 161 */
237  "spell_faery_fire", /* 162 */
238  "spell_cure_blindness", /* 163 */
239  "spell_dark_vision", /* 164 */
240  "spell_bullet_swarm", /* 165 */
241  "spell_bullet_storm", /* 166 */
242  "spell_cause_many_wounds", /* 167 */
243  "spell_small_snowstorm", /* 168 */
244  "spell_medium_snowstorm", /* 169 */
245  "spell_large_snowstorm", /* 170 */
246  "spell_cure_disease", /* 171 */
247  "spell_cause_red_death", /* 172 */
248  "spell_cause_flu", /* 173 */
249  "spell_cause_black_death", /* 174 */
250  "spell_cause_leprosy", /* 175 */
251  "spell_cause_smallpox", /* 176 */
252  "spell_cause_white_death", /* 177 */
253  "spell_cause_anthrax", /* 178 */
254  "spell_cause_typhoid", /* 179 */
255  "spell_mana_blast", /* 180 */
256  "spell_small_manaball", /* 181 */
257  "spell_medium_manaball", /* 182 */
258  "spell_large_manaball", /* 183 */
259  "spell_manabolt", /* 184 */
260  "spell_dancing_sword", /* 185 */
261  "spell_animate_weapon", /* 186 */
262  "spell_cause_cold", /* 187 */
263  "spell_divine_shock", /* 188 */
264  "spell_windstorm", /* 189 */
265  "spell_sanctuary", /* 190 */
266  "spell_peace", /* 191 */
267  "spell_spiderweb", /* 192 */
268  "spell_conflict", /* 193 */
269  "spell_rage", /* 194 */
270  "spell_forked_lightning", /* 195 */
271  "spell_poison_fog", /* 196 */
272  "spell_flaming_aura", /* 197 */
273  "spell_vitriol", /* 198 */
274  "spell_vitriol_splash", /* 199 */
275  "spell_iron_skin", /* 200 */
276  "spell_wrathful_eye", /* 201 */
277  "spell_town_portal", /* 202 */
278  "spell_missile_swarm", /* 203 */
279  "spell_cause_rabies", /* 204 */
280  "spell_glyph", /* 205 */
281 };
282 
283 #ifdef MEMORY_DEBUG
284 int nroffreeobjects = 0;
285 int nrofallocobjects = 0;
286 #undef OBJ_EXPAND
287 #define OBJ_EXPAND 1
288 #else
289 static object objarray[STARTMAX];
292 #endif
293 
294 object *objects;
295 static object *free_objects;
296 object *active_objects;
300  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,
301  0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
302 };
303 
306  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,
307  -3, -3, -3, -3, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3
308 };
309 
312  0, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 27, 30, 31, 32, 33, 36, 37, 39, 39, 42, 43, 44, 45,
313  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
314 };
315 
318  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,
319  1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
320 };
321 
327 void init_objects(void) {
328  /* Initialize all objects: */
329  objects = NULL;
330  active_objects = NULL;
331 
332 #ifdef MEMORY_DEBUG
333  free_objects = NULL;
334 #else
336  objarray[0].prev = NULL,
337  objarray[0].next = &objarray[1],
340  for (int i = 1; i < STARTMAX-1; i++) {
341  objarray[i].next = &objarray[i+1];
342  objarray[i].prev = &objarray[i-1];
345  }
346  objarray[STARTMAX-1].next = NULL;
350 #endif
351 }
352 
363 static int compare_ob_value_lists_one(const object *wants, const object *has) {
364  key_value *wants_field;
365 
366  /* n-squared behaviour (see object_get_key_value()), but I'm hoping both
367  * objects with lists are rare, and lists stay short. If not, use a
368  * different structure or at least keep the lists sorted...
369  */
370 
371  /* For each field in wants, */
372  for (wants_field = wants->key_values; wants_field != NULL; wants_field = wants_field->next) {
373  key_value *has_field;
374 
375  /* Look for a field in has with the same key. */
376  has_field = object_get_key_value(has, wants_field->key);
377 
378  if (has_field == NULL) {
379  /* No field with that name. */
380  return FALSE;
381  }
382 
383  /* Found the matching field. */
384  if (has_field->value != wants_field->value) {
385  /* Values don't match, so this half of the comparison is false. */
386  return FALSE;
387  }
388 
389  /* If we get here, we found a match. Now for the next field in wants. */
390  }
391 
392  /* If we get here, every field in wants has a matching field in has. */
393  return TRUE;
394 }
395 
404 static int compare_ob_value_lists(const object *ob1, const object *ob2) {
405  /* However, there may be fields in has which aren't partnered in wants,
406  * so we need to run the comparison *twice*. :(
407  */
409 }
410 
433 int object_can_merge(object *ob1, object *ob2) {
434  /* A couple quicksanity checks */
435  if (ob1 == ob2 || ob1->type != ob2->type)
436  return 0;
437 
438  if (ob1->speed != ob2->speed)
439  return 0;
440  /* Note sure why the following is the case - either the object has to
441  * be animated or have a very low speed. Is this an attempted monster
442  * check?
443  */
444  /*TODO is this check really needed?*/
445  if (!QUERY_FLAG(ob1, FLAG_ANIMATE) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED)
446  return 0;
447 
448  /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that
449  * value could not be stored in a int32_t (which unfortunately sometimes is
450  * used to store nrof).
451  */
452  if (ob1->nrof+ob2->nrof >= 1UL<<31)
453  return 0;
454 
455  /* This is really a spellbook check - really, we should
456  * check all objects in the inventory.
457  */
458  /*TODO is this check really needed?*/
459  if (ob1->inv || ob2->inv) {
460  /* if one object has inventory but the other doesn't, not equiv */
461  if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv))
462  return 0;
463 
464  /* Now check to see if the two inventory objects could merge */
465  if (!object_can_merge(ob1->inv, ob2->inv))
466  return 0;
467 
468  /* inventory ok - still need to check rest of this object to see
469  * if it is valid.
470  */
471  }
472 
473  /* If the objects have been identified, set the BEEN_APPLIED flag.
474  * This is to the comparison of the flags below will be OK. We
475  * just can't ignore the been applied or identified flags, as they
476  * are not equal - just if it has been identified, the been_applied
477  * flags lose any meaning.
478  */
479 
480  /*TODO is this hack on BEEN_APPLIED really needed? */
483 
486 
487 
488  /* Note: FLAG_INV_LOCKED is ignored for merging purposes */
489  if ((ob1->arch != ob2->arch)
490  || (ob1->flags[0] != ob2->flags[0])
491  || (ob1->flags[1] != ob2->flags[1])
492  || (ob1->flags[2] != ob2->flags[2])
493  || ((ob1->flags[3]&~0x84) != (ob2->flags[3]&~0x84)) /* ignore CLIENT_SENT and FLAG_OBJ_ORIGINAL */
494  || (ob1->name != ob2->name)
495  || (ob1->title != ob2->title)
496  || (ob1->msg != ob2->msg)
497  || (ob1->weight != ob2->weight)
498  || (ob1->item_power != ob2->item_power)
499  || (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist)) != 0)
500  || (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats)) != 0)
501  || (ob1->attacktype != ob2->attacktype)
502  || (ob1->magic != ob2->magic)
503  || (ob1->slaying != ob2->slaying)
504  || (ob1->skill != ob2->skill)
505  || (ob1->value != ob2->value)
506  || (ob1->animation != ob2->animation)
507  || (ob1->client_type != ob2->client_type)
508  || (ob1->materialname != ob2->materialname)
509  || (ob1->lore != ob2->lore)
510  || (ob1->subtype != ob2->subtype)
511  || (ob1->move_type != ob2->move_type)
512  || (ob1->move_block != ob2->move_block)
513  || (ob1->move_allow != ob2->move_allow)
514  || (ob1->move_on != ob2->move_on)
515  || (ob1->move_off != ob2->move_off)
516  || (ob1->move_slow != ob2->move_slow)
517  || (ob1->move_slow_penalty != ob2->move_slow_penalty)
518  || (ob1->map_layer != ob2->map_layer))
519  return 0;
520 
521  /* Don't merge objects that are applied. With the new 'body' code,
522  * it is possible for most any character to have more than one of
523  * some items equipped, and we don't want those to merge.
524  */
526  return 0;
527 
528  if (ob1->key_values != NULL || ob2->key_values != NULL) {
529  /* At least one of these has key_values. */
530  if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) {
531  /* One has fields, but the other one doesn't. */
532  return 0;
533  } else {
534  if (!compare_ob_value_lists(ob1, ob2)) {
535  return 0;
536  }
537  }
538  }
539 
540  /*TODO should this really be limited to scrolls?*/
541  switch (ob1->type) {
542  case SCROLL:
543  if (ob1->level != ob2->level)
544  return 0;
545  break;
546  }
547 
548  /* Everything passes, must be OK. */
549  return 1;
550 }
551 
567 /* TODO should check call this this are made a place where we really need reevaluaton of whole tree */
568 signed long object_sum_weight(object *op) {
569  signed long sum;
570 
571  sum = 0;
573  if (inv->inv)
575  sum += inv->carrying+inv->weight*NROF(inv);
576  } FOR_INV_FINISH();
577  if (op->type == CONTAINER && op->stats.Str)
578  sum = (sum*(100-op->stats.Str))/100;
579  op->carrying = sum;
580  return sum;
581 }
582 
590 object *object_get_env_recursive(object *op) {
591  while (op->env != NULL)
592  op = op->env;
593  return op;
594 }
595 
607 object *object_get_player_container(object *op) {
608  for (; op != NULL && op->type != PLAYER; op = op->env)
609  /*TODO this is patching the structure on the flight as side effect. Shoudln't be needed in clean code */
610  if (op->env == op)
611  op->env = NULL;
612  return op;
613 }
614 
624 static const object *object_get_owner_const(const object *op) {
625  if (op->owner == NULL)
626  return NULL;
627 
628  if (!QUERY_FLAG(op->owner, FLAG_FREED)
629  && !QUERY_FLAG(op->owner, FLAG_REMOVED)
630  && op->owner->count == op->ownercount)
631  return op->owner;
632 
633  LOG(llevError, "Warning, no owner found\n");
634  return NULL;
635 }
636 
645 void object_dump(const object *op, StringBuffer *sb) {
646  if (op == NULL) {
647  stringbuffer_append_string(sb, "[NULL pointer]");
648  return;
649  }
650 
651  /* object *tmp;*/
652 
653  if (op->arch != NULL) {
654  const object *owner;
655 
656  stringbuffer_append_string(sb, "arch ");
657  stringbuffer_append_string(sb, op->arch->name ? op->arch->name : "(null)");
658  stringbuffer_append_string(sb, "\n");
659 
660  if (op->artifact != NULL) {
661  stringbuffer_append_string(sb, "artifact ");
662  stringbuffer_append_string(sb, op->artifact);
663  stringbuffer_append_string(sb, "\n");
664  }
665 
667  if (op->more) {
668  stringbuffer_append_printf(sb, "more %u\n", op->more->count);
669  }
670  if (op->head) {
671  stringbuffer_append_printf(sb, "head %u\n", op->head->count);
672  }
673  if (op->env) {
674  stringbuffer_append_printf(sb, "env %u\n", op->env->count);
675  }
676  if (op->inv) {
677  stringbuffer_append_printf(sb, "inv %u\n", op->inv->count);
678  }
679  if (op->enemy) {
680  stringbuffer_append_printf(sb, "enemy %u\n", op->enemy->count);
681  }
682  if (op->attacked_by) {
683  stringbuffer_append_printf(sb, "attacked_by %u\n", op->attacked_by->count);
684  }
685  owner = object_get_owner_const(op);
686  if (owner != NULL) {
687  stringbuffer_append_printf(sb, "owner %u\n", owner->count);
688  }
689  stringbuffer_append_string(sb, "end\n");
690  } else {
691  stringbuffer_append_string(sb, "Object ");
692  stringbuffer_append_string(sb, op->name == NULL ? "(null)" : op->name);
693  stringbuffer_append_string(sb, "\nend\n");
694  }
695 }
696 
704 void object_dump_all(void) {
705  object *op;
706 
707  for (op = objects; op != NULL; op = op->next) {
708  StringBuffer *sb;
709  char *diff;
710 
711  sb = stringbuffer_new();
712  object_dump(op, sb);
713  diff = stringbuffer_finish(sb);
714  LOG(llevDebug, "Object %u\n:%s\n", op->count, diff);
715  free(diff);
716  }
717 }
718 
728  object *op;
729 
730  for (op = objects; op != NULL; op = op->next)
731  if (op->count == i)
732  break;
733  return op;
734 }
735 
747 object *object_find_by_name_global(const char *str) {
748  const char *name = add_string(str);
749  object *op;
750 
751  for (op = objects; op != NULL; op = op->next)
752  if (op->name == name)
753  break;
754  free_string(name);
755  return op;
756 }
757 
768 #ifdef MEMORY_DEBUG
769  object *op, *next;
770 
771  for (op = free_objects; op != NULL; ) {
772  next = op->next;
773  free(op);
775  nroffreeobjects--;
776  op = next;
777  }
778  free_objects = NULL;
779 
780  for (op = objects; op != NULL; ) {
781  next = op->next;
782  if (!QUERY_FLAG(op, FLAG_FREED)) {
783  LOG(llevDebug, "non freed object: %s\n", op->name);
784  }
785  op = next;
786  }
787 #endif
788 
789  LOG(llevDebug, "%d allocated objects, %d free objects, STARMAX=%d\n", nrofallocobjects, nroffreeobjects, STARTMAX);
790 }
791 
804 object *object_get_owner(object *op) {
805  if (op->owner == NULL)
806  return NULL;
807 
808  if (!QUERY_FLAG(op->owner, FLAG_FREED)
809  && !QUERY_FLAG(op->owner, FLAG_REMOVED)
810  && op->owner->count == op->ownercount)
811  return op->owner;
812 
814  return NULL;
815 }
816 
823 void object_clear_owner(object *op) {
824  if (!op)
825  return;
826 
827  op->owner = NULL;
828  op->ownercount = 0;
829 }
830 
840 void object_set_owner(object *op, object *owner) {
841  /* Assign temp to something, so it can't accidentally be NULL */
842  object *tmp = owner;
843  if (op == NULL)
844  return;
845  if (owner == NULL) {
847  return;
848  }
849 
850  /* next line added to allow objects which own objects */
851  /* Add a check for ownercounts in here, as I got into an endless loop
852  * with the fireball owning a poison cloud which then owned the
853  * fireball. I believe that was caused by one of the objects getting
854  * freed and then another object replacing it. Since the ownercounts
855  * didn't match, this check is valid and I believe that cause is valid.
856  */
857  /*
858  * if owner is NULL, function will have already returned,
859  * so loop should still function as before.
860  */
861  while (tmp) {
862  tmp = object_get_owner(owner);
863  if (tmp)
864  owner = tmp;
865  }
866 
867  /* must not cause owner cycles */
868  assert(op != owner);
869 
870  if (op->owner != NULL)
872 
873  op->owner = owner;
874  op->ownercount = owner->count;
875 }
876 
893 void object_copy_owner(object *op, object *clone) {
894  object *owner = object_get_owner(clone);
895  if (owner == NULL) {
896  /* players don't have owners - they own themselves. Update
897  * as appropriate.
898  */
899  /*TODO owner=self is dangerous and should be avoided*/
900  if (clone->type != PLAYER)
901  return;
902  owner = clone;
903  }
904  object_set_owner(op, owner);
905 }
906 
915 void object_set_enemy(object *op, object *enemy) {
916  if (op->enemy == enemy) {
917  return;
918  }
919 
920 #if 0
921  if (op->type != PLAYER) {
922  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);
923  }
924 #endif
925  op->enemy = enemy;
926 }
927 
934 void object_reset(object *op) {
935  op->name = NULL;
936  op->name_pl = NULL;
937  op->title = NULL;
938  op->race = NULL;
939  op->slaying = NULL;
940  op->skill = NULL;
941  op->msg = NULL;
942  op->materialname = NULL;
943  op->lore = NULL;
944  object_clear(op);
945 }
946 
954 void object_free_key_values(object *op) {
955  key_value *i;
956  key_value *next = NULL;
957 
958  if (op->key_values == NULL)
959  return;
960 
961  for (i = op->key_values; i != NULL; i = next) {
962  /* Store next *first*. */
963  next = i->next;
964 
965  if (i->key)
967  if (i->value)
969  i->next = NULL;
970  free(i);
971  }
972 
973  op->key_values = NULL;
974 }
975 
983 void object_clear(object *op) {
984  /*TODO this comment must be investigated*/
985  /* redo this to be simpler/more efficient. Was also seeing
986  * crashes in the old code. Move this to the top - am
987  * seeing periodic crashes in this code, and would like to have
988  * as much info available as possible (eg, object name).
989  */
992 
993  /* the memset will clear all these values for us, but we need
994  * to reduce the refcount on them.
995  */
996  if (op->name != NULL)
997  FREE_AND_CLEAR_STR(op->name);
998  if (op->name_pl != NULL)
999  FREE_AND_CLEAR_STR(op->name_pl);
1000  if (op->title != NULL)
1001  FREE_AND_CLEAR_STR(op->title);
1002  if (op->race != NULL)
1003  FREE_AND_CLEAR_STR(op->race);
1004  if (op->slaying != NULL)
1005  FREE_AND_CLEAR_STR(op->slaying);
1006  if (op->skill != NULL)
1007  FREE_AND_CLEAR_STR(op->skill);
1008  if (op->msg != NULL)
1009  FREE_AND_CLEAR_STR(op->msg);
1010  if (op->lore != NULL)
1011  FREE_AND_CLEAR_STR(op->lore);
1012  if (op->materialname != NULL)
1013  FREE_AND_CLEAR_STR(op->materialname);
1014 
1015  /* Remove object from friendly list if needed. */
1016  if (QUERY_FLAG(op, FLAG_FRIENDLY))
1018 
1019  memset((void *)((char *)op+offsetof(object, name)), 0, sizeof(object)-offsetof(object, name));
1020  /* Below here, we clear things that are not done by the memset,
1021  * or set default values that are not zero.
1022  */
1023  /* This is more or less true */
1025 
1026 
1027  op->contr = NULL;
1028  op->below = NULL;
1029  op->above = NULL;
1030  op->inv = NULL;
1031  op->container = NULL;
1032  op->env = NULL;
1033  op->more = NULL;
1034  op->head = NULL;
1035  op->map = NULL;
1036  op->active_next = NULL;
1037  op->active_prev = NULL;
1038  /* What is not cleared is next, prev, and count */
1039 
1040  op->expmul = 1.0;
1041  op->face = blank_face;
1042  op->attacked_by_count = -1;
1043  if (settings.casting_time)
1044  op->casting_time = -1;
1045 }
1046 
1061 void object_copy_no_speed(const object *src_ob, object *dest_ob) {
1062  int is_freed = QUERY_FLAG(dest_ob, FLAG_FREED), is_removed = QUERY_FLAG(dest_ob, FLAG_REMOVED);
1063 
1064  /* Decrement the refcounts, but don't bother zeroing the fields;
1065  they'll be overwritten by memcpy. */
1066  if (dest_ob->artifact != NULL)
1067  free_string(dest_ob->artifact);
1068  if (dest_ob->name != NULL)
1069  free_string(dest_ob->name);
1070  if (dest_ob->name_pl != NULL)
1071  free_string(dest_ob->name_pl);
1072  if (dest_ob->anim_suffix != NULL)
1073  free_string(dest_ob->anim_suffix);
1074  if (dest_ob->title != NULL)
1075  free_string(dest_ob->title);
1076  if (dest_ob->race != NULL)
1077  free_string(dest_ob->race);
1078  if (dest_ob->slaying != NULL)
1079  free_string(dest_ob->slaying);
1080  if (dest_ob->skill != NULL)
1081  free_string(dest_ob->skill);
1082  if (dest_ob->msg != NULL)
1083  free_string(dest_ob->msg);
1084  if (dest_ob->lore != NULL)
1085  free_string(dest_ob->lore);
1086  if (dest_ob->materialname != NULL)
1087  free_string(dest_ob->materialname);
1088  if (dest_ob->spell_tags != NULL)
1089  FREE_AND_CLEAR(dest_ob->spell_tags);
1090 
1091  /* Basically, same code as from object_clear() */
1092 
1093  object_free_key_values(dest_ob);
1094  free_dialog_information(dest_ob);
1095 
1096  /* Copy all attributes below name (name included). */
1097  (void)memcpy((void *)((char *)dest_ob+offsetof(object, name)),
1098  (void *)((char *)src_ob+offsetof(object, name)),
1099  sizeof(object)-offsetof(object, name));
1100 
1101  if (is_freed)
1102  SET_FLAG(dest_ob, FLAG_FREED);
1103  if (is_removed)
1104  SET_FLAG(dest_ob, FLAG_REMOVED);
1105  if (dest_ob->artifact != NULL)
1106  add_refcount(dest_ob->artifact);
1107  if (dest_ob->name != NULL)
1108  add_refcount(dest_ob->name);
1109  if (dest_ob->name_pl != NULL)
1110  add_refcount(dest_ob->name_pl);
1111  if (dest_ob->anim_suffix != NULL)
1112  add_refcount(dest_ob->anim_suffix);
1113  if (dest_ob->title != NULL)
1114  add_refcount(dest_ob->title);
1115  if (dest_ob->race != NULL)
1116  add_refcount(dest_ob->race);
1117  if (dest_ob->slaying != NULL)
1118  add_refcount(dest_ob->slaying);
1119  if (dest_ob->skill != NULL)
1120  add_refcount(dest_ob->skill);
1121  if (dest_ob->lore != NULL)
1122  add_refcount(dest_ob->lore);
1123  if (dest_ob->msg != NULL)
1124  add_refcount(dest_ob->msg);
1125  if (dest_ob->materialname != NULL)
1126  add_refcount(dest_ob->materialname);
1127 
1128  if (dest_ob->spell_tags != NULL) {
1129  dest_ob->spell_tags = static_cast<tag_t *>(malloc(sizeof(tag_t)*SPELL_TAG_SIZE));
1130  memcpy(dest_ob->spell_tags, src_ob->spell_tags, sizeof(tag_t)*SPELL_TAG_SIZE);
1131  }
1132 
1133  /* If archetype is a temporary one, we need to update reference count, because
1134  * that archetype will be freed by object_free_drop_inventory() when the last object is removed.
1135  */
1136  if (dest_ob->arch != NULL) {
1137  if (dest_ob->arch->reference_count > 0)
1138  dest_ob->arch->reference_count++;
1139  }
1140 
1141  if (src_ob->speed < 0)
1142  dest_ob->speed_left = src_ob->speed_left-RANDOM()%200/100.0;
1143 
1144  /* Copy over key_values, if any. */
1145  if (src_ob->key_values != NULL) {
1146  key_value *tail = NULL;
1147  key_value *i;
1148 
1149  dest_ob->key_values = NULL;
1150 
1151  for (i = src_ob->key_values; i != NULL; i = i->next) {
1152  key_value *new_link = static_cast<key_value *>(malloc(sizeof(key_value)));
1153 
1154  new_link->next = NULL;
1155  new_link->key = add_refcount(i->key);
1156  if (i->value)
1157  new_link->value = add_refcount(i->value);
1158  else
1159  new_link->value = NULL;
1160 
1161  /* Try and be clever here, too. */
1162  if (dest_ob->key_values == NULL) {
1163  dest_ob->key_values = new_link;
1164  tail = new_link;
1165  } else {
1166  tail->next = new_link;
1167  tail = new_link;
1168  }
1169  }
1170  }
1171 
1172  /* This way, dialog information will be parsed again when/if needed. */
1173  CLEAR_FLAG(dest_ob, FLAG_DIALOG_PARSED);
1174 
1175  dest_ob->event_bitmask = BITMASK_VALID; // Empty inventory so valid
1176 }
1177 
1192 void object_copy(const object *src_ob, object *dest_ob) {
1193  object_copy_no_speed(src_ob, dest_ob);
1194  object_update_speed(dest_ob);
1195 }
1196 
1208 void object_copy_with_inv(const object *src_ob, object *dest_ob, bool update_speed) {
1209  if (update_speed) {
1210  object_copy(src_ob, dest_ob);
1211  } else {
1212  object_copy_no_speed(src_ob, dest_ob);
1213  }
1214  FOR_INV_PREPARE(src_ob, walk) {
1215  object *tmp;
1216 
1217  tmp = object_new();
1218  object_copy_with_inv(walk, tmp, update_speed);
1219  object_insert_in_ob(tmp, dest_ob);
1220  } FOR_INV_FINISH();
1221 }
1222 
1223 #ifndef MEMORY_DEBUG
1224 
1231 static void expand_objects(void) {
1232  int i;
1233  object *add;
1234 
1235  add = (object *)CALLOC(OBJ_EXPAND, sizeof(object));
1236 
1237  if (add == NULL)
1239  free_objects = add;
1240  add[0].prev = NULL;
1241  add[0].next = &add[1],
1242  SET_FLAG(&add[0], FLAG_REMOVED);
1243  SET_FLAG(&add[0], FLAG_FREED);
1244 
1245  for (i = 1; i < OBJ_EXPAND-1; i++) {
1246  add[i].next = &add[i+1],
1247  add[i].prev = &add[i-1],
1248  SET_FLAG(&add[i], FLAG_REMOVED);
1249  SET_FLAG(&add[i], FLAG_FREED);
1250  }
1251  add[OBJ_EXPAND-1].prev = &add[OBJ_EXPAND-2],
1252  add[OBJ_EXPAND-1].next = NULL,
1253  SET_FLAG(&add[OBJ_EXPAND-1], FLAG_REMOVED);
1254  SET_FLAG(&add[OBJ_EXPAND-1], FLAG_FREED);
1255 
1258 }
1259 #endif
1260 
1273 object *object_new(void) {
1274  object *op;
1275 #ifdef MEMORY_DEBUG
1276  /* FIXME: However this doesn't work since object_free() sometimes add
1277  * objects back to the free_objects linked list, and some functions mess
1278  * with the object after return of object_free(). This is bad and should be
1279  * fixed. But it would need fairly extensive changes and a lot of debugging.
1280  */
1281  op = static_cast<object *>(calloc(1, sizeof(object)));
1282  if (op == NULL)
1284 #else
1285  if (free_objects == NULL) {
1286  expand_objects();
1287  }
1288  op = free_objects;
1289  if (!QUERY_FLAG(op, FLAG_FREED)) {
1290  LOG(llevError, "Fatal: Getting busy object.\n");
1291 #ifdef MANY_CORES
1292  abort();
1293 #endif
1294  }
1295  free_objects = op->next;
1296  if (free_objects != NULL)
1297  free_objects->prev = NULL;
1298  nroffreeobjects--;
1299 #endif
1300  op->count = ++ob_count;
1301  op->name = NULL;
1302  op->name_pl = NULL;
1303  op->title = NULL;
1304  op->race = NULL;
1305  op->slaying = NULL;
1306  op->skill = NULL;
1307  op->lore = NULL;
1308  op->msg = NULL;
1309  op->materialname = NULL;
1310  op->next = objects;
1311  op->prev = NULL;
1312  op->active_next = NULL;
1313  op->active_prev = NULL;
1314  op->spell_tags = NULL;
1315  op->event_bitmask = BITMASK_VALID;
1316  if (objects != NULL)
1317  objects->prev = op;
1318  objects = op;
1319  object_clear(op);
1321  return op;
1322 }
1323 
1333  if (op->animation == 0 || !QUERY_FLAG(op, FLAG_IS_TURNABLE))
1334  return;
1335  animate_object(op, op->direction);
1336 }
1337 
1349 void object_update_speed(object *op) {
1350 
1351  /* No reason putting the archetypes objects on the speed list,
1352  * since they never really need to be updated.
1353  */
1354 
1355  if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
1356  LOG(llevError, "Object %s is freed but has speed.\n", op->name);
1357 #ifdef MANY_CORES
1358  abort();
1359 #else
1360  op->speed = 0;
1361 #endif
1362  }
1363  if (FABS(op->speed) > MIN_ACTIVE_SPEED) {
1364  /* If already on active list, don't do anything */
1365  /* TODO this check can probably be simplified a lot */
1366  if (op->active_next || op->active_prev || op == active_objects)
1367  return;
1368 
1369  /* process_events() expects us to insert the object at the beginning
1370  * of the list. */
1371  op->active_next = active_objects;
1372  if (op->active_next != NULL)
1373  op->active_next->active_prev = op;
1374  active_objects = op;
1375  } else {
1377  }
1378 }
1379 
1393  /* If not on the active list, nothing needs to be done */
1394  if (!op->active_next && !op->active_prev && op != active_objects)
1395  return;
1396 
1397  if (op->active_prev == NULL) {
1398  active_objects = op->active_next;
1399  if (op->active_next != NULL)
1400  op->active_next->active_prev = NULL;
1401  } else {
1402  op->active_prev->active_next = op->active_next;
1403  if (op->active_next)
1404  op->active_next->active_prev = op->active_prev;
1405  }
1406  op->active_next = NULL;
1407  op->active_prev = NULL;
1408 }
1409 
1434 void object_update(object *op, int action) {
1435  int update_now = 0, flags;
1436  MoveType move_on, move_off, move_block, move_slow;
1437  object *pl;
1438 
1439  if (op == NULL) {
1440  /* this should never happen */
1441  LOG(llevDebug, "object_update() called for NULL object.\n");
1442  return;
1443  }
1444 
1445  if (op->env != NULL) {
1446  /* Animation is currently handled by client, so nothing
1447  * to do in this case.
1448  */
1449  return;
1450  }
1451 
1452  /* If the map is saving, don't do anything as everything is
1453  * going to get freed anyways.
1454  */
1455  if (!op->map || op->map->in_memory == MAP_SAVING)
1456  return;
1457 
1458  /* make sure the object is within map boundaries */
1459  if (op->x < 0 || op->x >= MAP_WIDTH(op->map)
1460  || op->y < 0 || op->y >= MAP_HEIGHT(op->map)) {
1461  LOG(llevError, "object_update() called for object out of map!\n");
1462 #ifdef MANY_CORES
1463  abort();
1464 #endif
1465  return;
1466  }
1467 
1468  flags = GET_MAP_FLAGS(op->map, op->x, op->y);
1469  SET_MAP_FLAGS(op->map, op->x, op->y, flags|P_NEED_UPDATE);
1470  move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
1471  move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
1472  move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
1473  move_off = GET_MAP_MOVE_OFF(op->map, op->x, op->y);
1474 
1475  if (action == UP_OBJ_INSERT) {
1477  update_now = 1;
1478 
1480  update_now = 1;
1481 
1483  update_now = 1;
1484 
1485  if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags&P_IS_ALIVE))
1486  update_now = 1;
1487 
1488  if ((move_on|op->move_on) != move_on)
1489  update_now = 1;
1490  if ((move_off|op->move_off) != move_off)
1491  update_now = 1;
1492  /* This isn't perfect, but I don't expect a lot of objects to
1493  * to have move_allow right now.
1494  */
1495  if (((move_block|op->move_block)&~op->move_allow) != move_block)
1496  update_now = 1;
1497  if ((move_slow|op->move_slow) != move_slow)
1498  update_now = 1;
1499 
1500  if (op->type == PLAYER)
1501  update_now = 1;
1502  /* if the object is being removed, we can't make intelligent
1503  * decisions, because object_remove() can't really pass the object
1504  * that is being removed.
1505  */
1506  } else if (action == UP_OBJ_REMOVE) {
1507  update_now = 1;
1508  } else if (action == UP_OBJ_FACE || action == UP_OBJ_CHANGE) {
1509  /* In addition to sending info to client, need to update space
1510  * information.
1511  */
1512  if (action == UP_OBJ_CHANGE)
1513  update_now = 1;
1514 
1515  /* There is a player on this space - we may need to send an
1516  * update to the client.
1517  * If this object is supposed to be animated by the client,
1518  * nothing to do here - let the client animate it.
1519  * We can't use FLAG_ANIMATE, as that is basically set for
1520  * all objects with multiple faces, regardless if they are animated.
1521  * (levers have it set for example).
1522  */
1523  if (flags&P_PLAYER
1526  pl = GET_MAP_PLAYER(op->map, op->x, op->y);
1527 
1528  /* If update_look is set, we're going to send this entire space
1529  * to the client, so no reason to send face information now.
1530  */
1531  if (!pl->contr->socket->update_look) {
1533  }
1534  }
1535  } else {
1536  LOG(llevError, "object_update called with invalid action: %d\n", action);
1537  }
1538 
1539  if (update_now) {
1541  update_position(op->map, op->x, op->y);
1542  }
1543 
1544  if (op->more != NULL)
1545  object_update(op->more, action);
1546 }
1547 
1561  object_free(ob, 0);
1562 }
1563 
1568 void object_free_inventory(object *ob) {
1569  while (ob->inv) {
1570  object *inv = ob->inv;
1571  object_remove(inv);
1573  }
1574 }
1575 
1592 void object_free(object *ob, int flags) {
1593  if (!QUERY_FLAG(ob, FLAG_REMOVED)) {
1594  StringBuffer *sb;
1595  char *diff;
1596 
1597  LOG(llevError, "Free object called with non removed object\n");
1598  sb = stringbuffer_new();
1599  object_dump(ob, sb);
1600  diff = stringbuffer_finish(sb);
1601  LOG(llevError, "%s", diff);
1602  free(diff);
1603 #ifdef MANY_CORES
1604  abort();
1605 #endif
1606  }
1607  if (QUERY_FLAG(ob, FLAG_FRIENDLY)) {
1608  LOG(llevMonster, "Warning: tried to free friendly object.\n");
1610  }
1611  if (QUERY_FLAG(ob, FLAG_FREED)) {
1612  StringBuffer *sb;
1613  char *diff;
1614 
1615  sb = stringbuffer_new();
1616  object_dump(ob, sb);
1617  diff = stringbuffer_finish(sb);
1618  LOG(llevError, "Trying to free freed object.\n%s\n", diff);
1619  free(diff);
1620  return;
1621  }
1622 
1623  if ((flags & FREE_OBJ_NO_DESTROY_CALLBACK) == 0) {
1625  }
1626 
1627  if (ob->inv) {
1628  /* Only if the space blocks everything do we not process -
1629  * if some form of movemnt is allowed, let objects
1630  * drop on that space.
1631  */
1632  if ((flags & FREE_OBJ_FREE_INVENTORY) != 0
1633  || ob->map == NULL
1634  || ob->map->in_memory != MAP_IN_MEMORY
1635  || (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) {
1636  FOR_INV_PREPARE(ob, op) {
1637  object_remove(op);
1638  object_free(op, flags);
1639  } FOR_INV_FINISH();
1640  } else { /* Put objects in inventory onto this space */
1641  FOR_INV_PREPARE(ob, op) {
1642  object_remove(op);
1643  /* No drop means no drop, including its inventory */
1644  if (QUERY_FLAG(op, FLAG_NO_DROP))
1646  else if (QUERY_FLAG(op, FLAG_STARTEQUIP)
1648  || op->type == RUNE
1649  || op->type == TRAP
1652  else {
1653  object *part;
1654 
1655  /* If it's a multi-tile object, scatter dropped items randomly */
1656  if (ob->more) {
1657  int partcount = 0;
1658  /* Get the number of non-head parts */
1659  for (part = ob; part; part = part->more) {
1660  partcount++;
1661  }
1662  /* Select a random part */
1663  partcount = RANDOM()%partcount;
1664  for (part = ob; partcount > 0; partcount--) {
1665  part = part->more;
1666  }
1667  } else {
1668  part = ob;
1669  }
1670 
1671  if (QUERY_FLAG(op, FLAG_ALIVE)) {
1672  object_insert_to_free_spot_or_free(op, part->map, part->x, part->y, 0, SIZEOFFREE, NULL);
1673  } else {
1674  int f = 0;
1677  object_insert_in_map_at(op, part->map, NULL, f, part->x, part->y); /* Insert in same map as the envir */
1678  }
1679  }
1680  } FOR_INV_FINISH();
1681  }
1682  }
1683 
1684  if (ob->more != NULL) {
1685  object_free(ob->more, flags);
1686  ob->more = NULL;
1687  }
1688 
1689  /* Remove object from the active list */
1690  ob->speed = 0;
1692 
1694  ob->count = 0;
1695 
1696  /* Remove this object from the list of used objects */
1697  if (ob->prev == NULL) {
1698  objects = ob->next;
1699  if (objects != NULL)
1700  objects->prev = NULL;
1701  } else {
1702  ob->prev->next = ob->next;
1703  if (ob->next != NULL)
1704  ob->next->prev = ob->prev;
1705  }
1706 
1707  if (ob->artifact != NULL) FREE_AND_CLEAR_STR(ob->artifact);
1708  if (ob->name != NULL) FREE_AND_CLEAR_STR(ob->name);
1709  if (ob->name_pl != NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1710  if (ob->title != NULL) FREE_AND_CLEAR_STR(ob->title);
1711  if (ob->race != NULL) FREE_AND_CLEAR_STR(ob->race);
1712  if (ob->slaying != NULL) FREE_AND_CLEAR_STR(ob->slaying);
1713  if (ob->skill != NULL) FREE_AND_CLEAR_STR(ob->skill);
1714  if (ob->lore != NULL) FREE_AND_CLEAR_STR(ob->lore);
1715  if (ob->msg != NULL) FREE_AND_CLEAR_STR(ob->msg);
1716  if (ob->materialname != NULL) FREE_AND_CLEAR_STR(ob->materialname);
1717  if (ob->spell_tags) FREE_AND_CLEAR(ob->spell_tags);
1718  FREE_AND_CLEAR_STR_IF(ob->anim_suffix);
1719 
1720  /* Why aren't events freed? */
1722 
1724 
1725  /* Test whether archetype is a temporary one, and if so look whether it should be trashed. */
1726  if (ob->arch && ob->arch->reference_count > 0) {
1727  if (--ob->arch->reference_count == 0) {
1728  free_arch(ob->arch);
1729  }
1730  }
1731 
1732 #ifdef MEMORY_DEBUG
1733  free(ob);
1734 #else
1735  /* Now link it with the free_objects list: */
1736  ob->prev = NULL;
1737  ob->next = free_objects;
1738  if (free_objects != NULL)
1739  free_objects->prev = ob;
1740  free_objects = ob;
1741  nroffreeobjects++;
1742 #endif
1743 }
1744 
1752  int i = 0;
1753  object *tmp = free_objects;
1754 
1755  while (tmp != NULL)
1756  tmp = tmp->next,
1757  i++;
1758  return i;
1759 }
1760 
1768  int i = 0;
1769  object *tmp = objects;
1770 
1771  while (tmp != NULL)
1772  tmp = tmp->next,
1773  i++;
1774  return i;
1775 }
1776 
1784  int i = 0;
1785  object *tmp = active_objects;
1786 
1787  while (tmp != NULL)
1788  tmp = tmp->active_next,
1789  i++;
1790  return i;
1791 }
1792 
1807 void object_sub_weight(object *op, signed long weight) {
1808  while (op != NULL) {
1809  if (op->type == CONTAINER) {
1810  weight = (signed long)(weight*(100-op->stats.Str)/100);
1811  }
1812  op->carrying -= weight;
1813  op = op->env;
1814  }
1815 }
1816 
1833 void object_remove(object *op) {
1834  object *last = NULL;
1835  object *otmp;
1836  tag_t tag;
1837  int check_walk_off;
1838  mapstruct *m;
1839  int16_t x, y;
1840 
1841  if (QUERY_FLAG(op, FLAG_REMOVED)) {
1842  StringBuffer *sb;
1843  char *diff;
1844 
1845  sb = stringbuffer_new();
1846  object_dump(op, sb);
1847  diff = stringbuffer_finish(sb);
1848  LOG(llevError, "Trying to remove removed object.\n%s\n", diff);
1849  free(diff);
1850  abort();
1851  }
1852  if (op->more != NULL)
1853  object_remove(op->more);
1854 
1856 
1857  /*
1858  * In this case, the object to be removed is in someones
1859  * inventory.
1860  */
1861  /* TODO try to call a generic inventory weight adjusting function like object_sub_weight */
1862  if (op->env != NULL) {
1863  player *pl = NULL;
1864 
1865  if (op->nrof)
1866  object_sub_weight(op->env, op->weight*op->nrof);
1867  else
1868  object_sub_weight(op->env, op->weight+op->carrying);
1869 
1870  /* Update in two cases: item is in a player, or in a container the player is looking into. */
1871  if (op->env->contr != NULL && op->head == NULL) {
1872  pl = op->env->contr;
1873  } else if (op->env->type == CONTAINER && QUERY_FLAG(op->env, FLAG_APPLIED)) {
1874 
1875  if (op->env->env && op->env->env->contr)
1876  /* Container is in player's inventory. */
1877  pl = op->env->env->contr;
1878  else if (op->env->map) {
1879  /* Container on map, look above for player. */
1880  object *above = op->env->above;
1881 
1882  while (above && !above->contr)
1883  above = above->above;
1884  if (above)
1885  pl = above->contr;
1886  }
1887  }
1888 
1889  /* NO_FIX_PLAYER is set when a great many changes are being
1890  * made to players inventory. If set, avoiding the call
1891  * to save cpu time.
1892  */
1893  otmp = object_get_player_container(op->env);
1894  if (otmp != NULL
1895  && otmp->contr
1896  && !QUERY_FLAG(otmp, FLAG_NO_FIX_PLAYER))
1897  fix_object(otmp);
1898 
1899  if (op->above != NULL)
1900  op->above->below = op->below;
1901  else
1902  op->env->inv = op->below;
1903 
1904  if (op->below != NULL)
1905  op->below->above = op->above;
1906 
1907  if (op->type == EVENT_CONNECTOR) {
1908  op->env->event_bitmask = 0; // Will be recomputed if needed
1909  }
1910 
1911  /* we set up values so that it could be inserted into
1912  * the map, but we don't actually do that - it is up
1913  * to the caller to decide what we want to do.
1914  */
1915  op->x = op->env->x;
1916  op->y = op->env->y;
1917  op->ox = op->x;
1918  op->oy = op->y;
1919  op->map = op->env->map;
1920  op->above = NULL;
1921  op->below = NULL;
1922  /* send the delitem before resetting env, so container's contents be may
1923  * refreshed */
1924  if (LOOK_OBJ(op) && pl != NULL)
1925  esrv_del_item(pl, op);
1926  op->env = NULL;
1927  return;
1928  }
1929 
1930  /* If we get here, we are removing it from a map */
1931  if (op->map == NULL)
1932  return;
1933 
1934  if (op->contr != NULL && !op->contr->hidden)
1935  op->map->players--;
1936 
1937  x = op->x;
1938  y = op->y;
1939  m = get_map_from_coord(op->map, &x, &y);
1940 
1941  if (!m) {
1942  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);
1943  abort();
1944  }
1945  if (op->map != m) {
1946  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);
1947  }
1948 
1949  /* link the object above us */
1950  if (op->above)
1951  op->above->below = op->below;
1952  else
1953  SET_MAP_TOP(m, x, y, op->below); /* we were top, set new top */
1954 
1955  /* Relink the object below us, if there is one */
1956  if (op->below) {
1957  op->below->above = op->above;
1958  } else {
1959  /* Nothing below, which means we need to relink map object for this space
1960  * use translated coordinates in case some oddness with map tiling is
1961  * evident
1962  */
1963  /*TODO is this check really needed?*/
1964  if (GET_MAP_OB(m, x, y) != op) {
1965  StringBuffer *sb;
1966  char *diff;
1967 
1968  sb = stringbuffer_new();
1969  object_dump(op, sb);
1970  diff = stringbuffer_finish(sb);
1971  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);
1972  free(diff);
1973 
1974  sb = stringbuffer_new();
1975  object_dump(GET_MAP_OB(m, x, y), sb);
1976  diff = stringbuffer_finish(sb);
1977  LOG(llevError, "%s\n", diff);
1978  free(diff);
1979  }
1980  SET_MAP_OB(m, x, y, op->above); /* goes on above it. */
1981  }
1982  op->above = NULL;
1983  op->below = NULL;
1984 
1985  if (op->map->in_memory == MAP_SAVING)
1986  return;
1987 
1988  tag = op->count;
1989  check_walk_off = !QUERY_FLAG(op, FLAG_NO_APPLY);
1990  FOR_MAP_PREPARE(m, x, y, tmp) {
1991  /* No point updating the players look faces if he is the object
1992  * being removed.
1993  */
1994 
1995  if (tmp->type == PLAYER && tmp != op) {
1996  /* If a container that the player is currently using somehow gets
1997  * removed (most likely destroyed), update the player view
1998  * appropriately.
1999  */
2000  if (tmp->container == op) {
2002  tmp->container = NULL;
2003  // Notify the client the container is gone so it clears its active container.
2005  }
2006  tmp->contr->socket->update_look = 1;
2007  }
2008  /* See if player moving off should effect something */
2009  if (check_walk_off
2010  && ((op->move_type&tmp->move_off) && (op->move_type&~tmp->move_off&~tmp->move_block) == 0)) {
2011  ob_move_on(tmp, op, NULL);
2012  if (object_was_destroyed(op, tag)) {
2013  LOG(llevError, "BUG: object_remove(): name %s, archname %s destroyed leaving object\n", tmp->name, tmp->arch->name);
2014  }
2015  }
2016 
2017  /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
2018  if (tmp->above == tmp)
2019  tmp->above = NULL;
2020  last = tmp;
2021  } FOR_MAP_FINISH();
2022  /* last == NULL or there are no objects on this space */
2023  if (last == NULL) {
2024  /* set P_NEED_UPDATE, otherwise update_position will complain. In theory,
2025  * we could preserve the flags (GET_MAP_FLAGS), but update_position figures
2026  * those out anyways, and if there are any flags set right now, they won't
2027  * be correct anyways.
2028  */
2029  SET_MAP_FLAGS(op->map, op->x, op->y, P_NEED_UPDATE);
2030  update_position(op->map, op->x, op->y);
2031  } else
2033 
2034  if (QUERY_FLAG(op, FLAG_BLOCKSVIEW) || (op->glow_radius != 0))
2035  update_all_los(op->map, op->x, op->y);
2036 }
2037 
2051 object *object_merge(object *op, object *top) {
2052  if (!op->nrof)
2053  return NULL;
2054 
2055  if (top == NULL)
2056  for (top = op; top != NULL && top->above != NULL; top = top->above)
2057  ;
2059  if (top == op)
2060  continue;
2061  if (object_can_merge(op, top)) {
2062  object_increase_nrof(top, op->nrof);
2063  /*
2064  * Previous behavior set weight to zero here.
2065  * This, however, caused the object_sub_weight
2066  * call in object_remove to subtract zero weight
2067  * when removing the object. Thus, until inventory
2068  * weight is next recalculated, the object merged
2069  * into another pile added weight in object_increase_nrof
2070  * but did not remove the weight from the original
2071  * instance of itself in object_remove, essentially
2072  * counting for double weight for several minutes.
2073  *
2074  * SilverNexus 2014-05-27
2075  */
2076  object_remove(op);
2078  return top;
2079  }
2081  return NULL;
2082 }
2083 
2100 object *object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y) {
2101  object *tmp;
2102 
2103  op = HEAD(op);
2104  for (tmp = op; tmp; tmp = tmp->more) {
2105  tmp->x = x+tmp->arch->clone.x;
2106  tmp->y = y+tmp->arch->clone.y;
2107  tmp->map = m;
2108  }
2109  return object_insert_in_map(op, m, originator, flag);
2110 }
2111 
2129 void object_merge_spell(object *op, int16_t x, int16_t y) {
2130  int i;
2131 
2132  /* We try to do some merging of spell objects - if something has same owner,
2133  * is same type of spell, and going in the same direction, it is somewhat
2134  * mergable.
2135  *
2136  * If the spell object has an other_arch, don't merge - when the spell
2137  * does something, like explodes, it will use this other_arch, and
2138  * if we merge, there is no easy way to make the correct values be
2139  * set on this new object (values should be doubled, tripled, etc.)
2140  *
2141  * We also care about speed - only process objects that will not be
2142  * active this tick. Without this, the results are incorrect - think
2143  * of a case where tmp would normally get processed this tick, but
2144  * get merges with op, which does not get processed.
2145  */
2146  FOR_MAP_PREPARE(op->map, x, y, tmp) {
2147  if (op->type == tmp->type
2148  && op->subtype == tmp->subtype
2149  && op->direction == tmp->direction
2150  && op->owner == tmp->owner && op->ownercount == tmp->ownercount
2151  && op->range == tmp->range
2152  && op->stats.wc == tmp->stats.wc
2153  && op->level == tmp->level
2154  && op->attacktype == tmp->attacktype
2155  && op->speed == tmp->speed
2156  && !tmp->other_arch
2157  && (tmp->speed_left+tmp->speed) < 0.0
2158  && op != tmp) {
2159  /* Quick test - if one or the other objects already have hash tables
2160  * set up, and that hash bucket contains a value that doesn't
2161  * match what we want to set it up, we won't be able to merge.
2162  * Note that these two if statements are the same, except
2163  * for which object they are checking against. They could
2164  * be merged, but the line wrapping would be large enough
2165  * that IMO it would become difficult to read the different clauses
2166  * so its cleaner just to do 2 statements - MSW
2167  */
2168  if (op->spell_tags
2169  && !OB_SPELL_TAG_MATCH(op, (tag_t)tmp->stats.maxhp)
2170  && OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) != 0)
2171  continue;
2172 
2173  if (tmp->spell_tags
2174  && !OB_SPELL_TAG_MATCH(tmp, (tag_t)op->stats.maxhp)
2175  && OB_SPELL_TAG_HASH(tmp, op->stats.maxhp) != 0)
2176  continue;
2177 
2178  /* If we merge, the data from tmp->spell_tags gets copied into op.
2179  * so we need to make sure that slot isn't filled up.
2180  */
2181  if (tmp->spell_tags
2182  && !OB_SPELL_TAG_MATCH(tmp, (tag_t)tmp->stats.maxhp)
2183  && OB_SPELL_TAG_HASH(tmp, tmp->stats.maxhp) != 0)
2184  continue;
2185 
2186  /* If both objects have spell_tags, we need to see if there are conflicting
2187  * values - if there are, we won't be able to merge then.
2188  */
2189  if (tmp->spell_tags && op->spell_tags) {
2190  int need_copy = 0;
2191 
2192  for (i = 0; i < SPELL_TAG_SIZE; i++) {
2193  /* If the two tag values in the hash are set, but are
2194  * not set to the same value, then these objects
2195  * can not be merged.
2196  */
2197  if (op->spell_tags[i] && tmp->spell_tags[i]
2198  && op->spell_tags[i] != tmp->spell_tags[i]) {
2200  break;
2201  }
2202  /* If one tag is set and the other is not, that is
2203  * fine, but we have to note that we need to copy
2204  * the data in that case.
2205  */
2206  if ((!op->spell_tags[i] && tmp->spell_tags[i])
2207  || (op->spell_tags[i] && !tmp->spell_tags[i])) {
2208  need_copy = 1;
2209  }
2210  }
2211  /* If we did not get through entire array, it means
2212  * we got a conflicting hash, and so we won't be
2213  * able to merge these - just continue processing
2214  * object on this space.
2215  */
2216  if (i <= SPELL_TAG_SIZE)
2217  continue;
2218 
2219  /* Ok - everything checked out - we should be able to
2220  * merge tmp in op. So lets copy the tag data if
2221  * needed. Note that this is a selective copy, as
2222  * we don't want to clear values that may be set in op.
2223  */
2224  if (need_copy) {
2225  for (i = 0; i < SPELL_TAG_SIZE; i++)
2226  if (!op->spell_tags[i]
2227  && tmp->spell_tags[i]
2228  && tmp->spell_tags[i] != (tag_t)op->stats.maxhp)
2229  op->spell_tags[i] = tmp->spell_tags[i];
2230  }
2231  FREE_AND_CLEAR(tmp->spell_tags);
2232  }
2233 
2234  /* if tmp has a spell_tags table, copy it to op and free tmps */
2235  if (tmp->spell_tags && !op->spell_tags) {
2236  op->spell_tags = tmp->spell_tags;
2237  tmp->spell_tags = NULL;
2238 
2239  /* We don't need to keep a copy of our maxhp value
2240  * in the copied over value
2241  */
2242  if (OB_SPELL_TAG_MATCH(op, (tag_t)op->stats.maxhp))
2243  OB_SPELL_TAG_HASH(op, op->stats.maxhp) = 0;
2244  }
2245 
2246  /* For spells to work correctly, we need to record what spell
2247  * tags we've merged in with this effect. This is used
2248  * in ok_to_put_more() to see if a spell effect is already on
2249  * the space.
2250  */
2251  if (op->stats.maxhp != tmp->stats.maxhp) {
2252 #ifdef OBJECT_DEBUG
2253  /* This if statement should never happen - the logic above should
2254  * have prevented it. It is a problem, because by now its possible
2255  * we've destroyed the spell_tags in tmp, so we can't really
2256  * just bail out.
2257  */
2258 
2259  if (op->spell_tags
2260  && OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) != 0
2261  && !OB_SPELL_TAG_MATCH(op, tmp->stats.maxhp)) {
2262  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);
2263  }
2264 #endif
2265  if (!op->spell_tags)
2266  op->spell_tags = static_cast<tag_t *>(calloc(SPELL_TAG_SIZE, sizeof(tag_t)));
2267 
2268  OB_SPELL_TAG_HASH(op, tmp->stats.maxhp) = tmp->stats.maxhp;
2269  }
2270 
2272  op->speed_left = MAX(op->speed_left, tmp->speed_left);
2273 
2274  if (tmp->duration != op->duration) {
2275  /* We need to use tmp_dam here because otherwise the
2276  * calculations can overflow the size of stats.dam.
2277  */
2278  int tmp_dam = tmp->stats.dam*(tmp->duration+1)+
2279  op->stats.dam*(op->duration+1);
2280 
2281  op->duration = MAX(op->duration, tmp->duration);
2282  tmp_dam /= op->duration+1;
2283  op->stats.dam = tmp_dam+1;
2284  } else {
2285  /* in this case, duration is the same, so simply adding
2286  * up damage works.
2287  */
2288  op->stats.dam += tmp->stats.dam;
2289  }
2290 
2291  object_remove(tmp);
2293  }
2294  } FOR_MAP_FINISH();
2295 }
2296 
2297 static object *find_insert_pos(object *op, const int flag) {
2298  object *floor = NULL;
2299  /*
2300  * If there are multiple objects on this space, we do some trickier handling.
2301  * We've already dealt with merging if appropriate.
2302  * Generally, we want to put the new object on top. But if
2303  * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last
2304  * floor, we want to insert above that and no further.
2305  * Also, if there are spell objects on this space, we stop processing
2306  * once we get to them. This reduces the need to traverse over all of
2307  * them when adding another one - this saves quite a bit of cpu time
2308  * when lots of spells are cast in one area. Currently, it is presumed
2309  * that flying non pickable objects are spell objects.
2310  */
2311  if (flag&INS_ON_TOP) {
2312  return GET_MAP_TOP(op->map, op->x, op->y);
2313  }
2314  object *last = NULL;
2315  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
2318  floor = tmp;
2319 
2321  && (tmp->move_type&(MOVE_FLY_LOW|MOVE_FLY_HIGH))
2322  && !QUERY_FLAG(tmp, FLAG_IS_FLOOR)) {
2323  /* We insert above tmp, so we want this object below this */
2324  break;
2325  }
2326  last = tmp;
2327  } FOR_MAP_FINISH();
2328  if (flag&INS_ABOVE_FLOOR_ONLY)
2329  return floor;
2330  return last;
2331 }
2332 
2361 object *object_insert_in_map(object *op, mapstruct *m, object *originator, int flag) {
2362  object *tmp, *top, *floor = NULL;
2363  int16_t x, y;
2364 
2365  if (QUERY_FLAG(op, FLAG_FREED)) {
2366  LOG(llevError, "Trying to insert freed object!\n");
2367  return NULL;
2368  }
2369  if (m == NULL) {
2370  StringBuffer *sb;
2371  char *diff;
2372 
2373  sb = stringbuffer_new();
2374  object_dump(op, sb);
2375  diff = stringbuffer_finish(sb);
2376  LOG(llevError, "Trying to insert in null-map!\n%s\n", diff);
2377  free(diff);
2378  return op;
2379  }
2380  if (out_of_map(m, op->x, op->y)) {
2381  StringBuffer *sb;
2382  char *diff;
2383 
2384  sb = stringbuffer_new();
2385  object_dump(op, sb);
2386  diff = stringbuffer_finish(sb);
2387  LOG(llevError, "Trying to insert object outside the map.\n%s\n", diff);
2388  free(diff);
2389 #ifdef MANY_CORES
2390  /* Better to catch this here, as otherwise the next use of this object
2391  * is likely to cause a crash. Better to find out where it is getting
2392  * improperly inserted.
2393  */
2394  abort();
2395 #endif
2396  return op;
2397  }
2398  if (!QUERY_FLAG(op, FLAG_REMOVED)) {
2399  StringBuffer *sb;
2400  char *diff;
2401 
2402  sb = stringbuffer_new();
2403  object_dump(op, sb);
2404  diff = stringbuffer_finish(sb);
2405  LOG(llevError, "Trying to insert (map) inserted object.\n%s\n", diff);
2406  free(diff);
2407  return op;
2408  }
2409  if (op->more != NULL) {
2410  /* The part may be on a different map. */
2411 
2412  object *more = op->more;
2413 
2414  /* We really need the caller to normalize coordinates - if
2415  * we set the map, that doesn't work if the location is within
2416  * a map and this is straddling an edge. So only if coordinate
2417  * is clear wrong do we normalize it.
2418  */
2419  if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
2420  /* Debugging information so you can see the last coordinates this object had */
2421  more->ox = more->x;
2422  more->oy = more->y;
2423  more->map = get_map_from_coord(more->map, &more->x, &more->y);
2424  } else if (!more->map) {
2425  /* For backwards compatibility - when not dealing with tiled maps,
2426  * more->map should always point to the parent.
2427  */
2428  more->map = m;
2429  }
2430 
2431  if (object_insert_in_map(more, more->map, originator, flag) == NULL) {
2432  if (!op->head)
2433  LOG(llevError, "BUG: object_insert_in_map(): inserting op->more killed op\n");
2434  return NULL;
2435  }
2436  }
2438 
2439  /* Debugging information so you can see the last coordinates this object had */
2440  op->ox = op->x;
2441  op->oy = op->y;
2442  x = op->x;
2443  y = op->y;
2444  op->map = get_map_from_coord(m, &x, &y);
2445 
2446  /* this has to be done after we translate the coordinates. */
2447  if (op->nrof
2448  && !(flag&INS_NO_MERGE)
2449  && op->type != SPELL_EFFECT) {
2450  FOR_MAP_PREPARE(op->map, x, y, spot) {
2451  if (object_can_merge(op, spot)) {
2452  op->nrof += spot->nrof;
2453  object_remove(spot);
2455  }
2456  } FOR_MAP_FINISH();
2457  } else if (op->type == SPELL_EFFECT
2458  && !op->range
2459  && !op->other_arch
2460  && (op->speed_left+op->speed) < 0.0) {
2461  object_merge_spell(op, x, y);
2462  }
2463 
2464  /* Ideally, the caller figures this out. However, it complicates a lot
2465  * of areas of callers (eg, anything that uses object_find_free_spot() would now
2466  * need extra work
2467  */
2468  if (op->map != m) {
2469  /* coordinates should not change unless map also changes */
2470  op->x = x;
2471  op->y = y;
2472  }
2473 
2474  if (op->type != LAMP)
2475  /* lamps use the FLAG_APPLIED to keep the light/unlit status, so don't reset it.
2476  Other objects just get unapplied, since the container "drops" them. */
2479  if (!QUERY_FLAG(op, FLAG_ALIVE))
2481 
2482  /* In many places, a player is passed as the originator, which
2483  * is fine. However, if the player is on a transport, they are not
2484  * actually on the map, so we can't use them for the linked pointers,
2485  * nor should the walk on function below use them either.
2486  */
2487  if (originator && originator->contr && originator->contr->transport)
2488  originator = originator->contr->transport;
2489 
2490  if (flag&INS_BELOW_ORIGINATOR) {
2491  if (originator->map != op->map
2492  || originator->x != op->x
2493  || originator->y != op->y) {
2494  LOG(llevError, "object_insert_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n");
2495  abort();
2496  }
2497  op->above = originator;
2498  op->below = originator->below;
2499  if (op->below)
2500  op->below->above = op;
2501  else
2502  SET_MAP_OB(op->map, op->x, op->y, op);
2503  /* since *below *originator, no need to update top */
2504  originator->below = op;
2505  } else {
2506  /* Top is the object that our object (op) is going to get inserted above. */
2507  top = find_insert_pos(op, flag);
2508 
2509  /* First object on this space */
2510  if (!top) {
2511  op->above = GET_MAP_OB(op->map, op->x, op->y);
2512  if (op->above)
2513  op->above->below = op;
2514  op->below = NULL;
2515  SET_MAP_OB(op->map, op->x, op->y, op);
2516  } else { /* get inserted into the stack above top */
2517  op->above = top->above;
2518  if (op->above)
2519  op->above->below = op;
2520  op->below = top;
2521  top->above = op;
2522  }
2523  if (op->above == NULL)
2524  SET_MAP_TOP(op->map, op->x, op->y, op);
2525  } /* else not INS_BELOW_ORIGINATOR */
2526 
2527  if (!(flag&INS_MAP_LOAD)) {
2528  if (op->type == PLAYER)
2529  op->contr->do_los = 1;
2530 
2531  /* If we have a floor, we know the player, if any, will be above
2532  * it, so save a few ticks and start from there.
2533  */
2534  tmp = floor ? floor : GET_MAP_OB(op->map, op->x, op->y);
2536  if (tmp->type == PLAYER)
2537  tmp->contr->socket->update_look = 1;
2539 
2540  /* If this object glows, it may affect lighting conditions that are
2541  * visible to others on this map. But update_all_los is really
2542  * an inefficient way to do this, as it means los for all players
2543  * on the map will get recalculated. The players could very well
2544  * be far away from this change and not affected in any way -
2545  * this should get redone to only look for players within range,
2546  * or just updating the P_NEED_UPDATE for spaces within this area
2547  * of effect may be sufficient.
2548  */
2549  if (MAP_DARKNESS(op->map) && (op->glow_radius != 0))
2550  update_all_los(op->map, op->x, op->y);
2551 
2552  if (op->contr && !op->contr->hidden)
2553  op->map->players++;
2554  }
2555 
2556  /* updates flags (blocked, alive, no magic, etc) for this map space */
2558 
2559  /* Don't know if moving this to the end will break anything. However,
2560  * we want to have update_look set above before calling this.
2561  *
2562  * object_check_move_on() must be after this because code called from
2563  * object_check_move_on() depends on correct map flags (so functions like
2564  * blocked() and wall() work properly), and these flags are updated by
2565  * object_update().
2566  */
2567 
2568  /* if this is not the head or flag has been passed, don't check walk on status */
2569 
2570  if (!(flag&INS_NO_WALK_ON) && !op->head) {
2571  if (object_check_move_on(op, originator))
2572  return NULL;
2573 
2574  /* If we are a multi part object, lets work our way through the check
2575  * walk on's.
2576  */
2577  for (tmp = op->more; tmp != NULL; tmp = tmp->more)
2578  if (object_check_move_on(tmp, originator))
2579  return NULL;
2580  }
2581  return op;
2582 }
2583 
2593 void object_replace_insert_in_map(const char *arch_string, object *op) {
2594  object *tmp1;
2595  archetype *at;
2596 
2597  /* first search for itself and remove any old instances */
2598  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
2599  if (!strcmp(tmp->arch->name, arch_string)) { /* same archetype */
2600  object_remove(tmp);
2602  }
2603  } FOR_MAP_FINISH();
2604 
2605  at = find_archetype(arch_string);
2606  if (at == NULL) {
2607  return;
2608  }
2609  tmp1 = arch_to_object(at);
2610  object_insert_in_map_at(tmp1, op->map, op, INS_BELOW_ORIGINATOR, op->x, op->y);
2611 }
2612 
2633 object *object_split(object *orig_ob, uint32_t nr, char *err, size_t size) {
2634  object *newob;
2635 
2636  if (MAX(1, orig_ob->nrof) < nr) {
2637  /* If err is set, the caller knows that nr can be wrong (player trying to drop items), thus don't log that. */
2638  if (err)
2639  snprintf(err, size, "There are only %u %ss.", NROF(orig_ob), orig_ob->name);
2640  else
2641  LOG(llevDebug, "There are only %u %ss.\n", NROF(orig_ob), orig_ob->name);
2642  return NULL;
2643  }
2644  newob = object_create_clone(orig_ob);
2645  if (orig_ob->nrof == 0) {
2646  if (!QUERY_FLAG(orig_ob, FLAG_REMOVED)) {
2647  object_remove(orig_ob);
2648  }
2650  } else {
2651  newob->nrof = nr;
2652  object_decrease_nrof(orig_ob, nr);
2653  }
2654 
2655  return newob;
2656 }
2657 
2672 object *object_decrease_nrof(object *op, uint32_t i) {
2673  object *tmp;
2674 
2675  if (i == 0) /* objects with op->nrof require this check */
2676  return op;
2677 
2678  if (i > op->nrof)
2679  i = op->nrof;
2680 
2681  if (QUERY_FLAG(op, FLAG_REMOVED)) {
2682  op->nrof -= i;
2683  } else if (op->env != NULL) {
2684  if (i < op->nrof) {
2685  player *pl;
2686  /* is this object in the players inventory, or sub container
2687  * therein?
2688  */
2690  /* nope. Is this a container the player has opened?
2691  * If so, set tmp to that player.
2692  * IMO, searching through all the players will mostly
2693  * likely be quicker than following op->env to the map,
2694  * and then searching the map for a player.
2695  */
2696  if (!tmp) {
2697  for (pl = first_player; pl; pl = pl->next)
2698  if (pl->ob->container == op->env)
2699  break;
2700  if (pl)
2701  tmp = pl->ob;
2702  else
2703  tmp = NULL;
2704  }
2705 
2706  /* Because of weight reduction by container and integer arithmetic,
2707  * there is no guarantee the rounded weight of combined items will be
2708  * the same as the sum of rounded weights.
2709  * Therefore just remove the current weight, and add the new.
2710  * Same adjustment done in object_increase_nrof().
2711  */
2712  object_sub_weight(op->env, op->weight * op->nrof);
2713  op->nrof -= i;
2714  object_add_weight(op->env, op->weight * op->nrof);
2715  if (tmp) {
2718  fix_object(tmp);
2719  }
2720  } else {
2721  object_remove(op);
2722  op->nrof = 0;
2723  }
2724  } else {
2725  /* On a map. */
2726  if (i < op->nrof) {
2727  op->nrof -= i;
2728 
2729  FOR_MAP_PREPARE(op->map, op->x, op->y, pl)
2730  if (pl->contr) {
2731  pl->contr->socket->update_look = 1;
2732  break;
2733  }
2734  FOR_MAP_FINISH();
2735  } else {
2736  object_remove(op);
2737  op->nrof = 0;
2738  }
2739  }
2740 
2741  if (op->nrof) {
2742  return op;
2743  } else {
2745  return NULL;
2746  }
2747 }
2748 
2759 static void object_increase_nrof(object *op, uint32_t i) {
2760  object *tmp;
2761 
2762  if (i == 0) /* objects with op->nrof require this check */
2763  return;
2764 
2765  if (QUERY_FLAG(op, FLAG_REMOVED)) {
2766  op->nrof += i;
2767  } else if (op->env != NULL) {
2768  player *pl;
2769  /* is this object in the players inventory, or sub container
2770  * therein?
2771  */
2773  /* nope. Is this a container the player has opened?
2774  * If so, set tmp to that player.
2775  * IMO, searching through all the players will mostly
2776  * likely be quicker than following op->env to the map,
2777  * and then searching the map for a player.
2778  */
2779  if (!tmp) {
2780  for (pl = first_player; pl; pl = pl->next)
2781  if (pl->ob->container == op->env)
2782  break;
2783  if (pl)
2784  tmp = pl->ob;
2785  else
2786  tmp = NULL;
2787  }
2788 
2789  /* Because of weight reduction by container and integer arithmetic,
2790  * there is no guarantee the rounded weight of combined items will be
2791  * the same as the sum of rounded weights.
2792  * Therefore just remove the current weight, and add the new.
2793  * Same adjustment done in object_decrease_nrof().
2794  */
2795  object_sub_weight(op->env, op->weight * op->nrof);
2796  op->nrof += i;
2797  object_add_weight(op->env, op->weight * op->nrof);
2798  if (tmp) {
2800  // Why don't we need to update weight of op->env here?
2801  }
2802  } else {
2803  /* On a map. */
2804  op->nrof += i;
2805 
2806  FOR_MAP_PREPARE(op->map, op->x, op->y, pl)
2807  if (pl->contr) {
2808  pl->contr->socket->update_look = 1;
2809  break;
2810  }
2811  FOR_MAP_FINISH();
2812  }
2813 }
2814 
2829 void object_add_weight(object *op, signed long weight) {
2830  while (op != NULL) {
2831  if (op->type == CONTAINER) {
2832  weight = (signed long)(weight*(100-op->stats.Str)/100);
2833  }
2834  op->carrying += weight;
2835  op = op->env;
2836  }
2837 }
2838 
2853 object *object_insert_in_ob(object *op, object *where) {
2854  object *otmp;
2855 
2856  if (!QUERY_FLAG(op, FLAG_REMOVED)) {
2857  StringBuffer *sb;
2858  char *diff;
2859 
2860  sb = stringbuffer_new();
2861  object_dump(op, sb);
2862  diff = stringbuffer_finish(sb);
2863  LOG(llevError, "Trying to insert (ob) inserted object.\n%s\n", diff);
2864  free(diff);
2865  return op;
2866  }
2867 
2868  if (where == NULL) {
2869  StringBuffer *sb;
2870  char *diff;
2871 
2872  sb = stringbuffer_new();
2873  object_dump(op, sb);
2874  diff = stringbuffer_finish(sb);
2875  LOG(llevError, "Trying to put object in NULL.\n%s\n", diff);
2876  free(diff);
2877  return op;
2878  }
2879  if (where->head) {
2880  LOG(llevDebug, "Warning: Tried to insert object wrong part of multipart object.\n");
2881  }
2882  where = HEAD(where);
2883  if (op->more) {
2884  LOG(llevError, "Tried to insert multipart object %s (%u)\n", op->name, op->count);
2885  return op;
2886  }
2889  if (op->nrof || op->type == SKILL) {
2891  // Since harvesting skills have the same subtype, we need to check our shared string skill name as well
2892  if (op->type == SKILL && tmp->type == SKILL && op->subtype == tmp->subtype && op->skill == tmp->skill) {
2893  // Duplicate skill. Sum exp and total_exp.
2894  LOG(llevDebug, "Merged duplicate skill %s for %s\n", op->arch->name, where->name);
2895  tmp->stats.exp += op->stats.exp;
2896  tmp->total_exp += op->total_exp;
2898  object_free(op, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK); /* free the inserted object */
2899  return tmp;
2900  } else if (object_can_merge(tmp, op)) {
2901  /* return the original object and remove inserted object
2902  * (client needs the original object) */
2903  object_increase_nrof(tmp, op->nrof);
2905  object_free(op, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK); /* free the inserted object */
2906  return tmp;
2907  }
2908  FOR_INV_FINISH();
2909 
2910  /* the item couldn't merge. */
2911  object_add_weight(where, op->weight*op->nrof);
2912  } else
2913  object_add_weight(where, op->weight+op->carrying);
2914 
2915  op->map = NULL;
2916  op->env = where;
2917  op->above = NULL;
2918  op->below = NULL;
2919  op->x = 0,
2920  op->y = 0;
2921  op->ox = 0,
2922  op->oy = 0;
2923 
2924  /* Client has no idea of ordering so lets not bother ordering it here.
2925  * It sure simplifies this function...
2926  */
2927  if (where->inv == NULL)
2928  where->inv = op;
2929  else {
2930  op->below = where->inv;
2931  op->below->above = op;
2932  where->inv = op;
2933  }
2934 
2935  if (op->type == EVENT_CONNECTOR) {
2936  where->event_bitmask |= BITMASK_EVENT(op->subtype);
2937  }
2938 
2939  /* Update in 2 cases: object goes into player's inventory, or object goes into container the player
2940  * is looking into. */
2941  if (where->contr != NULL)
2943  else if (where->type == CONTAINER && QUERY_FLAG(where, FLAG_APPLIED)) {
2944  object *pl = NULL;
2945 
2946  if (op->env->env && op->env->env->contr)
2947  /* Container is in player's inventory. */
2948  pl = op->env->env;
2949  else if (op->env->map) {
2950  /* Container on map, look above for player. */
2951  FOR_ABOVE_PREPARE(op->env, above)
2952  if (above->contr) {
2953  pl = above;
2954  break;
2955  }
2956  FOR_ABOVE_FINISH();
2957  }
2958  if (pl)
2959  esrv_send_item(pl, op);
2960  }
2961 
2963  if (otmp && otmp->contr != NULL) {
2964  if (!QUERY_FLAG(otmp, FLAG_NO_FIX_PLAYER)
2965  && (QUERY_FLAG(op, FLAG_APPLIED) || op->type == SKILL || op->glow_radius != 0))
2966  /* fix_object will only consider applied items, or skills, or items with a glow radius.
2967  thus no need to call it if our object hasn't that. */
2968  fix_object(otmp);
2969  }
2970 
2971  /* reset the light list and los of the players on the map */
2972  if (op->glow_radius != 0 && where->map) {
2973 #ifdef DEBUG_LIGHTS
2974  LOG(llevDebug, " object_insert_in_ob(): got %s to insert in map/op\n", op->name);
2975 #endif /* DEBUG_LIGHTS */
2976  if (MAP_DARKNESS(where->map)) {
2977  SET_MAP_FLAGS(where->map, where->x, where->y, P_NEED_UPDATE);
2978  update_position(where->map, where->x, where->y);
2979  update_all_los(where->map, where->x, where->y);
2980  }
2981  }
2982 
2983  return op;
2984 }
2985 
3008 int object_check_move_on(object *op, object *originator) {
3009  object *tmp;
3010  tag_t tag;
3011  mapstruct *m = op->map;
3012  int x = op->x, y = op->y;
3013  MoveType move_on, move_slow, move_block;
3014 
3015  if (QUERY_FLAG(op, FLAG_NO_APPLY))
3016  return 0;
3017 
3018  tag = op->count;
3019 
3020  move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
3021  move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
3022  move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
3023 
3024  /* if nothing on this space will slow op down or be applied,
3025  * no need to do checking below. have to make sure move_type
3026  * is set, as lots of objects don't have it set - we treat that
3027  * as walking.
3028  */
3029  if (op->move_type
3030  && !(op->move_type&move_on)
3031  && !(op->move_type&move_slow))
3032  return 0;
3033 
3034  /* This is basically inverse logic of that below - basically,
3035  * if the object can avoid the move on or slow move, they do so,
3036  * but can't do it if the alternate movement they are using is
3037  * blocked. Logic on this seems confusing, but does seem correct.
3038  */
3039  if ((op->move_type&~move_on&~move_block) != 0
3040  && (op->move_type&~move_slow&~move_block) != 0)
3041  return 0;
3042 
3043  /* The objects have to be checked from top to bottom.
3044  * Hence, we first go to the top:
3045  */
3046 
3047  tmp = GET_MAP_OB(op->map, op->x, op->y);
3049  if (tmp->above == NULL)
3050  break;
3051  /* Trim the search when we find the first other spell effect
3052  * this helps performance so that if a space has 50 spell objects,
3053  * we don't need to check all of them.
3054  */
3055  if ((tmp->move_type&MOVE_FLY_LOW) && QUERY_FLAG(tmp, FLAG_NO_PICK))
3056  break;
3059  if (tmp == op)
3060  continue; /* Can't apply yourself */
3061 
3062  /* Check to see if one of the movement types should be slowed down.
3063  * Second check makes sure that the movement types not being slowed
3064  * (~slow_move) is not blocked on this space - just because the
3065  * space doesn't slow down swimming (for example), if you can't actually
3066  * swim on that space, can't use it to avoid the penalty.
3067  */
3068  if (!QUERY_FLAG(op, FLAG_WIZPASS)) {
3069  if ((!op->move_type && tmp->move_slow&MOVE_WALK)
3070  || ((op->move_type&tmp->move_slow) && (op->move_type&~tmp->move_slow&~tmp->move_block) == 0)) {
3071  float diff;
3072 
3073  diff = tmp->move_slow_penalty*FABS(op->speed);
3074  if (op->type == PLAYER) {
3077  diff /= 4.0;
3078  }
3079  }
3080  op->speed_left -= diff;
3081  }
3082  }
3083 
3084  /* Basically same logic as above, except now for actual apply. */
3085  if ((!op->move_type && tmp->move_on&MOVE_WALK)
3086  || ((op->move_type&tmp->move_on) && (op->move_type&~tmp->move_on&~tmp->move_block) == 0)) {
3087  ob_move_on(tmp, op, originator);
3088  if (object_was_destroyed(op, tag))
3089  return 1;
3090 
3091  /* what the person/creature stepped onto has moved the object
3092  * someplace new. Don't process any further - if we did,
3093  * have a feeling strange problems would result.
3094  */
3095  if (op->map != m || op->x != x || op->y != y)
3096  return 0;
3097  }
3099  return 0;
3100 }
3101 
3114 object *map_find_by_archetype(mapstruct *m, int x, int y, const archetype *at) {
3115  if (m == NULL || OUT_OF_REAL_MAP(m, x, y)) {
3116  LOG(llevError, "Present_arch called outside map.\n");
3117  return NULL;
3118  }
3119 
3120  FOR_MAP_PREPARE(m, x, y, tmp)
3121  if (tmp->arch == at)
3122  return tmp;
3123  FOR_MAP_FINISH();
3124 
3125  return NULL;
3126 }
3127 
3141 object *map_find_by_type(mapstruct *m, int x, int y, uint8_t type) {
3142  if (OUT_OF_REAL_MAP(m, x, y)) {
3143  return NULL;
3144  }
3145 
3146  FOR_MAP_PREPARE(m, x, y, tmp)
3147  if (tmp->type == type)
3148  return tmp;
3149  FOR_MAP_FINISH();
3150 
3151  return NULL;
3152 }
3153 
3164 object *object_present_in_ob(uint8_t type, const object *op) {
3165  object *tmp;
3166 
3167  for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
3168  if (tmp->type == type)
3169  return tmp;
3170 
3171  return NULL;
3172 }
3173 
3199 object *object_present_in_ob_by_name(int type, const char *str, const object *op) {
3200  object *tmp;
3201 
3202  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
3203  if ((type == -1 || tmp->type == type) && !strcmp(str, tmp->name))
3204  return tmp;
3205  }
3206  return NULL;
3207 }
3208 
3218 object *arch_present_in_ob(const archetype *at, const object *op) {
3219  object *tmp;
3220 
3221  for (tmp = op->inv; tmp != NULL; tmp = tmp->below)
3222  if (tmp->arch == at)
3223  return tmp;
3224  return NULL;
3225 }
3226 
3235 void object_set_flag_inv(object*op, int flag) {
3236  object *tmp;
3237 
3238  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
3239  SET_FLAG(tmp, flag);
3240  object_set_flag_inv(tmp, flag);
3241  }
3242 }
3243 
3252 void object_unset_flag_inv(object*op, int flag) {
3253  object *tmp;
3254 
3255  for (tmp = op->inv; tmp != NULL; tmp = tmp->below) {
3256  CLEAR_FLAG(tmp, flag);
3257  object_unset_flag_inv(tmp, flag);
3258  }
3259 }
3260 
3270 void object_set_cheat(object *op) {
3273 }
3274 
3292 int object_find_multi_free_spot_around(const object *ob, const object *gen, int16_t *hx, int16_t *hy) {
3293  int genx, geny, genx2, geny2, sx, sy, sx2, sy2, ix, iy, nx, ny, i, flag;
3294  int freecount = 0;
3295 
3296  ob = HEAD(ob);
3297 
3298  object_get_multi_size(ob, &sx, &sy, &sx2, &sy2);
3299  object_get_multi_size(gen, &genx, &geny, &genx2, &geny2);
3300  /*
3301  * sx and sy are now the coords of the bottom right corner of ob relative to the head.
3302  * genx and geny are now the coords of the bottom right corner of gen relative to the head.
3303  * sx2 and sy2 are now the coords of the head of ob relative to the top left corner.
3304  * genx2 and geny2 are now the coords of the head of gen relative to the top left corner.
3305  */
3306 
3307  sx++;
3308  sy++;
3309  genx++;
3310  geny++;
3311  /*
3312  * sx, sy, genx, and geny, are now the size of the object, excluding parts left and above
3313  * the head.
3314  */
3315 
3316  ix = gen->x-sx-genx2;
3317  iy = gen->y-sy-geny2;
3318  sx += genx+sx2;
3319  sy += geny+sy2;
3320  /*
3321  * ix and iy are the map coords of the top left square where the head of ob could possibly
3322  * be placed. sx and sy are now the size of the square to search for placement of the head
3323  * relative to ix and iy.
3324  */
3325 
3326  /*
3327  * Loop around the square of possible positions for the head of ob object:
3328  */
3329  for (i = 0; i < (sx+sx+sy+sy); i++) {
3330  if (i <= sx) {
3331  nx = i+ix;
3332  ny = iy;
3333  } else if (i <= sx+sy) {
3334  nx = ix+sx;
3335  ny = iy+i-sx;
3336  } else if (i <= sx+sy+sx) {
3337  nx = ix+sx-(i-(sx+sy));
3338  ny = iy+sy;
3339  } else {
3340  nx = ix;
3341  ny = iy+sy-(i-(sx+sy+sx));
3342  }
3343  /* Check if the spot is free. */
3344  flag = ob_blocked(ob, gen->map, nx, ny);
3345  if (!flag) {
3346  freecount++;
3347  }
3348  }
3349  /* If no free spaces, return. */
3350  if (!freecount)
3351  return -1;
3352 
3353  /* Choose a random valid position */
3354  freecount = RANDOM()%freecount;
3355  for (i = 0; i < sx+sx+sy+sy; i++) {
3356  if (i <= sx) {
3357  nx = i+ix;
3358  ny = iy;
3359  } else if (i <= sx+sy) {
3360  nx = ix+sx;
3361  ny = iy+i-sx;
3362  } else if (i <= sx+sy+sx) {
3363  nx = ix+sx-(i-(sx+sy));
3364  ny = iy+sy;
3365  } else {
3366  nx = ix;
3367  ny = iy+sy-(i-(sx+sy+sx));
3368  }
3369 
3370  /* Make sure it's within map. */
3371  if (nx < 0 || nx >= MAP_WIDTH(gen->map)
3372  || ny < 0 || ny >= MAP_HEIGHT(gen->map))
3373  continue;
3374 
3375  /* Check if the spot is free.*/
3376  flag = ob_blocked(ob, gen->map, nx, ny);
3377  if (!flag) {
3378  freecount--;
3379  if (freecount <= 0) {
3380  *hx = nx;
3381  *hy = ny;
3382  return 0;
3383  }
3384  }
3385  }
3386  return -1;
3387 }
3388 
3408 int object_find_multi_free_spot_within_radius(const object *ob, const object *gen, int *hx, int *hy) {
3409  int genx, geny, genx2, geny2, sx, sy, sx2, sy2, ix, iy, nx, ny, i, flag;
3410  int8_t x, y, radius;
3411  int freecount = 0, freecountstop = 0;
3412  const char *value;
3413  int8_t *x_array;
3414  int8_t *y_array;
3415 
3416  /* If radius is not set, default to 1 */
3417  value = object_get_value(gen, "generator_radius");
3418  if (value) {
3419  radius = (int8_t)strtol(value, NULL, 10);
3420  if (radius < 1) {
3421  radius = 1;
3422  }
3423  } else {
3424  radius = 1;
3425  }
3426 
3427  ob = HEAD(ob);
3428 
3429  object_get_multi_size(ob, &sx, &sy, &sx2, &sy2);
3430  object_get_multi_size(gen, &genx, &geny, &genx2, &geny2);
3431  /*
3432  * sx and sy are now the coords of the bottom right corner
3433  * of ob relative to the head.
3434  * genx and geny are now the coords of the bottom right corner
3435  * of gen relative to the head.
3436  * sx2 and sy2 are now the coords of the head of ob relative
3437  * to the top left corner.
3438  * genx2 and geny2 are now the coords of the head of gen relative
3439  * to the top left corner.
3440  */
3441 
3442  sx++;
3443  sy++;
3444  genx++;
3445  geny++;
3446  /*
3447  * sx, sy, genx, and geny, are now the size of the object,
3448  * excluding parts left and above the head.
3449  */
3450 
3451  ix = gen->x-sx-genx2-radius+1;
3452  iy = gen->y-sy-geny2-radius+1;
3453  sx += genx+sx2+radius*2-1;
3454  sy += geny+sy2+radius*2-1;
3455 
3456  /*
3457  * ix and iy are the map coords of the top left square where
3458  * the head of ob could possibly be placed. sx and sy are now
3459  * the size of the square to search for placement of the head
3460  * relative to ix and iy.
3461  */
3462 
3463  /* Create arrays large enough to hold free space coordinates */
3464  x_array = static_cast<int8_t *>(malloc(sx*sy*sizeof(int8_t)));
3465  y_array = static_cast<int8_t *>(malloc(sx*sy*sizeof(int8_t)));
3466 
3467  /*
3468  * Loop through the area of possible positions for the head of ob object:
3469  */
3470  for (x = 0; x < sx; x++) {
3471  for (y = 0; y < sy; y++) {
3472  nx = ix+x;
3473  ny = iy+y;
3474 
3475 
3476  /* Make sure it's within map. */
3477  if (get_map_flags(gen->map, NULL, nx, ny, NULL, NULL)&P_OUT_OF_MAP) {
3478  continue;
3479  }
3480 
3481  /* Check if the spot is free. */
3482  flag = ob_blocked(ob, gen->map, nx, ny);
3483  if (!flag) {
3484  x_array[freecount] = nx;
3485  y_array[freecount] = ny;
3486  freecount++;
3487  }
3488  }
3489  }
3490  /* If no free spaces, return. */
3491  if (!freecount) {
3492  free(x_array);
3493  free(y_array);
3494  return -1;
3495  }
3496 
3497  /* Choose a random valid position */
3498  freecountstop = RANDOM()%freecount;
3499  for (i = 0; i < freecount; i++) {
3500  nx = x_array[i];
3501  ny = y_array[i];
3502 
3503  /* Check if the spot is free.*/
3504  flag = ob_blocked(ob, gen->map, nx, ny);
3505  if (!flag) {
3506  freecountstop--;
3507  if (freecountstop <= 0) {
3508  *hx = nx;
3509  *hy = ny;
3510  free(x_array);
3511  free(y_array);
3512  return 0;
3513  }
3514  }
3515  }
3516  free(x_array);
3517  free(y_array);
3518  return -1;
3519 }
3520 
3555 int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop) {
3556  int i, index = 0, flag;
3557  static int altern[SIZEOFFREE];
3558 
3559  for (i = start; i < stop; i++) {
3560  flag = ob_blocked(ob, m, x+freearr_x[i], y+freearr_y[i]);
3561  if (!flag)
3562  altern[index++] = i;
3563 
3564  /* Basically, if we find a wall on a space, we cut down the search size.
3565  * In this way, we won't return spaces that are on another side of a wall.
3566  * This mostly work, but it cuts down the search size in all directions -
3567  * if the space being examined only has a wall to the north and empty
3568  * spaces in all the other directions, this will reduce the search space
3569  * to only the spaces immediately surrounding the target area, and
3570  * won't look 2 spaces south of the target space.
3571  */
3572  else if ((flag&AB_NO_PASS) && maxfree[i] < stop)
3573  stop = maxfree[i];
3574  }
3575  if (!index)
3576  return -1;
3577  return altern[RANDOM()%index];
3578 }
3579 
3595 int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y) {
3596  int i;
3597 
3598  for (i = 0; i < SIZEOFFREE; i++) {
3599  if (!ob_blocked(ob, m, x+freearr_x[i], y+freearr_y[i]))
3600  return i;
3601  }
3602  return -1;
3603 }
3604 
3614 static void permute(int *arr, int begin, int end) {
3615  int i, j, tmp, len;
3616 
3617  len = end-begin;
3618  for (i = begin; i < end; i++) {
3619  j = begin+RANDOM()%len;
3620 
3621  tmp = arr[i];
3622  arr[i] = arr[j];
3623  arr[j] = tmp;
3624  }
3625 }
3626 
3638 void get_search_arr(int *search_arr) {
3639  int i;
3640 
3641  for (i = 0; i < SIZEOFFREE; i++) {
3642  search_arr[i] = i;
3643  }
3644 
3645  permute(search_arr, 1, SIZEOFFREE1+1);
3646  permute(search_arr, SIZEOFFREE1+1, SIZEOFFREE2+1);
3647  permute(search_arr, SIZEOFFREE2+1, SIZEOFFREE);
3648 }
3649 
3657 int object_distance(const object *ob1, const object *ob2) {
3658  int i;
3659 
3660  i = (ob1->x-ob2->x)*(ob1->x-ob2->x)+
3661  (ob1->y-ob2->y)*(ob1->y-ob2->y);
3662  return i;
3663 }
3664 
3673 int find_dir_2(int x, int y) {
3674  int q;
3675 
3676  if (!y)
3677  q = -300*x;
3678  else
3679  q = x*100/y;
3680  if (y > 0) {
3681  if (q < -242)
3682  return 3;
3683  if (q < -41)
3684  return 2;
3685  if (q < 41)
3686  return 1;
3687  if (q < 242)
3688  return 8;
3689  return 7;
3690  }
3691  if (q < -242)
3692  return 7;
3693  if (q < -41)
3694  return 6;
3695  if (q < 41)
3696  return 5;
3697  if (q < 242)
3698  return 4;
3699  return 3;
3700 }
3701 
3710 int absdir(int d) {
3711  // Shortcut for modulus that work becuase we have a power of 2
3712  d &= 7;
3713  // 0 needs to be 8
3714  if (!d)
3715  d = 8;
3716  return d;
3717 }
3718 
3728 int dirdiff(int dir1, int dir2) {
3729  int d;
3730 
3731  d = abs(dir1-dir2);
3732  if (d > 4)
3733  d = 8-d;
3734  return d;
3735 }
3736 
3748 static const int reduction_dir[SIZEOFFREE][3] = {
3749  { 0, 0, 0 }, /* 0 */
3750  { 0, 0, 0 }, /* 1 */
3751  { 0, 0, 0 }, /* 2 */
3752  { 0, 0, 0 }, /* 3 */
3753  { 0, 0, 0 }, /* 4 */
3754  { 0, 0, 0 }, /* 5 */
3755  { 0, 0, 0 }, /* 6 */
3756  { 0, 0, 0 }, /* 7 */
3757  { 0, 0, 0 }, /* 8 */
3758  { 8, 1, 2 }, /* 9 */
3759  { 1, 2, -1 }, /* 10 */
3760  { 2, 10, 12 }, /* 11 */
3761  { 2, 3, -1 }, /* 12 */
3762  { 2, 3, 4 }, /* 13 */
3763  { 3, 4, -1 }, /* 14 */
3764  { 4, 14, 16 }, /* 15 */
3765  { 5, 4, -1 }, /* 16 */
3766  { 4, 5, 6 }, /* 17 */
3767  { 6, 5, -1 }, /* 18 */
3768  { 6, 20, 18 }, /* 19 */
3769  { 7, 6, -1 }, /* 20 */
3770  { 6, 7, 8 }, /* 21 */
3771  { 7, 8, -1 }, /* 22 */
3772  { 8, 22, 24 }, /* 23 */
3773  { 8, 1, -1 }, /* 24 */
3774  { 24, 9, 10 }, /* 25 */
3775  { 9, 10, -1 }, /* 26 */
3776  { 10, 11, -1 }, /* 27 */
3777  { 27, 11, 29 }, /* 28 */
3778  { 11, 12, -1 }, /* 29 */
3779  { 12, 13, -1 }, /* 30 */
3780  { 12, 13, 14 }, /* 31 */
3781  { 13, 14, -1 }, /* 32 */
3782  { 14, 15, -1 }, /* 33 */
3783  { 33, 15, 35 }, /* 34 */
3784  { 16, 15, -1 }, /* 35 */
3785  { 17, 16, -1 }, /* 36 */
3786  { 18, 17, 16 }, /* 37 */
3787  { 18, 17, -1 }, /* 38 */
3788  { 18, 19, -1 }, /* 39 */
3789  { 41, 19, 39 }, /* 40 */
3790  { 19, 20, -1 }, /* 41 */
3791  { 20, 21, -1 }, /* 42 */
3792  { 20, 21, 22 }, /* 43 */
3793  { 21, 22, -1 }, /* 44 */
3794  { 23, 22, -1 }, /* 45 */
3795  { 45, 47, 23 }, /* 46 */
3796  { 23, 24, -1 }, /* 47 */
3797  { 24, 9, -1 } /* 48 */
3798 };
3799 
3818 int can_see_monsterP(mapstruct *m, int x, int y, int dir) {
3819  int16_t dx, dy;
3820  int mflags;
3821 
3822  if (dir < 0)
3823  return 0; /* exit condition: invalid direction */
3824 
3825  dx = x+freearr_x[dir];
3826  dy = y+freearr_y[dir];
3827 
3828  mflags = get_map_flags(m, &m, dx, dy, &dx, &dy);
3829 
3830  /* This functional arguably was incorrect before - it was
3831  * checking for P_WALL - that was basically seeing if
3832  * we could move to the monster - this is being more
3833  * literal on if we can see it. To know if we can actually
3834  * move to the monster, we'd need the monster passed in or
3835  * at least its move type.
3836  */
3837  if (mflags&(P_OUT_OF_MAP|P_BLOCKSVIEW))
3838  return 0;
3839 
3840  /* yes, can see. */
3841  if (dir < 9)
3842  return 1;
3843  return can_see_monsterP(m, x, y, reduction_dir[dir][0])|
3844  can_see_monsterP(m, x, y, reduction_dir[dir][1])|
3845  can_see_monsterP(m, x, y, reduction_dir[dir][2]);
3846 }
3847 
3863 int object_can_pick(const object *who, const object *item) {
3864  /* I re-wrote this as a series of if statements
3865  * instead of a nested return (foo & bar && yaz)
3866  * - I think this is much more readable,
3867  * and likely compiler effectively optimizes it the
3868  * same.
3869  */
3870  if (item->weight <= 0)
3871  return 0;
3873  return 0;
3874  if (QUERY_FLAG(item, FLAG_ALIVE))
3875  return 0;
3876  if (item->invisible)
3877  return 0;
3878  if (item->type == TRANSPORT && item->contr != NULL) {
3879  return 0;
3880  }
3881 
3882  /* Weight limit for monsters */
3883  if (who->type != PLAYER && ((uint32_t)(who->weight+who->carrying+item->weight)) > get_weight_limit(who->stats.Str))
3884  return 0;
3885 
3886  /* Can not pick up multipart objects */
3887  if (item->head || item->more)
3888  return 0;
3889 
3890  /* Everything passes, so OK to pick up */
3891  return 1;
3892 }
3893 
3905 object *object_create_clone(object *asrc) {
3906  object *dst = NULL, *tmp, *src, *part, *prev;
3907 
3908  if (!asrc)
3909  return NULL;
3910  src = HEAD(asrc);
3911 
3912  prev = NULL;
3913  for (part = src; part; part = part->more) {
3914  tmp = object_new();
3915  object_copy(part, tmp);
3916  /*
3917  * Need to reset the weight, since object_insert_in_ob() later will
3918  * recompute this field.
3919  */
3920  tmp->carrying = tmp->arch->clone.carrying;
3921  tmp->x -= src->x;
3922  tmp->y -= src->y;
3923  if (!part->head) {
3924  dst = tmp;
3925  tmp->head = NULL;
3926  } else {
3927  tmp->head = dst;
3928  }
3929  tmp->more = NULL;
3930  if (prev)
3931  prev->more = tmp;
3932  prev = tmp;
3933  }
3934  /*** copy inventory ***/
3935  FOR_INV_PREPARE(src, item)
3937  FOR_INV_FINISH();
3938 
3939  return dst;
3940 }
3941 
3952 object *object_find_by_name(const object *who, const char *name) {
3953  const char *name_shared = add_string(name);
3954  object *tmp;
3955 
3956  for (tmp = who->inv; tmp; tmp = tmp->below)
3957  if (tmp->name == name_shared)
3958  break;
3959  free_string(name_shared);
3960  return tmp;
3961 }
3962 
3976 object *object_find_by_type(const object *who, int type) {
3977  object *tmp;
3978 
3979  for (tmp = who->inv; tmp; tmp = tmp->below)
3980  if (tmp->type == type)
3981  return tmp;
3982 
3983  return NULL;
3984 }
3985 
4000 object *object_find_by_type_without_flags(const object *who, int type, int *flags, int num_flags) {
4001  int flag_okay;
4002  for (object *tmp = who->inv; tmp; tmp = tmp->below)
4003  if (tmp->type == type) {
4004  flag_okay = 1;
4005  for (int i = 0; i < num_flags; ++i) {
4006  if (QUERY_FLAG(tmp, flags[i])) {
4007  flag_okay = 0; // A flag we didn't want set was set. Skip this item.
4008  break;
4009  }
4010  }
4011  if (flag_okay) // If flag_okay == 1, then the flags specified were not set
4012  return tmp; // If we reach here, none of the flags specified were set. Just like we wanted.
4013  }
4014 
4015  return NULL;
4016 }
4017 
4033 object *object_find_by_type2(const object *who, int type1, int type2) {
4034  object *tmp;
4035 
4036  for (tmp = who->inv; tmp; tmp = tmp->below)
4037  if (tmp->type == type1 || tmp->type == type2)
4038  return tmp;
4039 
4040  return NULL;
4041 }
4042 
4056 object *object_find_by_tag(const object *who, tag_t tag) {
4057  object *tmp;
4058 
4059  for (tmp = who->inv; tmp; tmp = tmp->below)
4060  if (tmp->count == tag)
4061  return tmp;
4062 
4063  return NULL;
4064 }
4065 
4079 object *object_find_by_type_applied(const object *who, int type) {
4080  object *tmp;
4081 
4082  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4083  if (tmp->type == type && QUERY_FLAG(tmp, FLAG_APPLIED))
4084  return tmp;
4085 
4086  return NULL;
4087 }
4088 
4104 object *object_find_by_type_and_name(const object *who, int type, const char *name) {
4105  object *tmp;
4106 
4107  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4108  if (tmp->type == type && strcmp(tmp->name, name) == 0)
4109  return tmp;
4110 
4111  return NULL;
4112 }
4113 
4129 object *object_find_by_type_and_race(const object *who, int type, const char *race) {
4130  object *tmp;
4131 
4132  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4133  if (tmp->type == type && strcmp(tmp->race, race) == 0)
4134  return tmp;
4135 
4136  return NULL;
4137 }
4138 
4154 object *object_find_by_type_and_slaying(const object *who, int type, const char *slaying) {
4155  object *tmp;
4156 
4157  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4158  if (tmp->type == type && tmp->slaying != NULL && strcmp(tmp->slaying, slaying) == 0)
4159  return tmp;
4160 
4161  return NULL;
4162 }
4163 
4179 object *object_find_by_type_and_skill(const object *who, int type, const char *skill) {
4180  object *tmp;
4181 
4182  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4183  if (tmp->type == type && tmp->skill != NULL && strcmp(tmp->skill, skill) == 0)
4184  return tmp;
4185 
4186  return NULL;
4187 }
4188 
4202 object *object_find_by_flag(const object *who, int flag) {
4203  object *tmp;
4204 
4205  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4206  if (QUERY_FLAG(tmp, flag))
4207  return tmp;
4208 
4209  return NULL;
4210 }
4211 
4225 object *object_find_by_flag_applied(const object *who, int flag) {
4226  object *tmp;
4227 
4228  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4229  if (QUERY_FLAG(tmp, FLAG_APPLIED) && QUERY_FLAG(tmp, flag))
4230  return tmp;
4231 
4232  return NULL;
4233 }
4234 
4248 object *object_find_by_arch_name(const object *who, const char *name) {
4249  object *tmp;
4250 
4251  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4252  if (strcmp(tmp->arch->name, name) == 0)
4253  return tmp;
4254 
4255  return NULL;
4256 }
4257 
4273 object *object_find_by_type_and_arch_name(const object *who, int type, const char *name) {
4274  object *tmp;
4275 
4276  for (tmp = who->inv; tmp != NULL; tmp = tmp->below)
4277  if (tmp->type == type && strcmp(tmp->arch->name, name) == 0)
4278  return tmp;
4279 
4280  return NULL;
4281 }
4282 
4297 object *object_find_by_type_subtype(const object *who, int type, int subtype) {
4298  object *tmp;
4299 
4300  for (tmp = who->inv; tmp; tmp = tmp->below)
4301  if (tmp->type == type && tmp->subtype == subtype)
4302  return tmp;
4303 
4304  return NULL;
4305 }
4306 
4317 key_value *object_get_key_value(const object *ob, const char *key) {
4318  key_value *link;
4319 
4320  for (link = ob->key_values; link != NULL; link = link->next) {
4321  if (link->key == key) {
4322  return link;
4323  }
4324  }
4325 
4326  return NULL;
4327 }
4328 
4342 const char *object_get_value(const object *op, const char *const key) {
4343  key_value *link;
4344  const char *canonical_key;
4345 
4346  canonical_key = find_string(key);
4347 
4348  if (canonical_key == NULL) {
4349  /* 1. There being a field named key on any object
4350  * implies there'd be a shared string to find.
4351  * 2. Since there isn't, no object has this field.
4352  * 3. Therefore, *this *object doesn't have this field.
4353  */
4354  return NULL;
4355  }
4356 
4357  /* This is copied from object_get_key_value() above -
4358  * only 4 lines, and saves the function call overhead.
4359  */
4360  for (link = op->key_values; link != NULL; link = link->next) {
4361  if (link->key == canonical_key) {
4362  return link->value;
4363  }
4364  }
4365  return NULL;
4366 }
4367 
4372 bool object_value_set(const object *op, const char *const key) {
4373  const char *ret = object_get_value(op, key);
4374  if (ret == NULL || (strcmp(ret, "") == 0) || (strcmp(ret, "0") == 0)) {
4375  return false;
4376  }
4377  return true;
4378 }
4379 
4386 bool object_value_set_shared(const object *op, sstring key) {
4387  for (key_value *link = op->key_values; link != NULL; link = link->next) {
4388  if (link->key == key) {
4389  if ((strcmp(link->value, "") == 0) || (strcmp(link->value, "0") == 0)) {
4390  return false;
4391  }
4392  return true;
4393  }
4394  }
4395  return false;
4396 }
4397 
4412 static int object_set_value_s(object *op, const char *canonical_key, const char *value, int add_key) {
4413  key_value *field = NULL, *last = NULL;
4414 
4415  for (field = op->key_values; field != NULL; field = field->next) {
4416  if (field->key != canonical_key) {
4417  last = field;
4418  continue;
4419  }
4420 
4421  if (field->value)
4422  FREE_AND_CLEAR_STR(field->value);
4423  if (value)
4424  field->value = add_string(value);
4425  else {
4426  /* Basically, if the archetype has this key set,
4427  * we need to store the null value so when we save
4428  * it, we save the empty value so that when we load,
4429  * we get this value back again.
4430  */
4431  if (object_get_key_value(&op->arch->clone, canonical_key))
4432  field->value = NULL;
4433  else {
4434  /* Delete this link */
4435  if (field->key)
4436  FREE_AND_CLEAR_STR(field->key);
4437  if (field->value)
4438  FREE_AND_CLEAR_STR(field->value);
4439  if (last)
4440  last->next = field->next;
4441  else
4442  op->key_values = field->next;
4443  free(field);
4444  }
4445  }
4446  return TRUE;
4447  }
4448  /* IF we get here, key doesn't exist */
4449 
4450  /* No field, we'll have to add it. */
4451 
4452  if (!add_key) {
4453  return FALSE;
4454  }
4455  /* There isn't any good reason to store a null
4456  * value in the key/value list. If the archetype has
4457  * this key, then we should also have it, so shouldn't
4458  * be here. If user wants to store empty strings,
4459  * should pass in ""
4460  */
4461  if (value == NULL)
4462  return TRUE;
4463 
4464  field = static_cast<key_value *>(malloc(sizeof(key_value)));
4465 
4466  field->key = add_refcount(canonical_key);
4467  field->value = add_string(value);
4468  /* Usual prepend-addition. */
4469  field->next = op->key_values;
4470  op->key_values = field;
4471 
4472  return TRUE;
4473 }
4474 
4495 int object_set_value(object *op, const char *key, const char *value, int add_key) {
4496  const char *canonical_key = NULL;
4497  int floating_ref = FALSE;
4498  int ret;
4499 
4500  /* HACK This mess is to make sure set_ob_value() passes a shared string
4501  * to object_get_key_value(), without leaving a leaked refcount.
4502  */
4503 
4504  canonical_key = find_string(key);
4505  if (canonical_key == NULL) {
4506  canonical_key = add_string(key);
4507  floating_ref = TRUE;
4508  }
4509 
4510  ret = object_set_value_s(op, canonical_key, value, add_key);
4511 
4512  if (floating_ref) {
4513  free_string(canonical_key);
4514  }
4515 
4516  return ret;
4517 }
4518 
4570 int object_matches_string(object *pl, object *op, const char *name) {
4571  char *cp, local_name[MAX_BUF], name_op[MAX_BUF], name_short[HUGE_BUF], bname_s[MAX_BUF], bname_p[MAX_BUF];
4572  int count, retval = 0;
4573  /* strtok is destructive to name */
4574  safe_strncpy(local_name, name, sizeof(local_name));
4575  sstring custom_name = object_get_value(op, CUSTOM_NAME_FIELD);
4576 
4577  for (cp = strtok(local_name, ","); cp; cp = strtok(NULL, ",")) {
4578  while (cp[0] == ' ')
4579  ++cp; /* get rid of spaces */
4580 
4581  /* LOG(llevDebug, "Trying to match %s\n", cp);*/
4582  /* All is a very generic match - low match value */
4583  if (!strcmp(cp, "all"))
4584  return 1;
4585 
4586  /* unpaid is a little more specific */
4587  if (!strcmp(cp, "unpaid") && QUERY_FLAG(op, FLAG_UNPAID))
4588  return 2;
4589  if (!strcmp(cp, "cursed")
4592  return 2;
4593 
4594  if (!strcmp(cp, "unlocked") && !QUERY_FLAG(op, FLAG_INV_LOCKED))
4595  return 2;
4596 
4597  /* Allow for things like '100 arrows' */
4598  count = atoi(cp);
4599  if (*cp == '+' || *cp == '-')
4600  count=0; // Let +/- searches look in description for magic bonuses
4601  if (count != 0) {
4602  cp = strchr(cp, ' ');
4603  while (cp && cp[0] == ' ')
4604  ++cp; /* get rid of spaces */
4605  } else {
4606  if (pl->type == PLAYER)
4607  count = pl->contr->count;
4608  else
4609  count = 0;
4610  }
4611 
4612  if (!cp || cp[0] == '\0' || count < 0)
4613  continue;
4614 
4615  /* The code here should go from highest retval to lowest. That
4616  * is because of the 'else' handling - we don't want to match on
4617  * something and set a low retval, even though it may match a higher retcal
4618  * later. So keep it in descending order here, so we try for the best
4619  * match first, and work downward.
4620  */
4621  query_name(op, name_op, MAX_BUF);
4622  query_short_name(op, name_short, HUGE_BUF);
4623  query_base_name(op, 0, bname_s, MAX_BUF);
4624  query_base_name(op, 1, bname_p, MAX_BUF);
4625 
4626  if (!strcasecmp(cp, name_op))
4627  retval = 20;
4628  else if (!strcasecmp(cp, name_short))
4629  retval = 18;
4630  else if (!strcasecmp(cp, bname_s))
4631  retval = 16;
4632  else if (!strcasecmp(cp, bname_p))
4633  retval = 16;
4634  else if (custom_name && !strcasecmp(cp, custom_name))
4635  retval = 15;
4636  else if (!strncasecmp(cp, bname_s, strlen(cp)))
4637  retval = 14;
4638  else if (!strncasecmp(cp, bname_p, strlen(cp)))
4639  retval = 14;
4640  /* Do substring checks, so things like 'Str+1' will match.
4641  * retval of these should perhaps be lower - they are lower
4642  * then the specific strcasecmp aboves, but still higher than
4643  * some other match criteria.
4644  */
4645  else if (strstr(bname_p, cp))
4646  retval = 12;
4647  else if (strstr(bname_s, cp))
4648  retval = 12;
4649  else if (strstr(name_short, cp))
4650  retval = 12;
4651  /* Check against plural/non plural based on count. */
4652  else if (count > 1 && !strcasecmp(cp, op->name_pl)) {
4653  retval = 6;
4654  } else if (count == 1 && !strcasecmp(op->name, cp)) {
4655  retval = 6;
4656  }
4657  /* base name matched - not bad */
4658  else if (strcasecmp(cp, op->name) == 0 && !count)
4659  retval = 4;
4660  /* Check for partial custom name, but give a real low priority */
4661  else if (custom_name && strstr(custom_name, cp))
4662  retval = 3;
4663 
4664  if (retval) {
4665  if (pl->type == PLAYER)
4666  pl->contr->count = count;
4667  return retval;
4668  }
4669  }
4670  return 0;
4671 }
4672 
4681 void object_fix_multipart(object *tmp) {
4682  archetype *at;
4683  object *op, *last;
4684 
4685  if (!tmp->map) {
4686  LOG(llevError, "object_fix_multipart: not on a map!\n");
4687  return;
4688  }
4689 
4690  /* already multipart - don't do anything more */
4691  if (tmp->head || tmp->more)
4692  return;
4693 
4694  /* If there is nothing more to this object, this for loop
4695  * won't do anything.
4696  */
4697  for (at = tmp->arch->more, last = tmp; at != NULL; at = at->more, last = op) {
4698  op = arch_to_object(at);
4699 
4700  /* update x,y coordinates */
4701  op->x += tmp->x;
4702  op->y += tmp->y;
4703  op->head = tmp;
4704  op->map = tmp->map;
4705  last->more = op;
4706  if (tmp->name != op->name) {
4707  if (op->name)
4708  free_string(op->name);
4709  op->name = add_string(tmp->name);
4710  }
4711  if (tmp->title != op->title) {
4712  if (op->title)
4713  free_string(op->title);
4714  op->title = add_string(tmp->title);
4715  }
4716  /* we could link all the parts onto tmp, and then just
4717  * call object_insert_in_map once, but the effect is the same,
4718  * as object_insert_in_map will call itself with each part, and
4719  * the coding is simpler to just to it here with each part.
4720  */
4722  } /* for at = tmp->arch->more */
4723 }
4724 
4740 void object_get_multi_size(const object *ob, int *sx, int *sy, int *hx, int *hy) {
4741  archetype *part;
4742  int maxx = 0, maxy = 0, minx = 0, miny = 0;
4743 
4744  ob = HEAD(ob);
4745  *sx = 1;
4746  *sy = 1;
4747  if (ob->arch->more) {
4748  for (part = ob->arch; part; part = part->more) {
4749  if (part->clone.x > maxx)
4750  maxx = part->clone.x;
4751  if (part->clone.y > maxy)
4752  maxy = part->clone.y;
4753  if (part->clone.x < minx)
4754  minx = part->clone.x;
4755  if (part->clone.y < miny)
4756  miny = part->clone.y;
4757  }
4758  }
4759  *sx = maxx;
4760  *sy = maxy;
4761  if (hx)
4762  *hx = -minx;
4763  if (hy)
4764  *hy = -miny;
4765 }
4766 
4787 void object_insert_to_free_spot_or_free(object *op, mapstruct *map, int x, int y, int start, int stop, object *originator) {
4788  int pos;
4789 
4790  pos = object_find_free_spot(op, map, x, y, start, stop);
4791  if (pos == -1) {
4793  return;
4794  }
4795 
4796  object_insert_in_map_at(op, map, originator, 0, x+freearr_x[pos], y+freearr_y[pos]);
4797 }
4798 
4807 void object_set_msg(object *op, const char *msg) {
4808  if (op->msg != NULL) {
4809  free_string(op->msg);
4810  }
4811 
4812  if (msg != NULL) {
4813  // If the message does not have a trailing newline, add one.
4814  if (*msg != '\0' && strchr(msg, '\0')[-1] != '\n') {
4817  stringbuffer_append_string(sb, "\n");
4818  op->msg = stringbuffer_finish_shared(sb);
4819  } else {
4820  op->msg = add_string(msg);
4821  }
4822  } else {
4823  op->msg = NULL;
4824  }
4825 }
4826 
4828 const char *const move_name[] = {
4829  "walk",
4830  "fly_low",
4831  "fly_high",
4832  "swim",
4833  "boat",
4834  NULL
4835 };
4836 
4837 /* This array equates the FLAG_ values with the V_ values. Use -1 to
4838  * put gaps in the array that should not be processed.
4839  * The order matches the order of the define values in 'define.h'.
4840  */
4847 static const char *const flag_names[NUM_FLAGS+1] = {
4848  "alive", "wiz", NULL, NULL, "was_wiz", "applied", "unpaid",
4849  "can_use_shield", "no_pick", "client_anim_sync", "client_anim_random", /* 10 */
4850  "is_animated", NULL /* FLAG_DIALOG_PARSED, not saved */,
4851  NULL /* flying */, "monster", "friendly", "generator",
4852  "is_thrown", "auto_apply", "treasure", "player sold", /* 20 */
4853  "see_invisible", "can_roll", "overlay_floor",
4854  "is_turnable", NULL /* walk_off */, NULL /* fly_on */,
4855  NULL /*fly_off*/, "is_used_up", "identified", "reflecting", /* 30 */
4856  "changing", "splitting", "hitback", "startequip",
4857  "blocksview", "undead", "scared", "unaggressive",
4858  "reflect_missile", "reflect_spell", /* 40 */
4859  "no_magic", "no_fix_player", "is_lightable", "tear_down",
4860  "run_away", NULL /*pass_thru */, NULL /*can_pass_thru*/,
4861  NULL /*"pick_up"*/, "unique", "no_drop", /* 50 */
4862  NULL /* wizcast*/, "can_cast_spell", "can_use_scroll", "can_use_range",
4863  "can_use_bow", "can_use_armour", "can_use_weapon",
4864  "can_use_ring", "has_ready_range", "has_ready_bow", /* 60 */
4865  "xrays", NULL, "is_floor", "lifesave", "no_strength", "sleep",
4866  "stand_still", "random_movement", "only_attack", "confused", /* 70 */
4867  "stealth", NULL, NULL, "cursed", "damned",
4868  "see_anywhere", "known_magical", "known_cursed",
4869  "can_use_skill", "been_applied", /* 80 */
4870  "has_ready_scroll", NULL, NULL,
4871  NULL, "make_invisible", "inv_locked", "is_wooded",
4872  "is_hilly", "has_ready_skill", "has_ready_weapon", /* 90 */
4873  "no_skill_ident", "is_blind", "can_see_in_dark", "is_cauldron",
4874  NULL, "no_steal", "one_hit", NULL, "berserk", "neutral", /* 100 */
4875  "no_attack", "no_damage", NULL, NULL, "activate_on_push",
4876  "activate_on_release", "is_water", "use_content_on_gen", NULL, "is_buildable", /* 110 */
4877  NULL, "blessed", "known_blessed"
4878 };
4879 
4889 {
4890  static char retbuf[MAX_BUF], retbuf_all[MAX_BUF];
4891  int i, all_count = 0, count;
4892 
4893  strcpy(retbuf, "");
4894  strcpy(retbuf_all, " all");
4895 
4896  /* Quick check, and probably fairly common */
4897  if (mt == MOVE_ALL) {
4898  stringbuffer_append_string(sb, "all");
4899  return;
4900  }
4901  if (mt == 0) {
4902  stringbuffer_append_string(sb, "0");
4903  return;
4904  }
4905 
4906  /* We basically slide the bits down. Why look at MOVE_ALL?
4907  * because we may want to return a string like 'all -swim',
4908  * and if we just looked at mt, we couldn't get that.
4909  */
4910  for (i = MOVE_ALL, count = 0; i != 0; i >>= 1, count++) {
4911  if (mt&(1<<count)) {
4912  strcat(retbuf, " ");
4913  strcat(retbuf, move_name[count]);
4914  } else {
4915  strcat(retbuf_all, " -");
4916  strcat(retbuf_all, move_name[count]);
4917  all_count++;
4918  }
4919  }
4920  /* Basically, if there is a single negation, return it, eg
4921  * 'all -swim'. But more than that, just return the
4922  * enumerated values. It doesn't make sense to return
4923  * 'all -walk -fly_low' - it is shorter to return 'fly_high swim'
4924  */
4925  if (all_count <= 1)
4926  stringbuffer_append_string(sb, retbuf_all+1);
4927  else
4928  stringbuffer_append_string(sb, retbuf+1);
4929 }
4930 
4932 static inline void ADD_STRINGLINE_ENTRY(StringBuffer *sb, const char *name, const char *value) {
4935  stringbuffer_append_string(sb, "\n");
4936 }
4937 
4939 static inline void FAST_SAVE_LONG(StringBuffer *sb, const char *name, const long value) {
4942  stringbuffer_append_char(sb, '\n');
4943 }
4944 
4946 static inline void FAST_SAVE_DOUBLE(StringBuffer *sb, const char *name, const double value) {
4947  stringbuffer_append_printf(sb, "%s%f\n", name, value);
4948 }
4949 
4957  for (int i = 0; i < 4; i++) {
4958  int idx = ffs((*diff)[i]);
4959  if (idx != 0) {
4960  int bit = idx - 1;
4961  // Clear difference bit.
4962  (*diff)[i] &= ~(1 << bit);
4963  return 32*i + bit;
4964  }
4965  }
4966  return -1;
4967 }
4968 
4982 void get_ob_diff(StringBuffer *sb, const object *op, const object *op2) {
4983  static char buf2[64];
4984  int tmp;
4985  int i;
4986  key_value *my_field;
4987  key_value *arch_field;
4988 
4989  /* This saves the key/value lists. We do it first so that any
4990  * keys that match field names will be overwritten by the loader.
4991  */
4992  for (my_field = op->key_values; my_field != NULL; my_field = my_field->next) {
4993  /* Find the field in the opposing member. */
4994  arch_field = object_get_key_value(op2, my_field->key);
4995 
4996  /* If there's no partnering field, or it's got a different value, save our field. */
4997  if (arch_field == NULL || my_field->value != arch_field->value) {
4998  stringbuffer_append_string(sb, my_field->key);
4999  stringbuffer_append_string(sb, " ");
5000  /* If this is null, then saving it as a space should
5001  * cause it to be null again.
5002  */
5003  if (my_field->value)
5004  stringbuffer_append_string(sb, my_field->value);
5005  stringbuffer_append_string(sb, "\n");
5006  }
5007  }
5008  /* We don't need to worry about the arch's extra fields - they
5009  * will get taken care of the object_copy() function.
5010  */
5011 
5012  if (op->name && op->name != op2->name) {
5013  ADD_STRINGLINE_ENTRY(sb, "name ", op->name);
5014  }
5015  if (op->name_pl && op->name_pl != op2->name_pl) {
5016  ADD_STRINGLINE_ENTRY(sb, "name_pl ", op->name_pl);
5017  }
5018  if (op->anim_suffix && op->anim_suffix != op2->anim_suffix) {
5019  ADD_STRINGLINE_ENTRY(sb, "anim_suffix ", op->anim_suffix);
5020  }
5021  if (op->title && op->title != op2->title) {
5022  ADD_STRINGLINE_ENTRY(sb, "title ", op->title);
5023  }
5024  if (op->race && op->race != op2->race) {
5025  ADD_STRINGLINE_ENTRY(sb, "race ", op->race);
5026  }
5027  if (op->slaying && op->slaying != op2->slaying) {
5028  ADD_STRINGLINE_ENTRY(sb, "slaying ", op->slaying);
5029  }
5030  if (op->skill && op->skill != op2->skill) {
5031  ADD_STRINGLINE_ENTRY(sb, "skill ", op->skill);
5032  }
5033  if (op->msg && op->msg != op2->msg) {
5034  stringbuffer_append_string(sb, "msg\n");
5035  stringbuffer_append_string(sb, op->msg);
5036  stringbuffer_append_string(sb, "endmsg\n");
5037  }
5038  if (op->lore && op->lore != op2->lore) {
5039  stringbuffer_append_string(sb, "lore\n");
5040  stringbuffer_append_string(sb, op->lore);
5041  stringbuffer_append_string(sb, "endlore\n");
5042  }
5043  if (op->other_arch != op2->other_arch && op->other_arch != NULL && op->other_arch->name) {
5044  ADD_STRINGLINE_ENTRY(sb, "other_arch ", op->other_arch->name);
5045  }
5046  if (op->face != op2->face) {
5047  ADD_STRINGLINE_ENTRY(sb, "face ", op->face->name);
5048  }
5049 
5050  if (op->animation != op2->animation) {
5051  if (op->animation) {
5052  ADD_STRINGLINE_ENTRY(sb, "animation ", op->animation->name);
5053  if (!QUERY_FLAG (op, FLAG_ANIMATE)) {
5054  stringbuffer_append_string(sb, "is_animated 0\n");
5055  }
5056  } else {
5057  stringbuffer_append_string(sb, "animation NONE\n");
5058  }
5059  }
5060  if (op->stats.Str != op2->stats.Str)
5061  FAST_SAVE_LONG(sb, "Str ", op->stats.Str);
5062  if (op->stats.Dex != op2->stats.Dex)
5063  FAST_SAVE_LONG(sb, "Dex ", op->stats.Dex);
5064  if (op->stats.Con != op2->stats.Con)
5065  FAST_SAVE_LONG(sb, "Con ", op->stats.Con);
5066  if (op->stats.Wis != op2->stats.Wis)
5067  FAST_SAVE_LONG(sb, "Wis ", op->stats.Wis);
5068  if (op->stats.Pow != op2->stats.Pow)
5069  FAST_SAVE_LONG(sb, "Pow ", op->stats.Pow);
5070  if (op->stats.Cha != op2->stats.Cha)
5071  FAST_SAVE_LONG(sb, "Cha ", op->stats.Cha);
5072  if (op->stats.Int != op2->stats.Int)
5073  FAST_SAVE_LONG(sb, "Int ", op->stats.Int);
5074  if (op->stats.hp != op2->stats.hp)
5075  FAST_SAVE_LONG(sb, "hp ", op->stats.hp);
5076  if (op->stats.maxhp != op2->stats.maxhp)
5077  FAST_SAVE_LONG(sb, "maxhp ", op->stats.maxhp);
5078  if (op->stats.sp != op2->stats.sp)
5079  FAST_SAVE_LONG(sb, "sp ", op->stats.sp);
5080  if (op->stats.maxsp != op2->stats.maxsp)
5081  FAST_SAVE_LONG(sb, "maxsp ", op->stats.maxsp);
5082  if (op->stats.grace != op2->stats.grace)
5083  FAST_SAVE_LONG(sb, "grace ", op->stats.grace);
5084  if (op->stats.maxgrace != op2->stats.maxgrace)
5085  FAST_SAVE_LONG(sb, "maxgrace ", op->stats.maxgrace);
5086 
5087  if (op->stats.exp != op2->stats.exp) {
5088  snprintf(buf2, sizeof(buf2), "%" FMT64, op->stats.exp);
5089  ADD_STRINGLINE_ENTRY(sb, "exp ", buf2);
5090  }
5091 
5092  if (op->total_exp != op2->total_exp) {
5093  snprintf(buf2, sizeof(buf2), "%" FMT64, op->total_exp);
5094  ADD_STRINGLINE_ENTRY(sb, "total_exp ", buf2);
5095  }
5096 
5097  if (op->expmul != op2->expmul)
5098  FAST_SAVE_DOUBLE(sb, "expmul ", op->expmul);
5099  if (op->stats.food != op2->stats.food)
5100  FAST_SAVE_LONG(sb, "food ", op->stats.food);
5101  if (op->stats.dam != op2->stats.dam)
5102  FAST_SAVE_LONG(sb, "dam ", op->stats.dam);
5103  if (op->stats.luck != op2->stats.luck)
5104  FAST_SAVE_LONG(sb, "luck ", op->stats.luck);
5105  if (op->stats.wc != op2->stats.wc)
5106  FAST_SAVE_LONG(sb, "wc ", op->stats.wc);
5107  if (op->stats.ac != op2->stats.ac)
5108  FAST_SAVE_LONG(sb, "ac ", op->stats.ac);
5109  if (op->x != op2->x)
5110  FAST_SAVE_LONG(sb, "x ", op->x);
5111  if (op->y != op2->y)
5112  FAST_SAVE_LONG(sb, "y ", op->y);
5113  if (op->elevation != op2->elevation)
5114  FAST_SAVE_LONG(sb, "elevation ", op->elevation);
5115  if (op->speed != op2->speed) {
5116  FAST_SAVE_DOUBLE(sb, "speed ", op->speed);
5117  }
5118  if (op->speed > 0 && op->speed_left != op2->speed_left) {
5119  FAST_SAVE_DOUBLE(sb, "speed_left ", op->speed_left);
5120  }
5121  if (op->weapon_speed != op2->weapon_speed) {
5122  FAST_SAVE_DOUBLE(sb, "weapon_speed ", op->weapon_speed);
5123  }
5124  if (op->weapon_speed > 0 && op->weapon_speed_left != op2->weapon_speed_left) {
5125  FAST_SAVE_DOUBLE(sb, "weapon_speed_left ", op->weapon_speed_left);
5126  }
5127  if (op->move_status != op2->move_status)
5128  FAST_SAVE_LONG(sb, "move_state ", op->move_status);
5129  if (op->attack_movement != op2->attack_movement)
5130  FAST_SAVE_LONG(sb, "attack_movement ", op->attack_movement);
5131  if (op->nrof != op2->nrof)
5132  FAST_SAVE_LONG(sb, "nrof ", op->nrof);
5133  if (op->level != op2->level)
5134  FAST_SAVE_LONG(sb, "level ", op->level);
5135  if (op->direction != op2->direction)
5136  FAST_SAVE_LONG(sb, "direction ", op->direction);
5137  if (op->type != op2->type)
5138  FAST_SAVE_LONG(sb, "type ", op->type);
5139  if (op->subtype != op2->subtype)
5140  FAST_SAVE_LONG(sb, "subtype ", op->subtype);
5141  if (op->attacktype != op2->attacktype)
5142  FAST_SAVE_LONG(sb, "attacktype ", op->attacktype);
5143 
5144  for (tmp = 0; tmp < NROFATTACKS; tmp++) {
5145  if (op->resist[tmp] != op2->resist[tmp]) {
5146  stringbuffer_append_string(sb, "resist_");
5147  FAST_SAVE_LONG(sb, resist_save[tmp], op->resist[tmp]);
5148  }
5149  }
5150 
5151  if (op->path_attuned != op2->path_attuned)
5152  FAST_SAVE_LONG(sb, "path_attuned ", op->path_attuned);
5153  if (op->path_repelled != op2->path_repelled)
5154  FAST_SAVE_LONG(sb, "path_repelled ", op->path_repelled);
5155  if (op->path_denied != op2->path_denied)
5156  FAST_SAVE_LONG(sb, "path_denied ", op->path_denied);
5157  if (op->material != op2->material)
5158  FAST_SAVE_LONG(sb, "material ", op->material);
5159  if (op->materialname && op->materialname != op2->materialname) {
5160  ADD_STRINGLINE_ENTRY(sb, "materialname ", op->materialname);
5161  }
5162  if (op->value != op2->value)
5163  FAST_SAVE_LONG(sb, "value ", op->value);
5164  if (op->carrying != op2->carrying)
5165  FAST_SAVE_LONG(sb, "carrying ", op->carrying);
5166  if (op->weight != op2->weight)
5167  FAST_SAVE_LONG(sb, "weight ", op->weight);
5168  if (op->invisible != op2->invisible)
5169  FAST_SAVE_LONG(sb, "invisible ", op->invisible);
5170  if (op->state != op2->state)
5171  FAST_SAVE_LONG(sb, "state ", op->state);
5172  if (op->magic != op2->magic)
5173  FAST_SAVE_LONG(sb, "magic ", op->magic);
5174  if (op->last_heal != op2->last_heal)
5175  FAST_SAVE_LONG(sb, "last_heal ", op->last_heal);
5176  if (op->last_sp != op2->last_sp)
5177  FAST_SAVE_LONG(sb, "last_sp ", op->last_sp);
5178  if (op->last_grace != op2->last_grace)
5179  FAST_SAVE_LONG(sb, "last_grace ", op->last_grace);
5180  if (op->last_eat != op2->last_eat)
5181  FAST_SAVE_LONG(sb, "last_eat ", op->last_eat);
5183  FAST_SAVE_LONG(sb, "connected ", tmp);
5184  if (op->glow_radius != op2->glow_radius)
5185  FAST_SAVE_LONG(sb, "glow_radius ", op->glow_radius);
5186  if (op->randomitems != op2->randomitems) {
5187  ADD_STRINGLINE_ENTRY(sb, "randomitems ", op->randomitems ? op->randomitems->name : "none");
5188  }
5189 
5190  if (op->run_away != op2->run_away)
5191  FAST_SAVE_LONG(sb, "run_away ", op->run_away);
5192  if (op->pick_up != op2->pick_up)
5193  FAST_SAVE_LONG(sb, "pick_up ", op->pick_up);
5194  if (op->weight_limit != op2->weight_limit)
5195  FAST_SAVE_LONG(sb, "container ", op->weight_limit);
5196  if (op->will_apply != op2->will_apply)
5197  FAST_SAVE_LONG(sb, "will_apply ", op->will_apply);
5198  if (op->smoothlevel != op2->smoothlevel)
5199  FAST_SAVE_LONG(sb, "smoothlevel ", op->smoothlevel);
5200 
5201  if (op->map_layer != op2->map_layer)
5202  ADD_STRINGLINE_ENTRY(sb, "map_layer ", map_layer_name[op->map_layer]);
5203 
5204  if (op->weapontype && op->weapontype != op2->weapontype) {
5205  FAST_SAVE_LONG(sb, "weapontype ", op->weapontype);
5206  }
5207  if (op->client_type && op->client_type != op2->client_type) {
5208  FAST_SAVE_LONG(sb, "client_type ", op->client_type);
5209  }
5210 
5211  if (op->item_power != op2->item_power) {
5212  FAST_SAVE_LONG(sb, "item_power ", op->item_power);
5213  }
5214 
5215  if (op->duration != op2->duration)
5216  FAST_SAVE_LONG(sb, "duration ", op->duration);
5217 
5218  if (op->range != op2->range)
5219  FAST_SAVE_LONG(sb, "range ", op->range);
5220 
5221  if (op->range_modifier != op2->range_modifier)
5222  FAST_SAVE_LONG(sb, "range_modifier ", op->range_modifier);
5223 
5224  if (op->duration_modifier != op2->duration_modifier)
5225  FAST_SAVE_LONG(sb, "duration_modifier ", op->duration_modifier);
5226 
5227  if (op->dam_modifier != op2->dam_modifier)
5228  FAST_SAVE_LONG(sb, "dam_modifier ", op->dam_modifier);
5229 
5230  if (op->gen_sp_armour != op2->gen_sp_armour) {
5231  FAST_SAVE_LONG(sb, "gen_sp_armour ", op->gen_sp_armour);
5232  }
5233 
5234  /* I've kept the old int move type saving code commented out.
5235  * In an ideal world, we'd know if we want to do a quick
5236  * save (say to a temp map, where we don't care about strings),
5237  * or a slower save/dm dump, where printing out strings is handy.
5238  */
5239  if (op->move_type != op2->move_type) {
5240  /*FAST_SAVE_LONG(sb, "move_type ", op->move_type)*/
5241  stringbuffer_append_string(sb, "move_type ");
5242  get_string_move_type(sb, op->move_type);
5243  stringbuffer_append_string(sb, "\n");
5244  }
5245  if (op->move_block != op2->move_block) {
5246  /*FAST_SAVE_LONG(sb, "move_block ", op->move_block)*/
5247  stringbuffer_append_string(sb, "move_block ");
5248  get_string_move_type(sb, op->move_block);
5249  stringbuffer_append_string(sb, "\n");
5250  }
5251  if (op->move_allow != op2->move_allow) {
5252  /*FAST_SAVE_LONG(sb, "move_allow ", op->move_allow);*/
5253  stringbuffer_append_string(sb, "move_allow ");
5254  get_string_move_type(sb, op->move_allow);
5255  stringbuffer_append_string(sb, "\n");
5256  }
5257  if (op->move_on != op2->move_on) {
5258  /*FAST_SAVE_LONG(sb, "move_on ", op->move_on);*/
5259  stringbuffer_append_string(sb, "move_on ");
5260  get_string_move_type(sb, op->move_on);
5261  stringbuffer_append_string(sb, "\n");
5262  }
5263  if (op->move_off != op2->move_off) {
5264  /*FAST_SAVE_LONG(sb, "move_off ", op->move_off);*/
5265  stringbuffer_append_string(sb, "move_off ");
5266  get_string_move_type(sb, op->move_off);
5267  stringbuffer_append_string(sb, "\n");
5268  }
5269  if (op->move_slow != op2->move_slow) {
5270  /*FAST_SAVE_LONG(sb, "move_slow ", op->move_slow);*/
5271  stringbuffer_append_string(sb, "move_slow ");
5272  get_string_move_type(sb, op->move_slow);
5273  stringbuffer_append_string(sb, "\n");
5274  }
5275 
5276  if (op->move_slow_penalty != op2->move_slow_penalty) {
5277  FAST_SAVE_DOUBLE(sb, "move_slow_penalty ", op->move_slow_penalty);
5278  }
5279 
5280  // Make sure we save sound_chance overrides, or sound chance only works on the initial map load.
5281  if (op->sound_chance != op2->sound_chance) {
5282  FAST_SAVE_LONG(sb, "sound_chance ", op->sound_chance);
5283  }
5284 
5285  ob_flags diff_flags;
5286  compare_flags(&diff_flags, op, op2);
5287  int flag;
5288  while ((flag = flags_differ(&diff_flags)) != -1) {
5289  if (flag_names[flag]) {
5290  ADD_STRINGLINE_ENTRY(sb, flag_names[flag], QUERY_FLAG(op, flag) ? " 1" : " 0");
5291  }
5292  }
5293 
5294  /* Save body locations */
5295  for (i = 0; i < NUM_BODY_LOCATIONS; i++) {
5296  if (op->body_info[i] != op2->body_info[i]) {
5297  stringbuffer_append_string(sb, body_locations[i].save_name);
5298  FAST_SAVE_LONG(sb, " ", op->body_info[i]);
5299  }
5300  }
5301 }
5302 
5307 void save_object_in_sb(StringBuffer *sb, object *op, const int flag) {
5308  /* If the object has no_save set, just return */
5309  if (QUERY_FLAG(op, FLAG_NO_SAVE)) {
5310  return;
5311  }
5312 
5313  /* Even if the object does have an owner, it would seem that we should
5314  * still save it.
5315  */
5316  if (object_get_owner(op) != NULL) {
5317  return;
5318  }
5319 
5320  /* If it is unpaid and we don't want to save those, just return. */
5321  if (!(flag&SAVE_FLAG_SAVE_UNPAID) && (QUERY_FLAG(op, FLAG_UNPAID))) {
5322  return;
5323  }
5324 
5325  archetype *at = op->arch;
5326  if (at == NULL)
5327  at = empty_archetype;
5328 
5329  ADD_STRINGLINE_ENTRY(sb, "arch ", at->name);
5330 
5331  if (at->reference_count > 0) {
5332  /* The object is a custom item/monster, so we handle its save differently.
5333  * We compare the custom archetype to the "original" one, then only save hp/gr/sp
5334  * which are the only values we can't recompute later - all others are modified by items in inventory.
5335  * Note that hp/gr/sp will appear twice in save, but last value will take precedence.
5336  */
5337  archetype *original = find_archetype(at->name);
5338  if (!original) {
5339  LOG(llevError, "could not find original archetype %s for custom monster!\n", at->name);
5340  abort();
5341  }
5342  get_ob_diff(sb, &at->clone, &original->clone);
5343  if (op->stats.hp != at->clone.stats.hp)
5344  FAST_SAVE_LONG(sb, "hp ", op->stats.hp);
5345  if (op->stats.sp != at->clone.stats.sp)
5346  FAST_SAVE_LONG(sb, "sp ", op->stats.sp);
5347  if (op->stats.grace != at->clone.stats.grace)
5348  FAST_SAVE_LONG(sb, "grace ", op->stats.grace);
5349  if (op->x != at->clone.x)
5350  FAST_SAVE_LONG(sb, "x ", op->x);
5351  if (op->y != at->clone.y)
5352  FAST_SAVE_LONG(sb, "y ", op->y);
5353  } else if (op->artifact != NULL) {
5354  /* if op is an artifact, then find the "standard" artifact to use that for the diff */
5355  object *base;
5356  const artifact *artifact;
5357 
5358  artifact = find_artifact(op, op->artifact);
5359  if (artifact == NULL) {
5360  LOG(llevError, "could not find artifact %s [%d] to save data\n", op->artifact, op->type);
5361  get_ob_diff(sb, op, &at->clone);
5362  } else {
5363  ADD_STRINGLINE_ENTRY(sb, "artifact ", op->artifact);
5364  base = arch_to_object(at);
5366  get_ob_diff(sb, op, base);
5368  }
5369  } else {
5370  get_ob_diff(sb, op, &at->clone);
5371  }
5372 
5373  /* Eneq(@csd.uu.se): Added this to allow containers being saved with contents*/
5375  save_object_in_sb(sb, tmp, flag);
5376  FOR_INV_FINISH();
5377 
5378  stringbuffer_append_string(sb, "end\n");
5379 }
5380 
5393 int save_object(FILE *fp, object *op, int flag) {
5395  save_object_in_sb(sb, op, flag);
5396  char *cp = stringbuffer_finish(sb);
5397  if (fputs(cp, fp) == EOF) {
5398  free(cp);
5399  return SAVE_ERROR_WRITE;
5400  } else {
5401  free(cp);
5402  return SAVE_ERROR_OK;
5403  }
5404 }
5405 
5407  if (op->map) {
5408  sstring death_animation = object_get_value(op, "death_animation");
5409  if (death_animation != NULL) {
5410  object *death = create_archetype(death_animation);
5411 
5412  if (death != NULL) {
5413  object_insert_in_map_at(death, op->map, op, 0, op->x, op->y);
5414  if (death->arch->more)
5415  object_fix_multipart(death);
5416  }
5417  }
5418  }
5419 }
5420 
5421 object *find_force(object *op, const char *name) {
5422  assert(op != NULL);
5424 }
5425 
5426 object *add_force(object *op, const char *name, int duration) {
5427  assert(op != NULL);
5428  object *force = find_force(op, name);
5429  if (force == NULL) {
5431  force->slaying = add_string(name);
5433  }
5434 
5435  if (duration != 0) {
5436  force->speed = 0.01;
5437  force->speed_left = -duration;
5438  } else {
5439  force->speed = 0;
5440  }
5441 
5442  // Even if duration is zero, a force could be set previously.
5444 
5445  return force;
5446 }
object_was_destroyed
#define object_was_destroyed(op, old_tag)
Definition: object.h:70
Settings::casting_time
uint8_t casting_time
Definition: global.h:270
object_value_set
bool object_value_set(const object *op, const char *const key)
Definition: object.cpp:4372
free_objects
static object * free_objects
Definition: object.cpp:295
object::name_pl
sstring name_pl
Definition: object.h:323
give.next
def next
Definition: give.py:44
GET_MAP_OB
#define GET_MAP_OB(M, X, Y)
Definition: map.h:170
init_objects
void init_objects(void)
Definition: object.cpp:327
living::exp
int64_t exp
Definition: living.h:47
UP_OBJ_FACE
#define UP_OBJ_FACE
Definition: object.h:533
object::weapon_speed_left
float weapon_speed_left
Definition: object.h:340
PLAYER
@ PLAYER
Definition: object.h:112
object_get_owner_const
static const object * object_get_owner_const(const object *op)
Definition: object.cpp:624
object_get_owner
object * object_get_owner(object *op)
Definition: object.cpp:804
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:200
global.h
FREE_OBJ_NO_DESTROY_CALLBACK
#define FREE_OBJ_NO_DESTROY_CALLBACK
Definition: object.h:545
UPD_FACE
#define UPD_FACE
Definition: newclient.h:306
object_find_by_type_subtype
object * object_find_by_type_subtype(const object *who, int type, int subtype)
Definition: object.cpp:4297
object_clear_owner
void object_clear_owner(object *op)
Definition: object.cpp:823
first_player
player * first_player
Definition: init.cpp:106
settings
struct Settings settings
Definition: init.cpp:139
permute
static void permute(int *, int, int)
Definition: object.cpp:3614
object::range_modifier
uint8_t range_modifier
Definition: object.h:418
object::move_status
int32_t move_status
Definition: object.h:400
object_get_env_recursive
object * object_get_env_recursive(object *op)
Definition: object.cpp:590
INS_NO_WALK_ON
#define INS_NO_WALK_ON
Definition: object.h:582
object_update_turn_face
void object_update_turn_face(object *op)
Definition: object.cpp:1332
UP_OBJ_REMOVE
#define UP_OBJ_REMOVE
Definition: object.h:531
safe_strncpy
#define safe_strncpy
Definition: compat.h:27
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Definition: define.h:730
remove_friendly_object
void remove_friendly_object(object *op)
Definition: friend.cpp:52
living::maxhp
int16_t maxhp
Definition: living.h:41
object_count_active
int object_count_active(void)
Definition: object.cpp:1783
object_find_by_type_applied
object * object_find_by_type_applied(const object *who, int type)
Definition: object.cpp:4079
NUM_BODY_LOCATIONS
#define NUM_BODY_LOCATIONS
Definition: object.h:15
MAP_SAVING
#define MAP_SAVING
Definition: map.h:129
object::weapontype
uint32_t weapontype
Definition: object.h:381
object::map_layer
uint8_t map_layer
Definition: object.h:434
find_artifact
const artifact * find_artifact(const object *op, const char *name)
Definition: artifact.cpp:589
GET_MAP_TOP
#define GET_MAP_TOP(M, X, Y)
Definition: map.h:172
statistics
struct Statistics statistics
Definition: init.cpp:233
llevError
@ llevError
Definition: logger.h:11
object_set_flag_inv
void object_set_flag_inv(object *op, int flag)
Definition: object.cpp:3235
FABS
#define FABS(x)
Definition: define.h:22
FLAG_CLIENT_ANIM_RANDOM
#define FLAG_CLIENT_ANIM_RANDOM
Definition: define.h:241
EVENT_CONNECTOR
@ EVENT_CONNECTOR
Definition: object.h:232
find_skill_by_number
object * find_skill_by_number(object *who, int skillno)
Definition: main.cpp:375
MOVE_ALL
#define MOVE_ALL
Definition: define.h:398
object::path_attuned
uint32_t path_attuned
Definition: object.h:353
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:58
ADD_STRINGLINE_ENTRY
static void ADD_STRINGLINE_ENTRY(StringBuffer *sb, const char *name, const char *value)
Definition: object.cpp:4932
FLAG_OVERLAY_FLOOR
#define FLAG_OVERLAY_FLOOR
Definition: define.h:255
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
archetype::more
archetype * more
Definition: object.h:486
player
Definition: player.h:105
object::client_type
uint16_t client_type
Definition: object.h:350
object_free_all_data
void object_free_all_data(void)
Definition: object.cpp:767
diamondslots.x
x
Definition: diamondslots.py:15
FLAG_STARTEQUIP
#define FLAG_STARTEQUIP
Definition: define.h:268
Statistics::spell_merges
uint64_t spell_merges
Definition: global.h:363
SIZEOFFREE1
#define SIZEOFFREE1
Definition: define.h:153
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
map_find_by_archetype
object * map_find_by_archetype(mapstruct *m, int x, int y, const archetype *at)
Definition: object.cpp:3114
FLAG_IS_TURNABLE
#define FLAG_IS_TURNABLE
Definition: define.h:256
object_merge
object * object_merge(object *op, object *top)
Definition: object.cpp:2051
STARTMAX
#define STARTMAX
Definition: config.h:523
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.cpp:138
FALSE
#define FALSE
Definition: compat.h:14
update_position
void update_position(mapstruct *m, int x, int y)
Definition: map.cpp:2110
BITMASK_EVENT
#define BITMASK_EVENT(evt)
Definition: events.h:62
SET_MAP_TOP
#define SET_MAP_TOP(M, X, Y, tmp)
Definition: map.h:177
object::item_power
int8_t item_power
Definition: object.h:372
object::arch
struct archetype * arch
Definition: object.h:424
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
object::attack_movement
uint16_t attack_movement
Definition: object.h:401
absdir
int absdir(int d)
Definition: object.cpp:3710
UPD_WEIGHT
#define UPD_WEIGHT
Definition: newclient.h:305
flag_names
static const char *const flag_names[NUM_FLAGS+1]
Definition: object.cpp:4847
object::range
int8_t range
Definition: object.h:417
object::speed
float speed
Definition: object.h:337
object_set_enemy
void object_set_enemy(object *op, object *enemy)
Definition: object.cpp:915
object_insert_to_free_spot_or_free
void object_insert_to_free_spot_or_free(object *op, mapstruct *map, int x, int y, int start, int stop, object *originator)
Definition: object.cpp:4787
object::invisible
int16_t invisible
Definition: object.h:370
living::Dex
int8_t Dex
Definition: living.h:36
FLAG_OBJ_ORIGINAL
#define FLAG_OBJ_ORIGINAL
Definition: define.h:357
TRAP
@ TRAP
Definition: object.h:246
object::x
int16_t x
Definition: object.h:335
OB_SPELL_TAG_MATCH
#define OB_SPELL_TAG_MATCH(op, count)
Definition: object.h:95
object_dump_all
void object_dump_all(void)
Definition: object.cpp:704
player::transport
object * transport
Definition: player.h:214
object::speed_left
float speed_left
Definition: object.h:338
give_artifact_abilities
void give_artifact_abilities(object *op, const object *artifact)
Definition: artifact.cpp:230
object::pick_up
uint8_t pick_up
Definition: object.h:371
object::map
struct mapstruct * map
Definition: object.h:305
object::anim_suffix
sstring anim_suffix
Definition: object.h:324
key_value
Definition: object.h:42
object_find_first_free_spot
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
Definition: object.cpp:3595
MoveType
unsigned char MoveType
Definition: define.h:417
arch_present_in_ob
object * arch_present_in_ob(const archetype *at, const object *op)
Definition: object.cpp:3218
object_set_owner
void object_set_owner(object *op, object *owner)
Definition: object.cpp:840
object_set_value_s
static int object_set_value_s(object *, const char *, const char *, int)
Definition: object.cpp:4412
guildjoin.ob
ob
Definition: guildjoin.py:42
object::expmul
double expmul
Definition: object.h:407
object_handle_death_animation
void object_handle_death_animation(object *op)
Definition: object.cpp:5406
artifact::item
object * item
Definition: artifact.h:15
commongive.inv
inv
Definition: commongive.py:29
CALLOC
#define CALLOC(x, y)
Definition: compat.h:31
NUM_FLAGS
#define NUM_FLAGS
Definition: define.h:374
GET_MAP_MOVE_SLOW
#define GET_MAP_MOVE_SLOW(M, X, Y)
Definition: map.h:195
object_reset
void object_reset(object *op)
Definition: object.cpp:934
SKILL
@ SKILL
Definition: object.h:148
object::direction
int8_t direction
Definition: object.h:344
flags
static const flag_definition flags[]
Definition: gridarta-types-convert.cpp:101
object::count
tag_t count
Definition: object.h:307
object::last_grace
int16_t last_grace
Definition: object.h:369
RUNE
@ RUNE
Definition: object.h:245
object_copy
void object_copy(const object *src_ob, object *dest_ob)
Definition: object.cpp:1192
object::smoothlevel
uint8_t smoothlevel
Definition: object.h:433
give.top
def top
Definition: give.py:42
fix_object
void fix_object(object *op)
Definition: living.cpp:1125
object_find_by_flag_applied
object * object_find_by_flag_applied(const object *who, int flag)
Definition: object.cpp:4225
object_find_multi_free_spot_within_radius
int object_find_multi_free_spot_within_radius(const object *ob, const object *gen, int *hx, int *hy)
Definition: object.cpp:3408
object_find_by_type_and_skill
object * object_find_by_type_and_skill(const object *who, int type, const char *skill)
Definition: object.cpp:4179
Ice.tmp
int tmp
Definition: Ice.py:207
GET_MAP_PLAYER
#define GET_MAP_PLAYER(M, X, Y)
Definition: map.h:166
FLAG_NO_MAGIC
#define FLAG_NO_MAGIC
Definition: define.h:276
blank_face
const Face * blank_face
Definition: image.cpp:36
P_NO_MAGIC
#define P_NO_MAGIC
Definition: map.h:225
llevMonster
@ llevMonster
Definition: logger.h:14
flags_differ
int flags_differ(ob_flags *diff)
Definition: object.cpp:4956
TRANSPORT
@ TRANSPORT
Definition: object.h:113
UPD_NROF
#define UPD_NROF
Definition: newclient.h:310
FOR_ABOVE_PREPARE
#define FOR_ABOVE_PREPARE(op_, it_)
Definition: define.h:687
object_copy_with_inv
void object_copy_with_inv(const object *src_ob, object *dest_ob, bool update_speed)
Definition: object.cpp:1208
object::key_values
key_value * key_values
Definition: object.h:444
NROFATTACKS
#define NROFATTACKS
Definition: attack.h:17
stringbuffer_append_char
void stringbuffer_append_char(StringBuffer *sb, const char c)
Definition: stringbuffer.cpp:104
FLAG_BLOCKSVIEW
#define FLAG_BLOCKSVIEW
Definition: define.h:269
skills.h
compare_ob_value_lists
static int compare_ob_value_lists(const object *, const object *)
Definition: object.cpp:404
free_arch
void free_arch(archetype *at)
Definition: arch.cpp:169
object::title
sstring title
Definition: object.h:325
MAP_DARKNESS
#define MAP_DARKNESS(m)
Definition: map.h:70
FLAG_INV_LOCKED
#define FLAG_INV_LOCKED
Definition: define.h:329
FOR_OB_AND_ABOVE_FINISH
#define FOR_OB_AND_ABOVE_FINISH()
Definition: define.h:743
object_find_by_tag_global
object * object_find_by_tag_global(tag_t i)
Definition: object.cpp:727
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.cpp:4342
P_IS_ALIVE
#define P_IS_ALIVE
Definition: map.h:235
Statistics::spell_hash_full
uint64_t spell_hash_full
Definition: global.h:364
FLAG_APPLIED
#define FLAG_APPLIED
Definition: define.h:235
object::level
int16_t level
Definition: object.h:361
expand_objects
static void expand_objects(void)
Definition: object.cpp:1231
events_execute_object_event
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Definition: events.cpp:299
object_merge_spell
void object_merge_spell(object *op, int16_t x, int16_t y)
Definition: object.cpp:2129
active_objects
object * active_objects
Definition: object.cpp:296
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.cpp:2853
object::will_apply
uint8_t will_apply
Definition: object.h:402
object::above
object * above
Definition: object.h:296
HUGE_BUF
#define HUGE_BUF
Definition: define.h:37
FAST_SAVE_DOUBLE
static void FAST_SAVE_DOUBLE(StringBuffer *sb, const char *name, const double value)
Definition: object.cpp:4946
stringbuffer_append_int64
void stringbuffer_append_int64(StringBuffer *sb, int64_t x)
Definition: stringbuffer.cpp:111
AB_NO_PASS
#define AB_NO_PASS
Definition: map.h:233
MAX
#define MAX(x, y)
Definition: compat.h:24
ob_move_on
method_ret ob_move_on(object *op, object *victim, object *originator)
Definition: ob_methods.cpp:111
SAVE_ERROR_WRITE
#define SAVE_ERROR_WRITE
Definition: map.h:142
object::resist
int16_t resist[NROFATTACKS]
Definition: object.h:351
FLAG_NO_PICK
#define FLAG_NO_PICK
Definition: define.h:239
FLAG_DIALOG_PARSED
#define FLAG_DIALOG_PARSED
Definition: define.h:243
FLAG_ALIVE
#define FLAG_ALIVE
Definition: define.h:230
object::path_denied
uint32_t path_denied
Definition: object.h:355
object_clear
void object_clear(object *op)
Definition: object.cpp:983
esrv_send_item
void esrv_send_item(object *pl, object *op)
Definition: main.cpp:354
INS_ABOVE_FLOOR_ONLY
#define INS_ABOVE_FLOOR_ONLY
Definition: object.h:581
key_value::value
const char * value
Definition: object.h:44
FOR_OB_AND_ABOVE_PREPARE
#define FOR_OB_AND_ABOVE_PREPARE(op_)
Definition: define.h:739
object::carrying
int32_t carrying
Definition: object.h:377
FLAG_IS_A_TEMPLATE
#define FLAG_IS_A_TEMPLATE
Definition: define.h:366
LOOK_OBJ
#define LOOK_OBJ(ob)
Definition: object.h:521
stringbuffer.h
object::path_repelled
uint32_t path_repelled
Definition: object.h:354
object::y
int16_t y
Definition: object.h:335
object_find_by_type_and_race
object * object_find_by_type_and_race(const object *who, int type, const char *race)
Definition: object.cpp:4129
m
static event_registration m
Definition: citylife.cpp:425
autojail.who
who
Definition: autojail.py:3
MAP_IN_MEMORY
#define MAP_IN_MEMORY
Definition: map.h:126
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
OBJ_EXPAND
#define OBJ_EXPAND
Definition: config.h:524
get_button_value
int get_button_value(const object *button)
Definition: button.cpp:749
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.cpp:1560
object_update
void object_update(object *op, int action)
Definition: object.cpp:1434
object::contr
struct player * contr
Definition: object.h:284
item.q
q
Definition: item.py:32
FMT64
#define FMT64
Definition: compat.h:16
NROF
static uint32_t NROF(const object *const ob)
Definition: object.h:625
object_find_by_name
object * object_find_by_name(const object *who, const char *name)
Definition: object.cpp:3952
disinfect.map
map
Definition: disinfect.py:4
ob_flags
uint32_t ob_flags[4]
Definition: object.h:259
object_get_key_value
key_value * object_get_key_value(const object *ob, const char *key)
Definition: object.cpp:4317
object::last_heal
int32_t last_heal
Definition: object.h:367
object_find_by_type2
object * object_find_by_type2(const object *who, int type1, int type2)
Definition: object.cpp:4033
object::subtype
uint8_t subtype
Definition: object.h:349
FLAG_WAS_WIZ
#define FLAG_WAS_WIZ
Definition: define.h:234
add_refcount
sstring add_refcount(sstring str)
Definition: shstr.cpp:210
object_value_set_shared
bool object_value_set_shared(const object *op, sstring key)
Definition: object.cpp:4386
freearr_y
short freearr_y[SIZEOFFREE]
Definition: object.cpp:305
key_value::next
key_value * next
Definition: object.h:45
P_NO_ERROR
#define P_NO_ERROR
Definition: map.h:238
object_present_in_ob_by_name
object * object_present_in_ob_by_name(int type, const char *str, const object *op)
Definition: object.cpp:3199
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.cpp:588
map_find_by_type
object * map_find_by_type(mapstruct *m, int x, int y, uint8_t type)
Definition: object.cpp:3141
object::weapon_speed
float weapon_speed
Definition: object.h:339
can_see_monsterP
int can_see_monsterP(mapstruct *m, int x, int y, int dir)
Definition: object.cpp:3818
MOVE_WALK
#define MOVE_WALK
Definition: define.h:392
FLAG_KNOWN_CURSED
#define FLAG_KNOWN_CURSED
Definition: define.h:320
object::prev
object * prev
Definition: object.h:286
stringbuffer_finish_shared
sstring stringbuffer_finish_shared(StringBuffer *sb)
Definition: stringbuffer.cpp:85
FOR_ABOVE_FINISH
#define FOR_ABOVE_FINISH()
Definition: define.h:694
archetype::clone
object clone
Definition: object.h:487
object::run_away
uint8_t run_away
Definition: object.h:394
object_dump
void object_dump(const object *op, StringBuffer *sb)
Definition: object.cpp:645
object::ox
int16_t ox
Definition: object.h:336
object::weight_limit
int32_t weight_limit
Definition: object.h:376
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
object_count_free
int object_count_free(void)
Definition: object.cpp:1751
FOR_OB_AND_BELOW_FINISH
#define FOR_OB_AND_BELOW_FINISH()
Definition: define.h:754
object_get_multi_size
void object_get_multi_size(const object *ob, int *sx, int *sy, int *hx, int *hy)
Definition: object.cpp:4740
HEAD
#define HEAD(op)
Definition: object.h:607
CONTAINER
@ CONTAINER
Definition: object.h:236
object::below
object * below
Definition: object.h:295
make_face_from_files.str
str
Definition: make_face_from_files.py:30
object::move_type
MoveType move_type
Definition: object.h:436
object_find_by_type_and_name
object * object_find_by_type_and_name(const object *who, int type, const char *name)
Definition: object.cpp:4104
query_short_name
void query_short_name(const object *op, char *buf, size_t size)
Definition: item.cpp:513
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
object::event_bitmask
uint64_t event_bitmask
Definition: object.h:447
convert.action
action
Definition: convert.py:25
object::face
const Face * face
Definition: object.h:341
out_of_map
int out_of_map(mapstruct *m, int x, int y)
Definition: map.cpp:2289
find_string
sstring find_string(const char *str)
Definition: shstr.cpp:236
object::last_eat
int32_t last_eat
Definition: object.h:366
navar-midane_pickup.msg
list msg
Definition: navar-midane_pickup.py:13
object::value
int32_t value
Definition: object.h:360
object_update_speed
void object_update_speed(object *op)
Definition: object.cpp:1349
FLAG_IS_WOODED
#define FLAG_IS_WOODED
Definition: define.h:330
object::type
uint8_t type
Definition: object.h:348
INS_NO_MERGE
#define INS_NO_MERGE
Definition: object.h:580
FLAG_DAMNED
#define FLAG_DAMNED
Definition: define.h:317
living::dam
int16_t dam
Definition: living.h:46
object::magic
int8_t magic
Definition: object.h:358
SET_MAP_FLAGS
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:159
object::materialname
sstring materialname
Definition: object.h:356
UPD_FLAGS
#define UPD_FLAGS
Definition: newclient.h:304
CFweardisguise.tag
tag
Definition: CFweardisguise.py:25
object_find_by_tag
object * object_find_by_tag(const object *who, tag_t tag)
Definition: object.cpp:4056
FLAG_WIZPASS
#define FLAG_WIZPASS
Definition: define.h:314
object_find_by_flag
object * object_find_by_flag(const object *who, int flag)
Definition: object.cpp:4202
object_free
void object_free(object *ob, int flags)
Definition: object.cpp:1592
LAMP
@ LAMP
Definition: object.h:206
GET_MAP_MOVE_BLOCK
#define GET_MAP_MOVE_BLOCK(M, X, Y)
Definition: map.h:190
FOR_INV_FINISH
#define FOR_INV_FINISH()
Definition: define.h:677
INS_BELOW_ORIGINATOR
#define INS_BELOW_ORIGINATOR
Definition: object.h:584
object::move_on
MoveType move_on
Definition: object.h:439
living::food
int32_t food
Definition: living.h:48
resist_save
const char *const resist_save[NROFATTACKS]
Definition: init.cpp:31
disinfect.count
int count
Definition: disinfect.py:7
tag_t
uint32_t tag_t
Definition: object.h:14
body_locations
body_locations_struct body_locations[NUM_BODY_LOCATIONS]
Definition: item.cpp:55
archetype
Definition: object.h:483
P_PLAYER
#define P_PLAYER
Definition: map.h:234
object_matches_string
int object_matches_string(object *pl, object *op, const char *name)
Definition: object.cpp:4570
object_find_multi_free_spot_around
int object_find_multi_free_spot_around(const object *ob, const object *gen, int16_t *hx, int16_t *hy)
Definition: object.cpp:3292
sproto.h
FLAG_NO_DROP
#define FLAG_NO_DROP
Definition: define.h:288
FLAG_NO_SAVE
#define FLAG_NO_SAVE
Definition: define.h:244
living::sp
int16_t sp
Definition: living.h:42
nrofallocobjects
int nrofallocobjects
Definition: object.cpp:291
stringbuffer_append_string
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Definition: stringbuffer.cpp:95
get_map_from_coord
mapstruct * get_map_from_coord(mapstruct *m, int16_t *x, int16_t *y)
Definition: map.cpp:2362
FLAG_NO_STEAL
#define FLAG_NO_STEAL
Definition: define.h:342
FOR_OB_AND_BELOW_PREPARE
#define FOR_OB_AND_BELOW_PREPARE(op_)
Definition: define.h:750
living::Int
int8_t Int
Definition: living.h:36
object::sound_chance
uint8_t sound_chance
Definition: object.h:403
object::race
sstring race
Definition: object.h:326
compare_flags
static void compare_flags(ob_flags *ret, const object *p, const object *q)
Definition: object.h:499
object::animation
const Animations * animation
Definition: object.h:428
GET_MAP_MOVE_ON
#define GET_MAP_MOVE_ON(M, X, Y)
Definition: map.h:200
FREE_OBJ_DROP_ABOVE_FLOOR
#define FREE_OBJ_DROP_ABOVE_FLOOR
Definition: object.h:546
ob_blocked
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Definition: map.cpp:479
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
object_find_by_type_without_flags
object * object_find_by_type_without_flags(const object *who, int type, int *flags, int num_flags)
Definition: object.cpp:4000
reduction_dir
static const int reduction_dir[SIZEOFFREE][3]
Definition: object.cpp:3748
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.cpp:2100
object::other_arch
struct archetype * other_arch
Definition: object.h:425
object_create_clone
object * object_create_clone(object *asrc)
Definition: object.cpp:3905
object_copy_no_speed
void object_copy_no_speed(const object *src_ob, object *dest_ob)
Definition: object.cpp:1061
fatal
void fatal(enum fatal_error err)
Definition: utils.cpp:590
MAP_WIDTH
#define MAP_WIDTH(m)
Definition: map.h:73
SIZEOFFREE
#define SIZEOFFREE
Definition: define.h:155
P_OUT_OF_MAP
#define P_OUT_OF_MAP
Definition: map.h:247
MAX_BUF
#define MAX_BUF
Definition: define.h:35
INS_ON_TOP
#define INS_ON_TOP
Definition: object.h:583
move_name
const char *const move_name[]
Definition: object.cpp:4828
object_set_cheat
void object_set_cheat(object *op)
Definition: object.cpp:3270
object_new
object * object_new(void)
Definition: object.cpp:1273
object_insert_in_map
object * object_insert_in_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.cpp:2361
esrv_del_item
void esrv_del_item(player *pl, object *ob)
Definition: main.cpp:381
UP_OBJ_INSERT
#define UP_OBJ_INSERT
Definition: object.h:530
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:278
object_present_in_ob
object * object_present_in_ob(uint8_t type, const object *op)
Definition: object.cpp:3164
get_weight_limit
uint32_t get_weight_limit(int stat)
Definition: living.cpp:2362
object::weight
int32_t weight
Definition: object.h:375
nroffreeobjects
int nroffreeobjects
Definition: object.cpp:290
free_string
void free_string(sstring str)
Definition: shstr.cpp:280
object::move_slow
MoveType move_slow
Definition: object.h:441
object::spell_tags
tag_t * spell_tags
Definition: object.h:446
living::wc
int8_t wc
Definition: living.h:37
offsetof
#define offsetof(type, member)
Definition: shstr.h:37
object::move_slow_penalty
float move_slow_penalty
Definition: object.h:442
RANDOM
#define RANDOM()
Definition: define.h:644
StringBuffer
Definition: stringbuffer.cpp:25
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Definition: global.h:198
MOVE_FLY_LOW
#define MOVE_FLY_LOW
Definition: define.h:393
living::maxgrace
int16_t maxgrace
Definition: living.h:45
FLAG_CLIENT_ANIM_SYNC
#define FLAG_CLIENT_ANIM_SYNC
Definition: define.h:240
guildbuy.ob2
ob2
Definition: guildbuy.py:23
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:723
ob_count
long ob_count
Definition: init.cpp:123
OUT_OF_REAL_MAP
#define OUT_OF_REAL_MAP(M, X, Y)
Definition: map.h:215
get_search_arr
void get_search_arr(int *search_arr)
Definition: object.cpp:3638
find_dir_2
int find_dir_2(int x, int y)
Definition: object.cpp:3673
living::Wis
int8_t Wis
Definition: living.h:36
key_value::key
const char * key
Definition: object.h:43
object_add_weight
void object_add_weight(object *op, signed long weight)
Definition: object.cpp:2829
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
object_decrease_nrof
object * object_decrease_nrof(object *op, uint32_t i)
Definition: object.cpp:2672
object_find_by_type_and_slaying
object * object_find_by_type_and_slaying(const object *who, int type, const char *slaying)
Definition: object.cpp:4154
INS_MAP_LOAD
#define INS_MAP_LOAD
Definition: object.h:585
BITMASK_VALID
#define BITMASK_VALID
Definition: events.h:64
object::lore
sstring lore
Definition: object.h:332
object::dam_modifier
uint8_t dam_modifier
Definition: object.h:419
SK_WOODSMAN
@ SK_WOODSMAN
Definition: skills.h:40
object::slaying
sstring slaying
Definition: object.h:327
object::glow_radius
int8_t glow_radius
Definition: object.h:374
roll-o-matic.stop
def stop()
Definition: roll-o-matic.py:78
FLAG_FRIENDLY
#define FLAG_FRIENDLY
Definition: define.h:246
guildbuy.ob1
ob1
Definition: guildbuy.py:22
object::duration_modifier
uint8_t duration_modifier
Definition: object.h:416
object::name
sstring name
Definition: object.h:319
SPELL_MAPPINGS
#define SPELL_MAPPINGS
Definition: global.h:152
object_count_used
int object_count_used(void)
Definition: object.cpp:1767
FREE_AND_CLEAR
#define FREE_AND_CLEAR(xyz)
Definition: global.h:193
object_find_by_arch_name
object * object_find_by_arch_name(const object *who, const char *name)
Definition: object.cpp:4248
SIZEOFFREE2
#define SIZEOFFREE2
Definition: define.h:154
object_can_pick
int object_can_pick(const object *who, const object *item)
Definition: object.cpp:3863
SCRIPT_FIX_NOTHING
#define SCRIPT_FIX_NOTHING
Definition: global.h:390
objarray
static object objarray[STARTMAX]
Definition: object.cpp:289
get_map_flags
int get_map_flags(mapstruct *oldmap, mapstruct **newmap, int16_t x, int16_t y, int16_t *nx, int16_t *ny)
Definition: map.cpp:300
object::artifact
sstring artifact
Definition: object.h:322
object_find_by_type
object * object_find_by_type(const object *who, int type)
Definition: object.cpp:3976
item
Definition: item.py:1
living::maxsp
int16_t maxsp
Definition: living.h:43
P_NEED_UPDATE
#define P_NEED_UPDATE
Definition: map.h:237
mapstruct
Definition: map.h:313
object_find_by_name_global
object * object_find_by_name_global(const char *str)
Definition: object.cpp:747
object::last_sp
int32_t last_sp
Definition: object.h:368
sstring
const typedef char * sstring
Definition: sstring.h:2
living::Cha
int8_t Cha
Definition: living.h:36
give.op
op
Definition: give.py:33
autojail.value
value
Definition: autojail.py:6
get_string_move_type
static void get_string_move_type(StringBuffer *sb, MoveType mt)
Definition: object.cpp:4888
object::gen_sp_armour
int8_t gen_sp_armour
Definition: object.h:373
object::skill
sstring skill
Definition: object.h:329
animate_object
void animate_object(object *op, int dir)
Definition: anim.cpp:44
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:266
object_split
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
Definition: object.cpp:2633
object_find_free_spot
int object_find_free_spot(const object *ob, mapstruct *m, int x, int y, int start, int stop)
Definition: object.cpp:3555
object_set_msg
void object_set_msg(object *op, const char *msg)
Definition: object.cpp:4807
esrv_update_item
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.cpp:359
SPELL_EFFECT
@ SPELL_EFFECT
Definition: object.h:220
object::msg
sstring msg
Definition: object.h:330
spell_mapping
const char *const spell_mapping[SPELL_MAPPINGS]
Definition: object.cpp:74
objects
object * objects
Definition: object.cpp:294
archetype::reference_count
int reference_count
Definition: object.h:490
FLAG_NO_FIX_PLAYER
#define FLAG_NO_FIX_PLAYER
Definition: define.h:277
object_unset_flag_inv
void object_unset_flag_inv(object *op, int flag)
Definition: object.cpp:3252
object_free_key_values
void object_free_key_values(object *op)
Definition: object.cpp:954
diamondslots.y
y
Definition: diamondslots.py:16
add_force
object * add_force(object *op, const char *name, int duration)
Definition: object.cpp:5426
FLAG_BEEN_APPLIED
#define FLAG_BEEN_APPLIED
Definition: define.h:323
python_pickup.where
where
Definition: python_pickup.py:7
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
npc_dialog.index
int index
Definition: npc_dialog.py:102
SET_MAP_OB
#define SET_MAP_OB(M, X, Y, tmp)
Definition: map.h:175
living::ac
int8_t ac
Definition: living.h:38
MAP_HEIGHT
#define MAP_HEIGHT(m)
Definition: map.h:75
save_object_in_sb
void save_object_in_sb(StringBuffer *sb, object *op, const int flag)
Definition: object.cpp:5307
get_ob_diff
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.cpp:4982
object::duration
int16_t duration
Definition: object.h:415
object_remove_from_active_list
void object_remove_from_active_list(object *op)
Definition: object.cpp:1392
object_find_by_type_and_arch_name
object * object_find_by_type_and_arch_name(const object *who, int type, const char *name)
Definition: object.cpp:4273
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:229
find_insert_pos
static object * find_insert_pos(object *op, const int flag)
Definition: object.cpp:2297
strcasecmp
int strcasecmp(const char *s1, const char *s2)
object_distance
int object_distance(const object *ob1, const object *ob2)
Definition: object.cpp:3657
castle_read.key
key
Definition: castle_read.py:64
find_force
object * find_force(object *op, const char *name)
Definition: object.cpp:5421
MIN_ACTIVE_SPEED
#define MIN_ACTIVE_SPEED
Definition: define.h:639
object::body_info
int8_t body_info[NUM_BODY_LOCATIONS]
Definition: object.h:382
FLAG_ANIMATE
#define FLAG_ANIMATE
Definition: define.h:242
SAVE_ERROR_OK
#define SAVE_ERROR_OK
Definition: map.h:139
loader.h
object_fix_multipart
void object_fix_multipart(object *tmp)
Definition: object.cpp:4681
object::randomitems
struct treasurelist * randomitems
Definition: object.h:395
object::elevation
int elevation
Definition: object.h:450
GET_MAP_MOVE_OFF
#define GET_MAP_MOVE_OFF(M, X, Y)
Definition: map.h:205
FLAG_IS_HILLY
#define FLAG_IS_HILLY
Definition: define.h:332
MOVE_FLY_HIGH
#define MOVE_FLY_HIGH
Definition: define.h:394
object_remove
void object_remove(object *op)
Definition: object.cpp:1833
compare_ob_value_lists_one
static int compare_ob_value_lists_one(const object *, const object *)
Definition: object.cpp:363
UP_OBJ_CHANGE
#define UP_OBJ_CHANGE
Definition: object.h:532
GET_MAP_FLAGS
#define GET_MAP_FLAGS(M, X, Y)
Definition: map.h:157
object::move_off
MoveType move_off
Definition: object.h:440
object_sum_weight
signed long object_sum_weight(object *op)
Definition: object.cpp:568
FLAG_UNPAID
#define FLAG_UNPAID
Definition: define.h:236
empty_archetype
archetype * empty_archetype
Definition: init.cpp:119
freedir
int freedir[SIZEOFFREE]
Definition: object.cpp:317
P_NO_CLERIC
#define P_NO_CLERIC
Definition: map.h:236
object_check_move_on
int object_check_move_on(object *op, object *originator)
Definition: object.cpp:3008
FREE_OBJ_FREE_INVENTORY
#define FREE_OBJ_FREE_INVENTORY
Definition: object.h:544
update_all_los
void update_all_los(const mapstruct *map, int x, int y)
Definition: los.cpp:595
object::state
uint8_t state
Definition: object.h:359
object_copy_owner
void object_copy_owner(object *op, object *clone)
Definition: object.cpp:893
archetype::name
sstring name
Definition: object.h:484
SCROLL
@ SCROLL
Definition: object.h:226
FLAG_IS_LINKED
#define FLAG_IS_LINKED
Definition: define.h:315
object::nrof
uint32_t nrof
Definition: object.h:342
object_increase_nrof
static void object_increase_nrof(object *op, uint32_t i)
Definition: object.cpp:2759
living::grace
int16_t grace
Definition: living.h:44
query_base_name
void query_base_name(const object *op, int plural, char *buf, size_t size)
Definition: item.cpp:689
object::stats
living stats
Definition: object.h:378
object::move_allow
MoveType move_allow
Definition: object.h:438
artifact
Definition: artifact.h:14
object_set_value
int object_set_value(object *op, const char *key, const char *value, int add_key)
Definition: object.cpp:4495
object::more
object * more
Definition: object.h:303
freearr_x
short freearr_x[SIZEOFFREE]
Definition: object.cpp:299
object::total_exp
int64_t total_exp
Definition: object.h:379
object_sub_weight
void object_sub_weight(object *op, signed long weight)
Definition: object.cpp:1807
TRUE
#define TRUE
Definition: compat.h:11
EVENT_DESTROY
#define EVENT_DESTROY
Definition: events.h:26
living::Pow
int8_t Pow
Definition: living.h:36
commongive.newob
newob
Definition: commongive.py:47
SPELL_TAG_SIZE
#define SPELL_TAG_SIZE
Definition: object.h:83
object::attacktype
uint32_t attacktype
Definition: object.h:352
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
FLAG_CURSED
#define FLAG_CURSED
Definition: define.h:316
maxfree
int maxfree[SIZEOFFREE]
Definition: object.cpp:311
SAVE_FLAG_SAVE_UNPAID
#define SAVE_FLAG_SAVE_UNPAID
Definition: map.h:106
save_object
int save_object(FILE *fp, object *op, int flag)
Definition: object.cpp:5393
SK_CLIMBING
@ SK_CLIMBING
Definition: skills.h:39
object_get_player_container
object * object_get_player_container(object *op)
Definition: object.cpp:607
dirdiff
int dirdiff(int dir1, int dir2)
Definition: object.cpp:3728
OB_SPELL_TAG_HASH
#define OB_SPELL_TAG_HASH(op, count)
Definition: object.h:89
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
object::material
uint16_t material
Definition: object.h:357
map_layer_name
const char *const map_layer_name[MAP_LAYERS]
Definition: map.cpp:46
CUSTOM_NAME_FIELD
#define CUSTOM_NAME_FIELD
Definition: object.h:98
object_free_inventory
void object_free_inventory(object *ob)
Definition: object.cpp:1568
object::move_block
MoveType move_block
Definition: object.h:437
object::next
object * next
Definition: object.h:285
free_dialog_information
void free_dialog_information(object *op)
Definition: dialog.cpp:32
FOR_INV_PREPARE
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:670
FLAG_NO_APPLY
#define FLAG_NO_APPLY
Definition: define.h:301
living::hp
int16_t hp
Definition: living.h:40
living::luck
int8_t luck
Definition: living.h:39
FORCE
@ FORCE
Definition: object.h:229
object::oy
int16_t oy
Definition: object.h:336
object.h
object_replace_insert_in_map
void object_replace_insert_in_map(const char *arch_string, object *op)
Definition: object.cpp:2593
object_can_merge
int object_can_merge(object *ob1, object *ob2)
Definition: object.cpp:433
llevDebug
@ llevDebug
Definition: logger.h:13
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
FLAG_IS_FLOOR
#define FLAG_IS_FLOOR
Definition: define.h:302
P_BLOCKSVIEW
#define P_BLOCKSVIEW
Definition: map.h:224
FORCE_NAME
#define FORCE_NAME
Definition: spells.h:169
FAST_SAVE_LONG
static void FAST_SAVE_LONG(StringBuffer *sb, const char *name, const long value)
Definition: object.cpp:4939
living::Con
int8_t Con
Definition: living.h:36
FLAG_IDENTIFIED
#define FLAG_IDENTIFIED
Definition: define.h:261
give.name
name
Definition: give.py:27
living::Str
int8_t Str
Definition: living.h:36
dragon_attune.force
force
Definition: dragon_attune.py:45