From 3c83b881275b0b31382c0318f059e1cba318f31f Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 20 Jul 2018 09:26:41 -0400 Subject: [PATCH] Multi-threaded generation previews --- build.gradle | 2 +- .../maps/generation/WorldGenerator.java | 18 ++++++---- .../mindustry/ui/dialogs/GenViewDialog.java | 35 +++++++++++++------ 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index e176dc0d5b..370bf9c320 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ allprojects { gdxVersion = '1.9.8' roboVMVersion = '2.3.0' aiVersion = '1.8.1' - uCoreVersion = '3e5e261181' + uCoreVersion = '9807f5009e3e58045effb565d9106559413380ff' getVersionString = { String buildVersion = getBuildVersion() diff --git a/core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java b/core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java index 7ed393f5c2..c1a3048fde 100644 --- a/core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java +++ b/core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java @@ -33,7 +33,8 @@ public class WorldGenerator{ private Simplex sim = new Simplex(seed); private Simplex sim2 = new Simplex(seed + 1); private Simplex sim3 = new Simplex(seed + 2); - private VoronoiNoise vn = new VoronoiNoise(seed + 2, (short)0); + private RidgedPerlin rid = new RidgedPerlin(seed + 4, 1); + private VoronoiNoise vn = new VoronoiNoise(seed + 2, (short)1); private SeedRandom random = new SeedRandom(seed + 3); @@ -211,14 +212,19 @@ public class WorldGenerator{ } public GenResult generateTile(int sectorX, int sectorY, int localX, int localY, boolean detailed){ - int x = sectorX * sectorSize + localX; - int y = sectorY * sectorSize + localY; + return generateTile(result, sectorX, sectorY, localX, localY, detailed); + } + + public GenResult generateTile(GenResult result, int sectorX, int sectorY, int localX, int localY, boolean detailed){ + int x = sectorX * sectorSize + localX + Short.MAX_VALUE; + int y = sectorY * sectorSize + localY + Short.MAX_VALUE; Block floor = Blocks.stone; Block wall = Blocks.air; - double elevation = sim.octaveNoise2D(detailed ? 7 : 2, 0.5, 1f / 500, x, y) * 5.1 - 1; - double temp = vn.noise(x, y, 1f/400f)/2f + sim3.octaveNoise2D(detailed ? 12 : 6, 0.6, 1f / 620f, x, y); + double ridge = Mathf.clamp(rid.getValue(x, y, 1f / 400f))/3f; + double elevation = sim.octaveNoise2D(detailed ? 7 : 2, 0.5, 1f / 500, x, y) * 5.1 - 1 - ridge; + double temp = sim3.octaveNoise2D(detailed ? 2 : 0, 1, 1f / 13f, x, y)/13f + sim3.octaveNoise2D(detailed ? 12 : 6, 0.6, 1f / 620f, x, y) - ridge; double r = sim2.octaveNoise2D(1, 0.6, 1f / 70, x, y); double edgeDist = Math.max(sectorSize / 2, sectorSize / 2) - Math.max(Math.abs(x - sectorSize / 2), Math.abs(y - sectorSize / 2)); @@ -271,7 +277,7 @@ public class WorldGenerator{ return result; } - public class GenResult{ + public static class GenResult{ public Block floor, wall; public byte elevation; } diff --git a/core/src/io/anuke/mindustry/ui/dialogs/GenViewDialog.java b/core/src/io/anuke/mindustry/ui/dialogs/GenViewDialog.java index e5f8f2d1cc..7080cfdcb0 100644 --- a/core/src/io/anuke/mindustry/ui/dialogs/GenViewDialog.java +++ b/core/src/io/anuke/mindustry/ui/dialogs/GenViewDialog.java @@ -1,8 +1,10 @@ package io.anuke.mindustry.ui.dialogs; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.utils.async.AsyncExecutor; import io.anuke.mindustry.game.Team; import io.anuke.mindustry.maps.generation.WorldGenerator.GenResult; import io.anuke.mindustry.world.ColorMapper; @@ -12,6 +14,7 @@ import io.anuke.ucore.scene.event.InputEvent; import io.anuke.ucore.scene.event.InputListener; import io.anuke.ucore.scene.utils.Cursors; import io.anuke.ucore.util.GridMap; +import io.anuke.ucore.util.Mathf; import static io.anuke.mindustry.Vars.sectorSize; import static io.anuke.mindustry.Vars.world; @@ -26,9 +29,11 @@ public class GenViewDialog extends FloatingDialog{ public class GenView extends Element{ GridMap map = new GridMap<>(); + GridMap processing = new GridMap<>(); float panX, panY; float lastX, lastY; - int viewsize = 2; + int viewsize = 3; + AsyncExecutor async = new AsyncExecutor(Mathf.sqr(viewsize*2)); { addListener(new InputListener(){ @@ -57,9 +62,9 @@ public class GenViewDialog extends FloatingDialog{ } public void draw(){ - int tx = (int)(panX / sectorSize); - int ty = (int)(panY / sectorSize); float padSectorSize = 200f; + int tx = (int)(panX / padSectorSize); + int ty = (int)(panY / padSectorSize); Draw.color(); @@ -67,14 +72,24 @@ public class GenViewDialog extends FloatingDialog{ for(int y = -viewsize; y <= viewsize; y++){ int wx = tx + x, wy = ty + y; if(map.get(wx, wy) == null){ - Pixmap pixmap = new Pixmap(sectorSize, sectorSize, Format.RGBA8888); - for(int i = 0; i < sectorSize; i++){ - for(int j = 0; j < sectorSize; j++){ - GenResult result = world.generator().generateTile(wx, wy, i, j); - pixmap.drawPixel(i, sectorSize - 1 - j, ColorMapper.colorFor(result.floor, result.wall, Team.none, result.elevation)); - } + if(processing.get(wx, wy) == Boolean.TRUE){ + continue; } - map.put(wx, wy, new Texture(pixmap)); + processing.put(wx, wy, true); + async.submit(() -> { + GenResult result = new GenResult(); + Pixmap pixmap = new Pixmap(sectorSize, sectorSize, Format.RGBA8888); + for(int i = 0; i < sectorSize; i++){ + for(int j = 0; j < sectorSize; j++){ + world.generator().generateTile(result, wx, wy, i, j, true); + pixmap.drawPixel(i, sectorSize - 1 - j, ColorMapper.colorFor(result.floor, result.wall, Team.none, result.elevation)); + } + } + Gdx.app.postRunnable(() -> map.put(wx, wy, new Texture(pixmap))); + return pixmap; + }); + + continue; } float drawX = x + width/2f+ wx * padSectorSize - tx * padSectorSize - panX % padSectorSize;