Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script reference consistency #2841

Merged
merged 2 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions apps/openmw/mwscript/interpretercontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,6 @@ namespace MWScript
{
}

bool InterpreterContext::menuMode()
{
return MWBase::Environment::get().getWindowManager()->isGuiMode();
}

int InterpreterContext::getGlobalShort (const std::string& name) const
{
return MWBase::Environment::get().getWorld()->getGlobalInt (name);
Expand Down Expand Up @@ -415,58 +410,6 @@ namespace MWScript
return MWBase::Environment::get().getWorld()->getCellName();
}

bool InterpreterContext::isScriptRunning (const std::string& name) const
{
return MWBase::Environment::get().getScriptManager()->getGlobalScripts().isRunning (name);
}

void InterpreterContext::startScript (const std::string& name, const std::string& targetId)
{
MWWorld::Ptr target;
if (targetId.empty())
target = getReference(false);
else
target = getReferenceImp(targetId);
MWBase::Environment::get().getScriptManager()->getGlobalScripts().addScript (name, target);
}

void InterpreterContext::stopScript (const std::string& name)
{
MWBase::Environment::get().getScriptManager()->getGlobalScripts().removeScript (name);
}

float InterpreterContext::getDistance (const std::string& name, const std::string& id) const
{
// NOTE: id may be empty, indicating an implicit reference

MWWorld::Ptr ref2 = getReferenceImp(id);

if (ref2.getContainerStore()) // is the object contained?
{
MWWorld::Ptr container = MWBase::Environment::get().getWorld()->findContainer(ref2);

if (!container.isEmpty())
ref2 = container;
else
throw std::runtime_error("failed to find container ptr");
}

const MWWorld::Ptr ref = MWBase::Environment::get().getWorld()->getPtr(name, false);

// If the objects are in different worldspaces, return a large value (just like vanilla)
if (!ref.isInCell() || !ref2.isInCell() || ref.getCell()->getCell()->getCellId().mWorldspace != ref2.getCell()->getCell()->getCellId().mWorldspace)
return std::numeric_limits<float>::max();

double diff[3];

const float* const pos1 = ref.getRefData().getPosition().pos;
const float* const pos2 = ref2.getRefData().getPosition().pos;
for (int i=0; i<3; ++i)
diff[i] = pos1[i] - pos2[i];

return static_cast<float>(std::sqrt(diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2]));
}

void InterpreterContext::executeActivation(MWWorld::Ptr ptr, MWWorld::Ptr actor)
{
std::shared_ptr<MWWorld::Action> action = (ptr.getClass().activate(ptr, actor));
Expand All @@ -477,29 +420,6 @@ namespace MWScript
}
}

float InterpreterContext::getSecondsPassed() const
{
return MWBase::Environment::get().getFrameDuration();
}

bool InterpreterContext::isDisabled (const std::string& id) const
{
const MWWorld::Ptr ref = getReferenceImp (id, false);
return !ref.getRefData().isEnabled();
}

void InterpreterContext::enable (const std::string& id)
{
MWWorld::Ptr ref = getReferenceImp (id, false);
MWBase::Environment::get().getWorld()->enable (ref);
}

void InterpreterContext::disable (const std::string& id)
{
MWWorld::Ptr ref = getReferenceImp (id, false);
MWBase::Environment::get().getWorld()->disable (ref);
}

int InterpreterContext::getMemberShort (const std::string& id, const std::string& name,
bool global) const
{
Expand Down
19 changes: 0 additions & 19 deletions apps/openmw/mwscript/interpretercontext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ namespace MWScript
virtual void report (const std::string& message);
///< By default, do nothing.

virtual bool menuMode();

virtual int getGlobalShort (const std::string& name) const;

virtual int getGlobalLong (const std::string& name) const;
Expand Down Expand Up @@ -121,26 +119,9 @@ namespace MWScript

virtual std::string getCurrentCellName() const;

virtual bool isScriptRunning (const std::string& name) const;

virtual void startScript (const std::string& name, const std::string& targetId = "");

virtual void stopScript (const std::string& name);

virtual float getDistance (const std::string& name, const std::string& id = "") const;
///< @note if \a id is empty, assumes an implicit reference

void executeActivation(MWWorld::Ptr ptr, MWWorld::Ptr actor);
///< Execute the activation action for this ptr. If ptr is mActivated, mark activation as handled.

virtual float getSecondsPassed() const;

virtual bool isDisabled (const std::string& id = "") const;

virtual void enable (const std::string& id = "");

virtual void disable (const std::string& id = "");

virtual int getMemberShort (const std::string& id, const std::string& name, bool global) const;

virtual int getMemberLong (const std::string& id, const std::string& name, bool global) const;
Expand Down
126 changes: 126 additions & 0 deletions apps/openmw/mwscript/miscextensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <components/interpreter/runtime.hpp>
#include <components/interpreter/opcodes.hpp>

#include <components/misc/rng.hpp>

#include <components/esm/loadmgef.hpp>
#include <components/esm/loadcrea.hpp>

Expand Down Expand Up @@ -78,6 +80,117 @@ namespace MWScript
{
namespace Misc
{
class OpMenuMode : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
runtime.push (MWBase::Environment::get().getWindowManager()->isGuiMode());
}
};

class OpRandom : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
Interpreter::Type_Integer limit = runtime[0].mInteger;
runtime.pop();

if (limit<0)
throw std::runtime_error (
"random: argument out of range (Don't be so negative!)");

runtime.push (static_cast<Interpreter::Type_Float>(::Misc::Rng::rollDice(limit))); // [o, limit)
}
};

template<class R>
class OpStartScript : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
std::string name = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();
MWWorld::Ptr target = R()(runtime, false);
MWBase::Environment::get().getScriptManager()->getGlobalScripts().addScript (name, target);
}
};

class OpScriptRunning : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
std::string name = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();
runtime.push(MWBase::Environment::get().getScriptManager()->getGlobalScripts().isRunning (name));
}
};

class OpStopScript : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
std::string name = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();
MWBase::Environment::get().getScriptManager()->getGlobalScripts().removeScript (name);
}
};

class OpGetSecondsPassed : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
runtime.push (MWBase::Environment::get().getFrameDuration());
}
};

template<class R>
class OpEnable : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
MWBase::Environment::get().getWorld()->enable (ptr);
}
};

template<class R>
class OpDisable : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
MWBase::Environment::get().getWorld()->disable (ptr);
}
};

template<class R>
class OpGetDisabled : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = R()(runtime);
runtime.push (!ptr.getRefData().isEnabled());
}
};

class OpPlayBink : public Interpreter::Opcode0
{
public:
Expand Down Expand Up @@ -1456,6 +1569,19 @@ namespace MWScript

void installOpcodes (Interpreter::Interpreter& interpreter)
{
interpreter.installSegment5 (Compiler::Misc::opcodeMenuMode, new OpMenuMode);
interpreter.installSegment5 (Compiler::Misc::opcodeRandom, new OpRandom);
interpreter.installSegment5 (Compiler::Misc::opcodeScriptRunning, new OpScriptRunning);
interpreter.installSegment5 (Compiler::Misc::opcodeStartScript, new OpStartScript<ImplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeStartScriptExplicit, new OpStartScript<ExplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeStopScript, new OpStopScript);
interpreter.installSegment5 (Compiler::Misc::opcodeGetSecondsPassed, new OpGetSecondsPassed);
interpreter.installSegment5 (Compiler::Misc::opcodeEnable, new OpEnable<ImplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeEnableExplicit, new OpEnable<ExplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeDisable, new OpDisable<ImplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeDisableExplicit, new OpDisable<ExplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeGetDisabled, new OpGetDisabled<ImplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeGetDisabledExplicit, new OpGetDisabled<ExplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeXBox, new OpXBox);
interpreter.installSegment5 (Compiler::Misc::opcodeOnActivate, new OpOnActivate<ImplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeOnActivateExplicit, new OpOnActivate<ExplicitRef>);
Expand Down
45 changes: 45 additions & 0 deletions apps/openmw/mwscript/transformationextensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,49 @@ namespace MWScript
}
}

template<class R>
class OpGetDistance : public Interpreter::Opcode0
{
public:

virtual void execute (Interpreter::Runtime& runtime)
{
std::string name = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();

MWWorld::Ptr from = R()(runtime);
if (from.getContainerStore()) // is the object contained?
{
MWWorld::Ptr container = MWBase::Environment::get().getWorld()->findContainer(from);

if (!container.isEmpty())
from = container;
else
throw std::runtime_error("failed to find container ptr");
}

const MWWorld::Ptr to = MWBase::Environment::get().getWorld()->getPtr(name, false);

float distance;
// If the objects are in different worldspaces, return a large value (just like vanilla)
if (!to.isInCell() || !from.isInCell() || to.getCell()->getCell()->getCellId().mWorldspace != from.getCell()->getCell()->getCellId().mWorldspace)
distance = std::numeric_limits<float>::max();
else
{
double diff[3];

const float* const pos1 = to.getRefData().getPosition().pos;
const float* const pos2 = from.getRefData().getPosition().pos;
for (int i=0; i<3; ++i)
diff[i] = pos1[i] - pos2[i];

distance = static_cast<float>(std::sqrt(diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2]));
}

runtime.push(distance);
}
};

template<class R>
class OpSetScale : public Interpreter::Opcode0
{
Expand Down Expand Up @@ -730,6 +773,8 @@ namespace MWScript

void installOpcodes (Interpreter::Interpreter& interpreter)
{
interpreter.installSegment5(Compiler::Transformation::opcodeGetDistance, new OpGetDistance<ImplicitRef>);
interpreter.installSegment5(Compiler::Transformation::opcodeGetDistanceExplicit, new OpGetDistance<ExplicitRef>);
interpreter.installSegment5(Compiler::Transformation::opcodeSetScale,new OpSetScale<ImplicitRef>);
interpreter.installSegment5(Compiler::Transformation::opcodeSetScaleExplicit,new OpSetScale<ExplicitRef>);
interpreter.installSegment5(Compiler::Transformation::opcodeSetAngle,new OpSetAngle<ImplicitRef>);
Expand Down
2 changes: 1 addition & 1 deletion components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ add_component_dir (compiler

add_component_dir (interpreter
context controlopcodes genericopcodes installopcodes interpreter localopcodes mathopcodes
miscopcodes opcodes runtime scriptopcodes spatialopcodes types defines
miscopcodes opcodes runtime types defines
)

add_component_dir (translation
Expand Down
Loading