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.23 by root, Sun Sep 10 14:54:02 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.23 2006/09/10 14:54:02 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) {
293errmsg[0] = 0;
294return;
295 //TODO//D#d#
296#if 0
286 char *cp; 297 char *cp;
287/* object *tmp;*/ 298/* object *tmp;*/
288 299
289 if(op->arch!=NULL) { 300 if(op->arch!=NULL) {
290 strcat(errmsg,"arch "); 301 strcat(errmsg,"arch ");
315 for (tmp=op->inv; tmp; tmp=tmp->below) 326 for (tmp=op->inv; tmp; tmp=tmp->below)
316 dump_object2(tmp); 327 dump_object2(tmp);
317#endif 328#endif
318 strcat(errmsg,"end\n"); 329 strcat(errmsg,"end\n");
319 } 330 }
331#endif
320} 332}
321 333
322/* 334/*
323 * Dumps an object. Returns output in the static global errmsg array. 335 * Dumps an object. Returns output in the static global errmsg array.
324 */ 336 */
329 return; 341 return;
330 } 342 }
331 errmsg[0]='\0'; 343 errmsg[0]='\0';
332 dump_object2(op); 344 dump_object2(op);
333} 345}
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 346
375void dump_all_objects(void) { 347void dump_all_objects(void) {
376 object *op; 348 object *op;
377 for(op=objects;op!=NULL;op=op->next) { 349 for(op=objects;op!=NULL;op=op->next) {
378 dump_object(op); 350 dump_object(op);
414 * Used only by the patch command, but not all that useful. 386 * Used only by the patch command, but not all that useful.
415 * Enables features like "patch <name-of-other-player> food 999" 387 * Enables features like "patch <name-of-other-player> food 999"
416 */ 388 */
417 389
418object *find_object_name(const char *str) { 390object *find_object_name(const char *str) {
419 const char *name=add_string(str); 391 const char *name = shstr::find (str);
420 object *op; 392 object *op;
421 for(op=objects;op!=NULL;op=op->next) 393 for(op=objects;op!=NULL;op=op->next)
422 if(op->name==name) 394 if(&op->name == name)
423 break; 395 break;
424 free_string(name); 396
425 return op; 397 return op;
426} 398}
427 399
428void free_all_object_data(void) { 400void free_all_object_data ()
429#ifdef MEMORY_DEBUG 401{
430 object *op, *next; 402 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} 403}
443 404
444/* 405/*
445 * Returns the object which this object marks as being the owner. 406 * 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 407 * A id-scheme is used to avoid pointing to objects which have been
473 op->owner->refcount--; 434 op->owner->refcount--;
474 435
475 op->owner = NULL; 436 op->owner = NULL;
476 op->ownercount = 0; 437 op->ownercount = 0;
477} 438}
478
479
480 439
481/* 440/*
482 * 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
483 * skill and experience objects. 442 * skill and experience objects.
484 */ 443 */
531 } 490 }
532 set_owner(op, owner); 491 set_owner(op, owner);
533 492
534} 493}
535 494
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 495/* Zero the key_values on op, decrementing the shared-string
555 * refcounts and freeing the links. 496 * refcounts and freeing the links.
556 */ 497 */
557static void free_key_values(object * op) { 498static void free_key_values(object * op)
558 key_value * i; 499{
559 key_value * next = NULL; 500 for (key_value *i = op->key_values; i != 0; )
560 501 {
561 if (op->key_values == NULL) return; 502 key_value *next = i->next;
503 delete i;
504 i = next;
562 505 }
563 for (i = op->key_values; i != NULL; i = next) { 506
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; 507 op->key_values = 0;
574} 508}
575 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 = *this;
535 *(object_pod *)destination = *this;
536
537 if (self || cb)
538 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
539}
576 540
577/* 541/*
578 * clear_object() frees everything allocated by an object, and also 542 * clear_object() frees everything allocated by an object, and also
579 * clears all variables and flags to default settings. 543 * clears all variables and flags to default settings.
580 */ 544 */
581 545
582void clear_object(object *op) { 546void clear_object (object *op)
547{
583 op->clear (); 548 op->clear ();
584 549
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; 550 op->contr = NULL;
629 op->below=NULL; 551 op->below = NULL;
630 op->above=NULL; 552 op->above = NULL;
631 op->inv=NULL; 553 op->inv = NULL;
632 op->events=NULL;
633 op->container=NULL; 554 op->container=NULL;
634 op->env=NULL; 555 op->env=NULL;
635 op->more=NULL; 556 op->more=NULL;
636 op->head=NULL; 557 op->head=NULL;
637 op->map=NULL; 558 op->map=NULL;
638 op->refcount=0; 559 op->refcount=0;
639 op->active_next = NULL; 560 op->active_next = NULL;
640 op->active_prev = NULL; 561 op->active_prev = NULL;
641 /* What is not cleared is next, prev, and count */ 562 /* What is not cleared is next, prev, and count */
642 563
643 op->expmul=1.0; 564 op->expmul = 1.0;
644 op->face = blank_face; 565 op->face = blank_face;
645 op->attacked_by_count = (tag_t) -1; 566 op->attacked_by_count = -1;
567
646 if (settings.casting_time) 568 if (settings.casting_time)
647 op->casting_time = -1; 569 op->casting_time = -1;
648
649} 570}
650 571
651/* 572/*
652 * copy object first frees everything allocated by the second object, 573 * copy object first frees everything allocated by the second object,
653 * 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
655 * 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,
656 * 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
657 * will point at garbage. 578 * will point at garbage.
658 */ 579 */
659 580
660void copy_object(object *op2, object *op) { 581void copy_object (object *op2, object *op)
661 int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); 582{
662 event *evt, *evt2, *evt_new; 583 bool is_freed = QUERY_FLAG (op, FLAG_FREED);
584 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED);
663 585
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); 586 op2->clone (op);
695 587
696 if(is_freed) SET_FLAG(op,FLAG_FREED); 588 if (is_freed) SET_FLAG (op, FLAG_FREED);
697 if(is_removed) SET_FLAG(op,FLAG_REMOVED); 589 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 590
709 if((op2->speed<0) && !editor) 591 if (op2->speed < 0)
710 op->speed_left=op2->speed_left-RANDOM()%200/100.0; 592 op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0;
711 593
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. */ 594 /* Copy over key_values, if any. */
734 if (op2->key_values != NULL) { 595 if (op2->key_values)
596 {
735 key_value * tail = NULL; 597 key_value *tail = 0;
736 key_value * i; 598 key_value *i;
737 599
738 op->key_values = NULL; 600 op->key_values = 0;
739 601
740 for (i = op2->key_values; i != NULL; i = i->next) { 602 for (i = op2->key_values; i; i = i->next)
741 key_value * new_link = (key_value *) malloc(sizeof(key_value)); 603 {
604 key_value *new_link = new key_value;
742 605
743 new_link->next = NULL; 606 new_link->next = 0;
744 new_link->key = add_refcount(i->key); 607 new_link->key = i->key;
745 if (i->value)
746 new_link->value = add_refcount(i->value); 608 new_link->value = i->value;
747 else
748 new_link->value = NULL;
749 609
750 /* Try and be clever here, too. */ 610 /* Try and be clever here, too. */
751 if (op->key_values == NULL) { 611 if (!op->key_values)
612 {
752 op->key_values = new_link; 613 op->key_values = new_link;
753 tail = new_link; 614 tail = new_link;
615 }
754 } else { 616 else
617 {
755 tail->next = new_link; 618 tail->next = new_link;
756 tail = new_link; 619 tail = new_link;
757 } 620 }
758 } 621 }
759 } 622 }
760 623
761 update_ob_speed(op); 624 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}
796
797/*
798 * get_object() grabs an object from the list of unused objects, makes
799 * sure it is initialised, and returns it.
800 * If there are no free objects, expand_objects() is called to get more.
801 */
802
803object *get_object(void) {
804 object *op;
805
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);
817 SET_FLAG(op, FLAG_FREED);
818#endif
819
820 if(!QUERY_FLAG(op,FLAG_FREED)) {
821 LOG(llevError,"Fatal: Getting busy object.\n");
822 }
823 free_objects=op->next;
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;
847} 625}
848 626
849/* 627/*
850 * 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
851 * 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
871 /* No reason putting the archetypes objects on the speed list, 649 /* No reason putting the archetypes objects on the speed list,
872 * since they never really need to be updated. 650 * since they never really need to be updated.
873 */ 651 */
874 652
875 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { 653 if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
876 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);
877#ifdef MANY_CORES 655#ifdef MANY_CORES
878 abort(); 656 abort();
879#else 657#else
880 op->speed = 0; 658 op->speed = 0;
881#endif 659#endif
965 * UP_OBJ_FACE: only the objects face has changed. 743 * UP_OBJ_FACE: only the objects face has changed.
966 */ 744 */
967 745
968void update_object(object *op, int action) { 746void update_object(object *op, int action) {
969 int update_now=0, flags; 747 int update_now=0, flags;
970 MoveType move_on, move_off, move_block, move_slow; 748 MoveType move_on, move_off, move_block, move_slow;
971 749
972 if (op == NULL) { 750 if (op == NULL) {
973 /* this should never happen */ 751 /* this should never happen */
974 LOG(llevDebug,"update_object() called for NULL object.\n"); 752 LOG(llevDebug,"update_object() called for NULL object.\n");
975 return; 753 return;
1015 update_now=1; 793 update_now=1;
1016 794
1017 if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE)) 795 if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE))
1018 update_now=1; 796 update_now=1;
1019 797
798 if (op->type == SAFE_GROUND && !(flags & P_SAFE))
799 update_now=1;
800
1020 if ((move_on | op->move_on) != move_on) update_now=1; 801 if ((move_on | op->move_on) != move_on) update_now=1;
802
1021 if ((move_off | op->move_off) != move_off) update_now=1; 803 if ((move_off | op->move_off) != move_off) update_now=1;
804
1022 /* 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
1023 * to have move_allow right now. 806 * to have move_allow right now.
1024 */ 807 */
1025 if (((move_block | op->move_block) & ~op->move_allow) != move_block) 808 if (((move_block | op->move_block) & ~op->move_allow) != move_block)
1026 update_now=1; 809 update_now=1;
810
1027 if ((move_slow | op->move_slow) != move_slow) update_now=1; 811 if ((move_slow | op->move_slow) != move_slow)
812 update_now=1;
1028 } 813 }
1029 /* if the object is being removed, we can't make intelligent 814 /* if the object is being removed, we can't make intelligent
1030 * decisions, because remove_ob can't really pass the object 815 * decisions, because remove_ob can't really pass the object
1031 * that is being removed. 816 * that is being removed.
1032 */ 817 */
1046 831
1047 if(op->more!=NULL) 832 if(op->more!=NULL)
1048 update_object(op->more, action); 833 update_object(op->more, action);
1049} 834}
1050 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}
1051 891
1052/* 892/*
1053 * free_object() frees everything allocated by an object, removes 893 * free_object() frees everything allocated by an object, removes
1054 * 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
1055 * free objects. The IS_FREED() flag is set in the object. 895 * free objects. The IS_FREED() flag is set in the object.
1057 * this function to succeed. 897 * this function to succeed.
1058 * 898 *
1059 * 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
1060 * inventory to the ground. 900 * inventory to the ground.
1061 */ 901 */
1062 902void
1063void free_object(object *ob) { 903object::free (bool free_inventory)
1064 free_object2(ob, 0); 904{
1065}
1066void free_object2(object *ob, int free_inventory) {
1067 object *tmp,*op;
1068
1069 ob->clear ();
1070
1071 if (!QUERY_FLAG(ob,FLAG_REMOVED)) { 905 if (!QUERY_FLAG (this, FLAG_REMOVED))
906 {
1072 LOG(llevDebug,"Free object called with non removed object\n"); 907 LOG (llevDebug, "Free object called with non removed object\n");
1073 dump_object(ob); 908 dump_object (this);
1074#ifdef MANY_CORES 909#ifdef MANY_CORES
1075 abort(); 910 abort ();
1076#endif 911#endif
1077 } 912 }
913
1078 if(QUERY_FLAG(ob,FLAG_FRIENDLY)) { 914 if (QUERY_FLAG (this, FLAG_FRIENDLY))
915 {
1079 LOG(llevMonster,"Warning: tried to free friendly object.\n"); 916 LOG (llevMonster, "Warning: tried to free friendly object.\n");
1080 remove_friendly_object(ob); 917 remove_friendly_object (this);
1081 } 918 }
919
1082 if(QUERY_FLAG(ob,FLAG_FREED)) { 920 if (QUERY_FLAG (this, FLAG_FREED))
921 {
1083 dump_object(ob); 922 dump_object (this);
1084 LOG(llevError,"Trying to free freed object.\n%s\n",errmsg); 923 LOG (llevError, "Trying to free freed object.\n%s\n", errmsg);
1085 return; 924 return;
925 }
926
927 if (more)
1086 } 928 {
1087 if(ob->more!=NULL) { 929 more->free (free_inventory);
1088 free_object2(ob->more, free_inventory); 930 more = 0;
1089 ob->more=NULL; 931 }
932
933 if (inv)
1090 } 934 {
1091 if (ob->inv) {
1092 /* Only if the space blocks everything do we not process - 935 /* Only if the space blocks everything do we not process -
1093 * if some form of movemnt is allowed, let objects 936 * if some form of movement is allowed, let objects
1094 * drop on that space. 937 * drop on that space.
1095 */ 938 */
1096 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
1097 (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) 941 || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL))
1098 { 942 {
1099 op=ob->inv; 943 object *op = inv;
1100 while(op!=NULL) { 944
1101 tmp=op->below; 945 while (op)
946 {
947 object *tmp = op->below;
1102 remove_ob(op); 948 remove_ob (op);
1103 free_object2(op, free_inventory); 949 op->free (free_inventory);
1104 op=tmp; 950 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 } 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
1124 /* Remove object from the active list */ 978 /* Remove object from the active list */
1125 ob->speed = 0; 979 speed = 0;
1126 update_ob_speed(ob); 980 update_ob_speed (this);
1127 981
982 unlink ();
983
1128 SET_FLAG(ob, FLAG_FREED); 984 SET_FLAG (this, FLAG_FREED);
1129 ob->count = 0;
1130 985
1131 /* Remove this object from the list of used objects */ 986 mortals.push_back (this);
1132 if(ob->prev==NULL) {
1133 objects=ob->next;
1134 if(objects!=NULL)
1135 objects->prev=NULL;
1136 }
1137 else {
1138 ob->prev->next=ob->next;
1139 if(ob->next!=NULL)
1140 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
1154 /* Why aren't events freed? */
1155 free_key_values(ob);
1156
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: */
1172 ob->prev=NULL;
1173 ob->next=free_objects;
1174 if(free_objects!=NULL)
1175 free_objects->prev=ob;
1176 free_objects=ob;
1177 nroffreeobjects++;
1178#endif
1179}
1180
1181/*
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} 987}
1216 988
1217/* 989/*
1218 * sub_weight() recursively (outwards) subtracts a number from the 990 * sub_weight() recursively (outwards) subtracts a number from the
1219 * 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)).
1297 /* we set up values so that it could be inserted into 1069 /* we set up values so that it could be inserted into
1298 * 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
1299 * to the caller to decide what we want to do. 1071 * to the caller to decide what we want to do.
1300 */ 1072 */
1301 op->x=op->env->x,op->y=op->env->y; 1073 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; 1074 op->map=op->env->map;
1304 op->above=NULL,op->below=NULL; 1075 op->above=NULL,op->below=NULL;
1305 op->env=NULL; 1076 op->env=NULL;
1306 return; 1077 return;
1307 } 1078 }
1382 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) { 1153 (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1383 1154
1384 move_apply(tmp, op, NULL); 1155 move_apply(tmp, op, NULL);
1385 if (was_destroyed (op, tag)) { 1156 if (was_destroyed (op, tag)) {
1386 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " 1157 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1387 "leaving object\n", tmp->name, tmp->arch->name); 1158 "leaving object\n", &tmp->name, &tmp->arch->name);
1388 } 1159 }
1389 } 1160 }
1390 1161
1391 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1162 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1392 1163
1518 * 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
1519 * 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
1520 * is clear wrong do we normalize it. 1291 * is clear wrong do we normalize it.
1521 */ 1292 */
1522 if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { 1293 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); 1294 more->map = get_map_from_coord(m, &more->x, &more->y);
1527 } else if (!more->map) { 1295 } else if (!more->map) {
1528 /* For backwards compatibility - when not dealing with tiled maps, 1296 /* For backwards compatibility - when not dealing with tiled maps,
1529 * more->map should always point to the parent. 1297 * more->map should always point to the parent.
1530 */ 1298 */
1536 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");
1537 return NULL; 1305 return NULL;
1538 } 1306 }
1539 } 1307 }
1540 CLEAR_FLAG(op,FLAG_REMOVED); 1308 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 1309
1546 /* Ideally, the caller figures this out. However, it complicates a lot 1310 /* 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 1311 * of areas of callers (eg, anything that uses find_free_spot would now
1548 * need extra work 1312 * need extra work
1549 */ 1313 */
1617 * looks like instead of lots of conditions here. 1381 * looks like instead of lots of conditions here.
1618 * makes things faster, and effectively the same result. 1382 * makes things faster, and effectively the same result.
1619 */ 1383 */
1620 1384
1621 /* Have object 'fall below' other objects that block view. 1385 /* Have object 'fall below' other objects that block view.
1622 * Unless those objects are exits, type 66 1386 * Unless those objects are exits, type 66
1623 * If INS_ON_TOP is used, don't do this processing 1387 * If INS_ON_TOP is used, don't do this processing
1624 * Need to find the object that in fact blocks view, otherwise 1388 * Need to find the object that in fact blocks view, otherwise
1625 * stacking is a bit odd. 1389 * stacking is a bit odd.
1626 */ 1390 */
1627 if (!(flag & INS_ON_TOP) && 1391 if (!(flag & INS_ON_TOP) &&
1628 (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) &&
1629 (op->face && !op->face->visibility)) { 1393 (op->face && !op->face->visibility)) {
1630 for (last=top; last != floor; last=last->below) 1394 for (last=top; last != floor; last=last->below)
1631 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; 1395 if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1632 /* Check to see if we found the object that blocks view, 1396 /* Check to see if we found the object that blocks view,
1633 * 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
1634 * we can get inserted below this one, which requires we 1398 * we can get inserted below this one, which requires we
1635 * set top to the object below us. 1399 * set top to the object below us.
1636 */ 1400 */
1665 1429
1666 /* 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
1667 * it, so save a few ticks and start from there. 1431 * it, so save a few ticks and start from there.
1668 */ 1432 */
1669 if (!(flag & INS_MAP_LOAD)) 1433 if (!(flag & INS_MAP_LOAD))
1670 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) {
1671 if (tmp->type == PLAYER) 1435 if (tmp->type == PLAYER)
1672 tmp->contr->socket.update_look=1; 1436 tmp->contr->socket.update_look=1;
1673 } 1437 }
1674 1438
1675 /* If this object glows, it may affect lighting conditions that are 1439 /* If this object glows, it may affect lighting conditions that are
1676 * 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
1677 * 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
1678 * on the map will get recalculated. The players could very well 1442 * on the map will get recalculated. The players could very well
1750 object *newob; 1514 object *newob;
1751 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0); 1515 int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1752 1516
1753 if(orig_ob->nrof<nr) { 1517 if(orig_ob->nrof<nr) {
1754 sprintf(errmsg,"There are only %d %ss.", 1518 sprintf(errmsg,"There are only %d %ss.",
1755 orig_ob->nrof?orig_ob->nrof:1, orig_ob->name); 1519 orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name);
1756 return NULL; 1520 return NULL;
1757 } 1521 }
1758 newob = object_create_clone(orig_ob); 1522 newob = object_create_clone(orig_ob);
1759 if((orig_ob->nrof-=nr)<1) { 1523 if((orig_ob->nrof-=nr)<1) {
1760 if ( ! is_removed) 1524 if ( ! is_removed)
1907 "Warning: Tried to insert object wrong part of multipart object.\n"); 1671 "Warning: Tried to insert object wrong part of multipart object.\n");
1908 where = where->head; 1672 where = where->head;
1909 } 1673 }
1910 if (op->more) { 1674 if (op->more) {
1911 LOG(llevError, "Tried to insert multipart object %s (%d)\n", 1675 LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1912 op->name, op->count); 1676 &op->name, op->count);
1913 return op; 1677 return op;
1914 } 1678 }
1915 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); 1679 CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1916 CLEAR_FLAG(op, FLAG_REMOVED); 1680 CLEAR_FLAG(op, FLAG_REMOVED);
1917 if(op->nrof) { 1681 if(op->nrof) {
1951 op->map=NULL; 1715 op->map=NULL;
1952 op->env=where; 1716 op->env=where;
1953 op->above=NULL; 1717 op->above=NULL;
1954 op->below=NULL; 1718 op->below=NULL;
1955 op->x=0,op->y=0; 1719 op->x=0,op->y=0;
1956 op->ox=0,op->oy=0;
1957 1720
1958 /* 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 */
1959 if((op->glow_radius!=0)&&where->map) 1722 if((op->glow_radius!=0)&&where->map)
1960 { 1723 {
1961#ifdef DEBUG_LIGHTS 1724#ifdef DEBUG_LIGHTS
2003{ 1766{
2004 object *tmp; 1767 object *tmp;
2005 tag_t tag; 1768 tag_t tag;
2006 mapstruct *m=op->map; 1769 mapstruct *m=op->map;
2007 int x=op->x, y=op->y; 1770 int x=op->x, y=op->y;
2008 MoveType move_on, move_slow, move_block; 1771 MoveType move_on, move_slow, move_block;
2009 1772
2010 if(QUERY_FLAG(op,FLAG_NO_APPLY)) 1773 if(QUERY_FLAG(op,FLAG_NO_APPLY))
2011 return 0; 1774 return 0;
2012 1775
2013 tag = op->count; 1776 tag = op->count;
2331int find_dir(mapstruct *m, int x, int y, object *exclude) { 2094int find_dir(mapstruct *m, int x, int y, object *exclude) {
2332 int i,max=SIZEOFFREE, mflags; 2095 int i,max=SIZEOFFREE, mflags;
2333 sint16 nx, ny; 2096 sint16 nx, ny;
2334 object *tmp; 2097 object *tmp;
2335 mapstruct *mp; 2098 mapstruct *mp;
2336 MoveType blocked, move_type; 2099 MoveType blocked, move_type;
2337 2100
2338 if (exclude && exclude->head) { 2101 if (exclude && exclude->head) {
2339 exclude = exclude->head; 2102 exclude = exclude->head;
2340 move_type = exclude->move_type; 2103 move_type = exclude->move_type;
2341 } else { 2104 } else {
2618/* Also remember that multiparts objects are not supported for now. */ 2381/* Also remember that multiparts objects are not supported for now. */
2619 2382
2620object* load_object_str(const char *obstr) 2383object* load_object_str(const char *obstr)
2621{ 2384{
2622 object *op; 2385 object *op;
2623 FILE *tempfile;
2624 char filename[MAX_BUF]; 2386 char filename[MAX_BUF];
2625 sprintf(filename,"%s/cfloadobstr2044",settings.tmpdir); 2387 sprintf(filename,"%s/cfloadobstr2044",settings.tmpdir);
2388
2626 tempfile=fopen(filename,"w"); 2389 FILE *tempfile=fopen(filename,"w");
2627 if (tempfile == NULL) 2390 if (tempfile == NULL)
2628 { 2391 {
2629 LOG(llevError,"Error - Unable to access load object temp file\n"); 2392 LOG(llevError,"Error - Unable to access load object temp file\n");
2630 return NULL; 2393 return NULL;
2631 }; 2394 };
2632 fprintf(tempfile,obstr); 2395 fprintf(tempfile,obstr);
2633 fclose(tempfile); 2396 fclose(tempfile);
2634 2397
2635 op=get_object(); 2398 op=get_object();
2636 2399
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); 2400 object_thawer thawer (filename);
2401
2402 if (thawer)
2644 load_object(thawer,op,LO_NEWFILE,0); 2403 load_object(thawer,op,0);
2404
2645 LOG(llevDebug," load str completed, object=%s\n",op->name); 2405 LOG(llevDebug," load str completed, object=%s\n", &op->name);
2646 CLEAR_FLAG(op,FLAG_REMOVED); 2406 CLEAR_FLAG(op,FLAG_REMOVED);
2647 fclose(tempfile); 2407
2648 return op; 2408 return op;
2649} 2409}
2650 2410
2651/* This returns the first object in who's inventory that 2411/* This returns the first object in who's inventory that
2652 * has the same type and subtype match. 2412 * has the same type and subtype match.
2689 */ 2449 */
2690const 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) {
2691 key_value * link; 2451 key_value * link;
2692 const char * canonical_key; 2452 const char * canonical_key;
2693 2453
2694 canonical_key = find_string(key); 2454 canonical_key = shstr::find (key);
2695 2455
2696 if (canonical_key == NULL) { 2456 if (canonical_key == NULL) {
2697 /* 1. There being a field named key on any object 2457 /* 1. There being a field named key on any object
2698 * implies there'd be a shared string to find. 2458 * implies there'd be a shared string to find.
2699 * 2. Since there isn't, no object has this field. 2459 * 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 2482 * Unless add_key is TRUE, it won't add fields, only change the value of existing
2723 * keys. 2483 * keys.
2724 * 2484 *
2725 * Returns TRUE on success. 2485 * Returns TRUE on success.
2726 */ 2486 */
2727int 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) {
2728 key_value * field = NULL, *last=NULL; 2488 key_value * field = NULL, *last=NULL;
2729 2489
2730 for (field=op->key_values; field != NULL; field=field->next) { 2490 for (field=op->key_values; field != NULL; field=field->next) {
2731 if (field->key != canonical_key) { 2491 if (field->key != canonical_key) {
2732 last = field; 2492 last = field;
2733 continue; 2493 continue;
2734 } 2494 }
2735 2495
2736 if (field->value) FREE_AND_CLEAR_STR(field->value);
2737 if (value) 2496 if (value)
2738 field->value = add_string(value); 2497 field->value = value;
2739 else { 2498 else {
2740 /* Basically, if the archetype has this key set, 2499 /* Basically, if the archetype has this key set,
2741 * we need to store the null value so when we save 2500 * we need to store the null value so when we save
2742 * it, we save the empty value so that when we load, 2501 * it, we save the empty value so that when we load,
2743 * we get this value back again. 2502 * we get this value back again.
2744 */ 2503 */
2745 if (get_ob_key_link(&op->arch->clone, canonical_key)) 2504 if (get_ob_key_link (&op->arch->clone, canonical_key))
2746 field->value = NULL; 2505 field->value = 0;
2747 else { 2506 else
2748 /* Delete this link */ 2507 {
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; 2508 if (last) last->next = field->next;
2752 else op->key_values = field->next; 2509 else op->key_values = field->next;
2510
2753 free(field); 2511 delete field;
2754 } 2512 }
2755 } 2513 }
2756 return TRUE; 2514 return TRUE;
2757 } 2515 }
2758 /* IF we get here, key doesn't exist */ 2516 /* IF we get here, key doesn't exist */
2759 2517
2768 * be here. If user wants to store empty strings, 2526 * be here. If user wants to store empty strings,
2769 * should pass in "" 2527 * should pass in ""
2770 */ 2528 */
2771 if (value == NULL) return TRUE; 2529 if (value == NULL) return TRUE;
2772 2530
2773 field = (key_value *) malloc(sizeof(key_value)); 2531 field = new key_value;
2774 2532
2775 field->key = add_refcount(canonical_key); 2533 field->key = canonical_key;
2776 field->value = add_string(value); 2534 field->value = value;
2777 /* Usual prepend-addition. */ 2535 /* Usual prepend-addition. */
2778 field->next = op->key_values; 2536 field->next = op->key_values;
2779 op->key_values = field; 2537 op->key_values = field;
2780 2538
2781 return TRUE; 2539 return TRUE;
2788 * and not add new ones. 2546 * and not add new ones.
2789 * 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
2790 * 2548 *
2791 * Returns TRUE on success. 2549 * Returns TRUE on success.
2792 */ 2550 */
2793int 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)
2794 const char * canonical_key = NULL; 2552{
2795 int floating_ref = FALSE; 2553 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); 2554 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} 2555}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines