aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/state.cpp
blob: 0ebf52f3a4dad1fcff3e764fdc0d9e23f04f0fce (plain)
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
/* 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 "glk/alan3/state.h"
#include "glk/alan3/syserr.h"
#include "glk/alan3/current.h"
#include "glk/alan3/word.h"
#include "glk/alan3/state_stack.h"
#include "glk/alan3/instance.h"
#include "glk/alan3/attribute.h"
#include "glk/alan3/memory.h"
#include "glk/alan3/score.h"
#include "glk/alan3/event.h"
#include "glk/alan3/set.h"

namespace Glk {
namespace Alan3 {

/* PRIVATE TYPES */

/* Implementation of the abstract type typedef struct game_state GameState */
struct game_state {
	/* Event queue */
	EventQueueEntry *eventQueue;
	int eventQueueTop;          /* Event queue top pointer */

	/* Scores */
	int score;
	Aword *scores;              /* Score table pointer */

	/* Instance data */
	AdminEntry *admin;          /* Administrative data about instances */
	AttributeEntry *attributes; /* Attributes data area */
	/* Sets and strings are dynamically allocated areas for which the
	   attribute is just a pointer to. So they are not catched by the
	   saving of attributes, instead they require special storage */
	Set **sets;                 /* Array of set pointers */
	char **strings;             /* Array of string pointers */
};

/* PRIVATE DATA */
static GameState gameState;     /* TODO: Make pointer, then we don't have to copy to stack, we can just use the pointer */
static StateStackP stateStack = NULL;

static char *playerCommand;


/*----------------------------------------------------------------------*/
static int countStrings(void) {
	StringInitEntry *entry;
	int count = 0;

	if (header->stringInitTable != 0)
		for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); * (Aword *)entry != EOD; entry++)
			count++;
	return (count);
}


/*----------------------------------------------------------------------*/
static void deallocateStrings(GameState *gState) {
	int count = countStrings();
	int i;

	for (i = 0; i < count; i++)
		deallocate(gState->strings[i]);
	deallocate(gState->strings);
}

/*----------------------------------------------------------------------*/
static int countSets(void) {
	SetInitEntry *entry;
	int count = 0;

	if (header->setInitTable != 0)
		for (entry = (SetInitEntry *)pointerTo(header->setInitTable); * (Aword *)entry != EOD; entry++)
			count++;
	return (count);
}


/*----------------------------------------------------------------------*/
static void deallocateSets(GameState *gState) {
	int count = countSets();
	int i;

	for (i = 0; i < count; i++)
		freeSet(gState->sets[i]);
	deallocate(gState->sets);
}

/*======================================================================*/
void deallocateGameState(GameState *gState) {

	deallocate(gState->admin);
	deallocate(gState->attributes);

	if (gState->eventQueueTop > 0) {
		deallocate(gState->eventQueue);
		gState->eventQueue = NULL;
	}
	if (gState->scores)
		deallocate(gState->scores);

	deallocateStrings(gState);
	deallocateSets(gState);

	memset(gState, 0, sizeof(GameState));
}


/*======================================================================*/
void forgetGameState(void) {
	char *playerCmd;
	popGameState(stateStack, &gameState, &playerCmd);
	deallocateGameState(&gameState);
	if (playerCmd != NULL)
		deallocate(playerCmd);
}


/*======================================================================*/
void initStateStack(void) {
	if (stateStack != NULL)
		deleteStateStack(stateStack);
	stateStack = createStateStack(sizeof(GameState));
}


/*======================================================================*/
void terminateStateStack(void) {
	deleteStateStack(stateStack);
	stateStack = NULL;
}


/*======================================================================*/
bool anySavedState(void) {
	return !stateStackIsEmpty(stateStack);
}


/*----------------------------------------------------------------------*/
static Set **collectSets(void) {
	SetInitEntry *entry;
	int count = countSets();
	Set **sets;
	int i;

	if (count == 0) return NULL;

	sets = (Set **)allocate(count * sizeof(Set));

	entry = (SetInitEntry *)pointerTo(header->setInitTable);
	for (i = 0; i < count; i++)
		sets[i] = getInstanceSetAttribute(entry[i].instanceCode, entry[i].attributeCode);

	return sets;
}


/*----------------------------------------------------------------------*/
static char **collectStrings(void) {
	StringInitEntry *entry;
	int count = countStrings();
	char **strings;
	int i;

	if (count == 0) return NULL;

	strings = (char **)allocate(count * sizeof(char *));

	entry = (StringInitEntry *)pointerTo(header->stringInitTable);
	for (i = 0; i < count; i++)
		strings[i] = getInstanceStringAttribute(entry[i].instanceCode, entry[i].attributeCode);

	return strings;
}


/*======================================================================*/
void rememberCommands(void) {
	char *command = playerWordsAsCommandString();
	attachPlayerCommandsToLastState(stateStack, command);
	deallocate(command);
}


/*----------------------------------------------------------------------*/
static void collectEvents(void) {
	gameState.eventQueueTop = eventQueueTop;
	if (eventQueueTop > 0)
		gameState.eventQueue = (EventQueueEntry *)duplicate(eventQueue, eventQueueTop * sizeof(EventQueueEntry));
}


/*----------------------------------------------------------------------*/
static void collectInstanceData(void) {
	gameState.admin = (AdminEntry *)duplicate(admin, (header->instanceMax + 1) * sizeof(AdminEntry));
	gameState.attributes = (AttributeEntry *)duplicate(attributes, header->attributesAreaSize * sizeof(Aword));
	gameState.sets = collectSets();
	gameState.strings = collectStrings();
}


/*----------------------------------------------------------------------*/
static void collectScores(void) {
	gameState.score = current.score;
	if (scores == NULL)
		gameState.scores = NULL;
	else
		gameState.scores = (Aword *)duplicate(scores, header->scoreCount * sizeof(Aword));
}


/*======================================================================*/
void rememberGameState(void) {
	collectEvents();
	collectInstanceData();
	collectScores();

	if (stateStack == NULL)
		initStateStack();

	pushGameState(stateStack, &gameState);
	gameStateChanged = FALSE;
}


/*----------------------------------------------------------------------*/
static void freeCurrentSetAttributes(void) {
	SetInitEntry *entry;

	if (header->setInitTable == 0) return;
	for (entry = (SetInitEntry *)pointerTo(header->setInitTable); * (Aword *)entry != EOD; entry++) {
		Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode);
		freeSet((Set *)fromAptr(attributeValue));
	}
}


/*----------------------------------------------------------------------*/
static void recallSets(Set **sets) {
	SetInitEntry *entry;
	int count = countSets();
	int i;

	if (header->setInitTable == 0) return;

	entry = (SetInitEntry *)pointerTo(header->setInitTable);
	for (i = 0; i < count; i++) {
		setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(sets[i]));
		sets[i] = NULL; /* Since we reuse the saved set, we need to clear the pointer */
	}
}


/*----------------------------------------------------------------------*/
static void freeCurrentStringAttributes(void) {
	StringInitEntry *entry;

	if (header->stringInitTable == 0) return;
	for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); * (Aword *)entry != EOD; entry++) {
		Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode);
		deallocate(fromAptr(attributeValue));
	}
}


/*----------------------------------------------------------------------*/
static void recallStrings(char **strings) {
	StringInitEntry *entry;
	int count = countStrings();
	int i;

	if (header->stringInitTable == 0) return;

	entry = (StringInitEntry *)pointerTo(header->stringInitTable);
	for (i = 0; i < count; i++) {
		setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(strings[i]));
		strings[i] = NULL;      /* Since we reuse the saved, we need to clear the state */
	}
}


/*----------------------------------------------------------------------*/
static void recallEvents(void) {
	eventQueueTop = gameState.eventQueueTop;
	if (eventQueueTop > 0) {
		memcpy(eventQueue, gameState.eventQueue,
		       (eventQueueTop + 1)*sizeof(EventQueueEntry));
	}
}


/*----------------------------------------------------------------------*/
static void recallInstances(void) {

	if (admin == NULL)
		syserr("admin[] == NULL in recallInstances()");

	memcpy(admin, gameState.admin,
	       (header->instanceMax + 1)*sizeof(AdminEntry));

	freeCurrentSetAttributes();     /* Need to free previous set values */
	freeCurrentStringAttributes();  /* Need to free previous string values */

	memcpy(attributes, gameState.attributes,
	       header->attributesAreaSize * sizeof(Aword));

	recallSets(gameState.sets);
	recallStrings(gameState.strings);
}


/*----------------------------------------------------------------------*/
static void recallScores(void) {
	current.score = gameState.score;
	memcpy(scores, gameState.scores, header->scoreCount * sizeof(Aword));
}


/*======================================================================*/
void recallGameState(void) {
	popGameState(stateStack, &gameState, &playerCommand);
	recallEvents();
	recallInstances();
	recallScores();
	deallocateGameState(&gameState);
}


/*======================================================================*/
char *recreatePlayerCommand(void) {
	return playerCommand;
}

} // End of namespace Alan3
} // End of namespace Glk