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 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-13 12:38:11 +09:00
co-authored by Claude Opus 5.5
parent 29f33b289a
commit 47e867252f
8 changed files with 280 additions and 99 deletions
+73 -34
View File
@@ -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<TerrainHeightmap>();
if (!m_mapObjects) m_mapObjects = std::make_unique<MapObjectManager>();
if (!m_water) m_water = std::make_unique<WaterManager>();
if (!m_skybox) m_skybox = std::make_unique<SkyBox>();
if (!m_forest) m_forest = std::make_unique<SpeedTreeForest>();
}
@@ -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<WaterManager>();
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<WaterManager>());
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<int>(1, m_outdoorMap.terrainCountX), countY = std::max<int>(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<std::string> 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<TerrainHeightmap>();
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<int>(i) % nx, by = y0 + static_cast<int>(i) / nx;
auto water = std::make_unique<WaterManager>();
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<int>(std::fabs(fCenterX) / 25600.0f);
const int blockY = static_cast<int>(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];
+7 -2
View File
@@ -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<TerrainHeightmap> m_terrain;
std::unique_ptr<MapObjectManager> m_mapObjects;
std::unique_ptr<WaterManager> m_water;
std::vector<std::unique_ptr<WaterManager>> m_waters; // one per loaded terrain block
std::unique_ptr<SkyBox> m_skybox;
std::unique_ptr<SpeedTreeForest> 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<uint32_t, uint32_t> m_targetEffects;
std::map<uint32_t, uint32_t> m_targetEffectsShop;