diff --git a/scenes/ai/chop_provider.gd b/scenes/ai/chop_provider.gd index 9f9766c..09745ba 100644 --- a/scenes/ai/chop_provider.gd +++ b/scenes/ai/chop_provider.gd @@ -10,8 +10,9 @@ class_name ChopProvider extends WorkProvider ## reached. The toil finishes automatically when is_choppable() returns ## false (felled) or when the node is freed. ## -## Phase 4 simplification: trees do not block pathfinding, so walking directly -## to tree.tile is valid. No adjacency-offset needed. +## Reachability: trees are impassable, so the pawn stands at a walkable tile +## adjacent to the tree. A reachability pre-check on that adjacent tile prevents +## a busy-spin when all approach tiles are blocked. ## ## Duck-typing note: Tree is referenced without class_name (class may not be ## registered yet when this provider loads). We rely only on: @@ -40,6 +41,14 @@ func find_best_for(pawn) -> Job: continue if Job.is_target_taken_by_other(tree, pawn): continue + # Reachability pre-check — trees are impassable, pawn approaches from adjacent + # tile. Mirrors ConstructionProvider Pattern B. Skip if no walkable neighbour + # exists or none is reachable. + var approach: Vector2i = _find_adjacent_walkable(tree.tile, pawn.tile) + if approach == tree.tile: + continue # no walkable neighbour at all + if pawn.tile != approach and World.pathfinder.find_path(pawn.tile, approach).is_empty(): + continue var d: int = abs(tree.tile.x - pawn.tile.x) + abs(tree.tile.y - pawn.tile.y) if d < best_dist: best_dist = d @@ -51,6 +60,33 @@ func find_best_for(pawn) -> Job: var j := Job.new() j.label = "Chop tree at %s" % best.tile j.target_node = best - j.toils.append(Toil.walk_to(best.tile)) + # Walk to adjacent walkable tile — trees are impassable. + var stand_tile: Vector2i = _find_adjacent_walkable(best.tile, pawn.tile) + j.toils.append(Toil.walk_to(stand_tile)) j.toils.append(Toil.interact(best.get_path(), &"on_chop_tick")) return j + + +# ── helpers ────────────────────────────────────────────────────────────────── + +## Finds the 4-neighbour of `target` nearest to `prefer_near` that the pathfinder +## currently treats as walkable. Returns `target` itself if no walkable neighbour +## exists (caller treats that as "no approach available"). +## Mirrors ConstructionProvider._find_adjacent_walkable. +func _find_adjacent_walkable(target: Vector2i, prefer_near: Vector2i) -> Vector2i: + var offsets: Array[Vector2i] = [ + Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0), + ] + var best: Vector2i = target + var best_dist: int = 999999 + for off in offsets: + var t: Vector2i = target + off + if World.pathfinder == null: + continue + if not World.pathfinder.is_walkable(t): + continue + var d: int = abs(t.x - prefer_near.x) + abs(t.y - prefer_near.y) + if d < best_dist: + best_dist = d + best = t + return best diff --git a/scenes/ai/mine_provider.gd b/scenes/ai/mine_provider.gd index f6c08ce..f9f461b 100644 --- a/scenes/ai/mine_provider.gd +++ b/scenes/ai/mine_provider.gd @@ -43,6 +43,15 @@ func find_best_for(pawn) -> Job: continue if Job.is_target_taken_by_other(rock, pawn): continue + # Reachability pre-check — rocks are impassable. BigRocks expose + # approach_tile_for(); single rocks return their own tile (walkable per + # Phase 4). Either way, verify the approach tile is reachable before scoring. + var approach: Vector2i = ( + rock.approach_tile_for(pawn.tile) if rock.has_method("approach_tile_for") else rock.tile + ) + if pawn.tile != approach and World.pathfinder != null: + if World.pathfinder.find_path(pawn.tile, approach).is_empty(): + continue var d: int = abs(rock.tile.x - pawn.tile.x) + abs(rock.tile.y - pawn.tile.y) if d < best_dist: best_dist = d diff --git a/scenes/ai/plant_provider.gd b/scenes/ai/plant_provider.gd index 4f32949..b1a09df 100644 --- a/scenes/ai/plant_provider.gd +++ b/scenes/ai/plant_provider.gd @@ -63,6 +63,10 @@ func _find_harvest(pawn) -> Job: continue if Job.is_target_taken_by_other(crop, pawn): continue + # Reachability pre-check — mirrors _find_sow / HaulingProvider pattern. + if pawn.tile != crop.tile and World.pathfinder != null: + if World.pathfinder.find_path(pawn.tile, crop.tile).is_empty(): + continue var d: int = abs(crop.tile.x - pawn.tile.x) + abs(crop.tile.y - pawn.tile.y) if d < best_dist: best_dist = d diff --git a/scenes/ai/sleep_provider.gd b/scenes/ai/sleep_provider.gd index bd25e0e..03b57d4 100644 --- a/scenes/ai/sleep_provider.gd +++ b/scenes/ai/sleep_provider.gd @@ -58,6 +58,11 @@ func find_best_for(pawn) -> Job: # nearest bed and then losing the race at claim time. if Job.is_target_taken_by_other(bed, pawn): continue + # Reachability pre-check — beds are walkable tiles, use Pattern A. + # Prevents a busy-spin if all beds are walled off from this pawn. + if pawn.tile != bed.tile and World.pathfinder != null: + if World.pathfinder.find_path(pawn.tile, bed.tile).is_empty(): + continue var d: int = abs(bed.tile.x - pawn.tile.x) + abs(bed.tile.y - pawn.tile.y) if d < best_dist: best_dist = d