Files
ClassicAPI/src/guid/PlayerInfo.cpp
T
2026-05-13 23:25:59 -05:00

93 lines
3.5 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 "guid/Guid.h"
#include <cstdint>
namespace Guid::PlayerInfoBindings {
namespace {
// Shared body for every `C_PlayerInfo.GUIDIs*` function. Each
// registered entry stores the `Guid::Type` it tests for; we
// classify the input and push the boolean result.
//
// Lua C closures (`lua_pushcclosure` with upvalues) are the natural
// fit here, but we don't currently expose `RegisterTableFunction`
// with an upvalue-carrying variant. Cheaper to just register one
// thin wrapper per type — they're literally a single test each
// after the shared parse step.
bool TestType(void *L, Type expected) {
if (!Game::Lua::IsString(L, 1))
return false;
uint64_t guid = 0;
if (!Parse(Game::Lua::ToString(L, 1), &guid))
return false;
return Classify(guid) == expected;
}
int PushTypeResult(void *L, Type expected) {
Game::Lua::PushBoolean(L, TestType(L, expected) ? 1 : 0);
return 1;
}
// `C_PlayerInfo.GUIDIsPlayer(guid)` — modern API, returns true iff
// `guid` is a player GUID. Vanilla 1.12 GUIDs encode the type in
// the high 16 bits — players have `0x0000` (low 32 bits = player
// ID). The `guid == 0` sentinel returns false.
int __fastcall Script_GUIDIsPlayer(void *L) {
return PushTypeResult(L, Type::Player);
}
// `C_PlayerInfo.GUIDIsCreature(guid)` — true iff the GUID is an
// NPC / mob (high prefix `0xF130`). Not a stock modern function
// in `C_PlayerInfo`, but the natural sibling to `GUIDIsPlayer`
// for code that's deciding "is this combat-log row a player or an
// NPC kill?".
int __fastcall Script_GUIDIsCreature(void *L) {
return PushTypeResult(L, Type::Creature);
}
// `C_PlayerInfo.GUIDIsPet(guid)` — true iff the GUID is a pet
// (high prefix `0xF140`). Distinguishes warlock/hunter pets and
// mage water elementals from their owners.
int __fastcall Script_GUIDIsPet(void *L) {
return PushTypeResult(L, Type::Pet);
}
// `C_PlayerInfo.GUIDIsGameObject(guid)` — true iff the GUID is a
// game object (high prefix `0xF110`): herb nodes, chests, fishing
// bobbers, doors, etc.
int __fastcall Script_GUIDIsGameObject(void *L) {
return PushTypeResult(L, Type::GameObject);
}
void Register() {
Game::Lua::RegisterTableFunction("C_PlayerInfo", "GUIDIsPlayer",
&Script_GUIDIsPlayer);
Game::Lua::RegisterTableFunction("C_PlayerInfo", "GUIDIsCreature",
&Script_GUIDIsCreature);
Game::Lua::RegisterTableFunction("C_PlayerInfo", "GUIDIsPet",
&Script_GUIDIsPet);
Game::Lua::RegisterTableFunction("C_PlayerInfo", "GUIDIsGameObject",
&Script_GUIDIsGameObject);
}
} // namespace
static const Game::ModuleAutoRegister _autoreg{&Register};
} // namespace Guid::PlayerInfoBindings