TITANIC: Beginnings of loading NPC range data

This commit is contained in:
Paul Gilbert 2016-06-06 08:52:02 -04:00
parent 59c033e68f
commit c660bbf141
8 changed files with 55 additions and 34 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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<uint> &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<uint> 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<uint> &values, bool isRandom, bool isSequential) {
_ranges.push_back(TTscriptRange(id, values, isRandom, isSequential));
}
TTscriptRange *TTnpcScript::findRange(uint id) {

View File

@ -51,24 +51,17 @@ struct TTnpcScriptResponse {
struct TTscriptRange {
uint _id;
const uint *_arrayP;
Common::Array<uint> _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<uint> &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<uint> &values, bool isRandom, bool isSequential);
/**
* Finds an entry in the list of prevoiusly registered number ranges