2010-07-02 16:08:00 +00:00
|
|
|
|
|
|
|
#include "interpretercontext.hpp"
|
|
|
|
|
2010-07-05 10:30:45 +00:00
|
|
|
#include <cmath>
|
2010-07-02 16:08:00 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
|
2010-07-04 11:33:33 +00:00
|
|
|
#include <components/interpreter/types.hpp>
|
|
|
|
|
2010-07-03 13:41:20 +00:00
|
|
|
#include "../mwworld/world.hpp"
|
2010-07-02 16:08:00 +00:00
|
|
|
|
2010-07-20 18:23:37 +00:00
|
|
|
#include "../mwgui/window_manager.hpp"
|
2010-07-07 18:12:00 +00:00
|
|
|
|
2010-07-04 14:00:32 +00:00
|
|
|
#include "locals.hpp"
|
|
|
|
#include "globalscripts.hpp"
|
|
|
|
|
2010-07-02 16:08:00 +00:00
|
|
|
namespace MWScript
|
|
|
|
{
|
2010-07-10 11:19:04 +00:00
|
|
|
MWWorld::Ptr InterpreterContext::getReference (
|
2010-07-09 17:32:17 +00:00
|
|
|
const std::string& id, bool activeOnly)
|
|
|
|
{
|
|
|
|
if (!id.empty())
|
|
|
|
{
|
|
|
|
return mEnvironment.mWorld->getPtr (id, activeOnly);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mReference.isEmpty())
|
|
|
|
throw std::runtime_error ("no implicit reference");
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-10 11:19:04 +00:00
|
|
|
return mReference;
|
2010-08-03 20:43:53 +00:00
|
|
|
}
|
2010-07-09 17:32:17 +00:00
|
|
|
}
|
|
|
|
|
2010-07-10 11:19:04 +00:00
|
|
|
const MWWorld::Ptr InterpreterContext::getReference (
|
2010-07-09 17:32:17 +00:00
|
|
|
const std::string& id, bool activeOnly) const
|
|
|
|
{
|
|
|
|
if (!id.empty())
|
|
|
|
{
|
|
|
|
return mEnvironment.mWorld->getPtr (id, activeOnly);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mReference.isEmpty())
|
|
|
|
throw std::runtime_error ("no implicit reference");
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-10 11:19:04 +00:00
|
|
|
return mReference;
|
2010-08-03 20:43:53 +00:00
|
|
|
}
|
2010-07-09 17:32:17 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-04 14:00:32 +00:00
|
|
|
InterpreterContext::InterpreterContext (MWWorld::Environment& environment,
|
2010-07-04 08:43:34 +00:00
|
|
|
MWScript::Locals *locals, MWWorld::Ptr reference)
|
2010-08-03 20:43:53 +00:00
|
|
|
: mEnvironment (environment), mLocals (locals), mReference (reference),
|
|
|
|
mActivationHandled (false)
|
2010-07-02 16:08:00 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
int InterpreterContext::getLocalShort (int index) const
|
|
|
|
{
|
|
|
|
if (!mLocals)
|
|
|
|
throw std::runtime_error ("local variables not available in this context");
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-02 16:08:00 +00:00
|
|
|
return mLocals->mShorts.at (index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int InterpreterContext::getLocalLong (int index) const
|
|
|
|
{
|
|
|
|
if (!mLocals)
|
|
|
|
throw std::runtime_error ("local variables not available in this context");
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-02 16:08:00 +00:00
|
|
|
return mLocals->mLongs.at (index);
|
|
|
|
}
|
|
|
|
|
|
|
|
float InterpreterContext::getLocalFloat (int index) const
|
|
|
|
{
|
|
|
|
if (!mLocals)
|
|
|
|
throw std::runtime_error ("local variables not available in this context");
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-02 16:08:00 +00:00
|
|
|
return mLocals->mFloats.at (index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::setLocalShort (int index, int value)
|
|
|
|
{
|
|
|
|
if (!mLocals)
|
|
|
|
throw std::runtime_error ("local variables not available in this context");
|
|
|
|
|
|
|
|
mLocals->mShorts.at (index) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::setLocalLong (int index, int value)
|
|
|
|
{
|
|
|
|
if (!mLocals)
|
|
|
|
throw std::runtime_error ("local variables not available in this context");
|
|
|
|
|
2010-08-03 20:43:53 +00:00
|
|
|
mLocals->mLongs.at (index) = value;
|
2010-07-02 16:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::setLocalFloat (int index, float value)
|
|
|
|
{
|
|
|
|
if (!mLocals)
|
|
|
|
throw std::runtime_error ("local variables not available in this context");
|
2010-08-03 20:43:53 +00:00
|
|
|
|
|
|
|
mLocals->mFloats.at (index) = value;
|
2010-07-02 16:08:00 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-02 16:08:00 +00:00
|
|
|
void InterpreterContext::messageBox (const std::string& message,
|
|
|
|
const std::vector<std::string>& buttons)
|
|
|
|
{
|
|
|
|
std::cout << "message box: " << message << std::endl;
|
2010-08-03 20:43:53 +00:00
|
|
|
|
|
|
|
if (!buttons.empty())
|
2010-07-02 16:08:00 +00:00
|
|
|
std::cerr << "error: message box buttons not supported" << std::endl;
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-04 10:29:28 +00:00
|
|
|
bool InterpreterContext::menuMode()
|
|
|
|
{
|
2010-07-17 12:01:47 +00:00
|
|
|
return mEnvironment.mWindowManager->isGuiMode();
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-04 10:29:28 +00:00
|
|
|
int InterpreterContext::getGlobalShort (const std::string& name) const
|
|
|
|
{
|
2010-08-03 20:43:53 +00:00
|
|
|
return mEnvironment.mWorld->getGlobalVariable (name).mShort;
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int InterpreterContext::getGlobalLong (const std::string& name) const
|
|
|
|
{
|
2010-07-04 11:33:33 +00:00
|
|
|
// a global long is internally a float.
|
2010-08-03 20:43:53 +00:00
|
|
|
return mEnvironment.mWorld->getGlobalVariable (name).mLong;
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float InterpreterContext::getGlobalFloat (const std::string& name) const
|
|
|
|
{
|
2010-08-03 20:43:53 +00:00
|
|
|
return mEnvironment.mWorld->getGlobalVariable (name).mFloat;
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::setGlobalShort (const std::string& name, int value)
|
|
|
|
{
|
2010-07-18 16:29:16 +00:00
|
|
|
if (name=="gamehour")
|
|
|
|
mEnvironment.mWorld->setHour (value);
|
|
|
|
else if (name=="day")
|
|
|
|
mEnvironment.mWorld->setDay (value);
|
2010-07-22 09:48:27 +00:00
|
|
|
else if (name=="month")
|
|
|
|
mEnvironment.mWorld->setMonth (value);
|
2010-07-18 16:29:16 +00:00
|
|
|
else
|
|
|
|
mEnvironment.mWorld->getGlobalVariable (name).mShort = value;
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::setGlobalLong (const std::string& name, int value)
|
|
|
|
{
|
2010-07-18 16:29:16 +00:00
|
|
|
if (name=="gamehour")
|
|
|
|
mEnvironment.mWorld->setHour (value);
|
|
|
|
else if (name=="day")
|
|
|
|
mEnvironment.mWorld->setDay (value);
|
2010-07-22 09:48:27 +00:00
|
|
|
else if (name=="month")
|
|
|
|
mEnvironment.mWorld->setMonth (value);
|
2010-07-18 16:29:16 +00:00
|
|
|
else
|
|
|
|
mEnvironment.mWorld->getGlobalVariable (name).mLong = value;
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::setGlobalFloat (const std::string& name, float value)
|
|
|
|
{
|
2010-07-18 16:29:16 +00:00
|
|
|
if (name=="gamehour")
|
|
|
|
mEnvironment.mWorld->setHour (value);
|
|
|
|
else if (name=="day")
|
|
|
|
mEnvironment.mWorld->setDay (value);
|
2010-07-22 09:48:27 +00:00
|
|
|
else if (name=="month")
|
|
|
|
mEnvironment.mWorld->setMonth (value);
|
2010-07-18 16:29:16 +00:00
|
|
|
else
|
|
|
|
mEnvironment.mWorld->getGlobalVariable (name).mFloat = value;
|
2010-07-04 10:29:28 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-07 18:14:50 +00:00
|
|
|
bool InterpreterContext::isScriptRunning (const std::string& name) const
|
2010-07-04 14:00:32 +00:00
|
|
|
{
|
|
|
|
return mEnvironment.mGlobalScripts->isRunning (name);
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-04 14:00:32 +00:00
|
|
|
void InterpreterContext::startScript (const std::string& name)
|
|
|
|
{
|
|
|
|
mEnvironment.mGlobalScripts->addScript (name);
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-04 14:00:32 +00:00
|
|
|
void InterpreterContext::stopScript (const std::string& name)
|
|
|
|
{
|
|
|
|
mEnvironment.mGlobalScripts->removeScript (name);
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-09 18:55:39 +00:00
|
|
|
float InterpreterContext::getDistance (const std::string& name, const std::string& id) const
|
2010-07-05 10:30:45 +00:00
|
|
|
{
|
2010-07-09 18:55:39 +00:00
|
|
|
// TODO handle exterior cells (when ref and ref2 are located in different cells)
|
2010-07-10 11:19:04 +00:00
|
|
|
const MWWorld::Ptr ref2 = getReference (id, false);
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-10 11:19:04 +00:00
|
|
|
const MWWorld::Ptr ref = mEnvironment.mWorld->getPtr (name, true);
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-05 10:30:45 +00:00
|
|
|
double diff[3];
|
2010-08-03 20:43:53 +00:00
|
|
|
|
|
|
|
for (int i=0; i<3; ++i)
|
2010-07-10 11:19:04 +00:00
|
|
|
diff[i] = ref.getCellRef().pos.pos[i] - ref2.getCellRef().pos.pos[i];
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-05 10:30:45 +00:00
|
|
|
return std::sqrt (diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2]);
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
|
|
|
bool InterpreterContext::hasBeenActivated (const MWWorld::Ptr& ptr)
|
2010-07-06 08:25:42 +00:00
|
|
|
{
|
2010-08-03 20:43:53 +00:00
|
|
|
if (!mActivated.isEmpty() && mActivated==ptr)
|
|
|
|
{
|
|
|
|
mActivationHandled = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-06 08:25:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
|
|
|
bool InterpreterContext::hasActivationBeenHandled() const
|
|
|
|
{
|
|
|
|
return mActivationHandled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::activate (const MWWorld::Ptr& ptr,
|
|
|
|
boost::shared_ptr<MWWorld::Action> action)
|
|
|
|
{
|
|
|
|
mActivated = ptr;
|
|
|
|
mActivationHandled = false;
|
|
|
|
mAction = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::executeActivation()
|
|
|
|
{
|
|
|
|
if (!mAction.get())
|
|
|
|
throw std::runtime_error ("activation failed, because no action to perform");
|
|
|
|
|
|
|
|
mAction->execute (mEnvironment);
|
|
|
|
mActivationHandled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterContext::clearActivation()
|
|
|
|
{
|
|
|
|
mActivated = MWWorld::Ptr();
|
|
|
|
mActivationHandled = false;
|
|
|
|
mAction.reset();
|
|
|
|
}
|
|
|
|
|
2010-07-06 10:06:50 +00:00
|
|
|
float InterpreterContext::getSecondsPassed() const
|
|
|
|
{
|
|
|
|
return mEnvironment.mFrameDuration;
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-09 17:32:17 +00:00
|
|
|
bool InterpreterContext::isDisabled (const std::string& id) const
|
2010-07-09 14:07:03 +00:00
|
|
|
{
|
2010-07-10 11:19:04 +00:00
|
|
|
const MWWorld::Ptr ref = getReference (id, false);
|
|
|
|
return !ref.getRefData().isEnabled();
|
2010-07-09 14:07:03 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-09 17:32:17 +00:00
|
|
|
void InterpreterContext::enable (const std::string& id)
|
2010-07-09 14:07:03 +00:00
|
|
|
{
|
2010-07-10 11:19:04 +00:00
|
|
|
MWWorld::Ptr ref = getReference (id, false);
|
2010-07-09 14:07:03 +00:00
|
|
|
mEnvironment.mWorld->enable (ref);
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-09 17:32:17 +00:00
|
|
|
void InterpreterContext::disable (const std::string& id)
|
2010-07-09 14:07:03 +00:00
|
|
|
{
|
2010-08-03 20:43:53 +00:00
|
|
|
MWWorld::Ptr ref = getReference (id, false);
|
2010-07-09 14:07:03 +00:00
|
|
|
mEnvironment.mWorld->disable (ref);
|
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-17 12:01:47 +00:00
|
|
|
MWGui::WindowManager& InterpreterContext::getWindowManager()
|
2010-07-07 18:12:00 +00:00
|
|
|
{
|
2010-07-17 12:01:47 +00:00
|
|
|
return *mEnvironment.mWindowManager;
|
2010-07-07 18:12:00 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-03 13:41:20 +00:00
|
|
|
MWWorld::World& InterpreterContext::getWorld()
|
2010-07-03 10:12:13 +00:00
|
|
|
{
|
2010-07-04 08:43:34 +00:00
|
|
|
return *mEnvironment.mWorld;
|
2010-07-03 10:12:13 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-03 13:17:02 +00:00
|
|
|
MWSound::SoundManager& InterpreterContext::getSoundManager()
|
|
|
|
{
|
2010-07-04 08:43:34 +00:00
|
|
|
return *mEnvironment.mSoundManager;
|
2010-07-03 13:17:02 +00:00
|
|
|
}
|
2010-08-03 20:43:53 +00:00
|
|
|
|
2010-07-03 15:59:30 +00:00
|
|
|
MWWorld::Ptr InterpreterContext::getReference()
|
|
|
|
{
|
2010-07-26 21:09:37 +00:00
|
|
|
return getReference ("", true);
|
2010-07-03 15:59:30 +00:00
|
|
|
}
|
2010-07-02 16:08:00 +00:00
|
|
|
}
|