/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.maps.container;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import java.io.File;
import java.util.ArrayList;
//import org.lwjgl.input.Mouse;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.bindedobjects.gamecontent.Terrain;
import transientlibs.processors.misc.Detonator;
import transientlibs.maps.units.GenericUnit;
import transientlibs.maps.entities.Landmark;
import transientlibs.maps.tiles.Tile;
import transientlibs.maps.units.Unit;
import transientlibs.maps.implementation.TilelessMap;
import transientlibs.maps.interfaces.IGenericTiledMap;
import transientlibs.maps.implementation.BasicMap;
import transientlibs.maps.implementation.TiledMapGDX;
import transientlibs.maps.utils.fov.FOVBoard;
import transientlibs.objects.primitives.Coords;
import transientlibs.objects.primitives.Int;
import transientlibs.preui.gdx.gui.MouseHack;
import transientlibs.preui.objects.gui.interfaces.IImage;
import transientlibs.preui.objects.states.IState;
import transientlibs.slick2d.util.pathfinding.PathFindingContext;
import transientlibs.slick2d.util.pathfinding.TileBasedMap;
/**
*
* @author alta
*/
public class TransientAssembledMap extends TilelessMap implements TileBasedMap {
public IGenericTiledMap map;
public String dungeonFile;
public FOVBoard fov;
//public String code;
public int storedHeightInTiles;
public int storedWidthInTiles;
//public Color brown; //saddle brown
//public Color lightgreen; //Lawn Green
//public Color lightblue; //Sky Blue
public BasicMap transientMap;
//public GenericUnit player;
private int getTileY(int cameraY) {
int tileY = -cameraY / map.getTileHeight();
return Math.min(Math.max(tileY, 0), map.getHeight() - 1);
}
public boolean blockedOnTerrainLayer(int onX, int onY) {
return ((this.map.hasLayer(Detonator.INSTANCE.terrainLayerCode)) && (map.getTileProperty(onX, onY, Detonator.INSTANCE.terrainLayerCode, "iswall", "false").equals("true"))
|| (this.map.hasLayer(Detonator.INSTANCE.terrainLayerCode)) && (map.getTileProperty(onX, onY, Detonator.INSTANCE.terrainLayerCode, "iswall", "false").equals("+")));
}
public boolean blockedOnObstacleLayer(int onX, int onY) {
return ((this.map.hasLayer(Detonator.INSTANCE.obstaclesLayerCode)) && (map.getTileProperty(onX, onY, Detonator.INSTANCE.obstaclesLayerCode, "passable", "false").equals("false")));
}
private int getTileX(int cameraX) {
int tileX = -cameraX / map.getTileWidth();
return Math.min(Math.max(tileX, 0), map.getWidth() - 1);
}
public IImage background;
/**
* Load a dungeon from the given map file.
*
* @param ref the location of the map file.
* @throws
*/
public void loadTiledMap(String fromFile) {
}
public final void init() {
//try {
//brown = new Color(139, 69, 19); //saddle brown
//lightgreen = new Color(124, 252, 0); //Lawn Green
//lightblue = new Color(135, 206, 250); //Sky Blue
//map appears here
loadTiledMap(code);
transientMap = new BasicMap(code);
map.calculateDimensions();
storedHeightInTiles = map.getHeight();
storedWidthInTiles = map.getWidth();
//storedHeightInTiles = transientMap.getActualSizeX();
//storedWidthInTiles = transientMap.getActualSizeY();
transientMap.sizeX = storedWidthInTiles;
transientMap.sizeY = storedHeightInTiles;
transientMap.initArrays(storedWidthInTiles, storedHeightInTiles);
transientMap.initTiles();
transientMap.camera = camera;
//transientMap.eye = eye;
map.bindObjects();
//canSee = buildBoolArray();
//haveSeen = buildBoolArray();
//trackPlayerSight();
//Log.info("LOAD ALL");
if (map.hasLayer(Detonator.INSTANCE.terrainLayerCode)) {
loadAllTerrain();
}
if (map.hasLayer(Detonator.INSTANCE.objectsLayerCode)) {
loadAllLandmarks();
}
if (map.hasLayer(Detonator.INSTANCE.mapobjectsLayerCode)) {
((TiledMapGDX)map).objectLayer = ((TiledMapGDX)map).getObjectLayer(Detonator.INSTANCE.mapobjectsLayerCode);
loadAllObjectLandmarks();
}
if (map.hasLayer("creatures")) {
loadAllCreatures();
}
}
public void passTerrainToState(IState toState) {
for (Tile t : transientMap.tileList) {
toState.passMarkerMinorHoverOnly(t);
//Log.warn("Just passed tile: "+t.markerID());
//t.isDrawn = true;
}
}
public TransientAssembledMap(String ref) {
//super(ref);
dungeonFile = "data/" + Detonator.INSTANCE.currentModuleID + "/" + ref + ".tmx";
ID = Binding.getBinding(Binding.mapBinding, ref);
code = ref.replace("maps/", "");
//Log.info(code+"-> map");
//dungeonFile = "data/recall/"+ref+".tmx";
init();
}
public static void loadAllMaps(ArrayList<TilelessMap> mapList) {
File dir = new File(Detonator.INSTANCE.currentModule.dirName + "maps");
String[] fileList = dir.list();
if (fileList == null) {
Log.warn(Detonator.INSTANCE.currentModule.dirName + "maps");
Log.warn("Specified directory does not exist!");
} else {
for (int x = 0; x < fileList.length; x++) {
if (fileList[x].endsWith(".tmx")) {
Log.info(fileList[x]);
//loadMap("maps/" + fileList[x]);
Log.info("Adding map: " + Detonator.INSTANCE.currentModule.dirName + "maps/" + fileList[x]);
Log.notImplemented();
//mapList.add(new TransientSlickTiledMap(("maps/" + fileList[x]).replace(".tmx", "")));
//loadActionFile("actions/" + fileList[x]);
}
}
}
}
public int getTileID(int x, int y, String onLayer) {
//Log.info("Referenced layer: "+onLayer);
return map.getTileId(x, y, map.getLayerIndex(onLayer));
}
public void recalcObjectPositions() {
//Log.info("Recalc units: "+units.size());
for (GenericUnit u : units) {
//Log.info("Recalc unit");
u.calcImagePosition();
//if (u.getCreatures().hasTag("pc")) {
// Log.info("Recalced PC!!!");
//}
}
for (Tile t : transientMap.tileList) {
//Log.warn("recalc tile");
t.calcImagePosition();
}
for (Landmark l : landmarks) {
//Log.info("recalc");
l.calcImagePosition();
}
}
public void loadObjectLandmark(int x, int y) {
//String landmarkID = map.getObjectTileProperty(x, y, Detonator.INSTANCE.mapobjectsLayerCode, "id", "-1");
String landmarkID = map.getObjectTileName(x, y, Detonator.INSTANCE.mapobjectsLayerCode);
//map.getTileProperty(x, y, , "id", "-1");
if (!"-".equals(landmarkID)) {
Log.warn("ID: "+landmarkID);
Landmark l = Detonator.INSTANCE.creatureFactory.produceLandmark(landmarkID, x, y, this);
int dialogueID = Binding.getBinding(Binding.storyBinding, map.getObjectTileProperty(x, y, Detonator.INSTANCE.mapobjectsLayerCode, "dialogue", "-1"));
l.customIntFields.put("dialogue", new Int (dialogueID));
}
landmarkID = map.getObjectTileProperty(x, y, Detonator.INSTANCE.mapobjectsLayerCode, "tiletag", "-1");
if (!"-1".equals(landmarkID)) {
getTile(x, y).addTag(landmarkID);
}
}
public void loadLandmark(int x, int y) {
//Log.info("Load landmark from: "+x+"/"+y);
//String landmarkID = map.getTileProperty(getTileID(x, y, "objects"), "id", "-1");
String landmarkID = map.getTileProperty(x, y, Detonator.INSTANCE.objectsLayerCode, "id", "-1");
//Log.info(landmarkID);
if (!"-1".equals(landmarkID)) {
//Log.info("new landmark");
Detonator.INSTANCE.creatureFactory.produceLandmark(landmarkID, x, y, this);
//landmarks.get(landmarks.size() - 1).calcImagePosition();
}
//Log.info("Check for tile tag: "+x+"/"+y);
landmarkID = map.getTileProperty(x, y, Detonator.INSTANCE.objectsLayerCode, "tiletag", "-1");
if (!"-1".equals(landmarkID)) {
//Log.info("Tile tag is set here! "+x+"/"+y);
getTile(x, y).addTag(landmarkID);
}
}
public void loadTerrain(int x, int y) {
//Log.warn("Load terrain from: " + x + "/" + y);
String terrainID = map.getTileProperty(x, y, Detonator.INSTANCE.terrainLayerCode, "id", "-1");
//Log.info(landmarkID);
if (!"-1".equals(terrainID)) {
//Log.info("new landmark");
//units.add(Detonator.INSTANCE.creatureFactory.produceUnit(creatureID, x, y, this));
//transientMap.getTile(x, y).changeTerrain(terrainID);
transientMap.addTile(x, y, terrainID);
} else {
transientMap.addTile(x, y, "default");
}
}
public Unit placeUnit(int x, int y, String byCode) {
GenericUnit result = Detonator.INSTANCE.creatureFactory.produceUnit(byCode, x, y, this);
//placeUnit (x, y, result);
return (Unit) result;
//null is to avoid placing unit twice
}
@Override
public void placeUnit(int x, int y, GenericUnit unit) {
unit.calcImagePosition();
if (!(this.units.contains(unit))) {
units.add(unit);
if (!(transientMap.getTile(x, y).units.contains(unit))) {
transientMap.getTile(x, y).units.add(unit);
}
}
}
public void placeUnit(GenericUnit unit) {
unit.calcImagePosition();
if (!(this.units.contains(unit))) {
units.add(unit);
if (!(transientMap.getTile(unit.getMapCoords().getIntX(), unit.getMapCoords().getIntY()).units.contains(unit))) {
transientMap.getTile(unit.getMapCoords().getIntX(), unit.getMapCoords().getIntY()).units.add(unit);
}
}
}
public void loadCreature(int x, int y) {
String creatureID = map.getTileProperty(x, y, "creatures", "id", "-1");
//Log.info(landmarkID);
if (!"-1".equals(creatureID)) {
//Log.info("new landmark");
units.add(Detonator.INSTANCE.creatureFactory.produceUnit(creatureID, x, y, this));
units.get(units.size() - 1).calcImagePosition();
if ((map.getTileProperty(x, y, "creatures", "isplayer", "false").equals("true"))
|| (units.get(units.size() - 1).getCreatures().hasTag("pc"))) {
player = (Unit) units.get(units.size() - 1);
Log.info("Player was set!");
}
}
/*
creatureID = map.getTileProperty(getTileID(x, y, "animals"), "creatureid", "-1");
//Log.info(landmarkID);
if (!"-1".equals(creatureID)) {
//Log.info("new landmark");
units.add(Detonator.INSTANCE.creatureFactory.produceUnit(creatureID, x, y, this));
units.get(units.size() - 1).calcImagePosition();
}
*/
}
@Override
public int getWidthInTiles() {
//return sizeX+1; //incremented so that node[] array gets constructe dproperly for Astar
//return getActualSizeX();
return storedWidthInTiles;
}
@Override
public int getHeightInTiles() {
//return getActualSizeY();
return storedHeightInTiles;
}
@Override
public int getWidth() {
//return map.getWidth();
return storedWidthInTiles;
}
//@Override
@Override
public int getHeight() {
//return map.getHeight();
return storedHeightInTiles;
}
public void loadAllTerrain() {
for (int xCounter = 0; xCounter < map.getWidth(); xCounter++) {
for (int yCounter = 0; yCounter <= map.getHeight(); yCounter++) {
loadTerrain(xCounter, yCounter);
}
}
}
public void loadAllCreatures() {
for (int xCounter = 0; xCounter < map.getWidth(); xCounter++) {
for (int yCounter = 0; yCounter < map.getHeight(); yCounter++) {
loadCreature(xCounter, yCounter);
}
}
}
public void bindLandmarksToState(IState state) {
for (Landmark l : landmarks) {
state.passMarkerHoverOnly(l);
}
}
public void loadAllLandmarks() {
//Log.info("DOX: "+ getWidth());
//Log.info("DOY: "+ getHeight());
for (int xCounter = 0; xCounter < map.getWidth(); xCounter++) {
for (int yCounter = 0; yCounter < map.getHeight(); yCounter++) {
loadLandmark(xCounter, yCounter);
}
}
}
public void loadAllObjectLandmarks() {
//Log.info("DOX: "+ getWidth());
//Log.info("DOY: "+ getHeight());
for (int xCounter = 0; xCounter < map.getWidth(); xCounter++) {
for (int yCounter = 0; yCounter < map.getHeight(); yCounter++) {
loadObjectLandmark(xCounter, yCounter);
}
}
}
@Override
public Tile getTile(int x, int y) {
//Log.warn("ASSEMBLE GET");
return transientMap.getTile(x, y);
}
//@Override
public boolean isPassable(int x, int y) {
//Log.info("Check passable");
//int idT = map.getTileId(x, y, map.getLayerIndex("terrain"));
if (map.getTileProperty(x, y, Detonator.INSTANCE.terrainLayerCode, "impassable", "false").equals("true")) {
return false;
}
//idT = map.getTileId(x, y, map.getLayerIndex("objects"));
if (map.hasLayer(Detonator.INSTANCE.objectsLayerCode)) {
if (map.getTileProperty(x, y, Detonator.INSTANCE.objectsLayerCode, "impassable", "false").equals("true")) {
return false;
}
}
return true;
}
/*
private void checkForTerrain() throws LoadException {
//check for terrain
if (map.getLayerIndex("terrain") == -1) {
throw new AbstractLOSMap.LoadException("no terrain specified in " + dungeonFile);
}
}
*/
//methods for field of view / line of sight
/*
public void updateFov() {
clearSight();
trackPlayerSight();
IFovAlgorithm fov = new PrecisePermissive();
fov.visitFieldOfView(this, player.getMapCoords().getIntX(), player.getMapCoords().getIntY(), player.getSightRange().value);
}
* */
//public Tile getTile(int x, int y) {
//}
@Override
public void removeUnitFromTile(int x, int y, GenericUnit theUnit) {
//theUnit.mapCoords.x = -1;
//theUnit.mapCoords.y = -1;
getTile(x, y).units.remove(theUnit);
}
@Override
public void addUnitToTile(int x, int y, GenericUnit theUnit) {
//getTile(x, y).units.add(theUnit);
theUnit.getMapCoords().x = x;
theUnit.getMapCoords().y = y;
}
public void centerEyeOnPlayer() {
eye.eyePosition.x = player.getMapCoords().getIntX() - (eye.eyeSizeX / 2);
eye.eyePosition.y = player.getMapCoords().getIntY() - (eye.eyeSizeY / 2);
Log.notImplemented();
}
public void centerEyeOnPlayer(int startX, int endX, int startY, int endY) {
eye.eyePosition.x = player.getMapCoords().getIntX() - ((eye.eyeSizeX - (endX - startX)) / 2);
eye.eyePosition.y = player.getMapCoords().getIntY() - ((eye.eyeSizeY - (endY - startY)) / 2);
//Log.notImplemented();
}
@Override
public void processUnitEncounterWithLandmark(GenericUnit u) {
}
@Override
public Terrain getTerrain(int x, int y) {
return transientMap.getTile(x, y).terrain;
}
@Override
public Coords getTileScreenCoords(int x, int y) {
return Detonator.INSTANCE.screenCoordsCalculator.returnScreenCoords(x, y);
}
public GenericUnit returnClickedUnit() {
//Log.info("Check: "+onX +"/"+onY);
for (GenericUnit u : units) {
if (u.isHovered()) {
return u;
}
}
return null;
}
public Coords returnClickedTile() {
if ((returnClickedLandmark() == null) && (returnClickedUnit() == null)) {
return Detonator.INSTANCE.screenCoordsCalculator.returnMapCoordsAdjusted(MouseHack.getX(), Detonator.INSTANCE.ScreenSizeY - MouseHack.getY(), camera);
}
return null;
}
//public Landmark returnClickedLandmark (int onX, int onY) {
public Landmark returnClickedLandmark() {
//Log.info("Check: "+onX +"/"+onY);
for (Landmark l : landmarks) {
//Log.info("Start checking the landmark");
//if (l.screenCoords.isSame(onX, onY))
if (l.isHovered()) {
//Log.info("Checked: "+l.screenCoords.getIntX() +"/"+l.screenCoords.getIntY());
return l;
}
}
return null;
}
@Override
public int getActualSizeX() {
return this.map.getWidth();
}
@Override
public boolean tileIsObstacle(int x, int y) {
return false;
}
@Override
public int getActualSizeY() {
return map.getHeight();
}
@Override
public GenericUnit getPlayer() {
return player;
}
@Override
public void placeLandmark(int x, int y, Landmark landmark) {
landmarks.add(landmark);
getTile(x, y).placeLandmark(landmark);
}
public Coords returnRandomFreeSpot() {
Coords result = new Coords();
do {
result.x = Detonator.INSTANCE.rng.nextInt(storedWidthInTiles) + 1;
result.y = Detonator.INSTANCE.rng.nextInt(storedHeightInTiles) + 1;
//Log.info("Pick random spot: "+result.x+"/"+result.y);
} while (Detonator.INSTANCE.tileMovementCalculator.isBlocked(result.getIntX(), result.getIntY()));
return result;
}
public void removeUnit(Unit unit) {
units.remove(unit);
removeUnitFromTile(unit.mapCoords.getIntX(), unit.mapCoords.getIntY(), unit);
}
@Override
public boolean hasTiledProperty(GenericUnit pc, String onLayer, String property, String value) {
//return this.map.getTileProperty(getTileID(pc.getMapCoords().getIntX(), pc.getMapCoords().getIntY(), onLayer), property, "-").equals(value);
return this.map.getTileProperty(pc.getMapCoords().getIntX(), pc.getMapCoords().getIntY(), onLayer, property, "-").equals(value);
}
@Override
public boolean blocked(PathFindingContext pfc, int i, int i1) {
return Detonator.INSTANCE.tileMovementCalculator.isBlocked(i, i1);
}
@Override
public float getCost(PathFindingContext pfc, int i, int i1) {
return getTile(i, i1).terrain.moveCost;
}
//@Override
public boolean blocksSight(int x, int y) {
if ((map.layerHasTile(x, y, map.getLayerIndex(Detonator.INSTANCE.obstaclesLayerCode)))
&& ("true".equals(map.getTileProperty(x, y, Detonator.INSTANCE.obstaclesLayerCode, "blocks_sight", "true")))) {
return true;
}
if ((map.layerHasTile(x, y, map.getLayerIndex(Detonator.INSTANCE.terrainLayerCode)))
&& (("true".equals(map.getTileProperty(x, y, Detonator.INSTANCE.terrainLayerCode, "iswall", "false"))))
|| ("+".equals(map.getTileProperty(x, y, Detonator.INSTANCE.terrainLayerCode, "iswall", "false")))
&& (("false".equals(map.getTileProperty(x, y, Detonator.INSTANCE.terrainLayerCode, "transparent", "false")))))
{
return true;
}
return false;
}
@Override
public void pathFinderVisited(int i, int i1) {
//don't need to know whether pathfinder visited a tile
}
@Override
public void removeUnit(GenericUnit unit) {
units.remove(unit);
removeUnitFromTile(unit.getMapCoords().getIntX(), unit.getMapCoords().getIntY(), unit);
}
public GenericUnit getUnit(int x, int y, int index) {
if ((withinBorders(x, y))
&& (!(this.getTile(x, y).units.isEmpty()))) {
return getTile(x, y).units.get(index);
} else {
return null;
}
}
public void removeTerrainFromState(IState state) {
for (Tile t : transientMap.tileList) {
state.removeMarkerMinorHoverOnly(t);
//Log.info("Just passed tile: "+t.markerID());
//t.isDrawn = true;
}
}
}