Skip to content

Commit

Permalink
Fix air calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Meeples10 committed Dec 14, 2023
1 parent d521695 commit 79d73a9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>io.github.meeples10.mcresourceanalyzer</groupId>
<artifactId>mc-resource-analyzer</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
<packaging>jar</packaging>

<name>MCResourceAnalyzer</name>
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public boolean accept(File dir, String name) {
static RegionAnalyzer.Version selectedVersion = RegionAnalyzer.Version.values()[0];
static File inputFile = new File("region");
static boolean saveStatistics = false;
static boolean allowHack = true;
static boolean generateTable = false;
static boolean modernizeIDs = false;
static boolean silent = false;
Expand All @@ -62,8 +61,7 @@ public static void main(String[] args) {
} catch(IOException e) {
e.printStackTrace();
}
Main.println("Save statistics: " + saveStatistics + "\nAllow empty section hack: " + allowHack
+ "\nGenerate HTML table: " + generateTable
Main.println("Save statistics: " + saveStatistics + "\nGenerate HTML table: " + generateTable
+ (generateTable
? ("\nTable template: " + (tableTemplatePath.equals("") ? "(none)" : tableTemplatePath))
: "")
Expand All @@ -81,8 +79,8 @@ public static void main(String[] args) {
}
if(analyzer == null) analyzer = new RegionAnalyzerAnvil118();
analyzer.setVersion(selectedVersion);
if(inputFile.isDirectory() != selectedVersion.usesDirectory()) {
System.err.println("Input must be a " + (selectedVersion.usesDirectory() ? "directory" : "file") + ": "
if(inputFile.isDirectory() != selectedVersion.usesDirectory) {
System.err.println("Input must be a " + (selectedVersion.usesDirectory ? "directory" : "file") + ": "
+ inputFile.getAbsolutePath());
System.exit(1);
}
Expand Down Expand Up @@ -127,10 +125,6 @@ public String[] getVersion() throws Exception {
.description("When analyzing a world with block IDs outside the range of 0-255, any block with an"
+ "ID listed in this file will have all of its variants merged into a single value..")
.build());
spec.addOption(OptionSpec.builder("-H", "--no-hack").description(
"The program attempts to compensate for inaccuracies at high Y values by assuming that empty chunk"
+ "sections are filled with air. Use this option to disable this hack.")
.build());
spec.addOption(OptionSpec.builder("-n", "--num-threads").paramLabel("COUNT").type(int.class)
.description("The maximum number of threads to use for analysis. (default: 8)").build());
spec.addPositional(PositionalParamSpec.builder().paramLabel("INPUT").arity("0..1").type(String.class)
Expand All @@ -141,7 +135,6 @@ public String[] getVersion() throws Exception {
private static int parseArgs(ParseResult pr) {
if(pr.hasMatchedPositional(0)) inputFile = new File((String) pr.matchedPositional(0).getValue());
saveStatistics = pr.hasMatchedOption('s');
allowHack = !pr.hasMatchedOption('H');
generateTable = pr.hasMatchedOption('t');
modernizeIDs = pr.hasMatchedOption('m');
silent = pr.hasMatchedOption('S');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,12 @@ static boolean mergeStates(byte id) {
return mergeStates((int) id);
}

/* THIS IS A HACK TO ACCOUNT FOR MISSING SECTIONS AT HIGH Y VALUES */
static void airHack(Analysis a, int sectionY, String airID) {
if(Main.allowHack && sectionY < 15) {
if(!a.heights.containsKey(airID)) a.heights.put(airID, new Hashtable<Integer, Long>());
for(; sectionY < 16; sectionY++) {
a.blocks.put(airID, a.blocks.getOrDefault(airID, 0L) + 4096L);
for(int y = sectionY * 16; y < sectionY * 16 + 16; y++) {
a.heights.get(airID).put(y, a.heights.get(airID).getOrDefault(y, 0L) + 256L);
}
if(!a.heights.containsKey(airID)) a.heights.put(airID, new Hashtable<Integer, Long>());
for(; sectionY < 16; sectionY++) {
a.blocks.put(airID, a.blocks.getOrDefault(airID, 0L) + 4096L);
for(int y = sectionY * 16; y < sectionY * 16 + 16; y++) {
a.heights.get(airID).put(y, a.heights.get(airID).getOrDefault(y, 0L) + 256L);
}
}
}
Expand Down Expand Up @@ -304,8 +301,8 @@ public enum Version {
ALPHA(RegionAnalyzerAlpha.class, true),
INDEV(RegionAnalyzerIndev.class, false);

public final boolean usesDirectory;
private final Class<? extends RegionAnalyzer> analyzerClass;
private final boolean usesDirectory;

private Version(Class<? extends RegionAnalyzer> analyzerClass, boolean usesDirectory) {
this.analyzerClass = analyzerClass;
Expand All @@ -315,9 +312,5 @@ private Version(Class<? extends RegionAnalyzer> analyzerClass, boolean usesDirec
public RegionAnalyzer getAnalyzerInstance() throws Exception {
return analyzerClass.getDeclaredConstructor().newInstance();
}

public boolean usesDirectory() {
return usesDirectory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ private Analysis analyzeChunk(NBTTagList sections) {
for(; i < sections.tagCount(); i++) {
NBTTagCompound section = sections.getCompoundTagAt(i);
NBTTagList palette = section.getCompoundTag("block_states").getTagList("palette", 10);
if(palette.hasNoTags()) continue;
if(palette.hasNoTags()) {
airHack(a, i, "minecraft:air");
continue;
}
NBTTagLongArray blockStatesTag = ((NBTTagLongArray) section.getCompoundTag("block_states").getTag("data"));
int bitLength = Main.bitLength(palette.tagCount() - 1);
int[] blocks = Main.unstream(bitLength < 4 ? 4 : bitLength, 64, true,
blockStatesTag == null ? new long[0] : blockStatesTag.get());
if(blocks.length == 0) continue;
if(blocks.length == 0) {
airHack(a, i, "minecraft:air");
continue;
}
int sectionY = section.getByte("Y");

for(int y = 0; y < 16; y++) {
Expand All @@ -99,7 +105,6 @@ private Analysis analyzeChunk(NBTTagList sections) {
}
}
}
airHack(a, i, "minecraft:air");
return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ private Analysis analyzeChunk(NBTTagList sections) {
NBTTagCompound tag = sections.getCompoundTagAt(i);
int sectionY = tag.getByte("Y");
byte[] blocks = tag.getByteArray("Blocks");
if(blocks.length == 0) {
airHack(a, i, "0");
continue;
}
byte[] rawData = tag.getByteArray("Data");
byte[] data = new byte[rawData.length * 2];
int j = 0;
Expand Down Expand Up @@ -112,7 +116,6 @@ private Analysis analyzeChunk(NBTTagList sections) {
}
}
}
airHack(a, i, "0");
return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public void findChunks(File regionDir) {

@Override
public void analyze() {
// The air hack does not seem to work for this version
Main.allowHack = false;
for(Region r : regions) {
threads.add(new AnalyzerThread(this, r) {
public void process() {
Expand Down Expand Up @@ -72,11 +70,18 @@ private Analysis analyzeChunk(NBTTagList sections) {
for(; i < sections.tagCount(); i++) {
NBTTagCompound section = sections.getCompoundTagAt(i);
NBTTagList palette = section.getTagList("Palette", 10);
if(palette.hasNoTags()) continue;
if(palette.hasNoTags()) {
airHack(a, i, "minecraft:air");
continue;
}
NBTTagLongArray blockStatesTag = ((NBTTagLongArray) section.getTag("BlockStates"));
int bitLength = Main.bitLength(palette.tagCount() - 1);
int[] blocks = Main.unstream(bitLength < 4 ? 4 : bitLength, 64, false,
blockStatesTag == null ? new long[0] : blockStatesTag.get());
if(blocks.length == 0) {
airHack(a, i, "minecraft:air");
continue;
}
int sectionY = section.getByte("Y");

for(int y = 0; y < 16; y++) {
Expand All @@ -101,7 +106,6 @@ private Analysis analyzeChunk(NBTTagList sections) {
}
}
}
airHack(a, i, "minecraft:air");
return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void process() {
private Analysis processRegion(RegionFile r, int x, int z) throws IOException {
DataInputStream chunkDataInputStream = r.getChunkDataInputStream(x, z);
if(chunkDataInputStream == null) return null;
return analyzeChunk(CompressedStreamTools.read(r.getChunkDataInputStream(x, z)).getCompoundTag("Level")
.getTagList("Sections", 10));
return analyzeChunk(
CompressedStreamTools.read(chunkDataInputStream).getCompoundTag("Level").getTagList("Sections", 10));
}

private Analysis analyzeChunk(NBTTagList sections) {
Expand All @@ -70,11 +70,18 @@ private Analysis analyzeChunk(NBTTagList sections) {
for(; i < sections.tagCount(); i++) {
NBTTagCompound section = sections.getCompoundTagAt(i);
NBTTagList palette = section.getTagList("Palette", 10);
if(palette.hasNoTags()) continue;
if(palette.hasNoTags()) {
airHack(a, i, "minecraft:air");
continue;
}
NBTTagLongArray blockStatesTag = ((NBTTagLongArray) section.getTag("BlockStates"));
int bitLength = Main.bitLength(palette.tagCount() - 1);
int[] blocks = Main.unstream(bitLength < 4 ? 4 : bitLength, 64, true,
blockStatesTag == null ? new long[0] : blockStatesTag.get());
if(blocks.length == 0) {
airHack(a, i, "minecraft:air");
continue;
}
int sectionY = section.getByte("Y");

for(int y = 0; y < 16; y++) {
Expand All @@ -99,7 +106,6 @@ private Analysis analyzeChunk(NBTTagList sections) {
}
}
}
airHack(a, i, "minecraft:air");
return a;
}
}

0 comments on commit 79d73a9

Please sign in to comment.