Skip to content

Commit d615dfc

Browse files
authored
Merge pull request #8472 from MowFord/lua_binding_cleanup
Update add/del spell table param logic
2 parents 2d36d79 + a9a2157 commit d615dfc

2 files changed

Lines changed: 29 additions & 45 deletions

File tree

src/map/lua/lua_baseentity.cpp

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10818,42 +10818,32 @@ void CLuaBaseEntity::delLearnedAbility(uint16 abilityID)
1081810818
* Notes : Optional config table accepts bools for: silentLog, saveToDB, sendUpdate.
1081910819
************************************************************************/
1082010820

10821-
void CLuaBaseEntity::addSpell(uint16 spellID, sol::object const& arg0)
10821+
void CLuaBaseEntity::addSpell(uint16 spellID, const sol::optional<sol::table>& paramTable)
1082210822
{
1082310823
if (m_PBaseEntity->objtype != TYPE_PC)
1082410824
{
1082510825
ShowWarning("Invalid entity type calling function (%s).", m_PBaseEntity->getName());
1082610826
return;
1082710827
}
1082810828

10829-
auto* PChar = static_cast<CCharEntity*>(m_PBaseEntity);
10830-
10831-
bool silentLog = false;
10832-
bool saveToDB = true;
10833-
bool sendUpdate = true;
10834-
10835-
if (arg0.get_type() == sol::type::table)
10829+
const auto coalesceParam = [](const sol::optional<sol::table>& maybeParamTable, const std::string& key, bool defaultValue) -> bool
1083610830
{
10837-
for (const auto& kv : arg0.as<sol::table>())
10831+
if (maybeParamTable.has_value())
1083810832
{
10839-
if (kv.first.get_type() == sol::type::string && kv.second.get_type() == sol::type::boolean)
10833+
const auto& paramTable = *maybeParamTable;
10834+
if (paramTable[key].valid())
1084010835
{
10841-
auto keyName = kv.first.as<std::string>();
10842-
if (keyName == "silentLog")
10843-
{
10844-
silentLog = kv.second.as<bool>();
10845-
}
10846-
else if (keyName == "saveToDB")
10847-
{
10848-
saveToDB = kv.second.as<bool>();
10849-
}
10850-
else if (keyName == "sendUpdate")
10851-
{
10852-
sendUpdate = kv.second.as<bool>();
10853-
}
10836+
return paramTable[key].get_or<bool>(defaultValue);
1085410837
}
1085510838
}
10856-
}
10839+
return defaultValue;
10840+
};
10841+
10842+
auto* PChar = static_cast<CCharEntity*>(m_PBaseEntity);
10843+
10844+
const bool silentLog = coalesceParam(paramTable, "silentLog", /*defaultValue=*/false);
10845+
const bool saveToDB = coalesceParam(paramTable, "saveToDB", /*defaultValue=*/true);
10846+
const bool sendUpdate = coalesceParam(paramTable, "sendUpdate", /*defaultValue=*/true);
1085710847

1085810848
// Add spell to player's active spell list
1085910849
if (charutils::addSpell(PChar, spellID))
@@ -10933,37 +10923,31 @@ uint32 CLuaBaseEntity::canLearnSpell(uint16 spellID)
1093310923
* Notes : Optional config table accepts bools for: saveToDB, sendUpdate.
1093410924
************************************************************************/
1093510925

10936-
void CLuaBaseEntity::delSpell(uint16 spellID, sol::object const& arg0)
10926+
void CLuaBaseEntity::delSpell(uint16 spellID, const sol::optional<sol::table>& paramTable)
1093710927
{
1093810928
if (m_PBaseEntity->objtype != TYPE_PC)
1093910929
{
1094010930
ShowWarning("Invalid entity type calling function (%s).", m_PBaseEntity->getName());
1094110931
return;
1094210932
}
1094310933

10944-
auto* PChar = static_cast<CCharEntity*>(m_PBaseEntity);
10945-
10946-
bool saveToDB = true;
10947-
bool sendUpdate = true;
10948-
10949-
if (arg0.get_type() == sol::type::table)
10934+
const auto coalesceParam = [](const sol::optional<sol::table>& maybeParamTable, const std::string& key, bool defaultValue) -> bool
1095010935
{
10951-
for (const auto& kv : arg0.as<sol::table>())
10936+
if (maybeParamTable.has_value())
1095210937
{
10953-
if (kv.first.get_type() == sol::type::string && kv.second.get_type() == sol::type::boolean)
10938+
const auto& paramTable = *maybeParamTable;
10939+
if (paramTable[key].valid())
1095410940
{
10955-
auto keyName = kv.first.as<std::string>();
10956-
if (keyName == "saveToDB")
10957-
{
10958-
saveToDB = kv.second.as<bool>();
10959-
}
10960-
else if (keyName == "sendUpdate")
10961-
{
10962-
sendUpdate = kv.second.as<bool>();
10963-
}
10941+
return paramTable[key].get_or<bool>(defaultValue);
1096410942
}
1096510943
}
10966-
}
10944+
return defaultValue;
10945+
};
10946+
10947+
auto* PChar = static_cast<CCharEntity*>(m_PBaseEntity);
10948+
10949+
const bool saveToDB = coalesceParam(paramTable, "saveToDB", /*defaultValue=*/true);
10950+
const bool sendUpdate = coalesceParam(paramTable, "sendUpdate", /*defaultValue=*/true);
1096710951

1096810952
// Remove spell from player's active spell list
1096910953
if (charutils::delSpell(PChar, spellID))

src/map/lua/lua_baseentity.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,10 @@ class CLuaBaseEntity
547547
uint32 canLearnAbility(uint16 abilityID);
548548
void delLearnedAbility(uint16 abilityID);
549549

550-
void addSpell(uint16 spellID, sol::object const& arg0);
550+
void addSpell(uint16 spellID, const sol::optional<sol::table>& paramTable);
551551
bool hasSpell(uint16 spellID);
552552
uint32 canLearnSpell(uint16 spellID);
553-
void delSpell(uint16 spellID, sol::object const& arg0);
553+
void delSpell(uint16 spellID, const sol::optional<sol::table>& paramTable);
554554

555555
void recalculateSkillsTable();
556556
void recalculateAbilitiesTable();

0 commit comments

Comments
 (0)