File: restore_state.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (169 lines) | stat: -rw-r--r-- 4,542 bytes parent folder | download | duplicates (3)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Based on ScottFree interpreter version 1.14 developed by Swansea
 * University Computer Society without disassembly of any other game
 * drivers, only of game databases as permitted by EEC law (for purposes
 * of compatibility).
 *
 * Licensed under GPLv2
 *
 * https://github.com/angstsmurf/spatterlight/tree/master/terps/scott
 */

#include "glk/scott/scott.h"
#include "glk/scott/globals.h"
#include "glk/scott/definitions.h"
#include "glk/scott/restore_state.h"

namespace Glk {
namespace Scott {

#define MAX_UNDOS 100

void saveUndo() {
	if (_G(_justUndid)) {
		_G(_justUndid) = 0;
		return;
	}
	if (_G(_lastUndo) == nullptr) {
		_G(_lastUndo) = saveCurrentState();
		_G(_oldestUndo) = _G(_lastUndo);
		_G(_numberOfUndos) = 1;
		return;
	}

	if (_G(_numberOfUndos) == 0)
		g_scott->fatal("Number of undos == 0 but _G(_lastUndo) != nullptr!");

	_G(_lastUndo)->_nextState = saveCurrentState();
	SavedState *current = _G(_lastUndo)->_nextState;
	current->_previousState = _G(_lastUndo);
	_G(_lastUndo) = current;
	if (_G(_numberOfUndos) == MAX_UNDOS) {
		SavedState *oldest = _G(_oldestUndo);
		_G(_oldestUndo) = _G(_oldestUndo)->_nextState;
		_G(_oldestUndo)->_previousState = nullptr;
		delete[] oldest->_itemLocations;
		delete oldest;
	} else {
		_G(_numberOfUndos)++;
	}
}

void restoreUndo() {
	if (_G(_justStarted)) {
		g_scott->output(_G(_sys)[CANT_UNDO_ON_FIRST_TURN]);
		return;
	}
	if (_G(_lastUndo) == nullptr || _G(_lastUndo)->_previousState == nullptr) {
		g_scott->output(_G(_sys)[NO_UNDO_STATES]);
		return;
	}
	SavedState *current = _G(_lastUndo);
	_G(_lastUndo) = current->_previousState;
	if (_G(_lastUndo)->_previousState == nullptr)
		_G(_oldestUndo) = _G(_lastUndo);
	restoreState(_G(_lastUndo));
	g_scott->output(_G(_sys)[MOVE_UNDONE]);
	delete[] current->_itemLocations;
	delete current;
	_G(_numberOfUndos)--;
	_G(_justUndid) = 1;
}

void ramSave() {
	if (_G(_ramSave) != nullptr) {
		delete[] _G(_ramSave)->_itemLocations;
		delete _G(_ramSave);
	}

	_G(_ramSave) = saveCurrentState();
	g_scott->output(_G(_sys)[STATE_SAVED]);
}

void ramRestore() {
	if (_G(_ramSave) == nullptr) {
		g_scott->output(_G(_sys)[NO_SAVED_STATE]);
		return;
	}

	restoreState(_G(_ramSave));
	g_scott->output(_G(_sys)[STATE_RESTORED]);
	saveUndo();
}

SavedState *saveCurrentState() {
	SavedState *s = new SavedState;
	for (int ct = 0; ct < 16; ct++) {
		s->_counters[ct] = _G(_counters)[ct];
		s->_roomSaved[ct] = _G(_roomSaved)[ct];
	}

	s->_bitFlags = _G(_bitFlags);
	s->_currentLoc = MY_LOC;
	s->_currentCounter = _G(_currentCounter);
	s->_savedRoom = _G(_savedRoom);
	s->_lightTime = _G(_gameHeader)->_lightTime;
	s->_autoInventory = _G(_autoInventory);

	s->_itemLocations = new uint8_t[_G(_gameHeader)->_numItems + 1];

	for (int ct = 0; ct <= _G(_gameHeader)->_numItems; ct++) {
		s->_itemLocations[ct] = _G(_items)[ct]._location;
	}

	s->_previousState = nullptr;
	s->_nextState = nullptr;

	return s;
}

void recoverFromBadRestore(SavedState *state) {
	g_scott->output(_G(_sys)[BAD_DATA]);
	restoreState(state);
	delete state;
}

void restoreState(SavedState *state) {
	for (int ct = 0; ct < 16; ct++) {
		_G(_counters)[ct] = state->_counters[ct];
		_G(_roomSaved)[ct] = state->_roomSaved[ct];
	}

	_G(_bitFlags) = state->_bitFlags;

	MY_LOC = state->_currentLoc;
	_G(_currentCounter) = state->_currentCounter;
	_G(_savedRoom) = state->_savedRoom;
	_G(_gameHeader)->_lightTime = state->_lightTime;
	_G(_autoInventory) = state->_autoInventory;

	for (int ct = 0; ct <= _G(_gameHeader)->_numItems; ct++) {
		_G(_items)[ct]._location = state->_itemLocations[ct];
	}

	_G(_stopTime) = 1;
}

} // End of namespace Scott
} // End of namespace Glk