More work on pathfinding
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#Autogenerated file. Do not modify.
|
#Autogenerated file. Do not modify.
|
||||||
#Fri Apr 20 19:54:59 EDT 2018
|
#Sat Apr 21 01:07:37 EDT 2018
|
||||||
version=release
|
version=release
|
||||||
androidBuildCode=1076
|
androidBuildCode=1077
|
||||||
name=Mindustry
|
name=Mindustry
|
||||||
code=3.5
|
code=3.5
|
||||||
build=custom build
|
build=custom build
|
||||||
|
|||||||
@@ -26,9 +26,10 @@ public class OptimizedPathFinder {
|
|||||||
private static final byte OPEN = 1;
|
private static final byte OPEN = 1;
|
||||||
private static final byte CLOSED = 2;
|
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 unop = false;
|
||||||
|
public static boolean step = true;
|
||||||
|
|
||||||
public OptimizedPathFinder() {
|
public OptimizedPathFinder() {
|
||||||
this.openList = new BinaryHeap<>();
|
this.openList = new BinaryHeap<>();
|
||||||
@@ -70,6 +71,22 @@ public class OptimizedPathFinder {
|
|||||||
return false;
|
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) {
|
public boolean search(PathFinderRequest<Tile> request, long timeToRun) {
|
||||||
|
|
||||||
long lastTime = TimeUtils.nanoTime();
|
long lastTime = TimeUtils.nanoTime();
|
||||||
@@ -234,13 +251,6 @@ public class OptimizedPathFinder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}else{ //moving diagonal
|
}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));
|
Tile sl = scanDir(rel(current, Mathf.mod(direction - 1, 8)), end, Mathf.mod(direction - 1, 8));
|
||||||
|
|
||||||
if(sl != null){
|
if(sl != null){
|
||||||
@@ -253,7 +263,12 @@ public class OptimizedPathFinder {
|
|||||||
cons.accept(sr);
|
cons.accept(sr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Tile sf = scanDir(rel(current, direction), end, direction);
|
||||||
|
|
||||||
|
if(sf != null){
|
||||||
|
cons.accept(sf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(current == end){
|
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)))) {
|
(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());
|
if(debug) Effects.effect(Fx.node4, tile.worldx(), tile.worldy());
|
||||||
return tile;
|
return tile;
|
||||||
|
}else{
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tile next = rel(tile, direction);
|
Tile next = rel(tile, direction);
|
||||||
@@ -302,7 +319,7 @@ public class OptimizedPathFinder {
|
|||||||
protected boolean obstacle(Tile tile){
|
protected boolean obstacle(Tile tile){
|
||||||
return tile == null || tile.solid();
|
return tile == null || tile.solid();
|
||||||
}
|
}
|
||||||
;
|
|
||||||
protected float estimate(Tile tile, Tile other){
|
protected float estimate(Tile tile, Tile other){
|
||||||
return Math.abs(tile.worldx() - other.worldx()) + Math.abs(tile.worldy() - other.worldy());
|
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;
|
import io.anuke.ucore.util.Log;
|
||||||
|
|
||||||
public class Pathfinder {
|
public class Pathfinder {
|
||||||
private OptimizedPathFinder finder = new OptimizedPathFinder();
|
OptimizedPathFinder find = new OptimizedPathFinder();
|
||||||
|
Tile start, end;
|
||||||
|
|
||||||
public Pathfinder(){
|
public Pathfinder(){
|
||||||
Events.on(WorldLoadEvent.class, this::clear);
|
Events.on(WorldLoadEvent.class, this::clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test(Tile start, Tile end){
|
public void test(Tile start, Tile end){
|
||||||
|
this.start = start;
|
||||||
|
this.end = end;
|
||||||
DefaultGraphPath<Tile> p = new DefaultGraphPath<>();
|
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;
|
OptimizedPathFinder.unop = false;
|
||||||
Timers.markNs();
|
Timers.markNs();
|
||||||
finder.searchNodePath(start, end, p);
|
find.searchNodePath(start, end, p);
|
||||||
for(Tile tile : p.nodes){
|
|
||||||
Effects.effect(Fx.breakBlock, tile.worldx(), tile.worldy());
|
for(Tile tile : p){
|
||||||
|
Effects.effect(Fx.node1, tile.worldx(), tile.worldy());
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.info("JSFSAF elapsed: {0}", Timers.elapsedNs());
|
Log.info("JSFSAF elapsed: {0}", Timers.elapsedNs());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void step(){
|
||||||
|
find.runStep(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
private void clear(){
|
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));
|
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(!ui.hasMouse()) {
|
||||||
if (showCursor)
|
if (showCursor)
|
||||||
Cursors.setHand();
|
Cursors.setHand();
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package io.anuke.mindustry.world;
|
|||||||
import com.badlogic.gdx.math.GridPoint2;
|
import com.badlogic.gdx.math.GridPoint2;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||||
|
import io.anuke.mindustry.content.blocks.Blocks;
|
||||||
import io.anuke.mindustry.entities.TileEntity;
|
import io.anuke.mindustry.entities.TileEntity;
|
||||||
import io.anuke.mindustry.game.Team;
|
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.InventoryModule;
|
||||||
import io.anuke.mindustry.world.blocks.types.modules.LiquidModule;
|
import io.anuke.mindustry.world.blocks.types.modules.LiquidModule;
|
||||||
import io.anuke.mindustry.world.blocks.types.modules.PowerModule;
|
import io.anuke.mindustry.world.blocks.types.modules.PowerModule;
|
||||||
|
|||||||
Reference in New Issue
Block a user