• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

LuaSpell 0.0.1a

luzifix

Respected Member
Hey,

i recoded the TrinityCore Spell Script Hook in lua and make a small Beta from the Script.

Main.lua:

Code:
--Config START
local debugmode = false;
--Config END

-- Load SQL data in Array
local spell_script_lua = {};
local function load_scripts()
    spell_script_lua = {};
    local i = 0;
    local sql = WorldDBQuery("SELECT * FROM `spell_script_lua`;");
    if(sql) then
        repeat
            i = i + 1;
            local spell = sql:GetUInt32(0);
            local name = sql:GetString(1);
            spell_script_lua[spell] = name;
            dofile('scripts/scriptload/scripts/'..name..'.lss');
        until not sql:NextRow()
        print("LuaSpell::Loaded "..i.." Spell hooks..");
    end
end

-- Start Global Spell Function
local function func_start(plr, spell, func)
    if(debugmode) then
        print("-------------------------------------");
        print("Plr Name: ", plr:GetName());
        print("Spell Id: ", spell);
        print("Function: ", func);
    end
    
    local plrGUID = plr:GetGUID();        
    func = func.."("..plrGUID..", "..spell..");";
    func = load(func);
    func();
end

-- Capture Spell Cast Event
local function event_spellcast(event, plr, spell, skipCheck)
    local spellId = spell:GetEntry()
    if(debugmode) then
        print("-------------------------------------");
        print("Spell Id:", spellId);
        print("Skip: ", skipCheck);
        print("In Array: ", in_array(spellId, spell_script_lua));
    end
    if (in_array(spellId, spell_script_lua)) then
        func_start(plr, spellId, spell_script_lua[spellId])
    end
end

-- Capture Chat Event
local function event_chat(event, plr, msg, ...)
    if (plr:GetGMRank() > 0) then
        if(msg == "#luaspell reload") then
            load_scripts();
            send_msg(plr, "LuaSpell: Table \"spell_script_lua\" reloaded.");
            return false;
            
        elseif(msg == "#luaspell dump") then
            send_msg(plr, var_dump(spell_script_lua));
            return false;
            
        elseif(startswith(msg, "#luaspell debug")) then
            local sid = split(msg, " ")
            
            sid = tonumber(sid[3])
            if (sid ~= nil and in_array(sid, spell_script_lua)) then
                -- send_msg(plr, "LuaSpell: Spell id not found !")
                func_start(plr, sid, spell_script_lua[sid])
            else
                send_msg(plr, "LuaSpell: Spell id not found !")
            end
            return false
        end
    end
end

--Start LuaSpell
load_scripts();

--Hooks
RegisterServerHook(5, event_spellcast)
RegisterServerHook(18, event_chat)

function.lua:

Code:
--Doc: in_array("word", array);
function in_array(e,t)
    for k,_ in pairs(t) do
        if (k==e) then return true end
    end
    return false
end

--Doc: var_dump(array);
function var_dump(o)
    if type(o) == 'table' then
        local s = '{ '
        for k,v in pairs(o) do
                if type(k) ~= 'number' then k = '"'..k..'"' end
                s = s .. '['..k..'] = ' .. var_dump(v) .. ','
        end
        return s .. '} '
    else
        return tostring(o)
    end
end

--Doc: startswith("Hello World", "Hello");
function startswith(sbig, slittle)
  if type(slittle) == "table" then
    for k,v in ipairs(slittle) do
      if string.sub(sbig, 1, string.len(v)) == v then 
        return true
      end
    end
    return false
  end
  return string.sub(sbig, 1, string.len(slittle)) == slittle
end 

--Doc: split("1,2,3,4,5", ",");
function split(str, pat)
   local t = {}
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
     table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

function send_msg(plr, text)
    plr:SendBroadcastMessage(text);
end

World SQL:

Code:
-- ----------------------------
-- Table structure for `spell_script_lua`
-- ----------------------------
DROP TABLE IF EXISTS `spell_script_lua`;
CREATE TABLE `spell_script_lua` (
  `spell_id` int(11) NOT NULL,
  `ScriptName` char(64) NOT NULL,
  UNIQUE KEY `spell_id` (`spell_id`,`ScriptName`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of spell_script_lua
-- ----------------------------
INSERT INTO `spell_script_lua` VALUES ('2050', 'test_script_01');

TESTSCRIPT.lua:

Code:
--Test Script
function test_script_01(plrGUID, spellId)
    print("-------------------------------------");
    local plr = GetPlayerByGUID(plrGUID);    
    print(plr:GetName(), "cast Spell", spellId);
end

you can test it and give me your feedback.

Installation:
1. Batch the SQL in the scriptload/sql Folder in your World Database.
2. Insert a SpellId and a script name in the tabel spell_scripts_lua.
3. Create a new file .lss in the scriptload/scripts folder with the scriptname.
4. Create a function with the scriptname in the .lss file (look the exampel test_script_01.lss)
5. Go Ingame and cast the Spell.


InGame Comamnds (Need gmlevel 1):
#luaspell reload -- Reload the spell_script_lua Table
#luaspell dump -- Dump the current loaded Spell Hooks
#luaspell debug SpellId -- Debug a Spell Script


Sorry for my bad english, if you understand it and like to rewrit it for other, you can do this :p
 
Last edited by a moderator:

dsy86

Enthusiast
lua spell have any methods to get a spell target when casting? like GetSpellTarget(playerguid, spellid)?
 

Tommy

Founder
I removed the original link since it is pointless to zip up scripts when you can easily use pastebin, paste2 or the code tags. I just didn't see that a couple months ago. :p
[MENTION=550]dsy86[/MENTION]: No, only thing Spell has related to getting a 'unit' is 'GetCaster()'.
 

dsy86

Enthusiast
@Tommy, thanks.
i want to make a item upgrade system like this: right click an item, and the mouse pointer change to the hand style, then left click another item and opens a item gossip menu. in the menu the player can upgrade the target item which left clicked.
Can Eluna do this thing?
 

Rochet2

Moderator / Eluna Dev
You may be able to do it. But it's a little tricky..
there is an item hook for dummy effect.
this means that you should be able to trigger the gossip menu on the spell's dummy effect and go on from there.
it requires a correct kind of spell and each item needs the script.
 

dsy86

Enthusiast
thank you Rochet2, i still have some questions.
Q1: can you make a example how to use ITEM_EVENT_ON_DUMMY_EFFECT open a gossip menu?
Q2: which spell is the correct one?
 
Last edited:
Top