M7: Duos and revive

Two-player teams, enforced by the simulation:
- Match puts every player on a team (SimEntity.team); Weapons.damage_player
  refuses damage between teammates from every weapon, the boathook and blasts.
- A player whose health runs out with a teammate standing goes down instead of
  out: 60 health to bleed (one point every half second), crawling at 1 m/s, no
  firing, building, looting or items. Hits still take it; drowning too.
- A standing teammate within 2 m holding interact for 6 s revives them with 30
  health. Letting go or moving away starts it over.
- Downed players are out when their health runs out again or when no teammate
  is left standing, credited to whoever downed them. A team falls together and
  shares its place; the tie-break puts standing ahead of downed.
- Downs and revives go through Match events and the kill feed ("X knocked Y
  down"); the HUD says what to do while down.

Wire: FLAG_DOWNED in the player record and the own block (flags now u16),
so prediction crawls too. Protocol version 5.

Bots: never target a teammate, land with their teammate, regroup when they
stray past 30 m, crawl toward a standing teammate when down, go to revive a
downed teammate within 60 m when not in a fight, and turn from a downed target
to its standing teammate when it's in view.

Options: --team-size=2 on the server, --duos offline, and Solo/Duos under
Crew in the Practice menu. The figure has a downed crawl pose.

Tests: test_duos (downing, crawl, revive, interrupted revive, bleed-out,
finishing, team out, falling together, no friendly damage including kegs,
drowning while down, hash, match placement and tie-break), bots as teammates,
the downed flag on the wire, the feed, server and menu options. make
duo-matches plays 10 bot duos matches.

Closes #30

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 03:01:09 -05:00
co-authored by Claude Opus 5.5
parent 324bf84dd5
commit 2167900e45
53 changed files with 1105 additions and 61 deletions
+1
View File
@@ -147,6 +147,7 @@ func _show_player(entity: SimEntity, record: PackedInt32Array, shown: Dictionary
entity.crouching = flags & NetSnapshot.FLAG_CROUCHING != 0
entity.swimming = flags & NetSnapshot.FLAG_SWIMMING != 0
entity.eliminated = flags & NetSnapshot.FLAG_ELIMINATED != 0
entity.downed = flags & NetSnapshot.FLAG_DOWNED != 0
entity.drop_state = (flags >> NetSnapshot.FLAG_DROP_SHIFT) & 3
# Only whether they last, for drawing: the ticks left aren't replicated.
entity.reload_ticks = 1 if flags & NetSnapshot.FLAG_RELOADING != 0 else 0
+1 -1
View File
@@ -15,7 +15,7 @@ class_name ClientPrediction
extends RefCounted
## The flags movement reads and changes.
const _MOVEMENT_FLAGS: int = NetSnapshot.FLAG_ON_GROUND | NetSnapshot.FLAG_SWIMMING | NetSnapshot.FLAG_ELIMINATED | (3 << NetSnapshot.FLAG_DROP_SHIFT)
const _MOVEMENT_FLAGS: int = NetSnapshot.FLAG_ON_GROUND | NetSnapshot.FLAG_SWIMMING | NetSnapshot.FLAG_ELIMINATED | NetSnapshot.FLAG_DOWNED | (3 << NetSnapshot.FLAG_DROP_SHIFT)
## Predicted ticks kept to compare with the server: four seconds.
const HISTORY_TICKS: int = 120
+7 -4
View File
@@ -7,7 +7,8 @@
## server -> FEED
## u8 type, u8 standing, u8 players in the match, u8 entry count, then entries:
## s32 victim handle, s32 eliminator handle (0 = none), u8 cause (CAUSES),
## u8 item code plus one (0 = none), u8 place, u8 flags (1 = tied),
## u8 item code plus one (0 = none), u8 place, u8 flags (1 = tied, 2 = downed,
## not out: duos, M7),
## str victim name, str eliminator name,
## where str is u8 byte length then that many UTF-8 bytes (at most MAX_NAME_BYTES).
##
@@ -23,11 +24,12 @@ const ENTRY_FIXED_BYTES: int = 12
const MAX_ENTRIES: int = 16
const MAX_NAME_BYTES: int = 32
const FLAG_TIED: int = 1
const FLAG_DOWNED: int = 2
## Encodes the count and `entries`, newest last. Each entry has "victim", "by",
## "cause" (a name in CAUSES), "item" (-1 none), "place", "tied", "victim_name" and
## "by_name". Only the newest MAX_ENTRIES are kept.
## "cause" (a name in CAUSES), "item" (-1 none), "place", "tied", "downed", "victim_name"
## and "by_name". Only the newest MAX_ENTRIES are kept.
static func encode(standing: int, total: int, entries: Array[Dictionary]) -> PackedByteArray:
var out: StreamPeerBuffer = StreamPeerBuffer.new()
out.put_u8(MSG_FEED)
@@ -42,7 +44,7 @@ static func encode(standing: int, total: int, entries: Array[Dictionary]) -> Pac
out.put_u8(maxi(0, CAUSES.find(String(entry.get("cause", "none")))))
out.put_u8(clampi(int(entry.get("item", -1)) + 1, 0, 255))
out.put_u8(clampi(int(entry.get("place", 0)), 0, 255))
out.put_u8(FLAG_TIED if bool(entry.get("tied", false)) else 0)
out.put_u8((FLAG_TIED if bool(entry.get("tied", false)) else 0) | (FLAG_DOWNED if bool(entry.get("downed", false)) else 0))
_put_name(out, String(entry.get("victim_name", "")))
_put_name(out, String(entry.get("by_name", "")))
return out.data_array
@@ -67,6 +69,7 @@ static func decode(bytes: PackedByteArray) -> Dictionary:
var entry: Dictionary = {
"victim": bytes.decode_s32(at), "by": bytes.decode_s32(at + 4), "cause": CAUSES[cause],
"item": int(bytes[at + 9]) - 1, "place": int(bytes[at + 10]), "tied": bytes[at + 11] & FLAG_TIED != 0,
"downed": bytes[at + 11] & FLAG_DOWNED != 0,
}
at += ENTRY_FIXED_BYTES
var names: Array[String] = []
+1 -1
View File
@@ -16,7 +16,7 @@ class_name NetProtocol
extends RefCounted
## Raised whenever the wire format changes in a way old clients can't read.
const VERSION: int = 4
const VERSION: int = 5
const MAGIC: String = "TDWL"
const CHANNEL_CONTROL: int = 0
## Unreliable game traffic: commands up, snapshots down.
+10 -5
View File
@@ -38,8 +38,8 @@ const MATCH_BYTES: int = 14
## told. Exact, not quantized, so a prediction made from it matches the server's to the
## bit: position and velocity (three f32 each, which is all a Vector3 holds), the water
## level around the player (f64), which of the nine flood cells around the player that
## water reaches (u16), swimming stamina in ticks (u16) and the player's FLAG_ bits (u8).
const SELF_BYTES: int = 37
## water reaches (u16), swimming stamina in ticks (u16) and the player's FLAG_ bits (u16).
const SELF_BYTES: int = 38
## Every bit of the nine-cell water mask.
const WATER_EVERYWHERE: int = 0x1FF
## The most commands one INPUT may carry. Clients send fewer; see Tuning.
@@ -80,6 +80,8 @@ const FLAG_ELIMINATED: int = 1 << 3
const FLAG_DROP_SHIFT: int = 4
const FLAG_RELOADING: int = 1 << 6
const FLAG_USING: int = 1 << 7
## Down, not out (duos, M7): crawling and waiting for a teammate.
const FLAG_DOWNED: int = 1 << 8
const _U8: int = 0
const _S8: int = 1
@@ -89,7 +91,7 @@ const _WIDTH: Array[int] = [1, 1, 2, 4]
## Per archetype: the wire type of every record index.
const _TYPES: Dictionary = {
SimEntity.ARCHETYPE_PLAYER: [_U8, _U16, _U16, _U16, _U16, _S8, _U8, _U8, _U8, _U8],
SimEntity.ARCHETYPE_PLAYER: [_U8, _U16, _U16, _U16, _U16, _S8, _U8, _U8, _U16, _U8],
SimEntity.ARCHETYPE_PICKUP: [_U8, _U16, _U16, _U16, _U8, _U16, _U8],
SimEntity.ARCHETYPE_PROJECTILE: [_U8, _U16, _U16, _U16, _U8, _U32],
}
@@ -157,6 +159,8 @@ static func flags_of(entity: SimEntity) -> int:
flags |= FLAG_RELOADING
if entity.use_ticks > 0:
flags |= FLAG_USING
if entity.downed:
flags |= FLAG_DOWNED
return flags
@@ -180,6 +184,7 @@ static func apply_self(player: SimEntity, own: Dictionary) -> void:
player.crouching = flags & FLAG_CROUCHING != 0
player.swimming = flags & FLAG_SWIMMING != 0
player.eliminated = flags & FLAG_ELIMINATED != 0
player.downed = flags & FLAG_DOWNED != 0
player.drop_state = (flags >> FLAG_DROP_SHIFT) & 3
@@ -257,7 +262,7 @@ static func encode_snapshot(header: Dictionary, removed: PackedInt32Array, recor
out.put_double(own["water"])
out.put_u16(int(own["water_mask"]) & WATER_EVERYWHERE)
out.put_u16(clampi(int(own["stamina"]), 0, 65535))
out.put_u8(int(own["flags"]) & 0xFF)
out.put_u16(int(own["flags"]) & 0xFFFF)
out.put_u8(removed.size())
for entity_id: int in removed:
_put_varint(out, entity_id)
@@ -325,7 +330,7 @@ static func decode_snapshot(bytes: PackedByteArray) -> Dictionary:
"water": bytes.decode_double(at + 24),
"water_mask": bytes.decode_u16(at + 32) & WATER_EVERYWHERE,
"stamina": bytes.decode_u16(at + 34),
"flags": bytes[at + 36],
"flags": bytes.decode_u16(at + 36),
}
for value: float in [own["position"].x, own["position"].y, own["position"].z, own["velocity"].x, own["velocity"].y, own["velocity"].z, own["water"]]:
if not is_finite(value):