harvest/game/scripts/review_panel.gd
Forrest aaf5a81b4e Task #21: animals roam the whole farm, never lost on their own
Loose animals now roam the whole playable map, day and night. Each walk
goes to a goal 4-8 m away that is walkable, in a clear straight line,
at least 2 m inside FarmZones.playable (passed into AnimalSystem.setup),
and neither inside the pen nor across it. Every morning a loose animal
is back at its loose spot (no save change).

Animals no longer escape on their own:
- _stray and LootSystem.animal_escaped are gone.
- A new LootSystem.animal_loose makes an animal LOOSE where it stands
  and unpens it. It covers a carrier killed or scared (drop), a broken
  follow leash, and an animal getting out of an open pen (animal_strays,
  which still records animals_strayed).
- An animal dropped inside the pen is home again.
- An animal let out of an open pen walks out along the escape field,
  then roams. If the pen shuts first, it stays home.
- return_dropped still pens followers at dawn's end, but leaves loose
  animals loose.
- The only loss paths left are a carrier leaving the farm
  (thief escape, or removal with reason LEFT).

Loose spots at x = -12 move inward to (6|9|12, 0.5). The toast,
review-panel, dropped-animal toast, RULES.md and header text no longer
promise animals wander off or run for the edge.

test_targets: _loose_start has a 90 s day + 90 s night roam run (every
animal >4 m, never off the map or into the pen, none lost), a morning
reset to the spots, a killed carrier leaving the animal loose, and an
unopposed thief carrying one off. _animal_theft, _leading, _breach and
_make_loose follow the new rules. AWAY moves off the map so roaming
animals can't start following the parked scarecrow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 13:07:13 -04:00

226 lines
9.7 KiB
GDScript

class_name ReviewPanel
extends CenterContainer
## The morning screen (the untimed MORNING phase), between one night and the next day:
## * Last night's results (from the night's log entry): per category what was taken,
## recovered, and lost; enemies defeated by type and how many got away with loot; damage
## (the scarecrow's, structures hit, broken, and repaired); materials gathered and spent
## over the day and night.
## * This morning: what's left (crops, animals, supplies, the scarecrow's health), what needs
## repair (damaged structures keep their health bars on the farm), the pen's gate, the
## materials on hand, and tonight's plan.
## "Start the day" (focused, so Enter / A work) ends it. The first morning explains the vacation
## instead of results.
const CATEGORIES := ["Crops", "Animals", "Supplies"]
const GOLD := Color(0.95, 0.82, 0.45)
const GREY := Color(0.7, 0.72, 0.68)
const RED := Color(1.0, 0.5, 0.42)
var campaign: CampaignController
var structures: StructureSystem
## Set by FarmSim: whether the pen is open, and tonight's plan.
var animals: AnimalSystem
var waves: WaveDirector
var _title: Label
var _last_head: Label
var _grid: GridContainer
var _cells := {} # "Crops/taken" -> Label
var _results: Label
var _rule: HSeparator
var _morning_head: Label
var _body: Label
var _start: Button
func setup(p_campaign: CampaignController, p_structures: StructureSystem) -> void:
campaign = p_campaign
structures = p_structures
campaign.phase_changed.connect(func(_p: CampaignController.Phase, _prev: CampaignController.Phase) -> void: refresh())
refresh()
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
var panel := PanelContainer.new()
panel.add_theme_stylebox_override("panel", backing())
add_child(panel)
var margin := MarginContainer.new()
for side in ["left", "right", "top", "bottom"]:
margin.add_theme_constant_override("margin_" + side, 30)
panel.add_child(margin)
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 12)
margin.add_child(box)
_title = _label(box, 38)
_last_head = _label(box, 26, GOLD)
_grid = GridContainer.new()
_grid.columns = 4
_grid.add_theme_constant_override("h_separation", 48)
_grid.add_theme_constant_override("v_separation", 2)
box.add_child(_grid)
for head in ["", "Taken", "Recovered", "Lost"]:
_label(_grid, 20, GREY).text = head
for cat: String in CATEGORIES:
_label(_grid, 22).text = cat
for col in ["taken", "recovered", "lost"]:
var l := _label(_grid, 22)
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
_cells["%s/%s" % [cat, col]] = l
_results = _label(box, 21)
_results.custom_minimum_size = Vector2(760.0, 0.0)
_results.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_rule = HSeparator.new()
box.add_child(_rule)
_morning_head = _label(box, 26, GOLD)
_morning_head.text = "This morning"
_body = _label(box, 21)
_body.custom_minimum_size = Vector2(760.0, 0.0)
_body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
_start = Button.new()
_start.text = "Start the day"
_start.custom_minimum_size = Vector2(260.0, 56.0)
_start.add_theme_font_size_override("font_size", 24)
_start.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
_start.pressed.connect(func() -> void: campaign.begin_day())
box.add_child(_start)
visible = false
## The dark, nearly opaque panel behind the morning screen, the menus, and the end screens.
static func backing() -> StyleBoxFlat:
var bg := StyleBoxFlat.new()
bg.bg_color = Color(0.1, 0.12, 0.1)
bg.border_color = Color(0.55, 0.5, 0.35)
bg.set_border_width_all(2)
bg.set_corner_radius_all(6)
return bg
func refresh() -> void:
if campaign == null:
return
visible = campaign.state.phase == CampaignController.Phase.MORNING
mouse_filter = Control.MOUSE_FILTER_STOP if visible else Control.MOUSE_FILTER_IGNORE
if not visible:
return
var s := campaign.state
var nights := campaign.config.campaign_nights
var first := s.night_log.is_empty()
for n: Control in [_last_head, _grid, _results, _rule, _morning_head]:
n.visible = not first
var lines: Array[String] = []
if first:
_title.text = "The farmers are away"
lines.append("Keep the farm safe for %d nights until they come home: the crops, the animals in the pen, and the supplies in the shed." % nights)
lines.append("By day, gather wood, scrap, and straw (hold E), store them at the barn, and build defenses (B). There's more to gather off the farm, to the north, east, and south.")
lines.append("At night, stab aliens with your pitchfork (left mouse), scare them off (Q), and pick up anything they drop. Animals roam loose until you lead them into the pen: walk up to one and it follows you. The supply shed sells a corn shooter (switch weapons with Tab or 1-2) and pumpkin bombs (throw them with right mouse).")
else:
var e: Dictionary = s.night_log[s.night_log.size() - 1]
_title.text = "Morning of day %d of %d" % [s.night, nights]
var plan := waves.plan_for(int(e["night"])) if waves != null else null
_last_head.text = "Night %d: %s" % [e["night"], plan.title] if plan != null and plan.title != "" else "Night %d" % e["night"]
_fill_grid(e)
_results.text = "\n".join(result_lines(e, s))
lines.append("Left: %d of %d crops, %d of %d animals, %d supplies. Scarecrow: %d of %d HP." % [s.crops, s.crops_total,
s.animals, s.animals_total, s.supplies, s.scarecrow_hp, s.scarecrow_max_hp])
lines.append(_damage_line())
if animals != null and animals.pen_open and s.animals > 0:
lines.append("The pen is open: put a gate in its opening (B) before nightfall, or the animals in it will get out and roam loose.")
var r := s.resources
lines.append("In the shed: %s. In the pouch: %s." % [_or_none(ResourceStore.describe(r.shed)), _or_none(ResourceStore.describe(r.pouch))])
var plan := waves.tonight() if waves != null else null
if plan != null and plan.title != "":
if structures != null and structures.has_workbench():
lines.append("Tonight: %s. Upgrades at the work area: %d of %d done." % [plan.title, _upgrades_done(), Upgrades.ORDER.size()])
else: # #9: the Workbench hasn't been granted yet -- it arrives once Night 1 is survived
lines.append("Tonight: %s. The work area will be set up once you survive tonight." % plan.title)
_body.text = "\n".join(lines)
_start.grab_focus.call_deferred()
## The results table as text, one row per category ("Crops 4 3 1"), for tests.
func grid_text() -> String:
var rows: Array[String] = []
for cat: String in CATEGORIES:
rows.append("%s %s %s %s" % [cat, _cells[cat + "/taken"].text, _cells[cat + "/recovered"].text, _cells[cat + "/lost"].text])
return "\n".join(rows)
## Per category: (taken, recovered, lost) in a night's log entry. Animals taken include those
## that got out of an open pen (loose, not lost).
static func category_counts(e: Dictionary) -> Dictionary:
return {
"Crops": [int(e["crops_stolen"]), int(e["crops_recovered"]), int(e["crops_lost"])],
"Animals": [int(e["animals_stolen"]) + int(e["animals_strayed"]), int(e["animals_recovered"]), int(e["animals_lost"])],
"Supplies": [int(e["supplies_stolen"]), int(e["supplies_recovered"]), int(e["supplies_lost"])],
}
## Enemies, damage, and materials of a night's log entry.
static func result_lines(e: Dictionary, s: FarmState) -> Array[String]:
var out: Array[String] = []
var enemies := "Enemies defeated: %s." % Assessment.enemies_line(e)
if int(e["escaped"]) > 0:
enemies = enemies.trim_suffix(".") + "; %d got away with loot." % int(e["escaped"])
out.append(enemies)
var damage := PackedStringArray()
damage.append("the scarecrow took %d" % int(e["scarecrow_damage"]) if int(e["scarecrow_damage"]) > 0 else "the scarecrow wasn't hurt")
if int(e["patches"]) > 0:
damage.append("patched up %d %s" % [int(e["patches"]), "time" if int(e["patches"]) == 1 else "times"])
if int(e["structures_damaged"]) > 0:
damage.append("structures took %d damage" % int(e["structures_damaged"]))
if int(e["structures_destroyed"]) > 0:
damage.append("%d broken" % int(e["structures_destroyed"]))
if int(e["repairs"]) > 0:
damage.append("%d repaired" % int(e["repairs"]))
out.append("Damage: %s." % ", ".join(damage))
out.append("Materials: gathered %s; spent %s." % [Assessment.materials_line(e, "gathered_"), Assessment.materials_line(e, "spent_")])
if s != null and int(e["structures_built"]) + int(e["upgrades_bought"]) > 0:
out.append("Built %d, upgrades bought %d." % [int(e["structures_built"]), int(e["upgrades_bought"])])
return out
func _fill_grid(e: Dictionary) -> void:
var counts := category_counts(e)
for cat: String in CATEGORIES:
var c: Array = counts[cat]
for k in 3:
var l: Label = _cells["%s/%s" % [cat, ["taken", "recovered", "lost"][k]]]
l.text = str(c[k])
l.add_theme_color_override("font_color", RED if k == 2 and int(c[k]) > 0 else Color(0.92, 0.92, 0.88))
func _damage_line() -> String:
if structures == null or structures.count() == 0:
return "Nothing built yet."
var damaged := {}
for e in campaign.state.structures:
if int(e["hp"]) < structures.max_hp(e["type"]):
damaged[e["type"]] = int(damaged.get(e["type"], 0)) + 1
if damaged.is_empty():
return "No repairs needed."
var parts: Array[String] = []
for t in damaged:
parts.append("%d %s%s" % [damaged[t], StructureSystem.NAMES[t].to_lower(), "s" if int(damaged[t]) > 1 else ""])
return "Needs repair (marked with health bars): %s." % ", ".join(parts)
func _upgrades_done() -> int:
var n := 0
for id in Upgrades.ORDER:
if Upgrades.owned(campaign.state, id):
n += 1
return n
func _label(parent: Node, font_size: int, color: Color = Color(0.92, 0.92, 0.88)) -> Label:
var l := Label.new()
l.add_theme_font_size_override("font_size", font_size)
l.add_theme_color_override("font_color", color)
parent.add_child(l)
return l
static func _or_none(text: String) -> String:
return text if text != "" else "nothing"