Open History to see the last commit
1import XCTest
2@testable import ExpeditionCore
3
4final class ComputerTraversalTests: XCTestCase {
5 func testAgentUsesDisplayRimWhenOverlappingDialogRoofsBlockTheHook() {
6 let viewport = WorldRect(x: 0, y: 0, width: 1710, height: 1107)
7 let rim = DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 1106, width: 1710, height: 1))
8 let dialog = DesktopWindow(id: 30, frame: .init(x: 415, y: 468, width: 880, height: 448))
9 let behind = DesktopWindow(id: 40, frame: .init(x: 578, y: 162, width: 891, height: 757))
10 let terrain = TerrainSnapshot(windows: [rim, dialog, behind], viewport: viewport)
11 var explorer = ExplorerInteraction(position: .init(x: 1109, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 1686, y: 1031)))
12 explorer.autonomous = true
13 explorer.updateTerrain(terrain)
14 explorer.setKeyboardControl(true)
15
16 let button = WorldPoint(x: 652.5, y: 500)
17 for _ in 0..<60 * 20 {
18 explorer.steerAgent(towardHand: button)
19 explorer.advance(by: 1.0 / 60)
20 }
21 let localX = (button.x - explorer.position.x) * explorer.facing
22 XCTAssertTrue((-8...36).contains(localX))
23 XCTAssertLessThan(hypot(localX - 10, button.y - explorer.position.y - 46), 29,
24 "feet \(explorer.position), state \(explorer.state)")
25 }
26
27 func testPhysicalAutopilotUsesARealRopeAndMakesProgress() {
28 let rim = DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))
29 let terrain = TerrainSnapshot(windows: [rim], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
30 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
31 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
32 var usedRope = false
33 for _ in 0..<120 * 30 {
34 explorer.steerAgent(towardHand: .init(x: 600, y: 450)); explorer.advance(by: 1.0 / 120)
35 usedRope = usedRope || explorer.ropeSession != nil
36 XCTAssertTrue(explorer.position.x.isFinite && explorer.position.y.isFinite)
37 }
38 XCTAssertTrue(usedRope)
39 XCTAssertLessThan(abs(explorer.position.x - 580), 35)
40 XCTAssertLessThan(abs(explorer.position.y - 405), 35)
41 }
42 func testAutopilotCanCrossTheMenuBarAndReturnToTheDock() {
43 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
44 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
45 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
46 for target in [WorldPoint(x: 19, y: 788), .init(x: 260, y: 788), .init(x: 600, y: 35), .init(x: 460, y: 440)] {
47 for _ in 0..<120 * 35 { explorer.steerAgent(towardHand: target); explorer.advance(by: 1.0 / 120); explorer.updateTerrain(terrain) }
48 if target.y > 760 { XCTAssertTrue(explorer.agentCeilingHanging, "Menu-bar targets should finish with a planted top-edge grip") }
49 let localX = (target.x - explorer.position.x) * explorer.facing
50 let localY = target.y - explorer.position.y
51 let shoulderY = 46.0 - (explorer.canCrouch ? explorer.crouchBlend * 28 : 0)
52 XCTAssertLessThan(hypot(localX - 10, localY - shoulderY), 29, "target \(target), feet \(explorer.position), state \(explorer.state)")
53 }
54 }
55
56 func testTopEdgeGripLowersOntoConnectedRopeForAnOpenMenu() {
57 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
58 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
59 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
60 for _ in 0..<120 * 30 { explorer.steerAgent(towardHand: .init(x: 260, y: 788)); explorer.advance(by: 1.0 / 120) }
61 XCTAssertTrue(explorer.agentCeilingHanging)
62 let gripX = explorer.position.x
63 explorer.steerAgent(towardHand: .init(x: gripX + 20, y: 700))
64 XCTAssertFalse(explorer.agentCeilingHanging)
65 XCTAssertEqual(explorer.ropeSession?.anchor.windowID, UInt32.max)
66 XCTAssertEqual(explorer.ropeSession?.anchor.offset ?? -1, gripX, accuracy: 0.1)
67 XCTAssertEqual(explorer.ropeSession?.mode, .descending)
68 for _ in 0..<120 * 3 { explorer.steerAgent(towardHand: .init(x: gripX + 20, y: 700)); explorer.advance(by: 1.0 / 120) }
69 XCTAssertLessThan(explorer.position.y, 690)
70 XCTAssertNotNil(explorer.ropeSession)
71 }
72
73 func testMenuDescentCanPassAWindowRoofAndReachItsControls() {
74 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1)), DesktopWindow(id: 42, frame: .init(x: 200, y: 100, width: 600, height: 400))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
75 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
76 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
77 for target in [WorldPoint(x: 260, y: 788), .init(x: 400, y: 350), .init(x: 600, y: 230)] {
78 for tick in 0..<120 * 45 {
79 explorer.steerAgent(towardHand: target); explorer.advance(by: 1.0 / 120)
80 if tick % 12 == 0 { explorer.updateTerrain(terrain) }
81 }
82 XCTAssertLessThan(abs(explorer.position.x - max(24, target.x - 20)), 35, "x for \(target), \(explorer.position), \(explorer.state)")
83 XCTAssertLessThan(abs(explorer.position.y - min(724, max(0, target.y - 45))), 35, "y for \(target), \(explorer.position), \(explorer.state)")
84 }
85 }
86
87 func testLongFloorTravelWalksBeforeGrapplingUpward() {
88 let terrain = TerrainSnapshot(windows: [DesktopWindow(id: UInt32.max, frame: .init(x: 0, y: 799, width: 1000, height: 1))], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
89 var explorer = ExplorerInteraction(position: .init(x: 100, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
90 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true)
91 for _ in 0..<60 { explorer.steerAgent(towardHand: .init(x: 800, y: 500)); explorer.advance(by: 1.0 / 120) }
92 XCTAssertNil(explorer.ropeSession)
93 XCTAssertGreaterThan(explorer.position.x, 110)
94 XCTAssertTrue([ExplorerInteraction.State.walking, .running].contains(explorer.state))
95 }
96
97 func testLowerPointerDoesNotChangeTheCrouchPose() {
98 let terrain = TerrainSnapshot(windows: [], viewport: .init(x: 0, y: 0, width: 1000, height: 800))
99 var explorer = ExplorerInteraction(position: .init(x: 300, y: 0), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
100 explorer.autonomous = true; explorer.updateTerrain(terrain); explorer.setKeyboardControl(true); explorer.setControlCrouch(true)
101 for _ in 0..<60 { explorer.advance(by: 1.0 / 120) }
102 let blend = explorer.crouchBlend, point = explorer.controlClickPoint
103 explorer.setControlPointerLowered(true)
104 XCTAssertEqual(explorer.crouchBlend, blend)
105 XCTAssertEqual(explorer.controlClickPoint.x, point.x)
106 XCTAssertEqual(explorer.controlClickPoint.y, point.y - 16, accuracy: 0.01)
107 explorer.setControlPointerLowered(false)
108 XCTAssertEqual(explorer.controlClickPoint, point)
109 }
110
111}