aboutsummaryrefslogtreecommitdiff
path: root/engines/illusions/scriptman.cpp
blob: c438def393a89cf44c9df1469609efc1287a83e6 (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
/* 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 "illusions/illusions.h"
#include "illusions/scriptman.h"
#include "illusions/abortablethread.h"
#include "illusions/actor.h"
#include "illusions/camera.h"
#include "illusions/scriptthread.h"
#include "illusions/scriptopcodes.h"
#include "illusions/talkthread.h"
#include "illusions/timerthread.h"

namespace Illusions {

// ActiveScenes

ActiveScenes::ActiveScenes() {
	clear();
}

void ActiveScenes::clear() {
	_stack.clear();
}

void ActiveScenes::push(uint32 sceneId) {
	ActiveScene activeScene;
	activeScene._sceneId = sceneId;
	activeScene._pauseCtr = 0;
	_stack.push(activeScene);
}

void ActiveScenes::pop() {
	_stack.pop();
}

void ActiveScenes::pauseActiveScene() {
	++_stack.top()._pauseCtr;
}

void ActiveScenes::unpauseActiveScene() {
	--_stack.top()._pauseCtr;
}

uint ActiveScenes::getActiveScenesCount() {
	return _stack.size();
}

void ActiveScenes::getActiveSceneInfo(uint index, uint32 *sceneId, int *pauseCtr) {
	if (sceneId)
		*sceneId = _stack[index - 1]._sceneId;
	if (pauseCtr)
		*pauseCtr = _stack[index - 1]._pauseCtr;
}

uint32 ActiveScenes::getCurrentScene() {
	if (_stack.size() > 0)
		return _stack.top()._sceneId;
	return 0;
}

bool ActiveScenes::isSceneActive(uint32 sceneId) {
	for (uint i = 0; i < _stack.size(); ++i)
		if (_stack[i]._sceneId == sceneId && _stack[i]._pauseCtr <= 0)
			return true;
	return false;
}

// TriggerFunction

TriggerFunction::TriggerFunction(uint32 sceneId, uint32 verbId, uint32 objectId2, uint32 objectId, TriggerFunctionCallback *callback)
	: _sceneId(sceneId), _verbId(verbId), _objectId2(objectId2), _objectId(objectId), _callback(callback) {
}

TriggerFunction::~TriggerFunction() {
	delete _callback;
}

void TriggerFunction::run(uint32 callingThreadId) {
	(*_callback)(this, callingThreadId);
}

// TriggerFunctions

void TriggerFunctions::add(uint32 sceneId, uint32 verbId, uint32 objectId2, uint32 objectId, TriggerFunctionCallback *callback) {
	ItemsIterator it = findInternal(sceneId, verbId, objectId2, objectId);
	if (it != _triggerFunctions.end()) {
		delete *it;
		_triggerFunctions.erase(it);
	}
	_triggerFunctions.push_back(new TriggerFunction(sceneId, verbId, objectId2, objectId, callback));
}

TriggerFunction *TriggerFunctions::find(uint32 sceneId, uint32 verbId, uint32 objectId2, uint32 objectId) {
	ItemsIterator it = findInternal(sceneId, verbId, objectId2, objectId);
	if (it != _triggerFunctions.end())
		return (*it);
	return 0;
}

void TriggerFunctions::removeBySceneId(uint32 sceneId) {
	ItemsIterator it = _triggerFunctions.begin();
	while (it != _triggerFunctions.end()) {
		if ((*it)->_sceneId == sceneId) {
			delete *it;
			it = _triggerFunctions.erase(it);
		} else
			++it;
	}
}

TriggerFunctions::ItemsIterator TriggerFunctions::findInternal(uint32 sceneId, uint32 verbId, uint32 objectId2, uint32 objectId) {
	ItemsIterator it = _triggerFunctions.begin();
	for (; it != _triggerFunctions.end(); ++it) {
		TriggerFunction *triggerFunction = *it;
		if (triggerFunction->_sceneId == sceneId && triggerFunction->_verbId == verbId &&
			triggerFunction->_objectId2 == objectId2 && triggerFunction->_objectId == objectId)
			break;
	}
	return it;		
}

// ScriptStack

ScriptStack::ScriptStack() {
	clear();
}

void ScriptStack::clear() {
	for (uint i = 0; i < 256; ++i)
		_stack[i] = (int16)0xEEEE;
	_stackPos = 256;
}

void ScriptStack::push(int16 value) {
	--_stackPos;
	if (_stackPos > 0)
		_stack[_stackPos] = value;
}

int16 ScriptStack::pop() {
	int16 value = 0;
	if (_stackPos < 256) {
		value = _stack[_stackPos];
		_stack[_stackPos] = (int16)0xEEEE;
		++_stackPos;
	}
	return value;
}

int16 ScriptStack::peek() {
	int16 value = 0;
	if (_stackPos < 256)
		value = _stack[_stackPos];
	return value;
}

int16 *ScriptStack::topPtr() {
	return &_stack[_stackPos];
}

// ScriptMan

ScriptMan::ScriptMan(IllusionsEngine *vm)
	: _vm(vm), _pauseCtr(0), _doScriptThreadInit(false) {
	_threads = new ThreadList(vm);
	_scriptOpcodes = new ScriptOpcodes(vm);
	_field8 = 1;
	_fieldA = 0;
	_fieldE = 240;
}

ScriptMan::~ScriptMan() {
	delete _threads;
	delete _scriptOpcodes;
}

void ScriptMan::setSceneIdThreadId(uint32 theSceneId, uint32 theThreadId) {
	_theSceneId = theSceneId;
	_theThreadId = theThreadId;
}

void ScriptMan::startScriptThread(uint32 threadId, uint32 callingThreadId,
	uint32 value8, uint32 valueC, uint32 value10) {
	debug(2, "Starting script thread %08X", threadId);
	byte *scriptCodeIp = _scriptResource->getThreadCode(threadId);
	newScriptThread(threadId, callingThreadId, 0, scriptCodeIp, value8, valueC, value10);
}

void ScriptMan::startAnonScriptThread(int32 threadId, uint32 callingThreadId,
	uint32 value8, uint32 valueC, uint32 value10) {
	debug(2, "Starting anonymous script thread %08X", threadId);
	uint32 tempThreadId = newTempThreadId();
	byte *scriptCodeIp = _scriptResource->getThreadCode(threadId);
	scriptCodeIp = _scriptResource->getThreadCode(threadId);
	newScriptThread(tempThreadId, callingThreadId, 0, scriptCodeIp, value8, valueC, value10);
}

uint32 ScriptMan::startTempScriptThread(byte *scriptCodeIp, uint32 callingThreadId,
	uint32 value8, uint32 valueC, uint32 value10) {
	uint32 tempThreadId = newTempThreadId();
	debug(2, "Starting temp script thread %08X", tempThreadId);
	newScriptThread(tempThreadId, callingThreadId, 0, scriptCodeIp, value8, valueC, value10);
	return tempThreadId;
}

uint32 ScriptMan::startAbortableTimerThread(uint32 duration, uint32 threadId) {
	return newTimerThread(duration, threadId, true);
}

uint32 ScriptMan::startTimerThread(uint32 duration, uint32 threadId) {
	return newTimerThread(duration, threadId, false);
}

uint32 ScriptMan::startAbortableThread(byte *scriptCodeIp1, byte *scriptCodeIp2, uint32 callingThreadId) {
	uint32 tempThreadId = newTempThreadId();
	debug(2, "Starting abortable thread %08X", tempThreadId);
	uint32 scriptThreadId = startTempScriptThread(scriptCodeIp1, tempThreadId, 0, 0, 0);
	AbortableThread *abortableThread = new AbortableThread(_vm, tempThreadId, callingThreadId, 0,
		scriptThreadId, scriptCodeIp2);
	_threads->startThread(abortableThread);
	return tempThreadId;
}

uint32 ScriptMan::startTalkThread(int16 duration, uint32 objectId, uint32 talkId, uint32 sequenceId1,
	uint32 sequenceId2, uint32 namedPointId, uint32 callingThreadId) {
	debug(2, "Starting talk thread");
	uint32 tempThreadId = newTempThreadId();
	_threads->endTalkThreadsNoNotify();
	TalkThread *talkThread = new TalkThread(_vm, tempThreadId, callingThreadId, 0,
		duration, objectId, talkId, sequenceId1, sequenceId2, namedPointId);
	_threads->startThread(talkThread);
	return tempThreadId;
}

bool ScriptMan::findTriggerCause(uint32 sceneId, uint32 verbId, uint32 objectId2, uint32 objectId, uint32 &codeOffs) {
	ProgInfo *progInfo = _scriptResource->getProgInfo(sceneId & 0xFFFF);
	if (progInfo)
		return progInfo->findTriggerCause(verbId, objectId2, objectId, codeOffs);
	return false;
}

void ScriptMan::setCurrFontId(uint32 fontId) {
	_fontId = fontId;
}

bool ScriptMan::checkActiveTalkThreads() {
	return _threads->isActiveThread(kMsgQueryTalkThreadActive);
}

uint32 ScriptMan::clipTextDuration(uint32 duration) {
	switch (_field8) {
	case 2:
		if (duration == 0)
			duration = 240;
		break;
	case 3:
		if (duration < _fieldA)
			duration = _fieldA;
		break;
	case 4:
		if (duration > _fieldA)
			duration = _fieldA;
		break;
	}
	return duration;
}

void ScriptMan::reset() {
	_scriptResource->_blockCounters.clear();
	_scriptResource->_properties.clear();
	// TODO script_sub_417FF0(1, 0);
}

bool ScriptMan::enterScene(uint32 sceneId, uint32 threadId) {
	ProgInfo *progInfo = _scriptResource->getProgInfo(sceneId & 0xFFFF);
	if (!progInfo) {
		// TODO dumpActiveScenes(_globalSceneId, threadId);
		sceneId = _theSceneId;
	}
	_activeScenes.push(sceneId);
	return progInfo != 0;
}

void ScriptMan::exitScene(uint32 threadId) {
	uint32 sceneId = _activeScenes.getCurrentScene();
	// TODO krnfileDump(sceneId);
	// TODO UpdateFunctions_disableByTag__TODO_maybe(sceneId);
	_threads->terminateThreadsByTag(sceneId, threadId);
	_vm->_controls->destroyControlsByTag(sceneId);
	_vm->_triggerFunctions->removeBySceneId(sceneId);
	_vm->_resSys->unloadResourcesByTag(sceneId);
	_activeScenes.pop();
}

void ScriptMan::enterPause(uint32 threadId) {
	uint32 sceneId = _activeScenes.getCurrentScene();
	_vm->_camera->pushCameraMode();
	_threads->suspendThreadsByTag(sceneId, threadId);
	_vm->_controls->pauseControlsByTag(sceneId);
	_vm->_actorItems->pauseByTag(sceneId);
	_vm->_backgroundItems->pauseByTag(sceneId);
	_activeScenes.pauseActiveScene();
}

void ScriptMan::leavePause(uint32 threadId) {
	uint32 sceneId = _activeScenes.getCurrentScene();
	_vm->_backgroundItems->unpauseByTag(sceneId);
	_vm->_actorItems->unpauseByTag(sceneId);
	_vm->_controls->unpauseControlsByTag(sceneId);
	_threads->notifyThreadsByTag(sceneId, threadId);
	_vm->_camera->popCameraMode();
	_activeScenes.unpauseActiveScene();
}

void ScriptMan::dumpActiveScenes(uint32 sceneId, uint32 threadId) {
	uint activeScenesCount = _activeScenes.getActiveScenesCount();
	while (activeScenesCount > 0) {
		uint32 activeSceneId;
		_activeScenes.getActiveSceneInfo(activeScenesCount, &activeSceneId, 0);
		if (activeSceneId == sceneId)
			break;
		exitScene(threadId);
		--activeScenesCount;
	}
	_vm->_camera->clearCameraModeStack();
}

void ScriptMan::newScriptThread(uint32 threadId, uint32 callingThreadId, uint notifyFlags,
	byte *scriptCodeIp, uint32 value8, uint32 valueC, uint32 value10) {
	ScriptThread *scriptThread = new ScriptThread(_vm, threadId, callingThreadId, notifyFlags,
		scriptCodeIp, value8, valueC, value10);
	_threads->startThread(scriptThread);
	if (_pauseCtr > 0)
		scriptThread->pause();
	if (_doScriptThreadInit) {
		int updateResult = kTSRun;
		while (scriptThread->_pauseCtr <= 0 && updateResult != kTSTerminate && updateResult != kTSYield)
			updateResult = scriptThread->update();
	}
}

uint32 ScriptMan::newTimerThread(uint32 duration, uint32 callingThreadId, bool isAbortable) {
	uint32 tempThreadId = newTempThreadId();
	TimerThread *timerThread = new TimerThread(_vm, tempThreadId, callingThreadId, 0,
		duration, isAbortable);
	_threads->startThread(timerThread);
	return tempThreadId;
}

uint32 ScriptMan::newTempThreadId() {
	uint32 threadId = _nextTempThreadId + 2 * _scriptResource->_codeCount;
	if (threadId > 65535) {
		_nextTempThreadId = 0;
		threadId = 2 * _scriptResource->_codeCount;
	}
	++_nextTempThreadId;
	return 0x00020000 | threadId;
}

} // End of namespace Illusions