From a4a81657f5724c74a8014b1c06ee493a280fb7fb Mon Sep 17 00:00:00 2001 From: thedaneeffect Date: Mon, 2 Jan 2023 10:09:43 -0500 Subject: [PATCH] SceneOccluder Random stuff --- src/main/java/FloType.java | 4 +- src/main/java/Game.java | 29 +- src/main/java/Image24.java | 141 ++- src/main/java/Image8.java | 190 ++-- src/main/java/Model.java | 2 +- src/main/java/Scene.java | 1225 ++++++++++++++------------ src/main/java/SceneBuilder.java | 734 ++++++++------- src/main/java/SceneOccluder.java | 34 +- src/main/java/SceneTile.java | 20 +- src/main/java/SceneTileOverlay.java | 460 +++++----- src/main/java/SceneTileUnderlay.java | 29 +- 11 files changed, 1551 insertions(+), 1317 deletions(-) diff --git a/src/main/java/FloType.java b/src/main/java/FloType.java index c0a011b..7249f63 100644 --- a/src/main/java/FloType.java +++ b/src/main/java/FloType.java @@ -27,7 +27,7 @@ public class FloType { public int rgb; public int textureID = -1; public boolean unusedBool = false; - public boolean aBoolean393 = true; + public boolean occludes = true; public int hue; public int saturation; public int lightness; @@ -52,7 +52,7 @@ public class FloType { } else if (i == 3) { unusedBool = true; } else if (i == 5) { - aBoolean393 = false; + occludes = false; } else if (i == 6) { unusedString = buffer.readString(); } else if (i == 7) { diff --git a/src/main/java/Game.java b/src/main/java/Game.java index e75c970..ffe41ae 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -944,21 +944,21 @@ public class Game extends GameShell { imageRedstone2 = new Image8(archiveMedia, "redstone2", 0); imageRedstone3 = new Image8(archiveMedia, "redstone3", 0); imageRedstone1h = new Image8(archiveMedia, "redstone1", 0); - imageRedstone1h.flipH(); + imageRedstone1h.flipHorizontally(); imageRedstone2h = new Image8(archiveMedia, "redstone2", 0); - imageRedstone2h.flipH(); + imageRedstone2h.flipHorizontally(); imageRedstone1v = new Image8(archiveMedia, "redstone1", 0); - imageRedstone1v.flipV(); + imageRedstone1v.flipVertically(); imageRedstone2v = new Image8(archiveMedia, "redstone2", 0); - imageRedstone2v.flipV(); + imageRedstone2v.flipVertically(); imageRedstone3v = new Image8(archiveMedia, "redstone3", 0); - imageRedstone3v.flipV(); + imageRedstone3v.flipVertically(); imageRedstone1hv = new Image8(archiveMedia, "redstone1", 0); - imageRedstone1hv.flipH(); - imageRedstone1hv.flipV(); + imageRedstone1hv.flipHorizontally(); + imageRedstone1hv.flipVertically(); imageRedstone2hv = new Image8(archiveMedia, "redstone2", 0); - imageRedstone2hv.flipH(); - imageRedstone2hv.flipV(); + imageRedstone2hv.flipHorizontally(); + imageRedstone2hv.flipVertically(); for (int i = 0; i < 2; i++) { imageModIcons[i] = new Image8(archiveMedia, "mod_icons", i); } @@ -1061,14 +1061,7 @@ public class Game extends GameShell { Draw3D.init3D(512, 334); areaViewportOffsets = Draw3D.lineOffset; - int[] ai = new int[9]; - for (int i = 0; i < 9; i++) { - int angle = 128 + (i * 32) + 15; - int l8 = 600 + (angle * 3); - ai[i] = (l8 * Draw3D.sin[angle]) >> 16; - } - - Scene.method310(500, 800, 512, 334, ai); + Scene.init(512, 334); Censor.method487(archiveWordenc); mouseRecorder = new MouseRecorder(this); @@ -1768,7 +1761,7 @@ public class Game extends GameShell { } } out.writeOp(0); - sceneBuilder.method171(levelCollisionMap, scene); + sceneBuilder.build(levelCollisionMap, scene); areaViewport.bind(); out.writeOp(0); int k3 = SceneBuilder.minLevel; diff --git a/src/main/java/Image24.java b/src/main/java/Image24.java index 39996b1..fe4b6a3 100644 --- a/src/main/java/Image24.java +++ b/src/main/java/Image24.java @@ -19,12 +19,12 @@ public class Image24 extends DoublyLinkedList.Node { cropX = cropY = 0; } - public Image24(byte[] abyte0, java.awt.Component component) { + public Image24(byte[] src, java.awt.Component component) { try { - java.awt.Image image = Toolkit.getDefaultToolkit().createImage(abyte0); - MediaTracker mediatracker = new MediaTracker(component); - mediatracker.addImage(image, 0); - mediatracker.waitForAll(); + java.awt.Image image = Toolkit.getDefaultToolkit().createImage(src); + MediaTracker tracker = new MediaTracker(component); + tracker.addImage(image, 0); + tracker.waitForAll(); width = image.getWidth(component); height = image.getHeight(component); cropW = width; @@ -40,41 +40,41 @@ public class Image24 extends DoublyLinkedList.Node { } public Image24(FileArchive archive, String file, int index) throws IOException { - Buffer buffer = new Buffer(archive.read(file + ".dat")); - Buffer buffer_1 = new Buffer(archive.read("index.dat")); - buffer_1.position = buffer.read16U(); - cropW = buffer_1.read16U(); - cropH = buffer_1.read16U(); - int j = buffer_1.read8U(); - int[] ai = new int[j]; - for (int k = 0; k < (j - 1); k++) { - ai[k + 1] = buffer_1.read24(); - if (ai[k + 1] == 0) { - ai[k + 1] = 1; + Buffer dat = new Buffer(archive.read(file + ".dat")); + Buffer idx = new Buffer(archive.read("index.dat")); + idx.position = dat.read16U(); + cropW = idx.read16U(); + cropH = idx.read16U(); + int paletteSize = idx.read8U(); + int[] palette = new int[paletteSize]; + for (int k = 0; k < (paletteSize - 1); k++) { + palette[k + 1] = idx.read24(); + if (palette[k + 1] == 0) { + palette[k + 1] = 1; } } - for (int l = 0; l < index; l++) { - buffer_1.position += 2; - buffer.position += buffer_1.read16U() * buffer_1.read16U(); - buffer_1.position++; + for (int i = 0; i < index; i++) { + idx.position += 2; + dat.position += idx.read16U() * idx.read16U(); + idx.position++; } - cropX = buffer_1.read8U(); - cropY = buffer_1.read8U(); - width = buffer_1.read16U(); - height = buffer_1.read16U(); - int layout = buffer_1.read8U(); + cropX = idx.read8U(); + cropY = idx.read8U(); + width = idx.read16U(); + height = idx.read16U(); + int layout = idx.read8U(); int pixelLen = width * height; pixels = new int[pixelLen]; if (layout == 0) { - for (int k1 = 0; k1 < pixelLen; k1++) { - pixels[k1] = ai[buffer.read8U()]; + for (int i = 0; i < pixelLen; i++) { + pixels[i] = palette[dat.read8U()]; } return; } if (layout == 1) { - for (int l1 = 0; l1 < width; l1++) { - for (int i2 = 0; i2 < height; i2++) { - pixels[l1 + (i2 * width)] = ai[buffer.read8U()]; + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + pixels[x + (y * width)] = palette[dat.read8U()]; } } } @@ -170,14 +170,14 @@ public class Image24 extends DoublyLinkedList.Node { public void copyPixels(int dstOff, int w, int h, int srcStep, int srcOff, int dstStep, int[] src, int[] dst) { int quarterWidth = -(w >> 2); w = -(w & 3); - for (int i2 = -h; i2 < 0; i2++) { - for (int j2 = quarterWidth; j2 < 0; j2++) { + for (int y = -h; y < 0; y++) { + for (int i = quarterWidth; i < 0; i++) { dst[dstOff++] = src[srcOff++]; dst[dstOff++] = src[srcOff++]; dst[dstOff++] = src[srcOff++]; dst[dstOff++] = src[srcOff++]; } - for (int k2 = w; k2 < 0; k2++) { + for (int i = w; i < 0; i++) { dst[dstOff++] = src[srcOff++]; } dstOff += dstStep; @@ -195,23 +195,23 @@ public class Image24 extends DoublyLinkedList.Node { int dstStep = Draw2D.width - w; int srcStep = 0; if (y < Draw2D.top) { - int j2 = Draw2D.top - y; - h -= j2; + int trim = Draw2D.top - y; + h -= trim; y = Draw2D.top; - srcOff += j2 * w; - dstOff += j2 * Draw2D.width; + srcOff += trim * w; + dstOff += trim * Draw2D.width; } if ((y + h) > Draw2D.bottom) { h -= (y + h) - Draw2D.bottom; } if (x < Draw2D.left) { - int k2 = Draw2D.left - x; - w -= k2; + int trim = Draw2D.left - x; + w -= trim; x = Draw2D.left; - srcOff += k2; - dstOff += k2; - srcStep += k2; - dstStep += k2; + srcOff += trim; + dstOff += trim; + srcStep += trim; + dstStep += trim; } if ((x + w) > Draw2D.right) { int trim = (x + w) - Draw2D.right; @@ -271,43 +271,43 @@ public class Image24 extends DoublyLinkedList.Node { public void draw(int x, int y, int alpha) { x += cropX; y += cropY; - int i1 = x + (y * Draw2D.width); - int j1 = 0; - int k1 = height; + int dstOff = x + (y * Draw2D.width); + int srcOff = 0; + int h = height; int w = width; - int i2 = Draw2D.width - w; - int j2 = 0; + int dstStep = Draw2D.width - w; + int srcStep = 0; if (y < Draw2D.top) { - int k2 = Draw2D.top - y; - k1 -= k2; + int trim = Draw2D.top - y; + h -= trim; y = Draw2D.top; - j1 += k2 * w; - i1 += k2 * Draw2D.width; + srcOff += trim * w; + dstOff += trim * Draw2D.width; } - if ((y + k1) > Draw2D.bottom) { - k1 -= (y + k1) - Draw2D.bottom; + if ((y + h) > Draw2D.bottom) { + h -= (y + h) - Draw2D.bottom; } if (x < Draw2D.left) { - int l2 = Draw2D.left - x; - w -= l2; + int trim = Draw2D.left - x; + w -= trim; x = Draw2D.left; - j1 += l2; - i1 += l2; - j2 += l2; - i2 += l2; + srcOff += trim; + dstOff += trim; + srcStep += trim; + dstStep += trim; } if ((x + w) > Draw2D.right) { - int i3 = (x + w) - Draw2D.right; - w -= i3; - j2 += i3; - i2 += i3; + int trim = (x + w) - Draw2D.right; + w -= trim; + srcStep += trim; + dstStep += trim; } - if ((w > 0) && (k1 > 0)) { - copyPixelsAlpha(j1, w, Draw2D.pixels, pixels, j2, k1, i2, alpha, i1); + if ((w > 0) && (h > 0)) { + copyPixelsAlpha(srcOff, w, Draw2D.pixels, pixels, srcStep, h, dstStep, alpha, dstOff); } } - public void copyPixelsAlpha(int srcOff, int w, int[] dst, int[] src, int srcstep, int h, int dstStep, int alpha, int dstOff) { + public void copyPixelsAlpha(int srcOff, int w, int[] dst, int[] src, int srcStep, int h, int dstStep, int alpha, int dstOff) { int invAlpha = 256 - alpha; for (int k2 = -h; k2 < 0; k2++) { for (int l2 = -w; l2 < 0; l2++) { @@ -320,7 +320,7 @@ public class Image24 extends DoublyLinkedList.Node { } } dstOff += dstStep; - srcOff += srcstep; + srcOff += srcStep; } } @@ -431,7 +431,7 @@ public class Image24 extends DoublyLinkedList.Node { w = -(w & 3); for (int y = -h; y < 0; y++) { int rgb; - for (int k2 = quarterW; k2 < 0; k2++) { + for (int i = quarterW; i < 0; i++) { rgb = src[srcOff++]; if ((rgb != 0) && (mask[dstOff] == 0)) { dst[dstOff++] = rgb; @@ -457,7 +457,7 @@ public class Image24 extends DoublyLinkedList.Node { dstOff++; } } - for (int l2 = w; l2 < 0; l2++) { + for (int i = w; i < 0; i++) { rgb = src[srcOff++]; if ((rgb != 0) && (mask[dstOff] == 0)) { dst[dstOff++] = rgb; @@ -469,5 +469,4 @@ public class Image24 extends DoublyLinkedList.Node { srcOff += srcStep; } } - } diff --git a/src/main/java/Image8.java b/src/main/java/Image8.java index 7a79b9c..76ea54a 100644 --- a/src/main/java/Image8.java +++ b/src/main/java/Image8.java @@ -11,39 +11,41 @@ public class Image8 { public int cropW; public int cropH; - public Image8(FileArchive archive, String s, int i) throws IOException { - Buffer buffer = new Buffer(archive.read(s + ".dat")); - Buffer buffer_1 = new Buffer(archive.read("index.dat")); - buffer_1.position = buffer.read16U(); - cropW = buffer_1.read16U(); - cropH = buffer_1.read16U(); - int j = buffer_1.read8U(); - palette = new int[j]; - for (int k = 0; k < (j - 1); k++) { - palette[k + 1] = buffer_1.read24(); + public Image8(FileArchive archive, String name, int index) throws IOException { + Buffer dat = new Buffer(archive.read(name + ".dat")); + Buffer idx = new Buffer(archive.read("index.dat")); + idx.position = dat.read16U(); + cropW = idx.read16U(); + cropH = idx.read16U(); + int paletteSize = idx.read8U(); + palette = new int[paletteSize]; + for (int i = 0; i < (paletteSize - 1); i++) { + palette[i + 1] = idx.read24(); } - for (int l = 0; l < i; l++) { - buffer_1.position += 2; - buffer.position += buffer_1.read16U() * buffer_1.read16U(); - buffer_1.position++; + for (int i = 0; i < index; i++) { + idx.position += 2; + dat.position += idx.read16U() * idx.read16U(); + idx.position++; } - cropX = buffer_1.read8U(); - cropY = buffer_1.read8U(); - width = buffer_1.read16U(); - height = buffer_1.read16U(); - int i1 = buffer_1.read8U(); - int j1 = width * height; - pixels = new byte[j1]; - if (i1 == 0) { - for (int k1 = 0; k1 < j1; k1++) { - pixels[k1] = buffer.read8(); + cropX = idx.read8U(); + cropY = idx.read8U(); + width = idx.read16U(); + height = idx.read16U(); + int pixelOrder = idx.read8U(); + int pixelCount = width * height; + pixels = new byte[pixelCount]; + + if (pixelOrder == 0) { + for (int i = 0; i < pixelCount; i++) { + pixels[i] = dat.read8(); } return; } - if (i1 == 1) { - for (int l1 = 0; l1 < width; l1++) { - for (int i2 = 0; i2 < height; i2++) { - pixels[l1 + (i2 * width)] = buffer.read8(); + + if (pixelOrder == 1) { + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + pixels[x + (y * width)] = dat.read8(); } } } @@ -53,10 +55,10 @@ public class Image8 { cropW /= 2; cropH /= 2; byte[] pixels = new byte[cropW * cropH]; - int i = 0; - for (int j = 0; j < height; j++) { - for (int k = 0; k < width; k++) { - pixels[((k + cropX) >> 1) + (((j + cropY) >> 1) * cropW)] = this.pixels[i++]; + int off = 0; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixels[((x + cropX) >> 1) + (((y + cropY) >> 1) * cropW)] = this.pixels[off++]; } } this.pixels = pixels; @@ -71,10 +73,10 @@ public class Image8 { return; } byte[] pixels = new byte[cropW * cropH]; - int i = 0; - for (int j = 0; j < height; j++) { - for (int k = 0; k < width; k++) { - pixels[k + cropX + ((j + cropY) * cropW)] = this.pixels[i++]; + int off = 0; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixels[x + cropX + ((y + cropY) * cropW)] = this.pixels[off++]; } } this.pixels = pixels; @@ -84,19 +86,19 @@ public class Image8 { cropY = 0; } - public void flipH() { + public void flipHorizontally() { byte[] pixels = new byte[width * height]; - int i = 0; + int off = 0; for (int y = 0; y < height; y++) { for (int x = width - 1; x >= 0; x--) { - pixels[i++] = this.pixels[x + (y * width)]; + pixels[off++] = this.pixels[x + (y * width)]; } } this.pixels = pixels; cropX = cropW - width - cropX; } - public void flipV() { + public void flipVertically() { byte[] pixels = new byte[width * height]; int i = 0; for (int y = height - 1; y >= 0; y--) { @@ -138,82 +140,84 @@ public class Image8 { public void blit(int x, int y) { x += cropX; y += cropY; - int l = x + (y * Draw2D.width); - int i1 = 0; - int j1 = height; - int k1 = width; - int l1 = Draw2D.width - k1; - int i2 = 0; + int dstOff = x + (y * Draw2D.width); + int srcOff = 0; + int height = this.height; + int width = this.width; + int dstStep = Draw2D.width - width; + int srcStep = 0; if (y < Draw2D.top) { - int j2 = Draw2D.top - y; - j1 -= j2; + int trim = Draw2D.top - y; + height -= trim; y = Draw2D.top; - i1 += j2 * k1; - l += j2 * Draw2D.width; + srcOff += trim * width; + dstOff += trim * Draw2D.width; } - if ((y + j1) > Draw2D.bottom) { - j1 -= (y + j1) - Draw2D.bottom; + if ((y + height) > Draw2D.bottom) { + height -= (y + height) - Draw2D.bottom; } if (x < Draw2D.left) { - int k2 = Draw2D.left - x; - k1 -= k2; + int trim = Draw2D.left - x; + width -= trim; x = Draw2D.left; - i1 += k2; - l += k2; - i2 += k2; - l1 += k2; + srcOff += trim; + dstOff += trim; + srcStep += trim; + dstStep += trim; } - if ((x + k1) > Draw2D.right) { - int l2 = (x + k1) - Draw2D.right; - k1 -= l2; - i2 += l2; - l1 += l2; + if ((x + width) > Draw2D.right) { + int trim = (x + width) - Draw2D.right; + width -= trim; + srcStep += trim; + dstStep += trim; } - if ((k1 > 0) && (j1 > 0)) { - blit(j1, Draw2D.pixels, pixels, l1, l, k1, i1, palette, i2); + if ((width > 0) && (height > 0)) { + blit(height, Draw2D.pixels, pixels, dstStep, dstOff, width, srcOff, palette, srcStep); } } - public void blit(int i, int[] ai, byte[] abyte0, int j, int k, int l, int i1, int[] ai1, int j1) { - int k1 = -(l >> 2); - l = -(l & 3); - for (int l1 = -i; l1 < 0; l1++) { - for (int i2 = k1; i2 < 0; i2++) { - byte byte1 = abyte0[i1++]; - if (byte1 != 0) { - ai[k++] = ai1[byte1 & 0xff]; + public void blit(int height, int[] dst, byte[] src, int dstStep, int dstOff, int width, int srcOff, int[] palette, int srcStep) { + int quarterWidth = -(width >> 2); + width = -(width & 3); + for (int y = -height; y < 0; y++) { + for (int w = quarterWidth; w < 0; w++) { + byte paletteIndex = src[srcOff++]; + + if (paletteIndex != 0) { + dst[dstOff++] = palette[paletteIndex & 0xff]; } else { - k++; + dstOff++; } - byte1 = abyte0[i1++]; - if (byte1 != 0) { - ai[k++] = ai1[byte1 & 0xff]; + + paletteIndex = src[srcOff++]; + if (paletteIndex != 0) { + dst[dstOff++] = palette[paletteIndex & 0xff]; } else { - k++; + dstOff++; } - byte1 = abyte0[i1++]; - if (byte1 != 0) { - ai[k++] = ai1[byte1 & 0xff]; + paletteIndex = src[srcOff++]; + if (paletteIndex != 0) { + dst[dstOff++] = palette[paletteIndex & 0xff]; } else { - k++; + dstOff++; } - byte1 = abyte0[i1++]; - if (byte1 != 0) { - ai[k++] = ai1[byte1 & 0xff]; + paletteIndex = src[srcOff++]; + if (paletteIndex != 0) { + dst[dstOff++] = palette[paletteIndex & 0xff]; } else { - k++; + dstOff++; } } - for (int j2 = l; j2 < 0; j2++) { - byte byte2 = abyte0[i1++]; - if (byte2 != 0) { - ai[k++] = ai1[byte2 & 0xff]; + for (int i = width; i < 0; i++) { + byte paletteIndex = src[srcOff++]; + if (paletteIndex != 0) { + dst[dstOff++] = palette[paletteIndex & 0xff]; } else { - k++; + dstOff++; } } - k += j; - i1 += j1; + dstOff += dstStep; + srcOff += srcStep; } } diff --git a/src/main/java/Model.java b/src/main/java/Model.java index fea55aa..dffc9be 100644 --- a/src/main/java/Model.java +++ b/src/main/java/Model.java @@ -310,7 +310,7 @@ public class Model extends Entity { */ public boolean pickable = false; /** - * A storage for the original vertex normals to give {@link Scene#method308(Model, Model, int, int, int, boolean)} + * A storage for the original vertex normals to give {@link Scene#mergeNormals(Model, Model, int, int, int, boolean)} * a reference. */ public VertexNormal[] vertexNormalOriginal; diff --git a/src/main/java/Scene.java b/src/main/java/Scene.java index 7591073..9be686b 100644 --- a/src/main/java/Scene.java +++ b/src/main/java/Scene.java @@ -17,7 +17,7 @@ public class Scene { public static final int[] anIntArray482 = {2, 0, 0, 2, 0, 0, 0, 4, 4}; public static final int[] anIntArray483 = {0, 4, 4, 8, 0, 0, 8, 0, 0}; public static final int[] anIntArray484 = {1, 1, 0, 0, 0, 8, 0, 0, 8}; - public static final int[] anIntArray485 = {41, 39248, 41, 4643, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 43086, 41, 41, 41, 41, 41, 41, 41, 8602, 41, 28992, 41, 41, 41, 41, 41, 5056, 41, 41, 41, 7079, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 3131, 41, 41, 41}; + public static final int[] textureColor = {41, 39248, 41, 4643, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 43086, 41, 41, 41, 41, 41, 41, 41, 8602, 41, 28992, 41, 41, 41, 41, 41, 5056, 41, 41, 41, 7079, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 3131, 41, 41, 41}; public static final int LEVEL_COUNT = 4; public static final SceneOccluder[] activeOccluders = new SceneOccluder[500]; public static boolean lowmem = true; @@ -38,123 +38,146 @@ public class Scene { public static int sinEyeYaw; public static int cosEyeYaw; public static SceneLoc[] tmp = new SceneLoc[100]; - public static boolean aBoolean467; - public static int anInt468; - public static int anInt469; + public static boolean takingInput; + public static int mouseX; + public static int mouseY; public static int clickTileX = -1; public static int clickTileZ = -1; public static int[] levelOccluderCount = new int[LEVEL_COUNT]; public static SceneOccluder[][] levelOccluders = new SceneOccluder[LEVEL_COUNT][500]; public static int activeOccluderCount; public static DoublyLinkedList aList_477 = new DoublyLinkedList(); - public static boolean[][][][] aBooleanArrayArrayArrayArray491 = new boolean[8][32][51][51]; - public static boolean[][] aBooleanArrayArray492; - public static int anInt493; - public static int anInt494; - public static int anInt495; - public static int anInt496; - public static int anInt497; - public static int anInt498; + public static boolean[][][][] visibilityMatrix = new boolean[8][32][51][51]; + public static boolean[][] visibilityMap; + public static int viewportCenterX; + public static int viewportCenterY; + public static int viewportLeft; + public static int viewportTop; + public static int viewportRight; + public static int viewportBottom; public static void unload() { tmp = null; levelOccluderCount = null; levelOccluders = null; aList_477 = null; - aBooleanArrayArrayArrayArray491 = null; - aBooleanArrayArray492 = null; + visibilityMatrix = null; + visibilityMap = null; } - public static void method277(int level, int j, int k, int l, int i1, int j1, int l1, int i2) { + public static void addOccluder(int level, int minX, int maxY, int maxX, int maxZ, int minY, int minZ, int type) { SceneOccluder occluder = new SceneOccluder(); - occluder.anInt787 = j / 128; - occluder.anInt788 = l / 128; - occluder.anInt789 = l1 / 128; - occluder.anInt790 = i1 / 128; - occluder.anInt791 = i2; - occluder.anInt792 = j; - occluder.anInt793 = l; - occluder.anInt794 = l1; - occluder.anInt795 = i1; - occluder.anInt796 = j1; - occluder.anInt797 = k; + occluder.minTileX = minX / 128; + occluder.maxTileX = maxX / 128; + occluder.minTileZ = minZ / 128; + occluder.maxTileZ = maxZ / 128; + occluder.type = type; + occluder.minX = minX; + occluder.maxX = maxX; + occluder.minZ = minZ; + occluder.maxZ = maxZ; + occluder.minY = minY; + occluder.maxY = maxY; levelOccluders[level][levelOccluderCount[level]++] = occluder; } - public static void method310(int i, int j, int k, int l, int[] ai) { - anInt495 = 0; - anInt496 = 0; - anInt497 = k; - anInt498 = l; - anInt493 = k / 2; - anInt494 = l / 2; - boolean[][][][] aflag = new boolean[9][32][53][53]; - for (int i1 = 128; i1 <= 384; i1 += 32) { - for (int j1 = 0; j1 < 2048; j1 += 64) { - sinEyePitch = Model.sin[i1]; - cosEyePitch = Model.cos[i1]; - sinEyeYaw = Model.sin[j1]; - cosEyeYaw = Model.cos[j1]; - int l1 = (i1 - 128) / 32; - int j2 = j1 / 64; - for (int l2 = -26; l2 <= 26; l2++) { - for (int j3 = -26; j3 <= 26; j3++) { - int k3 = l2 * 128; - int i4 = j3 * 128; - boolean flag2 = false; - for (int k4 = -i; k4 <= j; k4 += 128) { - if (!method311(ai[l1] + k4, i4, k3)) { - continue; + public static void init(int viewportWidth, int viewportHeight) { + viewportLeft = 0; + viewportTop = 0; + Scene.viewportRight = viewportWidth; + Scene.viewportBottom = viewportHeight; + viewportCenterX = viewportWidth / 2; + viewportCenterY = viewportHeight / 2; + buildVisibilityMap(); + } + + /** + * Populates the {@link Scene#visibilityMatrix} lookup table which provides a rough approximation for relative tile + * visibility within a specific range of pitches and yaws. + */ + private static void buildVisibilityMap() { + // Later the visibility test will iterate between these values in steps of 128 to give tile visibility a margin + // of error. + final int minPadding = -500, maxPadding = 800; + + int[] pitchDistance = new int[9]; // = {437, 575, 724, 881, 1044, 1209, 1374, 1535, 1690} + + for (int pitchLevel = 0; pitchLevel < 9; pitchLevel++) { + int angle = 128 + (pitchLevel * 32) + 15; + int y = 600 + (angle * 3); + pitchDistance[pitchLevel] = (y * Draw3D.sin[angle]) >> 16; + } + + boolean[][][][] visibilityMap = new boolean[9][32][53][53]; + + for (int pitch = 128; pitch <= 384; pitch += 32) { + for (int yaw = 0; yaw < 2048; yaw += 64) { + sinEyePitch = Model.sin[pitch]; + cosEyePitch = Model.cos[pitch]; + sinEyeYaw = Model.sin[yaw]; + cosEyeYaw = Model.cos[yaw]; + + int pitchLevel = (pitch - 128) / 32; + int yawLevel = yaw / 64; + + for (int dx = -26; dx <= 26; dx++) { + for (int dz = -26; dz <= 26; dz++) { + int x = dx * 128; + int z = dz * 128; + boolean visible = false; + for (int y = minPadding; y <= maxPadding; y += 128) { + if (testPoint(pitchDistance[pitchLevel] + y, z, x)) { + visible = true; + break; } - flag2 = true; - break; } - aflag[l1][j2][l2 + 25 + 1][j3 + 25 + 1] = flag2; + visibilityMap[pitchLevel][yawLevel][dx + 25 + 1][dz + 25 + 1] = visible; } } } } - for (int k1 = 0; k1 < 8; k1++) { - for (int i2 = 0; i2 < 32; i2++) { - for (int k2 = -25; k2 < 25; k2++) { - for (int i3 = -25; i3 < 25; i3++) { - boolean flag1 = false; - label0: - for (int l3 = -1; l3 <= 1; l3++) { - for (int j4 = -1; j4 <= 1; j4++) { - if (aflag[k1][i2][k2 + l3 + 25 + 1][i3 + j4 + 25 + 1]) { - flag1 = true; - } else if (aflag[k1][(i2 + 1) % 31][k2 + l3 + 25 + 1][i3 + j4 + 25 + 1]) { - flag1 = true; - } else if (aflag[k1 + 1][i2][k2 + l3 + 25 + 1][i3 + j4 + 25 + 1]) { - flag1 = true; + + // One final pass to extend the visibility map up to 1 tile in any direction. + for (int pitchLevel = 0; pitchLevel < 8; pitchLevel++) { + for (int yawLevel = 0; yawLevel < 32; yawLevel++) { + for (int x = -25; x < 25; x++) { + for (int z = -25; z < 25; z++) { + boolean visible = false; + + check_area: + for (int dx = -1; dx <= 1; dx++) { + for (int dz = -1; dz <= 1; dz++) { + if (visibilityMap[pitchLevel][yawLevel][x + dx + 25 + 1][z + dz + 25 + 1]) { + visible = true; + } else if (visibilityMap[pitchLevel][(yawLevel + 1) % 31][x + dx + 25 + 1][z + dz + 25 + 1]) { + visible = true; + } else if (visibilityMap[pitchLevel + 1][yawLevel][x + dx + 25 + 1][z + dz + 25 + 1]) { + visible = true; + } else if (visibilityMap[pitchLevel + 1][(yawLevel + 1) % 31][x + dx + 25 + 1][z + dz + 25 + 1]) { + visible = true; } else { - if (!aflag[k1 + 1][(i2 + 1) % 31][k2 + l3 + 25 + 1][i3 + j4 + 25 + 1]) { - continue; - } - flag1 = true; + break check_area; } - break label0; } } - aBooleanArrayArrayArrayArray491[k1][i2][k2 + 25][i3 + 25] = flag1; + Scene.visibilityMatrix[pitchLevel][yawLevel][x + 25][z + 25] = visible; } } } } } - public static boolean method311(int i, int j, int k) { - int l = ((j * sinEyeYaw) + (k * cosEyeYaw)) >> 16; - int i1 = ((j * cosEyeYaw) - (k * sinEyeYaw)) >> 16; - int j1 = ((i * sinEyePitch) + (i1 * cosEyePitch)) >> 16; - int k1 = ((i * cosEyePitch) - (i1 * sinEyePitch)) >> 16; - if ((j1 < 50) || (j1 > 3500)) { + public static boolean testPoint(int y, int z, int x) { + int px = ((z * sinEyeYaw) + (x * cosEyeYaw)) >> 16; + int tmp = ((z * cosEyeYaw) - (x * sinEyeYaw)) >> 16; + int pz = ((y * sinEyePitch) + (tmp * cosEyePitch)) >> 16; + int py = ((y * cosEyePitch) - (tmp * sinEyePitch)) >> 16; + if ((pz < 50) || (pz > 3500)) { return false; } - int l1 = anInt493 + ((l << 9) / j1); - int i2 = anInt494 + ((k1 << 9) / j1); - return (l1 >= anInt495) && (l1 <= anInt497) && (i2 >= anInt496) && (i2 <= anInt498); + int viewportX = viewportCenterX + ((px << 9) / pz); + int viewportY = viewportCenterY + ((py << 9) / pz); + return (viewportX >= viewportLeft) && (viewportX <= viewportRight) && (viewportY >= viewportTop) && (viewportY <= viewportBottom); } public final int maxLevel; @@ -164,13 +187,13 @@ public class Scene { public final SceneTile[][][] levelTiles; public final SceneLoc[] temporaryLocs = new SceneLoc[5000]; public final int[][][] levelTileOcclusionCycles; - public final int[] anIntArray486 = new int[10000]; - public final int[] anIntArray487 = new int[10000]; + public final int[] mergeIndexA = new int[10000]; + public final int[] mergeIndexB = new int[10000]; public final int[][] MINIMAP_TILE_MASK = {new int[16], {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1}}; public final int[][] MINIMAP_TILE_ROTATION_MAP = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {12, 8, 4, 0, 13, 9, 5, 1, 14, 10, 6, 2, 15, 11, 7, 3}, {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {3, 7, 11, 15, 2, 6, 10, 14, 1, 5, 9, 13, 0, 4, 8, 12}}; public int minLevel; public int temporaryLocCount; - public int anInt488; + public int tmpMergeIndex; public Scene(int maxTileZ, int maxTileX, int[][][] levelHeightmaps, int maxLevel) { this.maxLevel = maxLevel; @@ -224,7 +247,7 @@ public class Scene { continue; } - above.anInt1307--; + above.dataLevel--; for (int i = 0; i < above.locCount; i++) { SceneLoc loc = above.locs[i]; @@ -250,17 +273,17 @@ public class Scene { } } - public void setTile(int level, int x, int z, int type, int i1, int j1, int k1, int l1, int i2, int j2, int k2, int l2, int i3, int j3, int k3, int l3, int i4, int j4, int k4, int l4) { - if (type == 0) { - SceneTileUnderlay underlay = new SceneTileUnderlay(k2, l2, i3, j3, -1, k4, false); + public void setTile(int level, int x, int z, int shape, int rotation, int textureID, int southwestY, int southeastY, int northeastY, int northwestY, int southwestColor1, int southeastColor1, int northeastColor1, int northwestColor1, int southwestColor2, int southeastColor2, int northeastColor2, int northwestColor2, int backgroundRGB, int foregroundRGB) { + if (shape == 0) { + SceneTileUnderlay underlay = new SceneTileUnderlay(southwestColor1, southeastColor1, northeastColor1, northwestColor1, -1, backgroundRGB, false); for (int l = level; l >= 0; l--) { if (levelTiles[l][x][z] == null) { levelTiles[l][x][z] = new SceneTile(l, x, z); } } levelTiles[level][x][z].underlay = underlay; - } else if (type == 1) { - SceneTileUnderlay underlay = new SceneTileUnderlay(k3, l3, i4, j4, j1, l4, (k1 == l1) && (k1 == i2) && (k1 == j2)); + } else if (shape == 1) { + SceneTileUnderlay underlay = new SceneTileUnderlay(southwestColor2, southeastColor2, northeastColor2, northwestColor2, textureID, foregroundRGB, (southwestY == southeastY) && (southwestY == northeastY) && (southwestY == northwestY)); for (int l = level; l >= 0; l--) { if (levelTiles[l][x][z] == null) { levelTiles[l][x][z] = new SceneTile(l, x, z); @@ -268,7 +291,7 @@ public class Scene { } levelTiles[level][x][z].underlay = underlay; } else { - SceneTileOverlay overlay = new SceneTileOverlay(z, k3, j3, i2, j1, i4, i1, k2, k4, i3, j2, l1, k1, type, j4, l3, l2, x, l4); + SceneTileOverlay overlay = new SceneTileOverlay(z, southwestColor2, northwestColor1, northeastY, textureID, northeastColor2, rotation, southwestColor1, backgroundRGB, northeastColor1, northwestY, southeastY, southwestY, shape, northwestColor2, southeastColor2, southeastColor1, x, foregroundRGB); for (int l = level; l >= 0; l--) { if (levelTiles[l][x][z] == null) { levelTiles[l][x][z] = new SceneTile(l, x, z); @@ -422,9 +445,9 @@ public class Scene { } } - public boolean add(Entity entity, int level, int tileX, int tileZ, int tileWidth, int tileLength, int x, int z, int y, int yaw, int bitset, byte info, boolean temporary) { - for (int tx = tileX; tx < (tileX + tileWidth); tx++) { - for (int tz = tileZ; tz < (tileZ + tileLength); tz++) { + public boolean add(Entity entity, int level, int tileX, int tileZ, int tileSizeX, int tileSizeZ, int x, int z, int y, int yaw, int bitset, byte info, boolean temporary) { + for (int tx = tileX; tx < (tileX + tileSizeX); tx++) { + for (int tz = tileZ; tz < (tileZ + tileSizeZ); tz++) { if ((tx < 0) || (tz < 0) || (tx >= maxTileX) || (tz >= maxTileZ)) { return false; } @@ -445,18 +468,18 @@ public class Scene { loc.yaw = yaw; loc.minSceneTileX = tileX; loc.minSceneTileZ = tileZ; - loc.maxSceneTileX = (tileX + tileWidth) - 1; - loc.maxSceneTileZ = (tileZ + tileLength) - 1; + loc.maxSceneTileX = (tileX + tileSizeX) - 1; + loc.maxSceneTileZ = (tileZ + tileSizeZ) - 1; - for (int tx = tileX; tx < (tileX + tileWidth); tx++) { - for (int tz = tileZ; tz < (tileZ + tileLength); tz++) { + for (int tx = tileX; tx < (tileX + tileSizeX); tx++) { + for (int tz = tileZ; tz < (tileZ + tileSizeZ); tz++) { int flags = 0; if (tx > tileX) { flags++; } - if (tx < ((tileX + tileWidth) - 1)) { + if (tx < ((tileX + tileSizeX) - 1)) { flags += 4; } @@ -464,7 +487,7 @@ public class Scene { flags += 8; } - if (tz < ((tileZ + tileLength) - 1)) { + if (tz < ((tileZ + tileSizeZ) - 1)) { flags += 2; } @@ -696,174 +719,213 @@ public class Scene { return -1; } - public void applyLighting(int i, int j, int k, int l, int i1) { - int j1 = (int) Math.sqrt((k * k) + (i * i) + (i1 * i1)); - int k1 = (l * j1) >> 8; - for (int l1 = 0; l1 < maxLevel; l1++) { - for (int i2 = 0; i2 < maxTileX; i2++) { - for (int j2 = 0; j2 < maxTileZ; j2++) { - SceneTile tile = levelTiles[l1][i2][j2]; - if (tile != null) { - SceneWall wall = tile.wall; - if ((wall != null) && (wall.entityA != null) && (wall.entityA.vertexNormal != null)) { - method307(l1, 1, 1, i2, j2, (Model) wall.entityA); - if ((wall.entityB != null) && (wall.entityB.vertexNormal != null)) { - method307(l1, 1, 1, i2, j2, (Model) wall.entityB); - method308((Model) wall.entityA, (Model) wall.entityB, 0, 0, 0, false); - ((Model) wall.entityB).applyLighting(j, k1, k, i, i1); - } - ((Model) wall.entityA).applyLighting(j, k1, k, i, i1); + /** + * Merges touching normals of all Locs (Walls, Ground Decorations, etc) and then reapplies their lighting. + * + * @param lightAmbient + * @param lightAttenuation + * @param lightSrcX + * @param lightSrcY + * @param lightSrcZ + */ + public void buildModels(int lightAmbient, int lightAttenuation, int lightSrcX, int lightSrcY, int lightSrcZ) { + int lightMagnitude = (int) Math.sqrt((lightSrcX * lightSrcX) + (lightSrcY * lightSrcY) + (lightSrcZ * lightSrcZ)); + int attenuation = (lightAttenuation * lightMagnitude) >> 8; + for (int level = 0; level < maxLevel; level++) { + for (int tileX = 0; tileX < maxTileX; tileX++) { + for (int tileZ = 0; tileZ < maxTileZ; tileZ++) { + SceneTile tile = levelTiles[level][tileX][tileZ]; + + if (tile == null) { + continue; + } + + SceneWall wall = tile.wall; + + if ((wall != null) && (wall.entityA != null) && (wall.entityA.vertexNormal != null)) { + mergeLocNormals(level, 1, 1, tileX, tileZ, (Model) wall.entityA); + + if ((wall.entityB != null) && (wall.entityB.vertexNormal != null)) { + mergeLocNormals(level, 1, 1, tileX, tileZ, (Model) wall.entityB); + mergeNormals((Model) wall.entityA, (Model) wall.entityB, 0, 0, 0, false); + ((Model) wall.entityB).applyLighting(lightAmbient, attenuation, lightSrcX, lightSrcY, lightSrcZ); } - for (int k2 = 0; k2 < tile.locCount; k2++) { - SceneLoc loc = tile.locs[k2]; - if ((loc != null) && (loc.entity != null) && (loc.entity.vertexNormal != null)) { - method307(l1, (loc.maxSceneTileX - loc.minSceneTileX) + 1, (loc.maxSceneTileZ - loc.minSceneTileZ) + 1, i2, j2, (Model) loc.entity); - ((Model) loc.entity).applyLighting(j, k1, k, i, i1); - } - } - SceneGroundDecoration groundDecoration = tile.groundDecoration; - if ((groundDecoration != null) && (groundDecoration.entity.vertexNormal != null)) { - method306(i2, l1, (Model) groundDecoration.entity, j2); - ((Model) groundDecoration.entity).applyLighting(j, k1, k, i, i1); + + ((Model) wall.entityA).applyLighting(lightAmbient, attenuation, lightSrcX, lightSrcY, lightSrcZ); + } + + for (int i = 0; i < tile.locCount; i++) { + SceneLoc loc = tile.locs[i]; + + if ((loc != null) && (loc.entity != null) && (loc.entity.vertexNormal != null)) { + mergeLocNormals(level, (loc.maxSceneTileX - loc.minSceneTileX) + 1, (loc.maxSceneTileZ - loc.minSceneTileZ) + 1, tileX, tileZ, (Model) loc.entity); + ((Model) loc.entity).applyLighting(lightAmbient, attenuation, lightSrcX, lightSrcY, lightSrcZ); } } + + SceneGroundDecoration decoration = tile.groundDecoration; + if ((decoration != null) && (decoration.entity.vertexNormal != null)) { + mergeGroundDecorationNormals(tileX, level, (Model) decoration.entity, tileZ); + ((Model) decoration.entity).applyLighting(lightAmbient, attenuation, lightSrcX, lightSrcY, lightSrcZ); + } } } } } - public void method306(int i, int j, Model model, int k) { - if (i < maxTileX) { - SceneTile tile = levelTiles[j][i + 1][k]; + public void mergeGroundDecorationNormals(int tileX, int level, Model model, int tileZ) { + if (tileX < maxTileX) { + SceneTile tile = levelTiles[level][tileX + 1][tileZ]; if ((tile != null) && (tile.groundDecoration != null) && (tile.groundDecoration.entity.vertexNormal != null)) { - method308(model, (Model) tile.groundDecoration.entity, 128, 0, 0, true); + mergeNormals(model, (Model) tile.groundDecoration.entity, 128, 0, 0, true); } } - if (k < maxTileX) { - SceneTile tile_1 = levelTiles[j][i][k + 1]; - if ((tile_1 != null) && (tile_1.groundDecoration != null) && (tile_1.groundDecoration.entity.vertexNormal != null)) { - method308(model, (Model) tile_1.groundDecoration.entity, 0, 0, 128, true); + if (tileZ < maxTileX) { + SceneTile tile = levelTiles[level][tileX][tileZ + 1]; + if ((tile != null) && (tile.groundDecoration != null) && (tile.groundDecoration.entity.vertexNormal != null)) { + mergeNormals(model, (Model) tile.groundDecoration.entity, 0, 0, 128, true); } } - if ((i < maxTileX) && (k < maxTileZ)) { - SceneTile tile_2 = levelTiles[j][i + 1][k + 1]; - if ((tile_2 != null) && (tile_2.groundDecoration != null) && (tile_2.groundDecoration.entity.vertexNormal != null)) { - method308(model, (Model) tile_2.groundDecoration.entity, 128, 0, 128, true); + if ((tileX < maxTileX) && (tileZ < maxTileZ)) { + SceneTile tile = levelTiles[level][tileX + 1][tileZ + 1]; + if ((tile != null) && (tile.groundDecoration != null) && (tile.groundDecoration.entity.vertexNormal != null)) { + mergeNormals(model, (Model) tile.groundDecoration.entity, 128, 0, 128, true); } } - if ((i < maxTileX) && (k > 0)) { - SceneTile tile_3 = levelTiles[j][i + 1][k - 1]; - if ((tile_3 != null) && (tile_3.groundDecoration != null) && (tile_3.groundDecoration.entity.vertexNormal != null)) { - method308(model, (Model) tile_3.groundDecoration.entity, 128, 0, -128, true); + if ((tileX < maxTileX) && (tileZ > 0)) { + SceneTile tile = levelTiles[level][tileX + 1][tileZ - 1]; + if ((tile != null) && (tile.groundDecoration != null) && (tile.groundDecoration.entity.vertexNormal != null)) { + mergeNormals(model, (Model) tile.groundDecoration.entity, 128, 0, -128, true); } } } - public void method307(int i, int j, int k, int l, int i1, Model model) { - boolean flag = true; - int j1 = l; - int k1 = l + j; - int l1 = i1 - 1; - int i2 = i1 + k; - for (int j2 = i; j2 <= (i + 1); j2++) { - if (j2 != maxLevel) { - for (int k2 = j1; k2 <= k1; k2++) { - if ((k2 >= 0) && (k2 < maxTileX)) { - for (int l2 = l1; l2 <= i2; l2++) { - if ((l2 >= 0) && (l2 < maxTileZ) && (!flag || (k2 >= k1) || (l2 >= i2) || ((l2 < i1) && (k2 != l)))) { - SceneTile tile = levelTiles[j2][k2][l2]; - if (tile != null) { - int i3 = ((levelHeightmaps[j2][k2][l2] + levelHeightmaps[j2][k2 + 1][l2] + levelHeightmaps[j2][k2][l2 + 1] + levelHeightmaps[j2][k2 + 1][l2 + 1]) / 4) - ((levelHeightmaps[i][l][i1] + levelHeightmaps[i][l + 1][i1] + levelHeightmaps[i][l][i1 + 1] + levelHeightmaps[i][l + 1][i1 + 1]) / 4); - SceneWall wall = tile.wall; - if ((wall != null) && (wall.entityA != null) && (wall.entityA.vertexNormal != null)) { - method308(model, (Model) wall.entityA, ((k2 - l) * 128) + ((1 - j) * 64), i3, ((l2 - i1) * 128) + ((1 - k) * 64), flag); - } - if ((wall != null) && (wall.entityB != null) && (wall.entityB.vertexNormal != null)) { - method308(model, (Model) wall.entityB, ((k2 - l) * 128) + ((1 - j) * 64), i3, ((l2 - i1) * 128) + ((1 - k) * 64), flag); - } - for (int j3 = 0; j3 < tile.locCount; j3++) { - SceneLoc loc = tile.locs[j3]; - if ((loc != null) && (loc.entity != null) && (loc.entity.vertexNormal != null)) { - int k3 = (loc.maxSceneTileX - loc.minSceneTileX) + 1; - int l3 = (loc.maxSceneTileZ - loc.minSceneTileZ) + 1; - method308(model, (Model) loc.entity, ((loc.minSceneTileX - l) * 128) + ((k3 - j) * 64), i3, ((loc.minSceneTileZ - i1) * 128) + ((l3 - k) * 64), flag); - } - } - } - } + public void mergeLocNormals(int level, int tileSizeX, int tileSizeZ, int tileX, int tileZ, Model model) { + boolean allowFaceRemoval = true; + int minTileX = tileX; + int maxTileX = tileX + tileSizeX; + int minTileZ = tileZ - 1; + int maxTileZ = tileZ + tileSizeZ; + + for (int l = level; l <= (level + 1); l++) { + if (l == maxLevel) { + continue; + } + + for (int x = minTileX; x <= maxTileX; x++) { + if ((x < 0) || (x >= this.maxTileX)) { + continue; + } + + for (int z = minTileZ; z <= maxTileZ; z++) { + if ((z < 0) || (z >= this.maxTileZ) || (allowFaceRemoval && (x < maxTileX) && (z < maxTileZ) && ((z >= tileZ) || (x == tileX)))) { + continue; + } + + SceneTile tile = levelTiles[l][x][z]; + + if (tile == null) { + continue; + } + + int offsetY = ((levelHeightmaps[l][x][z] + levelHeightmaps[l][x + 1][z] + levelHeightmaps[l][x][z + 1] + levelHeightmaps[l][x + 1][z + 1]) / 4) - ((levelHeightmaps[level][tileX][tileZ] + levelHeightmaps[level][tileX + 1][tileZ] + levelHeightmaps[level][tileX][tileZ + 1] + levelHeightmaps[level][tileX + 1][tileZ + 1]) / 4); + SceneWall wall = tile.wall; + + int offsetX = ((x - tileX) * 128) + ((1 - tileSizeX) * 64); + int offsetZ = ((z - tileZ) * 128) + ((1 - tileSizeZ) * 64); + + if ((wall != null) && (wall.entityA != null) && (wall.entityA.vertexNormal != null)) { + mergeNormals(model, (Model) wall.entityA, offsetX, offsetY, offsetZ, allowFaceRemoval); + } + + if ((wall != null) && (wall.entityB != null) && (wall.entityB.vertexNormal != null)) { + mergeNormals(model, (Model) wall.entityB, offsetX, offsetY, offsetZ, allowFaceRemoval); + } + + for (int i = 0; i < tile.locCount; i++) { + SceneLoc loc = tile.locs[i]; + + if ((loc != null) && (loc.entity != null) && (loc.entity.vertexNormal != null)) { + int locTileSizeX = (loc.maxSceneTileX - loc.minSceneTileX) + 1; + int locTileSizeZ = (loc.maxSceneTileZ - loc.minSceneTileZ) + 1; + mergeNormals(model, (Model) loc.entity, ((loc.minSceneTileX - tileX) * 128) + ((locTileSizeX - tileSizeX) * 64), offsetY, ((loc.minSceneTileZ - tileZ) * 128) + ((locTileSizeZ - tileSizeZ) * 64), allowFaceRemoval); } } } - j1--; - flag = false; } + + minTileX--; + allowFaceRemoval = false; } } - public void method308(Model model, Model model_1, int i, int j, int k, boolean flag) { - anInt488++; - int l = 0; - int[] ai = model_1.vertexX; - int i1 = model_1.vertexCount; - for (int j1 = 0; j1 < model.vertexCount; j1++) { - VertexNormal normal = model.vertexNormal[j1]; - VertexNormal normal_1 = model.vertexNormalOriginal[j1]; + public void mergeNormals(Model modelA, Model modelB, int offsetX, int offsetY, int offsetZ, boolean allowFaceRemoval) { + tmpMergeIndex++; + int merged = 0; + for (int vertexA = 0; vertexA < modelA.vertexCount; vertexA++) { + VertexNormal normalA = modelA.vertexNormal[vertexA]; + VertexNormal originalNormalA = modelA.vertexNormalOriginal[vertexA]; - if (normal_1.w == 0) { + // undefined normal + if (originalNormalA.w == 0) { continue; } - int i2 = model.vertexY[j1] - j; + int y = modelA.vertexY[vertexA] - offsetY; - if (i2 > model_1.maxY) { + if (y > modelB.maxY) { continue; } - int j2 = model.vertexX[j1] - i; + int x = modelA.vertexX[vertexA] - offsetX; - if ((j2 < model_1.minX) || (j2 > model_1.maxX)) { + if ((x < modelB.minX) || (x > modelB.maxX)) { continue; } - int k2 = model.vertexZ[j1] - k; + int z = modelA.vertexZ[vertexA] - offsetZ; - if ((k2 < model_1.minZ) || (k2 > model_1.maxZ)) { + if ((z < modelB.minZ) || (z > modelB.maxZ)) { continue; } - for (int l2 = 0; l2 < i1; l2++) { - VertexNormal normal_2 = model_1.vertexNormal[l2]; - VertexNormal normal_3 = model_1.vertexNormalOriginal[l2]; + for (int vertexB = 0; vertexB < modelB.vertexCount; vertexB++) { + VertexNormal normalB = modelB.vertexNormal[vertexB]; + VertexNormal originalNormalB = modelB.vertexNormalOriginal[vertexB]; - if ((j2 == ai[l2]) && (k2 == model_1.vertexZ[l2]) && (i2 == model_1.vertexY[l2]) && (normal_3.w != 0)) { - normal.x += normal_3.x; - normal.y += normal_3.y; - normal.z += normal_3.z; - normal.w += normal_3.w; - normal_2.x += normal_1.x; - normal_2.y += normal_1.y; - normal_2.z += normal_1.z; - normal_2.w += normal_1.w; - l++; - anIntArray486[j1] = anInt488; - anIntArray487[l2] = anInt488; + if ((x == modelB.vertexX[vertexB]) && (z == modelB.vertexZ[vertexB]) && (y == modelB.vertexY[vertexB]) && (originalNormalB.w != 0)) { + normalA.x += originalNormalB.x; + normalA.y += originalNormalB.y; + normalA.z += originalNormalB.z; + normalA.w += originalNormalB.w; + + normalB.x += originalNormalA.x; + normalB.y += originalNormalA.y; + normalB.z += originalNormalA.z; + normalB.w += originalNormalA.w; + + merged++; + mergeIndexA[vertexA] = tmpMergeIndex; + mergeIndexB[vertexB] = tmpMergeIndex; } } } - if ((l < 3) || !flag) { + if ((merged < 3) || !allowFaceRemoval) { return; } - for (int k1 = 0; k1 < model.faceCount; k1++) { - if ((anIntArray486[model.faceVertexA[k1]] == anInt488) && (anIntArray486[model.faceVertexB[k1]] == anInt488) && (anIntArray486[model.faceVertexC[k1]] == anInt488)) { - model.faceInfo[k1] = -1; + // if every vertex of a given face had their normals merged, clear the face info causing that face not to draw. + for (int i = 0; i < modelA.faceCount; i++) { + if ((mergeIndexA[modelA.faceVertexA[i]] == tmpMergeIndex) && (mergeIndexA[modelA.faceVertexB[i]] == tmpMergeIndex) && (mergeIndexA[modelA.faceVertexC[i]] == tmpMergeIndex)) { + modelA.faceInfo[i] = -1; } } - for (int l1 = 0; l1 < model_1.faceCount; l1++) { - if ((anIntArray487[model_1.faceVertexA[l1]] == anInt488) && (anIntArray487[model_1.faceVertexB[l1]] == anInt488) && (anIntArray487[model_1.faceVertexC[l1]] == anInt488)) { - model_1.faceInfo[l1] = -1; + // same as above but for model B + for (int i = 0; i < modelB.faceCount; i++) { + if ((mergeIndexB[modelB.faceVertexA[i]] == tmpMergeIndex) && (mergeIndexB[modelB.faceVertexB[i]] == tmpMergeIndex) && (mergeIndexB[modelB.faceVertexC[i]] == tmpMergeIndex)) { + modelB.faceInfo[i] = -1; } } } @@ -900,7 +962,7 @@ public class Scene { } int shape = overlay.shape; - int angle = overlay.angle; + int angle = overlay.rotation; int background = overlay.backgroundRGB; int foreground = overlay.foregroundRGB; int[] mask = MINIMAP_TILE_MASK[shape]; @@ -936,9 +998,9 @@ public class Scene { } public void method312(int i, int j) { - aBoolean467 = true; - anInt468 = j; - anInt469 = i; + takingInput = true; + mouseX = j; + mouseY = i; clickTileX = -1; clickTileZ = -1; } @@ -959,7 +1021,7 @@ public class Scene { cosEyePitch = Model.cos[eyePitch]; sinEyeYaw = Model.sin[eyeYaw]; cosEyeYaw = Model.cos[eyeYaw]; - aBooleanArrayArray492 = aBooleanArrayArrayArrayArray491[(eyePitch - 128) / 32][eyeYaw / 64]; + visibilityMap = visibilityMatrix[(eyePitch - 128) / 32][eyeYaw / 64]; Scene.eyeX = eyeX; Scene.eyeY = eyeY; Scene.eyeZ = eyeZ; @@ -982,15 +1044,15 @@ public class Scene { if (anInt452 > maxTileZ) { anInt452 = maxTileZ; } - method319(); + updateActiveOccluders(); anInt446 = 0; for (int k1 = minLevel; k1 < maxLevel; k1++) { - SceneTile[][] aclass30_sub3 = levelTiles[k1]; - for (int i2 = anInt449; i2 < anInt450; i2++) { - for (int k2 = anInt451; k2 < anInt452; k2++) { - SceneTile tile = aclass30_sub3[i2][k2]; + SceneTile[][] tiles = levelTiles[k1]; + for (int x = anInt449; x < anInt450; x++) { + for (int z = anInt451; z < anInt452; z++) { + SceneTile tile = tiles[x][z]; if (tile != null) { - if ((tile.drawLevel > topLevel) || (!aBooleanArrayArray492[(i2 - eyeTileX) + 25][(k2 - eyeTileZ) + 25] && ((levelHeightmaps[k1][i2][k2] - eyeY) < 2000))) { + if ((tile.drawLevel > topLevel) || (!visibilityMap[(x - eyeTileX) + 25][(z - eyeTileZ) + 25] && ((levelHeightmaps[k1][x][z] - eyeY) < 2000))) { tile.aBoolean1322 = false; tile.aBoolean1323 = false; tile.anInt1325 = 0; @@ -1042,7 +1104,7 @@ public class Scene { } } if (anInt446 == 0) { - aBoolean467 = false; + takingInput = false; return; } } @@ -1087,59 +1149,63 @@ public class Scene { } } if (anInt446 == 0) { - aBoolean467 = false; + takingInput = false; return; } } } } } - aBoolean467 = false; + takingInput = false; } public void method314(SceneTile tile, boolean flag) { aList_477.pushBack(tile); + do { SceneTile tile_1; + do { tile_1 = (SceneTile) aList_477.pollFront(); if (tile_1 == null) { return; } } while (!tile_1.aBoolean1323); - int i = tile_1.anInt1308; - int j = tile_1.anInt1309; - int k = tile_1.anInt1307; - int l = tile_1.anInt1310; - SceneTile[][] tiles = levelTiles[k]; + + int tileX = tile_1.x; + int tileZ = tile_1.z; + int dataLevel = tile_1.dataLevel; + int level = tile_1.level; + SceneTile[][] tiles = levelTiles[dataLevel]; + if (tile_1.aBoolean1322) { if (flag) { - if (k > 0) { - SceneTile tile_2 = levelTiles[k - 1][i][j]; + if (dataLevel > 0) { + SceneTile tile_2 = levelTiles[dataLevel - 1][tileX][tileZ]; if ((tile_2 != null) && tile_2.aBoolean1323) { continue; } } - if ((i <= eyeTileX) && (i > anInt449)) { - SceneTile tile_3 = tiles[i - 1][j]; + if ((tileX <= eyeTileX) && (tileX > anInt449)) { + SceneTile tile_3 = tiles[tileX - 1][tileZ]; if ((tile_3 != null) && tile_3.aBoolean1323 && (tile_3.aBoolean1322 || ((tile_1.flags & 1) == 0))) { continue; } } - if ((i >= eyeTileX) && (i < (anInt450 - 1))) { - SceneTile tile_4 = tiles[i + 1][j]; + if ((tileX >= eyeTileX) && (tileX < (anInt450 - 1))) { + SceneTile tile_4 = tiles[tileX + 1][tileZ]; if ((tile_4 != null) && tile_4.aBoolean1323 && (tile_4.aBoolean1322 || ((tile_1.flags & 4) == 0))) { continue; } } - if ((j <= eyeTileZ) && (j > anInt451)) { - SceneTile tile_5 = tiles[i][j - 1]; + if ((tileZ <= eyeTileZ) && (tileZ > anInt451)) { + SceneTile tile_5 = tiles[tileX][tileZ - 1]; if ((tile_5 != null) && tile_5.aBoolean1323 && (tile_5.aBoolean1322 || ((tile_1.flags & 8) == 0))) { continue; } } - if ((j >= eyeTileZ) && (j < (anInt452 - 1))) { - SceneTile tile_6 = tiles[i][j + 1]; + if ((tileZ >= eyeTileZ) && (tileZ < (anInt452 - 1))) { + SceneTile tile_6 = tiles[tileX][tileZ + 1]; if ((tile_6 != null) && tile_6.aBoolean1323 && (tile_6.aBoolean1322 || ((tile_1.flags & 2) == 0))) { continue; } @@ -1151,11 +1217,11 @@ public class Scene { if (tile_1.bridge != null) { SceneTile tile_7 = tile_1.bridge; if (tile_7.underlay != null) { - if (!tileOccluded(0, i, j)) { - method315(tile_7.underlay, 0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, i, j); + if (!tileOccluded(0, tileX, tileZ)) { + drawTileUnderlay(tile_7.underlay, 0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, tileX, tileZ); } - } else if ((tile_7.overlay != null) && !tileOccluded(0, i, j)) { - method316(i, sinEyePitch, sinEyeYaw, tile_7.overlay, cosEyePitch, j, cosEyeYaw); + } else if ((tile_7.overlay != null) && !tileOccluded(0, tileX, tileZ)) { + drawTileOverlay(tileX, sinEyePitch, sinEyeYaw, tile_7.overlay, cosEyePitch, tileZ, cosEyeYaw); } SceneWall wall = tile_7.wall; if (wall != null) { @@ -1170,27 +1236,27 @@ public class Scene { } boolean flag1 = false; if (tile_1.underlay != null) { - if (!tileOccluded(l, i, j)) { + if (!tileOccluded(level, tileX, tileZ)) { flag1 = true; - method315(tile_1.underlay, l, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, i, j); + drawTileUnderlay(tile_1.underlay, level, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, tileX, tileZ); } - } else if ((tile_1.overlay != null) && !tileOccluded(l, i, j)) { + } else if ((tile_1.overlay != null) && !tileOccluded(level, tileX, tileZ)) { flag1 = true; - method316(i, sinEyePitch, sinEyeYaw, tile_1.overlay, cosEyePitch, j, cosEyeYaw); + drawTileOverlay(tileX, sinEyePitch, sinEyeYaw, tile_1.overlay, cosEyePitch, tileZ, cosEyeYaw); } int j1 = 0; int j2 = 0; SceneWall wall = tile_1.wall; SceneWallDecoration decor = tile_1.wallDecoration; if ((wall != null) || (decor != null)) { - if (eyeTileX == i) { + if (eyeTileX == tileX) { j1++; - } else if (eyeTileX < i) { + } else if (eyeTileX < tileX) { j1 += 2; } - if (eyeTileZ == j) { + if (eyeTileZ == tileZ) { j1 += 3; - } else if (eyeTileZ > j) { + } else if (eyeTileZ > tileZ) { j1 += 6; } j2 = anIntArray478[j1]; @@ -1218,14 +1284,14 @@ public class Scene { } else { tile_1.anInt1325 = 0; } - if (((wall.occludeA & j2) != 0) && !wallOccluded(l, i, j, wall.occludeA)) { + if (((wall.occludeA & j2) != 0) && !wallOccluded(level, tileX, tileZ, wall.occludeA)) { wall.entityA.draw(0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, wall.x - eyeX, wall.y - eyeY, wall.z - eyeZ, wall.bitset); } - if (((wall.occludeB & j2) != 0) && !wallOccluded(l, i, j, wall.occludeB)) { + if (((wall.occludeB & j2) != 0) && !wallOccluded(level, tileX, tileZ, wall.occludeB)) { wall.entityB.draw(0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, wall.x - eyeX, wall.y - eyeY, wall.z - eyeZ, wall.bitset); } } - if ((decor != null) && !occluded(l, i, j, decor.entity.minY)) { + if ((decor != null) && !occluded(level, tileX, tileZ, decor.entity.minY)) { if ((decor.occlude & j2) != 0) { decor.entity.draw(decor.yaw, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, decor.x - eyeX, decor.y - eyeY, decor.z - eyeZ, decor.bitset); } else if ((decor.occlude & 0x300) != 0) { @@ -1277,26 +1343,26 @@ public class Scene { } int k4 = tile_1.flags; if (k4 != 0) { - if ((i < eyeTileX) && ((k4 & 4) != 0)) { - SceneTile tile_17 = tiles[i + 1][j]; + if ((tileX < eyeTileX) && ((k4 & 4) != 0)) { + SceneTile tile_17 = tiles[tileX + 1][tileZ]; if ((tile_17 != null) && tile_17.aBoolean1323) { aList_477.pushBack(tile_17); } } - if ((j < eyeTileZ) && ((k4 & 2) != 0)) { - SceneTile tile_18 = tiles[i][j + 1]; + if ((tileZ < eyeTileZ) && ((k4 & 2) != 0)) { + SceneTile tile_18 = tiles[tileX][tileZ + 1]; if ((tile_18 != null) && tile_18.aBoolean1323) { aList_477.pushBack(tile_18); } } - if ((i > eyeTileX) && ((k4 & 1) != 0)) { - SceneTile tile_19 = tiles[i - 1][j]; + if ((tileX > eyeTileX) && ((k4 & 1) != 0)) { + SceneTile tile_19 = tiles[tileX - 1][tileZ]; if ((tile_19 != null) && tile_19.aBoolean1323) { aList_477.pushBack(tile_19); } } - if ((j > eyeTileZ) && ((k4 & 8) != 0)) { - SceneTile tile_20 = tiles[i][j - 1]; + if ((tileZ > eyeTileZ) && ((k4 & 8) != 0)) { + SceneTile tile_20 = tiles[tileX][tileZ - 1]; if ((tile_20 != null) && tile_20.aBoolean1323) { aList_477.pushBack(tile_20); } @@ -1314,7 +1380,7 @@ public class Scene { } if (flag2) { SceneWall wall_1 = tile_1.wall; - if (!wallOccluded(l, i, j, wall_1.occludeA)) { + if (!wallOccluded(level, tileX, tileZ, wall_1.occludeA)) { wall_1.entityA.draw(0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, wall_1.x - eyeX, wall_1.y - eyeY, wall_1.z - eyeZ, wall_1.bitset); } tile_1.anInt1325 = 0; @@ -1415,7 +1481,7 @@ public class Scene { SceneLoc farthest = tmp[farthestIndex]; farthest.cycle = cycle; - if (!occluded(l, farthest.minSceneTileX, farthest.maxSceneTileX, farthest.minSceneTileZ, farthest.maxSceneTileZ, farthest.entity.minY)) { + if (!occluded(level, farthest.minSceneTileX, farthest.maxSceneTileX, farthest.minSceneTileZ, farthest.maxSceneTileZ, farthest.entity.minY)) { farthest.entity.draw(farthest.yaw, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, farthest.x - eyeX, farthest.y - eyeY, farthest.z - eyeZ, farthest.bitset); } @@ -1424,7 +1490,7 @@ public class Scene { SceneTile tile_22 = tiles[k7][l8]; if (tile_22.anInt1325 != 0) { aList_477.pushBack(tile_22); - } else if (((k7 != i) || (l8 != j)) && tile_22.aBoolean1323) { + } else if (((k7 != tileX) || (l8 != tileZ)) && tile_22.aBoolean1323) { aList_477.pushBack(tile_22); } } @@ -1440,26 +1506,26 @@ public class Scene { if (!tile_1.aBoolean1323 || (tile_1.anInt1325 != 0)) { continue; } - if ((i <= eyeTileX) && (i > anInt449)) { - SceneTile tile_8 = tiles[i - 1][j]; + if ((tileX <= eyeTileX) && (tileX > anInt449)) { + SceneTile tile_8 = tiles[tileX - 1][tileZ]; if ((tile_8 != null) && tile_8.aBoolean1323) { continue; } } - if ((i >= eyeTileX) && (i < (anInt450 - 1))) { - SceneTile tile_9 = tiles[i + 1][j]; + if ((tileX >= eyeTileX) && (tileX < (anInt450 - 1))) { + SceneTile tile_9 = tiles[tileX + 1][tileZ]; if ((tile_9 != null) && tile_9.aBoolean1323) { continue; } } - if ((j <= eyeTileZ) && (j > anInt451)) { - SceneTile tile_10 = tiles[i][j - 1]; + if ((tileZ <= eyeTileZ) && (tileZ > anInt451)) { + SceneTile tile_10 = tiles[tileX][tileZ - 1]; if ((tile_10 != null) && tile_10.aBoolean1323) { continue; } } - if ((j >= eyeTileZ) && (j < (anInt452 - 1))) { - SceneTile tile_11 = tiles[i][j + 1]; + if ((tileZ >= eyeTileZ) && (tileZ < (anInt452 - 1))) { + SceneTile tile_11 = tiles[tileX][tileZ + 1]; if ((tile_11 != null) && tile_11.aBoolean1323) { continue; } @@ -1480,7 +1546,7 @@ public class Scene { } if (tile_1.anInt1328 != 0) { SceneWallDecoration deco = tile_1.wallDecoration; - if ((deco != null) && !occluded(l, i, j, deco.entity.minY)) { + if ((deco != null) && !occluded(level, tileX, tileZ, deco.entity.minY)) { if ((deco.occlude & tile_1.anInt1328) != 0) { deco.entity.draw(deco.yaw, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, deco.x - eyeX, deco.y - eyeY, deco.z - eyeZ, deco.bitset); } else if ((deco.occlude & 0x300) != 0) { @@ -1514,40 +1580,40 @@ public class Scene { } SceneWall wall_2 = tile_1.wall; if (wall_2 != null) { - if (((wall_2.occludeB & tile_1.anInt1328) != 0) && !wallOccluded(l, i, j, wall_2.occludeB)) { + if (((wall_2.occludeB & tile_1.anInt1328) != 0) && !wallOccluded(level, tileX, tileZ, wall_2.occludeB)) { wall_2.entityB.draw(0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, wall_2.x - eyeX, wall_2.y - eyeY, wall_2.z - eyeZ, wall_2.bitset); } - if (((wall_2.occludeA & tile_1.anInt1328) != 0) && !wallOccluded(l, i, j, wall_2.occludeA)) { + if (((wall_2.occludeA & tile_1.anInt1328) != 0) && !wallOccluded(level, tileX, tileZ, wall_2.occludeA)) { wall_2.entityA.draw(0, sinEyePitch, cosEyePitch, sinEyeYaw, cosEyeYaw, wall_2.x - eyeX, wall_2.y - eyeY, wall_2.z - eyeZ, wall_2.bitset); } } } - if (k < (maxLevel - 1)) { - SceneTile tile_12 = levelTiles[k + 1][i][j]; + if (dataLevel < (maxLevel - 1)) { + SceneTile tile_12 = levelTiles[dataLevel + 1][tileX][tileZ]; if ((tile_12 != null) && tile_12.aBoolean1323) { aList_477.pushBack(tile_12); } } - if (i < eyeTileX) { - SceneTile tile_13 = tiles[i + 1][j]; + if (tileX < eyeTileX) { + SceneTile tile_13 = tiles[tileX + 1][tileZ]; if ((tile_13 != null) && tile_13.aBoolean1323) { aList_477.pushBack(tile_13); } } - if (j < eyeTileZ) { - SceneTile tile_14 = tiles[i][j + 1]; + if (tileZ < eyeTileZ) { + SceneTile tile_14 = tiles[tileX][tileZ + 1]; if ((tile_14 != null) && tile_14.aBoolean1323) { aList_477.pushBack(tile_14); } } - if (i > eyeTileX) { - SceneTile tile_15 = tiles[i - 1][j]; + if (tileX > eyeTileX) { + SceneTile tile_15 = tiles[tileX - 1][tileZ]; if ((tile_15 != null) && tile_15.aBoolean1323) { aList_477.pushBack(tile_15); } } - if (j > eyeTileZ) { - SceneTile tile_16 = tiles[i][j - 1]; + if (tileZ > eyeTileZ) { + SceneTile tile_16 = tiles[tileX][tileZ - 1]; if ((tile_16 != null) && tile_16.aBoolean1323) { aList_477.pushBack(tile_16); } @@ -1555,318 +1621,389 @@ public class Scene { } while (true); } - public void method315(SceneTileUnderlay underlay, int i, int j, int k, int l, int i1, int j1, int k1) { - int l1; - int i2 = l1 = (j1 << 7) - eyeX; - int j2; - int k2 = j2 = (k1 << 7) - eyeZ; - int l2; - int i3 = l2 = i2 + 128; - int j3; - int k3 = j3 = k2 + 128; - int l3 = levelHeightmaps[i][j1][k1] - eyeY; - int i4 = levelHeightmaps[i][j1 + 1][k1] - eyeY; - int j4 = levelHeightmaps[i][j1 + 1][k1 + 1] - eyeY; - int k4 = levelHeightmaps[i][j1][k1 + 1] - eyeY; - int l4 = ((k2 * l) + (i2 * i1)) >> 16; - k2 = ((k2 * i1) - (i2 * l)) >> 16; - i2 = l4; - l4 = ((l3 * k) - (k2 * j)) >> 16; - k2 = ((l3 * j) + (k2 * k)) >> 16; - l3 = l4; - if (k2 < 50) { + public void drawTileUnderlay(SceneTileUnderlay underlay, int level, int sinEyePitch, int cosEyePitch, int sinEyeYaw, int cosEyeYaw, int tileX, int tileZ) { + int x3; + int x0 = x3 = (tileX << 7) - eyeX; + int z1; + int z0 = z1 = (tileZ << 7) - eyeZ; + int x2; + int x1 = x2 = x0 + 128; + int z3; + int z2 = z3 = z0 + 128; + + int y0 = levelHeightmaps[level][tileX][tileZ] - eyeY; + int y1 = levelHeightmaps[level][tileX + 1][tileZ] - eyeY; + int y2 = levelHeightmaps[level][tileX + 1][tileZ + 1] - eyeY; + int y3 = levelHeightmaps[level][tileX][tileZ + 1] - eyeY; + + int tmp = ((z0 * sinEyeYaw) + (x0 * cosEyeYaw)) >> 16; + z0 = ((z0 * cosEyeYaw) - (x0 * sinEyeYaw)) >> 16; + x0 = tmp; + + tmp = ((y0 * cosEyePitch) - (z0 * sinEyePitch)) >> 16; + z0 = ((y0 * sinEyePitch) + (z0 * cosEyePitch)) >> 16; + y0 = tmp; + + if (z0 < 50) { return; } - l4 = ((j2 * l) + (i3 * i1)) >> 16; - j2 = ((j2 * i1) - (i3 * l)) >> 16; - i3 = l4; - l4 = ((i4 * k) - (j2 * j)) >> 16; - j2 = ((i4 * j) + (j2 * k)) >> 16; - i4 = l4; - if (j2 < 50) { + + tmp = ((z1 * sinEyeYaw) + (x1 * cosEyeYaw)) >> 16; + z1 = ((z1 * cosEyeYaw) - (x1 * sinEyeYaw)) >> 16; + x1 = tmp; + + tmp = ((y1 * cosEyePitch) - (z1 * sinEyePitch)) >> 16; + z1 = ((y1 * sinEyePitch) + (z1 * cosEyePitch)) >> 16; + y1 = tmp; + + if (z1 < 50) { return; } - l4 = ((k3 * l) + (l2 * i1)) >> 16; - k3 = ((k3 * i1) - (l2 * l)) >> 16; - l2 = l4; - l4 = ((j4 * k) - (k3 * j)) >> 16; - k3 = ((j4 * j) + (k3 * k)) >> 16; - j4 = l4; - if (k3 < 50) { + + tmp = ((z2 * sinEyeYaw) + (x2 * cosEyeYaw)) >> 16; + z2 = ((z2 * cosEyeYaw) - (x2 * sinEyeYaw)) >> 16; + x2 = tmp; + + tmp = ((y2 * cosEyePitch) - (z2 * sinEyePitch)) >> 16; + z2 = ((y2 * sinEyePitch) + (z2 * cosEyePitch)) >> 16; + y2 = tmp; + + if (z2 < 50) { return; } - l4 = ((j3 * l) + (l1 * i1)) >> 16; - j3 = ((j3 * i1) - (l1 * l)) >> 16; - l1 = l4; - l4 = ((k4 * k) - (j3 * j)) >> 16; - j3 = ((k4 * j) + (j3 * k)) >> 16; - k4 = l4; - if (j3 < 50) { + + tmp = ((z3 * sinEyeYaw) + (x3 * cosEyeYaw)) >> 16; + z3 = ((z3 * cosEyeYaw) - (x3 * sinEyeYaw)) >> 16; + x3 = tmp; + + tmp = ((y3 * cosEyePitch) - (z3 * sinEyePitch)) >> 16; + z3 = ((y3 * sinEyePitch) + (z3 * cosEyePitch)) >> 16; + y3 = tmp; + + if (z3 < 50) { return; } - int i5 = Draw3D.centerX + ((i2 << 9) / k2); - int j5 = Draw3D.centerY + ((l3 << 9) / k2); - int k5 = Draw3D.centerX + ((i3 << 9) / j2); - int l5 = Draw3D.centerY + ((i4 << 9) / j2); - int i6 = Draw3D.centerX + ((l2 << 9) / k3); - int j6 = Draw3D.centerY + ((j4 << 9) / k3); - int k6 = Draw3D.centerX + ((l1 << 9) / j3); - int l6 = Draw3D.centerY + ((k4 << 9) / j3); + + int px0 = Draw3D.centerX + ((x0 << 9) / z0); + int py0 = Draw3D.centerY + ((y0 << 9) / z0); + int px1 = Draw3D.centerX + ((x1 << 9) / z1); + int py1 = Draw3D.centerY + ((y1 << 9) / z1); + int px2 = Draw3D.centerX + ((x2 << 9) / z2); + int py2 = Draw3D.centerY + ((y2 << 9) / z2); + int px3 = Draw3D.centerX + ((x3 << 9) / z3); + int py3 = Draw3D.centerY + ((y3 << 9) / z3); + Draw3D.alpha = 0; - if ((((i6 - k6) * (l5 - l6)) - ((j6 - l6) * (k5 - k6))) > 0) { - Draw3D.clipX = (i6 < 0) || (k6 < 0) || (k5 < 0) || (i6 > Draw2D.boundX) || (k6 > Draw2D.boundX) || (k5 > Draw2D.boundX); - if (aBoolean467 && method318(anInt468, anInt469, j6, l6, l5, i6, k6, k5)) { - clickTileX = j1; - clickTileZ = k1; + + if ((((px2 - px3) * (py1 - py3)) - ((py2 - py3) * (px1 - px3))) > 0) { + Draw3D.clipX = (px2 < 0) || (px3 < 0) || (px1 < 0) || (px2 > Draw2D.boundX) || (px3 > Draw2D.boundX) || (px1 > Draw2D.boundX); + + if (takingInput && pointInsideTriangle(mouseX, mouseY, py2, py3, py1, px2, px3, px1)) { + clickTileX = tileX; + clickTileZ = tileZ; } - if (underlay.anInt720 == -1) { - if (underlay.anInt718 != 0xbc614e) { - Draw3D.fillGouraudTriangle(j6, l6, l5, i6, k6, k5, underlay.anInt718, underlay.anInt719, underlay.anInt717); + + if (underlay.textureID == -1) { + if (underlay.northeastColor != 12345678) { + Draw3D.fillGouraudTriangle(py2, py3, py1, px2, px3, px1, underlay.northeastColor, underlay.northwestColor, underlay.southeastColor); } } else if (!lowmem) { - if (underlay.aBoolean721) { - Draw3D.fillTexturedTriangle(j6, l6, l5, i6, k6, k5, underlay.anInt718, underlay.anInt719, underlay.anInt717, i2, i3, l1, l3, i4, k4, k2, j2, j3, underlay.anInt720); + if (underlay.flat) { + Draw3D.fillTexturedTriangle(py2, py3, py1, px2, px3, px1, underlay.northeastColor, underlay.northwestColor, underlay.southeastColor, x0, x1, x3, y0, y1, y3, z0, z1, z3, underlay.textureID); } else { - Draw3D.fillTexturedTriangle(j6, l6, l5, i6, k6, k5, underlay.anInt718, underlay.anInt719, underlay.anInt717, l2, l1, i3, j4, k4, i4, k3, j3, j2, underlay.anInt720); + Draw3D.fillTexturedTriangle(py2, py3, py1, px2, px3, px1, underlay.northeastColor, underlay.northwestColor, underlay.southeastColor, x2, x3, x1, y2, y3, y1, z2, z3, z1, underlay.textureID); } } else { - int i7 = anIntArray485[underlay.anInt720]; - Draw3D.fillGouraudTriangle(j6, l6, l5, i6, k6, k5, method317(i7, underlay.anInt718), method317(i7, underlay.anInt719), method317(i7, underlay.anInt717)); + int color = textureColor[underlay.textureID]; + Draw3D.fillGouraudTriangle(py2, py3, py1, px2, px3, px1, mulLightness(color, underlay.northeastColor), mulLightness(color, underlay.northwestColor), mulLightness(color, underlay.southeastColor)); } } - if ((((i5 - k5) * (l6 - l5)) - ((j5 - l5) * (k6 - k5))) > 0) { - Draw3D.clipX = (i5 < 0) || (k5 < 0) || (k6 < 0) || (i5 > Draw2D.boundX) || (k5 > Draw2D.boundX) || (k6 > Draw2D.boundX); - if (aBoolean467 && method318(anInt468, anInt469, j5, l5, l6, i5, k5, k6)) { - clickTileX = j1; - clickTileZ = k1; + + if ((((px0 - px1) * (py3 - py1)) - ((py0 - py1) * (px3 - px1))) > 0) { + Draw3D.clipX = (px0 < 0) || (px1 < 0) || (px3 < 0) || (px0 > Draw2D.boundX) || (px1 > Draw2D.boundX) || (px3 > Draw2D.boundX); + + if (takingInput && pointInsideTriangle(mouseX, mouseY, py0, py1, py3, px0, px1, px3)) { + clickTileX = tileX; + clickTileZ = tileZ; } - if (underlay.anInt720 == -1) { - if (underlay.anInt716 != 0xbc614e) { - Draw3D.fillGouraudTriangle(j5, l5, l6, i5, k5, k6, underlay.anInt716, underlay.anInt717, underlay.anInt719); + + if (underlay.textureID == -1) { + if (underlay.southwestColor != 12345678) { + Draw3D.fillGouraudTriangle(py0, py1, py3, px0, px1, px3, underlay.southwestColor, underlay.southeastColor, underlay.northwestColor); } } else { if (!lowmem) { - Draw3D.fillTexturedTriangle(j5, l5, l6, i5, k5, k6, underlay.anInt716, underlay.anInt717, underlay.anInt719, i2, i3, l1, l3, i4, k4, k2, j2, j3, underlay.anInt720); + Draw3D.fillTexturedTriangle(py0, py1, py3, px0, px1, px3, underlay.southwestColor, underlay.southeastColor, underlay.northwestColor, x0, x1, x3, y0, y1, y3, z0, z1, z3, underlay.textureID); return; } - int j7 = anIntArray485[underlay.anInt720]; - Draw3D.fillGouraudTriangle(j5, l5, l6, i5, k5, k6, method317(j7, underlay.anInt716), method317(j7, underlay.anInt717), method317(j7, underlay.anInt719)); + int color = textureColor[underlay.textureID]; + Draw3D.fillGouraudTriangle(py0, py1, py3, px0, px1, px3, mulLightness(color, underlay.southwestColor), mulLightness(color, underlay.southeastColor), mulLightness(color, underlay.northwestColor)); } } } - public void method316(int i, int j, int k, SceneTileOverlay overlay, int l, int i1, int j1) { - int k1 = overlay.anIntArray673.length; - for (int l1 = 0; l1 < k1; l1++) { - int i2 = overlay.anIntArray673[l1] - eyeX; - int k2 = overlay.anIntArray674[l1] - eyeY; - int i3 = overlay.anIntArray675[l1] - eyeZ; - int k3 = ((i3 * k) + (i2 * j1)) >> 16; - i3 = ((i3 * j1) - (i2 * k)) >> 16; - i2 = k3; - k3 = ((k2 * l) - (i3 * j)) >> 16; - i3 = ((k2 * j) + (i3 * l)) >> 16; - k2 = k3; - if (i3 < 50) { + public void drawTileOverlay(int tileX, int sinEyePitch, int sinEyeYaw, SceneTileOverlay overlay, int cosEyePitch, int tileZ, int cosEyeYaw) { + int vertexCount = overlay.vertexX.length; + for (int v = 0; v < vertexCount; v++) { + int x = overlay.vertexX[v] - eyeX; + int y = overlay.vertexY[v] - eyeY; + int z = overlay.vertexZ[v] - eyeZ; + + int tmp = ((z * sinEyeYaw) + (x * cosEyeYaw)) >> 16; + z = ((z * cosEyeYaw) - (x * sinEyeYaw)) >> 16; + x = tmp; + + tmp = ((y * cosEyePitch) - (z * sinEyePitch)) >> 16; + z = ((y * sinEyePitch) + (z * cosEyePitch)) >> 16; + y = tmp; + + if (z < 50) { return; } - if (overlay.anIntArray682 != null) { - SceneTileOverlay.anIntArray690[l1] = i2; - SceneTileOverlay.anIntArray691[l1] = k2; - SceneTileOverlay.anIntArray692[l1] = i3; + + if (overlay.triangleTextureIDs != null) { + SceneTileOverlay.tmpViewspaceX[v] = x; + SceneTileOverlay.tmpViewspaceY[v] = y; + SceneTileOverlay.tmpViewspaceZ[v] = z; } - SceneTileOverlay.anIntArray688[l1] = Draw3D.centerX + ((i2 << 9) / i3); - SceneTileOverlay.anIntArray689[l1] = Draw3D.centerY + ((k2 << 9) / i3); + + SceneTileOverlay.tmpScreenX[v] = Draw3D.centerX + ((x << 9) / z); + SceneTileOverlay.tmpScreenY[v] = Draw3D.centerY + ((y << 9) / z); } + Draw3D.alpha = 0; - k1 = overlay.anIntArray679.length; - for (int j2 = 0; j2 < k1; j2++) { - int l2 = overlay.anIntArray679[j2]; - int j3 = overlay.anIntArray680[j2]; - int l3 = overlay.anIntArray681[j2]; - int i4 = SceneTileOverlay.anIntArray688[l2]; - int j4 = SceneTileOverlay.anIntArray688[j3]; - int k4 = SceneTileOverlay.anIntArray688[l3]; - int l4 = SceneTileOverlay.anIntArray689[l2]; - int i5 = SceneTileOverlay.anIntArray689[j3]; - int j5 = SceneTileOverlay.anIntArray689[l3]; - if ((((i4 - j4) * (j5 - i5)) - ((l4 - i5) * (k4 - j4))) > 0) { - Draw3D.clipX = (i4 < 0) || (j4 < 0) || (k4 < 0) || (i4 > Draw2D.boundX) || (j4 > Draw2D.boundX) || (k4 > Draw2D.boundX); - if (aBoolean467 && method318(anInt468, anInt469, l4, i5, j5, i4, j4, k4)) { - clickTileX = i; - clickTileZ = i1; + vertexCount = overlay.triangleVertexA.length; + for (int v = 0; v < vertexCount; v++) { + int a = overlay.triangleVertexA[v]; + int b = overlay.triangleVertexB[v]; + int c = overlay.triangleVertexC[v]; + + int x0 = SceneTileOverlay.tmpScreenX[a]; + int x1 = SceneTileOverlay.tmpScreenX[b]; + int x2 = SceneTileOverlay.tmpScreenX[c]; + + int y0 = SceneTileOverlay.tmpScreenY[a]; + int y1 = SceneTileOverlay.tmpScreenY[b]; + int y2 = SceneTileOverlay.tmpScreenY[c]; + + if ((((x0 - x1) * (y2 - y1)) - ((y0 - y1) * (x2 - x1))) > 0) { + Draw3D.clipX = (x0 < 0) || (x1 < 0) || (x2 < 0) || (x0 > Draw2D.boundX) || (x1 > Draw2D.boundX) || (x2 > Draw2D.boundX); + + if (takingInput && pointInsideTriangle(mouseX, mouseY, y0, y1, y2, x0, x1, x2)) { + clickTileX = tileX; + clickTileZ = tileZ; } - if ((overlay.anIntArray682 == null) || (overlay.anIntArray682[j2] == -1)) { - if (overlay.anIntArray676[j2] != 0xbc614e) { - Draw3D.fillGouraudTriangle(l4, i5, j5, i4, j4, k4, overlay.anIntArray676[j2], overlay.anIntArray677[j2], overlay.anIntArray678[j2]); + + if ((overlay.triangleTextureIDs == null) || (overlay.triangleTextureIDs[v] == -1)) { + if (overlay.triangleColorA[v] != 12345678) { + Draw3D.fillGouraudTriangle(y0, y1, y2, x0, x1, x2, overlay.triangleColorA[v], overlay.triangleColorB[v], overlay.triangleColorC[v]); } } else if (!lowmem) { - if (overlay.aBoolean683) { - Draw3D.fillTexturedTriangle(l4, i5, j5, i4, j4, k4, overlay.anIntArray676[j2], overlay.anIntArray677[j2], overlay.anIntArray678[j2], SceneTileOverlay.anIntArray690[0], SceneTileOverlay.anIntArray690[1], SceneTileOverlay.anIntArray690[3], SceneTileOverlay.anIntArray691[0], SceneTileOverlay.anIntArray691[1], SceneTileOverlay.anIntArray691[3], SceneTileOverlay.anIntArray692[0], SceneTileOverlay.anIntArray692[1], SceneTileOverlay.anIntArray692[3], overlay.anIntArray682[j2]); + if (overlay.flat) { + Draw3D.fillTexturedTriangle(y0, y1, y2, x0, x1, x2, overlay.triangleColorA[v], overlay.triangleColorB[v], overlay.triangleColorC[v], SceneTileOverlay.tmpViewspaceX[0], SceneTileOverlay.tmpViewspaceX[1], SceneTileOverlay.tmpViewspaceX[3], SceneTileOverlay.tmpViewspaceY[0], SceneTileOverlay.tmpViewspaceY[1], SceneTileOverlay.tmpViewspaceY[3], SceneTileOverlay.tmpViewspaceZ[0], SceneTileOverlay.tmpViewspaceZ[1], SceneTileOverlay.tmpViewspaceZ[3], overlay.triangleTextureIDs[v]); } else { - Draw3D.fillTexturedTriangle(l4, i5, j5, i4, j4, k4, overlay.anIntArray676[j2], overlay.anIntArray677[j2], overlay.anIntArray678[j2], SceneTileOverlay.anIntArray690[l2], SceneTileOverlay.anIntArray690[j3], SceneTileOverlay.anIntArray690[l3], SceneTileOverlay.anIntArray691[l2], SceneTileOverlay.anIntArray691[j3], SceneTileOverlay.anIntArray691[l3], SceneTileOverlay.anIntArray692[l2], SceneTileOverlay.anIntArray692[j3], SceneTileOverlay.anIntArray692[l3], overlay.anIntArray682[j2]); + Draw3D.fillTexturedTriangle(y0, y1, y2, x0, x1, x2, overlay.triangleColorA[v], overlay.triangleColorB[v], overlay.triangleColorC[v], SceneTileOverlay.tmpViewspaceX[a], SceneTileOverlay.tmpViewspaceX[b], SceneTileOverlay.tmpViewspaceX[c], SceneTileOverlay.tmpViewspaceY[a], SceneTileOverlay.tmpViewspaceY[b], SceneTileOverlay.tmpViewspaceY[c], SceneTileOverlay.tmpViewspaceZ[a], SceneTileOverlay.tmpViewspaceZ[b], SceneTileOverlay.tmpViewspaceZ[c], overlay.triangleTextureIDs[v]); } } else { - int k5 = anIntArray485[overlay.anIntArray682[j2]]; - Draw3D.fillGouraudTriangle(l4, i5, j5, i4, j4, k4, method317(k5, overlay.anIntArray676[j2]), method317(k5, overlay.anIntArray677[j2]), method317(k5, overlay.anIntArray678[j2])); + int k5 = textureColor[overlay.triangleTextureIDs[v]]; + Draw3D.fillGouraudTriangle(y0, y1, y2, x0, x1, x2, mulLightness(k5, overlay.triangleColorA[v]), mulLightness(k5, overlay.triangleColorB[v]), mulLightness(k5, overlay.triangleColorC[v])); } } } } - public int method317(int j, int k) { - k = 127 - k; - k = (k * (j & 0x7f)) / 160; - if (k < 2) { - k = 2; - } else if (k > 126) { - k = 126; + public int mulLightness(int hsl, int lightness) { + lightness = 127 - lightness; + lightness = (lightness * (hsl & 0x7f)) / 160; + if (lightness < 2) { + lightness = 2; + } else if (lightness > 126) { + lightness = 126; } - return (j & 0xff80) + k; + return (hsl & 0xff80) + lightness; } - public boolean method318(int i, int j, int k, int l, int i1, int j1, int k1, int l1) { - if ((j < k) && (j < l) && (j < i1)) { + public boolean pointInsideTriangle(int x, int y, int y0, int y1, int y2, int x0, int x1, int x2) { + if ((y < y0) && (y < y1) && (y < y2)) { return false; } - if ((j > k) && (j > l) && (j > i1)) { + if ((y > y0) && (y > y1) && (y > y2)) { return false; } - if ((i < j1) && (i < k1) && (i < l1)) { + if ((x < x0) && (x < x1) && (x < x2)) { return false; } - if ((i > j1) && (i > k1) && (i > l1)) { + if ((x > x0) && (x > x1) && (x > x2)) { return false; } - int i2 = ((j - k) * (k1 - j1)) - ((i - j1) * (l - k)); - int j2 = ((j - i1) * (j1 - l1)) - ((i - l1) * (k - i1)); - int k2 = ((j - l) * (l1 - k1)) - ((i - k1) * (i1 - l)); + int i2 = ((y - y0) * (x1 - x0)) - ((x - x0) * (y1 - y0)); + int j2 = ((y - y2) * (x0 - x2)) - ((x - x2) * (y0 - y2)); + int k2 = ((y - y1) * (x2 - x1)) - ((x - x1) * (y2 - y1)); return ((i2 * k2) > 0) && ((k2 * j2) > 0); } - public void method319() { - int j = levelOccluderCount[topLevel]; - SceneOccluder[] aclass47 = levelOccluders[topLevel]; + public void updateActiveOccluders() { + int count = levelOccluderCount[topLevel]; + SceneOccluder[] occluders = levelOccluders[topLevel]; + activeOccluderCount = 0; - for (int k = 0; k < j; k++) { - SceneOccluder occluder = aclass47[k]; - if (occluder.anInt791 == 1) { - int l = (occluder.anInt787 - eyeTileX) + 25; - if ((l < 0) || (l > 50)) { + for (int i = 0; i < count; i++) { + SceneOccluder occluder = occluders[i]; + + if (occluder.type == 1) { + int x = (occluder.minTileX - eyeTileX) + 25; + + if ((x < 0) || (x > 50)) { continue; } - int k1 = (occluder.anInt789 - eyeTileZ) + 25; - if (k1 < 0) { - k1 = 0; + + // Think of min/maxZ as the relative Z value in our visibility map, the +25 is because + // the visibility map origin is at 25,25 + int minZ = (occluder.minTileZ - eyeTileZ) + 25; + int maxZ = (occluder.maxTileZ - eyeTileZ) + 25; + + if (minZ < 0) { + minZ = 0; } - int j2 = (occluder.anInt790 - eyeTileZ) + 25; - if (j2 > 50) { - j2 = 50; + + if (maxZ > 50) { + maxZ = 50; } - boolean flag = false; - while (k1 <= j2) { - if (aBooleanArrayArray492[l][k1++]) { - flag = true; + + boolean ok = false; + + // checks if we can at least see one tile in the forward direction starting from our occluder + while (minZ <= maxZ) { + if (visibilityMap[x][minZ++]) { + ok = true; break; } } - if (!flag) { + + if (!ok) { continue; } - int j3 = eyeX - occluder.anInt792; - if (j3 > 32) { + + int deltaMinX = eyeX - occluder.minX; + + if (deltaMinX > 32) { occluder.mode = 1; } else { - if (j3 >= -32) { + if (deltaMinX >= -32) { continue; } occluder.mode = 2; - j3 = -j3; + deltaMinX = -deltaMinX; } - occluder.anInt801 = ((occluder.anInt794 - eyeZ) << 8) / j3; - occluder.anInt802 = ((occluder.anInt795 - eyeZ) << 8) / j3; - occluder.anInt803 = ((occluder.anInt796 - eyeY) << 8) / j3; - occluder.anInt804 = ((occluder.anInt797 - eyeY) << 8) / j3; + + occluder.minDeltaZ = ((occluder.minZ - eyeZ) << 8) / deltaMinX; + occluder.maxDeltaZ = ((occluder.maxZ - eyeZ) << 8) / deltaMinX; + occluder.minDeltaY = ((occluder.minY - eyeY) << 8) / deltaMinX; + occluder.maxDeltaY = ((occluder.maxY - eyeY) << 8) / deltaMinX; activeOccluders[activeOccluderCount++] = occluder; continue; } - if (occluder.anInt791 == 2) { - int i1 = (occluder.anInt789 - eyeTileZ) + 25; - if ((i1 < 0) || (i1 > 50)) { + + if (occluder.type == 2) { + int distanceMinTileZ = (occluder.minTileZ - eyeTileZ) + 25; + + if ((distanceMinTileZ < 0) || (distanceMinTileZ > 50)) { continue; } - int l1 = (occluder.anInt787 - eyeTileX) + 25; - if (l1 < 0) { - l1 = 0; + + int distanceMinTileX = (occluder.minTileX - eyeTileX) + 25; + + if (distanceMinTileX < 0) { + distanceMinTileX = 0; } - int k2 = (occluder.anInt788 - eyeTileX) + 25; - if (k2 > 50) { - k2 = 50; + + int distanceMaxTileX = (occluder.maxTileX - eyeTileX) + 25; + + if (distanceMaxTileX > 50) { + distanceMaxTileX = 50; } - boolean flag1 = false; - while (l1 <= k2) { - if (aBooleanArrayArray492[l1++][i1]) { - flag1 = true; + + boolean ok = false; + + while (distanceMinTileX <= distanceMaxTileX) { + if (visibilityMap[distanceMinTileX++][distanceMinTileZ]) { + ok = true; break; } } - if (!flag1) { + + if (!ok) { continue; } - int k3 = eyeZ - occluder.anInt794; - if (k3 > 32) { + + int deltaMinZ = eyeZ - occluder.minZ; + + if (deltaMinZ > 32) { occluder.mode = 3; } else { - if (k3 >= -32) { + if (deltaMinZ >= -32) { continue; } occluder.mode = 4; - k3 = -k3; + deltaMinZ = -deltaMinZ; } - occluder.anInt799 = ((occluder.anInt792 - eyeX) << 8) / k3; - occluder.anInt800 = ((occluder.anInt793 - eyeX) << 8) / k3; - occluder.anInt803 = ((occluder.anInt796 - eyeY) << 8) / k3; - occluder.anInt804 = ((occluder.anInt797 - eyeY) << 8) / k3; + + occluder.minDeltaX = ((occluder.minX - eyeX) << 8) / deltaMinZ; + occluder.maxDeltaX = ((occluder.maxX - eyeX) << 8) / deltaMinZ; + occluder.minDeltaY = ((occluder.minY - eyeY) << 8) / deltaMinZ; + occluder.maxDeltaY = ((occluder.maxY - eyeY) << 8) / deltaMinZ; activeOccluders[activeOccluderCount++] = occluder; - } else if (occluder.anInt791 == 4) { - int j1 = occluder.anInt796 - eyeY; - if (j1 > 128) { - int i2 = (occluder.anInt789 - eyeTileZ) + 25; - if (i2 < 0) { - i2 = 0; + } else if (occluder.type == 4) { + int deltaMaxY = occluder.minY - eyeY; + + if (deltaMaxY <= 128) { + continue; + } + + int deltaMinTileZ = (occluder.minTileZ - eyeTileZ) + 25; + + if (deltaMinTileZ < 0) { + deltaMinTileZ = 0; + } + + int deltaMaxTileZ = (occluder.maxTileZ - eyeTileZ) + 25; + + if (deltaMaxTileZ > 50) { + deltaMaxTileZ = 50; + } + + if (deltaMinTileZ <= deltaMaxTileZ) { + int deltaMinTileX = (occluder.minTileX - eyeTileX) + 25; + + if (deltaMinTileX < 0) { + deltaMinTileX = 0; } - int l2 = (occluder.anInt790 - eyeTileZ) + 25; - if (l2 > 50) { - l2 = 50; + + int deltaMaxTileX = (occluder.maxTileX - eyeTileX) + 25; + + if (deltaMaxTileX > 50) { + deltaMaxTileX = 50; } - if (i2 <= l2) { - int i3 = (occluder.anInt787 - eyeTileX) + 25; - if (i3 < 0) { - i3 = 0; - } - int l3 = (occluder.anInt788 - eyeTileX) + 25; - if (l3 > 50) { - l3 = 50; - } - boolean flag2 = false; - label0: - for (int i4 = i3; i4 <= l3; i4++) { - for (int j4 = i2; j4 <= l2; j4++) { - if (!aBooleanArrayArray492[i4][j4]) { - continue; - } - flag2 = true; - break label0; + + boolean ok = false; + + find_visible_tile: + for (int x = deltaMinTileX; x <= deltaMaxTileX; x++) { + for (int z = deltaMinTileZ; z <= deltaMaxTileZ; z++) { + if (visibilityMap[x][z]) { + ok = true; + break find_visible_tile; } } - if (flag2) { - occluder.mode = 5; - occluder.anInt799 = ((occluder.anInt792 - eyeX) << 8) / j1; - occluder.anInt800 = ((occluder.anInt793 - eyeX) << 8) / j1; - occluder.anInt801 = ((occluder.anInt794 - eyeZ) << 8) / j1; - occluder.anInt802 = ((occluder.anInt795 - eyeZ) << 8) / j1; - activeOccluders[activeOccluderCount++] = occluder; - } + } + + if (ok) { + occluder.mode = 5; + occluder.minDeltaX = ((occluder.minX - eyeX) << 8) / deltaMaxY; + occluder.maxDeltaX = ((occluder.maxX - eyeX) << 8) / deltaMaxY; + occluder.minDeltaZ = ((occluder.minZ - eyeZ) << 8) / deltaMaxY; + occluder.maxDeltaZ = ((occluder.maxZ - eyeZ) << 8) / deltaMaxY; + activeOccluders[activeOccluderCount++] = occluder; } } } @@ -2076,64 +2213,66 @@ public class Scene { public boolean occluded(int x, int y, int z) { for (int i = 0; i < activeOccluderCount; i++) { SceneOccluder occluder = activeOccluders[i]; + if (occluder.mode == 1) { - int i1 = occluder.anInt792 - x; - if (i1 <= 0) { + int dx = occluder.minX - x; + if (dx <= 0) { continue; } - int j2 = occluder.anInt794 + ((occluder.anInt801 * i1) >> 8); - int k3 = occluder.anInt795 + ((occluder.anInt802 * i1) >> 8); - int l4 = occluder.anInt796 + ((occluder.anInt803 * i1) >> 8); - int i6 = occluder.anInt797 + ((occluder.anInt804 * i1) >> 8); - if ((z >= j2) && (z <= k3) && (y >= l4) && (y <= i6)) { + + int minZ = occluder.minZ + ((occluder.minDeltaZ * dx) >> 8); + int maxZ = occluder.maxZ + ((occluder.maxDeltaZ * dx) >> 8); + int minY = occluder.minY + ((occluder.minDeltaY * dx) >> 8); + int maxY = occluder.maxY + ((occluder.maxDeltaY * dx) >> 8); + if ((z >= minZ) && (z <= maxZ) && (y >= minY) && (y <= maxY)) { return true; } } else if (occluder.mode == 2) { - int j1 = x - occluder.anInt792; - if (j1 <= 0) { + int dx = x - occluder.minX; + if (dx <= 0) { continue; } - int k2 = occluder.anInt794 + ((occluder.anInt801 * j1) >> 8); - int l3 = occluder.anInt795 + ((occluder.anInt802 * j1) >> 8); - int i5 = occluder.anInt796 + ((occluder.anInt803 * j1) >> 8); - int j6 = occluder.anInt797 + ((occluder.anInt804 * j1) >> 8); - if ((z >= k2) && (z <= l3) && (y >= i5) && (y <= j6)) { + int minZ = occluder.minZ + ((occluder.minDeltaZ * dx) >> 8); + int macZ = occluder.maxZ + ((occluder.maxDeltaZ * dx) >> 8); + int minY = occluder.minY + ((occluder.minDeltaY * dx) >> 8); + int maxY = occluder.maxY + ((occluder.maxDeltaY * dx) >> 8); + if ((z >= minZ) && (z <= macZ) && (y >= minY) && (y <= maxY)) { return true; } } else if (occluder.mode == 3) { - int k1 = occluder.anInt794 - z; - if (k1 <= 0) { + int dz = occluder.minZ - z; + if (dz <= 0) { continue; } - int l2 = occluder.anInt792 + ((occluder.anInt799 * k1) >> 8); - int i4 = occluder.anInt793 + ((occluder.anInt800 * k1) >> 8); - int j5 = occluder.anInt796 + ((occluder.anInt803 * k1) >> 8); - int k6 = occluder.anInt797 + ((occluder.anInt804 * k1) >> 8); - if ((x >= l2) && (x <= i4) && (y >= j5) && (y <= k6)) { + int minX = occluder.minX + ((occluder.minDeltaX * dz) >> 8); + int maxX = occluder.maxX + ((occluder.maxDeltaX * dz) >> 8); + int minY = occluder.minY + ((occluder.minDeltaY * dz) >> 8); + int maxY = occluder.maxY + ((occluder.maxDeltaY * dz) >> 8); + if ((x >= minX) && (x <= maxX) && (y >= minY) && (y <= maxY)) { return true; } } else if (occluder.mode == 4) { - int l1 = z - occluder.anInt794; - if (l1 <= 0) { + int dz = z - occluder.minZ; + if (dz <= 0) { continue; } - int i3 = occluder.anInt792 + ((occluder.anInt799 * l1) >> 8); - int j4 = occluder.anInt793 + ((occluder.anInt800 * l1) >> 8); - int k5 = occluder.anInt796 + ((occluder.anInt803 * l1) >> 8); - int l6 = occluder.anInt797 + ((occluder.anInt804 * l1) >> 8); - if ((x >= i3) && (x <= j4) && (y >= k5) && (y <= l6)) { + int minX = occluder.minX + ((occluder.minDeltaX * dz) >> 8); + int maxX = occluder.maxX + ((occluder.maxDeltaX * dz) >> 8); + int minY = occluder.minY + ((occluder.minDeltaY * dz) >> 8); + int maxY = occluder.maxY + ((occluder.maxDeltaY * dz) >> 8); + if ((x >= minX) && (x <= maxX) && (y >= minY) && (y <= maxY)) { return true; } } else if (occluder.mode == 5) { - int i2 = y - occluder.anInt796; - if (i2 <= 0) { + int dy = y - occluder.minY; + if (dy <= 0) { continue; } - int j3 = occluder.anInt792 + ((occluder.anInt799 * i2) >> 8); - int k4 = occluder.anInt793 + ((occluder.anInt800 * i2) >> 8); - int l5 = occluder.anInt794 + ((occluder.anInt801 * i2) >> 8); - int i7 = occluder.anInt795 + ((occluder.anInt802 * i2) >> 8); - if ((x >= j3) && (x <= k4) && (z >= l5) && (z <= i7)) { + int minX = occluder.minX + ((occluder.minDeltaX * dy) >> 8); + int maxX = occluder.maxX + ((occluder.maxDeltaX * dy) >> 8); + int minZ = occluder.minZ + ((occluder.minDeltaZ * dy) >> 8); + int maxZ = occluder.maxZ + ((occluder.maxDeltaZ * dy) >> 8); + if ((x >= minX) && (x <= maxX) && (z >= minZ) && (z <= maxZ)) { return true; } } diff --git a/src/main/java/SceneBuilder.java b/src/main/java/SceneBuilder.java index c961447..0ba8ad6 100644 --- a/src/main/java/SceneBuilder.java +++ b/src/main/java/SceneBuilder.java @@ -8,9 +8,9 @@ public class SceneBuilder { public static final int[] WALL_ROTATION_OCCLUDE_TYPE_1 = {16, 32, 64, 128}; public static final int[] anIntArray144 = {0, -1, 0, 1}; public static final int[] WALL_ROTATION_OCCLUDE_TYPE = {1, 2, 4, 8}; - public static int anInt123 = (int) (Math.random() * 17.0) - 8; + public static int randomHueOffset = (int) (Math.random() * 17.0) - 8; public static int anInt131; - public static int anInt133 = (int) (Math.random() * 33.0) - 16; + public static int randomLightnessOffset = (int) (Math.random() * 33.0) - 16; public static int minLevel = 99; public static boolean lowmem = true; @@ -387,21 +387,21 @@ public class SceneBuilder { return bool; } - public final int[] anIntArray124; - public final int[] anIntArray125; - public final int[] anIntArray126; - public final int[] anIntArray127; - public final int[] anIntArray128; + public final int[] blendChroma; + public final int[] blendSaturation; + public final int[] blendLightness; + public final int[] blendLuminance; + public final int[] blendMagnitude; public final int[][][] levelHeightmap; - public final byte[][][] aByteArrayArrayArray130; + public final byte[][][] levelTileOverlayIDs; public final byte[][][] levelShademap; public final int[][][] levelOccludemap; - public final byte[][][] aByteArrayArrayArray136; + public final byte[][][] levelTileOverlayShape; public final int[][] levelLightmap; - public final byte[][][] aByteArrayArrayArray142; + public final byte[][][] levelTileUnderlayIDs; public final int maxTileX; public final int maxTileZ; - public final byte[][][] aByteArrayArrayArray148; + public final byte[][][] levelTileOverlayRotation; public final byte[][][] levelTileFlags; public SceneBuilder(byte[][][] levelTileFlags, int maxTileZ, int maxTileX, int[][][] levelHeightmap) { @@ -410,24 +410,41 @@ public class SceneBuilder { this.maxTileZ = maxTileZ; this.levelHeightmap = levelHeightmap; this.levelTileFlags = levelTileFlags; - aByteArrayArrayArray142 = new byte[4][this.maxTileX][this.maxTileZ]; - aByteArrayArrayArray130 = new byte[4][this.maxTileX][this.maxTileZ]; - aByteArrayArrayArray136 = new byte[4][this.maxTileX][this.maxTileZ]; - aByteArrayArrayArray148 = new byte[4][this.maxTileX][this.maxTileZ]; + levelTileUnderlayIDs = new byte[4][this.maxTileX][this.maxTileZ]; + levelTileOverlayIDs = new byte[4][this.maxTileX][this.maxTileZ]; + levelTileOverlayShape = new byte[4][this.maxTileX][this.maxTileZ]; + levelTileOverlayRotation = new byte[4][this.maxTileX][this.maxTileZ]; levelOccludemap = new int[4][this.maxTileX + 1][this.maxTileZ + 1]; levelShademap = new byte[4][this.maxTileX + 1][this.maxTileZ + 1]; levelLightmap = new int[this.maxTileX + 1][this.maxTileZ + 1]; - anIntArray124 = new int[this.maxTileZ]; - anIntArray125 = new int[this.maxTileZ]; - anIntArray126 = new int[this.maxTileZ]; - anIntArray127 = new int[this.maxTileZ]; - anIntArray128 = new int[this.maxTileZ]; + blendChroma = new int[this.maxTileZ]; + blendSaturation = new int[this.maxTileZ]; + blendLightness = new int[this.maxTileZ]; + blendLuminance = new int[this.maxTileZ]; + blendMagnitude = new int[this.maxTileZ]; } - public void method171(SceneCollisionMap[] levelCollisionMaps, Scene scene) { + public void build(SceneCollisionMap[] levelCollisionMaps, Scene scene) { + applyBlockFlags(levelCollisionMaps); + randomize(); + + for (int level = 0; level < 4; level++) { + buildTerrainLighting(level); + buildTiles(scene, level); + updateDrawLevels(scene, level); + } + + scene.buildModels(64, 768, -50, -10, -50); + + buildBridges(scene); + buildOccluders(); + } + + private void applyBlockFlags(SceneCollisionMap[] levelCollisionMaps) { for (int level = 0; level < 4; level++) { for (int x = 0; x < 104; x++) { for (int z = 0; z < 104; z++) { + // solid? if ((levelTileFlags[level][x][z] & 0x1) == 1) { int trueLevel = level; @@ -440,220 +457,232 @@ public class SceneBuilder { levelCollisionMaps[trueLevel].addSolid(z, x); } } + } } } - anInt123 += (int) (Math.random() * 5.0) - 2; + } - if (anInt123 < -8) { - anInt123 = -8; + private static void randomize() { + randomHueOffset += (int) (Math.random() * 5.0) - 2; + + if (randomHueOffset < -8) { + randomHueOffset = -8; } - if (anInt123 > 8) { - anInt123 = 8; + if (randomHueOffset > 8) { + randomHueOffset = 8; } - anInt133 += (int) (Math.random() * 5.0) - 2; + randomLightnessOffset += (int) (Math.random() * 5.0) - 2; - if (anInt133 < -16) { - anInt133 = -16; + if (randomLightnessOffset < -16) { + randomLightnessOffset = -16; } - if (anInt133 > 16) { - anInt133 = 16; + if (randomLightnessOffset > 16) { + randomLightnessOffset = 16; } + } - for (int level = 0; level < 4; level++) { - byte[][] shademap = levelShademap[level]; - int lightAmbient = 96; - int lightAttenuation = 768; - int lightX = -50; - int lightY = -10; - int lightZ = -50; - int lightMagnitude = (lightAttenuation * (int) Math.sqrt((lightX * lightX) + (lightY * lightY) + (lightZ * lightZ))) >> 8; - - for (int z = 1; z < (maxTileZ - 1); z++) { - for (int x = 1; x < (maxTileX - 1); x++) { - int dx = levelHeightmap[level][x + 1][z] - levelHeightmap[level][x - 1][z]; - int dz = levelHeightmap[level][x][z + 1] - levelHeightmap[level][x][z - 1]; - int len = (int) Math.sqrt((dx * dx) + 65536 + (dz * dz)); - int nx = (dx << 8) / len; - int ny = 65536 / len; - int nz = (dz << 8) / len; - int light = lightAmbient + (((lightX * nx) + (lightY * ny) + (lightZ * nz)) / lightMagnitude); - int shade = (shademap[x - 1][z] >> 2) + (shademap[x + 1][z] >> 3) + (shademap[x][z - 1] >> 2) + (shademap[x][z + 1] >> 3) + (shademap[x][z] >> 1); - levelLightmap[x][z] = light - shade; - } + private void updateDrawLevels(Scene scene, int level) { + for (int stz = 1; stz < (maxTileZ - 1); stz++) { + for (int stx = 1; stx < (maxTileX - 1); stx++) { + scene.setDrawLevel(level, stx, stz, getDrawLevel(level, stx, stz)); } + } + } - for (int z = 0; z < maxTileZ; z++) { - anIntArray124[z] = 0; - anIntArray125[z] = 0; - anIntArray126[z] = 0; - anIntArray127[z] = 0; - anIntArray128[z] = 0; - } + private void buildTiles(Scene scene, int level) { + for (int z = 0; z < maxTileZ; z++) { + blendChroma[z] = 0; + blendSaturation[z] = 0; + blendLightness[z] = 0; + blendLuminance[z] = 0; + blendMagnitude[z] = 0; + } - for (int x0 = -5; x0 < (maxTileX + 5); x0++) { - for (int z0 = 0; z0 < maxTileZ; z0++) { - int x1 = x0 + 5; + for (int x0 = -5; x0 < (maxTileX + 5); x0++) { + for (int z0 = 0; z0 < maxTileZ; z0++) { + int x1 = x0 + 5; - if ((x1 >= 0) && (x1 < maxTileX)) { - int floID = aByteArrayArrayArray142[level][x1][z0] & 0xff; + if ((x1 >= 0) && (x1 < maxTileX)) { + int floID = levelTileUnderlayIDs[level][x1][z0] & 0xff; - if (floID > 0) { - FloType flo = FloType.instances[floID - 1]; - anIntArray124[z0] += flo.chroma; - anIntArray125[z0] += flo.saturation; - anIntArray126[z0] += flo.lightness; - anIntArray127[z0] += flo.luminance; - anIntArray128[z0]++; - } - } - - int x2 = x0 - 5; - - if ((x2 >= 0) && (x2 < maxTileX)) { - int floID = aByteArrayArrayArray142[level][x2][z0] & 0xff; - - if (floID > 0) { - FloType flo = FloType.instances[floID - 1]; - anIntArray124[z0] -= flo.chroma; - anIntArray125[z0] -= flo.saturation; - anIntArray126[z0] -= flo.lightness; - anIntArray127[z0] -= flo.luminance; - anIntArray128[z0]--; - } + if (floID > 0) { + FloType flo = FloType.instances[floID - 1]; + blendChroma[z0] += flo.chroma; + blendSaturation[z0] += flo.saturation; + blendLightness[z0] += flo.lightness; + blendLuminance[z0] += flo.luminance; + blendMagnitude[z0]++; } } - if ((x0 >= 1) && (x0 < (maxTileX - 1))) { - int i_35_ = 0; - int i_36_ = 0; - int i_37_ = 0; - int i_38_ = 0; - int i_39_ = 0; + int x2 = x0 - 5; - for (int z0 = -5; z0 < (maxTileZ + 5); z0++) { - int dz1 = z0 + 5; + if ((x2 >= 0) && (x2 < maxTileX)) { + int floID = levelTileUnderlayIDs[level][x2][z0] & 0xff; - if ((dz1 >= 0) && (dz1 < maxTileZ)) { - i_35_ += anIntArray124[dz1]; - i_36_ += anIntArray125[dz1]; - i_37_ += anIntArray126[dz1]; - i_38_ += anIntArray127[dz1]; - i_39_ += anIntArray128[dz1]; + if (floID > 0) { + FloType flo = FloType.instances[floID - 1]; + blendChroma[z0] -= flo.chroma; + blendSaturation[z0] -= flo.saturation; + blendLightness[z0] -= flo.lightness; + blendLuminance[z0] -= flo.luminance; + blendMagnitude[z0]--; + } + } + } + + if ((x0 >= 1) && (x0 < (maxTileX - 1))) { + int hueAccumulator = 0; + int saturationAccumulator = 0; + int lightnessAccumulator = 0; + int luminanceAccumulator = 0; + int magnitudeAccumulator = 0; + + for (int z0 = -5; z0 < (maxTileZ + 5); z0++) { + int dz1 = z0 + 5; + + if ((dz1 >= 0) && (dz1 < maxTileZ)) { + hueAccumulator += blendChroma[dz1]; + saturationAccumulator += blendSaturation[dz1]; + lightnessAccumulator += blendLightness[dz1]; + luminanceAccumulator += blendLuminance[dz1]; + magnitudeAccumulator += blendMagnitude[dz1]; + } + + int dz2 = z0 - 5; + + if ((dz2 >= 0) && (dz2 < maxTileZ)) { + hueAccumulator -= blendChroma[dz2]; + saturationAccumulator -= blendSaturation[dz2]; + lightnessAccumulator -= blendLightness[dz2]; + luminanceAccumulator -= blendLuminance[dz2]; + magnitudeAccumulator -= blendMagnitude[dz2]; + } + + if ((z0 >= 1) && (z0 < (maxTileZ - 1)) && (!lowmem || ((levelTileFlags[0][x0][z0] & 0x2) != 0) || (((levelTileFlags[level][x0][z0] & 0x10) == 0) && (getDrawLevel(level, x0, z0) == anInt131)))) { + if (level < minLevel) { + minLevel = level; } - int dz2 = z0 - 5; + int underlayID = levelTileUnderlayIDs[level][x0][z0] & 0xff; + int overlayID = levelTileOverlayIDs[level][x0][z0] & 0xff; - if ((dz2 >= 0) && (dz2 < maxTileZ)) { - i_35_ -= anIntArray124[dz2]; - i_36_ -= anIntArray125[dz2]; - i_37_ -= anIntArray126[dz2]; - i_38_ -= anIntArray127[dz2]; - i_39_ -= anIntArray128[dz2]; - } + if ((underlayID > 0) || (overlayID > 0)) { + int heightSW = levelHeightmap[level][x0][z0]; + int heightSE = levelHeightmap[level][x0 + 1][z0]; + int heightNE = levelHeightmap[level][x0 + 1][z0 + 1]; + int heightNW = levelHeightmap[level][x0][z0 + 1]; - if ((z0 >= 1) && (z0 < (maxTileZ - 1)) && (!lowmem || ((levelTileFlags[0][x0][z0] & 0x2) != 0) || (((levelTileFlags[level][x0][z0] & 0x10) == 0) && (getDrawLevel(level, x0, z0) == anInt131)))) { - if (level < minLevel) { - minLevel = level; + int lightSW = levelLightmap[x0][z0]; + int lightSE = levelLightmap[x0 + 1][z0]; + int lightNE = levelLightmap[x0 + 1][z0 + 1]; + int lightNW = levelLightmap[x0][z0 + 1]; + + int baseColor = -1; + int tintColor = -1; + + if (underlayID > 0) { + int hue = (hueAccumulator * 256) / luminanceAccumulator; + int saturation = saturationAccumulator / magnitudeAccumulator; + int lightness = lightnessAccumulator / magnitudeAccumulator; + + baseColor = decimateHSL(hue, saturation, lightness); + + hue = (hue + randomHueOffset) & 0xff; + lightness += randomLightnessOffset; + + if (lightness < 0) { + lightness = 0; + } else if (lightness > 255) { + lightness = 255; + } + + tintColor = decimateHSL(hue, saturation, lightness); } - int i_43_ = aByteArrayArrayArray142[level][x0][z0] & 0xff; - int floID = aByteArrayArrayArray130[level][x0][z0] & 0xff; + if (level > 0) { + boolean occludes = true; - if ((i_43_ > 0) || (floID > 0)) { - int heightSW = levelHeightmap[level][x0][z0]; - int heightSE = levelHeightmap[level][x0 + 1][z0]; - int heightNE = levelHeightmap[level][x0 + 1][z0 + 1]; - int heightNW = levelHeightmap[level][x0][z0 + 1]; - - int tileHeight00 = levelLightmap[x0][z0]; - int tileHeight10 = levelLightmap[x0 + 1][z0]; - int tileHeight11 = levelLightmap[x0 + 1][z0 + 1]; - int tileHeight01 = levelLightmap[x0][z0 + 1]; - - int i_53_ = -1; - int i_54_ = -1; - - if (i_43_ > 0) { - int hue = (i_35_ * 256) / i_38_; - int saturation = i_36_ / i_39_; - int lightness = i_37_ / i_39_; - - i_53_ = decimateHSL(hue, saturation, lightness); - - hue = (hue + anInt123) & 0xff; - lightness += anInt133; - - if (lightness < 0) { - lightness = 0; - } else if (lightness > 255) { - lightness = 255; - } - - i_54_ = decimateHSL(hue, saturation, lightness); + if ((underlayID == 0) && (levelTileOverlayShape[level][x0][z0] != 0)) { + occludes = false; } - if (level > 0) { - boolean bool = true; - if ((i_43_ == 0) && (aByteArrayArrayArray136[level][x0][z0] != 0)) { - bool = false; - } - if ((floID > 0) && !FloType.instances[floID - 1].aBoolean393) { - bool = false; - } - if (bool && (heightSW == heightSE) && (heightSW == heightNE) && (heightSW == heightNW)) { - levelOccludemap[level][x0][z0] |= 0x924; - } + if ((overlayID > 0) && !FloType.instances[overlayID - 1].occludes) { + occludes = false; } - int i_58_ = 0; - - if (i_53_ != -1) { - i_58_ = Draw3D.palette[mulHSL(i_54_, 96)]; + // occludes && flat + if (occludes && (heightSW == heightSE) && (heightSW == heightNE) && (heightSW == heightNW)) { + levelOccludemap[level][x0][z0] |= 0b100_100_100_100; } + } - if (floID == 0) { - scene.setTile(level, x0, z0, 0, 0, -1, heightSW, heightSE, heightNE, heightNW, mulHSL(i_53_, tileHeight00), mulHSL(i_53_, tileHeight10), mulHSL(i_53_, tileHeight11), mulHSL(i_53_, tileHeight01), 0, 0, 0, 0, i_58_, 0); + int shadeColor = 0; + + if (baseColor != -1) { + shadeColor = Draw3D.palette[mulHSL(tintColor, 96)]; + } + + if (overlayID == 0) { + scene.setTile(level, x0, z0, 0, 0, -1, heightSW, heightSE, heightNE, heightNW, mulHSL(baseColor, lightSW), mulHSL(baseColor, lightSE), mulHSL(baseColor, lightNE), mulHSL(baseColor, lightNW), 0, 0, 0, 0, shadeColor, 0); + } else { + int shape = levelTileOverlayShape[level][x0][z0] + 1; + byte rotation = levelTileOverlayRotation[level][x0][z0]; + FloType flo = FloType.instances[overlayID - 1]; + int textureID = flo.textureID; + int rgb; + int hsl; + + if (textureID >= 0) { + rgb = Draw3D.getAverageTextureRGB(textureID); + hsl = -1; + } else if (flo.rgb == 16711935) { + rgb = 0; + hsl = -2; + textureID = -1; } else { - int i_59_ = aByteArrayArrayArray136[level][x0][z0] + 1; - byte i_60_ = aByteArrayArrayArray148[level][x0][z0]; - FloType flo = FloType.instances[floID - 1]; - int textureID = flo.textureID; - int rgb; - int hsl; - - if (textureID >= 0) { - rgb = Draw3D.getAverageTextureRGB(textureID); - hsl = -1; - } else if (flo.rgb == 16711935) { - rgb = 0; - hsl = -2; - textureID = -1; - } else { - hsl = decimateHSL(flo.hue, flo.saturation, flo.lightness); - rgb = Draw3D.palette[adjustLightness(flo.hsl, 96)]; - } - - scene.setTile(level, x0, z0, i_59_, i_60_, textureID, heightSW, heightSE, heightNE, heightNW, mulHSL(i_53_, tileHeight00), mulHSL(i_53_, tileHeight10), mulHSL(i_53_, tileHeight11), mulHSL(i_53_, tileHeight01), adjustLightness(hsl, tileHeight00), adjustLightness(hsl, tileHeight10), adjustLightness(hsl, tileHeight11), adjustLightness(hsl, tileHeight01), i_58_, rgb); + hsl = decimateHSL(flo.hue, flo.saturation, flo.lightness); + rgb = Draw3D.palette[adjustLightness(flo.hsl, 96)]; } + + scene.setTile(level, x0, z0, shape, rotation, textureID, heightSW, heightSE, heightNE, heightNW, mulHSL(baseColor, lightSW), mulHSL(baseColor, lightSE), mulHSL(baseColor, lightNE), mulHSL(baseColor, lightNW), adjustLightness(hsl, lightSW), adjustLightness(hsl, lightSE), adjustLightness(hsl, lightNE), adjustLightness(hsl, lightNW), shadeColor, rgb); } } } } } + } + } - for (int stz = 1; stz < (maxTileZ - 1); stz++) { - for (int stx = 1; stx < (maxTileX - 1); stx++) { - scene.setDrawLevel(level, stx, stz, getDrawLevel(level, stx, stz)); - } + private void buildTerrainLighting(int level) { + byte[][] shademap = levelShademap[level]; + int lightAmbient = 96; + int lightAttenuation = 768; + int lightX = -50; + int lightY = -10; + int lightZ = -50; + int lightMagnitude = (lightAttenuation * (int) Math.sqrt((lightX * lightX) + (lightY * lightY) + (lightZ * lightZ))) >> 8; + + for (int z = 1; z < (maxTileZ - 1); z++) { + for (int x = 1; x < (maxTileX - 1); x++) { + int dx = levelHeightmap[level][x + 1][z] - levelHeightmap[level][x - 1][z]; + int dz = levelHeightmap[level][x][z + 1] - levelHeightmap[level][x][z - 1]; + int len = (int) Math.sqrt((dx * dx) + 65536 + (dz * dz)); + int nx = (dx << 8) / len; + int ny = 65536 / len; + int nz = (dz << 8) / len; + int light = lightAmbient + (((lightX * nx) + (lightY * ny) + (lightZ * nz)) / lightMagnitude); + int shade = (shademap[x - 1][z] >> 2) + (shademap[x + 1][z] >> 3) + (shademap[x][z - 1] >> 2) + (shademap[x][z + 1] >> 3) + (shademap[x][z] >> 1); + levelLightmap[x][z] = light - shade; } } + } - scene.applyLighting(-10, 64, -50, 768, -50); - + private void buildBridges(Scene scene) { for (int x = 0; x < maxTileX; x++) { for (int z = 0; z < maxTileZ; z++) { if ((levelTileFlags[1][x][z] & 0x2) == 2) { @@ -661,162 +690,183 @@ public class SceneBuilder { } } } + } - // TODO: Find a reasonable name for these. - int maskA = 0b001; - int maskB = 0b010; - int maskC = 0b100; + private void buildOccluders() { + int wall0 = 0b001; // this flag is set by walls with rotation 0 or 2 + int wall1 = 0b010; // this flag is set by walls with rotation 1 or 3 + int floor = 0b100; // this flag is set by floors which are flat - for (int current = 0; current < 4; current++) { - if (current > 0) { - maskA <<= 3; - maskB <<= 3; - maskC <<= 3; + for (int top = 0; top < 4; top++) { + if (top > 0) { + wall0 <<= 3; + wall1 <<= 3; + floor <<= 3; } - for (int level = 0; level <= current; level++) { + for (int level = 0; level <= top; level++) { for (int tileZ = 0; tileZ <= maxTileZ; tileZ++) { for (int tileX = 0; tileX <= maxTileX; tileX++) { - if ((levelOccludemap[level][tileX][tileZ] & maskA) != 0) { - int minZ = tileZ; - int maxZ = tileZ; - int minLevel = level; - int maxLevel = level; - for (/**/; minZ > 0; minZ--) { - if ((levelOccludemap[level][tileX][minZ - 1] & maskA) == 0) { - break; - } - } - for (/**/; maxZ < maxTileZ; maxZ++) { - if ((levelOccludemap[level][tileX][maxZ + 1] & maskA) == 0) { - break; - } - } + buildWallOccluders0(wall0, top, level, tileX, tileZ); + buildWallOccluders1(wall1, top, level, tileX, tileZ); + buildFloorOccluders(floor, top, level, tileX, tileZ); + } + } + } + } + } - find_min_level: - for (/**/; minLevel > 0; minLevel--) { - for (int z = minZ; z <= maxZ; z++) { - if ((levelOccludemap[minLevel - 1][tileX][z] & maskA) == 0) { - break find_min_level; - } - } - } + private void buildFloorOccluders(int floor, int top, int level, int tileX, int tileZ) { + if ((levelOccludemap[level][tileX][tileZ] & floor) != 0) { + int minTileX = tileX; + int maxTileX = tileX; + int minTileZ = tileZ; + int maxTileZ = tileZ; - find_max_level: - for (/**/; maxLevel < current; maxLevel++) { - for (int z = minZ; z <= maxZ; z++) { - if ((levelOccludemap[maxLevel + 1][tileX][z] & maskA) == 0) { - break find_max_level; - } - } - } + for (/**/; minTileZ > 0; minTileZ--) { + if ((levelOccludemap[level][tileX][minTileZ - 1] & floor) == 0) { + break; + } + } + for (/**/; maxTileZ < this.maxTileZ; maxTileZ++) { + if ((levelOccludemap[level][tileX][maxTileZ + 1] & floor) == 0) { + break; + } + } - int area = ((maxLevel + 1) - minLevel) * ((maxZ - minZ) + 1); + find_min_tile_x: + for (/**/; minTileX > 0; minTileX--) { + for (int z = minTileZ; z <= maxTileZ; z++) { + if ((levelOccludemap[level][minTileX - 1][z] & floor) == 0) { + break find_min_tile_x; + } + } + } - if (area >= 8) { - int maxY = levelHeightmap[maxLevel][tileX][minZ] - 240; - int minY = levelHeightmap[minLevel][tileX][minZ]; + find_max_tile_x: + for (/**/; maxTileX < this.maxTileX; maxTileX++) { + for (int z = minTileZ; z <= maxTileZ; z++) { + if ((levelOccludemap[level][maxTileX + 1][z] & floor) == 0) { + break find_max_tile_x; + } + } + } - Scene.method277(current, tileX * 128, minY, tileX * 128, (maxZ * 128) + 128, maxY, minZ * 128, 1); + if ((((maxTileX - minTileX) + 1) * ((maxTileZ - minTileZ) + 1)) >= 4) { + int y = levelHeightmap[level][minTileX][minTileZ]; - for (int l = minLevel; l <= maxLevel; l++) { - for (int z = minZ; z <= maxZ; z++) { - levelOccludemap[l][tileX][z] &= ~maskA; - } - } - } - } + Scene.addOccluder(top, minTileX * 128, y, (maxTileX * 128) + 128, (maxTileZ * 128) + 128, y, minTileZ * 128, 4); - if ((levelOccludemap[level][tileX][tileZ] & maskB) != 0) { - int minX = tileX; - int maxX = tileX; - int minLevel = level; - int maxLevel = level; + for (int x = minTileX; x <= maxTileX; x++) { + for (int z = minTileZ; z <= maxTileZ; z++) { + levelOccludemap[level][x][z] &= ~floor; + } + } + } + } + } - for (/**/; minX > 0; minX--) { - if ((levelOccludemap[level][minX - 1][tileZ] & maskB) == 0) { - break; - } - } - for (/**/; maxX < maxTileX; maxX++) { - if ((levelOccludemap[level][maxX + 1][tileZ] & maskB) == 0) { - break; - } - } + private void buildWallOccluders1(int wall1, int top, int level, int tileX, int tileZ) { + if ((levelOccludemap[level][tileX][tileZ] & wall1) != 0) { + int minTileX = tileX; + int maxTileX = tileX; + int minLevel = level; + int maxLevel = level; - find_min_level: - for (/**/; minLevel > 0; minLevel--) { - for (int x = minX; x <= maxX; x++) { - if ((levelOccludemap[minLevel - 1][x][tileZ] & maskB) == 0) { - break find_min_level; - } - } - } + for (/**/; minTileX > 0; minTileX--) { + if ((levelOccludemap[level][minTileX - 1][tileZ] & wall1) == 0) { + break; + } + } + for (/**/; maxTileX < this.maxTileX; maxTileX++) { + if ((levelOccludemap[level][maxTileX + 1][tileZ] & wall1) == 0) { + break; + } + } - find_max_level: - for (/**/; maxLevel < current; maxLevel++) { - for (int x = minX; x <= maxX; x++) { - if ((levelOccludemap[maxLevel + 1][x][tileZ] & maskB) == 0) { - break find_max_level; - } - } - } + find_min_level: + for (/**/; minLevel > 0; minLevel--) { + for (int x = minTileX; x <= maxTileX; x++) { + if ((levelOccludemap[minLevel - 1][x][tileZ] & wall1) == 0) { + break find_min_level; + } + } + } - int area = ((maxLevel + 1) - minLevel) * ((maxX - minX) + 1); + find_max_level: + for (/**/; maxLevel < top; maxLevel++) { + for (int x = minTileX; x <= maxTileX; x++) { + if ((levelOccludemap[maxLevel + 1][x][tileZ] & wall1) == 0) { + break find_max_level; + } + } + } - if (area >= 8) { - int maxY = levelHeightmap[maxLevel][minX][tileZ] - 240; - int minY = levelHeightmap[minLevel][minX][tileZ]; - Scene.method277(current, minX * 128, minY, (maxX * 128) + 128, tileZ * 128, maxY, tileZ * 128, 2); - for (int i_97_ = minLevel; i_97_ <= maxLevel; i_97_++) { - for (int i_98_ = minX; i_98_ <= maxX; i_98_++) { - levelOccludemap[i_97_][i_98_][tileZ] &= ~maskB; - } - } - } - } + int area = ((maxLevel + 1) - minLevel) * ((maxTileX - minTileX) + 1); - if ((levelOccludemap[level][tileX][tileZ] & maskC) != 0) { - int i_99_ = tileX; - int i_100_ = tileX; - int i_101_ = tileZ; - int i_102_ = tileZ; - for (/**/; i_101_ > 0; i_101_--) { - if ((levelOccludemap[level][tileX][i_101_ - 1] & maskC) == 0) { - break; - } - } - for (/**/; i_102_ < maxTileZ; i_102_++) { - if ((levelOccludemap[level][tileX][i_102_ + 1] & maskC) == 0) { - break; - } - } - while_4_: - for (/**/; i_99_ > 0; i_99_--) { - for (int i_103_ = i_101_; i_103_ <= i_102_; i_103_++) { - if ((levelOccludemap[level][i_99_ - 1][i_103_] & maskC) == 0) { - break while_4_; - } - } - } - while_5_: - for (/**/; i_100_ < maxTileX; i_100_++) { - for (int i_104_ = i_101_; i_104_ <= i_102_; i_104_++) { - if ((levelOccludemap[level][i_100_ + 1][i_104_] & maskC) == 0) { - break while_5_; - } - } - } - if ((((i_100_ - i_99_) + 1) * ((i_102_ - i_101_) + 1)) >= 4) { - int i_105_ = levelHeightmap[level][i_99_][i_101_]; - Scene.method277(current, i_99_ * 128, i_105_, (i_100_ * 128) + 128, (i_102_ * 128) + 128, i_105_, i_101_ * 128, 4); - for (int i_106_ = i_99_; i_106_ <= i_100_; i_106_++) { - for (int i_107_ = i_101_; i_107_ <= i_102_; i_107_++) { - levelOccludemap[level][i_106_][i_107_] &= ~maskC; - } - } - } - } + if (area >= 8) { + int minY = levelHeightmap[maxLevel][minTileX][tileZ] - 240; + int maxY = levelHeightmap[minLevel][minTileX][tileZ]; + + Scene.addOccluder(top, minTileX * 128, maxY, (maxTileX * 128) + 128, tileZ * 128, minY, tileZ * 128, 2); + + for (int l = minLevel; l <= maxLevel; l++) { + for (int x = minTileX; x <= maxTileX; x++) { + levelOccludemap[l][x][tileZ] &= ~wall1; + } + } + } + } + } + + private void buildWallOccluders0(int wall0, int top, int level, int tileX, int tileZ) { + if ((levelOccludemap[level][tileX][tileZ] & wall0) != 0) { + int minTileZ = tileZ; + int maxTileZ = tileZ; + int minLevel = level; + int maxLevel = level; + + for (/**/; minTileZ > 0; minTileZ--) { + if ((levelOccludemap[level][tileX][minTileZ - 1] & wall0) == 0) { + break; + } + } + + for (/**/; maxTileZ < this.maxTileZ; maxTileZ++) { + if ((levelOccludemap[level][tileX][maxTileZ + 1] & wall0) == 0) { + break; + } + } + + find_min_level: + for (/**/; minLevel > 0; minLevel--) { + for (int z = minTileZ; z <= maxTileZ; z++) { + if ((levelOccludemap[minLevel - 1][tileX][z] & wall0) == 0) { + break find_min_level; + } + } + } + + find_max_level: + for (/**/; maxLevel < top; maxLevel++) { + for (int z = minTileZ; z <= maxTileZ; z++) { + if ((levelOccludemap[maxLevel + 1][tileX][z] & wall0) == 0) { + break find_max_level; + } + } + } + + int area = ((maxLevel + 1) - minLevel) * ((maxTileZ - minTileZ) + 1); + + if (area >= 8) { + int minY = levelHeightmap[maxLevel][tileX][minTileZ] - 240; + int maxY = levelHeightmap[minLevel][tileX][minTileZ]; + + Scene.addOccluder(top, tileX * 128, maxY, tileX * 128, (maxTileZ * 128) + 128, minY, minTileZ * 128, 1); + + for (int l = minLevel; l <= maxLevel; l++) { + for (int z = minTileZ; z <= maxTileZ; z++) { + levelOccludemap[l][tileX][z] &= ~wall0; } } } @@ -951,7 +1001,7 @@ public class SceneBuilder { scene.add(entity, level, x, z, heightmapAverage, 1, 1, 0, bitset, info); if ((kind >= 12) && (kind <= 17) && (kind != 13) && (level > 0)) { - levelOccludemap[level][x][z] |= 0x924; + levelOccludemap[level][x][z] |= 0b100_100_100_100; } if (type.solid && (collision != null)) { @@ -974,7 +1024,7 @@ public class SceneBuilder { levelShademap[level][x][z + 1] = (byte) 50; } if (type.occludes) { - levelOccludemap[level][x][z] |= 0x249; + levelOccludemap[level][x][z] |= 0b001_001_001_001; } } else if (rotation == 1) { if (type.castShadow) { @@ -982,7 +1032,7 @@ public class SceneBuilder { levelShademap[level][x + 1][z + 1] = (byte) 50; } if (type.occludes) { - levelOccludemap[level][x][z + 1] |= 0x492; + levelOccludemap[level][x][z + 1] |= 0b010_010_010_010; } } else if (rotation == 2) { if (type.castShadow) { @@ -990,7 +1040,7 @@ public class SceneBuilder { levelShademap[level][x + 1][z + 1] = (byte) 50; } if (type.occludes) { - levelOccludemap[level][x + 1][z] |= 0x249; + levelOccludemap[level][x + 1][z] |= 0b001_001_001_001; } } else if (rotation == 3) { if (type.castShadow) { @@ -998,7 +1048,7 @@ public class SceneBuilder { levelShademap[level][x + 1][z] = (byte) 50; } if (type.occludes) { - levelOccludemap[level][x][z] |= 0x492; + levelOccludemap[level][x][z] |= 0b010_010_010_010; } } @@ -1049,17 +1099,17 @@ public class SceneBuilder { scene.setWall(WALL_ROTATION_OCCLUDE_TYPE[rotation], entity, WALL_ROTATION_OCCLUDE_TYPE[i_137_], entity_138_, level, x, z, heightmapAverage, bitset, info); if (type.occludes) { if (rotation == 0) { - levelOccludemap[level][x][z] |= 0x249; - levelOccludemap[level][x][z + 1] |= 0x492; + levelOccludemap[level][x][z] |= 0b001_001_001_001; + levelOccludemap[level][x][z + 1] |= 0b010_010_010_010; } else if (rotation == 1) { - levelOccludemap[level][x][z + 1] |= 0x492; - levelOccludemap[level][x + 1][z] |= 0x249; + levelOccludemap[level][x][z + 1] |= 0b010_010_010_010; + levelOccludemap[level][x + 1][z] |= 0b001_001_001_001; } else if (rotation == 2) { - levelOccludemap[level][x + 1][z] |= 0x249; - levelOccludemap[level][x][z] |= 0x492; + levelOccludemap[level][x + 1][z] |= 0b001_001_001_001; + levelOccludemap[level][x][z] |= 0b010_010_010_010; } else if (rotation == 3) { - levelOccludemap[level][x][z] |= 0x492; - levelOccludemap[level][x][z] |= 0x249; + levelOccludemap[level][x][z] |= 0b010_010_010_010; + levelOccludemap[level][x][z] |= 0b001_001_001_001; } } if (type.solid && (collision != null)) { @@ -1259,13 +1309,13 @@ public class SceneBuilder { break; } if (i_191_ <= 49) { - aByteArrayArrayArray130[i_187_][i_186_][i] = buffer.read8(); - aByteArrayArrayArray136[i_187_][i_186_][i] = (byte) ((i_191_ - 2) / 4); - aByteArrayArrayArray148[i_187_][i_186_][i] = (byte) (((i_191_ - 2) + i_188_) & 0x3); + levelTileOverlayIDs[i_187_][i_186_][i] = buffer.read8(); + levelTileOverlayShape[i_187_][i_186_][i] = (byte) ((i_191_ - 2) / 4); + levelTileOverlayRotation[i_187_][i_186_][i] = (byte) (((i_191_ - 2) + i_188_) & 0x3); } else if (i_191_ <= 81) { levelTileFlags[i_187_][i_186_][i] = (byte) (i_191_ - 49); } else { - aByteArrayArrayArray142[i_187_][i_186_][i] = (byte) (i_191_ - 81); + levelTileUnderlayIDs[i_187_][i_186_][i] = (byte) (i_191_ - 81); } } } else { diff --git a/src/main/java/SceneOccluder.java b/src/main/java/SceneOccluder.java index a6a2d9f..42626f6 100644 --- a/src/main/java/SceneOccluder.java +++ b/src/main/java/SceneOccluder.java @@ -4,24 +4,24 @@ public class SceneOccluder { - public int anInt787; - public int anInt788; - public int anInt789; - public int anInt790; - public int anInt791; - public int anInt792; - public int anInt793; - public int anInt794; - public int anInt795; - public int anInt796; - public int anInt797; + public int minTileX; + public int maxTileX; + public int minTileZ; + public int maxTileZ; + public int type; + public int minX; + public int maxX; + public int minZ; + public int maxZ; + public int minY; + public int maxY; public int mode; - public int anInt799; - public int anInt800; - public int anInt801; - public int anInt802; - public int anInt803; - public int anInt804; + public int minDeltaX; + public int maxDeltaX; + public int minDeltaZ; + public int maxDeltaZ; + public int minDeltaY; + public int maxDeltaY; public SceneOccluder() { } diff --git a/src/main/java/SceneTile.java b/src/main/java/SceneTile.java index 4b76088..34cef48 100644 --- a/src/main/java/SceneTile.java +++ b/src/main/java/SceneTile.java @@ -4,12 +4,12 @@ public class SceneTile extends DoublyLinkedList.Node { - public final int anInt1308; - public final int anInt1309; - public final int anInt1310; + public final int x; + public final int z; + public final int level; public final SceneLoc[] locs = new SceneLoc[5]; public final int[] locFlags = new int[5]; - public int anInt1307; + public int dataLevel; public SceneTileUnderlay underlay; public SceneTileOverlay overlay; public SceneWall wall; @@ -18,6 +18,10 @@ public class SceneTile extends DoublyLinkedList.Node { public SceneObjStack objStack; public int locCount; public int flags; + /** + * Used as a short circuit to prevent drawing tiles above the top level. Mostly applies to lowmem. + * @see Scene#draw(int, int, int, int, int, int) + */ public int drawLevel; public boolean aBoolean1322; public boolean aBoolean1323; @@ -28,10 +32,10 @@ public class SceneTile extends DoublyLinkedList.Node { public int anInt1328; public SceneTile bridge; - public SceneTile(int i, int j, int k) { - anInt1310 = anInt1307 = i; - anInt1308 = j; - anInt1309 = k; + public SceneTile(int level, int x, int z) { + this.level = dataLevel = level; + this.x = x; + this.z = z; } } diff --git a/src/main/java/SceneTileOverlay.java b/src/main/java/SceneTileOverlay.java index bde978d..ee56f63 100644 --- a/src/main/java/SceneTileOverlay.java +++ b/src/main/java/SceneTileOverlay.java @@ -4,213 +4,259 @@ public class SceneTileOverlay { - public static final int[][] anIntArrayArray696 = {{1, 3, 5, 7}, {1, 3, 5, 7}, {1, 3, 5, 7}, {1, 3, 5, 7, 6}, {1, 3, 5, 7, 6}, {1, 3, 5, 7, 6}, {1, 3, 5, 7, 6}, {1, 3, 5, 7, 2, 6}, {1, 3, 5, 7, 2, 8}, {1, 3, 5, 7, 2, 8}, {1, 3, 5, 7, 11, 12}, {1, 3, 5, 7, 11, 12}, {1, 3, 5, 7, 13, 14}}; - public static final int[][] anIntArrayArray697 = {{0, 1, 2, 3, 0, 0, 1, 3}, {1, 1, 2, 3, 1, 0, 1, 3}, {0, 1, 2, 3, 1, 0, 1, 3}, {0, 0, 1, 2, 0, 0, 2, 4, 1, 0, 4, 3}, {0, 0, 1, 4, 0, 0, 4, 3, 1, 1, 2, 4}, {0, 0, 4, 3, 1, 0, 1, 2, 1, 0, 2, 4}, {0, 1, 2, 4, 1, 0, 1, 4, 1, 0, 4, 3}, {0, 4, 1, 2, 0, 4, 2, 5, 1, 0, 4, 5, 1, 0, 5, 3}, {0, 4, 1, 2, 0, 4, 2, 3, 0, 4, 3, 5, 1, 0, 4, 5}, {0, 0, 4, 5, 1, 4, 1, 2, 1, 4, 2, 3, 1, 4, 3, 5}, {0, 0, 1, 5, 0, 1, 4, 5, 0, 1, 2, 4, 1, 0, 5, 3, 1, 5, 4, 3, 1, 4, 2, 3}, {1, 0, 1, 5, 1, 1, 4, 5, 1, 1, 2, 4, 0, 0, 5, 3, 0, 5, 4, 3, 0, 4, 2, 3}, {1, 0, 5, 4, 1, 0, 1, 5, 0, 0, 4, 3, 0, 4, 5, 3, 0, 5, 2, 3, 0, 1, 2, 5}}; - public static final int[] anIntArray688 = new int[6]; - public static final int[] anIntArray689 = new int[6]; - public static final int[] anIntArray690 = new int[6]; - public static final int[] anIntArray691 = new int[6]; - public static final int[] anIntArray692 = new int[6]; - public final int[] anIntArray673; - public final int[] anIntArray674; - public final int[] anIntArray675; - public final int[] anIntArray676; - public final int[] anIntArray677; - public final int[] anIntArray678; - public final int[] anIntArray679; - public final int[] anIntArray680; - public final int[] anIntArray681; - public final boolean aBoolean683; - public final int shape; - public final int angle; - public final int backgroundRGB; - public final int foregroundRGB; - public int[] anIntArray682; + public static final int[][] SHAPE_POINTS = { + {1, 3, 5, 7}, + {1, 3, 5, 7}, + {1, 3, 5, 7}, + {1, 3, 5, 7, 6}, + {1, 3, 5, 7, 6}, + {1, 3, 5, 7, 6}, + {1, 3, 5, 7, 6}, + {1, 3, 5, 7, 2, 6}, + {1, 3, 5, 7, 2, 8}, + {1, 3, 5, 7, 2, 8}, + {1, 3, 5, 7, 11, 12}, + {1, 3, 5, 7, 11, 12}, + {1, 3, 5, 7, 13, 14}, + }; + public static final int[][] SHAPE_PATHS = { + {0, 1, 2, 3, 0, 0, 1, 3}, + {1, 1, 2, 3, 1, 0, 1, 3}, + {0, 1, 2, 3, 1, 0, 1, 3}, + {0, 0, 1, 2, 0, 0, 2, 4, 1, 0, 4, 3}, + {0, 0, 1, 4, 0, 0, 4, 3, 1, 1, 2, 4}, + {0, 0, 4, 3, 1, 0, 1, 2, 1, 0, 2, 4}, + {0, 1, 2, 4, 1, 0, 1, 4, 1, 0, 4, 3}, + {0, 4, 1, 2, 0, 4, 2, 5, 1, 0, 4, 5, 1, 0, 5, 3}, + {0, 4, 1, 2, 0, 4, 2, 3, 0, 4, 3, 5, 1, 0, 4, 5}, + {0, 0, 4, 5, 1, 4, 1, 2, 1, 4, 2, 3, 1, 4, 3, 5}, + {0, 0, 1, 5, 0, 1, 4, 5, 0, 1, 2, 4, 1, 0, 5, 3, 1, 5, 4, 3, 1, 4, 2, 3}, + {1, 0, 1, 5, 1, 1, 4, 5, 1, 1, 2, 4, 0, 0, 5, 3, 0, 5, 4, 3, 0, 4, 2, 3}, + {1, 0, 5, 4, 1, 0, 1, 5, 0, 0, 4, 3, 0, 4, 5, 3, 0, 5, 2, 3, 0, 1, 2, 5}, + }; - public SceneTileOverlay(int i, int j, int k, int l, int i1, int j1, int k1, int l1, int i2, int j2, int k2, int l2, int i3, int j3, int k3, int l3, int i4, int k4, int l4) { - aBoolean683 = (i3 == l2) && (i3 == l) && (i3 == k2); - shape = j3; - angle = k1; - backgroundRGB = i2; - foregroundRGB = l4; - char c = '\200'; - int i5 = c / 2; - int j5 = c / 4; - int k5 = (c * 3) / 4; - int[] ai = anIntArrayArray696[j3]; - int l5 = ai.length; - anIntArray673 = new int[l5]; - anIntArray674 = new int[l5]; - anIntArray675 = new int[l5]; - int[] ai1 = new int[l5]; - int[] ai2 = new int[l5]; - int i6 = k4 * c; - int j6 = i * c; - for (int k6 = 0; k6 < l5; k6++) { - int l6 = ai[k6]; - if (((l6 & 1) == 0) && (l6 <= 8)) { - l6 = ((l6 - k1 - k1 - 1) & 7) + 1; - } - if ((l6 > 8) && (l6 <= 12)) { - l6 = ((l6 - 9 - k1) & 3) + 9; - } - if ((l6 > 12) && (l6 <= 16)) { - l6 = ((l6 - 13 - k1) & 3) + 13; - } - int i7; - int k7; - int i8; - int k8; - int j9; - if (l6 == 1) { - i7 = i6; - k7 = j6; - i8 = i3; - k8 = l1; - j9 = j; - } else if (l6 == 2) { - i7 = i6 + i5; - k7 = j6; - i8 = (i3 + l2) >> 1; - k8 = (l1 + i4) >> 1; - j9 = (j + l3) >> 1; - } else if (l6 == 3) { - i7 = i6 + c; - k7 = j6; - i8 = l2; - k8 = i4; - j9 = l3; - } else if (l6 == 4) { - i7 = i6 + c; - k7 = j6 + i5; - i8 = (l2 + l) >> 1; - k8 = (i4 + j2) >> 1; - j9 = (l3 + j1) >> 1; - } else if (l6 == 5) { - i7 = i6 + c; - k7 = j6 + c; - i8 = l; - k8 = j2; - j9 = j1; - } else if (l6 == 6) { - i7 = i6 + i5; - k7 = j6 + c; - i8 = (l + k2) >> 1; - k8 = (j2 + k) >> 1; - j9 = (j1 + k3) >> 1; - } else if (l6 == 7) { - i7 = i6; - k7 = j6 + c; - i8 = k2; - k8 = k; - j9 = k3; - } else if (l6 == 8) { - i7 = i6; - k7 = j6 + i5; - i8 = (k2 + i3) >> 1; - k8 = (k + l1) >> 1; - j9 = (k3 + j) >> 1; - } else if (l6 == 9) { - i7 = i6 + i5; - k7 = j6 + j5; - i8 = (i3 + l2) >> 1; - k8 = (l1 + i4) >> 1; - j9 = (j + l3) >> 1; - } else if (l6 == 10) { - i7 = i6 + k5; - k7 = j6 + i5; - i8 = (l2 + l) >> 1; - k8 = (i4 + j2) >> 1; - j9 = (l3 + j1) >> 1; - } else if (l6 == 11) { - i7 = i6 + i5; - k7 = j6 + k5; - i8 = (l + k2) >> 1; - k8 = (j2 + k) >> 1; - j9 = (j1 + k3) >> 1; - } else if (l6 == 12) { - i7 = i6 + j5; - k7 = j6 + i5; - i8 = (k2 + i3) >> 1; - k8 = (k + l1) >> 1; - j9 = (k3 + j) >> 1; - } else if (l6 == 13) { - i7 = i6 + j5; - k7 = j6 + j5; - i8 = i3; - k8 = l1; - j9 = j; - } else if (l6 == 14) { - i7 = i6 + k5; - k7 = j6 + j5; - i8 = l2; - k8 = i4; - j9 = l3; - } else if (l6 == 15) { - i7 = i6 + k5; - k7 = j6 + k5; - i8 = l; - k8 = j2; - j9 = j1; - } else { - i7 = i6 + j5; - k7 = j6 + k5; - i8 = k2; - k8 = k; - j9 = k3; - } - anIntArray673[k6] = i7; - anIntArray674[k6] = i8; - anIntArray675[k6] = k7; - ai1[k6] = k8; - ai2[k6] = j9; - } - int[] ai3 = anIntArrayArray697[j3]; - int j7 = ai3.length / 4; - anIntArray679 = new int[j7]; - anIntArray680 = new int[j7]; - anIntArray681 = new int[j7]; - anIntArray676 = new int[j7]; - anIntArray677 = new int[j7]; - anIntArray678 = new int[j7]; - if (i1 != -1) { - anIntArray682 = new int[j7]; - } - int l7 = 0; - for (int j8 = 0; j8 < j7; j8++) { - int l8 = ai3[l7]; - int k9 = ai3[l7 + 1]; - int i10 = ai3[l7 + 2]; - int k10 = ai3[l7 + 3]; - l7 += 4; - if (k9 < 4) { - k9 = (k9 - k1) & 3; - } - if (i10 < 4) { - i10 = (i10 - k1) & 3; - } - if (k10 < 4) { - k10 = (k10 - k1) & 3; - } - anIntArray679[j8] = k9; - anIntArray680[j8] = i10; - anIntArray681[j8] = k10; - if (l8 == 0) { - anIntArray676[j8] = ai1[k9]; - anIntArray677[j8] = ai1[i10]; - anIntArray678[j8] = ai1[k10]; - if (anIntArray682 != null) { - anIntArray682[j8] = -1; - } - } else { - anIntArray676[j8] = ai2[k9]; - anIntArray677[j8] = ai2[i10]; - anIntArray678[j8] = ai2[k10]; - if (anIntArray682 != null) { - anIntArray682[j8] = i1; - } - } - } - } + public static final int[] tmpScreenX = new int[6]; + public static final int[] tmpScreenY = new int[6]; + public static final int[] tmpViewspaceX = new int[6]; + public static final int[] tmpViewspaceY = new int[6]; + public static final int[] tmpViewspaceZ = new int[6]; + public final int[] vertexX; + public final int[] vertexY; + public final int[] vertexZ; + public final int[] triangleColorA; + public final int[] triangleColorB; + public final int[] triangleColorC; + public final int[] triangleVertexA; + public final int[] triangleVertexB; + public final int[] triangleVertexC; + public final boolean flat; + public final int shape; + public final int rotation; + public final int backgroundRGB; + public final int foregroundRGB; + public int[] triangleTextureIDs; + + public SceneTileOverlay(int tileZ, int southwestColor2, int northwestColor1, int northeastY, int textureID, int northeastColor2, int rotation, int southwestColor1, int backgroundRGB, int northeastColor1, int northwestY, int southeastY, int southwestY, int shape, int northwestColor2, int southeastColor2, int southeastColor1, int tileX, int foregroundRGB) { + flat = (southwestY == southeastY) && (southwestY == northeastY) && (southwestY == northwestY); + this.shape = shape; + this.rotation = rotation; + this.backgroundRGB = backgroundRGB; + this.foregroundRGB = foregroundRGB; + + final int ONE = 128; + final int HALF = ONE / 2; + final int QUARTER = ONE / 4; + final int THREE_QUARTER = (ONE * 3) / 4; + + int[] points = SHAPE_POINTS[shape]; + int vertexCount = points.length; + vertexX = new int[vertexCount]; + vertexY = new int[vertexCount]; + vertexZ = new int[vertexCount]; + int[] primaryColors = new int[vertexCount]; + int[] secondaryColors = new int[vertexCount]; + + int sceneX = tileX * ONE; + int sceneZ = tileZ * ONE; + + for (int v = 0; v < vertexCount; v++) { + int type = points[v]; + + if (((type & 1) == 0) && (type <= 8)) { + type = ((type - rotation - rotation - 1) & 7) + 1; + } + + if ((type > 8) && (type <= 12)) { + type = ((type - 9 - rotation) & 3) + 9; + } + + if ((type > 12) && (type <= 16)) { + type = ((type - 13 - rotation) & 3) + 13; + } + + int x; + int z; + int y; + int color1; + int color2; + if (type == 1) { + x = sceneX; + z = sceneZ; + y = southwestY; + color1 = southwestColor1; + color2 = southwestColor2; + } else if (type == 2) { + x = sceneX + HALF; + z = sceneZ; + y = (southwestY + southeastY) >> 1; + color1 = (southwestColor1 + southeastColor1) >> 1; + color2 = (southwestColor2 + southeastColor2) >> 1; + } else if (type == 3) { + x = sceneX + ONE; + z = sceneZ; + y = southeastY; + color1 = southeastColor1; + color2 = southeastColor2; + } else if (type == 4) { + x = sceneX + ONE; + z = sceneZ + HALF; + y = (southeastY + northeastY) >> 1; + color1 = (southeastColor1 + northeastColor1) >> 1; + color2 = (southeastColor2 + northeastColor2) >> 1; + } else if (type == 5) { + x = sceneX + ONE; + z = sceneZ + ONE; + y = northeastY; + color1 = northeastColor1; + color2 = northeastColor2; + } else if (type == 6) { + x = sceneX + HALF; + z = sceneZ + ONE; + y = (northeastY + northwestY) >> 1; + color1 = (northeastColor1 + northwestColor1) >> 1; + color2 = (northeastColor2 + northwestColor2) >> 1; + } else if (type == 7) { + x = sceneX; + z = sceneZ + ONE; + y = northwestY; + color1 = northwestColor1; + color2 = northwestColor2; + } else if (type == 8) { + x = sceneX; + z = sceneZ + HALF; + y = (northwestY + southwestY) >> 1; + color1 = (northwestColor1 + southwestColor1) >> 1; + color2 = (northwestColor2 + southwestColor2) >> 1; + } else if (type == 9) { + x = sceneX + HALF; + z = sceneZ + QUARTER; + y = (southwestY + southeastY) >> 1; + color1 = (southwestColor1 + southeastColor1) >> 1; + color2 = (southwestColor2 + southeastColor2) >> 1; + } else if (type == 10) { + x = sceneX + THREE_QUARTER; + z = sceneZ + HALF; + y = (southeastY + northeastY) >> 1; + color1 = (southeastColor1 + northeastColor1) >> 1; + color2 = (southeastColor2 + northeastColor2) >> 1; + } else if (type == 11) { + x = sceneX + HALF; + z = sceneZ + THREE_QUARTER; + y = (northeastY + northwestY) >> 1; + color1 = (northeastColor1 + northwestColor1) >> 1; + color2 = (northeastColor2 + northwestColor2) >> 1; + } else if (type == 12) { + x = sceneX + QUARTER; + z = sceneZ + HALF; + y = (northwestY + southwestY) >> 1; + color1 = (northwestColor1 + southwestColor1) >> 1; + color2 = (northwestColor2 + southwestColor2) >> 1; + } else if (type == 13) { + x = sceneX + QUARTER; + z = sceneZ + QUARTER; + y = southwestY; + color1 = southwestColor1; + color2 = southwestColor2; + } else if (type == 14) { + x = sceneX + THREE_QUARTER; + z = sceneZ + QUARTER; + y = southeastY; + color1 = southeastColor1; + color2 = southeastColor2; + } else if (type == 15) { + x = sceneX + THREE_QUARTER; + z = sceneZ + THREE_QUARTER; + y = northeastY; + color1 = northeastColor1; + color2 = northeastColor2; + } else { + x = sceneX + QUARTER; + z = sceneZ + THREE_QUARTER; + y = northwestY; + color1 = northwestColor1; + color2 = northwestColor2; + } + vertexX[v] = x; + vertexY[v] = y; + vertexZ[v] = z; + primaryColors[v] = color1; + secondaryColors[v] = color2; + } + + int[] paths = SHAPE_PATHS[shape]; + int triangleCount = paths.length / 4; + triangleVertexA = new int[triangleCount]; + triangleVertexB = new int[triangleCount]; + triangleVertexC = new int[triangleCount]; + triangleColorA = new int[triangleCount]; + triangleColorB = new int[triangleCount]; + triangleColorC = new int[triangleCount]; + + if (textureID != -1) { + triangleTextureIDs = new int[triangleCount]; + } + + int index = 0; + for (int i = 0; i < triangleCount; i++) { + int color = paths[index]; + int a = paths[index + 1]; + int b = paths[index + 2]; + int c = paths[index + 3]; + + index += 4; + + if (a < 4) { + a = (a - rotation) & 3; + } + + if (b < 4) { + b = (b - rotation) & 3; + } + + if (c < 4) { + c = (c - rotation) & 3; + } + + triangleVertexA[i] = a; + triangleVertexB[i] = b; + triangleVertexC[i] = c; + + if (color == 0) { + triangleColorA[i] = primaryColors[a]; + triangleColorB[i] = primaryColors[b]; + triangleColorC[i] = primaryColors[c]; + if (triangleTextureIDs != null) { + triangleTextureIDs[i] = -1; + } + } else { + triangleColorA[i] = secondaryColors[a]; + triangleColorB[i] = secondaryColors[b]; + triangleColorC[i] = secondaryColors[c]; + if (triangleTextureIDs != null) { + triangleTextureIDs[i] = textureID; + } + } + } + } } diff --git a/src/main/java/SceneTileUnderlay.java b/src/main/java/SceneTileUnderlay.java index 324ea34..5717c65 100644 --- a/src/main/java/SceneTileUnderlay.java +++ b/src/main/java/SceneTileUnderlay.java @@ -4,23 +4,22 @@ public class SceneTileUnderlay { - public final int anInt716; - public final int anInt717; - public final int anInt718; - public final int anInt719; - public final int anInt720; + public final int southwestColor; + public final int southeastColor; + public final int northeastColor; + public final int northwestColor; + public final int textureID; public final int rgb; - public boolean aBoolean721; + public boolean flat; - public SceneTileUnderlay(int i, int j, int k, int l, int i1, int j1, boolean flag) { - aBoolean721 = true; - anInt716 = i; - anInt717 = j; - anInt718 = k; - anInt719 = l; - anInt720 = i1; - rgb = j1; - aBoolean721 = flag; + public SceneTileUnderlay(int southwestColor, int southeastColor, int northeastColor, int northwestColor, int textureID, int rgb, boolean flat) { + this.southwestColor = southwestColor; + this.southeastColor = southeastColor; + this.northeastColor = northeastColor; + this.northwestColor = northwestColor; + this.textureID = textureID; + this.rgb = rgb; + this.flat = flat; } }