diff --git a/application/editormode.cpp b/application/editormode.cpp index 56c49521a..b401d063f 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -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) { diff --git a/application/editormodeselftestterrain.cpp b/application/editormodeselftestterrain.cpp index ad56c3108..f3f5ebfd0 100644 --- a/application/editormodeselftestterrain.cpp +++ b/application/editormodeselftestterrain.cpp @@ -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 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) diff --git a/rendering/common/locallights.cpp b/rendering/common/locallights.cpp index 869ad015c..8cc80e435 100644 --- a/rendering/common/locallights.cpp +++ b/rendering/common/locallights.cpp @@ -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; diff --git a/rendering/common/scenewalk.cpp b/rendering/common/scenewalk.cpp index 7ea2c2bad..1f84644c6 100644 --- a/rendering/common/scenewalk.cpp +++ b/rendering/common/scenewalk.cpp @@ -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); diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index d7b1cb24f..199900740 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -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; diff --git a/scene/scene.cpp b/scene/scene.cpp index f997e9762..d41551ccd 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -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::floor( Location.z / EU07_SECTIONSIZE + EU07_REGIONSIDESECTIONCOUNT / 2 ) ), + static_cast( 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( Index / EU07_REGIONSIDESECTIONCOUNT ), + static_cast( Index % EU07_REGIONSIDESECTIONCOUNT ) }; +} + +std::uint32_t +classic_section_index( section_key const Key ) { + + return static_cast( 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( 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( 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( 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 &Shap basic_section & basic_region::section( glm::dvec3 const &Location ) { - auto const column { static_cast( std::floor( Location.x / EU07_SECTIONSIZE + EU07_REGIONSIDESECTIONCOUNT / 2 ) ) }; - auto const row { static_cast( 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 §ion = - m_sections[ - std::clamp( row, 0, EU07_REGIONSIDESECTIONCOUNT - 1 ) * EU07_REGIONSIDESECTIONCOUNT - + std::clamp( column, 0, EU07_REGIONSIDESECTIONCOUNT - 1 ) ] ; + auto §ion { 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(); } } diff --git a/scene/scene.h b/scene/scene.h index 3c6f9f0e6..1a56ff4dd 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -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( 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( 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 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; + using section_array = std::map; struct region_scratchpad { diff --git a/scene/sceneterrain.cpp b/scene/sceneterrain.cpp index 4248d4592..df3610bd8 100644 --- a/scene/sceneterrain.cpp +++ b/scene/sceneterrain.cpp @@ -491,13 +491,9 @@ bool terrain_file::write(std::string const &Text, std::string const &Binaryfile, // sections holding any geometry std::vector 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(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(); block.size = reader.get(); auto const radius{reader.get()}; - if (false == reader.good() || sectionindex >= Region.m_sections.size()) + if (false == reader.good() || sectionindex >= classic_section_count) { break; } diff --git a/widgets/map.cpp b/widgets/map.cpp index 19b509814..c523447cb 100644 --- a/widgets/map.cpp +++ b/widgets/map.cpp @@ -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)