This commit is contained in:
Forrest 2026-10-04 12:07:36 -04:00
parent 276d43613d
commit 4298caf5cb
552 changed files with 42990 additions and 776 deletions

View file

@ -1,7 +1,9 @@
class_name CampaignController
extends Node
## Owns the five-night vacation: the phase state machine, phase timers, the outcome rules, and
## the morning snapshot used to retry a failed night (rules: game/RULES.md).
## Owns the vacation (GameConfig.campaign_nights nights): the phase state machine, phase timers,
## the outcome rules, and the morning snapshot used to retry a failed night (rules:
## game/RULES.md). With save_path set, every morning is also saved to disk (SaveGame), and
## finishing the vacation deletes the save.
##
## MORNING -> DAY -> DUSK -> NIGHT -> DAWN -> MORNING of the next night; after the final dawn,
## FARMERS_RETURN. GAME_OVER can follow any phase. Both are terminal. The morning review is
@ -17,11 +19,24 @@ enum Outcome { CONTINUE, GAME_OVER_SCARECROW, GAME_OVER_STRIPPED, VACATION_COMPL
signal phase_changed(phase: Phase, previous: Phase)
signal campaign_ended(outcome: Outcome)
## The dawn grace is over: listeners settle the night (enemies leave, loot on the ground returns)
## before its losses are recorded and the outcome is checked.
signal night_ending
@export var config: GameConfig
var state: FarmState
var outcome: Outcome = Outcome.CONTINUE
## Set by FarmSim: night -> its plan's length, when the plan overrides GameConfig.night_length
## (returns 0 or less for "no override").
var night_length_for: Callable
## Co-op, on the partner's machine (a replica of the host's farm): what the player asks for
## (begin_day, retry_night, start_night_early) goes to the host instead ("begin_day" etc.).
var remote_request: Callable
## Where every morning is autosaved ("" = not saved; Main sets SaveGame.PATH).
var save_path := ""
## The last autosave's result.
var last_save_error: Error = OK
var _morning_snapshot: Dictionary = {}
var _night_start := PackedInt32Array()
@ -59,6 +74,9 @@ func tick(delta: float) -> void:
func begin_day() -> bool:
if state.phase != Phase.MORNING:
return false
if remote_request.is_valid():
remote_request.call("begin_day")
return true
_resolve(true)
return true
@ -67,6 +85,9 @@ func begin_day() -> bool:
func start_night_early() -> bool:
if not can_start_night_early():
return false
if remote_request.is_valid():
remote_request.call("start_night_early")
return true
_enter(Phase.NIGHT)
return true
@ -81,8 +102,23 @@ func advance_phase() -> void:
_resolve(true)
## D9: restarts the current night from its morning (the autosave point once saving exists).
## Continues a saved vacation (SaveGame.read): its morning becomes the current one.
func load_state(saved: FarmState) -> void:
state = saved
outcome = Outcome.CONTINUE
_enter(Phase.MORNING)
## The latest morning's state (what retry_night() restores and what was saved).
func morning_snapshot() -> Dictionary:
return _morning_snapshot.duplicate(true)
## D9: restarts the current night from its morning (the autosave point).
func retry_night() -> bool:
if remote_request.is_valid():
remote_request.call("retry_night")
return true
var restored := FarmState.from_dict(_morning_snapshot)
if restored == null:
return false
@ -92,18 +128,33 @@ func retry_night() -> bool:
return true
## D8: gathering, building, and upgrades happen by day (day and dusk), not at night or during
## the morning review.
func is_daytime() -> bool:
return state.phase == Phase.DAY or state.phase == Phase.DUSK
func is_over() -> bool:
return state.phase == Phase.GAME_OVER or state.phase == Phase.FARMERS_RETURN
func is_timed(phase: Phase) -> bool:
return is_finite(config.phase_length(phase))
return is_finite(phase_length(phase))
## Seconds a phase lasts tonight (the night can be longer or shorter than usual), or INF.
func phase_length(phase: Phase) -> float:
if phase == Phase.NIGHT and night_length_for.is_valid():
var override: float = night_length_for.call(state.night)
if override > 0.0:
return override
return config.phase_length(phase)
## The outcome rules as a pure function of the farm state. Scarecrow destruction is checked
## first, then a stripped farm, and only then completing the vacation.
static func check_outcome(s: FarmState, campaign_nights: int) -> Outcome:
if s.scarecrow_hp <= 0:
if s.scarecrow_hp <= 0 and (not s.coop or s.partner_hp <= 0): # in co-op, both down
return Outcome.GAME_OVER_SCARECROW
if s.is_stripped():
return Outcome.GAME_OVER_STRIPPED
@ -115,11 +166,15 @@ static func check_outcome(s: FarmState, campaign_nights: int) -> Outcome:
func _resolve(phase_finished: bool) -> void:
var finished_phase := state.phase
if phase_finished and finished_phase == Phase.DAWN:
night_ending.emit()
state.nights_survived = state.night
state.log_night(_night_start)
state.settle_supplies()
var result := check_outcome(state, config.campaign_nights)
if result != Outcome.CONTINUE:
outcome = result
if result == Outcome.VACATION_COMPLETE and save_path != "":
SaveGame.delete(save_path)
_enter(Phase.FARMERS_RETURN if result == Outcome.VACATION_COMPLETE else Phase.GAME_OVER)
campaign_ended.emit(result)
return
@ -133,11 +188,21 @@ func _resolve(phase_finished: bool) -> void:
func _enter(phase: Phase) -> void:
var previous := state.phase
state.phase = phase
var length := config.phase_length(phase)
var length := phase_length(phase)
state.phase_time_left = length if is_finite(length) else 0.0
match phase:
Phase.MORNING:
state.night_events.clear() # the day's and night's counts start now
if state.coop: # a scarecrow that fell in the night is back, at half health
if state.scarecrow_hp <= 0:
state.scarecrow_hp = ceili(state.scarecrow_max_hp / 2.0)
if state.partner_hp <= 0:
state.partner_hp = ceili(state.partner_max_hp / 2.0)
_morning_snapshot = state.to_dict()
if save_path != "":
last_save_error = SaveGame.write(_morning_snapshot, save_path)
if last_save_error != OK:
push_warning("Autosave to %s failed: %s" % [save_path, error_string(last_save_error)])
Phase.NIGHT:
_night_start = state.asset_counts()
phase_changed.emit(phase, previous)