… | |
… | |
29 | #include <stdio.h> |
29 | #include <stdio.h> |
30 | #include <sys/types.h> |
30 | #include <sys/types.h> |
31 | #include <sys/uio.h> |
31 | #include <sys/uio.h> |
32 | #include <object.h> |
32 | #include <object.h> |
33 | #include <funcpoint.h> |
33 | #include <funcpoint.h> |
34 | #include <skills.h> |
|
|
35 | #include <loader.h> |
34 | #include <loader.h> |
36 | |
35 | |
37 | int nrofallocobjects = 0; |
36 | int nrofallocobjects = 0; |
|
|
37 | static UUID uuid; |
|
|
38 | const uint64 UUID_SKIP = 1<<19; |
38 | |
39 | |
39 | object *objects; /* Pointer to the list of used objects */ |
40 | object *objects; /* Pointer to the list of used objects */ |
40 | object *active_objects; /* List of active objects that need to be processed */ |
41 | object *active_objects; /* List of active objects that need to be processed */ |
41 | |
42 | |
42 | short 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, |
43 | short 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, |
… | |
… | |
50 | }; |
51 | }; |
51 | int freedir[SIZEOFFREE] = { |
52 | int freedir[SIZEOFFREE] = { |
52 | 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, |
53 | 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, |
53 | 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 |
54 | 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 |
54 | }; |
55 | }; |
|
|
56 | |
|
|
57 | static void |
|
|
58 | write_uuid (void) |
|
|
59 | { |
|
|
60 | char filename1[MAX_BUF], filename2[MAX_BUF]; |
|
|
61 | |
|
|
62 | sprintf (filename1, "%s/uuid", settings.localdir); |
|
|
63 | sprintf (filename2, "%s/uuid~", settings.localdir); |
|
|
64 | |
|
|
65 | FILE *fp; |
|
|
66 | |
|
|
67 | if (!(fp = fopen (filename2, "w"))) |
|
|
68 | { |
|
|
69 | LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2); |
|
|
70 | return; |
|
|
71 | } |
|
|
72 | |
|
|
73 | fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2); |
|
|
74 | fclose (fp); |
|
|
75 | rename (filename2, filename1); |
|
|
76 | } |
|
|
77 | |
|
|
78 | static void |
|
|
79 | read_uuid (void) |
|
|
80 | { |
|
|
81 | char filename[MAX_BUF]; |
|
|
82 | |
|
|
83 | sprintf (filename, "%s/uuid", settings.localdir); |
|
|
84 | |
|
|
85 | FILE *fp; |
|
|
86 | |
|
|
87 | if (!(fp = fopen (filename, "r"))) |
|
|
88 | { |
|
|
89 | if (errno == ENOENT) |
|
|
90 | { |
|
|
91 | LOG (llevInfo, "RESET uid to 1\n"); |
|
|
92 | uuid.seq = 0; |
|
|
93 | write_uuid (); |
|
|
94 | return; |
|
|
95 | } |
|
|
96 | |
|
|
97 | LOG (llevError, "FATAL: cannot open %s for reading!\n", filename); |
|
|
98 | _exit (1); |
|
|
99 | } |
|
|
100 | |
|
|
101 | int version; |
|
|
102 | unsigned long long uid; |
|
|
103 | if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1) |
|
|
104 | { |
|
|
105 | LOG (llevError, "FATAL: error reading uid from %s!\n", filename); |
|
|
106 | _exit (1); |
|
|
107 | } |
|
|
108 | |
|
|
109 | uuid.seq = uid; |
|
|
110 | write_uuid (); |
|
|
111 | LOG (llevDebug, "read UID: %lld\n", uid); |
|
|
112 | fclose (fp); |
|
|
113 | } |
|
|
114 | |
|
|
115 | UUID |
|
|
116 | gen_uuid () |
|
|
117 | { |
|
|
118 | UUID uid; |
|
|
119 | |
|
|
120 | uid.seq = ++uuid.seq; |
|
|
121 | |
|
|
122 | if (!(uuid.seq & (UUID_SKIP - 1))) |
|
|
123 | write_uuid (); |
|
|
124 | |
|
|
125 | return uid; |
|
|
126 | } |
|
|
127 | |
|
|
128 | void |
|
|
129 | init_uuid () |
|
|
130 | { |
|
|
131 | read_uuid (); |
|
|
132 | } |
55 | |
133 | |
56 | /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ |
134 | /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ |
57 | static int |
135 | static int |
58 | compare_ob_value_lists_one (const object *wants, const object *has) |
136 | compare_ob_value_lists_one (const object *wants, const object *has) |
59 | { |
137 | { |
… | |
… | |
255 | { |
333 | { |
256 | if (inv->inv) |
334 | if (inv->inv) |
257 | sum_weight (inv); |
335 | sum_weight (inv); |
258 | sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); |
336 | sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); |
259 | } |
337 | } |
|
|
338 | |
260 | if (op->type == CONTAINER && op->stats.Str) |
339 | if (op->type == CONTAINER && op->stats.Str) |
261 | sum = (sum * (100 - op->stats.Str)) / 100; |
340 | sum = (sum * (100 - op->stats.Str)) / 100; |
|
|
341 | |
262 | if (op->carrying != sum) |
342 | if (op->carrying != sum) |
263 | op->carrying = sum; |
343 | op->carrying = sum; |
|
|
344 | |
264 | return sum; |
345 | return sum; |
265 | } |
346 | } |
266 | |
347 | |
267 | /** |
348 | /** |
268 | * Return the outermost environment object for a given object. |
349 | * Return the outermost environment object for a given object. |
… | |
… | |
417 | */ |
498 | */ |
418 | |
499 | |
419 | object * |
500 | object * |
420 | find_object_name (const char *str) |
501 | find_object_name (const char *str) |
421 | { |
502 | { |
422 | const char *name = shstr::find (str); |
503 | shstr_cmp str_ (str); |
423 | object *op; |
504 | object *op; |
424 | |
505 | |
425 | for (op = objects; op != NULL; op = op->next) |
506 | for (op = objects; op != NULL; op = op->next) |
426 | if (&op->name == name) |
507 | if (op->name == str_) |
427 | break; |
508 | break; |
428 | |
509 | |
429 | return op; |
510 | return op; |
430 | } |
511 | } |
431 | |
512 | |
… | |
… | |
443 | * Changed 2004-02-12 - if the player is setting at the play again |
524 | * Changed 2004-02-12 - if the player is setting at the play again |
444 | * prompt, he is removed, and we don't want to treat him as an owner of |
525 | * prompt, he is removed, and we don't want to treat him as an owner of |
445 | * anything, so check removed flag. I don't expect that this should break |
526 | * anything, so check removed flag. I don't expect that this should break |
446 | * anything - once an object is removed, it is basically dead anyways. |
527 | * anything - once an object is removed, it is basically dead anyways. |
447 | */ |
528 | */ |
448 | |
|
|
449 | object * |
529 | object * |
450 | get_owner (object *op) |
530 | object::get_owner () |
451 | { |
531 | { |
452 | if (op->owner == NULL) |
532 | if (!owner |
453 | return NULL; |
533 | || QUERY_FLAG (owner, FLAG_FREED) |
|
|
534 | || QUERY_FLAG (owner, FLAG_REMOVED)) |
|
|
535 | owner = 0; |
454 | |
536 | |
455 | if (!QUERY_FLAG (op->owner, FLAG_FREED) && !QUERY_FLAG (op->owner, FLAG_REMOVED) && op->owner->count == op->ownercount) |
|
|
456 | return op->owner; |
537 | return owner; |
457 | |
|
|
458 | op->owner = NULL; |
|
|
459 | op->ownercount = 0; |
|
|
460 | return NULL; |
|
|
461 | } |
|
|
462 | |
|
|
463 | void |
|
|
464 | clear_owner (object *op) |
|
|
465 | { |
|
|
466 | if (!op) |
|
|
467 | return; |
|
|
468 | |
|
|
469 | if (op->owner && op->ownercount == op->owner->count) |
|
|
470 | op->owner->refcount--; |
|
|
471 | |
|
|
472 | op->owner = NULL; |
|
|
473 | op->ownercount = 0; |
|
|
474 | } |
538 | } |
475 | |
539 | |
476 | /* |
540 | /* |
477 | * Sets the owner and sets the skill and exp pointers to owner's current |
541 | * Sets the owner and sets the skill and exp pointers to owner's current |
478 | * skill and experience objects. |
542 | * skill and experience objects. |
479 | */ |
543 | */ |
480 | void |
544 | void |
481 | set_owner (object *op, object *owner) |
545 | object::set_owner (object *owner) |
482 | { |
546 | { |
483 | if (owner == NULL || op == NULL) |
547 | if (!owner) |
484 | return; |
548 | return; |
485 | |
549 | |
486 | /* next line added to allow objects which own objects */ |
550 | /* next line added to allow objects which own objects */ |
487 | /* Add a check for ownercounts in here, as I got into an endless loop |
551 | /* Add a check for ownercounts in here, as I got into an endless loop |
488 | * with the fireball owning a poison cloud which then owned the |
552 | * with the fireball owning a poison cloud which then owned the |
489 | * fireball. I believe that was caused by one of the objects getting |
553 | * fireball. I believe that was caused by one of the objects getting |
490 | * freed and then another object replacing it. Since the ownercounts |
554 | * freed and then another object replacing it. Since the ownercounts |
491 | * didn't match, this check is valid and I believe that cause is valid. |
555 | * didn't match, this check is valid and I believe that cause is valid. |
492 | */ |
556 | */ |
493 | while (owner->owner && owner != owner->owner && owner->ownercount == owner->owner->count) |
557 | while (owner->owner) |
494 | owner = owner->owner; |
558 | owner = owner->owner; |
495 | |
559 | |
496 | /* IF the owner still has an owner, we did not resolve to a final owner. |
|
|
497 | * so lets not add to that. |
|
|
498 | */ |
|
|
499 | if (owner->owner) |
|
|
500 | return; |
|
|
501 | |
|
|
502 | op->owner = owner; |
560 | this->owner = owner; |
503 | |
|
|
504 | op->ownercount = owner->count; |
|
|
505 | owner->refcount++; |
|
|
506 | } |
|
|
507 | |
|
|
508 | /* Set the owner to clone's current owner and set the skill and experience |
|
|
509 | * objects to clone's objects (typically those objects that where the owner's |
|
|
510 | * current skill and experience objects at the time when clone's owner was |
|
|
511 | * set - not the owner's current skill and experience objects). |
|
|
512 | * |
|
|
513 | * Use this function if player created an object (e.g. fire bullet, swarm |
|
|
514 | * spell), and this object creates further objects whose kills should be |
|
|
515 | * accounted for the player's original skill, even if player has changed |
|
|
516 | * skills meanwhile. |
|
|
517 | */ |
|
|
518 | void |
|
|
519 | copy_owner (object *op, object *clone) |
|
|
520 | { |
|
|
521 | object *owner = get_owner (clone); |
|
|
522 | |
|
|
523 | if (owner == NULL) |
|
|
524 | { |
|
|
525 | /* players don't have owners - they own themselves. Update |
|
|
526 | * as appropriate. |
|
|
527 | */ |
|
|
528 | if (clone->type == PLAYER) |
|
|
529 | owner = clone; |
|
|
530 | else |
|
|
531 | return; |
|
|
532 | } |
|
|
533 | |
|
|
534 | set_owner (op, owner); |
|
|
535 | } |
561 | } |
536 | |
562 | |
537 | /* Zero the key_values on op, decrementing the shared-string |
563 | /* Zero the key_values on op, decrementing the shared-string |
538 | * refcounts and freeing the links. |
564 | * refcounts and freeing the links. |
539 | */ |
565 | */ |
… | |
… | |
555 | { |
581 | { |
556 | attachable_base::clear (); |
582 | attachable_base::clear (); |
557 | |
583 | |
558 | free_key_values (this); |
584 | free_key_values (this); |
559 | |
585 | |
560 | clear_owner (this); |
586 | owner = 0; |
561 | |
|
|
562 | name = 0; |
587 | name = 0; |
563 | name_pl = 0; |
588 | name_pl = 0; |
564 | title = 0; |
589 | title = 0; |
565 | race = 0; |
590 | race = 0; |
566 | slaying = 0; |
591 | slaying = 0; |
567 | skill = 0; |
592 | skill = 0; |
568 | msg = 0; |
593 | msg = 0; |
569 | lore = 0; |
594 | lore = 0; |
570 | custom_name = 0; |
595 | custom_name = 0; |
571 | materialname = 0; |
596 | materialname = 0; |
|
|
597 | contr = 0; |
|
|
598 | below = 0; |
|
|
599 | above = 0; |
|
|
600 | inv = 0; |
|
|
601 | container = 0; |
|
|
602 | env = 0; |
|
|
603 | more = 0; |
|
|
604 | head = 0; |
|
|
605 | map = 0; |
|
|
606 | active_next = 0; |
|
|
607 | active_prev = 0; |
572 | |
608 | |
573 | memset (static_cast < object_pod * >(this), 0, sizeof (object_pod)); |
609 | memset (static_cast<object_pod *>(this), 0, sizeof (object_pod)); |
574 | |
610 | |
575 | SET_FLAG (this, FLAG_REMOVED); |
611 | SET_FLAG (this, FLAG_REMOVED); |
|
|
612 | |
|
|
613 | /* What is not cleared is next, prev, and count */ |
|
|
614 | |
|
|
615 | expmul = 1.0; |
|
|
616 | face = blank_face; |
|
|
617 | attacked_by_count = -1; |
|
|
618 | |
|
|
619 | if (settings.casting_time) |
|
|
620 | casting_time = -1; |
576 | } |
621 | } |
577 | |
622 | |
578 | void object::clone (object *destination) |
623 | void object::clone (object *destination) |
579 | { |
624 | { |
580 | *(object_copy *) destination = *this; |
625 | *(object_copy *)destination = *this; |
581 | *(object_pod *) destination = *this; |
626 | *(object_pod *)destination = *this; |
582 | |
627 | |
583 | if (self || cb) |
628 | if (self || cb) |
584 | INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); |
629 | INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); |
585 | } |
|
|
586 | |
|
|
587 | /* |
|
|
588 | * clear_object() frees everything allocated by an object, and also |
|
|
589 | * clears all variables and flags to default settings. |
|
|
590 | */ |
|
|
591 | |
|
|
592 | void |
|
|
593 | clear_object (object *op) |
|
|
594 | { |
|
|
595 | op->clear (); |
|
|
596 | |
|
|
597 | op->contr = NULL; |
|
|
598 | op->below = NULL; |
|
|
599 | op->above = NULL; |
|
|
600 | op->inv = NULL; |
|
|
601 | op->container = NULL; |
|
|
602 | op->env = NULL; |
|
|
603 | op->more = NULL; |
|
|
604 | op->head = NULL; |
|
|
605 | op->map = NULL; |
|
|
606 | op->refcount = 0; |
|
|
607 | op->active_next = NULL; |
|
|
608 | op->active_prev = NULL; |
|
|
609 | /* What is not cleared is next, prev, and count */ |
|
|
610 | |
|
|
611 | op->expmul = 1.0; |
|
|
612 | op->face = blank_face; |
|
|
613 | op->attacked_by_count = -1; |
|
|
614 | |
|
|
615 | if (settings.casting_time) |
|
|
616 | op->casting_time = -1; |
|
|
617 | } |
630 | } |
618 | |
631 | |
619 | /* |
632 | /* |
620 | * copy object first frees everything allocated by the second object, |
633 | * copy object first frees everything allocated by the second object, |
621 | * and then copies the contends of the first object into the second |
634 | * and then copies the contends of the first object into the second |
622 | * object, allocating what needs to be allocated. Basically, any |
635 | * object, allocating what needs to be allocated. Basically, any |
623 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
636 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
624 | * if the first object is freed, the pointers in the new object |
637 | * if the first object is freed, the pointers in the new object |
625 | * will point at garbage. |
638 | * will point at garbage. |
626 | */ |
639 | */ |
627 | |
|
|
628 | void |
640 | void |
629 | copy_object (object *op2, object *op) |
641 | copy_object (object *op2, object *op) |
630 | { |
642 | { |
631 | bool is_freed = QUERY_FLAG (op, FLAG_FREED); |
643 | bool is_freed = QUERY_FLAG (op, FLAG_FREED); |
632 | bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); |
644 | bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); |
… | |
… | |
692 | /* |
704 | /* |
693 | * Updates the speed of an object. If the speed changes from 0 to another |
705 | * Updates the speed of an object. If the speed changes from 0 to another |
694 | * value, or vice versa, then add/remove the object from the active list. |
706 | * value, or vice versa, then add/remove the object from the active list. |
695 | * This function needs to be called whenever the speed of an object changes. |
707 | * This function needs to be called whenever the speed of an object changes. |
696 | */ |
708 | */ |
697 | |
|
|
698 | void |
709 | void |
699 | update_ob_speed (object *op) |
710 | update_ob_speed (object *op) |
700 | { |
711 | { |
701 | extern int arch_init; |
712 | extern int arch_init; |
702 | |
713 | |
… | |
… | |
711 | abort (); |
722 | abort (); |
712 | #else |
723 | #else |
713 | op->speed = 0; |
724 | op->speed = 0; |
714 | #endif |
725 | #endif |
715 | } |
726 | } |
|
|
727 | |
716 | if (arch_init) |
728 | if (arch_init) |
717 | { |
|
|
718 | return; |
729 | return; |
719 | } |
730 | |
720 | if (FABS (op->speed) > MIN_ACTIVE_SPEED) |
731 | if (FABS (op->speed) > MIN_ACTIVE_SPEED) |
721 | { |
732 | { |
722 | /* If already on active list, don't do anything */ |
733 | /* If already on active list, don't do anything */ |
723 | if (op->active_next || op->active_prev || op == active_objects) |
734 | if (op->active_next || op->active_prev || op == active_objects) |
724 | return; |
735 | return; |
725 | |
736 | |
726 | /* process_events() expects us to insert the object at the beginning |
737 | /* process_events() expects us to insert the object at the beginning |
727 | * of the list. */ |
738 | * of the list. */ |
728 | op->active_next = active_objects; |
739 | op->active_next = active_objects; |
|
|
740 | |
729 | if (op->active_next != NULL) |
741 | if (op->active_next != NULL) |
730 | op->active_next->active_prev = op; |
742 | op->active_next->active_prev = op; |
|
|
743 | |
731 | active_objects = op; |
744 | active_objects = op; |
732 | } |
745 | } |
733 | else |
746 | else |
734 | { |
747 | { |
735 | /* If not on the active list, nothing needs to be done */ |
748 | /* If not on the active list, nothing needs to be done */ |
… | |
… | |
737 | return; |
750 | return; |
738 | |
751 | |
739 | if (op->active_prev == NULL) |
752 | if (op->active_prev == NULL) |
740 | { |
753 | { |
741 | active_objects = op->active_next; |
754 | active_objects = op->active_next; |
|
|
755 | |
742 | if (op->active_next != NULL) |
756 | if (op->active_next != NULL) |
743 | op->active_next->active_prev = NULL; |
757 | op->active_next->active_prev = NULL; |
744 | } |
758 | } |
745 | else |
759 | else |
746 | { |
760 | { |
747 | op->active_prev->active_next = op->active_next; |
761 | op->active_prev->active_next = op->active_next; |
|
|
762 | |
748 | if (op->active_next) |
763 | if (op->active_next) |
749 | op->active_next->active_prev = op->active_prev; |
764 | op->active_next->active_prev = op->active_prev; |
750 | } |
765 | } |
|
|
766 | |
751 | op->active_next = NULL; |
767 | op->active_next = NULL; |
752 | op->active_prev = NULL; |
768 | op->active_prev = NULL; |
753 | } |
769 | } |
754 | } |
770 | } |
755 | |
771 | |
… | |
… | |
901 | |
917 | |
902 | if (op->more != NULL) |
918 | if (op->more != NULL) |
903 | update_object (op->more, action); |
919 | update_object (op->more, action); |
904 | } |
920 | } |
905 | |
921 | |
906 | static unordered_vector < object *>mortals; |
922 | static unordered_vector<object *> mortals; |
907 | static |
923 | static std::vector<object *, slice_allocator <object *> > freed; |
908 | std::vector < object *> |
|
|
909 | freed; |
|
|
910 | |
924 | |
911 | void object::free_mortals () |
925 | void object::free_mortals () |
912 | { |
926 | { |
913 | for (unordered_vector < object *>::iterator i = mortals.begin (); i != mortals.end ();) |
927 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
914 | if (!(*i)->refcount) |
928 | if ((*i)->refcnt) |
|
|
929 | ++i; // further delay freeing |
|
|
930 | else |
915 | { |
931 | { |
916 | freed.push_back (*i); |
932 | //freed.push_back (*i);//D |
|
|
933 | delete *i; |
917 | mortals.erase (i); |
934 | mortals.erase (i); |
918 | } |
935 | } |
919 | else |
936 | |
920 | ++i; |
937 | if (mortals.size() && 0)//D |
|
|
938 | LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D |
921 | } |
939 | } |
922 | |
940 | |
923 | object::object () |
941 | object::object () |
924 | { |
942 | { |
925 | SET_FLAG (this, FLAG_REMOVED); |
943 | SET_FLAG (this, FLAG_REMOVED); |
… | |
… | |
948 | } |
966 | } |
949 | |
967 | |
950 | void object::unlink () |
968 | void object::unlink () |
951 | { |
969 | { |
952 | count = 0; |
970 | count = 0; |
|
|
971 | uuid = gen_uuid (); |
|
|
972 | |
|
|
973 | if (this == objects) |
|
|
974 | objects = next; |
953 | |
975 | |
954 | /* Remove this object from the list of used objects */ |
976 | /* Remove this object from the list of used objects */ |
955 | if (prev) |
977 | if (prev) |
956 | { |
978 | { |
957 | prev->next = next; |
979 | prev->next = next; |
… | |
… | |
961 | if (next) |
983 | if (next) |
962 | { |
984 | { |
963 | next->prev = prev; |
985 | next->prev = prev; |
964 | next = 0; |
986 | next = 0; |
965 | } |
987 | } |
966 | |
|
|
967 | if (this == objects) |
|
|
968 | objects = next; |
|
|
969 | } |
988 | } |
970 | |
989 | |
971 | object *object::create () |
990 | object *object::create () |
972 | { |
991 | { |
973 | object * |
992 | object *op; |
974 | op; |
|
|
975 | |
993 | |
976 | if (freed.empty ()) |
994 | if (freed.empty ()) |
977 | op = new object; |
995 | op = new object; |
978 | |
|
|
979 | else |
996 | else |
980 | { |
997 | { |
981 | // highly annoying, but the only way to get it stable right now |
998 | // highly annoying, but the only way to get it stable right now |
982 | op = freed.back (); |
999 | op = freed.back (); |
983 | freed.pop_back (); |
1000 | freed.pop_back (); |
… | |
… | |
1002 | void object::free (bool free_inventory) |
1019 | void object::free (bool free_inventory) |
1003 | { |
1020 | { |
1004 | if (QUERY_FLAG (this, FLAG_FREED)) |
1021 | if (QUERY_FLAG (this, FLAG_FREED)) |
1005 | return; |
1022 | return; |
1006 | |
1023 | |
|
|
1024 | if (QUERY_FLAG (this, FLAG_FRIENDLY)) |
|
|
1025 | remove_friendly_object (this); |
|
|
1026 | |
1007 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
1027 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
1008 | remove_ob (this); |
1028 | remove_ob (this); |
1009 | |
|
|
1010 | if (QUERY_FLAG (this, FLAG_FRIENDLY)) |
|
|
1011 | remove_friendly_object (this); |
|
|
1012 | |
1029 | |
1013 | SET_FLAG (this, FLAG_FREED); |
1030 | SET_FLAG (this, FLAG_FREED); |
1014 | |
1031 | |
1015 | if (more) |
1032 | if (more) |
1016 | { |
1033 | { |
… | |
… | |
1022 | { |
1039 | { |
1023 | /* Only if the space blocks everything do we not process - |
1040 | /* Only if the space blocks everything do we not process - |
1024 | * if some form of movement is allowed, let objects |
1041 | * if some form of movement is allowed, let objects |
1025 | * drop on that space. |
1042 | * drop on that space. |
1026 | */ |
1043 | */ |
1027 | if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) |
1044 | if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) |
1028 | { |
1045 | { |
1029 | object *op = inv; |
1046 | object *op = inv; |
1030 | |
1047 | |
1031 | while (op) |
1048 | while (op) |
1032 | { |
1049 | { |
… | |
… | |
1058 | op = tmp; |
1075 | op = tmp; |
1059 | } |
1076 | } |
1060 | } |
1077 | } |
1061 | } |
1078 | } |
1062 | |
1079 | |
1063 | clear_owner (this); |
1080 | owner = 0; |
1064 | |
1081 | |
1065 | /* Remove object from the active list */ |
1082 | /* Remove object from the active list */ |
1066 | speed = 0; |
1083 | speed = 0; |
1067 | update_ob_speed (this); |
1084 | update_ob_speed (this); |
1068 | |
1085 | |
… | |
… | |
1288 | * This function goes through all objects below and including top, and |
1305 | * This function goes through all objects below and including top, and |
1289 | * merges op to the first matching object. |
1306 | * merges op to the first matching object. |
1290 | * If top is NULL, it is calculated. |
1307 | * If top is NULL, it is calculated. |
1291 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1308 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1292 | */ |
1309 | */ |
1293 | |
|
|
1294 | object * |
1310 | object * |
1295 | merge_ob (object *op, object *top) |
1311 | merge_ob (object *op, object *top) |
1296 | { |
1312 | { |
1297 | if (!op->nrof) |
1313 | if (!op->nrof) |
1298 | return 0; |
1314 | return 0; |
… | |
… | |
2786 | * The returned string is shared. |
2802 | * The returned string is shared. |
2787 | */ |
2803 | */ |
2788 | const char * |
2804 | const char * |
2789 | get_ob_key_value (const object *op, const char *const key) |
2805 | get_ob_key_value (const object *op, const char *const key) |
2790 | { |
2806 | { |
2791 | key_value * |
2807 | key_value *link; |
2792 | link; |
2808 | shstr_cmp canonical_key (key); |
2793 | const char * |
|
|
2794 | canonical_key; |
|
|
2795 | |
2809 | |
2796 | canonical_key = shstr::find (key); |
|
|
2797 | |
|
|
2798 | if (canonical_key == NULL) |
2810 | if (!canonical_key) |
2799 | { |
2811 | { |
2800 | /* 1. There being a field named key on any object |
2812 | /* 1. There being a field named key on any object |
2801 | * implies there'd be a shared string to find. |
2813 | * implies there'd be a shared string to find. |
2802 | * 2. Since there isn't, no object has this field. |
2814 | * 2. Since there isn't, no object has this field. |
2803 | * 3. Therefore, *this* object doesn't have this field. |
2815 | * 3. Therefore, *this* object doesn't have this field. |
2804 | */ |
2816 | */ |
2805 | return NULL; |
2817 | return 0; |
2806 | } |
2818 | } |
2807 | |
2819 | |
2808 | /* This is copied from get_ob_key_link() above - |
2820 | /* This is copied from get_ob_key_link() above - |
2809 | * only 4 lines, and saves the function call overhead. |
2821 | * only 4 lines, and saves the function call overhead. |
2810 | */ |
2822 | */ |
2811 | for (link = op->key_values; link != NULL; link = link->next) |
2823 | for (link = op->key_values; link; link = link->next) |
2812 | { |
|
|
2813 | if (link->key == canonical_key) |
2824 | if (link->key == canonical_key) |
2814 | { |
|
|
2815 | return link->value; |
2825 | return link->value; |
2816 | } |
2826 | |
2817 | } |
|
|
2818 | return NULL; |
2827 | return 0; |
2819 | } |
2828 | } |
2820 | |
2829 | |
2821 | |
2830 | |
2822 | /* |
2831 | /* |
2823 | * Updates the canonical_key in op to value. |
2832 | * Updates the canonical_key in op to value. |
… | |
… | |
2908 | { |
2917 | { |
2909 | shstr key_ (key); |
2918 | shstr key_ (key); |
2910 | |
2919 | |
2911 | return set_ob_key_value_s (op, key_, value, add_key); |
2920 | return set_ob_key_value_s (op, key_, value, add_key); |
2912 | } |
2921 | } |
|
|
2922 | |
|
|
2923 | object::depth_iterator::depth_iterator (object *container) |
|
|
2924 | : iterator_base (container) |
|
|
2925 | { |
|
|
2926 | while (item->inv) |
|
|
2927 | item = item->inv; |
|
|
2928 | } |
|
|
2929 | |
|
|
2930 | void |
|
|
2931 | object::depth_iterator::next () |
|
|
2932 | { |
|
|
2933 | if (item->below) |
|
|
2934 | { |
|
|
2935 | item = item->below; |
|
|
2936 | |
|
|
2937 | while (item->inv) |
|
|
2938 | item = item->inv; |
|
|
2939 | } |
|
|
2940 | else |
|
|
2941 | item = item->env; |
|
|
2942 | } |
|
|
2943 | |
|
|
2944 | // return a suitable string describing an objetc in enough detail to find it |
|
|
2945 | const char * |
|
|
2946 | object::debug_desc (char *info) const |
|
|
2947 | { |
|
|
2948 | char info2[256 * 3]; |
|
|
2949 | char *p = info; |
|
|
2950 | |
|
|
2951 | p += snprintf (p, 256, "%d=\"%s%s%s\"", |
|
|
2952 | count, |
|
|
2953 | &name, |
|
|
2954 | title ? " " : "", |
|
|
2955 | title ? (const char *)title : ""); |
|
|
2956 | |
|
|
2957 | if (env) |
|
|
2958 | p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); |
|
|
2959 | |
|
|
2960 | if (map) |
|
|
2961 | p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y); |
|
|
2962 | |
|
|
2963 | return info; |
|
|
2964 | } |
|
|
2965 | |
|
|
2966 | const char * |
|
|
2967 | object::debug_desc () const |
|
|
2968 | { |
|
|
2969 | static char info[256 * 3]; |
|
|
2970 | return debug_desc (info); |
|
|
2971 | } |
|
|
2972 | |