Files
magicbane/src/engine/InterestManagement/Terrain.java
T

223 lines
7.3 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.InterestManagement;
import engine.gameManager.ZoneManager;
import engine.math.Vector2f;
import engine.math.Vector3fImmutable;
import engine.objects.Zone;
2023-10-08 22:44:34 -04:00
import org.pmw.tinylog.Logger;
2022-04-30 09:41:17 -04:00
import java.util.HashMap;
2023-10-09 07:05:54 -04:00
import static java.lang.Math.PI;
2023-10-08 09:18:43 -04:00
public class Terrain {
2022-04-30 09:41:17 -04:00
// Class variables
2023-10-08 09:49:49 -04:00
public static final HashMap<Integer, short[][]> _heightmap_pixel_cache = new HashMap<>();
public short[][] terrain_pixel_data;
public Vector2f terrain_size = new Vector2f();
public Vector2f cell_size = new Vector2f();
public Vector2f cell_count = new Vector2f();
2023-10-08 09:04:15 -04:00
public float terrain_scale;
2023-10-09 07:05:54 -04:00
public float min_blend;
public float max_blend;
2023-10-08 22:36:26 -04:00
public int heightmap;
2023-10-09 07:05:54 -04:00
Zone zone;
2023-10-08 09:04:15 -04:00
2023-10-08 20:26:37 -04:00
public Terrain(Zone zone) {
2023-10-08 22:49:13 -04:00
this.zone = zone;
this.heightmap = this.zone.terrain_image;
2023-10-08 22:36:26 -04:00
2023-10-11 11:32:43 -04:00
// Configure PLANAR zones to use the same
// 16x16 pixel image that all other flat
// terrains share.
2023-10-08 22:36:26 -04:00
2023-10-08 22:49:13 -04:00
if (this.zone.terrain_type.equals("PLANAR"))
2023-10-08 22:36:26 -04:00
this.heightmap = 1006300;
2023-10-11 11:32:43 -04:00
// Load pixel data for this terrain from cache
2023-10-08 20:26:37 -04:00
2023-10-08 22:40:31 -04:00
this.terrain_pixel_data = Terrain._heightmap_pixel_cache.get(heightmap);
2022-04-30 09:41:17 -04:00
2023-10-08 22:44:34 -04:00
if (terrain_pixel_data == null)
2023-10-11 11:32:43 -04:00
Logger.error("Pixel map empty for zone: " + this.zone.getObjectUUID() + ":" + this.zone.zoneName);
// Configure terrain based on zone properties
this.terrain_size.x = this.zone.major_radius * 2;
this.terrain_size.y = this.zone.minor_radius * 2;
2023-10-08 22:44:34 -04:00
2023-10-08 21:50:27 -04:00
this.cell_count.x = this.terrain_pixel_data.length - 1;
this.cell_count.y = this.terrain_pixel_data[0].length - 1;
this.cell_size.x = terrain_size.x / this.cell_count.x;
this.cell_size.y = terrain_size.y / this.cell_count.y;
2023-10-09 07:05:54 -04:00
this.max_blend = this.zone.max_blend / this.zone.major_radius;
this.min_blend = this.zone.min_blend / this.zone.minor_radius;
2023-10-08 22:49:13 -04:00
this.terrain_scale = this.zone.terrain_max_y / 255f;
2022-04-30 09:41:17 -04:00
}
2023-03-22 05:41:39 -04:00
public static Zone getNextZoneWithTerrain(Zone zone) {
2023-10-11 11:32:43 -04:00
// Not all zones have a terrain. Some are for display only
// and heights returned are from the parent heightmap. This
// is controlled in the JSON via the has_terrain_gen field.
2023-10-08 09:09:50 -04:00
Zone terrain_zone = zone;
2023-03-22 05:41:39 -04:00
2023-10-09 06:14:06 -04:00
if (zone == null)
2023-10-09 06:16:25 -04:00
return ZoneManager.seaFloor;
2023-10-09 06:14:06 -04:00
2023-10-08 09:59:13 -04:00
if (zone.terrain != null)
2023-03-22 05:41:39 -04:00
return zone;
2023-10-09 06:16:25 -04:00
if (zone.equals(ZoneManager.seaFloor))
2023-03-22 05:41:39 -04:00
return zone;
2023-10-08 09:59:13 -04:00
while (terrain_zone.terrain == null)
2023-10-08 09:09:50 -04:00
terrain_zone = terrain_zone.parent;
2023-03-22 05:41:39 -04:00
2023-10-08 09:09:50 -04:00
return terrain_zone;
2023-03-22 05:41:39 -04:00
}
2023-09-11 11:01:09 -04:00
public static float getWorldHeight(Zone currentZone, Vector3fImmutable worldLoc) {
2022-04-30 09:41:17 -04:00
2023-10-08 09:09:50 -04:00
Zone terrainZone;
2022-04-30 09:41:17 -04:00
2023-10-11 11:32:43 -04:00
// Retrieve the next zone with a terrain defined.
2022-04-30 09:41:17 -04:00
2023-10-08 09:09:50 -04:00
terrainZone = getNextZoneWithTerrain(currentZone);
2023-09-11 11:01:09 -04:00
2023-09-16 13:18:36 -04:00
// Transform world loc into zone space coordinate system
2022-04-30 09:41:17 -04:00
2023-10-08 09:09:50 -04:00
Vector2f terrainLoc = ZoneManager.worldToZoneSpace(worldLoc, terrainZone);
2022-04-30 09:41:17 -04:00
2023-10-09 07:05:54 -04:00
// Interpolate height for this position in terrain
2022-04-30 09:41:17 -04:00
2023-10-09 07:47:50 -04:00
float interpolatedChildHeight = terrainZone.terrain.getInterpolatedTerrainHeight(terrainLoc);
interpolatedChildHeight += terrainZone.worldAltitude;
2023-09-16 13:18:36 -04:00
2023-10-10 12:34:21 -04:00
return interpolatedChildHeight;
2022-04-30 09:41:17 -04:00
}
public static float getWorldHeight(Vector3fImmutable worldLoc) {
Zone currentZone = ZoneManager.findSmallestZone(worldLoc);
if (currentZone == null)
return 0;
2023-10-08 09:04:15 -04:00
2023-09-11 11:01:09 -04:00
return getWorldHeight(currentZone, worldLoc);
2022-04-30 09:41:17 -04:00
}
2023-10-08 09:09:50 -04:00
public Vector2f getTerrainCell(Vector2f terrainLoc) {
2023-07-15 09:23:48 -04:00
2023-10-08 09:49:49 -04:00
Vector2f terrain_cell = new Vector2f(terrainLoc.x / this.cell_size.x, terrainLoc.y / this.cell_size.y);
2023-07-15 09:23:48 -04:00
2023-10-11 11:23:20 -04:00
// Clamp values when standing directly on pole
2023-10-07 20:32:08 -04:00
2023-10-11 11:23:20 -04:00
terrain_cell.x = Math.max(0, Math.min(this.cell_count.x - 1, terrain_cell.x));
terrain_cell.y = Math.max(0, Math.min(this.cell_count.y - 1, terrain_cell.y));
2023-10-07 20:47:35 -04:00
2023-10-08 09:49:49 -04:00
return terrain_cell;
2023-07-15 09:23:48 -04:00
}
2023-10-08 09:04:15 -04:00
public float getInterpolatedTerrainHeight(Vector2f terrainLoc) {
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
try {
float interpolatedHeight;
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
Vector2f terrain_cell = getTerrainCell(terrainLoc);
2023-07-15 09:23:48 -04:00
2023-10-11 11:32:43 -04:00
int pixel_x = (int) Math.floor(terrain_cell.x);
2023-10-11 11:34:37 -04:00
int pixel_y = (int) Math.floor(terrain_cell.y);
2023-10-08 09:04:15 -04:00
2023-10-11 11:32:43 -04:00
Vector2f pixel_offset = new Vector2f(terrain_cell.x % 1, terrain_cell.y % 1);
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
//get 4 surrounding vertices from the pixel array.
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
float topLeftHeight;
float topRightHeight;
float bottomLeftHeight;
float bottomRightHeight;
2023-07-15 09:23:48 -04:00
2023-10-11 11:34:37 -04:00
topLeftHeight = terrain_pixel_data[pixel_x][pixel_y];
topRightHeight = terrain_pixel_data[pixel_x + 1][pixel_y];
bottomLeftHeight = terrain_pixel_data[pixel_x][pixel_y + 1];
bottomRightHeight = terrain_pixel_data[pixel_x + 1][pixel_y + 1];
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
// Interpolate between the 4 vertices
2023-09-20 14:20:22 -04:00
2023-10-11 11:32:43 -04:00
interpolatedHeight = topLeftHeight * (1 - pixel_offset.x) * (1 - pixel_offset.y);
interpolatedHeight += topRightHeight * (1 - pixel_offset.y) * (pixel_offset.x);
interpolatedHeight += (bottomLeftHeight * (1 - pixel_offset.x) * pixel_offset.y);
interpolatedHeight += (bottomRightHeight * pixel_offset.y * pixel_offset.x);
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
interpolatedHeight *= this.terrain_scale; // Scale height
2023-07-15 09:23:48 -04:00
2023-10-11 10:40:27 -04:00
return interpolatedHeight;
2023-10-11 10:53:58 -04:00
2023-10-11 10:40:27 -04:00
} catch (Exception e) {
2023-10-11 10:53:58 -04:00
Logger.error(this.zone.zoneName + ":" + this.zone.getObjectUUID() + e);
2023-10-11 10:42:04 -04:00
return 0;
2023-10-11 10:40:27 -04:00
}
2023-07-15 09:23:48 -04:00
}
2023-10-09 07:05:54 -04:00
2023-10-09 09:35:24 -04:00
public float terrainBlend(Vector2f terrainLoc) {
2023-10-09 07:05:54 -04:00
// Normalize terrain loc
Vector2f normalizedLoc = new Vector2f(terrainLoc.x / this.terrain_size.x,
terrainLoc.y / terrain_size.y);
float minp = this.zone.min_blend / this.zone.major_radius;
float maxp = this.zone.max_blend / this.zone.major_radius;
2023-10-09 09:32:20 -04:00
float minpy = this.zone.min_blend / this.zone.minor_radius;
float maxpy = this.zone.max_blend / this.zone.minor_radius;
2023-10-09 07:05:54 -04:00
float xval;
if (minp > 0.4f)
xval = minp;
else
xval = Math.min(maxp, 0.4f);
float yval;
if (minpy > 0.4f)
yval = minpy;
else
yval = Math.min(maxpy, 0.4f);
float value;
2023-10-09 09:45:04 -04:00
if (normalizedLoc.x <= 1 - xval || normalizedLoc.x <= normalizedLoc.y) {
2023-10-09 07:05:54 -04:00
2023-10-09 09:45:04 -04:00
if (normalizedLoc.y < 1 - yval)
2023-10-09 07:05:54 -04:00
return 1;
value = (normalizedLoc.y - (1 - yval)) / yval;
} else
value = (normalizedLoc.x - (1 - xval)) / xval;
value = (float) Math.atan((0.5f - value) * PI);
return (value + 1) * 0.5f;
}
2022-04-30 09:41:17 -04:00
}