extends TestCase ## A7: visual effects. Every effect comes from a pool with a hard budget, starts from ## what the server's FX records and the client's world say happened, and is gone again ## once it has played. const FRAME: float = 1.0 / 60.0 var tree: SceneTree var effects: EffectsView var world: Simulation func before_each() -> void: super.before_each() tree = Engine.get_main_loop() as SceneTree effects = EffectsView.new() effects.live = false tree.root.add_child(effects) world = Simulation.new(5, SimGround.flat(0.0)) world.sea_level = -5.0 effects.sync(world, Vector3(0.0, 2.0, -10.0)) func after_each() -> void: effects.free() super.after_each() static func _shot(item: String, at: Vector3, aim: Vector3, tick: int = 10, player: int = 1) -> Dictionary: return {"kind": NetFx.SHOT, "tick": tick, "player": player, "item": ItemCatalog.code_of(item), "position": at, "yaw": atan2(aim.x, aim.z), "pitch": asin(aim.normalized().y)} static func _impact(item: String, at: Vector3, hit: int, target: int = 0, tick: int = 10, player: int = 1) -> Dictionary: return {"kind": NetFx.IMPACT, "tick": tick, "player": player, "item": ItemCatalog.code_of(item), "position": at, "hit": hit, "target": target} func _play_out(seconds: float) -> void: var left: float = seconds while left > 0.0: effects.advance(FRAME) left -= FRAME func test_a_shot_flashes_at_the_muzzle_runs_a_tracer_and_lights_up_then_is_gone() -> void: var records: Array[Dictionary] = [_shot("dock_carbine", Vector3(0.0, 1.5, 0.0), Vector3.FORWARD)] effects.hear(records) assert_true(effects.particle_count() > 0, "a muzzle flash") assert_eq(effects.tracer_count(), 1, "one tracer for a missed single shot") assert_eq(effects.light_count(), 1, "the flash lights its surroundings") effects.advance(FRAME) for at: Vector3 in effects.particle_positions(): assert_true(at.distance_to(Vector3(0.0, 1.5, 0.0)) < 2.5, "the flash is at the muzzle: %s" % at) _play_out(1.5) assert_eq(effects.particle_count(), 0, "every particle has died") assert_eq(effects.tracer_count(), 0, "the tracer has run out") assert_eq(effects.light_count(), 0, "the light is out") func test_tracers_run_to_where_each_pellet_landed() -> void: var from: Vector3 = Vector3(0.0, 1.5, 0.0) var records: Array[Dictionary] = [_shot("fowling_piece", from, Vector3.FORWARD)] var pellets: int = ItemCatalog.get_def(ItemCatalog.code_of("fowling_piece")).pellets assert_true(pellets > 1, "the fowling piece throws a spread") for pellet: int in pellets: records.append(_impact("fowling_piece", from + Vector3(pellet * 0.2, 0.0, -12.0), Weapons.HIT_TERRAIN)) effects.hear(records) assert_eq(effects.tracer_count(), pellets, "a streak per pellet") # A streak per pellet, and each gone once it has run its 12 m. _play_out(0.02) assert_eq(effects.tracer_count(), pellets, "still running after 8 m") _play_out(0.1) assert_eq(effects.tracer_count(), 0, "all reached their impacts") func test_projectile_weapons_and_swings_draw_no_hitscan_tracer() -> void: var projectile: String = "" for id: String in ["harpoon_gun", "line_thrower"]: if ItemCatalog.get_def(ItemCatalog.code_of(id)).projectile_speed > 0.0: projectile = id assert_ne(projectile, "", "a projectile weapon exists") var swing: Dictionary = {"kind": NetFx.SHOT, "tick": 1, "player": 1, "item": -1, "position": Vector3.ZERO, "yaw": 0.0, "pitch": 0.0} var records: Array[Dictionary] = [_shot(projectile, Vector3(0.0, 1.5, 0.0), Vector3.FORWARD), swing] effects.hear(records) assert_eq(effects.tracer_count(), 0, "the projectile draws its own streak in flight; the boathook nothing") assert_true(effects.particle_count() > 0, "the projectile weapon still flashes") func test_a_hit_on_a_player_bursts_bright_and_a_splash_rises_from_the_water() -> void: effects.hear([_impact("dock_carbine", Vector3(3.0, 1.2, 0.0), Weapons.HIT_PLAYER, 7)] as Array[Dictionary]) var burst: int = effects.particle_count() assert_true(burst >= 10, "a hit on a player is a big, bright burst: %d" % burst) effects.clear() world.sea_level = 0.5 effects.hear([_impact("dock_carbine", Vector3(3.0, 0.4, 0.0), Weapons.HIT_TERRAIN)] as Array[Dictionary]) _play_out(0.2) var above: int = 0 for at: Vector3 in effects.particle_positions(): above += 1 if at.y > 0.8 else 0 assert_true(above > 0, "a shot into the shallows throws water up") func test_a_keg_blast_is_a_fireball_with_smoke_and_a_light() -> void: effects.hear([_impact("powder_keg", Vector3(0.0, 0.5, 5.0), Weapons.HIT_NONE)] as Array[Dictionary]) assert_true(effects.particle_count() >= 50, "flash, fire, debris and smoke: %d" % effects.particle_count()) assert_eq(effects.light_count(), 1) _play_out(1.0) var high: float = -INF for at: Vector3 in effects.particle_positions(): high = maxf(high, at.y) assert_true(high > 2.0, "smoke still rising after a second: %.1f" % high) _play_out(3.0) assert_eq(effects.particle_count(), 0, "and gone after four") func test_pieces_that_break_collapse_in_dust_and_the_first_sync_starts_nothing() -> void: var piece: StructureSet.Piece = world.structures.add(StructureSet.WALL, Materials.STONE, Vector4i(1, 0, 1, StructureSet.SLOT_WALL_X), 0, 0, 0, world.ground) var fresh: EffectsView = EffectsView.new() tree.root.add_child(fresh) fresh.live = false fresh.sync(world, Vector3.ZERO) assert_eq(fresh.particle_count(), 0, "what was already there when the view began isn't news") world.structures.remove(piece.id, world.ground) fresh.sync(world, Vector3.ZERO) assert_true(fresh.particle_count() >= 12, "the wall comes down in dust and chips: %d" % fresh.particle_count()) fresh.free() func test_a_player_falling_into_the_sea_splashes() -> void: var player: SimEntity = world.entities.find(world.add_player()) player.drop_state = Drop.NONE player.position = Vector3(2.0, -5.0, 2.0) effects.sync(world, Vector3.ZERO) effects.sync(world, Vector3.ZERO) assert_eq(effects.particle_count(), 0, "a player who was already there") player.swimming = true world.tick += 1 effects.sync(world, Vector3.ZERO) assert_true(effects.particle_count() >= 10, "they went in with a splash") func test_effects_far_from_the_camera_are_not_started() -> void: effects.eye = Vector3.ZERO var far: Vector3 = Vector3(EffectsView.DRAW_METRES + 50.0, 0.0, 0.0) effects.hear([_impact("powder_keg", far, Weapons.HIT_NONE), _shot("dock_carbine", far, Vector3.FORWARD)] as Array[Dictionary]) assert_eq(effects.particle_count(), 0) assert_eq(effects.tracer_count(), 0) ## The stress scene: 24 players emptying their weapons into a fort while kegs go off, ## for two seconds. The pool never goes over its budget, draws in a fixed number of ## draw calls, never adds a node, and one frame's work on a full pool stays cheap. func test_a_firefight_stays_inside_the_budget() -> void: for budget: int in [256, 512, EffectsView.MAX_PARTICLES]: effects.clear() effects.budget = budget effects.peak = 0 effects.dropped = 0 var nodes: int = effects.get_child_count() var tick: int = 100 for frame: int in 120: var records: Array[Dictionary] = [] for shooter: int in 24: var from: Vector3 = Vector3(shooter * 2.0 - 24.0, 1.5, -20.0) records.append(_shot("rivet_repeater", from, Vector3(0.0, 0.0, 1.0), tick, shooter + 1)) records.append(_impact("rivet_repeater", from + Vector3(0.0, 0.0, 30.0), Weapons.HIT_PIECE if shooter % 2 == 0 else Weapons.HIT_PLAYER, 3, tick, shooter + 1)) if frame % 10 == 0: records.append(_impact("powder_keg", Vector3(frame * 0.1, 0.5, 8.0), Weapons.HIT_NONE, 0, tick, 1)) effects.hear(records) effects.advance(FRAME) tick += 1 assert_true(effects.particle_count() <= budget, "alive within the budget") assert_true(effects.tracer_count() <= EffectsView.MAX_TRACERS, "tracers within theirs") assert_true(effects.light_count() <= EffectsView.MAX_LIGHTS, "lights within theirs") assert_eq(effects.peak, budget, "the stress scene fills the pool") assert_true(effects.dropped > 0, "and the rest is cut, not allocated") assert_eq(effects.get_child_count(), nodes, "no node was added in play") # One frame on a full pool. Headless and shared, so the bound is loose; it's # here to catch a step that grows with something it shouldn't. var started: int = Time.get_ticks_usec() effects.advance(FRAME) var spent: float = (Time.get_ticks_usec() - started) / 1000.0 print("effects: one frame of %d particles and %d tracers took %.2f ms" % [effects.particle_count(), effects.tracer_count(), spent]) assert_true(spent < 12.0, "a full pool's frame took %.2f ms" % spent) func test_the_presets_set_rising_budgets_and_the_match_view_takes_them() -> void: var settings: GameSettings = GameSettings.new() var last: int = 0 for quality: int in GameSettings.QUALITY.size(): settings.quality = quality assert_true(settings.particle_budget() > last, "each preset allows more") assert_true(settings.particle_budget() <= EffectsView.MAX_PARTICLES) last = settings.particle_budget() var view: MatchView = MatchView.new() view.setup(world, 0) tree.root.add_child(view) settings.quality = GameSettings.QUALITY_LOW view.apply_settings(settings) assert_eq(view.effects.budget, settings.particle_budget()) view.free() func test_hit_feedback_marks_own_hits_and_flashes_when_hurt() -> void: var feedback: HitFeedback = HitFeedback.new() feedback.update(100, FRAME) feedback.hear([_impact("dock_carbine", Vector3.ZERO, Weapons.HIT_PLAYER, 9, 1, 4)] as Array[Dictionary], 3) assert_eq(feedback.hit(), 0.0, "someone else's hit isn't ours") feedback.hear([_impact("dock_carbine", Vector3.ZERO, Weapons.HIT_PIECE, 9, 1, 3)] as Array[Dictionary], 3) assert_eq(feedback.hit(), 0.0, "hitting a wall isn't a hit marker") feedback.hear([_impact("dock_carbine", Vector3.ZERO, Weapons.HIT_PLAYER, 9, 1, 3)] as Array[Dictionary], 3) assert_eq(feedback.hit(), 1.0, "our shot struck someone") feedback.update(100, HitFeedback.HIT_SECONDS * 0.5) assert_near(feedback.hit(), 0.5, 0.01, "and it fades") assert_eq(feedback.hurt(), 0.0) feedback.update(80, FRAME) assert_eq(feedback.hurt(), 1.0, "losing health flashes the edge") feedback.update(-1, 1.0) feedback.update(100, FRAME) assert_eq(feedback.hurt(), 0.0, "coming back into a match isn't being hurt") func test_the_hud_shows_the_reticle_and_marker_only_while_playing() -> void: var hud: Hud = Hud.new() tree.root.add_child(hud) var state: HudState = HudState.new() hud.show_state(state) var reticle: Control = null var edge: Control = null for child: Node in hud.get_children(): if child is Hud.Reticle: reticle = child elif child is Hud.HurtEdge: edge = child assert_not_null(reticle) assert_false(reticle.visible, "no reticle in the lobby") state.playing = true state.reticle = true state.hit = 0.7 state.hurt = 0.4 hud.show_state(state) assert_true(reticle.visible) assert_eq((reticle as Hud.Reticle).hit, 0.7) assert_true(edge.visible) hud.free()