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. const RUN_SPEED := 5.0 # PlayerCharacter.run_speed const MAX_TRAVEL := 12.0 const ENEMY_RADIUS := 0.35 const SCARECROW_RADIUS := 0.4 const CELL := 0.5 func run(r: GameTestRunner) -> void: var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D r.root.add_child(farm) await r.process_frame await r.physics_frame var zones := farm.get_node("Zones") as FarmZones r.check(zones != null, "farm.tscn has a FarmZones node") 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") _zones(r, zones) _crops(r, zones) _markers(r, zones) var nav := FarmNavigation.new() nav.build(farm, map.grow(5.0), CELL, ENEMY_RADIUS) r.info("enemy nav: %d cells, %d shapes rasterized, built in %.1f ms" % [nav.cell_count(), nav.shapes_rasterized, nav.build_usec / 1000.0]) _fence(r, zones, nav) _enemy_routes(r, zones, nav) _scarecrow(r, farm, zones) _travel_times(r, farm, zones) await _banner(r, farm, zones) farm.queue_free() await r.process_frame 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()) 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]) 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) r.check(zones.zone_at(a.global_position) == a, "zone_at(center of %s) finds it" % a.display_name) 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") 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 ids := {} var per_plot := {} var fewest := 1 << 30 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(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(fewest >= 8, "every unit is a visible cluster (fewest plants: %d)" % fewest) func _markers(r: GameTestRunner, zones: FarmZones) -> void: var map := zones.playable var gaps := zones.gaps() r.check(gaps.size() == 3, "3 gaps in what's left of the perimeter fence (got %d)" % gaps.size()) for m in gaps: var p := Vector2(m.global_position.x, m.global_position.z) r.check(not map.has_point(p) and map.grow(4.0).has_point(p), "%s sits just outside the perimeter" % m.name) var cfg := load("res://game/resources/game_config.tres") as GameConfig var edge := zones.edge(cfg.spawn_margin) var on_edge := true for k in 40: var p := zones.edge_point(k / 40.0 + 0.01, cfg.spawn_margin) 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 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]) ## 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. func _fence(r: GameTestRunner, zones: FarmZones, nav: FarmNavigation) -> void: var map := zones.playable var gaps := zones.gaps() 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] = [] for side in 4: # north, east, south, west var a: Vector2 = corners[side] var b: Vector2 = corners[(side + 1) % 4] var steps := ceili(a.distance_to(b) / (CELL * 0.5)) var free := 0 for k in steps + 1: var p := a.lerp(b, k / float(steps)) 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: 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]) 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)) r.check(nav.is_free(nav.cell_of(edge.x, edge.y)), "%s is open for ground enemies" % m.name) ## Ground enemies arrive anywhere on the edge with a route in (as FarmSim picks them: a route to ## the pen) and can walk from there to every zone; the lengths printed are from the middle of each ## side (the table in game/LAYOUT.md). func _enemy_routes(r: GameTestRunner, zones: FarmZones, nav: FarmNavigation) -> void: var cfg := load("res://game/resources/game_config.tres") as GameConfig var edge := zones.edge(cfg.spawn_margin) var ring := nav.free_cells_on_outline(edge, CELL * 0.75) var pen := nav.distance_field(nav.free_cells_in(zones.zone(FarmZone.Kind.ANIMAL_PEN).rect().grow(-1.0))) var spawns := PackedInt32Array() for c in ring: if pen[c] < FarmNavigation.UNREACHABLE: spawns.append(c) r.check(spawns.size() > ring.size() * 0.9, "enemies can arrive on nearly all of the farm's edge (%d of %d free cells)" % [spawns.size(), ring.size()]) var mid := edge.get_center() var middles := {"north": Vector2(mid.x, edge.position.y), "east": Vector2(edge.end.x, mid.y), "south": Vector2(mid.x, edge.end.y), "west": Vector2(edge.position.x, mid.y)} for zone in zones.zones(): var goal := nav.free_cells_in(zone.rect()) r.check(goal.size() > 0, "%s has walkable ground (%d cells)" % [zone.display_name, goal.size()]) var field := nav.distance_field(goal) var cut_off := 0 for s in spawns: if field[s] >= FarmNavigation.UNREACHABLE: cut_off += 1 r.check(cut_off == 0, "ground enemies can walk from anywhere on the edge to the %s and back (%d cells can't)" % [zone.display_name, cut_off]) var lengths: Array[String] = [] for side: String in middles: var at: Vector2 = middles[side] var m := nav.nearest_free(nav.cell_of(at.x, at.y)) lengths.append("%s %.0f" % [side, nav.meters(field[m])] if m >= 0 and field[m] < FarmNavigation.UNREACHABLE else "%s -" % side) r.info("%-12s from the edge (m): %s" % [zone.display_name, ", ".join(lengths)]) func _scarecrow(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void: var player := farm.get_node("Player") as CharacterBody3D var bounds := farm.get_node("Zones/Perimeter/PlayerBounds") as StaticBody3D r.check(zones.playable.has_point(Vector2(player.global_position.x, player.global_position.z)), "the scarecrow starts inside the map") r.check(bounds.collision_layer == 4 and (player.collision_mask & 4) != 0 and (player.collision_mask & 1) != 0, "PlayerBounds is on the player_bounds layer, which only the scarecrow collides with") # Stand in each gap and walk out: nothing stops the scarecrow (the gathering areas outside). var start := player.global_transform for m in zones.gaps(): var g := Vector2(m.global_position.x, m.global_position.z) var map := zones.playable var edge := Vector2(clampf(g.x, map.position.x, map.end.x), clampf(g.y, map.position.y, map.end.y)) var out := (g - edge).normalized() player.global_position = Vector3(edge.x - out.x * 1.0, 0.6, edge.y - out.y * 1.0) # above the driveway's road var hit := player.move_and_collide(Vector3(out.x, 0.0, out.y) * 3.0, true) r.check(hit == null, "the scarecrow can walk out through %s" % m.name) # ...but not past the old farm's outer fences: the invisible walls are there now. var roam := zones.roam var sides := {"north": [Vector2(-20.0, roam.position.y + 1.0), Vector2(0, -1)], "south": [Vector2(5.0, roam.end.y - 1.0), Vector2(0, 1)], "west": [Vector2(roam.position.x + 1.0, 0.0), Vector2(-1, 0)], "east": [Vector2(roam.end.x - 1.0, -30.0), Vector2(1, 0)]} for side: String in sides: var at: Vector2 = sides[side][0] var dir: Vector2 = sides[side][1] player.global_position = Vector3(at.x, 0.3, at.y) var hit := player.move_and_collide(Vector3(dir.x, 0.0, dir.y) * 3.0, true) r.check(hit != null, "the scarecrow can't go past the %s edge of the roaming area" % side) player.global_transform = start func _travel_times(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void: var nav := FarmNavigation.new() nav.build(farm, zones.playable, CELL, SCARECROW_RADIUS, 1 | 4) var all := zones.zones() var anchors := PackedInt32Array() for z in all: anchors.append(nav.nearest_free(nav.cell_of(z.global_position.x, z.global_position.z))) var header := "| From \\ to |" var rule := "|---|" for z in all: header += " %s |" % z.display_name rule += "---|" r.info(header) r.info(rule) var worst := 0.0 var worst_pair := "" for i in all.size(): var field := nav.distance_field(PackedInt32Array([anchors[i]])) var row := "| **%s** |" % all[i].display_name for j in all.size(): var cost := field[anchors[j]] 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] 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]) func _banner(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void: var banner := farm.get_node("ZoneBanner") as ZoneBanner var player := farm.get_node("Player") as Node3D var start := player.global_transform for zone in zones.zones(): player.global_position = zone.global_position + Vector3(0.0, 0.5, 0.0) await r.process_frame # emitted before _process, so wait for the next one too await r.process_frame r.check(banner.current == zone, "walking into the %s shows its name" % zone.display_name) player.global_position = Vector3(8.0, 0.5, -10.0) await r.process_frame await r.process_frame r.check(banner.current == null, "walking out of every zone clears the banner") player.global_transform = start