From c660bbf1416410dbb3a985300b8c037f7f30eab3 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Mon, 6 Jun 2016 08:52:02 -0400 Subject: [PATCH] TITANIC: Beginnings of loading NPC range data --- engines/titanic/true_talk/barbot_script.cpp | 3 +- engines/titanic/true_talk/bellbot_script.cpp | 2 +- engines/titanic/true_talk/deskbot_script.cpp | 2 +- engines/titanic/true_talk/doorbot_script.cpp | 2 +- engines/titanic/true_talk/liftbot_script.cpp | 2 +- engines/titanic/true_talk/maitred_script.cpp | 2 +- engines/titanic/true_talk/tt_npc_script.cpp | 54 ++++++++++++++------ engines/titanic/true_talk/tt_npc_script.h | 22 ++++---- 8 files changed, 55 insertions(+), 34 deletions(-) diff --git a/engines/titanic/true_talk/barbot_script.cpp b/engines/titanic/true_talk/barbot_script.cpp index 9d4bdf6bc08..2a67a7b4948 100644 --- a/engines/titanic/true_talk/barbot_script.cpp +++ b/engines/titanic/true_talk/barbot_script.cpp @@ -33,7 +33,8 @@ BarbotScript::BarbotScript(int val1, const char *charClass, int v2, const char *charName, int v3, int val2, int v4, int v5, int v6, int v7) : TTnpcScript(val1, charClass, v2, charName, v3, val2, v4, v5, v6, v7) { _state = 0; - load("Responses/Barbot"); + loadRanges("Ranges/Barbot"); + loadResponses("Responses/Barbot"); } int BarbotScript::chooseResponse(TTroomScript *roomScript, TTsentence *sentence, uint tag) { diff --git a/engines/titanic/true_talk/bellbot_script.cpp b/engines/titanic/true_talk/bellbot_script.cpp index d40de3d4b9d..a02ef147c44 100644 --- a/engines/titanic/true_talk/bellbot_script.cpp +++ b/engines/titanic/true_talk/bellbot_script.cpp @@ -39,7 +39,7 @@ BellbotScript::BellbotScript(int val1, const char *charClass, int v2, _array[0] = 100; _array[1] = 0; - load("Responses/Bellbot", 4); + loadResponses("Responses/Bellbot", 4); } void BellbotScript::proc7(int v1, int v2) { diff --git a/engines/titanic/true_talk/deskbot_script.cpp b/engines/titanic/true_talk/deskbot_script.cpp index 28a211b2d17..85b59b062a5 100644 --- a/engines/titanic/true_talk/deskbot_script.cpp +++ b/engines/titanic/true_talk/deskbot_script.cpp @@ -40,7 +40,7 @@ DeskbotScript::DeskbotScript(int val1, const char *charClass, int v2, if (_field74 == 1) _field74 = 0; - load("Responses/Deskbot", 4); + loadResponses("Responses/Deskbot", 4); } void DeskbotScript::proc7(int v1, int v2) { diff --git a/engines/titanic/true_talk/doorbot_script.cpp b/engines/titanic/true_talk/doorbot_script.cpp index b76f7aca9d9..7d291b6e7df 100644 --- a/engines/titanic/true_talk/doorbot_script.cpp +++ b/engines/titanic/true_talk/doorbot_script.cpp @@ -35,7 +35,7 @@ DoorbotScript::DoorbotScript(int val1, const char *charClass, int v2, TTnpcScript(val1, charClass, v2, charName, v3, val2, v4, v5, v6, v7) { Common::fill(&_array[0], &_array[148], 0); _state = 0; - load("Responses/Doorbot"); + loadResponses("Responses/Doorbot"); } int DoorbotScript::chooseResponse(TTroomScript *roomScript, TTsentence *sentence, uint tag) { diff --git a/engines/titanic/true_talk/liftbot_script.cpp b/engines/titanic/true_talk/liftbot_script.cpp index 9ba80a67b4c..5a63a51bb3b 100644 --- a/engines/titanic/true_talk/liftbot_script.cpp +++ b/engines/titanic/true_talk/liftbot_script.cpp @@ -33,7 +33,7 @@ LiftbotScript::LiftbotScript(int val1, const char *charClass, int v2, const char *charName, int v3, int val2, int v4, int v5, int v6, int v7) : TTnpcScript(val1, charClass, v2, charName, v3, val2, v4, v5, v6, v7) { _state = 0; - load("Responses/Liftbot"); + loadResponses("Responses/Liftbot"); } int LiftbotScript::chooseResponse(TTroomScript *roomScript, TTsentence *sentence, uint tag) { diff --git a/engines/titanic/true_talk/maitred_script.cpp b/engines/titanic/true_talk/maitred_script.cpp index bdfd505e6e6..7bd19c4c71d 100644 --- a/engines/titanic/true_talk/maitred_script.cpp +++ b/engines/titanic/true_talk/maitred_script.cpp @@ -38,7 +38,7 @@ MaitreDScript::MaitreDScript(int val1, const char *charClass, int v2, CTrueTalkManager::setFlags(15, 0); CTrueTalkManager::setFlags(16, 0); - load("Responses/MaitreD"); + loadResponses("Responses/MaitreD"); } int MaitreDScript::chooseResponse(TTroomScript *roomScript, TTsentence *sentence, uint tag) { diff --git a/engines/titanic/true_talk/tt_npc_script.cpp b/engines/titanic/true_talk/tt_npc_script.cpp index e31be05b13a..19ff05d1341 100644 --- a/engines/titanic/true_talk/tt_npc_script.cpp +++ b/engines/titanic/true_talk/tt_npc_script.cpp @@ -40,14 +40,17 @@ int TTnpcScriptResponse::size() const { /*------------------------------------------------------------------------*/ -TTscriptRange::TTscriptRange(uint id, const uint *arrayP, bool isRandom, - bool isSequential) : - _id(id), _arrayP(arrayP), _nextP(nullptr) { +TTscriptRange::TTscriptRange(uint id, const Common::Array &values, + bool isRandom, bool isSequential) : + _id(id), _nextP(nullptr) { _mode = SF_NONE; if (isRandom) _mode = SF_RANDOM; if (isSequential) _mode = SF_SEQUENTIAL; + + for (uint idx = 0; idx < values.size(); ++idx) + _values.push_back(values[idx]); } /*------------------------------------------------------------------------*/ @@ -77,7 +80,7 @@ TTnpcScript::TTnpcScript(int charId, const char *charClass, int v2, resetFlags(); } -void TTnpcScript::load(const char *name, int valuesPerResponse) { +void TTnpcScript::loadResponses(const char *name, int valuesPerResponse) { _valuesPerResponse = valuesPerResponse; Common::SeekableReadStream *r = g_vm->_filesManager->getResource(name); @@ -93,6 +96,27 @@ void TTnpcScript::load(const char *name, int valuesPerResponse) { delete r; } +void TTnpcScript::loadRanges(const char *name) { + Common::SeekableReadStream *r = g_vm->_filesManager->getResource(name); + + while (r->pos() < r->size()) { + Common::Array values; + uint id = r->readUint32LE(); + bool isRandom = r->readByte(); + bool isSequential = r->readByte(); + + uint v; + do { + v = r->readUint32LE(); + values.push_back(v); + } while (v); + + addRange(id, values, isRandom, isSequential); + } + + delete r; +} + void TTnpcScript::resetFlags() { Common::fill(&_array[20], &_array[136], 0); _field2CC = false; @@ -197,27 +221,25 @@ uint TTnpcScript::getRangeValue(uint id) { switch (range->_mode) { case SF_RANDOM: { - uint count = 0; - for (const uint *p = range->_arrayP; *p; ++p) - ++count; + uint count = range->_values.size(); uint index = getRandomNumber(count) - 1; - if (count > 1 && range->_arrayP[index] == range->_priorIndex) { + if (count > 1 && range->_values[index] == range->_priorIndex) { for (int retry = 0; retry < 8 && index != range->_priorIndex; ++retry) index = getRandomNumber(count) - 1; } range->_priorIndex = index; - return range->_arrayP[index]; + return range->_values[index]; } case SF_SEQUENTIAL: { // Get the next value from the array sequentially - int val = range->_arrayP[range->_priorIndex]; + int val = range->_values[range->_priorIndex]; if (!val) { // Reached end of array, so reset back to start range->_priorIndex = 1; - val = range->_arrayP[1]; + val = range->_values[1]; } ++range->_priorIndex; @@ -225,12 +247,12 @@ uint TTnpcScript::getRangeValue(uint id) { } default: - if (range->_arrayP[range->_priorIndex]) - return range->_arrayP[range->_priorIndex++]; + if (range->_values[range->_priorIndex]) + return range->_values[range->_priorIndex++]; range->_priorIndex = 1; ++_rangeResetCtr; - return range->_arrayP[0]; + return range->_values[0]; } } @@ -628,8 +650,8 @@ bool TTnpcScript::defaultProcess(TTroomScript *roomScript, TTsentence *sentence) return false; } -void TTnpcScript::addRange(uint id, const uint *arrayP, bool isRandom, bool isSequential) { - _ranges.push_back(TTscriptRange(id, arrayP, isRandom, isSequential)); +void TTnpcScript::addRange(uint id, const Common::Array &values, bool isRandom, bool isSequential) { + _ranges.push_back(TTscriptRange(id, values, isRandom, isSequential)); } TTscriptRange *TTnpcScript::findRange(uint id) { diff --git a/engines/titanic/true_talk/tt_npc_script.h b/engines/titanic/true_talk/tt_npc_script.h index dadea17ac24..77f44453c7b 100644 --- a/engines/titanic/true_talk/tt_npc_script.h +++ b/engines/titanic/true_talk/tt_npc_script.h @@ -51,24 +51,17 @@ struct TTnpcScriptResponse { struct TTscriptRange { uint _id; - const uint *_arrayP; + Common::Array _values; TTscriptRange *_nextP; uint _priorIndex; ScriptArrayFlag _mode; - TTscriptRange() : _id(0), _arrayP(nullptr), _nextP(nullptr), + TTscriptRange() : _id(0), _nextP(nullptr), _priorIndex(0), _mode(SF_NONE) {} - TTscriptRange(uint id, const uint *arrayP, bool isRandom, + TTscriptRange(uint id, const Common::Array &values, bool isRandom, bool isSequential); }; -struct TTscriptRangeInit { - uint id; - uint *_array; - bool _isRandom; - bool _isSequential; -}; - class TTnpcScriptBase : public TTscriptBase { protected: int _field54; @@ -123,7 +116,12 @@ protected: /** * Loads response data for the NPC from the given resource */ - void load(const char *name, int valuesPerResponse = 1); + void loadResponses(const char *name, int valuesPerResponse = 1); + + /** + * Load ranges data for the NPC from the given resource + */ + void loadRanges(const char *name); /** * Reset script flags @@ -167,7 +165,7 @@ protected: /** * Adds a new item to the list of number ranges */ - void addRange(uint id, const uint *arrayP, bool isRandom, bool isSequential); + void addRange(uint id, const Common::Array &values, bool isRandom, bool isSequential); /** * Finds an entry in the list of prevoiusly registered number ranges