563 lines
28 KiB
GDScript
563 lines
28 KiB
GDScript
extends RefCounted
|
|
## The thirty nights (Step 10, #27): every night plan and debug scenario is valid (types and
|
|
## targets are known, arrivals fit the night as played), each of the first five nights and the last
|
|
## tests what its plan says it should, the arc holds (the robots from night 3, the UFOs from night 5,
|
|
## no night much lighter than the one before, sieges every fifth night, the last night the longest),
|
|
## the upgrades apply to the systems they change and persist (next morning, JSON, retry), the plans
|
|
## load by count with each night's enemy speed and HP scales, the debug F3 jump moves the morning,
|
|
## and a sample of the nights (1-5, a siege, the last) plays back to back to the farmers' return,
|
|
## the rest skipped by the F3 morning path, each with its own plan and morning snapshot, every
|
|
## planned arrival accounted for (arrived, or dropped at sunrise with its pool full), everything
|
|
## carrying over.
|
|
|
|
const P := CampaignController.Phase
|
|
const T := ThiefBehavior.State
|
|
const DT := 1.0 / 60.0
|
|
const AWAY := Vector3(-11.0, 0.0, 13.0)
|
|
const SCENARIOS := "res://game/resources/scenarios/"
|
|
## The nights the back-to-back run plays (_back_to_back); the others are skipped on F3's path.
|
|
const PLAYED := [1, 2, 3, 4, 5, 10, 30]
|
|
## Every fifth night before the last is a siege (game/tools/make_nights.gd).
|
|
const SIEGES := [10, 15, 20, 25]
|
|
|
|
|
|
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 sim := farm.get_node("FarmSim") as FarmSim
|
|
sim.set_physics_process(false)
|
|
sim.waves.enabled = false
|
|
var player := farm.get_node("Player") as PlayerCharacter
|
|
player.global_position = AWAY
|
|
_validation(r, sim)
|
|
_designs(r, sim)
|
|
_arc(r, sim)
|
|
_upgrades(r, sim, player)
|
|
_plan_paths(r, sim)
|
|
_enemy_scales(r, sim)
|
|
_night_jump(r, farm, sim)
|
|
_back_to_back(r, farm, sim, player)
|
|
farm.queue_free()
|
|
await r.process_frame
|
|
|
|
|
|
## Every plan (the vacation's nights and the debug scenarios) checks out against the farm.
|
|
func _validation(r: GameTestRunner, sim: FarmSim) -> void:
|
|
var plans: Array[NightPlan] = sim.waves.plans.duplicate()
|
|
r.check(plans.size() == sim.config.campaign_nights, "a plan for each of the %d nights" % sim.config.campaign_nights)
|
|
for f in DirAccess.get_files_at(SCENARIOS):
|
|
if f.ends_with(".tres"):
|
|
plans.append(load(SCENARIOS + f) as NightPlan)
|
|
for plan in plans:
|
|
var length := plan.night_length if plan.night_length > 0.0 else sim.config.night_length
|
|
var bad := plan.problems(length, plan.count_scale_in(sim.config), plan.spacing_scale_in(sim.config))
|
|
r.check(bad.is_empty() and plan.title != "", "'%s' is valid%s" % [plan.title, "" if bad.is_empty() else ": %s" % [bad]])
|
|
# The checks catch mistakes.
|
|
var broken := NightPlan.new()
|
|
broken.groups = [{"type": "crow", "target": "crops", "time": 5.0, "count": 1, "every": 1.0},
|
|
{"type": "thief", "target": "crops", "time": 200.0, "count": 2, "every": 1.0},
|
|
{"type": "crow", "target": "animals", "time": 5.0, "count": 0, "every": 1.0},
|
|
{"type": "dragon", "time": 5.0, "count": 1, "every": 1.0}]
|
|
var found := broken.problems(150.0)
|
|
r.check(found.size() == 5, "a broken plan is caught: %s" % [found])
|
|
for k in sim.waves.plans.size():
|
|
r.check(sim.waves.plans[k].dusk_hint.length() > 20, "night %d has a dusk hint" % (k + 1))
|
|
|
|
|
|
## Each of the first five nights, and the last, tests what it's meant to.
|
|
func _designs(r: GameTestRunner, sim: FarmSim) -> void:
|
|
var w := sim.waves
|
|
var n := w.plans
|
|
var cfg := sim.config
|
|
var crow := EnemySystem.TYPE_CROW
|
|
var thief := EnemySystem.TYPE_THIEF
|
|
var breaker := EnemySystem.TYPE_BREAKER
|
|
var ufo := EnemySystem.TYPE_UFO
|
|
r.check(n[0].type_counts().keys() == [crow], "night 1: crows only, at the crops (%s)" % [n[0].type_counts()])
|
|
var targets_2 := {}
|
|
for g in n[1].groups:
|
|
if g["type"] == "thief":
|
|
targets_2[g["target"]] = true
|
|
r.check(n[1].type_counts().has(crow) and n[1].type_counts().has(thief) and not n[1].type_counts().has(breaker)
|
|
and targets_2.has("supplies") and targets_2.has("animals"), "night 2: crows, plus thieves at the shed and the pen")
|
|
r.check(int(n[2].type_counts().get(breaker, 0)) >= 2 and int(n[2].type_counts().get(thief, 0)) >= 3, "night 3: fence breakers, and thieves behind them (%s)" % [n[2].type_counts()])
|
|
var crowded := _crowded_moments(n[3])
|
|
r.check(crowded >= 3, "night 4: %d moments with two groups arriving at once" % crowded)
|
|
var ufo_groups := n[4].groups.filter(func(g: Dictionary) -> bool: return g["type"] == "ufo")
|
|
var ufos_5 := int(n[4].type_counts(n[4].count_scale_in(cfg)).get(ufo, 0))
|
|
r.check(n[4].type_counts().size() == 4 and ufo_groups.size() == 2 and ufo_groups.all(func(g: Dictionary) -> bool: return int(g["count"]) == 1)
|
|
and ufos_5 == 14 and _crowded_moments(n[4]) >= 3 and n[4].night_length == 0.0,
|
|
"night 5: all four types, the first UFOs in two single-UFO groups (%d as played), and night 4's moments at once" % ufos_5)
|
|
var last := n[n.size() - 1]
|
|
var waves_last := _waves(last)
|
|
r.check(last.type_counts().size() == 4 and waves_last >= 3 and last.night_length > cfg.night_length,
|
|
"the last night: all four types in %d waves, over a longer %.0f s night" % [waves_last, last.night_length])
|
|
var totals := []
|
|
for p in n:
|
|
totals.append(p.total(p.count_scale_in(cfg)))
|
|
r.check(totals[n.size() - 1] == totals.max() and totals.count(totals.max()) == 1 and totals[0] == totals.min(),
|
|
"the first night is the smallest, the last the biggest, as played (%s)" % [totals])
|
|
r.check(is_equal_approx(n[0].count_scale_in(cfg), 1.0) and is_equal_approx(n[0].spacing_scale_in(cfg), 1.0)
|
|
and totals[0] <= 12 and n[1].count_scale == 0.0, "night 1 plays as designed, unscaled (%d crows); the later nights at the config's scale" % totals[0])
|
|
|
|
|
|
## #27: the thirty nights' arc (_validation checks each plan as played). Thirty plans; the fence
|
|
## breakers come from night 3 and the UFOs from night 5; as played, no night is more than 15% lighter than the
|
|
## non-siege night before it (from night 3: night 2 -> 3 trades crows and thieves for the first
|
|
## robots); every fifth night is a siege, about 30% heavier at the normal length; and the last
|
|
## night is the longest.
|
|
func _arc(r: GameTestRunner, sim: FarmSim) -> void:
|
|
var cfg := sim.config
|
|
var n := sim.waves.plans
|
|
r.check(cfg.campaign_nights == 30 and n.size() == 30, "the vacation is thirty nights, with a plan for each (%d)" % n.size())
|
|
var first := {}
|
|
for k in n.size():
|
|
for t in n[k].type_counts():
|
|
if not first.has(t):
|
|
first[t] = k + 1
|
|
var robots := int(first.get(EnemySystem.TYPE_BREAKER, 0))
|
|
var ufos := int(first.get(EnemySystem.TYPE_UFO, 0))
|
|
r.check(robots >= 3 and ufos >= 5, "no fence breaker before night 3 (the first on night %d), no UFO before night 5 (night %d)" % [robots, ufos])
|
|
var drops := PackedStringArray()
|
|
var before := 0
|
|
var before_night := 0
|
|
for k in range(2, n.size()):
|
|
var night := k + 1
|
|
if SIEGES.has(night):
|
|
continue
|
|
var played := n[k].total(n[k].count_scale_in(cfg))
|
|
if before > 0 and played < before * 0.85:
|
|
drops.append("night %d -> %d: %d -> %d" % [before_night, night, before, played])
|
|
before = played
|
|
before_night = night
|
|
r.check(drops.is_empty(), "as played, no night from 3 on is more than 15%% lighter than the non-siege night before it%s"
|
|
% ("" if drops.is_empty() else ": %s" % [drops]))
|
|
var sieges := true
|
|
for night in SIEGES:
|
|
var p := n[night - 1]
|
|
var prev := n[night - 2]
|
|
sieges = sieges and p.title.begins_with("Siege: ") and p.night_length == 0.0 and p.total() >= roundi(prev.total() * 1.2)
|
|
r.check(sieges, "nights %s are sieges: 'Siege: ...', about 30%% heavier than the night before, at the normal %.0f s" % [SIEGES, cfg.night_length])
|
|
var longest := true
|
|
var last_length := n[n.size() - 1].night_length
|
|
for k in n.size() - 1:
|
|
longest = longest and (n[k].night_length if n[k].night_length > 0.0 else cfg.night_length) < last_length
|
|
r.check(longest, "the last night is the longest (%.0f s)" % last_length)
|
|
|
|
|
|
## Times when two groups start arriving within 3 s of each other.
|
|
func _crowded_moments(plan: NightPlan) -> int:
|
|
var moments := {}
|
|
for i in plan.groups.size():
|
|
for j in range(i + 1, plan.groups.size()):
|
|
var a := float(plan.groups[i]["time"])
|
|
var b := float(plan.groups[j]["time"])
|
|
if absf(a - b) <= 3.0:
|
|
moments[roundi(minf(a, b) / 10.0)] = true
|
|
return moments.size()
|
|
|
|
|
|
## Clusters of arrivals at least 25 s apart.
|
|
func _waves(plan: NightPlan) -> int:
|
|
var times: Array[float] = []
|
|
for g in plan.groups:
|
|
times.append(float(g["time"]))
|
|
times.sort()
|
|
var waves := 1
|
|
for k in range(1, times.size()):
|
|
if times[k] - times[k - 1] >= 25.0:
|
|
waves += 1
|
|
return waves
|
|
|
|
|
|
## The upgrades (level 1 of each) change the systems they're about, can't be bought twice (a
|
|
## one-off) or before their day (a next level) or at night, and persist.
|
|
func _upgrades(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
|
|
var c := sim.campaign
|
|
var s := c.state
|
|
var cfg := sim.config
|
|
var es := sim.enemies
|
|
var st := sim.structures
|
|
c.begin_day()
|
|
s.resources.shed = {"wood": 200, "scrap": 200, "straw": 200, "alien_goop": 200}
|
|
s.night_events["gathered_alien_goop"] = cfg.hidden_reveal_goop # so the Alien Ray (#7, hidden) buys below
|
|
var fence := st.place("fence", 8.0, -8.0, 0)
|
|
_settle(sim)
|
|
var before := {
|
|
"interval": sim.combat.interval_ticks(), "damage": sim.combat.shot_damage(), "radius": sim.scarecrow.fear_radius(),
|
|
"cooldown": sim.scarecrow.fear_cooldown(), "max_hp": s.scarecrow_max_hp, "fence": st.max_hp("fence"),
|
|
"gate": st.max_hp("gate"), "range": st.turret_range(), "slow": st.trap_slow_time(),
|
|
}
|
|
var morning := s.to_dict()
|
|
var shed := s.resources.shed_total()
|
|
var spent := 0
|
|
r.check(not player.can_sprint(), "(no sprint before the Sprint upgrade)")
|
|
var day := s.night
|
|
s.night = 14 # every shed item is in stock by day 14 (#26's from_day)
|
|
for id in Upgrades.ORDER:
|
|
spent += _sum(Upgrades.cost(cfg, id))
|
|
var text := Upgrades.buy(c, id, st)
|
|
r.check(Upgrades.level(s, id) == 1 and text.contains("done:"), "bought %s: '%s'" % [id, text])
|
|
s.night = day
|
|
r.check(s.resources.shed_total() == shed - spent, "all %d cost %d materials together (level 1 each)" % [Upgrades.ORDER.size(), spent])
|
|
r.check(player.can_sprint(), "sprint: the scarecrow can sprint")
|
|
r.check(Upgrades.buy(c, "sprint", st).contains("already done") and Upgrades.buy(c, "rapid_fire", st).contains("level 2 opens on day 8")
|
|
and Upgrades.level(s, "rapid_fire") == 1, "a one-off can't be bought twice, and an upgrade's level 2 waits for day 8")
|
|
r.check(sim.combat.interval_ticks() < int(before["interval"]) and sim.combat.shot_damage() == int(before["damage"]) + 1,
|
|
"rapid fire and heavy seeds: a shot every %d ticks (was %d), %d damage" % [sim.combat.interval_ticks(), before["interval"], sim.combat.shot_damage()])
|
|
r.check(is_equal_approx(sim.scarecrow.fear_radius(), float(before["radius"]) * 1.3) and is_equal_approx(sim.scarecrow.fear_cooldown(), float(before["cooldown"]) * 0.8),
|
|
"wide and quick fear: %.1f m, %.1f s" % [sim.scarecrow.fear_radius(), sim.scarecrow.fear_cooldown()])
|
|
r.check(s.scarecrow_max_hp == int(before["max_hp"]) + 2 and s.scarecrow_hp == s.scarecrow_max_hp, "straw stuffing: +2 max health")
|
|
r.check(st.max_hp("fence") == roundi(int(before["fence"]) * 1.5) and st.max_hp("gate") == roundi(int(before["gate"]) * 1.5)
|
|
and int(s.structure(fence)["hp"]) == st.max_hp("fence"), "reinforced fences: fences %d HP, gates %d; a standing fence keeps its full health" % [st.max_hp("fence"), st.max_hp("gate")])
|
|
r.check(is_equal_approx(st.turret_range(), float(before["range"]) * 1.3) and is_equal_approx(st.trap_slow_time(), float(before["slow"]) * 1.5),
|
|
"long-range defenses %.1f m, sticky tar %.1f s" % [st.turret_range(), st.trap_slow_time()])
|
|
# The effects are real: a shot does 2 damage, a defense reaches a thief beyond its old range,
|
|
# a trap holds longer.
|
|
var keep := es.behavior
|
|
es.behavior = null
|
|
var h := es.spawn(EnemySystem.TYPE_THIEF, 10.0, -8.0, Vector2.DOWN)
|
|
_step(sim, 1)
|
|
sim.projectiles.fire(Vector3(10.0, cfg.combat_height, -2.0), Vector3(10.0, cfg.combat_height, -3.0), Vector3(0, 0, -1), sim.combat.shot_damage())
|
|
for i in 20:
|
|
sim.projectiles.step(DT)
|
|
r.check(es.health[es.resolve_handle(h)] == es.t_hp[EnemySystem.TYPE_THIEF] - 2, "a heavy seed takes 2 health off a thief")
|
|
es.apply_hit_handle(h, 99)
|
|
st.dismantle(fence)
|
|
var turret := st.place("automated_defense", 6.0, -13.0, 0)
|
|
var far := es.spawn(EnemySystem.TYPE_THIEF, 6.0 + float(before["range"]) + 1.0, -13.0, Vector2.UP)
|
|
_step(sim, 90)
|
|
r.check(es.resolve_handle(far) < 0 or es.health[es.resolve_handle(far)] < es.t_hp[EnemySystem.TYPE_THIEF], "a defense hits a thief %.0f m away (its old range: %.0f m)" % [float(before["range"]) + 1.0, before["range"]])
|
|
for d in es.active_count:
|
|
es.apply_hit(d, 99)
|
|
st.dismantle(turret)
|
|
var trap := st.place("slowing_trap", 8.0, -12.0, 0)
|
|
_settle(sim)
|
|
h = es.spawn(EnemySystem.TYPE_THIEF, 8.0, -12.0, Vector2.RIGHT)
|
|
_step(sim, 1)
|
|
r.check(absf(es.slow_left[es.resolve_handle(h)] - st.trap_slow_time()) < 0.05, "a trap slows for %.1f s" % es.slow_left[es.resolve_handle(h)])
|
|
es.apply_hit_handle(h, 99)
|
|
st.dismantle(trap)
|
|
es.behavior = keep
|
|
_step(sim, 1)
|
|
# They persist: into the next morning, through JSON, and in a retry from that morning; a retry
|
|
# from before they were bought takes them back.
|
|
var bought := s.upgrades.duplicate()
|
|
while s.phase != P.NIGHT:
|
|
c.advance_phase()
|
|
r.check(Upgrades.blocker(c, "sprint") == "Done" and Upgrades.buy(c, "radar", st).contains("already done"), "(owned ones stay owned at night)")
|
|
s.upgrades.erase("sticky_tar")
|
|
r.check(Upgrades.blocker(c, "sticky_tar") == "The work area is for daytime", "nothing can be bought at night")
|
|
s.upgrades["sticky_tar"] = 1
|
|
while s.phase != P.MORNING:
|
|
c.advance_phase()
|
|
r.check(s.upgrades == bought, "the upgrades carry into the next morning")
|
|
var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(s.to_dict(), "", true, true)))
|
|
r.check(back != null and back.upgrades == bought, "and through a save")
|
|
c.begin_day()
|
|
s.scarecrow_hp = 0
|
|
_step(sim, 1)
|
|
c.retry_night()
|
|
r.check(s != c.state and c.state.upgrades == bought, "a retry from that morning keeps them")
|
|
var old := FarmState.from_dict(morning)
|
|
r.check(old.upgrades.is_empty() and old.resources.shed_total() == shed, "(the morning before they were bought had none, and the materials)")
|
|
c.new_campaign()
|
|
_step(sim, 1)
|
|
r.check(c.state.upgrades.is_empty() and sim.combat.shot_damage() == 1 and st.max_hp("fence") == cfg.fence_hp and not player.can_sprint(),
|
|
"a new vacation starts without them")
|
|
player.global_position = AWAY
|
|
|
|
|
|
## #24: the plans load by count, night_plan_path(n) for every night of the vacation; a missing
|
|
## file plays the last plan that loaded.
|
|
func _plan_paths(r: GameTestRunner, sim: FarmSim) -> void:
|
|
var nights := sim.config.campaign_nights
|
|
var all_load := sim.waves.plans.size() == nights
|
|
for n in range(1, nights + 1):
|
|
var path := FarmSim.night_plan_path(n)
|
|
var plan: NightPlan = load(path) as NightPlan if ResourceLoader.exists(path) else null
|
|
all_load = all_load and path == "res://game/resources/nights/night_%d.tres" % n and plan != null and sim.waves.plans[n - 1] == plan
|
|
r.check(all_load, "night_plan_path(n) loads a plan for every night 1..%d, in order" % nights)
|
|
r.info("(the error just below, about night %d's missing plan, is expected)" % (nights + 1))
|
|
var more := FarmSim.load_night_plans(nights + 1)
|
|
r.check(more.size() == nights + 1 and more[nights] == more[nights - 1] and more[nights] != null,
|
|
"a night without a plan file plays the last plan that loaded")
|
|
|
|
|
|
## #24: tonight's enemy scales. The vacation's own plans keep every enemy as it was; a plan with
|
|
## enemy_speed_scale and enemy_hp_scale makes that night's enemies faster and tougher (HP rounded
|
|
## down, never below the type's: a crow stays one hit); the morning puts them back to 1.
|
|
func _enemy_scales(r: GameTestRunner, sim: FarmSim) -> void:
|
|
var c := sim.campaign
|
|
var es := sim.enemies
|
|
var w := sim.waves
|
|
c.new_campaign()
|
|
_step(sim, 1)
|
|
var fastest := 0.0
|
|
for v in es.t_speed:
|
|
fastest = maxf(fastest, v)
|
|
var as_planned := true
|
|
for k in w.plans.size():
|
|
var after_5 := maxi(k + 1 - 5, 0)
|
|
as_planned = (as_planned and is_equal_approx(w.plans[k].enemy_speed_scale, 1.0 + 0.01 * after_5)
|
|
and is_equal_approx(w.plans[k].enemy_hp_scale, 1.0 + 0.025 * after_5))
|
|
var last := w.plans[w.plans.size() - 1]
|
|
r.check(as_planned, "nights 1-5 keep the enemies' own speed and HP; from night 6 they rise 1%% and 2.5%% a night, to x%.2f and x%.3f on night %d"
|
|
% [last.enemy_speed_scale, last.enemy_hp_scale, w.plans.size()])
|
|
# A plan with the default scales: as before.
|
|
while c.state.phase != P.NIGHT:
|
|
c.advance_phase()
|
|
var hp := _spawn_hp(es)
|
|
var top := _top_speed(sim, EnemySystem.TYPE_CROW)
|
|
r.check(es.night_speed_scale == 1.0 and es.night_hp_scale == 1.0 and hp == Array(es.t_hp)
|
|
and absf(top - es.t_speed[EnemySystem.TYPE_CROW]) < 0.05 and is_equal_approx(es.max_speed(), fastest),
|
|
"a night at the default scales: HP %s, a crow flies at %.2f m/s, as before" % [hp, top])
|
|
while c.state.phase != P.MORNING:
|
|
c.advance_phase()
|
|
# A night at 1.6 (the debug plan's own scales stand for tonight's).
|
|
var tough := NightPlan.new()
|
|
tough.title = "Tough"
|
|
tough.enemy_speed_scale = 1.6
|
|
tough.enemy_hp_scale = 1.6
|
|
tough.groups = [{"type": "crow", "target": "crops", "time": 5.0, "count": 1, "every": 1.0}]
|
|
w.debug_plan = tough
|
|
while c.state.phase != P.NIGHT:
|
|
c.advance_phase()
|
|
hp = _spawn_hp(es)
|
|
top = _top_speed(sim, EnemySystem.TYPE_CROW)
|
|
var ids := [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO]
|
|
r.check(hp[ids[0]] == 1 and hp[ids[1]] == 4 and hp[ids[2]] == 9 and hp[ids[3]] == 3,
|
|
"at HP x1.6 a crow stays at 1 HP, a thief goes 3 -> %d, a breaker 6 -> %d, a UFO 2 -> %d" % [hp[ids[1]], hp[ids[2]], hp[ids[3]]])
|
|
r.check(absf(top - es.t_speed[EnemySystem.TYPE_CROW] * 1.6) < 0.05 and is_equal_approx(es.max_speed(), fastest * 1.6),
|
|
"at speed x1.6 a crow flies at %.2f m/s (its own %.1f)" % [top, es.t_speed[EnemySystem.TYPE_CROW]])
|
|
while c.state.phase != P.MORNING:
|
|
c.advance_phase()
|
|
r.check(es.night_speed_scale == 1.0 and es.night_hp_scale == 1.0 and es.full_hp(EnemySystem.TYPE_THIEF) == es.t_hp[EnemySystem.TYPE_THIEF],
|
|
"the morning puts the scales back to 1")
|
|
w.debug_plan = null
|
|
c.new_campaign()
|
|
_step(sim, 1)
|
|
|
|
|
|
## The HP each enemy type arrives with right now (by type id); they leave again at once.
|
|
func _spawn_hp(es: EnemySystem) -> Array:
|
|
var out := []
|
|
for t in EnemySystem.TYPE_NAMES.size():
|
|
var h := es.spawn(t, 12.0 + 3.0 * t, -2.0, Vector2.UP)
|
|
out.append(es.health[es.resolve_handle(h)] if h >= 0 else -1)
|
|
es.remove_all()
|
|
return out
|
|
|
|
|
|
## How fast an enemy of type goes flat out (full desire, no behavior) after a second.
|
|
func _top_speed(sim: FarmSim, type: int) -> float:
|
|
var es := sim.enemies
|
|
var keep := es.behavior
|
|
es.behavior = null
|
|
var d := es.resolve_handle(es.spawn(type, 0.0, 0.0, Vector2.RIGHT))
|
|
var speed := -1.0
|
|
if d >= 0:
|
|
for i in 60:
|
|
es.desire_x[d] = 1.0
|
|
es.desire_z[d] = 0.0
|
|
es.pace[d] = 1.0
|
|
es.step(DT, Vector2(INF, INF), FarmSim.SCARECROW_RADIUS)
|
|
speed = Vector2(es.vel_x[d], es.vel_z[d]).length()
|
|
es.remove_all()
|
|
es.behavior = keep
|
|
return speed
|
|
|
|
|
|
## #24: F3 (CampaignDebug.jump_morning) moves the morning review to the next day's morning and
|
|
## Shift+F3 to the previous one, with the farm as it is: nights_survived follows, the Workbench
|
|
## comes, a field sprouts, the retry snapshot and the review panel are the new morning's, and
|
|
## nothing is saved.
|
|
func _night_jump(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void:
|
|
var c := sim.campaign
|
|
var debug := farm.get_node("CampaignDebug") as CampaignDebug
|
|
var st := sim.structures
|
|
c.new_campaign()
|
|
_step(sim, 1)
|
|
var s := c.state
|
|
s.resources.shed = {"wood": 99, "scrap": 99, "straw": 99}
|
|
var fence := st.place("fence", 8.0, -8.0, 0)
|
|
s.scarecrow_hp = s.scarecrow_max_hp - 1
|
|
var shed := s.resources.shed.duplicate()
|
|
var crops := s.crops_total
|
|
var dormant := sim.loot.count(LootSystem.Status.DORMANT)
|
|
var keep_path := c.save_path
|
|
c.save_path = "user://test_night_jump.json"
|
|
if FileAccess.file_exists(c.save_path):
|
|
DirAccess.remove_absolute(c.save_path)
|
|
r.check(not debug.jump_morning(-1) and s.night == 1, "Shift+F3 on the first morning stays there")
|
|
var f3 := InputEventKey.new()
|
|
f3.physical_keycode = KEY_F3
|
|
f3.pressed = true
|
|
debug._unhandled_input(f3)
|
|
var tonight := sim.waves.plan_for(2).title
|
|
var body := (sim.review_panel._body as Label).text
|
|
r.check(c.state == s and s.night == 2 and s.nights_survived == 1 and s.phase == P.MORNING,
|
|
"F3: the morning of day 2 (%d survived)" % s.nights_survived)
|
|
r.check(s.structure(fence).size() > 0 and s.resources.shed == shed and s.scarecrow_hp == s.scarecrow_max_hp - 1
|
|
and s.crops_total == crops and st.has_workbench(),
|
|
"the farm is as it was (the fence, the shed, the scarecrow's HP), and the Workbench is granted")
|
|
r.check(c.morning_snapshot() == s.to_dict() and sim.review_panel.visible and body.contains("Tonight: %s." % tonight),
|
|
"the retry snapshot and the review panel are day 2's")
|
|
debug._unhandled_input(f3)
|
|
r.check(s.night == 3 and s.nights_survived == 2 and s.crops_total == crops + 30 and sim.loot.count(LootSystem.Status.DORMANT) == dormant - 30,
|
|
"F3 again: day 3, and the field due after night 2 sprouts (%d crops)" % s.crops_total)
|
|
var back := f3.duplicate() as InputEventKey
|
|
back.shift_pressed = true
|
|
debug._unhandled_input(back)
|
|
r.check(s.night == 2 and s.nights_survived == 1 and c.morning_snapshot()["night"] == 2, "Shift+F3: back to day 2")
|
|
r.check(not FileAccess.file_exists(c.save_path), "a jump doesn't autosave")
|
|
c.begin_day()
|
|
r.check(not debug.jump_morning(1) and s.night == 2, "F3 only works in the morning review")
|
|
c.save_path = keep_path
|
|
c.new_campaign()
|
|
_step(sim, 1)
|
|
|
|
|
|
## The vacation back to back to the farmers' return: the PLAYED nights (1-5, a siege, the last),
|
|
## each with its own plan and morning snapshot; the nights between are skipped on F3's morning path
|
|
## (CampaignDebug.jump_morning: the next morning, the farm as it is). The farm is defended, and the
|
|
## scarecrow kept out of harm. Every planned arrival arrives, or waits for a free character and is
|
|
## dropped at sunrise (#25), and nothing vanishes: here no scarecrow fights and few breakers die or
|
|
## leave, so the breakers past their pool's 20 wait all night.
|
|
func _back_to_back(r: GameTestRunner, farm: Node3D, sim: FarmSim, player: PlayerCharacter) -> void:
|
|
var c := sim.campaign
|
|
var hud := farm.get_node("Hud/PhaseHud") as PhaseHud
|
|
var debug := farm.get_node("CampaignDebug") as CampaignDebug
|
|
var nights := c.config.campaign_nights
|
|
c.new_campaign()
|
|
_step(sim, 1)
|
|
sim.structures.grant_workbench() # #9: granted once, up front, so the per-night structure
|
|
# count below (broken aside) doesn't trip over the night-1 grant itself
|
|
c._morning_snapshot = c.state.to_dict() # ...and this morning's snapshot (the loop checks it) agrees
|
|
sim.waves.enabled = true
|
|
player.global_position = AWAY
|
|
var arrivals := {}
|
|
sim.waves.arrived.connect(func(h: int, _at: Vector2) -> void:
|
|
var t: int = sim.enemies.type[sim.enemies.resolve_handle(h)]
|
|
arrivals[t] = int(arrivals.get(t, 0)) + 1)
|
|
# Each ground type's character pool as the night ends (#25: arrivals still waiting for a free
|
|
# character are dropped then): full, or full at the end of the night's last tick before it
|
|
# (a character can come free in the very tick the night ends, after the arrivals were looked at).
|
|
var full_at_end := {}
|
|
var full_last_tick := {}
|
|
var ground: Array[int] = []
|
|
for t in EnemySystem.TYPE_NAMES.size():
|
|
if sim.enemies.t_flying[t] == 0:
|
|
ground.append(t)
|
|
sim.campaign.phase_changed.connect(func(phase: CampaignController.Phase, previous: CampaignController.Phase) -> void:
|
|
if previous == P.NIGHT and phase != P.NIGHT:
|
|
for t in ground:
|
|
full_at_end[t] = not sim.characters.has_free(t))
|
|
var broken: Array[int] = []
|
|
sim.structures.destroyed.connect(func(id: int, _t: String, _at: Vector3) -> void: broken.append(id))
|
|
var all_good := true
|
|
var skipped_ok := true
|
|
var lengths := {}
|
|
var t0 := Time.get_ticks_msec()
|
|
for night: int in PLAYED:
|
|
while c.state.night < night and c.state.phase == P.MORNING:
|
|
var from := c.state.night
|
|
if not debug.jump_morning(1):
|
|
skipped_ok = false
|
|
break
|
|
skipped_ok = skipped_ok and c.state.night == from + 1 and c.state.nights_survived == from
|
|
var s := c.state
|
|
all_good = all_good and s.night == night and s.phase == P.MORNING and c._morning_snapshot == s.to_dict()
|
|
c.begin_day()
|
|
if night == 1:
|
|
_defend(sim)
|
|
s.resources.shed = {"wood": 99, "scrap": 99, "straw": 99}
|
|
while c.state.phase != P.NIGHT:
|
|
c.advance_phase()
|
|
lengths[night] = c.state.phase_time_left
|
|
hud._on_phase_changed(P.NIGHT, P.DUSK)
|
|
var plan := sim.waves.plan_for(night)
|
|
var title_ok := hud._banner.text == "Night %d: %s" % [night, plan.title]
|
|
arrivals.clear()
|
|
full_at_end.clear()
|
|
full_last_tick.clear()
|
|
var skipped0 := sim.enemies.spawn_skipped
|
|
var structures_at_night := c.state.structures.size()
|
|
broken.clear()
|
|
var t_night := Time.get_ticks_msec()
|
|
while c.state.phase != P.MORNING and not c.is_over():
|
|
sim.scarecrow.invulnerable_left = 1.0
|
|
sim.step(DT)
|
|
if c.state.phase == P.NIGHT and c.state.phase_time_left <= 2.0 * DT: # the night's last ticks
|
|
for t in ground:
|
|
full_last_tick[t] = not sim.characters.has_free(t)
|
|
# Every planned arrival arrived or, its type's characters all in use when the night ended,
|
|
# was still waiting for one and dropped at sunrise (#25); none vanished (spawn_skipped).
|
|
var expect := plan.type_counts(plan.count_scale_in(c.config))
|
|
var dropped := {}
|
|
var accounted := sim.enemies.spawn_skipped == skipped0
|
|
for t in expect:
|
|
var gone := int(expect[t]) - int(arrivals.get(t, 0))
|
|
accounted = accounted and gone >= 0
|
|
if gone > 0:
|
|
dropped[t] = gone
|
|
accounted = accounted and ground.has(t) and (bool(full_at_end.get(t, false)) or bool(full_last_tick.get(t, false)))
|
|
for t in arrivals:
|
|
accounted = accounted and expect.has(t)
|
|
var dropped_total := 0
|
|
for t in dropped:
|
|
dropped_total += int(dropped[t])
|
|
accounted = accounted and dropped_total == sim.waves.dropped
|
|
r.check(accounted and title_ok, "night %d, '%s': the plan's %s arrived %s, %s dropped at sunrise with every character of the type in use (%d skipped)" % [
|
|
night, plan.title, expect, arrivals, dropped, sim.enemies.spawn_skipped - skipped0])
|
|
var e: Dictionary = c.state.night_log[c.state.night_log.size() - 1]
|
|
var dropped_names := PackedStringArray()
|
|
for t in dropped:
|
|
dropped_names.append("%d %s" % [dropped[t], EnemySystem.TYPE_NAMES[t]])
|
|
r.info("night %d: %d of %d arrived, %d dropped%s; %d crops, %d animals, %d supplies lost; %d structures broken (%.1f s)" % [
|
|
night, sim.waves.spawned, plan.total(plan.count_scale_in(c.config)), dropped_total,
|
|
" (%s)" % ", ".join(dropped_names) if not dropped_names.is_empty() else "", e["crops_lost"],
|
|
e["animals_lost"], e["supplies_lost"], broken.size(), (Time.get_ticks_msec() - t_night) / 1000.0])
|
|
if night < nights:
|
|
all_good = all_good and c.state.structures.size() == structures_at_night - broken.size() and c.state.supplies_in_transit == 0
|
|
r.check(all_good and skipped_ok, "every morning is its night's snapshot, the farm carries over night to night, and F3 skips the nights between")
|
|
r.check(c.state.phase == P.FARMERS_RETURN and c.state.nights_survived == nights and c.state.night_log.size() == PLAYED.size(),
|
|
"nights %s played, the rest skipped, to the farmers' return after night %d (%.1f s)" % [PLAYED, nights, (Time.get_ticks_msec() - t0) / 1000.0])
|
|
var others := true
|
|
for night: int in PLAYED:
|
|
others = others and (night == nights or is_equal_approx(lengths[night], c.config.night_length))
|
|
r.check(is_equal_approx(lengths[nights], sim.waves.plans[nights - 1].night_length) and others,
|
|
"the last night is longer (%.0f s; the others %.0f s)" % [lengths[nights], lengths[1]])
|
|
sim.waves.enabled = false
|
|
|
|
|
|
## Automated defenses by the crops, the pen, and the shed.
|
|
func _defend(sim: FarmSim) -> void:
|
|
var st := sim.structures
|
|
sim.campaign.state.resources.shed = {"wood": 99, "scrap": 99, "straw": 99}
|
|
for at in [Vector2(13.5, -15.0), Vector2(46.0, -15.0), Vector2(13.5, 9.0), Vector2(46.0, 9.0), Vector2(30.0, -17.5),
|
|
Vector2(30.0, 11.5), Vector2(7.0, -3.5), Vector2(9.5, -14.0), Vector2(-3.5, -12.0)]:
|
|
if st.validate("automated_defense", at.x, at.y, 0, at) in ["", "The scarecrow is in the way"]:
|
|
st.place("automated_defense", at.x, at.y, 0)
|
|
_settle(sim)
|
|
|
|
|
|
static func _sum(cost: Dictionary) -> int:
|
|
var n := 0
|
|
for m in cost:
|
|
n += int(cost[m])
|
|
return n
|
|
|
|
|
|
func _settle(sim: FarmSim) -> void:
|
|
var guard := 0
|
|
while sim.rebuilding and guard < 600:
|
|
sim.step(DT)
|
|
guard += 1
|
|
|
|
|
|
func _step(sim: FarmSim, n: int) -> void:
|
|
for i in n:
|
|
sim.step(DT)
|