#include "evo/simulation/world.hpp" #include #include namespace evo { std::expected SimulationWorld::create_entity(Position2 position) { if (position.x.value >= domain_extent_mm || position.y.value >= domain_extent_mm) { return std::unexpected{Error::invalid_position}; } if (entities_.size() >= max_entities) { return std::unexpected{Error::entity_limit}; } if (next_id_ == std::numeric_limits::max()) { return std::unexpected{Error::id_exhausted}; } const EntityId id{next_id_}; Random derivation{seed_ ^ (id.value * 0xd1b54a32d192ed03ULL)}; entities_.push_back(EntityState{id, position, Random{derivation.next()}}); ++next_id_; // Publish only after successful allocation/construction. return id; } std::expected SimulationWorld::remove_entity(EntityId id) { const auto found = std::ranges::lower_bound(entities_, id, {}, &EntityState::id); if (found == entities_.end() || found->id != id) { return std::unexpected{Error::entity_not_found}; } entities_.erase(found); return {}; } std::expected SimulationWorld::tick() noexcept { if (tick_.value == std::numeric_limits::max()) { return std::unexpected{Error::tick_exhausted}; } for (auto& entity : entities_) { auto& position = entity.position; position.x.value = (position.x.value + entity.random.ternary() + domain_extent_mm - 1U) % domain_extent_mm; position.y.value = (position.y.value + entity.random.ternary() + domain_extent_mm - 1U) % domain_extent_mm; } ++tick_.value; return {}; } } // namespace evo