/* * * MIT License * * Copyright (c) 2020 TerraForged * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.terraforged.app.biome; import com.terraforged.core.cell.Cell; import com.terraforged.core.util.grid.FixedGrid; import com.terraforged.core.world.biome.BiomeData; import com.terraforged.core.world.biome.BiomeType; import com.terraforged.core.world.terrain.Terrain; import processing.data.JSONArray; import processing.data.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; public class BiomeProvider { private final List> biomes; public BiomeProvider() { biomes = getBiomes(5); } public BiomeData getBiome(Cell cell) { FixedGrid grid = biomes.get(cell.biomeType.ordinal()); if (grid == null) { return null; } return grid.get(cell.moisture, cell.temperature, cell.biome); } private static List> getBiomes(int gridSize) { List> data = new ArrayList<>(); for (BiomeType type : BiomeType.values()) { data.add(type.ordinal(), null); } Map biomes = loadBiomes(); Map> types = loadBiomeTypes(); for (Map.Entry> e : types.entrySet()) { List list = new LinkedList<>(); for (String id : e.getValue()) { BiomeData biome = biomes.get(id); if (biome != null) { list.add(biome); } } FixedGrid grid = FixedGrid.generate(gridSize, list, b -> b.rainfall, b -> b.temperature); data.set(e.getKey().ordinal(), grid); } return data; } private static Map loadBiomes() { try (InputStream inputStream = BiomeProvider.class.getResourceAsStream("/biome_data.json")) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder sb = new StringBuilder(); reader.lines().forEach(sb::append); JSONArray array = JSONArray.parse(sb.toString()); Map biomes = new HashMap<>(); for (int i = 0; i < array.size(); i++) { JSONObject object = array.getJSONObject(i); String name = object.getString("id"); float moisture = object.getFloat("moisture"); float temperature = object.getFloat("temperature"); int color = BiomeColor.getRGB(temperature, moisture); BiomeData biome = new BiomeData(name, null, color, moisture, temperature); biomes.put(name, biome); } return biomes; } catch (IOException e) { e.printStackTrace(); return Collections.emptyMap(); } } private static Map> loadBiomeTypes() { try (InputStream inputStream = BiomeProvider.class.getResourceAsStream("/biome_groups.json")) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder sb = new StringBuilder(); reader.lines().forEach(sb::append); JSONObject object = JSONObject.parse(sb.toString()); Iterator iterator = object.keyIterator(); Map> biomes = new HashMap<>(); while (iterator.hasNext()) { String key = "" + iterator.next(); if (key.contains("rivers")) { continue; } if (key.contains("oceans")) { continue; } BiomeType type = BiomeType.valueOf(key); List group = new LinkedList<>(); JSONArray array = object.getJSONArray(key); for (int i = 0; i < array.size(); i++) { group.add(array.getString(i)); } biomes.put(type, group); } return biomes; } catch (IOException e) { e.printStackTrace(); return Collections.emptyMap(); } } }