aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/advsys/game.h
blob: cce7822eff2a59c50111adb8175bb5b44a6eaf26 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* 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.
 *
 */

#ifndef GLK_ADVSYS_GAME
#define GLK_ADVSYS_GAME

#include "common/array.h"
#include "common/stream.h"

namespace Glk {
namespace AdvSys {

#define NIL 0
#define MESSAGE_CACHE_SIZE 8
#define MESSAGE_BLOCK_SIZE 512

/**
 * Actions
 */
enum Action {
	A_VERBS = 0,
	A_PREPOSITIONS = 2,
	A_FLAG = 4,
	A_MASK = 5,
	A_CODE = 6,
	A_SIZE = 8
};

/**
 * Word types
 */
enum WordType {
	WT_UNKNOWN = 0,
	WT_VERB = 1,
	WT_NOUN = 2,
	WT_ADJECTIVE = 3,
	WT_PREPOSITION = 4,
	WT_CONJUNCTION = 5,
	WT_ARTICLE = 6
};

/**
 * Object fields
 */
enum ObjectField {
	O_CLASS = 0,
	O_NOUNS = 2,
	O_ADJECTIVES = 4,
	O_NPROPERTIES = 6,
	O_PROPERTIES = 8,
	O_SIZE = 8
};

/**
 * Built-in variables
 */
enum Variable {
	V_ACTOR = 1,        ///< Actor noun phrase number
	V_ACTION = 2,       ///< Action from phrase
	V_DOBJECT = 3,      ///< First direct object noun phrase number
	V_NDOBJECTS = 4,    ///< Number of direct object noun phrases
	V_IOBJECT = 5,      ///< Indirect object noun phrase number
	V_OCOUNT = 6        ///< Total object count
};

/**
 * Data decryption
 */
class Decrypter {
public:
	/**
	 * Decrypt a data block
	 */
	static void decrypt(byte *data, size_t size);
};

/**
 * AdvSys game header
 */
class Header : public Decrypter {
public:
	bool _valid;                ///< Signals whether header is valid
	size_t _size;               ///< Resident size in bytes
	uint _headerVersion;        ///< Header structure version
	Common::String _name;       ///< Adventure name
	uint _version;              ///< Adventure version
	uint _wordTableOffset;      ///< Word table offset
	uint _wordTypeTableOffset;  ///< Word type table offset
	uint _objectTableOffset;    ///< Object table offset
	uint _actionTableOffset;    ///< Action table offset
	uint _variableTableOffset;  ///< Variable table offset
	uint _dataSpaceOffset;      ///< Data space offset
	uint _codeSpaceOffset;      ///< Code space offset
	uint _dataBlockOffset;      ///< First data block offset
	uint _messageBlockOffset;   ///< First message block offset
	uint _initCodeOffset;       ///< Initialization code offset
	uint _updateCodeOffset;     ///< Update code offset
	uint _beforeOffset;         ///< Code offset before verb handler
	uint _afterOffset;          ///< Code offset after verb handler
	uint _errorHandlerOffset;   ///< Error handler code offset
	uint _saveAreaOffset;       ///< Save area offset
	uint _saveSize;             ///< Save area size
public:
	/**
	 * Constructor
	 */
	Header() : _valid(false), _size(0), _headerVersion(0), _version(0), _wordTableOffset(0),
		_wordTypeTableOffset(0), _objectTableOffset(0), _actionTableOffset(0), _variableTableOffset(0),
		_dataSpaceOffset(0), _codeSpaceOffset(0), _dataBlockOffset(0), _messageBlockOffset(0),
		_initCodeOffset(0), _updateCodeOffset(0), _beforeOffset(0), _afterOffset(0),
		_errorHandlerOffset(0), _saveAreaOffset(0), _saveSize(0) {
	}

	/**
	 * Constructor
	 */
	Header(Common::SeekableReadStream *s) {
		init(s);
	}

	/**
	 * init the header
	 */
	bool init(Common::SeekableReadStream *s);
};

/**
 * Game abstraction class
 */
class Game : public Header {
	struct CacheEntry {
		int _blockNum;
		char _data[MESSAGE_BLOCK_SIZE];

		/**
		 * Constructor
		 */
		CacheEntry() : _blockNum(-1) {
			Common::fill(&_data[0], &_data[MESSAGE_BLOCK_SIZE], '\0');
		}
	};
private:
	bool _restartFlag;
	Common::SeekableReadStream *_stream;
	Common::Array<CacheEntry *> _msgCache;
	int _msgBlockNum, _msgBlockOffset;
private:
	/**
	 * Find an object property field
	 */
	int findProperty(int obj, int prop) const;

	/**
	 * Returns true if an action has a given verb
	 */
	bool hasVerb(int act, const Common::Array<int> &verbs) const;

	/**
	 * Returns true if an action is in a given list
	 */
	bool hasPreposition(int act, int preposition) const {
		return inList(getActionField(act, A_PREPOSITIONS), preposition);
	}

	/**
	 * Check if a word is in an element of a given list
	 */
	bool inList(int link, int word) const;

	/**
	 * Reads in a message block from the game file
	 */
	void readMsgBlock();

	/**
	 * Read the next character for a string
	 */
	char readMsgChar();
protected:
	/**
	 * Returns true if an object has a given noun
	 */
	bool hasNoun(int obj, int noun) const;

	/**
	 * Returns true if an object has a given adjective
	 */
	bool hasAdjective(int obj, int adjective) const;
public:
	Common::Array<byte> _data;
	int _residentOffset;
	int _wordCount;
	int _objectCount;
	int _actionCount;
	int _variableCount;

	byte *_wordTable;
	byte *_wordTypeTable;
	byte *_objectTable;
	byte *_actionTable;
	byte *_variableTable;
	byte *_saveArea;
	byte *_dataSpace;
	byte *_codeSpace;
public:
	/**
	 * Constructor
	 */
	Game();

	/**
	 * Destructor
	 */
	~Game();

	/**
	 * init data for the game
	 */
	bool init(Common::SeekableReadStream *s);

	/**
	 * Restore savegame data from the game to it's initial state
	 */
	void restart();

	/**
	 * Returns true if the game is restarting, and resets the flag
	 */
	bool shouldRestart();

	/**
	 * Save the game data to a savegame
	 */
	void saveGameData(Common::WriteStream &ws);

	/**
	 * Restore the game data from a savegame
	 */
	void loadGameData(Common::ReadStream &rs);

	/**
	 * Find a word in the dictionary
	 */
	int findWord(const Common::String &word) const;

	/**
	 * Return a word's type
	 */
	WordType getWordType(int word) const {
		return (WordType)_wordTypeTable[word];
	}

	/**
	 * Check to see if this is a valid verb
	 */
	int checkVerb(const Common::Array<int> &verbs);

	/**
	 * Find an action matching a given description
	 */
	int findAction(const Common::Array<int> &verbs, int preposition, int flag);

	/**
	 * Get an object property
	 */
	int getObjectProperty(int obj, int prop);

	/**
	 * Sets an object property
	 */
	int setObjectProperty(int obj, int prop, int val);

	/**
	 * Gets a field from an object
	 */
	int getObjectField(int obj, int offset) const {
		return READ_LE_UINT16(_dataSpace + getObjectLocation(obj) + offset);
	}

	/**
	 * Sets a field in an object
	 */
	int setObjectField(int obj, int offset, int val) {
		WRITE_LE_UINT16(_dataSpace + getObjectLocation(obj) + offset, val);
		return val;
	}

	/**
	 * Gets a field from an action
	 */
	int getActionField(int action, int offset) const {
		return READ_LE_UINT16(_dataSpace + getActionLocation(action) + offset);
	}

	/**
	 * Gets a byte field from an action
	 */
	int getActionByte(int action, int offset) const {
		return _dataSpace[getActionLocation(action) + offset];
	}

	/**
	 * Gets the offset of an object from the object table
	 */
	int getObjectLocation(int obj) const;

	/**
	 * Gets the offset of an action from the action table
	 */
	int getActionLocation(int action) const;

	/**
	 * Get a variable value
	 */
	int getVariable(int variableNum);

	/**
	 * Set a variable value
	 */
	void setVariable(int variableNum, int value);

	/**
	 * Gets a code byte
	 */
	int getCodeByte(int offset) const {
		return _codeSpace[offset];
	}

	/**
	 * Gets a code byte
	 */
	int getCodeWord(int offset) const {
		return READ_LE_UINT16(_codeSpace + offset);
	}

	/**
	 * Read a word
	 */
	int readWord(int offset) const {
		return READ_LE_UINT16(_dataSpace + offset);
	}

	/**
	 * Write a word
	 */
	void writeWord(int offset, int val) {
		WRITE_LE_UINT16(_dataSpace + offset, val);
	}

	/**
	 * Read a string from the messages section
	 */
	Common::String readString(int msg);
};

} // End of namespace AdvSys
} // End of namespace Glk

#endif