Merge pull request #6106 from devLotto/issue-5325

puzzlesolver: wait a duration before recomputing the solution
This commit is contained in:
Tomas Slusny
2018-11-18 14:05:08 +01:00
committed by GitHub
2 changed files with 13 additions and 1 deletions

View File

@@ -332,7 +332,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;
@@ -33,11 +35,14 @@ public class PuzzleSolver implements Runnable
public static final int DIMENSION = 5;
public static final int BLANK_TILE_VALUE = -1;
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)
@@ -71,6 +76,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;
@@ -79,6 +89,7 @@ public class PuzzleSolver implements Runnable
@Override
public void run()
{
stopwatch = Stopwatch.createStarted();
solution = pathfinder.computePath(startState);
failed = solution == null;
}