Files
aevolution/include/evo/network/probe_motion.hpp
T
dingoandClaude Opus 5.5 a7a3d12283
Foundation / checks (deploy) (push) Successful in 3m29s
Foundation / checks (ecs) (push) Successful in 2m0s
Foundation / checks (client) (push) Successful in 12m51s
Foundation / checks (network) (push) Successful in 13m0s
Foundation / checks (tsan) (push) Successful in 7m50s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 32m4s
Foundation / checks (postgres) (push) Canceled after 19m13s
Wire M10.8c-1 region handoff: protocol v4, durable probes, crossing, ghosts
ADR-0096. Session protocol v4 (v3 is deployed and frozen) adds HandoffOffer,
SessionRedirect, close reasons handed_off and moved, and ghost marker records.
Sessions use the durable probe record: logins open it where it was saved
(redirected when it lives elsewhere), and session ends save it. A marker
crossing into a neighbour's area is frozen, prepared, its control released
and a signed ticket offered; uncommitted transfers are aborted after the
ticket's lifetime. The destination shows arriving probes as ghosts (schema
v13 incoming_probe_transfers) and opens the marker where it crossed.

Refreshes the dev session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 21:24:42 -04:00

128 lines
5.9 KiB
C++

#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <expected>
#include <optional>
#include <span>
#include <vector>
// M10.6 probe-motion experiment (ADR-0094). EXPERIMENT, not a game rule: one
// non-biological engineering marker per session moves at a fixed speed so that client
// prediction and reconciliation can be proven before M11 decides organism movement. A
// marker has a position only: no energy, contact, perception, biology or durability (it
// lives in the region's memory for its session and is respawned after a restart).
//
// The rule is shared, versioned and integer-exact, so the client replays exactly what the
// server simulated: each tick the held movement vector (norm <= 1000, as admitted) moves
// the marker by vector * speed / 1000 micrometres per axis, clamped to the area's bounds.
// An intention scheduled for tick T sets the held vector from tick T's step onward.
namespace evo::network::motion {
inline constexpr std::uint32_t rule_version = 1; // probe-motion-v1.
inline constexpr std::int64_t speed_um_per_tick = 100'000; // EXPERIMENT: 100 mm per tick.
inline constexpr std::int64_t micrometres_per_cell = 1'000'000; // 1 m cells.
struct Vector {
std::int16_t x{}, y{};
friend bool operator==(const Vector&, const Vector&) = default;
};
// Inclusive bounds in micrometres, within one Z slice of cells.
struct Area {
std::int64_t min_x{}, min_y{}, max_x{}, max_y{};
std::int64_t z{}; // Cell slice the markers live in.
friend bool operator==(const Area&, const Area&) = default;
};
struct State {
std::int64_t x{}, y{}; // Micrometres.
Vector vector; // Held movement vector.
friend bool operator==(const State&, const State&) = default;
};
// Whether a vector is within the admitted bound (norm <= 1000).
[[nodiscard]] bool valid(Vector v) noexcept;
[[nodiscard]] bool valid(const Area& a) noexcept;
// One simulated tick of the held vector, clamped to the area. Pure and integer-exact.
[[nodiscard]] State step(State s, const Area& a) noexcept;
// Server side: the markers of one region's sessions. Single-owner (the session host).
struct Marker {
std::uint64_t id{}, session{};
State state;
std::uint64_t applied_sequence{}; // Newest intention whose vector is in `state`.
// M10.8c (ADR-0096): frozen markers neither move nor take intentions (a handoff is being
// prepared); ghosts are read-only copies of probes being handed to this region (no
// session, never moving).
bool frozen{}, ghost{};
};
// A probe arriving here, shown read-only until its transfer commits (keyed by transfer id).
struct Ghost {
std::array<std::uint8_t, 16> transfer{};
std::int64_t x{}, y{};
friend bool operator==(const Ghost&, const Ghost&) = default;
};
enum class MarkerError : std::uint8_t { no_area, unknown_session, full, invalid };
class ProbeMarkers {
public:
explicit ProbeMarkers(std::size_t max_markers = 32, std::size_t max_scheduled = 16)
: max_markers_{max_markers}, max_scheduled_{max_scheduled} {}
// Where markers live and spawn (the spawn is clamped into the area). Existing markers
// are clamped into a new area.
[[nodiscard]] bool set_area(const Area& area, std::int64_t spawn_x, std::int64_t spawn_y);
[[nodiscard]] const std::optional<Area>& area() const noexcept {
return area_;
}
// A marker for the session, at the spawn, holding no movement.
[[nodiscard]] std::expected<std::uint64_t, MarkerError> open(std::uint64_t session);
// The same at a given position (a durable probe's, clamped into the area).
[[nodiscard]] std::expected<std::uint64_t, MarkerError> open_at(std::uint64_t session,
std::int64_t x, std::int64_t y);
// Moves the session's marker to a position (clamped into the area), keeping its vector.
void place(std::uint64_t session, std::int64_t x, std::int64_t y);
// Stops the session's marker where it is: no motion and no new intentions.
void freeze(std::uint64_t session);
// Replaces the ghosts (stable ids per transfer; at most max_markers).
void set_ghosts(std::span<const Ghost> ghosts);
void close(std::uint64_t session);
// Removes every marker and forgets the tick (a new epoch may restart at an earlier one).
void clear() noexcept;
// An admitted intention: from its tick on, the marker holds its vector. Intentions
// arrive in sequence order; the newest per tick wins. Bounded per marker.
[[nodiscard]] std::expected<void, MarkerError>
schedule(std::uint64_t session, std::uint64_t sequence, std::uint64_t tick, Vector vector);
// The host simulated every tick up to `tick`: steps each marker once per tick since the
// last advance (at most `max_catch_up` ticks; a longer gap respawns nothing, it only
// skips motion it can no longer attribute).
void advance(std::uint64_t tick);
[[nodiscard]] const std::vector<Marker>& markers() const noexcept {
return markers_;
}
[[nodiscard]] const Marker* of(std::uint64_t session) const noexcept;
[[nodiscard]] std::uint64_t tick() const noexcept {
return tick_;
}
static constexpr std::uint64_t max_catch_up = 64;
private:
struct Scheduled {
std::uint64_t sequence{}, tick{};
Vector vector;
};
struct Entry {
Marker marker;
std::deque<Scheduled> scheduled; // Ascending tick, then sequence.
};
struct GhostEntry {
Ghost ghost;
std::uint64_t id{};
};
std::size_t max_markers_, max_scheduled_;
std::optional<Area> area_;
std::int64_t spawn_x_{}, spawn_y_{};
std::vector<Marker> markers_; // Mirror of entries_ for readers, in id order.
std::vector<Entry> entries_;
std::vector<GhostEntry> ghosts_;
std::uint64_t tick_{}, next_id_{1};
void publish();
};
} // namespace evo::network::motion