multithreading good

pull/12/head
SuperCoder7979 2020-03-01 07:32:50 -05:00
parent d026feb525
commit 8d5a3efbdf
2 changed files with 41 additions and 42 deletions

View File

@ -136,7 +136,8 @@ public class World implements BlockAccess, WorldGenConstants
if (chunk == null) return false; if (chunk == null) return false;
// Otherwise save the chunk // Otherwise save the chunk
AtomicBoolean result = new AtomicBoolean(false); AtomicBoolean result = new AtomicBoolean(false);
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() ->
{
result.set(this.save.saveChunk(chunk)); result.set(this.save.saveChunk(chunk));
this.chunks.remove(posHash); this.chunks.remove(posHash);
}); });
@ -187,13 +188,16 @@ public class World implements BlockAccess, WorldGenConstants
public void unloadAllChunks() public void unloadAllChunks()
{ {
LongList chunkPositions = new LongArrayList(); LongList chunkPositions = new LongArrayList();
if (this.chunks != null) CompletableFuture.runAsync(() ->
this.chunks.forEach((pos, chunk) -> {
{ // for every chunk in memory if (this.chunks != null)
chunkPositions.add((long) pos); // add pos to chunk positions list for removal later this.chunks.forEach((pos, chunk) ->
this.save.saveChunk(chunk); // save chunk { // for every chunk in memory
}); chunkPositions.add((long) pos); // add pos to chunk positions list for removal later
chunkPositions.forEach((LongConsumer) (pos -> this.chunks.remove(pos))); // remove all chunks this.save.saveChunk(chunk); // save chunk
});
chunkPositions.forEach((LongConsumer) (pos -> this.chunks.remove(pos))); // remove all chunks
});
} }
public long getSeed() public long getSeed()
@ -203,43 +207,38 @@ public class World implements BlockAccess, WorldGenConstants
public void updateLoadedChunks(int chunkX, int chunkY, int chunkZ) public void updateLoadedChunks(int chunkX, int chunkY, int chunkZ)
{ {
//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA CompletableFuture.runAsync(() ->
//Ginger.getInstance().threading.registerChunkThreadToWaitlist(new DynamicChunkLoader(chunkX, chunkY, chunkZ, this));
Long2ObjectMap<Chunk> chunksList = this.chunks;
//FIXME completable futures
List<Chunk> toKeep = new ArrayList<>();
// loop over rendered area, adding chunks that are needed
for (int x = chunkX - this.renderBound; x < chunkX + this.renderBound; x++)
for (int z = chunkZ - this.renderBound; z < chunkZ + this.renderBound; z++)
for (int y = chunkY - this.renderBound; y < chunkY + this.renderBound; y++)
toKeep.add(this.getChunkToLoad(x, y, z));
LongList toRemove = new LongArrayList();
// check which loaded chunks are not neccesary
chunksList.forEach((pos, chunk) ->
{ {
if (!toKeep.contains(chunk)) List<Chunk> toKeep = new ArrayList<>();
toRemove.add((long) pos); // loop over rendered area, adding chunks that are needed
}); for (int x = chunkX - this.renderBound; x < chunkX + this.renderBound; x++)
// unload unneccesary chunks from chunk array for (int z = chunkZ - this.renderBound; z < chunkZ + this.renderBound; z++)
toRemove.forEach((LongConsumer) this::unloadChunk); for (int y = chunkY - this.renderBound; y < chunkY + this.renderBound; y++)
toKeep.add(this.getChunkToLoad(x, y, z));
toKeep.forEach(chunk -> { LongList toRemove = new LongArrayList();
if (!chunk.isFullyGenerated()) // check which loaded chunks are not neccesary
chunks.forEach((pos, chunk) ->
{ {
this.populateChunk(chunk); if (!toKeep.contains(chunk))
chunk.setFullyGenerated(true); toRemove.add((long) pos);
} });
boolean alreadyRendering = chunk.doRender(); // if it's already rendering then it's most likely in the map // unload unneccesary chunks from chunk array
chunk.setRender(true); toRemove.forEach((LongConsumer) this::unloadChunk);
if (!alreadyRendering)
chunksList.put(this.posHash(chunk.chunkX, chunk.chunkY, chunk.chunkZ), chunk);
});
this.chunks = chunksList;
toKeep.forEach(chunk ->
{
if (!chunk.isFullyGenerated())
{
this.populateChunk(chunk);
chunk.setFullyGenerated(true);
}
boolean alreadyRendering = chunk.doRender(); // if it's already rendering then it's most likely in the map
chunk.setRender(true);
if (!alreadyRendering)
chunks.put(this.posHash(chunk.chunkX, chunk.chunkY, chunk.chunkZ), chunk);
});
});
} }
private static final class GenerationWorld implements BlockAccess, WorldGenConstants private static final class GenerationWorld implements BlockAccess, WorldGenConstants

View File

@ -11,7 +11,7 @@ public class CavesModifier implements WorldModifier, WorldGenConstants
{ {
private OctaveSimplexNoise caveNoise; private OctaveSimplexNoise caveNoise;
private static final double THRESHOLD = 0.1; private static final double THRESHOLD = 0.1;
//
@Override @Override
public void initialize(long seed) public void initialize(long seed)
{ {