Task #22: the joining player can open the store

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>
This commit is contained in:
Forrest 2026-10-06 13:47:52 -04:00
parent 287b6ac6cc
commit 315e6890f7
7 changed files with 305 additions and 23 deletions

View file

@ -11,7 +11,9 @@ extends Node
## refused (reliable) the partner's game speaks another PROTOCOL
## batch (reliable, each tick with news) events in order: phase changes (with the state),
## the end of the campaign, sounds, toasts for the partner, loot and arrival news,
## breaches, fear bursts, gather popups, the workbench; enemies removed
## breaches, fear bursts, gather popups, opening a shop's menu (the workbench's, the
## shed's), a purchase the partner made (bought: the slot and the upgrade id, so a
## new weapon goes into its hand); enemies removed
## state (reliable, at most STATE_HZ, when it changed) the farm state (the phase clock
## alone isn't a change: it is in every snapshot)
## loot (reliable, at most LOOT_HZ, when it changed) every item
@ -22,7 +24,8 @@ extends Node
## hello (reliable) its player's name and PROTOCOL
## input (unreliable, up to INPUT_HZ when it changed, else every INPUT_KEEPALIVE) its
## scarecrow's position and facing, held buttons and press counts, aim, build mode,
## the upgrade menu (RemoteControls, packed bytes)
## whether a shop's menu is up (RemoteControls, packed bytes). A press of interact
## that closes the partner's own menu stays on its machine (Sender.take_back)
## req (reliable) start the day, start the night early, retry the night, build,
## dismantle, buy an upgrade
## Unreliable messages stay under CoopLink.UNRELIABLE_LIMIT; nothing depends on any single one
@ -45,8 +48,10 @@ signal welcomed
## The wire format: both machines must speak the same one (hello and welcome carry it). 3: #10
## added a time field and two int32s (the upgrade target/progress) to _put_avatar/_read_avatar's
## per-avatar snapshot record. 4: #16 gave each scarecrow its own pouch (the farm state's
## resources.partner_pouch, FarmState.VERSION 7).
const PROTOCOL := 4
## resources.partner_pouch, FarmState.VERSION 7). 5: #22's partner shops: the "bought" event, the
## shed's counter as a snapshot's focus, and the partner's press of interact that closes its own
## menu no longer sent (the host keeps a press pending until P2's copy is in reach).
const PROTOCOL := 5
const SNAP_EVERY := 4 # physics ticks: 15 Hz
const INPUT_EVERY := 3 # 20 Hz, when it changed
## Seconds: the partner's input goes out at least this often, changed or not.
@ -106,6 +111,8 @@ var _input_age: float = 0.0
var _snap_tick: int = -1
var _structures_json := ""
var _shed_json := ""
## Upgrade ids the host bought for this machine's scarecrow, until this farm's state has them.
var _bought: Array[String] = []
var _is_welcomed := false
var _welcome_sent := false
var _hello_wait: float = 0.0
@ -555,8 +562,11 @@ func _do_request(r: Array) -> void:
if Upgrades.shop_of(c.config, id) == "workbench" and (sim.structures == null or not sim.structures.workbench_available()):
_events.append(["toast", "The work area isn't available", Color(1.0, 0.8, 0.5)])
return
var was := Upgrades.owned(c.state, id)
var text := Upgrades.buy(c, id, sim.structures, a.slot) # paid from P2's pouch, then the shed
_events.append(["toast", text, Color(0.85, 0.75, 1.0) if text.contains("done:") else Color(1.0, 0.8, 0.5)])
if not was and Upgrades.owned(c.state, id):
_events.append(["bought", a.slot, id]) # the buyer's game: a new weapon into its hand
## Co-op is on for the farm state (after a retry, a new vacation, or a morning).
@ -646,6 +656,19 @@ func _apply_state(d: Dictionary, everything: bool = false) -> void:
sim.shed_panel.refresh()
if c.state.phase == CampaignController.Phase.MORNING and sim.review_panel != null:
sim.review_panel.refresh()
_settle_bought()
## The purchases the host made for this machine's scarecrow that this farm's state has by now
## count as bought here (UpgradePanel.confirm_bought: a new weapon goes into its hand). The
## "bought" news can come before the farm state that has the upgrade.
func _settle_bought() -> void:
for id in _bought.duplicate():
if Upgrades.owned(sim.campaign.state, id):
_bought.erase(id)
var panel := sim.shed_panel if Upgrades.shop_of(sim.config, id) == "shed" else sim.upgrade_panel
if panel != null:
panel.confirm_bought(id)
func _apply_event(e: Array) -> void:
@ -705,6 +728,10 @@ func _apply_event(e: Array) -> void:
"shed":
if sim.shed_panel != null and c.is_daytime():
sim.shed_panel.open()
"bought":
if int(e[1]) == sim.local_avatar.slot:
_bought.append(String(e[2]))
_settle_bought()
func _apply_snap(data: PackedByteArray) -> void:
@ -794,8 +821,8 @@ func _apply_avatar(a: Avatar, rec: Dictionary) -> void:
elif ch == "" and body.rooted and not sim._menu_open():
body.stop_channel()
var it := a.interactions
it.focus = _gather_node(String(rec["fo"]))
it.gathering = _gather_node(String(rec["ga"])) as GatherNode
it.focus = _focus_node(String(rec["fo"]))
it.gathering = _focus_node(String(rec["ga"])) as GatherNode
it.gather_elapsed = float(rec["ge"])
it.repair_target = int(rec["rt"])
it.repairing = int(rec["rp"])
@ -805,12 +832,17 @@ func _apply_avatar(a: Avatar, rec: Dictionary) -> void:
it.upgrade_elapsed = float(rec["ue"])
func _gather_node(node_name: String) -> Node3D:
## What a snapshot names as P2's focus (or gathering point) on this farm: a gather point, the
## workbench, or the shed's counter, so the local labels show the prompt the host's hands have
## (Interactions._process: "[E / X] Shop" at the counter, #22).
func _focus_node(node_name: String) -> Node3D:
if node_name == "":
return null
var it := sim.interactions
if it.workbench != null and String(it.workbench.name) == node_name:
return it.workbench
if it.shed_counter != null and String(it.shed_counter.name) == node_name:
return it.shed_counter
for n in it.nodes:
if String(n.name) == node_name:
return n
@ -908,6 +940,8 @@ func _reset_world() -> void:
## replaced soon; standing still sends little).
func _send_input() -> void:
var a := sim.local_avatar
if sim._menu_open() and _sender.take_back(&"interact") > 0:
_close_menus() # interact again closes it, as in solo; that press never reaches the host
var ui := sim._menu_open()
var rec := _sender.record(a.combat, a.body, sim.build.active, ui, sim.local_frozen)
if rec != _last_input or _input_age >= INPUT_KEEPALIVE - 0.001: # (sums of 1/60 fall a hair short)
@ -922,13 +956,19 @@ func _unhandled_input(event: InputEvent) -> void:
_sender.note(event)
## Either shop's menu closes here on the same rules as solo (Interactions): walking off its
## counter's reach, nightfall, build mode.
## Either shop's menu closes here on the same rules as solo (Interactions): interact again (in
## _send_input), walking off its counter's reach, nightfall, build mode.
func _close_menu_if_needed() -> void:
_close_menu(sim.upgrade_panel, sim.interactions.workbench)
_close_menu(sim.shed_panel, sim.interactions.shed_counter)
func _close_menus() -> void:
for panel: UpgradePanel in [sim.upgrade_panel, sim.shed_panel]:
if panel != null:
panel.close()
## counter: Workbench or ShedCounter (both just need global_position and reach).
func _close_menu(panel: UpgradePanel, counter) -> void:
if panel == null or not panel.is_open:

View file

@ -32,7 +32,10 @@ extends Node3D
## RemoteControls (remote), asks the partner's game to open its own upgrade menu
## (workbench_requested / shed_requested), and leaves the gather labels to the local player's
## (show_labels). Each one gathers into, deposits, and pays from its own scarecrow's pouch
## (kit.slot, ResourceStore): a partner standing in the shed banks only their own pouch.
## (kit.slot, ResourceStore): a partner standing in the shed banks only their own pouch. The
## host's copy of the partner trails the partner's own machine by a few ticks, so a press of
## interact made on arrival at a shop's counter can reach the host while the copy is still out of
## reach: it stays pending for PRESS_WINDOW seconds, until a counter in reach takes it (#22).
signal gathered(material: String, amount: int, node: GatherNode)
signal deposited(moved: Dictionary)
@ -46,6 +49,8 @@ const LABEL_RANGE := 7.0
const REPAIR_REACH := 1.4
const POPUP_TIME := 1.1
const POPUPS := 6
## Seconds a partner's press of interact waits (on the host) for its copy to reach a counter.
const PRESS_WINDOW := 0.3
var config: GameConfig
var campaign: CampaignController
@ -78,6 +83,8 @@ var upgrade_elapsed: float = 0.0
var _shed: FarmZone
var _interact_requested := false
var _interact_was_held := false
## Co-op: what's left of the partner's pending press (seconds; 0 = none).
var _press_left: float = 0.0
var _denied := false
var _popups: Array[Label3D] = []
var _repair_label: Label3D
@ -158,7 +165,7 @@ func step(dt: float) -> void:
var held := remote.held(&"interact") if remote != null else Input.is_action_pressed("interact")
var pressed := _interact_requested or (held and not _interact_was_held)
if remote != null:
pressed = remote.take(&"interact")
pressed = _remote_press(dt)
_interact_requested = false
_interact_was_held = held
if not held:
@ -172,6 +179,7 @@ func step(dt: float) -> void:
repair_target = -1
upgrade_target = -1
focus = null
_press_left = 0.0
return
var p := Vector2(player.global_position.x, player.global_position.z)
if upgrade_panel != null and upgrade_panel.is_open:
@ -261,6 +269,7 @@ func step(dt: float) -> void:
_denied = true
message.emit(why, Color(1.0, 0.8, 0.5))
elif focus is Workbench and pressed:
_press_left = 0.0
if structures != null and structures.is_broken(structures.workbench_id()):
message.emit("Broken -- repair it", Color(1.0, 0.8, 0.5))
elif not campaign.is_daytime():
@ -270,6 +279,7 @@ func step(dt: float) -> void:
elif remote != null:
workbench_requested.emit()
elif focus is ShedCounter and pressed:
_press_left = 0.0
if not campaign.is_daytime():
message.emit("The supply shed is for daytime", Color(1.0, 0.8, 0.5))
elif shed_panel != null:
@ -286,6 +296,18 @@ func _step_menu(panel: UpgradePanel, counter, p: Vector2, pressed: bool) -> void
var reach: float = counter.reach if counter != null else 0.0
if pressed or not campaign.is_daytime() or at.distance_to(p) > reach + 0.5 or (build != null and build.active):
panel.close()
_press_left = 0.0
## Co-op, the partner's hands on the host: whether a press of interact is pending. A new one
## (RemoteControls.take) waits up to PRESS_WINDOW seconds for a counter in reach to take it
## (which clears _press_left), instead of being dropped on the tick it arrives.
func _remote_press(dt: float) -> bool:
if remote.take(&"interact"):
_press_left = PRESS_WINDOW
else:
_press_left = maxf(_press_left - dt, 0.0)
return _press_left > 0.0
## Why a point can't be gathered right now ("" if it can).

View file

@ -89,6 +89,8 @@ func release() -> void:
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:
@ -97,17 +99,26 @@ class Sender:
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():
var down := Input.is_action_pressed(RemoteControls.ACTIONS[i])
if down:
if _sample(i):
bits |= 1 << i
if _was[i] == 0: # pressed without an event (simulated input)
presses[i] += 1
_was[i] = 1 if down else 0
_recorded = presses.duplicate()
var b := StreamPeerBuffer.new()
b.put_u8(0 if frozen else bits)
for i in RemoteControls.ACTIONS.size():
@ -123,3 +134,11 @@ class Sender:
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

View file

@ -18,7 +18,8 @@ var player: PlayerCharacter
var is_open := false
## Which shop this instance lists ("workbench" or "shed"); set by setup().
var shop := "workbench"
## Co-op partner: ask the host to buy (upgrade id); the result comes back as a message.
## Co-op partner: ask the host to buy (upgrade id); the result comes back as a message, and a
## purchase that went through as confirm_bought.
var buy_request: Callable
## The avatar slot of this machine's scarecrow (FarmSim.set_local): what's affordable is its own
## pouch plus the shed.
@ -144,6 +145,19 @@ func _buy(id: String) -> void:
if not was and Upgrades.owned(campaign.state, id):
bought.emit(id)
message.emit(text, Color(0.85, 0.75, 1.0) if text.contains("done:") else Color(1.0, 0.8, 0.5))
_after_buy(id)
## Co-op partner: the host bought id for this machine's scarecrow (buy_request), and this
## machine's farm state has it now. It counts as bought here, as a purchase in solo does (bought:
## a new weapon goes into this scarecrow's hand, the jingle plays) (#22).
func confirm_bought(id: String) -> void:
bought.emit(id)
if is_open and _rows.has(id):
_after_buy(id)
func _after_buy(id: String) -> void:
refresh()
if (_rows[id] as Button).has_focus() and (_rows[id] as Button).disabled:
_close.grab_focus()