H9: Boathook swings damage players; unarmed bots brawl
- Harvest.swing traces the swing as a shot is traced (Weapons.trace, lag compensated). A player reached before terrain, a solid, a prop's grown box or a built piece takes the boathook's damage through Weapons.damage_player, so armor, the kill feed and stats count it. A level swing meets the head and takes the headshot multiplier, as a shot does. - Unarmed bots go for an opponent in view within BOT_BRAWL_METRES (6 m) and swing within BOT_SWING_METRES; inside a hut they swing back without walking in. - Tests: harvest +5 (hit, armor first, out of reach, wall between, player in front of a tree), bot brain +2 (closes in and lands hits; leaves a distant opponent alone). - test_world_replication: an unarmed bot now meets the scripted player early on seed 2, so the test gives it full armor on landing so it lives to build; all assertions unchanged. Closes #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+21
-1
@@ -9,7 +9,8 @@
|
|||||||
## it reacts, how far it sees and how steady its aim is.
|
## it reacts, how far it sees and how steady its aim is.
|
||||||
##
|
##
|
||||||
## Each tick, in order of urgency: get to ground the coming tide will leave dry; fight
|
## Each tick, in order of urgency: get to ground the coming tide will leave dry; fight
|
||||||
## a visible opponent if armed; use a bandage or armor when nobody is in view; pick up
|
## a visible opponent if armed, or go for one within Tuning.BOT_BRAWL_METRES with the
|
||||||
|
## boathook if not; use a bandage or armor when nobody is in view; pick up
|
||||||
## useful items and open chests; walk to the next loot marker; otherwise wander the safe
|
## useful items and open chests; walk to the next loot marker; otherwise wander the safe
|
||||||
## ground, which is where everyone ends up.
|
## ground, which is where everyone ends up.
|
||||||
class_name BotBrain
|
class_name BotBrain
|
||||||
@@ -116,6 +117,9 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
|
|||||||
var selection: int = _weapon_slot + 1 if armed else Simulation.BOATHOOK_SLOT
|
var selection: int = _weapon_slot + 1 if armed else Simulation.BOATHOOK_SLOT
|
||||||
var buttons: int = 0
|
var buttons: int = 0
|
||||||
var fighting: bool = target != null and target_visible and armed and not me.swimming
|
var fighting: bool = target != null and target_visible and armed and not me.swimming
|
||||||
|
# Inside a hut or loft too: someone beating on you there gets a swing back.
|
||||||
|
var brawling: bool = (target != null and target_visible and not armed and not me.swimming
|
||||||
|
and not _fleeing and distance <= Tuning.BOT_BRAWL_METRES)
|
||||||
var interact: bool = false
|
var interact: bool = false
|
||||||
var walk: bool = true
|
var walk: bool = true
|
||||||
|
|
||||||
@@ -134,6 +138,9 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
|
|||||||
elif fighting:
|
elif fighting:
|
||||||
state = "fight"
|
state = "fight"
|
||||||
_stop()
|
_stop()
|
||||||
|
elif brawling:
|
||||||
|
state = "brawl"
|
||||||
|
_stop()
|
||||||
elif target != null and armed:
|
elif target != null and armed:
|
||||||
state = "chase"
|
state = "chase"
|
||||||
_go(_last_seen, 3.0)
|
_go(_last_seen, 3.0)
|
||||||
@@ -191,6 +198,19 @@ func think(sim: Simulation, grid: NavGrid, world: BotWorld) -> PlayerCommand:
|
|||||||
buttons |= PlayerCommand.BUTTON_FIRE
|
buttons |= PlayerCommand.BUTTON_FIRE
|
||||||
if distance > 10.0:
|
if distance > 10.0:
|
||||||
buttons |= PlayerCommand.BUTTON_AIM
|
buttons |= PlayerCommand.BUTTON_AIM
|
||||||
|
elif brawling:
|
||||||
|
# No gun: walk in and swing the boathook.
|
||||||
|
var aim: Vector2 = _aim_at(me, target, ItemCatalog.boathook())
|
||||||
|
yaw = aim.x
|
||||||
|
pitch = aim.y
|
||||||
|
var to: Vector2 = Vector2(target.position.x - me.position.x, target.position.z - me.position.z)
|
||||||
|
var gap: float = to.length()
|
||||||
|
if _stage == OUTSIDE:
|
||||||
|
direction = to / gap if gap > Tuning.PLAYER_RADIUS * 2.0 + 0.6 else Vector2.ZERO
|
||||||
|
buttons &= ~PlayerCommand.BUTTON_SPRINT
|
||||||
|
var reach: float = Harvest.eye(me).distance_to(target.position + Vector3(0.0, Movement.body_height(target) * 0.62, 0.0))
|
||||||
|
if reach <= Tuning.BOT_SWING_METRES and sim.tick - _acquired_tick >= _reaction_ticks:
|
||||||
|
buttons |= PlayerCommand.BUTTON_FIRE
|
||||||
elif armed and state != "heal" and me.reload_ticks == 0:
|
elif armed and state != "heal" and me.reload_ticks == 0:
|
||||||
# Nobody in view: top up the magazine.
|
# Nobody in view: top up the magazine.
|
||||||
var def: ItemDef = ItemCatalog.get_def(me.slot_items[_weapon_slot])
|
var def: ItemDef = ItemCatalog.get_def(me.slot_items[_weapon_slot])
|
||||||
|
|||||||
+28
-3
@@ -7,6 +7,11 @@
|
|||||||
## grown by Tuning.HARVEST_AIM_SLACK, the ray enters, unless terrain or another solid is
|
## grown by Tuning.HARVEST_AIM_SLACK, the ray enters, unless terrain or another solid is
|
||||||
## in the way first. Each hit does Tuning.HARVEST_DAMAGE to the prop and gives the
|
## in the way first. Each hit does Tuning.HARVEST_DAMAGE to the prop and gives the
|
||||||
## player Tuning.HARVEST_PER_HIT of the prop's material, up to Tuning.MATERIAL_CAP.
|
## player Tuning.HARVEST_PER_HIT of the prop's material, up to Tuning.MATERIAL_CAP.
|
||||||
|
##
|
||||||
|
## A swing also fights. The same ray is traced as a shot is (Weapons.trace, with lag
|
||||||
|
## compensation), and another player it reaches before terrain, a solid, a prop's grown
|
||||||
|
## box or a built piece takes the boathook's `damage` through Weapons.damage_player, so
|
||||||
|
## armor, the kill feed and stats count it like any other hit.
|
||||||
class_name Harvest
|
class_name Harvest
|
||||||
extends RefCounted
|
extends RefCounted
|
||||||
|
|
||||||
@@ -95,16 +100,19 @@ static func _prop_hit(player: SimEntity, ground: SimGround, props: PropSet) -> V
|
|||||||
|
|
||||||
## Swings if the player can and isn't cooling down. Returns the prop hit, or -1.
|
## Swings if the player can and isn't cooling down. Returns the prop hit, or -1.
|
||||||
## Appends "harvest" and "prop_destroyed" events to `events`. A built piece nearer than
|
## Appends "harvest" and "prop_destroyed" events to `events`. A built piece nearer than
|
||||||
## any prop takes the hit instead, losing Tuning.HARVEST_DAMAGE and giving nothing.
|
## any prop takes the hit instead, losing Tuning.HARVEST_DAMAGE and giving nothing. With
|
||||||
static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary], structures: StructureSet = null) -> int:
|
## `sim`, a player nearer than both takes the hit instead (see the class notes).
|
||||||
|
static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary], structures: StructureSet = null, sim: Simulation = null) -> int:
|
||||||
if player.action_cooldown > 0 or not can_swing(player):
|
if player.action_cooldown > 0 or not can_swing(player):
|
||||||
return -1
|
return -1
|
||||||
player.action_cooldown = swing_interval_ticks()
|
player.action_cooldown = swing_interval_ticks()
|
||||||
# Every swing, hit or miss, for whoever draws or hears it.
|
# Every swing, hit or miss, for whoever draws or hears it.
|
||||||
events.append({"type": "swing", "player": player.id, "from": eye(player), "direction": look_direction(player)})
|
events.append({"type": "swing", "player": player.id, "from": eye(player), "direction": look_direction(player)})
|
||||||
var hit: Vector2 = _prop_hit(player, ground, props)
|
var hit: Vector2 = _prop_hit(player, ground, props)
|
||||||
|
var piece_hit: Vector2 = _piece_hit(player, ground, structures) if structures != null else Vector2(1.0, 0.0)
|
||||||
|
if sim != null and _hit_player(sim, player, minf(hit.x, piece_hit.x)):
|
||||||
|
return -1
|
||||||
if structures != null:
|
if structures != null:
|
||||||
var piece_hit: Vector2 = _piece_hit(player, ground, structures)
|
|
||||||
if piece_hit.y != 0.0 and piece_hit.x <= hit.x:
|
if piece_hit.y != 0.0 and piece_hit.x <= hit.x:
|
||||||
events.append({"type": "piece_hit", "player": player.id, "piece": int(piece_hit.y), "amount": Tuning.HARVEST_DAMAGE})
|
events.append({"type": "piece_hit", "player": player.id, "piece": int(piece_hit.y), "amount": Tuning.HARVEST_DAMAGE})
|
||||||
structures.damage(int(piece_hit.y), Tuning.HARVEST_DAMAGE, "boathook", ground, events)
|
structures.damage(int(piece_hit.y), Tuning.HARVEST_DAMAGE, "boathook", ground, events)
|
||||||
@@ -118,3 +126,20 @@ static func swing(player: SimEntity, ground: SimGround, props: PropSet, events:
|
|||||||
if props.damage(prop, Tuning.HARVEST_DAMAGE):
|
if props.damage(prop, Tuning.HARVEST_DAMAGE):
|
||||||
events.append({"type": "prop_destroyed", "prop": prop})
|
events.append({"type": "prop_destroyed", "prop": prop})
|
||||||
return prop
|
return prop
|
||||||
|
|
||||||
|
|
||||||
|
## Traces the swing for players. If one is hit nearer than `nearest` (the fraction of
|
||||||
|
## the swing where a prop or piece would be hit), damages them and returns true.
|
||||||
|
static func _hit_player(sim: Simulation, player: SimEntity, nearest: float) -> bool:
|
||||||
|
var from: Vector3 = eye(player)
|
||||||
|
var to: Vector3 = from + look_direction(player) * reach()
|
||||||
|
var hit: Weapons.Hit = Weapons.trace(sim, from, to, player.id, Weapons.view_time(player, sim.tick))
|
||||||
|
if hit.kind != Weapons.HIT_PLAYER or hit.fraction > nearest:
|
||||||
|
return false
|
||||||
|
var def: ItemDef = ItemCatalog.boathook()
|
||||||
|
var amount: int = def.damage
|
||||||
|
if hit.headshot:
|
||||||
|
amount = roundi(amount * def.headshot_multiplier)
|
||||||
|
sim.events.append({"type": "impact", "player": player.id, "item": def.code, "point": hit.point, "hit": hit.kind, "target": hit.target})
|
||||||
|
Weapons.damage_player(sim, sim.entities.find(hit.target), amount, player.id, hit.headshot, def.code)
|
||||||
|
return true
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
|
|||||||
Building.act(self, player, command)
|
Building.act(self, player, command)
|
||||||
return
|
return
|
||||||
if player.selected == BOATHOOK_SLOT:
|
if player.selected == BOATHOOK_SLOT:
|
||||||
Harvest.swing(player, ground, props, events, structures)
|
Harvest.swing(player, ground, props, events, structures, self)
|
||||||
else:
|
else:
|
||||||
var def: ItemDef = Inventory.held_def(player)
|
var def: ItemDef = Inventory.held_def(player)
|
||||||
if def != null and def.kind == ItemDef.CONSUMABLE:
|
if def != null and def.kind == ItemDef.CONSUMABLE:
|
||||||
|
|||||||
@@ -290,6 +290,10 @@ const BOT_AIM_WANDER_TICKS: int = 12
|
|||||||
const BOT_BLOOM_PAUSE_DEGREES: float = 3.0
|
const BOT_BLOOM_PAUSE_DEGREES: float = 3.0
|
||||||
## Bots notice items lying within this distance.
|
## Bots notice items lying within this distance.
|
||||||
const BOT_LOOT_SIGHT_METRES: float = 12.0
|
const BOT_LOOT_SIGHT_METRES: float = 12.0
|
||||||
|
## A bot with no gun goes for an opponent in view this close with its boathook, in
|
||||||
|
## metres, and swings once its eye is this far from the opponent's body.
|
||||||
|
const BOT_BRAWL_METRES: float = 6.0
|
||||||
|
const BOT_SWING_METRES: float = 2.3
|
||||||
## The most weapons a bot carries; its other slots are for consumables.
|
## The most weapons a bot carries; its other slots are for consumables.
|
||||||
const BOT_MAX_WEAPONS: int = 3
|
const BOT_MAX_WEAPONS: int = 3
|
||||||
## Ground counts as safe from the next tide level when it stands this far above it.
|
## Ground counts as safe from the next tide level when it stands this far above it.
|
||||||
|
|||||||
@@ -322,3 +322,34 @@ func test_a_bot_only_match_ends_with_a_winner_and_replays_exactly() -> void:
|
|||||||
var again: Dictionary = BotBatch.run_match(20261112, 10)
|
var again: Dictionary = BotBatch.run_match(20261112, 10)
|
||||||
assert_eq(again["state_hash"], result["state_hash"], "the same seed plays the same match")
|
assert_eq(again["state_hash"], result["state_hash"], "the same seed plays the same match")
|
||||||
assert_eq(again["ticks"], result["ticks"])
|
assert_eq(again["ticks"], result["ticks"])
|
||||||
|
|
||||||
|
|
||||||
|
func test_an_unarmed_bot_closes_in_and_swings_its_boathook() -> void:
|
||||||
|
_setup()
|
||||||
|
var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X)
|
||||||
|
var victim: SimEntity = _player(HOME + Vector3(4.5, 0.0, 0.0))
|
||||||
|
var start: int = victim.health
|
||||||
|
_ready_world()
|
||||||
|
var swings: Array[int] = [0]
|
||||||
|
for _tick: int in 6 * TICKS:
|
||||||
|
_run(1)
|
||||||
|
for event: Dictionary in sim.events:
|
||||||
|
if event["type"] == "player_hit" and event["victim"] == victim.id and event["player"] == brain.player_id:
|
||||||
|
swings[0] += 1
|
||||||
|
if swings[0] >= 2:
|
||||||
|
break
|
||||||
|
assert_eq(brain.state, "brawl")
|
||||||
|
assert_true(swings[0] >= 2, "landed boathook hits (%d)" % swings[0])
|
||||||
|
assert_true(victim.health < start)
|
||||||
|
var me: SimEntity = sim.entities.find(brain.player_id)
|
||||||
|
assert_true(Vector2(me.position.x - victim.position.x, me.position.z - victim.position.z).length() < 3.0, "closed in")
|
||||||
|
|
||||||
|
|
||||||
|
func test_an_unarmed_bot_leaves_a_distant_opponent_alone() -> void:
|
||||||
|
_setup()
|
||||||
|
var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X)
|
||||||
|
var victim: SimEntity = _player(HOME + Vector3(Tuning.BOT_BRAWL_METRES + 8.0, 0.0, 0.0))
|
||||||
|
_ready_world()
|
||||||
|
_run(3 * TICKS)
|
||||||
|
assert_ne(brain.state, "brawl")
|
||||||
|
assert_eq(victim.health, Tuning.PLAYER_MAX_HEALTH)
|
||||||
|
|||||||
@@ -104,10 +104,18 @@ func test_a_clients_world_agrees_with_the_servers_through_a_scripted_match() ->
|
|||||||
var carried_checks: int = 0
|
var carried_checks: int = 0
|
||||||
var most_pending: int = 0
|
var most_pending: int = 0
|
||||||
var most_pieces: int = 0
|
var most_pieces: int = 0
|
||||||
|
var scripted: SimEntity = sim.entities.find(loop.scripted[0].player_id)
|
||||||
|
var shielded: bool = false
|
||||||
while flow.phase != Match.RESULTS and loop.ticks < LIMIT_TICKS and differed == 0:
|
while flow.phase != Match.RESULTS and loop.ticks < LIMIT_TICKS and differed == 0:
|
||||||
_round()
|
_round()
|
||||||
most_pending = maxi(most_pending, client.replica.pending_count())
|
most_pending = maxi(most_pending, client.replica.pending_count())
|
||||||
most_pieces = maxi(most_pieces, sim.structures.count())
|
most_pieces = maxi(most_pieces, sim.structures.count())
|
||||||
|
if not shielded and scripted.drop_state == Drop.NONE and scripted.on_ground:
|
||||||
|
# Since H9 unarmed bots brawl, and on this seed one meets the scripted player
|
||||||
|
# early. Full armor keeps it alive to harvest and build, which this test needs
|
||||||
|
# to replicate; the fight itself isn't what's under test.
|
||||||
|
shielded = true
|
||||||
|
scripted.armor = Tuning.PLAYER_MAX_ARMOR
|
||||||
if not landed and mine.drop_state == Drop.NONE and mine.on_ground and not mine.eliminated:
|
if not landed and mine.drop_state == Drop.NONE and mine.on_ground and not mine.eliminated:
|
||||||
# On the ground: hand it driftwood for two walls and a floor, and let it
|
# On the ground: hand it driftwood for two walls and a floor, and let it
|
||||||
# try the walls.
|
# try the walls.
|
||||||
|
|||||||
@@ -226,3 +226,96 @@ func test_every_swing_reports_itself_hit_or_miss() -> void:
|
|||||||
Harvest.swing(player, sim.ground, sim.props, events, sim.structures)
|
Harvest.swing(player, sim.ground, sim.props, events, sim.structures)
|
||||||
assert_eq(events.size(), 1, "not again until the boathook is ready")
|
assert_eq(events.size(), 1, "not again until the boathook is ready")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# H9: swings hit players.
|
||||||
|
|
||||||
|
## A sim with an attacker at (100, 0, 100) facing +z and a victim `gap` metres from
|
||||||
|
## the attacker's body along +z.
|
||||||
|
func _duel(gap: float) -> Simulation:
|
||||||
|
var sim: Simulation = _world([])
|
||||||
|
var attacker: SimEntity = sim.entities.find(1)
|
||||||
|
attacker.drop_state = Drop.NONE
|
||||||
|
attacker.on_ground = true
|
||||||
|
var victim: SimEntity = sim.entities.find(sim.add_player())
|
||||||
|
victim.drop_state = Drop.NONE
|
||||||
|
victim.on_ground = true
|
||||||
|
victim.position = attacker.position + Vector3(0.0, 0.0, Tuning.PLAYER_RADIUS * 2.0 + gap)
|
||||||
|
return sim
|
||||||
|
|
||||||
|
|
||||||
|
## Health and armor a boathook hit takes, headshot or not.
|
||||||
|
func _boathook(headshot: bool) -> int:
|
||||||
|
var def: ItemDef = ItemCatalog.boathook()
|
||||||
|
return roundi(def.damage * (def.headshot_multiplier if headshot else 1.0))
|
||||||
|
|
||||||
|
|
||||||
|
func _hits_on(sim: Simulation, victim: int) -> Array[Dictionary]:
|
||||||
|
var hits: Array[Dictionary] = []
|
||||||
|
for event: Dictionary in sim.events:
|
||||||
|
if event["type"] == "player_hit" and event["victim"] == victim:
|
||||||
|
hits.append(event)
|
||||||
|
return hits
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_swing_damages_a_player_in_reach() -> void:
|
||||||
|
var sim: Simulation = _duel(1.0)
|
||||||
|
var victim: SimEntity = sim.entities.find(2)
|
||||||
|
var start: int = victim.health
|
||||||
|
_swing_for(sim, 1)
|
||||||
|
var hits: Array[Dictionary] = _hits_on(sim, 2)
|
||||||
|
assert_eq(hits.size(), 1, "one hit")
|
||||||
|
assert_true(hits[0]["headshot"], "a level swing from the eye meets the head")
|
||||||
|
assert_eq(victim.health, start - _boathook(true))
|
||||||
|
assert_eq(hits[0]["player"], 1, "the kill feed knows who")
|
||||||
|
assert_eq(hits[0]["item"], ItemCatalog.boathook().code, "and with what")
|
||||||
|
assert_eq(victim.last_attacker, 1)
|
||||||
|
assert_eq(victim.last_cause, SimEntity.CAUSE_WEAPON)
|
||||||
|
|
||||||
|
|
||||||
|
func test_armor_takes_a_swing_first() -> void:
|
||||||
|
var sim: Simulation = _duel(1.0)
|
||||||
|
var victim: SimEntity = sim.entities.find(2)
|
||||||
|
victim.armor = 50
|
||||||
|
var start: int = victim.health
|
||||||
|
# Looking down a little, at the body.
|
||||||
|
_swing_for(sim, 1, -0.35)
|
||||||
|
var hits: Array[Dictionary] = _hits_on(sim, 2)
|
||||||
|
assert_eq(hits.size(), 1)
|
||||||
|
assert_false(hits[0]["headshot"])
|
||||||
|
assert_eq(victim.armor, 50 - _boathook(false))
|
||||||
|
assert_eq(victim.health, start)
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_player_out_of_reach_is_missed() -> void:
|
||||||
|
var sim: Simulation = _duel(Harvest.reach() + 0.3)
|
||||||
|
var victim: SimEntity = sim.entities.find(2)
|
||||||
|
var start: int = victim.health
|
||||||
|
_swing_for(sim, Harvest.swing_interval_ticks() * 2)
|
||||||
|
assert_eq(_hits_on(sim, 2).size(), 0)
|
||||||
|
assert_eq(victim.health, start)
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_wall_between_stops_the_swing() -> void:
|
||||||
|
var sim: Simulation = _duel(1.5)
|
||||||
|
var attacker: SimEntity = sim.entities.find(1)
|
||||||
|
var victim: SimEntity = sim.entities.find(2)
|
||||||
|
var start: int = victim.health
|
||||||
|
# A fixed solid between them, as a point of interest's wall would be.
|
||||||
|
var wall_z: float = attacker.position.z + Tuning.PLAYER_RADIUS + 0.5
|
||||||
|
sim.ground.add_solid(AABB(Vector3(95.0, -1.0, wall_z), Vector3(10.0, 4.0, 0.3)))
|
||||||
|
_swing_for(sim, Harvest.swing_interval_ticks() * 2)
|
||||||
|
assert_eq(_hits_on(sim, 2).size(), 0)
|
||||||
|
assert_eq(victim.health, start)
|
||||||
|
|
||||||
|
|
||||||
|
func test_a_player_in_front_of_a_tree_takes_the_swing_not_the_tree() -> void:
|
||||||
|
var sim: Simulation = _world([_pine_ahead(2.2)])
|
||||||
|
sim.entities.find(1).drop_state = Drop.NONE
|
||||||
|
var victim: SimEntity = sim.entities.find(sim.add_player())
|
||||||
|
victim.drop_state = Drop.NONE
|
||||||
|
victim.position = Vector3(100.0, 0.0, 100.0 + Tuning.PLAYER_RADIUS * 2.0 + 0.6)
|
||||||
|
var start: int = victim.health
|
||||||
|
assert_eq(_swing_for(sim, 1), 0, "no harvest")
|
||||||
|
assert_eq(victim.health, start - _boathook(true))
|
||||||
|
assert_eq(sim.props.health[0], 200, "the tree is untouched")
|
||||||
|
|||||||
Reference in New Issue
Block a user