Added executor to utilities.
This commit is contained in:
parent
14d0375c7e
commit
c2fb0ffea8
@ -3,11 +3,15 @@ package ca.recrown.islandsurvivalcraft;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
|
||||||
import ca.recrown.islandsurvivalcraft.datatypes.Point2;
|
import ca.recrown.islandsurvivalcraft.datatypes.Point2;
|
||||||
|
|
||||||
public class Utilities {
|
public class Utilities {
|
||||||
public final static int CHUNK_SIZE = 16;
|
public final static int CHUNK_SIZE = 16;
|
||||||
|
public final static ExecutorService ISC_EXECUTOR = Executors.newFixedThreadPool(2, createThreadFactory());
|
||||||
|
|
||||||
public static <K, V> HashMap<V, ArrayList<K>> invertHashMap(HashMap<K, V> hashMap) {
|
public static <K, V> HashMap<V, ArrayList<K>> invertHashMap(HashMap<K, V> hashMap) {
|
||||||
HashMap<V, ArrayList<K>> res = new HashMap<>();
|
HashMap<V, ArrayList<K>> res = new HashMap<>();
|
||||||
@ -71,4 +75,14 @@ public class Utilities {
|
|||||||
public static Point2 worldToLocalChunkCoordinates(Point2 worldCoordinates) {
|
public static Point2 worldToLocalChunkCoordinates(Point2 worldCoordinates) {
|
||||||
return worldToLocalChunkCoordinates(worldCoordinates.x, worldCoordinates.y);
|
return worldToLocalChunkCoordinates(worldCoordinates.x, worldCoordinates.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ThreadFactory createThreadFactory() {
|
||||||
|
return new ThreadFactory(){
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r) {
|
||||||
|
Thread thread = new Thread(r, "ISC-worker");
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
@ -28,7 +28,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
|
|||||||
private final Cache<Point2, Double> blockValueCache = new Cache<>(102400);
|
private final Cache<Point2, Double> blockValueCache = new Cache<>(102400);
|
||||||
private final Cache<Point2, Biome[]> biomeCache = new Cache<>(102400);
|
private final Cache<Point2, Biome[]> biomeCache = new Cache<>(102400);
|
||||||
private final Cache<Point2, Boolean> chunkExistenceCache = new Cache<>(16384);
|
private final Cache<Point2, Boolean> chunkExistenceCache = new Cache<>(16384);
|
||||||
private final ExecutorService executor = Executors.newFixedThreadPool(2);
|
private final ExecutorService executor = Utilities.ISC_EXECUTOR;
|
||||||
private volatile World currentWorld;
|
private volatile World currentWorld;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -43,7 +43,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
|
|||||||
BiomeGenerator biomeGenerator = new UniBiomeIslandGenerator();
|
BiomeGenerator biomeGenerator = new UniBiomeIslandGenerator();
|
||||||
|
|
||||||
int maxHeight = world.getMaxHeight();
|
int maxHeight = world.getMaxHeight();
|
||||||
LinkedList<Future<Boolean>> biomeColumns = new LinkedList<>();
|
LinkedList<Future<Boolean>> tasks = new LinkedList<>();
|
||||||
ChunkData chunkData = createChunkData(world);
|
ChunkData chunkData = createChunkData(world);
|
||||||
|
|
||||||
Biome[][] biomes = new Biome[Utilities.CHUNK_SIZE][Utilities.CHUNK_SIZE];
|
Biome[][] biomes = new Biome[Utilities.CHUNK_SIZE][Utilities.CHUNK_SIZE];
|
||||||
@ -57,23 +57,23 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
|
|||||||
|
|
||||||
final int biomeX = localX;
|
final int biomeX = localX;
|
||||||
final int biomeZ = localZ;
|
final int biomeZ = localZ;
|
||||||
biomeColumns.add(executor.submit(() -> {
|
tasks.add(executor.submit(() -> {
|
||||||
for (int y = 0; y < maxHeight; y++) {
|
for (int y = 0; y < maxHeight; y++) {
|
||||||
biomeGrid.setBiome(biomeX, y, biomeZ, biomes[biomeX][biomeZ]);
|
biomeGrid.setBiome(biomeX, y, biomeZ, biomes[biomeX][biomeZ]);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
int worldX = 16 * chunkX + localX;
|
final int worldX = 16 * chunkX + localX;
|
||||||
int worldZ = 16 * chunkZ + localZ;
|
final int worldZ = 16 * chunkZ + localZ;
|
||||||
int height = heightShader.getAltitude(worldX, worldZ, biomes[localX][localZ]);
|
int height = heightShader.getAltitude(worldX, worldZ, biomes[localX][localZ]);
|
||||||
chunkData.setRegion(localX, 1, localZ, localX + 1, height, localZ + 1, Material.DIAMOND_BLOCK);
|
chunkData.setRegion(localX, 1, localZ, localX + 1, height, localZ + 1, Material.DIAMOND_BLOCK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
chunkData.setRegion(0, 0, 0, 16, 1, 16, Material.BEDROCK);
|
chunkData.setRegion(0, 0, 0, 16, 1, 16, Material.BEDROCK);
|
||||||
while (!biomeColumns.isEmpty()) {
|
while (!tasks.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
biomeColumns.poll().get();
|
tasks.poll().get();
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new IllegalStateException(e.getCause().getMessage());
|
throw new IllegalStateException(e.getCause().getMessage());
|
||||||
@ -81,7 +81,7 @@ public class IslandWorldChunkGenerator extends ChunkGenerator implements Listene
|
|||||||
}
|
}
|
||||||
return chunkData;
|
return chunkData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onChunkLoaded(ChunkLoadEvent event) {
|
public void onChunkLoaded(ChunkLoadEvent event) {
|
||||||
if (event.isNewChunk()) {
|
if (event.isNewChunk()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user