extends TestCase ## Props from issue W5: deterministic placement, health, destruction and cost. const SEEDS: Array[int] = [1, 2, 3, 4, 5, 6] func test_client_and_server_place_identical_props() -> void: # The server builds props inside Simulation; a client builds them from the seed alone. for seed_value: int in [4, 77]: var server: Simulation = Simulation.new(seed_value) var island: Island = Island.generate(seed_value) var client: PropSet = PropSet.generate(island, PoiSet.generate(island)) assert_eq(client.count(), server.props.count(), "seed %d count" % seed_value) assert_eq(client.layout_hash(), server.props.layout_hash(), "seed %d layout" % seed_value) var other: PropSet = PropSet.generate(Island.generate(5), PoiSet.generate(Island.generate(5))) assert_ne(other.layout_hash(), Simulation.new(4).props.layout_hash(), "different seeds should differ") func test_every_island_gets_thousands_of_props_of_every_kind() -> void: for seed_value: int in SEEDS: var island: Island = Island.generate(seed_value) var props: PropSet = PropSet.generate(island, PoiSet.generate(island)) assert_true(props.count() >= 2000, "seed %d placed only %d props" % [seed_value, props.count()]) for kind: String in PropSet.KINDS: assert_true(props.count_of(kind) >= 5, "seed %d has %d %s" % [seed_value, props.count_of(kind), kind]) for prop: int in props.count(): assert_eq(props.health[prop], Tuning.PROP_KINDS[props.kind_name(prop)]["health"]) func test_props_sit_on_land_and_keep_clear_of_points_of_interest() -> void: for seed_value: int in [1, 2, 3]: var sim: Simulation = Simulation.new(seed_value) var props: PropSet = sim.props for prop: int in props.count(): var box: AABB = props.boxes[prop] var label: String = "seed %d %s at %s" % [seed_value, props.kind_name(prop), box] var centre: Vector3 = box.get_center() assert_true(sim.island.band_at(centre.x, centre.z) != Island.Band.SEA, label + " is in the sea") for corner: Vector2 in [Vector2(box.position.x, box.position.z), Vector2(box.end.x, box.position.z), Vector2(box.position.x, box.end.z), Vector2(box.end.x, box.end.z)]: var floor_height: float = sim.ground.terrain_height(corner.x, corner.y) assert_true(box.position.y <= floor_height + 0.001, label + " floats over a corner") assert_true(box.end.y > floor_height + 0.3, label + " is buried") for poi: Poi in sim.pois.pois: for solid: AABB in poi.solids: var grown: AABB = solid.grow(Tuning.PROP_POI_CLEARANCE - 0.01) assert_false(grown.position.x < box.end.x and grown.end.x > box.position.x and grown.position.z < box.end.z and grown.end.z > box.position.z, label + " crowds a " + poi.kind) func test_props_block_players_until_destroyed() -> void: var sim: Simulation = Simulation.new(3) var props: PropSet = sim.props var tree: int = _first_open(sim, "pine") if tree < 0: fail("no pine with open ground to its west") return var box: AABB = props.boxes[tree] var start: Vector2 = Vector2(box.position.x - 6.0, box.get_center().z) var id: int = sim.add_player() var player: SimEntity = sim.entities.find(id) player.position = Vector3(start.x, sim.ground.terrain_height(start.x, start.y), start.y) # Walk due east (+x, yaw 90 degrees) into the trunk for three seconds. _walk(sim, id, PI * 0.5, 90) assert_true(player.position.x < box.position.x, "walked through a standing pine to %s" % player.position) assert_true(player.position.x > box.position.x - Tuning.PLAYER_RADIUS - 0.3, "never reached the trunk, stopped at %s" % player.position) var hits: int = 0 var full: int = props.health[tree] while props.is_standing(tree): var destroyed_now: bool = sim.damage_prop(tree, 45) hits += 1 assert_eq(destroyed_now, not props.is_standing(tree)) if hits >= 100: fail("pine never fell") return assert_eq(hits, ceili(float(full) / 45.0)) assert_eq(props.health[tree], 0) assert_eq(props.destroyed, PackedInt32Array([tree])) assert_false(sim.damage_prop(tree, 45), "a fallen pine can't fall again") assert_eq(props.destroyed.size(), 1) _walk(sim, id, PI * 0.5, 90) assert_true(player.position.x > box.end.x + 1.0, "still blocked where the pine stood, at %s" % player.position) func test_damage_is_part_of_the_state_hash_and_deterministic() -> void: var a: Simulation = Simulation.new(8) var b: Simulation = Simulation.new(8) assert_eq(a.state_hash(), b.state_hash()) for prop: int in [3, 40, 41, 900]: a.damage_prop(prop, 30) b.damage_prop(prop, 30) assert_eq(a.state_hash(), b.state_hash(), "same damage, same hash") b.damage_prop(5, 1) assert_ne(a.state_hash(), b.state_hash(), "a single point of prop damage must change the hash") assert_false(a.damage_prop(-1, 10), "unknown props are ignored") assert_false(a.damage_prop(a.props.count(), 10), "unknown props are ignored") assert_false(a.damage_prop(7, 0), "zero damage does nothing") assert_false(a.damage_prop(7, -5), "negative damage does nothing") assert_eq(a.props.health[7], Tuning.PROP_KINDS[a.props.kind_name(7)]["health"]) func test_near_finds_standing_props_nearest_first() -> void: var sim: Simulation = Simulation.new(2) var props: PropSet = sim.props var target: int = props.count() / 2 var at: Vector3 = props.boxes[target].get_center() var found: PackedInt32Array = props.near(at, 3.0) assert_eq(found[0], target) while props.is_standing(target): sim.damage_prop(target, 1000) assert_false(props.near(at, 3.0).has(target), "destroyed props aren't near anything") func test_simulation_with_props_stays_within_tick_budget() -> void: # 24 players walking through the thickest forest: collision against thousands of # boxes must stay cheap. Generous bound; the smoke run reports the real p95. var sim: Simulation = Simulation.new(1) var random: RandomNumberGenerator = RandomNumberGenerator.new() random.seed = 1 var ids: Array[int] = [] var pines: PackedInt32Array = PackedInt32Array() for prop: int in sim.props.count(): if sim.props.kind_name(prop) == "pine": pines.append(prop) for index: int in Tuning.SOLO_PLAYERS: var id: int = sim.add_player() var near: AABB = sim.props.boxes[pines[(index * 97) % pines.size()]] var spot: Vector2 = Vector2(near.position.x - 1.5, near.position.z - 1.5) var player: SimEntity = sim.entities.find(id) player.position = Vector3(spot.x, sim.ground.terrain_height(spot.x, spot.y), spot.y) ids.append(id) var worst: int = 0 var total: int = 0 var ticks: int = 300 for _tick: int in ticks: for id: int in ids: var move: Vector2 = Vector2(random.randf_range(-1.0, 1.0), 1.0) sim.submit_command(id, PlayerCommand.from_input(sim.tick, move, random.randf() * TAU, 0.0, PlayerCommand.BUTTON_SPRINT)) var started: int = Time.get_ticks_usec() sim.step() var spent: int = Time.get_ticks_usec() - started total += spent worst = maxi(worst, spent) print(" props: 24 players in forest, mean tick %d us, worst %d us" % [total / ticks, worst]) assert_true(total / ticks < 6000, "mean tick %d us" % (total / ticks)) func _walk(sim: Simulation, id: int, yaw: float, ticks: int) -> void: for _tick: int in ticks: sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), yaw, 0.0, 0)) sim.step() ## The first standing prop of `kind` with 8 m of open, gentle ground to its west. func _first_open(sim: Simulation, kind: String) -> int: for prop: int in sim.props.count(): if sim.props.kind_name(prop) != kind: continue var box: AABB = sim.props.boxes[prop] var z: float = box.get_center().z var clear: bool = true var x: float = box.position.x - 8.0 while x < box.end.x + 3.0 and clear: # Skip the strip where a body would touch the trunk itself. var reach: float = Tuning.PLAYER_RADIUS + 0.2 if x < box.position.x - reach or x > box.end.x + reach: var feet: Vector3 = Vector3(x, sim.ground.terrain_height(x, z), z) if sim.ground.body_blocked(feet, Tuning.PLAYER_RADIUS + 0.1, Tuning.PLAYER_HEIGHT, Tuning.STEP_HEIGHT) or not sim.ground.walkable_terrain(x, z): clear = false x += 0.25 var rise: float = absf(sim.ground.terrain_height(box.end.x + 3.0, z) - sim.ground.terrain_height(box.position.x - 8.0, z)) if clear and rise < 1.5: return prop return -1