Proper pathfinding fixes

This commit is contained in:
Anuken
2022-02-17 15:15:51 -05:00
parent ba475b681c
commit 49a39d42e7
5 changed files with 65 additions and 10 deletions

View File

@@ -298,13 +298,27 @@ public class ControlPathfinder{
if(avoid(type, x + y * wwidth)) return true;
if(x == x2 && y == y2) return false;
//no diagonals
//TODO no diagonals???? is this a good idea?
/*
//no diagonal ver
if(2 * err + dy > dx - 2 * err){
err -= dy;
x += sx;
}else{
err += dx;
y += sy;
}*/
//diagonal ver
e2 = 2 * err;
if(e2 > -dy){
err -= dy;
x += sx;
}
if(e2 < dx){
err += dx;
y += sy;
}
}

View File

@@ -92,7 +92,7 @@ public class Pathfinder implements Runnable{
for(int i = 0; i < tiles.length; i++){
Tile tile = world.tiles.geti(i);
tiles[i] = packTile(tile);
tiles[i] = packTile(tile, 0);
}
preloadPath(getField(state.rules.waveTeam, costGround, fieldCore));
@@ -108,6 +108,34 @@ public class Pathfinder implements Runnable{
Events.on(ResetEvent.class, event -> stop());
Events.on(TileChangeEvent.class, event -> updateTile(event.tile));
//remove nearSolid flag for tiles
Events.on(TilePreChangeEvent.class, event -> {
Tile tile = event.tile;
if(tile.solid()){
for(int i = 0; i < 4; i++){
Tile other = tile.nearby(i);
if(other != null){
//other tile needs to update its nearSolid to be false if it's not solid and this tile just got un-solidified
if(!other.solid()){
boolean otherNearSolid = false;
for(int j = 0; j < 4; j++){
Tile othernear = other.nearby(i);
if(othernear != null && othernear.solid()){
otherNearSolid = true;
break;
}
}
//the other tile is no longer near solid, remove the solid bit
if(!otherNearSolid){
tiles[other.array()] &= ~(PathTile.bitMaskNearSolid);
}
}
}
}
}
});
}
private void clearCache(){
@@ -115,17 +143,23 @@ public class Pathfinder implements Runnable{
}
/** Packs a tile into its internal representation. */
private int packTile(Tile tile){
private int packTile(Tile tile, int prev){
boolean nearLiquid = false, nearSolid = false, nearGround = false, solid = tile.solid(), allDeep = tile.floor().isDeep();
for(int i = 0; i < 4; i++){
Tile other = tile.nearby(i);
if(other != null){
Floor floor = other.floor();
boolean osolid = other.solid();
if(floor.isLiquid) nearLiquid = true;
if(other.solid()) nearSolid = true;
if(osolid) nearSolid = true;
if(!floor.isLiquid) nearGround = true;
if(!floor.isDeep()) allDeep = false;
//other tile is now near solid
if(solid){
tiles[other.array()] |= PathTile.bitMaskNearSolid;
}
}
}
@@ -173,7 +207,7 @@ public class Pathfinder implements Runnable{
tile.getLinkedTiles(t -> {
int pos = t.array();
if(pos < tiles.length){
tiles[pos] = packTile(t);
tiles[pos] = packTile(t, tiles[pos]);
}
});

View File

@@ -70,7 +70,7 @@ public class CommandAI extends AIController{
faceTarget();
}
if(attackTarget == null && unit.within(targetPos, Math.max(5f, unit.hitSize) / 2.5f)){
if(attackTarget == null && unit.within(targetPos, Math.max(5f, unit.hitSize / 2.5f))){
targetPos = null;
}
}else if(target != null){

View File

@@ -253,6 +253,7 @@ public class Tile implements Position, QuadTreeObject, Displayable{
if(other != null){
if(pass == 0){
//first pass: delete existing blocks - this should automatically trigger removal if overlap exists
//TODO pointless setting air to air?
other.setBlock(Blocks.air);
}else{
//second pass: assign changed data