/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.maps.utils.pathfinding;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import transientlibs.maps.implementation.TilelessMap;
import transientlibs.bindedobjects.core.Maps;
import transientlibs.bindedobjects.gamecontent.Terrain;
import transientlibs.processors.misc.Detonator;
import transientlibs.maps.implementation.TilelessMap;
import transientlibs.maps.tiles.Tile;
import transientlibs.preui.objects.gui.interfaces.IImage;
import transientlibs.objects.primitives.Coords;
import transientlibs.objects.primitives.Int;
import transientlibs.slick2d.util.Log;
import transientlibs.slick2d.util.pathfinding.AStarPathFinder;
import transientlibs.slick2d.util.pathfinding.Path;
import transientlibs.slick2d.util.pathfinding.TileBasedMap;
/**
*
* @author kibertoad
*/
public class PathFinder {
public TilelessMap map;
public Path path = null;
public int goalX;
public int goalY;
public Coords mapCoords;
public Int currentMovementReach = new Int(10); //current action points
public Int globalMovementReach = new Int(10); //max action points
public static IImage path_marker;
public static IImage reachable_path_marker;
public int counter = 0;
public Coords drawCoords;
public boolean pathAvailable() {
return (path != null);
}
public PathFinder(Coords setCoords, TilelessMap setMap) {
mapCoords = setCoords;
map = setMap;
}
/**
* Set path to null
*/
public void resetPath() {
path = null;
}
public int getReachableSpotX() {
if (currentMovementReach.value < path.getLength()) {
return path.getX(currentMovementReach.value);
} else {
return path.getX(path.getLength() - 1);
}
}
public int getReachableSpotY() {
if (currentMovementReach.value < path.getLength()) {
return path.getY(currentMovementReach.value);
} else {
return path.getY(path.getLength() - 1);
}
}
public void drawPath(SpriteBatch spriteBatch) {
spriteBatch.begin();
if (path != null) {
//Log.warn("draw stuff path");
int length = path.getLength();
for (int i = 1; i < length; i++) {
//reachableCounter++;
//experiment
if (i <= currentMovementReach.value) {
// reachable_path_marker.draw( Maps.currentMap.getTile(path.getX(i), path.getY(i)).screenCoords.getIntX(),
// Maps.currentMap.getTile(path.getX(i), path.getY(i)).screenCoords.getIntY());
//Log.warn("Tile draw place: "+ map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX()+"/"+map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY());
//counter++;
//if (counter > 1000) {
// counter = 0;
drawCoords = Detonator.INSTANCE.screenCoordsCalculator.returnScreenCoords(path.getX(i), path.getY(i));
drawCoords.y = Detonator.INSTANCE.ScreenSizeY - drawCoords.y;
drawCoords.y -= Terrain.tileHeight;
//Log.warn("Tile draw place: " + drawCoords.toRoundedString());
//Log.warn("Drawing path tile of map: " + path.getX(i) + "/" + path.getY(i));
//}
//reachable_path_marker.draw(spriteBatch, map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX(),
// map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY());
//This one worked before
//reachable_path_marker.draw(spriteBatch, map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX(),
// Detonator.INSTANCE.ScreenSizeY - Terrain.tileHeight - map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY());
//experimental one
reachable_path_marker.draw(spriteBatch, drawCoords.getIntX(), drawCoords.getIntY());
} else {
//path_marker.draw(spriteBatch, map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX(),
// Detonator.INSTANCE.ScreenSizeY - Terrain.tileHeight - map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY());
Log.warn("Tile draw place: " + drawCoords.toRoundedString());
Log.warn("Drawing path tile of map: " + path.getX(i) + "/" + path.getY(i));
path_marker.draw(spriteBatch, drawCoords.getIntX(), drawCoords.getIntY());
}
if ((path.getX(i) == goalX) && (path.getY(i) == goalY)) {
int res = (length - 1) / globalMovementReach.value;
if ((length - 1) % globalMovementReach.value > 0) {
res++;
}
}
}
}
spriteBatch.end();
}
public void drawPath() {
if (path != null) {
int length = path.getLength();
for (int i = 1; i < length; i++) {
//reachableCounter++;
if (i <= currentMovementReach.value) {
// reachable_path_marker.draw( Maps.currentMap.getTile(path.getX(i), path.getY(i)).screenCoords.getIntX(),
// Maps.currentMap.getTile(path.getX(i), path.getY(i)).screenCoords.getIntY());
reachable_path_marker.draw(map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX(),
map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY());
} else {
path_marker.draw(map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX(),
map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY());
}
if ((path.getX(i) == goalX) && (path.getY(i) == goalY)) {
int res = (length - 1) / globalMovementReach.value;
if ((length - 1) % globalMovementReach.value > 0) {
res++;
}
//QuitState.systemFont.drawString(Maps.currentMap.getTile(path.getX(i), path.getY(i)).screenCoords.getIntX() + Tile.numberXOffset,
// Maps.currentMap.getTile(path.getX(i), path.getY(i)).screenCoords.getIntY() + Tile.numberXOffset, Integer.toString(res));
// QuitState.systemFont.drawString(map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntX() + Tile.numberXOffset,
// map.getTileScreenCoords(path.getX(i), path.getY(i)).getIntY() + Tile.numberXOffset, Integer.toString(res));
}
}
}
}
/**
* Find path to coords goalX and goalY
*/
public void updatePath() {
map.pathfindingGoalX = goalX;
map.pathfindingGoalY = goalY;
AStarPathFinder pathFinder = new AStarPathFinder((TileBasedMap) map, 100, Detonator.INSTANCE.diagonalMovementAllowed);
//Log.info("mapCoords.getIntX():"+mapCoords.getIntX()+"; mapCoords.getIntY()"+ mapCoords.getIntY() +"; goalX:"+goalX +"; goalY:"+ goalY);
path = pathFinder.findPath(null, mapCoords.getIntX(), mapCoords.getIntY(),
goalX, goalY);
//Log.warn("Path size:"+path.getLength());
}
}