From 47e867252f5099495722b08a72af7758226b4799 Mon Sep 17 00:00:00 2001 From: shenlei Date: Tue, 13 Oct 2026 12:38:11 +0900 Subject: [PATCH] Terrain: stream the 3x3 blocks around the player CMapOutdoor keeps the 3x3 terrain blocks around the player loaded and reloads them on a block change; we loaded only the warp block, so the map ended at its edge. Stitch the set into one TerrainHeightmap, fix the attr.atr cell mapping, and reload terrain, areadata and water per block from CPythonBackground::Update. Co-Authored-By: Claude Opus 5.5 --- docs/PORT-PLAN.md | 22 +++ src/app/app_loop.cpp | 2 +- src/eter_render/TerrainHeightmap.cpp | 203 ++++++++++++++++++++------- src/eter_render/TerrainHeightmap.h | 23 ++- src/scripting/python_background.cpp | 107 +++++++++----- src/scripting/python_background.h | 9 +- tests/ava2_render_3d_test.cpp | 8 +- tests/background_test.cpp | 5 + 8 files changed, 280 insertions(+), 99 deletions(-) diff --git a/docs/PORT-PLAN.md b/docs/PORT-PLAN.md index d9e127ba..215810bd 100644 --- a/docs/PORT-PLAN.md +++ b/docs/PORT-PLAN.md @@ -1674,3 +1674,25 @@ All four issues reproduced on desktop too (`--phone ... --phase select|game --pr - Desktop ctest 59/59. Device (OnePlus PJZ110): select screen shows the warrior and ninja in their intro idle with heads; the game preview shows full-geometry trees and no terrain seam. The preview player stands inside a pine, so its head was not visible in the game shot. + +### 2026-10-13 Terrain: load the 3x3 blocks around the player + +On the real server (Android, "AVA2 General" at 412,760) the terrain stopped at a block edge, +with empty sky beyond it. `CPythonBackground::LoadMap` loaded only the block under the warp +point and never loaded another one. The original `CMapOutdoor::Update`/`LoadTerrain` +(`LOAD_SIZE_WIDTH` 1) keeps the 3x3 terrains and areas around the player's terrain coordinate, +clamped to `MapSize`, and reloads them when that coordinate changes. + +- `TerrainHeightmap::LoadBlockDirs` stitches up to 3x3 blocks into one heightmap: + - heights are (128n+3)², tiles (256n+2)², attr 256n²; neighbours share their border samples; + - the patch grid is 8 patches per block, and the quadtree is built over the patch ranges; + - patches of blocks with no `height.raw` are skipped. + The active-terrain pointer stays the same, so height, attribute and actor queries work as before. +- `GetAttrAtWorld` now maps attr.atr the way the files are laid out: 256 cells of 100 per block, + starting at the block origin (raw index 1). Before, the 256 cells were stretched over the + 130-cell bordered height grid. +- `CPythonBackground::Update(x, y, z)` takes map x, map y and height (as `app_update_game` passes + them; the app_loop call had y and z swapped). When the terrain coordinate changes it reloads + the set: terrain, every block's `areadata.txt`, and one water surface per block. The object + loading centre now uses map y instead of height. +- Desktop ctest 59/59 (background_test checks a 2x2 stitch: heights, attributes, and Update with no block change). diff --git a/src/app/app_loop.cpp b/src/app/app_loop.cpp index 07ae7473..f80ec117 100644 --- a/src/app/app_loop.cpp +++ b/src/app/app_loop.cpp @@ -2293,7 +2293,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); #ifdef AVA2_HAS_PYTHON bool bg_rendered = false; if (ava2::CPythonBackground::Instance().IsMapReady()) { - ava2::CPythonBackground::Instance().Update(player_world_x, player_world_y, player_world_z); + ava2::CPythonBackground::Instance().Update(player_world_x, player_world_z, player_world_y); float camPos[3] = {eyeX, eyeY, eyeZ}; ava2::CPythonBackground::Instance().BuildDrawCommands(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), camPos, draws, &draw_refs); bg_rendered = true; diff --git a/src/eter_render/TerrainHeightmap.cpp b/src/eter_render/TerrainHeightmap.cpp index 76f75b14..07a1511b 100644 --- a/src/eter_render/TerrainHeightmap.cpp +++ b/src/eter_render/TerrainHeightmap.cpp @@ -109,6 +109,7 @@ bool TerrainHeightmap::LoadFromBuffer(const uint16_t* raw_data, size_t count, fl m_width = RAW_XSIZE; m_height = RAW_YSIZE; + m_patch_count_x = m_patch_count_z = PATCH_COUNT; m_cell_scale = cell_scale; m_height_scale = height_scale; @@ -129,6 +130,114 @@ bool TerrainHeightmap::LoadFromBuffer(const uint16_t* raw_data, size_t count, fl return true; } +namespace { +template +bool ReadExact(const std::string& path, std::vector& out, size_t count, size_t skip = 0) { + std::ifstream file(path, std::ios::binary); + if (!file) return false; + file.seekg(0, std::ios::end); + const auto size = static_cast(file.tellg()); + if (size < skip + count * sizeof(T)) return false; + file.seekg(static_cast(skip), std::ios::beg); + out.resize(count); + return static_cast(file.read(reinterpret_cast(out.data()), static_cast(count * sizeof(T)))); +} +} // namespace + +bool TerrainHeightmap::LoadBlockDirs(int nx, int nz, const std::vector& dirs, float origin_x, float origin_z, + float height_scale, float cell_scale) { + if (nx < 1 || nz < 1 || dirs.size() != static_cast(nx * nz)) return false; + m_patchDrawCache.clear(); + m_cell_scale = cell_scale; + m_height_scale = height_scale; + m_width = MAP_XSIZE * nx + 3; + m_height = MAP_YSIZE * nz + 3; + m_patch_count_x = PATCH_COUNT * nx; + m_patch_count_z = PATCH_COUNT * nz; + m_has_origin = true; + m_origin_x = origin_x - cell_scale; + m_origin_z = origin_z - cell_scale; + m_heights.assign(static_cast(m_width) * m_height, 0.0f); + m_tiles.clear(); + m_attr.clear(); + m_tile_width = m_tile_height = m_attr_width = m_attr_height = 0; + std::vector tiles(static_cast(ATTRMAP_XSIZE * nx + 2) * (ATTRMAP_YSIZE * nz + 2), 0); + std::vector attr(static_cast(ATTRMAP_XSIZE * nx) * (ATTRMAP_YSIZE * nz), 0); + const int tile_w = ATTRMAP_XSIZE * nx + 2, attr_w = ATTRMAP_XSIZE * nx; + std::vector present(dirs.size(), false); + bool any_tiles = false, any_attr = false, any = false; + m_min_height = 1e9f; + m_max_height = -1e9f; + + std::vector raw; + std::vector bytes; + for (int bz = 0; bz < nz; ++bz) { + for (int bx = 0; bx < nx; ++bx) { + const auto& dir = dirs[bz * nx + bx]; + if (dir.empty() || !ReadExact(dir + "/height.raw", raw, RAW_XSIZE * RAW_YSIZE)) continue; + present[bz * nx + bx] = any = true; + // Neighbouring blocks share their border samples; each block owns raw [1, 128] inside the set + // and its outer border only on the set's edge. + const int k0x = bx == 0 ? 0 : 1, k1x = bx == nx - 1 ? RAW_XSIZE - 1 : MAP_XSIZE; + const int k0z = bz == 0 ? 0 : 1, k1z = bz == nz - 1 ? RAW_YSIZE - 1 : MAP_YSIZE; + for (int kz = k0z; kz <= k1z; ++kz) { + for (int kx = k0x; kx <= k1x; ++kx) { + const float h = static_cast(raw[kz * RAW_XSIZE + kx]) * height_scale; + m_heights[static_cast(bz * MAP_YSIZE + kz) * m_width + bx * MAP_XSIZE + kx] = h; + m_min_height = std::min(m_min_height, h); + m_max_height = std::max(m_max_height, h); + } + } + if (ReadExact(dir + "/tile.raw", bytes, TILEMAP_RAW_XSIZE * TILEMAP_RAW_YSIZE)) { + any_tiles = true; + const int t0x = bx == 0 ? 0 : 1, t1x = bx == nx - 1 ? TILEMAP_RAW_XSIZE - 1 : ATTRMAP_XSIZE; + const int t0z = bz == 0 ? 0 : 1, t1z = bz == nz - 1 ? TILEMAP_RAW_YSIZE - 1 : ATTRMAP_YSIZE; + for (int tz = t0z; tz <= t1z; ++tz) + for (int tx = t0x; tx <= t1x; ++tx) + tiles[static_cast(bz * ATTRMAP_YSIZE + tz) * tile_w + bx * ATTRMAP_XSIZE + tx] = bytes[tz * TILEMAP_RAW_XSIZE + tx]; + } + std::ifstream attr_file(dir + "/attr.atr", std::ios::binary); + if (attr_file) { + attr_file.seekg(0, std::ios::end); + const auto size = static_cast(attr_file.tellg()); + // Same header rule as LoadAttrFromBuffer: magic 2634 + 256x256, else headerless. + const size_t skip = size >= 6 + ATTRMAP_XSIZE * ATTRMAP_YSIZE ? 6 : 0; + attr_file.close(); + if (ReadExact(dir + "/attr.atr", bytes, ATTRMAP_XSIZE * ATTRMAP_YSIZE, skip)) { + any_attr = true; + for (int az = 0; az < ATTRMAP_YSIZE; ++az) + std::copy_n(bytes.begin() + az * ATTRMAP_XSIZE, ATTRMAP_XSIZE, + attr.begin() + static_cast(bz * ATTRMAP_YSIZE + az) * attr_w + bx * ATTRMAP_XSIZE); + } + } + } + } + if (!any) { + m_heights.clear(); + m_patches.clear(); + m_quadtreeRoot.reset(); + m_width = m_height = 0; + return false; + } + if (any_tiles) { + m_tile_width = tile_w; + m_tile_height = ATTRMAP_YSIZE * nz + 2; + m_tiles = std::move(tiles); + } + if (any_attr) { + m_attr_width = attr_w; + m_attr_height = ATTRMAP_YSIZE * nz; + m_attr = std::move(attr); + } + UpdateBounds(); + CalculateNormals(); + BuildPatchesAndQuadtree(); + for (auto& patch : m_patches) + patch.present = present[(patch.patch_y / PATCH_COUNT) * nx + patch.patch_x / PATCH_COUNT]; + m_quadtreeRoot = BuildQuadtreeNode(0, m_patch_count_x, 0, m_patch_count_z, 0); + return true; +} + bool TerrainHeightmap::LoadTileRaw(const std::string& tile_path) { std::ifstream file(tile_path, std::ios::binary); if (!file.is_open()) { @@ -279,6 +388,7 @@ void TerrainHeightmap::GenerateProcedural(int width, int height, float cell_scal m_patchDrawCache.clear(); m_width = std::max(width, 16); m_height = std::max(height, 16); + m_patch_count_x = m_patch_count_z = PATCH_COUNT; m_cell_scale = cell_scale; m_height_scale = 1.0f; @@ -449,10 +559,11 @@ uint8_t TerrainHeightmap::GetAttrAtGrid(int x, int y) const { uint8_t TerrainHeightmap::GetAttrAtWorld(float world_x, float world_z) const { if (m_attr.empty() || m_attr_width <= 0 || m_attr_height <= 0 || m_width < 2 || m_height < 2) return 0; - float grid_x = (world_x - m_world_min_x) / (static_cast(m_width - 1) * m_cell_scale) * static_cast(m_attr_width - 1); - float grid_z = (world_z - m_world_min_z) / (static_cast(m_height - 1) * m_cell_scale) * static_cast(m_attr_height - 1); - int ax = std::clamp(static_cast(std::round(grid_x)), 0, m_attr_width - 1); - int az = std::clamp(static_cast(std::round(grid_z)), 0, m_attr_height - 1); + // attr.atr has no border: cell 0 starts at the block origin (raw index 1), half a height cell wide. + float grid_x = (world_x - m_world_min_x - m_cell_scale) / (m_cell_scale * 0.5f); + float grid_z = (world_z - m_world_min_z - m_cell_scale) / (m_cell_scale * 0.5f); + int ax = std::clamp(static_cast(std::floor(grid_x)), 0, m_attr_width - 1); + int az = std::clamp(static_cast(std::floor(grid_z)), 0, m_attr_height - 1); return m_attr[az * m_attr_width + ax]; } @@ -463,14 +574,14 @@ bool TerrainHeightmap::IsAttrOn(float world_x, float world_z, uint8_t flag) cons void TerrainHeightmap::BuildPatchesAndQuadtree() { if (m_width < 16 || m_height < 16) return; - m_patches.resize(PATCH_COUNT * PATCH_COUNT); + m_patches.assign(static_cast(m_patch_count_x) * m_patch_count_z, TerrainPatch{}); - int patch_cells_x = (m_width - 1) / PATCH_COUNT; - int patch_cells_z = (m_height - 1) / PATCH_COUNT; + int patch_cells_x = (m_width - 1) / m_patch_count_x; + int patch_cells_z = (m_height - 1) / m_patch_count_z; - for (int py = 0; py < PATCH_COUNT; ++py) { - for (int px = 0; px < PATCH_COUNT; ++px) { - int p_idx = py * PATCH_COUNT + px; + for (int py = 0; py < m_patch_count_z; ++py) { + for (int px = 0; px < m_patch_count_x; ++px) { + int p_idx = py * m_patch_count_x + px; auto& patch = m_patches[p_idx]; patch.patch_x = px; patch.patch_y = py; @@ -518,56 +629,44 @@ void TerrainHeightmap::BuildPatchesAndQuadtree() { } } - m_quadtreeRoot = BuildQuadtreeNode(m_world_min_x, m_world_max_x, m_world_min_z, m_world_max_z, 0); + m_quadtreeRoot = BuildQuadtreeNode(0, m_patch_count_x, 0, m_patch_count_z, 0); } -std::unique_ptr TerrainHeightmap::BuildQuadtreeNode(float min_x, float max_x, float min_z, float max_z, int depth) { +std::unique_ptr TerrainHeightmap::BuildQuadtreeNode(int px0, int px1, int pz0, int pz1, int depth) { + if (px0 >= px1 || pz0 >= pz1) return nullptr; auto node = std::make_unique(); node->depth = depth; - node->aabb_min[0] = min_x; - node->aabb_max[0] = max_x; - node->aabb_min[2] = min_z; - node->aabb_max[2] = max_z; - node->aabb_min[1] = m_min_height; - node->aabb_max[1] = m_max_height; - - if (depth >= 3) { - float cx = (min_x + max_x) * 0.5f; - float cz = (min_z + max_z) * 0.5f; - - float u = (cx - m_world_min_x) / (m_world_max_x - m_world_min_x); - float v = (cz - m_world_min_z) / (m_world_max_z - m_world_min_z); - int px = std::clamp(static_cast(u * static_cast(PATCH_COUNT)), 0, PATCH_COUNT - 1); - int py = std::clamp(static_cast(v * static_cast(PATCH_COUNT)), 0, PATCH_COUNT - 1); - int idx = py * PATCH_COUNT + px; + if (px1 - px0 == 1 && pz1 - pz0 == 1) { + const int idx = pz0 * m_patch_count_x + px0; + const auto& patch = m_patches[idx]; + if (!patch.present) return nullptr; node->patch_idx = idx; - if (idx < static_cast(m_patches.size())) { - node->aabb_min[1] = m_patches[idx].aabb_min[1]; - node->aabb_max[1] = m_patches[idx].aabb_max[1]; - } + std::copy_n(patch.aabb_min, 3, node->aabb_min); + std::copy_n(patch.aabb_max, 3, node->aabb_max); return node; } - float mid_x = (min_x + max_x) * 0.5f; - float mid_z = (min_z + max_z) * 0.5f; + const int mid_x = (px0 + px1 + 1) / 2, mid_z = (pz0 + pz1 + 1) / 2; + node->children[0] = BuildQuadtreeNode(px0, mid_x, pz0, mid_z, depth + 1); + node->children[1] = BuildQuadtreeNode(mid_x, px1, pz0, mid_z, depth + 1); + node->children[2] = BuildQuadtreeNode(px0, mid_x, mid_z, pz1, depth + 1); + node->children[3] = BuildQuadtreeNode(mid_x, px1, mid_z, pz1, depth + 1); - node->children[0] = BuildQuadtreeNode(min_x, mid_x, min_z, mid_z, depth + 1); - node->children[1] = BuildQuadtreeNode(mid_x, max_x, min_z, mid_z, depth + 1); - node->children[2] = BuildQuadtreeNode(min_x, mid_x, mid_z, max_z, depth + 1); - node->children[3] = BuildQuadtreeNode(mid_x, max_x, mid_z, max_z, depth + 1); - - float cmin_y = 1e9f, cmax_y = -1e9f; - for (int k = 0; k < 4; ++k) { - if (node->children[k]) { - cmin_y = std::min(cmin_y, node->children[k]->aabb_min[1]); - cmax_y = std::max(cmax_y, node->children[k]->aabb_max[1]); + bool any = false; + for (int axis = 0; axis < 3; ++axis) { + node->aabb_min[axis] = 1e30f; + node->aabb_max[axis] = -1e30f; + } + for (const auto& child : node->children) { + if (!child) continue; + any = true; + for (int axis = 0; axis < 3; ++axis) { + node->aabb_min[axis] = std::min(node->aabb_min[axis], child->aabb_min[axis]); + node->aabb_max[axis] = std::max(node->aabb_max[axis], child->aabb_max[axis]); } } - node->aabb_min[1] = cmin_y; - node->aabb_max[1] = cmax_y; - - return node; + return any ? std::move(node) : nullptr; } void TerrainHeightmap::QueryQuadtree(const TerrainQuadNode* node, const float planes[6][4], const float eye[3], float max_distance, std::vector& out_visible_patches) const { @@ -623,7 +722,7 @@ void TerrainHeightmap::AppendCulledDrawRefs(const float view[16], const float pr }; std::vector visible_patches; - visible_patches.reserve(64); + visible_patches.reserve(m_patches.size()); QueryQuadtree(m_quadtreeRoot.get(), planes, eye, max_distance, visible_patches); for (int p_idx : visible_patches) { @@ -656,7 +755,7 @@ Render3DDraw TerrainHeightmap::BuildPatchDraw(int patch_x, int patch_y, int step Render3DDraw draw; if (m_heights.empty() || m_width < 2 || m_height < 2) return draw; - int p_idx = patch_y * PATCH_COUNT + patch_x; + int p_idx = patch_y * m_patch_count_x + patch_x; uint8_t dom_tile = 4; if (p_idx >= 0 && p_idx < static_cast(m_patches.size())) { dom_tile = m_patches[p_idx].dominant_tile; @@ -666,8 +765,8 @@ Render3DDraw TerrainHeightmap::BuildPatchDraw(int patch_x, int patch_y, int step draw.cull_mode = 1; // D3DCULL_NONE: double-sided for terrain meshes step = std::max(1, step); - int patch_cells_x = (m_width - 1) / PATCH_COUNT; - int patch_cells_z = (m_height - 1) / PATCH_COUNT; + int patch_cells_x = (m_width - 1) / m_patch_count_x; + int patch_cells_z = (m_height - 1) / m_patch_count_z; int start_x = patch_x * patch_cells_x; int end_x = std::min((patch_x + 1) * patch_cells_x, m_width - 1); diff --git a/src/eter_render/TerrainHeightmap.h b/src/eter_render/TerrainHeightmap.h index c9f3e498..fc700ff0 100644 --- a/src/eter_render/TerrainHeightmap.h +++ b/src/eter_render/TerrainHeightmap.h @@ -20,8 +20,9 @@ enum TerrainAttr : uint8_t { }; struct TerrainPatch { - int patch_x = 0; // 0..7 - int patch_y = 0; // 0..7 + int patch_x = 0; // 0..patch count - 1 + int patch_y = 0; + bool present = true; // false where a stitched block had no height.raw float aabb_min[3] = {0}; float aabb_max[3] = {0}; uint8_t dominant_tile = 0; @@ -31,12 +32,11 @@ struct TerrainQuadNode { float aabb_min[3] = {0}; float aabb_max[3] = {0}; int depth = 0; - int patch_idx = -1; // -1 if internal, >=0 if leaf (0..63) + int patch_idx = -1; // -1 if internal, >=0 if leaf std::unique_ptr children[4]; }; -// Height/attribute query surface shared by a single TerrainHeightmap and the -// 3x3 CMapOutdoor block set (game_scene.cpp) so actors walk on whichever is active. +// Height/attribute query surface actors walk on. class TerrainHeightSource { public: virtual ~TerrainHeightSource() = default; @@ -88,6 +88,12 @@ public: bool LoadAttr(const std::string& attr_path); bool LoadAttrFromBuffer(const uint8_t* attr_data, size_t count); + // CMapOutdoor::Update loads the (up to) 3x3 CTerrain blocks around the player. Stitch them into + // one heightmap: dirs is nx*nz block directories, row-major by z, "" for a block without data. + // Each block's raw index 1 sits on its pixel origin; origin_x/z is the first block's origin. + bool LoadBlockDirs(int nx, int nz, const std::vector& dirs, float origin_x, float origin_z, + float height_scale = DEFAULT_HEIGHT_SCALE, float cell_scale = DEFAULT_CELL_SCALE); + // Load TextureSet file (e.g. textureset/metin2_map1.txt) bool LoadTextureSet(const std::string& textureset_path); bool LoadTextureSetFromString(const std::string& content); @@ -158,7 +164,7 @@ private: void CalculateNormals(); void UpdateBounds(); void BuildPatchesAndQuadtree(); - std::unique_ptr BuildQuadtreeNode(float min_x, float max_x, float min_z, float max_z, int depth); + std::unique_ptr BuildQuadtreeNode(int px0, int px1, int pz0, int pz1, int depth); void QueryQuadtree(const TerrainQuadNode* node, const float planes[6][4], const float eye[3], float max_distance, std::vector& out_visible_patches) const; int m_width = 0; @@ -172,6 +178,9 @@ private: float m_min_height = 0.0f; float m_max_height = 0.0f; + int m_patch_count_x = PATCH_COUNT; + int m_patch_count_z = PATCH_COUNT; + bool m_has_origin = false; float m_origin_x = 0.0f; float m_origin_z = 0.0f; @@ -190,7 +199,7 @@ private: std::unordered_map m_textureSet; // tile_id -> texture dds path mutable std::map, std::vector> m_patchDrawCache; - std::vector m_patches; // 8x8 = 64 patches + std::vector m_patches; // m_patch_count_x * m_patch_count_z (8x8 per block) std::unique_ptr m_quadtreeRoot; }; diff --git a/src/scripting/python_background.cpp b/src/scripting/python_background.cpp index 94bac85f..c8c53a83 100644 --- a/src/scripting/python_background.cpp +++ b/src/scripting/python_background.cpp @@ -106,7 +106,9 @@ void CPythonBackground::Destroy() { Initialize(); m_terrain.reset(); m_mapObjects.reset(); - m_water.reset(); + m_waters.clear(); + m_mapBaseDir.clear(); + m_centerBlockX = m_centerBlockY = -1; m_skybox.reset(); m_forest.reset(); m_outdoorMap = OutdoorMapInfo{}; @@ -118,7 +120,6 @@ void CPythonBackground::Destroy() { void CPythonBackground::Create() { if (!m_terrain) m_terrain = std::make_unique(); if (!m_mapObjects) m_mapObjects = std::make_unique(); - if (!m_water) m_water = std::make_unique(); if (!m_skybox) m_skybox = std::make_unique(); if (!m_forest) m_forest = std::make_unique(); } @@ -209,7 +210,9 @@ bool CPythonBackground::LoadMap(const std::string& mapName, float x, float y, fl if (m_mapObjects) m_mapObjects->ClearObjects(); if (m_forest) m_forest->Clear(); - m_water = std::make_unique(); + m_waters.clear(); + m_mapBaseDir.clear(); + m_centerBlockX = m_centerBlockY = -1; for (const auto& property : CResourceManager::GetCandidatePaths("property")) { if (!std::filesystem::is_directory(property)) continue; @@ -289,31 +292,8 @@ bool CPythonBackground::LoadMap(const std::string& mapName, float x, float y, fl bool ok = false; if (std::filesystem::is_directory(block_dir)) { - std::string height_path = block_dir + "/height.raw"; - if (std::filesystem::exists(height_path)) { - ok = m_terrain->LoadRaw(height_path); - // height.raw includes a one-sample border around each 25600-unit block. - m_terrain->SetWorldOrigin(bx * 25600.0f - m_terrain->GetCellScale(), - by * 25600.0f - m_terrain->GetCellScale()); - } - std::string tile_path = block_dir + "/tile.raw"; - if (std::filesystem::exists(tile_path)) { - m_terrain->LoadTileRaw(tile_path); - } - std::string attr_path = block_dir + "/attr.atr"; - if (std::filesystem::exists(attr_path)) { - m_terrain->LoadAttr(attr_path); - } - std::string areadata_path = block_dir + "/areadata.txt"; - if (std::filesystem::exists(areadata_path) && m_mapObjects) { - m_mapObjects->LoadAreaData(areadata_path, 1.0f, m_forest.get(), true); - } - std::string water_path = block_dir + "/water.wtr"; - if (std::filesystem::exists(water_path) && m_water) { - m_water->LoadWater(water_path, m_terrain ? m_terrain->GetCellScale() : 200.0f, 0.5f, 200.0f, - m_terrain ? m_terrain->GetWorldMinX() : -2560.0f, - m_terrain ? m_terrain->GetWorldMinZ() : -2560.0f); - } + m_mapBaseDir = found_base; + ok = LoadTerrainBlocks(bx, by); } else { // Fallback: look for 000000 or any block in the directory for (const auto& entry : std::filesystem::directory_iterator(found_base)) { @@ -330,8 +310,9 @@ bool CPythonBackground::LoadMap(const std::string& mapName, float x, float y, fl m_mapObjects->LoadAreaData(adpath, 1.0f, m_forest.get(), true); } std::string wpath = entry.path().string() + "/water.wtr"; - if (std::filesystem::exists(wpath) && m_water) { - m_water->LoadWater(wpath, m_terrain ? m_terrain->GetCellScale() : 200.0f, 0.5f, 200.0f, + if (std::filesystem::exists(wpath)) { + m_waters.push_back(std::make_unique()); + m_waters.back()->LoadWater(wpath, m_terrain ? m_terrain->GetCellScale() : 200.0f, 0.5f, 200.0f, m_terrain ? m_terrain->GetWorldMinX() : -2560.0f, m_terrain ? m_terrain->GetWorldMinZ() : -2560.0f); } @@ -352,6 +333,53 @@ bool CPythonBackground::LoadMap(const std::string& mapName, float x, float y, fl return ok; } +bool CPythonBackground::LoadTerrainBlocks(int blockX, int blockY) { + // CMapOutdoor::Update/LoadTerrain: the blocks within LOAD_SIZE_WIDTH (1) of the player's + // terrain coordinate, clamped to the map's MapSize, stitched into the one active heightmap. + const int countX = std::max(1, m_outdoorMap.terrainCountX), countY = std::max(1, m_outdoorMap.terrainCountY); + if (m_outdoorMap.terrainCountX > 0) blockX = std::clamp(blockX, 0, countX - 1); + if (m_outdoorMap.terrainCountY > 0) blockY = std::clamp(blockY, 0, countY - 1); + const int x0 = m_outdoorMap.terrainCountX > 0 ? std::max(0, blockX - 1) : blockX; + const int x1 = m_outdoorMap.terrainCountX > 0 ? std::min(countX - 1, blockX + 1) : blockX; + const int y0 = m_outdoorMap.terrainCountY > 0 ? std::max(0, blockY - 1) : blockY; + const int y1 = m_outdoorMap.terrainCountY > 0 ? std::min(countY - 1, blockY + 1) : blockY; + const int nx = x1 - x0 + 1, ny = y1 - y0 + 1; + + std::vector dirs; + for (int by = y0; by <= y1; ++by) { + for (int bx = x0; bx <= x1; ++bx) { + char block_str[32]; + std::snprintf(block_str, sizeof(block_str), "%03d%03d", bx, by); + std::string dir = m_mapBaseDir + "/" + block_str; + dirs.push_back(std::filesystem::is_directory(dir) ? dir : std::string()); + } + } + if (!m_terrain) m_terrain = std::make_unique(); + const bool ok = m_terrain->LoadBlockDirs(nx, ny, dirs, x0 * 25600.0f, y0 * 25600.0f); + m_centerBlockX = blockX; + m_centerBlockY = blockY; + + // CArea objects use global map coordinates, so every block's areadata adds to one set. + if (m_mapObjects) m_mapObjects->ClearObjects(); + if (m_forest) m_forest->Clear(); + m_waters.clear(); + const float cell = m_terrain->GetCellScale(); + for (size_t i = 0; i < dirs.size(); ++i) { + if (dirs[i].empty()) continue; + const std::string areadata_path = dirs[i] + "/areadata.txt"; + if (m_mapObjects && std::filesystem::exists(areadata_path)) + m_mapObjects->LoadAreaData(areadata_path, 1.0f, m_forest.get(), true); + const std::string water_path = dirs[i] + "/water.wtr"; + if (std::filesystem::exists(water_path)) { + const int bx = x0 + static_cast(i) % nx, by = y0 + static_cast(i) / nx; + auto water = std::make_unique(); + if (water->LoadWater(water_path, cell, 0.5f, 200.0f, bx * 25600.0f - cell, by * 25600.0f - cell)) + m_waters.push_back(std::move(water)); + } + } + return ok; +} + bool CPythonBackground::Warp(uint32_t x, uint32_t y) { m_terrain.reset(); m_strMapName.clear(); @@ -591,10 +619,20 @@ void CPythonBackground::DeleteSpecialEffect(uint32_t dwID) { } void CPythonBackground::Update(float fCenterX, float fCenterY, float fCenterZ) { - (void)fCenterY; + // (x, y, z) = map x, map y, height, as CPythonApplication passes the main actor's position. + (void)fCenterZ; + if (!m_mapBaseDir.empty()) { + // CMapOutdoor::Update: reload the 3x3 set when the player's terrain coordinate changes. + const int blockX = static_cast(std::fabs(fCenterX) / 25600.0f); + const int blockY = static_cast(std::fabs(fCenterY) / 25600.0f); + const int countX = m_outdoorMap.terrainCountX, countY = m_outdoorMap.terrainCountY; + const int clampedX = countX > 0 ? std::clamp(blockX, 0, countX - 1) : blockX; + const int clampedY = countY > 0 ? std::clamp(blockY, 0, countY - 1) : blockY; + if (clampedX != m_centerBlockX || clampedY != m_centerBlockY) LoadTerrainBlocks(clampedX, clampedY); + } if (m_mapObjects) { // CArea::GetMaxLoadingDistanceSqr: OBJECT_DIST, never under 5000. - m_mapObjects->SetLoadingCenter(fCenterX, fCenterZ, std::max(5000.0f, CPythonSystem::Instance().GetObjectDistance())); + m_mapObjects->SetLoadingCenter(fCenterX, fCenterY, std::max(5000.0f, CPythonSystem::Instance().GetObjectDistance())); } if (m_skybox) { m_skybox->Update(GetGameFrameDelta()); @@ -684,8 +722,9 @@ void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* } } - if (m_bVisiblePart[PART_WATER] && m_water && m_water->HasWater()) { - auto wd = m_water->BuildDrawCommand(0.0f); + for (const auto& water : m_waters) { + if (!m_bVisiblePart[PART_WATER] || !water->HasWater()) continue; + auto wd = water->BuildDrawCommand(0.0f); if (m_pCurrentEnvironment) { wd.light_direction[0] = m_pCurrentEnvironment->dir_light.direction[0]; wd.light_direction[1] = m_pCurrentEnvironment->dir_light.direction[1]; diff --git a/src/scripting/python_background.h b/src/scripting/python_background.h index d4491024..2c1a098b 100644 --- a/src/scripting/python_background.h +++ b/src/scripting/python_background.h @@ -201,7 +201,7 @@ public: TerrainHeightmap* GetTerrain() { return m_terrain.get(); } MapObjectManager* GetMapObjects() { return m_mapObjects.get(); } - WaterManager* GetWater() { return m_water.get(); } + WaterManager* GetWater() { return m_waters.empty() ? nullptr : m_waters.front().get(); } SkyBox* GetSkyBox() { return m_skybox.get(); } SpeedTreeForest* GetForest() { return m_forest.get(); } const OutdoorMapInfo* GetOutdoorMap() const { return m_strMapName.empty() ? nullptr : &m_outdoorMap; } @@ -247,11 +247,16 @@ private: std::unique_ptr m_terrain; std::unique_ptr m_mapObjects; - std::unique_ptr m_water; + std::vector> m_waters; // one per loaded terrain block std::unique_ptr m_skybox; std::unique_ptr m_forest; OutdoorMapInfo m_outdoorMap; + // CMapOutdoor terrain streaming: map directory and the block the 3x3 set is centred on. + std::string m_mapBaseDir; + int m_centerBlockX = -1; + int m_centerBlockY = -1; + bool LoadTerrainBlocks(int blockX, int blockY); std::map m_targetEffects; std::map m_targetEffectsShop; diff --git a/tests/ava2_render_3d_test.cpp b/tests/ava2_render_3d_test.cpp index 1bd23a6e..57649e3b 100644 --- a/tests/ava2_render_3d_test.cpp +++ b/tests/ava2_render_3d_test.cpp @@ -1232,9 +1232,11 @@ int main(int argc, char** argv) { if (block_gx >= 0) { std::cout << "Found ATTR_BLOCK boundary at grid (" << block_gx << ", " << block_gy << ")" << std::endl; - float b_world_x = pr_terrain.GetWorldMinX() + (static_cast(block_gx) / (pr_terrain.GetAttrWidth() - 1)) * (pr_terrain.GetWorldMaxX() - pr_terrain.GetWorldMinX()); - float b_world_z = pr_terrain.GetWorldMinZ() + (static_cast(block_gy) / (pr_terrain.GetAttrHeight() - 1)) * (pr_terrain.GetWorldMaxZ() - pr_terrain.GetWorldMinZ()); - float walk_z = pr_terrain.GetWorldMinZ() + (static_cast(block_gy - 1) / (pr_terrain.GetAttrHeight() - 1)) * (pr_terrain.GetWorldMaxZ() - pr_terrain.GetWorldMinZ()); + // attr.atr cells are half a height cell, starting at the block origin one height cell in. + const float attr_cell = pr_terrain.GetCellScale() * 0.5f; + float b_world_x = pr_terrain.GetWorldMinX() + pr_terrain.GetCellScale() + (static_cast(block_gx) + 0.5f) * attr_cell; + float b_world_z = pr_terrain.GetWorldMinZ() + pr_terrain.GetCellScale() + (static_cast(block_gy) + 0.5f) * attr_cell; + float walk_z = pr_terrain.GetWorldMinZ() + pr_terrain.GetCellScale() + (static_cast(block_gy - 1) + 0.5f) * attr_cell; assert(pr_terrain.IsAttrOn(b_world_x, b_world_z, ava2::TerrainAttr::ATTR_BLOCK)); assert(!pr_terrain.IsAttrOn(b_world_x, walk_z, ava2::TerrainAttr::ATTR_BLOCK)); diff --git a/tests/background_test.cpp b/tests/background_test.cpp index 4454d086..69f41adc 100644 --- a/tests/background_test.cpp +++ b/tests/background_test.cpp @@ -167,6 +167,11 @@ int main(int argc, char** argv) { "assert bg.IsAttrOn(0, 0, bg.ATTR_BLOCK) == 1\n" "bg.WarpTest(1284, 2308)\n" "assert bg.GetHeight(26000.0, 26000.0) == 209.0, bg.GetHeight(26000.0, 26000.0)\n" + "assert bg.GetHeight(0.0, 0.0) == 100.0, bg.GetHeight(0.0, 0.0)\n" + "assert bg.IsAttrOn(0, 0, bg.ATTR_BLOCK) == 1\n" + "assert bg.IsAttrOn(26000, 26000, bg.ATTR_BLOCK) == 0\n" + "bg.Update(400.0, 400.0, 0.0)\n" + "assert bg.GetHeight(26000.0, 26000.0) == 209.0, bg.GetHeight(26000.0, 26000.0)\n" "\n" "bg.Destroy()\n" "assert bg.GetCurrentMapName() == ''\n"