… | |
… | |
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 | |
… | |
… | |
434 | { |
515 | { |
435 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
516 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
436 | } |
517 | } |
437 | |
518 | |
438 | /* |
519 | /* |
439 | * Returns the object which this object marks as being the owner. |
|
|
440 | * A id-scheme is used to avoid pointing to objects which have been |
|
|
441 | * freed and are now reused. If this is detected, the owner is |
|
|
442 | * set to NULL, and NULL is returned. |
|
|
443 | * 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 |
|
|
445 | * 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. |
|
|
447 | */ |
|
|
448 | |
|
|
449 | object * |
|
|
450 | get_owner (object *op) |
|
|
451 | { |
|
|
452 | if (op->owner == NULL) |
|
|
453 | return NULL; |
|
|
454 | |
|
|
455 | if (!QUERY_FLAG (op->owner, FLAG_FREED) && !QUERY_FLAG (op->owner, FLAG_REMOVED) && op->owner->count == op->ownercount) |
|
|
456 | return op->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 | } |
|
|
475 | |
|
|
476 | /* |
|
|
477 | * Sets the owner and sets the skill and exp pointers to owner's current |
520 | * Sets the owner and sets the skill and exp pointers to owner's current |
478 | * skill and experience objects. |
521 | * skill and experience objects. |
479 | */ |
522 | */ |
480 | void |
523 | void |
481 | set_owner (object *op, object *owner) |
524 | object::set_owner (object *owner) |
482 | { |
525 | { |
483 | if (owner == NULL || op == NULL) |
526 | if (!owner) |
484 | return; |
527 | return; |
485 | |
528 | |
486 | /* next line added to allow objects which own objects */ |
529 | /* next line added to allow objects which own objects */ |
487 | /* Add a check for ownercounts in here, as I got into an endless loop |
530 | /* 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 |
531 | * with the fireball owning a poison cloud which then owned the |
489 | * fireball. I believe that was caused by one of the objects getting |
532 | * fireball. I believe that was caused by one of the objects getting |
490 | * freed and then another object replacing it. Since the ownercounts |
533 | * 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. |
534 | * didn't match, this check is valid and I believe that cause is valid. |
492 | */ |
535 | */ |
493 | while (owner->owner && owner != owner->owner && owner->ownercount == owner->owner->count) |
536 | while (owner->owner) |
494 | owner = owner->owner; |
537 | owner = owner->owner; |
495 | |
538 | |
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; |
539 | 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 | } |
540 | } |
536 | |
541 | |
537 | /* Zero the key_values on op, decrementing the shared-string |
542 | /* Zero the key_values on op, decrementing the shared-string |
538 | * refcounts and freeing the links. |
543 | * refcounts and freeing the links. |
539 | */ |
544 | */ |
… | |
… | |
555 | { |
560 | { |
556 | attachable_base::clear (); |
561 | attachable_base::clear (); |
557 | |
562 | |
558 | free_key_values (this); |
563 | free_key_values (this); |
559 | |
564 | |
560 | clear_owner (this); |
565 | owner = 0; |
561 | |
|
|
562 | name = 0; |
566 | name = 0; |
563 | name_pl = 0; |
567 | name_pl = 0; |
564 | title = 0; |
568 | title = 0; |
565 | race = 0; |
569 | race = 0; |
566 | slaying = 0; |
570 | slaying = 0; |
567 | skill = 0; |
571 | skill = 0; |
568 | msg = 0; |
572 | msg = 0; |
569 | lore = 0; |
573 | lore = 0; |
570 | custom_name = 0; |
574 | custom_name = 0; |
571 | materialname = 0; |
575 | materialname = 0; |
|
|
576 | contr = 0; |
|
|
577 | below = 0; |
|
|
578 | above = 0; |
|
|
579 | inv = 0; |
|
|
580 | container = 0; |
|
|
581 | env = 0; |
|
|
582 | more = 0; |
|
|
583 | head = 0; |
|
|
584 | map = 0; |
|
|
585 | active_next = 0; |
|
|
586 | active_prev = 0; |
572 | |
587 | |
573 | memset (static_cast < object_pod * >(this), 0, sizeof (object_pod)); |
588 | memset (static_cast<object_pod *>(this), 0, sizeof (object_pod)); |
574 | |
589 | |
575 | SET_FLAG (this, FLAG_REMOVED); |
590 | SET_FLAG (this, FLAG_REMOVED); |
|
|
591 | |
|
|
592 | /* What is not cleared is next, prev, and count */ |
|
|
593 | |
|
|
594 | expmul = 1.0; |
|
|
595 | face = blank_face; |
|
|
596 | |
|
|
597 | if (settings.casting_time) |
|
|
598 | casting_time = -1; |
576 | } |
599 | } |
577 | |
600 | |
578 | void object::clone (object *destination) |
601 | void object::clone (object *destination) |
579 | { |
602 | { |
580 | *(object_copy *) destination = *this; |
603 | *(object_copy *)destination = *this; |
581 | *(object_pod *) destination = *this; |
604 | *(object_pod *)destination = *this; |
582 | |
605 | |
583 | if (self || cb) |
606 | if (self || cb) |
584 | INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); |
607 | 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 | } |
608 | } |
618 | |
609 | |
619 | /* |
610 | /* |
620 | * copy object first frees everything allocated by the second object, |
611 | * copy object first frees everything allocated by the second object, |
621 | * and then copies the contends of the first object into the second |
612 | * and then copies the contends of the first object into the second |
622 | * object, allocating what needs to be allocated. Basically, any |
613 | * object, allocating what needs to be allocated. Basically, any |
623 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
614 | * 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 |
615 | * if the first object is freed, the pointers in the new object |
625 | * will point at garbage. |
616 | * will point at garbage. |
626 | */ |
617 | */ |
627 | |
|
|
628 | void |
618 | void |
629 | copy_object (object *op2, object *op) |
619 | copy_object (object *op2, object *op) |
630 | { |
620 | { |
631 | bool is_freed = QUERY_FLAG (op, FLAG_FREED); |
621 | bool is_freed = QUERY_FLAG (op, FLAG_FREED); |
632 | bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); |
622 | bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); |
… | |
… | |
692 | /* |
682 | /* |
693 | * Updates the speed of an object. If the speed changes from 0 to another |
683 | * 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. |
684 | * 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. |
685 | * This function needs to be called whenever the speed of an object changes. |
696 | */ |
686 | */ |
697 | |
|
|
698 | void |
687 | void |
699 | update_ob_speed (object *op) |
688 | update_ob_speed (object *op) |
700 | { |
689 | { |
701 | extern int arch_init; |
690 | extern int arch_init; |
702 | |
691 | |
… | |
… | |
711 | abort (); |
700 | abort (); |
712 | #else |
701 | #else |
713 | op->speed = 0; |
702 | op->speed = 0; |
714 | #endif |
703 | #endif |
715 | } |
704 | } |
|
|
705 | |
716 | if (arch_init) |
706 | if (arch_init) |
717 | { |
|
|
718 | return; |
707 | return; |
719 | } |
708 | |
720 | if (FABS (op->speed) > MIN_ACTIVE_SPEED) |
709 | if (FABS (op->speed) > MIN_ACTIVE_SPEED) |
721 | { |
710 | { |
722 | /* If already on active list, don't do anything */ |
711 | /* If already on active list, don't do anything */ |
723 | if (op->active_next || op->active_prev || op == active_objects) |
712 | if (op->active_next || op->active_prev || op == active_objects) |
724 | return; |
713 | return; |
725 | |
714 | |
726 | /* process_events() expects us to insert the object at the beginning |
715 | /* process_events() expects us to insert the object at the beginning |
727 | * of the list. */ |
716 | * of the list. */ |
728 | op->active_next = active_objects; |
717 | op->active_next = active_objects; |
|
|
718 | |
729 | if (op->active_next != NULL) |
719 | if (op->active_next != NULL) |
730 | op->active_next->active_prev = op; |
720 | op->active_next->active_prev = op; |
|
|
721 | |
731 | active_objects = op; |
722 | active_objects = op; |
732 | } |
723 | } |
733 | else |
724 | else |
734 | { |
725 | { |
735 | /* If not on the active list, nothing needs to be done */ |
726 | /* If not on the active list, nothing needs to be done */ |
… | |
… | |
737 | return; |
728 | return; |
738 | |
729 | |
739 | if (op->active_prev == NULL) |
730 | if (op->active_prev == NULL) |
740 | { |
731 | { |
741 | active_objects = op->active_next; |
732 | active_objects = op->active_next; |
|
|
733 | |
742 | if (op->active_next != NULL) |
734 | if (op->active_next != NULL) |
743 | op->active_next->active_prev = NULL; |
735 | op->active_next->active_prev = NULL; |
744 | } |
736 | } |
745 | else |
737 | else |
746 | { |
738 | { |
747 | op->active_prev->active_next = op->active_next; |
739 | op->active_prev->active_next = op->active_next; |
|
|
740 | |
748 | if (op->active_next) |
741 | if (op->active_next) |
749 | op->active_next->active_prev = op->active_prev; |
742 | op->active_next->active_prev = op->active_prev; |
750 | } |
743 | } |
|
|
744 | |
751 | op->active_next = NULL; |
745 | op->active_next = NULL; |
752 | op->active_prev = NULL; |
746 | op->active_prev = NULL; |
753 | } |
747 | } |
754 | } |
748 | } |
755 | |
749 | |
… | |
… | |
901 | |
895 | |
902 | if (op->more != NULL) |
896 | if (op->more != NULL) |
903 | update_object (op->more, action); |
897 | update_object (op->more, action); |
904 | } |
898 | } |
905 | |
899 | |
906 | static unordered_vector < object *>mortals; |
900 | static unordered_vector<object *> mortals; |
907 | static |
|
|
908 | std::vector < object *> |
|
|
909 | freed; |
|
|
910 | |
901 | |
911 | void object::free_mortals () |
902 | void object::free_mortals () |
912 | { |
903 | { |
913 | for (unordered_vector < object *>::iterator i = mortals.begin (); i != mortals.end ();) |
904 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
914 | if (!(*i)->refcount) |
905 | if ((*i)->refcnt) |
|
|
906 | ++i; // further delay freeing |
|
|
907 | else |
915 | { |
908 | { |
916 | freed.push_back (*i); |
909 | delete *i; |
917 | mortals.erase (i); |
910 | mortals.erase (i); |
918 | } |
911 | } |
919 | else |
912 | |
920 | ++i; |
913 | static int lastmortals = 0;//D |
|
|
914 | |
|
|
915 | if (mortals.size() != lastmortals)//D |
|
|
916 | { |
|
|
917 | lastmortals = mortals.size ();//D |
|
|
918 | LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D |
|
|
919 | } |
921 | } |
920 | } |
922 | |
921 | |
923 | object::object () |
922 | object::object () |
924 | { |
923 | { |
925 | SET_FLAG (this, FLAG_REMOVED); |
924 | SET_FLAG (this, FLAG_REMOVED); |
926 | |
925 | |
927 | expmul = 1.0; |
926 | expmul = 1.0; |
928 | face = blank_face; |
927 | face = blank_face; |
929 | attacked_by_count = -1; |
|
|
930 | } |
928 | } |
931 | |
929 | |
932 | object::~object () |
930 | object::~object () |
933 | { |
931 | { |
934 | free_key_values (this); |
932 | free_key_values (this); |
935 | } |
933 | } |
936 | |
934 | |
937 | void object::link () |
935 | void object::link () |
938 | { |
936 | { |
939 | count = ++ob_count; |
937 | count = ++ob_count; |
|
|
938 | uuid = gen_uuid (); |
940 | |
939 | |
941 | prev = 0; |
940 | prev = 0; |
942 | next = objects; |
941 | next = objects; |
943 | |
942 | |
944 | if (objects) |
943 | if (objects) |
… | |
… | |
947 | objects = this; |
946 | objects = this; |
948 | } |
947 | } |
949 | |
948 | |
950 | void object::unlink () |
949 | void object::unlink () |
951 | { |
950 | { |
952 | count = 0; |
951 | //count = 0;//D |
953 | |
952 | if (!prev && !next) return;//D |
954 | /* Remove this object from the list of used objects */ |
|
|
955 | if (prev) |
|
|
956 | { |
|
|
957 | prev->next = next; |
|
|
958 | prev = 0; |
|
|
959 | } |
|
|
960 | |
|
|
961 | if (next) |
|
|
962 | { |
|
|
963 | next->prev = prev; |
|
|
964 | next = 0; |
|
|
965 | } |
|
|
966 | |
953 | |
967 | if (this == objects) |
954 | if (this == objects) |
968 | objects = next; |
955 | objects = next; |
|
|
956 | |
|
|
957 | /* Remove this object from the list of used objects */ |
|
|
958 | if (prev) prev->next = next; |
|
|
959 | if (next) next->prev = prev; |
|
|
960 | |
|
|
961 | prev = 0; |
|
|
962 | next = 0; |
969 | } |
963 | } |
970 | |
964 | |
971 | object *object::create () |
965 | object *object::create () |
972 | { |
966 | { |
973 | object * |
|
|
974 | op; |
|
|
975 | |
|
|
976 | if (freed.empty ()) |
|
|
977 | op = new object; |
967 | object *op = new object; |
978 | |
|
|
979 | else |
|
|
980 | { |
|
|
981 | // highly annoying, but the only way to get it stable right now |
|
|
982 | op = freed.back (); |
|
|
983 | freed.pop_back (); |
|
|
984 | op->~object (); |
|
|
985 | new ((void *) op) object; |
|
|
986 | } |
|
|
987 | |
|
|
988 | op->link (); |
968 | op->link (); |
989 | return op; |
969 | return op; |
990 | } |
970 | } |
991 | |
971 | |
992 | /* |
972 | /* |
… | |
… | |
1002 | void object::free (bool free_inventory) |
982 | void object::free (bool free_inventory) |
1003 | { |
983 | { |
1004 | if (QUERY_FLAG (this, FLAG_FREED)) |
984 | if (QUERY_FLAG (this, FLAG_FREED)) |
1005 | return; |
985 | return; |
1006 | |
986 | |
|
|
987 | if (QUERY_FLAG (this, FLAG_FRIENDLY)) |
|
|
988 | remove_friendly_object (this); |
|
|
989 | |
1007 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
990 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
1008 | remove_ob (this); |
991 | remove_ob (this); |
1009 | |
|
|
1010 | if (QUERY_FLAG (this, FLAG_FRIENDLY)) |
|
|
1011 | remove_friendly_object (this); |
|
|
1012 | |
992 | |
1013 | SET_FLAG (this, FLAG_FREED); |
993 | SET_FLAG (this, FLAG_FREED); |
1014 | |
994 | |
1015 | if (more) |
995 | if (more) |
1016 | { |
996 | { |
… | |
… | |
1022 | { |
1002 | { |
1023 | /* Only if the space blocks everything do we not process - |
1003 | /* Only if the space blocks everything do we not process - |
1024 | * if some form of movement is allowed, let objects |
1004 | * if some form of movement is allowed, let objects |
1025 | * drop on that space. |
1005 | * drop on that space. |
1026 | */ |
1006 | */ |
1027 | if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) |
1007 | if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) |
1028 | { |
1008 | { |
1029 | object *op = inv; |
1009 | object *op = inv; |
1030 | |
1010 | |
1031 | while (op) |
1011 | while (op) |
1032 | { |
1012 | { |
… | |
… | |
1058 | op = tmp; |
1038 | op = tmp; |
1059 | } |
1039 | } |
1060 | } |
1040 | } |
1061 | } |
1041 | } |
1062 | |
1042 | |
1063 | clear_owner (this); |
1043 | // clear those pointers that likely might have circular references to us |
|
|
1044 | owner = 0; |
|
|
1045 | enemy = 0; |
|
|
1046 | attacked_by = 0; |
1064 | |
1047 | |
1065 | /* Remove object from the active list */ |
1048 | /* Remove object from the active list */ |
1066 | speed = 0; |
1049 | speed = 0; |
1067 | update_ob_speed (this); |
1050 | update_ob_speed (this); |
1068 | |
1051 | |
… | |
… | |
1288 | * This function goes through all objects below and including top, and |
1271 | * This function goes through all objects below and including top, and |
1289 | * merges op to the first matching object. |
1272 | * merges op to the first matching object. |
1290 | * If top is NULL, it is calculated. |
1273 | * If top is NULL, it is calculated. |
1291 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1274 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1292 | */ |
1275 | */ |
1293 | |
|
|
1294 | object * |
1276 | object * |
1295 | merge_ob (object *op, object *top) |
1277 | merge_ob (object *op, object *top) |
1296 | { |
1278 | { |
1297 | if (!op->nrof) |
1279 | if (!op->nrof) |
1298 | return 0; |
1280 | return 0; |
… | |
… | |
2719 | |
2701 | |
2720 | if (tempfile == NULL) |
2702 | if (tempfile == NULL) |
2721 | { |
2703 | { |
2722 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2704 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2723 | return NULL; |
2705 | return NULL; |
2724 | }; |
2706 | } |
|
|
2707 | |
2725 | fprintf (tempfile, obstr); |
2708 | fprintf (tempfile, obstr); |
2726 | fclose (tempfile); |
2709 | fclose (tempfile); |
2727 | |
2710 | |
2728 | op = get_object (); |
2711 | op = get_object (); |
2729 | |
2712 | |
… | |
… | |
2786 | * The returned string is shared. |
2769 | * The returned string is shared. |
2787 | */ |
2770 | */ |
2788 | const char * |
2771 | const char * |
2789 | get_ob_key_value (const object *op, const char *const key) |
2772 | get_ob_key_value (const object *op, const char *const key) |
2790 | { |
2773 | { |
2791 | key_value * |
2774 | key_value *link; |
2792 | link; |
2775 | shstr_cmp canonical_key (key); |
2793 | const char * |
|
|
2794 | canonical_key; |
|
|
2795 | |
2776 | |
2796 | canonical_key = shstr::find (key); |
|
|
2797 | |
|
|
2798 | if (canonical_key == NULL) |
2777 | if (!canonical_key) |
2799 | { |
2778 | { |
2800 | /* 1. There being a field named key on any object |
2779 | /* 1. There being a field named key on any object |
2801 | * implies there'd be a shared string to find. |
2780 | * implies there'd be a shared string to find. |
2802 | * 2. Since there isn't, no object has this field. |
2781 | * 2. Since there isn't, no object has this field. |
2803 | * 3. Therefore, *this* object doesn't have this field. |
2782 | * 3. Therefore, *this* object doesn't have this field. |
2804 | */ |
2783 | */ |
2805 | return NULL; |
2784 | return 0; |
2806 | } |
2785 | } |
2807 | |
2786 | |
2808 | /* This is copied from get_ob_key_link() above - |
2787 | /* This is copied from get_ob_key_link() above - |
2809 | * only 4 lines, and saves the function call overhead. |
2788 | * only 4 lines, and saves the function call overhead. |
2810 | */ |
2789 | */ |
2811 | for (link = op->key_values; link != NULL; link = link->next) |
2790 | for (link = op->key_values; link; link = link->next) |
2812 | { |
|
|
2813 | if (link->key == canonical_key) |
2791 | if (link->key == canonical_key) |
2814 | { |
|
|
2815 | return link->value; |
2792 | return link->value; |
2816 | } |
2793 | |
2817 | } |
|
|
2818 | return NULL; |
2794 | return 0; |
2819 | } |
2795 | } |
2820 | |
2796 | |
2821 | |
2797 | |
2822 | /* |
2798 | /* |
2823 | * Updates the canonical_key in op to value. |
2799 | * Updates the canonical_key in op to value. |
… | |
… | |
2908 | { |
2884 | { |
2909 | shstr key_ (key); |
2885 | shstr key_ (key); |
2910 | |
2886 | |
2911 | return set_ob_key_value_s (op, key_, value, add_key); |
2887 | return set_ob_key_value_s (op, key_, value, add_key); |
2912 | } |
2888 | } |
|
|
2889 | |
|
|
2890 | object::depth_iterator::depth_iterator (object *container) |
|
|
2891 | : iterator_base (container) |
|
|
2892 | { |
|
|
2893 | while (item->inv) |
|
|
2894 | item = item->inv; |
|
|
2895 | } |
|
|
2896 | |
|
|
2897 | void |
|
|
2898 | object::depth_iterator::next () |
|
|
2899 | { |
|
|
2900 | if (item->below) |
|
|
2901 | { |
|
|
2902 | item = item->below; |
|
|
2903 | |
|
|
2904 | while (item->inv) |
|
|
2905 | item = item->inv; |
|
|
2906 | } |
|
|
2907 | else |
|
|
2908 | item = item->env; |
|
|
2909 | } |
|
|
2910 | |
|
|
2911 | // return a suitable string describing an objetc in enough detail to find it |
|
|
2912 | const char * |
|
|
2913 | object::debug_desc (char *info) const |
|
|
2914 | { |
|
|
2915 | char info2[256 * 3]; |
|
|
2916 | char *p = info; |
|
|
2917 | |
|
|
2918 | p += snprintf (p, 256, "%d=\"%s%s%s\"", |
|
|
2919 | count, |
|
|
2920 | &name, |
|
|
2921 | title ? " " : "", |
|
|
2922 | title ? (const char *)title : ""); |
|
|
2923 | |
|
|
2924 | if (env) |
|
|
2925 | p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); |
|
|
2926 | |
|
|
2927 | if (map) |
|
|
2928 | p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y); |
|
|
2929 | |
|
|
2930 | return info; |
|
|
2931 | } |
|
|
2932 | |
|
|
2933 | const char * |
|
|
2934 | object::debug_desc () const |
|
|
2935 | { |
|
|
2936 | static char info[256 * 3]; |
|
|
2937 | return debug_desc (info); |
|
|
2938 | } |
|
|
2939 | |