From b4cca8024333ab2b9c5ae0d972411e9c5b90860b Mon Sep 17 00:00:00 2001 From: dags- Date: Wed, 1 Jul 2020 19:42:31 +0100 Subject: [PATCH] add extra bounds checks just in case --- .../mod/biome/map/SimpleBiomeMap.java | 22 ++++++++++++++----- .../mod/biome/map/set/BiomeSet.java | 11 +++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/terraforged/mod/biome/map/SimpleBiomeMap.java b/src/main/java/com/terraforged/mod/biome/map/SimpleBiomeMap.java index d6b4106..8c7fe81 100644 --- a/src/main/java/com/terraforged/mod/biome/map/SimpleBiomeMap.java +++ b/src/main/java/com/terraforged/mod/biome/map/SimpleBiomeMap.java @@ -80,13 +80,25 @@ public class SimpleBiomeMap implements BiomeMap { @Override public Biome getCoast(Cell cell) { - int inland = land.getSize(cell); + // treat land & coast biome-sets as one combined set + Biome[] inland = land.getSet(cell); Biome[] coastal = coast.getSet(cell); - int total = inland + coastal.length; - int index = NoiseUtil.round((total - 1) * cell.biome); - if (index >= inland) { - return coastal[index - inland]; + + // calculate where in the combined set the cell.biome points + int maxIndex = inland.length + coastal.length - 1; + int index = NoiseUtil.round(maxIndex * cell.biome); + + // if index lies within the coast section of the set + if (index >= inland.length) { + // relativize the index to start at 0 + index -= inland.length; + + // shouldn't be required but check that index is within bounds + if (index < coastal.length) { + return coastal[index]; + } } + return DefaultBiomes.NONE; } diff --git a/src/main/java/com/terraforged/mod/biome/map/set/BiomeSet.java b/src/main/java/com/terraforged/mod/biome/map/set/BiomeSet.java index 340bf6c..64324f2 100644 --- a/src/main/java/com/terraforged/mod/biome/map/set/BiomeSet.java +++ b/src/main/java/com/terraforged/mod/biome/map/set/BiomeSet.java @@ -47,7 +47,16 @@ public abstract class BiomeSet { if (set.length == 0) { return defaultBiome.getDefaultBiome(cell.temperature); } - return set[NoiseUtil.round((set.length - 1) * cell.biome)]; + + int maxIndex = set.length - 1; + int index = NoiseUtil.round(maxIndex * cell.biome); + + // shouldn't happen but safety check the bounds + if (index < 0 || index >= set.length) { + return defaultBiome.getDefaultBiome(cell.temperature); + } + + return set[index]; } public abstract int getIndex(Cell cell);