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
|
/* 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 "cryomni3d/dialogs_manager.h"
#include "common/debug.h"
#include "common/file.h"
namespace CryOmni3D {
DialogsManager::~DialogsManager() {
delete[] _gtoBuffer;
}
void DialogsManager::loadGTO(const Common::String >oFileName) {
Common::File gtoFile;
if (!gtoFile.open(gtoFileName)) {
error("Can't open GTO file '%s'", gtoFileName.c_str());
}
_labels.clear();
_gtoEnd = nullptr;
delete[] _gtoBuffer;
_gtoBuffer = nullptr;
uint gtoSize = gtoFile.size();
_gtoBuffer = new char[gtoSize];
gtoFile.read(_gtoBuffer, gtoSize);
gtoFile.close();
_gtoEnd = _gtoBuffer + gtoSize;
populateLabels();
}
void DialogsManager::populateLabels() {
/* Get labels count and populate the labels array */
uint numLabels;
const char *labelsP = strstr(_gtoBuffer, "LABELS=");
if (labelsP) {
labelsP += sizeof("LABELS=") - 1;
for (; *labelsP == ' '; labelsP++) { }
numLabels = atoi(labelsP);
} else {
numLabels = 0;
}
for (const char *labelP = _gtoBuffer; labelP != nullptr; labelP = nextLine(labelP)) {
if (*labelP == ':') {
/* Line starting with ':', it's a label */
_labels.push_back(nextChar(labelP));
}
}
if (_labels.size() != numLabels) {
error("Bad labels number in GTO");
}
}
const char *DialogsManager::findLabel(const char *label, const char **realLabel) const {
uint labelLen = 0;
/* Truncate input label */
for (const char *labelP = label;
*labelP != '\0' &&
*labelP != ' ' &&
*labelP != '.' &&
*labelP != '\r'; labelP++, labelLen++) { }
Common::Array<const char *>::const_iterator labelsIt;
for (labelsIt = _labels.begin(); labelsIt != _labels.end(); labelsIt++) {
if (!strncmp(*labelsIt, label, labelLen)) {
break;
}
}
if (labelsIt == _labels.end()) {
error("Label not found");
}
if (realLabel) {
*realLabel = *labelsIt;
}
return nextLine(*labelsIt);
}
Common::String DialogsManager::getLabelSound(const char *label) const {
/* Remove starting : if any */
if (*label == ':') {
label++;
}
const char *labelEnd;
for (labelEnd = label; *labelEnd >= '0' && *labelEnd <= 'Z'; labelEnd++) { }
return Common::String(label, labelEnd);
}
const char *DialogsManager::findSequence(const char *sequence) const {
uint sequenceLen = strlen(sequence);
const char *lineP;
for (lineP = _gtoBuffer; lineP != nullptr; lineP = nextLine(lineP)) {
if (!strncmp(lineP, sequence, sequenceLen)) {
/* Line starting with the sequence name */
break;
}
}
if (!lineP) {
return nullptr;
}
/* Find next label */
for (; lineP != nullptr && *lineP != ':'; lineP = nextLine(lineP)) { }
/* Return the label name without it's ':' */
return nextChar(lineP);
}
Common::String DialogsManager::findVideo(const char *data) const {
data = previousMatch(data, ".FLC");
if (data == nullptr) {
return Common::String();
}
// Video name is without the extension
const char *end = data;
for (; data >= _gtoBuffer && *data != '\r'; data--) { }
data++;
if (data < _gtoBuffer || *data == '.') {
return Common::String();
}
return Common::String(data, end);
}
Common::String DialogsManager::getText(const char *text) const {
/* Skip '<' */
text = nextChar(text);
if (text == nullptr) {
return Common::String();
}
const char *end;
for (end = text; end < _gtoEnd && *end != '>'; end++) { }
if (end == _gtoEnd) {
return Common::String();
}
return Common::String(text, end);
}
void DialogsManager::reinitVariables() {
for (Common::Array<DialogVariable>::iterator it = _dialogsVariables.begin();
it != _dialogsVariables.end(); it++) {
it->value = 'N';
}
}
const DialogsManager::DialogVariable &DialogsManager::find(const Common::String &name) const {
for (Common::Array<DialogVariable>::const_iterator it = _dialogsVariables.begin();
it != _dialogsVariables.end(); it++) {
if (it->name == name) {
return *it;
}
}
error("Can't find dialog variable %s", name.c_str());
}
DialogsManager::DialogVariable &DialogsManager::find(const Common::String &name) {
for (Common::Array<DialogVariable>::iterator it = _dialogsVariables.begin();
it != _dialogsVariables.end(); it++) {
if (it->name == name) {
return *it;
}
}
error("Can't find dialog variable %s", name.c_str());
}
const char *DialogsManager::nextLine(const char *currentPtr) const {
for (; currentPtr < _gtoEnd && *currentPtr != '\r'; currentPtr++) { }
/* Go after the \r */
return nextChar(currentPtr);
}
const char *DialogsManager::nextChar(const char *currentPtr) const {
if (currentPtr == nullptr || currentPtr < _gtoBuffer || currentPtr >= _gtoEnd) {
return nullptr;
}
currentPtr++;
if (currentPtr >= _gtoEnd) {
return nullptr;
} else {
return currentPtr;
}
}
const char *DialogsManager::previousMatch(const char *currentPtr, const char *str) const {
if (currentPtr == nullptr || currentPtr >= _gtoEnd || currentPtr < _gtoBuffer) {
return nullptr;
}
uint matchLen = strlen(str);
for (; currentPtr >= _gtoBuffer; currentPtr--) {
if (*currentPtr == str[0]) {
if (!strncmp(currentPtr, str, matchLen)) {
break;
}
}
}
if (currentPtr < _gtoBuffer) {
return nullptr;
} else {
return currentPtr;
}
}
bool DialogsManager::play(const Common::String &sequence, bool &slowStop) {
const char *label = findSequence(sequence.c_str());
if (!label) {
error("Can't find sequence '%s' in GTO", sequence.c_str());
}
Common::String video = sequence;
const char *text = findLabel(label);
slowStop = false;
bool playerLabel = !strncmp(label, "JOU", 3);
bool didSomething = false;
bool finished = false;
/* Keep the gotoList outside the loop to avoid it being freed at the end of it and
* having label possibly pointing on free memory */
Common::Array<DialogsManager::Goto> gotoList;
while (!finished) {
const char *actions;
if (playerLabel) {
/* If sequence begins with a player label go to action directly */
playerLabel = false;
actions = text;
// Maybe a bug in original game, we should go to next line
} else if (!strncmp(text, "<#>", 3)) {
/* Text is empty: go to action directly */
actions = nextLine(text);
} else {
/* Real text, play video */
video = findVideo(text);
Common::String properText = getText(text);
Common::String sound = getLabelSound(label);
Common::HashMap<Common::String, SubtitlesSettings>::const_iterator settingsIt =
_subtitlesSettings.find(video);
if (settingsIt == _subtitlesSettings.end()) {
settingsIt = _subtitlesSettings.find("default");
}
if (settingsIt == _subtitlesSettings.end()) {
error("No video settings for %s", video.c_str());
}
playDialog(video, sound, properText, settingsIt->_value);
didSomething = true;
actions = nextLine(text);
}
gotoList = executeAfterPlayAndBuildGotoList(actions);
Common::StringArray questions;
bool endOfConversationFound = false;;
if (_ignoreNoEndOfConversation) {
// Don't check if there is an end, so, there is one
endOfConversationFound = true;
}
for (Common::Array<DialogsManager::Goto>::iterator it = gotoList.begin(); it != gotoList.end();
it++) {
if (!endOfConversationFound && it->label.hasPrefix("JOU")) {
// No need to get the real label here, we just need to know if the question ends up
if (!executePlayerQuestion(it->text, true)) {
endOfConversationFound = true;
}
}
assert(it->text);
const char *questionStart = it->text + 1;
const char *questionEnd = questionStart;
for (; *questionEnd != '>'; questionEnd++) { }
questions.push_back(Common::String(questionStart, questionEnd));
}
uint eocInserted = uint(-1);
if (!endOfConversationFound && questions.size() > 0) {
eocInserted = questions.size();
questions.push_back(_endOfConversationText);
}
if (questions.size() == 0) {
// There are no choices, just quit with a pause to avoid abrupt ending
slowStop = true;
break;
}
if (gotoList[0].label.hasPrefix("JOU")) {
// We must give a subject
uint playerChoice = askPlayerQuestions(video, questions);
didSomething = true;
// -1 when shouldAbort
if (playerChoice == uint(-1) || playerChoice == eocInserted) {
break;
}
text = executePlayerQuestion(gotoList[playerChoice].text, false, &label);
if (!text) {
break;
}
} else if (gotoList[0].label.hasPrefix("MES")) {
// Display a simple message
const char *messageStart = gotoList[0].text + 1;
const char *messageEnd = messageStart;
for (; *messageEnd != '>'; messageEnd++) { }
displayMessage(Common::String(messageStart, messageEnd));
break;
} else {
// Unattended conversation: two NPC speak
label = gotoList[0].label.c_str();
text = gotoList[0].text;
}
}
return didSomething;
}
Common::Array<DialogsManager::Goto> DialogsManager::executeAfterPlayAndBuildGotoList(
const char *actions) {
Common::Array<DialogsManager::Goto> gotos;
for (; actions && *actions != ':'; actions = nextLine(actions)) {
if (!strncmp(actions, "GOTO ", 5)) {
buildGotoGoto(actions, gotos);
break;
} else if (!strncmp(actions, "IF ", 3)) {
if (buildGotoIf(actions, gotos)) {
break;
}
} else if (!strncmp(actions, "LET ", 4)) {
executeLet(actions);
} else if (!strncmp(actions, "SHOW ", 5)) {
executeShow(actions);
}
}
return gotos;
}
void DialogsManager::buildGotoGoto(const char *gotoLine, Common::Array<Goto> &gotos) {
Common::String label;
gotoLine = gotoLine + 5;
while (true) {
const char *labelEnd = gotoLine;
for (labelEnd = gotoLine; *labelEnd >= '0' && *labelEnd <= 'Z'; labelEnd++) { }
label = Common::String(gotoLine, labelEnd);
if (label == "REM") {
break;
}
// To build goto list, no need to get back the real label position
const char *text = findLabel(label.c_str());
gotos.push_back(Goto(label, text));
if (*labelEnd == '.') {
if (!strncmp(labelEnd, ".WAV", 4)) {
labelEnd += 4;
} else {
debug("Problem with GOTO.WAV: '%s'", gotoLine);
}
}
for (; *labelEnd == ' ' || *labelEnd == ','; labelEnd++) { }
if (*labelEnd == '\r') {
break;
}
// Next goto tag
gotoLine = labelEnd;
}
}
bool DialogsManager::buildGotoIf(const char *ifLine, Common::Array<Goto> &gotos) {
ifLine += 3;
bool finishedConditions = false;
while (!finishedConditions) {
const char *endVar = ifLine;
const char *equalPos;
// Find next '='
for (; *endVar != '='; endVar++) { }
equalPos = endVar;
// Strip spaces at the end
endVar--;
for (; *endVar == ' '; endVar--) { }
endVar++;
Common::String variable(ifLine, endVar);
const char *testValue = equalPos + 1;
for (; *testValue == ' ' || *testValue == '\t'; testValue++) { }
byte value = (*this)[variable];
if (value != *testValue) {
// IF is not taken, go to next line
return false;
}
ifLine = testValue + 1;
for (; *ifLine == ' ' || *ifLine == '\t'; ifLine++) { }
if (!strncmp(ifLine, "AND IF ", 7)) {
ifLine += 7;
} else {
finishedConditions = true;
}
}
/* We are in the (implicit) THEN part of the IF
* ifLine points to the instruction */
if (!strncmp(ifLine, "GOTO", 4)) {
buildGotoGoto(ifLine, gotos);
} else if (!strncmp(ifLine, "LET", 3)) {
executeLet(ifLine);
} else if (!strncmp(ifLine, "SHOW", 4)) {
executeShow(ifLine);
} else {
debug("Invalid IF line: %s", ifLine);
return false;
}
return true;
}
void DialogsManager::executeLet(const char *letLine) {
letLine = letLine + 4;
const char *endVar = letLine;
const char *equalPos;
// Find next '='
for (; *endVar != '='; endVar++) { }
equalPos = endVar;
// Strip spaces at the end
endVar--;
for (; *endVar == ' '; endVar--) { }
endVar++;
Common::String variable(letLine, endVar);
(*this)[variable] = equalPos[1];
}
void DialogsManager::executeShow(const char *showLine) {
showLine = showLine + 5;
const char *endShow = showLine;
// Find next ')' and include it
for (; *endShow != ')'; endShow++) { }
endShow++;
Common::String show(showLine, endShow);
executeShow(show);
}
const char *DialogsManager::executePlayerQuestion(const char *text, bool dryRun,
const char **realLabel) {
// Go after the text
const char *actions = nextLine(text);
while (actions && *actions != ':') {
if (!strncmp(actions, "IF ", 3)) {
actions = parseIf(actions);
} else if (!strncmp(actions, "LET ", 4)) {
if (!dryRun) {
executeLet(actions);
}
actions = nextLine(actions);
} else if (!strncmp(actions, "GOTO ", 5)) {
return findLabel(actions + 5, realLabel);
} else {
actions = nextLine(actions);
}
}
// There were no GOTO, so it's the end of the conversation
return nullptr;
}
const char *DialogsManager::parseIf(const char *ifLine) {
ifLine += 3;
bool finishedConditions = false;
while (!finishedConditions) {
const char *endVar = ifLine;
const char *equalPos;
// Find next '='
for (; *endVar != '='; endVar++) { }
equalPos = endVar;
// Strip spaces at the end
endVar--;
for (; *endVar == ' '; endVar--) { }
endVar++;
Common::String variable(ifLine, endVar);
const char *testValue = equalPos + 1;
for (; *testValue == ' ' || *testValue == '\t'; testValue++) { }
byte value = (*this)[variable];
if (value != *testValue) {
// IF is not taken, go to next line
return nextLine(ifLine);
}
ifLine = testValue + 1;
for (; *ifLine == ' ' || *ifLine == '\t'; ifLine++) { }
if (!strncmp(ifLine, "AND IF ", 7)) {
ifLine += 7;
} else {
finishedConditions = true;
}
}
/* We are in the (implicit) THEN part of the IF
* ifLine points to the instruction */
return ifLine;
}
void DialogsManager::registerSubtitlesSettings(const Common::String &videoName,
const SubtitlesSettings &settings) {
_subtitlesSettings[videoName] = settings;
}
} // End of namespace CryOmni3D
|