More work on pathfinding
This commit is contained in:
@@ -26,9 +26,10 @@ public class OptimizedPathFinder {
|
||||
private static final byte OPEN = 1;
|
||||
private static final byte CLOSED = 2;
|
||||
|
||||
private static final boolean debug = true;
|
||||
private static final boolean debug = false;
|
||||
|
||||
public static boolean unop = false;
|
||||
public static boolean step = true;
|
||||
|
||||
public OptimizedPathFinder() {
|
||||
this.openList = new BinaryHeap<>();
|
||||
@@ -70,6 +71,22 @@ public class OptimizedPathFinder {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void runStep(Tile startNode, Tile endNode){
|
||||
if(openList.size > 0) {
|
||||
// Retrieve the node with smallest estimated total cost from the open list
|
||||
current = openList.pop();
|
||||
current.category = CLOSED;
|
||||
|
||||
// Terminate if we reached the goal node
|
||||
if (current.node == endNode) return;
|
||||
|
||||
visitChildren(endNode);
|
||||
|
||||
cameFrom = current.node;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean search(PathFinderRequest<Tile> request, long timeToRun) {
|
||||
|
||||
long lastTime = TimeUtils.nanoTime();
|
||||
@@ -234,13 +251,6 @@ public class OptimizedPathFinder {
|
||||
return;
|
||||
}
|
||||
}else{ //moving diagonal
|
||||
Tile sf = scanDir(rel(current, direction), end, direction);
|
||||
|
||||
if(sf != null){
|
||||
cons.accept(sf);
|
||||
return;
|
||||
}
|
||||
|
||||
Tile sl = scanDir(rel(current, Mathf.mod(direction - 1, 8)), end, Mathf.mod(direction - 1, 8));
|
||||
|
||||
if(sl != null){
|
||||
@@ -253,7 +263,12 @@ public class OptimizedPathFinder {
|
||||
cons.accept(sr);
|
||||
}
|
||||
|
||||
Tile sf = scanDir(rel(current, direction), end, direction);
|
||||
|
||||
if(sf != null){
|
||||
cons.accept(sf);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(current == end){
|
||||
@@ -286,6 +301,8 @@ public class OptimizedPathFinder {
|
||||
(obstacle(rel(tile, direction - 3)) && !obstacle(rel(tile, direction - 2)) && !obstacle(rel(tile, direction + 2)))) {
|
||||
if(debug) Effects.effect(Fx.node4, tile.worldx(), tile.worldy());
|
||||
return tile;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Tile next = rel(tile, direction);
|
||||
@@ -302,7 +319,7 @@ public class OptimizedPathFinder {
|
||||
protected boolean obstacle(Tile tile){
|
||||
return tile == null || tile.solid();
|
||||
}
|
||||
;
|
||||
|
||||
protected float estimate(Tile tile, Tile other){
|
||||
return Math.abs(tile.worldx() - other.worldx()) + Math.abs(tile.worldy() - other.worldy());
|
||||
}
|
||||
|
||||
@@ -10,32 +10,33 @@ import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.util.Log;
|
||||
|
||||
public class Pathfinder {
|
||||
private OptimizedPathFinder finder = new OptimizedPathFinder();
|
||||
OptimizedPathFinder find = new OptimizedPathFinder();
|
||||
Tile start, end;
|
||||
|
||||
public Pathfinder(){
|
||||
Events.on(WorldLoadEvent.class, this::clear);
|
||||
}
|
||||
|
||||
public void test(Tile start, Tile end){
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
DefaultGraphPath<Tile> p = new DefaultGraphPath<>();
|
||||
/*
|
||||
OptimizedPathFinder.unop = true;
|
||||
Timers.markNs();
|
||||
finder.searchNodePath(start, end, p);
|
||||
for(Tile tile : p.nodes){
|
||||
Effects.effect(Fx.breakBlock, tile.worldx(), tile.worldy());
|
||||
}
|
||||
Log.info("Normal elapsed: {0}", Timers.elapsedNs());*/
|
||||
|
||||
OptimizedPathFinder.unop = false;
|
||||
Timers.markNs();
|
||||
finder.searchNodePath(start, end, p);
|
||||
for(Tile tile : p.nodes){
|
||||
Effects.effect(Fx.breakBlock, tile.worldx(), tile.worldy());
|
||||
find.searchNodePath(start, end, p);
|
||||
|
||||
for(Tile tile : p){
|
||||
Effects.effect(Fx.node1, tile.worldx(), tile.worldy());
|
||||
}
|
||||
|
||||
Log.info("JSFSAF elapsed: {0}", Timers.elapsedNs());
|
||||
}
|
||||
|
||||
public void step(){
|
||||
find.runStep(start, end);
|
||||
}
|
||||
|
||||
private void clear(){
|
||||
|
||||
}
|
||||
|
||||
@@ -194,6 +194,10 @@ public class DesktopInput extends InputHandler{
|
||||
world.pathfinder().test(world.tileWorld(player.x, player.y), world.tileWorld(Graphics.mouseWorld().x, Graphics.mouseWorld().y));
|
||||
}
|
||||
|
||||
if(Inputs.keyTap(Input.L)){
|
||||
world.pathfinder().step();
|
||||
}
|
||||
|
||||
if(!ui.hasMouse()) {
|
||||
if (showCursor)
|
||||
Cursors.setHand();
|
||||
|
||||
@@ -3,9 +3,9 @@ package io.anuke.mindustry.world;
|
||||
import com.badlogic.gdx.math.GridPoint2;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import io.anuke.mindustry.content.blocks.Blocks;
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.game.Team;
|
||||
import io.anuke.mindustry.content.blocks.Blocks;
|
||||
import io.anuke.mindustry.world.blocks.types.modules.InventoryModule;
|
||||
import io.anuke.mindustry.world.blocks.types.modules.LiquidModule;
|
||||
import io.anuke.mindustry.world.blocks.types.modules.PowerModule;
|
||||
@@ -328,7 +328,7 @@ public class Tile{
|
||||
|
||||
world.notifyChanged(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
Block block = block();
|
||||
|
||||
Reference in New Issue
Block a user