Skip to content

Commit

Permalink
Improved chunk generation speed, fix oitsjustjose#305, fix floating p…
Browse files Browse the repository at this point in the history
…oint error bug
  • Loading branch information
kyragit committed Mar 15, 2023
1 parent 77c933a commit 9ff21d5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Random;

public class DepositUtils {
private static HashSet<BlockState> defaultMatchersCached = null;
Expand All @@ -28,13 +27,6 @@ public class DepositUtils {
*/
@Nullable
public static BlockState pick(HashMap<BlockState, Float> map, float totl, RandomSource random) {
if (totl == 1.0F) {
totl = 0;
for (Entry<BlockState, Float> e : map.entrySet()) {
totl += e.getValue();
}
}

float rng = random.nextFloat();
for (Entry<BlockState, Float> e : map.entrySet()) {
float wt = e.getValue();
Expand Down Expand Up @@ -72,4 +64,12 @@ public static boolean addDefaultMatcher(Block block) {
}
return false;
}

/**
* Returns true if a and b are within epsilon of each other, where epsilon is the minimum
* representable value by a 32-bit floating point number.
*/
public static boolean nearlyEquals(float a, float b) {
return Math.abs(a - b) <= Float.MIN_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public DenseDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashM
float v = this.cumulOreWtMap.get(i.getKey());
this.cumulOreWtMap.put(i.getKey(), v + j.getValue());
}

if (this.cumulOreWtMap.get(i.getKey()) != 1.0F) {
if (!DepositUtils.nearlyEquals(this.cumulOreWtMap.get(i.getKey()), 1.0F)) {
throw new RuntimeException("Sum of weights for pluton blocks should equal 1.0");
}
}
Expand All @@ -81,7 +81,7 @@ public DenseDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashM
this.sumWtSamples += e.getValue();
}

if (sumWtSamples != 1.0F) {
if (!DepositUtils.nearlyEquals(sumWtSamples, 1.0F)) {
throw new RuntimeException("Sum of weights for pluton samples should equal 1.0");
}
}
Expand All @@ -97,13 +97,9 @@ public DenseDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashM
*/
@Nullable
public BlockState getOre(BlockState currentState, RandomSource rand) {
String res = Utils.getRegistryName(currentState);
if (this.oreToWtMap.containsKey(res)) {
// Return a choice from a specialized set here
HashMap<BlockState, Float> mp = this.oreToWtMap.get(res);
return DepositUtils.pick(mp, this.cumulOreWtMap.get(res), rand);
}
return DepositUtils.pick(this.oreToWtMap.get("default"), this.cumulOreWtMap.get("default"), rand);
String res = this.oreToWtMap.containsKey(Utils.getRegistryName(currentState)) ? Utils.getRegistryName(currentState) : "default";
// Return a choice from a specialized set here
return DepositUtils.pick(this.oreToWtMap.get(res), this.cumulOreWtMap.get(res), rand);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DikeDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashMa
this.cumulOreWtMap.put(i.getKey(), v + j.getValue());
}

if (this.cumulOreWtMap.get(i.getKey()) != 1.0F) {
if (!DepositUtils.nearlyEquals(this.cumulOreWtMap.get(i.getKey()), 1.0F)) {
throw new RuntimeException("Sum of weights for pluton blocks should equal 1.0");
}
}
Expand All @@ -81,7 +81,7 @@ public DikeDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashMa
this.sumWtSamples += e.getValue();
}

if (sumWtSamples != 1.0F) {
if (!DepositUtils.nearlyEquals(sumWtSamples, 1.0F)) {
throw new RuntimeException("Sum of weights for pluton samples should equal 1.0");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public LayerDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashM
this.cumulOreWtMap.put(i.getKey(), v + j.getValue());
}

if (this.cumulOreWtMap.get(i.getKey()) != 1.0F) {
if (!DepositUtils.nearlyEquals(this.cumulOreWtMap.get(i.getKey()), 1.0F)) {
throw new RuntimeException("Sum of weights for pluton blocks should equal 1.0");
}
}
Expand All @@ -83,7 +83,7 @@ public LayerDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, HashM
this.sumWtSamples += e.getValue();
}

if (sumWtSamples != 1.0F) {
if (!DepositUtils.nearlyEquals(sumWtSamples, 1.0F)) {
throw new RuntimeException("Sum of weights for pluton samples should equal 1.0");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public SparseDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, Hash
this.cumulOreWtMap.put(i.getKey(), v + j.getValue());
}

if (this.cumulOreWtMap.get(i.getKey()) != 1.0F) {
if (!DepositUtils.nearlyEquals(this.cumulOreWtMap.get(i.getKey()), 1.0F)) {
throw new RuntimeException("Sum of weights for pluton blocks should equal 1.0");
}
}
Expand All @@ -84,7 +84,7 @@ public SparseDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, Hash
this.sumWtSamples += e.getValue();
}

if (sumWtSamples != 1.0F) {
if (!DepositUtils.nearlyEquals(sumWtSamples, 1.0F)) {
throw new RuntimeException("Sum of weights for pluton samples should equal 1.0");
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ public void afterGen(WorldGenLevel level, BlockPos pos, IDepositCapability depos
}

ChunkPos thisChunk = new ChunkPos(pos);
int maxSampleCnt = (Math.min(CommonConfig.MAX_SAMPLES_PER_CHUNK.get(), (this.size / CommonConfig.MAX_SAMPLES_PER_CHUNK.get()) + (this.size % CommonConfig.MAX_SAMPLES_PER_CHUNK.get()))) * (spread / 16);
int maxSampleCnt = (int)((float)Math.min(CommonConfig.MAX_SAMPLES_PER_CHUNK.get(), (this.size / CommonConfig.MAX_SAMPLES_PER_CHUNK.get()) + (this.size % CommonConfig.MAX_SAMPLES_PER_CHUNK.get())) * ((float)spread / 16.0F));

for (int i = 0; i < maxSampleCnt; i++) {
BlockState tmp = this.getSample(level.getRandom());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public TopLayerDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, Ha
this.cumulOreWtMap.put(i.getKey(), v + j.getValue());
}

if (this.cumulOreWtMap.get(i.getKey()) != 1.0F) {
if (!DepositUtils.nearlyEquals(this.cumulOreWtMap.get(i.getKey()), 1.0F)) {
throw new RuntimeException("Sum of weights for pluton blocks should equal 1.0");
}
}
Expand All @@ -79,7 +79,7 @@ public TopLayerDeposit(HashMap<String, HashMap<BlockState, Float>> oreBlocks, Ha
this.sumWtSamples += e.getValue();
}

if (sumWtSamples != 1.0F) {
if (!DepositUtils.nearlyEquals(sumWtSamples, 1.0F)) {
throw new RuntimeException("Sum of weights for pluton samples should equal 1.0");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.oitsjustjose.geolosys.capability.world;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentHashMap;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand All @@ -10,13 +10,13 @@
import net.minecraftforge.common.capabilities.CapabilityToken;

public class ChunkGennedCapability implements IChunkGennedCapability {
private final ConcurrentLinkedQueue<ChunkPos> generatedChunks;
private final ConcurrentHashMap.KeySetView<ChunkPos, Boolean> generatedChunks;

public static final Capability<IChunkGennedCapability> CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {
});

public ChunkGennedCapability() {
this.generatedChunks = new ConcurrentLinkedQueue<>();
this.generatedChunks = ConcurrentHashMap.newKeySet();
}

@Override
Expand Down

0 comments on commit 9ff21d5

Please sign in to comment.