-
Notifications
You must be signed in to change notification settings - Fork 5
/
scene_saved_game.cpp
499 lines (436 loc) · 12.5 KB
/
scene_saved_game.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint 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 3 of the License, or
(at your option) any later version.
Clonepoint 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 Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "scene.h"
#define SAVE_INTERVAL_MS 3000
void Scene::loadGame(const char* filename)
{
FILE* input = fopen(filename, "rb");
int i;
SavedGameHeader header;
Enemy* enemy;
Door* door;
LightFixture* lt;
MainComputer* mc;
CollisionVolume* cvol;
CircuitBox* cb;
Alarm* alarm;
ElevatorDoor* ed;
if (!input)
{
LOGF((stderr, "Failed to open save game %s!\n", filename));
return;
}
fread(&header, sizeof(header), 1, input);
_playerShotFired = header.playerFiredShot;
_timeToSniper = header.timeToSniper;
_numPlayerBullets = header.numPlayerBullets;
_playerEnergy = header.playerEnergy;
std::vector<SavedGameLink> links(header.numLinks);
std::vector<SavedGameEntityState> states(header.numEnts);
fread(links.data(), sizeof(SavedGameLink) * header.numLinks, 1, input);
fread(states.data(), sizeof(SavedGameEntityState) * header.numEnts, 1, input);
fclose(input);
_currentMap->addMissingGuns();
_currentMap->clearLinks();
loadMap(header.filename, true);
if (_timeToSniper > 0)
{
_currentMap->removeSniper();
}
else
{
_currentMap->addSniper();
}
for (i = 0; i < header.numEnts; i++)
{
switch (states[i].type)
{
case SGET_Enemy:
Assert(dynamic_cast<Enemy*>(_currentMap->getEntAt(states[i].index3)));
enemy = static_cast<Enemy*>(_currentMap->getEntAt(states[i].index3));
enemy->setDirection(states[i].dir);
enemy->setPosition(states[i].position.x, states[i].position.y);
enemy->setVelocity(states[i].velocity.x, states[i].velocity.y);
enemy->setAlive(states[i].alive);
enemy->setCanSeePlayer(states[i].b2);
enemy->setReactionTime(states[i].index4);
enemy->setReadyToShoot(false);
enemy->setWaitingForAlert(states[i].b1);
if (states[i].index1 >= 0)
{
enemy->setLinkableTarget((LinkableEntity*)_currentMap->getEntAt(states[i].index1));
}
if (states[i].index2 >= 0)
{
enemy->setLightToActivate((FieldOfView*)_currentMap->getLightAt(states[i].index2));
}
if (states[i].targetType != TARGET_NONE)
{
enemy->alertToPosition(states[i].targetLocation.x, states[i].targetLocation.y, states[i].alertType, states[i].targetType);
}
else
{
enemy->forgetTarget();
}
if (checkIfEntOnGround(enemy))
{
enemy->setIgnoreFall(false);
}
enemy->setSecondaryTarget(states[i].index5 >= 0 ? _currentMap->getEntAt(states[i].index5) : NULL);
calculateStrongestLight(enemy); //placed here to prevent idle guard from patrolling if game was saved with activated light and was loaded while it was deactivated.
enemy->changeState(states[i].gs);
enemy->setResolve(states[i].esr.timeSinceShot, states[i].esr.shotType);
enemy->update(0); //placed here to move the enemy's gun accordingly so that links with them will restore correctly.
enemy->setSprite();
break;
case SGET_Player:
_player->setDirection(states[i].dir);
_player->_onGround = states[i].onGround;
_player->setPosition(states[i].position.x, states[i].position.y);
_player->setVelocity(states[i].velocity.x, states[i].velocity.y);
if (states[i].index1 >= 0)
{
_player->attachToVolume(_currentMap->getCollideVolPointerAt(states[i].index1), states[i].at);
}
else
{
_player->detach();
}
if (states[i].index2 >= 0)
{
_player->enterElevator((ElevatorDoor*)_currentMap->getEntAt(states[i].index2));
}
else
{
_player->leaveElevator();
}
_player->_numHackedTerminals = states[i].index3;
break;
case SGET_Objective:
Assert(dynamic_cast<MainComputer*>(_currentMap->getEntAt(states[i].index1)));
mc = static_cast<MainComputer*>(_currentMap->getEntAt(states[i].index1));
mc->setActive(states[i].b1);
break;
case SGET_Door:
Assert(dynamic_cast<Door*>(_currentMap->getEntAt(states[i].index1)));
door = static_cast<Door*>(_currentMap->getEntAt(states[i].index1));
if (states[i].b1 && !door->isOpened())
{
door->open(states[i].dir);
}
if (!states[i].b1 && door->isOpened())
{
door->close(states[i].dir);
}
door->setOpenDirection(states[i].dir);
door->updateCollisionVolume();
break;
case SGET_LightFixture:
Assert(dynamic_cast<LightFixture*>(_currentMap->getEntAt(states[i].index1)));
lt = static_cast<LightFixture*>(_currentMap->getEntAt(states[i].index1));
if (states[i].b1 xor lt->isSwitchedOn())
{
lt->setSwitchedOn(!lt->isSwitchedOn());
}
break;
case SGET_Glass:
cvol = _currentMap->getCollideVolPointerAt(states[i].index1);
Assert(cvol->glass());
if (states[i].b1)
{
cvol->flags |= COLLISION_ACTIVE;
}
else
{
cvol->flags &= ~COLLISION_ACTIVE;
}
break;
case SGET_CircuitBox:
cb = static_cast<CircuitBox*>(_currentMap->getEntAt(states[i].index1));
cb->setHacked(states[i].b1, false);
_circuitUnlocked[cb->getCircuit()] = states[i].b1;
break;
case SGET_Alarm:
alarm = static_cast<Alarm*>(_currentMap->getEntAt(states[i].index1));
alarm->setAnimating(states[i].b1);
break;
case SGET_ElevatorDoor:
ed = static_cast<ElevatorDoor*>(_currentMap->getEntAt(states[i].index1));
if (states[i].b1)
{
ed->_shaft->setOpenDoor(ed, false);
}
break;
default:
break;
}
}
for (i = 0; i < header.numLinks; i++)
{
_currentMap->addSavedLink(links[i].start, links[i].end);
}
makeInitialLinks();
}
void Scene::saveGame(const char* filename)
{
FILE* output = fopen(filename, "wb");
SavedGameHeader header;
std::vector<SavedGameLink> links;
std::vector<SavedGameEntityState> states;
strcpy(header.filename, _mapFilename);
getLinks(&links);
header.numLinks = links.size();
//SGESS
getSGESs(&states);
SavedGameEntityState player_SGES;
player_SGES.type = SGET_Player;
player_SGES.position = _player->getPosition();
player_SGES.velocity = _player->getVelocity();
player_SGES.dir = _player->getDirection();
player_SGES.at = _player->getAttachType();
player_SGES.index1 = _currentMap->indexOfCollideVol(_player->getAttachedVolume());
player_SGES.index2 = _currentMap->indexOfEntity(_player->getElevatorDoor());
player_SGES.index3 = _player->_numHackedTerminals;
player_SGES.onGround = _player->_onGround;
states.push_back(player_SGES);
header.numEnts = states.size();
header.playerFiredShot = _playerShotFired;
header.timeToSniper = _timeToSniper;
header.numPlayerBullets = _numPlayerBullets;
header.playerEnergy = _playerEnergy;
//Write
fwrite(&header, sizeof(header), 1, output);
fwrite(links.data(), sizeof(SavedGameLink) * header.numLinks, 1, output);
fwrite(states.data(), sizeof(SavedGameEntityState) * header.numEnts, 1, output);
fclose(output);
links.clear();
states.clear();
}
void Scene::getLinks(std::vector<SavedGameLink>* container)
{
LinkableEntity* curr;
LinkableEntity* target;
SavedGameLink sgl;
std::vector<LinkableEntity*>::iterator begin;
std::vector<LinkableEntity*>::iterator end;
std::vector<LinkableEntity*>::iterator i;
_currentMap->getLinkableIters(&begin, &end);
for (i = begin; i != end; ++i)
{
curr = *i;
target = curr->getTarget();
if (target != NULL)
{
sgl.start = curr->getCollisionRectPosition();
sgl.end = target->getCollisionRectPosition();
container->push_back(sgl);
}
}
}
void Scene::getSGESs(std::vector<SavedGameEntityState>* container)
{
unsigned int i;
SavedGameEntityState sges;
Entity* ent;
CollisionVolume* vol;
LivingEntity* le;
Door* door;
MainComputer* mc;
CircuitBox* cb;
Enemy* enemy;
LightFixture* lt;
Alarm* alarm;
ElevatorDoor* ed;
for (i = 0; i < _currentMap->getNumberOfEnts(); i++)
{
ent = _currentMap->getEntAt(i);
sges.type = SGET_Unknown;
sges.position = ent->getPosition();
sges.velocity = ent->getVelocity();
if (dynamic_cast<LivingEntity*>(ent))
{
le = static_cast<LivingEntity*>(ent);
sges.dir = le->getDirection();
sges.stairTimer = le->getStairTimer();
sges.onGround = le->_onGround;
sges.alive = le->isAlive();
}
if (dynamic_cast<Door*>(ent))
{
door = static_cast<Door*>(ent);
sges.type = SGET_Door;
sges.index1 = i;
sges.index2 = 0;
sges.index3 = 0;
sges.index4 = 0;
sges.b1 = door->isOpened();
sges.timeToClose = door->getTimeToClose();
sges.dir = door->getOpenDirection();
}
if (dynamic_cast<MainComputer*>(ent))
{
mc = static_cast<MainComputer*>(ent);
sges.type = SGET_Objective;
sges.b1 = mc->isActive();
sges.index1 = i;
sges.index2 = 0;
sges.index3 = 0;
sges.index4 = 0;
}
if (dynamic_cast<CircuitBox*>(ent))
{
cb = static_cast<CircuitBox*>(ent);
sges.type = SGET_CircuitBox;
sges.b1 = cb->isHacked();
sges.index1 = i;
sges.index2 = 0;
sges.index3 = 0;
sges.index4 = 0;
}
if (dynamic_cast<Enemy*>(ent))
{
enemy = static_cast<Enemy*>(ent);
sges.type = SGET_Enemy;
sges.gs = enemy->getState();
sges.targetType = enemy->getTargetType();
sges.alertType = enemy->getAlertType();
sges.targetLocation = enemy->getTarget();
sges.index1 = _currentMap->indexOfEntity(enemy->getTargetSwitch());
sges.index2 = _currentMap->indexOfFOV(enemy->getLightToActivate());
sges.index3 = i;
sges.index4 = enemy->getReactionTime();
sges.index5 = _currentMap->indexOfEntity(enemy->getSecondaryTarget());
sges.b1 = enemy->isWaitingForAlert();
sges.b2 = enemy->canSeePlayer();
sges.esr = enemy->getResolve();
}
if (dynamic_cast<LightFixture*>(ent))
{
lt = static_cast<LightFixture*>(ent);
sges.type = SGET_LightFixture;
sges.b1 = lt->isSwitchedOn();
sges.index1 = i;
sges.index2 = 0;
sges.index3 = 0;
sges.index4 = 0;
}
if (dynamic_cast<Alarm*>(ent))
{
alarm = static_cast<Alarm*>(ent);
sges.type = SGET_Alarm;
sges.b1 = alarm->isActivated();
sges.index1 = i;
sges.index2 = 0;
sges.index3 = 0;
sges.index4 = 0;
}
if (dynamic_cast<ElevatorDoor*>(ent))
{
ed = static_cast<ElevatorDoor*>(ent);
sges.type = SGET_ElevatorDoor;
sges.b1 = ed->isOpen();
sges.index1 = i;
}
container->push_back(sges);
}
for (i = 0; i < _currentMap->getNumberOfCollideVols(); i++)
{
vol = _currentMap->getCollideVolPointerAt(i);
if (vol->glass())
{
sges.type = SGET_Glass;
sges.b1 = vol->active();
sges.index1 = i;
container->push_back(sges);
}
}
}
void Scene::updateSaves(unsigned int dT)
{
if (_loadMenuVisible ||
(!_player->_onGround && _player->getAttachType() == NotAttached) ||
!_player->isAlive())
return;
_saveTimer += dT;
if (_saveTimer > SAVE_INTERVAL_MS)
{
setTimeAt(_currentSave, _saveTimer);
_saveTimer = 0;
sprintf(_saveFilename, "auto%i.sav", _currentSave);
saveGame(_saveFilename);
_currentSave++;
if (_currentSave >= MAX_SAVES)
{
_currentSave = 0;
}
}
}
bool Scene::isLoadMenuVisible()
{
return _loadMenuVisible;
}
void Scene::showLoadMenu(bool b)
{
_loadMenuVisible = b;
}
int Scene::getSaveTimeAt(unsigned int index)
{
return _saveTimeSince[index];
}
void Scene::loadAutosave(unsigned int index)
{
unsigned int currIndex = index;
unsigned int i;
if (_saveTimeSince[currIndex] == -1)
{
LOGF((stdout, "Can't load game in future!\n"));
return;
}
sprintf(_saveFilename, "auto%i.sav", currIndex);
//invalidate any saves made in the "future".
for (i = 0; i < MAX_SAVES - 1; i++)
{
currIndex = currIndex == MAX_SAVES - 1 ? 0 : currIndex + 1;
if (_saveTimeSince[currIndex] < _saveTimeSince[index])
{
_saveTimeSince[currIndex] = -1;
}
if (_saveTimeSince[currIndex] > _saveTimeSince[index])
{
_saveTimeSince[currIndex] -= _saveTimeSince[index];
}
}
_saveTimeSince[index] = 0; //this auto save was just loaded, so set saved time to 0.
loadGame(_saveFilename);
}
void Scene::setTimeAt(unsigned int index, unsigned int time)
{
unsigned int currIndex = index;
unsigned int i;
_saveTimeSince[currIndex] = 0;
for (i = 0; i < MAX_SAVES - 1; i++)
{
currIndex = currIndex == 0 ? MAX_SAVES - 1 : currIndex - 1;
if (_saveTimeSince[currIndex] != -1)
{
_saveTimeSince[currIndex] += time;
}
}
}
int Scene::getSecondsSince(unsigned int index)
{
int milliseconds = _saveTimer + _saveTimeSince[index];
return milliseconds / 1000;
}