Renaming before uploading new code
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package io.anuke.mindustry.ai;
|
||||
|
||||
import com.badlogic.gdx.ai.pfa.Heuristic;
|
||||
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.TileType;
|
||||
|
||||
public class MHueristic implements Heuristic<Tile>{
|
||||
//so this means that the cost of going through solids is 10x going through non solids
|
||||
float multiplier = 10f;
|
||||
|
||||
@Override
|
||||
public float estimate(Tile node, Tile other){
|
||||
float cost = Math.abs(node.worldx() - other.worldx()) + Math.abs(node.worldy() - other.worldy());
|
||||
|
||||
//TODO balance multiplier
|
||||
if(node.artifical() && node.block().solid) cost += TileType.tilesize*multiplier;
|
||||
if(other.artifical() && other.block().solid) cost += TileType.tilesize*multiplier;
|
||||
return cost;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.anuke.mindustry.ai;
|
||||
|
||||
import com.badlogic.gdx.ai.pfa.Connection;
|
||||
import com.badlogic.gdx.ai.pfa.indexed.IndexedGraph;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
import io.anuke.mindustry.Moment;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
|
||||
/**Tilegraph that ignores player-made tiles.*/
|
||||
public class PassTileGraph implements IndexedGraph<Tile>{
|
||||
private Array<Connection<Tile>> tempConnections = new Array<Connection<Tile>>();
|
||||
|
||||
@Override
|
||||
public Array<Connection<Tile>> getConnections(Tile fromNode){
|
||||
tempConnections.clear();
|
||||
|
||||
if(fromNode.block().solid && !fromNode.block().update)
|
||||
return tempConnections;
|
||||
|
||||
for(Tile tile : fromNode.getNearby()){
|
||||
if(tile != null && (!tile.block().solid || tile.block().update))
|
||||
tempConnections.add(new TileConnection(fromNode, tile));
|
||||
}
|
||||
|
||||
return tempConnections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex(Tile node){
|
||||
return node.x+node.y*Moment.i.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNodeCount(){
|
||||
return Moment.i.size*Moment.i.size;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package io.anuke.mindustry.ai;
|
||||
|
||||
import com.badlogic.gdx.ai.pfa.DefaultGraphPath;
|
||||
import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
import io.anuke.mindustry.Moment;
|
||||
import io.anuke.mindustry.entities.Enemy;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.entities.Entities;
|
||||
import io.anuke.ucore.entities.Entity;
|
||||
|
||||
public class Pathfind{
|
||||
static MHueristic heuristic = new MHueristic();
|
||||
static TileGraph graph = new TileGraph();
|
||||
static PassTileGraph passgraph = new PassTileGraph();
|
||||
static IndexedAStarPathFinder<Tile> pathfinder = new IndexedAStarPathFinder<Tile>(graph);
|
||||
static IndexedAStarPathFinder<Tile> passpathfinder = new IndexedAStarPathFinder<Tile>(passgraph);
|
||||
static Array<DefaultGraphPath<Tile>> paths = new Array<>();
|
||||
static Vector2 vector = new Vector2();
|
||||
|
||||
static public Vector2 find(Enemy enemy){
|
||||
if(enemy.node == -1){
|
||||
findNode(enemy);
|
||||
}
|
||||
|
||||
//-1 is only possible here if both pathfindings failed, which should NOT happen
|
||||
//check graph code
|
||||
|
||||
DefaultGraphPath<Tile> path = paths.get(enemy.spawn);
|
||||
|
||||
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.getCount()-2)
|
||||
enemy.node ++;
|
||||
|
||||
target = path.get(enemy.node);
|
||||
}
|
||||
|
||||
|
||||
return vector.set(target.worldx(), target.worldy());
|
||||
|
||||
}
|
||||
|
||||
static public void reset(){
|
||||
paths.clear();
|
||||
}
|
||||
|
||||
static public void updatePath(){
|
||||
if(paths.size == 0){
|
||||
for(int i = 0; i < Moment.i.spawnpoints.size; i ++){
|
||||
DefaultGraphPath<Tile> path = new DefaultGraphPath<>();
|
||||
paths.add(path);
|
||||
}
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for(DefaultGraphPath<Tile> path : paths){
|
||||
path.clear();
|
||||
passpathfinder.searchNodePath(
|
||||
Moment.i.spawnpoints.get(i),
|
||||
Moment.i.core, heuristic, path);
|
||||
|
||||
//for(Tile tile : path){
|
||||
// Effects.effect("ind", tile.worldx(), tile.worldy());
|
||||
///}
|
||||
i++;
|
||||
}
|
||||
|
||||
for(Entity e : Entities.all()){
|
||||
if(e instanceof Enemy){
|
||||
findNode((Enemy)e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void findNode(Enemy enemy){
|
||||
DefaultGraphPath<Tile> path = paths.get(enemy.spawn);
|
||||
|
||||
Tile closest = null;
|
||||
float ldst = 0f;
|
||||
int cindex = -1;
|
||||
|
||||
for(int i = 0; i < path.getCount(); i ++){
|
||||
Tile tile = path.get(i);
|
||||
float dst = Vector2.dst(tile.worldx(), tile.worldy(), enemy.x, enemy.y);
|
||||
|
||||
if(closest == null || dst < ldst){
|
||||
ldst = dst;
|
||||
closest = tile;
|
||||
cindex = i;
|
||||
}
|
||||
}
|
||||
enemy.node = cindex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.anuke.mindustry.ai;
|
||||
|
||||
import com.badlogic.gdx.ai.pfa.Connection;
|
||||
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
|
||||
public class TileConnection implements Connection{
|
||||
Tile a, b;
|
||||
|
||||
public TileConnection(Tile a, Tile b){
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCost(){
|
||||
return Math.abs(a.worldx() - b.worldx()) + Math.abs(a.worldy() - b.worldy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getFromNode(){
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getToNode(){
|
||||
return b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package io.anuke.mindustry.ai;
|
||||
|
||||
import com.badlogic.gdx.ai.pfa.Connection;
|
||||
import com.badlogic.gdx.ai.pfa.indexed.IndexedGraph;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
import io.anuke.mindustry.Moment;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
|
||||
public class TileGraph implements IndexedGraph<Tile>{
|
||||
private Array<Connection<Tile>> tempConnections = new Array<Connection<Tile>>();
|
||||
|
||||
@Override
|
||||
public Array<Connection<Tile>> getConnections(Tile fromNode){
|
||||
tempConnections.clear();
|
||||
|
||||
if(fromNode.block().solid && fromNode != Moment.i.core)
|
||||
return tempConnections;
|
||||
|
||||
for(Tile tile : fromNode.getNearby()){
|
||||
if(tile != null && (!tile.block().solid || tile == Moment.i.core))
|
||||
tempConnections.add(new TileConnection(fromNode, tile));
|
||||
}
|
||||
return tempConnections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex(Tile node){
|
||||
return node.x+node.y*Moment.i.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNodeCount(){
|
||||
return Moment.i.size*Moment.i.size;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user