Skip to content

Commit 697a4b3

Browse files
committed
Update Tactics and Goals path tolerance
1 parent 6ed1551 commit 697a4b3

File tree

8 files changed

+100
-85
lines changed

8 files changed

+100
-85
lines changed

JvmClient/src/jvmMain/kotlin/spaceEngineers/iv4xr/goal/GoalBuilder.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package spaceEngineers.iv4xr.goal
22

33
import environments.SeAgentState
44
import eu.iv4xr.framework.spatial.Vec3
5+
import nl.uu.cs.aplib.AplibEDSL.SEQ
56
import nl.uu.cs.aplib.mainConcepts.Goal
67
import nl.uu.cs.aplib.mainConcepts.GoalStructure
78
import nl.uu.cs.aplib.mainConcepts.Tactic
@@ -286,7 +287,7 @@ class GoalBuilder(
286287

287288
fun navigateNearToBlock(
288289
blockType: String,
289-
distance: Float,
290+
goalDistance: Float = 3f,
290291
tactic: Tactic = tactics.doNothing()
291292
): GoalStructure.PrimitiveGoal {
292293
val goal =
@@ -297,7 +298,7 @@ class GoalBuilder(
297298
}
298299
val blockPosition = foundBlock?.position
299300
if (blockPosition != null) {
300-
val agentIsNearBlock = belief.seEnv.controller.observer.distanceTo(blockPosition) < distance
301+
val agentIsNearBlock = belief.seEnv.controller.observer.distanceTo(blockPosition) < goalDistance
301302
if (agentIsNearBlock) {
302303
// Agent is near the block
303304
return@toSolve true
@@ -311,4 +312,14 @@ class GoalBuilder(
311312
)
312313
return goal.lift()
313314
}
315+
316+
fun navigateAimToBlock(
317+
blockType: String,
318+
goalDistance: Float = 3f,
319+
tactic: Tactic = tactics.doNothing()
320+
): GoalStructure {
321+
val navigateBlockGoal = navigateNearToBlock(blockType, goalDistance, tactic)
322+
val aimBlockGoal = aimToBlock(blockType, tactic)
323+
return SEQ(navigateBlockGoal, aimBlockGoal)
324+
}
314325
}

JvmClient/src/jvmMain/kotlin/spaceEngineers/iv4xr/goal/TacticLib.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import nl.uu.cs.aplib.AplibEDSL
66
import nl.uu.cs.aplib.mainConcepts.Tactic
77
import spaceEngineers.controller.extensions.distanceTo
88
import spaceEngineers.iv4xr.navigation.NavigableSystem
9-
import spaceEngineers.model.Block
109
import spaceEngineers.model.ToolbarLocation
1110
import spaceEngineers.model.Vec3F
1211

@@ -137,30 +136,30 @@ class TacticLib {
137136
* Navigation tactic on flat plane or ground levels.
138137
* NavigableSystem calculates and follows a path with a fixed Y-axis.
139138
*/
140-
fun groundedNavigationNearToBlock(
139+
fun navigateToBlock(
141140
desiredBlock: String,
142141
closestDistance: Float,
143-
blockPosition: (Block) -> Vec3F = { it.position }
142+
distancePathTolerance: Float = 1.2f
144143
): Tactic {
145-
return AplibEDSL.action("groundedNavigationNearToBlock($desiredBlock)").do1 { belief: SeAgentState ->
144+
return AplibEDSL.action("navigateToBlock($desiredBlock)").do1 { belief: SeAgentState ->
146145
belief.apply {
147146
val navigableSystem = NavigableSystem(seEnv.controller, seEnv.controller.observer)
148-
val blockPosition = navigableSystem.setDesiredBlockPosition(desiredBlock, blockPosition)
147+
val blockPosition = navigableSystem.setDesiredBlockPosition(desiredBlock)
149148

150-
println("groundedNavigationNearToBlock($desiredBlock): agentPosition is (${seEnv.controller.observer.observe().position})")
149+
println("navigateToBlock($desiredBlock): agentPosition is (${seEnv.controller.observer.observe().position})")
151150

152151
if (blockPosition == Vec3F(0, 0, 0)) {
153-
println("groundedNavigationNearToBlock($desiredBlock): blockPosition not found")
152+
println("navigateToBlock($desiredBlock): blockPosition not found")
154153
doNothing()
155154
} else {
156-
println("groundedNavigationNearToBlock($desiredBlock): blockPosition is ($blockPosition)")
157-
println("groundedNavigationNearToBlock($desiredBlock): distance is (${seEnv.controller.observer.distanceTo(blockPosition)})")
155+
println("navigateToBlock($desiredBlock): blockPosition is ($blockPosition)")
156+
println("navigateToBlock($desiredBlock): distance is (${seEnv.controller.observer.distanceTo(blockPosition)})")
158157

159158
val navigableGraph = navigableSystem.getNavigableGraph()
160159
val navigablePath = navigableSystem.getClosestPathToDesiredBlock(closestDistance)
161160

162161
runBlocking {
163-
navigableSystem.navigateGroundedPath(navigableGraph, navigablePath)
162+
navigableSystem.navigatePath(navigableGraph, navigablePath, distancePathTolerance)
164163
}
165164
}
166165
}
@@ -173,7 +172,7 @@ class TacticLib {
173172
*/
174173
fun dynamicNavigationNearToBlock(
175174
desiredBlock: String,
176-
closestDistance: Float
175+
distancePathTolerance: Float = 1.2f,
177176
): Tactic {
178177
return AplibEDSL.action("dynamicNavigationNearToBlock($desiredBlock)").do1 { belief: SeAgentState ->
179178
belief.apply {
@@ -190,7 +189,7 @@ class TacticLib {
190189
println("dynamicNavigationNearToBlock($desiredBlock): distance is (${seEnv.controller.observer.distanceTo(blockPosition)})")
191190

192191
runBlocking {
193-
navigableSystem.navigateDynamicPath(navigableSystem, closestDistance)
192+
navigableSystem.navigateDynamicPath(navigableSystem, distancePathTolerance)
194193
}
195194
}
196195
}

JvmClient/src/jvmMain/kotlin/spaceEngineers/iv4xr/navigation/NavigableSystem.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package spaceEngineers.iv4xr.navigation
33
import eu.iv4xr.framework.extensions.pathfinding.AStar
44
import spaceEngineers.controller.Observer
55
import spaceEngineers.controller.SpaceEngineers
6-
import spaceEngineers.model.Block
76
import spaceEngineers.model.CharacterMovementType
87
import spaceEngineers.model.Vec3F
98
import spaceEngineers.model.extensions.allBlocks
@@ -32,21 +31,23 @@ class NavigableSystem(
3231
* We need to find the closest nav node related to the desired block position.
3332
*/
3433
fun setDesiredBlockPosition(
35-
desiredBlock: String,
36-
blockPosition: (Block) -> Vec3F = { it.position }
34+
desiredBlock: String
3735
): Vec3F {
36+
// Reset position values
37+
desiredBlockPosition = Vec3F(0, 0, 0)
38+
3839
for (block in observer.observeBlocks().allBlocks) {
3940
if (desiredBlock in block.definitionId.toString()) {
40-
desiredBlockPosition = blockPosition(block)
41+
desiredBlockPosition = block.position
4142
return desiredBlockPosition
4243
}
4344
}
44-
desiredBlockPosition = Vec3F(0, 0, 0)
45+
4546
return desiredBlockPosition
4647
}
4748

4849
fun getClosestPathToDesiredBlock(
49-
closestDistance: Float
50+
closestDistance: Float = 9f
5051
): List<NodeId> {
5152
var reachablePosition = Vec3F(0, 0, 0)
5253
var reachableNode = ""
@@ -92,9 +93,10 @@ class NavigableSystem(
9293
return (desiredBlock in observer.observe().targetBlock?.definitionId.toString())
9394
}
9495

95-
suspend fun navigateGroundedPath(
96+
suspend fun navigatePath(
9697
navigableGraph: NavigableGraph,
97-
navigablePath: List<NodeId>
98+
navigablePath: List<NodeId>,
99+
distancePathTolerance: Float = 1.2f
98100
) {
99101
val navigator = CharacterNavigation(spaceEngineers = spaceEngineers, pathFinder = Iv4XRAStarPathFinder())
100102

@@ -105,22 +107,32 @@ class NavigableSystem(
105107

106108
for (nodeId in navigablePath) {
107109
println("Next navigatePath node: ${navigableGraph.node(nodeId).data}")
108-
navigator.moveInLine(navigableGraph.node(nodeId).data, movementType = CharacterMovementType.RUN, timeout = 10.seconds)
110+
navigator.moveInLine(
111+
navigableGraph.node(nodeId).data,
112+
movementType = CharacterMovementType.RUN,
113+
distanceTolerance = distancePathTolerance,
114+
timeout = 10.seconds
115+
)
109116
}
110117
}
111118

112119
suspend fun navigateDynamicPath(
113120
navigableSystem: NavigableSystem,
114-
closestDistance: Float
121+
distanceTolerance: Float
115122
) {
116123
val navigableGraph = navigableSystem.getNavigableGraph()
117-
val navigablePath = navigableSystem.getClosestPathToDesiredBlock(closestDistance)
124+
val navigablePath = navigableSystem.getClosestPathToDesiredBlock(distanceTolerance)
118125

119126
val navigator = CharacterNavigation(spaceEngineers = spaceEngineers, pathFinder = Iv4XRAStarPathFinder())
120127

121128
val firstNavigableNode = navigablePath.first()
122129

123130
println("Next navigateDynamicPath node: ${navigableGraph.node(firstNavigableNode).data}")
124-
navigator.moveInLine(navigableGraph.node(firstNavigableNode).data, movementType = CharacterMovementType.RUN, timeout = 10.seconds)
131+
navigator.moveInLine(
132+
navigableGraph.node(firstNavigableNode).data,
133+
movementType = CharacterMovementType.RUN,
134+
distanceTolerance = distanceTolerance,
135+
timeout = 10.seconds
136+
)
125137
}
126138
}

JvmClient/src/jvmTest/kotlin/spaceEngineers/iv4xr/GoalPlatformTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class GoalPlatformTest {
6565
goals.navigateNearToBlock(
6666
cryoChamber.first,
6767
cryoChamber.second,
68-
tactic = tactics.groundedNavigationNearToBlock(cryoChamber.first, cryoChamber.second),
68+
tactic = tactics.navigateToBlock(cryoChamber.first, cryoChamber.second),
6969
),
7070
goals.aimToBlock(
7171
cryoChamber.first,
@@ -87,7 +87,7 @@ class GoalPlatformTest {
8787
tactic = AplibEDSL.SEQ(
8888
tactics.equip(grinderLocation),
8989
tactics.sleep(500),
90-
tactics.groundedNavigationNearToBlock(gravityBlock.first, gravityBlock.second)
90+
tactics.navigateToBlock(gravityBlock.first, gravityBlock.second)
9191
)
9292
),
9393
goals.aimToBlock(
@@ -118,7 +118,7 @@ class GoalPlatformTest {
118118
goals.navigateNearToBlock(
119119
survivalKit.first,
120120
survivalKit.second,
121-
tactic = tactics.groundedNavigationNearToBlock(survivalKit.first, survivalKit.second),
121+
tactic = tactics.navigateToBlock(survivalKit.first, survivalKit.second),
122122
),
123123
goals.aimToBlock(
124124
survivalKit.first,
@@ -154,7 +154,7 @@ class GoalPlatformTest {
154154

155155
// Run the test agent to accomplish the attached Goal
156156
var i = 0
157-
while (goalStructure.status.inProgress() && i <= 20) {
157+
while (goalStructure.status.inProgress() && i <= 200) {
158158
testAgent.update()
159159
println("*** $i, ${myAgentState.worldmodel.agentId} @${myAgentState.worldmodel.position}")
160160
i++

JvmClient/src/jvmTest/kotlin/spaceEngineers/iv4xr/LoneSurvivorMedicalRoomTest.kt

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package spaceEngineers.iv4xr
33
import environments.SeAgentState
44
import environments.SeEnvironment
55
import eu.iv4xr.framework.mainConcepts.TestAgent
6-
import eu.iv4xr.framework.mainConcepts.TestDataCollector
76
import nl.uu.cs.aplib.AplibEDSL.SEQ
87
import nl.uu.cs.aplib.mainConcepts.GoalStructure
98
import org.junit.jupiter.api.Disabled
@@ -20,7 +19,7 @@ class LoneSurvivorMedicalRoomTest {
2019

2120
@Disabled
2221
@Test
23-
fun test_health_navigation_to_medical() {
22+
fun test_navigate_medical_room_and_restore_health() {
2423
val agentId = SpaceEngineers.DEFAULT_AGENT_ID
2524
val context = SpaceEngineersTestContext()
2625

@@ -32,60 +31,57 @@ class LoneSurvivorMedicalRoomTest {
3231
)
3332

3433
// Create iv4xr environment and pass ID of the world (scenario to load).
35-
val theEnv = SeEnvironment(
34+
val seEnv = SeEnvironment(
3635
controller = controllerWrapper,
3736
worldId = "LoneSurvivor_SpaceShip"
3837
)
3938

4039
// Create the iv4XR test agent
41-
val myAgentState = SeAgentState(agentId = agentId)
42-
val dataCollector = TestDataCollector()
43-
val testAgent = TestAgent(agentId, "goal solving agent")
44-
.attachState(myAgentState)
45-
.attachEnvironment(theEnv)
46-
.setTestDataCollector(dataCollector)
40+
val seAgentState = SeAgentState(agentId = agentId)
41+
val testAgent = TestAgent(agentId, "long play goal solving agent")
42+
.attachState(seAgentState)
43+
.attachEnvironment(seEnv)
4744

48-
// Create the desired testing goals and tactics.
49-
val goals = GoalBuilder()
50-
val tactics = TacticLib()
45+
val navigateAimToBlock = GoalBuilder().navigateAimToBlock(
46+
"MedicalRoom",
47+
5f, // Maximum distance allowed
48+
tactic = SEQ(
49+
TacticLib().navigateToBlock("MedicalRoom", 5f, 3f),
50+
TacticLib().rotateToBlock("MedicalRoom")
51+
)
52+
)
53+
54+
val reduceHealthBelow = GoalBuilder().agentHealthIsBelow(
55+
90.00, // Maximum health allowed
56+
tactic = SEQ(
57+
TacticLib().setHelmet(false, 5000),
58+
TacticLib().setHelmet(true)
59+
)
60+
)
5161

52-
val medical = Pair("Medical", 7f)
62+
val restoreHealthAbove = GoalBuilder().agentHealthIsAbove(
63+
99.99, // Minimum health allowed
64+
tactic = TacticLib().continuousUse(5000)
65+
)
5366

5467
val goalStructure: GoalStructure = SEQ(
55-
// Navigate Interact with the medical room
56-
goals.navigateNearToBlock(
57-
medical.first,
58-
medical.second,
59-
tactic = tactics.groundedNavigationNearToBlock(medical.first, medical.second) { it.maxPosition },
60-
),
61-
goals.aimToBlock(
62-
medical.first,
63-
tactic = tactics.rotateToBlock(medical.first)
64-
),
65-
goals.agentHealthIsBelow(
66-
90.00,
67-
tactic = tactics.setHelmet(false, 5000)
68-
),
69-
goals.agentHealthIsAbove(
70-
99.99,
71-
tactic = SEQ(
72-
tactics.setHelmet(true),
73-
tactics.continuousUse(5000, 1000)
74-
)
75-
)
68+
navigateAimToBlock,
69+
reduceHealthBelow,
70+
restoreHealthAbove
7671
)
72+
7773
testAgent.setGoal(goalStructure)
7874

7975
// Load the scenario.
80-
theEnv.loadWorld()
81-
theEnv.controller.screens.waitUntilTheGameLoaded()
76+
seEnv.loadWorld()
77+
seEnv.controller.screens.waitUntilTheGameLoaded()
8278
Thread.sleep(500)
8379

8480
// Run the test agent to accomplish the attached Goal
8581
var i = 0
8682
while (goalStructure.status.inProgress() && i <= 10) {
8783
testAgent.update()
88-
println("*** $i, ${myAgentState.worldmodel.agentId} @${myAgentState.worldmodel.position}")
84+
println("*** $i, ${seAgentState.worldmodel.agentId} @${seAgentState.worldmodel.position}")
8985
i++
9086
}
9187

@@ -94,7 +90,7 @@ class LoneSurvivorMedicalRoomTest {
9490
assertTrue { goalStructure.status.success() }
9591

9692
// Exit the scenario
97-
theEnv.exitLevelWithoutSaving()
98-
theEnv.close()
93+
seEnv.exitLevelWithoutSaving()
94+
seEnv.close()
9995
}
10096
}

JvmClient/src/jvmTest/kotlin/spaceEngineers/iv4xr/LoneSurvivorSpaceShipTest.kt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,30 @@ class LoneSurvivorSpaceShipTest {
4949
val goals = GoalBuilder()
5050
val tactics = TacticLib()
5151

52-
val door = Pair("Door", 3.5f)
53-
val cockpit = Pair("Cockpit", 3f)
54-
5552
val goalStructure: GoalStructure = SEQ(
5653
// Navigate Interact with the door
5754
goals.navigateNearToBlock(
58-
door.first,
59-
door.second,
60-
tactic = tactics.groundedNavigationNearToBlock(door.first, door.second),
55+
"Door",
56+
3.5f,
57+
tactic = tactics.navigateToBlock("Door", 3.5f),
6158
),
6259
goals.aimToBlock(
63-
door.first,
64-
tactic = tactics.rotateToBlock(door.first)
60+
"Door",
61+
tactic = tactics.rotateToBlock("Door")
6562
),
6663
goals.agentEnergyIsBelow(
6764
98.00,
6865
tactic = tactics.use(2000)
6966
),
7067
// Navigate Interact with the space-ship cockpit
7168
goals.navigateNearToBlock(
72-
cockpit.first,
73-
cockpit.second,
74-
tactic = tactics.groundedNavigationNearToBlock(cockpit.first, cockpit.second),
69+
"Cockpit",
70+
3f,
71+
tactic = tactics.navigateToBlock("Cockpit", 3f),
7572
),
7673
goals.aimToBlock(
77-
cockpit.first,
78-
tactic = tactics.rotateToBlock(cockpit.first)
74+
"Cockpit",
75+
tactic = tactics.rotateToBlock("Cockpit")
7976
),
8077
goals.agentEnergyIsAbove(
8178
99.99,

0 commit comments

Comments
 (0)