- farm.tscn: LettuceField moves to (-1, 0.44, 25) on the SW soil tile (ground_008_004) and CornField to (29, 0.31, 25) on the SE tile (ground_011_003), both still 10 x 10 m. Their units keep ids 31-40 / 41-50, plots 7-10 and sprout nights 2 / 4. The sunflower children are replaced: lettuce rows get 8 cabbage heads each (plant_004/005), corn rows 10 stalks each (plant_001, scaled ~0.8). Every plant's height follows the soil under it (raycast), so none floats. - South Fields (outside zone) moves to (9.5, 41), 66 x 10, between the new south edge (z 35) and the old south fence; its four hay points move there too and rest on the soil. - FarmZones.playable: Rect2(-13, -45, 60, 80), i.e. z -45..35. - ThiefBehavior: one rect per crop field (field_rects) instead of their bounding box; area_for() takes the target's position. - LootSystem: carried/dropped crop tokens show the crop's own plant (CROP_TOKENS), each scene loaded once. - Tests: test_farm_layout checks the 60 x 80 map, outside zones off the map, the later fields on the soil tiles with their own plants standing on the soil, side names for both fields, the open east side south of the fence, and splits the travel bound (12 s core rule, 16 s for the south fields). test_theft: new south edge cell, crop tokens. run_tests EDGE/SKY south points follow the edge. test_targets: the dropped animal is watched from outside the gate (the carrier now dies ~4 m from the west edge). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
551 lines
19 KiB
GDScript
551 lines
19 KiB
GDScript
class_name ThiefBehavior
|
|
extends RefCounted
|
|
## What each enemy wants, decided every tick (EnemySystem turns it into motion). Four types
|
|
## (stats in GameConfig.enemy_types):
|
|
## thief on foot, after its type's targets (animals and supplies): the theft lifecycle of
|
|
## game/RULES.md below. Routes around barriers when it can, breaks through when not.
|
|
## crow flies, after crops only: straight at a claimed crop (fences, gates, and traps mean
|
|
## nothing to it), lands and takes it, then flies straight off the farm out its
|
|
## nearest side. It never fights.
|
|
## breaker the fence breaker, on foot; steals nothing. It walks (breaker_field) to the nearest
|
|
## barrier guarding the crops, the pen, or the shed and smashes it, then goes for the
|
|
## next. It turns on the scarecrow when it comes close or shoots it, and hunts the
|
|
## scarecrow when nothing is left to break.
|
|
## ufo flies, steals nothing, and goes straight to FIGHT the moment it arrives (fences,
|
|
## gates, and traps mean nothing to it, same as a crow). Strafes the scarecrow: dives
|
|
## toward it while its attack is off cooldown, then peels away and swings around for
|
|
## another pass once it lands a hit (altitude dips for the dive, climbs on the pull-up).
|
|
## States:
|
|
## SEEK claim something to take (LootSystem.claim, limited to the type's categories: dropped
|
|
## loot in view first, then its wanted category, EnemySystem.want, then the others),
|
|
## reserving it so it shows as targeted, and go there. On foot: along its category's
|
|
## flow field (the crop rows, the pen, the shed piles) from afar, straight at it once
|
|
## inside that area or close with a clear line. Flying: straight at it. A breaker
|
|
## follows the breaker field instead.
|
|
## STEAL stand at it for its steal time (the category's, times the type's steal_time; the
|
|
## progress ring fills); a hit restarts it. An animal that moved off is chased again; a
|
|
## sack finds the shed empty and seeks again.
|
|
## CARRY take it off the farm at the type's carry_speed: on foot along the escape field to the
|
|
## nearest point of the farm's edge (reaching the edge carries it off), flying straight
|
|
## out the nearest side. The loot is lost.
|
|
## RETREAT nothing left to take, or sunrise: leave the same way.
|
|
## FLEE scared (the scarecrow's fear): run straight away for fear_duration, giving up any
|
|
## claim; carriers drop their loot (D5) unless fear_drops_loot is off. Afterwards an
|
|
## enemy seeks again, or keeps carrying what it still holds.
|
|
## BREAK its route runs into a player-built barrier (FarmNavigation.barrier_ahead), or a
|
|
## breaker reached the one it came for: it hits the barrier (structure_damage every
|
|
## hit_interval) until it breaks, then goes back to what it was doing. Steering straight
|
|
## at loot needs a line clear of barriers too, so a fence in the way is broken or walked
|
|
## around, not ignored.
|
|
## FIGHT a breaker goes for the scarecrow, until it's more than FIGHT_LEASH away and there's
|
|
## still something to break.
|
|
## An enemy whose claim disappears (another one took it, the scarecrow recovered it, an animal
|
|
## followed the scarecrow away) claims another. Who hits the scarecrow (EnemySystem.
|
|
## evaluate_attacks) depends on the type's aggression: thieves when touching it while seeking,
|
|
## stealing, carrying, or breaking; breakers while fighting; crows never.
|
|
|
|
enum State { SEEK, STEAL, CARRY, RETREAT, FLEE, BREAK, FIGHT }
|
|
|
|
## Distance at which an enemy starts taking its loot.
|
|
const STEAL_REACH := 0.9
|
|
## Seconds a thief keeps walking toward dropped loot it can't see before choosing another.
|
|
const GIVE_UP_TIME := 10.0
|
|
## Flyers see dropped crops from above within this distance.
|
|
const AIR_VIEW := 20.0
|
|
## A flyer this far outside the farm has left it.
|
|
const AIR_EXIT_MARGIN := 2.0
|
|
const FIGHT_LEASH := 12.0
|
|
|
|
var loot: LootSystem
|
|
var nav: FarmNavigation
|
|
var crop_field: PackedInt32Array
|
|
var escape_field: PackedInt32Array
|
|
## Every crop field's rectangle (#20: one per field, not their bounding box, now that the fields
|
|
## lie far apart). A crop thief inside the field its target grows in goes straight at it.
|
|
var field_rects: Array[Rect2] = []
|
|
## Set by FarmSim: the flow fields toward the pen, each shed pile (material -> field), and the
|
|
## barriers guarding valuables; those areas; the farm (flyers leave over its edge); and, every
|
|
## tick, where the scarecrow is.
|
|
var animal_field := PackedInt32Array()
|
|
var supply_fields := {}
|
|
var breaker_field := PackedInt32Array()
|
|
## Toward the scarecrow (FarmSim keeps it fresh while anything fights).
|
|
var hunt_field := PackedInt32Array()
|
|
var pen_rect := Rect2()
|
|
var shed_rect := Rect2()
|
|
var farm_rect := Rect2()
|
|
var scarecrow := Vector2(INF, INF)
|
|
## Co-op: where player 2's scarecrow is (INF when there's none, or it's down). Enemies go for the
|
|
## nearer of the two.
|
|
var scarecrow2 := Vector2(INF, INF)
|
|
var cfg: GameConfig
|
|
## Set by FarmSim: the barriers enemies break.
|
|
var structures: StructureSystem
|
|
var _dir := PackedFloat32Array([0.0, 0.0])
|
|
|
|
|
|
func setup(p_cfg: GameConfig, p_nav: FarmNavigation, p_loot: LootSystem, p_crop_field: PackedInt32Array,
|
|
p_escape_field: PackedInt32Array, p_field_rects: Array[Rect2]) -> void:
|
|
cfg = p_cfg
|
|
nav = p_nav
|
|
loot = p_loot
|
|
crop_field = p_crop_field
|
|
escape_field = p_escape_field
|
|
field_rects = p_field_rects
|
|
|
|
|
|
func update(es: EnemySystem, dt: float) -> void:
|
|
for i in es.active_count:
|
|
if es.alive[i] == 0:
|
|
continue
|
|
var t := es.type[i]
|
|
match es.state[i]:
|
|
State.SEEK:
|
|
if t == EnemySystem.TYPE_CROW:
|
|
_crow_seek(es, i)
|
|
elif t == EnemySystem.TYPE_UFO:
|
|
_start_fight(es, i)
|
|
elif es.t_breaks[t] == 1:
|
|
_breaker_seek(es, i)
|
|
else:
|
|
_seek(es, i, dt)
|
|
State.STEAL:
|
|
_steal(es, i, dt)
|
|
State.CARRY:
|
|
if es.t_flying[t] == 1:
|
|
_fly_off(es, i, EnemySystem.Removal.ESCAPED)
|
|
else:
|
|
_carry(es, i)
|
|
State.FLEE:
|
|
_flee(es, i, dt)
|
|
State.BREAK:
|
|
_break(es, i)
|
|
State.FIGHT:
|
|
if t == EnemySystem.TYPE_UFO:
|
|
_ufo_fight(es, i)
|
|
else:
|
|
_fight(es, i)
|
|
_:
|
|
if es.t_flying[t] == 1:
|
|
_fly_off(es, i, EnemySystem.Removal.LEFT)
|
|
else:
|
|
_leave(es, i)
|
|
|
|
|
|
## A hit while stealing restarts the theft; a hit on a fighter makes it turn on the scarecrow.
|
|
func on_hit(es: EnemySystem, d: int) -> void:
|
|
if es.state[d] == State.STEAL:
|
|
es.timer[d] = 0.0
|
|
loot.set_progress(es.target[d], es.handle_of(d), 0.0)
|
|
if es.t_aggression[es.type[d]] == EnemySystem.Aggression.FIGHT and (es.state[d] == State.SEEK or es.state[d] == State.BREAK):
|
|
_start_fight(es, d)
|
|
|
|
|
|
func can_attack(es: EnemySystem, d: int) -> bool:
|
|
var st := es.state[d]
|
|
match es.t_aggression[es.type[d]]:
|
|
EnemySystem.Aggression.CONTACT:
|
|
return st == State.SEEK or st == State.STEAL or st == State.CARRY or st == State.BREAK
|
|
EnemySystem.Aggression.FIGHT:
|
|
return st == State.FIGHT
|
|
return false
|
|
|
|
|
|
## Whether fear makes it flee: anything busy on the farm (not already fleeing or leaving).
|
|
func can_be_scared(es: EnemySystem, d: int) -> bool:
|
|
var st := es.state[d]
|
|
return st == State.SEEK or st == State.STEAL or st == State.CARRY or st == State.BREAK or st == State.FIGHT
|
|
|
|
|
|
## Fear from center: every enemy busy on the farm within radius flees straight away. Returns how
|
|
## many were scared.
|
|
func scare(es: EnemySystem, center: Vector2, radius: float) -> int:
|
|
var n := 0
|
|
for i in es.active_count:
|
|
if es.alive[i] == 0 or not can_be_scared(es, i):
|
|
continue
|
|
var away := Vector2(es.pos_x[i], es.pos_z[i]) - center
|
|
var d := away.length()
|
|
if d > radius:
|
|
continue
|
|
var h := es.handle_of(i)
|
|
if es.carry[i] >= 0 and cfg.fear_drops_loot:
|
|
loot.drop(es.carry[i], h, es.pos_x[i], es.pos_z[i])
|
|
es.carry[i] = -1
|
|
loot.release(es.target[i], h)
|
|
es.target[i] = -1
|
|
es.barrier[i] = -1
|
|
var dir := away / d if d > 1e-3 else Vector2(es.head_x[i], es.head_z[i])
|
|
es.desire_x[i] = dir.x
|
|
es.desire_z[i] = dir.y
|
|
es.timer[i] = cfg.fear_duration
|
|
es.state[i] = State.FLEE
|
|
n += 1
|
|
return n
|
|
|
|
|
|
## Sunrise: every enemy gives up what it was after and leaves; carriers keep running.
|
|
func retreat_all(es: EnemySystem) -> void:
|
|
for i in es.active_count:
|
|
if es.alive[i] == 1:
|
|
loot.release(es.target[i], es.handle_of(i))
|
|
es.target[i] = -1
|
|
es.barrier[i] = -1
|
|
es.state[i] = State.CARRY if es.carry[i] >= 0 else State.RETREAT
|
|
|
|
|
|
## A flyer's visual altitude: cruising, coming down as it nears its crop, sitting on it while it
|
|
## takes it. A UFO dives low for its attack run and climbs again once it has landed a hit.
|
|
func altitude_for(es: EnemySystem, i: int) -> float:
|
|
match es.state[i]:
|
|
State.STEAL:
|
|
return cfg.land_altitude
|
|
State.SEEK:
|
|
var unit := es.target[i]
|
|
if unit > 0:
|
|
var d := loot.position_of(unit).distance_to(Vector2(es.pos_x[i], es.pos_z[i]))
|
|
return lerpf(cfg.land_altitude, cfg.cruise_altitude, clampf((d - 0.5) / 4.0, 0.0, 1.0))
|
|
State.FIGHT:
|
|
if es.type[i] == EnemySystem.TYPE_UFO:
|
|
return cfg.cruise_altitude if es.attack_cd[i] > 0.0 else cfg.land_altitude
|
|
return cfg.cruise_altitude
|
|
|
|
|
|
## Body contact range for an attack: the enemy's hit proxy, the scarecrow's, and the melee reach.
|
|
func _attack_reach() -> float:
|
|
return cfg.enemy_radius + FarmSim.SCARECROW_RADIUS + cfg.enemy_attack_reach
|
|
|
|
|
|
func _seek(es: EnemySystem, i: int, dt: float) -> void:
|
|
var h := es.handle_of(i)
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
var unit := es.target[i]
|
|
if unit < 0 or not loot.is_claimed_by(unit, h):
|
|
unit = loot.claim(h, p, nav, cfg.direct_seek_distance, es.want[i], es.t_targets[es.type[i]])
|
|
es.target[i] = unit
|
|
es.timer[i] = 0.0
|
|
if unit < 0:
|
|
es.state[i] = State.RETREAT
|
|
_leave(es, i)
|
|
return
|
|
var goal := loot.position_of(unit)
|
|
var to := goal - p
|
|
var d := to.length()
|
|
if d <= STEAL_REACH:
|
|
es.state[i] = State.STEAL
|
|
es.timer[i] = 0.0
|
|
_still(es, i, to / maxf(d, 1e-4))
|
|
return
|
|
es.pace[i] = 1.0
|
|
var field := field_for(unit)
|
|
if (area_for(loot.category[unit], goal).has_point(p) or d < cfg.direct_seek_distance) and nav.line_clear(p.x, p.y, goal.x, goal.y):
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
es.timer[i] = 0.0
|
|
return
|
|
if field.is_empty():
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
return
|
|
if loot.is_home(unit) and nav.flow_direction(field, p.x, p.y, _dir):
|
|
es.desire_x[i] = _dir[0]
|
|
es.desire_z[i] = _dir[1]
|
|
_check_barrier(es, i, field)
|
|
return
|
|
# Dropped loot out of sight: head for its home area meanwhile, and give up after a while.
|
|
es.timer[i] += dt
|
|
if es.timer[i] > GIVE_UP_TIME:
|
|
loot.release(unit, h)
|
|
es.target[i] = -1
|
|
if nav.flow_direction(field, p.x, p.y, _dir):
|
|
es.desire_x[i] = _dir[0]
|
|
es.desire_z[i] = _dir[1]
|
|
_check_barrier(es, i, field)
|
|
else:
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
|
|
|
|
## A crow: straight at its crop, whatever is in the way.
|
|
func _crow_seek(es: EnemySystem, i: int) -> void:
|
|
var h := es.handle_of(i)
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
var unit := es.target[i]
|
|
if unit < 0 or not loot.is_claimed_by(unit, h):
|
|
unit = loot.claim(h, p, nav, AIR_VIEW, es.want[i], es.t_targets[es.type[i]], false)
|
|
es.target[i] = unit
|
|
es.timer[i] = 0.0
|
|
if unit < 0:
|
|
es.state[i] = State.RETREAT
|
|
return
|
|
var to := loot.position_of(unit) - p
|
|
var d := to.length()
|
|
if d <= STEAL_REACH:
|
|
es.state[i] = State.STEAL
|
|
es.timer[i] = 0.0
|
|
_still(es, i, to / maxf(d, 1e-4))
|
|
return
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
es.pace[i] = 1.0
|
|
|
|
|
|
## A flyer heading straight off the farm out its nearest side (carrying its loot, or not); once
|
|
## it's past the edge it's gone.
|
|
func _fly_off(es: EnemySystem, i: int, reason: EnemySystem.Removal) -> void:
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
if not farm_rect.grow(AIR_EXIT_MARGIN).has_point(p):
|
|
if es.carry[i] >= 0:
|
|
loot.escape(es.carry[i], es.handle_of(i))
|
|
es.carry[i] = -1
|
|
es.queue_removal(i, reason)
|
|
return
|
|
var exit := _nearest_exit(p)
|
|
var to := exit - p
|
|
var d := maxf(to.length(), 1e-4)
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
es.pace[i] = es.t_carry[es.type[i]] if es.carry[i] >= 0 else 1.0
|
|
|
|
|
|
## Just past the nearest point of the farm's edge.
|
|
func _nearest_exit(p: Vector2) -> Vector2:
|
|
return FarmZones.nearest_on_outline(farm_rect.grow(AIR_EXIT_MARGIN + 1.0), p)
|
|
|
|
|
|
## A fence breaker: to the nearest barrier guarding something, and smash it.
|
|
func _breaker_seek(es: EnemySystem, i: int) -> void:
|
|
if _aggro(es, i):
|
|
_start_fight(es, i)
|
|
return
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
if breaker_field.is_empty() or breaker_field[nav.cell_of(p.x, p.y)] >= FarmNavigation.UNREACHABLE:
|
|
_start_fight(es, i) # nothing left to break
|
|
return
|
|
es.pace[i] = 1.0
|
|
if nav.flow_direction(breaker_field, p.x, p.y, _dir):
|
|
es.desire_x[i] = _dir[0]
|
|
es.desire_z[i] = _dir[1]
|
|
_check_barrier(es, i, breaker_field)
|
|
return
|
|
var id := _adjacent_structure(p)
|
|
if id >= 0:
|
|
es.barrier[i] = id
|
|
es.resume[i] = State.SEEK
|
|
es.state[i] = State.BREAK
|
|
var e := structures.campaign.state.structure(id)
|
|
var at := Vector2(float(e["x"]), float(e["z"])) - p
|
|
_still(es, i, at.normalized() if at.length_squared() > 1e-4 else Vector2(es.head_x[i], es.head_z[i]))
|
|
else:
|
|
_still(es, i, Vector2(es.head_x[i], es.head_z[i])) # the field is catching up after a breach
|
|
|
|
|
|
## The structure (in the navigation's structure layer: every barrier) nearest to p within 1.5 m,
|
|
## or -1.
|
|
func _adjacent_structure(p: Vector2) -> int:
|
|
var best := -1
|
|
var best_d := INF
|
|
var r := 3
|
|
var c := nav.cell_of(p.x, p.y)
|
|
for dz in range(-r, r + 1):
|
|
for dx in range(-r, r + 1):
|
|
var cell := c + dz * nav.width + dx
|
|
if cell < 0 or cell >= nav.cell_count() or nav.structure_at[cell] < 0:
|
|
continue
|
|
var d := nav.cell_center(cell).distance_squared_to(p)
|
|
if d < best_d:
|
|
best_d = d
|
|
best = nav.structure_at[cell]
|
|
return best
|
|
|
|
|
|
## The nearer scarecrow standing (INF if none).
|
|
func nearest_scarecrow(p: Vector2) -> Vector2:
|
|
if not scarecrow2.is_finite():
|
|
return scarecrow
|
|
if not scarecrow.is_finite():
|
|
return scarecrow2
|
|
return scarecrow if p.distance_squared_to(scarecrow) <= p.distance_squared_to(scarecrow2) else scarecrow2
|
|
|
|
|
|
func _aggro(es: EnemySystem, i: int) -> bool:
|
|
var t := es.type[i]
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
var sc := nearest_scarecrow(p)
|
|
if es.t_aggression[t] != EnemySystem.Aggression.FIGHT or not sc.is_finite():
|
|
return false
|
|
return p.distance_to(sc) <= es.t_aggro_range[t] and nav.line_clear(p.x, p.y, sc.x, sc.y)
|
|
|
|
|
|
func _start_fight(es: EnemySystem, i: int) -> void:
|
|
es.barrier[i] = -1
|
|
es.state[i] = State.FIGHT
|
|
|
|
|
|
func _fight(es: EnemySystem, i: int) -> void:
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
var sc := nearest_scarecrow(p)
|
|
if not sc.is_finite():
|
|
es.state[i] = State.SEEK
|
|
return
|
|
var to := sc - p
|
|
var d := to.length()
|
|
var has_work := not breaker_field.is_empty() and breaker_field[nav.cell_of(p.x, p.y)] < FarmNavigation.UNREACHABLE
|
|
if d > FIGHT_LEASH and has_work:
|
|
es.state[i] = State.SEEK
|
|
return
|
|
var contact := cfg.enemy_radius + FarmSim.SCARECROW_RADIUS + cfg.enemy_attack_reach * 0.5
|
|
if d <= contact:
|
|
_still(es, i, to / maxf(d, 1e-4))
|
|
return
|
|
es.pace[i] = 1.0
|
|
if not nav.line_clear(p.x, p.y, sc.x, sc.y) and not hunt_field.is_empty() \
|
|
and nav.flow_direction(hunt_field, p.x, p.y, _dir):
|
|
es.desire_x[i] = _dir[0]
|
|
es.desire_z[i] = _dir[1]
|
|
_check_barrier(es, i, hunt_field) # smash through to it
|
|
return
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
|
|
|
|
## A UFO's strafing run: straight at the scarecrow while its attack is off cooldown (diving in),
|
|
## straight away from it right after landing a hit (pulling up and swinging around for the next
|
|
## pass). No scarecrow left standing: leave like anything else at the end of a fight.
|
|
func _ufo_fight(es: EnemySystem, i: int) -> void:
|
|
var p := Vector2(es.pos_x[i], es.pos_z[i])
|
|
var sc := nearest_scarecrow(p)
|
|
if not sc.is_finite():
|
|
es.state[i] = State.RETREAT
|
|
return
|
|
var to := sc - p
|
|
var d := maxf(to.length(), 1e-4)
|
|
es.pace[i] = 1.0
|
|
if es.attack_cd[i] > 0.0 and d <= _attack_reach() * 2.0:
|
|
es.desire_x[i] = -to.x / d
|
|
es.desire_z[i] = -to.y / d
|
|
else:
|
|
es.desire_x[i] = to.x / d
|
|
es.desire_z[i] = to.y / d
|
|
|
|
|
|
## The flow field toward an item's home: the crop rows, the pen, or its sack's pile at the shed.
|
|
func field_for(id: int) -> PackedInt32Array:
|
|
match loot.category[id]:
|
|
LootSystem.Category.ANIMAL:
|
|
return animal_field if animal_field.size() > 0 else crop_field
|
|
LootSystem.Category.SUPPLY:
|
|
return supply_fields.get(loot.sack_material[id], crop_field)
|
|
return crop_field
|
|
|
|
|
|
## The area around an item's home where a thief stops following the flow field and goes straight
|
|
## (given a clear line): the pen, the shed, or the crop field holding goal (none for a crop lying
|
|
## outside every field).
|
|
func area_for(c: int, goal: Vector2) -> Rect2:
|
|
match c:
|
|
LootSystem.Category.ANIMAL:
|
|
return pen_rect
|
|
LootSystem.Category.SUPPLY:
|
|
return shed_rect
|
|
for r in field_rects:
|
|
if r.has_point(goal):
|
|
return r
|
|
return Rect2()
|
|
|
|
|
|
func _steal(es: EnemySystem, i: int, dt: float) -> void:
|
|
var h := es.handle_of(i)
|
|
var unit := es.target[i]
|
|
if unit < 0 or not loot.is_claimed_by(unit, h):
|
|
es.state[i] = State.SEEK
|
|
return
|
|
if loot.category[unit] == LootSystem.Category.ANIMAL \
|
|
and loot.position_of(unit).distance_to(Vector2(es.pos_x[i], es.pos_z[i])) > STEAL_REACH + 0.4:
|
|
es.state[i] = State.SEEK # it got away (a loose animal): chase it
|
|
loot.set_progress(unit, h, 0.0)
|
|
return
|
|
es.pace[i] = 0.0
|
|
es.timer[i] += dt
|
|
var need := loot.steal_time(unit) * es.t_steal[es.type[i]]
|
|
loot.set_progress(unit, h, es.timer[i] / need)
|
|
if es.timer[i] >= need:
|
|
if loot.pick_up(unit, h):
|
|
es.carry[i] = unit
|
|
es.state[i] = State.CARRY
|
|
else:
|
|
es.state[i] = State.SEEK # nothing left to take there
|
|
es.target[i] = -1
|
|
es.timer[i] = 0.0
|
|
|
|
|
|
func _flee(es: EnemySystem, i: int, dt: float) -> void:
|
|
es.pace[i] = cfg.flee_speed_factor
|
|
es.timer[i] -= dt
|
|
if es.timer[i] <= 0.0:
|
|
es.timer[i] = 0.0
|
|
es.state[i] = State.CARRY if es.carry[i] >= 0 else State.SEEK
|
|
|
|
|
|
func _break(es: EnemySystem, i: int) -> void:
|
|
var id := es.barrier[i]
|
|
if structures == null or structures.campaign.state.structure(id).is_empty():
|
|
es.barrier[i] = -1
|
|
es.state[i] = es.resume[i]
|
|
return
|
|
if _aggro(es, i):
|
|
_start_fight(es, i)
|
|
return
|
|
es.pace[i] = 0.0
|
|
if es.attack_cd[i] == 0.0:
|
|
var t := es.type[i]
|
|
es.attack_cd[i] = es.t_hit_interval[t]
|
|
structures.damage(id, maxi(es.t_structure_damage[t], 1))
|
|
|
|
|
|
## Following a field into a barrier: stop and break it.
|
|
func _check_barrier(es: EnemySystem, i: int, field: PackedInt32Array) -> void:
|
|
var id := nav.barrier_ahead(field, es.pos_x[i], es.pos_z[i])
|
|
if id < 0:
|
|
return
|
|
es.barrier[i] = id
|
|
es.resume[i] = es.state[i]
|
|
es.state[i] = State.BREAK
|
|
es.pace[i] = 0.0
|
|
|
|
|
|
func _carry(es: EnemySystem, i: int) -> void:
|
|
if _at_exit(es, i):
|
|
loot.escape(es.carry[i], es.handle_of(i))
|
|
es.carry[i] = -1
|
|
es.queue_removal(i, EnemySystem.Removal.ESCAPED)
|
|
return
|
|
_follow(es, i, escape_field, es.t_carry[es.type[i]])
|
|
_check_barrier(es, i, escape_field)
|
|
|
|
|
|
func _leave(es: EnemySystem, i: int) -> void:
|
|
if _at_exit(es, i):
|
|
es.queue_removal(i, EnemySystem.Removal.LEFT)
|
|
return
|
|
_follow(es, i, escape_field, 1.0)
|
|
_check_barrier(es, i, escape_field)
|
|
|
|
|
|
func _at_exit(es: EnemySystem, i: int) -> bool:
|
|
return escape_field[nav.cell_of(es.pos_x[i], es.pos_z[i])] == 0
|
|
|
|
|
|
func _follow(es: EnemySystem, i: int, field: PackedInt32Array, p_pace: float) -> void:
|
|
if nav.flow_direction(field, es.pos_x[i], es.pos_z[i], _dir):
|
|
es.desire_x[i] = _dir[0]
|
|
es.desire_z[i] = _dir[1]
|
|
es.pace[i] = p_pace
|
|
else:
|
|
es.pace[i] = 0.0
|
|
|
|
|
|
func _still(es: EnemySystem, i: int, facing: Vector2) -> void:
|
|
es.desire_x[i] = facing.x
|
|
es.desire_z[i] = facing.y
|
|
es.pace[i] = 0.0
|