Implemented different heuristics for different difficulties
This commit is contained in:
@@ -2,12 +2,14 @@ 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.mindustry.world.blocks.types.defense.Turret;
|
||||
import io.anuke.mindustry.world.blocks.types.production.Drill;
|
||||
import io.anuke.mindustry.world.blocks.types.production.Generator;
|
||||
import io.anuke.mindustry.world.blocks.types.production.Pump;
|
||||
import io.anuke.mindustry.world.blocks.types.production.Smelter;
|
||||
import io.anuke.ucore.function.Predicate;
|
||||
|
||||
public class Heuristics {
|
||||
/**How many times more it costs to go through a destructible block than an empty block.*/
|
||||
@@ -36,6 +38,11 @@ public class Heuristics {
|
||||
}
|
||||
|
||||
public static class DestrutiveHeuristic implements Heuristic<Tile> {
|
||||
private final Predicate<Block> frees;
|
||||
|
||||
public DestrutiveHeuristic(Predicate<Block> frees){
|
||||
this.frees = frees;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float estimate(Tile node, Tile other){
|
||||
@@ -51,8 +58,11 @@ public class Heuristics {
|
||||
//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(other.getLinked() != null) other = other.getLinked();
|
||||
if(node.getLinked() != null) node = node.getLinked();
|
||||
|
||||
//generators are free!
|
||||
if(generator(other) || generator(node)) cost = 0;
|
||||
if(frees.test(other.block()) || frees.test(node.block())) cost = 0;
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package io.anuke.mindustry.ai;
|
||||
|
||||
import com.badlogic.gdx.ai.pfa.Heuristic;
|
||||
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.ai.Heuristics.DestrutiveHeuristic;
|
||||
import io.anuke.mindustry.entities.enemies.Enemy;
|
||||
import io.anuke.mindustry.game.SpawnPoint;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
@@ -20,8 +18,6 @@ public class Pathfind{
|
||||
/**Maximum time taken per frame on pathfinding for a single path.*/
|
||||
private static final long maxTime = 1000000 * 5;
|
||||
|
||||
/**Heuristic for determining cost between two tiles*/
|
||||
Heuristic<Tile> heuristic = new DestrutiveHeuristic();
|
||||
/**Tile graph, for determining conenctions between two tiles*/
|
||||
TileGraph graph = new TileGraph();
|
||||
/**Smoother that removes extra nodes from a path.*/
|
||||
@@ -53,38 +49,17 @@ public class Pathfind{
|
||||
enemy.node = -1;
|
||||
return vector.set(enemy.x, enemy.y);
|
||||
}
|
||||
|
||||
//if an enemy is idle for a while, it's probably stuck
|
||||
//if(enemy.idletime > EnemyType.maxIdle){
|
||||
|
||||
//Tile target = path[enemy.node];
|
||||
//if(Vars.world.raycastWorld(enemy.x, enemy.y, target.worldx(), target.worldy()) != null) {
|
||||
// if (enemy.node > 1)
|
||||
// enemy.node = enemy.node - 1;
|
||||
// enemy.idletime = 0;
|
||||
//}
|
||||
|
||||
//else, must be blocked by a playermade block, do nothing
|
||||
//}
|
||||
|
||||
//-1 is only possible here if both pathfindings failed, which should NOT happen
|
||||
//check graph code
|
||||
|
||||
if(enemy.node <= -1){
|
||||
return vector.set(enemy.x, enemy.y);
|
||||
}
|
||||
|
||||
if(enemy.node >= path.length){
|
||||
enemy.node = -1;
|
||||
return vector.set(enemy.x, enemy.y);
|
||||
}
|
||||
|
||||
//TODO documentation on what this does
|
||||
Tile prev = path[enemy.node - 1];
|
||||
|
||||
Tile target = path[enemy.node];
|
||||
|
||||
//a bridge has broken
|
||||
//a bridge has been broken, re-path
|
||||
if(!Vars.world.passable(target.x, target.y)){
|
||||
remakePath();
|
||||
return vector.set(enemy.x, enemy.y);
|
||||
@@ -131,6 +106,7 @@ 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);
|
||||
@@ -169,13 +145,13 @@ public class Pathfind{
|
||||
|
||||
//warmup
|
||||
for(int i = 0; i < 100; i ++){
|
||||
point.finder.searchNodePath(point.start, Vars.control.getCore(), heuristic, point.path);
|
||||
point.finder.searchNodePath(point.start, Vars.control.getCore(), Vars.control.getDifficulty().heuristic, point.path);
|
||||
point.path.clear();
|
||||
}
|
||||
|
||||
Timers.mark();
|
||||
for(int i = 0; i < amount; i ++){
|
||||
point.finder.searchNodePath(point.start, Vars.control.getCore(), heuristic, point.path);
|
||||
point.finder.searchNodePath(point.start, Vars.control.getCore(), Vars.control.getDifficulty().heuristic, point.path);
|
||||
point.path.clear();
|
||||
}
|
||||
UCore.log("Time elapsed: " + Timers.elapsed() + "ms\nAverage MS per path: " + Timers.elapsed()/amount);
|
||||
@@ -190,7 +166,7 @@ public class Pathfind{
|
||||
|
||||
point.pathTiles = null;
|
||||
|
||||
point.request = new PathFinderRequest<>(point.start, Vars.control.getCore(), heuristic, point.path);
|
||||
point.request = new PathFinderRequest<>(point.start, Vars.control.getCore(), Vars.control.getDifficulty().heuristic, point.path);
|
||||
point.request.statusChanged = true; //IMPORTANT!
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user