1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-28 17:39:42 +00:00

more dry + restrict mutation of disposition to global and on self

This commit is contained in:
Sebastian Fieber 2024-06-05 17:14:23 +02:00
parent da4531faa0
commit ed26708e4d
2 changed files with 30 additions and 20 deletions

View file

@ -50,6 +50,18 @@ namespace
throw std::runtime_error("Faction '" + std::string(faction) + "' does not exist"); throw std::runtime_error("Faction '" + std::string(faction) + "' does not exist");
return id; return id;
} }
void verifyPlayer(const MWLua::Object& o)
{
if (o.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
throw std::runtime_error("The argument must be a player!");
}
void verifyNpc(const MWWorld::Class& cls)
{
if (!cls.isNpc())
throw std::runtime_error("The argument must be a NPC!");
}
} }
namespace MWLua namespace MWLua
@ -99,38 +111,36 @@ namespace MWLua
}; };
npc["getDisposition"] = [](const Object& o, const Object& player) -> int { npc["getDisposition"] = [](const Object& o, const Object& player) -> int {
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
throw std::runtime_error("The argument must be a player!");
const MWWorld::Class& cls = o.ptr().getClass(); const MWWorld::Class& cls = o.ptr().getClass();
if (!cls.isNpc()) verifyPlayer(player);
throw std::runtime_error("NPC expected"); verifyNpc(cls);
return MWBase::Environment::get().getMechanicsManager()->getDerivedDisposition(o.ptr()); return MWBase::Environment::get().getMechanicsManager()->getDerivedDisposition(o.ptr());
}; };
npc["getBaseDisposition"] = [](const Object& o, const Object& player) -> int { npc["getBaseDisposition"] = [](const Object& o, const Object& player) -> int {
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
throw std::runtime_error("The argument must be a player!");
const MWWorld::Class& cls = o.ptr().getClass(); const MWWorld::Class& cls = o.ptr().getClass();
if (!cls.isNpc()) verifyPlayer(player);
throw std::runtime_error("NPC expected"); verifyNpc(cls);
return cls.getNpcStats(o.ptr()).getBaseDisposition(); return cls.getNpcStats(o.ptr()).getBaseDisposition();
}; };
npc["setBaseDisposition"] = [](const Object& o, const Object& player, int value) { npc["setBaseDisposition"] = [](Object& o, const Object& player, int value) {
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr()) if (dynamic_cast<LObject*>(&o) && !dynamic_cast<SelfObject*>(&o))
throw std::runtime_error("The argument must be a player!"); throw std::runtime_error("Local scripts can modify only self");
const MWWorld::Class& cls = o.ptr().getClass(); const MWWorld::Class& cls = o.ptr().getClass();
if (!cls.isNpc()) verifyPlayer(player);
throw std::runtime_error("NPC expected"); verifyNpc(cls);
cls.getNpcStats(o.ptr()).setBaseDisposition(value); cls.getNpcStats(o.ptr()).setBaseDisposition(value);
}; };
npc["modifyBaseDisposition"] = [](const Object& o, const Object& player, int value) { npc["modifyBaseDisposition"] = [](Object& o, const Object& player, int value) {
if (player.ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr()) if (dynamic_cast<LObject*>(&o) && !dynamic_cast<SelfObject*>(&o))
throw std::runtime_error("The argument must be a player!"); throw std::runtime_error("Local scripts can modify only self");
const MWWorld::Class& cls = o.ptr().getClass(); const MWWorld::Class& cls = o.ptr().getClass();
if (!cls.isNpc()) verifyPlayer(player);
throw std::runtime_error("NPC expected"); verifyNpc(cls);
auto& stats = cls.getNpcStats(o.ptr()); auto& stats = cls.getNpcStats(o.ptr());
stats.setBaseDisposition(stats.getBaseDisposition() + value); stats.setBaseDisposition(stats.getBaseDisposition() + value);
}; };

View file

@ -1011,14 +1011,14 @@
-- @return #number -- @return #number
--- ---
-- Set the base disposition of the provided NPC. -- Set the base disposition of the provided NPC (only in global scripts or on self).
-- @function [parent=#NPC] setBaseDisposition -- @function [parent=#NPC] setBaseDisposition
-- @param openmw.core#GameObject object -- @param openmw.core#GameObject object
-- @param openmw.core#GameObject player The player that you want to set the disposition for. -- @param openmw.core#GameObject player The player that you want to set the disposition for.
-- @param #number value Base disposition is set to this value -- @param #number value Base disposition is set to this value
--- ---
-- Modify the base disposition of the provided NPC by a certain amount. -- Modify the base disposition of the provided NPC by a certain amount (only in global scripts or on self).
-- @function [parent=#NPC] modifyBaseDisposition -- @function [parent=#NPC] modifyBaseDisposition
-- @param openmw.core#GameObject object -- @param openmw.core#GameObject object
-- @param openmw.core#GameObject player The player that you want to modify the disposition for. -- @param openmw.core#GameObject player The player that you want to modify the disposition for.