mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
commit 24ab88fac3c9da1d83e173aefacad92ad3b0ab2c Author: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun Jun 7 15:11:27 2026 -0500 C_Loot docs commit aca61c0cb7b90cb84c2d782490de38a2db099004 Author: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu Jun 4 21:47:33 2026 -0500 loot scan commit bf44cb9acc07504336c54323074126738ed623a0 Author: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu Jun 4 21:30:29 2026 -0500 aoe loot test commit 648cb053a9232ea9eef17e48ff45424c9eed5bbb Author: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu Jun 4 19:06:18 2026 -0500 LootUnit/GetNearbyLootableUnits
105 lines
4.3 KiB
C++
105 lines
4.3 KiB
C++
// This file is part of ClassicAPI.
|
|
//
|
|
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
|
// of the GNU Lesser General Public License as published by the Free Software Foundation, either
|
|
// version 3 of the License, or (at your option) any later version.
|
|
//
|
|
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
// PURPOSE. See the GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License along with
|
|
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#include "../Game.h"
|
|
#include "../Offsets.h"
|
|
#include "../guid/Guid.h"
|
|
|
|
#include <cstdint>
|
|
|
|
namespace Loot::Unit {
|
|
|
|
namespace {
|
|
|
|
using ClntObjMgrObjectPtr_t = void *(__fastcall *)(uint32_t typeMask,
|
|
const char *debugMsg,
|
|
uint32_t guidLo,
|
|
uint32_t guidHi,
|
|
int debugCode);
|
|
using ResolveUnitToken_t = void *(__fastcall *)(const char *token);
|
|
|
|
// `FUN_CMSG_LOOT_UNIT` is `__thiscall(player, target, useDistanceCheck)`.
|
|
// MSVC `__thiscall` puts `player` in ECX and the rest on the stack —
|
|
// matching the engine's own callsite at `FUN_0060FA20`. The trailing
|
|
// `char` argument is padded to 4 stack bytes by the ABI.
|
|
using LootUnit_t = void(__thiscall *)(void *player, void *target,
|
|
char useDistanceCheck);
|
|
|
|
// `C_Loot.LootUnit(guid)` — initiates a loot session against the unit
|
|
// identified by `guid` (a hex-string as returned by `UnitGUID` /
|
|
// `C_Loot.GetNearbyLootableUnits`). The engine handles release-prior-
|
|
// loot, packet construction, and dispatch; the loot window appears
|
|
// asynchronously via the regular `LOOT_OPENED` event once the server's
|
|
// `SMSG_LOOT_RESPONSE` lands.
|
|
//
|
|
// `useDistanceCheck` is hardcoded to `0` so the engine doesn't auto-
|
|
// walk the player toward a corpse that happens to be just out of
|
|
// range — which would be a surprise side-effect for an AoE-loot
|
|
// caller looping over `GetNearbyLootableUnits`. The server still
|
|
// enforces real range; OOR requests just don't get a response.
|
|
//
|
|
// Returns nothing. Silent no-op when:
|
|
// - The object manager is NULL (glue / character-select).
|
|
// - The GUID doesn't resolve to a visible `TYPEMASK_UNIT` object
|
|
// (target despawned, out of render range, wrong typemask).
|
|
// - The local player pointer can't be resolved.
|
|
// Errors only on Lua usage problems (missing arg, unparseable GUID).
|
|
int __fastcall Script_LootUnit(void *L) {
|
|
if (!Game::Lua::IsString(L, 1)) {
|
|
Game::Lua::Error(L, "Usage: C_Loot.LootUnit(guid)");
|
|
return 0;
|
|
}
|
|
const char *guidStr = Game::Lua::ToString(L, 1);
|
|
uint64_t guid = 0;
|
|
if (!Guid::Parse(guidStr, &guid) || guid == 0) {
|
|
Game::Lua::Error(L, "Usage: C_Loot.LootUnit(guid) — unparseable GUID");
|
|
return 0;
|
|
}
|
|
|
|
// Object manager NULL pre-login; bail silently rather than
|
|
// crashing inside the resolver.
|
|
if (*reinterpret_cast<void *volatile *>(Offsets::VAR_LOCAL_PLAYER_PTR) == nullptr)
|
|
return 0;
|
|
|
|
auto ObjectPtr = reinterpret_cast<ClntObjMgrObjectPtr_t>(
|
|
Offsets::FUN_CLNT_OBJ_MGR_OBJECT_PTR);
|
|
void *target = ObjectPtr(Offsets::TYPEMASK_UNIT, nullptr,
|
|
static_cast<uint32_t>(guid),
|
|
static_cast<uint32_t>(guid >> 32), 0);
|
|
if (target == nullptr)
|
|
return 0;
|
|
|
|
// `ResolveUnitToken("player")` gives the canonical CGPlayer_C the
|
|
// engine uses for `__thiscall` calls — distinct from the global
|
|
// at `VAR_LOCAL_PLAYER_PTR` (see the comment on that offset).
|
|
auto ResolveUnitToken = reinterpret_cast<ResolveUnitToken_t>(
|
|
Offsets::FUN_RESOLVE_UNIT_TOKEN);
|
|
void *player = ResolveUnitToken("player");
|
|
if (player == nullptr)
|
|
return 0;
|
|
|
|
auto LootUnit = reinterpret_cast<LootUnit_t>(Offsets::FUN_CMSG_LOOT_UNIT);
|
|
LootUnit(player, target, /*useDistanceCheck=*/0);
|
|
return 0;
|
|
}
|
|
|
|
void Register() {
|
|
Game::Lua::RegisterTableFunction("C_Loot", "LootUnit", &Script_LootUnit);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
static const Game::ModuleAutoRegister _autoreg{&Register};
|
|
|
|
} // namespace Loot::Unit
|