2015-01-01 16:46:23 +01:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-01-02 10:33:42 +01:00
|
|
|
#include "engines/stark/resources/animscript.h"
|
|
|
|
|
2015-01-01 16:46:23 +01:00
|
|
|
#include "common/debug.h"
|
2015-01-02 10:33:42 +01:00
|
|
|
#include "common/random.h"
|
2015-01-01 16:46:23 +01:00
|
|
|
|
2019-01-25 09:48:31 +01:00
|
|
|
#include "engines/stark/debug.h"
|
|
|
|
|
2015-01-14 18:10:09 +01:00
|
|
|
#include "engines/stark/formats/xrc.h"
|
2016-01-03 21:27:30 +01:00
|
|
|
|
2015-01-02 10:33:42 +01:00
|
|
|
#include "engines/stark/resources/anim.h"
|
2016-01-03 21:27:30 +01:00
|
|
|
#include "engines/stark/resources/container.h"
|
2016-11-12 09:17:02 +01:00
|
|
|
#include "engines/stark/resources/location.h"
|
2016-01-03 21:27:30 +01:00
|
|
|
#include "engines/stark/resources/sound.h"
|
2015-09-04 11:18:15 +02:00
|
|
|
|
2015-01-11 10:47:52 +01:00
|
|
|
#include "engines/stark/services/global.h"
|
2015-01-11 11:01:01 +01:00
|
|
|
#include "engines/stark/services/services.h"
|
2015-09-04 11:18:15 +02:00
|
|
|
#include "engines/stark/services/stateprovider.h"
|
2015-01-01 16:46:23 +01:00
|
|
|
|
|
|
|
namespace Stark {
|
2015-02-13 11:10:44 +01:00
|
|
|
namespace Resources {
|
2015-01-01 16:46:23 +01:00
|
|
|
|
|
|
|
AnimScript::~AnimScript() {
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:16:34 +01:00
|
|
|
AnimScript::AnimScript(Object *parent, byte subType, uint16 index, const Common::String &name) :
|
2015-12-20 21:43:04 +01:00
|
|
|
Object(parent, subType, index, name),
|
|
|
|
_anim(nullptr),
|
|
|
|
_msecsToNextUpdate(0),
|
|
|
|
_nextItemIndex(-1) {
|
2015-01-01 16:46:23 +01:00
|
|
|
_type = TYPE;
|
|
|
|
}
|
|
|
|
|
2015-01-02 10:33:42 +01:00
|
|
|
void AnimScript::onAllLoaded() {
|
2015-02-13 11:16:34 +01:00
|
|
|
Object::onAllLoaded();
|
2015-01-02 10:33:42 +01:00
|
|
|
|
2015-02-13 11:16:34 +01:00
|
|
|
_anim = Object::cast<Anim>(_parent);
|
2015-01-02 10:33:42 +01:00
|
|
|
_items = listChildren<AnimScriptItem>();
|
|
|
|
|
|
|
|
if (!_items.empty()) {
|
|
|
|
// Setup the next item to the first
|
|
|
|
_nextItemIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 10:27:32 +01:00
|
|
|
void AnimScript::onGameLoop() {
|
2015-02-13 11:16:34 +01:00
|
|
|
Object::onGameLoop();
|
2015-01-02 10:33:42 +01:00
|
|
|
|
2015-01-11 09:44:40 +01:00
|
|
|
if (!_anim || !_anim->isInUse() || _nextItemIndex == -1) {
|
2015-01-02 10:33:42 +01:00
|
|
|
// The script is disabled, do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-25 09:48:31 +01:00
|
|
|
int executedCommandCount = 0;
|
|
|
|
|
2015-07-16 20:04:00 +02:00
|
|
|
while (_msecsToNextUpdate <= (int32)StarkGlobal->getMillisecondsPerGameloop()) {
|
2015-01-02 10:33:42 +01:00
|
|
|
AnimScriptItem *item = _items[_nextItemIndex];
|
|
|
|
_msecsToNextUpdate += item->getDuration();
|
|
|
|
|
|
|
|
switch (item->getOpcode()) {
|
|
|
|
case AnimScriptItem::kDisplayFrame:
|
|
|
|
_anim->selectFrame(item->getOperand());
|
|
|
|
goToNextItem();
|
|
|
|
break;
|
2016-01-03 21:27:30 +01:00
|
|
|
case AnimScriptItem::kPlayAnimSound: {
|
|
|
|
Container *sounds = _parent->findChildWithSubtype<Container>(Container::kSounds);
|
|
|
|
Sound *sound = sounds->findChildWithOrder<Sound>(item->getOperand());
|
|
|
|
sound->play();
|
|
|
|
|
2015-01-02 10:33:42 +01:00
|
|
|
goToNextItem();
|
|
|
|
break;
|
2016-01-03 21:27:30 +01:00
|
|
|
}
|
2015-01-02 10:33:42 +01:00
|
|
|
case AnimScriptItem::kGoToItem:
|
|
|
|
_nextItemIndex = item->getOperand();
|
|
|
|
break;
|
|
|
|
case AnimScriptItem::kDisplayRandomFrame: {
|
|
|
|
uint32 startFrame = item->getOperand() >> 16;
|
|
|
|
uint32 endFrame = item->getOperand() & 0xFFFF;
|
|
|
|
|
2015-07-16 20:04:00 +02:00
|
|
|
uint32 frame = StarkRandomSource->getRandomNumberRng(startFrame, endFrame);
|
2015-01-02 10:33:42 +01:00
|
|
|
_anim->selectFrame(frame);
|
|
|
|
goToNextItem();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AnimScriptItem::kSleepRandomDuration: {
|
2015-07-16 20:04:00 +02:00
|
|
|
uint duration = StarkRandomSource->getRandomNumber(item->getOperand());
|
2015-01-02 10:33:42 +01:00
|
|
|
_msecsToNextUpdate += duration;
|
|
|
|
goToNextItem();
|
|
|
|
break;
|
|
|
|
}
|
2016-11-12 09:17:02 +01:00
|
|
|
case AnimScriptItem::kPlayStockSound: {
|
|
|
|
Location *location = StarkGlobal->getCurrent()->getLocation();
|
|
|
|
Sound *sound = location->findStockSound(item->getOperand());
|
|
|
|
if (sound) {
|
|
|
|
sound->play();
|
|
|
|
}
|
2015-01-02 10:33:42 +01:00
|
|
|
goToNextItem();
|
|
|
|
break;
|
2016-11-12 09:17:02 +01:00
|
|
|
}
|
2015-01-02 10:33:42 +01:00
|
|
|
default:
|
|
|
|
error("Unknown anim script type %d", item->getOpcode());
|
|
|
|
}
|
2019-01-25 09:48:31 +01:00
|
|
|
|
|
|
|
executedCommandCount++;
|
|
|
|
if (executedCommandCount >= 10) {
|
|
|
|
debugC(kDebugAnimation, "Potential infinite loop in anim script %s", getName().c_str());
|
|
|
|
break;
|
|
|
|
}
|
2015-01-02 10:33:42 +01:00
|
|
|
}
|
|
|
|
|
2015-07-16 20:04:00 +02:00
|
|
|
_msecsToNextUpdate -= StarkGlobal->getMillisecondsPerGameloop();
|
2015-01-02 10:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimScript::goToNextItem() {
|
|
|
|
_nextItemIndex += 1;
|
|
|
|
_nextItemIndex %= _items.size();
|
2015-01-01 16:46:23 +01:00
|
|
|
}
|
|
|
|
|
2015-09-01 14:46:45 +02:00
|
|
|
void AnimScript::goToScriptItem(AnimScriptItem *item) {
|
|
|
|
_nextItemIndex = findItemIndex(item);
|
|
|
|
_msecsToNextUpdate = 0;
|
|
|
|
|
|
|
|
if (item && item->getOpcode() == AnimScriptItem::kDisplayFrame) {
|
|
|
|
_anim->selectFrame(item->getOperand());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 AnimScript::getDurationStartingWithItem(AnimScriptItem *startItem) {
|
|
|
|
uint32 duration = 0;
|
|
|
|
uint32 itemIndex = findItemIndex(startItem);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
bool goingBackwards = false;
|
|
|
|
AnimScriptItem *item = _items[itemIndex];
|
|
|
|
|
|
|
|
switch (item->getOpcode()) {
|
|
|
|
case AnimScriptItem::kDisplayFrame:
|
|
|
|
case AnimScriptItem::kPlayAnimSound:
|
|
|
|
case AnimScriptItem::kDisplayRandomFrame:
|
|
|
|
itemIndex += 1;
|
|
|
|
itemIndex %= _items.size();
|
|
|
|
break;
|
|
|
|
case AnimScriptItem::kGoToItem:
|
|
|
|
if (item->getOperand() <= itemIndex) {
|
|
|
|
goingBackwards = true;
|
|
|
|
}
|
|
|
|
itemIndex = item->getOperand();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemIndex == 0 || goingBackwards) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
duration += item->getDuration();
|
|
|
|
}
|
|
|
|
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2015-09-24 20:21:34 +02:00
|
|
|
bool AnimScript::hasReached(AnimScriptItem *item) {
|
|
|
|
int32 index = findItemIndex(item);
|
|
|
|
return _nextItemIndex >= index;
|
|
|
|
}
|
|
|
|
|
2015-09-01 14:46:45 +02:00
|
|
|
int32 AnimScript::findItemIndex(AnimScriptItem *item) {
|
|
|
|
if (!item) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < _items.size(); i++) {
|
|
|
|
if (_items[i] == item) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-04 11:18:15 +02:00
|
|
|
void AnimScript::saveLoad(ResourceSerializer *serializer) {
|
|
|
|
serializer->syncAsSint32LE(_nextItemIndex);
|
|
|
|
|
|
|
|
if (serializer->isLoading()) {
|
|
|
|
_msecsToNextUpdate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 16:46:23 +01:00
|
|
|
AnimScriptItem::~AnimScriptItem() {
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:16:34 +01:00
|
|
|
AnimScriptItem::AnimScriptItem(Object *parent, byte subType, uint16 index, const Common::String &name) :
|
2015-12-20 21:43:04 +01:00
|
|
|
Object(parent, subType, index, name),
|
|
|
|
_opcode(0),
|
|
|
|
_duration(0),
|
|
|
|
_operand(0) {
|
2015-01-01 16:46:23 +01:00
|
|
|
_type = TYPE;
|
|
|
|
}
|
|
|
|
|
2015-02-13 12:52:32 +01:00
|
|
|
void AnimScriptItem::readData(Formats::XRCReadStream *stream) {
|
2015-01-01 16:46:23 +01:00
|
|
|
_opcode = stream->readUint32LE();
|
|
|
|
_duration = stream->readUint32LE();
|
|
|
|
_operand = stream->readUint32LE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimScriptItem::printData() {
|
|
|
|
debug("op: %d, duration: %d ms, operand: %d", _opcode, _duration, _operand);
|
|
|
|
}
|
|
|
|
|
2015-02-13 11:10:44 +01:00
|
|
|
} // End of namespace Resources
|
2015-01-01 16:46:23 +01:00
|
|
|
} // End of namespace Stark
|