From ed6390f1481cf3700193efc8d635b2677350af35 Mon Sep 17 00:00:00 2001 From: thedaneeffect Date: Sat, 7 Jan 2023 20:15:22 -0500 Subject: [PATCH] Removed: Unused fields --- src/main/java/FloType.java | 308 ++++---- src/main/java/Game.java | 53 +- src/main/java/IdkType.java | 220 +++--- src/main/java/NPCType.java | 562 +++++++------- src/main/java/ObjType.java | 1284 +++++++++++++++---------------- src/main/java/SeqType.java | 42 +- src/main/java/SoundTrack.java | 58 +- src/main/java/SpotAnimType.java | 157 ++-- src/main/java/VarbitType.java | 27 +- src/main/java/VarpType.java | 76 +- 10 files changed, 1362 insertions(+), 1425 deletions(-) diff --git a/src/main/java/FloType.java b/src/main/java/FloType.java index 7249f63..bef2ef6 100644 --- a/src/main/java/FloType.java +++ b/src/main/java/FloType.java @@ -6,191 +6,187 @@ import java.io.IOException; public class FloType { - public static int count; - public static FloType[] instances; + public static int count; + public static FloType[] instances; - public static void unpack(FileArchive archive) throws IOException { - Buffer buffer = new Buffer(archive.read("flo.dat")); - count = buffer.read16U(); - if (instances == null) { - instances = new FloType[count]; - } - for (int j = 0; j < count; j++) { - if (instances[j] == null) { - instances[j] = new FloType(); - } - instances[j].method261(buffer); - } - } + public static void unpack(FileArchive archive) throws IOException { + Buffer buffer = new Buffer(archive.read("flo.dat")); + count = buffer.read16U(); + if (instances == null) { + instances = new FloType[count]; + } + for (int j = 0; j < count; j++) { + if (instances[j] == null) { + instances[j] = new FloType(); + } + instances[j].read(buffer); + } + } - public String unusedString; - public int rgb; - public int textureID = -1; - public boolean unusedBool = false; - public boolean occludes = true; - public int hue; - public int saturation; - public int lightness; - public int chroma; - public int luminance; - public int hsl; + public int rgb; + public int textureID = -1; + public boolean occludes = true; + public int hue; + public int saturation; + public int lightness; + public int chroma; + public int luminance; + public int hsl; - public FloType() { - } + public FloType() { + } - public void method261(Buffer buffer) { - do { - int i = buffer.read8U(); - if (i == 0) { - return; - } - if (i == 1) { - rgb = buffer.read24(); - setColor(rgb); - } else if (i == 2) { - textureID = buffer.read8U(); - } else if (i == 3) { - unusedBool = true; - } else if (i == 5) { - occludes = false; - } else if (i == 6) { - unusedString = buffer.readString(); - } else if (i == 7) { - int hue = this.hue; - int saturation = this.saturation; - int lightness = this.lightness; - int chroma = this.chroma; - int rgb = buffer.read24(); - setColor(rgb); - this.hue = hue; - this.saturation = saturation; - this.lightness = lightness; - this.chroma = chroma; - luminance = chroma; - } else { - System.out.println("Error unrecognised config code: " + i); - } - } while (true); - } + public void read(Buffer buffer) { + while (true) { + int code = buffer.read8U(); + if (code == 0) { + return; + } else if (code == 1) { + rgb = buffer.read24(); + setColor(rgb); + } else if (code == 2) { + textureID = buffer.read8U(); + } else if (code == 3) { + } else if (code == 5) { + occludes = false; + } else if (code == 6) { + buffer.readString(); // name + } else if (code == 7) { + int hue = this.hue; + int saturation = this.saturation; + int lightness = this.lightness; + int chroma = this.chroma; + int rgb = buffer.read24(); + setColor(rgb); + this.hue = hue; + this.saturation = saturation; + this.lightness = lightness; + this.chroma = chroma; + luminance = chroma; + } else { + System.out.println("Error unrecognised flo config code: " + code); + } + } + } - public void setColor(int rgb) { - double red = (double) ((rgb >> 16) & 0xff) / 256D; - double green = (double) ((rgb >> 8) & 0xff) / 256D; - double blue = (double) (rgb & 0xff) / 256D; + public void setColor(int rgb) { + double red = (double) ((rgb >> 16) & 0xff) / 256D; + double green = (double) ((rgb >> 8) & 0xff) / 256D; + double blue = (double) (rgb & 0xff) / 256D; - double min = red; + double min = red; - if (green < min) { - min = green; - } + if (green < min) { + min = green; + } - if (blue < min) { - min = blue; - } + if (blue < min) { + min = blue; + } - double max = red; + double max = red; - if (green > max) { - max = green; - } + if (green > max) { + max = green; + } - if (blue > max) { - max = blue; - } + if (blue > max) { + max = blue; + } - double h = 0.0D; - double s = 0.0D; - double l = (min + max) / 2D; + double h = 0.0D; + double s = 0.0D; + double l = (min + max) / 2D; - if (min != max) { - if (l < 0.5D) { - s = (max - min) / (max + min); - } + if (min != max) { + if (l < 0.5D) { + s = (max - min) / (max + min); + } - if (l >= 0.5D) { - s = (max - min) / (2D - max - min); - } + if (l >= 0.5D) { + s = (max - min) / (2D - max - min); + } - if (red == max) { - h = (green - blue) / (max - min); - } else if (green == max) { - h = 2D + ((blue - red) / (max - min)); - } else if (blue == max) { - h = 4D + ((red - green) / (max - min)); - } - } + if (red == max) { + h = (green - blue) / (max - min); + } else if (green == max) { + h = 2D + ((blue - red) / (max - min)); + } else if (blue == max) { + h = 4D + ((red - green) / (max - min)); + } + } - h /= 6D; + h /= 6D; - this.hue = (int) (h * 256D); - this.saturation = (int) (s * 256D); - this.lightness = (int) (l * 256D); + this.hue = (int) (h * 256D); + this.saturation = (int) (s * 256D); + this.lightness = (int) (l * 256D); - if (this.saturation < 0) { - this.saturation = 0; - } else if (this.saturation > 255) { - this.saturation = 255; - } + if (this.saturation < 0) { + this.saturation = 0; + } else if (this.saturation > 255) { + this.saturation = 255; + } - if (this.lightness < 0) { - this.lightness = 0; - } else if (this.lightness > 255) { - this.lightness = 255; - } + if (this.lightness < 0) { + this.lightness = 0; + } else if (this.lightness > 255) { + this.lightness = 255; + } - if (l > 0.5D) { - luminance = (int) ((1.0D - l) * s * 512D); - } else { - luminance = (int) (l * s * 512D); - } + if (l > 0.5D) { + luminance = (int) ((1.0D - l) * s * 512D); + } else { + luminance = (int) (l * s * 512D); + } - if (luminance < 1) { - luminance = 1; - } + if (luminance < 1) { + luminance = 1; + } - chroma = (int) (h * (double) luminance); + chroma = (int) (h * (double) luminance); - int hue = (this.hue + (int) (Math.random() * 16D)) - 8; + int hue = (this.hue + (int) (Math.random() * 16D)) - 8; - if (hue < 0) { - hue = 0; - } else if (hue > 255) { - hue = 255; - } + if (hue < 0) { + hue = 0; + } else if (hue > 255) { + hue = 255; + } - int saturation = (this.saturation + (int) (Math.random() * 48D)) - 24; + int saturation = (this.saturation + (int) (Math.random() * 48D)) - 24; - if (saturation < 0) { - saturation = 0; - } else if (saturation > 255) { - saturation = 255; - } + if (saturation < 0) { + saturation = 0; + } else if (saturation > 255) { + saturation = 255; + } - int lightness = (this.lightness + (int) (Math.random() * 48D)) - 24; + int lightness = (this.lightness + (int) (Math.random() * 48D)) - 24; - if (lightness < 0) { - lightness = 0; - } else if (lightness > 255) { - lightness = 255; - } + if (lightness < 0) { + lightness = 0; + } else if (lightness > 255) { + lightness = 255; + } - hsl = decimateHSL(hue, saturation, lightness); - } + hsl = decimateHSL(hue, saturation, lightness); + } - public int decimateHSL(int hue, int saturation, int lightness) { - if (lightness > 179) { - saturation /= 2; - } - if (lightness > 192) { - saturation /= 2; - } - if (lightness > 217) { - saturation /= 2; - } - if (lightness > 243) { - saturation /= 2; - } - return ((hue / 4) << 10) + ((saturation / 32) << 7) + (lightness / 2); - } + public int decimateHSL(int hue, int saturation, int lightness) { + if (lightness > 179) { + saturation /= 2; + } + if (lightness > 192) { + saturation /= 2; + } + if (lightness > 217) { + saturation /= 2; + } + if (lightness > 243) { + saturation /= 2; + } + return ((hue / 4) << 10) + ((saturation / 32) << 7) + (lightness / 2); + } } diff --git a/src/main/java/Game.java b/src/main/java/Game.java index e7df996..acd3f68 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1024,7 +1024,7 @@ public class Game extends GameShell { if (!lowmem) { drawProgress(90, "Unpacking sounds"); - SoundTrack.method240(new Buffer(archiveSounds.read("sounds.dat"))); + SoundTrack.unpack(new Buffer(archiveSounds.read("sounds.dat"))); } drawProgress(95, "Unpacking interfaces"); @@ -9734,19 +9734,19 @@ public class Game extends GameShell { int arith = 0; do { - int opcode = script[pos++]; + int code = script[pos++]; int register = 0; byte nextArithmetic = 0; - if (opcode == 0) { + if (code == 0) { return acc; - } else if (opcode == 1) { // load_skill_level {skill} + } else if (code == 1) { // load_skill_level {skill} register = skillLevel[script[pos++]]; - } else if (opcode == 2) { // load_skill_base_level {skill} + } else if (code == 2) { // load_skill_base_level {skill} register = skillBaseLevel[script[pos++]]; - } else if (opcode == 3) { // load_skill_exp {skill} + } else if (code == 3) { // load_skill_exp {skill} register = skillExperience[script[pos++]]; - } else if (opcode == 4) {// load_inv_count {interface id} {obj id} + } else if (code == 4) {// load_inv_count {interface id} {obj id} IfType inventory = IfType.instances[script[pos++]]; int objID = script[pos++]; if ((objID >= 0) && (objID < ObjType.count) && (!ObjType.get(objID).members || members)) { @@ -9756,21 +9756,21 @@ public class Game extends GameShell { } } } - } else if (opcode == 5) { // load_var {id} + } else if (code == 5) { // load_var {id} register = varps[script[pos++]]; - } else if (opcode == 6) { // load_next_level_xp {skill} + } else if (code == 6) { // load_next_level_xp {skill} register = levelExperience[skillBaseLevel[script[pos++]] - 1]; - } else if (opcode == 7) { + } else if (code == 7) { register = (varps[script[pos++]] * 100) / 46875; - } else if (opcode == 8) { // load_combat_level + } else if (code == 8) { // load_combat_level register = localPlayer.combatLevel; - } else if (opcode == 9) { // load_total_level + } else if (code == 9) { // load_total_level for (int skill = 0; skill < Skill.COUNT; skill++) { if (Skill.ENABLED[skill]) { register += skillBaseLevel[skill]; } } - } else if (opcode == 10) {// load_inv_contains {interface id} {obj id} + } else if (code == 10) {// load_inv_contains {interface id} {obj id} IfType c = IfType.instances[script[pos++]]; int objID = script[pos++] + 1; if ((objID >= 0) && (objID < ObjType.count) && (!ObjType.get(objID).members || members)) { @@ -9782,29 +9782,29 @@ public class Game extends GameShell { break; } } - } else if (opcode == 11) { // load_energy + } else if (code == 11) { // load_energy register = energy; - } else if (opcode == 12) { // load_weight + } else if (code == 12) { // load_weight register = weightCarried; - } else if (opcode == 13) {// load_bool {varp} {bit: 0..31} + } else if (code == 13) {// load_bool {varp} {bit: 0..31} int varp = varps[script[pos++]]; int bit = script[pos++]; register = ((varp & (1 << bit)) == 0) ? 0 : 1; - } else if (opcode == 14) {// load_varbit {varbit} + } else if (code == 14) {// load_varbit {varbit} VarbitType varbit = VarbitType.instances[script[pos++]]; int lsb = varbit.lsb; register = (varps[varbit.varp] >> lsb) & BITMASK[varbit.msb - lsb]; - } else if (opcode == 15) { // sub + } else if (code == 15) { // sub nextArithmetic = 1; - } else if (opcode == 16) { // div + } else if (code == 16) { // div nextArithmetic = 2; - } else if (opcode == 17) { // mul + } else if (code == 17) { // mul nextArithmetic = 3; - } else if (opcode == 18) { // load_world_x + } else if (code == 18) { // load_world_x register = (localPlayer.x >> 7) + sceneBaseTileX; - } else if (opcode == 19) { // load_world_z + } else if (code == 19) { // load_world_z register = (localPlayer.z >> 7) + sceneBaseTileZ; - } else if (opcode == 20) { // load {value} + } else if (code == 20) { // load {value} register = script[pos++]; } @@ -10452,8 +10452,8 @@ public class Game extends GameShell { flameThread = false; } - public void readZonePacket(int opcode) { - switch (opcode) { + public void readZonePacket(int code) { + switch (code) { case PacketIn.OBJ_ADD: readObjAdd(); break; @@ -11909,8 +11909,7 @@ public class Game extends GameShell { baseZ = in.read8U(); baseX = in.read8UC(); while (in.position < packetSize) { - int opcode = in.read8U(); - readZonePacket(opcode); + readZonePacket(in.read8U()); } } diff --git a/src/main/java/IdkType.java b/src/main/java/IdkType.java index d9ca5bb..919fbd2 100644 --- a/src/main/java/IdkType.java +++ b/src/main/java/IdkType.java @@ -6,128 +6,126 @@ import java.io.IOException; public class IdkType { - public static int count; - public static IdkType[] instances; + public static int count; + public static IdkType[] instances; - public static void unpack(FileArchive archive) throws IOException { - Buffer buffer = new Buffer(archive.read("idk.dat")); - count = buffer.read16U(); - if (instances == null) { - instances = new IdkType[count]; - } - for (int j = 0; j < count; j++) { - if (instances[j] == null) { - instances[j] = new IdkType(); - } - instances[j].read(buffer); - } - } + public static void unpack(FileArchive archive) throws IOException { + Buffer buffer = new Buffer(archive.read("idk.dat")); + count = buffer.read16U(); + if (instances == null) { + instances = new IdkType[count]; + } + for (int j = 0; j < count; j++) { + if (instances[j] == null) { + instances[j] = new IdkType(); + } + instances[j].read(buffer); + } + } - public final int[] colorSrc = new int[6]; - public final int[] colorDst = new int[6]; - public final int[] headModelIDs = {-1, -1, -1, -1, -1}; - public int type = -1; - public int[] modelIDs; - public boolean selectable = false; + public final int[] colorSrc = new int[6]; + public final int[] colorDst = new int[6]; + public final int[] headModelIDs = {-1, -1, -1, -1, -1}; + public int type = -1; + public int[] modelIDs; + public boolean selectable = false; - public IdkType() { - } + public IdkType() { + } - public void read(Buffer in) { - do { - int opcode = in.read8U(); - if (opcode == 0) { - return; - } + public void read(Buffer in) { + while (true) { + int code = in.read8U(); + if (code == 0) { + return; + } else if (code == 1) { + type = in.read8U(); + } else if (code == 2) { + int j = in.read8U(); + modelIDs = new int[j]; + for (int k = 0; k < j; k++) { + modelIDs[k] = in.read16U(); + } + } else if (code == 3) { + selectable = true; + } else if ((code >= 40) && (code < 50)) { + colorSrc[code - 40] = in.read16U(); + } else if ((code >= 50) && (code < 60)) { + colorDst[code - 50] = in.read16U(); + } else if ((code >= 60) && (code < 70)) { + headModelIDs[code - 60] = in.read16U(); + } else { + System.out.println("Error unrecognised identikit config code: " + code); + } + } + } - if (opcode == 1) { - type = in.read8U(); - } else if (opcode == 2) { - int j = in.read8U(); - modelIDs = new int[j]; - for (int k = 0; k < j; k++) { - modelIDs[k] = in.read16U(); - } - } else if (opcode == 3) { - selectable = true; - } else if ((opcode >= 40) && (opcode < 50)) { - colorSrc[opcode - 40] = in.read16U(); - } else if ((opcode >= 50) && (opcode < 60)) { - colorDst[opcode - 50] = in.read16U(); - } else if ((opcode >= 60) && (opcode < 70)) { - headModelIDs[opcode - 60] = in.read16U(); - } else { - System.out.println("Error unrecognised config code: " + opcode); - } - } while (true); - } + public boolean validateModel() { + if (modelIDs == null) { + return true; + } + boolean loaded = true; + for (int modelId : modelIDs) { + if (!Model.validate(modelId)) { + loaded = false; + } + } + return loaded; + } - public boolean validateModel() { - if (modelIDs == null) { - return true; - } - boolean loaded = true; - for (int modelId : modelIDs) { - if (!Model.validate(modelId)) { - loaded = false; - } - } - return loaded; - } + public Model getModel() { + if (modelIDs == null) { + return null; + } + Model[] models = new Model[modelIDs.length]; + for (int i = 0; i < modelIDs.length; i++) { + models[i] = Model.tryGet(modelIDs[i]); + } - public Model getModel() { - if (modelIDs == null) { - return null; - } - Model[] models = new Model[modelIDs.length]; - for (int i = 0; i < modelIDs.length; i++) { - models[i] = Model.tryGet(modelIDs[i]); - } + Model model; - Model model; + if (models.length == 1) { + model = models[0]; + } else { + model = new Model(models.length, models); + } - if (models.length == 1) { - model = models[0]; - } else { - model = new Model(models.length, models); - } + for (int i = 0; i < 6; i++) { + if (colorSrc[i] == 0) { + break; + } + model.recolor(colorSrc[i], colorDst[i]); + } - for (int i = 0; i < 6; i++) { - if (colorSrc[i] == 0) { - break; - } - model.recolor(colorSrc[i], colorDst[i]); - } + return model; + } - return model; - } + public boolean validateHeadModel() { + boolean loaded = true; + for (int i = 0; i < 5; i++) { + if ((headModelIDs[i] != -1) && !Model.validate(headModelIDs[i])) { + loaded = false; + } + } + return loaded; + } - public boolean validateHeadModel() { - boolean loaded = true; - for (int i = 0; i < 5; i++) { - if ((headModelIDs[i] != -1) && !Model.validate(headModelIDs[i])) { - loaded = false; - } - } - return loaded; - } - - public Model getHeadModel() { - Model[] models = new Model[5]; - int i = 0; - for (int j = 0; j < 5; j++) { - if (headModelIDs[j] != -1) { - models[i++] = Model.tryGet(headModelIDs[j]); - } - } - Model model = new Model(i, models); - for (int k = 0; k < 6; k++) { - if (colorSrc[k] == 0) { - break; - } - model.recolor(colorSrc[k], colorDst[k]); - } - return model; - } + public Model getHeadModel() { + Model[] models = new Model[5]; + int i = 0; + for (int j = 0; j < 5; j++) { + if (headModelIDs[j] != -1) { + models[i++] = Model.tryGet(headModelIDs[j]); + } + } + Model model = new Model(i, models); + for (int k = 0; k < 6; k++) { + if (colorSrc[k] == 0) { + break; + } + model.recolor(colorSrc[k], colorDst[k]); + } + return model; + } } diff --git a/src/main/java/NPCType.java b/src/main/java/NPCType.java index 4e73dea..c84b4cb 100644 --- a/src/main/java/NPCType.java +++ b/src/main/java/NPCType.java @@ -8,332 +8,328 @@ import java.io.IOException; public class NPCType { - private static int cachePosition; - private static Buffer dat; - public static int[] offsets; - private static NPCType[] cache; - public static Game game; - public static int count; - public static LRUMap modelCache = new LRUMap<>(30); + private static int cachePosition; + private static Buffer dat; + public static int[] offsets; + private static NPCType[] cache; + public static Game game; + public static int count; + public static LRUMap modelCache = new LRUMap<>(30); - public static NPCType get(int id) { - for (int j = 0; j < 20; j++) { - if (cache[j].uid == (long) id) { - return cache[j]; - } - } - cachePosition = (cachePosition + 1) % 20; - NPCType type = cache[cachePosition] = new NPCType(); - dat.position = offsets[id]; - type.uid = id; - type.read(dat); - return type; - } + public static NPCType get(int id) { + for (int j = 0; j < 20; j++) { + if (cache[j].uid == (long) id) { + return cache[j]; + } + } + cachePosition = (cachePosition + 1) % 20; + NPCType type = cache[cachePosition] = new NPCType(); + dat.position = offsets[id]; + type.uid = id; + type.read(dat); + return type; + } - public static void unpack(FileArchive archive) throws IOException { - dat = new Buffer(archive.read("npc.dat")); - Buffer buffer = new Buffer(archive.read("npc.idx")); - count = buffer.read16U(); - offsets = new int[count]; + public static void unpack(FileArchive archive) throws IOException { + dat = new Buffer(archive.read("npc.dat")); + Buffer buffer = new Buffer(archive.read("npc.idx")); + count = buffer.read16U(); + offsets = new int[count]; - int offset = 2; - for (int i = 0; i < count; i++) { - offsets[i] = offset; - offset += buffer.read16U(); - } + int offset = 2; + for (int i = 0; i < count; i++) { + offsets[i] = offset; + offset += buffer.read16U(); + } - cache = new NPCType[20]; + cache = new NPCType[20]; - for (int k = 0; k < 20; k++) { - cache[k] = new NPCType(); - } - } + for (int k = 0; k < 20; k++) { + cache[k] = new NPCType(); + } + } - public static void unload() { - modelCache = null; - offsets = null; - cache = null; - dat = null; - } + public static void unload() { + modelCache = null; + offsets = null; + cache = null; + dat = null; + } - public int seqTurnRightID = -1; - public int varbitID = -1; - public int seqTurnAroundID = -1; - public int varpID = -1; - public int level = -1; - public String name; - public String[] options; - public int seqWalkID = -1; - public byte size = 1; - public int[] colorDst; - public int unusedInt0 = -1; - public int[] headModelIDs; - public int headicon = -1; - public int[] colorSrc; - public int seqStandID = -1; - public long uid = -1L; - public int turnSpeed = 32; - public int seqTurnLeftID = -1; - public boolean interactable = true; - public int lightAmbient; - public int scaleZ = 128; - public boolean showOnMinimap = true; - public int[] overrides; - public byte[] desc; - public int unusedInt1 = -1; - public int scaleXY = 128; - public int lightAttenuation; - /** - * Causes the npc to render on top of other npcs. - */ - public boolean important = false; - public int[] modelIDs; - public int unusedInt2 = -1; + public int seqTurnRightID = -1; + public int varbitID = -1; + public int seqTurnAroundID = -1; + public int varpID = -1; + public int level = -1; + public String name; + public String[] options; + public int seqWalkID = -1; + public byte size = 1; + public int[] colorDst; + public int[] headModelIDs; + public int headicon = -1; + public int[] colorSrc; + public int seqStandID = -1; + public long uid = -1L; + public int turnSpeed = 32; + public int seqTurnLeftID = -1; + public boolean interactable = true; + public int lightAmbient; + public int scaleZ = 128; + public boolean showOnMinimap = true; + public int[] overrides; + public byte[] desc; + public int scaleXY = 128; + public int lightAttenuation; + /** + * Causes the npc to render on top of other npcs. + */ + public boolean important = false; + public int[] modelIDs; - public NPCType() { - } + public NPCType() { + } - public Model getHeadModel() { - if (overrides != null) { - NPCType type = getOverrideType(); - if (type == null) { - return null; - } else { - return type.getHeadModel(); - } - } + public Model getHeadModel() { + if (overrides != null) { + NPCType type = getOverrideType(); + if (type == null) { + return null; + } else { + return type.getHeadModel(); + } + } - if (headModelIDs == null) { - return null; - } + if (headModelIDs == null) { + return null; + } - boolean loaded = false; + boolean loaded = false; - for (int value : headModelIDs) { - if (!Model.validate(value)) { - loaded = true; - } - } + for (int value : headModelIDs) { + if (!Model.validate(value)) { + loaded = true; + } + } - if (loaded) { - return null; - } + if (loaded) { + return null; + } - Model[] models = new Model[headModelIDs.length]; + Model[] models = new Model[headModelIDs.length]; - for (int i = 0; i < headModelIDs.length; i++) { - models[i] = Model.tryGet(headModelIDs[i]); - } + for (int i = 0; i < headModelIDs.length; i++) { + models[i] = Model.tryGet(headModelIDs[i]); + } - Model model; + Model model; - if (models.length == 1) { - model = models[0]; - } else { - model = new Model(models.length, models); - } + if (models.length == 1) { + model = models[0]; + } else { + model = new Model(models.length, models); + } - if (colorSrc != null) { - for (int i = 0; i < colorSrc.length; i++) { - model.recolor(colorSrc[i], colorDst[i]); - } - } - return model; - } + if (colorSrc != null) { + for (int i = 0; i < colorSrc.length; i++) { + model.recolor(colorSrc[i], colorDst[i]); + } + } + return model; + } - public NPCType getOverrideType() { - int value = -1; + public NPCType getOverrideType() { + int value = -1; - if (varbitID != -1) { - VarbitType vb = VarbitType.instances[this.varbitID]; - int varp = vb.varp; - int lsb = vb.lsb; - int msb = vb.msb; - int mask = Game.BITMASK[msb - lsb]; - value = (game.varps[varp] >> lsb) & mask; - } else if (varpID != -1) { - value = game.varps[varpID]; - } + if (varbitID != -1) { + VarbitType vb = VarbitType.instances[this.varbitID]; + int varp = vb.varp; + int lsb = vb.lsb; + int msb = vb.msb; + int mask = Game.BITMASK[msb - lsb]; + value = (game.varps[varp] >> lsb) & mask; + } else if (varpID != -1) { + value = game.varps[varpID]; + } - if ((value < 0) || (value >= overrides.length) || (overrides[value] == -1)) { - return null; - } else { - return get(overrides[value]); - } - } + if ((value < 0) || (value >= overrides.length) || (overrides[value] == -1)) { + return null; + } else { + return get(overrides[value]); + } + } - public Model getSequencedModel(int secondaryTransformID, int primaryTransformID, int[] seqMask) { - if (overrides != null) { - NPCType override = getOverrideType(); - if (override == null) { - return null; - } else { - return override.getSequencedModel(secondaryTransformID, primaryTransformID, seqMask); - } - } + public Model getSequencedModel(int secondaryTransformID, int primaryTransformID, int[] seqMask) { + if (overrides != null) { + NPCType override = getOverrideType(); + if (override == null) { + return null; + } else { + return override.getSequencedModel(secondaryTransformID, primaryTransformID, seqMask); + } + } - Model model = modelCache.get(uid); + Model model = modelCache.get(uid); - if (model == null) { - boolean invalid = false; + if (model == null) { + boolean invalid = false; - for (int value : modelIDs) { - if (!Model.validate(value)) { - invalid = true; - } - } + for (int value : modelIDs) { + if (!Model.validate(value)) { + invalid = true; + } + } - if (invalid) { - return null; - } + if (invalid) { + return null; + } - Model[] models = new Model[modelIDs.length]; + Model[] models = new Model[modelIDs.length]; - for (int i = 0; i < modelIDs.length; i++) { - models[i] = Model.tryGet(modelIDs[i]); - } + for (int i = 0; i < modelIDs.length; i++) { + models[i] = Model.tryGet(modelIDs[i]); + } - if (models.length == 1) { - model = models[0]; - } else { - model = new Model(models.length, models); - } + if (models.length == 1) { + model = models[0]; + } else { + model = new Model(models.length, models); + } - if (colorSrc != null) { - for (int i = 0; i < colorSrc.length; i++) { - model.recolor(colorSrc[i], colorDst[i]); - } - } + if (colorSrc != null) { + for (int i = 0; i < colorSrc.length; i++) { + model.recolor(colorSrc[i], colorDst[i]); + } + } - model.createLabelReferences(); - model.calculateNormals(64 + lightAmbient, 850 + lightAttenuation, -30, -50, -30, true); - modelCache.put(uid, model); - } + model.createLabelReferences(); + model.calculateNormals(64 + lightAmbient, 850 + lightAttenuation, -30, -50, -30, true); + modelCache.put(uid, model); + } - Model tmp = Model.EMPTY; - tmp.set(model, SeqTransform.isNull(primaryTransformID) & SeqTransform.isNull(secondaryTransformID)); + Model tmp = Model.EMPTY; + tmp.set(model, SeqTransform.isNull(primaryTransformID) & SeqTransform.isNull(secondaryTransformID)); - if ((primaryTransformID != -1) && (secondaryTransformID != -1)) { - tmp.applyTransforms(primaryTransformID, secondaryTransformID, seqMask); - } else if (primaryTransformID != -1) { - tmp.applyTransform(primaryTransformID); - } + if ((primaryTransformID != -1) && (secondaryTransformID != -1)) { + tmp.applyTransforms(primaryTransformID, secondaryTransformID, seqMask); + } else if (primaryTransformID != -1) { + tmp.applyTransform(primaryTransformID); + } - if ((scaleXY != 128) || (scaleZ != 128)) { - tmp.scale(scaleXY, scaleXY, scaleZ); - } + if ((scaleXY != 128) || (scaleZ != 128)) { + tmp.scale(scaleXY, scaleXY, scaleZ); + } - tmp.calculateBoundsCylinder(); - tmp.labelFaces = null; - tmp.labelVertices = null; + tmp.calculateBoundsCylinder(); + tmp.labelFaces = null; + tmp.labelVertices = null; - if (size == 1) { - tmp.pickable = true; - } + if (size == 1) { + tmp.pickable = true; + } - return tmp; - } + return tmp; + } - public void read(Buffer buffer) { - do { - int opcode = buffer.read8U(); + public void read(Buffer buffer) { + while (true) { + int code = buffer.read8U(); - if (opcode == 0) { - return; - } + if (code == 0) { + return; + } else if (code == 1) { + int modelCount = buffer.read8U(); + modelIDs = new int[modelCount]; + for (int i = 0; i < modelCount; i++) { + modelIDs[i] = buffer.read16U(); + } + } else if (code == 2) { + name = buffer.readString(); + } else if (code == 3) { + desc = buffer.readStringRaw(); + } else if (code == 12) { + size = buffer.read(); + } else if (code == 13) { + seqStandID = buffer.read16U(); + } else if (code == 14) { + seqWalkID = buffer.read16U(); + } else if (code == 17) { + seqWalkID = buffer.read16U(); + seqTurnAroundID = buffer.read16U(); + seqTurnLeftID = buffer.read16U(); + seqTurnRightID = buffer.read16U(); + } else if ((code >= 30) && (code < 40)) { + if (options == null) { + options = new String[5]; + } - if (opcode == 1) { - int modelCount = buffer.read8U(); - modelIDs = new int[modelCount]; - for (int i = 0; i < modelCount; i++) { - modelIDs[i] = buffer.read16U(); - } - } else if (opcode == 2) { - name = buffer.readString(); - } else if (opcode == 3) { - desc = buffer.readStringRaw(); - } else if (opcode == 12) { - size = buffer.read(); - } else if (opcode == 13) { - seqStandID = buffer.read16U(); - } else if (opcode == 14) { - seqWalkID = buffer.read16U(); - } else if (opcode == 17) { - seqWalkID = buffer.read16U(); - seqTurnAroundID = buffer.read16U(); - seqTurnLeftID = buffer.read16U(); - seqTurnRightID = buffer.read16U(); - } else if ((opcode >= 30) && (opcode < 40)) { - if (options == null) { - options = new String[5]; - } - options[opcode - 30] = buffer.readString(); - if (options[opcode - 30].equalsIgnoreCase("hidden")) { - options[opcode - 30] = null; - } - } else if (opcode == 40) { - int recolorCount = buffer.read8U(); - colorSrc = new int[recolorCount]; - colorDst = new int[recolorCount]; - for (int i = 0; i < recolorCount; i++) { - colorSrc[i] = buffer.read16U(); - colorDst[i] = buffer.read16U(); - } - } else if (opcode == 60) { - int l = buffer.read8U(); - headModelIDs = new int[l]; - for (int l1 = 0; l1 < l; l1++) { - headModelIDs[l1] = buffer.read16U(); - } - } else if (opcode == 90) { - unusedInt2 = buffer.read16U(); - } else if (opcode == 91) { - unusedInt0 = buffer.read16U(); - } else if (opcode == 92) { - unusedInt1 = buffer.read16U(); - } else if (opcode == 93) { - showOnMinimap = false; - } else if (opcode == 95) { - level = buffer.read16U(); - } else if (opcode == 97) { - scaleXY = buffer.read16U(); - } else if (opcode == 98) { - scaleZ = buffer.read16U(); - } else if (opcode == 99) { - important = true; - } else if (opcode == 100) { - lightAmbient = buffer.read(); - } else if (opcode == 101) { - lightAttenuation = buffer.read() * 5; - } else if (opcode == 102) { - headicon = buffer.read16U(); - } else if (opcode == 103) { - turnSpeed = buffer.read16U(); - } else if (opcode == 106) { - varbitID = buffer.read16U(); + options[code - 30] = buffer.readString(); - if (varbitID == 65535) { - varbitID = -1; - } - varpID = buffer.read16U(); + if (options[code - 30].equalsIgnoreCase("hidden")) { + options[code - 30] = null; + } + } else if (code == 40) { + int count = buffer.read8U(); + colorSrc = new int[count]; + colorDst = new int[count]; - if (varpID == 65535) { - varpID = -1; - } + for (int i = 0; i < count; i++) { + colorSrc[i] = buffer.read16U(); + colorDst[i] = buffer.read16U(); + } + } else if (code == 60) { + int count = buffer.read8U(); + headModelIDs = new int[count]; + for (int l1 = 0; l1 < count; l1++) { + headModelIDs[l1] = buffer.read16U(); + } + } else if ((code == 90) || (code == 91) || (code == 92)) { + buffer.read16U(); + } else if (code == 93) { + showOnMinimap = false; + } else if (code == 95) { + level = buffer.read16U(); + } else if (code == 97) { + scaleXY = buffer.read16U(); + } else if (code == 98) { + scaleZ = buffer.read16U(); + } else if (code == 99) { + important = true; + } else if (code == 100) { + lightAmbient = buffer.read(); + } else if (code == 101) { + lightAttenuation = buffer.read() * 5; + } else if (code == 102) { + headicon = buffer.read16U(); + } else if (code == 103) { + turnSpeed = buffer.read16U(); + } else if (code == 106) { + varbitID = buffer.read16U(); - int overrideCount = buffer.read8U(); - overrides = new int[overrideCount + 1]; + if (varbitID == 65535) { + varbitID = -1; + } - for (int i = 0; i <= overrideCount; i++) { - overrides[i] = buffer.read16U(); - if (overrides[i] == 65535) { - overrides[i] = -1; - } - } - } else if (opcode == 107) { - interactable = false; - } - } while (true); - } + varpID = buffer.read16U(); + + if (varpID == 65535) { + varpID = -1; + } + + int overrideCount = buffer.read8U(); + overrides = new int[overrideCount + 1]; + + for (int i = 0; i <= overrideCount; i++) { + overrides[i] = buffer.read16U(); + + if (overrides[i] == 65535) { + overrides[i] = -1; + } + } + } else if (code == 107) { + interactable = false; + } + } + } } diff --git a/src/main/java/ObjType.java b/src/main/java/ObjType.java index 876478a..0ecaef5 100644 --- a/src/main/java/ObjType.java +++ b/src/main/java/ObjType.java @@ -8,649 +8,645 @@ import java.io.IOException; public class ObjType { - public static LRUMap iconCache = new LRUMap<>(100); - public static LRUMap modelCache = new LRUMap<>(50); - public static ObjType[] cached; - public static int cachePos; - public static Buffer dat; - public static int[] typeOffset; - public static int count; - - public static void unload() { - modelCache = null; - iconCache = null; - typeOffset = null; - cached = null; - dat = null; - } - - public static void unpack(FileArchive archive) throws IOException { - dat = new Buffer(archive.read("obj.dat")); - Buffer idx = new Buffer(archive.read("obj.idx")); - count = idx.read16U(); - typeOffset = new int[count]; - int offset = 2; - for (int j = 0; j < count; j++) { - typeOffset[j] = offset; - offset += idx.read16U(); - } - cached = new ObjType[10]; - for (int k = 0; k < 10; k++) { - cached[k] = new ObjType(); - } - } - - public static ObjType get(int id) { - for (int j = 0; j < 10; j++) { - if (cached[j].id == id) { - return cached[j]; - } - } - cachePos = (cachePos + 1) % 10; - ObjType type = cached[cachePos]; - dat.position = typeOffset[id]; - type.id = id; - type.reset(); - type.read(dat); - if (type.certificateID != -1) { - type.toCertificate(); - } - if (!Game.members && type.members) { - type.name = "Members Object"; - type.examine = "Login to a members' server to use this object.".getBytes(); - type.options = null; - type.inventoryOptions = null; - type.team = 0; - } - return type; - } - - public static Image24 getIcon(int id, int amount, int outlineColor) { - if (outlineColor == 0) { - Image24 icon = iconCache.get(id); - - if ((icon != null) && (icon.cropH != amount) && (icon.cropH != -1)) { - icon.unlink(); - icon = null; - } - - if (icon != null) { - return icon; - } - } - - ObjType type = get(id); - - if (type.stackID == null) { - amount = -1; - } - - if (amount > 1) { - int newID = -1; - for (int j1 = 0; j1 < 10; j1++) { - if ((amount >= type.stackCount[j1]) && (type.stackCount[j1] != 0)) { - newID = type.stackID[j1]; - } - } - if (newID != -1) { - type = get(newID); - } - } - - Model model = type.getModel(1); - - if (model == null) { - return null; - } - - // this will typically be the certificate item for noted stuff - Image24 originalIcon = null; - - if (type.certificateID != -1) { - originalIcon = getIcon(type.linkedID, 10, -1); - - if (originalIcon == null) { - return null; - } - } - - Image24 icon = new Image24(32, 32); - - // store state - int _cx = Draw3D.centerX; - int _cy = Draw3D.centerY; - int[] _loff = Draw3D.lineOffset; - int[] _pix = Draw2D.pixels; - int _w = Draw2D.width; - int _h = Draw2D.height; - int _l = Draw2D.left; - int _r = Draw2D.right; - int _t = Draw2D.top; - int _b = Draw2D.bottom; - - // set up drawing area - Draw3D.jagged = false; - Draw2D.bind(icon.pixels, 32, 32); - Draw2D.fillRect(0, 0, 32, 32, 0); - Draw3D.init2D(); - - int zoom = type.iconZoom; - - if (outlineColor == -1) { - zoom = (int) ((double) zoom * 1.5D); - } - - if (outlineColor > 0) { - zoom = (int) ((double) zoom * 1.04D); - } - - int sinPitch = (Draw3D.sin[type.iconPitch] * zoom) >> 16; - int cosPitch = (Draw3D.cos[type.iconPitch] * zoom) >> 16; - - model.drawSimple(0, type.iconYaw, type.iconRoll, type.iconPitch, type.iconOffsetX, sinPitch + (model.minY / 2) + type.iconOffsetY, cosPitch + type.iconOffsetY); - - // define outline - for (int x = 31; x >= 0; x--) { - for (int y = 31; y >= 0; y--) { - if (icon.pixels[x + (y * 32)] != 0) { - continue; - } - if ((x > 0) && (icon.pixels[(x - 1) + (y * 32)] > 1)) { - icon.pixels[x + (y * 32)] = 1; - } else if ((y > 0) && (icon.pixels[x + ((y - 1) * 32)] > 1)) { - icon.pixels[x + (y * 32)] = 1; - } else if ((x < 31) && (icon.pixels[x + 1 + (y * 32)] > 1)) { - icon.pixels[x + (y * 32)] = 1; - } else if ((y < 31) && (icon.pixels[x + ((y + 1) * 32)] > 1)) { - icon.pixels[x + (y * 32)] = 1; - } - } - } - - // color outline - if (outlineColor > 0) { - for (int x = 31; x >= 0; x--) { - for (int y = 31; y >= 0; y--) { - if (icon.pixels[x + (y * 32)] == 0) { - if ((x > 0) && (icon.pixels[(x - 1) + (y * 32)] == 1)) { - icon.pixels[x + (y * 32)] = outlineColor; - } else if ((y > 0) && (icon.pixels[x + ((y - 1) * 32)] == 1)) { - icon.pixels[x + (y * 32)] = outlineColor; - } else if ((x < 31) && (icon.pixels[x + 1 + (y * 32)] == 1)) { - icon.pixels[x + (y * 32)] = outlineColor; - } else if ((y < 31) && (icon.pixels[x + ((y + 1) * 32)] == 1)) { - icon.pixels[x + (y * 32)] = outlineColor; - } - } - } - } - } - // default outline color - else if (outlineColor == 0) { - for (int x = 31; x >= 0; x--) { - for (int y = 31; y >= 0; y--) { - if ((icon.pixels[x + (y * 32)] == 0) && (x > 0) && (y > 0) && (icon.pixels[(x - 1) + ((y - 1) * 32)] > 0)) { - icon.pixels[x + (y * 32)] = 0x302020; - } - } - } - } - - if (type.certificateID != -1) { - int w = originalIcon.cropW; - int h = originalIcon.cropH; - originalIcon.cropW = 32; - originalIcon.cropH = 32; - originalIcon.draw(0, 0); - originalIcon.cropW = w; - originalIcon.cropH = h; - } - - if (outlineColor == 0) { - iconCache.put(id, icon); - } - - // restore state - Draw2D.bind(_pix, _w, _h); - Draw2D.setBounds(_l, _t, _r, _b); - Draw3D.centerX = _cx; - Draw3D.centerY = _cy; - Draw3D.lineOffset = _loff; - Draw3D.jagged = true; - - if (type.stackable) { - icon.cropW = 33; - } else { - icon.cropW = 32; - } - - icon.cropH = amount; - return icon; - } - - public byte femaleOffsetY; - public int cost; - public int[] srcColor; - public int id = -1; - public int[] dstColor; - public boolean members; - public int femaleModelID2; - public int certificateID; - public int femaleModelID1; - public int maleModelID0; - public int maleHeadModelID1; - public int scaleX; - public String[] options; - public int iconOffsetX; - public String name; - public int femaleHeadModelID1; - public int modelID; - public int maleHeadModelID0; - public boolean stackable; - public byte[] examine; - public int linkedID; - public int iconZoom; - public int lightAttenuation; - public int maleModelID2; - public int maleModelID1; - public String[] inventoryOptions; - public int iconPitch; - public int scaleY; - public int scaleZ; - public int[] stackID; - public int iconOffsetY; - public int lightAmbient; - public int femaleHeadModelID0; - public int iconYaw; - public int unusedInt; - public int femaleModelID0; - public int[] stackCount; - public int team; - public int iconRoll; - public byte maleOffsetY; - - public ObjType() { - } - - public boolean validateHeadModel(int gender) { - int modelID0 = maleHeadModelID0; - int modelID1 = maleHeadModelID1; - - if (gender == 1) { - modelID0 = femaleHeadModelID0; - modelID1 = femaleHeadModelID1; - } - - if (modelID0 == -1) { - return true; - } - - boolean valid = Model.validate(modelID0); - - if ((modelID1 != -1) && !Model.validate(modelID1)) { - valid = false; - } - return valid; - } - - public Model getHeadModel(int gender) { - int modelID0 = maleHeadModelID0; - int modelID1 = maleHeadModelID1; - - if (gender == 1) { - modelID0 = femaleHeadModelID0; - modelID1 = femaleHeadModelID1; - } - - if (modelID0 == -1) { - return null; - } - - Model model = Model.tryGet(modelID0); - - if (modelID1 != -1) { - model = new Model(2, new Model[]{model, Model.tryGet(modelID1)}); - } - - if (srcColor != null) { - for (int i = 0; i < srcColor.length; i++) { - model.recolor(srcColor[i], dstColor[i]); - } - } - return model; - } - - public boolean validateWornModel(int gender) { - int modelID0 = maleModelID0; - int modelID1 = maleModelID1; - int modelID2 = maleModelID2; - - if (gender == 1) { - modelID0 = femaleModelID0; - modelID1 = femaleModelID1; - modelID2 = femaleModelID2; - } - - if (modelID0 == -1) { - return true; - } - - boolean valid = Model.validate(modelID0); - - if ((modelID1 != -1) && !Model.validate(modelID1)) { - valid = false; - } - - if ((modelID2 != -1) && !Model.validate(modelID2)) { - valid = false; - } - - return valid; - } - - public Model getWornModel(int gender) { - int modelID0 = maleModelID0; - int modelID1 = maleModelID1; - int modelID2 = maleModelID2; - - if (gender == 1) { - modelID0 = femaleModelID0; - modelID1 = femaleModelID1; - modelID2 = femaleModelID2; - } - - if (modelID0 == -1) { - return null; - } - - Model model = Model.tryGet(modelID0); - - if (modelID1 != -1) { - if (modelID2 != -1) { - model = new Model(3, new Model[]{model, Model.tryGet(modelID1), Model.tryGet(modelID2)}); - } else { - model = new Model(2, new Model[]{model, Model.tryGet(modelID1)}); - } - } - - if ((gender == 0) && (maleOffsetY != 0)) { - model.translate(0, maleOffsetY, 0); - } - - if ((gender == 1) && (femaleOffsetY != 0)) { - model.translate(0, femaleOffsetY, 0); - } - - if (srcColor != null) { - for (int i = 0; i < srcColor.length; i++) { - model.recolor(srcColor[i], dstColor[i]); - } - } - - return model; - } - - public void reset() { - modelID = 0; - name = null; - examine = null; - srcColor = null; - dstColor = null; - iconZoom = 2000; - iconPitch = 0; - iconYaw = 0; - iconRoll = 0; - iconOffsetX = 0; - iconOffsetY = 0; - unusedInt = -1; - stackable = false; - cost = 1; - members = false; - options = null; - inventoryOptions = null; - maleModelID0 = -1; - maleModelID1 = -1; - maleOffsetY = 0; - femaleModelID0 = -1; - femaleModelID1 = -1; - femaleOffsetY = 0; - maleModelID2 = -1; - femaleModelID2 = -1; - maleHeadModelID0 = -1; - maleHeadModelID1 = -1; - femaleHeadModelID0 = -1; - femaleHeadModelID1 = -1; - stackID = null; - stackCount = null; - linkedID = -1; - certificateID = -1; - scaleX = 128; - scaleZ = 128; - scaleY = 128; - lightAmbient = 0; - lightAttenuation = 0; - team = 0; - } - - public void toCertificate() { - ObjType cert = get(certificateID); - modelID = cert.modelID; - iconZoom = cert.iconZoom; - iconPitch = cert.iconPitch; - iconYaw = cert.iconYaw; - iconRoll = cert.iconRoll; - iconOffsetX = cert.iconOffsetX; - iconOffsetY = cert.iconOffsetY; - srcColor = cert.srcColor; - dstColor = cert.dstColor; - ObjType linked = get(linkedID); - name = linked.name; - members = linked.members; - cost = linked.cost; - - String s = "a"; - char c = linked.name.charAt(0); - - if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') || (c == 'U')) { - s = "an"; - } - - examine = ("Swap this note at any bank for " + s + " " + linked.name + ".").getBytes(); - stackable = true; - } - - /** - * Retrieves a fully built ground model of this {@link ObjType}. - * @param count the stack count. - * @return the model or null if unavailable. - */ - public Model getModel(int count) { - if ((stackID != null) && (count > 1)) { - int id = -1; - - for (int i = 0; i < 10; i++) { - if ((count >= stackCount[i]) && (stackCount[i] != 0)) { - id = stackID[i]; - } - } - - if (id != -1) { - return get(id).getModel(1); - } - } - - Model model = modelCache.get(id); - - if (model != null) { - return model; - } - - model = Model.tryGet(modelID); - - if (model == null) { - return null; - } - - if ((scaleX != 128) || (scaleZ != 128) || (scaleY != 128)) { - model.scale(scaleX, scaleY, scaleZ); - } - - if (srcColor != null) { - for (int i = 0; i < srcColor.length; i++) { - model.recolor(srcColor[i], dstColor[i]); - } - } - - model.calculateNormals(64 + lightAmbient, 768 + lightAttenuation, -50, -10, -50, true); - model.pickable = true; - modelCache.put(id, model); - return model; - } - - /** - * Retrieves the ground model of this {@link ObjType} without caching, rescaling, calculating normals, applying lighting, or making pickable. As opposed to {@link #getModel(int)}. - * @param count the stack count. - * @return the model or null if unavailable. - */ - public Model getInterfaceModel(int count) { - if ((stackID != null) && (count > 1)) { - int id = -1; - - for (int i = 0; i < 10; i++) { - if ((count >= stackCount[i]) && (stackCount[i] != 0)) { - id = stackID[i]; - } - } - - if (id != -1) { - return get(id).getInterfaceModel(1); - } - } - - Model model = Model.tryGet(modelID); - - if (model == null) { - return null; - } - - if (srcColor != null) { - for (int i = 0; i < srcColor.length; i++) { - model.recolor(srcColor[i], dstColor[i]); - } - } - return model; - } - - public void read(Buffer buffer) { - do { - int opcode = buffer.read8U(); - - if (opcode == 0) { - return; - } - - if (opcode == 1) { - modelID = buffer.read16U(); - } else if (opcode == 2) { - name = buffer.readString(); - } else if (opcode == 3) { - examine = buffer.readStringRaw(); - } else if (opcode == 4) { - iconZoom = buffer.read16U(); - } else if (opcode == 5) { - iconPitch = buffer.read16U(); - } else if (opcode == 6) { - iconYaw = buffer.read16U(); - } else if (opcode == 7) { - iconOffsetX = buffer.read16U(); - if (iconOffsetX > 32767) { - iconOffsetX -= 0x10000; - } - } else if (opcode == 8) { - iconOffsetY = buffer.read16U(); - if (iconOffsetY > 32767) { - iconOffsetY -= 0x10000; - } - } else if (opcode == 10) { - unusedInt = buffer.read16U(); - } else if (opcode == 11) { - stackable = true; - } else if (opcode == 12) { - cost = buffer.read32(); - } else if (opcode == 16) { - members = true; - } else if (opcode == 23) { - maleModelID0 = buffer.read16U(); - maleOffsetY = buffer.read(); - } else if (opcode == 24) { - maleModelID1 = buffer.read16U(); - } else if (opcode == 25) { - femaleModelID0 = buffer.read16U(); - femaleOffsetY = buffer.read(); - } else if (opcode == 26) { - femaleModelID1 = buffer.read16U(); - } else if ((opcode >= 30) && (opcode < 35)) { - if (options == null) { - options = new String[5]; - } - - options[opcode - 30] = buffer.readString(); - - if (options[opcode - 30].equalsIgnoreCase("hidden")) { - options[opcode - 30] = null; - } - } else if ((opcode >= 35) && (opcode < 40)) { - if (inventoryOptions == null) { - inventoryOptions = new String[5]; - } - inventoryOptions[opcode - 35] = buffer.readString(); - } else if (opcode == 40) { - int recolorCount = buffer.read8U(); - srcColor = new int[recolorCount]; - dstColor = new int[recolorCount]; - for (int i = 0; i < recolorCount; i++) { - srcColor[i] = buffer.read16U(); - dstColor[i] = buffer.read16U(); - } - } else if (opcode == 78) { - maleModelID2 = buffer.read16U(); - } else if (opcode == 79) { - femaleModelID2 = buffer.read16U(); - } else if (opcode == 90) { - maleHeadModelID0 = buffer.read16U(); - } else if (opcode == 91) { - femaleHeadModelID0 = buffer.read16U(); - } else if (opcode == 92) { - maleHeadModelID1 = buffer.read16U(); - } else if (opcode == 93) { - femaleHeadModelID1 = buffer.read16U(); - } else if (opcode == 95) { - iconRoll = buffer.read16U(); - } else if (opcode == 97) { - linkedID = buffer.read16U(); - } else if (opcode == 98) { - certificateID = buffer.read16U(); - } else if ((opcode >= 100) && (opcode < 110)) { - if (stackID == null) { - stackID = new int[10]; - stackCount = new int[10]; - } - stackID[opcode - 100] = buffer.read16U(); - stackCount[opcode - 100] = buffer.read16U(); - } else if (opcode == 110) { - scaleX = buffer.read16U(); - } else if (opcode == 111) { - scaleZ = buffer.read16U(); - } else if (opcode == 112) { - scaleY = buffer.read16U(); - } else if (opcode == 113) { - lightAmbient = buffer.read(); - } else if (opcode == 114) { - lightAttenuation = buffer.read() * 5; - } else if (opcode == 115) { - team = buffer.read8U(); - } - } while (true); - } + public static LRUMap iconCache = new LRUMap<>(100); + public static LRUMap modelCache = new LRUMap<>(50); + public static ObjType[] cached; + public static int cachePos; + public static Buffer dat; + public static int[] typeOffset; + public static int count; + + public static void unload() { + modelCache = null; + iconCache = null; + typeOffset = null; + cached = null; + dat = null; + } + + public static void unpack(FileArchive archive) throws IOException { + dat = new Buffer(archive.read("obj.dat")); + Buffer idx = new Buffer(archive.read("obj.idx")); + count = idx.read16U(); + typeOffset = new int[count]; + int offset = 2; + for (int j = 0; j < count; j++) { + typeOffset[j] = offset; + offset += idx.read16U(); + } + cached = new ObjType[10]; + for (int k = 0; k < 10; k++) { + cached[k] = new ObjType(); + } + } + + public static ObjType get(int id) { + for (int j = 0; j < 10; j++) { + if (cached[j].id == id) { + return cached[j]; + } + } + cachePos = (cachePos + 1) % 10; + ObjType type = cached[cachePos]; + dat.position = typeOffset[id]; + type.id = id; + type.reset(); + type.read(dat); + if (type.certificateID != -1) { + type.toCertificate(); + } + if (!Game.members && type.members) { + type.name = "Members Object"; + type.examine = "Login to a members' server to use this object.".getBytes(); + type.options = null; + type.inventoryOptions = null; + type.team = 0; + } + return type; + } + + public static Image24 getIcon(int id, int amount, int outlineColor) { + if (outlineColor == 0) { + Image24 icon = iconCache.get(id); + + if ((icon != null) && (icon.cropH != amount) && (icon.cropH != -1)) { + icon.unlink(); + icon = null; + } + + if (icon != null) { + return icon; + } + } + + ObjType type = get(id); + + if (type.stackID == null) { + amount = -1; + } + + if (amount > 1) { + int newID = -1; + for (int j1 = 0; j1 < 10; j1++) { + if ((amount >= type.stackCount[j1]) && (type.stackCount[j1] != 0)) { + newID = type.stackID[j1]; + } + } + if (newID != -1) { + type = get(newID); + } + } + + Model model = type.getModel(1); + + if (model == null) { + return null; + } + + // this will typically be the certificate item for noted stuff + Image24 originalIcon = null; + + if (type.certificateID != -1) { + originalIcon = getIcon(type.linkedID, 10, -1); + + if (originalIcon == null) { + return null; + } + } + + Image24 icon = new Image24(32, 32); + + // store state + int _cx = Draw3D.centerX; + int _cy = Draw3D.centerY; + int[] _loff = Draw3D.lineOffset; + int[] _pix = Draw2D.pixels; + int _w = Draw2D.width; + int _h = Draw2D.height; + int _l = Draw2D.left; + int _r = Draw2D.right; + int _t = Draw2D.top; + int _b = Draw2D.bottom; + + // set up drawing area + Draw3D.jagged = false; + Draw2D.bind(icon.pixels, 32, 32); + Draw2D.fillRect(0, 0, 32, 32, 0); + Draw3D.init2D(); + + int zoom = type.iconZoom; + + if (outlineColor == -1) { + zoom = (int) ((double) zoom * 1.5D); + } + + if (outlineColor > 0) { + zoom = (int) ((double) zoom * 1.04D); + } + + int sinPitch = (Draw3D.sin[type.iconPitch] * zoom) >> 16; + int cosPitch = (Draw3D.cos[type.iconPitch] * zoom) >> 16; + + model.drawSimple(0, type.iconYaw, type.iconRoll, type.iconPitch, type.iconOffsetX, sinPitch + (model.minY / 2) + type.iconOffsetY, cosPitch + type.iconOffsetY); + + // define outline + for (int x = 31; x >= 0; x--) { + for (int y = 31; y >= 0; y--) { + if (icon.pixels[x + (y * 32)] != 0) { + continue; + } + if ((x > 0) && (icon.pixels[(x - 1) + (y * 32)] > 1)) { + icon.pixels[x + (y * 32)] = 1; + } else if ((y > 0) && (icon.pixels[x + ((y - 1) * 32)] > 1)) { + icon.pixels[x + (y * 32)] = 1; + } else if ((x < 31) && (icon.pixels[x + 1 + (y * 32)] > 1)) { + icon.pixels[x + (y * 32)] = 1; + } else if ((y < 31) && (icon.pixels[x + ((y + 1) * 32)] > 1)) { + icon.pixels[x + (y * 32)] = 1; + } + } + } + + // color outline + if (outlineColor > 0) { + for (int x = 31; x >= 0; x--) { + for (int y = 31; y >= 0; y--) { + if (icon.pixels[x + (y * 32)] == 0) { + if ((x > 0) && (icon.pixels[(x - 1) + (y * 32)] == 1)) { + icon.pixels[x + (y * 32)] = outlineColor; + } else if ((y > 0) && (icon.pixels[x + ((y - 1) * 32)] == 1)) { + icon.pixels[x + (y * 32)] = outlineColor; + } else if ((x < 31) && (icon.pixels[x + 1 + (y * 32)] == 1)) { + icon.pixels[x + (y * 32)] = outlineColor; + } else if ((y < 31) && (icon.pixels[x + ((y + 1) * 32)] == 1)) { + icon.pixels[x + (y * 32)] = outlineColor; + } + } + } + } + } + // default outline color + else if (outlineColor == 0) { + for (int x = 31; x >= 0; x--) { + for (int y = 31; y >= 0; y--) { + if ((icon.pixels[x + (y * 32)] == 0) && (x > 0) && (y > 0) && (icon.pixels[(x - 1) + ((y - 1) * 32)] > 0)) { + icon.pixels[x + (y * 32)] = 0x302020; + } + } + } + } + + if (type.certificateID != -1) { + int w = originalIcon.cropW; + int h = originalIcon.cropH; + originalIcon.cropW = 32; + originalIcon.cropH = 32; + originalIcon.draw(0, 0); + originalIcon.cropW = w; + originalIcon.cropH = h; + } + + if (outlineColor == 0) { + iconCache.put(id, icon); + } + + // restore state + Draw2D.bind(_pix, _w, _h); + Draw2D.setBounds(_l, _t, _r, _b); + Draw3D.centerX = _cx; + Draw3D.centerY = _cy; + Draw3D.lineOffset = _loff; + Draw3D.jagged = true; + + if (type.stackable) { + icon.cropW = 33; + } else { + icon.cropW = 32; + } + + icon.cropH = amount; + return icon; + } + + public byte femaleOffsetY; + public int cost; + public int[] srcColor; + public int id = -1; + public int[] dstColor; + public boolean members; + public int femaleModelID2; + public int certificateID; + public int femaleModelID1; + public int maleModelID0; + public int maleHeadModelID1; + public int scaleX; + public String[] options; + public int iconOffsetX; + public String name; + public int femaleHeadModelID1; + public int modelID; + public int maleHeadModelID0; + public boolean stackable; + public byte[] examine; + public int linkedID; + public int iconZoom; + public int lightAttenuation; + public int maleModelID2; + public int maleModelID1; + public String[] inventoryOptions; + public int iconPitch; + public int scaleY; + public int scaleZ; + public int[] stackID; + public int iconOffsetY; + public int lightAmbient; + public int femaleHeadModelID0; + public int iconYaw; + public int femaleModelID0; + public int[] stackCount; + public int team; + public int iconRoll; + public byte maleOffsetY; + + public ObjType() { + } + + public boolean validateHeadModel(int gender) { + int modelID0 = maleHeadModelID0; + int modelID1 = maleHeadModelID1; + + if (gender == 1) { + modelID0 = femaleHeadModelID0; + modelID1 = femaleHeadModelID1; + } + + if (modelID0 == -1) { + return true; + } + + boolean valid = Model.validate(modelID0); + + if ((modelID1 != -1) && !Model.validate(modelID1)) { + valid = false; + } + return valid; + } + + public Model getHeadModel(int gender) { + int modelID0 = maleHeadModelID0; + int modelID1 = maleHeadModelID1; + + if (gender == 1) { + modelID0 = femaleHeadModelID0; + modelID1 = femaleHeadModelID1; + } + + if (modelID0 == -1) { + return null; + } + + Model model = Model.tryGet(modelID0); + + if (modelID1 != -1) { + model = new Model(2, new Model[]{model, Model.tryGet(modelID1)}); + } + + if (srcColor != null) { + for (int i = 0; i < srcColor.length; i++) { + model.recolor(srcColor[i], dstColor[i]); + } + } + return model; + } + + public boolean validateWornModel(int gender) { + int modelID0 = maleModelID0; + int modelID1 = maleModelID1; + int modelID2 = maleModelID2; + + if (gender == 1) { + modelID0 = femaleModelID0; + modelID1 = femaleModelID1; + modelID2 = femaleModelID2; + } + + if (modelID0 == -1) { + return true; + } + + boolean valid = Model.validate(modelID0); + + if ((modelID1 != -1) && !Model.validate(modelID1)) { + valid = false; + } + + if ((modelID2 != -1) && !Model.validate(modelID2)) { + valid = false; + } + + return valid; + } + + public Model getWornModel(int gender) { + int modelID0 = maleModelID0; + int modelID1 = maleModelID1; + int modelID2 = maleModelID2; + + if (gender == 1) { + modelID0 = femaleModelID0; + modelID1 = femaleModelID1; + modelID2 = femaleModelID2; + } + + if (modelID0 == -1) { + return null; + } + + Model model = Model.tryGet(modelID0); + + if (modelID1 != -1) { + if (modelID2 != -1) { + model = new Model(3, new Model[]{model, Model.tryGet(modelID1), Model.tryGet(modelID2)}); + } else { + model = new Model(2, new Model[]{model, Model.tryGet(modelID1)}); + } + } + + if ((gender == 0) && (maleOffsetY != 0)) { + model.translate(0, maleOffsetY, 0); + } + + if ((gender == 1) && (femaleOffsetY != 0)) { + model.translate(0, femaleOffsetY, 0); + } + + if (srcColor != null) { + for (int i = 0; i < srcColor.length; i++) { + model.recolor(srcColor[i], dstColor[i]); + } + } + + return model; + } + + public void reset() { + modelID = 0; + name = null; + examine = null; + srcColor = null; + dstColor = null; + iconZoom = 2000; + iconPitch = 0; + iconYaw = 0; + iconRoll = 0; + iconOffsetX = 0; + iconOffsetY = 0; + stackable = false; + cost = 1; + members = false; + options = null; + inventoryOptions = null; + maleModelID0 = -1; + maleModelID1 = -1; + maleOffsetY = 0; + femaleModelID0 = -1; + femaleModelID1 = -1; + femaleOffsetY = 0; + maleModelID2 = -1; + femaleModelID2 = -1; + maleHeadModelID0 = -1; + maleHeadModelID1 = -1; + femaleHeadModelID0 = -1; + femaleHeadModelID1 = -1; + stackID = null; + stackCount = null; + linkedID = -1; + certificateID = -1; + scaleX = 128; + scaleZ = 128; + scaleY = 128; + lightAmbient = 0; + lightAttenuation = 0; + team = 0; + } + + public void toCertificate() { + ObjType cert = get(certificateID); + modelID = cert.modelID; + iconZoom = cert.iconZoom; + iconPitch = cert.iconPitch; + iconYaw = cert.iconYaw; + iconRoll = cert.iconRoll; + iconOffsetX = cert.iconOffsetX; + iconOffsetY = cert.iconOffsetY; + srcColor = cert.srcColor; + dstColor = cert.dstColor; + ObjType linked = get(linkedID); + name = linked.name; + members = linked.members; + cost = linked.cost; + + String s = "a"; + char c = linked.name.charAt(0); + + if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') || (c == 'U')) { + s = "an"; + } + + examine = ("Swap this note at any bank for " + s + " " + linked.name + ".").getBytes(); + stackable = true; + } + + /** + * Retrieves a fully built ground model of this {@link ObjType}. + * + * @param count the stack count. + * @return the model or null if unavailable. + */ + public Model getModel(int count) { + if ((stackID != null) && (count > 1)) { + int id = -1; + + for (int i = 0; i < 10; i++) { + if ((count >= stackCount[i]) && (stackCount[i] != 0)) { + id = stackID[i]; + } + } + + if (id != -1) { + return get(id).getModel(1); + } + } + + Model model = modelCache.get(id); + + if (model != null) { + return model; + } + + model = Model.tryGet(modelID); + + if (model == null) { + return null; + } + + if ((scaleX != 128) || (scaleZ != 128) || (scaleY != 128)) { + model.scale(scaleX, scaleY, scaleZ); + } + + if (srcColor != null) { + for (int i = 0; i < srcColor.length; i++) { + model.recolor(srcColor[i], dstColor[i]); + } + } + + model.calculateNormals(64 + lightAmbient, 768 + lightAttenuation, -50, -10, -50, true); + model.pickable = true; + modelCache.put(id, model); + return model; + } + + /** + * Retrieves the ground model of this {@link ObjType} without caching, rescaling, calculating normals, applying lighting, or making pickable. As opposed to {@link #getModel(int)}. + * + * @param count the stack count. + * @return the model or null if unavailable. + */ + public Model getInterfaceModel(int count) { + if ((stackID != null) && (count > 1)) { + int id = -1; + + for (int i = 0; i < 10; i++) { + if ((count >= stackCount[i]) && (stackCount[i] != 0)) { + id = stackID[i]; + } + } + + if (id != -1) { + return get(id).getInterfaceModel(1); + } + } + + Model model = Model.tryGet(modelID); + + if (model == null) { + return null; + } + + if (srcColor != null) { + for (int i = 0; i < srcColor.length; i++) { + model.recolor(srcColor[i], dstColor[i]); + } + } + return model; + } + + public void read(Buffer buffer) { + while (true) { + int code = buffer.read8U(); + + if (code == 0) { + return; + } else if (code == 1) { + modelID = buffer.read16U(); + } else if (code == 2) { + name = buffer.readString(); + } else if (code == 3) { + examine = buffer.readStringRaw(); + } else if (code == 4) { + iconZoom = buffer.read16U(); + } else if (code == 5) { + iconPitch = buffer.read16U(); + } else if (code == 6) { + iconYaw = buffer.read16U(); + } else if (code == 7) { + iconOffsetX = buffer.read16U(); + if (iconOffsetX > 32767) { + iconOffsetX -= 0x10000; + } + } else if (code == 8) { + iconOffsetY = buffer.read16U(); + if (iconOffsetY > 32767) { + iconOffsetY -= 0x10000; + } + } else if (code == 10) { + buffer.read16U(); + } else if (code == 11) { + stackable = true; + } else if (code == 12) { + cost = buffer.read32(); + } else if (code == 16) { + members = true; + } else if (code == 23) { + maleModelID0 = buffer.read16U(); + maleOffsetY = buffer.read(); + } else if (code == 24) { + maleModelID1 = buffer.read16U(); + } else if (code == 25) { + femaleModelID0 = buffer.read16U(); + femaleOffsetY = buffer.read(); + } else if (code == 26) { + femaleModelID1 = buffer.read16U(); + } else if ((code >= 30) && (code < 35)) { + if (options == null) { + options = new String[5]; + } + options[code - 30] = buffer.readString(); + if (options[code - 30].equalsIgnoreCase("hidden")) { + options[code - 30] = null; + } + } else if ((code >= 35) && (code < 40)) { + if (inventoryOptions == null) { + inventoryOptions = new String[5]; + } + inventoryOptions[code - 35] = buffer.readString(); + } else if (code == 40) { + int recolorCount = buffer.read8U(); + srcColor = new int[recolorCount]; + dstColor = new int[recolorCount]; + for (int i = 0; i < recolorCount; i++) { + srcColor[i] = buffer.read16U(); + dstColor[i] = buffer.read16U(); + } + } else if (code == 78) { + maleModelID2 = buffer.read16U(); + } else if (code == 79) { + femaleModelID2 = buffer.read16U(); + } else if (code == 90) { + maleHeadModelID0 = buffer.read16U(); + } else if (code == 91) { + femaleHeadModelID0 = buffer.read16U(); + } else if (code == 92) { + maleHeadModelID1 = buffer.read16U(); + } else if (code == 93) { + femaleHeadModelID1 = buffer.read16U(); + } else if (code == 95) { + iconRoll = buffer.read16U(); + } else if (code == 97) { + linkedID = buffer.read16U(); + } else if (code == 98) { + certificateID = buffer.read16U(); + } else if ((code >= 100) && (code < 110)) { + if (stackID == null) { + stackID = new int[10]; + stackCount = new int[10]; + } + stackID[code - 100] = buffer.read16U(); + stackCount[code - 100] = buffer.read16U(); + } else if (code == 110) { + scaleX = buffer.read16U(); + } else if (code == 111) { + scaleZ = buffer.read16U(); + } else if (code == 112) { + scaleY = buffer.read16U(); + } else if (code == 113) { + lightAmbient = buffer.read(); + } else if (code == 114) { + lightAttenuation = buffer.read() * 5; + } else if (code == 115) { + team = buffer.read8U(); + } + } + } } diff --git a/src/main/java/SeqType.java b/src/main/java/SeqType.java index fe883fb..be7e787 100644 --- a/src/main/java/SeqType.java +++ b/src/main/java/SeqType.java @@ -107,7 +107,6 @@ public class SeqType { */ public int replayStyle = 1; - public int unusedInt; public SeqType() { } @@ -130,11 +129,12 @@ public class SeqType { } public void load(Buffer buffer) { - do { - int opcode = buffer.read8U(); - if (opcode == 0) { + while (true) { + int code = buffer.read8U(); + + if (code == 0) { break; - } else if (opcode == 1) { + } else if (code == 1) { frameCount = buffer.read8U(); transformIDs = new int[frameCount]; auxiliaryTransformIDs = new int[frameCount]; @@ -147,37 +147,37 @@ public class SeqType { } frameDuration[f] = buffer.read16U(); } - } else if (opcode == 2) { + } else if (code == 2) { loopFrameCount = buffer.read16U(); - } else if (opcode == 3) { + } else if (code == 3) { int count = buffer.read8U(); mask = new int[count + 1]; for (int l = 0; l < count; l++) { mask[l] = buffer.read8U(); } mask[count] = 9999999; - } else if (opcode == 4) { + } else if (code == 4) { forwardRenderPadding = true; - } else if (opcode == 5) { + } else if (code == 5) { priority = buffer.read8U(); - } else if (opcode == 6) { + } else if (code == 6) { rightHandOverride = buffer.read16U(); - } else if (opcode == 7) { + } else if (code == 7) { leftHandOverride = buffer.read16U(); - } else if (opcode == 8) { + } else if (code == 8) { loopCount = buffer.read8U(); - } else if (opcode == 9) { + } else if (code == 9) { moveStyle = buffer.read8U(); - } else if (opcode == 10) { + } else if (code == 10) { idleStyle = buffer.read8U(); - } else if (opcode == 11) { + } else if (code == 11) { replayStyle = buffer.read8U(); - } else if (opcode == 12) { - unusedInt = buffer.read32(); + } else if (code == 12) { + buffer.read32(); } else { - System.out.println("Error unrecognised seq config code: " + opcode); + System.out.println("Error unrecognised seq config code: " + code); } - } while (true); + } if (frameCount == 0) { frameCount = 1; @@ -200,9 +200,9 @@ public class SeqType { if (idleStyle == -1) { if (mask != null) { idleStyle = 2; - return; + } else { + idleStyle = 0; } - idleStyle = 0; } } diff --git a/src/main/java/SoundTrack.java b/src/main/java/SoundTrack.java index 4d7f061..429b006 100644 --- a/src/main/java/SoundTrack.java +++ b/src/main/java/SoundTrack.java @@ -2,23 +2,21 @@ public class SoundTrack { public static final SoundTrack[] tracks = new SoundTrack[5000]; public static final int[] delays = new int[5000]; - public static byte[] bbuf; - public static Buffer buffer; + public static byte[] waveBytes = new byte[441000]; + public static Buffer waveBuffer = new Buffer(waveBytes); - public static void method240(Buffer src) { - bbuf = new byte[441000]; - - SoundTrack.buffer = new Buffer(bbuf); + public static void unpack(Buffer in) { SoundTone.init(); do { - int id = src.read16U(); + int id = in.read16U(); + if (id == 65535) { return; } tracks[id] = new SoundTrack(); - tracks[id].read(src); + tracks[id].read(in); delays[id] = tracks[id].trim(); } while (true); } @@ -26,7 +24,7 @@ public class SoundTrack { public static Buffer generate(int loopCount, int id) { if (tracks[id] != null) { SoundTrack track = tracks[id]; - return track.getWaveform(loopCount); + return track.getWave(loopCount); } else { return null; } @@ -85,24 +83,24 @@ public class SoundTrack { * @param loopCount the loop count. * @return the {@link Buffer} containing the waveform data. */ - public Buffer getWaveform(int loopCount) { + public Buffer getWave(int loopCount) { int length = generate(loopCount); - buffer.position = 0; - buffer.write32(0x52494646); // "RIFF" ChunkID - buffer.write32LE(36 + length); // ChunkSize - buffer.write32(0x57415645); // "WAVE" format - buffer.write32(0x666d7420); // "fmt " chunk id - buffer.write32LE(16); // chunk size - buffer.write16LE(1); // audio format - buffer.write16LE(1); // num channels - buffer.write32LE(22050); // sample rate - buffer.write32LE(22050); // byte rate - buffer.write16LE(1); // block align - buffer.write16LE(8); // bits per sample - buffer.write32(0x64617461); // "data" - buffer.write32LE(length); - buffer.position += length; - return buffer; + waveBuffer.position = 0; + waveBuffer.write32(0x52494646); // "RIFF" ChunkID + waveBuffer.write32LE(36 + length); // ChunkSize + waveBuffer.write32(0x57415645); // "WAVE" format + waveBuffer.write32(0x666d7420); // "fmt " chunk id + waveBuffer.write32LE(16); // chunk size + waveBuffer.write16LE(1); // audio format + waveBuffer.write16LE(1); // num channels + waveBuffer.write32LE(22050); // sample rate + waveBuffer.write32LE(22050); // byte rate + waveBuffer.write16LE(1); // block align + waveBuffer.write16LE(8); // bits per sample + waveBuffer.write32(0x64617461); // "data" + waveBuffer.write32LE(length); + waveBuffer.position += length; + return waveBuffer; } public int generate(int loopCount) { @@ -129,7 +127,7 @@ public class SoundTrack { int totalSampleCount = sampleCount + ((loopStop - loopStart) * (loopCount - 1)); for (int sample = 44; sample < (totalSampleCount + 44); sample++) { - bbuf[sample] = -128; + waveBytes[sample] = -128; } for (int tone = 0; tone < 10; tone++) { @@ -142,7 +140,7 @@ public class SoundTrack { int[] samples = tones[tone].generate(toneSampleCount, tones[tone].length); for (int sample = 0; sample < toneSampleCount; sample++) { - bbuf[sample + start + 44] += (byte) (samples[sample] >> 8); + waveBytes[sample + start + 44] += (byte) (samples[sample] >> 8); } } @@ -155,14 +153,14 @@ public class SoundTrack { // Moves the end of the sound (after the loops) to the true end of the buffer. int endOffset = (totalSampleCount += 44) - sampleCount; for (int sample = sampleCount - 1; sample >= loopStop; sample--) { - bbuf[sample + endOffset] = bbuf[sample]; + waveBytes[sample + endOffset] = waveBytes[sample]; } // Duplicates loop area. for (int loop = 1; loop < loopCount; loop++) { int offset = (loopStop - loopStart) * loop; for (int sample = loopStart; sample < loopStop; sample++) { - bbuf[sample + offset] = bbuf[sample]; + waveBytes[sample + offset] = waveBytes[sample]; } } diff --git a/src/main/java/SpotAnimType.java b/src/main/java/SpotAnimType.java index 17b7413..9b6e4b4 100644 --- a/src/main/java/SpotAnimType.java +++ b/src/main/java/SpotAnimType.java @@ -8,96 +8,95 @@ import java.io.IOException; public class SpotAnimType { - public static int count; - public static SpotAnimType[] instances; - public static LRUMap modelCache = new LRUMap<>(30); + public static int count; + public static SpotAnimType[] instances; + public static LRUMap modelCache = new LRUMap<>(30); - public static void unpack(FileArchive archive) throws IOException { - Buffer buffer = new Buffer(archive.read("spotanim.dat")); - count = buffer.read16U(); + public static void unpack(FileArchive archive) throws IOException { + Buffer buffer = new Buffer(archive.read("spotanim.dat")); + count = buffer.read16U(); - if (instances == null) { - instances = new SpotAnimType[count]; - } + if (instances == null) { + instances = new SpotAnimType[count]; + } - for (int i = 0; i < count; i++) { - if (instances[i] == null) { - instances[i] = new SpotAnimType(); - } - instances[i].index = i; - instances[i].read(buffer); - } - } + for (int i = 0; i < count; i++) { + if (instances[i] == null) { + instances[i] = new SpotAnimType(); + } + instances[i].index = i; + instances[i].read(buffer); + } + } - public final int[] colorSrc = new int[6]; - public final int[] colorDst = new int[6]; - public int index; - public int modelID; - public int seqID = -1; - public SeqType seq; - public int scaleXY = 128; - public int scaleZ = 128; - public int rotation; - public int lightAmbient; - public int lightAttenuation; + public final int[] colorSrc = new int[6]; + public final int[] colorDst = new int[6]; + public int index; + public int modelID; + public int seqID = -1; + public SeqType seq; + public int scaleXY = 128; + public int scaleZ = 128; + public int rotation; + public int lightAmbient; + public int lightAttenuation; - public SpotAnimType() { - } + public SpotAnimType() { + } - public void read(Buffer buffer) { - do { - int i = buffer.read8U(); - if (i == 0) { - return; - } - if (i == 1) { - modelID = buffer.read16U(); - } else if (i == 2) { - seqID = buffer.read16U(); - if (SeqType.instances != null) { - seq = SeqType.instances[seqID]; - } - } else if (i == 4) { - scaleXY = buffer.read16U(); - } else if (i == 5) { - scaleZ = buffer.read16U(); - } else if (i == 6) { - rotation = buffer.read16U(); - } else if (i == 7) { - lightAmbient = buffer.read8U(); - } else if (i == 8) { - lightAttenuation = buffer.read8U(); - } else if ((i >= 40) && (i < 50)) { - colorSrc[i - 40] = buffer.read16U(); - } else if ((i >= 50) && (i < 60)) { - colorDst[i - 50] = buffer.read16U(); - } else { - System.out.println("Error unrecognised spotanim config code: " + i); - } - } while (true); - } + public void read(Buffer buffer) { + while (true) { + int code = buffer.read8U(); + if (code == 0) { + return; + } else if (code == 1) { + modelID = buffer.read16U(); + } else if (code == 2) { + seqID = buffer.read16U(); + if (SeqType.instances != null) { + seq = SeqType.instances[seqID]; + } + } else if (code == 4) { + scaleXY = buffer.read16U(); + } else if (code == 5) { + scaleZ = buffer.read16U(); + } else if (code == 6) { + rotation = buffer.read16U(); + } else if (code == 7) { + lightAmbient = buffer.read8U(); + } else if (code == 8) { + lightAttenuation = buffer.read8U(); + } else if ((code >= 40) && (code < 50)) { + colorSrc[code - 40] = buffer.read16U(); + } else if ((code >= 50) && (code < 60)) { + colorDst[code - 50] = buffer.read16U(); + } else { + System.out.println("Error unrecognised spotanim config code: " + code); + } + } + } - public Model getModel() { - Model model = modelCache.get(index); + public Model getModel() { + Model model = modelCache.get(index); - if (model != null) { - return model; - } + if (model != null) { + return model; + } - model = Model.tryGet(modelID); + model = Model.tryGet(modelID); - if (model == null) { - return null; - } + if (model == null) { + return null; + } - for (int i = 0; i < 6; i++) { - if (colorSrc[0] != 0) { - model.recolor(colorSrc[i], colorDst[i]); - } - } + for (int i = 0; i < 6; i++) { + if (colorSrc[0] != 0) { + model.recolor(colorSrc[i], colorDst[i]); + } + } - modelCache.put(index, model); - return model; - } + modelCache.put(index, model); + return model; + } } diff --git a/src/main/java/VarbitType.java b/src/main/java/VarbitType.java index fa59a66..1dffefc 100644 --- a/src/main/java/VarbitType.java +++ b/src/main/java/VarbitType.java @@ -21,16 +21,12 @@ public class VarbitType { instances[j] = new VarbitType(); } instances[j].read(buffer); - if (instances[j].unusedBool) { - VarpType.instances[instances[j].varp].unusedBool3 = true; - } } if (buffer.position != buffer.data.length) { System.out.println("varbit load mismatch"); } } - public String unusedString; public int varp; /** * The least significant bit. @@ -40,32 +36,25 @@ public class VarbitType { * The most significant bit. */ public int msb; - public boolean unusedBool = false; - public int unusedInt0 = -1; - public int unusedInt1; public VarbitType() { } public void read(Buffer buffer) { do { - int opcode = buffer.read8U(); - if (opcode == 0) { + int code = buffer.read8U(); + if (code == 0) { return; - } else if (opcode == 1) { + } else if (code == 1) { varp = buffer.read16U(); lsb = buffer.read8U(); msb = buffer.read8U(); - } else if (opcode == 10) { - unusedString = buffer.readString(); - } else if (opcode == 2) { - unusedBool = true; - } else if (opcode == 3) { - unusedInt0 = buffer.read32(); - } else if (opcode == 4) { - unusedInt1 = buffer.read32(); + } else if (code == 10) { + buffer.readString(); + } else if (code == 3 || code == 4) { + buffer.read32(); } else { - System.out.println("Error unrecognised config code: " + opcode); + System.out.println("Error unrecognised varbit config code: " + code); } } while (true); } diff --git a/src/main/java/VarpType.java b/src/main/java/VarpType.java index a18287f..cdac3d2 100644 --- a/src/main/java/VarpType.java +++ b/src/main/java/VarpType.java @@ -6,82 +6,48 @@ import java.io.IOException; public class VarpType { - public static int anInt700; + public static int count; public static VarpType[] instances; - public static int anInt702; - public static int[] anIntArray703; public static void unpack(FileArchive archive) throws IOException { Buffer buffer = new Buffer(archive.read("varp.dat")); - anInt702 = 0; - anInt700 = buffer.read16U(); + count = buffer.read16U(); if (instances == null) { - instances = new VarpType[anInt700]; + instances = new VarpType[count]; } - if (anIntArray703 == null) { - anIntArray703 = new int[anInt700]; - } - for (int j = 0; j < anInt700; j++) { - if (instances[j] == null) { - instances[j] = new VarpType(); + for (int i = 0; i < count; i++) { + if (instances[i] == null) { + instances[i] = new VarpType(); } - instances[j].read(buffer, j); + instances[i].read(buffer); } if (buffer.position != buffer.data.length) { System.out.println("varptype load mismatch"); } } - public String unusedString; - public int unusedInt0; - public int unusedInt1; - public boolean unusedBool0 = false; - public boolean unusedBool1 = true; public int type; - public boolean unusedBool2 = false; - public int unusedInt2; - public int unusedInt3; - public boolean unusedBool3 = false; - public int unusedInt4 = -1; public VarpType() { } - public void read(Buffer buffer, int i) { - do { - int opcode = buffer.read8U(); - if (opcode == 0) { + public void read(Buffer in) { + while (true) { + int code = in.read8U(); + if (code == 0) { return; - } else if (opcode == 1) { - unusedInt0 = buffer.read8U(); - } else if (opcode == 2) { - unusedInt1 = buffer.read8U(); - } else if (opcode == 3) { - unusedBool0 = true; - anIntArray703[anInt702++] = i; - } else if (opcode == 4) { - unusedBool1 = false; - } else if (opcode == 5) { - type = buffer.read16U(); - } else if (opcode == 6) { - unusedBool2 = true; - } else if (opcode == 7) { - unusedInt2 = buffer.read32(); - } else if (opcode == 8) { - unusedInt3 = 1; - unusedBool3 = true; - } else if (opcode == 10) { - unusedString = buffer.readString(); - } else if (opcode == 11) { - unusedBool3 = true; - } else if (opcode == 12) { - unusedInt4 = buffer.read32(); - } else if (opcode == 13) { - unusedInt3 = 2; + } else if (code == 1 || code == 2) { + in.read8U(); + } else if (code == 5) { + type = in.read16U(); + } else if (code == 7 || code == 12) { + in.read32(); + } else if (code == 10) { + in.readString(); } else { - System.out.println("Error unrecognised config code: " + opcode); + System.out.println("Error unrecognised varp config code: " + code); } - } while (true); + } } }