From 56e43288bf846e09e176952cb7b898a4138944e9 Mon Sep 17 00:00:00 2001 From: Emagi Date: Mon, 2 Sep 2024 19:14:44 -0400 Subject: [PATCH] function call fixes and resets for procs / call spell process --- source/WorldServer/Combat.cpp | 94 +++++++++++++++-------------- source/WorldServer/LuaInterface.cpp | 2 + 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/source/WorldServer/Combat.cpp b/source/WorldServer/Combat.cpp index dc0e522..b2d3cd4 100644 --- a/source/WorldServer/Combat.cpp +++ b/source/WorldServer/Combat.cpp @@ -1721,54 +1721,58 @@ bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) { else { lua_getglobal(state, "proc"); } - if (item_proc) { - num_args++; - lua_interface->SetItemValue(state, proc->item); - } - - lua_interface->SetSpawnValue(state, this); - lua_interface->SetSpawnValue(state, target); - lua_interface->SetInt32Value(state, type); - lua_interface->SetInt32Value(state, proc->damage_type); - - /* - Add spell data from db in case of a spell proc here... - */ - if (!item_proc) { - // Append spell data to the param list - vector* data = proc->spell->spell->GetLUAData(); - for(int32 i = 0; i < data->size(); i++) { - switch(data->at(i)->type) { - case 0:{ - lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value); - break; - } - case 1:{ - lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value); - break; - } - case 2:{ - lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value); - break; - } - case 3:{ - lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str()); - break; - } - default:{ - LogWrite(SPELL__ERROR, 0, "Spell", "Error: Unknown LUA Type '%i' in Entity::CastProc for Spell '%s'", (int)data->at(i)->type, proc->spell->spell->GetName()); - return false; - } - } + if (lua_isfunction(state, -1)) { + if (item_proc) { num_args++; + lua_interface->SetItemValue(state, proc->item); + } + + lua_interface->SetSpawnValue(state, this); + lua_interface->SetSpawnValue(state, target); + lua_interface->SetInt32Value(state, type); + lua_interface->SetInt32Value(state, proc->damage_type); + + /* + Add spell data from db in case of a spell proc here... + */ + if (!item_proc) { + // Append spell data to the param list + vector* data = proc->spell->spell->GetLUAData(); + for(int32 i = 0; i < data->size(); i++) { + switch(data->at(i)->type) { + case 0:{ + lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value); + break; + } + case 1:{ + lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value); + break; + } + case 2:{ + lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value); + break; + } + case 3:{ + lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str()); + break; + } + default:{ + LogWrite(SPELL__ERROR, 0, "Spell", "Error: Unknown LUA Type '%i' in Entity::CastProc for Spell '%s'", (int)data->at(i)->type, proc->spell->spell->GetName()); + return false; + } + } + num_args++; + } + } + + if (lua_pcall(state, num_args, 0, 0) != 0) { + LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier()); + lua_pop(state, 1); + return false; } } - - if (lua_pcall(state, num_args, 0, 0) != 0) { - LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier()); - lua_pop(state, 1); - return false; - } + + lua_interface->ResetFunctionStack(state); return true; } diff --git a/source/WorldServer/LuaInterface.cpp b/source/WorldServer/LuaInterface.cpp index a32bdd7..9e52249 100644 --- a/source/WorldServer/LuaInterface.cpp +++ b/source/WorldServer/LuaInterface.cpp @@ -674,9 +674,11 @@ bool LuaInterface::CallSpellProcess(LuaSpell* spell, int8 num_parameters, std::s if(lua_pcall(spell->state, num_parameters, 0, 0) != 0){ LogError("Error running function '%s' in %s: %s", customFunction.c_str(), spell->spell->GetName(), lua_tostring(spell->state, -1)); lua_pop(spell->state, 1); + ResetFunctionStack(spell->state); RemoveSpell(spell, false); // may be in a lock return false; } + ResetFunctionStack(spell->state); return true; }