1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-07 05:15:34 +00:00

Merge branch 'sanespells' into 'master'

Don't modify base records from Lua

Closes #8204

See merge request OpenMW/openmw!4425
This commit is contained in:
Alexei Kotov 2024-10-27 03:57:21 +00:00
commit 1e6c0f459a
3 changed files with 27 additions and 19 deletions

View file

@ -830,10 +830,10 @@ namespace MWLua
spellsT["add"] = [context](const ActorSpells& spells, const sol::object& spellOrId) { spellsT["add"] = [context](const ActorSpells& spells, const sol::object& spellOrId) {
if (spells.mActor.isLObject()) if (spells.mActor.isLObject())
throw std::runtime_error("Local scripts can modify only spells of the actor they are attached to."); throw std::runtime_error("Local scripts can modify only spells of the actor they are attached to.");
context.mLuaManager->addAction([obj = spells.mActor.object(), id = toSpellId(spellOrId)]() { context.mLuaManager->addAction([obj = spells.mActor.object(), spell = toSpell(spellOrId)]() {
const MWWorld::Ptr& ptr = obj.ptr(); const MWWorld::Ptr& ptr = obj.ptr();
if (ptr.getClass().isActor()) if (ptr.getClass().isActor())
ptr.getClass().getCreatureStats(ptr).getSpells().add(id); ptr.getClass().getCreatureStats(ptr).getSpells().add(spell, false);
}); });
}; };
@ -841,10 +841,10 @@ namespace MWLua
spellsT["remove"] = [context](const ActorSpells& spells, const sol::object& spellOrId) { spellsT["remove"] = [context](const ActorSpells& spells, const sol::object& spellOrId) {
if (spells.mActor.isLObject()) if (spells.mActor.isLObject())
throw std::runtime_error("Local scripts can modify only spells of the actor they are attached to."); throw std::runtime_error("Local scripts can modify only spells of the actor they are attached to.");
context.mLuaManager->addAction([obj = spells.mActor.object(), id = toSpellId(spellOrId)]() { context.mLuaManager->addAction([obj = spells.mActor.object(), spell = toSpell(spellOrId)]() {
const MWWorld::Ptr& ptr = obj.ptr(); const MWWorld::Ptr& ptr = obj.ptr();
if (ptr.getClass().isActor()) if (ptr.getClass().isActor())
ptr.getClass().getCreatureStats(ptr).getSpells().remove(id); ptr.getClass().getCreatureStats(ptr).getSpells().remove(spell, false);
}); });
}; };

View file

@ -60,14 +60,17 @@ namespace MWMechanics
return std::find(mSpells.begin(), mSpells.end(), spell) != mSpells.end(); return std::find(mSpells.begin(), mSpells.end(), spell) != mSpells.end();
} }
void Spells::add(const ESM::Spell* spell) void Spells::add(const ESM::Spell* spell, bool modifyBase)
{ {
mSpellList->add(spell); if (modifyBase)
mSpellList->add(spell);
else
addSpell(spell);
} }
void Spells::add(const ESM::RefId& spellId) void Spells::add(const ESM::RefId& spellId, bool modifyBase)
{ {
add(SpellList::getSpell(spellId)); add(SpellList::getSpell(spellId), modifyBase);
} }
void Spells::addSpell(const ESM::Spell* spell) void Spells::addSpell(const ESM::Spell* spell)
@ -76,13 +79,17 @@ namespace MWMechanics
mSpells.emplace_back(spell); mSpells.emplace_back(spell);
} }
void Spells::remove(const ESM::RefId& spellId) void Spells::remove(const ESM::RefId& spellId, bool modifyBase)
{ {
const auto spell = SpellList::getSpell(spellId); remove(SpellList::getSpell(spellId), modifyBase);
removeSpell(spell); }
mSpellList->remove(spell);
if (spellId == mSelectedSpell) void Spells::remove(const ESM::Spell* spell, bool modifyBase)
{
removeSpell(spell);
if (modifyBase)
mSpellList->remove(spell);
if (spell->mId == mSelectedSpell)
mSelectedSpell = ESM::RefId(); mSelectedSpell = ESM::RefId();
} }

View file

@ -74,24 +74,25 @@ namespace MWMechanics
bool hasSpell(const ESM::RefId& spell) const; bool hasSpell(const ESM::RefId& spell) const;
bool hasSpell(const ESM::Spell* spell) const; bool hasSpell(const ESM::Spell* spell) const;
void add(const ESM::RefId& spell); void add(const ESM::RefId& spell, bool modifyBase = true);
///< Adding a spell that is already listed in *this is a no-op. ///< Adding a spell that is already listed in *this is a no-op.
void add(const ESM::Spell* spell); void add(const ESM::Spell* spell, bool modifyBase = true);
///< Adding a spell that is already listed in *this is a no-op. ///< Adding a spell that is already listed in *this is a no-op.
void remove(const ESM::RefId& spell); void remove(const ESM::RefId& spell, bool modifyBase = true);
void remove(const ESM::Spell* spell, bool modifyBase = true);
///< If the spell to be removed is the selected spell, the selected spell will be changed to ///< If the spell to be removed is the selected spell, the selected spell will be changed to
/// no spell (empty string). /// no spell (empty id).
void clear(bool modifyBase = false); void clear(bool modifyBase = false);
///< Remove all spells of al types. ///< Remove all spells of all types.
void setSelectedSpell(const ESM::RefId& spellId); void setSelectedSpell(const ESM::RefId& spellId);
///< This function does not verify, if the spell is available. ///< This function does not verify, if the spell is available.
const ESM::RefId& getSelectedSpell() const; const ESM::RefId& getSelectedSpell() const;
///< May return an empty string. ///< May return an empty id.
bool hasCommonDisease() const; bool hasCommonDisease() const;