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
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* 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 "engines/stark/resources/dialog.h"
#include "engines/stark/formats/xrc.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/knowledge.h"
#include "engines/stark/resources/script.h"
#include "engines/stark/resources/speech.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/stateprovider.h"
namespace Stark {
namespace Resources {
Dialog::~Dialog() {
}
Dialog::Dialog(Object *parent, byte subType, uint16 index, const Common::String &name) :
Object(parent, subType, index, name),
_character(0),
_hasAskAbout(0) {
_type = TYPE;
}
void Dialog::readData(Formats::XRCReadStream *stream) {
Object::readData(stream);
_hasAskAbout = stream->readUint32LE();
_character = stream->readUint32LE();
uint32 numTopics = stream->readUint32LE();
for (uint32 i = 0; i < numTopics; i++) {
Topic topic;
topic._removeOnceDepleted = stream->readBool();
uint32 numReplies = stream->readUint32LE();
for (uint j = 0; j < numReplies; j++) {
Reply reply;
reply._conditionType = stream->readUint32LE();
reply._conditionReference = stream->readResourceReference();
reply._conditionScriptReference = stream->readResourceReference();
reply._conditionReversed = stream->readUint32LE();
reply._field_88 = stream->readUint32LE();
reply._minChapter = stream->readUint32LE();
reply._maxChapter = stream->readUint32LE();
reply._noCaption = stream->readUint32LE();
reply._nextDialogIndex = stream->readSint32LE();
reply._nextScriptReference = stream->readResourceReference();
uint32 numLines = stream->readUint32LE();
for (uint k = 0; k < numLines; k++) {
reply._lines.push_back(stream->readResourceReference());
reply._lines.push_back(stream->readResourceReference());
}
topic._replies.push_back(reply);
}
_topics.push_back(topic);
}
}
void Dialog::saveLoad(ResourceSerializer *serializer) {
for (uint i = 0; i < _topics.size(); i++) {
serializer->syncAsSint32LE(_topics[i]._currentReplyIndex);
}
}
void Dialog::printData() {
Object::printData();
debug("character: %d", _character);
debug("hasAskAbout: %d", _hasAskAbout);
for (uint32 i = 0; i < _topics.size(); i++) {
Topic &topic = _topics[i];
debug("topic[%d].removeOnceDepleted: %d", i, topic._removeOnceDepleted);
for (uint j = 0; j < topic._replies.size(); j++) {
Reply reply = topic._replies[j];
debug("topic[%d].reply[%d].conditionType: %d", i, j, reply._conditionType);
debug("topic[%d].reply[%d].conditionReference: %s", i, j, reply._conditionReference.describe().c_str());
debug("topic[%d].reply[%d].conditionScriptReference: %s", i, j, reply._conditionScriptReference.describe().c_str());
debug("topic[%d].reply[%d].conditionReversed: %d", i, j, reply._conditionReversed);
debug("topic[%d].reply[%d].minChapter: %d", i, j, reply._minChapter);
debug("topic[%d].reply[%d].maxChapter: %d", i, j, reply._maxChapter);
debug("topic[%d].reply[%d].noCaption: %d", i, j, reply._noCaption);
debug("topic[%d].reply[%d].field_88: %d", i, j, reply._field_88);
debug("topic[%d].reply[%d].nextScriptReference: %s", i, j, reply._nextScriptReference.describe().c_str());
debug("topic[%d].reply[%d].nextDialogIndex: %d", i, j, reply._nextDialogIndex);
for (uint k = 0; k < reply._lines.size(); k++) {
debug("topic[%d].reply[%d].line[%d]: %s", i, j, k, reply._lines[k].describe().c_str());
}
}
}
}
Dialog::TopicArray Dialog::listAvailableTopics() {
Common::Array<Dialog::Topic *> topics;
for (uint i = 0; i < _topics.size(); i++) {
Topic *topic = &_topics[i];
if (topic->getNextReplyIndex() < 0) {
continue;
}
topics.push_back(topic);
}
return topics;
}
Dialog::Topic::Topic() :
_removeOnceDepleted(true),
_currentReplyIndex(-1) {
}
int32 Dialog::Topic::getNextReplyIndex() const {
uint32 nextIndex = _currentReplyIndex + 1;
if (nextIndex >= _replies.size()) {
// No more replies ...
if (_removeOnceDepleted || _replies.empty()) {
// Don't show this topic
return -1;
} else {
// Repeat the last reply
nextIndex = _replies.size() - 1;
}
}
uint32 currentChapter = StarkGlobal->getCurrentChapter();
// Skip replies from previous chapters
while (nextIndex < _replies.size() && _replies[nextIndex]._maxChapter < currentChapter) {
nextIndex++;
}
if (nextIndex >= _replies.size()) {
// No more replies ...
if (_removeOnceDepleted || _replies.empty()) {
// Don't show this topic
return -1;
} else {
// Repeat the last reply
nextIndex = _replies.size() - 1;
}
}
// Chapter check
const Reply &reply = _replies[nextIndex];
if (currentChapter < reply._minChapter || currentChapter >= reply._maxChapter) {
return -1;
}
return nextIndex;
}
Dialog::Reply *Dialog::Topic::startReply(uint32 index) {
_currentReplyIndex = index;
Reply *reply = &_replies[_currentReplyIndex];
reply->start();
return reply;
}
Dialog::Reply *Dialog::Topic::getReply(uint32 index) {
return &_replies[index];
}
Common::String Dialog::Topic::getCaption() const {
int32 replyIndex = getNextReplyIndex();
if (replyIndex < 0) {
error("Trying to obtain the caption of a depleted dialog topic.");
}
const Reply &reply = _replies[replyIndex];
if (reply._lines.empty()) {
error("Trying to obtain the caption of a reply with no lines.");
}
Speech *speech = reply._lines[0].resolve<Speech>();
if (speech) {
return speech->getPhrase();
} else {
return "No Caption";
}
}
Dialog::Reply::Reply() :
_conditionReversed(0),
_field_88(0),
_minChapter(0),
_maxChapter(999),
_conditionType(0),
_noCaption(0),
_nextDialogIndex(-1),
_nextSpeechIndex(-1) {
}
void Dialog::Reply::start() {
if (_noCaption) {
_nextSpeechIndex = -1;
} else {
// Skip the first line when it is a caption
_nextSpeechIndex = 0;
}
goToNextLine();
}
void Dialog::Reply::goToNextLine() {
_nextSpeechIndex++;
while ((uint32)_nextSpeechIndex < _lines.size() && _lines[_nextSpeechIndex].empty()) {
_nextSpeechIndex++;
}
if ((uint32)_nextSpeechIndex >= _lines.size()) {
_nextSpeechIndex = -2; // No more lines
}
}
Speech *Dialog::Reply::getCurrentSpeech() {
if (_nextSpeechIndex < 0) {
return nullptr;
}
return _lines[_nextSpeechIndex].resolve<Speech>();
}
bool Dialog::Reply::checkCondition() const {
switch (_conditionType) {
case kConditionTypeAlways:
return true;
case kConditionTypeNoOtherOptions:
return true; // Will be removed from to the options later if some other options are available
case kConditionTypeHasItem: {
Item *item = _conditionReference.resolve<Item>();
return item->isEnabled();
}
case kConditionTypeCheckValue4:
case kConditionTypeCheckValue5: {
Knowledge *condition = _conditionReference.resolve<Knowledge>();
return condition->getBooleanValue();
}
case kConditionTypeRunScriptCheckValue: {
Script *conditionScript = _conditionScriptReference.resolve<Script>();
conditionScript->execute(Resources::Script::kCallModeDialogAnswer);
Knowledge *condition = _conditionReference.resolve<Knowledge>();
return condition->getBooleanValue();
}
default:
warning("Unimplemented dialog reply condition %d", _conditionType);
return true;
}
}
bool Dialog::Reply::isLastOnly() const {
return _conditionType == kConditionTypeNoOtherOptions;
}
Dialog *Dialog::getNextDialog(Dialog::Reply *reply) {
if (reply->_nextDialogIndex < 0) {
return nullptr;
}
return _parent->findChildWithIndex<Dialog>(reply->_nextDialogIndex);
}
Script *Dialog::getNextScript(Dialog::Reply *reply) {
if (reply->_nextScriptReference.empty()) {
return nullptr;
}
return reply->_nextScriptReference.resolve<Script>();
}
Common::String Dialog::getDiaryTitle() const {
return _parent->getName();
}
int32 Dialog::getCharacter() const {
return _character;
}
} // End of namespace Resources
} // End of namespace Stark
|