/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.bindedobjects.core;
import transientlibs.maps.misc.MapInfo;
import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import transientlibs.maps.container.TransientAssembledMap;
import transientlibs.maps.implementation.BasicMap;
import transientlibs.slick2d.util.Log;
import transientlibs.processors.misc.Detonator;
import transientlibs.objects.primitives.Coords;
import transientlibs.tex.StringAnalyst;
/**
*
* @author kibertoad
*/
public class Maps {
public static ArrayList<MapInfo> mapCatalogue = new ArrayList<MapInfo>();
public static ArrayList<BasicMap> maps = new ArrayList<BasicMap>();
public static ArrayList<TransientAssembledMap> tiledMaps = new ArrayList<TransientAssembledMap>();
public static int loadStage = 0;
public static int loadYCounter = 0;
public static BasicMap currentMap;
public static void loadAllMaps() {
Log.info("Load all maps into catalogue");
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].equals(".svn"))) && (fileList[x].endsWith(".map"))) {
Log.info(fileList[x]);
//loadMap("maps/" + fileList[x]);
catalogueMap("maps/" + fileList[x]);
}
}
}
}
public static void catalogueMap(String fromFile) {
StringAnalyst reader = new StringAnalyst();
MapInfo newInfo = new MapInfo();
mapCatalogue.add(newInfo);
newInfo.filePath = fromFile;
reader.openFile(fromFile);
loadStage = 0;
//loadYCounter = 1;
loadYCounter = 0;
while (!reader.eof) {
reader.readRow();
analyzeStringForMapCatalogue(reader, newInfo);
}
reader.closeFile();
}
public static void loadCatalogueMap(String fromFile) {
loadGameMap(getMapInfoByCode(fromFile).filePath);
}
public static void loadStringMap(String randMap) {
/**
* Loads a map from a String. For procedurally generated maps, or if you
* hate Files.
*/
if (Maps.currentMap != null) {
Log.notImplemented();
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().removeMarker(Maps.currentMap);
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().removeMapTiles(Maps.currentMap);
}
StringAnalyst reader = new StringAnalyst();
//end stealing from Maps.loadMap(String theFile)
//if (reader.mapLineCount == -1) {reader.countMapSize(fromFile, null);}
reader.countMapSize(new StringReader(randMap), null); //new method
reader.openFile(new StringReader(randMap));//new method
//resume stealing from Maps.loadMap(String theFile)
currentMap = new BasicMap(randMap);
Detonator.INSTANCE.currentMap = currentMap;
loadStage = 0;
//loadYCounter = 1;
loadYCounter = 0;
//define length
reader.readRow();
//if (reader.mapLineCount == -1) {reader.countMapSize(fromFile, null);}
currentMap.sizeX = reader.longestLength;
// currentMap.sizeY = currentMap.sizeX;
currentMap.sizeY = reader.mapLineCount + 1;
//Log.info("X: "+currentMap.sizeX+" Y: "+currentMap.sizeY);
currentMap.eye.eyeSizeX = currentMap.sizeX + 1;
currentMap.eye.eyeSizeY = currentMap.sizeY;
currentMap.initArrays(currentMap.sizeX + 1, currentMap.sizeY + 1);
currentMap.initTiles();
// .tile = new Tile[currentMap.sizeX + 1][currentMap.sizeY + 1];
//currentMap.pathFinderMap = new boolean [currentMap.sizeX + 1][currentMap.sizeY + 1];
analyzeStringForMap(reader);
while (!reader.eof) {
if (loadStage == 0) {
loadYCounter++;
}
reader.readRow();
analyzeStringForMap(reader);
}
currentMap.sizeY = loadYCounter;
// reader.closeFile();
Log.notImplemented();
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().passMarkerNoHoverLowPriority(Maps.currentMap);
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().passMapTiles(Maps.currentMap);
//end stealing from Maps.loadMap(String theFile)
}
public static BasicMap loadGameMap(int byID) {
return loadGameMap(getMapInfoByID(byID).filePath);
}
public static BasicMap loadGameMap(String fromFile) {
return loadMap(Detonator.INSTANCE.gameDataDir + fromFile);
}
//use gamedatadir
public static BasicMap loadMap(String fromFile) {
if (Maps.currentMap != null) {
Log.notImplemented();
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().removeMarker(Maps.currentMap);
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().removeMapTiles(Maps.currentMap);
}
StringAnalyst reader = new StringAnalyst();
Log.info("Start loading map: " + fromFile);
//if (reader.mapLineCount == -1) {reader.countMapSize(fromFile, null);}
reader.countMapSize(fromFile, null);
reader.openGlobalFile(fromFile);
currentMap = new BasicMap(fromFile);
if (Detonator.INSTANCE != null) {
Detonator.INSTANCE.currentMap = currentMap;
}
loadStage = 0;
//loadYCounter = 1;
loadYCounter = 0;
//define length
reader.readRow();
//if (reader.mapLineCount == -1) {reader.countMapSize(fromFile, null);}
currentMap.sizeX = reader.longestLength;
// currentMap.sizeY = currentMap.sizeX;
currentMap.sizeY = reader.mapLineCount + 1;
//Log.info("X: "+currentMap.sizeX+" Y: "+currentMap.sizeY);
currentMap.eye.eyeSizeX = currentMap.sizeX + 1;
currentMap.eye.eyeSizeY = currentMap.sizeY;
currentMap.initArrays(currentMap.sizeX + 1, currentMap.sizeY + 1);
currentMap.initTiles();
// .tile = new Tile[currentMap.sizeX + 1][currentMap.sizeY + 1];
//currentMap.pathFinderMap = new boolean [currentMap.sizeX + 1][currentMap.sizeY + 1];
analyzeStringForMap(reader);
while (!reader.eof) {
if (loadStage == 0) {
loadYCounter++;
}
reader.readRow();
analyzeStringForMap(reader);
}
currentMap.sizeY = loadYCounter;
reader.closeFile();
if (Detonator.INSTANCE.currentGenericGame.getTiledMapState() != null) {
Log.notImplemented();
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().passMarkerNoHoverLowPriority(Maps.currentMap);
//Detonator.INSTANCE.currentGenericGame.getTiledMapState().passMapTiles(Maps.currentMap);
}
return currentMap;
}
public static void analyzeStringForMap(StringAnalyst analyst) {
analyzeStringForMap("-NO-", analyst);
}
public static void analyzeStringForMapCatalogue(StringAnalyst analyst, MapInfo setInfo) {
analyzeStringForMapCatalogue("-NO-", analyst, setInfo);
}
public static void analyzeStringForMapCatalogue(String getString, StringAnalyst analyst, MapInfo mapInfo) {
//Log.info("Load for catalogue");
if (!"-NO-".equals(getString)) {
analyst.fullStr = getString;
}
String nowStr = analyst.fullStr;
if (loadStage == 0) {
if ((nowStr.equals("end")) || (nowStr.length() == 0)) {
loadStage = 1;
loadYCounter--;
}
}
if (loadStage == 1) {
analyst.getNextString();
nowStr = analyst.lastStr;
if (nowStr.equals("id")) {
mapInfo.ID = analyst.getBinding(Binding.mapBinding);
mapInfo.StringID = analyst.lastStr;
}
}
}
public static void analyzeStringForMap(String getString, StringAnalyst analyst) {
if (!"-NO-".equals(getString)) {
analyst.fullStr = getString;
}
//analyst.checkForAliases();
//analyst.getNextString();
String nowStr = analyst.fullStr;
boolean withResult = false;
//currentMap.tile[][]
if (loadStage == 0) {
/*
if (nowStr.length() == currentMap.sizeX) {
currentMap.translateFromString(nowStr);
withResult = true;
} else {
loadStage = 1;
loadYCounter--;
}
*/
if ((nowStr.equals("end")) || (nowStr.length() == 0)) {
loadStage = 1;
loadYCounter--;
withResult = true;
} else {
if ((currentMap.sizeX != -1) && (currentMap.sizeX > nowStr.length())) {
StringBuilder build = new StringBuilder();
build.append(nowStr);
while (currentMap.sizeX > build.length()) {
build.append(" ");
}
nowStr = build.toString();
}
currentMap.translateFromString(nowStr);
withResult = true;
}
}
if (loadStage == 1) {
analyst.checkForAliases();
analyst.getNextString();
nowStr = analyst.lastStr;
if (nowStr.equals("id")) {
maps.add(currentMap);
currentMap.ID = analyst.getBinding(Binding.mapBinding);
withResult = true;
}
if (withResult == false) {
withResult = Tex.analyzeStringForTex(analyst.fullStr, analyst, false);
}
}
if ((withResult == false) && (nowStr.length() > 2) && (!nowStr.startsWith("//"))
&& (!nowStr.startsWith("#"))) {
Log.error("Unknown string: " + nowStr);
}
}
public static MapInfo getMapInfoByID(int getID) {
return mapCatalogue.get(getID);
}
public static MapInfo getMapInfoByCode(String getCode) {
return mapCatalogue.get(Binding.readBinding(Binding.mapBinding, getCode.toLowerCase()));
}
//Don't know if these will ever be usefull
public static Coords getMapCentreByCode(String getCode) {
int y = mapCatalogue.get(Binding.readBinding(Binding.mapBinding, getCode.toLowerCase())).sizeX;
int x = mapCatalogue.get(Binding.readBinding(Binding.mapBinding, getCode.toLowerCase())).sizeX;
y = y / 2;
x = x / 2;
//Coords ret = new Coords(Double.parseDouble(Integer.toString(x)), Double.parseDouble(Integer.toString(y)));
Coords ret = new Coords(Float.parseFloat(Integer.toString(x)), Float.parseFloat(Integer.toString(y)));
return ret;
}
public static Coords getMapCentreByID(int getID) {
int y = (mapCatalogue.get(getID).sizeY / 2);
int x = (mapCatalogue.get(getID).sizeX / 2);
//Coords ret = new Coords(Double.parseDouble(Integer.toString(x)), Double.parseDouble(Integer.toString(y)));
Coords ret = new Coords(Float.parseFloat(Integer.toString(x)), Float.parseFloat(Integer.toString(y)));
return ret;
}
public static Coords getCurrentMapCentre() {
int y = currentMap.sizeY / 2;
int x = currentMap.sizeX / 2;
//Coords ret = new Coords(Double.parseDouble(Integer.toString(x)), Double.parseDouble(Integer.toString(y)));
Coords ret = new Coords(Float.parseFloat(Integer.toString(x)), Float.parseFloat(Integer.toString(y)));
return ret;
}
/*
* /\/\
* 0.o
* (_||_)
* Note: use space bar on eyes to make bunny look left and right, it's fun!
*/
}