extends TestCase ## Bots that build, from issue M6: they gather materials with the boathook, wall off a ## shooter while they reload, wall off and heal instead of running when badly hurt, ## ramp up to an opponent on a cliff, and ring their cell with breakwaters at the last ## stand. Every piece goes through the same build command and checks as a player's. const FLOOR: float = 20.0 const HOME: Vector3 = Vector3(202.0, FLOOR, 202.0) const TICKS: int = Tuning.TICK_RATE_HZ ## Yaw steps facing +x. const FACING_X: int = PlayerCommand.YAW_STEPS / 4 var sim: Simulation var grid: NavGrid var world: BotWorld var brains: Array[BotBrain] = [] ## Victim ID -> attacker ID, written into the bots' world every tick, as if each victim ## kept being hit by its attacker. var hits: Dictionary = {} func _setup(ground: SimGround = null) -> void: sim = Simulation.new(7, ground if ground != null else SimGround.flat(FLOOR)) brains.clear() hits.clear() func _player(at: Vector3, yaw: int = 0) -> SimEntity: var player: SimEntity = sim.entities.find(sim.add_player()) player.position = at player.on_ground = true player.yaw = yaw return player func _bot(at: Vector3, level: int = BotBrain.NORMAL, yaw: int = 0) -> BotBrain: var brain: BotBrain = BotBrain.new(_player(at, yaw).id, level) brains.append(brain) return brain func _pin(brain: BotBrain) -> void: brain._hunt_goal = sim.entities.find(brain.player_id).position brain._holding = true brain._hunt_until = 1 << 30 func _arm(player: SimEntity, rounds: int = 150) -> void: assert_eq(Inventory.give(player, ItemCatalog.code_of("dock_carbine"), 1), 0) Inventory.give(player, ItemCatalog.code_of("cartridges"), rounds) func _ready_world() -> void: grid = NavGrid.build(sim) world = BotWorld.build(sim, grid) for entity: SimEntity in sim.entities.entities(): if entity.archetype == SimEntity.ARCHETYPE_PLAYER: world.player_ids.append(entity.id) func _run(ticks: int, until: Callable = Callable()) -> void: for _index: int in ticks: grid.sync(sim) world.observe(sim) for victim: int in hits: world.attackers[victim] = hits[victim] for brain: BotBrain in brains: sim.submit_command(brain.player_id, brain.think(sim, grid, world)) sim.step() if until.is_valid() and until.call(): return func _pieces_of(owner: int, kind: int = -1) -> Array[StructureSet.Piece]: var found: Array[StructureSet.Piece] = [] for piece: StructureSet.Piece in sim.structures.pieces.values(): if piece.owner == owner and (kind < 0 or piece.kind == kind): found.append(piece) return found ## Puts a single boulder (stone) on the flat test ground, `at` its footprint centre. func _boulder(at: Vector3) -> void: var props: PropSet = PropSet.new() props.kinds.append(PropSet.KINDS.find("boulder")) props.positions.append(at) props.yaws.append(0.0) props.boxes.append(AABB(at - Vector3(1.0, 0.0, 1.0), Vector3(2.0, 1.5, 2.0))) props.health.append(int(Tuning.PROP_KINDS["boulder"]["health"])) props.add_solids_to(sim.ground) sim.props = props sim.structures.props = props func test_an_armed_bot_gathers_materials_from_a_prop_nearby() -> void: _setup() var brain: BotBrain = _bot(HOME) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) _boulder(HOME + Vector3(6.0, 0.0, 0.0)) _ready_world() var states: Dictionary = {} _run(20 * TICKS, func() -> bool: states[brain.state] = true return me.materials[Materials.STONE] >= Tuning.HARVEST_PER_HIT * 3) assert_true(states.has("gather"), "it went to gather: %s" % [states.keys()]) assert_true(me.materials[Materials.STONE] >= Tuning.HARVEST_PER_HIT * 3, "it broke stone (%d)" % me.materials[Materials.STONE]) _run(1) assert_eq(brain.stats["harvested"], me.materials[Materials.STONE]) func test_a_bot_with_enough_stock_or_no_weapon_leaves_props_alone() -> void: for armed: bool in [true, false]: _setup() var brain: BotBrain = _bot(HOME) var me: SimEntity = sim.entities.find(brain.player_id) if armed: _arm(me) me.materials[Materials.DRIFTWOOD] = Tuning.BOT_BUILD_STOCK _boulder(HOME + Vector3(6.0, 0.0, 0.0)) _ready_world() var states: Dictionary = {} _run(8 * TICKS, func() -> bool: states[brain.state] = true return false) assert_false(states.has("gather"), "armed %s: %s" % [armed, states.keys()]) assert_eq(me.materials[Materials.STONE], 0) func test_a_control_bot_never_gathers_or_builds() -> void: _setup() var brain: BotBrain = _bot(HOME) brain.builds = false var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) _boulder(HOME + Vector3(6.0, 0.0, 0.0)) _ready_world() _run(8 * TICKS) assert_eq(me.materials[Materials.STONE], 0) assert_eq(brain.stats["pieces"], 0) func test_a_bot_shot_at_while_reloading_walls_off_the_shooter() -> void: _setup() var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) me.materials[Materials.DRIFTWOOD] = 50 var shooter: SimEntity = _player(HOME + Vector3(20.0, 0.0, 0.0), FACING_X * 3) _arm(shooter) shooter.selected = 1 _ready_world() _pin(brain) # Seen, then shot while its magazine runs dry. var seen: Dictionary = {} _run(2 * TICKS, func() -> bool: seen[brain.state] = true return brain.state == "fight") assert_eq(brain.state, "fight", "%s target %d visible %s" % [seen.keys(), brain.target_id, brain.target_visible]) me.slot_rounds[0] = 0 hits[me.id] = shooter.id var states: Dictionary = {} _run(2 * TICKS, func() -> bool: states[brain.state] = true return not _pieces_of(me.id).is_empty()) var walls: Array[StructureSet.Piece] = _pieces_of(me.id, StructureSet.WALL) assert_eq(walls.size(), 1, "one wall: %s" % [states.keys()]) var wall: AABB = walls[0].boxes[0] assert_true(wall.get_center().x > me.position.x and wall.get_center().x < shooter.position.x, "between them (wall at x %.1f)" % wall.get_center().x) assert_eq(me.materials[Materials.DRIFTWOOD], 50 - StructureSet.cost_of(StructureSet.WALL)) hits.clear() _run(Tuning.BOT_SENSE_TICKS + 2) assert_eq(brain.state, "cover") assert_false(brain.target_visible, "the wall hides the shooter") _run(roundi(Tuning.BOT_COVER_HOLD_SECONDS * TICKS) + TICKS) assert_true(me.slot_rounds[0] > 0, "it reloaded behind the wall") assert_ne(brain.state, "cover", "and came out again") func test_a_badly_hurt_bot_walls_off_and_heals_instead_of_running() -> void: _setup() var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) Inventory.give(me, ItemCatalog.code_of("sailcloth_bandage"), 2) me.health = 30 me.materials[Materials.DRIFTWOOD] = 30 var other: SimEntity = _player(HOME + Vector3(20.0, 0.0, 0.0), FACING_X * 3) _arm(other) other.selected = 1 _ready_world() _pin(brain) var states: Dictionary = {} var start: Vector3 = me.position _run(8 * TICKS, func() -> bool: states[brain.state] = true return false) assert_true(states.has("cover"), "it took cover: %s" % [states.keys()]) assert_false(states.has("retreat"), "rather than running: %s" % [states.keys()]) assert_eq(_pieces_of(me.id, StructureSet.WALL).size(), 1) assert_true(me.health > 30, "and healed (%d)" % me.health) assert_true(me.position.distance_to(start) < 3.0, "where it stood (moved %.1f m)" % me.position.distance_to(start)) ## Flat ground with a block 8 m high from x = 212 m: a cliff to its top. func _cliff() -> SimGround: var ground: SimGround = SimGround.flat(FLOOR) ground.add_solid(AABB(Vector3(212.0, 0.0, 180.0), Vector3(30.0, FLOOR + 8.0, 40.0))) return ground func test_a_bot_ramps_up_a_cliff_to_an_opponent_on_top() -> void: _setup(_cliff()) var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) me.materials[Materials.DRIFTWOOD] = 100 var above: SimEntity = _player(Vector3(222.0, FLOOR + 8.0, 202.0), FACING_X * 3) _arm(above) above.selected = 1 _ready_world() _pin(brain) # The one on top keeps shooting down at it. hits[me.id] = above.id var states: Dictionary = {} _run(12 * TICKS, func() -> bool: states[brain.state] = true return me.position.y >= FLOOR + 7.5 and me.on_ground) var ramps: Array[StructureSet.Piece] = _pieces_of(me.id, StructureSet.RAMP) assert_true(ramps.size() >= 2, "a staircase of ramps (%d): %s" % [ramps.size(), states.keys()]) assert_true(ramps.size() <= Tuning.BOT_RAMP_MAX) assert_true(me.position.y >= FLOOR + 7.5, "up on top (at %.1f m)" % me.position.y) func test_a_bot_on_a_gentle_rise_walks_rather_than_ramps() -> void: _setup() var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) me.materials[Materials.DRIFTWOOD] = 100 # 4 m higher but 20 m off, with open ground between: no cliff. var other: SimEntity = _player(HOME + Vector3(20.0, 4.0, 0.0), FACING_X * 3) other.on_ground = true _ready_world() _pin(brain) hits[me.id] = other.id _run(TICKS) assert_eq(_pieces_of(me.id, StructureSet.RAMP).size(), 0) func test_a_bot_rings_itself_with_breakwaters_at_the_last_stand() -> void: _setup() var brain: BotBrain = _bot(HOME) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) me.materials[Materials.STONE] = 4 * StructureSet.cost_of(StructureSet.BREAKWATER) sim.start_tide() _ready_world() _pin(brain) # Make the first phase a last stand on the bots' map, with its high point where the # bot stands, then fast forward to just before the rise. world.phase_cells[0] = 0 world.phase_tops[0] = me.position assert_true(brain._ring_due(sim, world, me) == (sim.tide.ticks_until_rise(sim.tick) <= roundi(Tuning.BOT_RING_SECONDS * TICKS))) var rise_in: int = sim.tide.ticks_until_rise(sim.tick) - roundi(Tuning.BOT_RING_SECONDS * TICKS) + 1 sim.tick += maxi(rise_in, 0) _run(TICKS, func() -> bool: return _pieces_of(me.id, StructureSet.BREAKWATER).size() >= 4) var ring: Array[StructureSet.Piece] = _pieces_of(me.id, StructureSet.BREAKWATER) assert_eq(ring.size(), 4, "a breakwater on each side of its cell") var g: float = StructureSet.cell_size() var cell: Vector2i = Vector2i(floori(me.position.x / g), floori(me.position.z / g)) for piece: StructureSet.Piece in ring: var centre: Vector3 = piece.boxes[0].get_center() assert_true(absf(centre.x - (cell.x + 0.5) * g) <= g * 0.5 + 0.5 and absf(centre.z - (cell.y + 0.5) * g) <= g * 0.5 + 0.5, "round its own cell") assert_eq(me.materials[Materials.STONE], 0) _run(1) assert_eq(brain.stats["breakwaters"], 4) func test_fights_are_counted_with_and_without_building() -> void: _setup() var brain: BotBrain = _bot(HOME, BotBrain.HARD, FACING_X) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) var other: SimEntity = _player(HOME + Vector3(15.0, 0.0, 0.0), FACING_X * 3) _ready_world() _pin(brain) _run(15 * TICKS, func() -> bool: return other.eliminated) assert_true(other.eliminated, "it won the fight") _run(roundi((Tuning.BOT_FIGHT_GAP_SECONDS + 1.0) * TICKS)) assert_eq(brain.stats["fights"], 1) assert_eq(brain.stats["fights_built"], 0) assert_eq(brain.stats["fights_lost"], 0) func test_the_batch_summary_compares_builders_with_the_control_group() -> void: var results: Array[Dictionary] = [] for builds: Array in [[4, 1, 2, 1], [6, 1, 1, 0]]: var build: Dictionary = BotBatch._empty_group() build["bots"] = 12 build["places"] = [1, 3] build["wins"] = 1 build["fights"] = builds[0] build["fights_built"] = builds[1] build["fights_lost"] = builds[2] build["fights_built_lost"] = builds[3] build["fights_stocked"] = 2 build["fights_stocked_built"] = builds[1] var control: Dictionary = BotBatch._empty_group() control["bots"] = 12 control["places"] = [2, 4] control["fights"] = 5 control["fights_lost"] = 3 results.append({"building": {"build": build, "control": control}}) var total: Dictionary = BotBatch.summarise_building(results) assert_eq(total["build"]["fights"], 10) assert_near(total["build"]["fight_share_built"], 0.2, 0.0001) assert_near(total["build"]["stocked_share"], 0.4, 0.0001) assert_near(total["build"]["stocked_share_built"], 0.5, 0.0001) assert_near(total["build"]["fight_loss"], 0.3, 0.0001) assert_near(total["build"]["loss_when_built"], 0.5, 0.0001) assert_near(total["build"]["loss_when_not"], 0.25, 0.0001) assert_near(total["build"]["mean_place"], 2.0, 0.0001) assert_near(total["control"]["mean_place"], 3.0, 0.0001) assert_near(total["control"]["fight_loss"], 0.6, 0.0001) assert_eq(total["build"]["wins"], 2) assert_false(total["build"].has("places")) func test_a_bot_the_tide_would_catch_rings_itself_in_and_stays_dry() -> void: _setup() var brain: BotBrain = _bot(HOME) var me: SimEntity = sim.entities.find(brain.player_id) _arm(me) me.materials[Materials.STONE] = 4 * StructureSet.cost_of(StructureSet.BREAKWATER) # The tide's late phases: a rise to 3 m over the bot's ground, starting now, with # dry ground 300 m off, far too far to walk before the water arrives. var phases: int = Tide.phase_count() var levels: PackedFloat32Array = PackedFloat32Array() for index: int in phases: levels.append(FLOOR + 3.0 if index >= phases - Tuning.BOT_STRAND_PHASES else FLOOR - 10.0) sim.start_tide() sim.tide = Tide.new(sim.tick - _rise_start_of(phases - Tuning.BOT_STRAND_PHASES), levels) _ready_world() _pin(brain) brain._fleeing = true brain._flee_goal = HOME + Vector3(300.0, 0.0, 0.0) brain._tide_checked = sim.tick assert_true(brain._stranded(sim, world, me), "it can't outrun the water") var states: Dictionary = {} _run(2 * TICKS, func() -> bool: states[brain.state] = true return false) assert_eq(_pieces_of(me.id, StructureSet.BREAKWATER).size(), 4, "it rang its cell: %s" % [states.keys()]) var at: Vector3 = me.position _run(20 * TICKS) assert_eq(brain.state, "breakwater", "and stays in it") assert_true(me.position.distance_to(at) < 1.0, "without wandering off (%.1f m)" % me.position.distance_to(at)) ## Ticks from the tide's start to the start of phase `index`'s rise. static func _rise_start_of(index: int) -> int: var at: int = 0 for phase: int in index + 1: at += roundi(float(Tuning.TIDE_PHASES[phase]["hold"]) * TICKS) if phase < index: at += maxi(roundi(float(Tuning.TIDE_PHASES[phase]["rise"]) * TICKS), 1) return at