Improved pathfinding

This commit is contained in:
Anuken
2017-05-03 11:52:54 -04:00
parent c374073e6f
commit 0719223146
8 changed files with 50 additions and 31 deletions

BIN
core/assets/maps/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 608 B

View File

@@ -87,7 +87,7 @@ public class Control extends RendererModule{
if(enemies <= 0) if(enemies <= 0)
wavetime -= delta(); wavetime -= delta();
if(wavetime <= 0){ if(wavetime <= 0 || (debug && Inputs.keyUp(Keys.F))){
GameState.runWave(); GameState.runWave();
} }

View File

@@ -175,6 +175,7 @@ public class UI extends SceneModule{
tutorial.content().row(); tutorial.content().row();
tutorial.content().addCheck("Don't show again", b->{ tutorial.content().addCheck("Don't show again", b->{
Settings.putBool("tutorial", !b); Settings.putBool("tutorial", !b);
Settings.save();
}).padTop(4); }).padTop(4);
restart = new Dialog("The core was destroyed.", "dialog"){ restart = new Dialog("The core was destroyed.", "dialog"){

View File

@@ -33,7 +33,7 @@ public class Vars{
public static float breaktime = 0; public static float breaktime = 0;
public static final String[] maps = {"delta", "canyon", "pit"}; public static final String[] maps = {"delta", "canyon", "pit", "test"};
public static Pixmap[] mapPixmaps; public static Pixmap[] mapPixmaps;
public static Texture[] mapTextures; public static Texture[] mapTextures;
public static int worldsize = 128; public static int worldsize = 128;

View File

@@ -9,13 +9,12 @@ import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.Enemy; import io.anuke.mindustry.entities.Enemy;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.Entity;
public class Pathfind{ public class Pathfind{
static MHueristic heuristic = new MHueristic(); static MHueristic heuristic = new MHueristic();
static PassTileGraph passgraph = new PassTileGraph(); static PassTileGraph passgraph = new PassTileGraph();
static IndexedAStarPathFinder<Tile> passpathfinder; static IndexedAStarPathFinder<Tile> passpathfinder;
static Array<DefaultGraphPath<Tile>> paths = new Array<>(); static Array<DefaultGraphPath<Tile>> paths = new Array<>();
static Tile[][] pathSequences;
static Vector2 vector = new Vector2(); static Vector2 vector = new Vector2();
static public Vector2 find(Enemy enemy){ static public Vector2 find(Enemy enemy){
@@ -26,17 +25,17 @@ public class Pathfind{
//-1 is only possible here if both pathfindings failed, which should NOT happen //-1 is only possible here if both pathfindings failed, which should NOT happen
//check graph code //check graph code
DefaultGraphPath<Tile> path = paths.get(enemy.spawn); Tile[] path = enemy.path;
Tile target = path.get(enemy.node); Tile target = path[enemy.node];
float dst = Vector2.dst(enemy.x, enemy.y, target.worldx(), target.worldy()); float dst = Vector2.dst(enemy.x, enemy.y, target.worldx(), target.worldy());
if(dst < 2){ if(dst < 2){
if(enemy.node <= path.getCount()-2) if(enemy.node <= path.length-2)
enemy.node ++; enemy.node ++;
target = path.get(enemy.node); target = path[enemy.node];
} }
@@ -46,45 +45,52 @@ public class Pathfind{
static public void reset(){ static public void reset(){
paths.clear(); paths.clear();
pathSequences = null;
passpathfinder = new IndexedAStarPathFinder<Tile>(passgraph); passpathfinder = new IndexedAStarPathFinder<Tile>(passgraph);
} }
static public void updatePath(){ static public void updatePath(){
if(paths.size == 0){ if(paths.size == 0){
pathSequences = new Tile[3][0];
for(int i = 0; i < spawnpoints.size; i ++){ for(int i = 0; i < spawnpoints.size; i ++){
DefaultGraphPath<Tile> path = new DefaultGraphPath<>(); DefaultGraphPath<Tile> path = new DefaultGraphPath<>();
paths.add(path); paths.add(path);
} }
} }
int i = 0; for(int i = 0; i < paths.size; i ++){
for(DefaultGraphPath<Tile> path : paths){ DefaultGraphPath<Tile> path = paths.get(i);
path.clear(); path.clear();
passpathfinder.searchNodePath( passpathfinder.searchNodePath(
spawnpoints.get(i), spawnpoints.get(i),
core, heuristic, path); core, heuristic, path);
//for(Tile tile : path){ pathSequences[i] = new Tile[path.getCount()];
// Effects.effect("ind", tile.worldx(), tile.worldy());
///} for(int node = 0; node < path.getCount(); node ++){
i++; Tile tile = path.get(node);
}
pathSequences[i][node] = tile;
for(Entity e : Entities.all()){
if(e instanceof Enemy){
findNode((Enemy)e);
} }
/*
for(Tile tile : path){
Effects.effect("ind", tile.worldx(), tile.worldy());
}
*/
} }
} }
static void findNode(Enemy enemy){ static void findNode(Enemy enemy){
DefaultGraphPath<Tile> path = paths.get(enemy.spawn); enemy.path = pathSequences[enemy.spawn];
Tile[] path = enemy.path;
Tile closest = null; Tile closest = null;
float ldst = 0f; float ldst = 0f;
int cindex = -1; int cindex = -1;
for(int i = 0; i < path.getCount(); i ++){ for(int i = 0; i < path.length; i ++){
Tile tile = path.get(i); Tile tile = path[i];
float dst = Vector2.dst(tile.worldx(), tile.worldy(), enemy.x, enemy.y); float dst = Vector2.dst(tile.worldx(), tile.worldy(), enemy.x, enemy.y);
if(closest == null || dst < ldst){ if(closest == null || dst < ldst){

View File

@@ -39,6 +39,13 @@ public class Bullet extends BulletEntity{
super.update(); super.update();
} }
@Override
public boolean collides(SolidEntity other){
if(owner instanceof TileEntity && other instanceof Player)
return false;
return super.collides(other);
}
@Override @Override
public void collision(SolidEntity other){ public void collision(SolidEntity other){
super.collision(other); super.collision(other);

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars; import io.anuke.mindustry.Vars;
import io.anuke.mindustry.World; import io.anuke.mindustry.World;
import io.anuke.mindustry.ai.Pathfind; import io.anuke.mindustry.ai.Pathfind;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Draw; import io.anuke.ucore.core.Draw;
import io.anuke.ucore.core.Effects; import io.anuke.ucore.core.Effects;
import io.anuke.ucore.entities.*; import io.anuke.ucore.entities.*;
@@ -12,6 +13,7 @@ import io.anuke.ucore.util.Timers;
public class Enemy extends DestructibleEntity{ public class Enemy extends DestructibleEntity{
public Vector2 direction = new Vector2(); public Vector2 direction = new Vector2();
public Tile[] path;
public float xvelocity, yvelocity; public float xvelocity, yvelocity;
public float speed = 0.3f; public float speed = 0.3f;
public int node = -1; public int node = -1;
@@ -40,21 +42,22 @@ public class Enemy extends DestructibleEntity{
move(vec.x*delta, vec.y*delta); move(vec.x*delta, vec.y*delta);
//if(Timers.get(this, 10)) if(Timers.get(this, 15)){
target = World.findTileTarget(x, y, null, range, false); target = World.findTileTarget(x, y, null, range, false);
//no tile found //no tile found
if(target == null) if(target == null)
target = Entities.getClosest(x, y, range, e->{ target = Entities.getClosest(x, y, range, e->{
return e instanceof Player; return e instanceof Player;
}); });
}
if(target != null){ if(target != null){
if(Timers.get(this, reload)){ if(Timers.get(hashCode()+"reload", reload)){
shoot(); shoot();
Effects.sound(shootsound, this); Effects.sound(shootsound, this);
} }
} }
} }

View File

@@ -2,6 +2,7 @@ package io.anuke.mindustry.desktop;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import io.anuke.mindustry.Mindustry; import io.anuke.mindustry.Mindustry;
public class DesktopLauncher { public class DesktopLauncher {
@@ -9,6 +10,7 @@ public class DesktopLauncher {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration(); Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setTitle("Mindustry"); config.setTitle("Mindustry");
config.setMaximized(true); config.setMaximized(true);
config.useVsync(false);
new Lwjgl3Application(new Mindustry(), config); new Lwjgl3Application(new Mindustry(), config);
} }
} }