scummvm/engines/fullpipe/behavior.cpp

251 lines
6.6 KiB
C++
Raw Normal View History

/* 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 "fullpipe/fullpipe.h"
#include "fullpipe/objects.h"
#include "fullpipe/behavior.h"
#include "fullpipe/statics.h"
#include "fullpipe/messages.h"
namespace Fullpipe {
BehaviorManager::BehaviorManager() {
_scene = 0;
_isActive = 1;
}
BehaviorManager::~BehaviorManager() {
clear();
}
void BehaviorManager::clear() {
for (uint i = 0; i < _behaviors.size(); i++) {
for (int j = 0; j < _behaviors[i]->_itemsCount; j++)
delete _behaviors[i]->_bheItems[j];
delete _behaviors[i];
}
_behaviors.clear();
}
void BehaviorManager::initBehavior(Scene *sc, CGameVar *var) {
clear();
_scene = sc;
BehaviorInfo *behinfo;
CGameVar *behvar = var->getSubVarByName("BEHAVIOR");
if (!behvar)
return;
for (CGameVar *subvar = behvar->_subVars; subvar; subvar = subvar->_nextVarObj) {
if (!strcmp(subvar->_varName, "AMBIENT")) {
behinfo = new BehaviorInfo;
behinfo->initAmbientBehavior(subvar);
_behaviors.push_back(behinfo);
} else {
StaticANIObject *ani = sc->getStaticANIObject1ByName(subvar->_varName, -1);
if (ani)
for (uint i = 0; i < sc->_staticANIObjectList1.size(); i++)
if (((StaticANIObject *)sc->_staticANIObjectList1[i])->_id == ani->_id) {
behinfo = new BehaviorInfo;
behinfo->initObjectBehavior(subvar, sc, ani);
behinfo->_ani = (StaticANIObject *)sc->_staticANIObjectList1[i];
_behaviors.push_back(behinfo);
}
}
}
}
2013-08-11 13:39:05 +03:00
void BehaviorManager::updateBehaviors() {
if (!_isActive)
return;
for (uint i = 0; i < _behaviors.size(); i++) {
BehaviorInfo *beh = _behaviors[i];
if (!beh->_ani) {
beh->_counter++;
if (beh->_counter >= beh->_counterMax)
updateBehavior(beh, beh->_bheItems[0]);
continue;
}
if (beh->_ani->_movement || !(beh->_ani->_flags & 4) || (beh->_ani->_flags & 2)) {
beh->_staticsId = 0;
continue;
}
if (beh->_ani->_statics->_staticsId == beh->_staticsId) {
beh->_counter++;
if (beh->_counter >= beh->_counterMax) {
if (beh->_subIndex >= 0 && !(beh->_flags & 1) && beh->_ani->_messageQueueId <= 0)
updateStaticAniBehavior(beh->_ani, beh->_counter, beh->_bheItems[beh->_subIndex]);
}
} else {
beh->_staticsId = beh->_ani->_statics->_staticsId;
beh->_counter = 0;
beh->_subIndex = -1;
for (int j = 0; j < beh->_itemsCount; j++)
if (beh->_bheItems[j]->_staticsId == beh->_staticsId) {
beh->_subIndex = j;
break;
}
}
}
}
void BehaviorManager::updateBehavior(BehaviorInfo *behaviorInfo, BehaviorEntry *entry) {
for (int i = 0; i < entry->_itemsCount; i++) {
BehaviorEntryInfo *bhi = entry->_items[i];
if (!(bhi->_flags & 1)) {
if (bhi->_flags & 2) {
MessageQueue *mq = new MessageQueue(entry->_items[i]->_messageQueue, 0, 1);
mq->sendNextCommand();
entry->_items[i]->_flags &= 0xFFFFFFFD;
} else if (behaviorInfo->_counter >= bhi->_delay && bhi->_percent && g_fullpipe->_rnd->getRandomNumber(32767) <= entry->_items[i]->_percent) {
MessageQueue *mq = new MessageQueue(entry->_items[i]->_messageQueue, 0, 1);
mq->sendNextCommand();
behaviorInfo->_counter = 0;
}
}
}
}
void BehaviorManager::updateStaticAniBehavior(StaticANIObject *ani, unsigned int delay, BehaviorEntry *behaviorEntry) {
warning("STUB: BehaviorManager::updateStaticAniBehavior()");
2013-08-11 13:39:05 +03:00
}
void BehaviorInfo::clear() {
_ani = 0;
_staticsId = 0;
_counter = 0;
_counterMax = 0;
_flags = 0;
_subIndex = 0;
_itemsCount = 0;
_bheItems.clear();
}
void BehaviorInfo::initAmbientBehavior(CGameVar *var) {
warning("STUB: BehaviorInfo::initAmbientBehavior(%s)", transCyrillic((byte *)var->_varName));
}
void BehaviorInfo::initObjectBehavior(CGameVar *var, Scene *sc, StaticANIObject *ani) {
debug(0, "BehaviorInfo::initObjectBehavior(%s)", transCyrillic((byte *)var->_varName));
clear();
_itemsCount = var->getSubVarsCount();
_counterMax = -1;
while (var->_varType == 2) {
if (strcmp(var->_value.stringValue, "ROOT"))
break;
CGameVar *v1 = g_fullpipe->getGameLoaderGameVar()->getSubVarByName("BEHAVIOR")->getSubVarByName(ani->getName());
if (v1 == var)
return;
sc = g_fullpipe->accessScene(ani->_sceneId);
clear();
var = v1;
_itemsCount = var->getSubVarsCount();
_counterMax = -1;
}
for (int i = 0; i < _itemsCount; i++) {
int maxDelay;
_bheItems.push_back(new BehaviorEntry(var->getSubVarByIndex(i), sc, ani, &maxDelay));
if (maxDelay < _counterMax )
_counterMax = maxDelay;
}
}
2013-08-14 12:52:59 +03:00
BehaviorEntry::BehaviorEntry(CGameVar *var, Scene *sc, StaticANIObject *ani, int *minDelay) {
_staticsId = 0;
_itemsCount = 0;
*minDelay = -1;
int totalPercent = 0;
_flags = 0;
_items = 0;
Statics *st = ani->getStaticsByName(var->_varName);
if (st)
_staticsId = st->_staticsId;
_itemsCount = var->getSubVarsCount();
if (_itemsCount) {
_items = (BehaviorEntryInfo**)calloc(_itemsCount, sizeof(BehaviorEntryInfo *));
for (int i = 0; i < _itemsCount; i++) {
CGameVar *subvar = var->getSubVarByIndex(i);
_items[i] = new BehaviorEntryInfo(subvar, sc);
totalPercent += _items[i]->_percent;
if (_items[i]->_delay < *minDelay)
*minDelay = _items[i]->_delay;
}
if (!*minDelay && totalPercent == 1000)
_flags |= 1;
}
}
BehaviorEntryInfo::BehaviorEntryInfo(CGameVar *subvar, Scene *sc) {
_messageQueue = 0;
_delay = 0;
_percent = 0;
_flags = 0;
_messageQueue = sc->getMessageQueueByName(subvar->_varName);
CGameVar *vart = subvar->getSubVarByName("dwDelay");
if (vart)
_delay = vart->_value.intValue;
vart = subvar->getSubVarByName("dwPercent");
if (vart)
_percent = 0x7FFF * vart->_value.intValue / 1000;
vart = subvar->getSubVarByName("dwFlags");
if (vart && vart->_varType == 2 && strstr(vart->_value.stringValue, "QDESC_AUTOSTART"))
_flags |= 2;
}
} // End of namespace Fullpipe