Skip to content

Commit

Permalink
fix performance and clean
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Oct 1, 2023
1 parent d8b8ffc commit 274d0f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package hunternif.mc.impl.atlas.core.scanning;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import dev.architectury.injectables.annotations.ExpectPlatform;
import hunternif.mc.impl.atlas.AntiqueAtlasMod;
import hunternif.mc.impl.atlas.core.TileIdMap;
Expand All @@ -17,11 +20,6 @@
import net.minecraft.world.biome.BiomeKeys;
import net.minecraft.world.chunk.Chunk;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
* Detects the 256 vanilla biomes, water pools and lava pools.
* Water and beach biomes are given priority because shore line is the defining
Expand Down Expand Up @@ -62,23 +60,6 @@ static int priorityForBiome(RegistryEntry<Biome> biomeTag) {
}
}

/* these are the values used by vanilla, but it just doesn't work for me.
protected static TileHeightType getHeightType(double weirdness) {
if (weirdness < (double) VanillaTerrainParameters.getNormalizedWeirdness(0.05f)) {
return TileHeightType.VALLEY;
}
if (weirdness < (double) VanillaTerrainParameters.getNormalizedWeirdness(0.26666668f)) {
return TileHeightType.LOW;
}
if (weirdness < (double) VanillaTerrainParameters.getNormalizedWeirdness(0.4f)) {
return TileHeightType.MID;
}
if (weirdness < (double) VanillaTerrainParameters.getNormalizedWeirdness(0.56666666f)) {
return TileHeightType.HIGH;
}
return TileHeightType.PEAK;
} */

protected static TileHeightType getHeightTypeFromY(int y, int sealevel) {
if (y < sealevel + 10) {
return TileHeightType.VALLEY;
Expand All @@ -100,18 +81,14 @@ protected static Identifier getBiomeIdentifier(World world, Biome biome) {
return world.getRegistryManager().get(Registry.BIOME_KEY).getId(biome);
}

protected static void updateOccurrencesMap(Map<Identifier, Integer> map, Identifier biome, int weight) {
int occurrence = map.getOrDefault(biome, 0) + weight;
map.put(biome, occurrence);
protected static void updateOccurrencesMap(Multiset<Identifier> map, Identifier biome, int weight) {
map.add(biome, weight);
}

protected static void updateOccurrencesMap(Map<Identifier, Integer> map, World world, Biome biome, TileHeightType type, int weight) {
protected static void updateOccurrencesMap(Multiset<Identifier> map, World world, Biome biome, TileHeightType type, int weight) {
Identifier id = getBiomeIdentifier(world, biome);
id = Identifier.tryParse(id.toString() + "_" + type.getName());


int occurrence = map.getOrDefault(id, 0) + weight;
map.put(id, occurrence);
id = new Identifier(id.getNamespace(), id.getPath() + "_" + type.getName());
map.add(id, weight);
}

@Override
Expand All @@ -126,7 +103,7 @@ public int getScanRadius() {
*/
@Override
public Identifier getBiomeID(World world, Chunk chunk) {
Map<Identifier, Integer> biomeOccurrences = new HashMap<>(BuiltinRegistries.BIOME.getIds().size());
Multiset<Identifier> biomeOccurrences = HashMultiset.create(BuiltinRegistries.BIOME.getIds().size());
Registry<Biome> biomeRegistry = world.getRegistryManager().get(Registry.BIOME_KEY);

for (int x = 0; x < 16; x++) {
Expand All @@ -135,20 +112,9 @@ public Identifier getBiomeID(World world, Chunk chunk) {
Biome biome = chunk.getBiomeForNoiseGen(x, world.getSeaLevel(), z).value();
RegistryEntry<Biome> biomeTag = biomeRegistry.entryOf(biomeRegistry.getKey(biome).orElse(null));


// get top block
int y = chunk.getHeightmap(Heightmap.Type.MOTION_BLOCKING).get(x, z);


//this code runs on the server
// ServerChunkManager man = (ServerChunkManager) world.getChunkManager();
// MultiNoiseUtil.MultiNoiseSampler sampler = man.getChunkGenerator().getMultiNoiseSampler();
// ChunkPos pos = chunk.getPos();
// MultiNoiseUtil.NoiseValuePoint sample = sampler.sample(pos.getStartX() + x, y + 10, pos.getStartZ() + z);

// float m = MultiNoiseUtil.method_38666(sample.weirdnessNoise());
// double weirdness = VanillaTerrainParameters.getNormalizedWeirdness(m);

if (AntiqueAtlasMod.CONFIG.doScanPonds) {
if (y > 0) {
Block topBlock = chunk.getBlockState(new BlockPos(x, y - 1, z)).getBlock();
Expand All @@ -172,14 +138,11 @@ public Identifier getBiomeID(World world, Chunk chunk) {
}
}

// updateOccurrencesMap(biomeOccurrences, world, biome, getHeightType(weirdness), priorityForBiome(getBiomeIdentifier(world, biome)));
updateOccurrencesMap(biomeOccurrences, world, biome, getHeightTypeFromY(y, world.getSeaLevel()), priorityForBiome(biomeTag));
}
}

if (biomeOccurrences.isEmpty()) return null;

Map.Entry<Identifier, Integer> meanBiome = Collections.max(biomeOccurrences.entrySet(), Comparator.comparingInt(Map.Entry::getValue));
return meanBiome.getKey();
return biomeOccurrences.entrySet().stream().max(Ordering.natural().onResultOf(Multiset.Entry::getCount)).orElseThrow().getElement();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package hunternif.mc.impl.atlas.core.scanning;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.Identifier;
Expand All @@ -11,11 +14,6 @@
import net.minecraft.world.biome.BiomeKeys;
import net.minecraft.world.chunk.Chunk;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
* Detects seas of lava, cave ground and cave walls in the Nether.
*
Expand All @@ -25,7 +23,7 @@ public class TileDetectorEnd extends TileDetectorBase implements ITileDetector {

@Override
public Identifier getBiomeID(World world, Chunk chunk) {
Map<Identifier, Integer> biomeOccurrences = new HashMap<>(BuiltinRegistries.BIOME.getIds().size());
Multiset<Identifier> biomeOccurrences = HashMultiset.create(BuiltinRegistries.BIOME.getIds().size());

for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
Expand Down Expand Up @@ -53,11 +51,7 @@ public Identifier getBiomeID(World world, Chunk chunk) {
}
}

if (biomeOccurrences.isEmpty())
return null;

Map.Entry<Identifier, Integer> meanBiome = Collections.max(biomeOccurrences.entrySet(), Comparator
.comparingInt(Map.Entry::getValue));
return meanBiome.getKey();
if (biomeOccurrences.isEmpty()) return null;
return biomeOccurrences.entrySet().stream().max(Ordering.natural().onResultOf(Multiset.Entry::getCount)).orElseThrow().getElement();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package hunternif.mc.impl.atlas.core.scanning;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import hunternif.mc.impl.atlas.core.TileIdMap;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
Expand All @@ -14,11 +17,6 @@
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
* Detects seas of lava, cave ground and cave walls in the Nether.
*
Expand All @@ -42,7 +40,7 @@ public class TileDetectorNether extends TileDetectorBase implements ITileDetecto

@Override
public Identifier getBiomeID(World world, Chunk chunk) {
Map<Identifier, Integer> biomeOccurrences = new HashMap<>(BuiltinRegistries.BIOME.getIds().size());
Multiset<Identifier> biomeOccurrences = HashMultiset.create(BuiltinRegistries.BIOME.getIds().size());
Registry<Biome> biomeRegistry = world.getRegistryManager().get(Registry.BIOME_KEY);

for (int x = 0; x < 16; x++) {
Expand Down Expand Up @@ -71,11 +69,7 @@ public Identifier getBiomeID(World world, Chunk chunk) {
}

if (biomeOccurrences.isEmpty()) return null;

Map.Entry<Identifier, Integer> meanBiome = Collections.max(biomeOccurrences.entrySet(), Comparator
.comparingInt(Map.Entry::getValue));

return meanBiome.getKey();
return biomeOccurrences.entrySet().stream().max(Ordering.natural().onResultOf(Multiset.Entry::getCount)).orElseThrow().getElement();
}

@Override
Expand Down

0 comments on commit 274d0f0

Please sign in to comment.