From dc8afa45b20857a9ffbcdeaa4aa834d879cae0d2 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Wed, 9 Jul 2014 21:18:20 +0300 Subject: [PATCH] Eluna add new hook (need to think where to place this) and corrected Power functions --- HookMgr.cpp | 16 +++++ HookMgr.h | 3 + LuaEngine.h | 10 ++- LuaFunctions.cpp | 13 ++-- UnitMethods.h | 157 +++++++++++++---------------------------------- 5 files changed, 72 insertions(+), 127 deletions(-) diff --git a/HookMgr.cpp b/HookMgr.cpp index 4eccaf1..001744e 100644 --- a/HookMgr.cpp +++ b/HookMgr.cpp @@ -1272,6 +1272,22 @@ void Eluna::OnUpdate(Map* map, uint32 diff) EVENT_EXECUTE(0); ENDCALL(); } +void Eluna::OnRemove(Map* map, Creature* creature) +{ + EVENT_BEGIN(ServerEventBindings, MAP_EVENT_ON_REMOVE_CREATURE, return); + Push(L, map); + Push(L, creature); + ENTRY_EXECUTE(0); + ENDCALL(); +} +void Eluna::OnRemove(Map* map, GameObject* gameobject) +{ + EVENT_BEGIN(ServerEventBindings, MAP_EVENT_ON_REMOVE_GAMEOBJECT, return); + Push(L, map); + Push(L, gameobject); + ENTRY_EXECUTE(0); + ENDCALL(); +} // creature bool Eluna::OnDummyEffect(Unit* pCaster, uint32 spellId, SpellEffIndex effIndex, Creature* pTarget) diff --git a/HookMgr.h b/HookMgr.h index d930d86..72d173e 100644 --- a/HookMgr.h +++ b/HookMgr.h @@ -86,6 +86,9 @@ namespace HookMgr // AddOns ADDON_EVENT_ON_MESSAGE = 30, // (event, sender, type, prefix, msg, target) - target can be nil/whisper_target/guild/group/channel + MAP_EVENT_ON_REMOVE_CREATURE = 31, // (event, map, creature) + MAP_EVENT_ON_REMOVE_GAMEOBJECT = 32, // (event, map, gameobject) + SERVER_EVENT_COUNT }; diff --git a/LuaEngine.h b/LuaEngine.h index c4aae66..11b8ebb 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -675,6 +675,8 @@ public: void OnPlayerEnter(Map* map, Player* player); void OnPlayerLeave(Map* map, Player* player); void OnUpdate(Map* map, uint32 diff); + void OnRemove(Map* map, Creature* creature); + void OnRemove(Map* map, GameObject* gameobject); /* World */ void OnOpenStateChange(bool open); @@ -1063,13 +1065,9 @@ public: { T* obj = Eluna::CHECKOBJ(L, 1); // get self if (obj) - { lua_pushfstring(L, "%s: (%p)", tname, obj); - return 1; - } - lua_pushnil(L); - lua_replace(L, 1); - luaL_tolstring(L, 1, NULL); + else + lua_pushstring(L, "nil"); return 1; } }; diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp index cf93fb8..2798caa 100644 --- a/LuaFunctions.cpp +++ b/LuaFunctions.cpp @@ -194,12 +194,12 @@ ElunaRegister UnitMethods[] = { "GetHealth", &LuaUnit::GetHealth }, // :GetHealth() { "GetDisplayId", &LuaUnit::GetDisplayId }, // :GetDisplayId() { "GetNativeDisplayId", &LuaUnit::GetNativeDisplayId }, // :GetNativeDisplayId() - { "GetPower", &LuaUnit::GetPower }, // :GetPower(index) - returns power at index. Index can be omitted - { "GetMaxPower", &LuaUnit::GetMaxPower }, // :GetMaxPower(index) - returns power at index. Index can be omitted - { "GetPowerType", &LuaUnit::GetPowerType }, // :GetPowerType() - Returns the power type + { "GetPower", &LuaUnit::GetPower }, // :GetPower([type]) - returns power for power type. type can be omitted + { "GetMaxPower", &LuaUnit::GetMaxPower }, // :GetMaxPower([type]) - returns max power for power type. type can be omitted + { "GetPowerType", &LuaUnit::GetPowerType }, // :GetPowerType() - Returns the power type tye unit uses { "GetMaxHealth", &LuaUnit::GetMaxHealth }, // :GetMaxHealth() { "GetHealthPct", &LuaUnit::GetHealthPct }, // :GetHealthPct() - { "GetPowerPct", &LuaUnit::GetPowerPct }, // :GetPowerPct(power_id) + { "GetPowerPct", &LuaUnit::GetPowerPct }, // :GetPowerPct([type]) - returns power percent for power type. type can be omitted { "GetGender", &LuaUnit::GetGender }, // :GetGender() - returns the gender where male = 0 female = 1 { "GetRace", &LuaUnit::GetRace }, // :GetRace() { "GetClass", &LuaUnit::GetClass }, // :GetClass() @@ -238,8 +238,9 @@ ElunaRegister UnitMethods[] = { "SetLevel", &LuaUnit::SetLevel }, // :SetLevel(amount) { "SetHealth", &LuaUnit::SetHealth }, // :SetHealth(amount) { "SetMaxHealth", &LuaUnit::SetMaxHealth }, // :SetMaxHealth(amount) - { "SetPower", &LuaUnit::SetPower }, // :SetPower(index, amount) - { "SetMaxPower", &LuaUnit::SetMaxPower }, // :SetMaxPower(index, amount) + { "SetPower", &LuaUnit::SetPower }, // :SetPower([type,] amount) + { "SetMaxPower", &LuaUnit::SetMaxPower }, // :SetMaxPower([type,] amount) + { "SetPowerType", &LuaUnit::SetPowerType }, // :SetPowerType(type) { "SetDisplayId", &LuaUnit::SetDisplayId }, // :SetDisplayId(id) { "SetNativeDisplayId", &LuaUnit::SetNativeDisplayId }, // :SetNativeDisplayId(id) { "SetFacing", &LuaUnit::SetFacing }, // :SetFacing(o) - Sets the Unit facing to arg diff --git a/UnitMethods.h b/UnitMethods.h index 3093934..cf66c9f 100644 --- a/UnitMethods.h +++ b/UnitMethods.h @@ -549,81 +549,46 @@ namespace LuaUnit return 1; } + Powers PowerSelectorHelper(lua_State* L, Unit* unit, int powerType = -1) + { + if (powerType == -1) + return unit->getPowerType(); + + if (powerType < 0 || powerType >= int(MAX_POWERS)) + luaL_argerror(L, 2, "valid Powers expected"); + + return (Powers)powerType; + } + int GetPower(lua_State* L, Unit* unit) { int type = Eluna::CHECKVAL(L, 2, -1); - if (type == -1) - { + Powers power = PowerSelectorHelper(L, unit, type); - switch (unit->getClass()) - { - case 1: - type = POWER_RAGE; - break; - case 4: - type = POWER_ENERGY; - break; -#if (!defined(TBC) && !defined(CLASSIC)) - case 6: - type = POWER_RUNIC_POWER; - break; -#endif - case 2: - case 3: - case 5: - case 7: - case 8: - case 9: - case 11: - type = POWER_MANA; - break; - default: - type = POWER_MANA; - } - } - else if (type < 0 || type >= int(MAX_POWERS)) - return luaL_argerror(L, 2, "valid Powers expected"); - - Eluna::Push(L, unit->GetPower((Powers)type)); + Eluna::Push(L, unit->GetPower(power)); return 1; } int GetMaxPower(lua_State* L, Unit* unit) { int type = Eluna::CHECKVAL(L, 2, -1); - if (type == -1) - { + Powers power = PowerSelectorHelper(L, unit, type); - switch (unit->getClass()) - { - case 1: - type = POWER_RAGE; - break; - case 4: - type = POWER_ENERGY; - break; -#if (!defined(TBC) && !defined(CLASSIC)) - case 6: - type = POWER_RUNIC_POWER; - break; + Eluna::Push(L, unit->GetMaxPower(power)); + return 1; + } + + int GetPowerPct(lua_State* L, Unit* unit) + { + int type = Eluna::CHECKVAL(L, 2, -1); + Powers power = PowerSelectorHelper(L, unit, type); + +#if (!defined(TRINITY) && defined(WOTLK)) + float percent = ((float)unit->GetPower(power) / (float)unit->GetMaxPower(power)) * 100.0f; +#else + float percent = ((float)unit->GetPower(power) / (float)unit->GetMaxPower(power)) * 100.0f; #endif - case 2: - case 3: - case 5: - case 7: - case 8: - case 9: - case 11: - type = POWER_MANA; - break; - default: - type = POWER_MANA; - } - } - else if (type < 0 || type >= int(MAX_POWERS)) - return luaL_argerror(L, 2, "valid Powers expected"); - - Eluna::Push(L, unit->GetMaxPower((Powers)type)); + Eluna::Push(L, percent); return 1; } @@ -653,17 +618,6 @@ namespace LuaUnit return 1; } - int GetPowerPct(lua_State* L, Unit* unit) - { -#if (!defined(TRINITY) && defined(WOTLK)) - float percent = (unit->GetPower(unit->GetPowerType()) / unit->GetMaxPower(unit->GetPowerType())) * 100; -#else - float percent = (unit->GetPower(unit->getPowerType()) / unit->GetMaxPower(unit->getPowerType())) * 100; -#endif - Eluna::Push(L, percent); - return 1; - } - int GetGender(lua_State* L, Unit* unit) { Eluna::Push(L, unit->getGender()); @@ -933,57 +887,30 @@ namespace LuaUnit int SetPower(lua_State* L, Unit* unit) { - int type = Eluna::CHECKVAL(L, 2); + int type = Eluna::CHECKVAL(L, 2, -1); uint32 amt = Eluna::CHECKVAL(L, 3); + Powers power = PowerSelectorHelper(L, unit, type); - switch (type) - { - case POWER_MANA: - unit->SetPower(POWER_MANA, amt); - break; - case POWER_RAGE: - unit->SetPower(POWER_RAGE, amt); - break; - case POWER_ENERGY: - unit->SetPower(POWER_ENERGY, amt); - break; -#if (!defined(TBC) && !defined(CLASSIC)) - case POWER_RUNIC_POWER: - unit->SetMaxPower(POWER_RUNIC_POWER, amt); - break; -#endif - default: - return luaL_argerror(L, 2, "valid Powers expected"); - break; - } + unit->SetPower(power, amt); return 0; } int SetMaxPower(lua_State* L, Unit* unit) { - int type = Eluna::CHECKVAL(L, 2); + int type = Eluna::CHECKVAL(L, 2, -1); uint32 amt = Eluna::CHECKVAL(L, 3); + Powers power = PowerSelectorHelper(L, unit, type); - switch (type) - { - case POWER_MANA: - unit->SetMaxPower(POWER_MANA, amt); - break; - case POWER_RAGE: - unit->SetMaxPower(POWER_RAGE, amt); - break; - case POWER_ENERGY: - unit->SetMaxPower(POWER_ENERGY, amt); - break; -#if (!defined(TBC) && !defined(CLASSIC)) - case POWER_RUNIC_POWER: - unit->SetMaxPower(POWER_RUNIC_POWER, amt); - break; -#endif - default: + unit->SetMaxPower(power, amt); + return 0; + } + + int SetPowerType(lua_State* L, Unit* unit) + { + uint32 type = Eluna::CHECKVAL(L, 2); + if (type >= int(MAX_POWERS)) return luaL_argerror(L, 2, "valid Powers expected"); - break; - } + unit->setPowerType((Powers)type); return 0; }