1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-15 22:21:00 +00:00

implement lua api for get/set item condition

This commit is contained in:
Kindi 2023-09-12 16:29:00 +08:00
parent 090da90302
commit ff16ee2d64
9 changed files with 96 additions and 10 deletions

View File

@ -62,7 +62,7 @@ add_openmw_dir (mwlua
luamanagerimp object objectlists userdataserializer luaevents engineevents objectvariant
context globalscripts localscripts playerscripts luabindings objectbindings cellbindings mwscriptbindings
camerabindings vfsbindings uibindings soundbindings inputbindings nearbybindings postprocessingbindings stats debugbindings
types/types types/door types/item types/actor types/container types/lockable types/weapon types/npc types/creature types/player types/activator types/book types/lockpick types/probe types/apparatus types/potion types/ingredient types/misc types/repair types/armor types/light types/static types/clothing types/levelledlist types/terminal
types/types types/door types/item types/actor types/container types/lockable types/weapon types/npc types/creature types/player types/activator types/book types/lockpick types/probe types/apparatus types/potion types/ingredient types/misc types/repair types/armor types/light types/static types/clothing types/levelledlist types/terminal types/itemstats
worker magicbindings factionbindings
)

View File

@ -91,6 +91,11 @@ namespace MWClass
return ptr.get<ESM::Light>()->mBase->mData.mFlags & ESM::Light::Carry;
}
bool Light::isLight(const MWWorld::ConstPtr& ptr) const
{
return true;
}
std::unique_ptr<MWWorld::Action> Light::activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const
{
if (!MWBase::Environment::get().getWindowManager()->isAllowed(MWGui::GW_Inventory))

View File

@ -38,6 +38,8 @@ namespace MWClass
bool isItem(const MWWorld::ConstPtr&) const override;
bool isLight(const MWWorld::ConstPtr&) const override;
std::unique_ptr<MWWorld::Action> activate(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) const override;
///< Generate action for activation

View File

@ -1,18 +1,31 @@
#include <sol/sol.hpp>
#include "itemstats.hpp"
#include "../../mwworld/class.hpp"
#include "types.hpp"
namespace sol
{
template <>
struct is_automagical<MWLua::ItemStat> : std::false_type
{
};
}
namespace MWLua
{
void addItemBindings(sol::table item)
void addItemBindings(sol::table item, const Context& context)
{
sol::usertype<ItemStat> ItemStats = context.mLua->sol().new_usertype<ItemStat>("ItemStat");
ItemStats[sol::meta_function::new_index] = [](const ItemStat& i, const sol::variadic_args args) {
throw std::runtime_error("Unknown itemStat property '" + args.get<std::string>() + "'");
};
ItemStats["condition"] = sol::property([](const ItemStat& i) { return i.getCondition(); },
[](const ItemStat& i, float cond) { i.setCondition(cond); });
item["getEnchantmentCharge"]
= [](const Object& object) { return object.ptr().getCellRef().getEnchantmentCharge(); };
item["setEnchantmentCharge"]
= [](const GObject& object, float charge) { object.ptr().getCellRef().setEnchantmentCharge(charge); };
item["isRestocking"]
= [](const Object& object) -> bool { return object.ptr().getRefData().getCount(false) < 0; };
item["itemStats"] = [](const sol::object& object) { return ItemStat(object); };
}
}

View File

@ -0,0 +1,33 @@
#include "itemstats.hpp"
namespace MWLua
{
ItemStat::ItemStat(const sol::object& object)
: mObject(ObjectVariant(object))
{
}
sol::optional<double> ItemStat::getCondition() const
{
MWWorld::Ptr o = mObject.ptr();
if (o.getClass().isLight(o))
return o.getClass().getRemainingUsageTime(o);
else if (o.getClass().hasItemHealth(o))
return o.getClass().getItemHealth(o);
else
return sol::nullopt;
}
void ItemStat::setCondition(float cond) const
{
if (!mObject.isGObject())
throw std::runtime_error("This property can only be set in global scripts");
MWWorld::Ptr o = mObject.ptr();
if (o.getClass().isLight(o))
return o.getClass().setRemainingUsageTime(o, cond);
else if (o.getClass().hasItemHealth(o))
o.getCellRef().setCharge(std::max(0, static_cast<int>(cond)));
else
throw std::runtime_error("'condition' property does not exist for " + std::string(o.getClass().getName(o))
+ "(" + std::string(o.getTypeDescription()) + ")");
};
}

View File

@ -0,0 +1,29 @@
#ifndef MWLUA_ITEMSTATS_H
#define MWLUA_ITEMSTATS_H
#include <sol/sol.hpp>
#include "../../mwworld/class.hpp"
#include "../objectvariant.hpp"
#include "types.hpp"
namespace MWLua
{
class ItemStat
{
public:
ItemStat(const sol::object& object);
sol::optional<double> getCondition() const;
void setCondition(float cond) const;
/*
* set,get, enchantmentCharge, soul? etc..
*/
ObjectVariant mObject;
};
}
#endif // MWLUA_ITEMSTATS_H

View File

@ -185,9 +185,11 @@ namespace MWLua
addActorBindings(
addType(ObjectTypeName::Actor, { ESM::REC_INTERNAL_PLAYER, ESM::REC_CREA, ESM::REC_NPC_ }), context);
addItemBindings(addType(ObjectTypeName::Item,
{ ESM::REC_ARMO, ESM::REC_BOOK, ESM::REC_CLOT, ESM::REC_INGR, ESM::REC_LIGH, ESM::REC_MISC, ESM::REC_ALCH,
ESM::REC_WEAP, ESM::REC_APPA, ESM::REC_LOCK, ESM::REC_PROB, ESM::REC_REPA }));
addItemBindings(
addType(ObjectTypeName::Item,
{ ESM::REC_ARMO, ESM::REC_BOOK, ESM::REC_CLOT, ESM::REC_INGR, ESM::REC_LIGH, ESM::REC_MISC,
ESM::REC_ALCH, ESM::REC_WEAP, ESM::REC_APPA, ESM::REC_LOCK, ESM::REC_PROB, ESM::REC_REPA }),
context);
addLockableBindings(
addType(ObjectTypeName::Lockable, { ESM::REC_CONT, ESM::REC_DOOR, ESM::REC_CONT4, ESM::REC_DOOR4 }));

View File

@ -47,7 +47,7 @@ namespace MWLua
void addBookBindings(sol::table book, const Context& context);
void addContainerBindings(sol::table container, const Context& context);
void addDoorBindings(sol::table door, const Context& context);
void addItemBindings(sol::table item);
void addItemBindings(sol::table item, const Context& context);
void addActorBindings(sol::table actor, const Context& context);
void addWeaponBindings(sol::table weapon, const Context& context);
void addNpcBindings(sol::table npc, const Context& context);

View File

@ -332,6 +332,8 @@ namespace MWWorld
// True if it is an item that can be picked up.
virtual bool isItem(const MWWorld::ConstPtr& ptr) const { return false; }
virtual bool isLight(const MWWorld::ConstPtr& ptr) const { return false; }
virtual bool isBipedal(const MWWorld::ConstPtr& ptr) const;
virtual bool canFly(const MWWorld::ConstPtr& ptr) const;
virtual bool canSwim(const MWWorld::ConstPtr& ptr) const;