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.8 by root, Tue Aug 29 08:01:35 2006 UTC vs.
Revision 1.16 by root, Mon Sep 4 16:46:32 2006 UTC

1/* 1/*
2 * static char *rcsid_object_c = 2 * static char *rcsid_object_c =
3 * "$Id: object.C,v 1.8 2006/08/29 08:01:35 root Exp $"; 3 * "$Id: object.C,v 1.16 2006/09/04 16:46:32 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]=
121 * 111 *
122 * Improvements made with merge: Better checking on potion, and also 112 * Improvements made with merge: Better checking on potion, and also
123 * check weight 113 * check weight
124 */ 114 */
125 115
126int CAN_MERGE(object *ob1, object *ob2) { 116bool
127 117object::can_merge (object *ob1, object *ob2)
118{
128 /* A couple quicksanity checks */ 119 /* A couple quicksanity checks */
129 if ((ob1 == ob2) || (ob1->type != ob2->type)) return 0; 120 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; 121 return 0;
138 122
123 if (ob1->speed != ob2->speed)
124 return 0;
125
139 /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that 126 /* 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 127 * value could not be stored in a sint32 (which unfortunately sometimes is
141 * used to store nrof). 128 * used to store nrof).
142 */ 129 */
143 if (ob1->nrof+ob2->nrof >= 1UL<<31) 130 if (ob1->nrof + ob2->nrof >= 1UL << 31)
144 return 0; 131 return 0;
145 132
133 /* If the objects have been identified, set the BEEN_APPLIED flag.
134 * This is to the comparison of the flags below will be OK. We
135 * just can't ignore the been applied or identified flags, as they
136 * are not equal - just if it has been identified, the been_applied
137 * flags lose any meaning.
138 */
139 if (QUERY_FLAG (ob1, FLAG_IDENTIFIED))
140 SET_FLAG (ob1, FLAG_BEEN_APPLIED);
141
142 if (QUERY_FLAG (ob2, FLAG_IDENTIFIED))
143 SET_FLAG (ob2, FLAG_BEEN_APPLIED);
144
145
146 /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something
147 * being locked in inventory should prevent merging.
148 * 0x4 in flags3 is CLIENT_SENT
149 */
150 if ((ob1->arch != ob2->arch) ||
151 (ob1->flags[0] != ob2->flags[0]) ||
152 (ob1->flags[1] != ob2->flags[1]) ||
153 ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~0x400000)) ||
154 ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) ||
155 (ob1->name != ob2->name) ||
156 (ob1->title != ob2->title) ||
157 (ob1->msg != ob2->msg) ||
158 (ob1->weight != ob2->weight) ||
159 (memcmp (&ob1->resist, &ob2->resist, sizeof (ob1->resist)) != 0) ||
160 (memcmp (&ob1->stats, &ob2->stats, sizeof (ob1->stats)) != 0) ||
161 (ob1->attacktype != ob2->attacktype) ||
162 (ob1->magic != ob2->magic) ||
163 (ob1->slaying != ob2->slaying) ||
164 (ob1->skill != ob2->skill) ||
165 (ob1->value != ob2->value) ||
166 (ob1->animation_id != ob2->animation_id) ||
167 (ob1->client_type != ob2->client_type) ||
168 (ob1->materialname != ob2->materialname) ||
169 (ob1->lore != ob2->lore) ||
170 (ob1->subtype != ob2->subtype) ||
171 (ob1->move_type != ob2->move_type) ||
172 (ob1->move_block != ob2->move_block) ||
173 (ob1->move_allow != ob2->move_allow) ||
174 (ob1->move_on != ob2->move_on) ||
175 (ob1->move_off != ob2->move_off) ||
176 (ob1->move_slow != ob2->move_slow) ||
177 (ob1->move_slow_penalty != ob2->move_slow_penalty))
178 return 0;
179
146 /* This is really a spellbook check - really, we should 180 /* This is really a spellbook check - really, we should
147 * check all objects in the inventory. 181 * check all objects in the inventory.
148 */ 182 */
149 if (ob1->inv || ob2->inv) { 183 if (ob1->inv || ob2->inv)
184 {
150 /* if one object has inventory but the other doesn't, not equiv */ 185 /* if one object has inventory but the other doesn't, not equiv */
151 if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) return 0; 186 if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv))
187 return 0;
152 188
153 /* Now check to see if the two inventory objects could merge */ 189 /* Now check to see if the two inventory objects could merge */
154 if (!CAN_MERGE(ob1->inv, ob2->inv)) return 0; 190 if (!CAN_MERGE (ob1->inv, ob2->inv))
191 return 0;
155 192
156 /* inventory ok - still need to check rest of this object to see 193 /* inventory ok - still need to check rest of this object to see
157 * if it is valid. 194 * if it is valid.
158 */ 195 */
159 } 196 }
160 197
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, 198 /* 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 199 * 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. 200 * some items equipped, and we don't want those to merge.
212 */ 201 */
213 if (QUERY_FLAG(ob1, FLAG_APPLIED) || QUERY_FLAG(ob2, FLAG_APPLIED)) 202 if (QUERY_FLAG (ob1, FLAG_APPLIED) || QUERY_FLAG (ob2, FLAG_APPLIED))
214 return 0; 203 return 0;
215 204
205 /* Note sure why the following is the case - either the object has to
206 * be animated or have a very low speed. Is this an attempted monster
207 * check?
208 */
209 if (!QUERY_FLAG (ob1, FLAG_ANIMATE)
210 && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED)
211 return 0;
212
216 switch (ob1->type) { 213 switch (ob1->type)
214 {
217 case SCROLL: 215 case SCROLL:
218 if (ob1->level != ob2->level) return 0; 216 if (ob1->level != ob2->level)
217 return 0;
219 break; 218 break;
220
221 } 219 }
220
222 if (ob1->key_values != NULL || ob2->key_values != NULL) { 221 if (ob1->key_values != NULL || ob2->key_values != NULL)
222 {
223 /* At least one of these has key_values. */ 223 /* At least one of these has key_values. */
224 if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) { 224 if ((ob1->key_values == NULL) != (ob2->key_values == NULL))
225 /* One has fields, but the other one doesn't. */ 225 /* One has fields, but the other one doesn't. */
226 return 0; 226 return 0;
227 } else if (!compare_ob_value_lists(ob1, ob2)) { 227 else if (!compare_ob_value_lists (ob1, ob2))
228 return 0; 228 return 0;
229 } 229 }
230
231 //TODO: generate an event or call into perl for additional checks
232 if (ob1->self || ob2->self)
230 } 233 {
234 ob1->optimise ();
235 ob2->optimise ();
231 236
237 if (ob1->self || ob2->self)
238 return 0;
239 }
240
232 /* Everything passes, must be OK. */ 241 /* Everything passes, must be OK. */
233 return 1; 242 return 1;
234} 243}
235
236/* 244/*
237 * sum_weight() is a recursive function which calculates the weight 245 * sum_weight() is a recursive function which calculates the weight
238 * an object is carrying. It goes through in figures out how much 246 * an object is carrying. It goes through in figures out how much
239 * containers are carrying, and sums it up. 247 * containers are carrying, and sums it up.
240 */ 248 */
276 return op; 284 return op;
277} 285}
278 286
279/* 287/*
280 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 288 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
281 * Some error messages. 289 * Some error messages.
282 * The result of the dump is stored in the static global errmsg array. 290 * The result of the dump is stored in the static global errmsg array.
283 */ 291 */
284 292
285void dump_object2(object *op) { 293void dump_object2(object *op) {
294errmsg[0] = 0;
295return;
296 //TODO//D#d#
297#if 0
286 char *cp; 298 char *cp;
287/* object *tmp;*/ 299/* object *tmp;*/
288 300
289 if(op->arch!=NULL) { 301 if(op->arch!=NULL) {
290 strcat(errmsg,"arch "); 302 strcat(errmsg,"arch ");
315 for (tmp=op->inv; tmp; tmp=tmp->below) 327 for (tmp=op->inv; tmp; tmp=tmp->below)
316 dump_object2(tmp); 328 dump_object2(tmp);
317#endif 329#endif
318 strcat(errmsg,"end\n"); 330 strcat(errmsg,"end\n");
319 } 331 }
332#endif
320} 333}
321 334
322/* 335/*
323 * Dumps an object. Returns output in the static global errmsg array. 336 * Dumps an object. Returns output in the static global errmsg array.
324 */ 337 */
329 return; 342 return;
330 } 343 }
331 errmsg[0]='\0'; 344 errmsg[0]='\0';
332 dump_object2(op); 345 dump_object2(op);
333} 346}
334
335/* GROS - Dumps an object. Return the result into a string */
336/* Note that no checking is done for the validity of the target string, so */
337/* you need to be sure that you allocated enough space for it. */
338void dump_me(object *op, char *outstr)
339{
340 char *cp;
341
342 if(op==NULL)
343 {
344 strcpy(outstr,"[NULL pointer]");
345 return;
346 }
347 outstr[0]='\0';
348
349 if(op->arch!=NULL)
350 {
351 strcat(outstr,"arch ");
352 strcat(outstr,op->arch->name?op->arch->name:"(null)");
353 strcat(outstr,"\n");
354 if((cp=get_ob_diff(op,&empty_archetype->clone))!=NULL)
355 strcat(outstr,cp);
356 strcat(outstr,"end\n");
357 }
358 else
359 {
360 strcat(outstr,"Object ");
361 if (op->name==NULL)
362 strcat(outstr, "(null)");
363 else
364 strcat(outstr,op->name);
365 strcat(outstr,"\n");
366 strcat(outstr,"end\n");
367 }
368}
369
370/*
371 * This is really verbose...Can be triggered by the P key while in DM mode.
372 * All objects are dumped to stderr (or alternate logfile, if in server-mode)
373 */
374 347
375void dump_all_objects(void) { 348void dump_all_objects(void) {
376 object *op; 349 object *op;
377 for(op=objects;op!=NULL;op=op->next) { 350 for(op=objects;op!=NULL;op=op->next) {
378 dump_object(op); 351 dump_object(op);
414 * Used only by the patch command, but not all that useful. 387 * Used only by the patch command, but not all that useful.
415 * Enables features like "patch <name-of-other-player> food 999" 388 * Enables features like "patch <name-of-other-player> food 999"
416 */ 389 */
417 390
418object *find_object_name(const char *str) { 391object *find_object_name(const char *str) {
419 const char *name=add_string(str); 392 const char *name = shstr::find (str);
420 object *op; 393 object *op;
421 for(op=objects;op!=NULL;op=op->next) 394 for(op=objects;op!=NULL;op=op->next)
422 if(op->name==name) 395 if(&op->name == name)
423 break; 396 break;
424 free_string(name); 397
425 return op; 398 return op;
426} 399}
427 400
428void free_all_object_data(void) { 401void free_all_object_data ()
429#ifdef MEMORY_DEBUG 402{
430 object *op, *next; 403 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
431
432 for (op=free_objects; op!=NULL; ) {
433 next=op->next;
434 free(op);
435 nrofallocobjects--;
436 nroffreeobjects--;
437 op=next;
438 }
439#endif
440 LOG(llevDebug,"%d allocated objects, %d free objects, STARMAX=%d\n",
441 nrofallocobjects, nroffreeobjects,STARTMAX);
442} 404}
443 405
444/* 406/*
445 * Returns the object which this object marks as being the owner. 407 * Returns the object which this object marks as being the owner.
446 * A id-scheme is used to avoid pointing to objects which have been 408 * A id-scheme is used to avoid pointing to objects which have been
473 op->owner->refcount--; 435 op->owner->refcount--;
474 436
475 op->owner = NULL; 437 op->owner = NULL;
476 op->ownercount = 0; 438 op->ownercount = 0;
477} 439}
478
479
480 440
481/* 441/*
482 * Sets the owner and sets the skill and exp pointers to owner's current 442 * Sets the owner and sets the skill and exp pointers to owner's current
483 * skill and experience objects. 443 * skill and experience objects.
484 */ 444 */
531 } 491 }
532 set_owner(op, owner); 492 set_owner(op, owner);
533 493
534} 494}
535 495
536/*
537 * Resets vital variables in an object
538 */
539
540void reset_object(object *op) {
541
542 op->name = NULL;
543 op->name_pl = NULL;
544 op->title = NULL;
545 op->race = NULL;
546 op->slaying = NULL;
547 op->skill = NULL;
548 op->msg = NULL;
549 op->materialname = NULL;
550 op->lore = NULL;
551 clear_object(op);
552}
553
554/* Zero the key_values on op, decrementing the shared-string 496/* Zero the key_values on op, decrementing the shared-string
555 * refcounts and freeing the links. 497 * refcounts and freeing the links.
556 */ 498 */
557static void free_key_values(object * op) { 499static void free_key_values(object * op)
558 key_value * i; 500{
559 key_value * next = NULL; 501 for (key_value *i = op->key_values; i != 0; )
560 502 {
561 if (op->key_values == NULL) return; 503 key_value *next = i->next;
504 delete i;
505 i = next;
562 506 }
563 for (i = op->key_values; i != NULL; i = next) { 507
564 /* Store next *first*. */
565 next = i->next;
566
567 if (i->key) FREE_AND_CLEAR_STR(i->key);
568 if (i->value) FREE_AND_CLEAR_STR(i->value);
569 i->next = NULL;
570 free(i);
571 }
572
573 op->key_values = NULL; 508 op->key_values = 0;
574} 509}
575 510
511void object::clear ()
512{
513 attachable_base::clear ();
514
515 free_key_values (this);
516
517 name = 0;
518 name_pl = 0;
519 title = 0;
520 race = 0;
521 slaying = 0;
522 skill = 0;
523 msg = 0;
524 lore = 0;
525 custom_name = 0;
526 materialname = 0;
527
528 memset (static_cast<object_pod *>(this), 0, sizeof (object_pod));
529
530 SET_FLAG (this, FLAG_REMOVED);
531}
532
533void object::clone (object *destination)
534{
535 *(object_copy *)destination = *(object_copy *)this;
536 *(object_pod *)destination = *(object_pod *)this;
537
538 if (self || cb)
539 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
540}
576 541
577/* 542/*
578 * clear_object() frees everything allocated by an object, and also 543 * clear_object() frees everything allocated by an object, and also
579 * clears all variables and flags to default settings. 544 * clears all variables and flags to default settings.
580 */ 545 */
581 546
582void clear_object(object *op) { 547void clear_object (object *op)
548{
583 op->clear (); 549 op->clear ();
584 550
585 event *evt;
586 event *evt2;
587
588 /* redo this to be simpler/more efficient. Was also seeing
589 * crashes in the old code. Move this to the top - am
590 * seeing periodic crashes in this code, and would like to have
591 * as much info available as possible (eg, object name).
592 */
593 for (evt = op->events; evt; evt=evt2) {
594 evt2 = evt->next;
595
596 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
597 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
598 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
599
600 free(evt);
601 }
602 op->events = NULL;
603
604 free_key_values(op);
605
606 /* the memset will clear all these values for us, but we need
607 * to reduce the refcount on them.
608 */
609 if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name);
610 if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl);
611 if (op->title != NULL) FREE_AND_CLEAR_STR(op->title);
612 if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race);
613 if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying);
614 if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill);
615 if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg);
616 if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore);
617 if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname);
618
619
620 memset((void*)op, 0, sizeof (object_special));
621 /* Below here, we clear things that are not done by the memset,
622 * or set default values that are not zero.
623 */
624 /* This is more or less true */
625 SET_FLAG(op, FLAG_REMOVED);
626
627
628 op->contr = NULL; 551 op->contr = NULL;
629 op->below=NULL; 552 op->below = NULL;
630 op->above=NULL; 553 op->above = NULL;
631 op->inv=NULL; 554 op->inv = NULL;
632 op->events=NULL;
633 op->container=NULL; 555 op->container=NULL;
634 op->env=NULL; 556 op->env=NULL;
635 op->more=NULL; 557 op->more=NULL;
636 op->head=NULL; 558 op->head=NULL;
637 op->map=NULL; 559 op->map=NULL;
638 op->refcount=0; 560 op->refcount=0;
639 op->active_next = NULL; 561 op->active_next = NULL;
640 op->active_prev = NULL; 562 op->active_prev = NULL;
641 /* What is not cleared is next, prev, and count */ 563 /* What is not cleared is next, prev, and count */
642 564
643 op->expmul=1.0; 565 op->expmul = 1.0;
644 op->face = blank_face; 566 op->face = blank_face;
645 op->attacked_by_count = (tag_t) -1; 567 op->attacked_by_count = -1;
568
646 if (settings.casting_time) 569 if (settings.casting_time)
647 op->casting_time = -1; 570 op->casting_time = -1;
648
649} 571}
650 572
651/* 573/*
652 * copy object first frees everything allocated by the second object, 574 * copy object first frees everything allocated by the second object,
653 * and then copies the contends of the first object into the second 575 * and then copies the contends of the first object into the second
655 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 577 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
656 * if the first object is freed, the pointers in the new object 578 * if the first object is freed, the pointers in the new object
657 * will point at garbage. 579 * will point at garbage.
658 */ 580 */
659 581
660void copy_object(object *op2, object *op) { 582void copy_object (object *op2, object *op)
661 int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); 583{
662 event *evt, *evt2, *evt_new; 584 bool is_freed = QUERY_FLAG (op, FLAG_FREED);
585 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED);
663 586
664 op->clear ();
665
666 /* Decrement the refcounts, but don't bother zeroing the fields;
667 they'll be overwritten by memcpy. */
668 if(op->name!=NULL) free_string(op->name);
669 if(op->name_pl!=NULL) free_string(op->name_pl);
670 if(op->title!=NULL) free_string(op->title);
671 if(op->race!=NULL) free_string(op->race);
672 if(op->slaying!=NULL) free_string(op->slaying);
673 if(op->skill!=NULL) free_string(op->skill);
674 if(op->msg!=NULL) free_string(op->msg);
675 if(op->lore!=NULL) free_string(op->lore);
676 if(op->materialname != NULL) free_string(op->materialname);
677 if(op->custom_name != NULL) free_string(op->custom_name);
678
679 /* Basically, same code as from clear_object() */
680 for (evt = op->events; evt; evt=evt2) {
681 evt2 = evt->next;
682
683 if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
684 if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
685 if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
686
687 free(evt);
688 }
689 op->events = NULL;
690
691 free_key_values(op);
692
693 *(object_special *)op = *(object_special *)op2;
694 op2->clone (op); 587 op2->clone (op);
695 588
696 if(is_freed) SET_FLAG(op,FLAG_FREED); 589 if (is_freed) SET_FLAG (op, FLAG_FREED);
697 if(is_removed) SET_FLAG(op,FLAG_REMOVED); 590 if (is_removed) SET_FLAG (op, FLAG_REMOVED);
698 if(op->name!=NULL) add_refcount(op->name);
699 if(op->name_pl!=NULL) add_refcount(op->name_pl);
700 if(op->title!=NULL) add_refcount(op->title);
701 if(op->race!=NULL) add_refcount(op->race);
702 if(op->slaying!=NULL) add_refcount(op->slaying);
703 if(op->skill!=NULL) add_refcount(op->skill);
704 if(op->lore!=NULL) add_refcount(op->lore);
705 if(op->msg!=NULL) add_refcount(op->msg);
706 if(op->custom_name!=NULL) add_refcount(op->custom_name);
707 if (op->materialname != NULL) add_refcount(op->materialname);
708 591
709 if((op2->speed<0) && !editor) 592 if (op2->speed < 0)
710 op->speed_left=op2->speed_left-RANDOM()%200/100.0; 593 op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0;
711 594
712 /* Copy over event information */
713 evt2 = NULL;
714 for (evt = op2->events; evt; evt=evt->next) {
715 evt_new = (event *) malloc(sizeof(event));
716 memcpy(evt_new, evt, sizeof(event));
717 if (evt_new->hook) add_refcount(evt_new->hook);
718 if (evt_new->plugin) add_refcount(evt_new->plugin);
719 if (evt_new->options) add_refcount(evt_new->options);
720 evt_new->next = NULL;
721
722 /* Try to be a little clever here, and store away the
723 * last event we copied, so that its simpler to update the
724 * pointer.
725 */
726 if (evt2)
727 evt2->next = evt_new;
728 else
729 op->events = evt_new;
730
731 evt2 = evt_new;
732 }
733 /* Copy over key_values, if any. */ 595 /* Copy over key_values, if any. */
734 if (op2->key_values != NULL) { 596 if (op2->key_values != NULL)
597 {
735 key_value * tail = NULL; 598 key_value *tail = NULL;
736 key_value * i; 599 key_value *i;
737 600
738 op->key_values = NULL; 601 op->key_values = NULL;
739 602
740 for (i = op2->key_values; i != NULL; i = i->next) { 603 for (i = op2->key_values; i != NULL; i = i->next)
741 key_value * new_link = (key_value *) malloc(sizeof(key_value)); 604 {
605 key_value *new_link = new key_value;
742 606
743 new_link->next = NULL; 607 new_link->next = NULL;
744 new_link->key = add_refcount(i->key); 608 new_link->key = i->key;
745 if (i->value)
746 new_link->value = add_refcount(i->value); 609 new_link->value = i->value;
747 else
748 new_link->value = NULL;
749 610
750 /* Try and be clever here, too. */ 611 /* Try and be clever here, too. */
751 if (op->key_values == NULL) { 612 if (op->key_values == NULL)
613 {
752 op->key_values = new_link; 614 op->key_values = new_link;
753 tail = new_link; 615 tail = new_link;
616 }
754 } else { 617 else
618 {
755 tail->next = new_link; 619 tail->next = new_link;
756 tail = new_link; 620 tail = new_link;
757 } 621 }
758 } 622 }
759 } 623 }
760 624
761 update_ob_speed(op); 625 update_ob_speed (op);
762}
763
764/*
765 * expand_objects() allocates more objects for the list of unused objects.
766 * It is called from get_object() if the unused list is empty.
767 */
768
769void expand_objects(void) {
770 int i;
771 object *obj;
772 obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object));
773
774 if(obj==NULL)
775 fatal(OUT_OF_MEMORY);
776 free_objects=obj;
777 obj[0].prev=NULL;
778 obj[0].next= &obj[1],
779 SET_FLAG(&(obj[0]), FLAG_REMOVED);
780 SET_FLAG(&(obj[0]), FLAG_FREED);
781
782 for(i=1;i<OBJ_EXPAND-1;i++) {
783 obj[i].next= &obj[i+1],
784 obj[i].prev= &obj[i-1],
785 SET_FLAG(&(obj[i]), FLAG_REMOVED);
786 SET_FLAG(&(obj[i]), FLAG_FREED);
787 }
788 obj[OBJ_EXPAND-1].prev= &obj[OBJ_EXPAND-2],
789 obj[OBJ_EXPAND-1].next=NULL,
790 SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_REMOVED);
791 SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_FREED);
792
793 nrofallocobjects += OBJ_EXPAND;
794 nroffreeobjects += OBJ_EXPAND;
795} 626}
796 627
797/* 628/*
798 * get_object() grabs an object from the list of unused objects, makes 629 * get_object() grabs an object from the list of unused objects, makes
799 * sure it is initialised, and returns it. 630 * sure it is initialised, and returns it.
800 * If there are no free objects, expand_objects() is called to get more. 631 * If there are no free objects, expand_objects() is called to get more.
801 */ 632 */
802 633
803object *get_object(void) { 634object *get_object ()
635{
636 object *op = new object;
637
638 op->count = ++ob_count;
639
640 op->active_next = 0;
641 op->active_prev = 0;
642
643 op->next = objects;
644 op->prev = 0;
645
646 if (objects)
647 objects->prev = op;
648
804 object *op; 649 objects = op;
805 650
806 if(free_objects==NULL) {
807 expand_objects();
808 }
809 op=free_objects;
810#ifdef MEMORY_DEBUG
811 /* The idea is hopefully by doing a realloc, the memory
812 * debugging program will now use the current stack trace to
813 * report leaks.
814 */
815 op = realloc(op, sizeof(object));
816 SET_FLAG(op, FLAG_REMOVED); 651 SET_FLAG (op, FLAG_REMOVED);
817 SET_FLAG(op, FLAG_FREED);
818#endif
819 652
820 if(!QUERY_FLAG(op,FLAG_FREED)) { 653 op->expmul = 1.0;
821 LOG(llevError,"Fatal: Getting busy object.\n"); 654 op->face = blank_face;
822 } 655 op->attacked_by_count = -1;
823 free_objects=op->next; 656
824 if(free_objects!=NULL)
825 free_objects->prev=NULL;
826 op->count= ++ob_count;
827 op->name=NULL;
828 op->name_pl=NULL;
829 op->title=NULL;
830 op->race=NULL;
831 op->slaying=NULL;
832 op->skill = NULL;
833 op->lore=NULL;
834 op->msg=NULL;
835 op->materialname=NULL;
836 op->next=objects;
837 op->prev=NULL;
838 op->active_next = NULL;
839 op->active_prev = NULL;
840 if(objects!=NULL)
841 objects->prev=op;
842 objects=op;
843 clear_object(op);
844 SET_FLAG(op,FLAG_REMOVED);
845 nroffreeobjects--;
846 return op; 657 return op;
847} 658}
848 659
849/* 660/*
850 * If an object with the IS_TURNABLE() flag needs to be turned due 661 * If an object with the IS_TURNABLE() flag needs to be turned due
871 /* No reason putting the archetypes objects on the speed list, 682 /* No reason putting the archetypes objects on the speed list,
872 * since they never really need to be updated. 683 * since they never really need to be updated.
873 */ 684 */
874 685
875 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { 686 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
876 LOG(llevError,"Object %s is freed but has speed.\n", op->name); 687 LOG(llevError,"Object %s is freed but has speed.\n", &op->name);
877#ifdef MANY_CORES 688#ifdef MANY_CORES
878 abort(); 689 abort();
879#else 690#else
880 op->speed = 0; 691 op->speed = 0;
881#endif 692#endif
965 * UP_OBJ_FACE: only the objects face has changed. 776 * UP_OBJ_FACE: only the objects face has changed.
966 */ 777 */
967 778
968void update_object(object *op, int action) { 779void update_object(object *op, int action) {
969 int update_now=0, flags; 780 int update_now=0, flags;
970 MoveType move_on, move_off, move_block, move_slow; 781 MoveType move_on, move_off, move_block, move_slow;
971 782
972 if (op == NULL) { 783 if (op == NULL) {
973 /* this should never happen */ 784 /* this should never happen */
974 LOG(llevDebug,"update_object() called for NULL object.\n"); 785 LOG(llevDebug,"update_object() called for NULL object.\n");
975 return; 786 return;
1058 * 869 *
1059 * If free_inventory is set, free inventory as well. Else drop items in 870 * If free_inventory is set, free inventory as well. Else drop items in
1060 * inventory to the ground. 871 * inventory to the ground.
1061 */ 872 */
1062 873
874void
1063void free_object(object *ob) { 875free_object (object * ob)
876{
1064 free_object2(ob, 0); 877 free_object2 (ob, 0);
1065} 878}
879
880void
1066void free_object2(object *ob, int free_inventory) { 881free_object2 (object * ob, int free_inventory)
882{
1067 object *tmp,*op; 883 object *tmp, *op;
1068 884
1069 ob->clear ();
1070
1071 if (!QUERY_FLAG(ob,FLAG_REMOVED)) { 885 if (!QUERY_FLAG (ob, FLAG_REMOVED))
886 {
1072 LOG(llevDebug,"Free object called with non removed object\n"); 887 LOG (llevDebug, "Free object called with non removed object\n");
1073 dump_object(ob); 888 dump_object (ob);
1074#ifdef MANY_CORES 889#ifdef MANY_CORES
1075 abort(); 890 abort ();
1076#endif 891#endif
1077 } 892 }
893
1078 if(QUERY_FLAG(ob,FLAG_FRIENDLY)) { 894 if (QUERY_FLAG (ob, FLAG_FRIENDLY))
895 {
1079 LOG(llevMonster,"Warning: tried to free friendly object.\n"); 896 LOG (llevMonster, "Warning: tried to free friendly object.\n");
1080 remove_friendly_object(ob); 897 remove_friendly_object (ob);
1081 } 898 }
899
1082 if(QUERY_FLAG(ob,FLAG_FREED)) { 900 if (QUERY_FLAG (ob, FLAG_FREED))
901 {
1083 dump_object(ob); 902 dump_object (ob);
1084 LOG(llevError,"Trying to free freed object.\n%s\n",errmsg); 903 LOG (llevError, "Trying to free freed object.\n%s\n", errmsg);
1085 return; 904 return;
1086 } 905 }
906
1087 if(ob->more!=NULL) { 907 if (ob->more != NULL)
908 {
1088 free_object2(ob->more, free_inventory); 909 free_object2 (ob->more, free_inventory);
1089 ob->more=NULL; 910 ob->more = NULL;
1090 } 911 }
912
1091 if (ob->inv) { 913 if (ob->inv)
914 {
1092 /* Only if the space blocks everything do we not process - 915 /* Only if the space blocks everything do we not process -
1093 * if some form of movemnt is allowed, let objects 916 * if some form of movemnt is allowed, let objects
1094 * drop on that space. 917 * drop on that space.
1095 */ 918 */
1096 if (free_inventory || ob->map==NULL || ob->map->in_memory!=MAP_IN_MEMORY || 919 if (free_inventory || ob->map == NULL
920 || ob->map->in_memory != MAP_IN_MEMORY
1097 (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) 921 || (GET_MAP_MOVE_BLOCK (ob->map, ob->x, ob->y) == MOVE_ALL))
1098 { 922 {
1099 op=ob->inv; 923 op = ob->inv;
1100 while(op!=NULL) { 924
925 while (op != NULL)
926 {
1101 tmp=op->below; 927 tmp = op->below;
1102 remove_ob(op); 928 remove_ob (op);
1103 free_object2(op, free_inventory); 929 free_object2 (op, free_inventory);
1104 op=tmp; 930 op = tmp;
1105 }
1106 }
1107 else { /* Put objects in inventory onto this space */
1108 op=ob->inv;
1109 while(op!=NULL) {
1110 tmp=op->below;
1111 remove_ob(op);
1112 if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) ||
1113 op->type==RUNE || op->type==TRAP || QUERY_FLAG(op,FLAG_IS_A_TEMPLATE))
1114 free_object(op);
1115 else {
1116 op->x=ob->x;
1117 op->y=ob->y;
1118 insert_ob_in_map(op,ob->map,NULL,0); /* Insert in same map as the envir */
1119 }
1120 op=tmp;
1121 }
1122 }
1123 } 931 }
932 }
933 else
934 { /* Put objects in inventory onto this space */
935 op = ob->inv;
936
937 while (op != NULL)
938 {
939 tmp = op->below;
940 remove_ob (op);
941
942 if (QUERY_FLAG (op, FLAG_STARTEQUIP)
943 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE
944 || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE))
945 free_object (op);
946 else
947 {
948 op->x = ob->x;
949 op->y = ob->y;
950 insert_ob_in_map (op, ob->map, NULL, 0); /* Insert in same map as the envir */
951 }
952
953 op = tmp;
954 }
955 }
956 }
957
1124 /* Remove object from the active list */ 958 /* Remove object from the active list */
1125 ob->speed = 0; 959 ob->speed = 0;
1126 update_ob_speed(ob); 960 update_ob_speed (ob);
1127 961
1128 SET_FLAG(ob, FLAG_FREED); 962 SET_FLAG (ob, FLAG_FREED);
1129 ob->count = 0; 963 ob->count = 0;
1130 964
1131 /* Remove this object from the list of used objects */ 965 /* Remove this object from the list of used objects */
1132 if(ob->prev==NULL) { 966 if (ob->prev == NULL)
967 {
1133 objects=ob->next; 968 objects = ob->next;
969
1134 if(objects!=NULL) 970 if (objects != NULL)
1135 objects->prev=NULL; 971 objects->prev = NULL;
972 }
973 else
1136 } 974 {
1137 else {
1138 ob->prev->next=ob->next; 975 ob->prev->next = ob->next;
976
1139 if(ob->next!=NULL) 977 if (ob->next != NULL)
1140 ob->next->prev=ob->prev; 978 ob->next->prev = ob->prev;
1141 }
1142
1143 if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name);
1144 if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1145 if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title);
1146 if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race);
1147 if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying);
1148 if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill);
1149 if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore);
1150 if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg);
1151 if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname);
1152
1153 979 }
1154 /* Why aren't events freed? */ 980
1155 free_key_values(ob); 981 free_key_values (ob);
1156 982
1157#if 0 /* MEMORY_DEBUG*/
1158 /* This is a nice idea. Unfortunately, a lot of the code in crossfire
1159 * presumes the freed_object will stick around for at least a little
1160 * bit
1161 */
1162 /* this is necessary so that memory debugging programs will
1163 * be able to accurately report source of malloc. If we recycle
1164 * objects, then some other area may be doing the get_object
1165 * and not freeing it, but the original one that malloc'd the
1166 * object will get the blame.
1167 */
1168 free(ob);
1169#else
1170
1171 /* Now link it with the free_objects list: */ 983 /* Now link it with the free_objects list: */
1172 ob->prev=NULL; 984 ob->prev = 0;
1173 ob->next=free_objects; 985 ob->next = 0;
1174 if(free_objects!=NULL)
1175 free_objects->prev=ob;
1176 free_objects=ob;
1177 nroffreeobjects++;
1178#endif
1179}
1180 986
1181/* 987 delete ob;
1182 * count_free() returns the number of objects on the list of free objects.
1183 */
1184
1185int count_free(void) {
1186 int i=0;
1187 object *tmp=free_objects;
1188 while(tmp!=NULL)
1189 tmp=tmp->next, i++;
1190 return i;
1191}
1192
1193/*
1194 * count_used() returns the number of objects on the list of used objects.
1195 */
1196
1197int count_used(void) {
1198 int i=0;
1199 object *tmp=objects;
1200 while(tmp!=NULL)
1201 tmp=tmp->next, i++;
1202 return i;
1203}
1204
1205/*
1206 * count_active() returns the number of objects on the list of active objects.
1207 */
1208
1209int count_active(void) {
1210 int i=0;
1211 object *tmp=active_objects;
1212 while(tmp!=NULL)
1213 tmp=tmp->active_next, i++;
1214 return i;
1215} 988}
1216 989
1217/* 990/*
1218 * sub_weight() recursively (outwards) subtracts a number from the 991 * sub_weight() recursively (outwards) subtracts a number from the
1219 * weight of an object (and what is carried by it's environment(s)). 992 * weight of an object (and what is carried by it's environment(s)).
1297 /* we set up values so that it could be inserted into 1070 /* we set up values so that it could be inserted into
1298 * the map, but we don't actually do that - it is up 1071 * the map, but we don't actually do that - it is up
1299 * to the caller to decide what we want to do. 1072 * to the caller to decide what we want to do.
1300 */ 1073 */
1301 op->x=op->env->x,op->y=op->env->y; 1074 op->x=op->env->x,op->y=op->env->y;
1302 op->ox=op->x,op->oy=op->y;
1303 op->map=op->env->map; 1075 op->map=op->env->map;
1304 op->above=NULL,op->below=NULL; 1076 op->above=NULL,op->below=NULL;
1305 op->env=NULL; 1077 op->env=NULL;
1306 return; 1078 return;
1307 } 1079 }
1382 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) { 1154 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1383 1155
1384 move_apply(tmp, op, NULL); 1156 move_apply(tmp, op, NULL);
1385 if (was_destroyed (op, tag)) { 1157 if (was_destroyed (op, tag)) {
1386 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " 1158 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1387 "leaving object\n", tmp->name, tmp->arch->name); 1159 "leaving object\n", &tmp->name, &tmp->arch->name);
1388 } 1160 }
1389 } 1161 }
1390 1162
1391 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1163 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1392 1164
1518 * we set the map, that doesn't work if the location is within 1290 * we set the map, that doesn't work if the location is within
1519 * a map and this is straddling an edge. So only if coordinate 1291 * a map and this is straddling an edge. So only if coordinate
1520 * is clear wrong do we normalize it. 1292 * is clear wrong do we normalize it.
1521 */ 1293 */
1522 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { 1294 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) {
1523 /* Debugging information so you can see the last coordinates this object had */
1524 more->ox = more->x;
1525 more->oy = more->y;
1526 more->map = get_map_from_coord(m, &more->x, &more->y); 1295 more->map = get_map_from_coord(m, &more->x, &more->y);
1527 } else if (!more->map) { 1296 } else if (!more->map) {
1528 /* For backwards compatibility - when not dealing with tiled maps, 1297 /* For backwards compatibility - when not dealing with tiled maps,
1529 * more->map should always point to the parent. 1298 * more->map should always point to the parent.
1530 */ 1299 */
1536 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n"); 1305 LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n");
1537 return NULL; 1306 return NULL;
1538 } 1307 }
1539 } 1308 }
1540 CLEAR_FLAG(op,FLAG_REMOVED); 1309 CLEAR_FLAG(op,FLAG_REMOVED);
1541
1542 /* Debugging information so you can see the last coordinates this object had */
1543 op->ox=op->x;
1544 op->oy=op->y;
1545 1310
1546 /* Ideally, the caller figures this out. However, it complicates a lot 1311 /* Ideally, the caller figures this out. However, it complicates a lot
1547 * of areas of callers (eg, anything that uses find_free_spot would now 1312 * of areas of callers (eg, anything that uses find_free_spot would now
1548 * need extra work 1313 * need extra work
1549 */ 1314 */
1617 * looks like instead of lots of conditions here. 1382 * looks like instead of lots of conditions here.
1618 * makes things faster, and effectively the same result. 1383 * makes things faster, and effectively the same result.
1619 */ 1384 */
1620 1385
1621 /* Have object 'fall below' other objects that block view. 1386 /* Have object 'fall below' other objects that block view.
1622 * Unless those objects are exits, type 66 1387 * Unless those objects are exits, type 66
1623 * If INS_ON_TOP is used, don't do this processing 1388 * If INS_ON_TOP is used, don't do this processing
1624 * Need to find the object that in fact blocks view, otherwise 1389 * Need to find the object that in fact blocks view, otherwise
1625 * stacking is a bit odd. 1390 * stacking is a bit odd.
1626 */ 1391 */
1627 if (!(flag & INS_ON_TOP) && 1392 if (!(flag & INS_ON_TOP) &&
1628 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) && 1393 (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) &&
1629 (op->face && !op->face->visibility)) { 1394 (op->face && !op->face->visibility)) {
1630 for (last=top; last != floor; last=last->below) 1395 for (last=top; last != floor; last=last->below)
1631 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; 1396 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1632 /* Check to see if we found the object that blocks view, 1397 /* Check to see if we found the object that blocks view,
1633 * and make sure we have a below pointer for it so that 1398 * and make sure we have a below pointer for it so that
1634 * we can get inserted below this one, which requires we 1399 * we can get inserted below this one, which requires we
1635 * set top to the object below us. 1400 * set top to the object below us.
1636 */ 1401 */
1665 1430
1666 /* If we have a floor, we know the player, if any, will be above 1431 /* If we have a floor, we know the player, if any, will be above
1667 * it, so save a few ticks and start from there. 1432 * it, so save a few ticks and start from there.
1668 */ 1433 */
1669 if (!(flag & INS_MAP_LOAD)) 1434 if (!(flag & INS_MAP_LOAD))
1670 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { 1435 for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) {
1671 if (tmp->type == PLAYER) 1436 if (tmp->type == PLAYER)
1672 tmp->contr->socket.update_look=1; 1437 tmp->contr->socket.update_look=1;
1673 } 1438 }
1674 1439
1675 /* If this object glows, it may affect lighting conditions that are 1440 /* If this object glows, it may affect lighting conditions that are
1676 * visible to others on this map. But update_all_los is really 1441 * visible to others on this map. But update_all_los is really
1677 * an inefficient way to do this, as it means los for all players 1442 * an inefficient way to do this, as it means los for all players
1678 * on the map will get recalculated. The players could very well 1443 * on the map will get recalculated. The players could very well
1750 object *newob; 1515 object *newob;
1751 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1516 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1752 1517
1753 if(orig_ob->nrof<nr) { 1518 if(orig_ob->nrof<nr) {
1754 sprintf(errmsg,"There are only %d %ss.", 1519 sprintf(errmsg,"There are only %d %ss.",
1755 orig_ob->nrof?orig_ob->nrof:1, orig_ob->name); 1520 orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name);
1756 return NULL; 1521 return NULL;
1757 } 1522 }
1758 newob = object_create_clone(orig_ob); 1523 newob = object_create_clone(orig_ob);
1759 if((orig_ob->nrof-=nr)<1) { 1524 if((orig_ob->nrof-=nr)<1) {
1760 if ( ! is_removed) 1525 if ( ! is_removed)
1907 "Warning: Tried to insert object wrong part of multipart object.\n"); 1672 "Warning: Tried to insert object wrong part of multipart object.\n");
1908 where = where->head; 1673 where = where->head;
1909 } 1674 }
1910 if (op->more) { 1675 if (op->more) {
1911 LOG(llevError, "Tried to insert multipart object %s (%d)\n", 1676 LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1912 op->name, op->count); 1677 &op->name, op->count);
1913 return op; 1678 return op;
1914 } 1679 }
1915 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); 1680 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1916 CLEAR_FLAG(op, FLAG_REMOVED); 1681 CLEAR_FLAG(op, FLAG_REMOVED);
1917 if(op->nrof) { 1682 if(op->nrof) {
1951 op->map=NULL; 1716 op->map=NULL;
1952 op->env=where; 1717 op->env=where;
1953 op->above=NULL; 1718 op->above=NULL;
1954 op->below=NULL; 1719 op->below=NULL;
1955 op->x=0,op->y=0; 1720 op->x=0,op->y=0;
1956 op->ox=0,op->oy=0;
1957 1721
1958 /* reset the light list and los of the players on the map */ 1722 /* reset the light list and los of the players on the map */
1959 if((op->glow_radius!=0)&&where->map) 1723 if((op->glow_radius!=0)&&where->map)
1960 { 1724 {
1961#ifdef DEBUG_LIGHTS 1725#ifdef DEBUG_LIGHTS
2003{ 1767{
2004 object *tmp; 1768 object *tmp;
2005 tag_t tag; 1769 tag_t tag;
2006 mapstruct *m=op->map; 1770 mapstruct *m=op->map;
2007 int x=op->x, y=op->y; 1771 int x=op->x, y=op->y;
2008 MoveType move_on, move_slow, move_block; 1772 MoveType move_on, move_slow, move_block;
2009 1773
2010 if(QUERY_FLAG(op,FLAG_NO_APPLY)) 1774 if(QUERY_FLAG(op,FLAG_NO_APPLY))
2011 return 0; 1775 return 0;
2012 1776
2013 tag = op->count; 1777 tag = op->count;
2331int find_dir(mapstruct *m, int x, int y, object *exclude) { 2095int find_dir(mapstruct *m, int x, int y, object *exclude) {
2332 int i,max=SIZEOFFREE, mflags; 2096 int i,max=SIZEOFFREE, mflags;
2333 sint16 nx, ny; 2097 sint16 nx, ny;
2334 object *tmp; 2098 object *tmp;
2335 mapstruct *mp; 2099 mapstruct *mp;
2336 MoveType blocked, move_type; 2100 MoveType blocked, move_type;
2337 2101
2338 if (exclude && exclude->head) { 2102 if (exclude && exclude->head) {
2339 exclude = exclude->head; 2103 exclude = exclude->head;
2340 move_type = exclude->move_type; 2104 move_type = exclude->move_type;
2341 } else { 2105 } else {
2618/* Also remember that multiparts objects are not supported for now. */ 2382/* Also remember that multiparts objects are not supported for now. */
2619 2383
2620object* load_object_str(const char *obstr) 2384object* load_object_str(const char *obstr)
2621{ 2385{
2622 object *op; 2386 object *op;
2623 FILE *tempfile;
2624 char filename[MAX_BUF]; 2387 char filename[MAX_BUF];
2625 sprintf(filename,"%s/cfloadobstr2044",settings.tmpdir); 2388 sprintf(filename,"%s/cfloadobstr2044",settings.tmpdir);
2389
2626 tempfile=fopen(filename,"w"); 2390 FILE *tempfile=fopen(filename,"w");
2627 if (tempfile == NULL) 2391 if (tempfile == NULL)
2628 { 2392 {
2629 LOG(llevError,"Error - Unable to access load object temp file\n"); 2393 LOG(llevError,"Error - Unable to access load object temp file\n");
2630 return NULL; 2394 return NULL;
2631 }; 2395 };
2632 fprintf(tempfile,obstr); 2396 fprintf(tempfile,obstr);
2633 fclose(tempfile); 2397 fclose(tempfile);
2634 2398
2635 op=get_object(); 2399 op=get_object();
2636 2400
2637 tempfile=fopen(filename,"r");
2638 if (tempfile == NULL)
2639 {
2640 LOG(llevError,"Error - Unable to read object temp file\n");
2641 return NULL;
2642 };
2643 object_thawer thawer (tempfile); 2401 object_thawer thawer (filename);
2402
2403 if (thawer)
2644 load_object(thawer,op,LO_NEWFILE,0); 2404 load_object(thawer,op,0);
2405
2645 LOG(llevDebug," load str completed, object=%s\n",op->name); 2406 LOG(llevDebug," load str completed, object=%s\n", &op->name);
2646 CLEAR_FLAG(op,FLAG_REMOVED); 2407 CLEAR_FLAG(op,FLAG_REMOVED);
2647 fclose(tempfile); 2408
2648 return op; 2409 return op;
2649} 2410}
2650 2411
2651/* This returns the first object in who's inventory that 2412/* This returns the first object in who's inventory that
2652 * has the same type and subtype match. 2413 * has the same type and subtype match.
2689 */ 2450 */
2690const char * get_ob_key_value(const object * op, const char * const key) { 2451const char * get_ob_key_value(const object * op, const char * const key) {
2691 key_value * link; 2452 key_value * link;
2692 const char * canonical_key; 2453 const char * canonical_key;
2693 2454
2694 canonical_key = find_string(key); 2455 canonical_key = shstr::find (key);
2695 2456
2696 if (canonical_key == NULL) { 2457 if (canonical_key == NULL) {
2697 /* 1. There being a field named key on any object 2458 /* 1. There being a field named key on any object
2698 * implies there'd be a shared string to find. 2459 * implies there'd be a shared string to find.
2699 * 2. Since there isn't, no object has this field. 2460 * 2. Since there isn't, no object has this field.
2722 * Unless add_key is TRUE, it won't add fields, only change the value of existing 2483 * Unless add_key is TRUE, it won't add fields, only change the value of existing
2723 * keys. 2484 * keys.
2724 * 2485 *
2725 * Returns TRUE on success. 2486 * Returns TRUE on success.
2726 */ 2487 */
2727int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) { 2488int set_ob_key_value_s(object * op, const shstr &canonical_key, const char * value, int add_key) {
2728 key_value * field = NULL, *last=NULL; 2489 key_value * field = NULL, *last=NULL;
2729 2490
2730 for (field=op->key_values; field != NULL; field=field->next) { 2491 for (field=op->key_values; field != NULL; field=field->next) {
2731 if (field->key != canonical_key) { 2492 if (field->key != canonical_key) {
2732 last = field; 2493 last = field;
2733 continue; 2494 continue;
2734 } 2495 }
2735 2496
2736 if (field->value) FREE_AND_CLEAR_STR(field->value);
2737 if (value) 2497 if (value)
2738 field->value = add_string(value); 2498 field->value = value;
2739 else { 2499 else {
2740 /* Basically, if the archetype has this key set, 2500 /* Basically, if the archetype has this key set,
2741 * we need to store the null value so when we save 2501 * we need to store the null value so when we save
2742 * it, we save the empty value so that when we load, 2502 * it, we save the empty value so that when we load,
2743 * we get this value back again. 2503 * we get this value back again.
2744 */ 2504 */
2745 if (get_ob_key_link(&op->arch->clone, canonical_key)) 2505 if (get_ob_key_link (&op->arch->clone, canonical_key))
2746 field->value = NULL; 2506 field->value = 0;
2747 else { 2507 else
2748 /* Delete this link */ 2508 {
2749 if (field->key) FREE_AND_CLEAR_STR(field->key);
2750 if (field->value) FREE_AND_CLEAR_STR(field->value);
2751 if (last) last->next = field->next; 2509 if (last) last->next = field->next;
2752 else op->key_values = field->next; 2510 else op->key_values = field->next;
2511
2753 free(field); 2512 delete field;
2754 } 2513 }
2755 } 2514 }
2756 return TRUE; 2515 return TRUE;
2757 } 2516 }
2758 /* IF we get here, key doesn't exist */ 2517 /* IF we get here, key doesn't exist */
2759 2518
2768 * be here. If user wants to store empty strings, 2527 * be here. If user wants to store empty strings,
2769 * should pass in "" 2528 * should pass in ""
2770 */ 2529 */
2771 if (value == NULL) return TRUE; 2530 if (value == NULL) return TRUE;
2772 2531
2773 field = (key_value *) malloc(sizeof(key_value)); 2532 field = new key_value;
2774 2533
2775 field->key = add_refcount(canonical_key); 2534 field->key = canonical_key;
2776 field->value = add_string(value); 2535 field->value = value;
2777 /* Usual prepend-addition. */ 2536 /* Usual prepend-addition. */
2778 field->next = op->key_values; 2537 field->next = op->key_values;
2779 op->key_values = field; 2538 op->key_values = field;
2780 2539
2781 return TRUE; 2540 return TRUE;
2788 * and not add new ones. 2547 * and not add new ones.
2789 * In general, should be little reason FALSE is ever passed in for add_key 2548 * In general, should be little reason FALSE is ever passed in for add_key
2790 * 2549 *
2791 * Returns TRUE on success. 2550 * Returns TRUE on success.
2792 */ 2551 */
2793int set_ob_key_value(object * op, const char * key, const char * value, int add_key) { 2552int set_ob_key_value(object * op, const char *key, const char * value, int add_key)
2794 const char * canonical_key = NULL; 2553{
2795 int floating_ref = FALSE; 2554 shstr key_ (key);
2796 int ret;
2797
2798 /* HACK This mess is to make sure set_ob_value() passes a shared string
2799 * to get_ob_key_link(), without leaving a leaked refcount.
2800 */
2801
2802 canonical_key = find_string(key);
2803 if (canonical_key == NULL) {
2804 canonical_key = add_string(key);
2805 floating_ref = TRUE;
2806 }
2807
2808 ret = set_ob_key_value_s(op, canonical_key, value, add_key); 2555 return set_ob_key_value_s (op, key_, value, add_key);
2809
2810 if (floating_ref) {
2811 free_string(canonical_key);
2812 }
2813
2814 return ret;
2815} 2556}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines