diff --git a/game/scripts/farm_navigation.gd b/game/scripts/farm_navigation.gd index 29a564b..d0bf860 100644 --- a/game/scripts/farm_navigation.gd +++ b/game/scripts/farm_navigation.gd @@ -289,7 +289,11 @@ func _rasterize(hull: PackedVector2Array, inflate: float) -> void: ## Marks the cells within inflate of a convex outline: as blocked (id < 0) or, for free cells, -## as belonging to structure id. +## as belonging to structure id. A structure (id >= 0) is tagged even over an already-blocked +## cell (#9: the Workbench, granted onto its own scene prop's real collision, bypassing the +## build-on-free-ground check every player-placed structure goes through in validate() -- without +## this, it would be invisible to _adjacent_structure()/barrier_ahead(), undetectable and +## unbreakable); this never changes anything for a structure that is on free ground to begin with. func _rasterize_into(hull: PackedVector2Array, inflate: float, id: int) -> void: var lo := hull[0] var hi := hull[0] @@ -304,7 +308,7 @@ func _rasterize_into(hull: PackedVector2Array, inflate: float, id: int) -> void: for cz in range(c0 / width, c1 / width + 1): for cx in range(c0 % width, c1 % width + 1): var i := cz * width + cx - if blocked[i] == 1: + if blocked[i] == 1 and id < 0: continue var p := cell_center(i) if (solid and Geometry2D.is_point_in_polygon(p, hull)) or _dist2_to_outline(p, hull) <= r2: diff --git a/game/scripts/farm_sim.gd b/game/scripts/farm_sim.gd index 3260920..d0aa91a 100644 --- a/game/scripts/farm_sim.gd +++ b/game/scripts/farm_sim.gd @@ -688,7 +688,12 @@ func _breaker_goals() -> PackedInt32Array: for e in campaign.state.structures: if not structures.blocks(e["type"]): continue + if e["type"] == "workbench" and int(e["hp"]) <= 0: + continue # broken: nothing left to gain by sending a breaker at it barriers.append(e) + if e["type"] == "workbench": + guarding.append(e) # #9 Decision 3: it guards its own work area, unconditionally + continue var at := Vector2(e["x"], e["z"]) for a in areas: if a.grow(config.guard_range).has_point(at): diff --git a/game/tests/test_structures.gd b/game/tests/test_structures.gd index bdf8d97..d23a4e7 100644 --- a/game/tests/test_structures.gd +++ b/game/tests/test_structures.gd @@ -607,25 +607,42 @@ func _workbench(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> voi r.check(sim.upgrade_panel.is_open, "now its shop opens like any other workbench day") _tap(sim, "interact") - # Damaged by an enemy: thief_behavior.gd's generic BREAK state (any barrier in a route, not - # just a "fence breaker"'s guarded ones) -- it's a nav barrier like a fence (StructureSystem - # .blocks() excludes only slowing_trap). - var h := es.spawn(EnemySystem.TYPE_THIEF, bench.global_position.x + 0.6, bench.global_position.z, Vector2.LEFT) - var d := es.resolve_handle(h) - es.state[d] = T.BREAK - es.barrier[d] = id + # Targeting (Decision 3): FarmSim._breaker_goals() treats an unbroken Workbench as guarding its + # own work area, alongside whatever else is actually guarding something -- PenRing.build_gate_only + # places one real guarding structure (the same single-gate setup test_roster.gd's own fence- + # breaker test uses), so this proves the Workbench joins a non-empty guarding set, not just a + # fallback when nothing else guards anything. + var gate_id := PenRing.build_gate_only(sim) + _settle(sim) + var goals := sim._breaker_goals() + r.check(_goals_near(sim, goals, bench.global_position) and _goals_near(sim, goals, Vector3(PenRing.EAST_X, 0.0, PenRing.GATE_Z)), + "an unbroken Workbench is in the breaker goals alongside the pen gate guarding the pen") + # Spawned far closer to the Work Area than to the pen, a fence breaker -- with no hand-set + # state or barrier, driven only by its own AI -- heads for and smashes the Workbench on its own. + # The scarecrow stays well clear (AWAY): close by, the breaker would aggro onto it instead + # (ThiefBehavior._aggro, aggro_range 4 m) and never reach BREAK. + player.global_position = AWAY + var hb := sim.spawn_enemy(EnemySystem.TYPE_BREAKER, LootSystem.ANY, GameTestRunner.EDGE["driveway"]) + var at := _until(sim, func() -> bool: return es.resolve_handle(hb) >= 0 and es.state[es.resolve_handle(hb)] == T.BREAK, 40.0) + var dh := es.resolve_handle(hb) + r.check(at < 40.0 and dh >= 0 and es.barrier[dh] == id, + "a fence breaker (no hand-set state) targets and walks to the Workbench on its own (%.1f s)" % at) var hp0 := int(c.state.structure(id)["hp"]) - _step(sim, roundi(es.t_hit_interval[EnemySystem.TYPE_THIEF] * 60.0) + 4) - r.check(int(c.state.structure(id)["hp"]) < hp0, "an enemy breaking against it damages it like any barrier") - es.apply_hit_handle(h, 99) + _step(sim, roundi(es.t_hit_interval[EnemySystem.TYPE_BREAKER] * 60.0) + 4) + r.check(int(c.state.structure(id)["hp"]) < hp0, "and actually damages it") + es.apply_hit_handle(hb, 99) _step(sim, 1) + st.dismantle(gate_id) + _settle(sim) - # Broken at 0 HP: the record stays (repair-only, not destroyed/removed); its shop is refused. + # Broken at 0 HP: the record stays (repair-only, not destroyed/removed); its shop is refused, + # and it drops out of the breaker goals (nothing left to gain by sending another breaker at it). var destroyed: Array[String] = [] st.destroyed.connect(func(_i: int, t: String, _at: Vector3) -> void: destroyed.append(t)) r.check(not st.damage(id, 9999) and not c.state.structure(id).is_empty() and int(c.state.structure(id)["hp"]) == 0, "damaging it to 0 HP leaves it 'broken', not destroyed: the record stays") r.check(destroyed.is_empty(), "no breach/destroyed signal for it") + r.check(not _goals_near(sim, sim._breaker_goals(), bench.global_position), "a broken Workbench drops out of the breaker goals") # Just outside REPAIR_REACH (1.4 m to the footprint's edge, 0.5 m out from center) but inside # the Workbench's own reach (2.2 m, to its center): repair takes priority within REPAIR_REACH # (Interactions), so this is where its shop's own refusal shows. @@ -670,6 +687,18 @@ func _workbench(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> voi _settle(sim) +## Whether any of FarmSim._breaker_goals()'s cells (walkable cells near a guarding barrier's +## footprint) sit near a world position -- 3 m is generous (the goal cells themselves are within +## ~1.1 m of a footprint's edge) but well short of the distance between the Workbench and the pen +## gate used alongside it above, so it can't mistake one guarding structure's goals for the other's. +func _goals_near(sim: FarmSim, goals: PackedInt32Array, at: Vector3) -> bool: + var p := Vector2(at.x, at.z) + for cell in goals: + if sim.nav.cell_center(cell).distance_to(p) <= 3.0: + return true + return false + + func _field_fences() -> Array: var parts: Array = [] for k in 15: