Skip to content

Commit

Permalink
Caching heightmaps in order to use for another generators
Browse files Browse the repository at this point in the history
  • Loading branch information
z3nth10n committed Apr 7, 2020
1 parent f8736ee commit 28f5d98
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/github/terra121/EarthTerrainProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.github.opencubicchunks.cubicchunks.cubicgen.common.biome.IBiomeBlockReplacerProvider;
import io.github.opencubicchunks.cubicchunks.cubicgen.customcubic.CustomGeneratorSettings;
import io.github.opencubicchunks.cubicchunks.cubicgen.customcubic.structure.CubicCaveGenerator;
import io.github.terra121.dataset.HeightmapModel;
import io.github.terra121.dataset.Heights;
import io.github.terra121.dataset.OpenStreetMaps;
import io.github.terra121.populator.CliffReplacer;
Expand Down Expand Up @@ -54,6 +55,8 @@ public class EarthTerrainProcessor extends BasicCubeGenerator {
private boolean doRoads;
private boolean doBuildings;

public static final int spawnSize = 5;

public EarthTerrainProcessor(World world) {
super(world);

Expand All @@ -75,7 +78,7 @@ public EarthTerrainProcessor(World world) {
unnaturals.add(Blocks.CONCRETE);
unnaturals.add(Blocks.BRICK_BLOCK);

surfacePopulators = new TreeSet<ICubicPopulator>();
surfacePopulators = new TreeSet<>();
if(doRoads || cfg.settings.osmwater)surfacePopulators.add(new RoadGenerator(osm, heights, projection));
surfacePopulators.add(new EarthTreePopulator(projection));
snow = new SnowPopulator(); //this will go after the rest
Expand Down Expand Up @@ -118,7 +121,7 @@ public CubePrimer generateCube(int cubeX, int cubeY, int cubeZ) {
boolean surface = false;

//null island
if(-5 < cubeX && cubeX < 5 && -5 < cubeZ && cubeZ < 5) {
if(-spawnSize < cubeX && cubeX < spawnSize && -spawnSize < cubeZ && cubeZ < spawnSize) {
for(int x=0; x<16; x++)
for(int z=0; z<16; z++)
heightarr[x][z] = 1;
Expand All @@ -139,6 +142,11 @@ public CubePrimer generateCube(int cubeX, int cubeY, int cubeZ) {
}
}

CubePos pos = new CubePos(cubeX, cubeY, cubeZ);
HeightmapModel model = new HeightmapModel(surface, heightarr);

HeightmapModel.add(pos, model);

//fill in the world
for(int x=0; x<16; x++) {
for(int z=0; z<16; z++) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/github/terra121/dataset/HeightmapModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.terra121.dataset;

import io.github.opencubicchunks.cubicchunks.api.util.CubePos;

import java.util.HashMap;
import java.util.Map;

public class HeightmapModel {
public boolean surface;
public double[][] heightmap;

private HeightmapModel() {}

public HeightmapModel(boolean surface, double[][] heightmap) {
this.heightmap = heightmap;
this.surface = surface;
}

private static Map<CubePos, HeightmapModel> cachedHeightmaps = new HashMap<>();

public static HeightmapModel getModel(int chunkX, int chunkY, int chunkZ) {
return getModel(new CubePos(chunkX, chunkY, chunkZ));
}

public static HeightmapModel getModel(CubePos pos) {
if(!cachedHeightmaps.containsKey(pos))
return null;

return cachedHeightmaps.get(pos);
}

public static void add(CubePos pos, HeightmapModel model) {
cachedHeightmaps.put(pos, model);
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/github/terra121/populator/RoadGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.terra121.populator;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.function.BiFunction;
Expand All @@ -22,7 +23,7 @@

public class RoadGenerator implements ICubicPopulator, Comparable<Object> {

private static HashSet<CubePos> roadChunks = new HashSet<>();
private static Set<CubePos> roadChunks = new HashSet<>();

private static final IBlockState ASPHALT = Blocks.CONCRETE.getDefaultState().withProperty(BlockColored.COLOR, EnumDyeColor.GRAY);
private static final IBlockState WATER_SOURCE = Blocks.WATER.getDefaultState();
Expand Down Expand Up @@ -222,7 +223,7 @@ private static double bound(double x, double slope, double j, double k, double r
public static boolean isRoad(int cubeX, int cubeY, int cubeZ) {
CubePos pos = new CubePos(cubeX, cubeY, cubeZ);
boolean isRoad = roadChunks.contains(pos);
roadChunks.remove(pos);
// roadChunks.remove(pos);
return isRoad;
}

Expand Down

0 comments on commit 28f5d98

Please sign in to comment.