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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "lastexpress/game/savepoint.h"
#include "lastexpress/game/entities.h"
#include "lastexpress/game/logic.h"
#include "lastexpress/game/state.h"
#include "lastexpress/lastexpress.h"
namespace LastExpress {
SavePoints::SavePoints(LastExpressEngine *engine) : _engine(engine) {
for (int i = 0; i < 40; i++)
_callbacks[i] = NULL;
}
SavePoints::~SavePoints() {
// Zero passed pointers
_engine = NULL;
}
//////////////////////////////////////////////////////////////////////////
// Savepoints
//////////////////////////////////////////////////////////////////////////
void SavePoints::push(EntityIndex entity2, EntityIndex entity1, ActionIndex action, uint32 param) {
if (_savepoints.size() >= _savePointsMaxSize)
return;
SavePoint point;
point.entity1 = entity1;
point.action = action;
point.entity2 = entity2;
point.param.intValue = param;
_savepoints.push_back(point);
}
void SavePoints::push(EntityIndex entity2, EntityIndex entity1, ActionIndex action, const Common::String param) {
if (_savepoints.size() >= _savePointsMaxSize)
return;
SavePoint point;
point.entity1 = entity1;
point.action = action;
point.entity2 = entity2;
assert(param.size() <= 5);
strncpy((char *)&point.param.charValue, param.c_str(), 5);
_savepoints.push_back(point);
}
SavePoint SavePoints::pop() {
SavePoint point = _savepoints.front();
_savepoints.pop_front();
return point;
}
void SavePoints::pushAll(EntityIndex entity, ActionIndex action, uint32 param) {
for (uint32 index = 1; index < 40; index++) {
if ((EntityIndex)index != entity)
push(entity, (EntityIndex)index, action, param);
}
}
// Process all savepoints
void SavePoints::process() {
while (_savepoints.size() > 0 && getFlags()->isGameRunning) {
SavePoint savepoint = pop();
// If this is a data savepoint, update the entity
// otherwise, execute the callback
if (!updateEntityFromData(savepoint)) {
// Call requested callback
Callback *callback = getCallback(savepoint.entity1);
if (callback && callback->isValid()) {
debugC(8, kLastExpressDebugLogic, "Savepoint: entity1=%s, action=%s, entity2=%s", ENTITY_NAME(savepoint.entity1), ACTION_NAME(savepoint.action), ENTITY_NAME(savepoint.entity2));
(*callback)(savepoint);
}
}
}
}
void SavePoints::reset() {
_savepoints.clear();
}
//////////////////////////////////////////////////////////////////////////
// Data
//////////////////////////////////////////////////////////////////////////
void SavePoints::addData(EntityIndex entity, ActionIndex action, uint32 param) {
if (_data.size() >= _savePointsMaxSize)
return;
SavePointData data;
data.entity1 = entity;
data.action = action;
data.param = param;
_data.push_back(data);
}
//////////////////////////////////////////////////////////////////////////
// Callbacks
//////////////////////////////////////////////////////////////////////////
void SavePoints::setCallback(EntityIndex index, Callback *callback) {
if (index >= 40)
error("[SavePoints::setCallback] Attempting to use an invalid entity index. Valid values 0-39, was %d", index);
if (!callback || !callback->isValid())
error("[SavePoints::setCallback] Attempting to set an invalid callback for entity %s", ENTITY_NAME(index));
_callbacks[index] = callback;
}
Callback *SavePoints::getCallback(EntityIndex index) const {
if (index >= 40)
error("[SavePoints::getCallback] Attempting to use an invalid entity index. Valid values 0-39, was %d", index);
return _callbacks[index];
}
void SavePoints::call(EntityIndex entity2, EntityIndex entity1, ActionIndex action, uint32 param) const {
SavePoint point;
point.entity1 = entity1;
point.action = action;
point.entity2 = entity2;
point.param.intValue = param;
Callback *callback = getCallback(entity1);
if (callback != NULL && callback->isValid()) {
debugC(8, kLastExpressDebugLogic, "Savepoint: entity1=%s, action=%s, entity2=%s, param=%d", ENTITY_NAME(entity1), ACTION_NAME(action), ENTITY_NAME(entity2), param);
(*callback)(point);
}
}
void SavePoints::call(EntityIndex entity2, EntityIndex entity1, ActionIndex action, const Common::String param) const {
SavePoint point;
point.entity1 = entity1;
point.action = action;
point.entity2 = entity2;
assert(param.size() <= 6); // "MUS%03d"
strncpy((char *)&point.param.charValue, param.c_str(), 6);
Callback *callback = getCallback(entity1);
if (callback != NULL && callback->isValid()) {
debugC(8, kLastExpressDebugLogic, "Savepoint: entity1=%s, action=%s, entity2=%s, param=%s", ENTITY_NAME(entity1), ACTION_NAME(action), ENTITY_NAME(entity2), param.c_str());
(*callback)(point);
}
}
void SavePoints::callAndProcess() {
SavePoint savepoint; // empty parameters
// We ignore the kEntityPlayer callback in the list
EntityIndex index = kEntityAnna;
// Call all callbacks with empty parameters
bool isRunning = getFlags()->isGameRunning;
while (isRunning) {
Callback *callback = getCallback(index);
if (callback != NULL && callback->isValid()) {
(*callback)(savepoint);
isRunning = getFlags()->isGameRunning;
}
index = (EntityIndex)(index + 1);
// Process all savepoints when done
if (index >= 40) {
if (isRunning)
process();
return;
}
}
}
//////////////////////////////////////////////////////////////////////////
// Misc
//////////////////////////////////////////////////////////////////////////
bool SavePoints::updateEntityFromData(const SavePoint &savepoint) {
for (uint i = 0; i < _data.size(); i++) {
// Not a data savepoint!
if (!_data[i].entity1)
return false;
// Found our data!
if (_data[i].entity1 == savepoint.entity1 && _data[i].action == savepoint.action) {
debugC(8, kLastExpressDebugLogic, "Update entity from data: entity1=%s, action=%s, param=%u", ENTITY_NAME(_data[i].entity1), ACTION_NAME(_data[i].action), _data[i].param);
// the SavePoint param value is the index of the entity call parameter to update
getEntities()->get(_data[i].entity1)->getParamData()->updateParameters(_data[i].param);
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////////////
// Serializable
//////////////////////////////////////////////////////////////////////////
void SavePoints::saveLoadWithSerializer(Common::Serializer &s) {
// Serialize savepoint data
if (s.isLoading())
_data.clear();
uint32 dataSize = (s.isLoading() ? _savePointsMaxSize : _data.size());
for (uint i = 0; i < dataSize; i++) {
if (s.isLoading()) {
SavePointData data;
_data.push_back(data);
}
s.syncAsUint32LE(_data[i].entity1);
s.syncAsUint32LE(_data[i].action);
s.syncAsUint32LE(_data[i].entity2);
s.syncAsUint32LE(_data[i].param);
}
// Skip uninitialized data if any
// (we are using a compressed stream, so we cannot seek on load)
uint32 unusedDataSize = (_savePointsMaxSize - dataSize) * 16;
if (s.isLoading()) {
byte *empty = (byte *)malloc(unusedDataSize);
s.syncBytes(empty, unusedDataSize);
free(empty);
} else {
s.skip(unusedDataSize);
}
// Number of savepoints
uint32 numSavepoints = _savepoints.size();
s.syncAsUint32LE(numSavepoints);
// Savepoints
if (s.isLoading()) {
for (uint i = 0; i < numSavepoints; i++) {
SavePoint point;
s.syncAsUint32LE(point.entity1);
s.syncAsUint32LE(point.action);
s.syncAsUint32LE(point.entity2);
s.syncAsUint32LE(point.param.intValue);
_savepoints.push_back(point);
if (_savepoints.size() >= _savePointsMaxSize)
break;
}
} else {
for (Common::List<SavePoint>::iterator it = _savepoints.begin(); it != _savepoints.end(); ++it) {
s.syncAsUint32LE((*it).entity1);
s.syncAsUint32LE((*it).action);
s.syncAsUint32LE((*it).entity2);
s.syncAsUint32LE((*it).param.intValue);
}
}
}
//////////////////////////////////////////////////////////////////////////
// toString
//////////////////////////////////////////////////////////////////////////
Common::String SavePoints::toString() {
Common::String ret = "";
ret += "Savepoint Data\n";
for (uint i = 0; i < _data.size(); i++)
ret += _data[i].toString() + "\n";
ret += "\nSavepoints\n";
for (Common::List<SavePoint>::iterator it = _savepoints.begin(); it != _savepoints.end(); ++it)
ret += (*it).toString() + "\n";
return ret;
}
} // End of namespace LastExpress
|