From 2b9e97e28ed0504de15d73ff5e1dd0ff9deb6d09 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 10 Apr 2023 00:14:47 +0200 Subject: src/world: pre-initialize entity ctr to 1024 This way, rtree object_id can only be equal to entity's id if it's an entity and not static geometry. --- serialize/world-impl.hpp | 3 ++- serialize/world-reader.cpp | 9 +++++---- src/world.hpp | 8 +++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/serialize/world-impl.hpp b/serialize/world-impl.hpp index 840cc6b3..88844869 100644 --- a/serialize/world-impl.hpp +++ b/serialize/world-impl.hpp @@ -25,6 +25,7 @@ * 10) Chunk Z level. * 11) RLE empty tiles. * 12) Don't write entity name twice. + * 13) Entity counter initialized to 1024. */ namespace floormat { @@ -51,7 +52,7 @@ constexpr inline auto null_atlas = (atlasid)-1LL; constexpr inline size_t character_name_max = 128; constexpr inline size_t string_max = 512; -constexpr inline proto_t proto_version = 11; +constexpr inline proto_t proto_version = 13; constexpr inline proto_t min_proto_version = 1; constexpr inline auto chunk_magic = (uint16_t)~0xc0d3; constexpr inline auto scenery_magic = (uint16_t)~0xb00b; diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index b9a6a528..09566afa 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -382,7 +382,7 @@ void reader_state::deserialize_world(ArrayView buf) (size_t)proto, (size_t)min_proto_version, (size_t)proto_version); PROTO = proto; fm_assert(PROTO > 0); - object_id entity_counter = 0; + object_id entity_counter = world::entity_counter_init; read_atlases(s); if (PROTO >= 3) [[likely]] read_sceneries(s); @@ -393,10 +393,11 @@ void reader_state::deserialize_world(ArrayView buf) read_chunks(s); s.assert_end(); if (PROTO >= 8) [[likely]] - { - fm_assert(_world->entity_counter() == 0); + fm_assert(_world->entity_counter() == world::entity_counter_init); + if (PROTO >= 13) [[likely]] _world->set_entity_counter(entity_counter); - } + else if (PROTO >= 8) [[likely]] + _world->set_entity_counter(std::max(world::entity_counter_init, entity_counter)); _world = nullptr; } diff --git a/src/world.hpp b/src/world.hpp index 8148bfd5..4b8ce797 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -19,6 +19,10 @@ template struct entity_type_; struct world final { + static constexpr object_id entity_counter_init = 1024; + static constexpr size_t initial_capacity = 64; + static constexpr float max_load_factor = .5; + private: struct chunk_tuple final { static constexpr chunk_coords_ invalid_coords = { -1 << 15, -1 << 15, chunk_min_z }; @@ -26,14 +30,12 @@ private: chunk_coords_ pos = invalid_coords; } _last_chunk; - static constexpr size_t initial_capacity = 64; - static constexpr float max_load_factor = .5; std::unordered_map _chunks; std::unordered_map> _entities; size_t _last_collection = 0; size_t _collect_every = 64; std::shared_ptr _unique_id = std::make_shared('A'); - object_id _entity_counter = 0; + object_id _entity_counter = entity_counter_init; uint64_t _current_frame = 1; // zero is special for struct entity bool _teardown : 1 = false; -- cgit v1.2.3