/** * Direct port of 317HD / RLHD's ProceduralGenerator: * - Computes smooth 3D terrain normals across the 105x105 heightmap grid. * - Resolves per-tile & per-vertex 317HD GroundMaterials from 317 Floor IDs and HSL colors. * - Computes cross-tile vertex material splatting so cobblestone paths, gravel, dirt, and grass * blend organically via height-map displacement across tile boundaries. * - Extracts dynamic point light emitters from world objects. */ public final class HdProceduralGenerator { // Smooth terrain normals: [plane][vx][vz][0..2 (nx, ny, nz)] public final float[][][][] tileNormals = new float[4][105][105][3]; // Resolved material layer per tile: [plane][tx][tz] public final int[][][] tileUnderlayMat = new int[4][104][104]; public final int[][][] tileOverlayMat = new int[4][104][104]; public final boolean[][][] tileHasOverlay = new boolean[4][104][104]; public final boolean[][][] tileIsIndoor = new boolean[4][104][104]; // Per-corner blended material on the 105x105 vertex grid for seamless cross-tile splatting public final int[][][] cornerBlendMat = new int[4][105][105]; public final int[][][] cornerBlendColor = new int[4][105][105]; // Dynamic Point Lights extracted from the scene (x, y, z, radius, r, g, b, intensity) public static final int MAX_SCENE_LIGHTS = 128; public final float[] sceneLightPosRad = new float[MAX_SCENE_LIGHTS * 4]; public final float[] sceneLightColorInt = new float[MAX_SCENE_LIGHTS * 4]; public int sceneLightCount = 0; public int baseX = 3200; public int baseZ = 3200; public HdProceduralGenerator() { for (int p = 0; p < 4; p++) { for (int vx = 0; vx < 105; vx++) { for (int vz = 0; vz < 105; vz++) { tileNormals[p][vx][vz][0] = 0.0f; tileNormals[p][vx][vz][1] = -1.0f; tileNormals[p][vx][vz][2] = 0.0f; cornerBlendMat[p][vx][vz] = HdMaterial.HD_GRASS_1; } } for (int x = 0; x < 104; x++) { for (int z = 0; z < 104; z++) { tileUnderlayMat[p][x][z] = HdMaterial.HD_GRASS_1; tileOverlayMat[p][x][z] = HdMaterial.HD_COBBLESTONE_1; } } } } public void generateSceneData(Game game, ObjectManager om, WorldController wc) { if (wc == null) return; int baseX = (game != null) ? game.baseX : 3200; int baseZ = (game != null) ? game.baseY : 3200; this.baseX = baseX; this.baseZ = baseZ; int[][][] heights = (om != null && om.anIntArrayArrayArray129 != null) ? om.anIntArrayArrayArray129 : wc.anIntArrayArrayArray440; if (heights == null) return; byte[][][] underlays = (om != null) ? om.aByteArrayArrayArray142 : null; byte[][][] overlays = (om != null) ? om.aByteArrayArrayArray130 : null; byte[][][] shapes = (om != null) ? om.aByteArrayArrayArray136 : null; byte[][][] tileFlags = (om != null && om.aByteArrayArrayArray149 != null) ? om.aByteArrayArrayArray149 : ((game != null) ? game.byteGroundArray : null); // 0. Detect indoor/roofed building tiles (RS2 roof flag bit 4 in byteGroundArray) for (int p = 0; p < 4; p++) { for (int x = 0; x < 104; x++) { for (int z = 0; z < 104; z++) { boolean indoor = false; if (tileFlags != null && ((tileFlags[p][x][z] & 4) != 0 || (tileFlags[0][x][z] & 4) != 0)) { indoor = true; // Open-air archways / gatehouses (unroofed on both opposite sides within 4 tiles with no blocking wall) // should keep their outdoor ground/cobblestone material instead of indoor marble. if (p == 0 && (isOpenAirPassThrough(tileFlags, wc, x, z, 1, 0) || isOpenAirPassThrough(tileFlags, wc, x, z, 0, 1))) { indoor = false; } } tileIsIndoor[p][x][z] = indoor; } } } // 1. Compute smooth per-vertex terrain normals from central height differences for (int p = 0; p < 4; p++) { for (int vx = 0; vx < 105; vx++) { int xm1 = Math.max(0, vx - 1); int xp1 = Math.min(104, vx + 1); for (int vz = 0; vz < 105; vz++) { int zm1 = Math.max(0, vz - 1); int zp1 = Math.min(104, vz + 1); float dhDx = (float) (heights[p][xp1][vz] - heights[p][xm1][vz]); float dhDz = (float) (heights[p][vx][zp1] - heights[p][vx][zm1]); // In RS2 coords: +X is East, -Y is Up, +Z is North; 2 tiles = 256 units float nx = dhDx; float ny = -256.0f; float nz = dhDz; float invLen = (float) (1.0 / Math.sqrt(nx * nx + ny * ny + nz * nz)); tileNormals[p][vx][vz][0] = nx * invLen; tileNormals[p][vx][vz][1] = ny * invLen; tileNormals[p][vx][vz][2] = nz * invLen; } } } // 2. Resolve per-tile Underlay & Overlay 317HD Materials for (int p = 0; p < 4; p++) { for (int x = 0; x < 104; x++) { for (int z = 0; z < 104; z++) { int uId = (underlays != null) ? (underlays[p][x][z] & 0xFF) : 0; int oId = (overlays != null) ? (overlays[p][x][z] & 0xFF) : 0; Ground g = wc.groundArray[p][x][z]; int uHsl = (20 << 10) | (4 << 7) | 40; // default grass HSL int oHsl = (8 << 10) | (1 << 7) | 48; // default stone/cobble HSL if (g != null) { if (g.aClass43_1311 != null) { int c = g.aClass43_1311.anInt716; if (c != 0xbc614e && c >= 0) { if (oId > 0 || g.aClass43_1311.isOverlay) oHsl = c; else uHsl = c; } } else if (g.aClass40_1312 != null) { Class40 ct = g.aClass40_1312; for (int f = 0; f < ct.anIntArray676.length; f++) { if (ct.isOverlayFace != null && ct.isOverlayFace[f]) { if (ct.anIntArray676[f] != 0xbc614e) oHsl = ct.anIntArray676[f]; } else { if (ct.anIntArray676[f] != 0xbc614e) uHsl = ct.anIntArray676[f]; } } } } HdGroundMaterial uGroup = HdGroundMaterial.resolve(p, uId, uHsl, false); tileUnderlayMat[p][x][z] = uGroup.getMaterialForVertex(p, baseX + x, baseZ + z); if (oId > 0 || (g != null && g.aClass43_1311 != null && g.aClass43_1311.isOverlay)) { HdGroundMaterial oGroup = HdGroundMaterial.resolve(p, oId, oHsl, true); int oMat = oGroup.getMaterialForVertex(p, baseX + x, baseZ + z); tileOverlayMat[p][x][z] = oMat; tileHasOverlay[p][x][z] = (oMat != HdMaterial.NONE); } else { tileOverlayMat[p][x][z] = HdMaterial.NONE; tileHasOverlay[p][x][z] = false; } } } } // 3. Compute Per-Corner Blended Material & Color on the 105x105 Grid for (int p = 0; p < 4; p++) { for (int vx = 0; vx < 105; vx++) { for (int vz = 0; vz < 105; vz++) { int chosenMat = HdMaterial.HD_GRASS_1; int chosenHsl = -1; boolean foundOverlayPath = false; for (int dx = -1; dx <= 0; dx++) { for (int dz = -1; dz <= 0; dz++) { int tx = vx + dx; int tz = vz + dz; if (tx < 0 || tx >= 104 || tz < 0 || tz >= 104) continue; Ground g = wc.groundArray[p][tx][tz]; if (g == null) continue; int shape = (shapes != null) ? (shapes[p][tx][tz] & 0xFF) : 0; if (tileHasOverlay[p][tx][tz] && (g.aClass43_1311 != null || shape == 0)) { chosenMat = tileOverlayMat[p][tx][tz]; if (g.aClass43_1311 != null && g.aClass43_1311.anInt716 != 0xbc614e) { chosenHsl = g.aClass43_1311.anInt716; } foundOverlayPath = true; } else if (!foundOverlayPath) { chosenMat = tileUnderlayMat[p][tx][tz]; if (g.aClass43_1311 != null && g.aClass43_1311.anInt716 != 0xbc614e) { chosenHsl = g.aClass43_1311.anInt716; } } } } cornerBlendMat[p][vx][vz] = chosenMat; cornerBlendColor[p][vx][vz] = chosenHsl; } } } // 4. Extract Dynamic Point Lights from Light-Emitting Scene Objects sceneLightCount = 0; for (int p = 0; p < 4 && sceneLightCount < MAX_SCENE_LIGHTS; p++) { for (int x = 0; x < 104 && sceneLightCount < MAX_SCENE_LIGHTS; x++) { for (int z = 0; z < 104 && sceneLightCount < MAX_SCENE_LIGHTS; z++) { Ground g = wc.groundArray[p][x][z]; if (g == null) continue; if (g.obj1 != null) { checkAndAddObjectLight(g.obj1.uid, g.obj1.anInt274, g.obj1.anInt273, g.obj1.anInt275); } if (g.obj2 != null) { checkAndAddObjectLight(g.obj2.uid, g.obj2.anInt500, g.obj2.anInt499, g.obj2.anInt501); } for (int i = 0; i < g.anInt1317 && sceneLightCount < MAX_SCENE_LIGHTS; i++) { Object5 o5 = g.obj5Array[i]; if (o5 != null && o5.anInt523 == x && o5.anInt525 == z) { checkAndAddObjectLight(o5.uid, o5.anInt519, o5.anInt518, o5.anInt520); } } } } } } private static boolean isOpenAirPassThrough(byte[][][] tileFlags, WorldController wc, int x, int z, int dx, int dz) { return reachesUnroofed(tileFlags, wc, x, z, dx, dz) && reachesUnroofed(tileFlags, wc, x, z, -dx, -dz); } private static boolean reachesUnroofed(byte[][][] tileFlags, WorldController wc, int x, int z, int dx, int dz) { for (int step = 1; step <= 4; step++) { int nx = x + dx * step; int nz = z + dz * step; if (nx < 0 || nx >= 104 || nz < 0 || nz >= 104) return false; Ground g = wc.groundArray[0][nx][nz]; if (g != null && g.obj1 != null) return false; if ((tileFlags[0][nx][nz] & 4) == 0) return true; } return false; } private void checkAndAddObjectLight(int uid, int worldX, int worldY, int worldZ) { if (uid == 0 || sceneLightCount >= MAX_SCENE_LIGHTS) return; int objId = (uid >> 14) & 0x7FFF; if (objId <= 0) return; boolean isFireOrTorch = false; boolean isMagicOrWaterLight = false; try { ObjectDef def = ObjectDef.forID(objId); if (def != null && def.name != null) { String n = def.name.toLowerCase(); if (n.contains("fire") || n.contains("torch") || n.contains("lamp") || n.contains("candle") || n.contains("furnace") || n.contains("brazier") || n.contains("lantern") || n.contains("stove") || n.contains("range") || n.contains("lava")) { isFireOrTorch = true; } else if (n.contains("altar") || n.contains("portal") || n.contains("fountain") || n.contains("crystal")) { isMagicOrWaterLight = true; } } } catch (Throwable ignored) { } if (!isFireOrTorch && !isMagicOrWaterLight) { return; } int base = sceneLightCount * 4; sceneLightPosRad[base] = worldX; sceneLightPosRad[base + 1] = worldY - 90.0f; sceneLightPosRad[base + 2] = worldZ; if (isFireOrTorch) { sceneLightPosRad[base + 3] = 640.0f; sceneLightColorInt[base] = 1.00f; sceneLightColorInt[base + 1] = 0.68f; sceneLightColorInt[base + 2] = 0.28f; sceneLightColorInt[base + 3] = 1.35f; } else { sceneLightPosRad[base + 3] = 512.0f; sceneLightColorInt[base] = 0.65f; sceneLightColorInt[base + 1] = 0.85f; sceneLightColorInt[base + 2] = 1.00f; sceneLightColorInt[base + 3] = 0.75f; } sceneLightCount++; } }