18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

refactor(scene): the region keeps its sections in a map by row and column, not in a grid of 500 x 500

Step 3a of docs/world-format-eu07.md (#516, section 13): the dense std::array of 250 000 section pointers
gives way to a std::map keyed by section_key (row and column, counted as before with the middle of the region
at 250, 250), holding only the sections that hold something. The map runs row by row, the order of the index
of the classic square, so the .sbt and .btf files keep their indices and come out byte for byte the same.

basic_region gains find_section(key or row and column) and all_sections(); get_section(index) stays for the
classic index. The renderer's walk, its prefetch and memory count, the local lights, the map, the terrain
files, the editor and its self-test use them instead of indexing the grid; the loops over all 250 000 slots
(the map every frame, the editor's terrain drop) go over the sections there are.

Nothing changes in what the game does: the region still ends at +-250 km, a point beyond it still goes to the
section at the edge, and the centre of a new section is computed from the row and column before that clamp,
as it was. Lifting the limit is a later step, behind a switch.

Checked in the game (td.scn, Glinojeck.scn, --test 120 s, seed 1): the physics logs are the same bit for bit
as before the change, with the .sbt written and with it read back, and the .sbt files written are byte for
byte the same as those of the build before.
This commit is contained in:
maj00r
2026-12-02 23:20:42 +01:00
parent ed8708c5e2
commit 602b0bdcf7
9 changed files with 107 additions and 72 deletions
+1 -4
View File
@@ -3036,11 +3036,8 @@ namespace
dropped += drop ? 1 : 0;
return drop;
};
for (std::size_t i = 0; i < sq(scene::EU07_REGIONSIDESECTIONCOUNT); ++i)
for (auto const &[key, section] : simulation::Region->all_sections())
{
auto *section = simulation::Region->get_section(i);
if (section == nullptr)
continue;
section->m_shapes.erase(std::remove_if(section->m_shapes.begin(), section->m_shapes.end(), dropping), section->m_shapes.end());
for (auto &cell : section->m_cells)
{
+1 -3
View File
@@ -579,10 +579,8 @@ editor_mode::selftest_result editor_mode::selftest_terrain(std::string const &co
// models the cells hold which aren't in the scene any more, or are about to be destroyed
std::set<TAnimModel const *> const alive{simulation::Instances.sequence().begin(), simulation::Instances.sequence().end()};
std::size_t held{0}, gone{0}, retired{0};
for (auto const *section : simulation::Region->m_sections)
for (auto const &[key, section] : simulation::Region->all_sections())
{
if (section == nullptr)
continue;
for (auto const &cell : section->m_cells)
for (auto const *sequence : {&cell.m_instancesopaque, &cell.m_instancetranslucent})
for (auto const *instance : *sequence)
+1 -1
View File
@@ -233,7 +233,7 @@ void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Re
{
for (int column = std::max(0, camerax - reach); column <= std::min(scene::EU07_REGIONSIDESECTIONCOUNT - 1, camerax + reach); ++column)
{
auto *section{Region.m_sections[row * scene::EU07_REGIONSIDESECTIONCOUNT + column]};
auto *section{Region.find_section(row, column)};
if (section == nullptr)
{
continue;
+1 -1
View File
@@ -355,7 +355,7 @@ void scene_walk::Render(scene::basic_region *Region)
{
break;
}
auto *section{Region->m_sections[row * scene::EU07_REGIONSIDESECTIONCOUNT + column]};
auto *section{Region->find_section(row, column)};
if ((section != nullptr) && (m_renderpass.pass_camera.visible(section->m_area)))
{
m_sectionqueue.emplace_back(section);
+2 -3
View File
@@ -1336,8 +1336,7 @@ void opengl46_renderer::Update_Memory_Info()
auto const shape = [ &shapes ]( scene::shape_node const &Shape ) {
++shapes.count;
shapes.bytes += memory_of( Shape.data().vertices ) + memory_of( Shape.data().userdata ); };
for( auto const *section : simulation::Region->m_sections ) {
if( section == nullptr ) { continue; }
for( auto const &[ key, section ] : simulation::Region->all_sections() ) {
for( auto const &item : section->m_shapes ) { shape( item ); }
for( auto const &cell : section->m_cells ) {
for( auto const &item : cell.m_shapesopaque ) { shape( item ); }
@@ -5024,7 +5023,7 @@ void opengl46_renderer::Update_Texture_Prefetch()
{
continue;
}
auto *section{region->m_sections[row * scene::EU07_REGIONSIDESECTIONCOUNT + column]};
auto *section{region->find_section(row, column)};
if (section == nullptr)
{
continue;
+63 -46
View File
@@ -1209,14 +1209,48 @@ basic_section::cell( glm::dvec3 const &Location, const glm::ivec2 &offset ) {
basic_region::basic_region() {
section_key
section_key_at( glm::dvec3 const &Location ) {
m_sections.fill( nullptr );
return {
static_cast<std::int32_t>( std::floor( Location.z / EU07_SECTIONSIZE + EU07_REGIONSIDESECTIONCOUNT / 2 ) ),
static_cast<std::int32_t>( std::floor( Location.x / EU07_SECTIONSIZE + EU07_REGIONSIDESECTIONCOUNT / 2 ) ) };
}
glm::dvec3
section_center( section_key const Key ) {
auto const centeroffset = -( EU07_REGIONSIDESECTIONCOUNT / 2 * EU07_SECTIONSIZE ) + EU07_SECTIONSIZE / 2;
glm::dvec3 const regioncornercenter { centeroffset, 0, centeroffset };
return regioncornercenter + glm::dvec3{ Key.column * EU07_SECTIONSIZE, 0.0, Key.row * EU07_SECTIONSIZE };
}
section_key
classic_section_key( std::uint32_t const Index ) {
return {
static_cast<std::int32_t>( Index / EU07_REGIONSIDESECTIONCOUNT ),
static_cast<std::int32_t>( Index % EU07_REGIONSIDESECTIONCOUNT ) };
}
std::uint32_t
classic_section_index( section_key const Key ) {
return static_cast<std::uint32_t>( Key.row * EU07_REGIONSIDESECTIONCOUNT + Key.column );
}
basic_region::basic_region() = default;
basic_region::~basic_region() {
for( auto *section : m_sections ) { if( section != nullptr ) { delete section; } }
for( auto const &[ key, section ] : m_sections ) { delete section; }
}
basic_section *
basic_region::find_section( section_key const Key ) const {
auto const found { m_sections.find( Key ) };
return found != m_sections.end() ? found->second : nullptr;
}
// potentially activates event handler with the same name as provided node, and within handler activation range
@@ -1312,7 +1346,7 @@ basic_region::is_scene( std::string const &Scenariofile ) const {
// file type and version check, and whether the sections add up to the whole file. a file cut short (by a write
// interrupted with an older version, or a copy) is caught here, while the scenery can still be loaded from the text
std::ifstream input( filename, std::ios::binary );
if( auto const layout { region_file::check_layout( input, EU07_FILEHEADER, EU07_FILEVERSION_REGION, static_cast<std::uint32_t>( m_sections.size() ) ) };
if( auto const layout { region_file::check_layout( input, EU07_FILEHEADER, EU07_FILEVERSION_REGION, classic_section_count ) };
false == layout.empty() ) {
WriteLog( "Bad file: \"" + filename + "\" " + layout + ", ignored" );
return false;
@@ -1337,21 +1371,13 @@ basic_region::serialize( std::string const &Scenariofile ) const {
sn_utils::ls_uint32( output, EU07_FILEVERSION_REGION );
// sections
// TBD, TODO: build table of sections and file offsets, if we postpone section loading until they're within range
std::uint32_t sectioncount { 0 };
for( auto *section : m_sections ) {
if( section != nullptr ) {
++sectioncount;
}
}
auto const sectioncount { static_cast<std::uint32_t>( m_sections.size() ) };
// section count, followed by section data
sn_utils::ls_uint32( output, sectioncount );
std::uint32_t sectionindex { 0 };
for( auto *section : m_sections ) {
for( auto const &[ key, section ] : m_sections ) {
// section data: section index, followed by length of section data, followed by section data
if( section != nullptr ) {
sn_utils::ls_uint32( output, sectionindex );
section->serialize( output ); }
++sectionindex;
sn_utils::ls_uint32( output, classic_section_index( key ) );
section->serialize( output );
} } ) };
if( false == error.empty() ) {
ErrorLog( "Bad file: binary version of the scenery wasn't written, " + error );
@@ -1372,7 +1398,7 @@ basic_region::deserialize( std::string const &Scenariofile ) {
// region file version 1
// file type and version check, and whether the sections add up to the whole file
std::ifstream input( filename, std::ios::binary );
auto const sectionlimit { static_cast<std::uint32_t>( m_sections.size() ) };
auto const sectionlimit { classic_section_count };
auto error { region_file::check_layout( input, EU07_FILEHEADER, EU07_FILEVERSION_REGION, sectionlimit ) };
if( false == error.empty() ) {
ErrorLog( "Bad file: \"" + filename + "\" " + error + ", the geometry it holds is missing" );
@@ -1401,11 +1427,12 @@ basic_region::deserialize( std::string const &Scenariofile ) {
return false;
}
for( auto &[ sectionindex, section ] : sections ) {
if( m_sections[ sectionindex ] == nullptr ) {
m_sections[ sectionindex ] = section.release();
auto &target { m_sections[ classic_section_key( sectionindex ) ] };
if( target == nullptr ) {
target = section.release();
}
else {
m_sections[ sectionindex ]->merge( std::move( *section ) );
target->merge( std::move( *section ) );
}
}
@@ -1430,11 +1457,9 @@ basic_region::discard_scene( std::string const &Scenariofile ) const {
void
basic_region::export_as_text( std::ostream &Output ) const {
for( auto *section : m_sections ) {
for( auto const &[ key, section ] : m_sections ) {
// text format export dumps only relevant basic objects from non-empty sections
if( section != nullptr ) {
section->export_as_text( Output );
}
section->export_as_text( Output );
}
}
@@ -1720,7 +1745,7 @@ basic_region::sections( glm::dvec3 const &Point, float const Radius, std::vector
if( column < 0 ) { continue; }
if( column >= EU07_REGIONSIDESECTIONCOUNT ) { break; }
auto *section { m_sections[ row * EU07_REGIONSIDESECTIONCOUNT + column ] };
auto *section { find_section( row, column ) };
if( section != nullptr
&& glm::length2(section->area().center - Point) <= sq(section->area().radius + padding + Radius) ) {
@@ -1920,21 +1945,19 @@ basic_region::RaTriangleDivider( shape_node &Shape, std::deque<shape_node> &Shap
basic_section &
basic_region::section( glm::dvec3 const &Location ) {
auto const column { static_cast<int>( std::floor( Location.x / EU07_SECTIONSIZE + EU07_REGIONSIDESECTIONCOUNT / 2 ) ) };
auto const row { static_cast<int>( std::floor( Location.z / EU07_SECTIONSIZE + EU07_REGIONSIDESECTIONCOUNT / 2 ) ) };
auto const located { section_key_at( Location ) };
// a point beyond the classic square goes to the section at its edge, as it always did
section_key const key {
std::clamp( located.row, 0, EU07_REGIONSIDESECTIONCOUNT - 1 ),
std::clamp( located.column, 0, EU07_REGIONSIDESECTIONCOUNT - 1 ) };
auto &section =
m_sections[
std::clamp( row, 0, EU07_REGIONSIDESECTIONCOUNT - 1 ) * EU07_REGIONSIDESECTIONCOUNT
+ std::clamp( column, 0, EU07_REGIONSIDESECTIONCOUNT - 1 ) ] ;
auto &section { m_sections[ key ] };
if( section == nullptr ) {
// there's no guarantee the section exists at this point, so check and if needed, create it
section = new basic_section();
// assign center of the section
auto const centeroffset = -( EU07_REGIONSIDESECTIONCOUNT / 2 * EU07_SECTIONSIZE ) + EU07_SECTIONSIZE / 2;
glm::dvec3 regioncornercenter { centeroffset, 0, centeroffset };
section->center( regioncornercenter + glm::dvec3{ column * EU07_SECTIONSIZE, 0.0, row * EU07_SECTIONSIZE } );
// assign center of the section. NOTE: the column and the row before the clamp, as it always was
section->center( section_center( located ) );
}
return *section;
@@ -1944,22 +1967,16 @@ void basic_region::create_map_geometry()
{
m_map_geometrybank = GfxRenderer->Create_Bank();
for (int row = 0; row < EU07_REGIONSIDESECTIONCOUNT; row++)
for (int column = 0; column < EU07_REGIONSIDESECTIONCOUNT; column++)
{
basic_section *s = m_sections[row * EU07_REGIONSIDESECTIONCOUNT + column];
if (s)
s->create_map_geometry(m_map_geometrybank);
}
for( auto const &[ key, section ] : m_sections ) {
section->create_map_geometry( m_map_geometrybank );
}
}
// loads all geometry kept in binary terrain files which wasn't needed so far
void basic_region::load_terrain()
{
for( auto *section : m_sections ) {
if( section != nullptr ) {
section->load_terrain();
}
for( auto const &[ key, section ] : m_sections ) {
section->load_terrain();
}
}
+31 -2
View File
@@ -34,6 +34,24 @@ int constexpr EU07_CELLSIZE = 250;
int constexpr EU07_SECTIONSIZE = 1000;
int constexpr EU07_REGIONSIDESECTIONCOUNT = 500; // number of sections along a side of square region
// a section of the region by its row (along z) and column (along x), counted with the middle of the region at 250, 250 as the region
// always counted them. the region keeps only the sections that hold something, in a map of these keys, ordered row by row: the same
// order as the index of the classic square of 500 x 500 sections, which the binary files (.sbt, .btf) keep
struct section_key {
std::int32_t row { 0 };
std::int32_t column { 0 };
friend constexpr auto operator<=>( section_key const &, section_key const & ) = default;
};
// the section of the point
section_key section_key_at( glm::dvec3 const &Location );
// the middle of the section
glm::dvec3 section_center( section_key const Key );
// the count of the sections of the classic square, and the index of a section in it, as the binary files keep it
std::uint32_t constexpr classic_section_count { static_cast<std::uint32_t>( EU07_REGIONSIDESECTIONCOUNT * EU07_REGIONSIDESECTIONCOUNT ) };
section_key classic_section_key( std::uint32_t const Index );
std::uint32_t classic_section_index( section_key const Key );
struct scratch_data {
struct binary_data {
@@ -588,8 +606,19 @@ public:
// loads all geometry kept in binary terrain files which wasn't needed so far
void
load_terrain();
// the section of the index of the classic square, if the region has it
basic_section* get_section(size_t section)
{ return m_sections[section]; }
{ return find_section( classic_section_key( static_cast<std::uint32_t>( section ) ) ); }
// the section of the key, if the region has it
basic_section *
find_section( section_key const Key ) const;
basic_section *
find_section( int const Row, int const Column ) const {
return find_section( section_key { Row, Column } ); }
// every section the region has, row by row
std::map<section_key, basic_section *> const &
all_sections() const {
return m_sections; }
gfx::geometrybank_handle
get_map_poi_geometry() { return m_map_poipoints; }
glm::vec3 find_nearest_track_point(const glm::dvec3 &pos)
@@ -597,7 +626,7 @@ public:
//private:
// types
using section_array = std::array<basic_section *, sq(EU07_REGIONSIDESECTIONCOUNT)>;
using section_array = std::map<section_key, basic_section *>;
struct region_scratchpad {
+4 -8
View File
@@ -491,13 +491,9 @@ bool terrain_file::write(std::string const &Text, std::string const &Binaryfile,
// sections holding any geometry
std::vector<std::uint32_t> sections;
for (std::size_t index{0}; index < region->m_sections.size(); ++index)
for (auto const &[key, section] : region->all_sections())
{
auto const *section{region->m_sections[index]};
if (section == nullptr)
{
continue;
}
auto const index{classic_section_index(key)};
if (std::any_of(std::begin(section->m_cells), std::end(section->m_cells), [](basic_cell const &Cell) { return false == Cell.m_shapestranslucent.empty(); }))
{
sections.emplace_back(static_cast<std::uint32_t>(index));
@@ -544,7 +540,7 @@ bool terrain_file::write(std::string const &Text, std::string const &Binaryfile,
std::string directory;
for (auto const sectionindex : sections)
{
auto *section{region->m_sections[sectionindex]};
auto *section{region->find_section(classic_section_key(sectionindex))};
data.clear();
put(data, EU07_TERRAINBLOCK);
put(data, sectionindex);
@@ -1006,7 +1002,7 @@ bool terrain_file::attach(std::string const &Binaryfile, basic_region &Region, b
block.offset = reader.get<std::uint64_t>();
block.size = reader.get<std::uint32_t>();
auto const radius{reader.get<float>()};
if (false == reader.good() || sectionindex >= Region.m_sections.size())
if (false == reader.good() || sectionindex >= classic_section_count)
{
break;
}
+3 -4
View File
@@ -92,12 +92,11 @@ void ui::map_panel::render_map_texture(glm::mat4 transform, glm::vec2 surface_si
m_colored_paths.highlighted.clear();
m_section_handles.clear();
for (int row = 0; row < scene::EU07_REGIONSIDESECTIONCOUNT; row++)
// every section the region has, row by row as the grid of 500 x 500 went
for (auto const &[key, section] : simulation::Region->all_sections())
{
for (int column = 0; column < scene::EU07_REGIONSIDESECTIONCOUNT; column++)
{
scene::basic_section *section = simulation::Region->get_section(row * scene::EU07_REGIONSIDESECTIONCOUNT + column);
if (section && frustum.sphere_inside(section->area().center, section->area().radius) > 0.f)
if (frustum.sphere_inside(section->area().center, section->area().radius) > 0.f)
{
const gfx::geometrybank_handle handle = section->get_map_geometry();
if (handle != null_handle)