From f42e6f08de31107883e5272c7013533dabb7de51 Mon Sep 17 00:00:00 2001
From: image <>
Date: Wed, 23 Sep 2020 13:45:29 -0400
Subject: [PATCH] CustomSpell addition, customspell function: Create custom
 spells within themselves

Spells/Fighter/Crusader/Shadowknight/Painbringer.lua

function customspell(Spell, Caster, Target)
	SetSpellData(Spell,"name","Painbringer 2.0!")
end

sets name to Painbringer 2.0! instead of 'Painbringer'
---
 EQ2/source/WorldServer/LuaInterface.cpp |  6 +++--
 EQ2/source/WorldServer/LuaInterface.h   |  2 +-
 EQ2/source/WorldServer/SpellProcess.cpp | 36 ++++++++++++++++++++++++-
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/EQ2/source/WorldServer/LuaInterface.cpp b/EQ2/source/WorldServer/LuaInterface.cpp
index a630d076c..9c381b4f8 100644
--- a/EQ2/source/WorldServer/LuaInterface.cpp
+++ b/EQ2/source/WorldServer/LuaInterface.cpp
@@ -477,7 +477,7 @@ bool LuaInterface::LoadZoneScript(string name) {
 	return LoadZoneScript(name.c_str());
 }
 
-void LuaInterface::AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast, const char* function, SpellScriptTimer* timer) {
+void LuaInterface::AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast, const char* function, SpellScriptTimer* timer, bool passLuaSpell) {
 	if (function)
 		lua_getglobal(spell->state, function);
 	else if (precast)
@@ -487,6 +487,9 @@ void LuaInterface::AddSpawnPointers(LuaSpell* spell, bool first_cast, bool preca
 	else
 		lua_getglobal(spell->state, "tick");
 
+	if(passLuaSpell)
+		SetSpellValue(spell->state, spell);
+
 	Spawn* temp_spawn = 0;
 	if (timer && timer->caster && spell->caster)
 		temp_spawn = spell->caster->GetZone()->GetSpawnByID(timer->caster);
@@ -1717,7 +1720,6 @@ bool LuaInterface::RunItemScript(string script_name, const char* function_name,
 			return false;
 		}
 		lua_getglobal(state, function_name);
-		lua_getglobal(state, function_name);
 		if (!lua_isfunction(state, lua_gettop(state))){
 			lua_pop(state, 1);
 			mutex->releasereadlock(__FUNCTION__);
diff --git a/EQ2/source/WorldServer/LuaInterface.h b/EQ2/source/WorldServer/LuaInterface.h
index d75dfa811..5f6460aa6 100644
--- a/EQ2/source/WorldServer/LuaInterface.h
+++ b/EQ2/source/WorldServer/LuaInterface.h
@@ -222,7 +222,7 @@ public:
 	void			SetConversationValue(lua_State* state, vector<ConversationOption>* conversation);
 	void			SetOptionWindowValue(lua_State* state, vector<OptionWindowOption>* optionWindow);
 
-	void			AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast = false, const char* function = 0, SpellScriptTimer* timer = 0);
+	void			AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast = false, const char* function = 0, SpellScriptTimer* timer = 0, bool passLuaSpell=false);
 	LuaSpell*		GetCurrentSpell(lua_State* state);
 	bool			CallSpellProcess(LuaSpell* spell, int8 num_parameters);
 	LuaSpell*		GetSpell(const char* name);
diff --git a/EQ2/source/WorldServer/SpellProcess.cpp b/EQ2/source/WorldServer/SpellProcess.cpp
index 77f659b9a..bd54533ef 100644
--- a/EQ2/source/WorldServer/SpellProcess.cpp
+++ b/EQ2/source/WorldServer/SpellProcess.cpp
@@ -475,6 +475,23 @@ bool SpellProcess::CastInstant(Spell* spell, Entity* caster, Entity* target, boo
 	lua_spell->initial_target = target->GetID();
 	GetSpellTargets(lua_spell);
 
+	if (!lua_spell->spell->IsCopiedSpell())
+	{
+		lua_getglobal(lua_spell->state, "customspell");
+		if (lua_isfunction(lua_spell->state, lua_gettop(lua_spell->state))) {
+			lua_pop(lua_spell->state, 1);
+			Spell* tmpSpell = lua_spell->spell;
+			lua_spell->spell = new Spell(lua_spell->spell);
+			lua_interface->AddCustomSpell(lua_spell);
+			lua_interface->AddSpawnPointers(lua_spell, false, false, "customspell",0,true);
+			if (lua_pcall(lua_spell->state, 3, 3, 0) != 0) {
+				lua_interface->RemoveCustomSpell(lua_spell->spell->GetSpellID());
+				safe_delete(lua_spell->spell);
+				lua_spell->spell = tmpSpell;
+			}
+		}
+	}
+
 	caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
 
 	if (!remove)
@@ -787,7 +804,6 @@ void SpellProcess::ProcessSpell(ZoneServer* zone, Spell* spell, Entity* caster,
 {
 	if((customSpell != 0 || spell != 0) && caster)
 	{
-
 		Client* client = 0;
 		//int16 version = 0;
 
@@ -823,6 +839,7 @@ void SpellProcess::ProcessSpell(ZoneServer* zone, Spell* spell, Entity* caster,
 
 		lua_spell->caster = caster;
 		lua_spell->spell = spell;
+
 		int32 target_id = target->GetID();
 		lua_spell->initial_target = target_id;
 
@@ -852,6 +869,23 @@ void SpellProcess::ProcessSpell(ZoneServer* zone, Spell* spell, Entity* caster,
 			//version = client->GetVersion();
 		}
 
+		if (!customSpell)
+		{
+			lua_getglobal(lua_spell->state, "customspell");
+			if (lua_isfunction(lua_spell->state, lua_gettop(lua_spell->state))) {
+				lua_pop(lua_spell->state, 1);
+				Spell* tmpSpell = lua_spell->spell;
+				lua_spell->spell = new Spell(lua_spell->spell);
+				lua_interface->AddCustomSpell(lua_spell);
+				lua_interface->AddSpawnPointers(lua_spell, false, false, "customspell", 0, true);
+				if (lua_pcall(lua_spell->state, 3, 3, 0) != 0) {
+					lua_interface->RemoveCustomSpell(lua_spell->spell->GetSpellID());
+					safe_delete(lua_spell->spell);
+					lua_spell->spell = tmpSpell;
+				}
+			}
+		}
+
 		//If this spell is the toggle cast type and is being toggled off, do this now
 		if (spell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE)
 		{