Cause: the open path itself worked. A real two-process run with an E key event opened the shed's menu when P2 stood at the counter. What failed in play was reach: - The partner's game never showed the counter's "[E / X] Shop" prompt. CoopSession._gather_node resolved only the workbench and gather points, so P2's replicated focus was null at the counter. P2 couldn't tell where the 2.2 m reach around the counter's invisible point was. - The host took P2's press on the first tick after the input record arrived, before its copy of P2 had eased to the position in that same record. It dropped the press silently if the lagging copy was out of reach. Walking in at 5 or 9 m/s and pressing E at 2.1 m (inside the reach on P2's screen) failed 3/3. The host's copy was 2.31-2.66 m out. Fixes: - The snapshot's focus now resolves the ShedCounter (_focus_node), so the prompt shows. - The host keeps a partner's interact press pending for 0.3 s (Interactions.PRESS_WINDOW) until a counter in reach takes it. - Interact while the partner's shop menu is open closes it on the partner's machine, and that press is taken back (RemoteControls.Sender.take_back), so the host never reopens it. - When the host completes P2's "buy", it sends a "bought" event. Once P2's farm state has the upgrade, the partner's panel emits bought (UpgradePanel.confirm_bought). As in solo, the new weapon goes into P2's hand and the jingle plays. Still paid from P2's pouch, then the shed. - CoopSession.PROTOCOL 4 -> 5. Tests: test_coop _shops covers both shops opening on the client only, the prompt, interact closing, a press taken while the host's copy is 3.8 m out still opening, an early press not opening later, and a client weapon buy paid from P2's pouch and the shed and equipped on both farms. coop_smoke now opens and closes the shed by day with E key events, both standing and on arrival at a run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
144 lines
5.1 KiB
GDScript
144 lines
5.1 KiB
GDScript
class_name RemoteControls
|
|
extends RefCounted
|
|
## What a scarecrow played on another machine wants this tick, as the host sees it (co-op,
|
|
## CoopSession). The partner's game sends its input record (Sender.record, RECORD_SIZE bytes)
|
|
## whenever it changes, and again at least every few frames: which actions are held, a running
|
|
## press count per action (so a press is never lost with a dropped packet: the host counts the
|
|
## new ones), its aim (and the ground point a pumpkin bomb would land on), the weapon in hand,
|
|
## whether it is building or has the upgrade menu open, and where its scarecrow is. The kit
|
|
## classes (Scarecrow, Interactions, ScarecrowCombat) read this instead of the local Input when
|
|
## their remote is set.
|
|
|
|
const ACTIONS: Array[StringName] = [&"fire", &"fear", &"patch", &"interact", &"offhand"]
|
|
## Held bits, a 16-bit count per action, the aim (2 x s16), the aim and ground points and the
|
|
## scarecrow's position (3 x s16 cm each), device, weapon, flags, facing (s16), speed.
|
|
const RECORD_SIZE := 1 + 2 * 5 + 4 + 6 + 6 + 3 + 6 + 2 + 1
|
|
|
|
var aim := Vector2(0.0, -1.0)
|
|
var aim_point := Vector3.ZERO
|
|
var ground_point := Vector3.ZERO
|
|
## ScarecrowCombat.Device of the partner's aim, and its main ScarecrowCombat.Weapon (-1: none
|
|
## said).
|
|
var device: int = 0
|
|
var weapon: int = -1
|
|
var build_active := false
|
|
var ui_open := false
|
|
## The partner's scarecrow, as its machine moved it (CoopSession puts this on the host's copy).
|
|
var position := Vector3.ZERO
|
|
var yaw: float = 0.0
|
|
var speed: float = 0.0
|
|
|
|
var _held := 0
|
|
var _presses := RemoteControls.counts()
|
|
var _seen := RemoteControls.counts()
|
|
|
|
|
|
## A count per action, all 0.
|
|
static func counts() -> PackedInt32Array:
|
|
var c := PackedInt32Array()
|
|
c.resize(ACTIONS.size())
|
|
return c
|
|
|
|
|
|
func held(action: StringName) -> bool:
|
|
var i := ACTIONS.find(action)
|
|
return i >= 0 and (_held & (1 << i)) != 0
|
|
|
|
|
|
## Whether the action was pressed since the last time this was asked (one press each time).
|
|
func take(action: StringName) -> bool:
|
|
var i := ACTIONS.find(action)
|
|
if i < 0 or _seen[i] >= _presses[i]:
|
|
return false
|
|
_seen[i] += 1
|
|
return true
|
|
|
|
|
|
## The partner's input record (Sender.record). False if it isn't one.
|
|
func apply(rec: PackedByteArray) -> bool:
|
|
if rec.size() != RECORD_SIZE:
|
|
return false
|
|
var b := CoopLink.reader(rec)
|
|
_held = b.get_u8()
|
|
for i in ACTIONS.size():
|
|
var step := (b.get_u16() - _presses[i]) & 0xFFFF # the counts go round at 65536
|
|
if step < 0x8000:
|
|
_presses[i] += step
|
|
var ax := b.get_16() / 10000.0
|
|
aim = Vector2(ax, b.get_16() / 10000.0)
|
|
aim_point = CoopLink.get_v3(b)
|
|
ground_point = CoopLink.get_v3(b)
|
|
device = b.get_u8()
|
|
weapon = b.get_8()
|
|
var flags := b.get_u8()
|
|
build_active = (flags & 1) != 0
|
|
ui_open = (flags & 2) != 0
|
|
position = CoopLink.get_v3(b)
|
|
yaw = CoopLink.get_angle(b)
|
|
speed = b.get_u8() / 100.0
|
|
return true
|
|
|
|
|
|
## Forget held actions (the partner left, or a new morning).
|
|
func release() -> void:
|
|
_held = 0
|
|
_seen = _presses.duplicate()
|
|
|
|
|
|
## The local side: an input record for the actions held now and every press so far.
|
|
class Sender:
|
|
var presses := RemoteControls.counts()
|
|
var _was := RemoteControls.counts()
|
|
## The press counts in the last record.
|
|
var _recorded := RemoteControls.counts()
|
|
|
|
## Counts presses from input events (taps between two frames still count).
|
|
func note(event: InputEvent) -> void:
|
|
for i in RemoteControls.ACTIONS.size():
|
|
if event.is_action_pressed(RemoteControls.ACTIONS[i], false):
|
|
presses[i] += 1
|
|
_was[i] = 1
|
|
|
|
## Takes back the presses of an action made since the last record, so they never reach the
|
|
## host: this machine used them itself (interact closing the partner's own shop menu, #22).
|
|
## Returns how many there were.
|
|
func take_back(action: StringName) -> int:
|
|
var i := RemoteControls.ACTIONS.find(action)
|
|
if i < 0:
|
|
return 0
|
|
_sample(i)
|
|
var n := presses[i] - _recorded[i]
|
|
presses[i] = _recorded[i]
|
|
return n
|
|
|
|
## The input record (RemoteControls.apply): the actions held now (none while frozen: a menu
|
|
## is up), every press so far, and the scarecrow.
|
|
func record(combat: ScarecrowCombat, body: PlayerCharacter, build: bool, ui: bool, frozen: bool) -> PackedByteArray:
|
|
var bits := 0
|
|
for i in RemoteControls.ACTIONS.size():
|
|
if _sample(i):
|
|
bits |= 1 << i
|
|
_recorded = presses.duplicate()
|
|
var b := StreamPeerBuffer.new()
|
|
b.put_u8(0 if frozen else bits)
|
|
for i in RemoteControls.ACTIONS.size():
|
|
b.put_u16(presses[i] & 0xFFFF)
|
|
b.put_16(clampi(roundi(combat.aim.x * 10000.0), -32767, 32767))
|
|
b.put_16(clampi(roundi(combat.aim.y * 10000.0), -32767, 32767))
|
|
CoopLink.put_v3(b, combat.aim_point)
|
|
CoopLink.put_v3(b, combat.ground_point)
|
|
b.put_u8(int(combat.device))
|
|
b.put_8(int(combat.weapon))
|
|
b.put_u8((1 if build else 0) | (2 if ui else 0))
|
|
CoopLink.put_v3(b, body.global_position)
|
|
CoopLink.put_angle(b, body.model.rotation.y)
|
|
b.put_u8(clampi(roundi(body.move_strength() * 100.0), 0, 255))
|
|
return b.data_array
|
|
|
|
## Whether action i is held now; a press without an input event (simulated input) counts.
|
|
func _sample(i: int) -> bool:
|
|
var down := Input.is_action_pressed(RemoteControls.ACTIONS[i])
|
|
if down and _was[i] == 0:
|
|
presses[i] += 1
|
|
_was[i] = 1 if down else 0
|
|
return down
|