Multi-threaded generation previews

This commit is contained in:
Anuken
2018-07-20 09:26:41 -04:00
parent 3a63fa5475
commit 3c83b88127
3 changed files with 38 additions and 17 deletions

View File

@@ -27,7 +27,7 @@ allprojects {
gdxVersion = '1.9.8' gdxVersion = '1.9.8'
roboVMVersion = '2.3.0' roboVMVersion = '2.3.0'
aiVersion = '1.8.1' aiVersion = '1.8.1'
uCoreVersion = '3e5e261181' uCoreVersion = '9807f5009e3e58045effb565d9106559413380ff'
getVersionString = { getVersionString = {
String buildVersion = getBuildVersion() String buildVersion = getBuildVersion()

View File

@@ -33,7 +33,8 @@ public class WorldGenerator{
private Simplex sim = new Simplex(seed); private Simplex sim = new Simplex(seed);
private Simplex sim2 = new Simplex(seed + 1); private Simplex sim2 = new Simplex(seed + 1);
private Simplex sim3 = new Simplex(seed + 2); 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); 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){ public GenResult generateTile(int sectorX, int sectorY, int localX, int localY, boolean detailed){
int x = sectorX * sectorSize + localX; return generateTile(result, sectorX, sectorY, localX, localY, detailed);
int y = sectorY * sectorSize + localY; }
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 floor = Blocks.stone;
Block wall = Blocks.air; Block wall = Blocks.air;
double elevation = sim.octaveNoise2D(detailed ? 7 : 2, 0.5, 1f / 500, x, y) * 5.1 - 1; double ridge = Mathf.clamp(rid.getValue(x, y, 1f / 400f))/3f;
double temp = vn.noise(x, y, 1f/400f)/2f + sim3.octaveNoise2D(detailed ? 12 : 6, 0.6, 1f / 620f, x, y); 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 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)); 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; return result;
} }
public class GenResult{ public static class GenResult{
public Block floor, wall; public Block floor, wall;
public byte elevation; public byte elevation;
} }

View File

@@ -1,8 +1,10 @@
package io.anuke.mindustry.ui.dialogs; package io.anuke.mindustry.ui.dialogs;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.async.AsyncExecutor;
import io.anuke.mindustry.game.Team; import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.maps.generation.WorldGenerator.GenResult; import io.anuke.mindustry.maps.generation.WorldGenerator.GenResult;
import io.anuke.mindustry.world.ColorMapper; 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.event.InputListener;
import io.anuke.ucore.scene.utils.Cursors; import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.GridMap; 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.sectorSize;
import static io.anuke.mindustry.Vars.world; import static io.anuke.mindustry.Vars.world;
@@ -26,9 +29,11 @@ public class GenViewDialog extends FloatingDialog{
public class GenView extends Element{ public class GenView extends Element{
GridMap<Texture> map = new GridMap<>(); GridMap<Texture> map = new GridMap<>();
GridMap<Boolean> processing = new GridMap<>();
float panX, panY; float panX, panY;
float lastX, lastY; float lastX, lastY;
int viewsize = 2; int viewsize = 3;
AsyncExecutor async = new AsyncExecutor(Mathf.sqr(viewsize*2));
{ {
addListener(new InputListener(){ addListener(new InputListener(){
@@ -57,9 +62,9 @@ public class GenViewDialog extends FloatingDialog{
} }
public void draw(){ public void draw(){
int tx = (int)(panX / sectorSize);
int ty = (int)(panY / sectorSize);
float padSectorSize = 200f; float padSectorSize = 200f;
int tx = (int)(panX / padSectorSize);
int ty = (int)(panY / padSectorSize);
Draw.color(); Draw.color();
@@ -67,14 +72,24 @@ public class GenViewDialog extends FloatingDialog{
for(int y = -viewsize; y <= viewsize; y++){ for(int y = -viewsize; y <= viewsize; y++){
int wx = tx + x, wy = ty + y; int wx = tx + x, wy = ty + y;
if(map.get(wx, wy) == null){ if(map.get(wx, wy) == null){
Pixmap pixmap = new Pixmap(sectorSize, sectorSize, Format.RGBA8888); if(processing.get(wx, wy) == Boolean.TRUE){
for(int i = 0; i < sectorSize; i++){ continue;
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));
}
} }
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; float drawX = x + width/2f+ wx * padSectorSize - tx * padSectorSize - panX % padSectorSize;