puzzlesolver: wait a duration before recomputing the solution

This commit is contained in:
Lotto
2018-10-20 20:46:58 +02:00
parent 9622fd6ea2
commit e4d3b6642b
2 changed files with 13 additions and 1 deletions

View File

@@ -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;

View File

@@ -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<PuzzleState> 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;
}