From b9834237f2100379d96cfc90fcc4bb938de9fcd2 Mon Sep 17 00:00:00 2001 From: Axel Cocat Date: Mon, 3 Apr 2023 22:55:07 +0200 Subject: [PATCH 1/2] feat: add Unit:GetAttackers --- src/LuaEngine/LuaFunctions.cpp | 1 + src/LuaEngine/UnitMethods.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/LuaEngine/LuaFunctions.cpp b/src/LuaEngine/LuaFunctions.cpp index 2425c7ebeb..65ac6d6949 100644 --- a/src/LuaEngine/LuaFunctions.cpp +++ b/src/LuaEngine/LuaFunctions.cpp @@ -311,6 +311,7 @@ ElunaRegister UnitMethods[] = // {"GetVehicle", &LuaUnit::GetVehicle}, // :GetVehicle() - UNDOCUMENTED - Gets the Vehicle kit of the vehicle the unit is on #endif { "GetMovementType", &LuaUnit::GetMovementType }, + { "GetAttackers", &LuaUnit::GetAttackers }, // Setters { "SetFaction", &LuaUnit::SetFaction }, diff --git a/src/LuaEngine/UnitMethods.h b/src/LuaEngine/UnitMethods.h index 6e26113623..22361b431c 100644 --- a/src/LuaEngine/UnitMethods.h +++ b/src/LuaEngine/UnitMethods.h @@ -1437,6 +1437,34 @@ namespace LuaUnit return 1; } + /** + * Returns the [Unit]'s attackers. + * + * @return table attackers : table of [Unit]s attacking the unit + */ + int GetAttackers(lua_State* L, Unit* unit) + { + const Unit::AttackerSet& attackers = unit->getAttackers(); + + lua_newtable(L); + int table = lua_gettop(L); + uint32 i = 1; + for (Unit* attacker : attackers) + { + if (!attacker) + { + continue; + } + + Eluna::Push(L, attacker); + lua_rawseti(L, table, i); + ++i; + } + + lua_settop(L, table); // push table to top of stack + return 1; + } + /** * Sets the [Unit]'s owner GUID to given GUID. * From 3a6c812eee2fd729d742d799ae036ac1af3671c8 Mon Sep 17 00:00:00 2001 From: Axel Cocat Date: Mon, 3 Apr 2023 22:57:31 +0200 Subject: [PATCH 2/2] update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9f5a43b0e1..a2fa2ad7ca 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ Eluna API for AC: ### Unit - Added `Unit:ModifyThreatPct()`: https://github.com/azerothcore/mod-eluna/pull/25 +- Added `Unit:GetAttackers()`: https://github.com/azerothcore/mod-eluna/pull/116 ### GameObject - Added `GameObject:AddLoot()` to add loot at runtime to an **empty** container: https://github.com/azerothcore/mod-eluna/pull/52