scummvm/engines/dragons/sequenceopcodes.cpp

243 lines
6.4 KiB
C++
Raw Normal View History

2018-12-13 11:30:30 +00:00
/* 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 3 of the License, or
* (at your option) any later version.
2018-12-13 11:30:30 +00:00
* 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, see <http://www.gnu.org/licenses/>.
2018-12-13 11:30:30 +00:00
*
*/
#include "dragons/dragons.h"
#include "dragons/sequenceopcodes.h"
#include "dragons/actor.h"
namespace Dragons {
// OpCall
void OpCall::skip(uint size) {
_code += size;
}
byte OpCall::readByte() {
return *_code++;
}
int16 OpCall::readSint16() {
int16 value = READ_LE_UINT16(_code);
_code += 2;
return value;
}
uint32 OpCall::readUint32() {
uint32 value = READ_LE_UINT32(_code);
_code += 4;
return value;
}
// SequenceOpcodes
SequenceOpcodes::SequenceOpcodes(DragonsEngine *vm)
: _vm(vm) {
initOpcodes();
}
SequenceOpcodes::~SequenceOpcodes() {
freeOpcodes();
}
void SequenceOpcodes::execOpcode(Actor *control, OpCall &opCall) {
2019-02-01 12:07:04 +00:00
assert(opCall._op < DRAGONS_NUM_SEQ_OPCODES);
2018-12-13 11:30:30 +00:00
if (!_opcodes[opCall._op])
error("SequenceOpcodes::execOpcode() Unimplemented opcode %d", opCall._op);
2019-05-27 21:58:50 +00:00
debug(4, "execSequenceOpcode(%d) %s", opCall._op, _opcodeNames[opCall._op].c_str());
2018-12-13 11:30:30 +00:00
(*_opcodes[opCall._op])(control, opCall);
}
typedef Common::Functor2Mem<Actor*, OpCall&, void, SequenceOpcodes> SequenceOpcodeI;
#define OPCODE(op, func) \
_opcodes[op] = new SequenceOpcodeI(this, &SequenceOpcodes::func); \
_opcodeNames[op] = #func;
void SequenceOpcodes::initOpcodes() {
// First clear everything
for (uint i = 0; i < DRAGONS_NUM_SEQ_OPCODES; ++i) {
_opcodes[i] = nullptr;
2018-12-13 11:30:30 +00:00
}
// Register opcodes
OPCODE(1, opSetFramePointer);
OPCODE(2, opSetFramePointerAndStop);
2019-01-06 03:19:20 +00:00
OPCODE(3, opJmp);
2020-02-19 06:47:09 +00:00
OPCODE(4, opSetSequenceTimerStartValue);
2019-01-06 03:19:20 +00:00
OPCODE(5, opSetSequenceTimer);
OPCODE(6, opUpdateXYResetSeqTimer);
OPCODE(7, opUpdateXYResetSeqTimerAndStop);
// unused
OPCODE(9, opSetActorFlag4AndStop);
2019-01-06 03:19:20 +00:00
// unused
OPCODE(11, opSetActorFlags404);
2019-01-06 03:19:20 +00:00
OPCODE(12, opClearActorFlag400);
OPCODE(13, opChangeSequence);
// unused
OPCODE(15, opSetField7a);
OPCODE(16, opUpdateFlags);
OPCODE(17, opPlaySound);
2019-01-06 03:19:20 +00:00
OPCODE(18, opSetXY);
OPCODE(19, opSetXYAndStop);
2018-12-13 11:30:30 +00:00
}
#undef OPCODE
void SequenceOpcodes::freeOpcodes() {
for (uint i = 0; i < DRAGONS_NUM_SEQ_OPCODES; ++i) {
delete _opcodes[i];
}
}
void SequenceOpcodes::updateReturn(OpCall &opCall, uint16 size) {
opCall._deltaOfs = size * 2 + 2;
}
2018-12-13 11:30:30 +00:00
// Opcodes
void SequenceOpcodes::opSetFramePointer(Actor *actor, OpCall &opCall) {
ARG_INT16(framePointer);
2019-05-27 21:58:50 +00:00
debug(4, "set frame pointer %X", framePointer);
actor->loadFrame((uint16)framePointer);
actor->_flags |= ACTOR_FLAG_2;
2020-02-19 06:47:09 +00:00
actor->_sequenceTimer = actor->_sequenceTimerMaxValue;
updateReturn(opCall, 1);
}
void SequenceOpcodes::opSetFramePointerAndStop(Actor *actor, OpCall &opCall) {
opSetFramePointer(actor, opCall);
opCall._result = 0;
2018-12-13 11:30:30 +00:00
}
2019-01-06 03:19:20 +00:00
void SequenceOpcodes::opJmp(Actor *actor, OpCall &opCall) {
ARG_INT16(newIp);
if (!(actor->_flags & ACTOR_FLAG_1000)) {
2019-01-06 03:19:20 +00:00
byte *newOffset = actor->getSeqIpAtOffset((uint32)newIp);
2019-02-01 12:07:04 +00:00
opCall._deltaOfs = (int32)(newOffset - actor->_seqCodeIp); //opCall._code);
2019-05-27 21:58:50 +00:00
debug(5, "opJump delta: %d", opCall._deltaOfs);
2019-01-06 03:19:20 +00:00
} else {
updateReturn(opCall, 1);
}
}
2020-02-19 06:47:09 +00:00
void SequenceOpcodes::opSetSequenceTimerStartValue(Actor *actor, OpCall &opCall) {
ARG_INT16(startValue);
actor->_sequenceTimerMaxValue = (uint16)startValue;
debug(5, "set sequenceTimerStartValue: %d", startValue);
updateReturn(opCall, 1);
}
2019-01-06 03:19:20 +00:00
void SequenceOpcodes::opSetSequenceTimer(Actor *actor, OpCall &opCall) {
ARG_INT16(newSeqTimer);
actor->_sequenceTimer = (uint16)newSeqTimer;
debug(5, "set _sequenceTimer: %d", newSeqTimer);
2019-01-06 03:19:20 +00:00
updateReturn(opCall, 1);
opCall._result = 0;
}
void SequenceOpcodes::opUpdateXYResetSeqTimer(Actor *actor, OpCall &opCall) {
ARG_INT8(xOffset);
ARG_INT8(yOffset);
actor->_x_pos += xOffset;
actor->_y_pos += yOffset;
2020-02-19 06:47:09 +00:00
actor->_sequenceTimer = actor->_sequenceTimerMaxValue;
2019-01-06 03:19:20 +00:00
2020-02-07 17:06:57 +00:00
debug(5, "update actor %d XY offset (%d, %d) new values (%d, %d) %d", actor->_actorID, xOffset, yOffset, actor->_x_pos, actor->_y_pos, actor->_sequenceTimer);
2019-01-06 03:19:20 +00:00
updateReturn(opCall, 1);
}
void SequenceOpcodes::opUpdateXYResetSeqTimerAndStop(Actor *actor, OpCall &opCall) {
opUpdateXYResetSeqTimer(actor, opCall);
opCall._result = 0;
}
void SequenceOpcodes::opSetActorFlag4AndStop(Actor *actor, OpCall &opCall) {
actor->_flags |= ACTOR_FLAG_4;
opCall._deltaOfs = 0;
opCall._result = 0;
//updateReturn(opCall, 1);
}
void SequenceOpcodes::opSetActorFlags404(Actor *actor, OpCall &opCall) {
2020-02-07 17:06:57 +00:00
actor->_flags |= (ACTOR_FLAG_4 | Dragons::ACTOR_FLAG_400);
updateReturn(opCall, 1);
}
2019-01-06 03:19:20 +00:00
void SequenceOpcodes::opClearActorFlag400(Actor *actor, OpCall &opCall) {
actor->_flags &= ~ACTOR_FLAG_400;
2019-01-06 03:19:20 +00:00
updateReturn(opCall, 1);
}
void SequenceOpcodes::opChangeSequence(Actor *actor, OpCall &opCall) {
ARG_INT16(newValue);
actor->_sequenceID = (uint16)newValue;
updateReturn(opCall, 1);
}
void SequenceOpcodes::opSetField7a(Actor *actor, OpCall &opCall) {
ARG_INT16(newValue);
actor->_field_7a = (uint16)newValue;
2019-01-06 03:19:20 +00:00
updateReturn(opCall, 1);
}
void SequenceOpcodes::opUpdateFlags(Actor *actor, OpCall &opCall) {
2020-02-06 21:34:58 +00:00
if (actor->isFlagSet(ACTOR_FLAG_1000)) {
2019-06-17 09:41:54 +00:00
actor->setFlag(ACTOR_FLAG_4);
2019-01-06 03:19:20 +00:00
}
updateReturn(opCall, 0);
}
void SequenceOpcodes::opPlaySound(Actor *actor, OpCall &opCall) {
ARG_INT16(soundId);
2019-05-27 21:58:50 +00:00
debug(5, "opPlaySound actorId: %d soundId: %d", actor->_actorID, soundId);
2019-01-06 03:19:20 +00:00
2019-12-11 20:23:56 +00:00
_vm->playOrStopSound((uint16) soundId);
updateReturn(opCall, 1);
}
2019-01-06 03:19:20 +00:00
void SequenceOpcodes::opSetXY(Actor *actor, OpCall &opCall) {
ARG_INT16(x);
ARG_INT16(y);
actor->_x_pos = x;
actor->_y_pos = y;
2019-01-06 03:19:20 +00:00
updateReturn(opCall, 2);
}
void SequenceOpcodes::opSetXYAndStop(Actor *actor, OpCall &opCall) {
opSetXY(actor, opCall);
opCall._result = 0;
}
2018-12-13 11:30:30 +00:00
//void SequenceOpcodes::opYield(Control *control, OpCall &opCall) {
// opCall._result = 2;
//}
//
//void SequenceOpcodes::opStopSubSequence(Control *control, OpCall &opCall) {
// ARG_INT16(linkIndex);
// control->stopSubSequence(linkIndex);
//}
} // End of namespace Dragons