Broken attempt at per-enemy pathfinding; balancing

This commit is contained in:
Anuken
2017-11-23 20:58:32 -05:00
parent 88e4154ea3
commit b2cd95899c
12 changed files with 93 additions and 54 deletions

View File

@@ -1,49 +1,47 @@
package io.anuke.mindustry.ai;
import com.badlogic.gdx.ai.pfa.PathFinder;
import com.badlogic.gdx.ai.pfa.PathFinderRequest;
import com.badlogic.gdx.ai.pfa.PathSmoother;
import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.effect.Fx;
import io.anuke.mindustry.entities.enemies.Enemy;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.World;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Timers;
public class Pathfind{
static MHueristic heuristic = new MHueristic();
static PassTileGraph passgraph = new PassTileGraph();
static PathFinder<Tile> passpathfinder;
static Array<SmoothGraphPath> paths = new Array<>();
static Tile[][] pathSequences;
static PassTileGraph graph = new PassTileGraph();
static PathFinder<Tile> finder = new IndexedAStarPathFinder<Tile>(graph);
static PathSmoother<Tile, Vector2> smoother = new PathSmoother<Tile, Vector2>(new Raycaster());
static Vector2 vector = new Vector2();
static public Vector2 find(Enemy enemy){
if(enemy.node == -1){
findNode(enemy);
}
findNode(enemy);
if(enemy.node <= -1) return vector.set(enemy.x, enemy.y);
//-1 is only possible here if both pathfindings failed, which should NOT happen
//check graph code
Tile[] path = enemy.path;
//Tile[] path = enemy.path;
Array<Tile> path = enemy.gpath.nodes;
Tile target = path[enemy.node];
Tile target = path.get(enemy.node);
float dst = Vector2.dst(enemy.x, enemy.y, target.worldx(), target.worldy());
if(dst < 2){
if(enemy.node <= path.length-2)
if(enemy.node <= path.size-2)
enemy.node ++;
target = path[enemy.node];
target = path.get(enemy.node);
}
//near the core, stop
if(enemy.node == path.length - 1){
if(enemy.node == path.size - 1){
vector.set(target.worldx(), target.worldy());
}
@@ -51,13 +49,9 @@ public class Pathfind{
}
static public void reset(){
paths.clear();
pathSequences = null;
passpathfinder = new IndexedAStarPathFinder<Tile>(passgraph);
}
static public void updatePath(){
/*
if(paths.size == 0 || paths.size != World.spawnpoints.size){
paths.clear();
pathSequences = new Tile[World.spawnpoints.size][0];
@@ -67,13 +61,6 @@ public class Pathfind{
}
}
//TODO make this work?
/*
PathFinderRequest<Tile> request = new PathFinderRequest<Tile>();
request.startNode = World.spawnpoints.get(0);
request.endNode = World.core;
passpathfinder.search(request, 1000); */
for(int i = 0; i < paths.size; i ++){
SmoothGraphPath path = paths.get(i);
@@ -98,18 +85,47 @@ public class Pathfind{
Effects.effect(Fx.ind, tile.worldx(), tile.worldy());
}
}
}*/
}
static void findNode(Enemy enemy){
/*
enemy.path = pathSequences[enemy.spawn];
Tile[] path = enemy.path;
*/
if(enemy.node == -1 || (Timers.get(enemy, "pathfind", 120) && enemy.request.pathFound)){
enemy.gpath = new SmoothGraphPath();
enemy.finder = new IndexedAStarPathFinder<Tile>(graph);
enemy.gpath.clear();
enemy.request = new PathFinderRequest<Tile>(World.tileWorld(enemy.x, enemy.y),
World.core,
heuristic, enemy.gpath);
enemy.request.statusChanged = true;
enemy.node = -2;
}
if(enemy.gpath != null && !enemy.request.pathFound){
enemy.request.executionFrames ++;
if(enemy.finder.search(enemy.request, 1000000 / 5)){
smoother.smoothPath(enemy.gpath);
enemy.node = 1;
//UCore.log("done in " + enemy.request.executionFrames + " frames with path of size " + enemy.gpath.getCount());
}
}
/*
Tile closest = null;
float ldst = 0f;
int cindex = -1;
for(int i = 0; i < path.length; i ++){
Tile tile = path[i];
for(int i = 0; i < path.size; i ++){
Tile tile = path.get(i);
float dst = Vector2.dst(tile.worldx(), tile.worldy(), enemy.x, enemy.y);
if(closest == null || dst < ldst){
@@ -118,6 +134,6 @@ public class Pathfind{
cindex = i;
}
}
enemy.node = cindex;
enemy.node = cindex;*/
}
}

View File

@@ -12,7 +12,7 @@ public class SmoothGraphPath extends DefaultGraphPath<Tile> implements Smoothabl
@Override
public Vector2 getNodePosition(int index){
Tile tile = nodes.get(index);
return Tmp.v1.set(tile.worldx(), tile.worldy());
return Tmp.v3.set(tile.worldx(), tile.worldy());
}
@Override
@@ -24,5 +24,10 @@ public class SmoothGraphPath extends DefaultGraphPath<Tile> implements Smoothabl
public void truncatePath(int newLength){
nodes.truncate(newLength);
}
@Override
public void add (Tile node) {
nodes.add(node);
}
}