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.30 by root, Mon Sep 11 20:28:37 2006 UTC vs.
Revision 1.44 by root, Thu Sep 14 17:29:27 2006 UTC

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
37int nrofallocobjects = 0; 36int nrofallocobjects = 0;
37static UUID uuid;
38const uint64 UUID_SKIP = 1<<19;
38 39
39object *objects; /* Pointer to the list of used objects */ 40object *objects; /* Pointer to the list of used objects */
40object *active_objects; /* List of active objects that need to be processed */ 41object *active_objects; /* List of active objects that need to be processed */
41 42
42short 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, 43short 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};
51int freedir[SIZEOFFREE] = { 52int 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
57static void
58write_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
78static void
79read_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
115UUID
116gen_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
128void
129init_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. */
57static int 135static int
58compare_ob_value_lists_one (const object *wants, const object *has) 136compare_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
419object * 500object *
420find_object_name (const char *str) 501find_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
432void 513void
433free_all_object_data () 514free_all_object_data ()
434{ 515{
435 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); 516 LOG (llevDebug, "%d allocated objects\n", nrofallocobjects);
436}
437
438/*
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 */
448object *
449object::get_owner ()
450{
451 if (!owner
452 || QUERY_FLAG (owner, FLAG_FREED)
453 || QUERY_FLAG (owner, FLAG_REMOVED))
454 owner = 0;
455
456 return owner;
457} 517}
458 518
459/* 519/*
460 * 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
461 * skill and experience objects. 521 * skill and experience objects.
531 591
532 /* What is not cleared is next, prev, and count */ 592 /* What is not cleared is next, prev, and count */
533 593
534 expmul = 1.0; 594 expmul = 1.0;
535 face = blank_face; 595 face = blank_face;
536 attacked_by_count = -1;
537 596
538 if (settings.casting_time) 597 if (settings.casting_time)
539 casting_time = -1; 598 casting_time = -1;
540} 599}
541 600
623/* 682/*
624 * 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
625 * 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.
626 * 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.
627 */ 686 */
628
629void 687void
630update_ob_speed (object *op) 688update_ob_speed (object *op)
631{ 689{
632 extern int arch_init; 690 extern int arch_init;
633 691
642 abort (); 700 abort ();
643#else 701#else
644 op->speed = 0; 702 op->speed = 0;
645#endif 703#endif
646 } 704 }
705
647 if (arch_init) 706 if (arch_init)
648 {
649 return; 707 return;
650 } 708
651 if (FABS (op->speed) > MIN_ACTIVE_SPEED) 709 if (FABS (op->speed) > MIN_ACTIVE_SPEED)
652 { 710 {
653 /* If already on active list, don't do anything */ 711 /* If already on active list, don't do anything */
654 if (op->active_next || op->active_prev || op == active_objects) 712 if (op->active_next || op->active_prev || op == active_objects)
655 return; 713 return;
656 714
657 /* process_events() expects us to insert the object at the beginning 715 /* process_events() expects us to insert the object at the beginning
658 * of the list. */ 716 * of the list. */
659 op->active_next = active_objects; 717 op->active_next = active_objects;
718
660 if (op->active_next != NULL) 719 if (op->active_next != NULL)
661 op->active_next->active_prev = op; 720 op->active_next->active_prev = op;
721
662 active_objects = op; 722 active_objects = op;
663 } 723 }
664 else 724 else
665 { 725 {
666 /* If not on the active list, nothing needs to be done */ 726 /* If not on the active list, nothing needs to be done */
668 return; 728 return;
669 729
670 if (op->active_prev == NULL) 730 if (op->active_prev == NULL)
671 { 731 {
672 active_objects = op->active_next; 732 active_objects = op->active_next;
733
673 if (op->active_next != NULL) 734 if (op->active_next != NULL)
674 op->active_next->active_prev = NULL; 735 op->active_next->active_prev = NULL;
675 } 736 }
676 else 737 else
677 { 738 {
678 op->active_prev->active_next = op->active_next; 739 op->active_prev->active_next = op->active_next;
740
679 if (op->active_next) 741 if (op->active_next)
680 op->active_next->active_prev = op->active_prev; 742 op->active_next->active_prev = op->active_prev;
681 } 743 }
744
682 op->active_next = NULL; 745 op->active_next = NULL;
683 op->active_prev = NULL; 746 op->active_prev = NULL;
684 } 747 }
685} 748}
686 749
833 if (op->more != NULL) 896 if (op->more != NULL)
834 update_object (op->more, action); 897 update_object (op->more, action);
835} 898}
836 899
837static unordered_vector<object *> mortals; 900static unordered_vector<object *> mortals;
838static std::vector<object *> freed;
839 901
840void object::free_mortals () 902void object::free_mortals ()
841{ 903{
842 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) 904 for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();)
843 if ((*i)->refcnt) 905 if ((*i)->refcnt)
844 ++i; // further delay freeing 906 ++i; // further delay freeing
845 else 907 else
846 { 908 {
847 freed.push_back (*i);//D
848 //delete *i; 909 delete *i;
849 mortals.erase (i); 910 mortals.erase (i);
850 } 911 }
851 912
913 static int lastmortals = 0;//D
914
852 if (mortals.size())//D 915 if (mortals.size() != lastmortals)//D
916 {
917 lastmortals = mortals.size ();//D
853 LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D 918 LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D
919 }
854} 920}
855 921
856object::object () 922object::object ()
857{ 923{
858 SET_FLAG (this, FLAG_REMOVED); 924 SET_FLAG (this, FLAG_REMOVED);
859 925
860 expmul = 1.0; 926 expmul = 1.0;
861 face = blank_face; 927 face = blank_face;
862 attacked_by_count = -1;
863} 928}
864 929
865object::~object () 930object::~object ()
866{ 931{
867 free_key_values (this); 932 free_key_values (this);
868} 933}
869 934
870void object::link () 935void object::link ()
871{ 936{
872 count = ++ob_count; 937 count = ++ob_count;
938 uuid = gen_uuid ();
873 939
874 prev = 0; 940 prev = 0;
875 next = objects; 941 next = objects;
876 942
877 if (objects) 943 if (objects)
880 objects = this; 946 objects = this;
881} 947}
882 948
883void object::unlink () 949void object::unlink ()
884{ 950{
885 count = 0; 951 //count = 0;//D
886 952 if (!prev && !next) return;//D
887 /* Remove this object from the list of used objects */
888 if (prev)
889 {
890 prev->next = next;
891 prev = 0;
892 }
893
894 if (next)
895 {
896 next->prev = prev;
897 next = 0;
898 }
899 953
900 if (this == objects) 954 if (this == objects)
901 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;
902} 963}
903 964
904object *object::create () 965object *object::create ()
905{ 966{
906 object *
907 op;
908
909 if (freed.empty ())
910 op = new object; 967 object *op = new object;
911 else
912 {
913 // highly annoying, but the only way to get it stable right now
914 op = freed.back ();
915 freed.pop_back ();
916 op->~object ();
917 new ((void *) op) object;
918 }
919
920 op->link (); 968 op->link ();
921 return op; 969 return op;
922} 970}
923 971
924/* 972/*
934void object::free (bool free_inventory) 982void object::free (bool free_inventory)
935{ 983{
936 if (QUERY_FLAG (this, FLAG_FREED)) 984 if (QUERY_FLAG (this, FLAG_FREED))
937 return; 985 return;
938 986
987 if (QUERY_FLAG (this, FLAG_FRIENDLY))
988 remove_friendly_object (this);
989
939 if (!QUERY_FLAG (this, FLAG_REMOVED)) 990 if (!QUERY_FLAG (this, FLAG_REMOVED))
940 remove_ob (this); 991 remove_ob (this);
941
942 if (QUERY_FLAG (this, FLAG_FRIENDLY))
943 remove_friendly_object (this);
944 992
945 SET_FLAG (this, FLAG_FREED); 993 SET_FLAG (this, FLAG_FREED);
946 994
947 if (more) 995 if (more)
948 { 996 {
954 { 1002 {
955 /* Only if the space blocks everything do we not process - 1003 /* Only if the space blocks everything do we not process -
956 * if some form of movement is allowed, let objects 1004 * if some form of movement is allowed, let objects
957 * drop on that space. 1005 * drop on that space.
958 */ 1006 */
959 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)
960 { 1008 {
961 object *op = inv; 1009 object *op = inv;
962 1010
963 while (op) 1011 while (op)
964 { 1012 {
990 op = tmp; 1038 op = tmp;
991 } 1039 }
992 } 1040 }
993 } 1041 }
994 1042
1043 // clear those pointers that likely might have circular references to us
995 owner = 0; 1044 owner = 0;
1045 enemy = 0;
1046 attacked_by = 0;
996 1047
997 /* Remove object from the active list */ 1048 /* Remove object from the active list */
998 speed = 0; 1049 speed = 0;
999 update_ob_speed (this); 1050 update_ob_speed (this);
1000 1051
1220 * This function goes through all objects below and including top, and 1271 * This function goes through all objects below and including top, and
1221 * merges op to the first matching object. 1272 * merges op to the first matching object.
1222 * If top is NULL, it is calculated. 1273 * If top is NULL, it is calculated.
1223 * 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
1224 */ 1275 */
1225
1226object * 1276object *
1227merge_ob (object *op, object *top) 1277merge_ob (object *op, object *top)
1228{ 1278{
1229 if (!op->nrof) 1279 if (!op->nrof)
1230 return 0; 1280 return 0;
2651 2701
2652 if (tempfile == NULL) 2702 if (tempfile == NULL)
2653 { 2703 {
2654 LOG (llevError, "Error - Unable to access load object temp file\n"); 2704 LOG (llevError, "Error - Unable to access load object temp file\n");
2655 return NULL; 2705 return NULL;
2656 }; 2706 }
2707
2657 fprintf (tempfile, obstr); 2708 fprintf (tempfile, obstr);
2658 fclose (tempfile); 2709 fclose (tempfile);
2659 2710
2660 op = get_object (); 2711 op = get_object ();
2661 2712
2718 * The returned string is shared. 2769 * The returned string is shared.
2719 */ 2770 */
2720const char * 2771const char *
2721get_ob_key_value (const object *op, const char *const key) 2772get_ob_key_value (const object *op, const char *const key)
2722{ 2773{
2723 key_value * 2774 key_value *link;
2724 link; 2775 shstr_cmp canonical_key (key);
2725 const char *
2726 canonical_key;
2727 2776
2728 canonical_key = shstr::find (key);
2729
2730 if (canonical_key == NULL) 2777 if (!canonical_key)
2731 { 2778 {
2732 /* 1. There being a field named key on any object 2779 /* 1. There being a field named key on any object
2733 * implies there'd be a shared string to find. 2780 * implies there'd be a shared string to find.
2734 * 2. Since there isn't, no object has this field. 2781 * 2. Since there isn't, no object has this field.
2735 * 3. Therefore, *this* object doesn't have this field. 2782 * 3. Therefore, *this* object doesn't have this field.
2736 */ 2783 */
2737 return NULL; 2784 return 0;
2738 } 2785 }
2739 2786
2740 /* This is copied from get_ob_key_link() above - 2787 /* This is copied from get_ob_key_link() above -
2741 * only 4 lines, and saves the function call overhead. 2788 * only 4 lines, and saves the function call overhead.
2742 */ 2789 */
2743 for (link = op->key_values; link != NULL; link = link->next) 2790 for (link = op->key_values; link; link = link->next)
2744 {
2745 if (link->key == canonical_key) 2791 if (link->key == canonical_key)
2746 {
2747 return link->value; 2792 return link->value;
2748 } 2793
2749 }
2750 return NULL; 2794 return 0;
2751} 2795}
2752 2796
2753 2797
2754/* 2798/*
2755 * Updates the canonical_key in op to value. 2799 * Updates the canonical_key in op to value.
2840{ 2884{
2841 shstr key_ (key); 2885 shstr key_ (key);
2842 2886
2843 return set_ob_key_value_s (op, key_, value, add_key); 2887 return set_ob_key_value_s (op, key_, value, add_key);
2844} 2888}
2889
2890object::depth_iterator::depth_iterator (object *container)
2891: iterator_base (container)
2892{
2893 while (item->inv)
2894 item = item->inv;
2895}
2896
2897void
2898object::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
2912const char *
2913object::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
2933const char *
2934object::debug_desc () const
2935{
2936 static char info[256 * 3];
2937 return debug_desc (info);
2938}
2939

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines