mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-28 15:37:14 +00:00
* feat: raw terrain parsing for scene data Replace old dNavigation/dTerrain raw parser with new Raw module in dZoneManager. Parse heightmaps, color maps, and scene maps from .raw files to determine which scene a position belongs to. Build scene adjacency graph from terrain data and scene transitions. Adds NiColor type, SceneColor lookup table, eSceneType enum, terrain mesh generation with OBJ export, and debug slash commands for scene visualization. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor: deduplicate terrain grid lookup logic, remove unnecessary cast Add IsValidForSceneLookup(), GetSceneIDAtGrid(), and GridToWorldPos() to Raw::Chunk. Use them in GenerateTerrainMesh, GetSceneIDFromPosition, SpawnScenePoints, and SpawnAllScenePoints instead of duplicated inline math. Remove unnecessary static_cast in LoadSceneTransitionInfo. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: add version < 32 support to raw terrain parser The parser was only correct for v32 files. For v<32: - colorMapResolution is width-1, not width - Color map reads width*width*4 bytes (BGRA per-pixel), not colorMapRes^2*4 - No DDS lightmap - No texture settings byte or blend DDS - Scene map: v31 reads (colorMapRes+1)^2 cells with border skip, v<31 skips 1 byte - No mesh data Verified against all 309 .raw files across multiple client versions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: log and reject legacy raw terrain format, clean up ReadRaw API Log a clear message and throw if the zone uses a legacy raw format (LUZ version < 30) since no known maps use it. Remove ReadLegacyRaw and the mapVersion parameter from ReadRaw. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: revert v33 scene field renames to unknown1/unknown2 These fields are skipped by the client in every known build (ptr += 12 and ptr += 4). No client ever reads them into a struct. Keep the original unknown1/unknown2 names from main. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: load the zone without scene lookups when its raw terrain is missing or unreadable Log at debug when it parses; build the terrain mesh only for the OBJ export; read the zone name and description only for version 31 and later, as the client does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: look up the scene at a position as the client does, in constant time Chunk bounds span (width - 1) * scale; the nearest scene map cell is used and the far edge is kept one cell inside. Chunks are indexed by grid cell when they form a grid. Skipping reads instead of seeking, so a cut-off file can't seek past the end. Terrain mesh indices follow the vertex order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: scene point commands take a step and use brick LOTs from the BrickIDTable Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: rename NiColor::ToXYZ to Luminance Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: raw terrain reading, scene lookup, scene graph and scene commands Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: close the OBJ file before removing it so the test passes on Windows Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep the zone boundary lines and read the luz scene count as the client does Each boundary line's destination is read as a map ID and an instance ID into a LWOZONEID. The scene count is a byte before version 37 and a u32 from it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: name the fourth flair colour byte padding The client never sets or reads it, so it holds leftover memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep the scene map of version 31 and older raws These files have a (resolution + 1)^2 scene map, kept in sceneMapLegacy. Version 31 uses its first resolution rows and columns; older versions are all scene 0, as in the client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor: simplify the raw heightmap size check The dimensions are capped at kMaxResolution, so the product can't overflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: remove the scene point spawn commands and send /getadjacentscenes as one message The spawn commands were only for testing. /getscene and /getadjacentscenes stay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
267 lines
6.8 KiB
C++
267 lines
6.8 KiB
C++
#include "dNavMesh.h"
|
|
|
|
|
|
#include "Game.h"
|
|
#include "Logger.h"
|
|
#include "dPlatforms.h"
|
|
#include "NiPoint3.h"
|
|
#include "BinaryIO.h"
|
|
#include "BinaryPathFinder.h"
|
|
|
|
#include "dZoneManager.h"
|
|
#include "DluAssert.h"
|
|
#include "DetourExtensions.h"
|
|
|
|
dNavMesh::dNavMesh(uint32_t zoneId) {
|
|
m_ZoneId = zoneId;
|
|
|
|
this->LoadNavmesh();
|
|
|
|
if (m_NavMesh) {
|
|
m_NavQuery = dtAllocNavMeshQuery();
|
|
m_NavQuery->init(m_NavMesh, 2048);
|
|
|
|
LOG("Navmesh loaded successfully!");
|
|
} else {
|
|
LOG("Navmesh loading failed (This may be intended).");
|
|
}
|
|
}
|
|
|
|
dNavMesh::~dNavMesh() {
|
|
// Clean up Recast information
|
|
|
|
if (m_NavMesh) dtFreeNavMesh(m_NavMesh);
|
|
if (m_NavQuery) dtFreeNavMeshQuery(m_NavQuery);
|
|
}
|
|
|
|
|
|
void dNavMesh::LoadNavmesh() {
|
|
struct FPClose {
|
|
FILE* fp{};
|
|
~FPClose() { if (fp) fclose(fp); }
|
|
} fp;
|
|
|
|
std::string path = (BinaryPathFinder::GetBinaryDir() / "navmeshes/" / (std::to_string(m_ZoneId) + ".bin")).string();
|
|
|
|
if (!BinaryIO::DoesFileExist(path)) {
|
|
return;
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
fopen_s(&fp.fp, path.c_str(), "rb");
|
|
#elif __APPLE__
|
|
// macOS has 64bit file IO by default
|
|
fp.fp = fopen(path.c_str(), "rb");
|
|
#else
|
|
fp.fp = fopen64(path.c_str(), "rb");
|
|
#endif
|
|
|
|
if (!fp.fp) {
|
|
return;
|
|
}
|
|
|
|
// Read header.
|
|
NavMeshSetHeader header;
|
|
size_t readLen = fread(&header, sizeof(NavMeshSetHeader), 1, fp.fp);
|
|
if (readLen != 1) {
|
|
return;
|
|
}
|
|
|
|
if (header.magic != NAVMESHSET_MAGIC) {
|
|
return;
|
|
}
|
|
|
|
if (header.version != NAVMESHSET_VERSION) {
|
|
return;
|
|
}
|
|
|
|
dtNavMesh* mesh = dtAllocNavMesh();
|
|
if (!mesh) {
|
|
return;
|
|
}
|
|
|
|
dtStatus status = mesh->init(&header.params);
|
|
if (dtStatusFailed(status)) {
|
|
return;
|
|
}
|
|
|
|
// Read tiles.
|
|
for (int i = 0; i < header.numTiles; ++i) {
|
|
NavMeshTileHeader tileHeader;
|
|
readLen = fread(&tileHeader, sizeof(tileHeader), 1, fp.fp);
|
|
if (readLen != 1) return;
|
|
|
|
if (!tileHeader.tileRef || !tileHeader.dataSize)
|
|
break;
|
|
|
|
unsigned char* data = static_cast<unsigned char*>(dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM));
|
|
if (!data) break;
|
|
memset(data, 0, tileHeader.dataSize);
|
|
readLen = fread(data, tileHeader.dataSize, 1, fp.fp);
|
|
if (readLen != 1) {
|
|
dtFree(data);
|
|
return;
|
|
}
|
|
|
|
mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
|
|
}
|
|
|
|
m_NavMesh = mesh;
|
|
}
|
|
|
|
NiPoint3 dNavMesh::NearestPoint(const NiPoint3& location, const float halfExtent) const {
|
|
NiPoint3 toReturn = location;
|
|
if (m_NavMesh != nullptr) {
|
|
float pos[3];
|
|
pos[0] = location.x;
|
|
pos[1] = location.y;
|
|
pos[2] = location.z;
|
|
|
|
dtPolyRef nearestRef = 0;
|
|
float polyPickExt[3] = { halfExtent, halfExtent, halfExtent };
|
|
float nearestPoint[3] = { 0.0f, 0.0f, 0.0f };
|
|
dtQueryFilter filter{};
|
|
|
|
auto hasPoly = m_NavQuery->findNearestPoly(pos, polyPickExt, &filter, &nearestRef, nearestPoint);
|
|
if (hasPoly != DT_SUCCESS) {
|
|
toReturn = location;
|
|
} else {
|
|
toReturn.x = nearestPoint[0];
|
|
toReturn.y = nearestPoint[1];
|
|
toReturn.z = nearestPoint[2];
|
|
}
|
|
}
|
|
return toReturn;
|
|
}
|
|
|
|
float dNavMesh::GetHeightAtPoint(const NiPoint3& location, const float halfExtentsHeight) const {
|
|
if (m_NavMesh == nullptr) {
|
|
return location.y;
|
|
}
|
|
|
|
float toReturn = 0.0f;
|
|
float pos[3];
|
|
pos[0] = location.x;
|
|
pos[1] = location.y;
|
|
pos[2] = location.z;
|
|
|
|
dtPolyRef nearestRef = 0;
|
|
float polyPickExt[3] = { 32.0f, halfExtentsHeight, 32.0f };
|
|
float nearestPoint[3] = { 0.0f, 0.0f, 0.0f };
|
|
dtQueryFilter filter{};
|
|
|
|
auto hasPoly = m_NavQuery->findNearestPoly(pos, polyPickExt, &filter, &nearestRef, nearestPoint);
|
|
m_NavQuery->getPolyHeight(nearestRef, pos, &toReturn);
|
|
#ifdef _DEBUG
|
|
if (toReturn != 0.0f) {
|
|
DluAssert(toReturn == nearestPoint[1]);
|
|
}
|
|
#endif
|
|
if (toReturn == 0.0f) {
|
|
// If we were unable to get the poly height, but the query returned a success, just use the height of the nearest point.
|
|
// This is what seems to happen anyways and it is better than returning zero.
|
|
if (hasPoly == DT_SUCCESS) {
|
|
toReturn = nearestPoint[1];
|
|
} else {
|
|
toReturn = location.y;
|
|
}
|
|
}
|
|
// If we failed to even find a poly, do not change the height since we have no idea what it should be.
|
|
|
|
return toReturn;
|
|
}
|
|
|
|
std::vector<NiPoint3> dNavMesh::GetPath(const NiPoint3& startPos, const NiPoint3& endPos, float speed) {
|
|
std::vector<NiPoint3> path;
|
|
|
|
// Allows for non-navmesh maps (like new custom maps) to have "basic" enemies.
|
|
if (m_NavMesh == nullptr) {
|
|
// How many points to generate between start/end?
|
|
// Note: not actually 100% accurate due to rounding, but worst case it causes them to go a tiny bit faster
|
|
// than their speed value would normally allow at the end.
|
|
int numPoints = startPos.Distance(startPos, endPos) / speed;
|
|
|
|
path.push_back(startPos); //insert the start pos
|
|
|
|
// Linearly interpolate between these two points:
|
|
const auto yDist = endPos.y - startPos.y;
|
|
// Ensure we cannot divide by zero
|
|
const auto xDist = endPos.x - startPos.x;
|
|
if (xDist != 0.0f) {
|
|
for (int i = 0; i < numPoints; i++) {
|
|
NiPoint3 newPoint{ startPos };
|
|
|
|
newPoint.x += speed;
|
|
newPoint.y = newPoint.y + ((yDist / xDist) * (newPoint.x - startPos.x));
|
|
|
|
path.push_back(newPoint);
|
|
}
|
|
}
|
|
|
|
path.push_back(endPos); //finally insert our end pos
|
|
|
|
return path;
|
|
}
|
|
|
|
float sPos[3];
|
|
float ePos[3];
|
|
sPos[0] = startPos.x;
|
|
sPos[1] = startPos.y;
|
|
sPos[2] = startPos.z;
|
|
|
|
ePos[0] = endPos.x;
|
|
ePos[1] = endPos.y;
|
|
ePos[2] = endPos.z;
|
|
|
|
dtStatus pathFindStatus;
|
|
dtPolyRef startRef{};
|
|
dtPolyRef endRef{};
|
|
float polyPickExt[3] = { 32.0f, 32.0f, 32.0f };
|
|
dtQueryFilter filter{};
|
|
|
|
//Find our start poly
|
|
const auto startResult = m_NavQuery->findNearestPoly(sPos, polyPickExt, &filter, &startRef, 0);
|
|
|
|
//Find our end poly
|
|
const auto endResult = m_NavQuery->findNearestPoly(ePos, polyPickExt, &filter, &endRef, 0);
|
|
|
|
pathFindStatus = DT_FAILURE;
|
|
int m_nstraightPath = 0;
|
|
int m_npolys = 0;
|
|
dtPolyRef m_polys[MAX_POLYS];
|
|
float m_straightPath[MAX_POLYS * 3];
|
|
unsigned char m_straightPathFlags[MAX_POLYS];
|
|
dtPolyRef m_straightPathPolys[MAX_POLYS];
|
|
int m_straightPathOptions = 0;
|
|
|
|
if (dtStatusSucceed(startResult) && dtStatusSucceed(endResult)) {
|
|
m_NavQuery->findPath(startRef, endRef, sPos, ePos, &filter, m_polys, &m_npolys, MAX_POLYS);
|
|
|
|
if (m_npolys) {
|
|
// In case of partial path, make sure the end point is clamped to the last polygon.
|
|
float epos[3];
|
|
dtVcopy(epos, ePos);
|
|
|
|
if (m_polys[m_npolys - 1] != endRef) {
|
|
m_NavQuery->closestPointOnPoly(m_polys[m_npolys - 1], ePos, epos, 0);
|
|
}
|
|
|
|
m_NavQuery->findStraightPath(sPos, epos, m_polys, m_npolys,
|
|
m_straightPath, m_straightPathFlags,
|
|
m_straightPathPolys, &m_nstraightPath, MAX_POLYS, m_straightPathOptions);
|
|
|
|
// At this point we have our path. Copy it to the path store
|
|
int nIndex = 0;
|
|
for (int nVert = 0; nVert < m_nstraightPath; nVert++) {
|
|
NiPoint3 newPoint{ m_straightPath[nIndex++], m_straightPath[nIndex++], m_straightPath[nIndex++] };
|
|
path.push_back(newPoint);
|
|
}
|
|
}
|
|
} else {
|
|
m_npolys = 0;
|
|
m_nstraightPath = 0;
|
|
}
|
|
|
|
return path;
|
|
}
|