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
|
/* 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$
*
*/
#include "kyra/script_tim.h"
#include "kyra/script.h"
#include "kyra/resource.h"
#include "common/endian.h"
namespace Kyra {
TIMInterpreter::TIMInterpreter(KyraEngine_v1 *vm, OSystem *system) : _vm(vm), _system(system), _currentTim(0) {
#define COMMAND(x) { &TIMInterpreter::x, #x }
#define COMMAND_UNIMPL() { 0, 0 }
static CommandEntry commandProcs[] = {
// 0x00
COMMAND(cmd_initFunc0),
COMMAND(cmd_stopCurFunc),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
// 0x04
COMMAND(cmd_initFunc),
COMMAND(cmd_stopFunc),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
// 0x08
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
// 0x0C
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
// 0x10
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
// 0x14
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND_UNIMPL(),
COMMAND(cmd_resetAllRuntimes),
// 0x18
COMMAND(cmd_return<1>),
COMMAND(cmd_execOpcode),
COMMAND(cmd_initFuncNow),
COMMAND(cmd_stopFuncNow),
// 0x1C
COMMAND(cmd_return<1>),
COMMAND(cmd_return<1>),
COMMAND(cmd_return<-1>)
};
_commands = commandProcs;
_commandsSize = ARRAYSIZE(commandProcs);
}
TIM *TIMInterpreter::load(const char *filename, const Common::Array<const TIMOpcode*> *opcodes) {
if (!_vm->resource()->exists(filename))
return 0;
ScriptFileParser file(filename, _vm->resource());
if (!file)
error("Couldn't open TIM file '%s'", filename);
uint32 formBlockSize = file.getFORMBlockSize();
if (formBlockSize == 0xFFFFFFFF)
error("No FORM chunk found in TIM file '%s'", filename);
if (formBlockSize < 20)
error("TIM file '%s' FORM chunk size smaller than 20", filename);
TIM *tim = new TIM;
assert(tim);
memset(tim, 0, sizeof(TIM));
tim->procFunc = -1;
tim->opcodes = opcodes;
uint32 avtlChunkSize = file.getIFFBlockSize(AVTL_CHUNK);
uint32 textChunkSize = file.getIFFBlockSize(TEXT_CHUNK);
tim->avtl = new uint16[avtlChunkSize/2];
if (textChunkSize != 0xFFFFFFFF)
tim->text = new byte[textChunkSize];
if (!file.loadIFFBlock(AVTL_CHUNK, tim->avtl, avtlChunkSize))
error("Couldn't read AVTL chunk in TIM file '%s'", filename);
if (textChunkSize != 0xFFFFFFFF && !file.loadIFFBlock(TEXT_CHUNK, tim->text, textChunkSize))
error("Couldn't read TEXT chunk in TIM file '%s'", filename);
avtlChunkSize >>= 1;
for (uint i = 0; i < avtlChunkSize; ++i)
tim->avtl[i] = READ_LE_UINT16(tim->avtl + i);
int num = (avtlChunkSize < TIM::kCountFuncs) ? avtlChunkSize : (int)TIM::kCountFuncs;
for (int i = 0; i < num; ++i)
tim->func[i].avtl = tim->avtl + tim->avtl[i];
return tim;
}
void TIMInterpreter::unload(TIM *&tim) const {
if (!tim)
return;
delete[] tim->text;
delete[] tim->avtl;
delete tim;
tim = 0;
}
void TIMInterpreter::exec(TIM *tim, bool loop) {
if (!tim)
return;
_currentTim = tim;
if (!_currentTim->func[0].ip) {
_currentTim->func[0].ip = _currentTim->func[0].avtl;
_currentTim->func[0].nextTime = _currentTim->func[0].lastTime = _system->getMillis();
}
do {
for (_currentFunc = 0; _currentFunc < TIM::kCountFuncs; ++_currentFunc) {
TIM::Function &cur = _currentTim->func[_currentFunc];
if (_currentTim->procFunc != -1)
execCommand(28, &_currentTim->procParam);
bool running = true;
while (cur.ip && cur.nextTime <= _system->getMillis() && running) {
int8 opcode = int8(cur.ip[2] & 0xFF);
switch (execCommand(opcode, cur.ip + 3)) {
case -1:
loop = false;
running = false;
_currentFunc = 11;
break;
case -2:
running = false;
break;
case -3:
_currentTim->procFunc = _currentFunc;
break;
default:
break;
}
if (cur.ip) {
cur.ip += cur.ip[0];
cur.lastTime = cur.nextTime;
cur.nextTime += cur.ip[1] * _vm->tickLength();
}
}
}
} while (loop);
}
void TIMInterpreter::refreshTimersAfterPause(uint32 elapsedTime) {
if (!_currentTim)
return;
for (int i = 0; i < TIM::kCountFuncs; i++) {
if (_currentTim->func[i].lastTime)
_currentTim->func[i].lastTime += elapsedTime;
if (_currentTim->func[i].nextTime)
_currentTim->func[i].nextTime += elapsedTime;
}
}
int TIMInterpreter::execCommand(int cmd, const uint16 *param) {
if (cmd < 0 || cmd >= _commandsSize) {
warning("Calling unimplemented TIM command %d", cmd);
return 0;
}
if (_commands[cmd].proc == 0) {
warning("Calling unimplemented TIM command %d", cmd);
return 0;
}
debugC(5, kDebugLevelScript, "TIMInterpreter::%s(%p)", _commands[cmd].desc, (const void*)param);
return (this->*_commands[cmd].proc)(param);
}
int TIMInterpreter::cmd_initFunc0(const uint16 *param) {
_currentTim->func[0].ip = _currentTim->func[0].avtl;
_currentTim->func[0].lastTime = _system->getMillis();
return 1;
}
int TIMInterpreter::cmd_stopCurFunc(const uint16 *param) {
if (_currentFunc < TIM::kCountFuncs)
_currentTim->func[_currentFunc].ip = 0;
if (!_currentFunc)
_finished = true;
return -2;
}
int TIMInterpreter::cmd_initFunc(const uint16 *param) {
uint16 func = *param;
assert(func < TIM::kCountFuncs);
if (_currentTim->func[func].avtl)
_currentTim->func[func].ip = _currentTim->func[func].avtl;
else
_currentTim->func[func].avtl = _currentTim->func[func].ip = _currentTim->avtl + _currentTim->avtl[func];
return 1;
}
int TIMInterpreter::cmd_stopFunc(const uint16 *param) {
uint16 func = *param;
assert(func < TIM::kCountFuncs);
_currentTim->func[func].ip = 0;
return 1;
}
int TIMInterpreter::cmd_resetAllRuntimes(const uint16 *param) {
for (int i = 0; i < TIM::kCountFuncs; ++i) {
if (_currentTim->func[i].ip)
_currentTim->func[i].nextTime = _system->getMillis();
}
return 1;
}
int TIMInterpreter::cmd_execOpcode(const uint16 *param) {
if (!_currentTim->opcodes) {
warning("Trying to execute TIM opcode without opcode list");
return 0;
}
uint16 opcode = *param++;
if (opcode > _currentTim->opcodes->size()) {
warning("Calling unimplemented TIM opcode(0x%.02X/%d)", opcode, opcode);
return 0;
}
return (*(*_currentTim->opcodes)[opcode])(_currentTim, param);
}
int TIMInterpreter::cmd_initFuncNow(const uint16 *param) {
uint16 func = *param;
assert(func < TIM::kCountFuncs);
_currentTim->func[func].ip = _currentTim->func[func].avtl;
_currentTim->func[func].lastTime = _currentTim->func[func].nextTime = _system->getMillis();
return 1;
}
int TIMInterpreter::cmd_stopFuncNow(const uint16 *param) {
uint16 func = *param;
assert(func < TIM::kCountFuncs);
_currentTim->func[func].ip = 0;
_currentTim->func[func].lastTime = _currentTim->func[func].nextTime = _system->getMillis();
return 1;
}
} // end of namespace Kyra
|