9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-29 12:09:07 +00:00

Bugfixes & Performance Improvements

This commit is contained in:
Daniel Mills
2020-10-16 23:45:44 -04:00
parent 4ccfa8c3b6
commit 468c69d2dd
11 changed files with 296 additions and 59 deletions

View File

@@ -1,6 +1,7 @@
package com.volmit.iris.util;
import java.awt.Color;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import org.bukkit.Bukkit;
@@ -16,6 +17,8 @@ import com.volmit.iris.gen.IrisTerrainProvider;
import com.volmit.iris.gen.provisions.ProvisionBukkit;
import com.volmit.iris.gui.PregenGui;
import io.papermc.lib.PaperLib;
public class PregenJob implements Listener
{
private World world;
@@ -24,6 +27,7 @@ public class PregenJob implements Listener
private int genned;
private boolean completed;
public static int task = -1;
private Semaphore working;
private AtomicInteger g = new AtomicInteger();
private PrecisionStopwatch s;
private ChronoLatch cl;
@@ -41,20 +45,22 @@ public class PregenJob implements Listener
private boolean first;
private Consumer2<ChunkPosition, Color> consumer;
private IrisTerrainProvider tp;
private double cps = 0;
private int lg = 0;
private long lt = M.ms();
private int cubeSize = IrisSettings.get().getPregenTileSize();
private RollingSequence acps = new RollingSequence(PaperLib.isPaper() ? 8 : 32);
int xc = 0;
public PregenJob(World world, int size, MortarSender sender, Runnable onDone)
{
g.set(0);
working = new Semaphore(tc());
this.s = PrecisionStopwatch.start();
Iris.instance.registerListener(this);
this.world = world;
this.size = size;
this.onDone = onDone;
world.getWorldBorder().setCenter(0, 0);
world.getWorldBorder().setWarningDistance(64);
world.getWorldBorder().setSize(size);
this.sender = sender;
cl = new ChronoLatch(3000);
clx = new ChronoLatch(20000);
@@ -93,6 +99,11 @@ public class PregenJob implements Listener
task = Bukkit.getScheduler().scheduleSyncRepeatingTask(Iris.instance, this::onTick, 0, 0);
}
public int tc()
{
return 48;
}
public static void stop()
{
try
@@ -116,9 +127,17 @@ public class PregenJob implements Listener
PrecisionStopwatch p = PrecisionStopwatch.start();
while(p.getMilliseconds() < 7000)
if(PaperLib.isPaper())
{
tick();
tickPaper();
}
else
{
while(p.getMilliseconds() < 7000)
{
tick();
}
}
if(cl.flip())
@@ -138,8 +157,37 @@ public class PregenJob implements Listener
}
}
public void tickPaper()
{
if(working.getQueueLength() >= tc() / 2)
{
return;
}
for(int i = 0; i < 128; i++)
{
tick();
}
}
public void tick()
{
if((total - genned < 0 || genned > (((size + 32) / 16) * (size + 32) / 16)) && !completed)
{
completed = true;
for(Chunk i : world.getLoadedChunks())
{
i.unload(true);
}
saveAll();
Iris.instance.unregisterListener(this);
completed = true;
sender.sendMessage("Pregen Completed!");
onDone.run();
}
if(completed)
{
return;
@@ -190,18 +238,21 @@ public class PregenJob implements Listener
chunkSpiraler.retarget(cubeSize, cubeSize);
}
else
else if(!completed)
{
for(Chunk i : world.getLoadedChunks())
{
i.unload(true);
}
genned += (((size + 32) / 16) * (size + 32) / 16) + 100000;
}
saveAll();
Iris.instance.unregisterListener(this);
completed = true;
sender.sendMessage("Pregen Completed!");
onDone.run();
double dur = M.ms() - lt;
if(dur > 1000 && genned > lg)
{
int gain = genned - lg;
double rat = dur / 1000D;
acps.put((double) gain / rat);
cps = acps.getAverage();
lt = M.ms();
lg = genned;
}
}
@@ -214,17 +265,58 @@ public class PregenJob implements Listener
{
if(isChunkWithin(chunkX, chunkZ))
{
if(consumer != null)
if(PaperLib.isPaper())
{
consumer.accept(new ChunkPosition(chunkX, chunkZ), Color.blue.darker().darker());
if(consumer != null)
{
consumer.accept(new ChunkPosition(chunkX, chunkZ), Color.magenta.darker().darker().darker());
}
int cx = chunkX;
int cz = chunkZ;
J.a(() ->
{
try
{
working.acquire();
if(consumer != null)
{
consumer.accept(new ChunkPosition(cx, cz), Color.magenta);
}
PaperLib.getChunkAtAsync(world, cx, cz).thenAccept(chunk ->
{
working.release();
genned++;
if(consumer != null)
{
consumer.accept(new ChunkPosition(chunk.getX(), chunk.getZ()), tp != null ? tp.render(chunk.getX() * 16, chunk.getZ() * 16) : Color.blue);
}
});
}
catch(InterruptedException e)
{
e.printStackTrace();
}
});
}
world.loadChunk(chunkX, chunkZ);
genned++;
if(consumer != null)
else
{
consumer.accept(new ChunkPosition(chunkX, chunkZ), tp != null ? tp.render(chunkX * 16, chunkZ * 16) : Color.blue);
if(consumer != null)
{
consumer.accept(new ChunkPosition(chunkX, chunkZ), Color.blue.darker().darker());
}
world.loadChunk(chunkX, chunkZ);
genned++;
if(consumer != null)
{
consumer.accept(new ChunkPosition(chunkX, chunkZ), tp != null ? tp.render(chunkX * 16, chunkZ * 16) : Color.blue);
}
}
}
@@ -295,9 +387,9 @@ public class PregenJob implements Listener
public String[] getProgress()
{
long eta = (long) ((total - genned) * (s.getMilliseconds() / (double) genned));
long eta = (long) ((total - genned) * 1000D / cps);
return new String[] {"Progress: " + Form.pc(Math.min((double) genned / (double) total, 1.0), 0), "Generated: " + Form.f(genned) + " Chunks", "Remaining: " + Form.f(total - genned) + " Chunks", "Elapsed: " + Form.duration((long) s.getMilliseconds(), 2), "Estimate: " + ((genned >= total - 5 ? "Any second..." : s.getMilliseconds() < 25000 ? "Calculating..." : Form.duration(eta, 2))), "ChunksMS: " + Form.duration((s.getMilliseconds() / (double) genned), 2), "Chunks/s: " + Form.f(1000D / (s.getMilliseconds() / genned), 1),
return new String[] {"Progress: " + Form.pc(Math.min((double) genned / (double) total, 1.0), 0), "Generated: " + Form.f(genned) + " Chunks", "Remaining: " + Form.f(total - genned) + " Chunks", "Elapsed: " + Form.duration((long) s.getMilliseconds(), 2), "Estimate: " + ((genned >= total - 5 ? "Any second..." : s.getMilliseconds() < 25000 ? "Calculating..." : Form.duration(eta, 2))), "ChunksMS: " + Form.duration(1000D / cps, 2), "Chunks/s: " + Form.f(cps, 1),
};
}
}