Files
ClassicAPI/src/unit/ClosestPosition.cpp
T
Brues 8629e13e1f gameobject: add ClosestGameObjectPosition(gameObjectID)
Returns xPos, yPos, distance for the nearest visible game object of a
given GO-template ID, or nothing when none is in range — the game-object
counterpart of ClosestUnitPosition.

Extract the shared visible-object-manager scan into
Object::ClosestByEntry (object/ClosestByEntry.{h,cpp}): enumerate visible
objects, match GUID type + template entry (bits 24-47), return the
closest one's world position + center distance. Both ClosestUnitPosition
(TYPEMASK_UNIT / Creature) and ClosestGameObjectPosition
(TYPEMASK_GAMEOBJECT / GameObject) are now thin wrappers over it;
ClosestUnitPosition behavior is unchanged.

Same retail-vs-vanilla caveat as the unit version: returns the nearest
CURRENTLY-VISIBLE object rather than reading retail's static starting-
zone database (which vanilla lacks). Documented in docs/API.md.
2026-07-20 18:26:46 -05:00

65 lines
2.4 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 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
// `ClosestUnitPosition(creatureID)` → `xPos, yPos, distance` — the world
// position of the nearest creature with the given NPC (creature-template)
// ID, and its distance from the player in yards. Returns nothing when no
// matching creature is found (the modern `MayReturnNothing` contract).
//
// Semantic note vs. retail: modern WoW's `ClosestUnitPosition` reads a
// static client-side spawn database and only works for starting-zone mobs
// (a tutorial/help helper). Vanilla 1.12 has no such database, so we
// implement the useful, 1.12-native reading: the closest creature of that
// entry that is CURRENTLY VISIBLE (in the client's object manager /
// broadcast window). The shared scan lives in Object::ClosestByEntry.
#include "Game.h"
#include "Offsets.h"
#include "guid/Guid.h"
#include "object/ClosestByEntry.h"
#include <cmath>
#include <cstdint>
namespace Unit::ClosestPosition {
namespace {
int __fastcall Script_ClosestUnitPosition(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: ClosestUnitPosition(creatureID)");
return 0;
}
const auto entry = static_cast<uint32_t>(Game::Lua::ToNumber(L, 1));
const Object::ClosestByEntry::Result r = Object::ClosestByEntry::Find(
Offsets::TYPEMASK_UNIT, Guid::Type::Creature, entry);
if (!r.found)
return 0;
Game::Lua::PushNumber(L, static_cast<double>(r.x));
Game::Lua::PushNumber(L, static_cast<double>(r.y));
Game::Lua::PushNumber(L, std::sqrt(static_cast<double>(r.distSq)));
return 3;
}
void RegisterLuaFunctions() {
Game::Lua::RegisterGlobalFunction("ClosestUnitPosition",
&Script_ClosestUnitPosition);
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace Unit::ClosestPosition