extends TestCase ## Structure health and integrity from issue B3: build-up health, material strength, ## damage, tide wear, and cascading collapse when support goes. const W: int = StructureSet.WALL const F: int = StructureSet.FLOOR const BW: int = StructureSet.BREAKWATER func _sim() -> Simulation: var sim: Simulation = Simulation.new(5, SimGround.flat()) var id: int = sim.add_player() var player: SimEntity = sim.entities.find(id) player.position = Vector3(10.0, 0.0, 10.0) return sim ## Adds a piece directly, as if placed by player 1, skipping range and cost checks. func _add(sim: Simulation, kind: int, slot: Vector4i, material: int = 0, facing: int = 0) -> int: return sim.structures.add(kind, material, slot, facing, 1, sim.tick, sim.ground).id func _finish_building(sim: Simulation) -> void: for _i: int in ceili(9.0 * Tuning.TICK_RATE_HZ) + 1: sim.step() func _destroyed(sim: Simulation, cause: String = "") -> Array[int]: var ids: Array[int] = [] for event: Dictionary in sim.events: if event["type"] == "piece_destroyed" and (cause.is_empty() or event["cause"] == cause): ids.append(event["piece"]) return ids func test_pieces_gain_health_while_building() -> void: for material: int in 3: var sim: Simulation = _sim() var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), material) var piece: StructureSet.Piece = sim.structures.find(id) var spec: Dictionary = Tuning.MATERIALS[Materials.NAMES[material]] var full: int = spec["health"] var ticks: int = ceili(float(spec["build_seconds"]) * Tuning.TICK_RATE_HZ) assert_eq(piece.health, ceili(full * Tuning.BUILD_START_HEALTH_FRACTION)) var last: int = piece.health for tick: int in ticks: sim.step() assert_true(piece.health >= last, "health never drops while building") last = piece.health if tick == ticks / 2 - 1: assert_near(piece.health, full * (0.1 + 0.9 * 0.5), full * 0.03, "%s halfway" % Materials.NAMES[material]) if tick < ticks - 1: assert_true(sim.structures.is_building(id)) assert_eq(piece.health, full, "%s at full health after %d ticks" % [Materials.NAMES[material], ticks]) assert_false(sim.structures.is_building(id)) sim.step() assert_eq(piece.health, full, "no gain past full") func test_damage_while_building_stays_taken() -> void: var sim: Simulation = _sim() var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) for _i: int in 30: sim.step() assert_false(sim.damage_piece(id, 40)) _finish_building(sim) assert_eq(sim.structures.find(id).health, 300 - 40) func test_material_strength() -> void: # The same blows that break a finished driftwood wall leave stone and iron standing. var survivors: Array[bool] = [] for material: int in 3: var sim: Simulation = _sim() var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), material) _finish_building(sim) for _blow: int in 4: sim.damage_piece(id, 50) survivors.append(sim.structures.find(id) != null) assert_eq(survivors, [false, true, true]) assert_true(Tuning.MATERIALS["driftwood"]["build_seconds"] < Tuning.MATERIALS["stone"]["build_seconds"]) assert_true(Tuning.MATERIALS["stone"]["build_seconds"] < Tuning.MATERIALS["salvage_iron"]["build_seconds"]) func test_a_fresh_piece_breaks_easily() -> void: var sim: Simulation = _sim() var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), Materials.SALVAGE_IRON) assert_true(sim.damage_piece(id, 50), "a just-placed iron wall has only a tenth of its health") assert_null(sim.structures.find(id)) assert_eq(_destroyed(sim, "damage"), [id]) assert_eq(sim.structures.at_slot(Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z)), 0, "the slot is free again") # Its collision is gone. assert_false(sim.ground.body_blocked(Vector3(10.0, 0.0, 12.0), Tuning.PLAYER_RADIUS, Tuning.PLAYER_HEIGHT, 0.0)) func test_cascading_collapse() -> void: # A wall holding a floor, holding a wall, holding a floor: a little tower. var sim: Simulation = _sim() var base_wall: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z)) var floor_one: int = _add(sim, F, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR)) var upper_wall: int = _add(sim, W, Vector4i(2, 2, 4, StructureSet.SLOT_WALL_Z)) var floor_two: int = _add(sim, F, Vector4i(2, 4, 3, StructureSet.SLOT_FLOOR)) # And a separate grounded wall that must not be touched. var other: int = _add(sim, W, Vector4i(8, 0, 8, StructureSet.SLOT_WALL_Z)) assert_true(sim.structures.find(base_wall).grounded) assert_false(sim.structures.find(floor_one).grounded) sim.damage_piece(base_wall, 100000) var gone: Array[int] = _destroyed(sim, "collapse") assert_eq(gone, [floor_one, upper_wall, floor_two]) assert_eq(sim.structures.count(), 1) assert_not_null(sim.structures.find(other)) func test_a_second_support_keeps_a_structure_up() -> void: var sim: Simulation = _sim() var left: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_X)) var right: int = _add(sim, W, Vector4i(3, 0, 3, StructureSet.SLOT_WALL_X)) var deck: int = _add(sim, F, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR)) sim.damage_piece(left, 100000) assert_eq(_destroyed(sim, "collapse"), []) assert_not_null(sim.structures.find(deck), "the right wall still holds the floor") sim.step() sim.damage_piece(right, 100000) assert_eq(_destroyed(sim, "collapse"), [deck]) func test_boathook_damages_pieces_and_can_bring_them_down() -> void: var sim: Simulation = _sim() var player: SimEntity = sim.entities.find(1) player.materials[0] = 100 assert_eq(Building.place(sim, player, Building.target(player, W), 0), Building.Result.OK) var wall: int = 1 var floor_on_top: int = _add(sim, F, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR)) _finish_building(sim) var hits: int = 0 var collapsed: Array[int] = [] for _tick: int in Harvest.swing_interval_ticks() * 4: sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, PlayerCommand.BUTTON_FIRE)) sim.step() for event: Dictionary in sim.events: if event["type"] == "piece_hit": hits += 1 collapsed.append_array(_destroyed(sim, "collapse")) assert_eq(hits, 3, "150 health takes three 50-point hits") assert_null(sim.structures.find(wall)) assert_eq(collapsed, [floor_on_top]) assert_eq(player.materials[0], 90, "hitting pieces gives nothing back") func test_tide_wears_submerged_pieces() -> void: var sim: Simulation = _sim() var dry: int = _add(sim, W, Vector4i(2, 1, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) var wet: int = _add(sim, W, Vector4i(4, 0, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) var breakwater: int = _add(sim, BW, Vector4i(6, 0, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) _finish_building(sim) sim.set_sea_level(1.5) var before: Array[int] = [sim.structures.find(dry).health, sim.structures.find(wet).health, sim.structures.find(breakwater).health] for _i: int in Tuning.TICK_RATE_HZ * 10: sim.step() assert_eq(sim.structures.find(dry).health, before[0], "base above the sea: no wear") assert_eq(sim.structures.find(wet).health, before[1] - Tuning.TIDE_STRUCTURE_DPS * 10) assert_eq(sim.structures.find(breakwater).health, before[2] - roundi(Tuning.BREAKWATER_WEAR_PER_METRE * 1.5) * 10) # Past its maximum head a breakwater fails at once. sim.set_sea_level(Tuning.BREAKWATER_MAX_HEAD + 0.5) var failed: Array[int] = [] for _i: int in Tuning.TICK_RATE_HZ: sim.step() failed.append_array(_destroyed(sim, "tide")) assert_true(failed.has(breakwater)) func test_tide_destroying_a_support_collapses_what_it_held() -> void: var sim: Simulation = _sim() var low: int = _add(sim, W, Vector4i(2, -1, 3, StructureSet.SLOT_WALL_Z), Materials.DRIFTWOOD) var deck: int = _add(sim, F, Vector4i(2, 1, 3, StructureSet.SLOT_FLOOR)) _finish_building(sim) sim.set_sea_level(0.5) var causes: Dictionary = {} for _i: int in Tuning.TICK_RATE_HZ * 60: sim.step() for event: Dictionary in sim.events: if event["type"] == "piece_destroyed": causes[event["piece"]] = event["cause"] if causes.size() == 2: break assert_eq(causes.get(low), "tide") assert_eq(causes.get(deck), "collapse") func test_terrain_change_triggers_collapse() -> void: # A heightfield we can dig: a hill under the first wall. var side: int = 33 var heights: PackedFloat32Array = PackedFloat32Array() heights.resize(side * side) var ground: SimGround = SimGround.from_heights(heights, side, 32.0) var sim: Simulation = Simulation.new(5, ground) var wall: int = sim.structures.add(W, 0, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), 0, 0, 0, ground).id var deck: int = sim.structures.add(F, 0, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR), 0, 0, 0, ground).id assert_true(sim.structures.find(wall).grounded) # Nothing changed yet: no collapse. assert_eq(sim.ground_changed(AABB(Vector3(0.0, -10.0, 0.0), Vector3(64.0, 20.0, 64.0))), 0) # The ground falls away by 20 m: the wall no longer touches it (and is past skirt depth). for index: int in heights.size(): ground.heights[index] = -20.0 var collapsed: int = sim.ground_changed(AABB(Vector3(0.0, -30.0, 0.0), Vector3(64.0, 40.0, 64.0))) assert_eq(collapsed, 2) assert_null(sim.structures.find(wall)) assert_null(sim.structures.find(deck)) func test_fixed_solids_count_as_ground() -> void: var sim: Simulation = _sim() # A pier deck 6 m up, like a point of interest's solid. sim.ground.add_solid(AABB(Vector3(0.0, 5.0, 0.0), Vector3(40.0, 1.0, 40.0))) var on_pier: int = _add(sim, W, Vector4i(2, 3, 3, StructureSet.SLOT_WALL_Z)) assert_true(sim.structures.find(on_pier).grounded) # Props are not ground: they can be felled. assert_false(sim.structures.touches_fixed_ground([AABB(Vector3(500.0, 20.0, 500.0), Vector3(1.0, 1.0, 1.0))], sim.ground)) func test_removing_a_piece_from_a_500_piece_fort_stays_within_budget() -> void: var sim: Simulation = Simulation.new(5, SimGround.flat()) # A 6 x 6-cell fort, 5 storeys: floors above the ground, walls on every edge. var size: int = 6 var storeys: int = 5 var up: int = roundi(Tuning.GRID_CELL_METRES / Tuning.BUILD_LEVEL_METRES) var ground_walls: Array[int] = [] var top_floor: int = 0 for storey: int in storeys: var level: int = storey * up for cz: int in size: for cx: int in size: if storey > 0: top_floor = _add(sim, F, Vector4i(10 + cx, level, 10 + cz, StructureSet.SLOT_FLOOR)) for cz: int in size: for edge: int in size + 1: var id: int = _add(sim, W, Vector4i(10 + edge, level, 10 + cz, StructureSet.SLOT_WALL_X)) if storey == 0: ground_walls.append(id) for edge: int in size + 1: for cx: int in size: var id: int = _add(sim, W, Vector4i(10 + cx, level, 10 + edge, StructureSet.SLOT_WALL_Z)) if storey == 0: ground_walls.append(id) var total: int = sim.structures.count() assert_true(total >= 500, "fort has %d pieces" % total) # Removing one ground wall: the search reaches a grounded piece almost at once. var started: int = Time.get_ticks_usec() sim.damage_piece(ground_walls[7], 100000) var one: int = Time.get_ticks_usec() - started assert_eq(sim.structures.count(), total - 1) # A worst case: a top floor whose only support search must cross most of the fort. started = Time.get_ticks_usec() sim.damage_piece(top_floor, 100000) var two: int = Time.get_ticks_usec() - started # Cutting every ground wall brings down all 500 in one cascade. var remaining: Array[int] = [] for id: int in ground_walls: if sim.structures.find(id) != null: remaining.append(id) var last: int = remaining.pop_back() for id: int in remaining: sim.structures.remove(id, sim.ground) sim.events.clear() started = Time.get_ticks_usec() sim.damage_piece(last, 100000) var cascade: int = Time.get_ticks_usec() - started print("integrity: %d pieces; remove one %d us, top floor %d us, full cascade of %d pieces %d us" % [total, one, two, sim.events.size() - 1, cascade]) assert_eq(sim.structures.count(), 0, "with no ground walls left, everything falls") assert_true(one < 12000 and two < 12000, "single removals took %d and %d us" % [one, two]) assert_true(cascade < 12000 * 3, "the full cascade took %d us" % cascade)