Guest User

Untitled

a guest
Oct 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.81 KB | None | 0 0
  1. /*
  2.  * Eluna Lua Engine
  3.  * TrinityCore
  4.  * Sympathy
  5.  */
  6.  
  7. // The way ScriptMgr in Trinity works is very simple;
  8. // When a event is fired it will execute every corrosponding class with that method.
  9. // That is, it has it's own call back system making the hard work done for us - we just need to hook that up from the Lua Engine to create a new instance of a class every time we create a hook.
  10. // we can't inject directly into script mgr because there's no way for us to pass a C function to it, so we have to make the ScriptMgr call the LuaEventMgr.
  11.  
  12. #ifndef __ELUNA__H
  13. #define __ELUNA__H
  14.  
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <string>
  18. #include <set>
  19. #include <hash_map>
  20. #include "Common.h"
  21. #include "Scripting/ScriptMgr.h"
  22. // includes for types..
  23. #include "Entities/Unit/Unit.h"
  24. #include "Entities/Player/Player.h"
  25. #include "Entities/Creature/Creature.h"
  26. #include "Entities/Item/Item.h"
  27.  
  28. // AI imports
  29. #include "CreatureAI.h"
  30. #include "ScriptedCreature.h"
  31.  
  32. extern "C" {
  33.     #include "../lua/core/lua.h"
  34.     #include "../lua/core/lualib.h"
  35.     #include "../lua/core/lauxlib.h"
  36. };
  37.  
  38. class LuaCreatureAI;
  39.  
  40.  
  41. struct LoadedScripts
  42. {
  43.     std::set<std::string> luaFiles;
  44. };
  45.  
  46. enum REGISTER_TYPE
  47. {
  48.     REGTYPE_PLAYER,
  49.     REGTYPE_GOSSIP,
  50.    
  51.     REGTYPE_COUNT
  52. };
  53.  
  54. enum PlayerEvents
  55. {
  56.     PLAYER_EVENT_ON_LOGIN           = 1,
  57.     PLAYER_EVENT_ON_LOGOUT          = 2,
  58.     PLAYER_EVENT_ON_CHARACTER_CREATE= 3,
  59.     PLAYER_EVENT_ON_CHARACTER_DELETE= 4,
  60.     PLAYER_EVENT_ON_CHAT            = 5,
  61.     PLAYER_EVENT_ON_KILL_PLAYER     = 6,
  62.     PLAYER_EVENT_COUNT
  63. };
  64.  
  65. enum CreatureEvents
  66. {
  67.     CREATURE_EVENT_ON_COMBAT        = 1,
  68.     CREATURE_EVENT_COUNT
  69. };
  70. enum GossipEvents
  71. {
  72.     GOSSIP_EVENT_ON_HELLO = 1,
  73.     GOSSIP_EVENT_ON_SELECT = 2,
  74.     GOSSIP_EVENT_COUNT
  75. };
  76.  
  77. template<class T>
  78. struct ElunaRegister
  79. {
  80.     const char* name;
  81.     int(*mfunc)(lua_State*, T*);
  82. };
  83.  
  84. template<typename T> ElunaRegister<T>* GetTableForType();
  85. template<typename T> const char* GetTName();
  86. class Eluna
  87. {
  88.     friend class ElunaScript;
  89.     friend class ScriptMgr;
  90.     static Eluna* LuaEngine;
  91.     static ElunaScript* Script;
  92.  
  93. public:
  94.     static Eluna* get() { return LuaEngine; }
  95.     static ElunaScript* getScript() { return Script; }
  96.     // Maps to hold the binds
  97.     typedef std::map<int, std::vector<uint16>> ElunaBindingMap; // Stores general-purpose hooks
  98.     ElunaBindingMap m_playerEventBinds;
  99.     ElunaBindingMap m_gossipEventBinds;
  100.  
  101.     static void initialize_tables()
  102.     {
  103.         // PlayerEvents
  104.         for(int i = 0; i < PLAYER_EVENT_COUNT; i++) {
  105.             std::vector<uint16> vector;
  106.             get()->m_playerEventBinds.insert(std::pair<int, std::vector<uint16>>(i, vector));
  107.         }
  108.  
  109.         // GossipEvents
  110.         for(int i = 0; i < GOSSIP_EVENT_COUNT; i++) {
  111.             std::vector<uint16> vector;
  112.             get()->m_gossipEventBinds.insert(std::pair<int,std::vector<uint16>>(i, vector));
  113.         }
  114.     }
  115.  
  116.     static void restart()
  117.     {
  118.         // unref everything
  119.         for(ElunaBindingMap::iterator itr = get()->m_playerEventBinds.begin(); itr != get()->m_playerEventBinds.end();
  120.             itr++)
  121.         {
  122.             for(std::vector<uint16>::iterator _itr = itr->second.begin(); _itr != itr->second.end(); _itr++)
  123.             {
  124.                 lua_unref(get()->m_luaState, (*_itr));
  125.             }
  126.             itr->second.clear(); // clear the vector
  127.         }
  128.         for(ElunaBindingMap::iterator itr = get()->m_gossipEventBinds.begin(); itr != get()->m_gossipEventBinds.end();
  129.             itr++)
  130.         {
  131.             for(std::vector<uint16>::iterator _itr = itr->second.begin(); _itr != itr->second.end(); _itr++)
  132.             {
  133.                 lua_unref(get()->m_luaState, (*_itr));
  134.             }
  135.             itr->second.clear(); // clear the vector
  136.         }
  137.  
  138.         // close state
  139.         lua_close(get()->m_luaState);
  140.  
  141.         // restart
  142.         get()->Start();
  143.     }
  144.  
  145.     ~Eluna()
  146.     {
  147.         m_playerEventBinds.clear();
  148.         delete Script;
  149.     }
  150.  
  151.  
  152.     lua_State* m_luaState;
  153.     void Start();
  154.     void Register(uint8 regtype, uint32 id, uint32 evt, uint16 functionRef);
  155.     void Unload();
  156.  
  157.     static void report(lua_State* /*L*/);
  158.  
  159.     void BeginCall(uint16 fReference)
  160.     {
  161.         lua_settop(m_luaState, 0); //stack should be empty
  162.         lua_getref(m_luaState, fReference);
  163.     }
  164.     bool ExecuteCall(uint8 params, uint8 res)
  165.     {
  166.         bool ret = true;
  167.         int top = lua_gettop(m_luaState);
  168.         if(strcmp(luaL_typename(m_luaState,top-params), "function") )
  169.         {
  170.             ret = false;
  171.             if(params > 0)
  172.             {
  173.                 for(int i = top; i >= (top-params); i--)
  174.                 {
  175.                     if(!lua_isnone(m_luaState, i) )
  176.                         lua_remove(m_luaState, i);
  177.                 }
  178.             }
  179.         }
  180.         else
  181.         {
  182.             if(lua_pcall(m_luaState,params,res,0) )
  183.             {
  184.                 report(m_luaState);
  185.                 ret = false;
  186.             }
  187.         }
  188.         return ret;
  189.     }
  190.     void EndCall(uint8 res)
  191.     {
  192.         for(int i = res; i > 0; i--)
  193.         {
  194.             if(!lua_isnone(m_luaState,res))
  195.                 lua_remove(m_luaState,res);
  196.         }
  197.     }
  198.    
  199.     void registerGlobalVariables(lua_State* L);
  200.    
  201.     void PushPlayer(lua_State*L, Player* pPlayer);
  202.     void PushCreature(lua_State* L, Creature*);
  203.     void PushLong(lua_State*L, uint64);
  204.     void PushInteger(lua_State*, uint32);
  205.     void PushString(lua_State*, const char*);
  206.     void PushGroup(lua_State*, Group*);
  207.     void PushGuild(lua_State*, Guild*);
  208.     void PushUnit(lua_State*, Unit*);
  209.  
  210.     void LoadEngine();
  211.  
  212.     void LoadDirectory(char* Dirname, LoadedScripts* lscr);
  213.     static void initialize();
  214.  
  215.     Player* CHECK_PLAYER(lua_State* L, int narg)
  216.     {
  217.         if(L == NULL) return ElunaTemplate<Player>::check(m_luaState, narg);
  218.         else return ElunaTemplate<Player>::check(L, narg);
  219.     }
  220.  
  221.     Group* CHECK_GROUP(lua_State* L, int narg)
  222.     {
  223.         if(L == NULL) return ElunaTemplate<Group>::check(m_luaState, narg);
  224.         else return ElunaTemplate<Group>::check(L, narg);
  225.     }
  226.  
  227.     Guild* CHECK_GUILD(lua_State* L, int narg)
  228.     {
  229.         if(L == NULL) return ElunaTemplate<Guild>::check(m_luaState, narg);
  230.         else return ElunaTemplate<Guild>::check(L, narg);
  231.     }
  232.     Creature* CHECK_CREATURE(lua_State* L, int narg)
  233.     {
  234.         if(L == NULL) return ElunaTemplate<Creature>::check(m_luaState, narg);
  235.         else return ElunaTemplate<Creature>::check(L, narg);
  236.     }
  237.  
  238. protected:
  239.     template<typename T>
  240.     class ElunaTemplate
  241.     {
  242.     public:
  243.         typedef int (*m_funcptr)(lua_State* L, T* ptr);
  244.         typedef struct { const char* name; m_funcptr mfunc; } ElunaRegister;
  245.         static void Register(lua_State* L)
  246.         {
  247.             lua_newtable(L);
  248.             int methods = lua_gettop(L);
  249.  
  250.             luaL_newmetatable(L, GetTName<T>());
  251.             int metatable = lua_gettop(L);
  252.  
  253.             // store method table in globals so that
  254.             // scripts can add functions in Lua
  255.             lua_pushvalue(L, methods);
  256.             lua_setglobal(L, GetTName<T>());
  257.  
  258.             // hide metatable
  259.             lua_pushvalue(L, methods);
  260.             lua_setfield(L, metatable, "__metatable");
  261.  
  262.             lua_pushvalue(L, methods);
  263.             lua_setfield(L, metatable, "__index");
  264.  
  265.             lua_pushcfunction(L, tostringT);
  266.             lua_setfield(L, metatable, "__tostring");
  267.  
  268.             lua_pushcfunction(L, gcT);
  269.             lua_setfield(L, metatable, "__gc");
  270.  
  271.             lua_newtable(L);
  272.             lua_setmetatable(L, methods);
  273.            
  274.  
  275.             // fill method table.
  276.             if(GetMethodTable<T>() == NULL)
  277.             {
  278.                 lua_pop(L, 2);
  279.                 return;
  280.             }
  281.  
  282.             for (ElunaRegister* l = ((ElunaRegister*)GetMethodTable<T>()); l->name; l++)
  283.             {
  284.                 lua_pushstring(L, l->name);
  285.                 lua_pushlightuserdata(L, (void*)l);
  286.                 lua_pushcclosure(L, thunk, 1);
  287.                 lua_settable(L, methods);
  288.             }
  289.  
  290.             lua_pop(L, 2);
  291.         }
  292.  
  293.         static int push(lua_State* L, T* obj, bool gc = false)
  294.         {
  295.             if (!obj)
  296.             {
  297.                 lua_pushnil(L);
  298.                 return lua_gettop(L);
  299.             }
  300.             luaL_getmetatable(L, GetTName<T>());
  301.             if(lua_isnil(L, -1))
  302.                 luaL_error(L, "%s missing metatable", GetTName<T>());
  303.             int idxMt = lua_gettop(L);
  304.             T** ptrHold = (T**)lua_newuserdata(L, sizeof(T**));
  305.             int ud = lua_gettop(L);
  306.             if(ptrHold != NULL)
  307.             {
  308.                 *ptrHold = obj;
  309.                 lua_pushvalue(L, idxMt);
  310.                 lua_setmetatable(L, -2);
  311.                 char name[32];
  312.                 tostring(name, obj);
  313.                 lua_getfield(L, LUA_REGISTRYINDEX, "DO NOT TRASH");
  314.                 if(lua_isnil(L, -1))
  315.                 {
  316.                     luaL_newmetatable(L, "DO NOT TRASH");
  317.                     lua_pop(L, 1);
  318.                 }
  319.                 lua_getfield(L, LUA_REGISTRYINDEX, "DO NOT TRASH");
  320.                 if(gc == false)
  321.                 {
  322.                     lua_pushboolean(L, 1);
  323.                     lua_setfield(L, -2, name);
  324.                 }
  325.                 lua_pop(L, 1);
  326.             }
  327.             lua_settop(L, ud);
  328.             lua_replace(L, idxMt);
  329.             lua_settop(L, idxMt);
  330.             return idxMt;
  331.         }
  332.  
  333.         static T* check(lua_State* L, int narg)
  334.         {
  335.             T** ptrHold = static_cast<T**>(lua_touserdata(L, narg));
  336.             if(ptrHold == NULL)
  337.                 return NULL;
  338.             return *ptrHold;
  339.         }
  340.  
  341.     private:
  342.         static int thunk(lua_State* L)
  343.         {
  344.            
  345.             T* obj = check(L, 1); // get self
  346.             lua_remove(L, 1); // remove self
  347.             ElunaRegister* l = static_cast<ElunaRegister*>(lua_touserdata(L, lua_upvalueindex(1)));
  348.             return l->mfunc(L, obj);
  349.         }
  350.         static int gcT(lua_State* L)
  351.         {
  352.             T* obj = check(L, 1);
  353.             if(obj == NULL) return 0;
  354.             lua_getfield(L, LUA_REGISTRYINDEX, "DO NO TRASH");
  355.             if(lua_istable(L, -1))
  356.             {
  357.                 char name[32];
  358.                 tostring(name, obj);
  359.                 lua_getfield(L, -1, std::string(name).c_str());
  360.                 if(lua_isnil(L, -1))
  361.                 {
  362.                     delete obj;
  363.                     obj = NULL;
  364.                 }
  365.             }
  366.             return 1;
  367.         }
  368.         static int tostringT(lua_State* L)
  369.         {
  370.             char buff[32];
  371.             T** ptrHold = (T**)lua_touserdata(L, 1);
  372.             T* obj = *ptrHold;
  373.             sprintf(buff, "%p", obj);
  374.             lua_pushfstring(L, "%s (%s)", GetTName<T>(), buff);
  375.             return 1;
  376.         }
  377.         inline static void tostring(char* buff, void* obj)
  378.         {
  379.             sprintf(buff, "%p", obj);
  380.         }
  381.         static int index(lua_State* L)
  382.         {
  383.             lua_getglobal(L, GetTName<T>());
  384.             const char* key = lua_tostring(L, 2);
  385.             if(lua_istable(L, - 1))
  386.             {
  387.                 lua_pushvalue(L, 2);
  388.                 lua_rawget(L, -2);
  389.                 if(lua_isnil(L, -1))
  390.                 {
  391.                     lua_getmetatable(L, -2);
  392.                     if(lua_istable(L, -1))
  393.                     {
  394.                         lua_getfield(L, -1, "__index");
  395.                         if(lua_isfunction(L, -1))
  396.                         {
  397.                             lua_pushvalue(L, 1);
  398.                             lua_pushvalue(L, 2);
  399.                             lua_pcall(L, 2, 1, 0);
  400.                         }
  401.                         else if(lua_istable(L, -1))
  402.                             lua_getfield(L, -1, key);
  403.                         else
  404.                             lua_pushnil(L);
  405.                     }
  406.                     else
  407.                         lua_pushnil(L);
  408.                 }
  409.                 else if(lua_istable(L, -1))
  410.                 {
  411.                     lua_pushvalue(L, 2);
  412.                     lua_rawget(L, -2);
  413.                 }
  414.             }
  415.             else
  416.                 lua_pushnil(L);
  417.             lua_insert(L, 1);
  418.             lua_settop(L, 1);
  419.             return 1;
  420.         }
  421.     };
  422.  
  423.  
  424. };
  425.  
  426.  
  427. class GUID_MGR
  428. {
  429.     static const char * GetName() { return "WoWGUID"; }
  430. public:
  431.     static void Register(lua_State * L) {
  432.         luaL_newmetatable(L,GetName());
  433.         int mt = lua_gettop(L);
  434.         //Hide metatable.
  435.         lua_pushnil(L);
  436.         lua_setfield(L,mt,"__metatable");
  437.         //nil gc method
  438.         lua_pushnil(L);
  439.         lua_setfield(L,mt,"__gc");
  440.         //set our tostring method
  441.         lua_pushcfunction(L,_tostring);
  442.         lua_setfield(L,mt,"__tostring");
  443.         //nil __index field
  444.         lua_pushnil(L);
  445.         lua_setfield(L,mt,"__index");
  446.         //set __newindex method
  447.         lua_pushcfunction(L,_newindex);
  448.         lua_setfield(L,mt,"__newindex");
  449.         //no call method
  450.         lua_pushnil(L);
  451.         lua_setfield(L,mt,"__call");
  452.         //pop metatable
  453.         lua_pop(L,1);
  454.     }
  455.     static uint64 check(lua_State * L, int narg)
  456.     {
  457.         uint64 GUID = 0;
  458.         uint64 * ptrHold = (uint64*)lua_touserdata(L,narg);
  459.         if(ptrHold != NULL)
  460.             GUID = *ptrHold;
  461.         return GUID;
  462.     }
  463.     static int push(lua_State *L, uint64 guid)
  464.     {
  465.         int index = 0;
  466.         if(guid == 0)
  467.         {
  468.             lua_pushnil(L);
  469.             index = lua_gettop(L);
  470.         }
  471.         else
  472.         {
  473.             luaL_getmetatable(L,GetName());
  474.             if(lua_isnoneornil(L,-1) )
  475.                 luaL_error(L,"%s metatable not found!. \n",GetName());
  476.             else
  477.             {
  478.                 int mt = lua_gettop(L);
  479.                 uint64* guidHold = (uint64*)lua_newuserdata(L,sizeof(uint64));
  480.                 int ud = lua_gettop(L);
  481.                 if(guidHold == NULL)
  482.                     luaL_error(L,"Lua tried to allocate size %d of memory and failed! \n",sizeof(uint64*));
  483.                 else
  484.                 {
  485.                     (*guidHold) = guid;
  486.                     lua_pushvalue(L,mt);
  487.                     lua_setmetatable(L,ud);
  488.                     lua_replace(L,mt);
  489.                     lua_settop(L,mt);
  490.                     index = mt;
  491.                 }
  492.             }
  493.         }
  494.         return index;
  495.     }
  496. private:
  497.     GUID_MGR() {}
  498.     //This method prints formats the GUID in hexform and pushes to the stack.
  499.     static int _tostring(lua_State * L)
  500.     {
  501.         uint64 GUID = GUID_MGR::check(L,1);
  502.         if(GUID == 0)
  503.             lua_pushnil(L);
  504.         else {
  505.             char buff[32];
  506.             sprintf(buff,"%X",GUID);
  507.             lua_pushfstring(L,"%s",buff);
  508.         }
  509.         return 1;
  510.     }
  511.     static int _newindex(lua_State *L)
  512.     {
  513.         //Remove table, key, and value
  514.         lua_remove(L,1);
  515.         lua_remove(L,1);
  516.         lua_remove(L,1);
  517.         luaL_error(L,"OPERATION PROHIBITED!\n");
  518.         return 0;
  519.     }
  520. };
  521.  
  522. class ElunaScript : public ScriptObject
  523. {
  524. public:
  525.     static ElunaScript* get() {
  526.         return Eluna::Script;
  527.     }
  528.  
  529.     ElunaScript(char const* name) : ScriptObject(name)
  530.     {
  531.         ScriptMgr::ScriptRegistry<ElunaScript>::_scriptIdCounter = 0;
  532.         ScriptMgr::ScriptRegistry<ElunaScript>::AddScript(this);
  533.     }
  534.  
  535.     bool IsDatabaseBound() const { return false; }
  536.  
  537.     ~ElunaScript()
  538.     {
  539.         delete this;
  540.     }
  541.  
  542.     /// PLAYER EVENTS
  543.     void OnPlayerLogin(Player* pPlayer)
  544.     {
  545.         std::vector<uint16>::iterator itr;
  546.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_LOGIN).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_LOGIN).end();
  547.             itr++)
  548.         {
  549.             Eluna::get()->BeginCall((*itr));
  550.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  551.             Eluna::get()->ExecuteCall(1, 0);
  552.         }
  553.     }
  554.     void OnPlayerLogout(Player* pPlayer)
  555.     {
  556.         std::vector<uint16>::iterator itr;
  557.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_LOGOUT).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_LOGOUT).end();
  558.             itr++)
  559.         {
  560.             Eluna::get()->BeginCall((*itr));
  561.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  562.             Eluna::get()->ExecuteCall(1, 0);
  563.         }
  564.     }
  565.     void OnCharacterCreate(Player* pPlayer)
  566.     {
  567.         std::vector<uint16>::iterator itr;
  568.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHARACTER_CREATE).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHARACTER_CREATE).end();
  569.             itr++)
  570.         {
  571.             Eluna::get()->BeginCall((*itr));
  572.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  573.             Eluna::get()->ExecuteCall(1, 0);
  574.         }
  575.     }
  576.     void OnCharacterDeleted(uint64 guid)
  577.     {
  578.         std::vector<uint16>::iterator itr;
  579.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHARACTER_DELETE).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHARACTER_DELETE).end();
  580.             itr++)
  581.         {
  582.             Eluna::get()->BeginCall((*itr));
  583.             GUID_MGR::push(Eluna::get()->m_luaState, guid);
  584.             Eluna::get()->ExecuteCall(1, 0);
  585.         }
  586.     }
  587.     void OnChat(Player* pPlayer, uint32 type, uint32 language, std::string& msg)
  588.     {
  589.         std::vector<uint16>::iterator itr;
  590.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).end();
  591.             itr++)
  592.         {
  593.             Eluna::get()->BeginCall((*itr));
  594.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  595.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, type);
  596.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, language);
  597.             Eluna::get()->PushString(Eluna::get()->m_luaState, msg.c_str());
  598.             Eluna::get()->ExecuteCall(4, 0);
  599.         }
  600.     }
  601.     void OnChat(Player* pPlayer, uint32 type, uint32 language, std::string& msg, Player* reciever)
  602.     {
  603.         std::vector<uint16>::iterator itr;
  604.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).end();
  605.             itr++)
  606.         {
  607.             Eluna::get()->BeginCall((*itr));
  608.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  609.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, type);
  610.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, language);
  611.             Eluna::get()->PushString(Eluna::get()->m_luaState, msg.c_str());
  612.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, reciever);
  613.             Eluna::get()->ExecuteCall(5, 0);
  614.         }
  615.     }
  616.     void OnChat(Player* pPlayer, uint32 type, uint32 language, std::string& msg, Group* pGroup)
  617.     {
  618.         std::vector<uint16>::iterator itr;
  619.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).end();
  620.             itr++)
  621.         {
  622.             Eluna::get()->BeginCall((*itr));
  623.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  624.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, type);
  625.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, language);
  626.             Eluna::get()->PushString(Eluna::get()->m_luaState, msg.c_str());
  627.             Eluna::get()->PushGroup(Eluna::get()->m_luaState, pGroup);
  628.             Eluna::get()->ExecuteCall(5, 0);
  629.         }
  630.     }
  631.     void OnChat(Player* pPlayer, uint32 type, uint32 language, std::string& msg, Guild* pGuild)
  632.     {
  633.         std::vector<uint16>::iterator itr;
  634.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_CHAT).end();
  635.             itr++)
  636.         {
  637.             Eluna::get()->BeginCall((*itr));
  638.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  639.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, type);
  640.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, language);
  641.             Eluna::get()->PushString(Eluna::get()->m_luaState, msg.c_str());
  642.             Eluna::get()->PushGuild(Eluna::get()->m_luaState, pGuild);
  643.             Eluna::get()->ExecuteCall(5, 0);
  644.         }
  645.     }
  646.     void OnPvPKill(Player* pKiller, Player* pKilled)
  647.     {
  648.         std::vector<uint16>::iterator itr;
  649.         for(itr = Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_KILL_PLAYER).begin(); itr != Eluna::get()->m_playerEventBinds.at(PLAYER_EVENT_ON_KILL_PLAYER).end();
  650.             itr++)
  651.         {
  652.             Eluna::get()->BeginCall((*itr));
  653.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pKiller);
  654.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pKilled);
  655.             Eluna::get()->ExecuteCall(2, 0);
  656.         }
  657.     }
  658.  
  659.  
  660.     /// GOSSIP EVENTS
  661.     bool OnGossipHello(Player* pPlayer, Creature* pCreature)
  662.     {
  663.         std::vector<uint16>::iterator itr;
  664.         for(itr = Eluna::get()->m_gossipEventBinds.at(GOSSIP_EVENT_ON_HELLO).begin(); itr != Eluna::get()->m_gossipEventBinds.at(GOSSIP_EVENT_ON_HELLO).end();
  665.             itr++)
  666.         {
  667.             Eluna::get()->BeginCall((*itr));
  668.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  669.             Eluna::get()->PushCreature(Eluna::get()->m_luaState, pCreature);
  670.             Eluna::get()->ExecuteCall(2, 0);
  671.         }
  672.         return true;
  673.     }
  674.  
  675.     bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 sender, uint32 action)
  676.     {
  677.         std::vector<uint16>::iterator itr;
  678.         for(itr = Eluna::get()->m_gossipEventBinds.at(GOSSIP_EVENT_ON_SELECT).begin(); itr != Eluna::get()->m_gossipEventBinds.at(GOSSIP_EVENT_ON_SELECT).end();
  679.             itr++)
  680.         {
  681.             Eluna::get()->BeginCall((*itr));
  682.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  683.             Eluna::get()->PushCreature(Eluna::get()->m_luaState, pCreature);
  684.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, sender);
  685.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, action);
  686.             Eluna::get()->ExecuteCall(4, 0);
  687.         }
  688.         return true;
  689.     }
  690.  
  691.     bool OnGossipSelectCode(Player* pPlayer, Creature* pCreature, uint32 sender, uint32 action, const char* code)
  692.     {
  693.         std::vector<uint16>::iterator itr;
  694.         for(itr = Eluna::get()->m_gossipEventBinds.at(GOSSIP_EVENT_ON_SELECT).begin(); itr != Eluna::get()->m_gossipEventBinds.at(GOSSIP_EVENT_ON_SELECT).end();
  695.             itr++)
  696.         {
  697.             Eluna::get()->BeginCall((*itr));
  698.             Eluna::get()->PushPlayer(Eluna::get()->m_luaState, pPlayer);
  699.             Eluna::get()->PushCreature(Eluna::get()->m_luaState, pCreature);
  700.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, sender);
  701.             Eluna::get()->PushInteger(Eluna::get()->m_luaState, action);
  702.             Eluna::get()->PushString(Eluna::get()->m_luaState, code);
  703.             Eluna::get()->ExecuteCall(5, 0);
  704.         }
  705.         return true;
  706.     }
  707. };
  708.  
  709.  
  710. #endif
Add Comment
Please, Sign In to add comment