Add M10.6a probe-motion experiment core and session protocol v3
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
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
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>
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
#pragma once
|
||||
#include "evo/network/probe_motion.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
// M10.6 client prediction and reconciliation of the probe marker (ADR-0094). Headless,
|
||||
// single-owner, presentation only: nothing here is authority.
|
||||
//
|
||||
// The predictor keeps a bounded list of sent, unacknowledged inputs. On each authoritative
|
||||
// marker state (from a snapshot: state after tick T, newest applied intention sequence) it
|
||||
// restores that state, drops inputs that are applied or can no longer apply (tick <= T),
|
||||
// and replays the rest with the shared rule up to the presented tick. The difference that
|
||||
// rebasing makes to the presented position is a correction; the smoother hides it over a
|
||||
// short time (or snaps when it is large). Prediction never outlives its inputs: a refused
|
||||
// intention is removed, and a reset (new session or epoch) forgets everything.
|
||||
namespace evo::client {
|
||||
struct PredictionLimits {
|
||||
std::size_t max_pending{32}; // Sent, unacknowledged inputs (bounded replay).
|
||||
std::uint64_t max_replay_ticks{32}; // Ticks predicted beyond the authoritative state.
|
||||
std::size_t max_history{128}; // Predicted states kept to measure errors.
|
||||
std::size_t max_errors{4096}; // Error samples awaiting take_errors().
|
||||
};
|
||||
struct MotionInput {
|
||||
std::uint64_t sequence{}, tick{};
|
||||
network::motion::Vector vector;
|
||||
};
|
||||
// The authoritative marker as a snapshot reports it: the state after `tick`.
|
||||
struct AuthoritativeMarker {
|
||||
std::uint64_t tick{}, applied_sequence{};
|
||||
network::motion::State state;
|
||||
network::motion::Area area;
|
||||
};
|
||||
struct PredictionStats {
|
||||
std::uint64_t authoritative{}, corrections{}, replayed_ticks{}, dropped_inputs{},
|
||||
rejected_inputs{}, refused_inputs{};
|
||||
std::uint64_t max_error_um{}, max_pending{};
|
||||
};
|
||||
|
||||
class MotionPredictor {
|
||||
public:
|
||||
explicit MotionPredictor(PredictionLimits limits = {}) : limits_{limits} {}
|
||||
// A new session or epoch: forget the authoritative state, inputs and history.
|
||||
void reset();
|
||||
// Records a sent input. False (nothing recorded) when the list is full or the input's
|
||||
// tick is not after the authoritative state; the caller then does not send it.
|
||||
[[nodiscard]] bool input(const MotionInput& in);
|
||||
// The server refused this input: it never takes effect.
|
||||
void rejected(std::uint64_t sequence);
|
||||
// A newer authoritative state (older or equal ticks are ignored).
|
||||
void authoritative(const AuthoritativeMarker& a);
|
||||
// The predicted state after `tick` (the presented tick: server tick plus input lead);
|
||||
// nothing before the first authoritative state.
|
||||
[[nodiscard]] std::optional<network::motion::State> predict(std::uint64_t tick);
|
||||
// The jump rebasing made to the newest prediction (old minus new), since the last call.
|
||||
struct Offset {
|
||||
std::int64_t x{}, y{};
|
||||
};
|
||||
[[nodiscard]] Offset take_correction() noexcept;
|
||||
// Distance in micrometres between what was predicted for a tick and what the authority
|
||||
// reported for it, one sample per authoritative state that had a prediction.
|
||||
[[nodiscard]] std::vector<std::uint64_t> take_errors();
|
||||
[[nodiscard]] std::size_t pending() const noexcept {
|
||||
return pending_.size();
|
||||
}
|
||||
[[nodiscard]] bool full() const noexcept {
|
||||
return pending_.size() >= limits_.max_pending;
|
||||
}
|
||||
[[nodiscard]] const std::optional<AuthoritativeMarker>& authority() const noexcept {
|
||||
return authority_;
|
||||
}
|
||||
[[nodiscard]] const PredictionStats& stats() const noexcept {
|
||||
return stats_;
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] std::optional<network::motion::State> replay(std::uint64_t tick);
|
||||
// Re-predicts the newest presented tick after a change and accumulates the jump.
|
||||
void rebase(const std::optional<network::motion::State>& before);
|
||||
PredictionLimits limits_;
|
||||
std::optional<AuthoritativeMarker> authority_;
|
||||
std::vector<MotionInput> pending_; // Ascending tick, then sequence.
|
||||
std::map<std::uint64_t, network::motion::State> history_;
|
||||
std::optional<std::uint64_t> presented_tick_;
|
||||
Offset correction_;
|
||||
std::vector<std::uint64_t> errors_;
|
||||
PredictionStats stats_;
|
||||
};
|
||||
|
||||
// Presentation smoothing, separate from prediction: a visual offset that decays to zero.
|
||||
class CorrectionSmoother {
|
||||
public:
|
||||
explicit CorrectionSmoother(double half_life_ms = 60, std::int64_t snap_um = 2'000'000)
|
||||
: half_life_ms_{half_life_ms}, snap_um_{snap_um} {}
|
||||
// Adds a correction (old minus new predicted position). Larger offsets snap.
|
||||
void add(MotionPredictor::Offset correction);
|
||||
// Advances by `elapsed_ms` and returns the offset to add to the predicted position.
|
||||
[[nodiscard]] MotionPredictor::Offset advance(double elapsed_ms);
|
||||
void reset() noexcept {
|
||||
x_ = y_ = 0;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t snaps() const noexcept {
|
||||
return snaps_;
|
||||
}
|
||||
|
||||
private:
|
||||
double half_life_ms_;
|
||||
std::int64_t snap_um_;
|
||||
double x_{}, y_{};
|
||||
std::uint64_t snaps_{};
|
||||
};
|
||||
|
||||
// How many ticks ahead of the client's newest known server tick inputs are scheduled. It
|
||||
// grows when the server refuses an input as too late (outside_window) and shrinks slowly
|
||||
// when receipts show spare ticks, within [min, max] (max below the admission window).
|
||||
class InputLead {
|
||||
public:
|
||||
explicit InputLead(std::uint64_t min = 2, std::uint64_t max = 7)
|
||||
: min_{min}, max_{max}, lead_{min} {}
|
||||
[[nodiscard]] std::uint64_t lead() const noexcept {
|
||||
return lead_;
|
||||
}
|
||||
void too_late() noexcept;
|
||||
// An admitted input for `tick` received at the server's `server_tick`.
|
||||
void admitted(std::uint64_t tick, std::uint64_t server_tick) noexcept;
|
||||
void reset() noexcept {
|
||||
lead_ = min_;
|
||||
spare_ = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::uint64_t min_, max_, lead_;
|
||||
std::uint32_t spare_{};
|
||||
};
|
||||
} // namespace evo::client
|
||||
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#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`.
|
||||
};
|
||||
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);
|
||||
void close(std::uint64_t session);
|
||||
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.
|
||||
};
|
||||
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::uint64_t tick_{}, next_id_{1};
|
||||
void publish();
|
||||
};
|
||||
} // namespace evo::network::motion
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "evo/client/environment_view.hpp"
|
||||
#include "evo/client/organism_view.hpp"
|
||||
#include "evo/network/probe_motion.hpp"
|
||||
#include "evo/network/session_protocol.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -26,6 +27,11 @@
|
||||
// already holds. A snapshot is split into self-contained chunks of whole records, one datagram
|
||||
// each.
|
||||
//
|
||||
// M10.6 (ADR-0094): probe markers are replicated too. A session's own marker is always
|
||||
// sent (whatever its interest), first and every tick it changed, with the newest intention
|
||||
// sequence applied and the area it moves in, so the client can reconcile its prediction;
|
||||
// other markers pass the spatial filter like any entity and carry neither.
|
||||
//
|
||||
// Client: chunks are assembled per snapshot; a complete snapshot is applied to its baseline
|
||||
// (one of the two newest applied snapshots) and acknowledged; presentations are rebuilt
|
||||
// through the validated presentation constructors.
|
||||
@@ -47,6 +53,9 @@ struct ReplicationSource {
|
||||
std::uint32_t mask{};
|
||||
std::vector<client::EnvironmentCell> environment;
|
||||
std::vector<client::OrganismCell> organisms;
|
||||
// Probe markers of the region (all of them; filtered per session) and their area.
|
||||
std::vector<motion::Marker> markers;
|
||||
std::optional<motion::Area> marker_area;
|
||||
};
|
||||
// Whether a session may perceive an organism (the perception extension boundary). Spatial
|
||||
// interest is applied first; this decides concealment. Default: everything in interest.
|
||||
@@ -104,6 +113,17 @@ class ReplicationServer {
|
||||
ReplicationStats stats_;
|
||||
};
|
||||
|
||||
// A probe marker as a client receives it. `own` is the receiving session's marker: only it
|
||||
// carries the applied sequence and the area (zero otherwise).
|
||||
struct ReplicatedMarker {
|
||||
std::uint64_t id{};
|
||||
motion::State state;
|
||||
std::int64_t z{};
|
||||
bool own{};
|
||||
std::uint64_t applied_sequence{};
|
||||
motion::Area area;
|
||||
friend bool operator==(const ReplicatedMarker&, const ReplicatedMarker&) = default;
|
||||
};
|
||||
// One applied snapshot as the client holds it.
|
||||
struct ReplicatedView {
|
||||
std::uint64_t tick{}, field_revision{}, organism_revision{};
|
||||
@@ -111,6 +131,7 @@ struct ReplicatedView {
|
||||
std::int64_t slice_z{};
|
||||
std::vector<client::EnvironmentCell> environment; // Sorted by voxel.
|
||||
std::vector<client::OrganismCell> organisms; // Sorted by id.
|
||||
std::vector<ReplicatedMarker> markers; // Sorted by id; at most one own.
|
||||
};
|
||||
enum class AssemblyError : std::uint8_t { malformed, invalid_view };
|
||||
|
||||
@@ -148,8 +169,13 @@ enum class RecordKind : std::uint8_t {
|
||||
environment_upsert = 1,
|
||||
environment_remove,
|
||||
organism_upsert,
|
||||
organism_remove
|
||||
organism_remove,
|
||||
marker_upsert, // v3 (ADR-0094).
|
||||
marker_remove
|
||||
};
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode_record(const client::EnvironmentCell& cell);
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode_record(const client::OrganismCell& cell);
|
||||
// The own marker also carries its applied sequence and area.
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode_record(const motion::Marker& marker, std::int64_t z,
|
||||
const motion::Area* own_area);
|
||||
} // namespace evo::network
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
// 160-byte credentials here. Later M10 segments add message types under a new version.
|
||||
namespace evo::network {
|
||||
inline constexpr std::uint32_t session_magic = 0x4E535645; // Bytes "EVSN".
|
||||
// v2 (M10.5, ADR-0093): v1 plus interest, snapshot chunks and snapshot acks. v1 is frozen
|
||||
// and no longer accepted (it was only ever deployed to evolution-dev, with the probe).
|
||||
inline constexpr std::uint16_t session_protocol_version = 2;
|
||||
// v2 (M10.5, ADR-0093): v1 plus interest, snapshot chunks and snapshot acks.
|
||||
// v3 (M10.6, ADR-0094): v2 plus probe marker records inside snapshot chunks; no new message
|
||||
// types. v1 and v2 were deployed to evolution-dev and are frozen; only v3 is accepted.
|
||||
inline constexpr std::uint16_t session_protocol_version = 3;
|
||||
inline constexpr std::size_t session_datagram_bytes = 1200; // Whole message, ADR-0089.
|
||||
inline constexpr std::size_t session_header_bytes = 8;
|
||||
inline constexpr std::size_t session_grant_wire_bytes = 160;
|
||||
|
||||
Reference in New Issue
Block a user