Files
ClassicAPI/src/unit/LineOfSight.cpp
T
Brues 91e88d768a unit: add UnitInLineOfSight(unit)
ClassicAPI extension (retail has no such global): true if the player
has clear LoS to unit, false if world geometry blocks it, nil when the
check can't apply (absent/unresolvable token, or non-unit object).

Traces a ray between the two units through the client's world-collision
core CWorld::Intersect (0x672170, verified in-binary: __fastcall(start,
end, unused, hitOut, tOut, flags), tail-calls the real intersect
FUN_0069BFF0). Flags 0x100111 = terrain + WMO (0x100171 also tests M2
doodads but crashes in some dungeons; matches vanilla server LoS which
ignores doodads). Endpoints are raised by each unit's collision-box
height (CMovement+0xB4) so foot-to-foot rays don't false-block on
terrain, with a two-pass level/look-up trace for height mismatches and
a 150yd guard against a long-segment crash.

Mirrors UnitXP_SP3's proven implementation (same VA, flags, guard, and
two-pass logic) but native, so it works with or without that addon --
we call the intersect rather than hooking it, and our flag isn't one
UnitXP_SP3's detour acts on. Gated on object type (unit/player) so it
never reads CMovement off a GameObject.

Adds FUN_CWORLD_INTERSECT, OFF_CMOVEMENT_COLLISION_BOX_HEIGHT,
OFF_CGOBJECT_TYPE_ID, OBJECT_TYPE_UNIT/PLAYER (reuses the existing
OFF_UNIT_MOVEMENT_INFO_PTR = 0x118), plus API.md and README entries.
2026-07-14 15:48:55 -05:00

165 lines
6.0 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/>.
// `UnitInLineOfSight(unit)` — true if the player has clear line of sight
// to `unit`, false if world geometry blocks it, nil when the check can't
// apply (unresolvable / absent token, or a non-unit object). A ClassicAPI
// extension — retail has no such global — implemented natively on the
// engine's world-collision core so it works without UnitXP_SP3.
//
// Mechanism: trace a ray between the two units through the client's world
// geometry via `CWorld::Intersect` (`FUN_CWORLD_INTERSECT`). Endpoints are
// raised by each unit's collision-box height so a foot-to-foot ray doesn't
// false-block on terrain, and the trace runs in two passes (level look,
// then look up/down) exactly as UnitXP_SP3 does — this file mirrors that
// proven implementation (same VA, flags `0x100111` = terrain + WMO, and the
// 150yd guard against a long-segment crash it documented). We *call* the
// intersect; if UnitXP_SP3 is loaded it has that function hooked, but a
// call routes through its detour harmlessly (our flag isn't one it acts on).
#include "Game.h"
#include "Offsets.h"
#include "unit/Position.h"
#include <cmath>
#include <cstdint>
namespace Unit::LineOfSight {
namespace {
struct Vec3 {
float x, y, z;
};
// CWorld::Intersect(start, end, unused, hitOut, tOut, flags) -> hit?
using Intersect_t = char(__fastcall *)(const Vec3 *start, const Vec3 *end,
int unused, Vec3 *hitOut, float *tOut,
uint32_t flags);
constexpr uint32_t kLoSFlags = 0x100111; // terrain + WMO (LoS combo)
constexpr float kMaxTraceYards = 150.0f; // guard: raw call can crash beyond
constexpr float kSamePointYards = 0.05f;
int ObjectType(void *obj) {
return *reinterpret_cast<int *>(reinterpret_cast<uint8_t *>(obj) +
Offsets::OFF_CGOBJECT_TYPE_ID);
}
bool IsUnitOrPlayer(void *obj) {
const int t = ObjectType(obj);
return t == Offsets::OBJECT_TYPE_UNIT || t == Offsets::OBJECT_TYPE_PLAYER;
}
// Eye-height offset for a unit: its collision-box height, read out of the
// CMovement sub-struct. 0 if the movement pointer is absent (degrades to a
// foot-level trace rather than crashing).
float EyeHeight(void *unit) {
const uint32_t cmov = *reinterpret_cast<uint32_t *>(
reinterpret_cast<uint8_t *>(unit) + Offsets::OFF_UNIT_MOVEMENT_INFO_PTR);
if (cmov == 0)
return 0.0f;
return *reinterpret_cast<float *>(cmov +
Offsets::OFF_CMOVEMENT_COLLISION_BOX_HEIGHT);
}
// True if the segment a→b is clear of world geometry. `CWorld::Intersect`
// returns whether it hit and sets `t` to the hit fraction along a→b; a hit
// with t in [0,1] lies between the endpoints and blocks. The 150yd guard
// (over-range → treat as blocked) and same-point short-circuit mirror
// UnitXP_SP3's wrapper, which documented a crash on very long segments.
bool SegmentClear(const Vec3 &a, const Vec3 &b) {
const float dx = b.x - a.x, dy = b.y - a.y, dz = b.z - a.z;
const float dist = std::sqrt(dx * dx + dy * dy + dz * dz);
if (dist > kMaxTraceYards)
return false;
if (dist <= kSamePointYards)
return true;
Vec3 hit{};
float t = 1.0f;
auto intersect = reinterpret_cast<Intersect_t>(Offsets::FUN_CWORLD_INTERSECT);
const char hitGeometry = intersect(&a, &b, 0, &hit, &t, kLoSFlags);
if (hitGeometry == 0)
return true;
return !(t >= 0.0f && t <= 1.0f);
}
// Player↔unit LoS. Returns 1 = in sight, 0 = blocked, -1 = unknown
// (position unavailable). Two-pass eye-height handling: trace both
// endpoints at the shorter unit's eye height first (level look), then —
// only if that's blocked and the two heights differ — retry with each at
// its own eye height (look up/down).
int InSight(void *a, void *b) {
if (a == b)
return 1;
if (EyeHeight(a) > EyeHeight(b)) {
void *tmp = a;
a = b;
b = tmp;
}
float pa[3];
float pb[3];
if (!Unit::Position::Read(a, pa) || !Unit::Position::Read(b, pb))
return -1;
const float shortEye = EyeHeight(a);
const Vec3 la{pa[0], pa[1], pa[2] + shortEye};
const Vec3 lb{pb[0], pb[1], pb[2] + shortEye};
if (SegmentClear(la, lb))
return 1;
if (EyeHeight(a) == EyeHeight(b))
return 0;
const Vec3 ua{pa[0], pa[1], pa[2] + EyeHeight(a)};
const Vec3 ub{pb[0], pb[1], pb[2] + EyeHeight(b)};
return SegmentClear(ua, ub) ? 1 : 0;
}
int __fastcall Script_UnitInLineOfSight(void *L) {
if (!Game::Lua::IsString(L, 1)) {
Game::Lua::Error(L, "Usage: UnitInLineOfSight(\"unit\")");
return 0;
}
const char *token = Game::Lua::ToString(L, 1);
void *player = Unit::Position::ResolveToken("player");
void *unit = Unit::Position::ResolveToken(token);
if (player == nullptr || unit == nullptr || !IsUnitOrPlayer(player) ||
!IsUnitOrPlayer(unit)) {
Game::Lua::PushNil(L);
return 1;
}
const int sight = InSight(player, unit);
if (sight < 0) {
Game::Lua::PushNil(L);
return 1;
}
Game::Lua::PushBool(L, sight != 0);
return 1;
}
void RegisterLuaFunctions() {
Game::Lua::RegisterGlobalFunction("UnitInLineOfSight",
&Script_UnitInLineOfSight);
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace Unit::LineOfSight