505 lines
21 KiB
GDScript
505 lines
21 KiB
GDScript
extends SceneTree
|
|
## Plays whole nights headless against the night thief trickle with a scripted scarecrow and
|
|
## reports each night, as supporting evidence for Step 4's exit gate (the gate itself is a
|
|
## human playtest). Days are skipped; each night runs its full night_length plus the dawn grace.
|
|
## godot --headless --path . --script res://game/tools/soak_night.gd -- --mode=guard
|
|
## Options:
|
|
## --mode=cycle plays whole cycles: by day it gathers until GATHER_UNTIL, then puts a
|
|
## gate back in the pen opening if it's gone and builds BUILD_PLAN in order
|
|
## (what it can afford, walking to each site), and at night it guards (as
|
|
## --mode=guard) against the night's plan. Reports each cycle (per target)
|
|
## and checks that structures, materials, and health carry over.
|
|
## --cycles=3 sets how many (5 plays the whole vacation, one night plan
|
|
## each); --build=0 skips building, --guard=0 leaves the night to the
|
|
## defenses. With materials left after the plan it buys upgrades
|
|
## (UPGRADE_ORDER) at the workbench; --upgrades=0 skips them.
|
|
## --mode=gather plays one day instead: the scarecrow walks (along the navigation
|
|
## grid) to the nearest gather point with charges, holds interact until it
|
|
## is used up or the pouch is full, stores the pouch at the shed, and
|
|
## reports how long gathering everything takes and what it yields.
|
|
## --mode=idle | guard idle: the scarecrow stands in the open doing nothing (baseline).
|
|
## guard: it goes for the most urgent threat (carriers, then thieves
|
|
## taking something, then dropped loot and loose animals, then other
|
|
## thieves), attacks thieves in range with a clear line with its best
|
|
## weapon (a pumpkin bomb into a crowd, the corn shooter once bought, else
|
|
## the pitchfork, closing in to stab), uses fear on a
|
|
## carrier or a crowd in range, leads animals following it back to the pen
|
|
## when no carrier is close, and patches up when hurt and nothing is close.
|
|
## --minutes=10 night time to play (rounded up to whole nights)
|
|
## --thieves=1 GameConfig.night_thieves (at most this many at once)
|
|
## --seed=1 randomizes thief targets and arrival points
|
|
|
|
const DT := 1.0 / 60.0
|
|
const GATHER_UNTIL := 115.0
|
|
## The scripted scarecrow's defenses, in order: automated defenses around the crop field's
|
|
## corners, slowing traps on the approaches, then fences down the field's east side.
|
|
const BUILD_PLAN := [
|
|
["automated_defense", Vector2(46.0, -15.0), 0],
|
|
["automated_defense", Vector2(34.0, 12.5), 0],
|
|
["slowing_trap", Vector2(43.5, -17.0), 0],
|
|
["automated_defense", Vector2(13.5, -15.0), 0],
|
|
["slowing_trap", Vector2(31.0, 11.5), 0],
|
|
["fence", Vector2(45.0, -15.5), 2], ["fence", Vector2(45.0, -13.5), 2], ["fence", Vector2(45.0, -11.5), 2],
|
|
["fence", Vector2(45.0, -9.5), 2], ["fence", Vector2(45.0, -7.5), 2], ["fence", Vector2(45.0, -5.5), 2],
|
|
["automated_defense", Vector2(13.5, 9.0), 0],
|
|
["automated_defense", Vector2(7.0, -3.5), 0],
|
|
["automated_defense", Vector2(9.5, -14.0), 0],
|
|
]
|
|
## The scripted scarecrow's upgrades, in order, bought with what's left after building.
|
|
const UPGRADE_ORDER: Array[String] = ["corn_shooter", "long_range", "rapid_fire", "heavy_seeds", "pumpkin_bomb",
|
|
"reinforced_fences", "quick_fear", "wide_fear", "sticky_tar", "straw_stuffing"]
|
|
const PEN_GATE := Vector2(4.0, -6.0)
|
|
const PEN_INSIDE := Vector2(-2.0, -6.0)
|
|
const HOME := Vector2(10.0, -8.0) # open ground between the pen, the shed, and the field
|
|
const SHOOT_RANGE := 11.0
|
|
|
|
var _sim: FarmSim
|
|
var _player: PlayerCharacter
|
|
var _mode := "guard"
|
|
var _fears: int = 0
|
|
var _shots: int = 0
|
|
|
|
|
|
func _initialize() -> void:
|
|
_run.call_deferred()
|
|
|
|
|
|
func _run() -> void:
|
|
var minutes := 10.0
|
|
var thieves := 1
|
|
var seed_value := 1
|
|
for a in OS.get_cmdline_user_args():
|
|
var arg := String(a)
|
|
if arg.begins_with("--mode="):
|
|
_mode = arg.trim_prefix("--mode=")
|
|
elif arg.begins_with("--minutes="):
|
|
minutes = float(arg.trim_prefix("--minutes="))
|
|
elif arg.begins_with("--thieves="):
|
|
thieves = int(arg.trim_prefix("--thieves="))
|
|
elif arg.begins_with("--seed="):
|
|
seed_value = int(arg.trim_prefix("--seed="))
|
|
var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D
|
|
root.add_child(farm)
|
|
await process_frame
|
|
_sim = farm.get_node("FarmSim") as FarmSim
|
|
_sim.set_physics_process(false)
|
|
_player = farm.get_node("Player") as PlayerCharacter
|
|
_player.set_physics_process(false)
|
|
var c := _sim.campaign
|
|
var cfg := _sim.config
|
|
cfg.night_thieves = thieves
|
|
cfg.campaign_nights = 99
|
|
_sim.loot._rng.seed = seed_value
|
|
_sim.waves.rng.seed = seed_value
|
|
_player.global_position = Vector3(HOME.x, 0.0, HOME.y)
|
|
if _mode == "gather":
|
|
_gather_day()
|
|
_quit()
|
|
return
|
|
if _mode == "cycle":
|
|
var cycles := 3
|
|
for a in OS.get_cmdline_user_args():
|
|
if String(a).begins_with("--cycles="):
|
|
cycles = int(String(a).trim_prefix("--cycles="))
|
|
cfg.night_thieves = 0 # the night's plan
|
|
for a in OS.get_cmdline_user_args():
|
|
if String(a).begins_with("--thieves="):
|
|
cfg.night_thieves = int(String(a).trim_prefix("--thieves="))
|
|
for a in OS.get_cmdline_user_args():
|
|
if String(a) == "--build=0":
|
|
_do_build = false
|
|
elif String(a) == "--guard=0":
|
|
_do_guard = false
|
|
elif String(a) == "--upgrades=0":
|
|
_do_upgrades = false
|
|
_cycles(cycles)
|
|
_quit()
|
|
return
|
|
var nights := ceili(minutes * 60.0 / cfg.night_length)
|
|
print("soak: mode=%s, %d nights of %.0f s + %.0f s dawn, night_thieves=%d, seed=%d" % [
|
|
_mode, nights, cfg.night_length, cfg.dawn_grace, thieves, seed_value])
|
|
print("| Night | Stolen | Recovered | Lost | Crops left | Scarecrow HP | Fears | Attacks | Kills |")
|
|
print("|---|---|---|---|---|---|---|---|---|")
|
|
var t0 := Time.get_ticks_msec()
|
|
for n in nights:
|
|
c.begin_day()
|
|
c.advance_phase()
|
|
c.advance_phase()
|
|
var kills0 := _sim.enemies.kills
|
|
var fears0 := _fears
|
|
var shots0 := _shots
|
|
while c.state.phase != CampaignController.Phase.MORNING and not c.is_over():
|
|
if _mode == "guard":
|
|
_guard()
|
|
_sim.step(DT)
|
|
if c.is_over():
|
|
print("| %d | campaign over: %s |" % [c.state.night, CampaignController.Outcome.keys()[c.outcome]])
|
|
break
|
|
var e: Dictionary = c.state.night_log[c.state.night_log.size() - 1]
|
|
print("| %d | %d | %d | %d | %d/%d | %d/%d | %d | %d | %d |" % [e["night"], e["crops_stolen"], e["crops_recovered"],
|
|
e["crops_lost"], c.state.crops, c.state.crops_total, c.state.scarecrow_hp, c.state.scarecrow_max_hp,
|
|
_fears - fears0, _shots - shots0, _sim.enemies.kills - kills0])
|
|
Input.action_release("patch")
|
|
print("soak: %.1f s of wall time" % ((Time.get_ticks_msec() - t0) / 1000.0))
|
|
_quit()
|
|
|
|
|
|
## Frees the farm, then quits once the audio server has dropped its stopped sounds.
|
|
func _quit() -> void:
|
|
for n in root.get_children():
|
|
n.queue_free()
|
|
await process_frame
|
|
await create_timer(0.25).timeout
|
|
quit()
|
|
|
|
|
|
var _fields := {}
|
|
var _built := {} # BUILD_PLAN index -> true
|
|
var _do_build := true
|
|
var _do_guard := true
|
|
var _do_upgrades := true
|
|
var _carry_errors: Array[String] = []
|
|
|
|
|
|
func _cycles(n: int) -> void:
|
|
var c := _sim.campaign
|
|
var s := c.state
|
|
print("soak cycle: %d cycles, night plan '%s' (%d thieves), gather until %.0f s, build %s, guard %s" % [
|
|
n, _sim.waves.tonight().title, _sim.waves.tonight().total(), GATHER_UNTIL, _do_build, _do_guard])
|
|
print("| Night | Gathered | Built (total) | Upgrades | Crops: stolen / recovered / lost | Animals: grabbed / out / led back / lost | Supplies: taken / back / lost | Left | HP | Broken | Shed after |")
|
|
print("|---|---|---|---|---|---|---|---|---|---|")
|
|
var lost_by := {}
|
|
_sim.loot.loot_event.connect(func(kind: StringName, id: int, _p: Vector3) -> void:
|
|
if kind == &"lost" and _sim.loot.category[id] == LootSystem.Category.SUPPLY:
|
|
lost_by[_sim.loot.sack_material[id]] = int(lost_by.get(_sim.loot.sack_material[id], 0)) + _sim.loot.sack_amount[id])
|
|
var t0 := Time.get_ticks_msec()
|
|
for k in n:
|
|
if c.is_over():
|
|
break
|
|
var shed0: Dictionary = s.resources.shed.duplicate()
|
|
var built0 := _sim.structures.count()
|
|
var gathered := {"wood": 0, "scrap": 0, "straw": 0}
|
|
var cb := func(m: String, amount: int, _node: GatherNode) -> void: gathered[m] += amount
|
|
_sim.interactions.gathered.connect(cb)
|
|
c.begin_day()
|
|
var t := 0.0
|
|
while c.is_daytime():
|
|
var me := Vector2(_player.global_position.x, _player.global_position.z)
|
|
if not (t < GATHER_UNTIL and _gather_tick(me)):
|
|
Input.action_release("interact")
|
|
if _do_build:
|
|
_build_tick(me)
|
|
else:
|
|
_walk_field(Vector2(30.0, -3.0), me)
|
|
_sim.step(DT)
|
|
t += DT
|
|
Input.action_release("interact")
|
|
_sim.interactions.gathered.disconnect(cb)
|
|
# Night: guard. Record what should carry over.
|
|
var breaks: Array[int] = [] # a lambda can't add to a captured int
|
|
var cb2 := func(_i: int, _t: String, _at: Vector3) -> void: breaks.append(1)
|
|
_sim.structures.destroyed.connect(cb2)
|
|
var built_night := _sim.structures.count()
|
|
var res_night: Dictionary = s.resources.to_dict()
|
|
lost_by.clear()
|
|
var fears0 := _fears
|
|
if not _do_guard:
|
|
_player.global_position = Vector3(HOME.x, 0.0, HOME.y)
|
|
while s.phase != CampaignController.Phase.MORNING and not c.is_over():
|
|
if _do_guard:
|
|
_guard()
|
|
_sim.step(DT)
|
|
_sim.structures.destroyed.disconnect(cb2)
|
|
var broken := breaks.size()
|
|
Input.action_release("patch")
|
|
if c.is_over():
|
|
print("| %d | campaign over: %s |" % [k + 1, CampaignController.Outcome.keys()[c.outcome]])
|
|
break
|
|
# Carry-over checks: nothing is built at night, so structures only go down by breaks;
|
|
# wood and scrap only go down by what thieves carried off (straw also by patches).
|
|
if _sim.structures.count() != built_night - broken:
|
|
_carry_errors.append("cycle %d: %d structures at night - %d broken != %d in the morning" % [k + 1, built_night, broken, _sim.structures.count()])
|
|
var now := s.resources.to_dict()
|
|
for m in ["wood", "scrap"]:
|
|
var before := int(res_night["shed"][m]) + int(res_night["pouch"][m])
|
|
var after := int(now["shed"][m]) + int(now["pouch"][m])
|
|
if after != before - int(lost_by.get(m, 0)):
|
|
_carry_errors.append("cycle %d: %s %d -> %d overnight, %d carried off" % [k + 1, m, before, after, int(lost_by.get(m, 0))])
|
|
if s.supplies_in_transit != 0:
|
|
_carry_errors.append("cycle %d: %d supplies still in transit in the morning" % [k + 1, s.supplies_in_transit])
|
|
var e: Dictionary = s.night_log[s.night_log.size() - 1]
|
|
print("| %d %s | %s | %d (%d) | %d | %d / %d / %d | %d / %d / %d / %d | %d / %d / %d | %d crops, %d animals | %d/%d | %d | %s |" % [k + 1,
|
|
_sim.waves.plan_for(k + 1).title, ResourceStore.describe(gathered), built_night - built0, built_night, s.upgrades.size(),
|
|
e["crops_stolen"], e["crops_recovered"], e["crops_lost"],
|
|
e["animals_stolen"], e["animals_strayed"], e["animals_recovered"], e["animals_lost"], e["supplies_stolen"], e["supplies_recovered"],
|
|
e["supplies_lost"], s.crops, s.animals, s.scarecrow_hp, s.scarecrow_max_hp, broken, ResourceStore.describe(s.resources.shed)])
|
|
print("soak cycle: carry-over %s; %.1f s of wall time" % ["OK" if _carry_errors.is_empty() else "ERRORS %s" % [_carry_errors],
|
|
(Time.get_ticks_msec() - t0) / 1000.0])
|
|
|
|
|
|
## One gathering tick: walk to the nearest point with charges and gather, or store a full pouch.
|
|
## Returns false when nothing is left to gather or store.
|
|
func _gather_tick(me: Vector2) -> bool:
|
|
var it := _sim.interactions
|
|
var r := _sim.campaign.state.resources
|
|
var target: GatherNode = null
|
|
var best := INF
|
|
for n in it.nodes:
|
|
var d := Vector2(n.global_position.x, n.global_position.z).distance_to(me)
|
|
if it.charges(n) > 0 and d < best:
|
|
best = d
|
|
target = n
|
|
if target == null and r.pouch_total() == 0:
|
|
return false
|
|
if target == null or r.pouch_room() < target.yield_amount:
|
|
Input.action_release("interact")
|
|
var shed := _sim.zones.zone(FarmZone.Kind.SUPPLY_SHED)
|
|
_walk_field(Vector2(shed.global_position.x, shed.global_position.z + 3.0), me)
|
|
return true
|
|
if Vector2(target.global_position.x, target.global_position.z).distance_to(me) > target.reach - 0.1:
|
|
Input.action_release("interact")
|
|
_walk_field(_stand_spot(target), me)
|
|
else:
|
|
Input.action_press("interact")
|
|
return true
|
|
|
|
|
|
## One building tick: put a gate back in the pen opening if it's gone, else walk to the next
|
|
## BUILD_PLAN site it can afford and place it; otherwise wait in the middle of the crop field.
|
|
func _build_tick(me: Vector2) -> void:
|
|
var st := _sim.structures
|
|
var r := _sim.campaign.state.resources
|
|
if _sim.nav.structure_at[_sim.nav.cell_of(PEN_GATE.x, PEN_GATE.y)] < 0 and r.can_afford(_sim.config.costs["gate"]):
|
|
if PEN_GATE.distance_to(me) > 4.0:
|
|
_walk_field(PEN_GATE + Vector2(2.5, 0.0), me)
|
|
elif st.validate("gate", PEN_GATE.x, PEN_GATE.y, 2, me) == "":
|
|
st.place("gate", PEN_GATE.x, PEN_GATE.y, 2)
|
|
return
|
|
for i in BUILD_PLAN.size():
|
|
if _built.has(i):
|
|
continue
|
|
var part: Array = BUILD_PLAN[i]
|
|
if not r.can_afford(_sim.config.costs.get(part[0], {})):
|
|
continue
|
|
var at: Vector2 = part[1]
|
|
if at.distance_to(me) > 6.0:
|
|
_walk_field(at + Vector2(-2.0, 0.0), me)
|
|
return
|
|
var why := st.validate(part[0], at.x, at.y, part[2], me)
|
|
if why == "":
|
|
st.place(part[0], at.x, at.y, part[2])
|
|
else:
|
|
print("soak: can't build %s at %s: %s" % [part[0], at, why])
|
|
_built[i] = true # built, or impossible here: either way move on
|
|
return
|
|
if _do_upgrades and _upgrade_tick(me):
|
|
return
|
|
_walk_field(Vector2(30.0, -3.0), me)
|
|
|
|
|
|
## Walks to the workbench and buys the next upgrade it can afford. Returns false if there's none.
|
|
func _upgrade_tick(me: Vector2) -> bool:
|
|
var c := _sim.campaign
|
|
for id in UPGRADE_ORDER:
|
|
if Upgrades.blocker(c, id) != "":
|
|
continue
|
|
var bench := _sim.interactions.workbench
|
|
var at := Vector2(bench.global_position.x, bench.global_position.z)
|
|
if at.distance_to(me) > bench.reach - 0.3:
|
|
_walk_field(at, me)
|
|
else:
|
|
Upgrades.buy(c, id, _sim.structures)
|
|
return true
|
|
return false
|
|
|
|
|
|
func _gather_day() -> void:
|
|
var c := _sim.campaign
|
|
var it := _sim.interactions
|
|
var r := c.state.resources
|
|
var shed_start: Dictionary = r.shed.duplicate()
|
|
var shed := _sim.zones.zone(FarmZone.Kind.SUPPLY_SHED)
|
|
var drop_off := Vector2(shed.global_position.x, shed.global_position.z + 3.0)
|
|
c.begin_day()
|
|
var t := 0.0
|
|
var trips := 0
|
|
var walking := 0.0
|
|
var gathering := 0.0
|
|
var done_at := -1.0
|
|
while c.is_daytime():
|
|
var me := Vector2(_player.global_position.x, _player.global_position.z)
|
|
var target: GatherNode = null
|
|
var best := INF
|
|
for n in it.nodes:
|
|
var d := Vector2(n.global_position.x, n.global_position.z).distance_to(me)
|
|
if it.charges(n) > 0 and d < best:
|
|
best = d
|
|
target = n
|
|
if target == null and r.pouch_total() == 0:
|
|
done_at = t
|
|
break
|
|
var store := target == null or r.pouch_room() < target.yield_amount
|
|
if store:
|
|
Input.action_release("interact")
|
|
_walk_field(drop_off, me)
|
|
walking += DT
|
|
if r.pouch_total() == 0:
|
|
pass
|
|
else:
|
|
var at := Vector2(target.global_position.x, target.global_position.z)
|
|
if at.distance_to(me) > target.reach - 0.1:
|
|
Input.action_release("interact")
|
|
_walk_field(_stand_spot(target), me)
|
|
walking += DT
|
|
else:
|
|
Input.action_press("interact")
|
|
gathering += DT
|
|
var before := r.pouch_total()
|
|
_sim.step(DT)
|
|
if before > 0 and r.pouch_total() == 0:
|
|
trips += 1
|
|
t += DT
|
|
Input.action_release("interact")
|
|
var got := {}
|
|
for m in ResourceStore.MATERIALS:
|
|
got[m] = int(r.shed[m]) - int(shed_start[m])
|
|
print("gather: %s stored in %d trips; everything gathered and stored after %s (day %.0f s + dusk %.0f s)" % [
|
|
ResourceStore.describe(got), trips, ("%.0f s" % done_at) if done_at >= 0.0 else "the day ran out", _sim.config.day_length, _sim.config.dusk_length])
|
|
print("gather: %.0f s walking, %.0f s gathering" % [walking, gathering])
|
|
|
|
|
|
## The free spot closest to a gather point (where the bot stands to gather it).
|
|
func _stand_spot(n: GatherNode) -> Vector2:
|
|
var nav := _sim.nav
|
|
var c := Vector2(n.global_position.x, n.global_position.z)
|
|
var best := c
|
|
var best_d := INF
|
|
for cell in nav.free_cells_in(Rect2(c - Vector2.ONE * n.reach, Vector2.ONE * n.reach * 2.0)):
|
|
var d := nav.cell_center(cell).distance_to(c)
|
|
if d < best_d:
|
|
best_d = d
|
|
best = nav.cell_center(cell)
|
|
return best
|
|
|
|
|
|
## Walks along the navigation grid toward goal (one distance field per goal cell, cached).
|
|
func _walk_field(goal: Vector2, me: Vector2) -> void:
|
|
var nav := _sim.nav
|
|
var cell := nav.nearest_free(nav.cell_of(goal.x, goal.y))
|
|
if not _fields.has(cell):
|
|
_fields[cell] = nav.distance_field(PackedInt32Array([cell]))
|
|
var dir := PackedFloat32Array([0.0, 0.0])
|
|
if nav.flow_direction(_fields[cell], me.x, me.y, dir):
|
|
var next := me + Vector2(dir[0], dir[1]) * _player.run_speed * DT
|
|
_player.global_position = Vector3(next.x, 0.0, next.y)
|
|
|
|
|
|
func _guard() -> void:
|
|
var es := _sim.enemies
|
|
var loot := _sim.loot
|
|
var kit := _sim.scarecrow
|
|
var cfg := _sim.config
|
|
var me := Vector2(_player.global_position.x, _player.global_position.z)
|
|
# Most urgent threat: carriers, then thieves taking a crop, then dropped crops, then others.
|
|
var goal := HOME
|
|
var best := INF
|
|
var near := 0
|
|
var carrier_near := false
|
|
var nearest_thief := -1
|
|
var nearest_d := INF
|
|
for d in es.active_count:
|
|
if es.alive[d] == 0:
|
|
continue
|
|
var p := Vector2(es.pos_x[d], es.pos_z[d])
|
|
var dist := p.distance_to(me)
|
|
var urgency := dist
|
|
match es.state[d]:
|
|
ThiefBehavior.State.CARRY:
|
|
urgency -= 40.0
|
|
carrier_near = carrier_near or dist < cfg.fear_radius
|
|
ThiefBehavior.State.STEAL:
|
|
urgency -= 20.0
|
|
if urgency < best:
|
|
best = urgency
|
|
goal = p
|
|
if dist < cfg.fear_radius and es.state[d] != ThiefBehavior.State.FLEE:
|
|
near += 1
|
|
if dist < nearest_d and _sim.nav.line_clear(me.x, me.y, p.x, p.y):
|
|
nearest_d = dist
|
|
nearest_thief = d
|
|
for id in range(1, loot.item_count + 1):
|
|
if loot.status[id] == LootSystem.Status.DROPPED:
|
|
var at := loot.position_of(id)
|
|
var dist := at.distance_to(me) - 10.0
|
|
if loot.category[id] == LootSystem.Category.ANIMAL and loot.followers() >= cfg.max_following_animals:
|
|
continue
|
|
if dist < best:
|
|
best = dist
|
|
goal = at
|
|
# Lead followers home when no carrier is close.
|
|
if loot.followers() > 0 and best > -25.0:
|
|
goal = PEN_INSIDE
|
|
best = -25.0
|
|
var hurt := kit.hp() <= cfg.scarecrow_max_hp / 2
|
|
if hurt and nearest_d > 8.0:
|
|
Input.action_press("patch")
|
|
return
|
|
Input.action_release("patch")
|
|
if carrier_near or near >= 2:
|
|
if kit.use_fear() >= 0:
|
|
_fears += 1
|
|
if nearest_thief >= 0 and nearest_d < SHOOT_RANGE:
|
|
var target := Vector2(es.pos_x[nearest_thief], es.pos_z[nearest_thief])
|
|
var reach := _attack(target, me)
|
|
if nearest_d < reach:
|
|
return # hold ground and attack rather than walk into it
|
|
if _sim.combat.weapon == ScarecrowCombat.Weapon.PITCHFORK:
|
|
goal = target # close in to stab it
|
|
_walk_toward(goal, me)
|
|
|
|
|
|
## Attacks the thief at target with the best weapon owned: a pumpkin bomb into a crowd of three
|
|
## or more when one is ready, else the corn shooter, else the pitchfork (only within its reach).
|
|
## Returns the distance it holds ground at.
|
|
func _attack(target: Vector2, me: Vector2) -> float:
|
|
var c := _sim.combat
|
|
var cfg := _sim.config
|
|
var es := _sim.enemies
|
|
var crowd := 0
|
|
for d in es.active_count:
|
|
if es.alive[d] == 1 and Vector2(es.pos_x[d], es.pos_z[d]).distance_to(target) <= cfg.bomb_radius:
|
|
crowd += 1
|
|
var far := target.distance_to(me)
|
|
if crowd >= 3 and c.bomb_left == 0.0 and far >= cfg.bomb_min_range and far <= cfg.bomb_range and c.equip(ScarecrowCombat.Weapon.PUMPKIN_BOMB):
|
|
if c.attack_toward(target):
|
|
_shots += 1
|
|
if c.equip(ScarecrowCombat.Weapon.CORN_SHOOTER):
|
|
if c.attack_toward(target):
|
|
_shots += 1
|
|
return 3.0
|
|
c.equip(ScarecrowCombat.Weapon.PITCHFORK)
|
|
if far <= cfg.pitchfork_reach and c.attack_toward(target):
|
|
_shots += 1
|
|
return cfg.pitchfork_reach * 0.8
|
|
|
|
|
|
func _walk_toward(goal: Vector2, me: Vector2) -> void:
|
|
var inside := _sim.zones.playable.grow(-1.0) # the scarecrow can't leave the farm
|
|
goal = Vector2(clampf(goal.x, inside.position.x, inside.end.x), clampf(goal.y, inside.position.y, inside.end.y))
|
|
if not _sim.nav.line_clear(me.x, me.y, goal.x, goal.y, true) and goal.distance_to(me) > 2.0:
|
|
_walk_field((goal / 2.0).round() * 2.0, me) # around walls (gates and fences are no wall to it)
|
|
return
|
|
var to := goal - me
|
|
if to.length() < 0.5:
|
|
return
|
|
var step := to.normalized() * _player.run_speed * DT
|
|
var nav := _sim.nav
|
|
var next := me + step
|
|
if not nav.is_free(nav.cell_of(next.x, next.y)):
|
|
next = Vector2(me.x + step.x, me.y) if nav.is_free(nav.cell_of(me.x + step.x, me.y)) else Vector2(me.x, me.y + step.y)
|
|
if not nav.is_free(nav.cell_of(next.x, next.y)):
|
|
var side := Vector2(-step.y, step.x) # boxed in: sidestep
|
|
next = me + side
|
|
if not nav.is_free(nav.cell_of(next.x, next.y)):
|
|
return
|
|
_player.global_position = Vector3(next.x, 0.0, next.y)
|