Skip to content

Commit

Permalink
PINK: rework scripting system
Browse files Browse the repository at this point in the history
  • Loading branch information
voltya authored and sev- committed Jun 28, 2018
1 parent cfc5538 commit e2ac931
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 250 deletions.
5 changes: 2 additions & 3 deletions engines/pink/objects/actors/actor.cpp
Expand Up @@ -124,9 +124,8 @@ void Actor::setAction(Action *newAction) {
}
}

void Actor::setAction(Action *newAction, bool unk) {
if (unk) {
//assert(0); // want to see this
void Actor::setAction(Action *newAction, bool loadingSave) {
if (loadingSave) {
_isActionEnded = 1;
_action = newAction;
} else {
Expand Down
4 changes: 2 additions & 2 deletions engines/pink/objects/actors/actor.h
Expand Up @@ -50,7 +50,7 @@ class Actor : public NamedObject {
virtual void init(bool paused);
bool initPalette(Director *director);

void toConsole() override ;
void toConsole() override;

bool isPlaying() { return !_isActionEnded; }
virtual void pause(bool paused);
Expand Down Expand Up @@ -84,7 +84,7 @@ class Actor : public NamedObject {

void setAction(const Common::String &name) { setAction(findAction(name)); }
void setAction(Action *newAction);
void setAction(Action *newAction, bool unk);
void setAction(Action *newAction, bool loadingSave);

void setPage(Page *page) { _page = page;}

Expand Down
4 changes: 2 additions & 2 deletions engines/pink/objects/actors/lead_actor.cpp
Expand Up @@ -127,7 +127,7 @@ void LeadActor::update() {
break;
case kPlayingSequence:
_sequencer->update();
if (!_sequencer->_context) {
if (!_sequencer->isPlaying()) {
_state = _nextState;
_nextState = kUndefined;
forceUpdateCursor();
Expand All @@ -141,7 +141,7 @@ void LeadActor::update() {
break;
case kPlayingExitSequence:
_sequencer->update();
if (!_sequencer->_context) {
if (!_sequencer->isPlaying()) {
_state = kUndefined;
_page->getGame()->changeScene();
}
Expand Down
2 changes: 1 addition & 1 deletion engines/pink/objects/handlers/handler.cpp
Expand Up @@ -87,7 +87,7 @@ void HandlerSequences::handle(Actor *actor) {
}

void HandlerStartPage::execute(Sequence *sequence) {
sequence->_unk = 1;
sequence->allowSkipping();
}

void HandlerStartPage::toConsole() {
Expand Down
8 changes: 4 additions & 4 deletions engines/pink/objects/sequences/seq_timer.cpp
Expand Up @@ -46,15 +46,15 @@ void SeqTimer::toConsole() {
}

void SeqTimer::update() {
Common::RandomSource &rnd = _sequencer->_page->getGame()->getRnd();
Page *page = _sequencer->getPage();
Common::RandomSource &rnd = page->getGame()->getRnd();
if (_updatesToMessage--)
return;

_updatesToMessage = _range ? _period + rnd.getRandomNumber(_range) : _period;

Actor *actor = _sequencer->_page->findActor(_actor);
if (actor && !_sequencer->findMainSequenceActorState(actor->getName()) &&
!_sequencer->findParralelSequenceActorState(actor->getName())) {
Actor *actor = page->findActor(_actor);
if (actor && !_sequencer->findState(_actor)) {
actor->onTimerMessage();
}
}
Expand Down
8 changes: 5 additions & 3 deletions engines/pink/objects/sequences/seq_timer.h
Expand Up @@ -32,9 +32,11 @@ class Sequencer;
class SeqTimer : public Object {
public:
SeqTimer();
virtual void deserialize(Archive &archive);
virtual void toConsole();
virtual void update();

void deserialize(Archive &archive) override;
void toConsole() override;

void update();

private:
Common::String _actor;
Expand Down
76 changes: 36 additions & 40 deletions engines/pink/objects/sequences/sequence.cpp
Expand Up @@ -34,7 +34,7 @@
namespace Pink {

Sequence::Sequence()
: _unk(0), _context(nullptr),
: _canBeSkipped(0), _context(nullptr),
_sequencer(nullptr) {}

Sequence::~Sequence() {
Expand All @@ -57,52 +57,42 @@ void Sequence::toConsole() {
}
}

void Sequence::init(int unk) {
assert(_items.size());
assert(dynamic_cast<SequenceItemLeader*>(_items[0])); // first item must always be a leader
start(unk);
}

void Sequence::start(int unk) {
if (_context->_nextItemIndex >= _items.size() ||
!_items[_context->_nextItemIndex]->execute(_context->_index, this, unk)) {
void Sequence::start(bool loadingSave) {
uint nextItemIndex = _context->getNextItemIndex();
if (nextItemIndex >= _items.size() ||
!_items[nextItemIndex]->execute(_context->getSegment(), this, loadingSave)) {
debug("Sequence %s ended", _name.c_str());
end();
return;
}

uint i;
for (i = _context->_nextItemIndex + 1; i <_items.size(); ++i){
if (_items[i]->isLeader())
uint i = nextItemIndex + 1;
while (i < _items.size()) {
if (_items[i]->isLeader()) {
break;
_items[i]->execute(_context->_index, this, unk);
}
_items[i++]->execute(_context->getSegment(), this, loadingSave);
}
_context->_nextItemIndex = i;


Common::Array<SequenceActorState> &states = _context->_states;
for (uint j = 0; j < states.size(); ++j) {
states[j].check(_context->_index, this, unk);
}
_context->_index++;
_context->execute(i, loadingSave);
}

void Sequence::update() {
if (!_context->_actor->isPlaying()) {
if (!_context->getActor()->isPlaying()) {
debug("Sequence step ended");
start(0);
}
}

void Sequence::end() {
_context->_actor = 0;
_unk = 1;
_context->setActor(nullptr);
_canBeSkipped = 1;
_sequencer->removeContext(_context);
}

void Sequence::restart() {
_context->setNextItemIndex(0);
_context->clearActionsFromActorStates();
_context->clearDefaultActions();
start(0);
}

Expand All @@ -113,23 +103,28 @@ void Sequence::skip() {
for (int i = _items.size() - 1; i >= 0; --i) {
if (_items[i]->isLeader()) {
_context->setNextItemIndex(i);
_context->clearActionsFromActorStates();
skipItemsTo(i);
_context->clearDefaultActions();
for (int j = 0; j < i; ++j) {
_items[j]->skip(this);
}
start(0);
break;
}
}
}

void Sequence::skipItemsTo(int index) {
for (int i = 0; i < index; ++i) {
_items[i]->skip(this);
}
void Sequence::skipSubSequence() {
if (_context->getNextItemIndex() < _items.size())
this->start(0);
}

void Sequence::skipSubSequence() {
if (_context->getNextItemIndex() < _context->getSequence()->getItems().size())
_context->getSequence()->start(0);
void Sequence::forceEnd() {
skip();
end();
}

void Sequence::init(bool loadingSave) {
start(loadingSave);
}

void SequenceAudio::deserialize(Archive &archive) {
Expand All @@ -145,8 +140,8 @@ void SequenceAudio::toConsole() {
}
}

void SequenceAudio::start(int unk) {
Sequence::start(unk);
void SequenceAudio::start(bool loadingSave) {
Sequence::start(loadingSave);
uint index = _context->getNextItemIndex();
if (index < _items.size()) {
SequenceItemLeaderAudio* leaderAudio = (SequenceItemLeaderAudio*) _items[index];
Expand All @@ -166,14 +161,15 @@ void SequenceAudio::update() {
start(0);
}

void SequenceAudio::init(int unk) {
void SequenceAudio::init(bool loadingSave) {
_sample = 0;
_sound.play(_sequencer->_page->getResourceStream(_soundName), Audio::Mixer::kMusicSoundType);
Sequence::init(unk);
_sound.play(_sequencer->getPage()->getResourceStream(_soundName), Audio::Mixer::kMusicSoundType);
start(loadingSave);
}

void SequenceAudio::restart() {
_sound.play(_sequencer->_page->getResourceStream(_soundName), Audio::Mixer::kMusicSoundType);
_sample = 0;
_sound.play(_sequencer->getPage()->getResourceStream(_soundName), Audio::Mixer::kMusicSoundType);
Sequence::restart();
}

Expand Down
50 changes: 27 additions & 23 deletions engines/pink/objects/sequences/sequence.h
Expand Up @@ -35,51 +35,55 @@ class SequenceContext;
class Sequence : public NamedObject {
public:
Sequence();
virtual ~Sequence();
virtual void deserialize(Archive &archive);
~Sequence();

virtual void toConsole();
void deserialize(Archive &archive) override ;
void toConsole() override;

Common::Array<SequenceItem *> &getItems() { return _items; }
public:
virtual void init(bool loadingSave);

void setContext(SequenceContext *context) { _context = context; }
virtual void init(int unk);
virtual void start(int unk);
virtual void start(bool loadingSave);
virtual void end();
virtual void restart();

void forceEnd();

virtual void update();
virtual void restart();

virtual void skipSubSequence();
virtual void skip();
void skipItemsTo(int index);

void allowSkipping() { _canBeSkipped = true; }
bool isSkippingAllowed() { return _canBeSkipped; }

public:
bool canAddContext(const Common::String name);
SequenceContext *getContext() const { return _context; }
Sequencer *getSequencer() const { return _sequencer; }
Common::Array<SequenceItem *> &getItems() { return _items; }

void setContext(SequenceContext *context) { _context = context; }

protected:
SequenceContext *_context;
Sequencer *_sequencer;
Array<SequenceItem *> _items;
int _unk;
bool _canBeSkipped;
};

class Sound;

class SequenceAudio : public Sequence {
public:
virtual void deserialize(Archive &archive);
virtual void toConsole();
void deserialize(Archive &archive) override;
void toConsole() override;

virtual void init(int unk);
virtual void start(int unk);
virtual void end();
void init(bool loadingSave) override;
void start(bool loadingSave) override;
void end() override;

virtual void update();
virtual void restart();
void update() override;
void restart() override;

virtual void skipSubSequence() {};
virtual void skip();
void skipSubSequence() override {}
void skip() override;

private:
Common::String _soundName;
Expand Down
55 changes: 39 additions & 16 deletions engines/pink/objects/sequences/sequence_context.cpp
Expand Up @@ -31,22 +31,19 @@

namespace Pink {

SequenceActorState::SequenceActorState(const Common::String &name)
:_actorName(name), _index(0) {}

void SequenceActorState::check(int index, Sequence *sequence, bool unk) {
Actor *actor = sequence->_sequencer->_page->findActor(_actorName);
debug("%s %s", _actorName.c_str(), _actionName.c_str());
if (_index != index && !_actionName.empty()) {
Action *action = actor->findAction(_actionName);
if (actor->getAction() != action)
actor->setAction(action, unk);
void SequenceActorState::execute(uint segment, Sequence *sequence, bool loadingSave) const {
Actor *actor = sequence->getSequencer()->getPage()->findActor(this->actor);
if (actor && this->segment != segment && !defaultAction.empty()) {
Action *action = actor->findAction(defaultAction);
if (action && actor->getAction() != action) {
actor->setAction(action, loadingSave);
}
}
}

SequenceContext::SequenceContext(Sequence *sequence, Sequencer *sequencer)
: _sequence(sequence), _sequencer(sequencer),
_nextItemIndex(0), _index(1), _actor(nullptr)
SequenceContext::SequenceContext(Sequence *sequence)
: _sequence(sequence), _nextItemIndex(0),
_segment(1), _actor(nullptr)
{
sequence->setContext(this);
Common::Array<SequenceItem*> &items = sequence->getItems();
Expand All @@ -55,7 +52,7 @@ SequenceContext::SequenceContext(Sequence *sequence, Sequencer *sequencer)
for (uint i = 0; i < items.size(); ++i) {
bool found = 0;
for (uint j = 0; j < _states.size(); ++j) {
if (items[i]->getActor() == _states[j].getActor()) {
if (items[i]->getActor() == _states[j].actor) {
found = 1;
break;
}
Expand All @@ -67,10 +64,36 @@ SequenceContext::SequenceContext(Sequence *sequence, Sequencer *sequencer)
}
}

void SequenceContext::clearActionsFromActorStates() {
void SequenceContext::execute(uint nextItemIndex, bool loadingSave) {
for (uint j = 0; j < _states.size(); ++j) {
_states[j].execute(_segment, _sequence, loadingSave);
}

_nextItemIndex = nextItemIndex;
_segment++;
}


void SequenceContext::clearDefaultActions() {
for (uint i = 0; i < _states.size(); ++i) {
_states[i].defaultAction.clear();
}
}

SequenceActorState *SequenceContext::findState(const Common::String &actor) {
for (uint i = 0; i < _states.size(); ++i) {
if (_states[i].actor == actor)
return &_states[i];
}
return nullptr;
}

bool SequenceContext::isConflictsWith(SequenceContext *context) {
for (uint i = 0; i < _states.size(); ++i) {
_states[i]._actionName.clear();
if (context->findState(_states[i].actor))
return true;
}
return false;
}

} // End of namespace Pink

0 comments on commit e2ac931

Please sign in to comment.