From 017a145968114f0a83512fbe0cd42fd9ca3e4d87 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sun, 27 Jun 2021 12:09:53 -0700 Subject: [PATCH 1/2] refactor: rework pathfinding logic from PR: https://github.com/Terasology/Pathfinding/pull/62 --- .../minion/move/FindPathToNode.java | 34 ++++++------------- .../org/terasology/minion/work/WorkBoard.java | 1 + 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/terasology/minion/move/FindPathToNode.java b/src/main/java/org/terasology/minion/move/FindPathToNode.java index 85fb1f2c..09143c10 100644 --- a/src/main/java/org/terasology/minion/move/FindPathToNode.java +++ b/src/main/java/org/terasology/minion/move/FindPathToNode.java @@ -2,10 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 package org.terasology.minion.move; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.MoreExecutors; -import com.google.common.util.concurrent.SettableFuture; import org.joml.Vector3f; import org.terasology.engine.logic.behavior.BehaviorAction; import org.terasology.engine.logic.behavior.core.Actor; @@ -18,8 +14,7 @@ import org.terasology.pathfinding.componentSystem.PathfinderSystem; import org.terasology.pathfinding.model.Path; -import java.util.Arrays; -import java.util.List; +import java.util.Collections; /** * Requests a path to a target defined using the MinionMoveComponent.target.

@@ -61,26 +56,19 @@ public void construct(final Actor actor) { moveComponent.path = Path.INVALID; return; } - SettableFuture> pathFuture = pathfinderSystem.requestPath( + pathfinderSystem.requestPath( actor.getEntity(), currentBlock.getBlockPosition(), - Arrays.asList(workTarget.getBlockPosition())); + Collections.singletonList(workTarget.getBlockPosition())).blockingSubscribe(paths -> { + if (paths == null) { + moveComponent.path = Path.INVALID; + } else if (paths.size() > 0) { + moveComponent.path = paths.get(0); + } + actor.save(moveComponent); + }, throwable -> { - Futures.addCallback(pathFuture, new FutureCallback>() { - @Override - public void onSuccess(List paths) { - if (paths == null) { - moveComponent.path = Path.INVALID; - } else if (paths.size() > 0) { - moveComponent.path = paths.get(0); - } - actor.save(moveComponent); - } + }, () -> moveComponent.path = Path.INVALID); - @Override - public void onFailure(Throwable t) { - moveComponent.path = Path.INVALID; - } - }, MoreExecutors.directExecutor()); } @Override diff --git a/src/main/java/org/terasology/minion/work/WorkBoard.java b/src/main/java/org/terasology/minion/work/WorkBoard.java index 67585ba2..1d442628 100644 --- a/src/main/java/org/terasology/minion/work/WorkBoard.java +++ b/src/main/java/org/terasology/minion/work/WorkBoard.java @@ -7,6 +7,7 @@ import org.joml.Vector3ic; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.terasology.engine.core.GameThread; import org.terasology.engine.entitySystem.entity.EntityManager; import org.terasology.engine.entitySystem.entity.EntityRef; import org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent; From 0148eb9913dd22b88ad33677e58c38e608edd30f Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sat, 3 Jul 2021 22:33:06 -0700 Subject: [PATCH 2/2] chore: update with reactor --- .../minion/move/FindPathToNode.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/terasology/minion/move/FindPathToNode.java b/src/main/java/org/terasology/minion/move/FindPathToNode.java index 09143c10..d0c69417 100644 --- a/src/main/java/org/terasology/minion/move/FindPathToNode.java +++ b/src/main/java/org/terasology/minion/move/FindPathToNode.java @@ -15,6 +15,8 @@ import org.terasology.pathfinding.model.Path; import java.util.Collections; +import java.util.List; +import java.util.Optional; /** * Requests a path to a target defined using the MinionMoveComponent.target.

@@ -56,19 +58,15 @@ public void construct(final Actor actor) { moveComponent.path = Path.INVALID; return; } + moveComponent.path = Path.INVALID; pathfinderSystem.requestPath( actor.getEntity(), currentBlock.getBlockPosition(), - Collections.singletonList(workTarget.getBlockPosition())).blockingSubscribe(paths -> { - if (paths == null) { - moveComponent.path = Path.INVALID; - } else if (paths.size() > 0) { - moveComponent.path = paths.get(0); - } - actor.save(moveComponent); - }, throwable -> { - - }, () -> moveComponent.path = Path.INVALID); - + Collections.singletonList(workTarget.getBlockPosition())).blockOptional().ifPresent(paths -> { + if (paths.size() > 0) { + moveComponent.path = paths.get(0); + } + }); + actor.save(moveComponent); } @Override