extends RefCounted ## The resource economy: ResourceStore rules (pouch capacity, deposit, paying pouch first then ## shed, all-or-nothing, validation), FarmState keeping materials, gather points, and upgrades ## through JSON, and on the real farm (FarmSim stepped by hand): the gather points' layout, ## gathering by day only (charges, rooted, cancelled by letting go / a hit / fear, a full pouch, ## nightfall), the shed drop-off, overnight regrowth, retry restoring the morning's materials, ## patching's straw cost, and buying the Straw Stuffing upgrade from the workbench's menu. const P := CampaignController.Phase const DT := 1.0 / 60.0 func run(r: GameTestRunner) -> void: _store(r) _state(r) var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D r.root.add_child(farm) await r.process_frame await r.physics_frame var sim := farm.get_node("FarmSim") as FarmSim sim.set_physics_process(false) sim.waves.enabled = false var player := farm.get_node("Player") as PlayerCharacter _layout(r, sim) _gathering(r, sim, player) _deposit_and_night(r, sim, player) _patch_cost(r, sim) _stuffing(r, sim, player) farm.queue_free() await r.process_frame func _store(r: GameTestRunner) -> void: var s := ResourceStore.new() r.check(s.add_to_pouch("wood", 10) == 10 and s.add_to_pouch("straw", 10) == 5 and s.pouch_total() == 15 and s.pouch_room() == 0, "the pouch takes only what fits (15 units across materials)") var moved := s.deposit() r.check(moved == {"wood": 10, "straw": 5} and s.pouch_total() == 0 and s.shed["wood"] == 10, "deposit moves the whole pouch into the shed") s = ResourceStore.new() s.shed["wood"] = 3 s.pouch["wood"] = 2 r.check(s.available("wood") == 5 and s.pay({"wood": 4}) and s.pouch["wood"] == 0 and s.shed["wood"] == 1, "costs are paid from the pouch first, then the shed") r.check(not s.pay({"wood": 2}) and s.shed["wood"] == 1 and s.missing({"wood": 2}) == {"wood": 1}, "a cost that can't be met pays nothing and says what's missing") s.shed["straw"] = 5 r.check(not s.pay({"straw": 1, "scrap": 3}) and s.shed["straw"] == 5, "a cost is all-or-nothing across materials") r.check(ResourceStore.describe({"straw": 2, "wood": 6}) == "6 wood, 2 straw", "costs read in material order") s.pouch["scrap"] = 4 var back := ResourceStore.from_dict(JSON.parse_string(JSON.stringify(s.to_dict()))) r.check(back != null and back.to_dict() == s.to_dict(), "the store round-trips through JSON") var bad := { "a negative amount": func(d: Dictionary) -> void: d["shed"]["wood"] = -1, "an unknown material": func(d: Dictionary) -> void: d["pouch"]["gold"] = 1, "an overfull pouch": func(d: Dictionary) -> void: d["pouch"]["wood"] = 99, "a missing part": func(d: Dictionary) -> void: d.erase("shed"), } for label: String in bad: var d := s.to_dict() d["shed"] = (d["shed"] as Dictionary).duplicate() d["pouch"] = (d["pouch"] as Dictionary).duplicate() (bad[label] as Callable).call(d) r.check(ResourceStore.from_dict(d) == null, "the store rejects %s" % label) func _state(r: GameTestRunner) -> void: var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig var s := FarmState.create(cfg) r.check(s.resources.shed == {"wood": cfg.start_wood, "scrap": cfg.start_scrap, "straw": cfg.start_straw} and s.resources.pouch_capacity == cfg.pouch_capacity, "a new farm starts with the configured stock and pouch size") s.resources.add_to_pouch("scrap", 3) s.gather_charges = {"Snag": 1, "Wreck": 0} s.upgrades = {"straw_stuffing": 1} var d := s.to_dict() var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(d))) r.check(back != null and back.to_dict() == d, "FarmState keeps materials, used gather points, and upgrades through JSON") d["gather_charges"] = {"Snag": -2} r.check(FarmState.from_dict(d) == null, "FarmState rejects negative gather charges") func _layout(r: GameTestRunner, sim: FarmSim) -> void: var by_material := {} var names := {} for n in sim.interactions.nodes: by_material[n.material] = int(by_material.get(n.material, 0)) + 1 names[String(n.name)] = true var zone := sim.zones.zone_at(n.global_position) var want := {"wood": FarmZone.Kind.WOOD, "scrap": FarmZone.Kind.SCRAP, "straw": FarmZone.Kind.STRAW}[n.material] as FarmZone.Kind r.check(zone != null and zone.kind == want and n.get_child_count() >= 2, "%s (%s) stands in its zone with its props" % [n.name, n.material]) var inside := {} var outside := {} for n in sim.interactions.nodes: var bucket := inside if sim.zones.is_inside_fence(n.global_position) else outside bucket[n.material] = int(bucket.get(n.material, 0)) + 1 if bucket == outside: r.check(n.yield_amount > 2 and sim.zones.zone_at(n.global_position).outside, "%s, outside the fence, yields more (%d)" % [n.name, n.yield_amount]) for m in ResourceStore.MATERIALS: r.check(int(inside.get(m, 0)) >= 3 and int(inside.get(m, 0)) <= 5, "3-5 gather points for %s on the farm (%d)" % [m, int(inside.get(m, 0))]) r.check(int(outside.get(m, 0)) >= 2, "and more outside the fence (%d)" % int(outside.get(m, 0))) r.check(sim.zones.outside_zones().size() == 4 and sim.zones.zone(FarmZone.Kind.WOOD).display_name == "Woodlot", "four gathering areas outside, and the farm's own zones still come first") r.check(names.size() == sim.interactions.nodes.size(), "gather point names are unique (they key the farm state)") # The scarecrow's body must be able to stand within reach of every point (and the workbench), # walking there from its start: out through the gaps for the points outside. var nav := FarmNavigation.new() nav.build(sim.get_parent(), sim.zones.roam, 0.5, FarmSim.SCARECROW_RADIUS, 1 | 4) var from := nav.nearest_free(nav.cell_of(sim.player.global_position.x, sim.player.global_position.z)) var walk := nav.distance_field(PackedInt32Array([from])) var spots: Array[Node3D] = [] spots.assign(sim.interactions.nodes) spots.append(sim.interactions.workbench) for n in spots: var reach: float = n.reach var c := Vector2(n.global_position.x, n.global_position.z) var closest := INF for cell in nav.free_cells_in(Rect2(c - Vector2.ONE * reach, Vector2.ONE * reach * 2.0)): if walk[cell] < FarmNavigation.UNREACHABLE: closest = minf(closest, nav.cell_center(cell).distance_to(c)) r.check(closest <= reach - 0.25, "the scarecrow can walk to within reach of %s (%.1f of %.1f m)" % [n.name, closest, reach]) r.check(sim.interactions.workbench != null and sim.zones.zone(FarmZone.Kind.WORK_AREA).contains(sim.interactions.workbench.global_position), "the workbench stands in the work area") func _gathering(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var it := sim.interactions var res := sim.campaign.state.resources var tree := _node(it, "DeadTree") var msgs: Array[String] = [] it.message.connect(func(t: String, _c: Color) -> void: msgs.append(t)) _stand_at(player, tree) _hold(sim, 30) r.check(res.pouch_total() == 0 and msgs.has("Gathering is for daytime"), "no gathering before the day starts (once per press: %d)" % msgs.size()) Input.action_release("interact") sim.campaign.begin_day() _step(sim, 1) _hold(sim, 30) r.check(it.gathering == tree and player.rooted and not it.can_shoot(), "holding interact gathers: rooted, no shooting") _hold(sim, roundi(sim.config.gather_time * 60.0) - 30 + 2) r.check(res.pouch["wood"] == tree.yield_amount and it.charges(tree) == tree.max_charges - 1, "after %.1f s: +%d wood in the pouch, one charge used" % [sim.config.gather_time, tree.yield_amount]) _hold(sim, roundi(sim.config.gather_time * 60.0) + 4) r.check(it.charges(tree) == 0 and res.pouch["wood"] == tree.yield_amount * 2, "holding on gathers again until the point runs out") _hold(sim, 10) r.check(it.gathering == null and msgs.has("Nothing left here; it grows back overnight"), "a used-up point can't be gathered") # Next to both a used-up point and a full one, interact goes to the full one. var logs := _node(it, "LogPile") player.global_position = (tree.global_position + logs.global_position) * 0.5 player.global_position.y = 0.0 Input.action_release("interact") _step(sim, 1) var both := Vector2(player.global_position.x - tree.global_position.x, player.global_position.z - tree.global_position.z).length() <= tree.reach \ and Vector2(player.global_position.x - logs.global_position.x, player.global_position.z - logs.global_position.z).length() <= logs.reach _hold(sim, 5) r.check(both and it.gathering == logs, "between a used-up point and a full one, the full one is gathered") Input.action_release("interact") _step(sim, 1) res.pouch["wood"] = tree.yield_amount * 2 Input.action_release("interact") _step(sim, 1) var snag := _node(it, "Snag") _stand_at(player, snag) _hold(sim, 40) Input.action_release("interact") _step(sim, 1) r.check(it.gathering == null and it.charges(snag) == snag.max_charges and not player.rooted, "letting go early gathers nothing") _hold(sim, 40) sim.scarecrow.try_damage(1) r.check(it.gathering == null, "a hit stops gathering") sim.scarecrow.invulnerable_left = 0.0 sim.campaign.state.scarecrow_hp = sim.campaign.state.scarecrow_max_hp Input.action_release("interact") _step(sim, 1) _hold(sim, 40) sim.scarecrow.use_fear() sim.scarecrow.fear_left = 0.0 r.check(it.gathering == null, "using fear stops gathering") Input.action_release("interact") _step(sim, 1) res.add_to_pouch("scrap", res.pouch_room() - 1) _hold(sim, 20) r.check(it.gathering == null and msgs.has("Pouch full: drop it off at the supply shed") and it.charges(snag) == snag.max_charges, "without room for the whole yield, nothing is gathered (and no charge is wasted)") Input.action_release("interact") _step(sim, 1) func _deposit_and_night(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var c := sim.campaign var res := c.state.resources var it := sim.interactions var shed_before: Dictionary = res.shed.duplicate() var pouch: Dictionary = res.pouch.duplicate() var moved: Array[Dictionary] = [] it.deposited.connect(func(m: Dictionary) -> void: moved.append(m)) var shed := sim.zones.zone(FarmZone.Kind.SUPPLY_SHED) player.global_position = shed.global_position + Vector3(0.0, 0.0, 2.0) _step(sim, 1) r.check(res.pouch_total() == 0 and moved.size() == 1 and res.shed["wood"] == int(shed_before["wood"]) + int(pouch["wood"]) and res.shed["scrap"] == int(shed_before["scrap"]) + int(pouch["scrap"]), "walking into the supply shed stores the pouch") var tree := _node(it, "DeadTree") var deadfall := _node(it, "Deadfall") c.state.gather_charges["Deadfall"] = 0 deadfall.regen_per_night = 1 c.advance_phase() c.advance_phase() r.check(c.state.phase == P.NIGHT, "night falls") _stand_at(player, _node(it, "LogPile")) _hold(sim, 30) r.check(it.gathering == null and res.pouch_total() == 0, "no gathering at night") Input.action_release("interact") _step(sim, 1) c.advance_phase() c.advance_phase() r.check(c.state.phase == P.MORNING and it.charges(tree) == tree.max_charges and it.charges(deadfall) == 1, "overnight the points grow back (a point regrowing 1 a night has 1 again)") deadfall.regen_per_night = 20 # Retry brings back the morning's materials and points. var morning := c.state.to_dict() c.begin_day() _stand_at(player, _node(it, "Wreck")) _hold(sim, roundi(sim.config.gather_time * 60.0) + 2) Input.action_release("interact") _step(sim, 1) r.check(res.pouch["scrap"] > 0, "scrap gathered on day 2") c.advance_phase() c.advance_phase() c.state.scarecrow_hp = 0 _step(sim, 1) c.retry_night() _step(sim, 1) r.check(c.state.resources.to_dict() == morning["resources"] and c.state.gather_charges == morning["gather_charges"], "retry restores the morning's materials and gather points") func _patch_cost(r: GameTestRunner, sim: FarmSim) -> void: var s := sim.campaign.state var kit := sim.scarecrow var denied: Array[Dictionary] = [] kit.patch_denied.connect(func(m: Dictionary) -> void: denied.append(m)) s.resources.shed["straw"] = 2 s.resources.pouch["straw"] = 0 s.scarecrow_hp = 5 Input.action_press("patch") _step(sim, roundi(sim.config.patch_time * 60.0) + 2) r.check(s.scarecrow_hp == 5 + sim.config.patch_heal and s.resources.available("straw") == 0, "a patch costs 2 straw") _step(sim, 30) r.check(not kit.patching and denied.size() == 1 and denied[0] == {"straw": 2}, "without straw the patch is refused, once per press") Input.action_release("patch") _step(sim, 1) s.scarecrow_hp = s.scarecrow_max_hp func _stuffing(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var c := sim.campaign var s := c.state var it := sim.interactions var bench := it.workbench var menu := sim.upgrade_panel var add := int(sim.config.upgrades["straw_stuffing"]["add"]) var msgs: Array[String] = [] it.message.connect(func(t: String, _col: Color) -> void: msgs.append(t)) menu.message.connect(func(t: String, _col: Color) -> void: msgs.append(t)) player.global_position = bench.global_position + Vector3(0.0, 0.0, 1.2) _press(sim) r.check(not menu.is_open and msgs[-1] == "The work area is for daytime", "no upgrades during the morning review") c.begin_day() s.resources.shed = {"wood": 0, "scrap": 0, "straw": 3} _press(sim) r.check(menu.is_open and player.rooted and not it.can_shoot(), "interact at the workbench opens the upgrade menu; the scarecrow stands still") var row := menu._rows["straw_stuffing"] as Button r.check(row.disabled and row.text.contains("Need 4 wood, 9 straw more"), "short of materials, the row says what's missing: '%s'" % row.text) s.resources.shed = {"wood": 3, "scrap": 0, "straw": 12} s.resources.pouch = {"wood": 1, "scrap": 0, "straw": 0} menu.refresh() var max_hp := s.scarecrow_max_hp row.pressed.emit() r.check(Upgrades.owned(s, "straw_stuffing") and s.scarecrow_max_hp == max_hp + add and s.scarecrow_hp == s.scarecrow_max_hp, "Straw stuffing: +%d max HP (%s)" % [add, msgs[-1]]) r.check(s.resources.shed == {"wood": 0, "scrap": 0, "straw": 0} and s.resources.pouch["wood"] == 0, "it costs 12 straw and 4 wood") r.check(row.disabled and row.text.contains("Done"), "it can only be bought once") _press(sim) r.check(not menu.is_open and not player.rooted, "interact again closes the menu") _press(sim) menu.close() func _node(it: Interactions, node_name: String) -> GatherNode: for n in it.nodes: if n.name == node_name: return n return null ## Puts the scarecrow next to a gather point, within its reach. func _stand_at(player: PlayerCharacter, n: GatherNode) -> void: var to_center := Vector3(n.global_position.x, 0.0, n.global_position.z) player.global_position = to_center + Vector3(n.reach * 0.7, 0.0, 0.0) func _hold(sim: FarmSim, ticks: int) -> void: Input.action_press("interact") _step(sim, ticks) func _press(sim: FarmSim) -> void: Input.action_release("interact") _step(sim, 1) Input.action_press("interact") _step(sim, 1) Input.action_release("interact") _step(sim, 1) func _step(sim: FarmSim, n: int) -> void: for i in n: sim.step(DT)