Skip to content

Commit

Permalink
feat: add Unit:GetAttackers (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-o-b-o-t-o authored Apr 4, 2023
1 parent 995ca16 commit 8a85df8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/LuaFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ ElunaRegister<Unit> 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 },
Expand Down
28 changes: 28 additions & 0 deletions src/LuaEngine/UnitMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 8a85df8

Please sign in to comment.