/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.maps.implementation;
import transientlibs.maps.entities.Bullet;
import transientlibs.maps.tiles.Tile;
import transientlibs.maps.entities.Landmark;
import transientlibs.bindedobjects.core.Maps;
import transientlibs.bindedobjects.gamecontent.Terrain;
import transientlibs.objects.primitives.Coords;
import transientlibs.processors.misc.Detonator;
import transientlibs.bindedobjects.core.Binding;
import java.util.ArrayList;
import java.util.Iterator;
import transientlibs.slick2d.util.Log;
import transientlibs.maps.units.GenericUnit;
import transientlibs.preui.objects.states.IState;
import transientlibs.slick2d.util.pathfinding.PathFindingContext;
import transientlibs.slick2d.util.pathfinding.TileBasedMap;
/**
*
* @author kibertoad
*/
// TODO:
// * should not be drawable by itself
// * may be create infinite map
// * add default tile
public class BasicMap extends TilelessMap implements TileBasedMap {
public String LName = "-";
//public static ArrayList<Terrain> terrains = new ArrayList<Terrain>();
public Tile[][] tile;
public ArrayList<ArrayList<Tile>> tiles = new ArrayList<ArrayList<Tile>>();
public boolean[][] pathFinderMap;
public ArrayList<Tile> tileList = new ArrayList<Tile>();
// TODO: replace all eye stuff with camera
public static Tile nowTile;
public static GenericUnit nowUnit;
public static Bullet nowBullet;
public static Landmark nowLandmark;
@Override
public void reduceCompositionPriority(IState state) {
//super.reduceCompositionPriority(state);
state.minimizeMapHoverPriority(this);
}
public BasicMap(String setName) {
super();
LName = setName;
Log.info("NEW MAP: " + setName);
}
@Override
public Terrain getTerrain(int x, int y) {
return getTile(x, y).terrain;
}
@Override
public void removeUnitFromTile(int x, int y, GenericUnit theUnit) {
getTile(x, y).units.remove(theUnit);
}
@Override
public void addUnitToTile(int x, int y, GenericUnit theUnit) {
getTile(x, y).units.add(theUnit);
}
@Override
public boolean tileIsObstacle(int onX, int onY) {
return ((getTile(onX, onY).isObstacle()));
}
@Override
public void processUnitEncounterWithLandmark(GenericUnit u) {
for (Landmark l : getTile(u.getMapCoords().getIntX(), u.getMapCoords().getIntY()).landmarks) {
if (l.landmark.onVisitTex != null) {
l.landmark.onVisitTex.processSilently(Detonator.INSTANCE.currentGenericGame.getTiledMapState().getMessageLog());
//use static messagelog
}
}
}
//@Override
@Override
public boolean isClickable() {
return false;
}
@Override
public boolean hasTiles() {
return true;
}
@Override
public Tile getTile(int x, int y) {
//return tile[x][y];
//Log.warn("Returning tile: "+tiles.get(x).get(y).mapCoords.toRoundedString());
return tiles.get(x).get(y + 1);
}
@Override
public Tile getTile(Coords fromCoords) {
//return getTile(fromCoords.getIntX(), fromCoords.getIntY());
return tiles.get(fromCoords.getIntX()).get(fromCoords.getIntY());
}
public void fillTilesWithLandmark(Landmark l) {
if (l.sizeX > 1) {
Log.info("landmark: " + (l.mapCoords.getIntX()) + "/" + l.mapCoords.getIntY());
for (int counterX = 0; counterX < l.sizeX; counterX++) {
for (int counterY = 0; counterY < l.sizeY; counterY++) {
//if (getTile (l.mapCoords.getIntX()+counterX, l.mapCoords.getIntY()+ counterY).landmarks.contains(l)) {
// Log.info("Uh, oh, this tile already has it");
//}
getTile(l.mapCoords.getIntX() + counterX, l.mapCoords.getIntY() + counterY).landmarks.add(l);
Log.info("binded tile: " + (l.mapCoords.getIntX() + counterX) + "/" + l.mapCoords.getIntY() + counterY);
//Log.info("sizeY: "+l.sizeY);
}
}
}
}
/**
* Mainly used variant
*/
public void addTile(int toX, int toY, String terrainCode) {
addTile(toX, toY, Binding.readBinding(Binding.terrainBinding, terrainCode));
}
public void addTile(int toX, int toY, int terrainID) {
//Note: crashes if toX is greater then map sizeX
tile[toX][toY] = new Tile(terrainID, toX, toY, this);
tile[toX][toY].calcImagePosition();
//tileList.add(tile[toX][toY]);
if (terrainID != -1) {
tiles.get(toX).get(toY).changeTerrain(terrainID);
tiles.get(toX).get(toY).calcImagePosition();
}
tileList.add(tiles.get(toX).get(toY));
}
public void initTiles() {
ArrayList<Tile> currentRow;
for (int xc = 0; xc <= sizeX; xc++) {
currentRow = new ArrayList<Tile>();
tiles.add(currentRow);
for (int yc = 0; yc <= sizeY; yc++) {
currentRow.add(new Tile(Terrain.terrains.get(0).ID, xc, yc - 1, this));
}
}
}
public void fillWithRandom() {
tile = new Tile[sizeX + 1][sizeY + 1];
for (int x = 0; x <= sizeX; x++) {
for (int y = 0; y <= sizeY; y++) {
//tile[x][y] = new Tile(Detonator.instance.rng.nextInt(Terrain.terrains.size()), x, y, this);
//tile[x][y].calcImagePosition();
//tileList.add(tile[x][y]);
addTile(x, y, Detonator.INSTANCE.rng.nextInt(Terrain.terrains.size()));
}
}
}
public void fillWithGenerics() {
tile = new Tile[sizeX + 1][sizeY + 1];
for (int x = 0; x <= sizeX; x++) {
for (int y = 0; y <= sizeY; y++) {
//tile[x][y] = new Tile(0, x, y, this);
//tile[x][y].calcImagePosition();
//tileList.add(tile[x][y]);
addTile(x, y, 0);
}
}
// }
}
@Override
public void recountOnScreenTiles() {
//Log.info("Recount tiles!");
Iterator<Tile> x = tileList.iterator();
while (x.hasNext()) {
nowTile = x.next();
nowTile.calcImagePosition();
}
//Log.info("Iterate units: total " + units.size());
Iterator<GenericUnit> u = units.iterator();
while (u.hasNext()) {
nowUnit = u.next();
nowUnit.calcImagePosition();
}
Log.info("Iterate recountOnScreenTiles. Landmarks: total " + landmarks.size());
for (Landmark l : landmarks) {
l.calcImagePosition();
}
Iterator<Bullet> b = missiles.iterator();
while (b.hasNext()) {
nowBullet = b.next();
nowBullet.calcImagePosition();
}
}
//@Override
public void draw() {
//Log.info("draw map");
//for (int x=1; x<=eyeSizeX; x++)
// for (int y=1; y<=eyeSizeY; y++)
// tile[x][y].draw (eyePosition);
Iterator<Tile> x = tileList.iterator();
while (x.hasNext()) {
nowTile = x.next();
//nowTile.draw(eyePosition);
nowTile.draw();
}
for (GenericUnit u : units) {
u.draw();
}
for (Bullet b : missiles) {
b.draw();
}
}
@Override
public GenericUnit returnTileUnit(int setX, int setY, int notSide) {
if ((setX > 0) && (setX < sizeX)
&& (setY > 0) && (setY < sizeY)) {
return tile[setX][setY].returnUnit();
} else {
return null;
}
}
public void initArrays(int setX, int setY) {
tile = new Tile[setX][setY + 1];
pathFinderMap = new boolean[setX][setY + 1];
}
public void translateFromString(String mapString) {
char nowChar;
int x = 0;
while ((x < mapString.length())) {
nowChar = mapString.charAt(x);
//Log.info("x "+(x+1)+" y "+Maps.loadYCounter);
//addTile(x + 1, Maps.loadYCounter, Terrain.charToTerrain(nowChar));
addTile(x + 1, Maps.loadYCounter, Terrain.charToTerrain(nowChar));
x++;
}
}
public static void selectMap(int byID) {
Maps.currentMap = Maps.maps.get(byID);
}
@Override
public void placeUnit(int x, int y, GenericUnit unit) {
tile[x][y].placeUnit(unit);
units.add(unit);
}
@Override
public void placeLandmark(int x, int y, Landmark landmark) {
//tile[x][y].placeLandmark (new Landmark (coordX, coordY, ID, currentMap));
tile[x][y].placeLandmark(landmark);
landmarks.add(landmark);
}
public Landmark getLandmark(int compareValue) {
for (Landmark l : landmarks) {
if (l.landmark.ID == compareValue) {
return l;
}
}
return null;
}
public Landmark hasLandmarkByCode(String byCode) {
return getLandmark(Binding.readBinding(Binding.landmarkBinding, byCode));
}
@Override
public int getWidthInTiles() {
//return sizeX+1; //incremented so that node[] array gets constructe dproperly for Astar
return getActualSizeX();
}
@Override
public int getHeightInTiles() {
return getActualSizeY();
}
@Override
public void pathFinderVisited(int i, int i1) {
pathFinderMap[i][i1] = true;
}
@Override
public boolean blocked(PathFindingContext pfc, int i, int i1) {
//Log.info("PF: "+i+":"+i1);
//Log.info("Goal: "+pathfindingGoalX+":"+pathfindingGoalY);
//Log.info("C: "+i+"/"+i1);
//Log.info("Size: "+sizeX+"/"+sizeY);
/*
if ((i <= 0) || (i1 <= 0) || (i >= sizeX - 1) || (i1 >= sizeY - 1)) {
return true;
} else {
return ((!(tile[i][i1].isPassable()))
//&& (!(tile[i][i1].isMoveable() && ((i != pathfindingGoalX) || (i1 != pathfindingGoalY))))
&& (!(tile[i][i1].isValidDestination(pathfindingGoalX, pathfindingGoalY))));
}
*/
return Detonator.INSTANCE.tileMovementCalculator.isBlocked(i, i1);
}
@Override
public float getCost(PathFindingContext pfc, int i, int i1) {
//return tile[i][i1].terrain.moveCost;
return Detonator.INSTANCE.tileMovementCalculator.getCost(i, i1);
}
}