From 07192231462ed178fca56f276ad5c6eae299bcf8 Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 3 May 2017 11:52:54 -0400 Subject: [PATCH] Improved pathfinding --- core/assets/maps/test.png | Bin 0 -> 608 bytes core/src/io/anuke/mindustry/Control.java | 2 +- core/src/io/anuke/mindustry/UI.java | 1 + core/src/io/anuke/mindustry/Vars.java | 2 +- core/src/io/anuke/mindustry/ai/Pathfind.java | 46 ++++++++++-------- .../io/anuke/mindustry/entities/Bullet.java | 7 +++ .../io/anuke/mindustry/entities/Enemy.java | 21 ++++---- .../mindustry/desktop/DesktopLauncher.java | 2 + 8 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 core/assets/maps/test.png diff --git a/core/assets/maps/test.png b/core/assets/maps/test.png new file mode 100644 index 0000000000000000000000000000000000000000..667762131777821d1bef33d186d535418fa0ddb7 GIT binary patch literal 608 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1SEZ8zRdwroCO|{#S9GG!XV7ZFl&wkP>{XE z)7O>#AuBV#v4r}eA5K6a$r9Iy66gHf+|;}h2Ir#G#FEq$h4Rdj3$l6|ooqQFTU!gjF%qSQ{7^6c`vd7#Ns9&c$FdumEjhaA05%V1P0ISYChq z^`9_A^_RPi^7ZTr_RDi=~x9?yv569QOZ7Y{0Ky9H;HeCoKGRyq2Xm^zU3Z=~eYd_$0Rcs^+l{ zu`mCy@Yl}Z46phQTZgZgtsGvizTd)l;tIp#x@32Dh6^lK3=KYv4>YITXXLSI0(t%L z^R2TZ?HerPy7&FjxcF7%iNcis9ESpu_$96+^e4JMO4;J{Pm{xWGh6ZeneP}+T8H`A zAAXeF-F#;An-z<$@~ literal 0 HcmV?d00001 diff --git a/core/src/io/anuke/mindustry/Control.java b/core/src/io/anuke/mindustry/Control.java index 719555b177..cff3c765b7 100644 --- a/core/src/io/anuke/mindustry/Control.java +++ b/core/src/io/anuke/mindustry/Control.java @@ -87,7 +87,7 @@ public class Control extends RendererModule{ if(enemies <= 0) wavetime -= delta(); - if(wavetime <= 0){ + if(wavetime <= 0 || (debug && Inputs.keyUp(Keys.F))){ GameState.runWave(); } diff --git a/core/src/io/anuke/mindustry/UI.java b/core/src/io/anuke/mindustry/UI.java index fae3604a19..5afffd17d9 100644 --- a/core/src/io/anuke/mindustry/UI.java +++ b/core/src/io/anuke/mindustry/UI.java @@ -175,6 +175,7 @@ public class UI extends SceneModule{ tutorial.content().row(); tutorial.content().addCheck("Don't show again", b->{ Settings.putBool("tutorial", !b); + Settings.save(); }).padTop(4); restart = new Dialog("The core was destroyed.", "dialog"){ diff --git a/core/src/io/anuke/mindustry/Vars.java b/core/src/io/anuke/mindustry/Vars.java index aea398704d..0f01f2b0d9 100644 --- a/core/src/io/anuke/mindustry/Vars.java +++ b/core/src/io/anuke/mindustry/Vars.java @@ -33,7 +33,7 @@ public class Vars{ 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 Texture[] mapTextures; public static int worldsize = 128; diff --git a/core/src/io/anuke/mindustry/ai/Pathfind.java b/core/src/io/anuke/mindustry/ai/Pathfind.java index 0c466ef4d4..4ea6f6e578 100644 --- a/core/src/io/anuke/mindustry/ai/Pathfind.java +++ b/core/src/io/anuke/mindustry/ai/Pathfind.java @@ -9,13 +9,12 @@ import com.badlogic.gdx.utils.Array; 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 PassTileGraph passgraph = new PassTileGraph(); static IndexedAStarPathFinder passpathfinder; static Array> paths = new Array<>(); + static Tile[][] pathSequences; static Vector2 vector = new Vector2(); 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 //check graph code - DefaultGraphPath 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()); if(dst < 2){ - if(enemy.node <= path.getCount()-2) + if(enemy.node <= path.length-2) enemy.node ++; - target = path.get(enemy.node); + target = path[enemy.node]; } @@ -46,45 +45,52 @@ public class Pathfind{ static public void reset(){ paths.clear(); + pathSequences = null; passpathfinder = new IndexedAStarPathFinder(passgraph); } static public void updatePath(){ if(paths.size == 0){ + pathSequences = new Tile[3][0]; for(int i = 0; i < spawnpoints.size; i ++){ DefaultGraphPath path = new DefaultGraphPath<>(); paths.add(path); } } - int i = 0; - for(DefaultGraphPath path : paths){ + for(int i = 0; i < paths.size; i ++){ + DefaultGraphPath path = paths.get(i); + path.clear(); passpathfinder.searchNodePath( spawnpoints.get(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); + pathSequences[i] = new Tile[path.getCount()]; + + for(int node = 0; node < path.getCount(); node ++){ + Tile tile = path.get(node); + + pathSequences[i][node] = tile; } + + /* + for(Tile tile : path){ + Effects.effect("ind", tile.worldx(), tile.worldy()); + } + */ } } static void findNode(Enemy enemy){ - DefaultGraphPath path = paths.get(enemy.spawn); + enemy.path = pathSequences[enemy.spawn]; + Tile[] path = enemy.path; Tile closest = null; float ldst = 0f; int cindex = -1; - for(int i = 0; i < path.getCount(); i ++){ - Tile tile = path.get(i); + for(int i = 0; i < path.length; i ++){ + Tile tile = path[i]; float dst = Vector2.dst(tile.worldx(), tile.worldy(), enemy.x, enemy.y); if(closest == null || dst < ldst){ diff --git a/core/src/io/anuke/mindustry/entities/Bullet.java b/core/src/io/anuke/mindustry/entities/Bullet.java index d130a8776e..b81a49569c 100644 --- a/core/src/io/anuke/mindustry/entities/Bullet.java +++ b/core/src/io/anuke/mindustry/entities/Bullet.java @@ -39,6 +39,13 @@ public class Bullet extends BulletEntity{ super.update(); } + @Override + public boolean collides(SolidEntity other){ + if(owner instanceof TileEntity && other instanceof Player) + return false; + return super.collides(other); + } + @Override public void collision(SolidEntity other){ super.collision(other); diff --git a/core/src/io/anuke/mindustry/entities/Enemy.java b/core/src/io/anuke/mindustry/entities/Enemy.java index 26ee0b9315..6404803297 100644 --- a/core/src/io/anuke/mindustry/entities/Enemy.java +++ b/core/src/io/anuke/mindustry/entities/Enemy.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2; import io.anuke.mindustry.Vars; import io.anuke.mindustry.World; import io.anuke.mindustry.ai.Pathfind; +import io.anuke.mindustry.world.Tile; import io.anuke.ucore.core.Draw; import io.anuke.ucore.core.Effects; import io.anuke.ucore.entities.*; @@ -12,6 +13,7 @@ import io.anuke.ucore.util.Timers; public class Enemy extends DestructibleEntity{ public Vector2 direction = new Vector2(); + public Tile[] path; public float xvelocity, yvelocity; public float speed = 0.3f; public int node = -1; @@ -40,21 +42,22 @@ public class Enemy extends DestructibleEntity{ move(vec.x*delta, vec.y*delta); - //if(Timers.get(this, 10)) - target = World.findTileTarget(x, y, null, range, false); + if(Timers.get(this, 15)){ + target = World.findTileTarget(x, y, null, range, false); - //no tile found - if(target == null) - target = Entities.getClosest(x, y, range, e->{ - return e instanceof Player; - }); + //no tile found + if(target == null) + target = Entities.getClosest(x, y, range, e->{ + return e instanceof Player; + }); + + } if(target != null){ - if(Timers.get(this, reload)){ + if(Timers.get(hashCode()+"reload", reload)){ shoot(); Effects.sound(shootsound, this); } - } } diff --git a/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java b/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java index 0c416136e6..38528b0810 100644 --- a/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java +++ b/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java @@ -2,6 +2,7 @@ package io.anuke.mindustry.desktop; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration; + import io.anuke.mindustry.Mindustry; public class DesktopLauncher { @@ -9,6 +10,7 @@ public class DesktopLauncher { Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration(); config.setTitle("Mindustry"); config.setMaximized(true); + config.useVsync(false); new Lwjgl3Application(new Mindustry(), config); } }