extends TestCase ## Duos from issue M7: going down instead of out while a teammate stands, crawling, ## revives, bleeding out, a team eliminated when both are out, no friendly damage, and ## team placement in a match. const FEET: Vector3 = Vector3(100.0, 0.0, 100.0) const REVIVE_TICKS: int = roundi(Tuning.REVIVE_SECONDS * Tuning.TICK_RATE_HZ) ## A flat world with four players in a row 1 m apart, facing +z: 1 and 2 on team 0, ## 3 and 4 on team 1. func _sim() -> Simulation: var sim: Simulation = Simulation.new(77, SimGround.flat()) for index: int in 4: var player: SimEntity = sim.entities.find(sim.add_player()) player.position = FEET + Vector3(index * 1.0, 0.0, 0.0) player.on_ground = true sim.set_team(player.id, index / 2) return sim func _p(sim: Simulation, id: int) -> SimEntity: return sim.entities.find(id) ## Steps once, with `buttons` and `move` for the players in `held`, and nothing else. func _step(sim: Simulation, held: Dictionary = {}, move: Vector2 = Vector2.ZERO) -> void: for id: int in [1, 2, 3, 4]: if not sim.entities.contains(id): continue var buttons: int = int(held.get(id, 0)) var player: SimEntity = _p(sim, id) sim.submit_command(id, PlayerCommand.from_input(sim.tick, move if held.has(id) else Vector2.ZERO, 0.0, 0.0, buttons, player.selected)) sim.step() func _events(sim: Simulation, kind: String) -> Array[Dictionary]: var found: Array[Dictionary] = [] for event: Dictionary in sim.events: if event["type"] == kind: found.append(event) return found ## Lands a lethal hit on `victim` from `attacker`, then steps. func _knock(sim: Simulation, victim: int, attacker: int) -> void: _p(sim, victim).health = 5 _p(sim, victim).armor = 0 assert_eq(Weapons.damage_player(sim, _p(sim, victim), 50, attacker), 5) _step(sim) func test_a_lethal_hit_downs_a_player_whose_teammate_stands() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) var downed: SimEntity = _p(sim, 1) assert_true(downed.downed, "down, not out") assert_false(downed.eliminated) assert_eq(downed.health, Tuning.DOWNED_HEALTH) var events: Array[Dictionary] = _events(sim, "downed") assert_eq(events.size(), 1) assert_eq(events[0]["player"], 1) assert_eq(events[0]["by"], 3) assert_eq(_events(sim, "eliminated").size(), 0) func test_a_player_with_no_team_or_no_teammate_standing_is_eliminated_outright() -> void: var sim: Simulation = _sim() sim.set_team(1, -1) _knock(sim, 1, 3) assert_true(_p(sim, 1).eliminated, "no team: out") assert_false(_p(sim, 1).downed) # Player 2's teammate is out, so 2 has nobody to wait for either. _knock(sim, 2, 3) assert_true(_p(sim, 2).eliminated) func test_downed_players_crawl_and_cannot_fire_build_or_loot() -> void: var sim: Simulation = _sim() Inventory.give(_p(sim, 1), ItemCatalog.code_of("dock_carbine"), 1) Inventory.give(_p(sim, 1), ItemCatalog.get_def(ItemCatalog.code_of("dock_carbine")).ammo_code, 20) _p(sim, 1).selected = 1 _knock(sim, 1, 3) var start: Vector3 = _p(sim, 1).position var shots: int = 0 for _tick: int in Tuning.TICK_RATE_HZ: _step(sim, {1: PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_SPRINT | PlayerCommand.BUTTON_JUMP}, Vector2(0.0, 1.0)) shots += _events(sim, "shot").size() assert_true(_p(sim, 1).on_ground, "no jumping") assert_eq(shots, 0, "no firing") var crawled: float = _p(sim, 1).position.distance_to(start) assert_near(crawled, Tuning.DOWNED_CRAWL_SPEED, 0.1, "a second of crawling, sprint held") assert_true(_p(sim, 1).crouching, "low to the ground") _step(sim, {1: PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD}) assert_eq(sim.structures.pieces.size(), 0, "no building") func test_a_teammate_holding_interact_revives_after_the_revive_time() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) for _tick: int in REVIVE_TICKS - 1: _step(sim, {2: PlayerCommand.BUTTON_INTERACT}) assert_true(_p(sim, 1).downed) assert_eq(_p(sim, 1).revive_ticks, REVIVE_TICKS - 1) assert_eq(_p(sim, 1).reviver, 2) _step(sim, {2: PlayerCommand.BUTTON_INTERACT}) assert_false(_p(sim, 1).downed, "back up") assert_eq(_p(sim, 1).health, Tuning.REVIVE_HEALTH) var revived: Array[Dictionary] = _events(sim, "revived") assert_eq(revived.size(), 1) assert_eq(revived[0]["by"], 2) func test_letting_go_or_moving_away_starts_the_revive_over() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) for _tick: int in 60: _step(sim, {2: PlayerCommand.BUTTON_INTERACT}) _step(sim) assert_eq(_p(sim, 1).revive_ticks, 0, "let go") _p(sim, 2).position = _p(sim, 1).position + Vector3(Tuning.REVIVE_RANGE + 0.5, 0.0, 0.0) for _tick: int in REVIVE_TICKS + 5: _step(sim, {2: PlayerCommand.BUTTON_INTERACT}) assert_true(_p(sim, 1).downed, "out of reach") assert_eq(_p(sim, 1).revive_ticks, 0) func test_an_opponent_cannot_revive_and_a_downed_player_cannot_revive() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) _knock(sim, 4, 2) for _tick: int in REVIVE_TICKS + 5: _step(sim, {3: PlayerCommand.BUTTON_INTERACT, 4: PlayerCommand.BUTTON_INTERACT}) assert_true(_p(sim, 1).downed, "3 is an opponent") assert_true(_p(sim, 3).health > 0 and not _p(sim, 3).downed) func test_a_downed_player_bleeds_out_credited_to_who_downed_them() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) var ticks: int = 0 while not _p(sim, 1).eliminated and ticks < 2000: _step(sim) ticks += 1 assert_true(_p(sim, 1).eliminated) assert_near(ticks, Tuning.DOWNED_HEALTH * Tuning.DOWNED_BLEED_INTERVAL_TICKS, Tuning.DOWNED_BLEED_INTERVAL_TICKS, "about 30 s") var out: Array[Dictionary] = _events(sim, "eliminated") assert_eq(out.size(), 1) assert_eq(out[0]["by"], 3) assert_eq(out[0]["cause"], "weapon") func test_hits_on_a_downed_player_finish_them() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) Weapons.damage_player(sim, _p(sim, 1), Tuning.DOWNED_HEALTH, 4) _step(sim) assert_true(_p(sim, 1).eliminated) assert_eq(_events(sim, "eliminated")[0]["by"], 4) func test_the_team_is_out_when_both_are_down_or_out() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) assert_true(_p(sim, 1).downed) _knock(sim, 2, 4) assert_true(_p(sim, 2).eliminated, "nobody left to down for") assert_true(_p(sim, 1).eliminated, "and nobody left to revive 1") assert_eq(_events(sim, "eliminated").size(), 2) var credited: Dictionary = {} for event: Dictionary in _events(sim, "eliminated"): credited[event["player"]] = event["by"] assert_eq(credited, {1: 3, 2: 4}) func test_teammates_falling_together_are_both_out() -> void: var sim: Simulation = _sim() _p(sim, 1).health = 0 _p(sim, 2).health = 0 _step(sim) assert_true(_p(sim, 1).eliminated and _p(sim, 2).eliminated) assert_false(_p(sim, 1).downed or _p(sim, 2).downed) func test_no_friendly_damage_from_any_weapon() -> void: var sim: Simulation = _sim() assert_eq(Weapons.damage_player(sim, _p(sim, 2), 30, 1), 0, "teammate") assert_eq(_p(sim, 2).health, Tuning.PLAYER_MAX_HEALTH) assert_eq(Weapons.damage_player(sim, _p(sim, 3), 30, 1), 30, "opponent") assert_eq(Weapons.damage_player(sim, _p(sim, 1), 10, 1), 10, "yourself, as with your own keg") # A keg thrown into the team: the teammate beside the blast takes nothing. var keg: int = ItemCatalog.code_of("powder_keg") var def: ItemDef = ItemCatalog.get_def(keg) var before: int = _p(sim, 2).health Explosives.blast(sim, 1, def, 0, _p(sim, 2).position + Vector3(0.0, 0.3, 0.0)) assert_eq(_p(sim, 2).health, before, "no blast damage to a teammate") assert_true(_p(sim, 3).health < Tuning.PLAYER_MAX_HEALTH - 30, "the opponent a metre further on is hurt") func test_a_downed_player_in_the_sea_still_drowns() -> void: var sim: Simulation = _sim() _knock(sim, 1, 3) sim.set_sea_level(5.0) for _tick: int in 3: _step(sim) assert_true(_p(sim, 1).swimming) var ticks: int = 0 while not _p(sim, 1).eliminated and ticks < 1200: _step(sim) ticks += 1 assert_true(_p(sim, 1).eliminated, "drowning takes the downed health") assert_true(ticks < Tuning.DOWNED_HEALTH * Tuning.DOWNED_BLEED_INTERVAL_TICKS, "sooner than bleeding out") func test_the_hash_tells_downed_and_revive_progress_apart() -> void: var a: Simulation = _sim() var b: Simulation = _sim() assert_eq(a.state_hash(), b.state_hash()) _p(a, 1).downed = true assert_ne(a.state_hash(), b.state_hash()) _p(b, 1).downed = true _p(b, 1).revive_ticks = 3 assert_ne(a.state_hash(), b.state_hash()) # Match: team placement. func _rules() -> MatchRules: var rules: MatchRules = MatchRules.immediate() rules.max_players = 6 rules.results_ticks = 2 rules.team_size = Tuning.DUO_TEAM_SIZE return rules func _duos_match() -> Match: var m: Match = Match.new(4242, _rules()) m.sim = Simulation.new(4242, SimGround.flat(20.0)) m.join(1) m.step() assert_eq(m.phase, Match.DROP) var index: int = 0 for handle: int in m.participants: var player: SimEntity = m.sim.entities.find(m.entity_of[handle]) player.drop_state = Drop.NONE player.position = Vector3(100.0 + index * 4.0, 20.0, 100.0) player.velocity = Vector3.ZERO player.on_ground = true index += 1 m.sim.drop.finished = true m.step() assert_eq(m.phase, Match.PLAY) return m func _mp(m: Match, handle: int) -> SimEntity: return m.sim.entities.find(m.entity_of[handle]) func _place_of(m: Match, handle: int) -> int: for record: Dictionary in m.placements: if record["handle"] == handle: return int(record["place"]) return 0 func test_match_puts_each_pair_on_a_team_in_the_simulation() -> void: var m: Match = _duos_match() var hs: Array[int] = m.participants.duplicate() assert_eq(hs.size(), 6) for index: int in hs.size(): assert_eq(m.team_of[hs[index]], index / 2) assert_eq(_mp(m, hs[index]).team, index / 2) func test_match_keeps_a_downed_team_alive_and_places_the_pair_together() -> void: var m: Match = _duos_match() var hs: Array[int] = m.participants.duplicate() _mp(m, hs[0]).health = 0 _mp(m, hs[0]).last_attacker = m.entity_of[hs[2]] _mp(m, hs[0]).last_cause = SimEntity.CAUSE_WEAPON m.step() assert_true(_mp(m, hs[0]).downed) assert_eq(_place_of(m, hs[0]), 0, "down isn't placed") assert_true(m.alive().has(hs[0]), "still in the match") var downs: Array[Dictionary] = [] for event: Dictionary in m.events: if event["type"] == "downed": downs.append(event) assert_eq(downs.size(), 1) assert_eq(downs[0]["by"], hs[2]) assert_true(bool(m.kill_feed[-1].get("downed", false)), "the down is in the feed") assert_eq(m.stats[hs[2]]["eliminations"], 0, "a down isn't an elimination yet") # The teammate falls: both are out, sharing the place below the two teams left. _mp(m, hs[1]).health = 0 _mp(m, hs[1]).last_attacker = m.entity_of[hs[4]] _mp(m, hs[1]).last_cause = SimEntity.CAUSE_WEAPON m.step() assert_eq(_place_of(m, hs[0]), 5) assert_eq(_place_of(m, hs[1]), 5) assert_eq(m.stats[hs[2]]["eliminations"], 1, "credited to who downed them") assert_eq(m.stats[hs[4]]["eliminations"], 1) assert_eq(m.phase, Match.PLAY) # One of the next team goes down, then the other: the last team wins, both of them. _mp(m, hs[2]).health = 0 m.step() assert_true(_mp(m, hs[2]).downed) assert_eq(m.phase, Match.PLAY, "a downed player keeps the team in it") _mp(m, hs[3]).health = 0 m.step() assert_eq(m.phase, Match.RESULTS) assert_eq(m.winners, [hs[4], hs[5]] as Array[int]) assert_eq(_place_of(m, hs[2]), 3) assert_eq(_place_of(m, hs[3]), 3) func test_match_tie_break_prefers_a_standing_team_over_a_downed_one() -> void: var m: Match = _duos_match() var hs: Array[int] = m.participants.duplicate() # Teams 0 and 1 left, team 2 out. _mp(m, hs[4]).health = 0 _mp(m, hs[5]).health = 0 m.step() _mp(m, hs[0]).health = 0 m.step() assert_true(_mp(m, hs[0]).downed) _mp(m, hs[1]).health = 1 _mp(m, hs[2]).health = 1 _mp(m, hs[3]).health = 1 assert_eq(m._tie_break([hs[0], hs[2]] as Array[int]), hs[2], "standing beats downed, despite more health")