harvest/game/scripts/review_panel.gd
Forrest df10a1967c Task #9: Workbench auto-grants after Night 1, destructible/repairable
Gate the Workbench to appear once Night 1 is survived instead of being
buildable: StructureSystem.grant_workbench() (an unvalidated, unpurchased
place() variant, grant()) adds a "workbench" structure record at the scene
node's position, reusing the existing damage/repair/targeting/save pipeline
end to end. The grant happens on CampaignController.night_ending (so it
lands in state before the morning snapshot, like #13's crop growth) and is
re-derived from FarmState.nights_survived on load/retry (sync_from_state).

The Workbench's view is the scene's own node (hidden until granted, shown
by StructureSystem._add_view) instead of a second built mesh, so it gets
the standard HP bar, damage tint, and wobble for free. It's repair-only:
damage() never removes its record at 0 HP ("broken" instead of destroyed),
and it can't be dismantled. Interactions refuses to open its shop before
it exists or while broken (and no longer treats it as focusable), and
coop_session.gd's "buy" request handler refuses a workbench-shop item
(Upgrades.shop_of) the same way for a co-op partner. review_panel.gd's
pre-grant summary line no longer promises the work area from night one.

No FarmState.VERSION bump: a "workbench" record is a new type string, not
a new field, and from_dict() already validates types generically.

Tests: a new test_structures.gd check covers absent/no-prompt/shop-refused
before Night 1 (including a co-op "buy" request), granted after, damaged
by an enemy (thief_behavior.gd's generic BREAK state), broken-not-destroyed
at 0 HP with its shop refused, repaired at the normal proportional cost,
and persistence through a save/retry round trip. test_cycle.gd and
test_nights.gd pre-grant the workbench in setup (grant_workbench()) so
their per-night structure-count invariants aren't thrown off by the
night-1 grant itself.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 16:27:55 -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. A dropped animal runs off: walk up to it and lead it back to the pen. 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 animals will wander out.")
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 wandered out of an open pen.
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"