aboutsummaryrefslogtreecommitdiff
path: root/kyra/script.cpp
blob: 99e9999d543be1e0a61b4380417d62f7afe44ad3 (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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/* ScummVM - Kyrandia Interpreter
 * Copyright (C) 2003-2004 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "kyra.h"
#include "script.h"
#include "resource.h"

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

#define COMMAND(x) { &VMContext::x, #x }
#define OPCODE(x) { &VMContext::x, #x }

namespace Kyra {
VMContext::VMContext(KyraEngine* engine) {
	_engine = engine;
	_error = false;
		
	// now we create a list of all Command/Opcode procs and so
	static CommandEntry commandProcs[] = {
		// 0x00
		COMMAND(c1_goToLine),
		COMMAND(c1_setReturn),
		COMMAND(c1_pushRetRec),
		COMMAND(c1_push),
		// 0x04
		COMMAND(c1_push),			
		COMMAND(c1_pushVar),
		COMMAND(c1_pushFrameNeg),
		COMMAND(c1_pushFramePos),
		// 0x08
		COMMAND(c1_popRetRec),
		COMMAND(c1_popVar),
		COMMAND(c1_popFrameNeg),
		COMMAND(c1_popFramePos),
		// 0x0C
		COMMAND(c1_addToSP),
		COMMAND(c1_subFromSP),
		COMMAND(c1_execOpcode),
		COMMAND(c1_ifNotGoTo),
		// 0x10
		COMMAND(c1_negate),
		COMMAND(c1_evaluate)
	};
	_numCommands = ARRAYSIZE(commandProcs);
	_commands = commandProcs;
		
	static OpcodeEntry opcodeProcs[] = {
		// 0x00
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x04
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x08
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x0C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x10
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x14
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x18
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x1C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x20
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x24
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x28
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x2C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x30
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x34
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x38
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x3C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x40
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x44
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x48
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x4C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x50
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x54
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x58
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x5C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x60
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x64
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x68
		OPCODE(o1_0x68),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x6C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x70
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x74
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x78
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x7C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x80
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x84
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x88
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x8C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x90
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x94
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x98
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0x9C
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xA0
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xA4
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xA8
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xAC
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xB0
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xB4
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xB8
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xBC
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xC0
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xC4
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xC8
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xCC
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xD0
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xD4
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xD8
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xDC
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xE0
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xE4
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xE8
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xEC
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xF0
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xF4
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xF8
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		// 0xFC
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode),
		OPCODE(o1_unknownOpcode)
	};
	_numOpcodes = ARRAYSIZE(opcodeProcs);
	_opcodes = opcodeProcs;

	_scriptFile = NULL;
	_scriptFileSize = 0;
}
	
void VMContext::loadScript(const char* file) {
	if (_scriptFile) {
		delete [] _scriptFile;
		_scriptFileSize = 0;
	}
		
	memset(_stack, 0, sizeof(int32) * ARRAYSIZE(_stack));

	// loads the new file
	_scriptFile = _engine->resManager()->fileData(file, &_scriptFileSize);
	
	if (!_scriptFileSize || !_scriptFile) {
		error("couldn't load script file '%s'", file);
	}
	
	Common::MemoryReadStream script(_scriptFile, _scriptFileSize);
	memset(_chunks, 0, sizeof(ScriptChunk) * kCountChunkTypes);
	uint8 chunkName[sizeof("EMC2ORDR") + 1];
	
	// so lets look for our chunks :)
	while(true) {
		if (script.eof()) {
			break;
		}
		// lets read only the first 4 chars
		script.read(chunkName, sizeof(uint8) * 4);
		chunkName[4] = '\0';
			
		// check name of chunk
		if (!scumm_stricmp((const char *)chunkName, "FORM")) {			
			// FreeKyra swaps the size I only read it in BigEndian :)
			_chunks[kForm]._size = script.readUint32BE();
		} else if (!scumm_stricmp((const char *)chunkName, "TEXT")) {
			uint32 text_size = script.readUint32BE();
			text_size += text_size % 2 != 0 ? 1 : 0;
			
			_chunks[kText]._data = _scriptFile + script.pos();
			_chunks[kText]._size = READ_BE_UINT16(_chunks[kText]._data) >> 1;
			_chunks[kText]._additional = _chunks[kText]._data + (_chunks[kText]._size << 1);
			script.seek(script.pos() + text_size);
		} else if (!scumm_stricmp((const char *)chunkName, "DATA")) {
			_chunks[kData]._size = script.readUint32BE();
			_chunks[kData]._data = _scriptFile + script.pos();
			// mostly it will be the end of the file because all files should end with a 'DATA' chunk
			script.seek(script.pos() + _chunks[kData]._size);
		} else {
			// read next 4 chars
			script.read(&chunkName[4], sizeof(uint8) * 4);
			chunkName[8] = '\0';
			
			if (!scumm_stricmp((const char *)chunkName, "EMC2ORDR")) {
				_chunks[kEmc2Ordr]._size = script.readUint32BE() >> 1;
				_chunks[kEmc2Ordr]._data = _scriptFile + script.pos();
				script.seek(script.pos() + _chunks[kEmc2Ordr]._size * 2);
			} else {
				// any unkown chunk or problems with seeking through the file
				error("unknown chunk(%s)", chunkName);
			}
		}
	}
}
	
int32 VMContext::param(int32 index) {
	if (_stackPos - index - 1 >= ARRAYSIZE(_stack) || _stackPos - index - 1 < 0)
		return -0xFFFF;
	return _stack[_stackPos - index - 1];
}
	
const char* VMContext::stringAtIndex(int32 index) {
	if (index < 0 || (uint32)index >= _chunks[kText]._size)
		return 0;
	
	return (const char *)(_chunks[kText]._additional + _chunks[kText]._data[index]);
}
	
bool VMContext::startScript(int32 func) {
	if ((uint32)func >= _chunks[kEmc2Ordr]._size || func < 0) {
		debug("script doesn't support function %d", func);
		return false;
	}
			
	_instructionPos = READ_BE_UINT16(&_chunks[kEmc2Ordr]._data[func]) << 1;
	_stackPos = 0;
	_tempPos = 0;
	_delay = 0;
	_error = false;
	_scriptState = kScriptRunning;
	
	uint32 pos = 0xFFFFFFFE;
		
	// get start of next script
	for (uint32 tmp = 0; tmp < _chunks[kEmc2Ordr]._size; ++tmp) {
		if ((uint32)((READ_BE_UINT16(&_chunks[kEmc2Ordr]._data[tmp]) << 1)) > (uint32)_instructionPos &&
			(uint32)((READ_BE_UINT16(&_chunks[kEmc2Ordr]._data[tmp]) << 1)) < pos) {
			pos = ((READ_BE_UINT16(&_chunks[kEmc2Ordr]._data[tmp]) << 1));
		}
	}
		
	if (pos > _scriptFileSize) {
		pos = _scriptFileSize;
	}
		
	_nextScriptPos = pos;

	return true;
}
	
uint32 VMContext::contScript(void) {
	uint8* script_start = _chunks[kData]._data;
	assert(script_start);
	
	uint32 scriptStateAtStart = _scriptState;
		
	// runs the script
	while(true) {
		if ((uint32)_instructionPos > _chunks[kData]._size) {
			debug("_instructionPos( = %d) > _chunks[kData]._size( = %d)", _instructionPos, _chunks[kData]._size);
			_error = true;
			break;
		} else if(_instructionPos >= _nextScriptPos) {
			_scriptState = kScriptStopped;
			break;
		}
			
		_currentCommand = *(script_start + _instructionPos++);
		
		// gets out 
		if (_currentCommand & 0x80) {
			_argument = ((_currentCommand & 0x0F) << 8) | *(script_start + _instructionPos++);
			_currentCommand &= 0xF0;
		} else if (_currentCommand & 0x40) {
			_argument = *(script_start + _instructionPos++);
		} else if (_currentCommand & 0x20) {
			_instructionPos++;
			
			uint16 tmp = *(uint16*)(script_start + _instructionPos);
			tmp &= 0xFF7F;
			
			_argument = READ_BE_UINT16(&tmp);
			_instructionPos += 2;
		} else {
			debug("unknown way of getting the command (0x%X)", _currentCommand);
			// next thing
			continue;
		}
		
		_currentCommand &= 0x1f;
			
		if (_currentCommand < _numCommands) {
			CommandProc currentProc = _commands[_currentCommand].proc;
			(this->*currentProc)();
		} else {
			c1_unknownCommand();
		}
		
		if (_error) {
			_scriptState = kScriptError;
			break;
		}
			
		if (scriptStateAtStart != _scriptState) {
			break;
		}
	}

	return _scriptState;
}
} // end of namespace Kyra