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

@ -125,10 +125,12 @@ func _requests() -> void:
_pump(2)
r.check(host.campaign.state.phase == P.DAY and client.campaign.state.phase == P.DAY, "the partner can start the day (the host does it)")
host.campaign.state.resources.shed = {"wood": 40, "scrap": 30, "straw": 30}
client.player.global_position = Vector3(2.0, 0.0, 6.0)
# #13: the 3 m walkway between the two new crop fields (x -1..2, z 2..12) -- open ground, clear
# of both fields' crop rows -- stands in for the once-empty spot this used to build in.
client.player.global_position = Vector3(0.5, 0.0, 6.0)
_pump(4)
var before := host.structures.count()
client.build.place_request.call("fence", 3.5, 8.0, 0)
client.build.place_request.call("fence", 0.5, 8.0, 0)
_pump(20)
r.check(host.structures.count() == before + 1 and client.structures.count() == before + 1 and _same_state(),
"the partner builds a fence: the host checks it, builds it, and both farms show it")

View file

@ -7,7 +7,10 @@ extends RefCounted
## game/LAYOUT.md), and the zone banner.
const RUN_SPEED := 5.0 # PlayerCharacter.run_speed
const MAX_TRAVEL := 12.0
## #13: the two new south-west crop fields are a legitimate corner of the map (user-decided
## placement), and the farthest pair (a new field to the Scrap Heap, clear across the map) is a
## little past the old 12 s bar; raised to fit it with a little room.
const MAX_TRAVEL := 13.0
const ENEMY_RADIUS := 0.35
const SCARECROW_RADIUS := 0.4
const CELL := 0.5
@ -42,10 +45,12 @@ func run(r: GameTestRunner) -> void:
func _zones(r: GameTestRunner, zones: FarmZones) -> void:
var all := zones.zones()
r.check(all.size() == FarmZone.Kind.size(), "one zone per kind (%d zones)" % all.size())
# #13: two more CROP_FIELD zones (lettuce/cabbage, corn), so it's no longer one zone per kind.
r.check(all.size() == FarmZone.Kind.size() + 2, "every kind has a zone, plus 2 more crop fields (%d zones)" % all.size())
for kind: FarmZone.Kind in FarmZone.Kind.values():
var z := zones.zone(kind)
r.check(z != null and z.display_name != "", "zone %s exists and has a name" % FarmZone.Kind.keys()[kind])
r.check(zones.zones_of(FarmZone.Kind.CROP_FIELD).size() == 3, "3 crop fields: the original, lettuce/cabbage, and corn")
for i in all.size():
var a := all[i]
r.check(zones.playable.encloses(a.rect()), "%s lies inside the playable map" % a.display_name)
@ -55,22 +60,37 @@ func _zones(r: GameTestRunner, zones: FarmZones) -> void:
r.check(zones.zone_at(Vector3(8.0, 0.0, -10.0)) == null, "open ground between zones belongs to no zone")
## #13: 20 more crop units in two later-sprouting fields (lettuce/cabbage, corn), 10 each, ids
## 31..40 and 41..50, continuing the original field's plot numbering (7..10). GameConfig.start_crops
## (30) only counts the original, always-grown field; the later fields grow FarmState.crops_total
## when they sprout (LootSystem), so it's not part of this static count.
func _crops(r: GameTestRunner, zones: FarmZones) -> void:
var cfg := load("res://game/resources/game_config.tres") as GameConfig
var units := zones.crop_units()
var field := zones.zone(FarmZone.Kind.CROP_FIELD)
r.check(units.size() == cfg.start_crops, "%d crop units, matching GameConfig.start_crops (%d)" % [units.size(), cfg.start_crops])
var fields := zones.zones_of(FarmZone.Kind.CROP_FIELD)
r.check(units.size() == cfg.start_crops + 20, "%d crop units: %d in the original field, 20 in the two later ones" % [units.size(), cfg.start_crops])
var ids := {}
var per_plot := {}
var fewest := 1 << 30
var by_crop := {} # crop name -> {sprout_night, count}
for u in units:
ids[u.unit_id] = true
per_plot[u.plot] = int(per_plot.get(u.plot, 0)) + 1
fewest = mini(fewest, u.get_child_count())
r.check(field.contains(u.global_position), "crop unit %d lies in the crop field" % u.unit_id)
r.check(fields.any(func(z: FarmZone) -> bool: return z.contains(u.global_position)), "crop unit %d lies in a crop field" % u.unit_id)
var g: Dictionary = by_crop.get(u.crop, {"sprout_night": u.sprout_night, "count": 0})
r.check(g["sprout_night"] == u.sprout_night, "every %s unit shares one sprout_night" % u.crop)
g["count"] = int(g["count"]) + 1
by_crop[u.crop] = g
r.check(ids.size() == units.size() and ids.has(1) and ids.has(units.size()), "crop unit ids are unique, 1..%d" % units.size())
r.check(per_plot.size() == 6 and per_plot.values().all(func(n: int) -> bool: return n == 5), "6 plots of 5 units (%s)" % [per_plot])
r.check(per_plot.size() == 10 and per_plot.values().all(func(n: int) -> bool: return n == 5), "10 plots of 5 units (%s)" % [per_plot])
r.check(fewest >= 8, "every unit is a visible cluster (fewest plants: %d)" % fewest)
r.check(by_crop.get("sunflower", {}).get("count", 0) == cfg.start_crops and by_crop["sunflower"]["sprout_night"] == 0,
"the original field's %d units are sunflowers, already grown" % cfg.start_crops)
r.check(by_crop.get("lettuce", {}).get("count", 0) == 10 and by_crop["lettuce"]["sprout_night"] == 2,
"10 lettuce/cabbage units sprout after night 2")
r.check(by_crop.get("corn", {}).get("count", 0) == 10 and by_crop["corn"]["sprout_night"] == 4,
"10 corn units sprout after night 4")
func _markers(r: GameTestRunner, zones: FarmZones) -> void:

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())