From c3825552613eed36093ad68be76c37bb7a480bda Mon Sep 17 00:00:00 2001 From: Grimm Date: Mon, 8 Aug 2022 11:10:18 +0200 Subject: [PATCH 1/3] adventure Editor rework --- .../src/forge/adventure/world/World.java | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/forge-gui-mobile/src/forge/adventure/world/World.java b/forge-gui-mobile/src/forge/adventure/world/World.java index 0d0c68838bf..4878283b1e6 100644 --- a/forge-gui-mobile/src/forge/adventure/world/World.java +++ b/forge-gui-mobile/src/forge/adventure/world/World.java @@ -278,7 +278,7 @@ private void clearTerrain(int x,int y,int size) private long measureGenerationTime(String msg,long lastTime) { long currentTime = System.currentTimeMillis(); - //System.out.print("\n"+msg+" :\t\t"+((currentTime-lastTime)/1000f)+" s"); + System.out.print("\n"+msg+" :\t\t"+((currentTime-lastTime)/1000f)+" s"); return currentTime; } public World generateNew(long seed) { @@ -288,8 +288,10 @@ private long measureGenerationTime(String msg,long lastTime) loadWorldData(); - if(seed==0) { seed=random.nextLong(); } - this.seed=seed; + if (seed == 0) { + seed = random.nextLong(); + } + this.seed = seed; random.setSeed(seed); OpenSimplexNoise noise = new OpenSimplexNoise(seed); @@ -298,7 +300,7 @@ private long measureGenerationTime(String msg,long lastTime) height = data.height; //save at all data biomeMap = new long[width][height]; - terrainMap= new int[width][height]; + terrainMap = new int[width][height]; Pixmap pix = new Pixmap(width, height, Pixmap.Format.RGBA8888); for (int x = 0; x < width; x++) { @@ -312,7 +314,32 @@ private long measureGenerationTime(String msg,long lastTime) pix.fill(); int biomeIndex = -1; - currentTime=measureGenerationTime("loading data",currentTime); + currentTime = measureGenerationTime("loading data", currentTime); + HashMap structureDataMap = new HashMap<>(); + + + for (BiomeData biome : data.GetBiomes()) { + if (biome.structures != null) { + int biomeWidth = (int) Math.round(biome.width * (double) width); + int biomeHeight = (int) Math.round(biome.height * (double) height); + for (BiomeStructureData data : biome.structures) { + long localSeed=seed; + Thread worker=new Thread(()-> + { + long threadStartTime = System.currentTimeMillis(); + BiomeStructure structure = new BiomeStructure(data, localSeed, biomeWidth, biomeHeight); + structure.initialize(); + structureDataMap.put(data, structure); + measureGenerationTime("wavefunctioncollapse " + data.sourcePath, threadStartTime); + }); + + worker.start(); + + } + } + } + + for (BiomeData biome : data.GetBiomes()) { biomeIndex++; @@ -331,7 +358,6 @@ private long measureGenerationTime(String msg,long lastTime) endX = width; endY = height; } - HashMap structureDataMap=new HashMap<>(); for (int x = beginX; x < endX; x++) { for (int y = beginY; y < endY; y++) { //value 0-1 based on noise @@ -370,19 +396,15 @@ private long measureGenerationTime(String msg,long lastTime) { for(BiomeStructureData data:biome.structures) { + while(!structureDataMap.containsKey(data)) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } - BiomeStructure structure; - if(!structureDataMap.containsKey(data)) - { - structure=new BiomeStructure(data,seed,biomeWidth,biomeHeight); - structure.initialize(); - structureDataMap.put(data,structure); - currentTime=measureGenerationTime("wavefunctioncollapse "+data.sourcePath,currentTime); - } - else - { - structure=structureDataMap.get(data); - } + BiomeStructure structure=structureDataMap.get(data); int structureXStart= x-(biomeXStart - biomeWidth / 2)-(int) ((data.x*biomeWidth)-(data.width*biomeWidth/2)); int structureYStart= y-(biomeYStart - biomeHeight / 2)- (int) ((data.y*biomeHeight)-(data.height*biomeHeight/2)); @@ -406,6 +428,7 @@ private long measureGenerationTime(String msg,long lastTime) } } } + currentTime=measureGenerationTime("biomes in total",currentTime); mapPoiIds = new PointOfInterestMap(getChunkSize(), data.tileSize, data.width / getChunkSize(),data.height / getChunkSize()); List towns = new ArrayList<>(); @@ -679,7 +702,7 @@ private long measureGenerationTime(String msg,long lastTime) } biomeImage = pix; measureGenerationTime("sprites",currentTime); - //System.out.print("\nGenerating world took :\t\t"+((System.currentTimeMillis()-startTime)/1000f)+" s"); + System.out.print("\nGenerating world took :\t\t"+((System.currentTimeMillis()-startTime)/1000f)+" s"); WorldStage.getInstance().clearCache(); return this; } From 670f35ddec605d9fce245770084290502846f2e3 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Mon, 8 Aug 2022 17:38:12 +0800 Subject: [PATCH 2/3] use threadutil servicepool --- forge-gui-mobile/src/forge/adventure/world/World.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/forge-gui-mobile/src/forge/adventure/world/World.java b/forge-gui-mobile/src/forge/adventure/world/World.java index 4878283b1e6..15569daf69a 100644 --- a/forge-gui-mobile/src/forge/adventure/world/World.java +++ b/forge-gui-mobile/src/forge/adventure/world/World.java @@ -18,6 +18,7 @@ import forge.adventure.util.Config; import forge.adventure.util.Paths; import forge.adventure.util.SaveFileContent; import forge.adventure.util.SaveFileData; +import forge.util.ThreadUtil; import org.apache.commons.lang3.tuple.Pair; import java.util.*; @@ -324,17 +325,13 @@ private long measureGenerationTime(String msg,long lastTime) int biomeHeight = (int) Math.round(biome.height * (double) height); for (BiomeStructureData data : biome.structures) { long localSeed=seed; - Thread worker=new Thread(()-> - { + ThreadUtil.getServicePool().submit(() -> { long threadStartTime = System.currentTimeMillis(); BiomeStructure structure = new BiomeStructure(data, localSeed, biomeWidth, biomeHeight); structure.initialize(); structureDataMap.put(data, structure); measureGenerationTime("wavefunctioncollapse " + data.sourcePath, threadStartTime); }); - - worker.start(); - } } } @@ -704,6 +701,8 @@ private long measureGenerationTime(String msg,long lastTime) measureGenerationTime("sprites",currentTime); System.out.print("\nGenerating world took :\t\t"+((System.currentTimeMillis()-startTime)/1000f)+" s"); WorldStage.getInstance().clearCache(); + ThreadUtil.getServicePool().shutdownNow(); + ThreadUtil.refreshServicePool(); return this; } From 08f61bc8915541d6bd3357c0349947f1d7a4866b Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Mon, 8 Aug 2022 18:03:06 +0800 Subject: [PATCH 3/3] Update World.java --- forge-gui-mobile/src/forge/adventure/world/World.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forge-gui-mobile/src/forge/adventure/world/World.java b/forge-gui-mobile/src/forge/adventure/world/World.java index 15569daf69a..20a37e35330 100644 --- a/forge-gui-mobile/src/forge/adventure/world/World.java +++ b/forge-gui-mobile/src/forge/adventure/world/World.java @@ -699,7 +699,7 @@ private long measureGenerationTime(String msg,long lastTime) } biomeImage = pix; measureGenerationTime("sprites",currentTime); - System.out.print("\nGenerating world took :\t\t"+((System.currentTimeMillis()-startTime)/1000f)+" s"); + System.out.print("\nGenerating world took :\t\t"+((System.currentTimeMillis()-startTime)/1000f)+" s\n"); WorldStage.getInstance().clearCache(); ThreadUtil.getServicePool().shutdownNow(); ThreadUtil.refreshServicePool();