… | |
… | |
32 | #include <object.h> |
32 | #include <object.h> |
33 | #include <funcpoint.h> |
33 | #include <funcpoint.h> |
34 | #include <loader.h> |
34 | #include <loader.h> |
35 | |
35 | |
36 | int nrofallocobjects = 0; |
36 | int nrofallocobjects = 0; |
|
|
37 | static UUID uuid; |
|
|
38 | const uint64 UUID_SKIP = 1<<19; |
37 | |
39 | |
38 | object *objects; /* Pointer to the list of used objects */ |
40 | object *objects; /* Pointer to the list of used objects */ |
39 | 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 */ |
40 | |
42 | |
41 | 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, |
… | |
… | |
49 | }; |
51 | }; |
50 | int freedir[SIZEOFFREE] = { |
52 | int freedir[SIZEOFFREE] = { |
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, |
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, |
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 |
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 |
53 | }; |
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 | } |
54 | |
133 | |
55 | /* 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. */ |
56 | static int |
135 | static int |
57 | compare_ob_value_lists_one (const object *wants, const object *has) |
136 | compare_ob_value_lists_one (const object *wants, const object *has) |
58 | { |
137 | { |
… | |
… | |
254 | { |
333 | { |
255 | if (inv->inv) |
334 | if (inv->inv) |
256 | sum_weight (inv); |
335 | sum_weight (inv); |
257 | sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); |
336 | sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); |
258 | } |
337 | } |
|
|
338 | |
259 | if (op->type == CONTAINER && op->stats.Str) |
339 | if (op->type == CONTAINER && op->stats.Str) |
260 | sum = (sum * (100 - op->stats.Str)) / 100; |
340 | sum = (sum * (100 - op->stats.Str)) / 100; |
|
|
341 | |
261 | if (op->carrying != sum) |
342 | if (op->carrying != sum) |
262 | op->carrying = sum; |
343 | op->carrying = sum; |
|
|
344 | |
263 | return sum; |
345 | return sum; |
264 | } |
346 | } |
265 | |
347 | |
266 | /** |
348 | /** |
267 | * Return the outermost environment object for a given object. |
349 | * Return the outermost environment object for a given object. |
… | |
… | |
416 | */ |
498 | */ |
417 | |
499 | |
418 | object * |
500 | object * |
419 | find_object_name (const char *str) |
501 | find_object_name (const char *str) |
420 | { |
502 | { |
421 | const char *name = shstr::find (str); |
503 | shstr_cmp str_ (str); |
422 | object *op; |
504 | object *op; |
423 | |
505 | |
424 | for (op = objects; op != NULL; op = op->next) |
506 | for (op = objects; op != NULL; op = op->next) |
425 | if (&op->name == name) |
507 | if (op->name == str_) |
426 | break; |
508 | break; |
427 | |
509 | |
428 | return op; |
510 | return op; |
429 | } |
511 | } |
430 | |
512 | |
… | |
… | |
836 | if (op->more != NULL) |
918 | if (op->more != NULL) |
837 | update_object (op->more, action); |
919 | update_object (op->more, action); |
838 | } |
920 | } |
839 | |
921 | |
840 | static unordered_vector<object *> mortals; |
922 | static unordered_vector<object *> mortals; |
841 | static std::vector<object *> freed; |
923 | static std::vector<object *, slice_allocator <object *> > freed; |
842 | |
924 | |
843 | void object::free_mortals () |
925 | void object::free_mortals () |
844 | { |
926 | { |
845 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
927 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
846 | if ((*i)->refcnt) |
928 | if ((*i)->refcnt) |
847 | ++i; // further delay freeing |
929 | ++i; // further delay freeing |
848 | else |
930 | else |
849 | { |
931 | { |
|
|
932 | //printf ("free_mortal(%p,%ld,%ld)\n", *i, pticks, (*i)->count);//D |
850 | freed.push_back (*i);//D |
933 | //freed.push_back (*i);//D |
851 | //delete *i; |
934 | delete *i; |
852 | mortals.erase (i); |
935 | mortals.erase (i); |
853 | } |
936 | } |
854 | |
937 | |
855 | if (mortals.size())//D |
938 | if (mortals.size() && 0)//D |
856 | LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D |
939 | LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D |
857 | } |
940 | } |
858 | |
941 | |
859 | object::object () |
942 | object::object () |
860 | { |
943 | { |
… | |
… | |
871 | } |
954 | } |
872 | |
955 | |
873 | void object::link () |
956 | void object::link () |
874 | { |
957 | { |
875 | count = ++ob_count; |
958 | count = ++ob_count; |
|
|
959 | uuid = gen_uuid (); |
876 | |
960 | |
877 | prev = 0; |
961 | prev = 0; |
878 | next = objects; |
962 | next = objects; |
879 | |
963 | |
880 | if (objects) |
964 | if (objects) |
… | |
… | |
883 | objects = this; |
967 | objects = this; |
884 | } |
968 | } |
885 | |
969 | |
886 | void object::unlink () |
970 | void object::unlink () |
887 | { |
971 | { |
888 | count = 0; |
972 | //count = 0;//D |
889 | |
973 | if (!prev && !next) return;//D |
890 | /* Remove this object from the list of used objects */ |
|
|
891 | if (prev) |
|
|
892 | { |
|
|
893 | prev->next = next; |
|
|
894 | prev = 0; |
|
|
895 | } |
|
|
896 | |
|
|
897 | if (next) |
|
|
898 | { |
|
|
899 | next->prev = prev; |
|
|
900 | next = 0; |
|
|
901 | } |
|
|
902 | |
974 | |
903 | if (this == objects) |
975 | if (this == objects) |
904 | objects = next; |
976 | objects = next; |
|
|
977 | |
|
|
978 | /* Remove this object from the list of used objects */ |
|
|
979 | if (prev) prev->next = next; |
|
|
980 | if (next) next->prev = prev; |
|
|
981 | |
|
|
982 | prev = 0; |
|
|
983 | next = 0; |
905 | } |
984 | } |
906 | |
985 | |
907 | object *object::create () |
986 | object *object::create () |
908 | { |
987 | { |
909 | object *op; |
988 | object *op; |
… | |
… | |
943 | |
1022 | |
944 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
1023 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
945 | remove_ob (this); |
1024 | remove_ob (this); |
946 | |
1025 | |
947 | SET_FLAG (this, FLAG_FREED); |
1026 | SET_FLAG (this, FLAG_FREED); |
|
|
1027 | |
|
|
1028 | //printf ("free(%p,%ld,%ld)\n", this, pticks, count);//D |
948 | |
1029 | |
949 | if (more) |
1030 | if (more) |
950 | { |
1031 | { |
951 | more->free (free_inventory); |
1032 | more->free (free_inventory); |
952 | more = 0; |
1033 | more = 0; |
… | |
… | |
1222 | * This function goes through all objects below and including top, and |
1303 | * This function goes through all objects below and including top, and |
1223 | * merges op to the first matching object. |
1304 | * merges op to the first matching object. |
1224 | * If top is NULL, it is calculated. |
1305 | * If top is NULL, it is calculated. |
1225 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1306 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1226 | */ |
1307 | */ |
1227 | |
|
|
1228 | object * |
1308 | object * |
1229 | merge_ob (object *op, object *top) |
1309 | merge_ob (object *op, object *top) |
1230 | { |
1310 | { |
1231 | if (!op->nrof) |
1311 | if (!op->nrof) |
1232 | return 0; |
1312 | return 0; |
… | |
… | |
2653 | |
2733 | |
2654 | if (tempfile == NULL) |
2734 | if (tempfile == NULL) |
2655 | { |
2735 | { |
2656 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2736 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2657 | return NULL; |
2737 | return NULL; |
2658 | }; |
2738 | } |
|
|
2739 | |
2659 | fprintf (tempfile, obstr); |
2740 | fprintf (tempfile, obstr); |
2660 | fclose (tempfile); |
2741 | fclose (tempfile); |
2661 | |
2742 | |
2662 | op = get_object (); |
2743 | op = get_object (); |
2663 | |
2744 | |
… | |
… | |
2720 | * The returned string is shared. |
2801 | * The returned string is shared. |
2721 | */ |
2802 | */ |
2722 | const char * |
2803 | const char * |
2723 | get_ob_key_value (const object *op, const char *const key) |
2804 | get_ob_key_value (const object *op, const char *const key) |
2724 | { |
2805 | { |
2725 | key_value * |
2806 | key_value *link; |
2726 | link; |
2807 | shstr_cmp canonical_key (key); |
2727 | const char * |
|
|
2728 | canonical_key; |
|
|
2729 | |
2808 | |
2730 | canonical_key = shstr::find (key); |
|
|
2731 | |
|
|
2732 | if (canonical_key == NULL) |
2809 | if (!canonical_key) |
2733 | { |
2810 | { |
2734 | /* 1. There being a field named key on any object |
2811 | /* 1. There being a field named key on any object |
2735 | * implies there'd be a shared string to find. |
2812 | * implies there'd be a shared string to find. |
2736 | * 2. Since there isn't, no object has this field. |
2813 | * 2. Since there isn't, no object has this field. |
2737 | * 3. Therefore, *this* object doesn't have this field. |
2814 | * 3. Therefore, *this* object doesn't have this field. |
2738 | */ |
2815 | */ |
2739 | return NULL; |
2816 | return 0; |
2740 | } |
2817 | } |
2741 | |
2818 | |
2742 | /* This is copied from get_ob_key_link() above - |
2819 | /* This is copied from get_ob_key_link() above - |
2743 | * only 4 lines, and saves the function call overhead. |
2820 | * only 4 lines, and saves the function call overhead. |
2744 | */ |
2821 | */ |
2745 | for (link = op->key_values; link != NULL; link = link->next) |
2822 | for (link = op->key_values; link; link = link->next) |
2746 | { |
|
|
2747 | if (link->key == canonical_key) |
2823 | if (link->key == canonical_key) |
2748 | { |
|
|
2749 | return link->value; |
2824 | return link->value; |
2750 | } |
2825 | |
2751 | } |
|
|
2752 | return NULL; |
2826 | return 0; |
2753 | } |
2827 | } |
2754 | |
2828 | |
2755 | |
2829 | |
2756 | /* |
2830 | /* |
2757 | * Updates the canonical_key in op to value. |
2831 | * Updates the canonical_key in op to value. |
… | |
… | |
2864 | } |
2938 | } |
2865 | else |
2939 | else |
2866 | item = item->env; |
2940 | item = item->env; |
2867 | } |
2941 | } |
2868 | |
2942 | |
|
|
2943 | // return a suitable string describing an objetc in enough detail to find it |
|
|
2944 | const char * |
|
|
2945 | object::debug_desc (char *info) const |
|
|
2946 | { |
|
|
2947 | char info2[256 * 3]; |
|
|
2948 | char *p = info; |
|
|
2949 | |
|
|
2950 | p += snprintf (p, 256, "%d=\"%s%s%s\"", |
|
|
2951 | count, |
|
|
2952 | &name, |
|
|
2953 | title ? " " : "", |
|
|
2954 | title ? (const char *)title : ""); |
|
|
2955 | |
|
|
2956 | if (env) |
|
|
2957 | p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); |
|
|
2958 | |
|
|
2959 | if (map) |
|
|
2960 | p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y); |
|
|
2961 | |
|
|
2962 | return info; |
|
|
2963 | } |
|
|
2964 | |
|
|
2965 | const char * |
|
|
2966 | object::debug_desc () const |
|
|
2967 | { |
|
|
2968 | static char info[256 * 3]; |
|
|
2969 | return debug_desc (info); |
|
|
2970 | } |
|
|
2971 | |