forked from MagicBane/Server
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
372 lines
12 KiB
372 lines
12 KiB
3 years ago
|
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
|
||
|
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
|
||
|
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
|
||
|
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
|
||
|
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
|
||
|
// Magicbane Emulator Project © 2013 - 2022
|
||
|
// www.magicbane.com
|
||
|
|
||
|
package engine.InterestManagement;
|
||
|
|
||
|
import engine.Enum;
|
||
1 year ago
|
import engine.gameManager.ConfigManager;
|
||
3 years ago
|
import engine.gameManager.DbManager;
|
||
|
import engine.gameManager.ZoneManager;
|
||
|
import engine.math.Vector2f;
|
||
|
import engine.math.Vector3fImmutable;
|
||
|
import engine.objects.Zone;
|
||
|
import org.pmw.tinylog.Logger;
|
||
|
|
||
|
import javax.imageio.ImageIO;
|
||
|
import java.awt.*;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import java.io.File;
|
||
|
import java.io.IOException;
|
||
1 year ago
|
import java.nio.file.Files;
|
||
|
import java.nio.file.Path;
|
||
|
import java.nio.file.Paths;
|
||
3 years ago
|
import java.sql.ResultSet;
|
||
|
import java.sql.SQLException;
|
||
|
import java.util.HashMap;
|
||
1 year ago
|
import java.util.stream.Stream;
|
||
3 years ago
|
|
||
1 year ago
|
public class Terrain {
|
||
3 years ago
|
|
||
|
// Class variables
|
||
|
|
||
1 year ago
|
public static final HashMap<Integer, Terrain> heightmapByLoadNum = new HashMap<>();
|
||
1 year ago
|
|
||
1 year ago
|
public static final HashMap<Integer, short[][]> _pixelData = new HashMap<>();
|
||
3 years ago
|
|
||
|
// Bootstrap Tracking
|
||
|
public static int heightMapsCreated = 0;
|
||
1 year ago
|
public static Terrain playerCityTerrain;
|
||
3 years ago
|
|
||
|
// Heightmap data for this heightmap
|
||
1 year ago
|
public final int heightMapID;
|
||
|
public final int maxHeight;
|
||
|
public final int fullExtentsX;
|
||
|
public final int fullExtentsY;
|
||
1 year ago
|
public final int zoneLoadID;
|
||
|
public BufferedImage heightmapImage;
|
||
1 year ago
|
public float cell_size_x;
|
||
|
public float cell_size_y;
|
||
1 year ago
|
public float seaLevel = 0;
|
||
1 year ago
|
public short[][] pixelColorValues;
|
||
|
public int cell_count_x;
|
||
|
public int cell_count_y;
|
||
1 year ago
|
|
||
1 year ago
|
public float terrain_scale;
|
||
|
|
||
1 year ago
|
public Terrain(ResultSet rs) throws SQLException {
|
||
3 years ago
|
|
||
|
this.heightMapID = rs.getInt("heightMapID");
|
||
|
this.maxHeight = rs.getInt("maxHeight");
|
||
|
int halfExtentsX = rs.getInt("xRadius");
|
||
|
int halfExtentsY = rs.getInt("zRadius");
|
||
|
this.zoneLoadID = rs.getInt("zoneLoadID");
|
||
|
this.seaLevel = rs.getFloat("seaLevel");
|
||
|
|
||
|
// Cache the full extents to avoid the calculation
|
||
|
|
||
|
this.fullExtentsX = halfExtentsX * 2;
|
||
|
this.fullExtentsY = halfExtentsY * 2;
|
||
|
|
||
|
this.heightmapImage = null;
|
||
1 year ago
|
File imageFile = new File(ConfigManager.DEFAULT_DATA_DIR + "heightmaps/" + this.heightMapID + ".bmp");
|
||
3 years ago
|
|
||
|
// early exit if no image file was found. Will log in caller.
|
||
|
|
||
|
if (!imageFile.exists())
|
||
|
return;
|
||
|
|
||
|
// load the heightmap image.
|
||
|
|
||
|
try {
|
||
|
this.heightmapImage = ImageIO.read(imageFile);
|
||
|
} catch (IOException e) {
|
||
1 year ago
|
Logger.error("***Error loading heightmap data for heightmap " + this.heightMapID + e);
|
||
3 years ago
|
}
|
||
|
|
||
|
// Calculate the data we do not load from table
|
||
|
|
||
1 year ago
|
this.cell_count_x = this.heightmapImage.getWidth() - 1;
|
||
|
this.cell_size_x = this.fullExtentsX / (float) cell_count_x;
|
||
1 year ago
|
|
||
1 year ago
|
this.cell_count_y = this.heightmapImage.getHeight() - 1;
|
||
|
this.cell_size_y = this.fullExtentsY / (float) cell_count_y;
|
||
|
|
||
|
this.terrain_scale = this.maxHeight / 255f;
|
||
3 years ago
|
|
||
|
// Generate pixel array from image data
|
||
|
|
||
1 year ago
|
generatePixelData(this);
|
||
3 years ago
|
|
||
1 year ago
|
Terrain.heightmapByLoadNum.put(this.zoneLoadID, this);
|
||
3 years ago
|
|
||
|
heightMapsCreated++;
|
||
|
}
|
||
|
|
||
|
//Created for PlayerCities
|
||
1 year ago
|
public Terrain() {
|
||
3 years ago
|
|
||
|
this.heightMapID = 999999;
|
||
|
this.maxHeight = 5; // for real...
|
||
1 year ago
|
int halfExtentsX = (int) Enum.CityBoundsType.ZONE.halfExtents;
|
||
|
int halfExtentsY = (int) Enum.CityBoundsType.ZONE.halfExtents;
|
||
3 years ago
|
this.zoneLoadID = 0;
|
||
|
this.seaLevel = 0;
|
||
|
|
||
|
// Cache the full extents to avoid the calculation
|
||
|
|
||
|
this.fullExtentsX = halfExtentsX * 2;
|
||
|
this.fullExtentsY = halfExtentsY * 2;
|
||
|
|
||
|
this.heightmapImage = null;
|
||
|
|
||
|
// Calculate the data we do not load from table
|
||
|
|
||
1 year ago
|
this.cell_size_x = halfExtentsX;
|
||
|
this.cell_size_y = halfExtentsY;
|
||
3 years ago
|
|
||
1 year ago
|
this.pixelColorValues = new short[this.fullExtentsX][this.fullExtentsY];
|
||
3 years ago
|
|
||
1 year ago
|
for (int y = 0; y < this.fullExtentsY; y++) {
|
||
|
for (int x = 0; x < this.fullExtentsX; x++) {
|
||
1 year ago
|
pixelColorValues[x][y] = 255;
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
cell_count_x = this.pixelColorValues.length - 1;
|
||
|
cell_count_y = this.pixelColorValues[0].length - 1;
|
||
|
this.terrain_scale = this.maxHeight / 255f;
|
||
3 years ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
public Terrain(Zone zone) {
|
||
3 years ago
|
|
||
|
this.heightMapID = 999999;
|
||
|
this.maxHeight = 0;
|
||
1 year ago
|
int halfExtentsX = (int) zone.bounds.getHalfExtents().x;
|
||
|
int halfExtentsY = (int) zone.bounds.getHalfExtents().y;
|
||
3 years ago
|
this.zoneLoadID = 0;
|
||
|
this.seaLevel = 0;
|
||
|
|
||
|
// Cache the full extents to avoid the calculation
|
||
|
|
||
|
this.fullExtentsX = halfExtentsX * 2;
|
||
|
this.fullExtentsY = halfExtentsY * 2;
|
||
|
|
||
|
this.heightmapImage = null;
|
||
|
|
||
|
// Calculate the data we do not load from table
|
||
|
|
||
1 year ago
|
this.cell_size_x = halfExtentsX;
|
||
|
this.cell_size_y = halfExtentsY;
|
||
3 years ago
|
|
||
1 year ago
|
this.pixelColorValues = new short[this.fullExtentsX][this.fullExtentsY];
|
||
3 years ago
|
|
||
1 year ago
|
for (int y = 0; y < this.fullExtentsY; y++) {
|
||
|
for (int x = 0; x < this.fullExtentsX; x++) {
|
||
1 year ago
|
pixelColorValues[x][y] = 0;
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
cell_count_x = this.pixelColorValues.length - 1;
|
||
|
cell_count_y = this.pixelColorValues[0].length - 1;
|
||
|
this.terrain_scale = this.maxHeight / 255f;
|
||
3 years ago
|
}
|
||
|
|
||
|
public static void GeneratePlayerCityHeightMap() {
|
||
|
|
||
1 year ago
|
Terrain.playerCityTerrain = new Terrain();
|
||
3 years ago
|
|
||
|
}
|
||
1 year ago
|
|
||
3 years ago
|
public static void GenerateCustomHeightMap(Zone zone) {
|
||
|
|
||
1 year ago
|
Terrain heightMap = new Terrain(zone);
|
||
3 years ago
|
|
||
1 year ago
|
Terrain.heightmapByLoadNum.put(zone.template, heightMap);
|
||
3 years ago
|
|
||
|
}
|
||
|
|
||
2 years ago
|
public static Zone getNextZoneWithTerrain(Zone zone) {
|
||
|
|
||
1 year ago
|
Zone terrain_zone = zone;
|
||
2 years ago
|
|
||
|
if (zone.getHeightMap() != null)
|
||
|
return zone;
|
||
|
|
||
|
if (zone.equals(ZoneManager.getSeaFloor()))
|
||
|
return zone;
|
||
|
|
||
1 year ago
|
while (terrain_zone.getHeightMap() == null)
|
||
|
terrain_zone = terrain_zone.parent;
|
||
2 years ago
|
|
||
1 year ago
|
return terrain_zone;
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
public static float getWorldHeight(Zone currentZone, Vector3fImmutable worldLoc) {
|
||
3 years ago
|
|
||
1 year ago
|
Zone terrainZone;
|
||
3 years ago
|
|
||
1 year ago
|
// Seafloor is rather flat.
|
||
3 years ago
|
|
||
2 years ago
|
if (currentZone == ZoneManager.getSeaFloor())
|
||
1 year ago
|
return currentZone.worldAltitude;
|
||
|
|
||
|
// Retrieve the next zone with a heightmap attached.
|
||
|
// Zones without a heightmap use the next zone up the
|
||
|
// tree to calculate heights from.
|
||
|
|
||
1 year ago
|
terrainZone = getNextZoneWithTerrain(currentZone);
|
||
2 years ago
|
|
||
1 year ago
|
// Transform world loc into zone space coordinate system
|
||
3 years ago
|
|
||
1 year ago
|
Vector2f terrainLoc = ZoneManager.worldToZoneSpace(worldLoc, terrainZone);
|
||
1 year ago
|
|
||
1 year ago
|
// Interpolate height for this position using pixel array.
|
||
3 years ago
|
|
||
1 year ago
|
float interpolatedTerrainHeight = terrainZone.getHeightMap().getInterpolatedTerrainHeight(terrainLoc);
|
||
|
interpolatedTerrainHeight += terrainZone.worldAltitude;
|
||
3 years ago
|
|
||
1 year ago
|
return interpolatedTerrainHeight;
|
||
1 year ago
|
|
||
|
}
|
||
|
|
||
3 years ago
|
public static float getWorldHeight(Vector3fImmutable worldLoc) {
|
||
|
|
||
|
Zone currentZone = ZoneManager.findSmallestZone(worldLoc);
|
||
|
|
||
|
if (currentZone == null)
|
||
|
return 0;
|
||
1 year ago
|
|
||
1 year ago
|
return getWorldHeight(currentZone, worldLoc);
|
||
3 years ago
|
|
||
|
}
|
||
|
|
||
|
public static void loadAlHeightMaps() {
|
||
|
|
||
|
// Load the heightmaps into staging hashmap keyed by HashMapID
|
||
|
|
||
|
DbManager.HeightMapQueries.LOAD_ALL_HEIGHTMAPS();
|
||
|
|
||
|
//generate static player city heightmap.
|
||
|
|
||
1 year ago
|
Terrain.GeneratePlayerCityHeightMap();
|
||
1 year ago
|
|
||
1 year ago
|
Logger.info(Terrain.heightmapByLoadNum.size() + " Heightmaps cached.");
|
||
1 year ago
|
|
||
1 year ago
|
// Load pixel data for heightmaps
|
||
1 year ago
|
|
||
1 year ago
|
try (Stream<Path> filePathStream = Files.walk(Paths.get(ConfigManager.DEFAULT_DATA_DIR + "heightmaps/TARGA/"))) {
|
||
1 year ago
|
filePathStream.forEach(filePath -> {
|
||
|
|
||
|
if (Files.isRegularFile(filePath)) {
|
||
|
File imageFile = filePath.toFile();
|
||
|
|
||
|
try {
|
||
|
BufferedImage heightmapImage = ImageIO.read(imageFile);
|
||
1 year ago
|
|
||
1 year ago
|
// Generate pixel data for this heightmap. RPG channels are all the same
|
||
1 year ago
|
// in this greyscale TGA heightmap. We will choose red.
|
||
|
|
||
1 year ago
|
short[][] colorData = new short[heightmapImage.getWidth()][heightmapImage.getHeight()];
|
||
1 year ago
|
|
||
1 year ago
|
for (int y = 0; y < heightmapImage.getHeight(); y++)
|
||
1 year ago
|
for (int x = 0; x < heightmapImage.getWidth(); x++) {
|
||
|
|
||
|
Color color = new Color(heightmapImage.getRGB(x, y));
|
||
1 year ago
|
colorData[x][y] = (short) color.getRed();
|
||
1 year ago
|
}
|
||
|
|
||
|
// Insert color data into lookup table
|
||
|
|
||
1 year ago
|
int heightMapID = Integer.parseInt(imageFile.getName().substring(0, imageFile.getName().lastIndexOf(".")));
|
||
|
_pixelData.put(heightMapID, colorData);
|
||
1 year ago
|
|
||
1 year ago
|
} catch (IOException e) {
|
||
1 year ago
|
Logger.error(e);
|
||
1 year ago
|
}
|
||
|
|
||
|
}
|
||
1 year ago
|
}); // Try with resources block
|
||
|
|
||
1 year ago
|
} catch (IOException e) {
|
||
|
Logger.error(e);
|
||
|
}
|
||
1 year ago
|
|
||
3 years ago
|
}
|
||
|
|
||
1 year ago
|
private static void generatePixelData(Terrain terrain) {
|
||
1 year ago
|
|
||
|
Color color;
|
||
|
|
||
|
// Generate altitude lookup table for this heightmap
|
||
|
|
||
1 year ago
|
terrain.pixelColorValues = new short[terrain.heightmapImage.getWidth()][terrain.heightmapImage.getHeight()];
|
||
1 year ago
|
|
||
1 year ago
|
for (int y = 0; y < terrain.heightmapImage.getHeight(); y++) {
|
||
|
for (int x = 0; x < terrain.heightmapImage.getWidth(); x++) {
|
||
1 year ago
|
|
||
1 year ago
|
color = new Color(terrain.heightmapImage.getRGB(x, y));
|
||
|
terrain.pixelColorValues[x][y] = (short) color.getRed();
|
||
1 year ago
|
}
|
||
|
}
|
||
1 year ago
|
|
||
|
}
|
||
|
|
||
1 year ago
|
public Vector2f getTerrainCell(Vector2f terrainLoc) {
|
||
1 year ago
|
|
||
1 year ago
|
float terrainCell_x = terrainLoc.x / this.cell_size_x;
|
||
|
float terrainCell_y = terrainLoc.y / this.cell_size_y;
|
||
1 year ago
|
|
||
1 year ago
|
// Clamp values when standing directly on max pole
|
||
1 year ago
|
|
||
1 year ago
|
if (terrainCell_x >= this.cell_count_x)
|
||
|
terrainCell_x = terrainCell_x - 1;
|
||
1 year ago
|
|
||
1 year ago
|
if (terrainCell_y >= this.cell_count_y)
|
||
|
terrainCell_y = terrainCell_x - 1;
|
||
1 year ago
|
|
||
1 year ago
|
return new Vector2f(terrainCell_x, terrainCell_y);
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
public float getInterpolatedTerrainHeight(Vector2f terrainLoc) {
|
||
1 year ago
|
|
||
1 year ago
|
float interpolatedHeight;
|
||
1 year ago
|
|
||
1 year ago
|
Vector2f terrain_cell = getTerrainCell(terrainLoc);
|
||
1 year ago
|
|
||
1 year ago
|
int gridX = (int) Math.floor(terrain_cell.x);
|
||
|
int gridY = (int) Math.floor(terrain_cell.y);
|
||
1 year ago
|
|
||
1 year ago
|
float offsetX = terrain_cell.x % 1;
|
||
|
float offsetY = terrain_cell.y % 1;
|
||
1 year ago
|
|
||
1 year ago
|
//get 4 surrounding vertices from the pixel array.
|
||
1 year ago
|
|
||
1 year ago
|
float topLeftHeight;
|
||
|
float topRightHeight;
|
||
|
float bottomLeftHeight;
|
||
|
float bottomRightHeight;
|
||
1 year ago
|
|
||
|
topLeftHeight = pixelColorValues[gridX][gridY];
|
||
1 year ago
|
topRightHeight = pixelColorValues[gridX + 1][gridY];
|
||
|
bottomLeftHeight = pixelColorValues[gridX][gridY + 1];
|
||
|
bottomRightHeight = pixelColorValues[gridX + 1][gridY + 1];
|
||
1 year ago
|
|
||
1 year ago
|
// Interpolate between the 4 vertices
|
||
|
|
||
1 year ago
|
interpolatedHeight = topRightHeight * (1 - offsetY) * (offsetX);
|
||
|
interpolatedHeight += (bottomRightHeight * offsetY * offsetX);
|
||
|
interpolatedHeight += (bottomLeftHeight * (1 - offsetX) * offsetY);
|
||
|
interpolatedHeight += (topLeftHeight * (1 - offsetX) * (1 - offsetY));
|
||
|
|
||
1 year ago
|
interpolatedHeight *= this.terrain_scale; // Scale height
|
||
1 year ago
|
|
||
|
return interpolatedHeight;
|
||
|
}
|
||
|
|
||
3 years ago
|
}
|