rimlike/scenes/ai/cleaning_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

71 lines
2.5 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name CleaningProvider extends WorkProvider
## Phase 13 — WorkProvider for the Cleaning work category.
##
## Priority 2: fires when there is nothing more productive to do
## (below hauling=3, above rest=0).
##
## make_job / find_best_for logic:
## Scan DirtinessSystem.dirt_map for tiles with dirt >= DIRTY_THRESHOLD.
## Pick the nearest to the pawn by Manhattan distance.
## Build a 2-toil job: walk_to(tile) → kind_clean(tile).
##
## There is no Cleaning skill yet (Phase 17+ skill matrix). The clean toil
## takes a flat CLEAN_TICKS sim ticks to reduce dirt from any level to 0.
##
## JobRunner._tick_clean reduces DirtinessSystem.dirt_at(tile) by
## DIRT_REDUCTION_PER_TICK each sim tick until dirt <= 0.
##
## Audit.log fires on job start and on toil completion (via JobRunner).
## Dirt level at or above which a tile is worth cleaning.
const DIRTY_THRESHOLD: float = 25.0
## Base number of sim ticks to clean a tile from any level to 0.
## No skill modifier for now; Phase 17 wires skill × speed.
const CLEAN_TICKS: int = 40
func _init() -> void:
category = &"clean"
priority = 2
# ── WorkProvider override ─────────────────────────────────────────────────────
## Returns a cleaning Job for `pawn`, or null if no dirty tiles exist.
## Picks the tile closest to `pawn` (Manhattan distance) with dirt >= DIRTY_THRESHOLD.
func find_best_for(pawn) -> Job:
# Safety — dirtiness system may not be wired yet (Agent A rooms arrive slightly later).
if World.get("dirtiness_system") == null:
return null
var ds = World.dirtiness_system
if ds == null:
return null
var best_tile: Vector2i = Vector2i(-1, -1)
var best_dist: int = 999999
for tile in ds.dirt_map.keys():
var dirt_val: float = float(ds.dirt_map[tile])
if dirt_val < DIRTY_THRESHOLD:
continue
# target_node stores the Vector2i tile coordinate (field is untyped — accepts
# non-Node values). Dirty tiles have no scene Node; the tile position itself
# is the unique claim key.
if Job.is_target_taken_by_other(tile, pawn):
continue
var d: int = abs(tile.x - pawn.tile.x) + abs(tile.y - pawn.tile.y)
if d < best_dist:
best_dist = d
best_tile = tile
if best_tile == Vector2i(-1, -1):
return null
var j := Job.new()
j.label = "Clean (%d,%d)" % [best_tile.x, best_tile.y]
j.target_node = best_tile
j.toils.append(Toil.walk_to(best_tile))
j.toils.append(Toil.clean_at(best_tile))
return j