… | |
… | |
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. |
… | |
… | |
430 | |
512 | |
431 | void |
513 | void |
432 | free_all_object_data () |
514 | free_all_object_data () |
433 | { |
515 | { |
434 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
516 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
435 | } |
|
|
436 | |
|
|
437 | /* |
|
|
438 | * Returns the object which this object marks as being the owner. |
|
|
439 | * A id-scheme is used to avoid pointing to objects which have been |
|
|
440 | * freed and are now reused. If this is detected, the owner is |
|
|
441 | * set to NULL, and NULL is returned. |
|
|
442 | * Changed 2004-02-12 - if the player is setting at the play again |
|
|
443 | * prompt, he is removed, and we don't want to treat him as an owner of |
|
|
444 | * anything, so check removed flag. I don't expect that this should break |
|
|
445 | * anything - once an object is removed, it is basically dead anyways. |
|
|
446 | */ |
|
|
447 | object * |
|
|
448 | object::get_owner () |
|
|
449 | { |
|
|
450 | if (!owner |
|
|
451 | || QUERY_FLAG (owner, FLAG_FREED) |
|
|
452 | || QUERY_FLAG (owner, FLAG_REMOVED)) |
|
|
453 | owner = 0; |
|
|
454 | |
|
|
455 | return owner; |
|
|
456 | } |
517 | } |
457 | |
518 | |
458 | /* |
519 | /* |
459 | * 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 |
460 | * skill and experience objects. |
521 | * skill and experience objects. |
… | |
… | |
530 | |
591 | |
531 | /* What is not cleared is next, prev, and count */ |
592 | /* What is not cleared is next, prev, and count */ |
532 | |
593 | |
533 | expmul = 1.0; |
594 | expmul = 1.0; |
534 | face = blank_face; |
595 | face = blank_face; |
535 | attacked_by_count = -1; |
|
|
536 | |
596 | |
537 | if (settings.casting_time) |
597 | if (settings.casting_time) |
538 | casting_time = -1; |
598 | casting_time = -1; |
539 | } |
599 | } |
540 | |
600 | |
… | |
… | |
836 | if (op->more != NULL) |
896 | if (op->more != NULL) |
837 | update_object (op->more, action); |
897 | update_object (op->more, action); |
838 | } |
898 | } |
839 | |
899 | |
840 | static unordered_vector<object *> mortals; |
900 | static unordered_vector<object *> mortals; |
841 | static std::vector<object *> freed; |
|
|
842 | |
901 | |
843 | void object::free_mortals () |
902 | void object::free_mortals () |
844 | { |
903 | { |
845 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
904 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
846 | if ((*i)->refcnt) |
905 | if ((*i)->refcnt) |
847 | ++i; // further delay freeing |
906 | ++i; // further delay freeing |
848 | else |
907 | else |
849 | { |
908 | { |
850 | freed.push_back (*i);//D |
|
|
851 | //delete *i; |
909 | delete *i; |
852 | mortals.erase (i); |
910 | mortals.erase (i); |
853 | } |
911 | } |
854 | |
912 | |
855 | if (mortals.size() && 0)//D |
913 | static int lastmortals = 0;//D |
|
|
914 | |
|
|
915 | if (mortals.size() != lastmortals)//D |
|
|
916 | { |
|
|
917 | lastmortals = mortals.size ();//D |
856 | LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D |
918 | LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D |
|
|
919 | } |
857 | } |
920 | } |
858 | |
921 | |
859 | object::object () |
922 | object::object () |
860 | { |
923 | { |
861 | SET_FLAG (this, FLAG_REMOVED); |
924 | SET_FLAG (this, FLAG_REMOVED); |
862 | |
925 | |
863 | expmul = 1.0; |
926 | expmul = 1.0; |
864 | face = blank_face; |
927 | face = blank_face; |
865 | attacked_by_count = -1; |
|
|
866 | } |
928 | } |
867 | |
929 | |
868 | object::~object () |
930 | object::~object () |
869 | { |
931 | { |
870 | free_key_values (this); |
932 | free_key_values (this); |
871 | } |
933 | } |
872 | |
934 | |
873 | void object::link () |
935 | void object::link () |
874 | { |
936 | { |
875 | count = ++ob_count; |
937 | count = ++ob_count; |
|
|
938 | uuid = gen_uuid (); |
876 | |
939 | |
877 | prev = 0; |
940 | prev = 0; |
878 | next = objects; |
941 | next = objects; |
879 | |
942 | |
880 | if (objects) |
943 | if (objects) |
… | |
… | |
883 | objects = this; |
946 | objects = this; |
884 | } |
947 | } |
885 | |
948 | |
886 | void object::unlink () |
949 | void object::unlink () |
887 | { |
950 | { |
888 | count = 0; |
951 | //count = 0;//D |
889 | |
952 | 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 | |
953 | |
903 | if (this == objects) |
954 | if (this == objects) |
904 | 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; |
905 | } |
963 | } |
906 | |
964 | |
907 | object *object::create () |
965 | object *object::create () |
908 | { |
966 | { |
909 | object *op; |
|
|
910 | |
|
|
911 | if (freed.empty ()) |
|
|
912 | op = new object; |
967 | object *op = new object; |
913 | else |
|
|
914 | { |
|
|
915 | // highly annoying, but the only way to get it stable right now |
|
|
916 | op = freed.back (); |
|
|
917 | freed.pop_back (); |
|
|
918 | op->~object (); |
|
|
919 | new ((void *) op) object; |
|
|
920 | } |
|
|
921 | |
|
|
922 | op->link (); |
968 | op->link (); |
923 | return op; |
969 | return op; |
924 | } |
970 | } |
925 | |
971 | |
926 | /* |
972 | /* |
… | |
… | |
1222 | * This function goes through all objects below and including top, and |
1268 | * This function goes through all objects below and including top, and |
1223 | * merges op to the first matching object. |
1269 | * merges op to the first matching object. |
1224 | * If top is NULL, it is calculated. |
1270 | * If top is NULL, it is calculated. |
1225 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1271 | * Returns pointer to object if it succeded in the merge, otherwise NULL |
1226 | */ |
1272 | */ |
1227 | |
|
|
1228 | object * |
1273 | object * |
1229 | merge_ob (object *op, object *top) |
1274 | merge_ob (object *op, object *top) |
1230 | { |
1275 | { |
1231 | if (!op->nrof) |
1276 | if (!op->nrof) |
1232 | return 0; |
1277 | return 0; |
… | |
… | |
2653 | |
2698 | |
2654 | if (tempfile == NULL) |
2699 | if (tempfile == NULL) |
2655 | { |
2700 | { |
2656 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2701 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2657 | return NULL; |
2702 | return NULL; |
2658 | }; |
2703 | } |
|
|
2704 | |
2659 | fprintf (tempfile, obstr); |
2705 | fprintf (tempfile, obstr); |
2660 | fclose (tempfile); |
2706 | fclose (tempfile); |
2661 | |
2707 | |
2662 | op = get_object (); |
2708 | op = get_object (); |
2663 | |
2709 | |