Task #20: lettuce and corn fields on the south soil, map grows to 60 x 80

- farm.tscn: LettuceField moves to (-1, 0.44, 25) on the SW soil tile
  (ground_008_004) and CornField to (29, 0.31, 25) on the SE tile
  (ground_011_003), both still 10 x 10 m. Their units keep ids 31-40 /
  41-50, plots 7-10 and sprout nights 2 / 4. The sunflower children are
  replaced: lettuce rows get 8 cabbage heads each (plant_004/005), corn
  rows 10 stalks each (plant_001, scaled ~0.8). Every plant's height
  follows the soil under it (raycast), so none floats.
- South Fields (outside zone) moves to (9.5, 41), 66 x 10, between the
  new south edge (z 35) and the old south fence; its four hay points
  move there too and rest on the soil.
- FarmZones.playable: Rect2(-13, -45, 60, 80), i.e. z -45..35.
- ThiefBehavior: one rect per crop field (field_rects) instead of their
  bounding box; area_for() takes the target's position.
- LootSystem: carried/dropped crop tokens show the crop's own plant
  (CROP_TOKENS), each scene loaded once.
- Tests: test_farm_layout checks the 60 x 80 map, outside zones off the
  map, the later fields on the soil tiles with their own plants standing
  on the soil, side names for both fields, the open east side south of
  the fence, and splits the travel bound (12 s core rule, 16 s for the
  south fields). test_theft: new south edge cell, crop tokens.
  run_tests EDGE/SKY south points follow the edge. test_targets: the
  dropped animal is watched from outside the gate (the carrier now dies
  ~4 m from the west edge).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-06 13:10:08 -04:00
parent 26b735d3e6
commit fc7c74fa32
10 changed files with 593 additions and 475 deletions

View file

@ -26,14 +26,15 @@ const SUITES := [
]
const TICK := 1.0 / 60.0
## Points on the farm's edge (FarmZones.edge) for tests that send an enemy in from a known side:
## on foot GameConfig.spawn_margin (3 m) outside the map, where the perimeter gaps used to be...
## on foot GameConfig.spawn_margin (3 m) outside the map, where the perimeter gaps used to be (#20:
## the south ones moved with the map's south edge, now z 35)...
const EDGE := {
"north": Vector2(12.0, -48.0), "driveway": Vector2(50.0, -39.5), "east": Vector2(50.0, -18.5),
"south-east": Vector2(31.0, 18.0), "south-west": Vector2(-4.0, 18.0), "west": Vector2(-16.0, -6.0),
"south-east": Vector2(31.0, 38.0), "south-west": Vector2(-4.0, 38.0), "west": Vector2(-16.0, -6.0),
}
## ...and flying flyer_spawn_margin (8 m) out, where the crow approaches were.
const SKY := {
"north": Vector2(17.0, -53.0), "east": Vector2(55.0, -5.0), "south": Vector2(17.0, 23.0), "west": Vector2(-21.0, -15.0),
"north": Vector2(17.0, -53.0), "east": Vector2(55.0, -5.0), "south": Vector2(17.0, 43.0), "west": Vector2(-21.0, -15.0),
}
var passes: int = 0

View file

@ -1,16 +1,27 @@
extends RefCounted
## The playable farm: every zone exists once inside the 60 x 60 m map without overlapping,
## lookups by position, the crop units, the farm's edge (where enemies arrive) and the gaps in
## what's left of the perimeter fence (the north and east sides, without holes except their gaps;
## the south and west are open), ground navigation from anywhere on the edge to every zone,
## scarecrow-only bounds, zone-to-zone travel times (printed as the table kept in
## game/LAYOUT.md), and the zone banner.
## The playable farm: every zone exists once inside the 60 x 80 m map without overlapping (the
## outside zones stay off it), lookups by position, the crop units, the later crop fields on the
## plowed soil south of the original map with their own plants, the farm's edge (where enemies
## arrive) and the gaps in what's left of the perimeter fence (the north and east sides, without
## holes except their gaps; the south, the west, and the east side south of the fence are open),
## ground navigation from anywhere on the edge to every zone, scarecrow-only bounds, zone-to-zone
## travel times (printed as the table kept in game/LAYOUT.md), and the zone banner.
const RUN_SPEED := 5.0 # PlayerCharacter.run_speed
## #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
## The layout rule: no key area more than 12 s from another. It holds for the farm's original
## zones.
const MAX_TRAVEL := 12.0
## #20: the lettuce/cabbage and corn fields sprout later, on the plowed soil at the map's south
## end (user-decided placement), so trips to them run longer: the farthest, the lettuce field to
## the Haystacks clear across the map, is 15.5 s. They get their own bound with a little room.
const MAX_TRAVEL_SOUTH_FIELDS := 16.0
## #20: the later fields' plants: cabbage heads and corn stalks, never the original field's
## sunflowers.
const PLANTS := "res://assets/farm/ithappy/farm/prefabs/assets/plant/plant_%s.tscn"
const FIELD_PLANTS := {"lettuce": ["004", "005"], "corn": ["001"]}
const SUNFLOWER_PLANTS := ["007", "008"]
## How deep a later field's plant may sit in the soil (its base below the surface).
const SUNK := 0.2
const ENEMY_RADIUS := 0.35
const SCARECROW_RADIUS := 0.4
const CELL := 0.5
@ -26,10 +37,12 @@ func run(r: GameTestRunner) -> void:
if zones == null:
return
var map := zones.playable
r.check(is_equal_approx(map.size.x, 60.0) and is_equal_approx(map.size.y, 60.0), "the playable map is 60 x 60 m")
r.check(map.position.is_equal_approx(Vector2(-13.0, -45.0)) and map.size.is_equal_approx(Vector2(60.0, 80.0)),
"the playable map is 60 x 80 m, x -13..47 and z -45..35 (%s)" % map)
_zones(r, zones)
_crops(r, zones)
_south_fields(r, farm, zones)
_markers(r, zones)
var nav := FarmNavigation.new()
nav.build(farm, map.grow(5.0), CELL, ENEMY_RADIUS)
@ -58,6 +71,15 @@ func _zones(r: GameTestRunner, zones: FarmZones) -> void:
for j in range(i + 1, all.size()):
r.check(not a.rect().intersects(all[j].rect()), "%s and %s don't overlap" % [a.display_name, all[j].display_name])
r.check(zones.zone_at(Vector3(8.0, 0.0, -10.0)) == null, "open ground between zones belongs to no zone")
# The gathering areas beyond the fence stay off the map (#20: the South Fields moved south of
# the map's new south edge) and clear of every other zone.
var outside := zones.outside_zones()
for i in outside.size():
var o := outside[i]
r.check(not zones.playable.intersects(o.rect()), "%s lies outside the playable map" % o.display_name)
r.check(zones.zone_at(o.global_position) == o, "zone_at(center of %s) finds it" % o.display_name)
for z in all + outside.slice(i + 1):
r.check(not o.rect().intersects(z.rect()), "%s and %s don't overlap" % [o.display_name, z.display_name])
## #13: 20 more crop units in two later-sprouting fields (lettuce/cabbage, corn), 10 each, ids
@ -93,6 +115,106 @@ func _crops(r: GameTestRunner, zones: FarmZones) -> void:
"10 corn units sprout after night 4")
## #20: the lettuce/cabbage and corn fields grow on the bare plowed soil south of the original map
## (the ithappy ground_008 / ground_011 tiles under Level/): each lies on one tile's footprint, with
## a meter to spare for its sloped rim, and is inside the playable map. Their plants are cabbage
## heads and corn stalks, not the original field's sunflowers, and stand on the soil: none floats
## above it, none is sunk deeper than SUNK.
func _south_fields(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:
var soil: Array[Rect2] = []
var soil_bodies: Array[RID] = []
for tile in farm.get_node("Level").get_children():
var tile_name := String(tile.name)
if not (tile_name.begins_with("ground_008") or tile_name.begins_with("ground_011")):
continue
var a := _footprint(tile as Node3D)
soil.append(Rect2(a.position.x, a.position.z, a.size.x, a.size.z))
for body in tile.find_children("*", "StaticBody3D", true, false):
soil_bodies.append((body as StaticBody3D).get_rid())
var space := farm.get_world_3d().direct_space_state
for crop: String in FIELD_PLANTS:
var field := _field_of(zones, crop)
r.check(field != null, "there is a %s field" % crop)
if field == null:
continue
var rect := field.rect()
r.check(soil.any(func(s: Rect2) -> bool: return s.grow(-1.0).encloses(rect)) and zones.playable.encloses(rect),
"the %s (%s) lies on the plowed soil, inside the playable map" % [field.display_name, rect])
var want: Array = FIELD_PLANTS[crop].map(func(n: String) -> String: return PLANTS % n)
var wrong: Array[String] = []
var sunflowers := 0
var plants := 0
var lowest := INF
var highest := -INF
for u in _units_of(field):
for plant in u.get_children():
plants += 1
var path := (plant as Node).scene_file_path
if path not in want:
wrong.append("%s/%s" % [u.unit_id, plant.name])
if SUNFLOWER_PLANTS.any(func(n: String) -> bool: return path == PLANTS % n):
sunflowers += 1
var at := (plant as Node3D).global_position
var ground := _soil_height(space, soil_bodies, at)
lowest = minf(lowest, at.y - ground)
highest = maxf(highest, at.y - ground)
r.check(plants > 0 and wrong.is_empty() and sunflowers == 0, "the %s's %d plants are %s, none a sunflower (%s)"
% [field.display_name, plants, " / ".join(want.map(func(p: String) -> String: return p.get_file().get_basename())), wrong.slice(0, 4)])
r.check(lowest >= -SUNK and highest <= 0.01, "the %s's plants stand on the soil (base %.2f..%.2f m from it)" % [field.display_name, lowest, highest])
## The soil's height under a point (a ray down that skips everything but the soil tiles), or INF.
func _soil_height(space: PhysicsDirectSpaceState3D, soil_bodies: Array[RID], at: Vector3) -> float:
var skip: Array[RID] = []
for attempt in 8:
var q := PhysicsRayQueryParameters3D.create(Vector3(at.x, 6.0, at.z), Vector3(at.x, -3.0, at.z))
q.exclude = skip
var hit := space.intersect_ray(q)
if hit.is_empty():
break
if hit["rid"] in soil_bodies:
return (hit["position"] as Vector3).y
skip.append(hit["rid"])
return INF
## The world-space box around every mesh under a node.
static func _footprint(node: Node3D) -> AABB:
var box := AABB()
var first := true
for mi in node.find_children("*", "MeshInstance3D", true, false):
var m := mi as MeshInstance3D
if m.mesh == null:
continue
var a := m.global_transform * m.mesh.get_aabb()
box = a if first else box.merge(a)
first = false
return box
## The crop growing in a crop field ("sunflower", "lettuce", "corn"), or "" for other zones.
static func _crop_of(zone: FarmZone) -> String:
if zone.kind != FarmZone.Kind.CROP_FIELD:
return ""
var units := _units_of(zone)
return units[0].crop if units.size() > 0 else ""
static func _units_of(zone: FarmZone) -> Array[CropUnit]:
var out: Array[CropUnit] = []
for node in zone.find_children("*", "Node3D", true, false):
if node is CropUnit:
out.append(node)
return out
static func _field_of(zones: FarmZones, crop: String) -> FarmZone:
for z in zones.zones_of(FarmZone.Kind.CROP_FIELD):
if _crop_of(z) == crop:
return z
return null
func _markers(r: GameTestRunner, zones: FarmZones) -> void:
var map := zones.playable
var gaps := zones.gaps()
@ -108,19 +230,26 @@ func _markers(r: GameTestRunner, zones: FarmZones) -> void:
on_edge = on_edge and FarmZones.nearest_on_outline(edge, p).distance_to(p) < 1e-3
r.check(on_edge and zones.edge_point(0.0, cfg.spawn_margin).is_equal_approx(edge.position)
and zones.edge_point(0.5, cfg.spawn_margin).is_equal_approx(edge.end), "edge_point walks the farm's edge, %.0f m out" % cfg.spawn_margin)
var lettuce := _field_of(zones, "lettuce").global_position
var corn := _field_of(zones, "corn").global_position
var names := [zones.side_of(Vector2(17.0, -48.0)), zones.side_of(Vector2(50.0, -40.0)), zones.side_of(Vector2(-16.0, -15.0)),
zones.side_of(Vector2(31.0, 18.0)), zones.side_of(map.get_center())]
r.check(names == ["north", "north-east", "west", "south-east", ""], "the farm's sides are named by thirds: %s" % [names])
zones.side_of(Vector2(corn.x, corn.z)), zones.side_of(Vector2(lettuce.x, lettuce.z)), zones.side_of(map.get_center())]
r.check(names == ["north", "north-east", "west", "south-east", "south-west", ""],
"the farm's sides are named by thirds (the corn field south-east, the lettuce field south-west): %s" % [names])
## What's left of the perimeter fence (the north and east sides) has no holes except its gaps, and
## every gap is open; the south and west sides are open ground.
## What's left of the perimeter fence (the north side, and the east side down to the original map's
## south-east corner) has no holes except its gaps, and every gap is open; the south and west
## sides are open ground, and so is the east side south of the fence (#20: the map reaches 20 m
## further south there, with no fence).
func _fence(r: GameTestRunner, zones: FarmZones, nav: FarmNavigation) -> void:
var map := zones.playable
var gaps := zones.gaps()
var fence_end := _footprint(zones.get_node("Perimeter/Fence") as Node3D).end.z
var corners := [map.position, Vector2(map.end.x, map.position.y), map.end, Vector2(map.position.x, map.end.y)]
var holes: Array[Vector2] = []
var free_share: Array[float] = []
var east_open := Vector2i.ZERO # free, all: the east side south of the fence
for side in 4: # north, east, south, west
var a: Vector2 = corners[side]
var b: Vector2 = corners[(side + 1) % 4]
@ -128,15 +257,22 @@ func _fence(r: GameTestRunner, zones: FarmZones, nav: FarmNavigation) -> void:
var free := 0
for k in steps + 1:
var p := a.lerp(b, k / float(steps))
var past_fence := side == 1 and p.y > fence_end + CELL
east_open.y += 1 if past_fence else 0
if not nav.is_free(nav.cell_of(p.x, p.y)):
continue
free += 1
if side < 2 and not gaps.any(func(m: FarmMarker) -> bool:
if past_fence:
east_open.x += 1
elif side < 2 and not gaps.any(func(m: FarmMarker) -> bool:
return Vector2(m.global_position.x, m.global_position.z).distance_to(p) < 4.5):
holes.append(p)
free_share.append(free / float(steps + 1))
r.check(holes.is_empty(), "the north and east fence has no holes outside its gaps %s" % [holes.slice(0, 5)])
r.check(free_share[2] > 0.9 and free_share[3] > 0.5, "the south and west sides are open (%.0f%% and %.0f%% free)" % [free_share[2] * 100.0, free_share[3] * 100.0])
var east_share := east_open.x / float(maxi(east_open.y, 1))
r.check(free_share[2] > 0.9 and free_share[3] > 0.5 and east_open.y > 0 and east_share > 0.9,
"the south and west sides are open (%.0f%% and %.0f%% free), and the east side south of the fence (z %.0f..%.0f, %.0f%% free)"
% [free_share[2] * 100.0, free_share[3] * 100.0, fence_end, map.end.y, east_share * 100.0])
for m in gaps:
var g := Vector2(m.global_position.x, m.global_position.z)
var edge := Vector2(clampf(g.x, map.position.x, map.end.x), clampf(g.y, map.position.y, map.end.y))
@ -218,8 +354,9 @@ func _travel_times(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:
rule += "---|"
r.info(header)
r.info(rule)
var worst := 0.0
var worst_pair := ""
var later := all.map(func(z: FarmZone) -> bool: return _crop_of(z) in FIELD_PLANTS)
var worst := [0.0, 0.0] # among the original zones; pairs with a later field
var worst_pair := ["", ""]
for i in all.size():
var field := nav.distance_field(PackedInt32Array([anchors[i]]))
var row := "| **%s** |" % all[i].display_name
@ -228,11 +365,14 @@ func _travel_times(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:
r.check(cost < FarmNavigation.UNREACHABLE, "the scarecrow can walk from %s to %s" % [all[i].display_name, all[j].display_name])
var seconds := nav.meters(cost) / RUN_SPEED
row += " — |" if i == j else " %.1f s |" % seconds
if seconds > worst:
worst = seconds
worst_pair = "%s -> %s" % [all[i].display_name, all[j].display_name]
var k := 1 if later[i] or later[j] else 0
if seconds > worst[k]:
worst[k] = seconds
worst_pair[k] = "%s -> %s" % [all[i].display_name, all[j].display_name]
r.info(row)
r.check(worst <= MAX_TRAVEL, "no key area is more than %.0f s from another (worst %.1f s: %s)" % [MAX_TRAVEL, worst, worst_pair])
r.check(worst[0] <= MAX_TRAVEL, "no key area is more than %.0f s from another (worst %.1f s: %s)" % [MAX_TRAVEL, worst[0], worst_pair[0]])
r.check(worst[1] <= MAX_TRAVEL_SOUTH_FIELDS, "the later crop fields, at the map's south end, are at most %.0f s from any key area (worst %.1f s: %s)"
% [MAX_TRAVEL_SOUTH_FIELDS, worst[1], worst_pair[1]])
func _banner(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:

View file

@ -222,6 +222,10 @@ func _animal_theft(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) ->
es.apply_hit(d, 99)
_step(sim, 1)
r.check(loot.status[item] == S.DROPPED and sim.animals.position(index).distance_to(at) < 0.2, "killing the carrier drops the animal where it fell")
# #20: with the map's south edge 20 m further off, the carrier heads west round the pen and
# falls about 4 m short of the west edge, which the animal would reach (and be lost at) well
# within the 5 s watched below. Watch it set off from outside the gate instead.
sim.animals.place(index, Vector2(7.0, -6.0))
var near_edge := _edge_distance(sim, sim.animals.position(index))
_step(sim, 300)
var later := _edge_distance(sim, sim.animals.position(index))

View file

@ -32,9 +32,14 @@ func run(r: GameTestRunner) -> void:
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:
# #20: the map reaches z 35 now, so its south edge (3 m out) is at z 38; z 18 is inside the map.
var edge_cells := [Vector2(-16.0, -6.0), Vector2(20.0, 38.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")
r.check(sim.escape_field[sim.nav.cell_of(20.0, 18.0)] > 0, "(20, 18), where the south edge used to be, is inside the map now")
# #20: a carried or dropped crop shows the plant of its own field.
var tokens := [1, 31, 41].map(func(id: int) -> String: return sim.loot._tokens[id].scene_file_path.get_file().get_basename())
r.check(tokens == ["plant_007", "plant_004", "plant_001"], "crop tokens match their crop: sunflower, cabbage, corn (%s)" % [tokens])
_steal_and_drop(r, sim, player)
_escape(r, sim)