the reference for a targeted script is now determined when needed instead of at the start of the script execution

deque
Marc Zinnschlag 11 years ago
parent 35b27ea8cb
commit 27c84d6cb7

@ -77,12 +77,8 @@ namespace MWScript
{ {
MWWorld::Ptr ptr; MWWorld::Ptr ptr;
if (!iter->second.mId.empty())
ptr = MWBase::Environment::get().getWorld()->getPtr (
iter->second.mId, false);
MWScript::InterpreterContext interpreterContext ( MWScript::InterpreterContext interpreterContext (
&iter->second.mLocals, ptr); &iter->second.mLocals, MWWorld::Ptr(), iter->second.mId);
MWBase::Environment::get().getScriptManager()->run (iter->first, interpreterContext); MWBase::Environment::get().getScriptManager()->run (iter->first, interpreterContext);
} }

@ -22,7 +22,7 @@
namespace MWScript namespace MWScript
{ {
MWWorld::Ptr InterpreterContext::getReference ( MWWorld::Ptr InterpreterContext::getReferenceImp (
const std::string& id, bool activeOnly, bool doThrow) const std::string& id, bool activeOnly, bool doThrow)
{ {
if (!id.empty()) if (!id.empty())
@ -31,6 +31,10 @@ namespace MWScript
} }
else else
{ {
if (mReference.isEmpty() && !mTargetId.empty())
mReference =
MWBase::Environment::get().getWorld()->searchPtr (mTargetId, false);
if (mReference.isEmpty() && doThrow) if (mReference.isEmpty() && doThrow)
throw std::runtime_error ("no implicit reference"); throw std::runtime_error ("no implicit reference");
@ -38,7 +42,7 @@ namespace MWScript
} }
} }
const MWWorld::Ptr InterpreterContext::getReference ( const MWWorld::Ptr InterpreterContext::getReferenceImp (
const std::string& id, bool activeOnly, bool doThrow) const const std::string& id, bool activeOnly, bool doThrow) const
{ {
if (!id.empty()) if (!id.empty())
@ -47,6 +51,10 @@ namespace MWScript
} }
else else
{ {
if (mReference.isEmpty() && !mTargetId.empty())
mReference =
MWBase::Environment::get().getWorld()->searchPtr (mTargetId, false);
if (mReference.isEmpty() && doThrow) if (mReference.isEmpty() && doThrow)
throw std::runtime_error ("no implicit reference"); throw std::runtime_error ("no implicit reference");
@ -64,7 +72,7 @@ namespace MWScript
} }
else else
{ {
const MWWorld::Ptr ptr = getReference (id, false); const MWWorld::Ptr ptr = getReferenceImp (id, false);
id = ptr.getClass().getScript (ptr); id = ptr.getClass().getScript (ptr);
@ -84,7 +92,7 @@ namespace MWScript
} }
else else
{ {
const MWWorld::Ptr ptr = getReference (id, false); const MWWorld::Ptr ptr = getReferenceImp (id, false);
id = ptr.getClass().getScript (ptr); id = ptr.getClass().getScript (ptr);
@ -96,9 +104,9 @@ namespace MWScript
} }
InterpreterContext::InterpreterContext ( InterpreterContext::InterpreterContext (
MWScript::Locals *locals, MWWorld::Ptr reference) MWScript::Locals *locals, MWWorld::Ptr reference, const std::string& targetId)
: mLocals (locals), mReference (reference), : mLocals (locals), mReference (reference),
mActivationHandled (false) mActivationHandled (false), mTargetId (targetId)
{} {}
int InterpreterContext::getLocalShort (int index) const int InterpreterContext::getLocalShort (int index) const
@ -236,34 +244,34 @@ namespace MWScript
std::string InterpreterContext::getNPCName() const std::string InterpreterContext::getNPCName() const
{ {
ESM::NPC npc = *mReference.get<ESM::NPC>()->mBase; ESM::NPC npc = *getReferenceImp().get<ESM::NPC>()->mBase;
return npc.mName; return npc.mName;
} }
std::string InterpreterContext::getNPCRace() const std::string InterpreterContext::getNPCRace() const
{ {
ESM::NPC npc = *mReference.get<ESM::NPC>()->mBase; ESM::NPC npc = *getReferenceImp().get<ESM::NPC>()->mBase;
const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc.mRace); const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc.mRace);
return race->mName; return race->mName;
} }
std::string InterpreterContext::getNPCClass() const std::string InterpreterContext::getNPCClass() const
{ {
ESM::NPC npc = *mReference.get<ESM::NPC>()->mBase; ESM::NPC npc = *getReferenceImp().get<ESM::NPC>()->mBase;
const ESM::Class* class_ = MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find(npc.mClass); const ESM::Class* class_ = MWBase::Environment::get().getWorld()->getStore().get<ESM::Class>().find(npc.mClass);
return class_->mName; return class_->mName;
} }
std::string InterpreterContext::getNPCFaction() const std::string InterpreterContext::getNPCFaction() const
{ {
ESM::NPC npc = *mReference.get<ESM::NPC>()->mBase; ESM::NPC npc = *getReferenceImp().get<ESM::NPC>()->mBase;
const ESM::Faction* faction = MWBase::Environment::get().getWorld()->getStore().get<ESM::Faction>().find(npc.mFaction); const ESM::Faction* faction = MWBase::Environment::get().getWorld()->getStore().get<ESM::Faction>().find(npc.mFaction);
return faction->mName; return faction->mName;
} }
std::string InterpreterContext::getNPCRank() const std::string InterpreterContext::getNPCRank() const
{ {
std::map<std::string, int> ranks = mReference.getClass().getNpcStats (mReference).getFactionRanks(); std::map<std::string, int> ranks = getReferenceImp().getClass().getNpcStats (getReferenceImp()).getFactionRanks();
std::map<std::string, int>::const_iterator it = ranks.begin(); std::map<std::string, int>::const_iterator it = ranks.begin();
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
@ -299,7 +307,7 @@ namespace MWScript
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
MWWorld::Ptr player = world->getPlayerPtr(); MWWorld::Ptr player = world->getPlayerPtr();
std::string factionId = mReference.getClass().getNpcStats (mReference).getFactionRanks().begin()->first; std::string factionId = getReferenceImp().getClass().getNpcStats (getReferenceImp()).getFactionRanks().begin()->first;
std::map<std::string, int> ranks = player.getClass().getNpcStats (player).getFactionRanks(); std::map<std::string, int> ranks = player.getClass().getNpcStats (player).getFactionRanks();
std::map<std::string, int>::const_iterator it = ranks.find(factionId); std::map<std::string, int>::const_iterator it = ranks.find(factionId);
@ -326,7 +334,7 @@ namespace MWScript
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
MWWorld::Ptr player = world->getPlayerPtr(); MWWorld::Ptr player = world->getPlayerPtr();
std::string factionId = mReference.getClass().getNpcStats (mReference).getFactionRanks().begin()->first; std::string factionId = getReferenceImp().getClass().getNpcStats (getReferenceImp()).getFactionRanks().begin()->first;
std::map<std::string, int> ranks = player.getClass().getNpcStats (player).getFactionRanks(); std::map<std::string, int> ranks = player.getClass().getNpcStats (player).getFactionRanks();
std::map<std::string, int>::const_iterator it = ranks.find(factionId); std::map<std::string, int>::const_iterator it = ranks.find(factionId);
@ -383,7 +391,7 @@ namespace MWScript
MWWorld::Ptr ref2; MWWorld::Ptr ref2;
if (id.empty()) if (id.empty())
ref2 = getReference("", true, true); ref2 = getReferenceImp();
else else
ref2 = MWBase::Environment::get().getWorld()->searchPtr(id, true); ref2 = MWBase::Environment::get().getWorld()->searchPtr(id, true);
@ -448,19 +456,19 @@ namespace MWScript
bool InterpreterContext::isDisabled (const std::string& id) const bool InterpreterContext::isDisabled (const std::string& id) const
{ {
const MWWorld::Ptr ref = getReference (id, false); const MWWorld::Ptr ref = getReferenceImp (id, false);
return !ref.getRefData().isEnabled(); return !ref.getRefData().isEnabled();
} }
void InterpreterContext::enable (const std::string& id) void InterpreterContext::enable (const std::string& id)
{ {
MWWorld::Ptr ref = getReference (id, false); MWWorld::Ptr ref = getReferenceImp (id, false);
MWBase::Environment::get().getWorld()->enable (ref); MWBase::Environment::get().getWorld()->enable (ref);
} }
void InterpreterContext::disable (const std::string& id) void InterpreterContext::disable (const std::string& id)
{ {
MWWorld::Ptr ref = getReference (id, false); MWWorld::Ptr ref = getReferenceImp (id, false);
MWBase::Environment::get().getWorld()->disable (ref); MWBase::Environment::get().getWorld()->disable (ref);
} }
@ -542,6 +550,11 @@ namespace MWScript
MWWorld::Ptr InterpreterContext::getReference(bool required) MWWorld::Ptr InterpreterContext::getReference(bool required)
{ {
return getReference ("", true, required); return getReferenceImp ("", true, required);
}
std::string InterpreterContext::getTargetId() const
{
return mTargetId;
} }
} }

@ -27,14 +27,22 @@ namespace MWScript
class InterpreterContext : public Interpreter::Context class InterpreterContext : public Interpreter::Context
{ {
Locals *mLocals; Locals *mLocals;
MWWorld::Ptr mReference; mutable MWWorld::Ptr mReference;
MWWorld::Ptr mActivated; MWWorld::Ptr mActivated;
bool mActivationHandled; bool mActivationHandled;
MWWorld::Ptr getReference (const std::string& id, bool activeOnly, bool doThrow=true); std::string mTargetId;
const MWWorld::Ptr getReference (const std::string& id, bool activeOnly, bool doThrow=true) const; /// If \a id is empty, a reference the script is run from is returned or in case
/// of a non-local script the reference derived from the target ID.
MWWorld::Ptr getReferenceImp (const std::string& id = "", bool activeOnly = false,
bool doThrow=true);
/// If \a id is empty, a reference the script is run from is returned or in case
/// of a non-local script the reference derived from the target ID.
const MWWorld::Ptr getReferenceImp (const std::string& id = "",
bool activeOnly = false, bool doThrow=true) const;
const Locals& getMemberLocals (std::string& id, bool global) const; const Locals& getMemberLocals (std::string& id, bool global) const;
///< \a id is changed to the respective script ID, if \a id wasn't a script ID before ///< \a id is changed to the respective script ID, if \a id wasn't a script ID before
@ -44,7 +52,8 @@ namespace MWScript
public: public:
InterpreterContext (MWScript::Locals *locals, MWWorld::Ptr reference); InterpreterContext (MWScript::Locals *locals, MWWorld::Ptr reference,
const std::string& targetId = "");
///< The ownership of \a locals is not transferred. 0-pointer allowed. ///< The ownership of \a locals is not transferred. 0-pointer allowed.
virtual int getLocalShort (int index) const; virtual int getLocalShort (int index) const;
@ -158,6 +167,8 @@ namespace MWScript
MWWorld::Ptr getReference(bool required=true); MWWorld::Ptr getReference(bool required=true);
///< Reference, that the script is running from (can be empty) ///< Reference, that the script is running from (can be empty)
virtual std::string getTargetId() const;
}; };
} }

@ -108,6 +108,8 @@ namespace Interpreter
virtual void setMemberFloat (const std::string& id, const std::string& name, float value, bool global) virtual void setMemberFloat (const std::string& id, const std::string& name, float value, bool global)
= 0; = 0;
virtual std::string getTargetId() const = 0;
}; };
} }

Loading…
Cancel
Save