/
mapstatewriter.cpp
274 lines (232 loc) · 6.97 KB
/
mapstatewriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/** @file mapstatewriter.cpp Saved map state writer.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "common.h"
#include "mapstatewriter.h"
#include "dmu_lib.h"
#include "p_saveg.h"
#include "p_saveio.h"
#include "thinkerinfo.h"
DENG2_PIMPL(MapStateWriter)
{
bool excludePlayers;
Writer *writer;
MaterialArchive *materialArchive;
Instance(Public *i)
: Base(i)
, excludePlayers(false)
, writer(0)
, materialArchive(0)
{}
void beginSegment(int segmentId)
{
SV_BeginSegment(segmentId);
}
void endSegment()
{
SV_EndSegment();
}
void beginMapSegment()
{
beginSegment(ASEG_MAP_HEADER2);
#if __JHEXEN__
Writer_WriteByte(writer, MY_SAVE_VERSION); // Map version also.
// Write the map timer
Writer_WriteInt32(writer, mapTime);
#endif
// Create and populate the MaterialArchive.
#ifdef __JHEXEN__
materialArchive = MaterialArchive_New(true /* segment check */);
#else
materialArchive = MaterialArchive_New(false);
#endif
MaterialArchive_Write(materialArchive, writer);
}
void endMapSegment()
{
endSegment();
MaterialArchive_Delete(materialArchive); materialArchive = 0;
}
void writeElements()
{
beginSegment(ASEG_MAP_ELEMENTS);
for(int i = 0; i < numsectors; ++i)
{
SV_WriteSector((Sector *)P_ToPtr(DMU_SECTOR, i), thisPublic);
}
for(int i = 0; i < numlines; ++i)
{
SV_WriteLine((Line *)P_ToPtr(DMU_LINE, i), thisPublic);
}
}
void writePolyobjs()
{
#if __JHEXEN__
beginSegment(ASEG_POLYOBJS);
Writer_WriteInt32(writer, numpolyobjs);
for(int i = 0; i < numpolyobjs; ++i)
{
SV_WritePolyObj(Polyobj_ById(i), thisPublic);
}
#endif
}
struct writethinkerworker_params_t
{
MapStateWriter *msw;
bool excludePlayers;
};
/**
* Serializes the specified thinker and writes it to save state.
*/
static int writeThinkerWorker(thinker_t *th, void *context)
{
writethinkerworker_params_t &p = *static_cast<writethinkerworker_params_t *>(context);
// We are only concerned with thinkers we have save info for.
ThinkerClassInfo *thInfo = SV_ThinkerInfo(*th);
if(!thInfo) return false;
// Are we excluding players?
if(p.excludePlayers)
{
if(th->function == (thinkfunc_t) P_MobjThinker && ((mobj_t *) th)->player)
return false; // Continue iteration.
}
// Only the server saves this class of thinker?
if((thInfo->flags & TSF_SERVERONLY) && IS_CLIENT)
return false;
// Write the header block for this thinker.
Writer_WriteByte(p.msw->writer(), thInfo->thinkclass); // Thinker type byte.
Writer_WriteByte(p.msw->writer(), th->inStasis? 1 : 0); // In stasis?
// Write the thinker data.
thInfo->writeFunc(th, p.msw);
return false; // Continue iteration.
}
/**
* Serializes thinkers for both client and server.
*
* @note Clients do not save data for all thinkers. In some cases the server
* will send it anyway (so saving it would just bloat client save states).
*
* @note Some thinker classes are NEVER saved by clients.
*/
void writeThinkers()
{
beginSegment(ASEG_THINKERS);
#if __JHEXEN__
Writer_WriteInt32(writer, thingArchiveSize); // number of mobjs.
#endif
// Serialize qualifying thinkers.
writethinkerworker_params_t parm; de::zap(parm);
parm.msw = thisPublic;
parm.excludePlayers = excludePlayers;
Thinker_Iterate(0/*all thinkers*/, writeThinkerWorker, &parm);
// Mark the end of the thinkers.
Writer_WriteByte(writer, TC_END);
}
void writeACScriptData()
{
#if __JHEXEN__
beginSegment(ASEG_SCRIPTS);
Game_ACScriptInterpreter().writeMapScriptData(writer);
#endif
}
void writeSoundSequences()
{
#if __JHEXEN__
beginSegment(ASEG_SOUNDS);
SN_WriteSequences(writer);
#endif
}
void writeBrain()
{
#if __JDOOM__
DENG_ASSERT(bossBrain != 0);
bossBrain->write(thisPublic);
#endif
}
void writeSoundTargets()
{
#if !__JHEXEN__
// Not for us?
if(!IS_SERVER) return;
// Write the total number.
int count = 0;
for(int i = 0; i < numsectors; ++i)
{
xsector_t *xsec = P_ToXSector((Sector *)P_ToPtr(DMU_SECTOR, i));
if(xsec->soundTarget)
{
count += 1;
}
}
Writer_WriteInt32(writer, count);
// Write the mobj references using the mobj archive.
for(int i = 0; i < numsectors; ++i)
{
xsector_t *xsec = P_ToXSector((Sector *)P_ToPtr(DMU_SECTOR, i));
if(xsec->soundTarget)
{
Writer_WriteInt32(writer, i);
Writer_WriteInt16(writer, SV_ThingArchiveId(xsec->soundTarget));
}
}
#endif
}
void writeMisc()
{
#if __JHEXEN__
beginSegment(ASEG_MISC);
for(int i = 0; i < MAXPLAYERS; ++i)
{
Writer_WriteInt32(writer, localQuakeHappening[i]);
}
#endif
}
};
MapStateWriter::MapStateWriter(bool excludePlayers)
: d(new Instance(this))
{
d->excludePlayers = excludePlayers;
}
void MapStateWriter::write(Writer *writer)
{
DENG_ASSERT(writer != 0);
d->writer = writer;
d->beginMapSegment();
{
d->writeElements();
d->writePolyobjs();
d->writeThinkers();
d->writeACScriptData();
d->writeSoundSequences();
d->writeMisc();
d->writeBrain();
d->writeSoundTargets();
}
d->endMapSegment();
}
Writer *MapStateWriter::writer()
{
DENG_ASSERT(d->writer != 0);
return d->writer;
}
materialarchive_serialid_t MapStateWriter::serialIdFor(Material *material)
{
DENG_ASSERT(d->materialArchive != 0);
return MaterialArchive_FindUniqueSerialId(d->materialArchive, material);
}