… | |
… | |
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 | { |
… | |
… | |
436 | { |
515 | { |
437 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
516 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
438 | } |
517 | } |
439 | |
518 | |
440 | /* |
519 | /* |
441 | * Returns the object which this object marks as being the owner. |
|
|
442 | * A id-scheme is used to avoid pointing to objects which have been |
|
|
443 | * freed and are now reused. If this is detected, the owner is |
|
|
444 | * set to NULL, and NULL is returned. |
|
|
445 | * Changed 2004-02-12 - if the player is setting at the play again |
|
|
446 | * prompt, he is removed, and we don't want to treat him as an owner of |
|
|
447 | * anything, so check removed flag. I don't expect that this should break |
|
|
448 | * anything - once an object is removed, it is basically dead anyways. |
|
|
449 | */ |
|
|
450 | object * |
|
|
451 | object::get_owner () |
|
|
452 | { |
|
|
453 | if (!owner |
|
|
454 | || QUERY_FLAG (owner, FLAG_FREED) |
|
|
455 | || QUERY_FLAG (owner, FLAG_REMOVED)) |
|
|
456 | owner = 0; |
|
|
457 | |
|
|
458 | return owner; |
|
|
459 | } |
|
|
460 | |
|
|
461 | /* |
|
|
462 | * 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 |
463 | * skill and experience objects. |
521 | * skill and experience objects. |
464 | */ |
522 | */ |
465 | void |
523 | void |
466 | object::set_owner (object *owner) |
524 | object::set_owner (object *owner) |
… | |
… | |
533 | |
591 | |
534 | /* What is not cleared is next, prev, and count */ |
592 | /* What is not cleared is next, prev, and count */ |
535 | |
593 | |
536 | expmul = 1.0; |
594 | expmul = 1.0; |
537 | face = blank_face; |
595 | face = blank_face; |
538 | attacked_by_count = -1; |
|
|
539 | |
596 | |
540 | if (settings.casting_time) |
597 | if (settings.casting_time) |
541 | casting_time = -1; |
598 | casting_time = -1; |
542 | } |
599 | } |
543 | |
600 | |
… | |
… | |
839 | if (op->more != NULL) |
896 | if (op->more != NULL) |
840 | update_object (op->more, action); |
897 | update_object (op->more, action); |
841 | } |
898 | } |
842 | |
899 | |
843 | static unordered_vector<object *> mortals; |
900 | static unordered_vector<object *> mortals; |
844 | static std::vector<object *, slice_allocator <object *> > freed; |
|
|
845 | |
901 | |
846 | void object::free_mortals () |
902 | void object::free_mortals () |
847 | { |
903 | { |
848 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
904 | for (unordered_vector<object *>::iterator i = mortals.begin (); i != mortals.end ();) |
849 | if ((*i)->refcnt) |
905 | if ((*i)->refcnt) |
850 | ++i; // further delay freeing |
906 | ++i; // further delay freeing |
851 | else |
907 | else |
852 | { |
908 | { |
853 | freed.push_back (*i);//D |
|
|
854 | //delete *i; |
909 | delete *i; |
855 | mortals.erase (i); |
910 | mortals.erase (i); |
856 | } |
911 | } |
857 | |
912 | |
858 | if (mortals.size() && 0)//D |
913 | static int lastmortals = 0;//D |
|
|
914 | |
|
|
915 | if (mortals.size() != lastmortals)//D |
|
|
916 | { |
|
|
917 | lastmortals = mortals.size ();//D |
859 | LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D |
918 | LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D |
|
|
919 | } |
860 | } |
920 | } |
861 | |
921 | |
862 | object::object () |
922 | object::object () |
863 | { |
923 | { |
864 | SET_FLAG (this, FLAG_REMOVED); |
924 | SET_FLAG (this, FLAG_REMOVED); |
865 | |
925 | |
866 | expmul = 1.0; |
926 | expmul = 1.0; |
867 | face = blank_face; |
927 | face = blank_face; |
868 | attacked_by_count = -1; |
|
|
869 | } |
928 | } |
870 | |
929 | |
871 | object::~object () |
930 | object::~object () |
872 | { |
931 | { |
873 | free_key_values (this); |
932 | free_key_values (this); |
874 | } |
933 | } |
875 | |
934 | |
876 | void object::link () |
935 | void object::link () |
877 | { |
936 | { |
878 | count = ++ob_count; |
937 | count = ++ob_count; |
|
|
938 | uuid = gen_uuid (); |
879 | |
939 | |
880 | prev = 0; |
940 | prev = 0; |
881 | next = objects; |
941 | next = objects; |
882 | |
942 | |
883 | if (objects) |
943 | if (objects) |
… | |
… | |
886 | objects = this; |
946 | objects = this; |
887 | } |
947 | } |
888 | |
948 | |
889 | void object::unlink () |
949 | void object::unlink () |
890 | { |
950 | { |
891 | count = 0; |
951 | //count = 0;//D |
|
|
952 | if (!prev && !next) return;//D |
892 | |
953 | |
893 | if (this == objects) |
954 | if (this == objects) |
894 | objects = next; |
955 | objects = next; |
895 | |
956 | |
896 | /* Remove this object from the list of used objects */ |
957 | /* Remove this object from the list of used objects */ |
897 | if (prev) |
|
|
898 | { |
|
|
899 | prev->next = next; |
958 | if (prev) prev->next = next; |
|
|
959 | if (next) next->prev = prev; |
|
|
960 | |
900 | prev = 0; |
961 | prev = 0; |
901 | } |
|
|
902 | |
|
|
903 | if (next) |
|
|
904 | { |
|
|
905 | next->prev = prev; |
|
|
906 | next = 0; |
962 | next = 0; |
907 | } |
|
|
908 | } |
963 | } |
909 | |
964 | |
910 | object *object::create () |
965 | object *object::create () |
911 | { |
966 | { |
912 | object *op; |
|
|
913 | |
|
|
914 | if (freed.empty ()) |
|
|
915 | op = new object; |
967 | object *op = new object; |
916 | else |
|
|
917 | { |
|
|
918 | // highly annoying, but the only way to get it stable right now |
|
|
919 | op = freed.back (); |
|
|
920 | freed.pop_back (); |
|
|
921 | op->~object (); |
|
|
922 | new ((void *) op) object; |
|
|
923 | } |
|
|
924 | |
|
|
925 | op->link (); |
968 | op->link (); |
926 | return op; |
969 | return op; |
927 | } |
970 | } |
928 | |
971 | |
929 | /* |
972 | /* |
… | |
… | |
995 | op = tmp; |
1038 | op = tmp; |
996 | } |
1039 | } |
997 | } |
1040 | } |
998 | } |
1041 | } |
999 | |
1042 | |
|
|
1043 | // clear those pointers that likely might have circular references to us |
1000 | owner = 0; |
1044 | owner = 0; |
|
|
1045 | enemy = 0; |
|
|
1046 | attacked_by = 0; |
1001 | |
1047 | |
1002 | /* Remove object from the active list */ |
1048 | /* Remove object from the active list */ |
1003 | speed = 0; |
1049 | speed = 0; |
1004 | update_ob_speed (this); |
1050 | update_ob_speed (this); |
1005 | |
1051 | |
… | |
… | |
2655 | |
2701 | |
2656 | if (tempfile == NULL) |
2702 | if (tempfile == NULL) |
2657 | { |
2703 | { |
2658 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2704 | LOG (llevError, "Error - Unable to access load object temp file\n"); |
2659 | return NULL; |
2705 | return NULL; |
2660 | }; |
2706 | } |
|
|
2707 | |
2661 | fprintf (tempfile, obstr); |
2708 | fprintf (tempfile, obstr); |
2662 | fclose (tempfile); |
2709 | fclose (tempfile); |
2663 | |
2710 | |
2664 | op = get_object (); |
2711 | op = get_object (); |
2665 | |
2712 | |