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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
/* 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 "common/events.h"
#include "common/debug-channels.h"
#include "common/textconsole.h"
#include "parallaction/exec.h"
#include "parallaction/input.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
*/
struct BalloonPositions {
Common::Point _questionBalloon;
Common::Point _questionChar;
Common::Point _answerChar;
};
class DialogueManager {
Parallaction *_vm;
Dialogue *_dialogue;
bool isNpc;
GfxObj *_questioner;
GfxObj *_answerer;
int _faceId;
Question *_q;
int _answerId;
int _selection, _oldSelection;
uint32 _mouseButtons;
Common::Point _mousePos;
protected:
BalloonPositions _ballonPos;
struct VisibleAnswer {
Answer *_a;
int _index; // index into Question::_answers[]
int _balloon;
} _visAnswers[5];
int _numVisAnswers;
bool _isKeyDown;
uint16 _downKey;
protected:
Gfx *_gfx;
BalloonManager *_balloonMan;
public:
DialogueManager(Parallaction *vm, ZonePtr z);
virtual ~DialogueManager();
void start();
bool isOver() {
return _state == DIALOGUE_OVER;
}
void run();
ZonePtr _z;
CommandList *_cmdList;
protected:
enum DialogueState {
DIALOGUE_START,
RUN_QUESTION,
RUN_ANSWER,
NEXT_QUESTION,
NEXT_ANSWER,
DIALOGUE_OVER
} _state;
static const int NO_ANSWER_SELECTED = -1;
void transitionToState(DialogueState newState);
bool displayQuestion();
void displayAnswers();
bool testAnswerFlags(Answer *a);
virtual void addVisibleAnswers(Question *q) = 0;
virtual int16 selectAnswer() = 0;
int16 selectAnswer1();
int16 selectAnswerN();
int16 getHoverAnswer(int16 x, int16 y);
void runQuestion();
void runAnswer();
void nextQuestion();
void nextAnswer();
};
DialogueManager::DialogueManager(Parallaction *vm, ZonePtr z) : _vm(vm), _z(z) {
_gfx = _vm->_gfx;
_balloonMan = _vm->_balloonMan;
_dialogue = _z->u._speakDialogue;
isNpc = !_z->u._filename.empty() && _z->u._filename.compareToIgnoreCase("yourself");
_questioner = isNpc ? _vm->_disk->loadTalk(_z->u._filename.c_str()) : _vm->_char._talk;
_answerer = _vm->_char._talk;
_cmdList = 0;
_answerId = 0;
_faceId = 0;
_q = NULL;
memset(_visAnswers, 0, sizeof(_visAnswers));
_numVisAnswers = 0;
_selection = _oldSelection = 0;
_isKeyDown = false;
_downKey = 0;
_mouseButtons = 0;
_state = DIALOGUE_START;
}
void DialogueManager::start() {
assert(_dialogue);
_q = _dialogue->_questions[0];
_state = DIALOGUE_START;
transitionToState(displayQuestion() ? RUN_QUESTION : NEXT_ANSWER);
}
DialogueManager::~DialogueManager() {
if (isNpc) {
delete _questioner;
}
_z.reset();
}
void DialogueManager::transitionToState(DialogueState newState) {
static const char *dialogueStates[] = {
"start",
"runquestion",
"runanswer",
"nextquestion",
"nextanswer",
"over"
};
if (_state != newState) {
debugC(3, kDebugDialogue, "DialogueManager moved to state '%s'", dialogueStates[newState]);
if (DebugMan.isDebugChannelEnabled(kDebugDialogue) && gDebugLevel == 9) {
switch (newState) {
case RUN_QUESTION:
debug(" Q : %s", _q->_text.c_str());
break;
case RUN_ANSWER:
for (int i = 0; i < _numVisAnswers; ++i) {
debug(" A%02i: %s", i, _visAnswers[i]._a->_text.c_str());
}
break;
default:
break;
}
}
}
_state = newState;
}
bool DialogueManager::testAnswerFlags(Answer *a) {
uint32 flags = _vm->getLocationFlags();
if (a->_yesFlags & kFlagsGlobal)
flags = g_globalFlags | kFlagsGlobal;
return ((a->_yesFlags & flags) == a->_yesFlags) && ((a->_noFlags & ~flags) == a->_noFlags);
}
void DialogueManager::displayAnswers() {
// create balloons
int id;
for (int i = 0; i < _numVisAnswers; ++i) {
id = _balloonMan->setDialogueBalloon(_visAnswers[i]._a->_text, 1, BalloonManager::kUnselectedColor);
assert(id >= 0);
_visAnswers[i]._balloon = id;
}
int mood = 0;
if (_numVisAnswers == 1) {
mood = _visAnswers[0]._a->speakerMood();
_balloonMan->setBalloonText(_visAnswers[0]._balloon, _visAnswers[0]._a->_text, BalloonManager::kNormalColor);
} else
if (_numVisAnswers > 1) {
mood = _visAnswers[0]._a->speakerMood();
_oldSelection = NO_ANSWER_SELECTED;
_selection = 0;
}
_faceId = _gfx->setItem(_answerer, _ballonPos._answerChar.x, _ballonPos._answerChar.y);
_gfx->setItemFrame(_faceId, mood);
}
int16 DialogueManager::selectAnswer1() {
if (_visAnswers[0]._a->textIsNull()) {
return _visAnswers[0]._index;
}
if (_mouseButtons == kMouseLeftUp) {
return _visAnswers[0]._index;
}
return NO_ANSWER_SELECTED;
}
int16 DialogueManager::selectAnswerN() {
_selection = _balloonMan->hitTestDialogueBalloon(_mousePos.x, _mousePos.y);
if (_selection != _oldSelection) {
if (_oldSelection != NO_ANSWER_SELECTED) {
VisibleAnswer *oldAnswer = &_visAnswers[_oldSelection];
_balloonMan->setBalloonText(oldAnswer->_balloon, oldAnswer->_a->_text, BalloonManager::kUnselectedColor);
}
if (_selection != NO_ANSWER_SELECTED) {
VisibleAnswer *answer = &_visAnswers[_selection];
_balloonMan->setBalloonText(answer->_balloon, answer->_a->_text, BalloonManager::kSelectedColor);
_gfx->setItemFrame(_faceId, answer->_a->speakerMood());
}
}
_oldSelection = _selection;
if ((_mouseButtons == kMouseLeftUp) && (_selection != NO_ANSWER_SELECTED)) {
return _visAnswers[_selection]._index;
}
return NO_ANSWER_SELECTED;
}
bool DialogueManager::displayQuestion() {
if (_q->textIsNull()) return false;
_balloonMan->setSingleBalloon(_q->_text, _ballonPos._questionBalloon.x, _ballonPos._questionBalloon.y, _q->balloonWinding(), BalloonManager::kNormalColor);
_faceId = _gfx->setItem(_questioner, _ballonPos._questionChar.x, _ballonPos._questionChar.y);
_gfx->setItemFrame(_faceId, _q->speakerMood());
return true;
}
void DialogueManager::runQuestion() {
if (_mouseButtons == kMouseLeftUp) {
_gfx->freeDialogueObjects();
transitionToState(NEXT_ANSWER);
}
}
void DialogueManager::nextAnswer() {
if (_q->_answers[0] == NULL) {
transitionToState(DIALOGUE_OVER);
return;
}
// try and check if there are any suitable answers,
// given the current game state.
addVisibleAnswers(_q);
if (!_numVisAnswers) {
// if there are no answers, then chicken out
transitionToState(DIALOGUE_OVER);
return;
}
if (_visAnswers[0]._a->textIsNull()) {
// if the first answer is null (it's implied that it's the
// only one because we already called addVisibleAnswers),
// then jump to the next question
_answerId = _visAnswers[0]._index;
transitionToState(NEXT_QUESTION);
} else {
// at this point we are sure there are non-null answers to show
displayAnswers();
transitionToState(RUN_ANSWER);
}
}
void DialogueManager::runAnswer() {
_answerId = selectAnswer();
if (_answerId != NO_ANSWER_SELECTED) {
_cmdList = &_q->_answers[_answerId]->_commands;
_gfx->freeDialogueObjects();
transitionToState(NEXT_QUESTION);
}
}
void DialogueManager::nextQuestion() {
_q = _dialogue->findQuestion(_q->_answers[_answerId]->_followingName);
if (_q == 0) {
transitionToState(DIALOGUE_OVER);
} else {
transitionToState(displayQuestion() ? RUN_QUESTION : NEXT_ANSWER);
}
}
void DialogueManager::run() {
// cache event data
_mouseButtons = _vm->_input->getLastButtonEvent();
_vm->_input->getCursorPos(_mousePos);
_isKeyDown = _vm->_input->getLastKeyDown(_downKey);
switch (_state) {
case RUN_QUESTION:
runQuestion();
break;
case NEXT_ANSWER:
nextAnswer();
break;
case NEXT_QUESTION:
nextQuestion();
break;
case RUN_ANSWER:
runAnswer();
break;
case DIALOGUE_OVER:
break;
default:
error("unknown state in DialogueManager");
}
}
class DialogueManager_ns : public DialogueManager {
protected:
Parallaction_ns *_vm;
bool _passwordChanged;
bool _askPassword;
bool checkPassword() {
return ((!scumm_stricmp(_vm->_char.getBaseName(), g_doughName) && _vm->_password.hasPrefix("1732461")) ||
(!scumm_stricmp(_vm->_char.getBaseName(), g_donnaName) && _vm->_password.hasPrefix("1622")) ||
(!scumm_stricmp(_vm->_char.getBaseName(), g_dinoName) && _vm->_password.hasPrefix("179")));
}
void resetPassword() {
_vm->_password.clear();
_passwordChanged = true;
}
void accumPassword(uint16 ascii) {
if (!Common::isDigit(ascii)) {
return;
}
_vm->_password += ascii;
_passwordChanged = true;
}
int16 askPassword() {
if (_isKeyDown) {
accumPassword(_downKey);
}
if (_passwordChanged) {
_balloonMan->setBalloonText(_visAnswers[0]._balloon, _visAnswers[0]._a->_text, BalloonManager::kNormalColor);
_passwordChanged = false;
}
if ((_vm->_password.size() == MAX_PASSWORD_LENGTH) || ((_isKeyDown) && (_downKey == Common::KEYCODE_RETURN))) {
if (checkPassword()) {
return 0;
} else {
resetPassword();
}
}
return NO_ANSWER_SELECTED;
}
public:
DialogueManager_ns(Parallaction_ns *vm, ZonePtr z) : DialogueManager(vm, z), _vm(vm),
_passwordChanged(false), _askPassword(false) {
_ballonPos._questionBalloon = Common::Point(140, 10);
_ballonPos._questionChar = Common::Point(190, 80);
_ballonPos._answerChar = Common::Point(10, 80);
}
bool canDisplayAnswer(Answer *a) {
return testAnswerFlags(a);
}
virtual void addVisibleAnswers(Question *q) {
_askPassword = false;
_numVisAnswers = 0;
for (int i = 0; i < NUM_ANSWERS && q->_answers[i]; i++) {
Answer *a = q->_answers[i];
if (!canDisplayAnswer(a)) {
continue;
}
if (a->_text.contains("%P")) {
_askPassword = true;
}
_visAnswers[_numVisAnswers]._a = a;
_visAnswers[_numVisAnswers]._index = i;
_numVisAnswers++;
}
resetPassword();
}
virtual int16 selectAnswer() {
int ans = NO_ANSWER_SELECTED;
if (_askPassword) {
ans = askPassword();
} else
if (_numVisAnswers == 1) {
ans = selectAnswer1();
} else {
ans = selectAnswerN();
}
return ans;
}
};
class DialogueManager_br : public DialogueManager {
Parallaction_br *_vm;
public:
DialogueManager_br(Parallaction_br *vm, ZonePtr z) : DialogueManager(vm, z), _vm(vm) {
_ballonPos._questionBalloon = Common::Point(0, 0);
_ballonPos._questionChar = Common::Point(380, 80);
_ballonPos._answerChar = Common::Point(10, 80);
}
bool canDisplayAnswer(Answer *a) {
if (!a)
return false;
if (a->_hasCounterCondition) {
_vm->testCounterCondition(a->_counterName, a->_counterOp, a->_counterValue);
return (_vm->getLocationFlags() & kFlagsTestTrue) != 0;
}
return testAnswerFlags(a);
}
virtual void addVisibleAnswers(Question *q) {
_numVisAnswers = 0;
for (int i = 0; i < NUM_ANSWERS && q->_answers[i]; i++) {
Answer *a = q->_answers[i];
if (!canDisplayAnswer(a)) {
continue;
}
_visAnswers[_numVisAnswers]._a = a;
_visAnswers[_numVisAnswers]._index = i;
_numVisAnswers++;
}
}
virtual int16 selectAnswer() {
int16 ans = NO_ANSWER_SELECTED;
if (_numVisAnswers == 1) {
ans = selectAnswer1();
} else {
ans = selectAnswerN();
}
return ans;
}
};
void Parallaction::enterDialogueMode(ZonePtr z) {
if (!z->u._speakDialogue) {
return;
}
debugC(1, kDebugDialogue, "Parallaction::enterDialogueMode(%s)", z->u._filename.c_str());
_dialogueMan = createDialogueManager(z);
assert(_dialogueMan);
_dialogueMan->start();
_input->_inputMode = Input::kInputModeDialogue;
}
void Parallaction::exitDialogueMode() {
debugC(1, kDebugDialogue, "Parallaction::exitDialogueMode()");
_input->_inputMode = Input::kInputModeGame;
/* Since the current instance of _dialogueMan must be destroyed before the
zone commands are executed, as they may create a new instance of _dialogueMan that
would overwrite the current, we need to save the references to the command lists.
*/
CommandList *_cmdList = _dialogueMan->_cmdList;
ZonePtr z = _dialogueMan->_z;
// destroy the _dialogueMan here
destroyDialogueManager();
// run the lists saved
if (_cmdList) {
_cmdExec->run(*_cmdList);
}
_cmdExec->run(z->_commands, z);
}
void Parallaction::destroyDialogueManager() {
// destroy the _dialogueMan here
delete _dialogueMan;
_dialogueMan = 0;
}
void Parallaction::runDialogueFrame() {
if (_input->_inputMode != Input::kInputModeDialogue) {
return;
}
_dialogueMan->run();
if (_dialogueMan->isOver()) {
exitDialogueMode();
}
return;
}
DialogueManager *Parallaction_ns::createDialogueManager(ZonePtr z) {
return new DialogueManager_ns(this, z);
}
DialogueManager *Parallaction_br::createDialogueManager(ZonePtr z) {
return new DialogueManager_br(this, z);
}
} // namespace Parallaction
|