ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/object.C
(Generate patch)

Comparing deliantra/server/common/object.C (file contents):
Revision 1.10 by root, Thu Aug 31 17:54:14 2006 UTC vs.
Revision 1.22 by root, Sun Sep 10 00:51:23 2006 UTC

1/* 1/*
2 * static char *rcsid_object_c = 2 * static char *rcsid_object_c =
3 * "$Id: object.C,v 1.10 2006/08/31 17:54:14 root Exp $"; 3 * "$Id: object.C,v 1.22 2006/09/10 00:51:23 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
37#endif /* win32 */ 37#endif /* win32 */
38#include <object.h> 38#include <object.h>
39#include <funcpoint.h> 39#include <funcpoint.h>
40#include <skills.h> 40#include <skills.h>
41#include <loader.h> 41#include <loader.h>
42#ifdef MEMORY_DEBUG
43int nroffreeobjects = 0;
44int nrofallocobjects = 0; 42int nrofallocobjects = 0;
45#undef OBJ_EXPAND
46#define OBJ_EXPAND 1
47#else
48object objarray[STARTMAX]; /* All objects, allocated this way at first */
49int nroffreeobjects = STARTMAX; /* How many OBs allocated and free (free) */
50int nrofallocobjects = STARTMAX; /* How many OBs allocated (free + used) */
51#endif
52 43
53object *objects; /* Pointer to the list of used objects */ 44object *objects; /* Pointer to the list of used objects */
54object *free_objects; /* Pointer to the list of unused objects */
55object *active_objects; /* List of active objects that need to be processed */ 45object *active_objects; /* List of active objects that need to be processed */
56 46
57short freearr_x[SIZEOFFREE]= 47short freearr_x[SIZEOFFREE]=
58 {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, 48 {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,
59 0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1}; 49 0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1};
60short freearr_y[SIZEOFFREE]= 50short freearr_y[SIZEOFFREE]=
64 {0,9,10,13,14,17,18,21,22,25,26,27,30,31,32,33,36,37,39,39,42,43,44,45, 54 {0,9,10,13,14,17,18,21,22,25,26,27,30,31,32,33,36,37,39,39,42,43,44,45,
65 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}; 55 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};
66int freedir[SIZEOFFREE]= { 56int freedir[SIZEOFFREE]= {
67 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, 57 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,
68 1,2,2,2,2,2,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8}; 58 1,2,2,2,2,2,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8};
69
70 59
71/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 60/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
72static int compare_ob_value_lists_one(const object * wants, const object * has) { 61static int compare_ob_value_lists_one(const object * wants, const object * has) {
73 key_value * wants_field; 62 key_value * wants_field;
74 63
121 * 110 *
122 * Improvements made with merge: Better checking on potion, and also 111 * Improvements made with merge: Better checking on potion, and also
123 * check weight 112 * check weight
124 */ 113 */
125 114
126int CAN_MERGE(object *ob1, object *ob2) { 115bool
127 116object::can_merge (object *ob1, object *ob2)
117{
128 /* A couple quicksanity checks */ 118 /* A couple quicksanity checks */
129 if ((ob1 == ob2) || (ob1->type != ob2->type)) return 0; 119 if ((ob1 == ob2) || (ob1->type != ob2->type))
130
131 if (ob1->speed != ob2->speed) return 0;
132 /* Note sure why the following is the case - either the object has to
133 * be animated or have a very low speed. Is this an attempted monster
134 * check?
135 */
136 if (!QUERY_FLAG(ob1,FLAG_ANIMATE) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED)
137 return 0; 120 return 0;
138 121
122 if (ob1->speed != ob2->speed)
123 return 0;
124
139 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that 125 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that
140 * value could not be stored in a sint32 (which unfortunately sometimes is 126 * value could not be stored in a sint32 (which unfortunately sometimes is
141 * used to store nrof). 127 * used to store nrof).
142 */ 128 */
143 if (ob1->nrof+ob2->nrof >= 1UL<<31) 129 if (ob1->nrof + ob2->nrof >= 1UL << 31)
144 return 0; 130 return 0;
145 131
132 /* If the objects have been identified, set the BEEN_APPLIED flag.
133 * This is to the comparison of the flags below will be OK. We
134 * just can't ignore the been applied or identified flags, as they
135 * are not equal - just if it has been identified, the been_applied
136 * flags lose any meaning.
137 */
138 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED))
139 SET_FLAG (ob1, FLAG_BEEN_APPLIED);
140
141 if (QUERY_FLAG (ob2, FLAG_IDENTIFIED))
142 SET_FLAG (ob2, FLAG_BEEN_APPLIED);
143
144
145 /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something
146 * being locked in inventory should prevent merging.
147 * 0x4 in flags3 is CLIENT_SENT
148 */
149 if ((ob1->arch != ob2->arch) ||
150 (ob1->flags[0] != ob2->flags[0]) ||
151 (ob1->flags[1] != ob2->flags[1]) ||
152 ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~0x400000)) ||
153 ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) ||
154 (ob1->name != ob2->name) ||
155 (ob1->title != ob2->title) ||
156 (ob1->msg != ob2->msg) ||
157 (ob1->weight != ob2->weight) ||
158 (memcmp (&ob1->resist, &ob2->resist, sizeof (ob1->resist)) != 0) ||
159 (memcmp (&ob1->stats, &ob2->stats, sizeof (ob1->stats)) != 0) ||
160 (ob1->attacktype != ob2->attacktype) ||
161 (ob1->magic != ob2->magic) ||
162 (ob1->slaying != ob2->slaying) ||
163 (ob1->skill != ob2->skill) ||
164 (ob1->value != ob2->value) ||
165 (ob1->animation_id != ob2->animation_id) ||
166 (ob1->client_type != ob2->client_type) ||
167 (ob1->materialname != ob2->materialname) ||
168 (ob1->lore != ob2->lore) ||
169 (ob1->subtype != ob2->subtype) ||
170 (ob1->move_type != ob2->move_type) ||
171 (ob1->move_block != ob2->move_block) ||
172 (ob1->move_allow != ob2->move_allow) ||
173 (ob1->move_on != ob2->move_on) ||
174 (ob1->move_off != ob2->move_off) ||
175 (ob1->move_slow != ob2->move_slow) ||
176 (ob1->move_slow_penalty != ob2->move_slow_penalty))
177 return 0;
178
146 /* This is really a spellbook check - really, we should 179 /* This is really a spellbook check - really, we should
147 * check all objects in the inventory. 180 * check all objects in the inventory.
148 */ 181 */
149 if (ob1->inv || ob2->inv) { 182 if (ob1->inv || ob2->inv)
183 {
150 /* if one object has inventory but the other doesn't, not equiv */ 184 /* if one object has inventory but the other doesn't, not equiv */
151 if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) return 0; 185 if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv))
186 return 0;
152 187
153 /* Now check to see if the two inventory objects could merge */ 188 /* Now check to see if the two inventory objects could merge */
154 if (!CAN_MERGE(ob1->inv, ob2->inv)) return 0; 189 if (!CAN_MERGE (ob1->inv, ob2->inv))
190 return 0;
155 191
156 /* inventory ok - still need to check rest of this object to see 192 /* inventory ok - still need to check rest of this object to see
157 * if it is valid. 193 * if it is valid.
158 */ 194 */
159 } 195 }
160 196
161 /* If the objects have been identified, set the BEEN_APPLIED flag.
162 * This is to the comparison of the flags below will be OK. We
163 * just can't ignore the been applied or identified flags, as they
164 * are not equal - just if it has been identified, the been_applied
165 * flags lose any meaning.
166 */
167 if (QUERY_FLAG(ob1, FLAG_IDENTIFIED))
168 SET_FLAG(ob1, FLAG_BEEN_APPLIED);
169
170 if (QUERY_FLAG(ob2, FLAG_IDENTIFIED))
171 SET_FLAG(ob2, FLAG_BEEN_APPLIED);
172
173
174 /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something
175 * being locked in inventory should prevent merging.
176 * 0x4 in flags3 is CLIENT_SENT
177 */
178 if ((ob1->arch != ob2->arch) ||
179 (ob1->flags[0] != ob2->flags[0]) ||
180 (ob1->flags[1] != ob2->flags[1]) ||
181 ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~ 0x400000)) ||
182 ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) ||
183 (ob1->name != ob2->name) ||
184 (ob1->title != ob2->title) ||
185 (ob1->msg != ob2->msg) ||
186 (ob1->weight != ob2->weight) ||
187 (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist))!=0) ||
188 (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats))!=0) ||
189 (ob1->attacktype != ob2->attacktype) ||
190 (ob1->magic != ob2->magic) ||
191 (ob1->slaying != ob2->slaying) ||
192 (ob1->skill != ob2->skill) ||
193 (ob1->value != ob2->value) ||
194 (ob1->animation_id != ob2->animation_id) ||
195 (ob1->client_type != ob2->client_type) ||
196 (ob1->materialname != ob2->materialname) ||
197 (ob1->lore != ob2->lore) ||
198 (ob1->subtype != ob2->subtype) ||
199 (ob1->move_type != ob2->move_type) ||
200 (ob1->move_block != ob2->move_block) ||
201 (ob1->move_allow != ob2->move_allow) ||
202 (ob1->move_on != ob2->move_on) ||
203 (ob1->move_off != ob2->move_off) ||
204 (ob1->move_slow != ob2->move_slow) ||
205 (ob1->move_slow_penalty != ob2->move_slow_penalty)
206 )
207 return 0;
208
209 /* Don't merge objects that are applied. With the new 'body' code, 197 /* Don't merge objects that are applied. With the new 'body' code,
210 * it is possible for most any character to have more than one of 198 * it is possible for most any character to have more than one of
211 * some items equipped, and we don't want those to merge. 199 * some items equipped, and we don't want those to merge.
212 */ 200 */
213 if (QUERY_FLAG(ob1, FLAG_APPLIED) || QUERY_FLAG(ob2, FLAG_APPLIED)) 201 if (QUERY_FLAG (ob1, FLAG_APPLIED) || QUERY_FLAG (ob2, FLAG_APPLIED))
214 return 0; 202 return 0;
215 203
204 /* Note sure why the following is the case - either the object has to
205 * be animated or have a very low speed. Is this an attempted monster
206 * check?
207 */
208 if (!QUERY_FLAG (ob1, FLAG_ANIMATE)
209 && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED)
210 return 0;
211
216 switch (ob1->type) { 212 switch (ob1->type)
213 {
217 case SCROLL: 214 case SCROLL:
218 if (ob1->level != ob2->level) return 0; 215 if (ob1->level != ob2->level)
216 return 0;
219 break; 217 break;
220
221 } 218 }
219
222 if (ob1->key_values != NULL || ob2->key_values != NULL) { 220 if (ob1->key_values != NULL || ob2->key_values != NULL)
221 {
223 /* At least one of these has key_values. */ 222 /* At least one of these has key_values. */
224 if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) { 223 if ((ob1->key_values == NULL) != (ob2->key_values == NULL))
225 /* One has fields, but the other one doesn't. */ 224 /* One has fields, but the other one doesn't. */
226 return 0; 225 return 0;
227 } else if (!compare_ob_value_lists(ob1, ob2)) { 226 else if (!compare_ob_value_lists (ob1, ob2))
228 return 0; 227 return 0;
229 } 228 }
229
230 //TODO: generate an event or call into perl for additional checks
231 if (ob1->self || ob2->self)
230 } 232 {
233 ob1->optimise ();
234 ob2->optimise ();
231 235
236 if (ob1->self || ob2->self)
237 return 0;
238 }
239
232 /* Everything passes, must be OK. */ 240 /* Everything passes, must be OK. */
233 return 1; 241 return 1;
234} 242}
235
236/* 243/*
237 * sum_weight() is a recursive function which calculates the weight 244 * sum_weight() is a recursive function which calculates the weight
238 * an object is carrying. It goes through in figures out how much 245 * an object is carrying. It goes through in figures out how much
239 * containers are carrying, and sums it up. 246 * containers are carrying, and sums it up.
240 */ 247 */
276 return op; 283 return op;
277} 284}
278 285
279/* 286/*
280 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 287 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
281 * Some error messages. 288 * Some error messages.
282 * The result of the dump is stored in the static global errmsg array. 289 * The result of the dump is stored in the static global errmsg array.
283 */ 290 */
284 291
285void dump_object2(object *op) { 292void dump_object2(object *op) {
286errmsg[0] = 0; 293errmsg[0] = 0;
379 * Used only by the patch command, but not all that useful. 386 * Used only by the patch command, but not all that useful.
380 * Enables features like "patch <name-of-other-player> food 999" 387 * Enables features like "patch <name-of-other-player> food 999"
381 */ 388 */
382 389
383object *find_object_name(const char *str) { 390object *find_object_name(const char *str) {
384 const char *name=add_string(str); 391 const char *name = shstr::find (str);
385 object *op; 392 object *op;
386 for(op=objects;op!=NULL;op=op->next) 393 for(op=objects;op!=NULL;op=op->next)
387 if(op->name==name) 394 if(&op->name == name)
388 break; 395 break;
389 free_string(name); 396
390 return op; 397 return op;
391} 398}
392 399
393void free_all_object_data(void) { 400void free_all_object_data ()
394#ifdef MEMORY_DEBUG 401{
395 object *op, *next; 402 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
396
397 for (op=free_objects; op!=NULL; ) {
398 next=op->next;
399 free(op);
400 nrofallocobjects--;
401 nroffreeobjects--;
402 op=next;
403 }
404#endif
405 LOG(llevDebug,"%d allocated objects, %d free objects, STARMAX=%d\n",
406 nrofallocobjects, nroffreeobjects,STARTMAX);
407} 403}
408 404
409/* 405/*
410 * Returns the object which this object marks as being the owner. 406 * Returns the object which this object marks as being the owner.
411 * A id-scheme is used to avoid pointing to objects which have been 407 * A id-scheme is used to avoid pointing to objects which have been
438 op->owner->refcount--; 434 op->owner->refcount--;
439 435
440 op->owner = NULL; 436 op->owner = NULL;
441 op->ownercount = 0; 437 op->ownercount = 0;
442} 438}
443
444
445 439
446/* 440/*
447 * Sets the owner and sets the skill and exp pointers to owner's current 441 * Sets the owner and sets the skill and exp pointers to owner's current
448 * skill and experience objects. 442 * skill and experience objects.
449 */ 443 */
496 } 490 }
497 set_owner(op, owner); 491 set_owner(op, owner);
498 492
499} 493}
500 494
501/*
502 * Resets vital variables in an object
503 */
504
505void reset_object(object *op) {
506
507 op->name = NULL;
508 op->name_pl = NULL;
509 op->title = NULL;
510 op->race = NULL;
511 op->slaying = NULL;
512 op->skill = NULL;
513 op->msg = NULL;
514 op->materialname = NULL;
515 op->lore = NULL;
516 clear_object(op);
517}
518
519/* Zero the key_values on op, decrementing the shared-string 495/* Zero the key_values on op, decrementing the shared-string
520 * refcounts and freeing the links. 496 * refcounts and freeing the links.
521 */ 497 */
522static void free_key_values(object * op) { 498static void free_key_values(object * op)
523 key_value * i; 499{
524 key_value * next = NULL; 500 for (key_value *i = op->key_values; i != 0; )
525 501 {
526 if (op->key_values == NULL) return; 502 key_value *next = i->next;
503 delete i;
504 i = next;
527 505 }
528 for (i = op->key_values; i != NULL; i = next) { 506
529 /* Store next *first*. */
530 next = i->next;
531
532 if (i->key) FREE_AND_CLEAR_STR(i->key);
533 if (i->value) FREE_AND_CLEAR_STR(i->value);
534 i->next = NULL;
535 free(i);
536 }
537
538 op->key_values = NULL; 507 op->key_values = 0;
539} 508}
540 509
510void object::clear ()
511{
512 attachable_base::clear ();
513
514 free_key_values (this);
515
516 name = 0;
517 name_pl = 0;
518 title = 0;
519 race = 0;
520 slaying = 0;
521 skill = 0;
522 msg = 0;
523 lore = 0;
524 custom_name = 0;
525 materialname = 0;
526
527 memset (static_cast<object_pod *>(this), 0, sizeof (object_pod));
528
529 SET_FLAG (this, FLAG_REMOVED);
530}
531
532void object::clone (object *destination)
533{
534 *(object_copy *)destination = *(object_copy *)this;
535 *(object_pod *)destination = *(object_pod *)this;
536
537 if (self || cb)
538 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
539}
541 540
542/* 541/*
543 * clear_object() frees everything allocated by an object, and also 542 * clear_object() frees everything allocated by an object, and also
544 * clears all variables and flags to default settings. 543 * clears all variables and flags to default settings.
545 */ 544 */
546 545
547void clear_object(object *op) { 546void clear_object (object *op)
547{
548 op->clear (); 548 op->clear ();
549 549
550 event *evt;
551 event *evt2;
552
553 /* redo this to be simpler/more efficient. Was also seeing
554 * crashes in the old code. Move this to the top - am
555 * seeing periodic crashes in this code, and would like to have
556 * as much info available as possible (eg, object name).
557 */
558 for (evt = op->events; evt; evt=evt2) {
559 evt2 = evt->next;
560
561 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
562 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
563 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
564
565 free(evt);
566 }
567 op->events = NULL;
568
569 free_key_values(op);
570
571 /* the memset will clear all these values for us, but we need
572 * to reduce the refcount on them.
573 */
574 if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name);
575 if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl);
576 if (op->title != NULL) FREE_AND_CLEAR_STR(op->title);
577 if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race);
578 if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying);
579 if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill);
580 if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg);
581 if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore);
582 if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname);
583
584
585 memset((void*)op, 0, sizeof (object_special));
586 /* Below here, we clear things that are not done by the memset,
587 * or set default values that are not zero.
588 */
589 /* This is more or less true */
590 SET_FLAG(op, FLAG_REMOVED);
591
592
593 op->contr = NULL; 550 op->contr = NULL;
594 op->below=NULL; 551 op->below = NULL;
595 op->above=NULL; 552 op->above = NULL;
596 op->inv=NULL; 553 op->inv = NULL;
597 op->events=NULL;
598 op->container=NULL; 554 op->container=NULL;
599 op->env=NULL; 555 op->env=NULL;
600 op->more=NULL; 556 op->more=NULL;
601 op->head=NULL; 557 op->head=NULL;
602 op->map=NULL; 558 op->map=NULL;
603 op->refcount=0; 559 op->refcount=0;
604 op->active_next = NULL; 560 op->active_next = NULL;
605 op->active_prev = NULL; 561 op->active_prev = NULL;
606 /* What is not cleared is next, prev, and count */ 562 /* What is not cleared is next, prev, and count */
607 563
608 op->expmul=1.0; 564 op->expmul = 1.0;
609 op->face = blank_face; 565 op->face = blank_face;
610 op->attacked_by_count = (tag_t) -1; 566 op->attacked_by_count = -1;
567
611 if (settings.casting_time) 568 if (settings.casting_time)
612 op->casting_time = -1; 569 op->casting_time = -1;
613
614} 570}
615 571
616/* 572/*
617 * copy object first frees everything allocated by the second object, 573 * copy object first frees everything allocated by the second object,
618 * and then copies the contends of the first object into the second 574 * and then copies the contends of the first object into the second
620 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 576 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
621 * if the first object is freed, the pointers in the new object 577 * if the first object is freed, the pointers in the new object
622 * will point at garbage. 578 * will point at garbage.
623 */ 579 */
624 580
625void copy_object(object *op2, object *op) { 581void copy_object (object *op2, object *op)
626 int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); 582{
627 event *evt, *evt2, *evt_new; 583 bool is_freed = QUERY_FLAG (op, FLAG_FREED);
584 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED);
628 585
629 op->clear ();
630
631 /* Decrement the refcounts, but don't bother zeroing the fields;
632 they'll be overwritten by memcpy. */
633 if(op->name!=NULL) free_string(op->name);
634 if(op->name_pl!=NULL) free_string(op->name_pl);
635 if(op->title!=NULL) free_string(op->title);
636 if(op->race!=NULL) free_string(op->race);
637 if(op->slaying!=NULL) free_string(op->slaying);
638 if(op->skill!=NULL) free_string(op->skill);
639 if(op->msg!=NULL) free_string(op->msg);
640 if(op->lore!=NULL) free_string(op->lore);
641 if(op->materialname != NULL) free_string(op->materialname);
642 if(op->custom_name != NULL) free_string(op->custom_name);
643
644 /* Basically, same code as from clear_object() */
645 for (evt = op->events; evt; evt=evt2) {
646 evt2 = evt->next;
647
648 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
649 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
650 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
651
652 free(evt);
653 }
654 op->events = NULL;
655
656 free_key_values(op);
657
658 *(object_special *)op = *(object_special *)op2;
659 op2->clone (op); 586 op2->clone (op);
660 587
661 if(is_freed) SET_FLAG(op,FLAG_FREED); 588 if (is_freed) SET_FLAG (op, FLAG_FREED);
662 if(is_removed) SET_FLAG(op,FLAG_REMOVED); 589 if (is_removed) SET_FLAG (op, FLAG_REMOVED);
663 if(op->name!=NULL) add_refcount(op->name);
664 if(op->name_pl!=NULL) add_refcount(op->name_pl);
665 if(op->title!=NULL) add_refcount(op->title);
666 if(op->race!=NULL) add_refcount(op->race);
667 if(op->slaying!=NULL) add_refcount(op->slaying);
668 if(op->skill!=NULL) add_refcount(op->skill);
669 if(op->lore!=NULL) add_refcount(op->lore);
670 if(op->msg!=NULL) add_refcount(op->msg);
671 if(op->custom_name!=NULL) add_refcount(op->custom_name);
672 if (op->materialname != NULL) add_refcount(op->materialname);
673 590
674 if((op2->speed<0) && !editor) 591 if (op2->speed < 0)
675 op->speed_left=op2->speed_left-RANDOM()%200/100.0; 592 op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0;
676 593
677 /* Copy over event information */
678 evt2 = NULL;
679 for (evt = op2->events; evt; evt=evt->next) {
680 evt_new = (event *) malloc(sizeof(event));
681 memcpy(evt_new, evt, sizeof(event));
682 if (evt_new->hook) add_refcount(evt_new->hook);
683 if (evt_new->plugin) add_refcount(evt_new->plugin);
684 if (evt_new->options) add_refcount(evt_new->options);
685 evt_new->next = NULL;
686
687 /* Try to be a little clever here, and store away the
688 * last event we copied, so that its simpler to update the
689 * pointer.
690 */
691 if (evt2)
692 evt2->next = evt_new;
693 else
694 op->events = evt_new;
695
696 evt2 = evt_new;
697 }
698 /* Copy over key_values, if any. */ 594 /* Copy over key_values, if any. */
699 if (op2->key_values != NULL) { 595 if (op2->key_values != NULL)
596 {
700 key_value * tail = NULL; 597 key_value *tail = NULL;
701 key_value * i; 598 key_value *i;
702 599
703 op->key_values = NULL; 600 op->key_values = NULL;
704 601
705 for (i = op2->key_values; i != NULL; i = i->next) { 602 for (i = op2->key_values; i != NULL; i = i->next)
706 key_value * new_link = (key_value *) malloc(sizeof(key_value)); 603 {
604 key_value *new_link = new key_value;
707 605
708 new_link->next = NULL; 606 new_link->next = NULL;
709 new_link->key = add_refcount(i->key); 607 new_link->key = i->key;
710 if (i->value)
711 new_link->value = add_refcount(i->value); 608 new_link->value = i->value;
712 else
713 new_link->value = NULL;
714 609
715 /* Try and be clever here, too. */ 610 /* Try and be clever here, too. */
716 if (op->key_values == NULL) { 611 if (op->key_values == NULL)
612 {
717 op->key_values = new_link; 613 op->key_values = new_link;
718 tail = new_link; 614 tail = new_link;
615 }
719 } else { 616 else
617 {
720 tail->next = new_link; 618 tail->next = new_link;
721 tail = new_link; 619 tail = new_link;
722 } 620 }
723 } 621 }
724 } 622 }
725 623
726 update_ob_speed(op); 624 update_ob_speed (op);
727}
728
729/*
730 * expand_objects() allocates more objects for the list of unused objects.
731 * It is called from get_object() if the unused list is empty.
732 */
733
734void expand_objects(void) {
735 int i;
736 object *obj;
737 obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object));
738
739 if(obj==NULL)
740 fatal(OUT_OF_MEMORY);
741 free_objects=obj;
742 obj[0].prev=NULL;
743 obj[0].next= &obj[1],
744 SET_FLAG(&(obj[0]), FLAG_REMOVED);
745 SET_FLAG(&(obj[0]), FLAG_FREED);
746
747 for(i=1;i<OBJ_EXPAND-1;i++) {
748 obj[i].next= &obj[i+1],
749 obj[i].prev= &obj[i-1],
750 SET_FLAG(&(obj[i]), FLAG_REMOVED);
751 SET_FLAG(&(obj[i]), FLAG_FREED);
752 }
753 obj[OBJ_EXPAND-1].prev= &obj[OBJ_EXPAND-2],
754 obj[OBJ_EXPAND-1].next=NULL,
755 SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_REMOVED);
756 SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_FREED);
757
758 nrofallocobjects += OBJ_EXPAND;
759 nroffreeobjects += OBJ_EXPAND;
760}
761
762/*
763 * get_object() grabs an object from the list of unused objects, makes
764 * sure it is initialised, and returns it.
765 * If there are no free objects, expand_objects() is called to get more.
766 */
767
768object *get_object(void) {
769 object *op;
770
771 if(free_objects==NULL) {
772 expand_objects();
773 }
774 op=free_objects;
775#ifdef MEMORY_DEBUG
776 /* The idea is hopefully by doing a realloc, the memory
777 * debugging program will now use the current stack trace to
778 * report leaks.
779 */
780 op = realloc(op, sizeof(object));
781 SET_FLAG(op, FLAG_REMOVED);
782 SET_FLAG(op, FLAG_FREED);
783#endif
784
785 if(!QUERY_FLAG(op,FLAG_FREED)) {
786 LOG(llevError,"Fatal: Getting busy object.\n");
787 }
788 free_objects=op->next;
789 if(free_objects!=NULL)
790 free_objects->prev=NULL;
791 op->count= ++ob_count;
792 op->name=NULL;
793 op->name_pl=NULL;
794 op->title=NULL;
795 op->race=NULL;
796 op->slaying=NULL;
797 op->skill = NULL;
798 op->lore=NULL;
799 op->msg=NULL;
800 op->materialname=NULL;
801 op->next=objects;
802 op->prev=NULL;
803 op->active_next = NULL;
804 op->active_prev = NULL;
805 if(objects!=NULL)
806 objects->prev=op;
807 objects=op;
808 clear_object(op);
809 SET_FLAG(op,FLAG_REMOVED);
810 nroffreeobjects--;
811 return op;
812} 625}
813 626
814/* 627/*
815 * If an object with the IS_TURNABLE() flag needs to be turned due 628 * If an object with the IS_TURNABLE() flag needs to be turned due
816 * to the closest player being on the other side, this function can 629 * to the closest player being on the other side, this function can
836 /* No reason putting the archetypes objects on the speed list, 649 /* No reason putting the archetypes objects on the speed list,
837 * since they never really need to be updated. 650 * since they never really need to be updated.
838 */ 651 */
839 652
840 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { 653 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
841 LOG(llevError,"Object %s is freed but has speed.\n", op->name); 654 LOG(llevError,"Object %s is freed but has speed.\n", &op->name);
842#ifdef MANY_CORES 655#ifdef MANY_CORES
843 abort(); 656 abort();
844#else 657#else
845 op->speed = 0; 658 op->speed = 0;
846#endif 659#endif
930 * UP_OBJ_FACE: only the objects face has changed. 743 * UP_OBJ_FACE: only the objects face has changed.
931 */ 744 */
932 745
933void update_object(object *op, int action) { 746void update_object(object *op, int action) {
934 int update_now=0, flags; 747 int update_now=0, flags;
935 MoveType move_on, move_off, move_block, move_slow; 748 MoveType move_on, move_off, move_block, move_slow;
936 749
937 if (op == NULL) { 750 if (op == NULL) {
938 /* this should never happen */ 751 /* this should never happen */
939 LOG(llevDebug,"update_object() called for NULL object.\n"); 752 LOG(llevDebug,"update_object() called for NULL object.\n");
940 return; 753 return;
980 update_now=1; 793 update_now=1;
981 794
982 if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE)) 795 if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE))
983 update_now=1; 796 update_now=1;
984 797
798 if (op->type == SAFE_GROUND && !(flags & P_SAFE))
799 update_now=1;
800
985 if ((move_on | op->move_on) != move_on) update_now=1; 801 if ((move_on | op->move_on) != move_on) update_now=1;
802
986 if ((move_off | op->move_off) != move_off) update_now=1; 803 if ((move_off | op->move_off) != move_off) update_now=1;
804
987 /* This isn't perfect, but I don't expect a lot of objects to 805 /* This isn't perfect, but I don't expect a lot of objects to
988 * to have move_allow right now. 806 * to have move_allow right now.
989 */ 807 */
990 if (((move_block | op->move_block) & ~op->move_allow) != move_block) 808 if (((move_block | op->move_block) & ~op->move_allow) != move_block)
991 update_now=1; 809 update_now=1;
810
992 if ((move_slow | op->move_slow) != move_slow) update_now=1; 811 if ((move_slow | op->move_slow) != move_slow)
812 update_now=1;
993 } 813 }
994 /* if the object is being removed, we can't make intelligent 814 /* if the object is being removed, we can't make intelligent
995 * decisions, because remove_ob can't really pass the object 815 * decisions, because remove_ob can't really pass the object
996 * that is being removed. 816 * that is being removed.
997 */ 817 */
1011 831
1012 if(op->more!=NULL) 832 if(op->more!=NULL)
1013 update_object(op->more, action); 833 update_object(op->more, action);
1014} 834}
1015 835
836static std::vector<object *> mortals;
837
838void object::free_mortals ()
839{
840 for (std::vector<object *>::iterator i = mortals.begin (); i != mortals.end (); ++i)
841 delete *i;
842
843 mortals.clear ();
844}
845
846object::object ()
847{
848 SET_FLAG (this, FLAG_REMOVED);
849
850 expmul = 1.0;
851 face = blank_face;
852 attacked_by_count = -1;
853}
854
855object::~object ()
856{
857 free_key_values (this);
858}
859
860void
861object::link ()
862{
863 count = ++ob_count;
864
865 prev = 0;
866 next = objects;
867
868 if (objects)
869 objects->prev = this;
870
871 objects = this;
872}
873
874void
875object::unlink ()
876{
877 count = 0;
878
879 /* Remove this object from the list of used objects */
880 if (prev) prev->next = next;
881 if (next) next->prev = prev;
882 if (this == objects) objects = next;
883}
884
885object *object::create ()
886{
887 object *op = new object;
888 op->link ();
889 return op;
890}
1016 891
1017/* 892/*
1018 * free_object() frees everything allocated by an object, removes 893 * free_object() frees everything allocated by an object, removes
1019 * it from the list of used objects, and puts it on the list of 894 * it from the list of used objects, and puts it on the list of
1020 * free objects. The IS_FREED() flag is set in the object. 895 * free objects. The IS_FREED() flag is set in the object.
1022 * this function to succeed. 897 * this function to succeed.
1023 * 898 *
1024 * If free_inventory is set, free inventory as well. Else drop items in 899 * If free_inventory is set, free inventory as well. Else drop items in
1025 * inventory to the ground. 900 * inventory to the ground.
1026 */ 901 */
1027 902void
1028void free_object(object *ob) { 903object::free (bool free_inventory)
1029 free_object2(ob, 0); 904{
1030}
1031void free_object2(object *ob, int free_inventory) {
1032 object *tmp,*op;
1033
1034 ob->clear ();
1035
1036 if (!QUERY_FLAG(ob,FLAG_REMOVED)) { 905 if (!QUERY_FLAG (this, FLAG_REMOVED))
906 {
1037 LOG(llevDebug,"Free object called with non removed object\n"); 907 LOG (llevDebug, "Free object called with non removed object\n");
1038 dump_object(ob); 908 dump_object (this);
1039#ifdef MANY_CORES 909#ifdef MANY_CORES
1040 abort(); 910 abort ();
1041#endif 911#endif
1042 } 912 }
913
1043 if(QUERY_FLAG(ob,FLAG_FRIENDLY)) { 914 if (QUERY_FLAG (this, FLAG_FRIENDLY))
915 {
1044 LOG(llevMonster,"Warning: tried to free friendly object.\n"); 916 LOG (llevMonster, "Warning: tried to free friendly object.\n");
1045 remove_friendly_object(ob); 917 remove_friendly_object (this);
1046 } 918 }
919
1047 if(QUERY_FLAG(ob,FLAG_FREED)) { 920 if (QUERY_FLAG (this, FLAG_FREED))
921 {
1048 dump_object(ob); 922 dump_object (this);
1049 LOG(llevError,"Trying to free freed object.\n%s\n",errmsg); 923 LOG (llevError, "Trying to free freed object.\n%s\n", errmsg);
1050 return; 924 return;
925 }
926
927 if (more)
1051 } 928 {
1052 if(ob->more!=NULL) { 929 more->free (free_inventory);
1053 free_object2(ob->more, free_inventory); 930 more = 0;
1054 ob->more=NULL; 931 }
932
933 if (inv)
1055 } 934 {
1056 if (ob->inv) {
1057 /* Only if the space blocks everything do we not process - 935 /* Only if the space blocks everything do we not process -
1058 * if some form of movemnt is allowed, let objects 936 * if some form of movement is allowed, let objects
1059 * drop on that space. 937 * drop on that space.
1060 */ 938 */
1061 if (free_inventory || ob->map==NULL || ob->map->in_memory!=MAP_IN_MEMORY || 939 if (free_inventory || !map
940 || map->in_memory != MAP_IN_MEMORY
1062 (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) 941 || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL))
1063 { 942 {
1064 op=ob->inv; 943 object *op = inv;
1065 while(op!=NULL) { 944
1066 tmp=op->below; 945 while (op)
946 {
947 object *tmp = op->below;
1067 remove_ob(op); 948 remove_ob (op);
1068 free_object2(op, free_inventory); 949 op->free (free_inventory);
1069 op=tmp; 950 op = tmp;
1070 }
1071 }
1072 else { /* Put objects in inventory onto this space */
1073 op=ob->inv;
1074 while(op!=NULL) {
1075 tmp=op->below;
1076 remove_ob(op);
1077 if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) ||
1078 op->type==RUNE || op->type==TRAP || QUERY_FLAG(op,FLAG_IS_A_TEMPLATE))
1079 free_object(op);
1080 else {
1081 op->x=ob->x;
1082 op->y=ob->y;
1083 insert_ob_in_map(op,ob->map,NULL,0); /* Insert in same map as the envir */
1084 }
1085 op=tmp;
1086 }
1087 }
1088 } 951 }
952 }
953 else
954 { /* Put objects in inventory onto this space */
955 object *op = inv;
956
957 while (op)
958 {
959 object *tmp = op->below;
960 remove_ob (op);
961
962 if (QUERY_FLAG (op, FLAG_STARTEQUIP)
963 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE
964 || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE))
965 free_object (op);
966 else
967 {
968 op->x = x;
969 op->y = y;
970 insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */
971 }
972
973 op = tmp;
974 }
975 }
976 }
977
1089 /* Remove object from the active list */ 978 /* Remove object from the active list */
1090 ob->speed = 0; 979 speed = 0;
1091 update_ob_speed(ob); 980 update_ob_speed (this);
1092 981
982 unlink ();
983
1093 SET_FLAG(ob, FLAG_FREED); 984 SET_FLAG (this, FLAG_FREED);
1094 ob->count = 0;
1095 985
1096 /* Remove this object from the list of used objects */ 986 mortals.push_back (this);
1097 if(ob->prev==NULL) {
1098 objects=ob->next;
1099 if(objects!=NULL)
1100 objects->prev=NULL;
1101 }
1102 else {
1103 ob->prev->next=ob->next;
1104 if(ob->next!=NULL)
1105 ob->next->prev=ob->prev;
1106 }
1107
1108 if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name);
1109 if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1110 if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title);
1111 if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race);
1112 if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying);
1113 if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill);
1114 if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore);
1115 if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg);
1116 if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname);
1117
1118
1119 /* Why aren't events freed? */
1120 free_key_values(ob);
1121
1122#if 0 /* MEMORY_DEBUG*/
1123 /* This is a nice idea. Unfortunately, a lot of the code in crossfire
1124 * presumes the freed_object will stick around for at least a little
1125 * bit
1126 */
1127 /* this is necessary so that memory debugging programs will
1128 * be able to accurately report source of malloc. If we recycle
1129 * objects, then some other area may be doing the get_object
1130 * and not freeing it, but the original one that malloc'd the
1131 * object will get the blame.
1132 */
1133 free(ob);
1134#else
1135
1136 /* Now link it with the free_objects list: */
1137 ob->prev=NULL;
1138 ob->next=free_objects;
1139 if(free_objects!=NULL)
1140 free_objects->prev=ob;
1141 free_objects=ob;
1142 nroffreeobjects++;
1143#endif
1144}
1145
1146/*
1147 * count_free() returns the number of objects on the list of free objects.
1148 */
1149
1150int count_free(void) {
1151 int i=0;
1152 object *tmp=free_objects;
1153 while(tmp!=NULL)
1154 tmp=tmp->next, i++;
1155 return i;
1156}
1157
1158/*
1159 * count_used() returns the number of objects on the list of used objects.
1160 */
1161
1162int count_used(void) {
1163 int i=0;
1164 object *tmp=objects;
1165 while(tmp!=NULL)
1166 tmp=tmp->next, i++;
1167 return i;
1168}
1169
1170/*
1171 * count_active() returns the number of objects on the list of active objects.
1172 */
1173
1174int count_active(void) {
1175 int i=0;
1176 object *tmp=active_objects;
1177 while(tmp!=NULL)
1178 tmp=tmp->active_next, i++;
1179 return i;
1180} 987}
1181 988
1182/* 989/*
1183 * sub_weight() recursively (outwards) subtracts a number from the 990 * sub_weight() recursively (outwards) subtracts a number from the
1184 * weight of an object (and what is carried by it's environment(s)). 991 * weight of an object (and what is carried by it's environment(s)).
1262 /* we set up values so that it could be inserted into 1069 /* we set up values so that it could be inserted into
1263 * the map, but we don't actually do that - it is up 1070 * the map, but we don't actually do that - it is up
1264 * to the caller to decide what we want to do. 1071 * to the caller to decide what we want to do.
1265 */ 1072 */
1266 op->x=op->env->x,op->y=op->env->y; 1073 op->x=op->env->x,op->y=op->env->y;
1267 op->ox=op->x,op->oy=op->y;
1268 op->map=op->env->map; 1074 op->map=op->env->map;
1269 op->above=NULL,op->below=NULL; 1075 op->above=NULL,op->below=NULL;
1270 op->env=NULL; 1076 op->env=NULL;
1271 return; 1077 return;
1272 } 1078 }
1347 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) { 1153 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1348 1154
1349 move_apply(tmp, op, NULL); 1155 move_apply(tmp, op, NULL);
1350 if (was_destroyed (op, tag)) { 1156 if (was_destroyed (op, tag)) {
1351 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " 1157 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1352 "leaving object\n", tmp->name, tmp->arch->name); 1158 "leaving object\n", &tmp->name, &tmp->arch->name);
1353 } 1159 }
1354 } 1160 }
1355 1161
1356 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1162 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1357 1163
1483 * we set the map, that doesn't work if the location is within 1289 * we set the map, that doesn't work if the location is within
1484 * a map and this is straddling an edge. So only if coordinate 1290 * a map and this is straddling an edge. So only if coordinate
1485 * is clear wrong do we normalize it. 1291 * is clear wrong do we normalize it.
1486 */ 1292 */
1487 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { 1293 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
1488 /* Debugging information so you can see the last coordinates this object had */
1489 more->ox = more->x;
1490 more->oy = more->y;
1491 more->map = get_map_from_coord(m, &more->x, &more->y); 1294 more->map = get_map_from_coord(m, &more->x, &more->y);
1492 } else if (!more->map) { 1295 } else if (!more->map) {
1493 /* For backwards compatibility - when not dealing with tiled maps, 1296 /* For backwards compatibility - when not dealing with tiled maps,
1494 * more->map should always point to the parent. 1297 * more->map should always point to the parent.
1495 */ 1298 */
1501 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n"); 1304 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n");
1502 return NULL; 1305 return NULL;
1503 } 1306 }
1504 } 1307 }
1505 CLEAR_FLAG(op,FLAG_REMOVED); 1308 CLEAR_FLAG(op,FLAG_REMOVED);
1506
1507 /* Debugging information so you can see the last coordinates this object had */
1508 op->ox=op->x;
1509 op->oy=op->y;
1510 1309
1511 /* Ideally, the caller figures this out. However, it complicates a lot 1310 /* Ideally, the caller figures this out. However, it complicates a lot
1512 * of areas of callers (eg, anything that uses find_free_spot would now 1311 * of areas of callers (eg, anything that uses find_free_spot would now
1513 * need extra work 1312 * need extra work
1514 */ 1313 */
1582 * looks like instead of lots of conditions here. 1381 * looks like instead of lots of conditions here.
1583 * makes things faster, and effectively the same result. 1382 * makes things faster, and effectively the same result.
1584 */ 1383 */
1585 1384
1586 /* Have object 'fall below' other objects that block view. 1385 /* Have object 'fall below' other objects that block view.
1587 * Unless those objects are exits, type 66 1386 * Unless those objects are exits, type 66
1588 * If INS_ON_TOP is used, don't do this processing 1387 * If INS_ON_TOP is used, don't do this processing
1589 * Need to find the object that in fact blocks view, otherwise 1388 * Need to find the object that in fact blocks view, otherwise
1590 * stacking is a bit odd. 1389 * stacking is a bit odd.
1591 */ 1390 */
1592 if (!(flag & INS_ON_TOP) && 1391 if (!(flag & INS_ON_TOP) &&
1593 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) && 1392 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) &&
1594 (op->face && !op->face->visibility)) { 1393 (op->face && !op->face->visibility)) {
1595 for (last=top; last != floor; last=last->below) 1394 for (last=top; last != floor; last=last->below)
1596 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; 1395 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1597 /* Check to see if we found the object that blocks view, 1396 /* Check to see if we found the object that blocks view,
1598 * and make sure we have a below pointer for it so that 1397 * and make sure we have a below pointer for it so that
1599 * we can get inserted below this one, which requires we 1398 * we can get inserted below this one, which requires we
1600 * set top to the object below us. 1399 * set top to the object below us.
1601 */ 1400 */
1630 1429
1631 /* If we have a floor, we know the player, if any, will be above 1430 /* If we have a floor, we know the player, if any, will be above
1632 * it, so save a few ticks and start from there. 1431 * it, so save a few ticks and start from there.
1633 */ 1432 */
1634 if (!(flag & INS_MAP_LOAD)) 1433 if (!(flag & INS_MAP_LOAD))
1635 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { 1434 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) {
1636 if (tmp->type == PLAYER) 1435 if (tmp->type == PLAYER)
1637 tmp->contr->socket.update_look=1; 1436 tmp->contr->socket.update_look=1;
1638 } 1437 }
1639 1438
1640 /* If this object glows, it may affect lighting conditions that are 1439 /* If this object glows, it may affect lighting conditions that are
1641 * visible to others on this map. But update_all_los is really 1440 * visible to others on this map. But update_all_los is really
1642 * an inefficient way to do this, as it means los for all players 1441 * an inefficient way to do this, as it means los for all players
1643 * on the map will get recalculated. The players could very well 1442 * on the map will get recalculated. The players could very well
1715 object *newob; 1514 object *newob;
1716 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1515 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1717 1516
1718 if(orig_ob->nrof<nr) { 1517 if(orig_ob->nrof<nr) {
1719 sprintf(errmsg,"There are only %d %ss.", 1518 sprintf(errmsg,"There are only %d %ss.",
1720 orig_ob->nrof?orig_ob->nrof:1, orig_ob->name); 1519 orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name);
1721 return NULL; 1520 return NULL;
1722 } 1521 }
1723 newob = object_create_clone(orig_ob); 1522 newob = object_create_clone(orig_ob);
1724 if((orig_ob->nrof-=nr)<1) { 1523 if((orig_ob->nrof-=nr)<1) {
1725 if ( ! is_removed) 1524 if ( ! is_removed)
1872 "Warning: Tried to insert object wrong part of multipart object.\n"); 1671 "Warning: Tried to insert object wrong part of multipart object.\n");
1873 where = where->head; 1672 where = where->head;
1874 } 1673 }
1875 if (op->more) { 1674 if (op->more) {
1876 LOG(llevError, "Tried to insert multipart object %s (%d)\n", 1675 LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1877 op->name, op->count); 1676 &op->name, op->count);
1878 return op; 1677 return op;
1879 } 1678 }
1880 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); 1679 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1881 CLEAR_FLAG(op, FLAG_REMOVED); 1680 CLEAR_FLAG(op, FLAG_REMOVED);
1882 if(op->nrof) { 1681 if(op->nrof) {
1916 op->map=NULL; 1715 op->map=NULL;
1917 op->env=where; 1716 op->env=where;
1918 op->above=NULL; 1717 op->above=NULL;
1919 op->below=NULL; 1718 op->below=NULL;
1920 op->x=0,op->y=0; 1719 op->x=0,op->y=0;
1921 op->ox=0,op->oy=0;
1922 1720
1923 /* reset the light list and los of the players on the map */ 1721 /* reset the light list and los of the players on the map */
1924 if((op->glow_radius!=0)&&where->map) 1722 if((op->glow_radius!=0)&&where->map)
1925 { 1723 {
1926#ifdef DEBUG_LIGHTS 1724#ifdef DEBUG_LIGHTS
1968{ 1766{
1969 object *tmp; 1767 object *tmp;
1970 tag_t tag; 1768 tag_t tag;
1971 mapstruct *m=op->map; 1769 mapstruct *m=op->map;
1972 int x=op->x, y=op->y; 1770 int x=op->x, y=op->y;
1973 MoveType move_on, move_slow, move_block; 1771 MoveType move_on, move_slow, move_block;
1974 1772
1975 if(QUERY_FLAG(op,FLAG_NO_APPLY)) 1773 if(QUERY_FLAG(op,FLAG_NO_APPLY))
1976 return 0; 1774 return 0;
1977 1775
1978 tag = op->count; 1776 tag = op->count;
2296int find_dir(mapstruct *m, int x, int y, object *exclude) { 2094int find_dir(mapstruct *m, int x, int y, object *exclude) {
2297 int i,max=SIZEOFFREE, mflags; 2095 int i,max=SIZEOFFREE, mflags;
2298 sint16 nx, ny; 2096 sint16 nx, ny;
2299 object *tmp; 2097 object *tmp;
2300 mapstruct *mp; 2098 mapstruct *mp;
2301 MoveType blocked, move_type; 2099 MoveType blocked, move_type;
2302 2100
2303 if (exclude && exclude->head) { 2101 if (exclude && exclude->head) {
2304 exclude = exclude->head; 2102 exclude = exclude->head;
2305 move_type = exclude->move_type; 2103 move_type = exclude->move_type;
2306 } else { 2104 } else {
2598 fclose(tempfile); 2396 fclose(tempfile);
2599 2397
2600 op=get_object(); 2398 op=get_object();
2601 2399
2602 object_thawer thawer (filename); 2400 object_thawer thawer (filename);
2401
2603 if (thawer) 2402 if (thawer)
2604 load_object(thawer,op,LO_NEWFILE,0); 2403 load_object(thawer,op,0);
2404
2605 LOG(llevDebug," load str completed, object=%s\n",op->name); 2405 LOG(llevDebug," load str completed, object=%s\n", &op->name);
2606 CLEAR_FLAG(op,FLAG_REMOVED); 2406 CLEAR_FLAG(op,FLAG_REMOVED);
2607 2407
2608 return op; 2408 return op;
2609} 2409}
2610 2410
2649 */ 2449 */
2650const char * get_ob_key_value(const object * op, const char * const key) { 2450const char * get_ob_key_value(const object * op, const char * const key) {
2651 key_value * link; 2451 key_value * link;
2652 const char * canonical_key; 2452 const char * canonical_key;
2653 2453
2654 canonical_key = find_string(key); 2454 canonical_key = shstr::find (key);
2655 2455
2656 if (canonical_key == NULL) { 2456 if (canonical_key == NULL) {
2657 /* 1. There being a field named key on any object 2457 /* 1. There being a field named key on any object
2658 * implies there'd be a shared string to find. 2458 * implies there'd be a shared string to find.
2659 * 2. Since there isn't, no object has this field. 2459 * 2. Since there isn't, no object has this field.
2682 * Unless add_key is TRUE, it won't add fields, only change the value of existing 2482 * Unless add_key is TRUE, it won't add fields, only change the value of existing
2683 * keys. 2483 * keys.
2684 * 2484 *
2685 * Returns TRUE on success. 2485 * Returns TRUE on success.
2686 */ 2486 */
2687int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) { 2487int set_ob_key_value_s(object * op, const shstr &canonical_key, const char * value, int add_key) {
2688 key_value * field = NULL, *last=NULL; 2488 key_value * field = NULL, *last=NULL;
2689 2489
2690 for (field=op->key_values; field != NULL; field=field->next) { 2490 for (field=op->key_values; field != NULL; field=field->next) {
2691 if (field->key != canonical_key) { 2491 if (field->key != canonical_key) {
2692 last = field; 2492 last = field;
2693 continue; 2493 continue;
2694 } 2494 }
2695 2495
2696 if (field->value) FREE_AND_CLEAR_STR(field->value);
2697 if (value) 2496 if (value)
2698 field->value = add_string(value); 2497 field->value = value;
2699 else { 2498 else {
2700 /* Basically, if the archetype has this key set, 2499 /* Basically, if the archetype has this key set,
2701 * we need to store the null value so when we save 2500 * we need to store the null value so when we save
2702 * it, we save the empty value so that when we load, 2501 * it, we save the empty value so that when we load,
2703 * we get this value back again. 2502 * we get this value back again.
2704 */ 2503 */
2705 if (get_ob_key_link(&op->arch->clone, canonical_key)) 2504 if (get_ob_key_link (&op->arch->clone, canonical_key))
2706 field->value = NULL; 2505 field->value = 0;
2707 else { 2506 else
2708 /* Delete this link */ 2507 {
2709 if (field->key) FREE_AND_CLEAR_STR(field->key);
2710 if (field->value) FREE_AND_CLEAR_STR(field->value);
2711 if (last) last->next = field->next; 2508 if (last) last->next = field->next;
2712 else op->key_values = field->next; 2509 else op->key_values = field->next;
2510
2713 free(field); 2511 delete field;
2714 } 2512 }
2715 } 2513 }
2716 return TRUE; 2514 return TRUE;
2717 } 2515 }
2718 /* IF we get here, key doesn't exist */ 2516 /* IF we get here, key doesn't exist */
2719 2517
2728 * be here. If user wants to store empty strings, 2526 * be here. If user wants to store empty strings,
2729 * should pass in "" 2527 * should pass in ""
2730 */ 2528 */
2731 if (value == NULL) return TRUE; 2529 if (value == NULL) return TRUE;
2732 2530
2733 field = (key_value *) malloc(sizeof(key_value)); 2531 field = new key_value;
2734 2532
2735 field->key = add_refcount(canonical_key); 2533 field->key = canonical_key;
2736 field->value = add_string(value); 2534 field->value = value;
2737 /* Usual prepend-addition. */ 2535 /* Usual prepend-addition. */
2738 field->next = op->key_values; 2536 field->next = op->key_values;
2739 op->key_values = field; 2537 op->key_values = field;
2740 2538
2741 return TRUE; 2539 return TRUE;
2748 * and not add new ones. 2546 * and not add new ones.
2749 * In general, should be little reason FALSE is ever passed in for add_key 2547 * In general, should be little reason FALSE is ever passed in for add_key
2750 * 2548 *
2751 * Returns TRUE on success. 2549 * Returns TRUE on success.
2752 */ 2550 */
2753int set_ob_key_value(object * op, const char * key, const char * value, int add_key) { 2551int set_ob_key_value(object * op, const char *key, const char * value, int add_key)
2754 const char * canonical_key = NULL; 2552{
2755 int floating_ref = FALSE; 2553 shstr key_ (key);
2756 int ret;
2757
2758 /* HACK This mess is to make sure set_ob_value() passes a shared string
2759 * to get_ob_key_link(), without leaving a leaked refcount.
2760 */
2761
2762 canonical_key = find_string(key);
2763 if (canonical_key == NULL) {
2764 canonical_key = add_string(key);
2765 floating_ref = TRUE;
2766 }
2767
2768 ret = set_ob_key_value_s(op, canonical_key, value, add_key); 2554 return set_ob_key_value_s (op, key_, value, add_key);
2769
2770 if (floating_ref) {
2771 free_string(canonical_key);
2772 }
2773
2774 return ret;
2775} 2555}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines