From 5bb9157c10ac5d9c86a518710120a2feb787fd84 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Thu, 3 Dec 2026 02:13:29 +0100 Subject: [PATCH] feat(world): a repeated name goes to the item of the later statement, whatever order the statements load in (D4) The first rule of D4 (docs/world-format-eu07.md, section 13). basic_table keeps the order of each item: while the statements of a scenery load out of the order of the file (EU07_WORLD_REPLAY_ORDER), the place of its statement in the file (table_statement_order(), set by the loader for each statement it replays), otherwise one after the item before. A repeated name goes to the item of the later order, which with the statements loading in the order of the file is the item inserted later, as it always was; nothing changes for a load in that order. Events keep their own rule (the first one keeps the name): they are statements of the whole package, which load in the order of the file among themselves whatever order the regions take. Checked in the game (--test 120 s, seed 1, world.binary 1) on drawinowo.scn, with 35 repeated track names: replayed with the regions first and the whole package after them, the 13 physics logs are the same bit for bit as from the files, and so are the 1064 lines of the error log, the 35 repeated names among them. In the order of the file by moves on the tape too. Tests: the later item takes the name, the item of the later statement takes it while statements load out of order, an item added after the load is the newest. --- simulation/simulationstateserializer.cpp | 3 ++ tests/unit/test_names_order.cpp | 43 ++++++++++++++++++++++++ utilities/Names.h | 27 +++++++++++++-- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_names_order.cpp diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index bec32f107..f7499cd61 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -341,9 +341,12 @@ state_serializer::deserialize_ordered( deserializer_state &State, std::chrono::s auto const end { index + 1 < statements.size() ? statements[ index + 1 ].offset : State.tape.size() - 1 }; State.replayer.seek( static_cast( item.offset ), static_cast( end ), item.start, State.tapefiles ); restore_transformations( State.scratchpad.location, State.replaytable.contexts[ item.context ] ); + // a repeated name goes to the item of the later statement in the file, whatever the order they load in (D4) + table_statement_order() = index; if( auto const token { State.input.getToken() }; false == token.empty() ) { deserialize_statement( State, token ); } + table_statement_order().reset(); if( std::chrono::duration_cast( std::chrono::steady_clock::now() - Started ).count() >= 200 ) { Application.set_progress( static_cast( 100 * State.ordercursor / State.order.size() ), static_cast( 100 * State.ordercursor / State.order.size() ) ); return true; diff --git a/tests/unit/test_names_order.cpp b/tests/unit/test_names_order.cpp new file mode 100644 index 000000000..917600a87 --- /dev/null +++ b/tests/unit/test_names_order.cpp @@ -0,0 +1,43 @@ +#include "stdafx.h" +#include "doctest.h" +#include "utilities/Names.h" + +#include + +// the table of names: a repeated name goes to the later item, and while the statements of a scenery load out of the order of the file, +// to the item of the later statement (docs/world-format-eu07.md, D4) + +namespace { + +struct named { + std::string label; + std::string const &name() const { return label; } +}; + +} // namespace + +TEST_CASE("a repeated name goes to the item inserted later, as it always did") { + basic_table table; + auto *first{new named{"tor1"}}; + auto *second{new named{"tor1"}}; + CHECK(table.insert(first)); + CHECK_FALSE(table.insert(second)); + CHECK(table.find("tor1") == second); +} + +TEST_CASE("with the statements loading out of order, a repeated name goes to the item of the later statement") { + basic_table table; + auto *later{new named{"tor1"}}; + auto *earlier{new named{"tor1"}}; + // the statement 20 of the file loads first, the statement 5 after it + table_statement_order() = 20; + CHECK(table.insert(later)); + table_statement_order() = 5; + CHECK_FALSE(table.insert(earlier)); + table_statement_order().reset(); + CHECK(table.find("tor1") == later); + // an item added after the load, at no statement, is the newest one + auto *added{new named{"tor1"}}; + CHECK_FALSE(table.insert(added)); + CHECK(table.find("tor1") == added); +} diff --git a/utilities/Names.h b/utilities/Names.h index 52d438f6b..cfa09179f 100644 --- a/utilities/Names.h +++ b/utilities/Names.h @@ -12,6 +12,18 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include +#include +#include + +// the place in the scenery file of the statement being loaded, while the statements of a scenery load in another order than the +// file's (docs/world-format-eu07.md, D4): a repeated name then goes to the item of the later statement, as a load in the order of the +// file gives it, whatever order the statements load in. none otherwise: the item inserted later takes the name, as it always did +inline std::optional & +table_statement_order() { + static std::optional order; + return order; +} template class basic_table { @@ -27,6 +39,11 @@ public: bool insert( Type_ *Item, std::string itemname ) { m_items.emplace_back( Item ); + // the order of the item: its statement in the file while statements load out of order, after every item before otherwise + auto const order { table_statement_order().value_or( m_lastorder + 1 ) }; + m_lastorder = std::max( m_lastorder, order ); + m_orders.resize( m_items.size(), 0 ); + m_orders.back() = order; if( true == itemname.empty() || itemname == "none" ) { return true; } @@ -36,8 +53,11 @@ public: if( true == mapping.second ) { return true; } - // item with this name already exists; update mapping to point to the new one, for backward compatibility - mapping.first->second = itemhandle; + // item with this name already exists; update mapping to point to the later one, for backward compatibility. with the + // statements loading in the order of the file the later one is the one inserted now, as it always was + if( order >= m_orders[ mapping.first->second ] ) { + mapping.first->second = itemhandle; + } return false; } bool insert (Type_ *Item) { @@ -117,6 +137,9 @@ protected: // members type_sequence m_items; index_map m_itemmap; + // the order of each item, and the latest one, see insert() + std::vector m_orders; + std::uint64_t m_lastorder { 0 }; public: // data access