Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Unit:GetAttackers #116

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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