aboutsummaryrefslogtreecommitdiff
path: root/engines/illusions/threads/talkthread_duckman.cpp
blob: 36911597ff04b4e7af713e0fcc43934e6f450424 (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
/* 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/duckman/illusions_duckman.h"
#include "illusions/threads/talkthread_duckman.h"
#include "illusions/actor.h"
#include "illusions/dictionary.h"
#include "illusions/input.h"
#include "illusions/resources/talkresource.h"
#include "illusions/screentext.h"
#include "illusions/sound.h"
#include "illusions/time.h"

namespace Illusions {

// TalkThread_Duckman

TalkThread_Duckman::TalkThread_Duckman(IllusionsEngine_Duckman *vm, uint32 threadId, uint32 callingThreadId, uint notifyFlags,
	uint32 objectId, uint32 talkId, uint32 sequenceId1, uint32 sequenceId2)
	: Thread(vm, threadId, callingThreadId, notifyFlags), _vm(vm), _objectId(objectId), _talkId(talkId) {
	_type = kTTTalkThread;

	if ((sequenceId1 & 0xFFFF0000) == 0x60000) {
		_sequenceId1 = sequenceId1;
		_sequenceId2 = sequenceId2;
		_namedPointId1 = 0;
		_namedPointId2 = 0;
	} else {
		_sequenceId1 = 0;
		_sequenceId2 = 0;
		_namedPointId1 = sequenceId1;
		_namedPointId2 = sequenceId2;
	}

	if (_vm->checkActiveTalkThreads())
		_status = 1;
	else
		_status = 2;

	_durationMult = _vm->clipTextDuration(_vm->_subtitleDuration);
	_textDuration = _durationMult;
	_defDurationMult = _vm->clipTextDuration(240);

	_sceneId = _vm->getCurrentScene();

}

int TalkThread_Duckman::onUpdate() {

	TalkEntry *talkEntry;

	switch (_status) {

	case 1:
		if (_vm->checkActiveTalkThreads())
			return kTSYield;
		_status = 3;
		// fall through

	case 2:
		talkEntry = getTalkResourceEntry(_talkId);
		_flags = 0;
		_currEntryText = 0;
		_entryText = talkEntry->_text;
		_entryTblPtr = talkEntry->_tblPtr;
		if (_sequenceId1) {
			_pauseCtrPtr = &_pauseCtr;
			_pauseCtr = 0;
		} else {
			_pauseCtrPtr = 0;
			_flags |= 2;
			_flags |= 1;
		}
		if (_vm->isSoundActive()) {
			if (!_vm->_soundMan->cueVoice((char*)talkEntry->_voiceName) && !_durationMult)
				_durationMult = _defDurationMult;
		} else {
			_flags |= 4;
			if (_durationMult == 0)
				_durationMult = _defDurationMult;
		}
		if (_objectId == 0 || _durationMult == 0)
			_flags |= 8;
		_status = 3;
		// fall through

	case 3:
		if (!(_flags & 4) && !_vm->_soundMan->isVoiceCued())
			return kTSYield;
		_status = 4;
		// fall through

	case 4:
		if (!(_flags & 8) ) {
			uint32 actorTypeId = _vm->getObjectActorTypeId(_objectId);
			getActorTypeColor(actorTypeId, _color);
			refreshText();
		}
		if (!(_flags & 2)) {
			Control *control = _vm->_dict->getObjectControl(_objectId);
			control->startTalkActor(_sequenceId1, _entryTblPtr, _threadId);
		}
		if (!(_flags & 4)) {
			int16 panX = 0;
			if (_flags & 1) {
				if (_namedPointId2) {
					panX = _vm->getNamedPointPosition(_namedPointId2).x;
					panX = _vm->convertPanXCoord(panX);
				}
			} else {
				Control *control = _vm->_dict->getObjectControl(_objectId);
				panX = control->getActorPosition().x;
				panX = _vm->convertPanXCoord(panX);
			}
			_vm->_soundMan->startVoice(255, panX);
		}
		_vm->_input->discardEvent(kEventSkip);
		_status = 5;
		return kTSYield;

	case 5:
		if (!(_flags & 4) && !_vm->_soundMan->isVoicePlaying())
			_flags |= 4;
		if (!(_flags & 8) && isTimerExpired(_textStartTime, _textEndTime)) {
			_vm->_screenText->removeText();
			if (_entryText && *_entryText) {
				refreshText();
				_vm->_input->discardEvent(kEventSkip);
			} else {
				_flags |= 8;
			}
		}
		if (!(_flags & 2)) {
			if (*_pauseCtrPtr < 0) {
				++(*_pauseCtrPtr);
				Control *control = _vm->_dict->getObjectControl(_objectId);
				control->startSequenceActor(_sequenceId2, 2, 0);
				_flags |= 2;
			}
		}
		if (_objectId && _vm->_input->pollEvent(kEventSkip)) {
			if (!(_flags & 8)) {
				_vm->_screenText->removeText();
				if (_entryText && *_entryText)
					refreshText();
				else
					_flags |= 8;
			}
			if (_flags & 8) {
				if (!(_flags & 4)) {
					_vm->_soundMan->stopVoice();
					_flags |= 4;
				}
				if (!(_flags & 2)) {
					Control *control = _vm->_dict->getObjectControl(_objectId);
					control->clearNotifyThreadId1();
					control->startSequenceActor(_sequenceId2, 2, 0);
					_flags |= 2;
				}
			}
		}
		if ((_flags & 8) && (_flags & 2) && (_flags & 4)) {
			_vm->_input->discardEvent(kEventSkip);
			return kTSTerminate;
		}
		return kTSYield;

	case 6:
		if (!(_flags & 2)) {
			Control *control = _vm->_dict->getObjectControl(_objectId);
			if (*_pauseCtrPtr >= 0) {
				control->clearNotifyThreadId1();
			} else {
				++(*_pauseCtrPtr);
			}
			control->startSequenceActor(_sequenceId2, 2, 0);
			_flags |= 2;
		}
		return kTSTerminate;

	}

	return kTSTerminate;

}

void TalkThread_Duckman::onPause() {
	if (_status == 5) {
		if (!(_flags & 4)) {
			_vm->_soundMan->pauseVoice(true);
		}
		if (!(_flags & 8))
			_textDurationElapsed = getDurationElapsed(_textStartTime, _textEndTime);
	}
}

void TalkThread_Duckman::onUnpause() {
	if (_status == 3) {
		TalkEntry *talkEntry = getTalkResourceEntry(_talkId);
		if (!_vm->isSoundActive())
			_vm->_soundMan->cueVoice((char*)talkEntry->_voiceName);
	} else if (_status == 5) {
		if (!(_flags & 4)) {
			_vm->_soundMan->pauseVoice(false);
		}
		if (!(_flags & 8)) {
			_textStartTime = getCurrentTime();
			if (_textDuration <= _textDurationElapsed)
				_textEndTime = _textStartTime;
			else
				_textEndTime = _textStartTime + _textDuration - _textDurationElapsed;
			_textDurationElapsed = 0;
		}
	}
}

void TalkThread_Duckman::onResume() {
}

void TalkThread_Duckman::onTerminated() {
	if (_status == 5) {
		if (!(_flags & 4))
			_vm->_soundMan->stopVoice();
		if (!(_flags & 8)) {
			_vm->_screenText->removeText();
		}
		if (!(_flags & 2)) {
			Control *control = _vm->_dict->getObjectControl(_objectId);
			if (control) {
				control->clearNotifyThreadId1();
				control->startSequenceActor(_sequenceId2, 2, 0);
			}
		}
	}
}

void TalkThread_Duckman::onKill() {
	_callingThreadId = 0;
	sendMessage(kMsgClearSequenceId1, 0);
	sendMessage(kMsgClearSequenceId2, 0);
}

uint32 TalkThread_Duckman::sendMessage(int msgNum, uint32 msgValue) {
	switch (msgNum) {
	case kMsgQueryTalkThreadActive:
		if (_status != 1)
			return 1;
		break;
	case kMsgClearSequenceId1:
		_sequenceId1 = 0;
		_flags |= 3;
		// TODO _pauseCtrPtr = 0;
		break;
	case kMsgClearSequenceId2:
		_sequenceId2 = 0;
		break;
	}
	return 0;
}

void TalkThread_Duckman::refreshText() {
	_currEntryText = _entryText;
	int charCount = insertText();
	uint32 duration = _durationMult;
	if (charCount < 80) {
		duration = _durationMult * charCount / 80;
		if (duration < 25 * _durationMult / 100)
			duration = 25 * _durationMult / 100;
		if (duration < 60)
			duration = 60;
	}
	_textDuration = duration;
	_textStartTime = getCurrentTime();
	_textEndTime = _textStartTime + _textDuration;
}

int TalkThread_Duckman::insertText() {
	debug(0, "%08X %08X [%s]", _threadId, _talkId, debugW2I(_currEntryText));
	WidthHeight dimensions;
	_vm->getDefaultTextDimensions(dimensions);
	uint16 *outTextPtr;
	_vm->_screenText->insertText((uint16*)_currEntryText, 0x120001, dimensions,
		Common::Point(0, 0), TEXT_FLAG_CENTER_ALIGN, 0, 0, _color.r, _color.g, _color.b, outTextPtr);
	_entryText = (byte*)outTextPtr;
	Common::Point pt;
	_vm->getDefaultTextPosition(pt);
	_vm->_screenText->updateTextInfoPosition(pt);
	int charCount = (_entryText - _currEntryText) / 2;
	return charCount;
}

TalkEntry *TalkThread_Duckman::getTalkResourceEntry(uint32 talkId) {
	TalkEntry *talkEntry = _vm->_dict->findTalkEntry(talkId);
	return talkEntry;
}

void TalkThread_Duckman::getActorTypeColor(uint32 actorTypeId, RGB &color) {
	ActorType *actorType = _vm->_dict->findActorType(actorTypeId);
	color = actorType->_color;
}

} // End of namespace Illusions