diff --git a/src/main/java/electrosphere/logger/Logger.java b/src/main/java/electrosphere/logger/Logger.java index 30f8ccc0..f51b5f6f 100644 --- a/src/main/java/electrosphere/logger/Logger.java +++ b/src/main/java/electrosphere/logger/Logger.java @@ -130,6 +130,17 @@ public class Logger { } } + /** + * Logs an error message. + * This should be used every time we throw any kind of error in the engine + * @param e The exception to report + */ + public void ERROR(Error e){ + if(level == LogLevel.LOOP_DEBUG || level == LogLevel.DEBUG || level == LogLevel.INFO || level == LogLevel.WARNING || level == LogLevel.ERROR){ + e.printStackTrace(); + } + } + /** * Prints a message at the specified logging level * @param level The logging level diff --git a/src/main/java/electrosphere/server/terrain/generation/TestGenerationChunkGenerator.java b/src/main/java/electrosphere/server/terrain/generation/TestGenerationChunkGenerator.java index 5b65606e..65a86e8b 100644 --- a/src/main/java/electrosphere/server/terrain/generation/TestGenerationChunkGenerator.java +++ b/src/main/java/electrosphere/server/terrain/generation/TestGenerationChunkGenerator.java @@ -68,81 +68,80 @@ public class TestGenerationChunkGenerator implements ChunkGenerator { public ServerTerrainChunk generateChunk(int worldX, int worldY, int worldZ, int stride) { Globals.profiler.beginAggregateCpuSample("TestGenerationChunkGenerator.generateChunk"); ServerTerrainChunk rVal = null; - float[][][] weights; - int[][][] values; + float[][][] weights = new float[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION];; + int[][][] values = new int[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; - if(worldX == 0 || worldZ == 0){ - //generate flat ground for the player to spawn on - weights = new float[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; - values = new int[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; - for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ - for(int y = 0; y < ServerTerrainChunk.CHUNK_DIMENSION; y++){ - Arrays.fill(weights[x][y],-1f); + try { + if(worldX == 0 || worldZ == 0){ + //generate flat ground for the player to spawn on + for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ + for(int y = 0; y < ServerTerrainChunk.CHUNK_DIMENSION; y++){ + Arrays.fill(weights[x][y],-1f); + } } - } - if(worldY == 0){ + if(worldY == 0){ + for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ + for(int z = 0; z < ServerTerrainChunk.CHUNK_DIMENSION; z++){ + values[x][0][z] = 1; + weights[x][0][z] = 0.1f; + } + } + } + + + + } else { + //actual generation algo + + //biome of the current chunk + BiomeData surfaceBiome = this.terrainModel.getSurfaceBiome(worldX, worldY, worldZ); + + BiomeSurfaceGenerationParams surfaceParams = surfaceBiome.getSurfaceGenerationParams(); + HeightmapGenerator heightmapGen = this.tagGeneratorMap.get(surfaceParams.getSurfaceGenTag()); + if(heightmapGen == null){ + throw new Error("Undefined heightmap generator in biome! " + surfaceBiome.getId() + " " + surfaceBiome.getDisplayName() + " " + surfaceParams.getSurfaceGenTag()); + } + + //stride value + int strideValue = (int)Math.pow(2,stride); + + //presolve heightfield + float[][] heightfield = new float[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ - for(int z = 0; z < ServerTerrainChunk.CHUNK_DIMENSION; z++){ - values[x][0][z] = 1; - weights[x][0][z] = 0.1f; - } - } - } - - - - } else { - //actual generation algo - weights = new float[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; - values = new int[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; - - //biome of the current chunk - BiomeData surfaceBiome = this.terrainModel.getSurfaceBiome(worldX, worldY, worldZ); - - BiomeSurfaceGenerationParams surfaceParams = surfaceBiome.getSurfaceGenerationParams(); - HeightmapGenerator heightmapGen = this.tagGeneratorMap.get(surfaceParams.getSurfaceGenTag()); - if(heightmapGen == null){ - throw new Error("Undefined heightmap generator in biome! " + surfaceBiome.getId() + " " + surfaceBiome.getDisplayName() + " " + surfaceParams.getSurfaceGenTag()); - } - - //stride value - int strideValue = (int)Math.pow(2,stride); - - //presolve heightfield - float[][] heightfield = new float[ServerTerrainChunk.CHUNK_DIMENSION][ServerTerrainChunk.CHUNK_DIMENSION]; - for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ - for(int z = 0; z < ServerTerrainChunk.CHUNK_DIMENSION; z++){ - int finalWorldX = worldX + ((x * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); - int finalWorldZ = worldZ + ((z * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); - int finalChunkX = (x * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; - int finalChunkZ = (z * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; - heightfield[x][z] = heightmapGen.getHeight( - this.terrainModel.getSeed(), - this.serverWorldData.convertVoxelToRealSpace(finalChunkX, finalWorldX), - this.serverWorldData.convertVoxelToRealSpace(finalChunkZ, finalWorldZ) - ); - } - } - - for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ - Globals.profiler.beginAggregateCpuSample("TestGenerationChunkGenerator - Generate slice"); - for(int y = 0; y < ServerTerrainChunk.CHUNK_DIMENSION; y++){ for(int z = 0; z < ServerTerrainChunk.CHUNK_DIMENSION; z++){ int finalWorldX = worldX + ((x * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); - int finalWorldY = worldY + ((y * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); int finalWorldZ = worldZ + ((z * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); int finalChunkX = (x * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; - int finalChunkY = (y * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; int finalChunkZ = (z * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; - GeneratedVoxel voxel = this.getVoxel(finalWorldX, finalWorldY, finalWorldZ, finalChunkX, finalChunkY, finalChunkZ, heightfield[x][z], this.terrainModel, surfaceBiome); - weights[x][y][z] = voxel.weight; - values[x][y][z] = voxel.type; + heightfield[x][z] = heightmapGen.getHeight( + this.terrainModel.getSeed(), + this.serverWorldData.convertVoxelToRealSpace(finalChunkX, finalWorldX), + this.serverWorldData.convertVoxelToRealSpace(finalChunkZ, finalWorldZ) + ); } } - Globals.profiler.endCpuSample(); - } - } + for(int x = 0; x < ServerTerrainChunk.CHUNK_DIMENSION; x++){ + Globals.profiler.beginAggregateCpuSample("TestGenerationChunkGenerator - Generate slice"); + for(int y = 0; y < ServerTerrainChunk.CHUNK_DIMENSION; y++){ + for(int z = 0; z < ServerTerrainChunk.CHUNK_DIMENSION; z++){ + int finalWorldX = worldX + ((x * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); + int finalWorldY = worldY + ((y * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); + int finalWorldZ = worldZ + ((z * strideValue) / ServerTerrainChunk.CHUNK_DIMENSION); + int finalChunkX = (x * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; + int finalChunkY = (y * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; + int finalChunkZ = (z * strideValue) % ServerTerrainChunk.CHUNK_DIMENSION; + GeneratedVoxel voxel = this.getVoxel(finalWorldX, finalWorldY, finalWorldZ, finalChunkX, finalChunkY, finalChunkZ, heightfield[x][z], this.terrainModel, surfaceBiome); + weights[x][y][z] = voxel.weight; + values[x][y][z] = voxel.type; + } + } + Globals.profiler.endCpuSample(); + } + } + } catch(Exception ex){ + ex.printStackTrace(); + } rVal = new ServerTerrainChunk(worldX, worldY, worldZ, weights, values); Globals.profiler.endCpuSample(); return rVal; diff --git a/src/main/java/electrosphere/server/terrain/manager/ChunkGenerationThread.java b/src/main/java/electrosphere/server/terrain/manager/ChunkGenerationThread.java index 816036b6..cf1ed51b 100644 --- a/src/main/java/electrosphere/server/terrain/manager/ChunkGenerationThread.java +++ b/src/main/java/electrosphere/server/terrain/manager/ChunkGenerationThread.java @@ -4,6 +4,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import electrosphere.engine.Globals; +import electrosphere.logger.LoggerInterface; import electrosphere.server.terrain.diskmap.ChunkDiskMap; import electrosphere.server.terrain.generation.interfaces.ChunkGenerator; @@ -95,37 +96,41 @@ public class ChunkGenerationThread implements Runnable { public void run() { ServerTerrainChunk chunk = null; int i = 0; - while(chunk == null && i < MAX_TIME_TO_WAIT && Globals.threadManager.shouldKeepRunning()){ - if(chunkCache.containsChunk(worldX,worldY,worldZ,stride)){ - chunk = chunkCache.get(worldX, worldY, worldZ, stride); - } else { - //pull from disk if it exists - if(chunkDiskMap != null){ - if(chunkDiskMap.containsTerrainAtPosition(worldX, worldY, worldZ)){ - chunk = chunkDiskMap.getTerrainChunk(worldX, worldY, worldZ); + try { + while(chunk == null && i < MAX_TIME_TO_WAIT && Globals.threadManager.shouldKeepRunning()){ + if(chunkCache.containsChunk(worldX,worldY,worldZ,stride)){ + chunk = chunkCache.get(worldX, worldY, worldZ, stride); + } else { + //pull from disk if it exists + if(chunkDiskMap != null){ + if(chunkDiskMap.containsTerrainAtPosition(worldX, worldY, worldZ)){ + chunk = chunkDiskMap.getTerrainChunk(worldX, worldY, worldZ); + } + } + //generate if it does not exist + if(chunk == null){ + chunk = chunkGenerator.generateChunk(worldX, worldY, worldZ, stride); + } + if(chunk != null){ + chunkCache.add(worldX, worldY, worldZ, stride, chunk); } } - //generate if it does not exist if(chunk == null){ - chunk = chunkGenerator.generateChunk(worldX, worldY, worldZ, stride); - } - if(chunk != null){ - chunkCache.add(worldX, worldY, worldZ, stride, chunk); + try { + TimeUnit.MILLISECONDS.sleep(WAIT_TIME_MS); + } catch (InterruptedException e) { + e.printStackTrace(); + } } + i++; } - if(chunk == null){ - try { - TimeUnit.MILLISECONDS.sleep(WAIT_TIME_MS); - } catch (InterruptedException e) { - e.printStackTrace(); - } + if(i >= MAX_TIME_TO_WAIT){ + throw new Error("Failed to resolve chunk!"); } - i++; + this.onLoad.accept(chunk); + } catch (Error e){ + LoggerInterface.loggerEngine.ERROR(e); } - if(i >= MAX_TIME_TO_WAIT){ - throw new Error("Failed to resolve chunk!"); - } - this.onLoad.accept(chunk); } } diff --git a/src/main/java/electrosphere/server/terrain/models/TerrainModel.java b/src/main/java/electrosphere/server/terrain/models/TerrainModel.java index 7c97cb86..17f4757e 100644 --- a/src/main/java/electrosphere/server/terrain/models/TerrainModel.java +++ b/src/main/java/electrosphere/server/terrain/models/TerrainModel.java @@ -127,11 +127,13 @@ public class TerrainModel { TerrainModel rVal = new TerrainModel(); rVal.discreteArrayDimension = TestGenerationChunkGenerator.GENERATOR_REALM_SIZE; rVal.dynamicInterpolationRatio = 1; - rVal.biome = new short[2][2]; - rVal.biome[0][0] = TestGenerationChunkGenerator.DEFAULT_BIOME_INDEX; - rVal.biome[1][0] = TestGenerationChunkGenerator.DEFAULT_BIOME_INDEX; - rVal.biome[0][1] = TestGenerationChunkGenerator.DEFAULT_BIOME_INDEX; - rVal.biome[1][1] = TestGenerationChunkGenerator.DEFAULT_BIOME_INDEX; + int macroDataImageScale = TestGenerationChunkGenerator.GENERATOR_REALM_SIZE / DEFAULT_MACRO_DATA_SCALE + 1; + rVal.biome = new short[macroDataImageScale][macroDataImageScale]; + for(int x = 0; x < macroDataImageScale; x++){ + for(int z = 0; z < macroDataImageScale; z++){ + rVal.biome[x][z] = TestGenerationChunkGenerator.DEFAULT_BIOME_INDEX; + } + } return rVal; }