Skip to content

Commit

Permalink
Don't modify base records from Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Assumeru committed Oct 25, 2024
1 parent a11e683 commit a8710b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
8 changes: 4 additions & 4 deletions apps/openmw/mwlua/magicbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,21 +830,21 @@ namespace MWLua
spellsT["add"] = [context](const ActorSpells& spells, const sol::object& spellOrId) {
if (spells.mActor.isLObject())
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();
if (ptr.getClass().isActor())
ptr.getClass().getCreatureStats(ptr).getSpells().add(id);
ptr.getClass().getCreatureStats(ptr).getSpells().add(spell, false);
});
};

// types.Actor.spells(o):remove(id)
spellsT["remove"] = [context](const ActorSpells& spells, const sol::object& spellOrId) {
if (spells.mActor.isLObject())
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();
if (ptr.getClass().isActor())
ptr.getClass().getCreatureStats(ptr).getSpells().remove(id);
ptr.getClass().getCreatureStats(ptr).getSpells().remove(spell, false);
});
};

Expand Down
25 changes: 16 additions & 9 deletions apps/openmw/mwmechanics/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ namespace MWMechanics
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)
Expand All @@ -76,13 +79,17 @@ namespace MWMechanics
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);
removeSpell(spell);
mSpellList->remove(spell);
remove(SpellList::getSpell(spellId), modifyBase);
}

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();
}

Expand Down
13 changes: 7 additions & 6 deletions apps/openmw/mwmechanics/spells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,25 @@ namespace MWMechanics
bool hasSpell(const ESM::RefId& 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.

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.

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
/// no spell (empty string).
/// no spell (empty id).

void clear(bool modifyBase = false);
///< Remove all spells of al types.
///< Remove all spells of all types.

void setSelectedSpell(const ESM::RefId& spellId);
///< This function does not verify, if the spell is available.

const ESM::RefId& getSelectedSpell() const;
///< May return an empty string.
///< May return an empty id.

bool hasCommonDisease() const;

Expand Down

0 comments on commit a8710b7

Please sign in to comment.