added selector that helps with selecting which biome to generate.

Untested.
This commit is contained in:
Harrison Deng 2020-04-19 20:20:16 -05:00
parent 23f761c36f
commit 3c41444dfa

View File

@ -0,0 +1,187 @@
package ca.recrown.islandsurvivalcraft.worldgen;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.Map.Entry;
import org.bukkit.block.Biome;
import ca.recrown.islandsurvivalcraft.Utilities;
public class BiomeSelector {
private boolean initialized = false;
private final Random random;
private final HashMap<Biome, Float> lands;
private HashMap<Float, ArrayList<Biome>> temperaturesForLand;
private final HashMap<Float, ArrayList<Biome>> temperaturePartitionedLandBiomes;
private final HashMap<Biome, Float> oceans;
private HashMap<Float, ArrayList<Biome>> temperaturesForOcean;
private final HashMap<Float, ArrayList<Biome>> temperaturePartitionedOceanBiomes;
public BiomeSelector(Random random) {
this.random = random;
lands = new HashMap<>();
oceans = new HashMap<>();
temperaturePartitionedLandBiomes = new HashMap<>();
temperaturePartitionedLandBiomes.put(0.05f, new ArrayList<>());
temperaturePartitionedLandBiomes.put(0.30f, new ArrayList<>());
temperaturePartitionedLandBiomes.put(0.95f, new ArrayList<>());
temperaturePartitionedLandBiomes.put(2f, new ArrayList<>());
temperaturePartitionedOceanBiomes = new HashMap<>();
temperaturePartitionedOceanBiomes.put(0.00f, new ArrayList<>());
temperaturePartitionedOceanBiomes.put(0.5f, new ArrayList<>());
}
public void initialize() {
if (initialized) throw new IllegalStateException("Biome selector is already initialized.");
initialized = true;
registerLandBiomeTemperatures();
registerOceanBiomeTemperatures();
}
private void registerLandBiomeTemperatures() {
lands.put(Biome.SNOWY_TUNDRA, 0.0f);
lands.put(Biome.ICE_SPIKES, 0.0f);
lands.put(Biome.SNOWY_TAIGA, -0.5f);
lands.put(Biome.SNOWY_TAIGA_MOUNTAINS, -0.5f);
lands.put(Biome.MOUNTAINS, 0.2f);
lands.put(Biome.GRAVELLY_MOUNTAINS, 0.2f);
lands.put(Biome.WOODED_MOUNTAINS, 0.2f);
lands.put(Biome.MODIFIED_GRAVELLY_MOUNTAINS, 0.2f);
lands.put(Biome.TAIGA, 0.25f);
lands.put(Biome.TAIGA_MOUNTAINS, 0.25f);
lands.put(Biome.GIANT_TREE_TAIGA, 0.3f);
lands.put(Biome.PLAINS, 0.8f);
lands.put(Biome.SUNFLOWER_PLAINS, 0.8f);
lands.put(Biome.FOREST, 0.7f);
lands.put(Biome.FLOWER_FOREST, 0.7f);
lands.put(Biome.BIRCH_FOREST, 0.6f);
lands.put(Biome.TALL_BIRCH_FOREST, 0.7f);
lands.put(Biome.DARK_FOREST, 0.7f);
lands.put(Biome.SWAMP, 0.8f);
lands.put(Biome.JUNGLE, 0.95f);
lands.put(Biome.MODIFIED_JUNGLE, 0.95f);
lands.put(Biome.BAMBOO_JUNGLE, 0.95f);
lands.put(Biome.MUSHROOM_FIELDS, 0.9f);
lands.put(Biome.DESERT, 2.0f);
lands.put(Biome.SAVANNA, 1.2f);
lands.put(Biome.SHATTERED_SAVANNA, 1.1f);
lands.put(Biome.BADLANDS, 2.0f);
lands.put(Biome.ERODED_BADLANDS, 2.0f);
lands.put(Biome.WOODED_BADLANDS_PLATEAU, 2.0f);
lands.put(Biome.MODIFIED_WOODED_BADLANDS_PLATEAU, 2.0f);
lands.put(Biome.SAVANNA_PLATEAU, 1.0f);
lands.put(Biome.BADLANDS_PLATEAU, 2.0f);
lands.put(Biome.MODIFIED_BADLANDS_PLATEAU, 2.0f);
lands.put(Biome.SHATTERED_SAVANNA_PLATEAU, 1.0f);
temperaturesForLand = Utilities.invertHashMap(lands);
for (Entry<Float, ArrayList<Biome>> entry : temperaturesForLand.entrySet()) {
if (entry.getKey() <= 0.05f) {
temperaturePartitionedLandBiomes.get(0.05f).addAll(entry.getValue());
} else if (entry.getKey() <= 0.3) {
temperaturePartitionedLandBiomes.get(0.3f).addAll(entry.getValue());
} else if (entry.getKey() <= 0.85f) {
temperaturePartitionedLandBiomes.get(0.95f).addAll(entry.getValue());
} else {
temperaturePartitionedLandBiomes.get(2.00f).addAll(entry.getValue());
}
}
}
private void registerOceanBiomeTemperatures() {
oceans.put(Biome.WARM_OCEAN, 0.5f);
oceans.put(Biome.LUKEWARM_OCEAN, 0.5f);
oceans.put(Biome.DEEP_LUKEWARM_OCEAN, 0.5f);
oceans.put(Biome.OCEAN, 0.5f);
oceans.put(Biome.DEEP_OCEAN, 0.5f);
oceans.put(Biome.COLD_OCEAN, 0.5f);
oceans.put(Biome.DEEP_COLD_OCEAN, 0.5f);
oceans.put(Biome.FROZEN_OCEAN, 0f);
oceans.put(Biome.DEEP_FROZEN_OCEAN, 0.5f);
temperaturesForOcean = Utilities.invertHashMap(oceans);
for (Entry<Float, ArrayList<Biome>> entry : temperaturesForOcean.entrySet()) {
if (entry.getKey() <= 0.00f) {
temperaturePartitionedOceanBiomes.get(0.0f).addAll(entry.getValue());
} else {
temperaturePartitionedOceanBiomes.get(0.5f).addAll(entry.getValue());
}
}
}
public float getBiomeTemperature(Biome biome) {
String biomeName = biome.name().toLowerCase();
if (biomeName.endsWith("hills") || biomeName.endsWith("beach") || biomeName.endsWith("shore")) {
String biomeTypeName = biomeName;
biomeTypeName = biomeTypeName.replace("hills", "");
biomeTypeName = biomeTypeName.replace("beach", "");
biomeTypeName = biomeTypeName.replace("shore", "");
biomeTypeName = biomeTypeName.substring(0, biomeTypeName.length() - 2);
for (Entry<Biome, Float> entry : lands.entrySet()) {
if (entry.getKey().name().startsWith(biomeTypeName)) {
return entry.getValue();
}
}
}
return lands.get(biome);
}
public Biome getTransitionBiome(Biome from) {
String biomeName = from.name().toLowerCase();
if (biomeName.contains("jungle")) {
if (biomeName.contains("modified")) {
return Biome.MODIFIED_JUNGLE_EDGE;
}
return Biome.JUNGLE_EDGE;
} else if (biomeName.contains("mountain")) {
return Biome.MOUNTAIN_EDGE;
}
return null;
}
public Biome getShoreBiome(Biome from, float temperature) {
String biomeName = from.name().toLowerCase();
if (biomeName.contains("mushroom")) {
return Biome.MUSHROOM_FIELD_SHORE;
} else if (biomeName.contains("mountains")) {
return Biome.STONE_SHORE;
}
if (temperature >= 0.8) {
return Biome.BEACH;
}
return Biome.SNOWY_BEACH;
}
public Biome getLandBiome(float temperature) {
ArrayList<Biome> biomes = null;
if (temperature <= 0.05f) {
biomes = temperaturePartitionedLandBiomes.get(0.05f);
} else if (temperature <= 0.3) {
biomes = temperaturePartitionedLandBiomes.get(0.3f);
} else if (temperature <= 0.85f) {
biomes = temperaturePartitionedLandBiomes.get(0.95f);
} else {
biomes = temperaturePartitionedLandBiomes.get(2.00f);
}
return biomes.get((int) random.nextFloat() * biomes.size());
}
public Biome getOceanBiome(float temperature) {
ArrayList<Biome> biomes = null;
if (temperature <= 0.00f) {
biomes = temperaturePartitionedOceanBiomes.get(0.0f);
} else {
biomes = temperaturePartitionedOceanBiomes.get(0.5f);
}
return biomes.get((int) random.nextFloat() * biomes.size());
}
}