rimlike/scenes/ai/mine_provider.gd
megaproxy e7a2407af2 Job: target_node claim so pawns don't cluster on the same target
Visible bug: with 1 wall ghost queued, all 3 pawns picked the same site;
2 stood idle while 1 built. Same shape would affect chop/mine/haul/etc.

Design: Job carries an untyped target_node ref (the tree/rock/build-site/
crop/item/patient/workbench/etc the job is acting on). Job.is_target_taken_
by_other(target, excluding_pawn) does an O(pawns) scan of live job state to
ask 'is anyone else already working this?'. Each WorkProvider's find_best_
for() now skips claimed targets in its scan and sets j.target_node before
returning. No per-entity claim state, no .claim()/.release() bookkeeping,
no save-format change — target_node is intentionally not serialized
because pawns re-decide and re-bind naturally after load.

Providers updated: construction / chop / mine / plant (harvest path) /
hauling (item AND corpse) / cleaning (target is Vector2i tile not Node;
field is untyped, doc'd) / doctor / crafting (workbench).

Not touched: rest (everyone shares the rest tile, that's fine), eat /
sleep (food and beds have their own availability gates; flagged as a
followup if multi-pawn food contention surfaces).

Verified MCP runtime: fresh boot, 3 pawns picked 3 distinct wall sites
(44,28)/(45,28)/(44,27) with distinct target_node refs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 13:45:44 +01:00

53 lines
1.8 KiB
GDScript

class_name MineProvider extends WorkProvider
## WorkProvider for the "mine" work category.
##
## Scans World.rocks for the nearest mineable Rock (Manhattan distance from
## the requesting pawn) and returns a two-toil Job:
## walk_to(rock.tile) → interact(rock.get_path(), "on_mine_tick")
##
## The INTERACT toil calls Rock.on_mine_tick() once per sim tick; the Rock
## internally tracks mine_progress and handles removal when MINE_TICKS is
## reached. The toil finishes automatically when is_mineable() returns
## false (exhausted) or when the node is freed.
##
## Phase 4 simplification: rocks are assumed walkable during mining approach
## (they may block movement in a later phase once obstruction is added to the
## pathfinder).
##
## Duck-typing note: Rock is referenced without class_name (class may not be
## registered yet when this provider loads). We rely only on:
## rock.tile: Vector2i
## rock.is_mineable() -> bool
## rock.get_path() -> NodePath
func _init() -> void:
category = &"mine"
priority = 4 # Slightly lower than chop (5); both higher than rest (0).
## Returns a Job targeting the nearest mineable Rock, or null if none exists.
## `pawn` is duck-typed: must expose .tile (Vector2i).
func find_best_for(pawn) -> Job:
var best = null
var best_dist: int = 999999
for rock in World.rocks:
if not rock.is_mineable():
continue
if Job.is_target_taken_by_other(rock, pawn):
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
best = rock
if best == null:
return null
var j := Job.new()
j.label = "Mine rock at %s" % best.tile
j.target_node = best
j.toils.append(Toil.walk_to(best.tile))
j.toils.append(Toil.interact(best.get_path(), &"on_mine_tick"))
return j