#include "evo/network/probe_motion.hpp" #include namespace evo::network::motion { namespace { constexpr std::int32_t max_component = 1000; // Far inside int64: a marker never moves more than one speed step per axis per tick. constexpr std::int64_t max_coordinate = std::int64_t{1} << 60; } // namespace bool valid(Vector v) noexcept { const std::int32_t x = v.x, y = v.y; return (x * x) + (y * y) <= max_component * max_component; } bool valid(const Area& a) noexcept { return a.min_x <= a.max_x && a.min_y <= a.max_y && a.min_x >= -max_coordinate && a.max_x <= max_coordinate && a.min_y >= -max_coordinate && a.max_y <= max_coordinate; } State step(State s, const Area& a) noexcept { const auto dx = (std::int64_t{s.vector.x} * speed_um_per_tick) / max_component; const auto dy = (std::int64_t{s.vector.y} * speed_um_per_tick) / max_component; s.x = std::clamp(s.x + dx, a.min_x, a.max_x); s.y = std::clamp(s.y + dy, a.min_y, a.max_y); return s; } bool ProbeMarkers::set_area(const Area& area, std::int64_t spawn_x, std::int64_t spawn_y) { if (!valid(area)) return false; area_ = area; spawn_x_ = std::clamp(spawn_x, area.min_x, area.max_x); spawn_y_ = std::clamp(spawn_y, area.min_y, area.max_y); for (auto& e : entries_) { e.marker.state.x = std::clamp(e.marker.state.x, area.min_x, area.max_x); e.marker.state.y = std::clamp(e.marker.state.y, area.min_y, area.max_y); } publish(); return true; } std::expected ProbeMarkers::open(std::uint64_t session) { if (!area_) return std::unexpected(MarkerError::no_area); if (session == 0 || std::ranges::any_of(entries_, [&](const Entry& e) { return e.marker.session == session; })) return std::unexpected(MarkerError::invalid); if (entries_.size() >= max_markers_) return std::unexpected(MarkerError::full); Entry e; e.marker.id = next_id_++; e.marker.session = session; e.marker.state = {spawn_x_, spawn_y_, {}}; entries_.push_back(std::move(e)); publish(); return entries_.back().marker.id; } void ProbeMarkers::close(std::uint64_t session) { std::erase_if(entries_, [&](const Entry& e) { return e.marker.session == session; }); publish(); } void ProbeMarkers::clear() noexcept { entries_.clear(); markers_.clear(); } std::expected ProbeMarkers::schedule(std::uint64_t session, std::uint64_t sequence, std::uint64_t tick, Vector vector) { const auto it = std::ranges::find_if(entries_, [&](const Entry& e) { return e.marker.session == session; }); if (it == entries_.end()) return std::unexpected(MarkerError::unknown_session); if (!valid(vector) || tick <= tick_ || sequence <= it->marker.applied_sequence || (!it->scheduled.empty() && sequence <= it->scheduled.back().sequence)) return std::unexpected(MarkerError::invalid); if (it->scheduled.size() >= max_scheduled_) return std::unexpected(MarkerError::full); // Keep ascending tick order; a later sequence for an earlier tick supersedes nothing // already scheduled after it (each takes effect at its own tick). const auto at = std::ranges::upper_bound(it->scheduled, tick, {}, &Scheduled::tick); it->scheduled.insert(at, Scheduled{sequence, tick, vector}); return {}; } void ProbeMarkers::advance(std::uint64_t tick) { if (tick <= tick_) return; if (!area_) { tick_ = tick; return; } const auto first = tick - tick_ > max_catch_up ? tick - max_catch_up + 1 : tick_ + 1; for (auto& e : entries_) { auto& m = e.marker; // Intentions for ticks that can no longer be stepped still set the held vector. while (!e.scheduled.empty() && e.scheduled.front().tick < first) { m.state.vector = e.scheduled.front().vector; m.applied_sequence = std::max(m.applied_sequence, e.scheduled.front().sequence); e.scheduled.pop_front(); } for (auto t = first; t <= tick; ++t) { while (!e.scheduled.empty() && e.scheduled.front().tick == t) { m.state.vector = e.scheduled.front().vector; m.applied_sequence = std::max(m.applied_sequence, e.scheduled.front().sequence); e.scheduled.pop_front(); } m.state = step(m.state, *area_); } } tick_ = tick; publish(); } const Marker* ProbeMarkers::of(std::uint64_t session) const noexcept { const auto it = std::ranges::find_if(markers_, [&](const Marker& m) { return m.session == session; }); return it == markers_.end() ? nullptr : &*it; } void ProbeMarkers::publish() { markers_.clear(); for (const auto& e : entries_) markers_.push_back(e.marker); std::ranges::sort(markers_, {}, &Marker::id); } } // namespace evo::network::motion