#include "evo/simulation/scheduler.hpp" #include #include #include #include namespace evo { std::expected, ScheduleError> advance(SimulationWorld& world, Tick target, std::span commands) { if (world.current_tick().value == std::numeric_limits::max()) { return std::unexpected{ ScheduleError{ScheduleErrorCode::simulation_failure, 0, Error::tick_exhausted}}; } if (target.value != world.current_tick().value + 1) { return std::unexpected{ScheduleError{ScheduleErrorCode::wrong_tick}}; } if (commands.size() > max_commands_per_tick) { return std::unexpected{ScheduleError{ScheduleErrorCode::batch_limit}}; } if (commands.empty()) { const auto result = world.tick(); if (!result) { return std::unexpected{ ScheduleError{ScheduleErrorCode::simulation_failure, 0, result.error()}}; } return std::vector{}; } std::vector ordered{commands.begin(), commands.end()}; std::ranges::sort(ordered, {}, &Command::sequence); std::uint64_t previous{}; for (const auto& command : ordered) { if (command.sequence == 0) { return std::unexpected{ScheduleError{ScheduleErrorCode::invalid_sequence}}; } if (command.sequence == previous) { return std::unexpected{ ScheduleError{ScheduleErrorCode::duplicate_sequence, command.sequence}}; } previous = command.sequence; } auto candidate = world; std::vector receipts; receipts.reserve(ordered.size()); for (const auto& command : ordered) { EntityId affected{}; if (const auto* create = std::get_if(&command.operation)) { const auto result = candidate.create_entity(create->position); if (!result) { return std::unexpected{ScheduleError{ScheduleErrorCode::simulation_failure, command.sequence, result.error()}}; } affected = *result; } else { affected = std::get(command.operation).id; const auto result = candidate.remove_entity(affected); if (!result) { return std::unexpected{ScheduleError{ScheduleErrorCode::simulation_failure, command.sequence, result.error()}}; } } receipts.push_back({command.sequence, affected}); } const auto result = candidate.tick(); if (!result) { return std::unexpected{ ScheduleError{ScheduleErrorCode::simulation_failure, 0, result.error()}}; } static_assert(std::is_nothrow_move_assignable_v); static_assert(std::is_nothrow_move_constructible_v>); world = std::move(candidate); return receipts; } } // namespace evo