aboutsummaryrefslogtreecommitdiff
path: root/engines/parallaction/dialogue.cpp
blob: 6d756f172fea73c68d1806bb639d9dc98e19b534 (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
/* 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 "common/events.h"
#include "parallaction/parallaction.h"



namespace Parallaction {

#define MAX_PASSWORD_LENGTH 		 7

#define QUESTION_BALLOON_X			140
#define QUESTION_BALLOON_Y			10
#define QUESTION_CHARACTER_X		  190
#define QUESTION_CHARACTER_Y		  80

#define ANSWER_CHARACTER_X			10
#define ANSWER_CHARACTER_Y			80

class DialogueManager {

	Parallaction	*_vm;
	SpeakData		*_data;
	Dialogue		*_dialogue;

	bool 			_askPassword;

	bool 			isNpc;
	Frames			*_questioner;
	Frames			*_answerer;

	Question		*_q;

	uint16			_visAnswers[5];
	int			_numVisAnswers;

public:
	DialogueManager(Parallaction *vm, SpeakData *data) : _vm(vm), _data(data) {
		_dialogue = _data->_dialogue;
		isNpc = scumm_stricmp(_data->_name, "yourself") && _data->_name[0] != '\0';
		_questioner = isNpc ? _vm->_disk->loadTalk(_data->_name) : _vm->_char._talk;
		_answerer = _vm->_char._talk;
	}

	~DialogueManager() {
		if (isNpc) {
			delete _questioner;
		}
	}

	void run();

protected:
	void displayQuestion();
	bool displayAnswers();
	bool displayAnswer(uint16 i);

	uint16 getAnswer();
	int16 selectAnswer();
	uint16 askPassword();
	int16 getHoverAnswer(int16 x, int16 y);

};

uint16 DialogueManager::askPassword() {
	debugC(3, kDebugExec, "checkDialoguePassword()");

	uint16 passwordLen = 0;
	_password[0] = '\0';

	_vm->_gfx->setDialogueBalloon(_q->_answers[0]->_text, 1, 3);
	int id = _vm->_gfx->setItem(_answerer, ANSWER_CHARACTER_X, ANSWER_CHARACTER_Y);
	_vm->_gfx->setItemFrame(id, 0);

	Common::Event e;
	bool changed = true;	// force first refresh

	while (true) {
		e.kbd.ascii = 0;

		if (g_system->getEventManager()->pollEvent(e)) {
			if (e.type == Common::EVENT_QUIT) {
				// TODO: don't quit() here, just have caller routines to check
				// on kEngineQuit and exit gracefully to allow the engine to shut down
				_engineFlags |= kEngineQuit;
				g_system->quit();
			}

			if ((e.type == Common::EVENT_KEYDOWN) && isdigit(e.kbd.ascii)) {
				_password[passwordLen] = e.kbd.ascii;
				passwordLen++;
				_password[passwordLen] = '\0';
				changed = true;
			}
		}

		if (changed) {
			_vm->_gfx->setBalloonText(0, _q->_answers[0]->_text, 3);
			_vm->_gfx->updateScreen();
			changed = false;
		}

		if ((passwordLen == MAX_PASSWORD_LENGTH) || (e.kbd.ascii == Common::KEYCODE_RETURN)) {

			if ((!scumm_stricmp(_vm->_char.getBaseName(), _doughName) && !scumm_strnicmp(_password, "1732461", 7)) ||
			   (!scumm_stricmp(_vm->_char.getBaseName(), _donnaName) && !scumm_strnicmp(_password, "1622", 4)) ||
			   (!scumm_stricmp(_vm->_char.getBaseName(), _dinoName) && !scumm_strnicmp(_password, "179", 3))) {

				break;

			} else {
				passwordLen = 0;
				_password[0] = '\0';
				changed = true;
			}

		}

		g_system->delayMillis(20);

	}

	_vm->_gfx->hideDialogueStuff();

	return 0;

}



bool DialogueManager::displayAnswer(uint16 i) {

	Answer *a = _q->_answers[i];

	uint32 flags = _vm->_localFlags[_vm->_currentLocationIndex];
	if (a->_yesFlags & kFlagsGlobal)
		flags = _commandFlags | kFlagsGlobal;

	// display suitable answers
	if (((a->_yesFlags & flags) == a->_yesFlags) && ((a->_noFlags & ~flags) == a->_noFlags)) {

		int id = _vm->_gfx->setDialogueBalloon(a->_text, 1, 3);
		assert(id >= 0);
		_visAnswers[id] = i;

		_askPassword = (strstr(a->_text, "%p") != NULL);
		_numVisAnswers++;

		return true;
	}

	return false;
}

bool DialogueManager::displayAnswers() {

	_numVisAnswers = 0;

	for (int i = 0; i < NUM_ANSWERS && _q->_answers[i]; i++) {
		displayAnswer(i);
	}

	return _numVisAnswers > 0;
}

void DialogueManager::displayQuestion() {

	if (!scumm_stricmp(_q->_text, "NULL")) return;

	_vm->_gfx->setSingleBalloon(_q->_text, QUESTION_BALLOON_X, QUESTION_BALLOON_Y, _q->_mood & 0x10, 0);
	int id = _vm->_gfx->setItem(_questioner, QUESTION_CHARACTER_X, QUESTION_CHARACTER_Y);
	_vm->_gfx->setItemFrame(id, _q->_mood & 0xF);

	_vm->_gfx->updateScreen();
	waitUntilLeftClick();
	_vm->_gfx->hideDialogueStuff();

	return;
}

uint16 DialogueManager::getAnswer() {

	uint16 answer = 0;

	if (_askPassword == false) {
		answer = selectAnswer();
	} else {
		answer = askPassword();
	}

	debugC(3, kDebugExec, "runDialogue: user selected answer #%i", answer);

	return answer;
}

void DialogueManager::run() {

	_askPassword = false;
	CommandList *cmdlist = NULL;

	_q = _dialogue->_questions[0];
	int16 answer;

	while (_q) {

		answer = 0;

		displayQuestion();
		if (_q->_answers[0] == NULL) break;

		if (scumm_stricmp(_q->_answers[0]->_text, "NULL")) {
			if (!displayAnswers()) break;
			answer = getAnswer();
			cmdlist = &_q->_answers[answer]->_commands;
		}

		_q = _q->_answers[answer]->_following._question;
	}

	if (cmdlist)
		_vm->runCommands(*cmdlist);

}

int16 DialogueManager::selectAnswer() {

	int16 numAvailableAnswers = _numVisAnswers;

	int id = _vm->_gfx->setItem(_answerer, ANSWER_CHARACTER_X, ANSWER_CHARACTER_Y);
	_vm->_gfx->setItemFrame(id, _q->_answers[0]->_mood & 0xF);

	if (numAvailableAnswers == 1) {
		_vm->_gfx->setBalloonText(0, _q->_answers[0]->_text, 0);
		waitUntilLeftClick();
		_vm->_gfx->hideDialogueStuff();
		return 0;
	}

	int oldSelection = -1;
	int selection;

	while (true) {

		_vm->readInput();
		selection = _vm->_gfx->hitTestDialogueBalloon(_vm->_mousePos.x, _vm->_mousePos.y);

		if (selection != oldSelection) {
			if (oldSelection != -1) {
				_vm->_gfx->setBalloonText(oldSelection, _q->_answers[_visAnswers[oldSelection]]->_text, 3);
			}

			if (selection != -1) {
				_vm->_gfx->setBalloonText(selection, _q->_answers[_visAnswers[selection]]->_text, 0);
				_vm->_gfx->setItemFrame(0, _q->_answers[_visAnswers[selection]]->_mood & 0xF);
			}
		}

		if ((selection != -1) && (_mouseButtons == kMouseLeftUp)) {
			break;
		}

		_vm->_gfx->updateScreen();
		g_system->delayMillis(20);

		oldSelection = selection;
	}

	_vm->_gfx->hideDialogueStuff();

	return _visAnswers[selection];
}


void Parallaction::runDialogue(SpeakData *data) {
	debugC(1, kDebugExec, "runDialogue: starting dialogue '%s'", data->_name);

	DialogueManager man(this, data);
	man.run();

	return;
}


} // namespace Parallaction