Skip to content

Commit

Permalink
More optimization & bug fixes (#55)
Browse files Browse the repository at this point in the history
* remove old i18n & fix saving

* save about 20k allocations/sec and just return the same empty ore/fluid instance

* fix bartworks ores missing some contained materials

* more stable chunk analysis

* spotlessApply

* verify that min y level matches for fast scan

* chat formatting is already added elsewhere

* update

* remove double jm dep

* use arrayset for ores to keep ordering consistent for getContainedOres

* more early returns for detailed analysis

* reasonable aliases for these long ass commands

* update

* fix primary ore being added to contained ores multiple times after nei search

* bimap is not needed or used

* update

---------

Co-authored-by: GitHub GTNH Actions <>
Co-authored-by: Martin Robertz <dream-master@gmx.net>
  • Loading branch information
Lyfts and Dream-Master authored Sep 20, 2024
1 parent 7dbc66d commit 0c3ace2
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 165 deletions.
3 changes: 1 addition & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
dependencies {
api('com.github.GTNewHorizons:Navigator:1.0.12:dev')
api('com.github.GTNewHorizons:GTNHLib:0.5.11:dev')
api('com.github.GTNewHorizons:GT5-Unofficial:5.09.49.105:dev')
api('com.github.GTNewHorizons:GT5-Unofficial:5.09.50.01:dev')

runtimeOnlyNonPublishable(rfg.deobf('maven.modrinth:journeymap:5.2.5'))
runtimeOnlyNonPublishable(rfg.deobf('maven.modrinth:journeymap:5.2.6'))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;
Expand Down Expand Up @@ -39,7 +38,7 @@ private void notifyNewOreVein(OreVeinPosition oreVeinPosition) {
final String location = "(" + (oreVeinPosition.getBlockX() + 8) + "," + (oreVeinPosition.getBlockZ() + 8) + ")";
final IChatComponent veinNotification = new ChatComponentTranslation(
"visualprospecting.vein.prospected",
I18n.format(oreVeinPosition.veinType.name),
oreVeinPosition.veinType.getPrimaryOreName(),
location);
veinNotification.getChatStyle().setItalic(true);
veinNotification.getChatStyle().setColor(EnumChatFormatting.GRAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;

public class DimensionCache {

Expand All @@ -31,8 +29,6 @@ public enum UpdateResult {

private final Long2ObjectMap<OreVeinPosition> oreChunks = new Long2ObjectOpenHashMap<>();
private final Long2ObjectMap<UndergroundFluidPosition> undergroundFluids = new Long2ObjectOpenHashMap<>();
private final LongSet changedOrNewOreChunks = new LongOpenHashSet();
private final LongSet changedOrNewUndergroundFluids = new LongOpenHashSet();
public final int dimensionId;
private boolean isDirty = false;

Expand All @@ -51,32 +47,30 @@ public NBTTagCompound saveToNbt() {

private NBTTagCompound saveOres() {
NBTTagCompound compound = new NBTTagCompound();
for (long key : changedOrNewOreChunks) {
OreVeinPosition oreVeinPosition = oreChunks.get(key);
for (OreVeinPosition vein : oreChunks.values()) {
NBTTagCompound veinCompound = new NBTTagCompound();
veinCompound.setInteger("chunkX", oreVeinPosition.chunkX);
veinCompound.setInteger("chunkZ", oreVeinPosition.chunkZ);
veinCompound.setShort("veinTypeId", VeinTypeCaching.getVeinTypeId(oreVeinPosition.veinType));
veinCompound.setBoolean("depleted", oreVeinPosition.isDepleted());
compound.setTag(String.valueOf(key), veinCompound);
veinCompound.setInteger("chunkX", vein.chunkX);
veinCompound.setInteger("chunkZ", vein.chunkZ);
veinCompound.setShort("veinTypeId", vein.veinType.veinId);
veinCompound.setBoolean("depleted", vein.isDepleted());
compound.setTag(String.valueOf(getOreVeinKey(vein.chunkX, vein.chunkZ)), veinCompound);
}
return compound;
}

private NBTTagCompound saveFluids() {
NBTTagCompound compound = new NBTTagCompound();
for (long key : changedOrNewUndergroundFluids) {
UndergroundFluidPosition undergroundFluidPosition = undergroundFluids.get(key);
for (UndergroundFluidPosition fluid : undergroundFluids.values()) {
NBTTagCompound fluidCompound = new NBTTagCompound();
fluidCompound.setInteger("chunkX", undergroundFluidPosition.chunkX);
fluidCompound.setInteger("chunkZ", undergroundFluidPosition.chunkZ);
fluidCompound.setString("fluidName", undergroundFluidPosition.fluid.getName());
fluidCompound.setInteger("chunkX", fluid.chunkX);
fluidCompound.setInteger("chunkZ", fluid.chunkZ);
fluidCompound.setString("fluidName", fluid.fluid.getName());
NBTTagList chunkList = new NBTTagList();
for (int i = 0; i < VP.undergroundFluidSizeChunkX; i++) {
chunkList.appendTag(new NBTTagIntArray(undergroundFluidPosition.chunks[i]));
chunkList.appendTag(new NBTTagIntArray(fluid.chunks[i]));
}
fluidCompound.setTag("chunks", chunkList);
compound.setTag(String.valueOf(key), fluidCompound);
compound.setTag(String.valueOf(getUndergroundFluid(fluid.chunkX, fluid.chunkZ)), fluidCompound);
}
return compound;
}
Expand Down Expand Up @@ -125,7 +119,6 @@ void loadLegacy(ByteBuffer oreChunksBuffer, ByteBuffer undergroundFluidsBuffer)
oreChunks.put(
getOreVeinKey(chunkX, chunkZ),
new OreVeinPosition(dimensionId, chunkX, chunkZ, veinType, depleted));
changedOrNewOreChunks.add(getOreVeinKey(chunkX, chunkZ));
}
}
if (undergroundFluidsBuffer != null) {
Expand Down Expand Up @@ -153,7 +146,6 @@ void loadLegacy(ByteBuffer oreChunksBuffer, ByteBuffer undergroundFluidsBuffer)
undergroundFluids.put(
getUndergroundFluidKey(chunkX, chunkZ),
new UndergroundFluidPosition(dimensionId, chunkX, chunkZ, fluid, chunks));
changedOrNewUndergroundFluids.add(getUndergroundFluidKey(chunkX, chunkZ));
}
}
}
Expand All @@ -174,7 +166,6 @@ public void toggleOreVein(int chunkX, int chunkZ) {
final OreVeinPosition oreVeinPosition = oreChunks.get(key);
if (oreVeinPosition != null) {
oreVeinPosition.toggleDepleted();
changedOrNewOreChunks.add(key);
markDirty();
}
}
Expand All @@ -184,13 +175,11 @@ public UpdateResult putOreVein(final OreVeinPosition oreVeinPosition) {
final OreVeinPosition storedOreVeinPosition = oreChunks.get(key);
if (storedOreVeinPosition == null) {
oreChunks.put(key, oreVeinPosition);
changedOrNewOreChunks.add(key);
markDirty();
return UpdateResult.New;
}
if (storedOreVeinPosition.veinType != oreVeinPosition.veinType) {
oreChunks.put(key, oreVeinPosition.joinDepletedState(storedOreVeinPosition));
changedOrNewOreChunks.add(key);
markDirty();
return UpdateResult.New;
}
Expand All @@ -201,12 +190,10 @@ public UpdateResult putUndergroundFluid(final UndergroundFluidPosition undergrou
final long key = getUndergroundFluidKey(undergroundFluid.chunkX, undergroundFluid.chunkZ);
final UndergroundFluidPosition storedUndergroundFluid = undergroundFluids.get(key);
if (storedUndergroundFluid == null) {
changedOrNewUndergroundFluids.add(key);
undergroundFluids.put(key, undergroundFluid);
markDirty();
return UpdateResult.New;
} else if (!storedUndergroundFluid.equals(undergroundFluid)) {
changedOrNewUndergroundFluids.add(key);
undergroundFluids.put(key, undergroundFluid);
markDirty();
return UpdateResult.Updated;
Expand All @@ -216,13 +203,12 @@ public UpdateResult putUndergroundFluid(final UndergroundFluidPosition undergrou

public OreVeinPosition getOreVein(int chunkX, int chunkZ) {
final long key = getOreVeinKey(chunkX, chunkZ);
return oreChunks.getOrDefault(key, new OreVeinPosition(dimensionId, chunkX, chunkZ, VeinType.NO_VEIN, true));
return oreChunks.getOrDefault(key, OreVeinPosition.EMPTY_VEIN);
}

public UndergroundFluidPosition getUndergroundFluid(int chunkX, int chunkZ) {
final long key = getUndergroundFluidKey(chunkX, chunkZ);
return undergroundFluids
.getOrDefault(key, UndergroundFluidPosition.getNotProspected(dimensionId, chunkX, chunkZ));
return undergroundFluids.getOrDefault(key, UndergroundFluidPosition.NOT_PROSPECTED);
}

public Collection<OreVeinPosition> getAllOreVeins() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class OreVeinPosition {

public static final int MAX_BYTES = 3 * Integer.BYTES + Short.BYTES;
public static final OreVeinPosition EMPTY_VEIN = new OreVeinPosition(0, 0, 0, VeinType.NO_VEIN, true);

public final int dimensionId;
public final int chunkX;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sinthoras.visualprospecting.database;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.zip.DataFormatException;

Expand Down Expand Up @@ -29,6 +31,11 @@ public String getCommandUsage(ICommandSender sender) {
return StatCollector.translateToLocal("visualprospecting.redoservercache.command");
}

@Override
public List<String> getCommandAliases() {
return Collections.singletonList("vp_recache");
}

@Override
public void processCommand(ICommandSender sender, String[] parameters) {
MinecraftServer server;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sinthoras.visualprospecting.database;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.zip.DataFormatException;

Expand Down Expand Up @@ -29,6 +31,11 @@ public String getCommandUsage(ICommandSender sender) {
return StatCollector.translateToLocal("visualprospecting.redoserverspawncache.command");
}

@Override
public List<String> getCommandAliases() {
return Collections.singletonList("vp_recachespawn");
}

@Override
public void processCommand(ICommandSender sender, String[] parameters) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ public class UndergroundFluidPosition {

public static final int BYTES = (3 + 1 + VP.undergroundFluidSizeChunkX * VP.undergroundFluidSizeChunkZ)
* Integer.BYTES;
public static final UndergroundFluidPosition NOT_PROSPECTED = new UndergroundFluidPosition(0, 0, 0, null, null);

public final int dimensionId;
public final int chunkX;
public final int chunkZ;
public final Fluid fluid;
public final int[][] chunks;

public static UndergroundFluidPosition getNotProspected(int dimensionId, int chunkX, int chunkZ) {
return new UndergroundFluidPosition(dimensionId, chunkX, chunkZ, null, null);
}

public UndergroundFluidPosition(int dimensionId, int chunkX, int chunkZ, Fluid fluid, int[][] chunks) {
this.dimensionId = dimensionId;
this.chunkX = Utils.mapToCornerUndergroundFluidChunkCoord(chunkX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import com.sinthoras.visualprospecting.Tags;
import com.sinthoras.visualprospecting.Utils;
import com.sinthoras.visualprospecting.database.veintypes.VeinType;

public abstract class WorldCache {

Expand All @@ -31,8 +30,9 @@ public boolean loadVeinCache(String worldId) {
if (loadLegacyVeinCache(worldCache)) return true;

final File[] dimensionFiles = worldCache.listFiles();
if (dimensionFiles == null) return false;
if (dimensionFiles == null || dimensionFiles.length == 0) return false;

boolean loadedAny = false;
for (File dimensionFile : dimensionFiles) {
final String fileName = dimensionFile.getName();
if (!dimensionFile.isFile() || !fileName.endsWith(".dat")) {
Expand All @@ -46,9 +46,10 @@ public boolean loadVeinCache(String worldId) {
final DimensionCache dimension = new DimensionCache(dimensionId);
dimension.loadFromNbt(dimCompound);
dimensions.put(dimensionId, dimension);
loadedAny = true;
}

return true;
return loadedAny;
}

private boolean loadLegacyVeinCache(File worldCacheDirectory) {
Expand Down Expand Up @@ -167,7 +168,7 @@ protected void toggleOreVein(int dimensionId, int chunkX, int chunkZ) {
public OreVeinPosition getOreVein(int dimensionId, int chunkX, int chunkZ) {
DimensionCache dimension = dimensions.get(dimensionId);
if (dimension == null) {
return new OreVeinPosition(dimensionId, chunkX, chunkZ, VeinType.NO_VEIN, true);
return OreVeinPosition.EMPTY_VEIN;
}
return dimension.getOreVein(chunkX, chunkZ);
}
Expand All @@ -184,7 +185,7 @@ protected DimensionCache.UpdateResult putUndergroundFluids(final UndergroundFlui
public UndergroundFluidPosition getUndergroundFluid(int dimensionId, int chunkX, int chunkZ) {
DimensionCache dimension = dimensions.get(dimensionId);
if (dimension == null) {
return UndergroundFluidPosition.getNotProspected(dimensionId, chunkX, chunkZ);
return UndergroundFluidPosition.NOT_PROSPECTED;
}
return dimension.getUndergroundFluid(chunkX, chunkZ);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ public class ChunkAnalysis {
private final ObjectSet<VeinType> matchedVeins = new ObjectOpenHashSet<>();
private final Short2IntMap oreCounts = new Short2IntArrayMap();
private int minVeinBlockY = VP.minecraftWorldHeight;
private short primaryMeta, secondaryMeta;
private short primaryMeta;
private final String dimName;

public ChunkAnalysis(String dimName) {
this.dimName = dimName;
}

public void processMinecraftChunk(final NBTTagList tileEntities) {
if (tileEntities == null || tileEntities.tagCount() == 0) return;
for (int i = 0; i < tileEntities.tagCount(); i++) {
final NBTTagCompound tile = tileEntities.getCompoundTagAt(i);
if (tile == null || !tile.hasKey("m")) continue;

if (!"GT_TileEntity_Ores".equals(tile.getString("id"))) {
String id = tile.getString("id");
if (!"GT_TileEntity_Ores".equals(id) && !"bw.blockoresTE".equals(id)) {
continue;
}

Expand All @@ -46,35 +52,20 @@ public void processMinecraftChunk(final NBTTagList tileEntities) {
}

if (oreCounts.size() == 1) {
primaryMeta = secondaryMeta = oreCounts.keySet().iterator().nextShort();
primaryMeta = oreCounts.keySet().iterator().nextShort();
} else if (oreCounts.size() > 1) {
ShortList metaCounts = new ShortArrayList(oreCounts.keySet());
metaCounts.sort((a, b) -> Integer.compare(oreCounts.get(b), oreCounts.get(a)));
primaryMeta = metaCounts.getShort(0);
secondaryMeta = metaCounts.getShort(1);
}
}

public boolean matchesSingleVein() {
if (oreCounts.isEmpty()) return true;
if (oreCounts.size() > 4) return false;
ObjectSet<VeinType> veins = VeinTypeCaching.getVeinTypesForPrimaryMeta(primaryMeta);
if (veins.isEmpty()) return trySecondaryMeta();

veins.stream().filter(veinType -> veinType.matches(oreCounts.keySet())).forEach(matchedVeins::add);

if (matchedVeins.size() != 1) {
matchedVeins.clear();
return trySecondaryMeta();
}

return true;
}

private boolean trySecondaryMeta() {
if (secondaryMeta == primaryMeta) return false;
VeinTypeCaching.getVeinTypesForPrimaryMeta(secondaryMeta).stream()
.filter(veinType -> veinType.matches(oreCounts.keySet())).forEach(matchedVeins::add);
VeinTypeCaching.veinTypes.stream()
.filter(vein -> vein.containsAllFoundOres(oreCounts.keySet(), dimName, primaryMeta, minVeinBlockY))
.forEach(matchedVeins::add);
return matchedVeins.size() <= 1;
}

Expand Down
Loading

0 comments on commit 0c3ace2

Please sign in to comment.