Debian Codegraph @

prboom2/src/dsda/msecnode.c

raw
1 //
2 // Copyright(C) 2020 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA MSecNode Management
16 //
17
18 #include "r_defs.h"
19 #include "p_saveg.h"
20 #include "lprintf.h"
21 #include "p_tick.h"
22
23 #include "msecnode.h"
24
25 extern sector_t *sectors;
26 extern int numsectors;
27
28 msecnode_t* P_GetSecnode(void);
29 msecnode_t* P_DelSecnode(msecnode_t* node);
30 int P_GetMobj(mobj_t* mi, size_t s);
31
32 static dboolean dsda_IsMSecNodeMobj(thinker_t* thinker)
33 {
34 return thinker->function == P_MobjThinker ||
35 thinker->function == P_BlasterMobjThinker;
36 }
37
38 // The MSecNodes store an intersecting web of nodes.
39 // The nodes are ordered by sector for each thing
40 // and by thing for each sector.
41 // The order of these nodes matters:
42 // A crusher damages things in the msecnode order,
43 // which causes painstates based on rng, which is order-dependent.
44 // In order to have syncing key-frames / saves, we must preserve the order.
45
46 // Serialization strategy:
47 //
48 // The msecnode_t struct has 6 pointers:
49 // 1 sector, 1 mobj, 4 msecnodes
50 // A trivial serialization is not possible.
51 //
52 // Since the position of everything is preserved,
53 // the number of msecnodes in any list is preserved.
54 //
55 // We could build a sparse 2d array of msecnodes,
56 // but this isn't really feasible.
57 // Example: eviternity map 32,
58 // 7207 sectors and 2420 things = 17 million entries.
59 //
60 // To serialize:
61 // - iterate over all the sectors and store the order of things.
62 // - must also store the number of things here because
63 // there is a bug in the sector management code:
64 // despite being in the same position, a reloaded monster
65 // sometimes has a different list of sectors that it is in!
66 // - iterate over all the things and store the order of sectors.
67 // - must also store the number of sectors here because
68 // this information will be missing while unarchiving.
69 //
70 // To deserialize:
71 // - iterate over all the sectors
72 // - iterate over their current msecnode lists
73 // - replace the msecnode thing based on the stored order
74 // - expand the list if it has too few elements (as described above)
75 // - iterate over all the things
76 // - because of the order change in the sector msecnode lists,
77 // the thing's msecnode list may be completely scrambled.
78 // - iterate over the stored sectors
79 // - iterate over the sector's restored msecnode list
80 // - find the msecnode referencing this thing
81 // - restore the prev / next values for the thing msecnode list
82 // - after iterating over all the things and through their sectors,
83 // the sector ordering of msecnodes should be corrected,
84 // and the thing ordering should still be correct from the first step.
85
86 void dsda_ArchiveMSecNodes(void) {
87 int sector_i;
88 thinker_t* th;
89 msecnode_t* msecnode;
90 int count;
91
92 // Store the m_things using the mobj_p indices (see p_saveg.c)
93 for (sector_i = 0; sector_i < numsectors; ++sector_i) {
94 // While this shouldn't be necessary, sometimes the m_snext chain
95 // is not managed correctly, leading to a different length on load.
96 count = 0;
97 msecnode = sectors[sector_i].touching_thinglist;
98 while (msecnode) {
99 if (dsda_IsMSecNodeMobj(&msecnode->m_thing->thinker))
100 count++;
101 msecnode = msecnode->m_snext;
102 }
103
104 P_SAVE_X(count);
105
106 msecnode = sectors[sector_i].touching_thinglist;
107 while (msecnode) {
108 th = &msecnode->m_thing->thinker;
109
110 if (!dsda_IsMSecNodeMobj(th)) {
111 lprintf(LO_WARN, "Orphan mobj in msecnode list - undefined behaviour may occur!\n");
112 msecnode = msecnode->m_snext;
113 continue;
114 }
115
116 P_SAVE_X(th->prev);
117
118 msecnode = msecnode->m_snext;
119 }
120 }
121
122 // Store the m_sectors using the sector indices
123 for (th = thinkercap.next; th != &thinkercap; th = th->next) {
124 if (!dsda_IsMSecNodeMobj(th)) continue;
125
126 // We must store the count because it will be unobtainable
127 // while we are rewriting the m_tnext chains
128 count = 0;
129 msecnode = ((mobj_t*) th)->touching_sectorlist;
130 while (msecnode) {
131 count++;
132 msecnode = msecnode->m_tnext;
133 }
134
135 P_SAVE_X(count);
136
137 msecnode = ((mobj_t*) th)->touching_sectorlist;
138 while (msecnode) {
139 sector_i = msecnode->m_sector - sectors;
140
141 P_SAVE_X(sector_i);
142
143 msecnode = msecnode->m_tnext;
144 }
145 }
146 }
147
148 static void dsda_UnArchiveMSecNodeMobj(msecnode_t* msecnode, mobj_t** mobj_p, int mobj_count) {
149 mobj_t* mobj;
150
151 P_LOAD_X(mobj);
152
153 mobj = mobj_p[P_GetMobj(mobj, mobj_count + 1)];
154
155 if (mobj)
156 msecnode->m_thing = mobj;
157 else
158 I_Error("dsda_UnArchiveMSecNodeMobj: mobj does not exist!\n");
159 }
160
161 void dsda_UnArchiveMSecNodes(mobj_t** mobj_p, int mobj_count) {
162 int i, sector_i;
163 thinker_t* th;
164 mobj_t* mobj;
165 msecnode_t* msecnode;
166 msecnode_t* msecnode_prev;
167 msecnode_t** msecnode_next;
168 dboolean found;
169 int count, existing_count;
170
171 for (sector_i = 0; sector_i < numsectors; ++sector_i) {
172 P_LOAD_X(count);
173
174 // Due to a bug in m_snext chain management,
175 // this value can differ from the one stored originally.
176 existing_count = 0;
177
178 msecnode_prev = NULL;
179 msecnode_next = &sectors[sector_i].touching_thinglist;
180 msecnode = sectors[sector_i].touching_thinglist;
181
182 // Preserve the m_snext chain, but overwrite the m_things
183 while (msecnode) {
184 existing_count++;
185
186 // If there are too many, delete the rest of them
187 if (existing_count > count) {
188 msecnode_prev = msecnode->m_sprev;
189 while (msecnode) {
190 P_DelSecnode(msecnode);
191
192 if (msecnode_prev)
193 msecnode = msecnode_prev->m_snext;
194 else
195 msecnode = NULL;
196 }
197
198 break;
199 }
200
201 dsda_UnArchiveMSecNodeMobj(msecnode, mobj_p, mobj_count);
202
203 msecnode_prev = msecnode;
204 msecnode_next = &msecnode->m_snext;
205 msecnode = msecnode->m_snext;
206 }
207
208 // If the count is too low, add the missing msecnodes
209 for (i = existing_count; i < count; ++i) {
210 msecnode = P_GetSecnode();
211 msecnode->visited = 0;
212 msecnode->m_sector = &sectors[sector_i];
213
214 dsda_UnArchiveMSecNodeMobj(msecnode, mobj_p, mobj_count);
215
216 // This will be filled in below
217 msecnode->m_tprev = NULL;
218 msecnode->m_tnext = NULL;
219
220 // Hook into the chain
221 msecnode->m_sprev = msecnode_prev;
222 msecnode->m_snext = NULL;
223 *msecnode_next = msecnode;
224 msecnode_prev = msecnode;
225 msecnode_next = &msecnode->m_snext;
226 }
227 }
228
229 // The msecnode (sector, thing) pairs are set,
230 // but the m_tnext chains now weave through the wrong msecnodes
231 for (th = thinkercap.next; th != &thinkercap; th = th->next) {
232 if (!dsda_IsMSecNodeMobj(th)) continue;
233
234 mobj = ((mobj_t*) th);
235
236 // The rewriting going on in this loop reorganizes the m_tnext chains,
237 // so we cannot trust the existing chain length
238 P_LOAD_X(count);
239
240 mobj->touching_sectorlist = NULL;
241 msecnode_prev = NULL;
242 msecnode_next = &mobj->touching_sectorlist;
243
244 for (i = 0; i < count; ++i) {
245 P_LOAD_X(sector_i);
246
247 // The sector msecnodes were rewritten above,
248 // so we need to find the actual msecnode match (sector, thing)
249 found = false;
250 msecnode = sectors[sector_i].touching_thinglist;
251 while (msecnode) {
252 if (msecnode->m_thing == mobj) {
253 found = true;
254 break;
255 }
256
257 msecnode = msecnode->m_snext;
258 }
259
260 if (!found)
261 I_Error("dsda_UnArchiveMSecNodes: mobj missing in sector msecnodes!\n");
262
263 // Rewrite the m_tnext chain with the new msecnodes
264 msecnode->m_tprev = msecnode_prev;
265 msecnode->m_tnext = NULL;
266 *msecnode_next = msecnode;
267 msecnode_prev = msecnode;
268 msecnode_next = &msecnode->m_tnext;
269 }
270 }
271 }