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.26 by root, Mon Sep 11 11:46:52 2006 UTC vs.
Revision 1.63 by root, Tue Dec 12 20:53:02 2006 UTC

1
2/*
3 * static char *rcsid_object_c =
4 * "$Id: object.C,v 1.26 2006/09/11 11:46:52 root Exp $";
5 */
6
7/* 1/*
8 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
9 3
10 Copyright (C) 2001 Mark Wedel & Crossfire Development Team 4 Copyright (C) 2001 Mark Wedel & Crossfire Development Team
11 Copyright (C) 1992 Frank Tore Johansen 5 Copyright (C) 1992 Frank Tore Johansen
22 16
23 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 20
27 The authors can be reached via e-mail at crossfire-devel@real-time.com 21 The authors can be reached via e-mail at <crossfire@schmorp.de>
28*/ 22*/
29 23
30/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. 24/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects.
31 sub/add_weight will transcend the environment updating the carrying 25 sub/add_weight will transcend the environment updating the carrying
32 variable. */ 26 variable. */
33#include <global.h> 27#include <global.h>
34#ifndef WIN32 /* ---win32 exclude headers */
35# include <stdio.h> 28#include <stdio.h>
36# include <sys/types.h> 29#include <sys/types.h>
37# include <sys/uio.h> 30#include <sys/uio.h>
38#endif /* win32 */
39#include <object.h> 31#include <object.h>
40#include <funcpoint.h> 32#include <funcpoint.h>
41#include <skills.h>
42#include <loader.h> 33#include <loader.h>
34
43int nrofallocobjects = 0; 35int nrofallocobjects = 0;
36static UUID uuid;
37const uint64 UUID_SKIP = 1<<19;
44 38
45object *objects; /* Pointer to the list of used objects */
46object *active_objects; /* List of active objects that need to be processed */ 39object *active_objects; /* List of active objects that need to be processed */
47 40
48short freearr_x[SIZEOFFREE] = { 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, 41short freearr_x[SIZEOFFREE] = { 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,
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 42 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1
50}; 43};
56}; 49};
57int freedir[SIZEOFFREE] = { 50int freedir[SIZEOFFREE] = {
58 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, 51 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,
59 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 52 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8
60}; 53};
54
55static void
56write_uuid (void)
57{
58 char filename1[MAX_BUF], filename2[MAX_BUF];
59
60 sprintf (filename1, "%s/uuid", settings.localdir);
61 sprintf (filename2, "%s/uuid~", settings.localdir);
62
63 FILE *fp;
64
65 if (!(fp = fopen (filename2, "w")))
66 {
67 LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2);
68 return;
69 }
70
71 fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2);
72 fclose (fp);
73 rename (filename2, filename1);
74}
75
76static void
77read_uuid (void)
78{
79 char filename[MAX_BUF];
80
81 sprintf (filename, "%s/uuid", settings.localdir);
82
83 FILE *fp;
84
85 if (!(fp = fopen (filename, "r")))
86 {
87 if (errno == ENOENT)
88 {
89 LOG (llevInfo, "RESET uid to 1\n");
90 uuid.seq = 0;
91 write_uuid ();
92 return;
93 }
94
95 LOG (llevError, "FATAL: cannot open %s for reading!\n", filename);
96 _exit (1);
97 }
98
99 int version;
100 unsigned long long uid;
101 if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1)
102 {
103 LOG (llevError, "FATAL: error reading uid from %s!\n", filename);
104 _exit (1);
105 }
106
107 uuid.seq = uid;
108 write_uuid ();
109 LOG (llevDebug, "read UID: %" PRId64 "\n", uid);
110 fclose (fp);
111}
112
113UUID
114gen_uuid ()
115{
116 UUID uid;
117
118 uid.seq = ++uuid.seq;
119
120 if (!(uuid.seq & (UUID_SKIP - 1)))
121 write_uuid ();
122
123 return uid;
124}
125
126void
127init_uuid ()
128{
129 read_uuid ();
130}
61 131
62/* Returns TRUE if every key_values in wants has a partner with the same value in has. */ 132/* Returns TRUE if every key_values in wants has a partner with the same value in has. */
63static int 133static int
64compare_ob_value_lists_one (const object *wants, const object *has) 134compare_ob_value_lists_one (const object *wants, const object *has)
65{ 135{
214 if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED) 284 if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED)
215 return 0; 285 return 0;
216 286
217 switch (ob1->type) 287 switch (ob1->type)
218 { 288 {
219 case SCROLL: 289 case SCROLL:
220 if (ob1->level != ob2->level) 290 if (ob1->level != ob2->level)
221 return 0; 291 return 0;
222 break; 292 break;
223 } 293 }
224 294
225 if (ob1->key_values != NULL || ob2->key_values != NULL) 295 if (ob1->key_values != NULL || ob2->key_values != NULL)
226 { 296 {
227 /* At least one of these has key_values. */ 297 /* At least one of these has key_values. */
249/* 319/*
250 * sum_weight() is a recursive function which calculates the weight 320 * sum_weight() is a recursive function which calculates the weight
251 * an object is carrying. It goes through in figures out how much 321 * an object is carrying. It goes through in figures out how much
252 * containers are carrying, and sums it up. 322 * containers are carrying, and sums it up.
253 */ 323 */
254signed long 324long
255sum_weight (object *op) 325sum_weight (object *op)
256{ 326{
257 signed long sum; 327 long sum;
258 object *inv; 328 object *inv;
259 329
260 for (sum = 0, inv = op->inv; inv != NULL; inv = inv->below) 330 for (sum = 0, inv = op->inv; inv != NULL; inv = inv->below)
261 { 331 {
262 if (inv->inv) 332 if (inv->inv)
263 sum_weight (inv); 333 sum_weight (inv);
264 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); 334 sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
265 } 335 }
336
266 if (op->type == CONTAINER && op->stats.Str) 337 if (op->type == CONTAINER && op->stats.Str)
267 sum = (sum * (100 - op->stats.Str)) / 100; 338 sum = (sum * (100 - op->stats.Str)) / 100;
339
268 if (op->carrying != sum) 340 if (op->carrying != sum)
269 op->carrying = sum; 341 op->carrying = sum;
342
270 return sum; 343 return sum;
271} 344}
272 345
273/** 346/**
274 * Return the outermost environment object for a given object. 347 * Return the outermost environment object for a given object.
301 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. 374 * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
302 * Some error messages. 375 * Some error messages.
303 * The result of the dump is stored in the static global errmsg array. 376 * The result of the dump is stored in the static global errmsg array.
304 */ 377 */
305 378
306void 379char *
307dump_object2 (object *op)
308{
309 errmsg[0] = 0;
310 return;
311 //TODO//D#d#
312#if 0
313 char *cp;
314
315/* object *tmp;*/
316
317 if (op->arch != NULL)
318 {
319 strcat (errmsg, "arch ");
320 strcat (errmsg, op->arch->name ? op->arch->name : "(null)");
321 strcat (errmsg, "\n");
322 if ((cp = get_ob_diff (op, &empty_archetype->clone)) != NULL)
323 strcat (errmsg, cp);
324# if 0
325 /* Don't dump player diffs - they are too long, mostly meaningless, and
326 * will overflow the buffer.
327 * Changed so that we don't dump inventory either. This may
328 * also overflow the buffer.
329 */
330 if (op->type != PLAYER && (cp = get_ob_diff (op, &empty_archetype->clone)) != NULL)
331 strcat (errmsg, cp);
332 for (tmp = op->inv; tmp; tmp = tmp->below)
333 dump_object2 (tmp);
334# endif
335 strcat (errmsg, "end\n");
336 }
337 else
338 {
339 strcat (errmsg, "Object ");
340 if (op->name == NULL)
341 strcat (errmsg, "(null)");
342 else
343 strcat (errmsg, op->name);
344 strcat (errmsg, "\n");
345# if 0
346 if ((cp = get_ob_diff (op, &empty_archetype->clone)) != NULL)
347 strcat (errmsg, cp);
348 for (tmp = op->inv; tmp; tmp = tmp->below)
349 dump_object2 (tmp);
350# endif
351 strcat (errmsg, "end\n");
352 }
353#endif
354}
355
356/*
357 * Dumps an object. Returns output in the static global errmsg array.
358 */
359
360void
361dump_object (object *op) 380dump_object (object *op)
362{ 381{
363 if (op == NULL) 382 if (!op)
364 { 383 return strdup ("[NULLOBJ]");
365 strcpy (errmsg, "[NULL pointer]");
366 return;
367 }
368 errmsg[0] = '\0';
369 dump_object2 (op);
370}
371 384
372void 385 object_freezer freezer;
373dump_all_objects (void) 386 save_object (freezer, op, 3);
374{ 387 return freezer.as_string ();
375 object *op;
376
377 for (op = objects; op != NULL; op = op->next)
378 {
379 dump_object (op);
380 fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg);
381 }
382} 388}
383 389
384/* 390/*
385 * get_nearest_part(multi-object, object 2) returns the part of the 391 * get_nearest_part(multi-object, object 2) returns the part of the
386 * multi-object 1 which is closest to the second object. 392 * multi-object 1 which is closest to the second object.
406 */ 412 */
407 413
408object * 414object *
409find_object (tag_t i) 415find_object (tag_t i)
410{ 416{
411 object *op; 417 for (object *op = object::first; op; op = op->next)
412
413 for (op = objects; op != NULL; op = op->next)
414 if (op->count == i) 418 if (op->count == i)
415 break; 419 return op;
420
416 return op; 421 return 0;
417} 422}
418 423
419/* 424/*
420 * Returns the first object which has a name equal to the argument. 425 * Returns the first object which has a name equal to the argument.
421 * Used only by the patch command, but not all that useful. 426 * Used only by the patch command, but not all that useful.
423 */ 428 */
424 429
425object * 430object *
426find_object_name (const char *str) 431find_object_name (const char *str)
427{ 432{
428 const char *name = shstr::find (str); 433 shstr_cmp str_ (str);
429 object *op; 434 object *op;
430 435
431 for (op = objects; op != NULL; op = op->next) 436 for (op = object::first; op != NULL; op = op->next)
432 if (&op->name == name) 437 if (op->name == str_)
433 break; 438 break;
434 439
435 return op; 440 return op;
436} 441}
437 442
440{ 445{
441 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); 446 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
442} 447}
443 448
444/* 449/*
445 * 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
447 * freed and are now reused. If this is detected, the owner is
448 * set to NULL, and NULL is returned.
449 * Changed 2004-02-12 - if the player is setting at the play again
450 * prompt, he is removed, and we don't want to treat him as an owner of
451 * anything, so check removed flag. I don't expect that this should break
452 * anything - once an object is removed, it is basically dead anyways.
453 */
454
455object *
456get_owner (object *op)
457{
458 if (op->owner == NULL)
459 return NULL;
460
461 if (!QUERY_FLAG (op->owner, FLAG_FREED) && !QUERY_FLAG (op->owner, FLAG_REMOVED) && op->owner->count == op->ownercount)
462 return op->owner;
463
464 op->owner = NULL;
465 op->ownercount = 0;
466 return NULL;
467}
468
469void
470clear_owner (object *op)
471{
472 if (!op)
473 return;
474
475 if (op->owner && op->ownercount == op->owner->count)
476 op->owner->refcount--;
477
478 op->owner = NULL;
479 op->ownercount = 0;
480}
481
482/*
483 * Sets the owner and sets the skill and exp pointers to owner's current 450 * Sets the owner and sets the skill and exp pointers to owner's current
484 * skill and experience objects. 451 * skill and experience objects.
485 */ 452 */
486void 453void
487set_owner (object *op, object *owner) 454object::set_owner (object *owner)
488{ 455{
489 if (owner == NULL || op == NULL) 456 if (!owner)
490 return; 457 return;
491 458
492 /* next line added to allow objects which own objects */ 459 /* next line added to allow objects which own objects */
493 /* Add a check for ownercounts in here, as I got into an endless loop 460 /* Add a check for ownercounts in here, as I got into an endless loop
494 * with the fireball owning a poison cloud which then owned the 461 * with the fireball owning a poison cloud which then owned the
495 * fireball. I believe that was caused by one of the objects getting 462 * fireball. I believe that was caused by one of the objects getting
496 * freed and then another object replacing it. Since the ownercounts 463 * freed and then another object replacing it. Since the ownercounts
497 * didn't match, this check is valid and I believe that cause is valid. 464 * didn't match, this check is valid and I believe that cause is valid.
498 */ 465 */
499 while (owner->owner && owner != owner->owner && owner->ownercount == owner->owner->count) 466 while (owner->owner)
500 owner = owner->owner; 467 owner = owner->owner;
501 468
502 /* IF the owner still has an owner, we did not resolve to a final owner.
503 * so lets not add to that.
504 */
505 if (owner->owner)
506 return;
507
508 op->owner = owner; 469 this->owner = owner;
509
510 op->ownercount = owner->count;
511 owner->refcount++;
512}
513
514/* Set the owner to clone's current owner and set the skill and experience
515 * objects to clone's objects (typically those objects that where the owner's
516 * current skill and experience objects at the time when clone's owner was
517 * set - not the owner's current skill and experience objects).
518 *
519 * Use this function if player created an object (e.g. fire bullet, swarm
520 * spell), and this object creates further objects whose kills should be
521 * accounted for the player's original skill, even if player has changed
522 * skills meanwhile.
523 */
524void
525copy_owner (object *op, object *clone)
526{
527 object *owner = get_owner (clone);
528
529 if (owner == NULL)
530 {
531 /* players don't have owners - they own themselves. Update
532 * as appropriate.
533 */
534 if (clone->type == PLAYER)
535 owner = clone;
536 else
537 return;
538 }
539
540 set_owner (op, owner);
541} 470}
542 471
543/* Zero the key_values on op, decrementing the shared-string 472/* Zero the key_values on op, decrementing the shared-string
544 * refcounts and freeing the links. 473 * refcounts and freeing the links.
545 */ 474 */
561{ 490{
562 attachable_base::clear (); 491 attachable_base::clear ();
563 492
564 free_key_values (this); 493 free_key_values (this);
565 494
566 clear_owner (this); 495 owner = 0;
567
568 name = 0; 496 name = 0;
569 name_pl = 0; 497 name_pl = 0;
570 title = 0; 498 title = 0;
571 race = 0; 499 race = 0;
572 slaying = 0; 500 slaying = 0;
573 skill = 0; 501 skill = 0;
574 msg = 0; 502 msg = 0;
575 lore = 0; 503 lore = 0;
576 custom_name = 0; 504 custom_name = 0;
577 materialname = 0; 505 materialname = 0;
506 contr = 0;
507 below = 0;
508 above = 0;
509 inv = 0;
510 container = 0;
511 env = 0;
512 more = 0;
513 head = 0;
514 map = 0;
515 active_next = 0;
516 active_prev = 0;
578 517
579 memset (static_cast < object_pod * >(this), 0, sizeof (object_pod)); 518 memset (static_cast<object_pod *>(this), 0, sizeof (object_pod));
580 519
581 SET_FLAG (this, FLAG_REMOVED); 520 SET_FLAG (this, FLAG_REMOVED);
521
522 /* What is not cleared is next, prev, and count */
523
524 expmul = 1.0;
525 face = blank_face;
526
527 if (settings.casting_time)
528 casting_time = -1;
582} 529}
583 530
584void object::clone (object *destination) 531void object::clone (object *destination)
585{ 532{
586 *(object_copy *) destination = *this; 533 *(object_copy *)destination = *this;
587 *(object_pod *) destination = *this; 534 *(object_pod *)destination = *this;
588 535
589 if (self || cb) 536 if (self || cb)
590 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); 537 INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination));
591}
592
593/*
594 * clear_object() frees everything allocated by an object, and also
595 * clears all variables and flags to default settings.
596 */
597
598void
599clear_object (object *op)
600{
601 op->clear ();
602
603 op->contr = NULL;
604 op->below = NULL;
605 op->above = NULL;
606 op->inv = NULL;
607 op->container = NULL;
608 op->env = NULL;
609 op->more = NULL;
610 op->head = NULL;
611 op->map = NULL;
612 op->refcount = 0;
613 op->active_next = NULL;
614 op->active_prev = NULL;
615 /* What is not cleared is next, prev, and count */
616
617 op->expmul = 1.0;
618 op->face = blank_face;
619 op->attacked_by_count = -1;
620
621 if (settings.casting_time)
622 op->casting_time = -1;
623} 538}
624 539
625/* 540/*
626 * copy object first frees everything allocated by the second object, 541 * copy object first frees everything allocated by the second object,
627 * and then copies the contends of the first object into the second 542 * and then copies the contends of the first object into the second
628 * object, allocating what needs to be allocated. Basically, any 543 * object, allocating what needs to be allocated. Basically, any
629 * data that is malloc'd needs to be re-malloc/copied. Otherwise, 544 * data that is malloc'd needs to be re-malloc/copied. Otherwise,
630 * if the first object is freed, the pointers in the new object 545 * if the first object is freed, the pointers in the new object
631 * will point at garbage. 546 * will point at garbage.
632 */ 547 */
633
634void 548void
635copy_object (object *op2, object *op) 549copy_object (object *op2, object *op)
636{ 550{
637 bool is_freed = QUERY_FLAG (op, FLAG_FREED); 551 bool is_freed = QUERY_FLAG (op, FLAG_FREED);
638 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); 552 bool is_removed = QUERY_FLAG (op, FLAG_REMOVED);
639 553
640 op2->clone (op); 554 op2->clone (op);
641 555
642 if (is_freed) 556 if (is_freed)
643 SET_FLAG (op, FLAG_FREED); 557 SET_FLAG (op, FLAG_FREED);
558
644 if (is_removed) 559 if (is_removed)
645 SET_FLAG (op, FLAG_REMOVED); 560 SET_FLAG (op, FLAG_REMOVED);
646 561
647 if (op2->speed < 0) 562 if (op2->speed < 0)
648 op->speed_left = op2->speed_left - RANDOM () % 200 / 100.0; 563 op->speed_left = op2->speed_left - RANDOM () % 200 / 100.0;
698/* 613/*
699 * Updates the speed of an object. If the speed changes from 0 to another 614 * Updates the speed of an object. If the speed changes from 0 to another
700 * value, or vice versa, then add/remove the object from the active list. 615 * value, or vice versa, then add/remove the object from the active list.
701 * This function needs to be called whenever the speed of an object changes. 616 * This function needs to be called whenever the speed of an object changes.
702 */ 617 */
703
704void 618void
705update_ob_speed (object *op) 619update_ob_speed (object *op)
706{ 620{
707 extern int arch_init; 621 extern int arch_init;
708 622
717 abort (); 631 abort ();
718#else 632#else
719 op->speed = 0; 633 op->speed = 0;
720#endif 634#endif
721 } 635 }
636
722 if (arch_init) 637 if (arch_init)
723 {
724 return; 638 return;
725 } 639
726 if (FABS (op->speed) > MIN_ACTIVE_SPEED) 640 if (FABS (op->speed) > MIN_ACTIVE_SPEED)
727 { 641 {
728 /* If already on active list, don't do anything */ 642 /* If already on active list, don't do anything */
729 if (op->active_next || op->active_prev || op == active_objects) 643 if (op->active_next || op->active_prev || op == active_objects)
730 return; 644 return;
731 645
732 /* process_events() expects us to insert the object at the beginning 646 /* process_events() expects us to insert the object at the beginning
733 * of the list. */ 647 * of the list. */
734 op->active_next = active_objects; 648 op->active_next = active_objects;
649
735 if (op->active_next != NULL) 650 if (op->active_next != NULL)
736 op->active_next->active_prev = op; 651 op->active_next->active_prev = op;
652
737 active_objects = op; 653 active_objects = op;
738 } 654 }
739 else 655 else
740 { 656 {
741 /* If not on the active list, nothing needs to be done */ 657 /* If not on the active list, nothing needs to be done */
743 return; 659 return;
744 660
745 if (op->active_prev == NULL) 661 if (op->active_prev == NULL)
746 { 662 {
747 active_objects = op->active_next; 663 active_objects = op->active_next;
664
748 if (op->active_next != NULL) 665 if (op->active_next != NULL)
749 op->active_next->active_prev = NULL; 666 op->active_next->active_prev = NULL;
750 } 667 }
751 else 668 else
752 { 669 {
753 op->active_prev->active_next = op->active_next; 670 op->active_prev->active_next = op->active_next;
671
754 if (op->active_next) 672 if (op->active_next)
755 op->active_next->active_prev = op->active_prev; 673 op->active_next->active_prev = op->active_prev;
756 } 674 }
675
757 op->active_next = NULL; 676 op->active_next = NULL;
758 op->active_prev = NULL; 677 op->active_prev = NULL;
759 } 678 }
760} 679}
761 680
886 update_now = 1; 805 update_now = 1;
887 806
888 if ((move_slow | op->move_slow) != move_slow) 807 if ((move_slow | op->move_slow) != move_slow)
889 update_now = 1; 808 update_now = 1;
890 } 809 }
810
891 /* if the object is being removed, we can't make intelligent 811 /* if the object is being removed, we can't make intelligent
892 * decisions, because remove_ob can't really pass the object 812 * decisions, because remove_ob can't really pass the object
893 * that is being removed. 813 * that is being removed.
894 */ 814 */
895 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) 815 else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE)
896 {
897 update_now = 1; 816 update_now = 1;
898 }
899 else if (action == UP_OBJ_FACE) 817 else if (action == UP_OBJ_FACE)
900 {
901 /* Nothing to do for that case */ 818 /* Nothing to do for that case */ ;
902 }
903 else 819 else
904 {
905 LOG (llevError, "update_object called with invalid action: %d\n", action); 820 LOG (llevError, "update_object called with invalid action: %d\n", action);
906 }
907 821
908 if (update_now) 822 if (update_now)
909 { 823 {
910 SET_MAP_FLAGS (op->map, op->x, op->y, flags | P_NO_ERROR | P_NEED_UPDATE); 824 SET_MAP_FLAGS (op->map, op->x, op->y, flags | P_NO_ERROR | P_NEED_UPDATE);
911 update_position (op->map, op->x, op->y); 825 update_position (op->map, op->x, op->y);
913 827
914 if (op->more != NULL) 828 if (op->more != NULL)
915 update_object (op->more, action); 829 update_object (op->more, action);
916} 830}
917 831
918static unordered_vector<object *> mortals; 832object::vector object::mortals;
919static std::vector<object *> freed; 833object::vector object::objects; // not yet used
834object *object::first;
920 835
921void object::free_mortals () 836void object::free_mortals ()
922{ 837{
923 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end (); ) 838 for (AUTODECL (i, mortals.begin ()); i != mortals.end ();)
924 if (!(*i)->refcount) 839 if ((*i)->refcnt)
840 ++i; // further delay freeing
841 else
925 { 842 {
926 freed.push_back (*i); 843 delete *i;
927 mortals.erase (i); 844 mortals.erase (i);
928 } 845 }
929 else
930 ++i;
931} 846}
932 847
933object::object () 848object::object ()
934{ 849{
935 SET_FLAG (this, FLAG_REMOVED); 850 SET_FLAG (this, FLAG_REMOVED);
936 851
937 expmul = 1.0; 852 expmul = 1.0;
938 face = blank_face; 853 face = blank_face;
939 attacked_by_count = -1;
940} 854}
941 855
942object::~object () 856object::~object ()
943{ 857{
944 free_key_values (this); 858 free_key_values (this);
945} 859}
946 860
947void object::link () 861void object::link ()
948{ 862{
949 count = ++ob_count; 863 count = ++ob_count;
864 uuid = gen_uuid ();
950 865
951 prev = 0; 866 prev = 0;
952 next = objects; 867 next = object::first;
953 868
954 if (objects) 869 if (object::first)
955 objects->prev = this; 870 object::first->prev = this;
956 871
957 objects = this; 872 object::first = this;
958} 873}
959 874
960void object::unlink () 875void object::unlink ()
961{ 876{
962 count = 0; 877 if (this == object::first)
878 object::first = next;
963 879
964 /* Remove this object from the list of used objects */ 880 /* Remove this object from the list of used objects */
965 if (prev)
966 {
967 prev->next = next; 881 if (prev) prev->next = next;
882 if (next) next->prev = prev;
883
968 prev = 0; 884 prev = 0;
969 }
970
971 if (next)
972 {
973 next->prev = prev;
974 next = 0; 885 next = 0;
975 }
976
977 if (this == objects)
978 objects = next;
979} 886}
980 887
981object *object::create () 888object *object::create ()
982{ 889{
983 object *op;
984
985 if (freed.empty ())
986 op = new object; 890 object *op = new object;
987 else
988 {
989 // highly annoying, but the only way to get it stable right now
990 op = freed.back (); freed.pop_back ();
991 op->~object ();
992 new ((void *)op) object;
993 }
994
995 op->link (); 891 op->link ();
996 return op; 892 return op;
997} 893}
998 894
999/* 895/*
1001 * it from the list of used objects, and puts it on the list of 897 * it from the list of used objects, and puts it on the list of
1002 * free objects. The IS_FREED() flag is set in the object. 898 * free objects. The IS_FREED() flag is set in the object.
1003 * The object must have been removed by remove_ob() first for 899 * The object must have been removed by remove_ob() first for
1004 * this function to succeed. 900 * this function to succeed.
1005 * 901 *
1006 * If free_inventory is set, free inventory as well. Else drop items in 902 * If destroy_inventory is set, free inventory as well. Else drop items in
1007 * inventory to the ground. 903 * inventory to the ground.
1008 */ 904 */
1009void object::free (bool free_inventory) 905void object::destroy (bool destroy_inventory)
1010{ 906{
907 if (QUERY_FLAG (this, FLAG_FREED))
908 return;
909
910 if (QUERY_FLAG (this, FLAG_FRIENDLY))
911 remove_friendly_object (this);
912
1011 if (!QUERY_FLAG (this, FLAG_REMOVED)) 913 if (!QUERY_FLAG (this, FLAG_REMOVED))
1012 { 914 remove ();
1013 LOG (llevDebug, "Free object called with non removed object\n");
1014 dump_object (this);
1015#ifdef MANY_CORES
1016 abort ();
1017#endif
1018 }
1019 915
1020 if (QUERY_FLAG (this, FLAG_FRIENDLY))
1021 {
1022 LOG (llevMonster, "Warning: tried to free friendly object.\n");
1023 remove_friendly_object (this);
1024 }
1025
1026 if (QUERY_FLAG (this, FLAG_FREED)) 916 SET_FLAG (this, FLAG_FREED);
1027 {
1028 dump_object (this);
1029 LOG (llevError, "Trying to free freed object.\n%s\n", errmsg);
1030 return;
1031 }
1032 917
1033 if (more) 918 if (more)
1034 { 919 {
1035 more->free (free_inventory); 920 more->destroy (destroy_inventory);
1036 more = 0; 921 more = 0;
1037 } 922 }
1038 923
1039 if (inv) 924 if (inv)
1040 { 925 {
1041 /* Only if the space blocks everything do we not process - 926 /* Only if the space blocks everything do we not process -
1042 * if some form of movement is allowed, let objects 927 * if some form of movement is allowed, let objects
1043 * drop on that space. 928 * drop on that space.
1044 */ 929 */
1045 if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) 930 if (destroy_inventory || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)
1046 { 931 {
1047 object * 932 object *op = inv;
1048 op = inv;
1049 933
1050 while (op) 934 while (op)
1051 { 935 {
1052 object *
1053 tmp = op->below; 936 object *tmp = op->below;
1054
1055 remove_ob (op);
1056 op->free (free_inventory); 937 op->destroy (destroy_inventory);
1057 op = tmp; 938 op = tmp;
1058 } 939 }
1059 } 940 }
1060 else 941 else
1061 { /* Put objects in inventory onto this space */ 942 { /* Put objects in inventory onto this space */
1062 object * 943 object *op = inv;
1063 op = inv;
1064 944
1065 while (op) 945 while (op)
1066 { 946 {
1067 object *
1068 tmp = op->below; 947 object *tmp = op->below;
1069 948
1070 remove_ob (op); 949 op->remove ();
1071 950
1072 if (QUERY_FLAG (op, FLAG_STARTEQUIP) 951 if (QUERY_FLAG (op, FLAG_STARTEQUIP)
1073 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE)) 952 || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE))
1074 free_object (op); 953 op->destroy (0);
1075 else 954 else
1076 { 955 {
1077 op->x = x; 956 op->x = x;
1078 op->y = y; 957 op->y = y;
1079 insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */ 958 insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */
1082 op = tmp; 961 op = tmp;
1083 } 962 }
1084 } 963 }
1085 } 964 }
1086 965
1087 clear_owner (this); 966 // hack to ensure that freed objects still have a valid map
967 {
968 static maptile *freed_map; // freed objects are moved here to avoid crashes
969
970 if (!freed_map)
971 {
972 freed_map = new maptile;
973
974 freed_map->name = "/internal/freed_objects_map";
975 freed_map->width = 3;
976 freed_map->height = 3;
977
978 freed_map->allocate ();
979 }
980
981 map = freed_map;
982 x = 1;
983 y = 1;
984 }
985
986 // clear those pointers that likely might have circular references to us
987 owner = 0;
988 enemy = 0;
989 attacked_by = 0;
990
991 // only relevant for players(?), but make sure of it anyways
992 contr = 0;
1088 993
1089 /* Remove object from the active list */ 994 /* Remove object from the active list */
1090 speed = 0; 995 speed = 0;
1091 update_ob_speed (this); 996 update_ob_speed (this);
1092 997
1093 unlink (); 998 unlink ();
1094 999
1095 SET_FLAG (this, FLAG_FREED);
1096
1097 mortals.push_back (this); 1000 mortals.push_back (this);
1098} 1001}
1099 1002
1100/* 1003/*
1101 * sub_weight() recursively (outwards) subtracts a number from the 1004 * sub_weight() recursively (outwards) subtracts a number from the
1102 * weight of an object (and what is carried by it's environment(s)). 1005 * weight of an object (and what is carried by it's environment(s)).
1103 */ 1006 */
1104
1105void 1007void
1106sub_weight (object *op, signed long weight) 1008sub_weight (object *op, signed long weight)
1107{ 1009{
1108 while (op != NULL) 1010 while (op != NULL)
1109 { 1011 {
1110 if (op->type == CONTAINER) 1012 if (op->type == CONTAINER)
1111 {
1112 weight = (signed long) (weight * (100 - op->stats.Str) / 100); 1013 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1113 } 1014
1114 op->carrying -= weight; 1015 op->carrying -= weight;
1115 op = op->env; 1016 op = op->env;
1116 } 1017 }
1117} 1018}
1118 1019
1119/* remove_ob(op): 1020/* op->remove ():
1120 * This function removes the object op from the linked list of objects 1021 * This function removes the object op from the linked list of objects
1121 * which it is currently tied to. When this function is done, the 1022 * which it is currently tied to. When this function is done, the
1122 * object will have no environment. If the object previously had an 1023 * object will have no environment. If the object previously had an
1123 * environment, the x and y coordinates will be updated to 1024 * environment, the x and y coordinates will be updated to
1124 * the previous environment. 1025 * the previous environment.
1125 * Beware: This function is called from the editor as well! 1026 * Beware: This function is called from the editor as well!
1126 */ 1027 */
1127
1128void 1028void
1129remove_ob (object *op) 1029object::remove ()
1130{ 1030{
1131 object *tmp, *last = NULL; 1031 object *tmp, *last = 0;
1132 object *otmp; 1032 object *otmp;
1133 1033
1134 tag_t tag;
1135 int check_walk_off; 1034 int check_walk_off;
1136 mapstruct *m;
1137 1035
1138 sint16 x, y;
1139
1140 if (QUERY_FLAG (op, FLAG_REMOVED)) 1036 if (QUERY_FLAG (this, FLAG_REMOVED))
1141 { 1037 return;
1142 dump_object (op);
1143 LOG (llevError, "Trying to remove removed object.\n%s\n", errmsg);
1144 1038
1145 /* Changed it to always dump core in this case. As has been learned
1146 * in the past, trying to recover from errors almost always
1147 * make things worse, and this is a real error here - something
1148 * that should not happen.
1149 * Yes, if this was a mission critical app, trying to do something
1150 * to recover may make sense, but that is because failure of the app
1151 * may have other disastrous problems. Cf runs out of a script
1152 * so is easily enough restarted without any real problems.
1153 * MSW 2001-07-01
1154 */
1155 abort ();
1156 }
1157
1158 if (op->more != NULL)
1159 remove_ob (op->more);
1160
1161 SET_FLAG (op, FLAG_REMOVED); 1039 SET_FLAG (this, FLAG_REMOVED);
1040
1041 if (more)
1042 more->remove ();
1162 1043
1163 /* 1044 /*
1164 * In this case, the object to be removed is in someones 1045 * In this case, the object to be removed is in someones
1165 * inventory. 1046 * inventory.
1166 */ 1047 */
1167 if (op->env != NULL) 1048 if (env)
1168 { 1049 {
1169 if (op->nrof) 1050 if (nrof)
1170 sub_weight (op->env, op->weight * op->nrof); 1051 sub_weight (env, weight * nrof);
1171 else 1052 else
1172 sub_weight (op->env, op->weight + op->carrying); 1053 sub_weight (env, weight + carrying);
1173 1054
1174 /* NO_FIX_PLAYER is set when a great many changes are being 1055 /* NO_FIX_PLAYER is set when a great many changes are being
1175 * made to players inventory. If set, avoiding the call 1056 * made to players inventory. If set, avoiding the call
1176 * to save cpu time. 1057 * to save cpu time.
1177 */ 1058 */
1178 if ((otmp = is_player_inv (op->env)) != NULL && otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) 1059 if ((otmp = is_player_inv (env)) != NULL && otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER))
1179 fix_player (otmp); 1060 fix_player (otmp);
1180 1061
1181 if (op->above != NULL) 1062 if (above != NULL)
1182 op->above->below = op->below; 1063 above->below = below;
1183 else 1064 else
1184 op->env->inv = op->below; 1065 env->inv = below;
1185 1066
1186 if (op->below != NULL) 1067 if (below != NULL)
1187 op->below->above = op->above; 1068 below->above = above;
1188 1069
1189 /* we set up values so that it could be inserted into 1070 /* we set up values so that it could be inserted into
1190 * 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
1191 * to the caller to decide what we want to do. 1072 * to the caller to decide what we want to do.
1192 */ 1073 */
1193 op->x = op->env->x, op->y = op->env->y; 1074 x = env->x, y = env->y;
1194 op->map = op->env->map; 1075 map = env->map;
1195 op->above = NULL, op->below = NULL; 1076 above = 0, below = 0;
1196 op->env = NULL; 1077 env = 0;
1197 return;
1198 }
1199
1200 /* If we get here, we are removing it from a map */
1201 if (op->map == NULL)
1202 return;
1203
1204 x = op->x;
1205 y = op->y;
1206 m = get_map_from_coord (op->map, &x, &y);
1207
1208 if (!m)
1209 { 1078 }
1210 LOG (llevError, "remove_ob called when object was on map but appears to not be within valid coordinates? %s (%d,%d)\n", 1079 else if (map)
1211 op->map->path, op->x, op->y); 1080 {
1212 /* in old days, we used to set x and y to 0 and continue. 1081 /* Re did the following section of code - it looks like it had
1213 * it seems if we get into this case, something is probablye 1082 * lots of logic for things we no longer care about
1214 * screwed up and should be fixed.
1215 */ 1083 */
1216 abort ();
1217 }
1218 if (op->map != m)
1219 {
1220 LOG (llevDebug, "remove_ob: Object not really on map it claimed to be on? %s != %s, %d,%d != %d,%d\n",
1221 op->map->path, m->path, op->x, op->y, x, y);
1222 }
1223 1084
1224 /* Re did the following section of code - it looks like it had
1225 * lots of logic for things we no longer care about
1226 */
1227
1228 /* link the object above us */ 1085 /* link the object above us */
1229 if (op->above) 1086 if (above)
1230 op->above->below = op->below; 1087 above->below = below;
1231 else 1088 else
1232 SET_MAP_TOP (m, x, y, op->below); /* we were top, set new top */ 1089 SET_MAP_TOP (map, x, y, below); /* we were top, set new top */
1233 1090
1234 /* Relink the object below us, if there is one */ 1091 /* Relink the object below us, if there is one */
1235 if (op->below) 1092 if (below)
1236 op->below->above = op->above; 1093 below->above = above;
1237 else 1094 else
1238 { 1095 {
1239 /* Nothing below, which means we need to relink map object for this space 1096 /* Nothing below, which means we need to relink map object for this space
1240 * use translated coordinates in case some oddness with map tiling is 1097 * use translated coordinates in case some oddness with map tiling is
1241 * evident 1098 * evident
1242 */
1243 if (GET_MAP_OB (m, x, y) != op)
1244 {
1245 dump_object (op);
1246 LOG (llevError, "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom?\n%s\n",
1247 errmsg);
1248 dump_object (GET_MAP_OB (m, x, y));
1249 LOG (llevError, "%s\n", errmsg);
1250 }
1251
1252 SET_MAP_OB (m, x, y, op->above); /* goes on above it. */
1253 }
1254
1255 op->above = NULL;
1256 op->below = NULL;
1257
1258 if (op->map->in_memory == MAP_SAVING)
1259 return;
1260
1261 tag = op->count;
1262 check_walk_off = !QUERY_FLAG (op, FLAG_NO_APPLY);
1263
1264 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above)
1265 {
1266 /* No point updating the players look faces if he is the object
1267 * being removed.
1268 */
1269
1270 if (tmp->type == PLAYER && tmp != op)
1271 {
1272 /* If a container that the player is currently using somehow gets
1273 * removed (most likely destroyed), update the player view
1274 * appropriately.
1275 */ 1099 */
1276 if (tmp->container == op) 1100 if (GET_MAP_OB (map, x, y) != this)
1277 { 1101 {
1278 CLEAR_FLAG (op, FLAG_APPLIED); 1102 char *dump = dump_object (this);
1279 tmp->container = NULL; 1103 LOG (llevError,
1104 "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom?\n%s\n", dump);
1105 free (dump);
1106 dump = dump_object (GET_MAP_OB (map, x, y));
1107 LOG (llevError, "%s\n", dump);
1108 free (dump);
1280 } 1109 }
1281 1110
1282 tmp->contr->socket.update_look = 1; 1111 SET_MAP_OB (map, x, y, above); /* goes on above it. */
1283 }
1284 /* See if player moving off should effect something */
1285 if (check_walk_off && ((op->move_type & tmp->move_off) && (op->move_type & ~tmp->move_off & ~tmp->move_block) == 0))
1286 { 1112 }
1287 move_apply (tmp, op, NULL);
1288 1113
1289 if (was_destroyed (op, tag)) 1114 above = 0;
1115 below = 0;
1116
1117 if (map->in_memory == MAP_SAVING)
1118 return;
1119
1120 check_walk_off = !QUERY_FLAG (this, FLAG_NO_APPLY);
1121
1122 for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above)
1123 {
1124 /* No point updating the players look faces if he is the object
1125 * being removed.
1126 */
1127
1128 if (tmp->type == PLAYER && tmp != this)
1290 { 1129 {
1291 LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " "leaving object\n", &tmp->name, &tmp->arch->name); 1130 /* If a container that the player is currently using somehow gets
1131 * removed (most likely destroyed), update the player view
1132 * appropriately.
1133 */
1134 if (tmp->container == this)
1135 {
1136 CLEAR_FLAG (this, FLAG_APPLIED);
1137 tmp->container = 0;
1138 }
1139
1140 tmp->contr->socket.update_look = 1;
1292 } 1141 }
1142
1143 /* See if player moving off should effect something */
1144 if (check_walk_off
1145 && ((move_type & tmp->move_off)
1146 && (move_type & ~tmp->move_off & ~tmp->move_block) == 0))
1147 {
1148 move_apply (tmp, this, 0);
1149
1150 if (destroyed ())
1151 LOG (llevError, "BUG: remove_ob(): name %s, destroyed leaving object\n", tmp->debug_desc ());
1293 } 1152 }
1294 1153
1295 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ 1154 /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1296 1155
1297 if (tmp->above == tmp) 1156 if (tmp->above == tmp)
1298 tmp->above = NULL; 1157 tmp->above = 0;
1299 1158
1300 last = tmp; 1159 last = tmp;
1301 } 1160 }
1302 1161
1303 /* last == NULL of there are no objects on this space */ 1162 /* last == NULL of there are no objects on this space */
1304 if (last == NULL) 1163 if (!last)
1305 { 1164 {
1306 /* set P_NEED_UPDATE, otherwise update_position will complain. In theory, 1165 /* set P_NEED_UPDATE, otherwise update_position will complain. In theory,
1307 * we could preserve the flags (GET_MAP_FLAGS), but update_position figures 1166 * we could preserve the flags (GET_MAP_FLAGS), but update_position figures
1308 * those out anyways, and if there are any flags set right now, they won't 1167 * those out anyways, and if there are any flags set right now, they won't
1309 * be correct anyways. 1168 * be correct anyways.
1310 */ 1169 */
1311 SET_MAP_FLAGS (op->map, op->x, op->y, P_NEED_UPDATE); 1170 SET_MAP_FLAGS (map, x, y, P_NEED_UPDATE);
1312 update_position (op->map, op->x, op->y); 1171 update_position (map, x, y);
1313 } 1172 }
1314 else 1173 else
1315 update_object (last, UP_OBJ_REMOVE); 1174 update_object (last, UP_OBJ_REMOVE);
1316 1175
1317 if (QUERY_FLAG (op, FLAG_BLOCKSVIEW) || (op->glow_radius != 0)) 1176 if (QUERY_FLAG (this, FLAG_BLOCKSVIEW) || glow_radius)
1318 update_all_los (op->map, op->x, op->y); 1177 update_all_los (map, x, y);
1178 }
1319} 1179}
1320 1180
1321/* 1181/*
1322 * merge_ob(op,top): 1182 * merge_ob(op,top):
1323 * 1183 *
1324 * This function goes through all objects below and including top, and 1184 * This function goes through all objects below and including top, and
1325 * merges op to the first matching object. 1185 * merges op to the first matching object.
1326 * If top is NULL, it is calculated. 1186 * If top is NULL, it is calculated.
1327 * Returns pointer to object if it succeded in the merge, otherwise NULL 1187 * Returns pointer to object if it succeded in the merge, otherwise NULL
1328 */ 1188 */
1329
1330object * 1189object *
1331merge_ob (object *op, object *top) 1190merge_ob (object *op, object *top)
1332{ 1191{
1333 if (!op->nrof) 1192 if (!op->nrof)
1334 return 0; 1193 return 0;
1194
1335 if (top == NULL) 1195 if (top == NULL)
1336 for (top = op; top != NULL && top->above != NULL; top = top->above); 1196 for (top = op; top != NULL && top->above != NULL; top = top->above);
1197
1337 for (; top != NULL; top = top->below) 1198 for (; top != NULL; top = top->below)
1338 { 1199 {
1339 if (top == op) 1200 if (top == op)
1340 continue; 1201 continue;
1341 if (CAN_MERGE (op, top)) 1202 if (CAN_MERGE (op, top))
1342 { 1203 {
1343 top->nrof += op->nrof; 1204 top->nrof += op->nrof;
1344 1205
1345/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ 1206/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/
1346 op->weight = 0; /* Don't want any adjustements now */ 1207 op->weight = 0; /* Don't want any adjustements now */
1347 remove_ob (op); 1208 op->remove ();
1348 free_object (op); 1209 op->destroy (0);
1349 return top; 1210 return top;
1350 } 1211 }
1351 } 1212 }
1213
1352 return NULL; 1214 return 0;
1353} 1215}
1354 1216
1355/* 1217/*
1356 * same as insert_ob_in_map except it handle separate coordinates and do a clean 1218 * same as insert_ob_in_map except it handle separate coordinates and do a clean
1357 * job preparing multi-part monsters 1219 * job preparing multi-part monsters
1358 */ 1220 */
1359object * 1221object *
1360insert_ob_in_map_at (object *op, mapstruct *m, object *originator, int flag, int x, int y) 1222insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y)
1361{ 1223{
1362 object * 1224 object *tmp;
1363 tmp;
1364 1225
1365 if (op->head) 1226 if (op->head)
1366 op = op->head; 1227 op = op->head;
1228
1367 for (tmp = op; tmp; tmp = tmp->more) 1229 for (tmp = op; tmp; tmp = tmp->more)
1368 { 1230 {
1369 tmp->x = x + tmp->arch->clone.x; 1231 tmp->x = x + tmp->arch->clone.x;
1370 tmp->y = y + tmp->arch->clone.y; 1232 tmp->y = y + tmp->arch->clone.y;
1371 } 1233 }
1234
1372 return insert_ob_in_map (op, m, originator, flag); 1235 return insert_ob_in_map (op, m, originator, flag);
1373} 1236}
1374 1237
1375/* 1238/*
1376 * insert_ob_in_map (op, map, originator, flag): 1239 * insert_ob_in_map (op, map, originator, flag):
1392 * NULL if 'op' was destroyed 1255 * NULL if 'op' was destroyed
1393 * just 'op' otherwise 1256 * just 'op' otherwise
1394 */ 1257 */
1395 1258
1396object * 1259object *
1397insert_ob_in_map (object *op, mapstruct *m, object *originator, int flag) 1260insert_ob_in_map (object *op, maptile *m, object *originator, int flag)
1398{ 1261{
1399 object *tmp, *top, *floor = NULL; 1262 object *tmp, *top, *floor = NULL;
1400 sint16 x, y; 1263 sint16 x, y;
1401 1264
1402 if (QUERY_FLAG (op, FLAG_FREED)) 1265 if (QUERY_FLAG (op, FLAG_FREED))
1405 return NULL; 1268 return NULL;
1406 } 1269 }
1407 1270
1408 if (m == NULL) 1271 if (m == NULL)
1409 { 1272 {
1410 dump_object (op); 1273 char *dump = dump_object (op);
1411 LOG (llevError, "Trying to insert in null-map!\n%s\n", errmsg); 1274 LOG (llevError, "Trying to insert in null-map!\n%s\n", dump);
1275 free (dump);
1412 return op; 1276 return op;
1413 } 1277 }
1414 1278
1415 if (out_of_map (m, op->x, op->y)) 1279 if (out_of_map (m, op->x, op->y))
1416 { 1280 {
1417 dump_object (op); 1281 char *dump = dump_object (op);
1418 LOG (llevError, "Trying to insert object outside the map.\n%s\n", errmsg); 1282 LOG (llevError, "Trying to insert object outside the map.\n%s\n", dump);
1419#ifdef MANY_CORES 1283#ifdef MANY_CORES
1420 /* Better to catch this here, as otherwise the next use of this object 1284 /* Better to catch this here, as otherwise the next use of this object
1421 * is likely to cause a crash. Better to find out where it is getting 1285 * is likely to cause a crash. Better to find out where it is getting
1422 * improperly inserted. 1286 * improperly inserted.
1423 */ 1287 */
1424 abort (); 1288 abort ();
1425#endif 1289#endif
1290 free (dump);
1426 return op; 1291 return op;
1427 } 1292 }
1428 1293
1429 if (!QUERY_FLAG (op, FLAG_REMOVED)) 1294 if (!QUERY_FLAG (op, FLAG_REMOVED))
1430 { 1295 {
1431 dump_object (op); 1296 char *dump = dump_object (op);
1432 LOG (llevError, "Trying to insert (map) inserted object.\n%s\n", errmsg); 1297 LOG (llevError, "Trying to insert (map) inserted object.\n%s\n", dump);
1298 free (dump);
1433 return op; 1299 return op;
1434 } 1300 }
1435 1301
1436 if (op->more != NULL) 1302 if (op->more != NULL)
1437 { 1303 {
1478 if (op->nrof && !(flag & INS_NO_MERGE)) 1344 if (op->nrof && !(flag & INS_NO_MERGE))
1479 for (tmp = GET_MAP_OB (op->map, x, y); tmp != NULL; tmp = tmp->above) 1345 for (tmp = GET_MAP_OB (op->map, x, y); tmp != NULL; tmp = tmp->above)
1480 if (CAN_MERGE (op, tmp)) 1346 if (CAN_MERGE (op, tmp))
1481 { 1347 {
1482 op->nrof += tmp->nrof; 1348 op->nrof += tmp->nrof;
1483 remove_ob (tmp); 1349 tmp->remove ();
1484 free_object (tmp); 1350 tmp->destroy (0);
1485 } 1351 }
1486 1352
1487 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ 1353 CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */
1488 CLEAR_FLAG (op, FLAG_INV_LOCKED); 1354 CLEAR_FLAG (op, FLAG_INV_LOCKED);
1489 1355
1667 * op is the object to insert it under: supplies x and the map. 1533 * op is the object to insert it under: supplies x and the map.
1668 */ 1534 */
1669void 1535void
1670replace_insert_ob_in_map (const char *arch_string, object *op) 1536replace_insert_ob_in_map (const char *arch_string, object *op)
1671{ 1537{
1672 object *tmp; 1538 object *
1539 tmp;
1673 object *tmp1; 1540 object *
1541 tmp1;
1674 1542
1675 /* first search for itself and remove any old instances */ 1543 /* first search for itself and remove any old instances */
1676 1544
1677 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) 1545 for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above)
1678 { 1546 {
1679 if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */ 1547 if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */
1680 { 1548 {
1681 remove_ob (tmp); 1549 tmp->remove ();
1682 free_object (tmp); 1550 tmp->destroy (0);
1683 } 1551 }
1684 } 1552 }
1685 1553
1686 tmp1 = arch_to_object (find_archetype (arch_string)); 1554 tmp1 = arch_to_object (archetype::find (arch_string));
1687 1555
1688 tmp1->x = op->x; 1556 tmp1->x = op->x;
1689 tmp1->y = op->y; 1557 tmp1->y = op->y;
1690 insert_ob_in_map (tmp1, op->map, op, 0); 1558 insert_ob_in_map (tmp1, op->map, op, 0);
1691} 1559}
1709 if (orig_ob->nrof < nr) 1577 if (orig_ob->nrof < nr)
1710 { 1578 {
1711 sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name); 1579 sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name);
1712 return NULL; 1580 return NULL;
1713 } 1581 }
1582
1714 newob = object_create_clone (orig_ob); 1583 newob = object_create_clone (orig_ob);
1584
1715 if ((orig_ob->nrof -= nr) < 1) 1585 if ((orig_ob->nrof -= nr) < 1)
1716 { 1586 orig_ob->destroy (1);
1717 if (!is_removed)
1718 remove_ob (orig_ob);
1719 free_object2 (orig_ob, 1);
1720 }
1721 else if (!is_removed) 1587 else if (!is_removed)
1722 { 1588 {
1723 if (orig_ob->env != NULL) 1589 if (orig_ob->env != NULL)
1724 sub_weight (orig_ob->env, orig_ob->weight * nr); 1590 sub_weight (orig_ob->env, orig_ob->weight * nr);
1725 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_IN_MEMORY) 1591 if (orig_ob->env == NULL && orig_ob->map->in_memory != MAP_IN_MEMORY)
1727 strcpy (errmsg, "Tried to split object whose map is not in memory."); 1593 strcpy (errmsg, "Tried to split object whose map is not in memory.");
1728 LOG (llevDebug, "Error, Tried to split object whose map is not in memory.\n"); 1594 LOG (llevDebug, "Error, Tried to split object whose map is not in memory.\n");
1729 return NULL; 1595 return NULL;
1730 } 1596 }
1731 } 1597 }
1598
1732 newob->nrof = nr; 1599 newob->nrof = nr;
1733 1600
1734 return newob; 1601 return newob;
1735} 1602}
1736 1603
1743 */ 1610 */
1744 1611
1745object * 1612object *
1746decrease_ob_nr (object *op, uint32 i) 1613decrease_ob_nr (object *op, uint32 i)
1747{ 1614{
1748 object * 1615 object *tmp;
1749 tmp;
1750 player * 1616 player *pl;
1751 pl;
1752 1617
1753 if (i == 0) /* objects with op->nrof require this check */ 1618 if (i == 0) /* objects with op->nrof require this check */
1754 return op; 1619 return op;
1755 1620
1756 if (i > op->nrof) 1621 if (i > op->nrof)
1757 i = op->nrof; 1622 i = op->nrof;
1758 1623
1759 if (QUERY_FLAG (op, FLAG_REMOVED)) 1624 if (QUERY_FLAG (op, FLAG_REMOVED))
1760 {
1761 op->nrof -= i; 1625 op->nrof -= i;
1762 }
1763 else if (op->env != NULL) 1626 else if (op->env != NULL)
1764 { 1627 {
1765 /* is this object in the players inventory, or sub container 1628 /* is this object in the players inventory, or sub container
1766 * therein? 1629 * therein?
1767 */ 1630 */
1792 esrv_send_item (tmp, op); 1655 esrv_send_item (tmp, op);
1793 } 1656 }
1794 } 1657 }
1795 else 1658 else
1796 { 1659 {
1797 remove_ob (op); 1660 op->remove ();
1798 op->nrof = 0; 1661 op->nrof = 0;
1799 if (tmp) 1662 if (tmp)
1800 { 1663 {
1801 esrv_del_item (tmp->contr, op->count); 1664 esrv_del_item (tmp->contr, op->count);
1802 } 1665 }
1803 } 1666 }
1804 } 1667 }
1805 else 1668 else
1806 { 1669 {
1807 object *
1808 above = op->above; 1670 object *above = op->above;
1809 1671
1810 if (i < op->nrof) 1672 if (i < op->nrof)
1811 {
1812 op->nrof -= i; 1673 op->nrof -= i;
1813 }
1814 else 1674 else
1815 { 1675 {
1816 remove_ob (op); 1676 op->remove ();
1817 op->nrof = 0; 1677 op->nrof = 0;
1818 } 1678 }
1679
1819 /* Since we just removed op, op->above is null */ 1680 /* Since we just removed op, op->above is null */
1820 for (tmp = above; tmp != NULL; tmp = tmp->above) 1681 for (tmp = above; tmp != NULL; tmp = tmp->above)
1821 if (tmp->type == PLAYER) 1682 if (tmp->type == PLAYER)
1822 { 1683 {
1823 if (op->nrof) 1684 if (op->nrof)
1826 esrv_del_item (tmp->contr, op->count); 1687 esrv_del_item (tmp->contr, op->count);
1827 } 1688 }
1828 } 1689 }
1829 1690
1830 if (op->nrof) 1691 if (op->nrof)
1831 {
1832 return op; 1692 return op;
1833 }
1834 else 1693 else
1835 { 1694 {
1836 free_object (op); 1695 op->destroy (0);
1837 return NULL; 1696 return NULL;
1838 } 1697 }
1839} 1698}
1840 1699
1841/* 1700/*
1847add_weight (object *op, signed long weight) 1706add_weight (object *op, signed long weight)
1848{ 1707{
1849 while (op != NULL) 1708 while (op != NULL)
1850 { 1709 {
1851 if (op->type == CONTAINER) 1710 if (op->type == CONTAINER)
1852 {
1853 weight = (signed long) (weight * (100 - op->stats.Str) / 100); 1711 weight = (signed long) (weight * (100 - op->stats.Str) / 100);
1854 } 1712
1855 op->carrying += weight; 1713 op->carrying += weight;
1856 op = op->env; 1714 op = op->env;
1857 } 1715 }
1858} 1716}
1859 1717
1718object *
1719insert_ob_in_ob (object *op, object *where)
1720{
1721 if (!where)
1722 {
1723 char *dump = dump_object (op);
1724 LOG (llevError, "Trying to put object in NULL.\n%s\n", dump);
1725 free (dump);
1726 return op;
1727 }
1728
1729 if (where->head)
1730 {
1731 LOG (llevDebug, "Warning: Tried to insert object into wrong part of multipart object.\n");
1732 where = where->head;
1733 }
1734
1735 return where->insert (op);
1736}
1737
1860/* 1738/*
1861 * insert_ob_in_ob(op,environment): 1739 * env->insert (op)
1862 * This function inserts the object op in the linked list 1740 * This function inserts the object op in the linked list
1863 * inside the object environment. 1741 * inside the object environment.
1864 * 1742 *
1865 * Eneq(@csd.uu.se): Altered insert_ob_in_ob to make things picked up enter
1866 * the inventory at the last position or next to other objects of the same
1867 * type.
1868 * Frank: Now sorted by type, archetype and magic!
1869 *
1870 * The function returns now pointer to inserted item, and return value can 1743 * The function returns now pointer to inserted item, and return value can
1871 * be != op, if items are merged. -Tero 1744 * be != op, if items are merged. -Tero
1872 */ 1745 */
1873 1746
1874object * 1747object *
1875insert_ob_in_ob (object *op, object *where) 1748object::insert (object *op)
1876{ 1749{
1877 object * 1750 object *tmp, *otmp;
1878 tmp, *
1879 otmp;
1880 1751
1881 if (!QUERY_FLAG (op, FLAG_REMOVED)) 1752 if (!QUERY_FLAG (op, FLAG_REMOVED))
1882 { 1753 op->remove ();
1883 dump_object (op); 1754
1884 LOG (llevError, "Trying to insert (ob) inserted object.\n%s\n", errmsg);
1885 return op;
1886 }
1887 if (where == NULL)
1888 {
1889 dump_object (op);
1890 LOG (llevError, "Trying to put object in NULL.\n%s\n", errmsg);
1891 return op;
1892 }
1893 if (where->head)
1894 {
1895 LOG (llevDebug, "Warning: Tried to insert object wrong part of multipart object.\n");
1896 where = where->head;
1897 }
1898 if (op->more) 1755 if (op->more)
1899 { 1756 {
1900 LOG (llevError, "Tried to insert multipart object %s (%d)\n", &op->name, op->count); 1757 LOG (llevError, "Tried to insert multipart object %s (%d)\n", &op->name, op->count);
1901 return op; 1758 return op;
1902 } 1759 }
1760
1903 CLEAR_FLAG (op, FLAG_OBJ_ORIGINAL); 1761 CLEAR_FLAG (op, FLAG_OBJ_ORIGINAL);
1904 CLEAR_FLAG (op, FLAG_REMOVED); 1762 CLEAR_FLAG (op, FLAG_REMOVED);
1905 if (op->nrof) 1763 if (op->nrof)
1906 { 1764 {
1907 for (tmp = where->inv; tmp != NULL; tmp = tmp->below) 1765 for (tmp = inv; tmp != NULL; tmp = tmp->below)
1908 if (CAN_MERGE (tmp, op)) 1766 if (CAN_MERGE (tmp, op))
1909 { 1767 {
1910 /* return the original object and remove inserted object 1768 /* return the original object and remove inserted object
1911 (client needs the original object) */ 1769 (client needs the original object) */
1912 tmp->nrof += op->nrof; 1770 tmp->nrof += op->nrof;
1913 /* Weight handling gets pretty funky. Since we are adding to 1771 /* Weight handling gets pretty funky. Since we are adding to
1914 * tmp->nrof, we need to increase the weight. 1772 * tmp->nrof, we need to increase the weight.
1915 */ 1773 */
1916 add_weight (where, op->weight * op->nrof); 1774 add_weight (this, op->weight * op->nrof);
1917 SET_FLAG (op, FLAG_REMOVED); 1775 SET_FLAG (op, FLAG_REMOVED);
1918 free_object (op); /* free the inserted object */ 1776 op->destroy (); /* free the inserted object */
1919 op = tmp; 1777 op = tmp;
1920 remove_ob (op); /* and fix old object's links */ 1778 op->remove (); /* and fix old object's links */
1921 CLEAR_FLAG (op, FLAG_REMOVED); 1779 CLEAR_FLAG (op, FLAG_REMOVED);
1922 break; 1780 break;
1923 } 1781 }
1924 1782
1925 /* I assume combined objects have no inventory 1783 /* I assume combined objects have no inventory
1926 * We add the weight - this object could have just been removed 1784 * We add the weight - this object could have just been removed
1927 * (if it was possible to merge). calling remove_ob will subtract 1785 * (if it was possible to merge). calling remove_ob will subtract
1928 * the weight, so we need to add it in again, since we actually do 1786 * the weight, so we need to add it in again, since we actually do
1929 * the linking below 1787 * the linking below
1930 */ 1788 */
1931 add_weight (where, op->weight * op->nrof); 1789 add_weight (this, op->weight * op->nrof);
1932 } 1790 }
1933 else 1791 else
1934 add_weight (where, (op->weight + op->carrying)); 1792 add_weight (this, (op->weight + op->carrying));
1935 1793
1936 otmp = is_player_inv (where); 1794 otmp = is_player_inv (this);
1937 if (otmp && otmp->contr != NULL) 1795 if (otmp && otmp->contr)
1938 {
1939 if (!QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) 1796 if (!QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER))
1940 fix_player (otmp); 1797 fix_player (otmp);
1941 }
1942 1798
1943 op->map = NULL; 1799 op->map = NULL;
1944 op->env = where; 1800 op->env = this;
1945 op->above = NULL; 1801 op->above = NULL;
1946 op->below = NULL; 1802 op->below = NULL;
1947 op->x = 0, op->y = 0; 1803 op->x = 0, op->y = 0;
1948 1804
1949 /* reset the light list and los of the players on the map */ 1805 /* reset the light list and los of the players on the map */
1950 if ((op->glow_radius != 0) && where->map) 1806 if ((op->glow_radius != 0) && map)
1951 { 1807 {
1952#ifdef DEBUG_LIGHTS 1808#ifdef DEBUG_LIGHTS
1953 LOG (llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n", op->name); 1809 LOG (llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n", op->name);
1954#endif /* DEBUG_LIGHTS */ 1810#endif /* DEBUG_LIGHTS */
1955 if (MAP_DARKNESS (where->map)) 1811 if (MAP_DARKNESS (map))
1956 update_all_los (where->map, where->x, where->y); 1812 update_all_los (map, x, y);
1957 } 1813 }
1958 1814
1959 /* Client has no idea of ordering so lets not bother ordering it here. 1815 /* Client has no idea of ordering so lets not bother ordering it here.
1960 * It sure simplifies this function... 1816 * It sure simplifies this function...
1961 */ 1817 */
1962 if (where->inv == NULL) 1818 if (!inv)
1963 where->inv = op; 1819 inv = op;
1964 else 1820 else
1965 { 1821 {
1966 op->below = where->inv; 1822 op->below = inv;
1967 op->below->above = op; 1823 op->below->above = op;
1968 where->inv = op; 1824 inv = op;
1969 } 1825 }
1826
1970 return op; 1827 return op;
1971} 1828}
1972 1829
1973/* 1830/*
1974 * Checks if any objects has a move_type that matches objects 1831 * Checks if any objects has a move_type that matches objects
1993 1850
1994int 1851int
1995check_move_on (object *op, object *originator) 1852check_move_on (object *op, object *originator)
1996{ 1853{
1997 object *tmp; 1854 object *tmp;
1998 tag_t tag;
1999 mapstruct *m = op->map; 1855 maptile *m = op->map;
2000 int x = op->x, y = op->y; 1856 int x = op->x, y = op->y;
2001 1857
2002 MoveType move_on, move_slow, move_block; 1858 MoveType move_on, move_slow, move_block;
2003 1859
2004 if (QUERY_FLAG (op, FLAG_NO_APPLY)) 1860 if (QUERY_FLAG (op, FLAG_NO_APPLY))
2005 return 0; 1861 return 0;
2006
2007 tag = op->count;
2008 1862
2009 move_on = GET_MAP_MOVE_ON (op->map, op->x, op->y); 1863 move_on = GET_MAP_MOVE_ON (op->map, op->x, op->y);
2010 move_slow = GET_MAP_MOVE_SLOW (op->map, op->x, op->y); 1864 move_slow = GET_MAP_MOVE_SLOW (op->map, op->x, op->y);
2011 move_block = GET_MAP_MOVE_BLOCK (op->map, op->x, op->y); 1865 move_block = GET_MAP_MOVE_BLOCK (op->map, op->x, op->y);
2012 1866
2055 { 1909 {
2056 if ((!op->move_type && tmp->move_slow & MOVE_WALK) || 1910 if ((!op->move_type && tmp->move_slow & MOVE_WALK) ||
2057 ((op->move_type & tmp->move_slow) && (op->move_type & ~tmp->move_slow & ~tmp->move_block) == 0)) 1911 ((op->move_type & tmp->move_slow) && (op->move_type & ~tmp->move_slow & ~tmp->move_block) == 0))
2058 { 1912 {
2059 1913
1914 float
2060 float diff = tmp->move_slow_penalty * FABS (op->speed); 1915 diff = tmp->move_slow_penalty * FABS (op->speed);
2061 1916
2062 if (op->type == PLAYER) 1917 if (op->type == PLAYER)
2063 if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) || 1918 if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) ||
2064 (QUERY_FLAG (tmp, FLAG_IS_WOODED) && find_skill_by_number (op, SK_WOODSMAN))) 1919 (QUERY_FLAG (tmp, FLAG_IS_WOODED) && find_skill_by_number (op, SK_WOODSMAN)))
2065 diff /= 4.0; 1920 diff /= 4.0;
2072 if ((!op->move_type && tmp->move_on & MOVE_WALK) || 1927 if ((!op->move_type && tmp->move_on & MOVE_WALK) ||
2073 ((op->move_type & tmp->move_on) && (op->move_type & ~tmp->move_on & ~tmp->move_block) == 0)) 1928 ((op->move_type & tmp->move_on) && (op->move_type & ~tmp->move_on & ~tmp->move_block) == 0))
2074 { 1929 {
2075 move_apply (tmp, op, originator); 1930 move_apply (tmp, op, originator);
2076 1931
2077 if (was_destroyed (op, tag)) 1932 if (op->destroyed ())
2078 return 1; 1933 return 1;
2079 1934
2080 /* what the person/creature stepped onto has moved the object 1935 /* what the person/creature stepped onto has moved the object
2081 * someplace new. Don't process any further - if we did, 1936 * someplace new. Don't process any further - if we did,
2082 * have a feeling strange problems would result. 1937 * have a feeling strange problems would result.
2094 * a matching archetype at the given map and coordinates. 1949 * a matching archetype at the given map and coordinates.
2095 * The first matching object is returned, or NULL if none. 1950 * The first matching object is returned, or NULL if none.
2096 */ 1951 */
2097 1952
2098object * 1953object *
2099present_arch (const archetype *at, mapstruct *m, int x, int y) 1954present_arch (const archetype *at, maptile *m, int x, int y)
2100{ 1955{
2101 object * 1956 object *
2102 tmp; 1957 tmp;
2103 1958
2104 if (m == NULL || out_of_map (m, x, y)) 1959 if (m == NULL || out_of_map (m, x, y))
2117 * a matching type variable at the given map and coordinates. 1972 * a matching type variable at the given map and coordinates.
2118 * The first matching object is returned, or NULL if none. 1973 * The first matching object is returned, or NULL if none.
2119 */ 1974 */
2120 1975
2121object * 1976object *
2122present (unsigned char type, mapstruct *m, int x, int y) 1977present (unsigned char type, maptile *m, int x, int y)
2123{ 1978{
2124 object * 1979 object *
2125 tmp; 1980 tmp;
2126 1981
2127 if (out_of_map (m, x, y)) 1982 if (out_of_map (m, x, y))
2267 * the archetype because that isn't correct if the monster has been 2122 * the archetype because that isn't correct if the monster has been
2268 * customized, changed states, etc. 2123 * customized, changed states, etc.
2269 */ 2124 */
2270 2125
2271int 2126int
2272find_free_spot (const object *ob, mapstruct *m, int x, int y, int start, int stop) 2127find_free_spot (const object *ob, maptile *m, int x, int y, int start, int stop)
2273{ 2128{
2274 int 2129 int
2275 i, 2130 i,
2276 index = 0, flag; 2131 index = 0, flag;
2277 static int 2132 static int
2298 return -1; 2153 return -1;
2299 return altern[RANDOM () % index]; 2154 return altern[RANDOM () % index];
2300} 2155}
2301 2156
2302/* 2157/*
2303 * find_first_free_spot(archetype, mapstruct, x, y) works like 2158 * find_first_free_spot(archetype, maptile, x, y) works like
2304 * find_free_spot(), but it will search max number of squares. 2159 * find_free_spot(), but it will search max number of squares.
2305 * But it will return the first available spot, not a random choice. 2160 * But it will return the first available spot, not a random choice.
2306 * Changed 0.93.2: Have it return -1 if there is no free spot available. 2161 * Changed 0.93.2: Have it return -1 if there is no free spot available.
2307 */ 2162 */
2308 2163
2309int 2164int
2310find_first_free_spot (const object *ob, mapstruct *m, int x, int y) 2165find_first_free_spot (const object *ob, maptile *m, int x, int y)
2311{ 2166{
2312 int 2167 int
2313 i; 2168 i;
2314 2169
2315 for (i = 0; i < SIZEOFFREE; i++) 2170 for (i = 0; i < SIZEOFFREE; i++)
2379 * because we have to know what movement the thing looking to move 2234 * because we have to know what movement the thing looking to move
2380 * there is capable of. 2235 * there is capable of.
2381 */ 2236 */
2382 2237
2383int 2238int
2384find_dir (mapstruct *m, int x, int y, object *exclude) 2239find_dir (maptile *m, int x, int y, object *exclude)
2385{ 2240{
2386 int 2241 int
2387 i, 2242 i,
2388 max = SIZEOFFREE, mflags; 2243 max = SIZEOFFREE, mflags;
2389 sint16 2244
2390 nx, 2245 sint16 nx, ny;
2391 ny;
2392 object * 2246 object *
2393 tmp; 2247 tmp;
2394 mapstruct * 2248 maptile *
2395 mp; 2249 mp;
2396 MoveType 2250
2397 blocked, 2251 MoveType blocked, move_type;
2398 move_type;
2399 2252
2400 if (exclude && exclude->head) 2253 if (exclude && exclude->head)
2401 { 2254 {
2402 exclude = exclude->head; 2255 exclude = exclude->head;
2403 move_type = exclude->move_type; 2256 move_type = exclude->move_type;
2608 * Modified to be map tile aware -.MSW 2461 * Modified to be map tile aware -.MSW
2609 */ 2462 */
2610 2463
2611 2464
2612int 2465int
2613can_see_monsterP (mapstruct *m, int x, int y, int dir) 2466can_see_monsterP (maptile *m, int x, int y, int dir)
2614{ 2467{
2615 sint16 2468 sint16 dx, dy;
2616 dx,
2617 dy;
2618 int 2469 int
2619 mflags; 2470 mflags;
2620 2471
2621 if (dir < 0) 2472 if (dir < 0)
2622 return 0; /* exit condition: invalid direction */ 2473 return 0; /* exit condition: invalid direction */
2669 * create clone from object to another 2520 * create clone from object to another
2670 */ 2521 */
2671object * 2522object *
2672object_create_clone (object *asrc) 2523object_create_clone (object *asrc)
2673{ 2524{
2674 object *
2675 dst = NULL, *tmp, *src, *part, *prev, *item; 2525 object *dst = 0, *tmp, *src, *part, *prev, *item;
2676 2526
2677 if (!asrc) 2527 if (!asrc)
2678 return NULL; 2528 return 0;
2529
2679 src = asrc; 2530 src = asrc;
2680 if (src->head) 2531 if (src->head)
2681 src = src->head; 2532 src = src->head;
2682 2533
2683 prev = NULL; 2534 prev = 0;
2684 for (part = src; part; part = part->more) 2535 for (part = src; part; part = part->more)
2685 { 2536 {
2686 tmp = get_object (); 2537 tmp = get_object ();
2687 copy_object (part, tmp); 2538 copy_object (part, tmp);
2688 tmp->x -= src->x; 2539 tmp->x -= src->x;
2689 tmp->y -= src->y; 2540 tmp->y -= src->y;
2541
2690 if (!part->head) 2542 if (!part->head)
2691 { 2543 {
2692 dst = tmp; 2544 dst = tmp;
2693 tmp->head = NULL; 2545 tmp->head = 0;
2694 } 2546 }
2695 else 2547 else
2696 { 2548 {
2697 tmp->head = dst; 2549 tmp->head = dst;
2698 } 2550 }
2551
2699 tmp->more = NULL; 2552 tmp->more = 0;
2553
2700 if (prev) 2554 if (prev)
2701 prev->more = tmp; 2555 prev->more = tmp;
2556
2702 prev = tmp; 2557 prev = tmp;
2703 } 2558 }
2704 2559
2705 /*** copy inventory ***/
2706 for (item = src->inv; item; item = item->below) 2560 for (item = src->inv; item; item = item->below)
2707 {
2708 (void) insert_ob_in_ob (object_create_clone (item), dst); 2561 insert_ob_in_ob (object_create_clone (item), dst);
2709 }
2710 2562
2711 return dst; 2563 return dst;
2712} 2564}
2713 2565
2714/* return true if the object was destroyed, 0 otherwise */
2715int
2716was_destroyed (const object *op, tag_t old_tag)
2717{
2718 /* checking for FLAG_FREED isn't necessary, but makes this function more
2719 * robust */
2720 return op->count != old_tag || QUERY_FLAG (op, FLAG_FREED);
2721}
2722
2723/* GROS - Creates an object using a string representing its content. */ 2566/* GROS - Creates an object using a string representing its content. */
2724
2725/* Basically, we save the content of the string to a temp file, then call */ 2567/* Basically, we save the content of the string to a temp file, then call */
2726
2727/* load_object on it. I admit it is a highly inefficient way to make things, */ 2568/* load_object on it. I admit it is a highly inefficient way to make things, */
2728
2729/* but it was simple to make and allows reusing the load_object function. */ 2569/* but it was simple to make and allows reusing the load_object function. */
2730
2731/* Remember not to use load_object_str in a time-critical situation. */ 2570/* Remember not to use load_object_str in a time-critical situation. */
2732
2733/* Also remember that multiparts objects are not supported for now. */ 2571/* Also remember that multiparts objects are not supported for now. */
2734 2572
2735object * 2573object *
2736load_object_str (const char *obstr) 2574load_object_str (const char *obstr)
2737{ 2575{
2738 object * 2576 object *op;
2739 op;
2740 char
2741 filename[MAX_BUF]; 2577 char filename[MAX_BUF];
2742 2578
2743 sprintf (filename, "%s/cfloadobstr2044", settings.tmpdir); 2579 sprintf (filename, "%s/cfloadobstr2044", settings.tmpdir);
2744 2580
2745 FILE *
2746 tempfile = fopen (filename, "w"); 2581 FILE *tempfile = fopen (filename, "w");
2747 2582
2748 if (tempfile == NULL) 2583 if (tempfile == NULL)
2749 { 2584 {
2750 LOG (llevError, "Error - Unable to access load object temp file\n"); 2585 LOG (llevError, "Error - Unable to access load object temp file\n");
2751 return NULL; 2586 return NULL;
2752 }; 2587 }
2588
2753 fprintf (tempfile, obstr); 2589 fprintf (tempfile, obstr);
2754 fclose (tempfile); 2590 fclose (tempfile);
2755 2591
2756 op = get_object (); 2592 op = get_object ();
2757 2593
2758 object_thawer 2594 object_thawer thawer (filename);
2759 thawer (filename);
2760 2595
2761 if (thawer) 2596 if (thawer)
2762 load_object (thawer, op, 0); 2597 load_object (thawer, op, 0);
2763 2598
2764 LOG (llevDebug, " load str completed, object=%s\n", &op->name); 2599 LOG (llevDebug, " load str completed, object=%s\n", &op->name);
2772 * returns NULL if no match. 2607 * returns NULL if no match.
2773 */ 2608 */
2774object * 2609object *
2775find_obj_by_type_subtype (const object *who, int type, int subtype) 2610find_obj_by_type_subtype (const object *who, int type, int subtype)
2776{ 2611{
2777 object * 2612 object *tmp;
2778 tmp;
2779 2613
2780 for (tmp = who->inv; tmp; tmp = tmp->below) 2614 for (tmp = who->inv; tmp; tmp = tmp->below)
2781 if (tmp->type == type && tmp->subtype == subtype) 2615 if (tmp->type == type && tmp->subtype == subtype)
2782 return tmp; 2616 return tmp;
2783 2617
2791 * do the desired thing. 2625 * do the desired thing.
2792 */ 2626 */
2793key_value * 2627key_value *
2794get_ob_key_link (const object *ob, const char *key) 2628get_ob_key_link (const object *ob, const char *key)
2795{ 2629{
2796 key_value * 2630 key_value *link;
2797 link;
2798 2631
2799 for (link = ob->key_values; link != NULL; link = link->next) 2632 for (link = ob->key_values; link != NULL; link = link->next)
2800 {
2801 if (link->key == key) 2633 if (link->key == key)
2802 {
2803 return link; 2634 return link;
2804 }
2805 }
2806 2635
2807 return NULL; 2636 return NULL;
2808} 2637}
2809 2638
2810/* 2639/*
2815 * The returned string is shared. 2644 * The returned string is shared.
2816 */ 2645 */
2817const char * 2646const char *
2818get_ob_key_value (const object *op, const char *const key) 2647get_ob_key_value (const object *op, const char *const key)
2819{ 2648{
2820 key_value * 2649 key_value *link;
2821 link; 2650 shstr_cmp canonical_key (key);
2822 const char *
2823 canonical_key;
2824 2651
2825 canonical_key = shstr::find (key);
2826
2827 if (canonical_key == NULL) 2652 if (!canonical_key)
2828 { 2653 {
2829 /* 1. There being a field named key on any object 2654 /* 1. There being a field named key on any object
2830 * implies there'd be a shared string to find. 2655 * implies there'd be a shared string to find.
2831 * 2. Since there isn't, no object has this field. 2656 * 2. Since there isn't, no object has this field.
2832 * 3. Therefore, *this* object doesn't have this field. 2657 * 3. Therefore, *this* object doesn't have this field.
2833 */ 2658 */
2834 return NULL; 2659 return 0;
2835 } 2660 }
2836 2661
2837 /* This is copied from get_ob_key_link() above - 2662 /* This is copied from get_ob_key_link() above -
2838 * only 4 lines, and saves the function call overhead. 2663 * only 4 lines, and saves the function call overhead.
2839 */ 2664 */
2840 for (link = op->key_values; link != NULL; link = link->next) 2665 for (link = op->key_values; link; link = link->next)
2841 {
2842 if (link->key == canonical_key) 2666 if (link->key == canonical_key)
2843 {
2844 return link->value; 2667 return link->value;
2845 } 2668
2846 }
2847 return NULL; 2669 return 0;
2848} 2670}
2849 2671
2850 2672
2851/* 2673/*
2852 * Updates the canonical_key in op to value. 2674 * Updates the canonical_key in op to value.
2888 if (last) 2710 if (last)
2889 last->next = field->next; 2711 last->next = field->next;
2890 else 2712 else
2891 op->key_values = field->next; 2713 op->key_values = field->next;
2892 2714
2893 delete
2894 field; 2715 delete field;
2895 } 2716 }
2896 } 2717 }
2897 return TRUE; 2718 return TRUE;
2898 } 2719 }
2899 /* IF we get here, key doesn't exist */ 2720 /* IF we get here, key doesn't exist */
2934 * Returns TRUE on success. 2755 * Returns TRUE on success.
2935 */ 2756 */
2936int 2757int
2937set_ob_key_value (object *op, const char *key, const char *value, int add_key) 2758set_ob_key_value (object *op, const char *key, const char *value, int add_key)
2938{ 2759{
2939 shstr
2940 key_ (key); 2760 shstr key_ (key);
2941 2761
2942 return set_ob_key_value_s (op, key_, value, add_key); 2762 return set_ob_key_value_s (op, key_, value, add_key);
2943} 2763}
2764
2765object::depth_iterator::depth_iterator (object *container)
2766: iterator_base (container)
2767{
2768 while (item->inv)
2769 item = item->inv;
2770}
2771
2772void
2773object::depth_iterator::next ()
2774{
2775 if (item->below)
2776 {
2777 item = item->below;
2778
2779 while (item->inv)
2780 item = item->inv;
2781 }
2782 else
2783 item = item->env;
2784}
2785
2786// return a suitable string describing an objetc in enough detail to find it
2787const char *
2788object::debug_desc (char *info) const
2789{
2790 char info2[256 * 3];
2791 char *p = info;
2792
2793 p += snprintf (p, 256, "%d=\"%s%s%s\"",
2794 count,
2795 &name,
2796 title ? " " : "",
2797 title ? (const char *)title : "");
2798
2799 if (env)
2800 p += snprintf (p, 256, "(in %s)", env->debug_desc (info2));
2801
2802 if (map)
2803 p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y);
2804
2805 return info;
2806}
2807
2808const char *
2809object::debug_desc () const
2810{
2811 static char info[256 * 3];
2812 return debug_desc (info);
2813}
2814

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines