Make API for creating potions

depth-refraction
Mitten Orvan 2 years ago committed by psi29a
parent ba1f91661f
commit 4744b1eeda

@ -159,6 +159,7 @@ Programmers
Miroslav Puda (pakanek)
MiroslavR
Mitchell Schwitzer (schwitzerm)
Mitten.O
naclander
Narmo
Nat Meo (Utopium)

@ -2,6 +2,7 @@
#include <chrono>
#include <components/esm3/loadalch.hpp>
#include <components/lua/l10n.hpp>
#include <components/lua/luastate.hpp>
@ -51,7 +52,7 @@ namespace MWLua
{
auto* lua = context.mLua;
sol::table api(lua->sol(), sol::create);
api["API_REVISION"] = 34;
api["API_REVISION"] = 35;
api["quit"] = [lua]() {
Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback();
MWBase::Environment::get().getStateManager()->requestQuit();
@ -91,12 +92,20 @@ namespace MWLua
MWWorld::CellStore* cell = MWBase::Environment::get().getWorldScene()->getCurrentCell();
MWWorld::ManualRef mref(
MWBase::Environment::get().getWorld()->getStore(), ESM::RefId::stringRefId(recordId));
MWBase::Environment::get().getWorld()->getStore(), ESM::RefId::deserializeText(recordId));
const MWWorld::Ptr& ptr = mref.getPtr();
ptr.getRefData().disable();
MWWorld::Ptr newPtr = ptr.getClass().copyToCell(ptr, *cell, count.value_or(1));
return GObject(getId(newPtr));
};
// Creates a new record in the world database.
api["createRecord"] = sol::overload([](const ESM::Potion& potion) -> const ESM::Potion* {
return MWBase::Environment::get().getWorld()->createRecord(potion);
}
// TODO: add here overloads for other records
);
return LuaUtil::makeReadOnly(api);
}

@ -17,12 +17,38 @@ namespace sol
};
}
namespace
{
// Populates a potion struct from a Lua table.
ESM::Potion tableToPotion(const sol::table& rec)
{
ESM::Potion potion;
potion.mName = rec["name"];
potion.mModel = rec["model"];
potion.mIcon = rec["icon"];
std::string_view scriptId = rec["mwscript"].get<std::string_view>();
potion.mScript = ESM::RefId::deserializeText(scriptId);
potion.mData.mWeight = rec["weight"];
potion.mData.mValue = rec["value"];
// Note: The list of effects is not yet present in openmw.types.Potion,
// so we don't map it here either.
return potion;
}
}
namespace MWLua
{
void addPotionBindings(sol::table potion, const Context& context)
{
addRecordFunctionBinding<ESM::Potion>(potion);
// Creates a new potion struct but does not store it in MWWorld::ESMStore.
// Global scripts can use world.createRecord to add the potion to the world.
// Note: This potion instance must be owned by lua, so we return it
// by value.
potion["createRecordDraft"] = tableToPotion;
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS();
sol::usertype<ESM::Potion> record = context.mLua->sol().new_usertype<ESM::Potion>("ESM3_Potion");
record[sol::meta_function::to_string]

@ -823,6 +823,13 @@
-- @param #any objectOrRecordId
-- @return #PotionRecord
---
-- Creates a @{#PotionRecord} without adding it to the world database.
-- Use @{openmw_world#(world).createRecord} to add the record to the world.
-- @function [parent=#Potion] createRecordDraft
-- @param #PotionRecord potion A Lua table with the fields of a PotionRecord.
-- @return #PotionRecord A strongly typed Potion record.
---
-- @type PotionRecord
-- @field #string id Record id

@ -73,5 +73,14 @@
-- money = world.createObject('gold_001', 50)
-- money:moveInto(types.Actor.inventory(actor))
---
-- Creates a custom record in the world database.
-- Eventually meant to support all records, but the current
-- set of supported types is limited to:
-- * @{openmw.types#PotionRecord}
-- @function [parent=#world] createRecord
-- @param #any record A record to be registered in the database. Must be one of the supported types.
-- @return #any A new record added to the database. The type is the same as the input's.
return nil

Loading…
Cancel
Save