Refactored almost every class, somehow didn't break game yet

This commit is contained in:
Anuken
2018-01-27 23:42:42 -05:00
parent 78c8dc4902
commit 35b6b41f24
110 changed files with 1648 additions and 1463 deletions

View File

@@ -1,11 +1,12 @@
package io.anuke.mindustry.ai;
import com.badlogic.gdx.ai.pfa.Heuristic;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.function.Predicate;
import static io.anuke.mindustry.Vars.tilesize;
public class Heuristics {
/**How many times more it costs to go through a destructible block than an empty block.*/
static final float solidMultiplier = 5f;
@@ -23,11 +24,11 @@ public class Heuristics {
//If either one of the tiles is a breakable solid block (that is, it's player-made),
//increase the cost by the tilesize times the solid block multiplier
//Also add the block health, so blocks with more health cost more to traverse
if(node.breakable() && node.block().solid) cost += Vars.tilesize* solidMultiplier + node.block().health;
if(other.breakable() && other.block().solid) cost += Vars.tilesize* solidMultiplier + other.block().health;
if(node.breakable() && node.block().solid) cost += tilesize* solidMultiplier + node.block().health;
if(other.breakable() && other.block().solid) cost += tilesize* solidMultiplier + other.block().health;
//if this block has solid blocks near it, increase the cost, as we don't want enemies hugging walls
if(node.occluded) cost += Vars.tilesize*occludedMultiplier;
if(node.occluded) cost += tilesize*occludedMultiplier;
return cost;
}
@@ -50,11 +51,11 @@ public class Heuristics {
//If either one of the tiles is a breakable solid block (that is, it's player-made),
//increase the cost by the tilesize times the solid block multiplier
//Also add the block health, so blocks with more health cost more to traverse
if(node.breakable() && node.block().solid) cost += Vars.tilesize* solidMultiplier + node.block().health;
if(other.breakable() && other.block().solid) cost += Vars.tilesize* solidMultiplier + other.block().health;
if(node.breakable() && node.block().solid) cost += tilesize* solidMultiplier + node.block().health;
if(other.breakable() && other.block().solid) cost += tilesize* solidMultiplier + other.block().health;
//if this block has solid blocks near it, increase the cost, as we don't want enemies hugging walls
if(node.occluded) cost += Vars.tilesize*occludedMultiplier;
if(node.occluded) cost += tilesize*occludedMultiplier;
if(other.getLinked() != null) other = other.getLinked();
if(node.getLinked() != null) node = node.getLinked();

View File

@@ -4,7 +4,6 @@ import com.badlogic.gdx.ai.pfa.PathFinderRequest;
import com.badlogic.gdx.ai.pfa.PathSmoother;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.enemies.Enemy;
import io.anuke.mindustry.game.SpawnPoint;
import io.anuke.mindustry.world.Tile;
@@ -14,6 +13,8 @@ import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
import static io.anuke.mindustry.Vars.*;
public class Pathfind{
/**Maximum time taken per frame on pathfinding for a single path.*/
private static final long maxTime = 1000000 * 5;
@@ -39,11 +40,11 @@ public class Pathfind{
enemy.node = -1;
}
if(enemy.node < 0 || Vars.control.getSpawnPoints().get(enemy.lane).pathTiles == null){
if(enemy.node < 0 || world.getSpawns().get(enemy.lane).pathTiles == null){
return vector.set(enemy.x, enemy.y);
}
Tile[] path = Vars.control.getSpawnPoints().get(enemy.lane).pathTiles;
Tile[] path = world.getSpawns().get(enemy.lane).pathTiles;
if(enemy.node >= path.length){
enemy.node = -1;
@@ -60,7 +61,7 @@ public class Pathfind{
Tile target = path[enemy.node];
//a bridge has been broken, re-path
if(!Vars.world.passable(target.x, target.y)){
if(!world.passable(target.x, target.y)){
remakePath();
return vector.set(enemy.x, enemy.y);
}
@@ -108,8 +109,8 @@ public class Pathfind{
/**Re-calculate paths for all enemies. Runs when a path changes while moving.*/
private void remakePath(){
for(int i = 0; i < Vars.control.enemyGroup.amount(); i ++){
Enemy enemy = Vars.control.enemyGroup.all().get(i);
for(int i = 0; i < enemyGroup.amount(); i ++){
Enemy enemy = enemyGroup.all().get(i);
enemy.node = -1;
}
@@ -121,7 +122,7 @@ public class Pathfind{
public void update(){
//go through each spawnpoint, and if it's not found a path yet, update it
for(SpawnPoint point : Vars.control.getSpawnPoints()){
for(SpawnPoint point : world.getSpawns()){
if(point.request == null || point.finder == null){
resetPathFor(point);
}
@@ -145,18 +146,18 @@ public class Pathfind{
//1300-1500ms, usually 1400 unoptimized on Caldera
/**Benchmark pathfinding speed. Debugging stuff.*/
public void benchmark(){
SpawnPoint point = Vars.control.getSpawnPoints().first();
SpawnPoint point = world.getSpawns().first();
int amount = 100;
//warmup
for(int i = 0; i < 100; i ++){
point.finder.searchNodePath(point.start, Vars.world.getCore(), Vars.control.getDifficulty().heuristic, point.path);
point.finder.searchNodePath(point.start, world.getCore(), state.difficulty.heuristic, point.path);
point.path.clear();
}
Timers.mark();
for(int i = 0; i < amount; i ++){
point.finder.searchNodePath(point.start, Vars.world.getCore(), Vars.control.getDifficulty().heuristic, point.path);
point.finder.searchNodePath(point.start, world.getCore(), state.difficulty.heuristic, point.path);
point.path.clear();
}
UCore.log("Time elapsed: " + Timers.elapsed() + "ms\nAverage MS per path: " + Timers.elapsed()/amount);
@@ -164,7 +165,7 @@ public class Pathfind{
/**Reset and clear the paths.*/
public void resetPaths(){
for(SpawnPoint point : Vars.control.getSpawnPoints()){
for(SpawnPoint point : world.getSpawns()){
resetPathFor(point);
}
}
@@ -176,21 +177,21 @@ public class Pathfind{
point.pathTiles = null;
point.request = new PathFinderRequest<>(point.start, Vars.world.getCore(), Vars.control.getDifficulty().heuristic, point.path);
point.request = new PathFinderRequest<>(point.start, world.getCore(), state.difficulty.heuristic, point.path);
point.request.statusChanged = true; //IMPORTANT!
}
/**For an enemy that was just loaded from a save, find the node in the path it should be following.*/
void findNode(Enemy enemy){
if(enemy.lane >= Vars.control.getSpawnPoints().size || enemy.lane < 0){
if(enemy.lane >= world.getSpawns().size || enemy.lane < 0){
enemy.lane = 0;
}
if(Vars.control.getSpawnPoints().get(enemy.lane).pathTiles == null){
if(world.getSpawns().get(enemy.lane).pathTiles == null){
return;
}
Tile[] path = Vars.control.getSpawnPoints().get(enemy.lane).pathTiles;
Tile[] path = world.getSpawns().get(enemy.lane).pathTiles;
int closest = findClosest(path, enemy.x, enemy.y);

View File

@@ -5,7 +5,7 @@ import com.badlogic.gdx.ai.utils.Ray;
import com.badlogic.gdx.ai.utils.RaycastCollisionDetector;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import static io.anuke.mindustry.Vars.*;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.util.Geometry;
import io.anuke.ucore.util.Mathf;
@@ -17,7 +17,7 @@ public class Raycaster implements RaycastCollisionDetector<Vector2>{
public boolean collides(Ray<Vector2> ray){
found = false;
Geometry.iterateLine(0f, ray.start.x, ray.start.y, ray.end.x, ray.end.y, Vars.tilesize, (x, y)->{
Geometry.iterateLine(0f, ray.start.x, ray.start.y, ray.end.x, ray.end.y, tilesize, (x, y)->{
if(solid(x, y)){
found = true;
return;
@@ -71,7 +71,7 @@ public class Raycaster implements RaycastCollisionDetector<Vector2>{
}
private boolean solid(float x, float y){
Tile tile = Vars.world.tile(Mathf.scl2(x, Vars.tilesize), Mathf.scl2(y, Vars.tilesize));
Tile tile = world.tile(Mathf.scl2(x, tilesize), Mathf.scl2(y, tilesize));
if(tile == null || tile.solid()) return true;

View File

@@ -2,7 +2,7 @@ package io.anuke.mindustry.ai;
import com.badlogic.gdx.ai.pfa.Connection;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.Vars;
import static io.anuke.mindustry.Vars.*;
import io.anuke.mindustry.world.Tile;
/**Tilegraph that ignores player-made tiles.*/
@@ -31,6 +31,6 @@ public class TileGraph implements OptimizedGraph<Tile> {
@Override
public int getNodeCount(){
return Vars.world.width() * Vars.world.height();
return world.width() * world.height();
}
}