detphys_turn.hturn-based sessions, transport interface, loopback (version 9)
Transport interface and loopback
| Function | Description |
|---|---|
detphys_loopback_desc_default | Default loopback settings (no delay, no loss) |
detphys_loopback_create | Creates a loopback joining two peers in one process |
detphys_loopback_destroy | Destroys it |
detphys_loopback_transport | Gets the transport of side 0 or 1 |
detphys_loopback_pump | Advances time by one (the unit of delay) |
detphys_loopback_get_stats | Sent, dropped, duplicated, delivered counts |
| Struct | Description |
|---|---|
detphys_transport | The transport interface (send, recv and user). Implement this for Steam, EOS and so onUsage: Transports and room codes › Usage (the same for every transport) |
detphys_loopback | A loopback (opaque) |
detphys_loopback_desc | Delay, loss and duplication rates, seed |
detphys_loopback_stats | Loopback counters |
Sessions (rule interface)
| Function | Description |
|---|---|
detphys_turn_abi_version | This header's version |
detphys_turn_config_default | Default session settings (up to 900 ticks per turn, 5000 ms peer timeout, 20 ms minimum resend, 250 ms keepalive) |
detphys_turn_create_rule | Creates a session with a rule |
detphys_turn_create_rule_kinematic | Same, also passing a kinematic table (moving concave shapes)Usage: Concepts › Moving concave shapes |
detphys_turn_destroy | Destroys it |
detphys_turn_poll | Reads what arrived, advances up to max_ticks, sends (resends by count; for in-process tests) |
detphys_turn_poll_at | Same, with the engine's monotonic clock [µs] (use this on real networks)Usage: Concepts › Transports |
detphys_turn_state | Current state (DETPHYS_, WAIT_LOCAL_INPUT, WAIT_, SIMULATING, WAIT_HASH, WAIT_RESYNC, ERROR, DETPHYS_) |
detphys_turn_current | Current turn number |
detphys_turn_current_mover | Current mover |
detphys_turn_submit_words | Submits the mover's input (int32 × input_words)Usage: Concepts › Turn-based |
detphys_turn_next_result | Takes one finished turn's result (with inputs and state) |
detphys_turn_get_status | Current status (turn, match, mover, world replacement counter, time since the last packet, …) |
detphys_turn_read_state | Reads the current match state words |
detphys_turn_world | Current world (read only) |
detphys_turn_error | Reason when in ERRORUsage: Desyncs › When peers do not connect (config check mismatch) |
detphys_turn_set_timing | Minimum resend time and keepalive interval |
detphys_turn_set_peer_timeout | Peer timeout (until PEER_LOST) |
detphys_turn_set_link_id | Your link id (a different value after a restart) |
detphys_turn_get_link_stats | Round trip, resends, bytes, … |
detphys_turn_set_checkpoint_kind | Kind of the guest's per-turn checkpoint (FULL default / DYNAMIC / LEGACY)Usage: Concepts › Snapshots |
detphys_turn_get_checkpoint_stats | Checkpoint counts, sizes and times |
detphys_turn_debug_nudge_at | Test only: shifts the state by 1 ULP at the start of that turnUsage: Determinism rules › How to check |
detphys_turn_get_tag_info, detphys_tag_part_name | Reads what a peer with a mismatching config check sends (detphys_tag_info) / returns a part's name. Version 9. See the section belowUsage: Desyncs › When peers do not connect (config check mismatch) |
| Struct | Description |
|---|---|
detphys_turn_session | A session (opaque) |
detphys_turn_config | Session settings (session id, shapes, peer, first mover, tick limit, world, network values) |
detphys_turn_rule | A rule (input and state word counts, params, begin_match, check_input, apply_input, after_tick, end_turn)Usage: Concepts › Rules |
detphys_rule_ctx | Values passed to callbacks (world, params, state, mover, turn, tick, settled) |
detphys_turn_mesh | Shape data (.dnshape bytes) |
detphys_turn_result | One turn's result (hashes, bytes, turn, mover, next mover, ticks, flags [DETPHYS_]) |
detphys_turn_status | Current status |
detphys_turn_link_stats | Network statistics |
detphys_turn_checkpoint_stats | Guest checkpoint statistics |
detphys_tag_info | State of a peer whose config check does not match (136 B, the same as for rt; see the section below)Usage: Desyncs › When peers do not connect (config check mismatch) |
Rule return values: after_tick returns DETPHYS_ / END_TURN / END_MATCH; end_turn returns the next mover (OR DETPHYS_ to end the match).
Built-in rule: stacking
| Function | Description |
|---|---|
detphys_rule_stack_desc_default | Default stacking settings (base 7 m wide, 0.6 m high, top at y = 0; drop clearance 300 mm; x range ±5000 mm; fall line −1000 mm) |
detphys_rule_stack_next_default | Default next-shape settings (SEED, UNIFORM, a different sequence per match, equal weights, 2 shapes ahead) |
detphys_turn_rule_stack_ex | Writes the stacking rule (with the next-shape settings) |
detphys_turn_create_stack_ex | Creates a stacking session (the easy way from C# and others)Usage: Concepts › Rules |
detphys_rule_stack_next_shape | With SEED, computes the shape of match m, turn t (no session needed) |
detphys_turn_rule_stack | Writes the stacking rule where the input picks the shape (3 input words) |
detphys_turn_create_stack | Creates a session with that rule |
| Struct | Description |
|---|---|
detphys_rule_stack_desc | Drop clearance, range, fall line, material, base boxes (1–16) |
detphys_rule_stack_next | Next shape (mode SEED / INPUT, seed, draw UNIFORM / BAG, per_match, weights, preview 0–8) |
detphys_turn_box | A static box [mm] |
3D sessions (version 8)
The turn flow, hash checks, resync, rejoin, host restart and the guest's per-turn checkpoint are the same as in 2D; the world is 3D (detphys3d.h). The config's world and meshes are not used (mesh_count must be 0); register convex hulls in begin_match with detphys3d_, in the same order on both peers. State sent to another peer (resync, joining) uses detphys3d_. Pass a 3D kinematic table with detphys_ in detphys3d_ (the 2D table of detphys_ cannot be used in 3D). A moving kinematic body keeps the world from settling, so end turns in after_tick by tick count or similar. The desync debugger (detphys_) works in 3D too (when detphys_ is 3 or later; recordings use the 3D format and reports the 3D fields; see 5. Troubleshooting desyncs).
| Function | Description |
|---|---|
detphys_turn_create_rule3d | Creates a session from a 3D rule and 3D world settings (detphys3d_, NULL = defaults). The world is rebuilt for every matchUsage: Concepts › 3D |
detphys_turn_world3d | The current 3D world (read only). For 3D sessions detphys_ returns NULL |
| Struct | Description |
|---|---|
detphys_turn_rule3d | A 3D rule (same layout as detphys_; callbacks receive detphys_; 80 B) |
detphys_rule_ctx3d | Values passed to callbacks (same layout as detphys_rule_ctx; only world is a detphys3d_world*) |
Stacking-only functions (new code should use the rule interface above)
| Function / struct | Description |
|---|---|
detphys_turn_desc_default, detphys_turn_create | Create a session from stacking-only settings (uses the built-in stacking rule) |
detphys_turn_submit | Submits a stacking input (shape, x, angle) |
detphys_turn_next_report | Takes one turn's report (detphys_) |
detphys_turn_desc, detphys_turn_input, detphys_turn_report, detphys_turn_shape | Settings, input, report and shape for the functions above |
Desync debugger (turn-based)
| Function | Description |
|---|---|
detphys_turn_set_debug | Turns the desync debugger on / off (detphys_)Usage: Desyncs › Turning the desync debugger on |
detphys_turn_debug_count | Number of reports |
detphys_turn_debug_get | Reads a report (with the list of differences and the spread) |
detphys_turn_debug_bundle | Packs a report and both recordings into a bundle (DNDB) |
detphys_turn_debug_stats | Recording and exchange counters |
Peers whose config check does not match (version 9)
Peers whose settings, rule, shapes, hulls, kinematic tables or core version differ do not connect (packets whose config check tag does not match are dropped). Since version 9 (rt: version 7) the session counts those packets and exchanges the tag contents (one hash per part) with that peer, so you can tell which part differs. Read it with detphys_ (rt: detphys_); both fill the same struct, detphys_tag_info. The tag values themselves, the existing structs and the protocol are the same as in version 8 (rt: 6). How to read it and what to fix is in 5. Troubleshooting desyncs.
Field (detphys_tag_info, 136 B) | Contents |
|---|---|
struct_size | Set to sizeof(detphys_ |
flags | SEEN 0x01 (a mismatching tag arrived), REMOTE_KNOWN 0x02 (the peer's contents arrived), PEER_KNOWS 0x04 (the peer got ours), ACTIVE 0x08 (not connected yet) |
local_tag, remote_tag | Our tag and the last mismatching peer tag (rt sends only the low 32 bits; before the contents arrive, rt's remote_tag is those 32 bits. Turn-based: 0 when only the session_id differs) |
mismatch_packets | Number of packets dropped because the tag did not match |
diff_parts | Parts that differ (OR of DETPHYS_; only with REMOTE_KNOWN. 0 means all parts are equal and only the whole tag differs) |
dimensions, remote_dimensions | 2 or 3 (the peer's only with REMOTE_KNOWN, otherwise 0) |
local_version, remote_version | Check version × 65536 + protocol version (rt: 3 and 3, turn-based: 6 and 4) |
local_shape_count, remote_shape_count | Number of shapes (2D: registered shapes / 3D: hulls) |
first_shape_diff | Index of the first differing shape (the first DETPHYS_ = 64 are compared; if only the counts differ, the smaller count; −1 = none or unknown) |
local_parts[8], remote_parts[8] | Hash per part ([i] is part 1 << i; 32 bits, independent of build and device; entries after 5 are 0) |
info_sent, info_received | How many times we sent our contents / received the peer's (usually 1–2 per side) |
Part (DETPHYS_) | detphys_ | Contents |
|---|---|---|
VERSION (0x01) | version | The core's check and protocol versions, 2D / 3D |
CONFIG (0x02) | config | session_id, tick rate, substeps, gravity, 3D contact tuning, turn-based first mover and tick limit per turn |
RULE (0x04) | rule | rule_id, input and state word counts, params |
SHAPES (0x08) | shapes | 2D: checksums of the registered shapes (.dnshape) / 3D: checksums and vertex counts of the hulls registered in begin |
KINEMATIC (0x10) | kinematic | The kinematic table (whether one was passed, flags, contents after begin) |
DETPHYS_ is 5; unknown values are named "unknown". Flags: rt uses DETPHYS_ (0x200) in detphys_; turn-based sessions use DETPHYS_ in flags. Both reads can be called at any time (every frame is fine) and return 0 on success, negative for NULL or a wrong struct_size.