Files
aevolution/src/network/protocol/probe_motion.cpp
T
dingoandClaude Opus 5.5 ab7e26c02e
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m1s
Foundation / checks (network) (push) Successful in 12m13s
Foundation / checks (client) (push) Successful in 14m28s
Foundation / checks (tsan) (push) Successful in 7m56s
Foundation / checks (checks) (push) Successful in 32m49s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 40m58s
Add M10.6a probe-motion experiment core and session protocol v3
EXPERIMENT (ADR-0094), not an organism movement rule: one non-biological
marker per session, moved by admitted intentions with a shared integer rule
(probe-motion-v1, 100 mm per tick, clamped to the area), held in region
memory only.

- ProbeMarkers: spawn, area, bounded per-marker schedule, catch-up bound
- session protocol v3: marker records in snapshot chunks; the own marker is
  always sent with its applied sequence and area; v1 and v2 are refused
- MotionPredictor (bounded replay from the authoritative marker, corrections,
  error samples), CorrectionSmoother and InputLead
- tests: exact prediction with enough lead, refusal and lead adaptation,
  lost inputs, bounds, marker records through the codec

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 15:12:13 -04:00

131 lines
4.9 KiB
C++

#include "evo/network/probe_motion.hpp"
#include <algorithm>
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<std::uint64_t, MarkerError> 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<void, MarkerError> 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