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
|
/* 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 "mutationofjb/tasks/conversationtask.h"
#include "mutationofjb/assets.h"
#include "mutationofjb/game.h"
#include "mutationofjb/gamedata.h"
#include "mutationofjb/gui.h"
#include "mutationofjb/script.h"
#include "mutationofjb/tasks/saytask.h"
#include "mutationofjb/tasks/sequentialtask.h"
#include "mutationofjb/tasks/taskmanager.h"
#include "mutationofjb/util.h"
#include "mutationofjb/widgets/conversationwidget.h"
namespace MutationOfJB {
void ConversationTask::start() {
setState(RUNNING);
Game &game = getTaskManager()->getGame();
ConversationWidget &widget = game.getGui().getConversationWidget();
widget.setCallback(this);
widget.setVisible(true);
_currentGroupIndex = 0;
showChoicesOrPick();
}
void ConversationTask::update() {
if (_sayTask) {
if (_sayTask->getState() == Task::FINISHED) {
_sayTask.reset();
switch (_substate) {
case SAYING_NO_QUESTIONS:
finish();
break;
case SAYING_QUESTION: {
const ConversationLineList &responseList = getTaskManager()->getGame().getAssets().getResponseList();
const ConversationLineList::Line *const line = responseList.getLine(_currentItem->_response);
_substate = SAYING_RESPONSE;
createSayTasks(line);
getTaskManager()->startTask(_sayTask);
break;
}
case SAYING_RESPONSE: {
startExtra();
if (_substate != RUNNING_EXTRA) {
gotoNextGroup();
}
break;
}
default:
break;
}
}
}
if (_innerExecCtx) {
Command::ExecuteResult res = _innerExecCtx->runActiveCommand();
if (res == Command::Finished) {
delete _innerExecCtx;
_innerExecCtx = nullptr;
gotoNextGroup();
}
}
}
void ConversationTask::onChoiceClicked(ConversationWidget *convWidget, int, uint32 data) {
const ConversationInfo::Item &item = getCurrentGroup()[data];
convWidget->clearChoices();
const ConversationLineList &toSayList = getTaskManager()->getGame().getAssets().getToSayList();
const ConversationLineList::Line *line = toSayList.getLine(item._question);
_substate = SAYING_QUESTION;
createSayTasks(line);
getTaskManager()->startTask(_sayTask);
_currentItem = &item;
if (!line->_speeches[0].isRepeating()) {
getTaskManager()->getGame().getGameData().getCurrentScene()->addExhaustedConvItem(_convInfo._context, data + 1, _currentGroupIndex + 1);
}
}
void ConversationTask::showChoicesOrPick() {
Game &game = getTaskManager()->getGame();
GameData &gameData = game.getGameData();
Scene *const scene = gameData.getScene(_sceneId);
Common::Array<uint32> itemsWithValidQuestions;
Common::Array<uint32> itemsWithValidResponses;
Common::Array<uint32> itemsWithValidNext;
/*
Collect valid questions (not exhausted and not empty).
Collect valid responses (not exhausted and not empty).
If there are at least two visible questions, we show them.
If there is just one visible question, pick it automatically ONLY if this is not the first question in this conversation.
Otherwise we don't start the conversation.
If there are no visible questions, automatically pick the first valid response.
If nothing above applies, don't start the conversation.
*/
const ConversationInfo::ItemGroup ¤tGroup = getCurrentGroup();
for (ConversationInfo::ItemGroup::size_type i = 0; i < currentGroup.size(); ++i) {
const ConversationInfo::Item &item = currentGroup[i];
if (scene->isConvItemExhausted(_convInfo._context, (uint8) i + 1, (uint8) _currentGroupIndex + 1)) {
continue;
}
const uint8 toSay = item._question;
const uint8 response = item._response;
const uint8 next = item._nextGroupIndex;
if (toSay != 0) {
itemsWithValidQuestions.push_back(i);
}
if (response != 0) {
itemsWithValidResponses.push_back(i);
}
if (next != 0) {
itemsWithValidNext.push_back(i);
}
}
if (itemsWithValidQuestions.size() > 1) {
ConversationWidget &widget = game.getGui().getConversationWidget();
const ConversationLineList &toSayList = game.getAssets().getToSayList();
for (Common::Array<uint32>::size_type i = 0; i < itemsWithValidQuestions.size() && i < ConversationWidget::CONVERSATION_MAX_CHOICES; ++i) {
const ConversationInfo::Item &item = currentGroup[itemsWithValidQuestions[i]];
const ConversationLineList::Line *const line = toSayList.getLine(item._question);
const Common::String widgetText = toUpperCP895(line->_speeches[0]._text);
widget.setChoice((int) i, widgetText, itemsWithValidQuestions[i]);
}
_substate = IDLE;
_currentItem = nullptr;
_haveChoices = true;
} else if (itemsWithValidQuestions.size() == 1 && _haveChoices) {
const ConversationLineList &toSayList = game.getAssets().getToSayList();
const ConversationInfo::Item &item = currentGroup[itemsWithValidQuestions.front()];
const ConversationLineList::Line *const line = toSayList.getLine(item._question);
_substate = SAYING_QUESTION;
createSayTasks(line);
getTaskManager()->startTask(_sayTask);
_currentItem = &item;
if (!line->_speeches[0].isRepeating()) {
game.getGameData().getCurrentScene()->addExhaustedConvItem(_convInfo._context, itemsWithValidQuestions.front() + 1, _currentGroupIndex + 1);
}
_haveChoices = true;
} else if (!itemsWithValidResponses.empty() && _haveChoices) {
const ConversationLineList &responseList = game.getAssets().getResponseList();
const ConversationInfo::Item &item = currentGroup[itemsWithValidResponses.front()];
const ConversationLineList::Line *const line = responseList.getLine(item._response);
_substate = SAYING_RESPONSE;
createSayTasks(line);
getTaskManager()->startTask(_sayTask);
_currentItem = &item;
_haveChoices = true;
} else if (!itemsWithValidNext.empty() && _haveChoices) {
_currentGroupIndex = currentGroup[itemsWithValidNext.front()]._nextGroupIndex - 1;
showChoicesOrPick();
} else {
if (_haveChoices) {
finish();
} else {
_sayTask = TaskPtr(new SayTask("Nothing to talk about.", _convInfo._color)); // TODO: This is hardcoded in executable. Load it.
getTaskManager()->startTask(_sayTask);
_substate = SAYING_NO_QUESTIONS;
_currentItem = nullptr;
}
}
}
const ConversationInfo::ItemGroup &ConversationTask::getCurrentGroup() const {
assert(_currentGroupIndex < _convInfo._itemGroups.size());
return _convInfo._itemGroups[_currentGroupIndex];
}
void ConversationTask::finish() {
setState(FINISHED);
Game &game = getTaskManager()->getGame();
ConversationWidget &widget = game.getGui().getConversationWidget();
widget.setVisible(false);
game.getGui().markDirty(); // TODO: Handle automatically when changing visibility.
}
void ConversationTask::startExtra() {
const ConversationLineList &responseList = getTaskManager()->getGame().getAssets().getResponseList();
const ConversationLineList::Line *const line = responseList.getLine(_currentItem->_response);
if (!line->_extra.empty()) {
_innerExecCtx = new ScriptExecutionContext(getTaskManager()->getGame());
Command *const extraCmd = _innerExecCtx->getExtra(line->_extra);
if (extraCmd) {
Command::ExecuteResult res = _innerExecCtx->startCommand(extraCmd);
if (res == Command::InProgress) {
_substate = RUNNING_EXTRA;
} else {
delete _innerExecCtx;
_innerExecCtx = nullptr;
}
} else {
warning("Extra '%s' not found", line->_extra.c_str());
delete _innerExecCtx;
_innerExecCtx = nullptr;
}
}
}
void ConversationTask::gotoNextGroup() {
if (_currentItem->_nextGroupIndex == 0) {
finish();
} else {
_currentGroupIndex = _currentItem->_nextGroupIndex - 1;
showChoicesOrPick();
}
}
void ConversationTask::createSayTasks(const ConversationLineList::Line *line) {
if (line->_speeches.size() == 1) {
const ConversationLineList::Speech &speech = line->_speeches[0];
_sayTask = TaskPtr(new SayTask(speech._text, getSpeechColor(speech)));
} else {
TaskPtrs tasks;
for (ConversationLineList::Speeches::const_iterator it = line->_speeches.begin(); it != line->_speeches.end(); ++it) {
tasks.push_back(TaskPtr(new SayTask(it->_text, getSpeechColor(*it))));
}
_sayTask = TaskPtr(new SequentialTask(tasks));
}
}
uint8 ConversationTask::getSpeechColor(const ConversationLineList::Speech &speech) {
uint8 color = WHITE;
if (_substate == SAYING_RESPONSE) {
color = _convInfo._color;
if (_mode == TalkCommand::RAY_AND_BUTTLEG_MODE) {
if (speech.isFirstSpeaker()) {
color = GREEN;
} else if (speech.isSecondSpeaker()) {
color = LIGHTBLUE;
}
}
}
return color;
}
}
|