From e4d3b6642b5bf44928a5930a9b51b9ea347024cc Mon Sep 17 00:00:00 2001 From: Lotto Date: Sat, 20 Oct 2018 20:46:58 +0200 Subject: [PATCH] puzzlesolver: wait a duration before recomputing the solution --- .../plugins/puzzlesolver/PuzzleSolverOverlay.java | 3 ++- .../plugins/puzzlesolver/solver/PuzzleSolver.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/PuzzleSolverOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/PuzzleSolverOverlay.java index c01e521e1e..37127a8034 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/PuzzleSolverOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/PuzzleSolverOverlay.java @@ -333,7 +333,8 @@ public class PuzzleSolverOverlay extends Overlay } // Solve the puzzle if we don't have an up to date solution - if (solver == null || cachedItems == null || (!shouldCache && !Arrays.equals(cachedItems, itemIds))) + if (solver == null || cachedItems == null + || (!shouldCache && solver.hasExceededWaitDuration() && !Arrays.equals(cachedItems, itemIds))) { solve(itemIds); shouldCache = true; diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/solver/PuzzleSolver.java b/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/solver/PuzzleSolver.java index 9749a7769e..682d7247b4 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/solver/PuzzleSolver.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/puzzlesolver/solver/PuzzleSolver.java @@ -25,6 +25,8 @@ */ package net.runelite.client.plugins.puzzlesolver.solver; +import com.google.common.base.Stopwatch; +import java.time.Duration; import java.util.List; import net.runelite.client.plugins.puzzlesolver.solver.pathfinding.Pathfinder; @@ -32,11 +34,14 @@ public class PuzzleSolver implements Runnable { public static final int DIMENSION = 5; + private static final Duration MAX_WAIT_DURATION = Duration.ofMillis(1500); + private Pathfinder pathfinder; private PuzzleState startState; private List solution; private int position; + private Stopwatch stopwatch; private boolean failed = false; public PuzzleSolver(Pathfinder pathfinder, PuzzleState startState) @@ -70,6 +75,11 @@ public class PuzzleSolver implements Runnable this.position = position; } + public boolean hasExceededWaitDuration() + { + return stopwatch.elapsed().compareTo(MAX_WAIT_DURATION) > 0; + } + public boolean hasFailed() { return failed; @@ -78,6 +88,7 @@ public class PuzzleSolver implements Runnable @Override public void run() { + stopwatch = Stopwatch.createStarted(); solution = pathfinder.computePath(startState); failed = solution == null; }