Task #13: two new crop fields with staged growth (Night 2 / Night 4)

Adds a Lettuce/Cabbage field (ids 31-40, x -11..-1 z 2..12) and a Corn field
(ids 41-50, x 2..12 z 2..12), each 2 plots of 5 units, placeholder plants
reusing plant_007/plant_008. CropUnit gains crop (cosmetic) and sprout_night
(0/2/4) fields. LootSystem gains a DORMANT status: a unit whose field hasn't
sprouted (FarmState.nights_survived < sprout_night) is hidden and never
claimable/recoverable, and is excluded from crops_total so it can't affect
scoring or the "farm stripped" check. sync_from_state() derives DORMANT vs.
HOME from nights_survived every morning (and after a retry/load), and
self-heals crops_total to the sprouted count; FarmSim.grow_crops_total() also
grows it right at night_ending so the morning snapshot (retry, the save file)
already has it right. FarmZones.crop_units() now gathers CropUnits from every
CROP_FIELD zone (there are three now), not just the first found; the same fix
applies to FarmSim's crop-area rect and the fence breakers' guarded areas.

Reworks test_farm_layout's zone-count and crop checks for the extra fields,
and test_theft's start-of-run and FarmState-consistency checks, which assumed
every crop unit was always in play; adds test_theft cases for both fields
sprouting on schedule, dormant units being unclaimable, and a sprouted unit
being stealable and scored, including a save/JSON round trip. Nudges
test_coop's co-op build spot into the 3 m walkway between the two new fields,
since its old spot now sits on the corn field's rows.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 15:52:38 -04:00
parent 0613cca408
commit 62edb8e04c
8 changed files with 974 additions and 28 deletions

View file

@ -29,8 +29,9 @@ func run(r: GameTestRunner) -> void:
sim.loot.loot_event.connect(func(kind: StringName, _id: int, _p: Vector3) -> void:
_events[kind] = int(_events.get(kind, 0)) + 1)
r.info("farm sim loaded in %d ms (nav %d cells)" % [sim.load_msec, sim.nav.cell_count()])
r.check(sim.loot.unit_count == 30 and sim.loot.count(S.HOME, LootSystem.Category.CROP) == 30 and sim.campaign.state.crops == 30,
"30 crop units start at home")
r.check(sim.loot.unit_count == 50 and sim.loot.count(S.HOME, LootSystem.Category.CROP) == 30 and sim.campaign.state.crops == 30
and sim.loot.count(S.DORMANT, LootSystem.Category.CROP) == 20,
"30 crop units start at home, 20 more dormant in the two later fields (#13)")
var edge_cells := [Vector2(-16.0, -6.0), Vector2(20.0, 18.0), Vector2(50.0, 0.0), Vector2(-16.0, 10.0)].filter(func(p: Vector2) -> bool:
return sim.escape_field[sim.nav.cell_of(p.x, p.y)] == 0)
r.check(edge_cells.size() == 4 and sim.escape_field[sim.nav.cell_of(10.0, -8.0)] > 0, "the farm's edge, all around, is where carriers escape")
@ -43,6 +44,7 @@ func run(r: GameTestRunner) -> void:
_sunrise_and_dawn_end(r, sim)
_retry(r, sim)
_night_arrivals(r, sim)
_staged_fields(r, sim)
farm.queue_free()
await r.process_frame
@ -295,13 +297,96 @@ func _night_arrivals(r: GameTestRunner, sim: FarmSim) -> void:
sim.waves.enabled = false
## FarmState's lost crops and the loot system's LOST units are the same set.
## #13: the two later fields (lettuce/cabbage, ids 31-40; corn, 41-50) start dormant -- hidden,
## not claimable, not scored -- and sprout on schedule: field A the morning after night 2, field B
## the morning after night 4. Once sprouted a unit is a crop like any other: visible, stealable by
## a crow, and counted in crops_total and the score.
func _staged_fields(r: GameTestRunner, sim: FarmSim) -> void:
var c := sim.campaign
var loot := sim.loot
var es := sim.enemies
while c.state.nights_survived < 2:
_play_to_next_morning(sim)
r.check(c.state.phase == P.MORNING and c.state.nights_survived == 2, "played to the morning after night 2")
var a_ok := true
for id in range(31, 41):
a_ok = a_ok and loot.status[id] == S.HOME and loot._units[id].visible
r.check(a_ok, "field A (lettuce/cabbage, 31..40) sprouted after night 2: visible and home")
var b_dormant := true
for id in range(41, 51):
b_dormant = b_dormant and loot.status[id] == S.DORMANT and not loot._units[id].visible
r.check(b_dormant, "field B (corn, 41..50) is still dormant: hidden, not home")
r.check(c.state.crops_total == 30 + 10, "crops_total grew by field A's 10 units")
r.check(not loot.recover(41) and not loot.is_claimed_by(41, -1), "a dormant unit can't be recovered or claimed")
# A crow can claim (by hand, as other tests here do), steal, and carry off a field-A crop: it
# counts exactly like any other crop.
var before := c.state.crops
var h := es.spawn(EnemySystem.TYPE_CROW, -20.0, 0.0, Vector2.RIGHT)
var d := es.resolve_handle(h)
loot.holder[31] = h
r.check(loot.pick_up(31, h), "field A crop 31 can be claimed and picked up")
es.carry[d] = 31
loot.escape(31, h)
r.check(c.state.is_crop_lost(31) and loot.status[31] == S.LOST and c.state.crops == before - 1,
"a stolen field-A crop is lost and scored like any other")
es.apply_hit(d, 99)
_step(sim, 1)
_consistent(r, sim)
while c.state.nights_survived < 4:
_play_to_next_morning(sim)
r.check(c.state.phase == P.MORNING and c.state.nights_survived == 4, "played to the morning after night 4")
var b_ok := true
for id in range(41, 51):
b_ok = b_ok and loot.status[id] == S.HOME and loot._units[id].visible
r.check(b_ok, "field B (corn, 41..50) sprouted after night 4: visible and home")
r.check(c.state.crops_total == 30 + 10 + 10, "crops_total grew by field B's 10 units too")
var before2 := c.state.crops
var h2 := es.spawn(EnemySystem.TYPE_CROW, -20.0, 0.0, Vector2.RIGHT)
var d2 := es.resolve_handle(h2)
loot.holder[41] = h2
r.check(loot.pick_up(41, h2), "field B crop 41 can be claimed and picked up")
es.carry[d2] = 41
loot.escape(41, h2)
r.check(c.state.is_crop_lost(41) and loot.status[41] == S.LOST and c.state.crops == before2 - 1,
"a stolen field-B crop is lost and scored like any other")
es.apply_hit(d2, 99)
_step(sim, 1)
_consistent(r, sim)
# Save/load and retry round trips agree on crops_total and every unit's status.
var dict := c.state.to_dict()
var json := JSON.stringify(dict)
var restored := FarmState.from_dict(JSON.parse_string(json))
r.check(restored.crops_total == c.state.crops_total and restored.nights_survived == c.state.nights_survived,
"crops_total and nights_survived survive a save/JSON round trip")
loot.sync_from_state() # as a real load or retry would trigger
r.check(c.state.crops_total == 50, "sync_from_state() repairs crops_total to the same, fully-sprouted total")
## Advances from wherever the campaign is now to the next morning (one full day/night/dawn cycle),
## through the normal transition path (same as the debug keys / UI would).
func _play_to_next_morning(sim: FarmSim) -> void:
var c := sim.campaign
c.advance_phase()
while c.state.phase != P.MORNING and not c.is_over():
c.advance_phase()
## FarmState's lost crops and the loot system's LOST units are the same set, and crops is the
## sprouted units (#13: everything but DORMANT, i.e. the later fields before they sprout) minus
## what's lost -- not plain unit_count, now that not every unit is in play from the start.
func _consistent(r: GameTestRunner, sim: FarmSim) -> void:
var lost := PackedInt32Array()
var sprouted := 0
for id in range(1, sim.loot.unit_count + 1):
if sim.loot.status[id] == S.LOST:
lost.append(id)
r.check(lost == sim.campaign.state.lost_crops and sim.campaign.state.crops == sim.loot.unit_count - lost.size(),
if sim.loot.status[id] != S.DORMANT:
sprouted += 1
r.check(lost == sim.campaign.state.lost_crops and sim.campaign.state.crops == sprouted - lost.size(),
"FarmState agrees with the loot state (%d lost)" % lost.size())