mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-07-19 18:55:18 +00:00
The Iris pregenerator (logic)
This commit is contained in:
parent
e4da11727a
commit
2d531c3a15
@ -0,0 +1,220 @@
|
|||||||
|
/*
|
||||||
|
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||||
|
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.volmit.iris.core.pregenerator;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.AtomicDouble;
|
||||||
|
import com.volmit.iris.util.collection.KList;
|
||||||
|
import com.volmit.iris.util.collection.KSet;
|
||||||
|
import com.volmit.iris.util.math.M;
|
||||||
|
import com.volmit.iris.util.math.Position2;
|
||||||
|
import com.volmit.iris.util.math.RollingSequence;
|
||||||
|
import com.volmit.iris.util.scheduling.ChronoLatch;
|
||||||
|
import com.volmit.iris.util.scheduling.J;
|
||||||
|
import com.volmit.iris.util.scheduling.Looper;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
public class IrisPregenerator {
|
||||||
|
private final PregenTask task;
|
||||||
|
private final PregeneratorMethod generator;
|
||||||
|
private final PregenListener listener;
|
||||||
|
private final Looper ticker;
|
||||||
|
private final AtomicBoolean paused;
|
||||||
|
private final AtomicBoolean shutdown;
|
||||||
|
private final RollingSequence chunksPerSecond;
|
||||||
|
private final RollingSequence chunksPerMinute;
|
||||||
|
private final RollingSequence regionsPerMinute;
|
||||||
|
private final AtomicInteger generated;
|
||||||
|
private final AtomicInteger generatedLast;
|
||||||
|
private final AtomicInteger generatedLastMinute;
|
||||||
|
private final AtomicInteger totalChunks;
|
||||||
|
private final AtomicLong startTime;
|
||||||
|
private final ChronoLatch minuteLatch;
|
||||||
|
private final AtomicReference<String> currentGeneratorMethod;
|
||||||
|
|
||||||
|
public IrisPregenerator(PregenTask task, PregeneratorMethod generator, PregenListener listener)
|
||||||
|
{
|
||||||
|
this.listener = listenify(listener);
|
||||||
|
this.shutdown = new AtomicBoolean(false);
|
||||||
|
this.paused = new AtomicBoolean(false);
|
||||||
|
this.task = task;
|
||||||
|
this.generator = generator;
|
||||||
|
currentGeneratorMethod = new AtomicReference<>("Void");
|
||||||
|
minuteLatch = new ChronoLatch(60000, false);
|
||||||
|
chunksPerSecond = new RollingSequence(10);
|
||||||
|
chunksPerMinute = new RollingSequence(10);
|
||||||
|
regionsPerMinute = new RollingSequence(10);
|
||||||
|
generated = new AtomicInteger(0);
|
||||||
|
generatedLast = new AtomicInteger(0);
|
||||||
|
generatedLastMinute = new AtomicInteger(0);
|
||||||
|
totalChunks = new AtomicInteger(0);
|
||||||
|
startTime = new AtomicLong(M.ms());
|
||||||
|
ticker = new Looper() {
|
||||||
|
@Override
|
||||||
|
protected long loop() {
|
||||||
|
long eta = computeETA();
|
||||||
|
int secondGenerated = generated.get() - generatedLast.get();
|
||||||
|
generatedLast.set(generated.get());
|
||||||
|
chunksPerSecond.put(secondGenerated);
|
||||||
|
|
||||||
|
if(minuteLatch.flip())
|
||||||
|
{
|
||||||
|
int minuteGenerated = generated.get() - generatedLastMinute.get();
|
||||||
|
generatedLastMinute.set(generated.get());
|
||||||
|
chunksPerMinute.put(minuteGenerated);
|
||||||
|
regionsPerMinute.put((double)minuteGenerated / 1024D);
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.onTick(chunksPerSecond.getAverage(), chunksPerMinute.getAverage(),
|
||||||
|
regionsPerMinute.getAverage(),
|
||||||
|
(double)generated.get() / (double)totalChunks.get(),
|
||||||
|
generated.get(), totalChunks.get(),
|
||||||
|
totalChunks.get() - generated.get(),
|
||||||
|
eta, M.ms() - startTime.get(), currentGeneratorMethod.get());
|
||||||
|
return 1000;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private long computeETA() {
|
||||||
|
return (long) ((totalChunks.get() - generated.get()) *
|
||||||
|
((double) (M.ms() - startTime.get()) / (double) generated.get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
shutdown.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start()
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
task.iterateRegions((__, ___) -> totalChunks.addAndGet(1024));
|
||||||
|
ticker.start();
|
||||||
|
task.iterateRegions(this::visitRegion);
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
generator.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shutdown() {
|
||||||
|
listener.onSaving();
|
||||||
|
generator.close();
|
||||||
|
ticker.interrupt();
|
||||||
|
listener.onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void visitRegion(int x, int z) {
|
||||||
|
while(paused.get() && !shutdown.get())
|
||||||
|
{
|
||||||
|
J.sleep(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(shutdown.get())
|
||||||
|
{
|
||||||
|
listener.onRegionSkipped(x, z);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.onRegionGenerating(x, z);
|
||||||
|
currentGeneratorMethod.set(generator.getMethod(x, z));
|
||||||
|
|
||||||
|
if(generator.supportsRegions(x, z))
|
||||||
|
{
|
||||||
|
generator.generateRegion(x, z, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
task.iterateRegion(x, z, (xx, zz) -> {
|
||||||
|
listener.onChunkGenerating(xx, zz);
|
||||||
|
generator.generateChunk(xx, zz);
|
||||||
|
listener.onChunkGenerated(xx, zz);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.onRegionGenerated(x, z);
|
||||||
|
listener.onSaving();
|
||||||
|
generator.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pause()
|
||||||
|
{
|
||||||
|
paused.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resume()
|
||||||
|
{
|
||||||
|
paused.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PregenListener listenify(PregenListener listener) {
|
||||||
|
return new PregenListener() {
|
||||||
|
@Override
|
||||||
|
public void onTick(double chunksPerSecond, double chunksPerMinute, double regionsPerMinute, double percent, int generated, int totalChunks, int chunksRemaining, long eta, long elapsed, String method) {
|
||||||
|
listener.onTick( chunksPerSecond, chunksPerMinute, regionsPerMinute, percent, generated, totalChunks, chunksRemaining, eta, elapsed, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChunkGenerating(int x, int z) {
|
||||||
|
listener.onChunkGenerating(x, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChunkGenerated(int x, int z) {
|
||||||
|
listener.onChunkGenerated(x, z);
|
||||||
|
generated.addAndGet(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRegionGenerated(int x, int z) {
|
||||||
|
listener.onRegionGenerated(x, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRegionGenerating(int x, int z) {
|
||||||
|
listener.onRegionGenerating(x, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRegionSkipped(int x, int z) {
|
||||||
|
listener.onRegionSkipped(x, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClose() {
|
||||||
|
listener.onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaving() {
|
||||||
|
listener.onSaving();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean paused() {
|
||||||
|
return paused.get();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user