aboutsummaryrefslogtreecommitdiff
path: root/engines/m4/script.h
blob: 20129263307caa3d2868046796e18dae59902c4d (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
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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#ifndef M4_SCRIPT_H
#define M4_SCRIPT_H

#include "common/file.h"
#include "common/stream.h"
#include "common/hashmap.h"
#include "common/str.h"
#include "common/stack.h"

#include "m4/woodscript.h"

namespace M4 {

const unsigned long kScriptFileMagic = 0x5845344D;
const unsigned long kScriptFileVersion = 1;

enum ScriptValueType {
	kInteger,
	kConstString,
	kLogicVar,
	kLogicVarRef,
	kGameVar,
	kKernelVar,
	kDataRef,
	kRegister,
	kStackVar
};

enum ScriptDataType {
	kStreamBreakSeries,
	kStreamPlaySeries,
	kSaidArray,
	kParserArray,
	kSpeechArray,
	kCreditsArray,
	kInvObj,
	kMineRoom,
	kButtonItem
};

class ScriptInterpreter;

class StringTable {
public:
	StringTable();
	~StringTable();
	void load(Common::File *fd);
	int size() { return _strings.size(); }
	const char *operator[](uint32 index) const {
		assert(index < _strings.size() );
		return _strings[index];
	}
protected:
	Common::Array<const char*> _strings;
	char *_stringsData;
};

struct ScriptValue {

	ScriptValueType type;

	union {
		int value;
	};

	ScriptValue() : type(kInteger), value(0) {}
	ScriptValue(ScriptValueType itype, int ivalue) : type(itype), value(ivalue) {}

	ScriptValue(const int intValue) : type(kInteger), value(intValue) {}

	ScriptValue& operator=(const int intValue) {
		type = kInteger;
		value = intValue;
		return *this;
	}

};

class ScriptDataItem {
public:
	ScriptDataItem() : _inter(NULL) {}
	ScriptDataItem(ScriptInterpreter *inter) : _inter(inter) {}
	virtual ~ScriptDataItem() {}
	virtual void load(Common::File *fd) = 0;
	static int type() { return -1; }
protected:
	ScriptInterpreter *_inter;
};

class ScriptDataCache {
public:
	ScriptDataCache(ScriptInterpreter *inter) : _inter(inter) {
	}
	~ScriptDataCache() {
		clear();
	}

	// WORKAROUND: The old prototype for this function was:
	// template<class T> T *load(Common::File *fd, uint32 ofs);
	// that caused a parser error in g++ 3.3.6 used by our
	// "motoezx" target of our buildbot. The actual parser
	// error happended, when calling the function like this:
	// "T *result = _dataCache->load<T>(_scriptFile, _data[value.value]->offset);"
	// in ScriptInterpreter::toData. To work around this
	// we moved the return value as parameter instead.
	template<class T>
	void load(Common::File *fd, uint32 ofs, T *&item) {
		if (_cache.contains(ofs)) {
			item = (T*)(_cache[ofs]);
		} else {
			item = new T(_inter);
			fd->seek(ofs + 4); // "+4" skips the data size
			item->load(fd);
			_cache[ofs] = item;
		}
	}
	void clear() {
		// TODO: Free all cached items
	}
protected:
	typedef Common::HashMap<uint32, ScriptDataItem*> CacheMap;
	CacheMap _cache;
	ScriptInterpreter *_inter;
};

struct SeriesStreamBreakItem {
	int frameNum;
	const char *digiName;
	int digiChannel;
	int digiVolume;
	int trigger;
	int flags;
	ScriptValue variable;
	int value;
};

class SeriesStreamBreakList : public ScriptDataItem {
public:
	SeriesStreamBreakList(ScriptInterpreter *inter) : ScriptDataItem(inter) {}
	~SeriesStreamBreakList();
	void load(Common::File *fd);
	int size() const { return _items.size(); }
	SeriesStreamBreakItem *operator[](int index) const { return _items[index]; }
	static int type() { return 0; }
protected:
	Common::Array<SeriesStreamBreakItem*> _items;
};

struct SaidArrayItem {
	const char *itemName;
	const char *digiNameLook;
	const char *digiNameTake;
	const char *digiNameGear;
};

class SaidArray : public ScriptDataItem {
public:
	SaidArray(ScriptInterpreter *inter) : ScriptDataItem(inter) {}
	~SaidArray();
	void load(Common::File *fd);
	int size() const { return _items.size(); }
	SaidArrayItem *operator[](int index) const { return _items[index]; }
	static int type() { return 2; }
protected:
	Common::Array<SaidArrayItem*> _items;
};

struct ParserArrayItem {
	const char *w0;
	const char *w1;
	int trigger;
	ScriptValue testVariable;
	int testValue;
	ScriptValue variable;
	int value;
};

class ParserArray : public ScriptDataItem {
public:
	ParserArray(ScriptInterpreter *inter) : ScriptDataItem(inter) {}
	~ParserArray();
	void load(Common::File *fd);
	int size() const { return _items.size(); }
	ParserArrayItem *operator[](int index) const { return _items[index]; }
	static int type() { return 3; }
protected:
	Common::Array<ParserArrayItem*> _items;
};

class ScriptFunction {
public:
	ScriptFunction(ScriptInterpreter *inter);
	~ScriptFunction();
	void load(Common::File *fd);
	void jumpAbsolute(uint32 ofs);
	void jumpRelative(int32 ofs);
	byte readByte();
	uint32 readUint32();
protected:
	ScriptInterpreter *_inter;
	Common::MemoryReadStream *_code;
};

struct ScriptFunctionEntry {
	uint32 offset;
	ScriptFunction *func;
	ScriptFunctionEntry(uint32 funcOffset) : offset(funcOffset), func(NULL) {
	}
};

struct ScriptDataEntry {
	uint32 offset;
	ScriptDataType type;
	ScriptDataEntry(uint32 dataOffset, ScriptDataType dataType) : offset(dataOffset), type(dataType) {
	}
};

enum ScriptKernelVariable {
	kGameLanguage,
	kGameVersion,
	kGameCurrentRoom,
	kGameNewRoom,
	kGamePreviousRoom,
	kGameNewSection,
	kKernelTrigger,
	kKernelTriggerMode,
	kKernelFirstFade,
	kKernelSuppressFadeUp,
	kKernelContinueHandlingTrigger,
	kKernelUseDebugMonitor,
	kPlayerPosX,
	kPlayerPosY,
	kPlayerFacing,
	kPlayerScale,
	kPlayerDepth,
	kPlayerWalkX,
	kPlayerWalkY,
	kPlayerReadyToWalk,
	kPlayerNeedToWalk,
	kPlayerCommandReady,
	kPlayerWalkerInThisScene,
	kPlayerVerb,
	kWalkerInitialized,
	kCallDaemonEveryLoop,
	kConvCurrentTalker,
	kConvCurrentNode,
	kConvCurrentEntry,
	kConvSoundToPlay,
	kInterfaceVisible
};

class ScriptInterpreter {
public:
	ScriptInterpreter(MadsM4Engine *vm);
	~ScriptInterpreter();
	/* Opens a M4 program file */
	void open(const char *filename);
	void close();
	/* Loads a function via the index. Creates the function object if it's not already loaded. */
	ScriptFunction *loadFunction(uint32 index);
	/* Loads a function via the exported name. */
	ScriptFunction *loadFunction(const Common::String &name);
	/* Unload all loaded functions.
	   This should be called before entering a new room to free unused functions. */
	void unloadFunctions();
	//TODO void unloadData();
	/* Executes a function. */
	int runFunction(ScriptFunction *scriptFunction);

	void push(const ScriptValue &value);
	void pop(ScriptValue &value);
	void dumpStack();
	void dumpRegisters();
	void dumpGlobalVars();

	int toInteger(const ScriptValue &value);

	const char *toString(const ScriptValue &value);

	// Is this ok?
	template<class T>
	const T& toData(const ScriptValue &value) {
		debugCN(kDebugScript, "ScriptInterpreter::toData() index = %d; type = %d; max = %d\n", value.value, _data[value.value]->type, _data.size());
		assert((uint32)value.value < _data.size());
		T *result = 0;
		_dataCache->load(_scriptFile, _data[value.value]->offset, result);
		return *result;
	}

	const char *getGlobalString(int index) const {
		return _constStrings[index];
	}

	const char *loadGlobalString(Common::File *fd);

	void test();

protected:

	MadsM4Engine *_vm;

	typedef Common::HashMap<Common::String, uint32, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> FunctionNameMap;
	Common::File *_scriptFile;
	/* An array of offset/ScriptFunction* pairs for each script function */
	Common::Array<ScriptFunctionEntry*> _functions;

	// DEBUG only
	Common::Array<Common::String> _scriptFunctionNames;

	Common::Array<ScriptDataEntry*> _data;
	/* Maps function name -> index of function in _functions array */
	FunctionNameMap _functionNames;
	StringTable _constStrings;
	/* The currently running function */
	ScriptFunction *_runningFunction;
	int _localStackPtr;

	ScriptValue _registers[8];

	ScriptValue _stack[512];
	int _stackPtr;

	int _globalVarCount;
	ScriptValue _globalVars[1024];

	int _logicGlobals[512];

	int _cmpFlags;

	ScriptDataCache *_dataCache;

	int _lineNum;

	typedef int (ScriptInterpreter::*KernelFunction)();
	struct KernelFunctionEntry {
		KernelFunction proc;
		const char *desc;
	};
	const KernelFunctionEntry *_kernelFunctions;
	uint16 _kernelFunctionsMax;

	struct KernelVariableEntry {
		ScriptKernelVariable var;
		const char *desc;
	};
	const KernelVariableEntry *_kernelVars;
	int16 _kernelVarsMax;

	void initScriptKernel();

	void loadValue(ScriptValue &value);
	void writeValue(ScriptValue &value);
	void copyValue(ScriptValue &destValue, ScriptValue &sourceValue);
	void derefValue(ScriptValue &value);

	void callKernelFunction(uint32 index);
	ScriptValue getArg(uint32 index);
	void dumpArgs(uint32 count);

	void callFunction(uint32 index);

	bool execOpcode(byte opcode);

	// Kernel functions
	int o1_handleStreamBreak();
	int o1_handlePlayBreak();
	int o1_dispatchTriggerOnSoundState();
	int o1_getRangedRandomValue();
	int o1_getTicks();
	int o1_preloadSound();
	int o1_unloadSound();
	int o1_stopSound();
	int o1_playSound();
	int o1_playLoopingSound();
	int o1_setSoundVolume();
	int o1_getSoundStatus();
	int o1_getSoundDuration();
	int o1_loadSeries();
	int o1_unloadSeries();
	int o1_showSeries();
	int o1_playSeries();
	int o1_setSeriesFrameRate();
	int o1_playBreakSeries();
	int o1_preloadBreakSeries();
	int o1_unloadBreakSeries();
	int o1_startBreakSeries();
	int o1_dispatchTrigger();
	int o1_terminateMachine();
	int o1_sendWoodScriptMessage();
	int o1_runConversation();
	int o1_loadConversation();
	int o1_exportConversationValue();
	int o1_exportConversationPointer();
	int o1_fadeInit();
	int o1_fadeSetStart();
	int o1_fadeToBlack();
	int o1_initPaletteCycle();
	int o1_stopPaletteCycle();
	int o1_setHotspot();
	int o1_hideWalker();
	int o1_showWalker();
	int o1_setWalkerLocation();
	int o1_setWalkerFacing();
	int o1_walk();
	int o1_overrideCrunchTime();
	int o1_addBlockingRect();
	int o1_triggerTimerProc();
	int o1_setPlayerCommandsAllowed();
	int o1_getPlayerCommandsAllowed();
	int o1_updatePlayerInfo();
	int o1_hasPlayerSaid();
	int o1_hasPlayerSaidAny();
	int o1_playerHotspotWalkOverride();
	int o1_setPlayerFacingAngle();
	int o1_disablePlayerFadeToBlack();
	int o1_enablePlayer();
	int o1_disablePlayer();
	int o1_freshenSentence();
	int o1_playerHasItem();
	int o1_playerGiveItem();
	int o1_moveObject();
	int o1_setStopSoundsBetweenRooms();
	int o1_backupPalette();
	int o1_unloadWilburWalker();
	int o1_globalTriggerProc();
	int o1_wilburSpeech();
	int o1_wilburSaid();
	int o1_wilburParse();
	int o1_wilburTalk();
	int o1_wilburFinishedTalking();
	//int ();

	// Kernel vars
	void getKernelVar(int index, ScriptValue &value);
	void setKernelVar(int index, const ScriptValue &value);

};

} // End of namespace M4

#endif