Skip to content

Commit

Permalink
Added guard tower and structures offline generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett92 committed Aug 8, 2017
1 parent 1949e85 commit 2cf17de
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 17 deletions.
192 changes: 178 additions & 14 deletions src/com/imraginbro/wurm/mapgen/FileGeneration.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,194 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;

import com.wurmonline.mesh.MeshIO;

public class FileGeneration {

final static String newLine = System.lineSeparator();
final static String separator = java.io.File.separator;

static int html_nativeZoom = 0;
static int html_mapMinZoom = 0;
static int html_mapMaxZoom = 0;
static int html_actualMapSize = 0;
static int html_maxMapSize = 0;

public static void generateFiles(MeshIO map) throws IOException, SQLException {
setHTMLvars(map);
generateDeedsFile(MapGen.saveLocation, MapGen.db_wurmZones);
generateGuardTowersFile(MapGen.saveLocation, MapGen.db_wurmItems, MapGen.db_wurmPlayers);
generateStructuresFile(MapGen.saveLocation, MapGen.db_wurmZones, MapGen.db_wurmPlayers);
generateConfigFile(MapGen.saveLocation);
}

public static void generateStructuresFile(File saveLocation, File db_wurmZones, File db_wurmPlayers) throws IOException, SQLException {
if (!MapGen.showStructures) {
return;
}
if (!db_wurmZones.exists()) {
System.out.println("[ERROR] Could not find wurmzones.db. Skipping structures.js file generation.");
return;
}
if (!db_wurmPlayers.exists()) {
System.out.println("[ERROR] Could not find wurmplayers.db. Skipping structures.js file generation.");
return;
}
System.out.println("Writing structures.js file...");
BufferedWriter bw = new BufferedWriter(new FileWriter(saveLocation.getAbsolutePath() + separator + "structures.js", false));
String structBordersString = "";
System.out.println("Loading structures from wurmzones.db...");
Connection zonesDBcon = DriverManager.getConnection("jdbc:sqlite:"+db_wurmZones);
Connection playersDBcon = DriverManager.getConnection("jdbc:sqlite:"+db_wurmPlayers);
Statement statement = zonesDBcon.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM STRUCTURES WHERE FINISHED='1';");

structBordersString += ("function getStructures() {" + newLine);
structBordersString += ("\tvar structureBorders = [];" + newLine);

int count = 0;
while (resultSet.next()) {
int minX = -1;
int maxX = -1;
int minY = -1;
int maxY = -1;
long structureID = resultSet.getLong("WURMID");

Statement structStatement = zonesDBcon.createStatement();
ResultSet structRes = structStatement.executeQuery("SELECT TILEX, TILEY FROM BUILDTILES WHERE STRUCTUREID='"+structureID+"';");
if (structRes.next()) {
minX = structRes.getInt("TILEX");
maxX = structRes.getInt("TILEX");
minY = structRes.getInt("TILEY");
maxY = structRes.getInt("TILEY");
while (structRes.next()) {
final int nX = structRes.getInt("TILEX");
final int nY = structRes.getInt("TILEY");
if (nX < minX) {
minX = nX;
}
if (nX > maxX) {
maxX = nX;
}
if (nY < minY) {
minY = nY;
}
if (nY > maxY) {
maxY = nY;
}
}
}

maxX++;
maxY++;

structStatement.close();

String name = resultSet.getString("NAME");
long ownerID = resultSet.getLong("OWNERID");
Statement nameStatement = playersDBcon.createStatement();
ResultSet nameRes = nameStatement.executeQuery("SELECT NAME FROM PLAYERS WHERE WURMID='"+ownerID+"';");
String pname = "";
if (nameRes.next()) {
pname = nameRes.getString("NAME");
}
nameStatement.close();
structBordersString += ("\tstructureBorders.push(L.polygon([");
structBordersString += ("xy("+minX+","+minY+"),");
structBordersString += ("xy("+maxX+","+minY+"),");
structBordersString += ("xy("+maxX+","+maxY+"),");
structBordersString += ("xy("+minX+","+maxY+")]");
structBordersString += (", {color:'blue',fillOpacity:0.1,weight:1})");
structBordersString += (".bindPopup(\"<div align='center'><b>" + name + "</b><br><i>Created by " + pname + "</i></div>\"));" + newLine);

count++;
}
System.out.println("Added "+count+" structures to structures.js...");
structBordersString += ("\treturn structureBorders;" + newLine);
structBordersString += ("}" + newLine + newLine);
bw.append(structBordersString);
zonesDBcon.close();
playersDBcon.close();
bw.close();
}

public static void generateGuardTowersFile(File saveLocation, File db_wurmItems, File db_wurmPlayers) throws IOException, SQLException {
if (!MapGen.showGuardTowers) {
return;
}
if (!db_wurmItems.exists()) {
System.out.println("[ERROR] Could not find wurmitems.db. Skipping guardtowers.js file generation.");
return;
}
if (!db_wurmPlayers.exists()) {
System.out.println("[ERROR] Could not find wurmplayers.db. Skipping guardtowers.js file generation.");
return;
}
System.out.println("Writing guardtowers.js file...");
BufferedWriter bw = new BufferedWriter(new FileWriter(saveLocation.getAbsolutePath() + separator + "guardtowers.js", false));
String deedBordersString = "";
String deedMarkersString = "";
System.out.println("Loading guard towers from wurmitems.db...");
Connection itemsDBcon = DriverManager.getConnection("jdbc:sqlite:"+db_wurmItems);
Connection playersDBcon = DriverManager.getConnection("jdbc:sqlite:"+db_wurmPlayers);
Statement statement = itemsDBcon.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM ITEMS WHERE (TEMPLATEID='384' OR TEMPLATEID='430' OR TEMPLATEID='528' OR TEMPLATEID='638' OR TEMPLATEID='996') AND CREATIONSTATE='0';");

deedBordersString += ("function getGuardTowerBorders() {" + newLine);
deedBordersString += ("\tvar guardTowerBorders = [];" + newLine);

deedMarkersString += ("function getGuardTowers() {" + newLine);
deedMarkersString += ("\tvar guardTower = [];" + newLine);

int count = 0;
while (resultSet.next()) {
int x = (int) Math.floor(resultSet.getInt("POSX")/4);
int y = (int) Math.floor(resultSet.getInt("POSY")/4);
float ql = resultSet.getFloat("QUALITYLEVEL");
float dmg = resultSet.getFloat("DAMAGE");
long ownerID = resultSet.getLong("LASTOWNERID");
Statement nameStatement = playersDBcon.createStatement();
ResultSet nameRes = nameStatement.executeQuery("SELECT NAME FROM PLAYERS WHERE WURMID='"+ownerID+"';");
String pname = "";
if (nameRes.next()) {
pname = nameRes.getString("NAME");
}
nameStatement.close();
deedBordersString += ("\tguardTowerBorders.push(L.polygon([");
deedBordersString += ("xy("+(x-50)+","+(y-50)+"),");
deedBordersString += ("xy("+(x+51)+","+(y-50)+"),");
deedBordersString += ("xy("+(x+51)+","+(y+51)+"),");
deedBordersString += ("xy("+(x-50)+","+(y+51)+")]");
deedBordersString += (", {color:'red',fillOpacity:0.1,weight:1}));" + newLine);

deedMarkersString += ("\tguardTower.push(L.marker(");
deedMarkersString += ("xy("+(x+0.5)+","+(y+0.5)+"),");
deedMarkersString += ("{icon: guardTowerIcon})");
DecimalFormat f = new DecimalFormat("0.00");
deedMarkersString += (".bindPopup(\"<div align='center'><b>Guard Tower</b><br><i>Created by " + pname + "</i></div><br><b>QL:</b> " + f.format(ql) + "<br><b>DMG:</b> " + f.format(dmg) + "\"));" + newLine);

count++;
}
System.out.println("Added "+count+" guard towers to guardtowers.js...");
deedBordersString += ("\treturn guardTowerBorders;" + newLine);
deedBordersString += ("}" + newLine + newLine);
deedMarkersString += ("\treturn guardTower;" + newLine);
deedMarkersString += ("}");
bw.append(deedBordersString);
bw.append(deedMarkersString);
itemsDBcon.close();
playersDBcon.close();
bw.close();
}

public static void generateDeedsFile(File saveLocation, File db_wurmZones) throws IOException, SQLException {
if (!MapGen.showDeeds) {
return;
}
if (!db_wurmZones.exists()) {
System.out.println("[ERROR] Could not find zones.db. Skipping deed file generation.");
System.out.println("[ERROR] Could not find wurmzones.db. Skipping deeds.js file generation.");
return;
}
System.out.println("Writing deeds.js file...");
Expand All @@ -43,18 +208,18 @@ public static void generateDeedsFile(File saveLocation, File db_wurmZones) throw
Connection connection = DriverManager.getConnection("jdbc:sqlite:"+db_wurmZones);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM VILLAGES WHERE DISBANDED=0;");

mainDeedString += ("function setViewOnMainDeed(map) {" + newLine);

deedBordersString += ("function deedBorders() {" + newLine);
deedBordersString += ("\tvar deedBorders = [];" + newLine);

deedMarkersString += ("function deedMarkers() {" + newLine);
deedMarkersString += ("\tvar deedMarkers = [];" + newLine);

double mainX = 0;
double mainY = 0;

int count = 0;
while (resultSet.next()) {
int sx = resultSet.getInt("STARTX");
Expand All @@ -80,7 +245,7 @@ public static void generateDeedsFile(File saveLocation, File db_wurmZones) throw
deedBordersString += (", {color:'white',fillOpacity:0,weight:1})");
}
deedBordersString += (".bindPopup(\"" + resultSet.getString("NAME") + "\"));" + newLine);

String firstLetter = resultSet.getString("NAME").substring(0, 1).toLowerCase();
deedMarkersString += ("\tdeedMarkers.push(L.marker(");
deedMarkersString += ("xy("+(x+0.5)+","+(y+0.5)+"),");
Expand All @@ -90,7 +255,6 @@ public static void generateDeedsFile(File saveLocation, File db_wurmZones) throw
deedMarkersString += ("{icon: letter_"+firstLetter+"Icon})");
}
deedMarkersString += (".bindPopup(\""+resultSet.getString("NAME")+"\"));" + newLine);

count++;
}
System.out.println("Added "+count+" deeds to deeds.js...");
Expand All @@ -105,7 +269,7 @@ public static void generateDeedsFile(File saveLocation, File db_wurmZones) throw
connection.close();
bw.close();
}

public static void generateConfigFile(File saveLocation) throws IOException {
System.out.println("Writing config.js file...");
BufferedWriter bw = new BufferedWriter(new FileWriter(saveLocation.getAbsolutePath() + separator + "config.js", false));
Expand All @@ -125,7 +289,7 @@ public static void generateConfigFile(File saveLocation) throws IOException {
bw.append("};");
bw.close();
}

public static void setHTMLvars(MeshIO map) {
System.out.println("Generating config.js variables...");
html_actualMapSize = map.getSize();
Expand All @@ -144,5 +308,5 @@ public static void setHTMLvars(MeshIO map) {
}
html_mapMinZoom = 1;
}

}
6 changes: 6 additions & 0 deletions src/com/imraginbro/wurm/mapgen/FileManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class FileManagement {
public static void relocateFileVars() {
MapGen.map_topLayer = new File(MapGen.saveLocation.getAbsolutePath() + separator + "tmp" + separator + MapGen.map_topLayer.getName());
MapGen.db_wurmZones = new File(MapGen.saveLocation.getAbsolutePath() + separator + "tmp" + separator + MapGen.db_wurmZones.getName());
MapGen.db_wurmItems = new File(MapGen.saveLocation.getAbsolutePath() + separator + "tmp" + separator + MapGen.db_wurmItems.getName());
MapGen.db_wurmPlayers = new File(MapGen.saveLocation.getAbsolutePath() + separator + "tmp" + separator + MapGen.db_wurmPlayers.getName());
}

public static void saveToFile(BufferedImage newImg, File file) throws IOException {
Expand Down Expand Up @@ -183,6 +185,10 @@ public static boolean loadPropValues() {
return false;
}

MapGen.showDeeds = Boolean.parseBoolean(prop.getProperty("showDeeds", Boolean.toString(MapGen.showDeeds)));
MapGen.showGuardTowers = Boolean.parseBoolean(prop.getProperty("showGuardTowers", Boolean.toString(MapGen.showGuardTowers)));
MapGen.showStructures = Boolean.parseBoolean(prop.getProperty("showStructures", Boolean.toString(MapGen.showStructures)));

MapGen.gen_map_shading = Boolean.parseBoolean(prop.getProperty("mapGenerateShading", Boolean.toString(MapGen.gen_map_shading)));
MapGen.gen_map_shade_paths = Boolean.parseBoolean(prop.getProperty("mapShadePaths", Boolean.toString(MapGen.gen_map_shade_paths)));
MapGen.gen_map_water = Boolean.parseBoolean(prop.getProperty("mapGenerateWater", Boolean.toString(MapGen.gen_map_water)));
Expand Down
15 changes: 13 additions & 2 deletions src/com/imraginbro/wurm/mapgen/MapGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ public class MapGen {

public static File map_topLayer = null;
public static File db_wurmZones = null;
public static File db_wurmItems = null;
public static File db_wurmPlayers = null;

public static File[] fileBackupArray = new File[2];
public static File[] fileBackupArray = new File[4];

//vars for map gen
public static boolean gen_map_shading = true;
public static boolean gen_map_shade_paths = true;
public static boolean gen_map_water = true;
public static boolean gen_map_bridges = true;

//marker generation
public static boolean showDeeds = true;
public static boolean showGuardTowers = true;
public static boolean showStructures = true;

//config settings
public static boolean replaceFiles = true;
public static File wurmMapLocation = null;
Expand All @@ -55,6 +62,8 @@ public static void main(String[] args) throws Exception {

map_topLayer = new File(wurmMapLocation.getAbsolutePath() + separator + "top_layer.map");
db_wurmZones = new File(wurmMapLocation.getAbsolutePath() + separator + "sqlite" + separator + "wurmzones.db");
db_wurmItems = new File(wurmMapLocation.getAbsolutePath() + separator + "sqlite" + separator + "wurmitems.db");
db_wurmPlayers = new File(wurmMapLocation.getAbsolutePath() + separator + "sqlite" + separator + "wurmplayers.db");

if (!map_topLayer.exists()) {
System.out.println("[ERROR] Could not find top_layer.map! Stopping program.");
Expand All @@ -63,6 +72,8 @@ public static void main(String[] args) throws Exception {

fileBackupArray[0] = map_topLayer;
fileBackupArray[1] = db_wurmZones;
fileBackupArray[2] = db_wurmItems;
fileBackupArray[3] = db_wurmPlayers;

final long startTime = System.currentTimeMillis();

Expand Down Expand Up @@ -100,7 +111,7 @@ public static BufferedImage genMap(MeshIO map, int PIXEL_SIZE) throws SQLExcepti

BufferedImage mapImage = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_INT_ARGB);

ExecutorService executor = Executors.newFixedThreadPool(2);
ExecutorService executor = Executors.newFixedThreadPool(5);
System.out.println("Starting multi-thread image generation");

for (int y = 0; y < MAP_SIZE; y++) {
Expand Down
13 changes: 12 additions & 1 deletion src/resources/WurmMapGen.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@ mapShadePaths=true
mapGenerateWater=true

#Generate bridges on map
mapGenerateBridges=true
mapGenerateBridges=true

###Map Items

#Show deeds on map
showDeeds=true

#Show guard towers on map
showGuardTowers=true

#Show structures on map
showStructures=true
Binary file modified src/resources/required.zip
Binary file not shown.

0 comments on commit 2cf17de

Please sign in to comment.