# compass_test.ludic - a viewer at (vx, vz) facing yaw with a compass of `tier`, and two providers: # the places (a camp ahead, a sign west, a far legend) and the objective. The tier gate, the order # and the cap, bearings and distances, the tracked mark, a capture, the radar's tiers, the points. import "ludic.compass" import "ludic.base" program CompassTest { numbers float state CompassTestState { vx: float = 0.0 vz: float = 0.0 yaw: float = 0.0 tier: int = 4 objective: bool = true } function fk_x(compass_test_st: CompassTestState) -> float { return compass_test_st.vx } function fk_z(compass_test_st: CompassTestState) -> float { return compass_test_st.vz } function fk_yaw(compass_test_st: CompassTestState) -> float { return compass_test_st.yaw } function fk_tier(compass_test_st: CompassTestState) -> int { return compass_test_st.tier } bind CompassWorld { x: fn fk_x, z: fn fk_z, yaw: fn fk_yaw, tier: fn fk_tier } function places(compass_st: mut CompassState) -> void { compass_mark(compass_st, 0.0, -100.0, 1, 10, 1, COMPASS_PLAIN, "camp") compass_mark(compass_st, -50.0, 0.0, 2, 11, 2, COMPASS_PLAIN, "sign") compass_mark(compass_st, 0.0, 300.0, 3, 12, 4, COMPASS_PLAIN, "legend") compass_mark(compass_st, 10.0, 10.0, 4, 13, 1, COMPASS_NOTE, "my mark") } function goal(compass_st: mut CompassState, compass_test_st: CompassTestState) -> void { if compass_test_st.objective { compass_mark(compass_st, 80.0, 0.0, 5, 14, 2, COMPASS_TRACKED, "the dock") } } def CompassProviders places { gather: fn places } def CompassProviders goal { gather: fn goal } function fresh(compass_st: mut CompassState, compass_test_st: mut CompassTestState) -> void { compass_config(compass_st, 64) compass_config_radar(compass_st, [0.0, 0.0, 0.0, 120.0, 260.0], 2) compass_test_st.vx = 0.0 compass_test_st.vz = 0.0 compass_test_st.yaw = 0.0 compass_test_st.tier = 4 compass_test_st.objective = true } function labels(compass_st: CompassState) -> string { var s = "" for i in 0 .. compass_count(compass_st) { s = s + compass_at(compass_st, i).label + ";" } return s } test "every provider is asked, the tracked mark first, then the player's, then the rest in order" (compass_st: mut CompassState, compass_test_st: mut CompassTestState) { fresh(compass_st, compass_test_st) compass_gather(compass_st) expect_eq(compass_count(compass_st), 5) expect(labels(compass_st) == "the dock;my mark;camp;sign;legend;") expect_eq(compass_tracked(compass_st), 0) } test "a mark shows from its tier up, and without a compass nothing is gathered" (compass_st: mut CompassState, compass_test_st: mut CompassTestState) { fresh(compass_st, compass_test_st) compass_test_st.tier = 1 compass_gather(compass_st) expect(labels(compass_st) == "my mark;camp;") expect_eq(compass_tracked(compass_st), -1) compass_test_st.tier = 0 compass_gather(compass_st) expect_eq(compass_count(compass_st), 0) } test "past the cap the lowest priorities go" (compass_st: mut CompassState, compass_test_st: mut CompassTestState) { fresh(compass_st, compass_test_st) compass_config(compass_st, 2) compass_gather(compass_st) expect(labels(compass_st) == "the dock;my mark;") } test "a bearing is from the viewer's facing, a distance from where it stands" (compass_st: mut CompassState, compass_test_st: mut CompassTestState) { fresh(compass_st, compass_test_st) compass_gather(compass_st) expect_near(compass_bearing(compass_st, 2), 0.0, 0.001) expect_near(compass_dist(compass_st, 2), 100.0, 0.01) expect_near(compass_bearing(compass_st, 3), 1.5708, 0.001) expect_near(compass_bearing(compass_st, 0), -1.5708, 0.001) compass_test_st.yaw = 1.5708 expect_near(compass_bearing(compass_st, 3), 0.0, 0.001) expect_near(compass_bearing(compass_st, 2), -1.5708, 0.001) compass_test_st.vx = -50.0 compass_test_st.vz = 100.0 expect_near(compass_dist(compass_st, 3), 100.0, 0.01) expect_near(compass_wrap(7.0), 7.0 - 6.2832, 0.001) expect_near(compass_strip_t(0.5, 1.0), 0.5, 0.001) } test "a capture hears every mark whatever the tier and leaves the frame's list alone" (compass_st: mut CompassState, compass_test_st: mut CompassTestState) { fresh(compass_st, compass_test_st) compass_gather(compass_st) compass_test_st.tier = 1 compass_capture_begin(compass_st) expect(compass_capturing(compass_st)) goal(compass_st, compass_test_st) places(compass_st) let got = compass_capture_end(compass_st) expect(not compass_capturing(compass_st)) expect_eq(len(got), 5) expect(got[0].label == "the dock") expect_eq(compass_count(compass_st), 5) } test "the radar: its range by tier, the blips in it, and where each falls on the disc" (compass_st: mut CompassState, compass_test_st: mut CompassTestState) { fresh(compass_st, compass_test_st) compass_test_st.tier = 2 compass_gather(compass_st) expect(not compass_radar_on(compass_st)) expect(not compass_radar_has(compass_st, 0)) compass_test_st.tier = 3 compass_gather(compass_st) expect(compass_radar_on(compass_st)) expect_near(compass_radar_range(compass_st), 120.0, 0.01) expect(compass_radar_has(compass_st, 0)) expect(not compass_radar_has(compass_st, 1)) expect(not compass_radar_has(compass_st, 2)) expect(compass_radar_has(compass_st, 3)) expect_near(compass_radar_x(compass_st, 3), -50.0 / 120.0, 0.001) expect_near(compass_radar_y(compass_st, 3), 0.0, 0.001) expect_near(compass_radar_x(compass_st, 0), 80.0 / 120.0, 0.001) compass_test_st.tier = 9 expect_near(compass_radar_range(compass_st), 260.0, 0.01) } test "the eight points go round from the heading 0 toward -x" { expect_eq(compass_octant(0.0, -10.0), 0) expect_eq(compass_octant(-10.0, -10.0), 1) expect_eq(compass_octant(-10.0, 0.0), 2) expect_eq(compass_octant(0.0, 10.0), 4) expect_eq(compass_octant(10.0, 0.0), 6) expect_eq(compass_octant(10.0, -10.0), 7) expect_eq(compass_octant(1.0, -10.0), 0) } }