ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

engines/stark/resources/dialog.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/resources/dialog.h"
00024 
00025 #include "engines/stark/formats/xrc.h"
00026 
00027 #include "engines/stark/resources/item.h"
00028 #include "engines/stark/resources/knowledge.h"
00029 #include "engines/stark/resources/script.h"
00030 #include "engines/stark/resources/speech.h"
00031 
00032 #include "engines/stark/services/global.h"
00033 #include "engines/stark/services/services.h"
00034 #include "engines/stark/services/stateprovider.h"
00035 
00036 namespace Stark {
00037 namespace Resources {
00038 
00039 Dialog::~Dialog() {
00040 }
00041 
00042 Dialog::Dialog(Object *parent, byte subType, uint16 index, const Common::String &name) :
00043         Object(parent, subType, index, name),
00044         _character(0),
00045         _hasAskAbout(0) {
00046     _type = TYPE;
00047 }
00048 
00049 void Dialog::readData(Formats::XRCReadStream *stream) {
00050     Object::readData(stream);
00051 
00052     _hasAskAbout = stream->readUint32LE();
00053     _character = stream->readUint32LE();
00054 
00055     uint32 numTopics = stream->readUint32LE();
00056     for (uint32 i = 0; i < numTopics; i++) {
00057         Topic topic;
00058         topic._removeOnceDepleted = stream->readBool();
00059 
00060         uint32 numReplies = stream->readUint32LE();
00061         for (uint j = 0; j < numReplies; j++) {
00062             Reply reply;
00063 
00064             reply._conditionType = stream->readUint32LE();
00065             reply._conditionReference = stream->readResourceReference();
00066             reply._conditionScriptReference = stream->readResourceReference();
00067             reply._conditionReversed = stream->readUint32LE();
00068             reply._field_88 = stream->readUint32LE();
00069             reply._minChapter = stream->readUint32LE();
00070             reply._maxChapter = stream->readUint32LE();
00071             reply._noCaption = stream->readUint32LE();
00072             reply._nextDialogIndex = stream->readSint32LE();
00073             reply._nextScriptReference = stream->readResourceReference();
00074 
00075             uint32 numLines = stream->readUint32LE();
00076             for (uint k = 0; k < numLines; k++) {
00077                 reply._lines.push_back(stream->readResourceReference());
00078                 reply._lines.push_back(stream->readResourceReference());
00079             }
00080 
00081             topic._replies.push_back(reply);
00082         }
00083 
00084         _topics.push_back(topic);
00085     }
00086 }
00087 
00088 void Dialog::saveLoad(ResourceSerializer *serializer) {
00089     for (uint i = 0; i < _topics.size(); i++) {
00090         serializer->syncAsSint32LE(_topics[i]._currentReplyIndex);
00091     }
00092 }
00093 
00094 void Dialog::printData() {
00095     Object::printData();
00096 
00097     debug("character: %d", _character);
00098     debug("hasAskAbout: %d", _hasAskAbout);
00099 
00100     for (uint32 i = 0; i < _topics.size(); i++) {
00101         Topic &topic = _topics[i];
00102         debug("topic[%d].removeOnceDepleted: %d", i, topic._removeOnceDepleted);
00103 
00104         for (uint j = 0; j < topic._replies.size(); j++) {
00105             Reply reply = topic._replies[j];
00106 
00107             debug("topic[%d].reply[%d].conditionType: %d", i, j, reply._conditionType);
00108             debug("topic[%d].reply[%d].conditionReference: %s", i, j, reply._conditionReference.describe().c_str());
00109             debug("topic[%d].reply[%d].conditionScriptReference: %s", i, j, reply._conditionScriptReference.describe().c_str());
00110             debug("topic[%d].reply[%d].conditionReversed: %d", i, j, reply._conditionReversed);
00111             debug("topic[%d].reply[%d].minChapter: %d", i, j, reply._minChapter);
00112             debug("topic[%d].reply[%d].maxChapter: %d", i, j, reply._maxChapter);
00113             debug("topic[%d].reply[%d].noCaption: %d", i, j, reply._noCaption);
00114             debug("topic[%d].reply[%d].field_88: %d", i, j, reply._field_88);
00115             debug("topic[%d].reply[%d].nextScriptReference: %s", i, j, reply._nextScriptReference.describe().c_str());
00116             debug("topic[%d].reply[%d].nextDialogIndex: %d", i, j, reply._nextDialogIndex);
00117 
00118             for (uint k = 0; k < reply._lines.size(); k++) {
00119                 debug("topic[%d].reply[%d].line[%d]: %s", i, j, k, reply._lines[k].describe().c_str());
00120             }
00121         }
00122     }
00123 }
00124 
00125 Dialog::TopicArray Dialog::listAvailableTopics() {
00126     Common::Array<Dialog::Topic *> topics;
00127 
00128     for (uint i = 0; i < _topics.size(); i++) {
00129         Topic *topic = &_topics[i];
00130         if (topic->getNextReplyIndex() < 0) {
00131             continue;
00132         }
00133 
00134         topics.push_back(topic);
00135     }
00136 
00137     return topics;
00138 }
00139 
00140 Dialog::Topic::Topic() :
00141         _removeOnceDepleted(true),
00142         _currentReplyIndex(-1) {
00143 }
00144 
00145 int32 Dialog::Topic::getNextReplyIndex() const {
00146     uint32 nextIndex = _currentReplyIndex + 1;
00147 
00148     if (nextIndex >= _replies.size()) {
00149         // No more replies ...
00150         if (_removeOnceDepleted || _replies.empty()) {
00151             // Don't show this topic
00152             return -1;
00153         } else {
00154             // Repeat the last reply
00155             nextIndex = _replies.size() - 1;
00156         }
00157     }
00158 
00159     uint32 currentChapter = StarkGlobal->getCurrentChapter();
00160 
00161     // Skip replies from previous chapters
00162     while (nextIndex < _replies.size() && _replies[nextIndex]._maxChapter < currentChapter) {
00163         nextIndex++;
00164     }
00165 
00166     if (nextIndex >= _replies.size()) {
00167         // No more replies ...
00168         if (_removeOnceDepleted || _replies.empty()) {
00169             // Don't show this topic
00170             return -1;
00171         } else {
00172             // Repeat the last reply
00173             nextIndex = _replies.size() - 1;
00174         }
00175     }
00176 
00177     // Chapter check
00178     const Reply &reply = _replies[nextIndex];
00179     if (currentChapter < reply._minChapter || currentChapter >= reply._maxChapter) {
00180         return -1;
00181     }
00182 
00183     return nextIndex;
00184 }
00185 
00186 Dialog::Reply *Dialog::Topic::startReply(uint32 index) {
00187     _currentReplyIndex = index;
00188 
00189     Reply *reply = &_replies[_currentReplyIndex];
00190     reply->start();
00191 
00192     return reply;
00193 }
00194 
00195 Dialog::Reply *Dialog::Topic::getReply(uint32 index) {
00196     return &_replies[index];
00197 }
00198 
00199 Common::String Dialog::Topic::getCaption() const {
00200     int32 replyIndex = getNextReplyIndex();
00201     if (replyIndex < 0) {
00202         error("Trying to obtain the caption of a depleted dialog topic.");
00203     }
00204 
00205     const Reply &reply = _replies[replyIndex];
00206 
00207     if (reply._lines.empty()) {
00208         error("Trying to obtain the caption of a reply with no lines.");
00209     }
00210 
00211     Speech *speech = reply._lines[0].resolve<Speech>();
00212     if (speech) {
00213         return speech->getPhrase();
00214     } else {
00215         return "No Caption";
00216     }
00217 }
00218 
00219 
00220 Dialog::Reply::Reply() :
00221         _conditionReversed(0),
00222         _field_88(0),
00223         _minChapter(0),
00224         _maxChapter(999),
00225         _conditionType(0),
00226         _noCaption(0),
00227         _nextDialogIndex(-1),
00228         _nextSpeechIndex(-1) {
00229 }
00230 
00231 void Dialog::Reply::start() {
00232     if (_noCaption) {
00233         _nextSpeechIndex = -1;
00234     } else {
00235         // Skip the first line when it is a caption
00236         _nextSpeechIndex = 0;
00237     }
00238 
00239     goToNextLine();
00240 }
00241 
00242 void Dialog::Reply::goToNextLine() {
00243     _nextSpeechIndex++;
00244     while ((uint32)_nextSpeechIndex < _lines.size() && _lines[_nextSpeechIndex].empty()) {
00245         _nextSpeechIndex++;
00246     }
00247 
00248     if ((uint32)_nextSpeechIndex >= _lines.size()) {
00249         _nextSpeechIndex = -2; // No more lines
00250     }
00251 }
00252 
00253 Speech *Dialog::Reply::getCurrentSpeech() {
00254     if (_nextSpeechIndex < 0) {
00255         return nullptr;
00256     }
00257 
00258     return _lines[_nextSpeechIndex].resolve<Speech>();
00259 }
00260 
00261 bool Dialog::Reply::checkCondition() const {
00262     bool result;
00263 
00264     switch (_conditionType) {
00265         case kConditionTypeAlways:
00266             result = true;
00267             break;
00268         case kConditionTypeNoOtherOptions:
00269             result = true; // Will be removed from to the options later if some other options are available
00270             break;
00271         case kConditionTypeHasItem: {
00272             Item *item = _conditionReference.resolve<Item>();
00273             result = item->isEnabled();
00274             break;
00275         }
00276         case kConditionTypeCheckValue4:
00277         case kConditionTypeCheckValue5: {
00278             Knowledge *condition = _conditionReference.resolve<Knowledge>();
00279             result = condition->getBooleanValue();
00280             break;
00281         }
00282         case kConditionTypeRunScriptCheckValue: {
00283             Script *conditionScript = _conditionScriptReference.resolve<Script>();
00284             conditionScript->execute(Resources::Script::kCallModeDialogAnswer);
00285 
00286             Knowledge *condition = _conditionReference.resolve<Knowledge>();
00287             result = condition->getBooleanValue();
00288             break;
00289         }
00290         default:
00291             warning("Unimplemented dialog reply condition %d", _conditionType);
00292             result = true;
00293             break;
00294     }
00295 
00296     if (_conditionReversed && (_conditionType == kConditionTypeHasItem
00297             || _conditionType == kConditionTypeCheckValue4
00298             || _conditionType == kConditionTypeCheckValue5
00299             || _conditionType == kConditionTypeRunScriptCheckValue)) {
00300         result = !result;
00301     }
00302 
00303     return result;
00304 }
00305 
00306 bool Dialog::Reply::isLastOnly() const {
00307     return _conditionType == kConditionTypeNoOtherOptions;
00308 }
00309 
00310 Dialog *Dialog::getNextDialog(Dialog::Reply *reply) {
00311     if (reply->_nextDialogIndex < 0) {
00312         return nullptr;
00313     }
00314 
00315     return _parent->findChildWithIndex<Dialog>(reply->_nextDialogIndex);
00316 }
00317 
00318 Script *Dialog::getNextScript(Dialog::Reply *reply) {
00319     if (reply->_nextScriptReference.empty()) {
00320         return nullptr;
00321     }
00322 
00323     return reply->_nextScriptReference.resolve<Script>();
00324 }
00325 
00326 Common::String Dialog::getDiaryTitle() const {
00327     return _parent->getName();
00328 }
00329 
00330 int32 Dialog::getCharacter() const {
00331     return _character;
00332 }
00333 
00334 } // End of namespace Resources
00335 } // End of namespace Stark


Generated on Sat Mar 16 2019 05:01:24 for ResidualVM by doxygen 1.7.1
curved edge   curved edge