2021-05-02 14:51:42 +02:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2021-04-24 22:11:26 +02:00
|
|
|
package de.bluecolored.bluemap.common.rendermanager;
|
|
|
|
|
|
|
|
import com.flowpowered.math.vector.Vector2i;
|
2021-05-08 23:47:13 +02:00
|
|
|
import de.bluecolored.bluemap.core.logger.Logger;
|
2021-04-24 22:11:26 +02:00
|
|
|
import de.bluecolored.bluemap.core.map.BmMap;
|
|
|
|
import de.bluecolored.bluemap.core.world.Grid;
|
|
|
|
import de.bluecolored.bluemap.core.world.Region;
|
2021-05-08 23:47:13 +02:00
|
|
|
import de.bluecolored.bluemap.core.world.World;
|
2021-04-24 22:11:26 +02:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.TreeSet;
|
|
|
|
|
|
|
|
public class WorldRegionRenderTask implements RenderTask {
|
|
|
|
|
|
|
|
private final BmMap map;
|
|
|
|
private final Vector2i worldRegion;
|
|
|
|
private final boolean force;
|
|
|
|
|
|
|
|
private TreeSet<Vector2i> tiles;
|
|
|
|
private int tileCount;
|
|
|
|
private long startTime;
|
|
|
|
|
|
|
|
private volatile int atWork;
|
|
|
|
private volatile boolean cancelled;
|
|
|
|
|
|
|
|
public WorldRegionRenderTask(BmMap map, Vector2i worldRegion) {
|
|
|
|
this(map, worldRegion, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public WorldRegionRenderTask(BmMap map, Vector2i worldRegion, boolean force) {
|
|
|
|
this.map = map;
|
|
|
|
this.worldRegion = worldRegion;
|
|
|
|
this.force = force;
|
|
|
|
|
|
|
|
this.tiles = null;
|
|
|
|
this.tileCount = -1;
|
|
|
|
this.startTime = -1;
|
|
|
|
|
|
|
|
this.atWork = 0;
|
|
|
|
this.cancelled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void init() {
|
2021-05-02 14:51:42 +02:00
|
|
|
tiles = new TreeSet<>(WorldRegionRenderTask::tileComparator);
|
2021-04-24 22:11:26 +02:00
|
|
|
startTime = System.currentTimeMillis();
|
|
|
|
|
2021-05-08 23:47:13 +02:00
|
|
|
//Logger.global.logInfo("Starting: " + worldRegion);
|
|
|
|
|
2021-04-24 22:11:26 +02:00
|
|
|
long changesSince = 0;
|
|
|
|
if (!force) changesSince = map.getRenderState().getRenderTime(worldRegion);
|
|
|
|
|
|
|
|
Region region = map.getWorld().getRegion(worldRegion.getX(), worldRegion.getY());
|
|
|
|
Collection<Vector2i> chunks = region.listChunks(changesSince);
|
|
|
|
|
|
|
|
Grid tileGrid = map.getHiresModelManager().getTileGrid();
|
|
|
|
Grid chunkGrid = map.getWorld().getChunkGrid();
|
|
|
|
|
|
|
|
for (Vector2i chunk : chunks) {
|
|
|
|
Vector2i tileMin = chunkGrid.getCellMin(chunk, tileGrid);
|
|
|
|
Vector2i tileMax = chunkGrid.getCellMax(chunk, tileGrid);
|
|
|
|
|
2021-05-02 14:51:42 +02:00
|
|
|
for (int x = tileMin.getX(); x <= tileMax.getX(); x++) {
|
|
|
|
for (int z = tileMin.getY(); z <= tileMax.getY(); z++) {
|
2021-04-24 22:11:26 +02:00
|
|
|
tiles.add(new Vector2i(x, z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tileCount = tiles.size();
|
|
|
|
|
|
|
|
if (tiles.isEmpty()) complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doWork() {
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
|
|
Vector2i tile;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
if (tiles == null) init();
|
|
|
|
if (tiles.isEmpty()) return;
|
|
|
|
|
|
|
|
tile = tiles.pollFirst();
|
|
|
|
|
|
|
|
this.atWork++;
|
|
|
|
}
|
|
|
|
|
2021-05-08 23:47:13 +02:00
|
|
|
//Logger.global.logInfo("Working on " + worldRegion + " - Tile " + tile);
|
2021-04-24 22:11:26 +02:00
|
|
|
map.renderTile(tile); // <- actual work
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
this.atWork--;
|
|
|
|
|
|
|
|
if (atWork <= 0 && tiles.isEmpty() && !cancelled) {
|
|
|
|
complete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void complete() {
|
|
|
|
map.getRenderState().setRenderTime(worldRegion, startTime);
|
2021-05-08 23:47:13 +02:00
|
|
|
|
|
|
|
//Logger.global.logInfo("Done with: " + worldRegion);
|
2021-04-24 22:11:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-05-08 23:47:13 +02:00
|
|
|
public synchronized boolean hasMoreWork() {
|
2021-05-02 14:51:42 +02:00
|
|
|
return !cancelled && (tiles == null || !tiles.isEmpty());
|
2021-04-24 22:11:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double estimateProgress() {
|
|
|
|
if (tiles == null) return 0;
|
|
|
|
if (tileCount == 0) return 1;
|
|
|
|
|
|
|
|
double remainingTiles = tiles.size();
|
2021-05-02 14:51:42 +02:00
|
|
|
return 1 - (remainingTiles / this.tileCount);
|
2021-04-24 22:11:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void cancel() {
|
|
|
|
this.cancelled = true;
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
if (tiles != null) this.tiles.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 23:47:13 +02:00
|
|
|
public BmMap getMap() {
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2i getWorldRegion() {
|
|
|
|
return worldRegion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isForce() {
|
|
|
|
return force;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
if (force) {
|
|
|
|
return "Render region " + getWorldRegion() + " for map '" + map.getId() + "'";
|
|
|
|
} else {
|
|
|
|
return "Update region " + getWorldRegion() + " for map '" + map.getId() + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 22:11:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
WorldRegionRenderTask that = (WorldRegionRenderTask) o;
|
|
|
|
return force == that.force && map.equals(that.map) && worldRegion.equals(that.worldRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return worldRegion.hashCode();
|
|
|
|
}
|
|
|
|
|
2021-05-02 14:51:42 +02:00
|
|
|
private static int tileComparator(Vector2i v1, Vector2i v2) {
|
|
|
|
int comp = v1.compareTo(v2);
|
|
|
|
if (comp != 0) return comp;
|
|
|
|
if (v1.getX() != v2.getX()) return v2.getX() - v1.getX();
|
|
|
|
return v2.getY() - v1.getY();
|
|
|
|
}
|
|
|
|
|
2021-05-08 23:47:13 +02:00
|
|
|
public static int compare(WorldRegionRenderTask task1, WorldRegionRenderTask task2) {
|
|
|
|
if (task1.equals(task2)) return 0;
|
|
|
|
|
|
|
|
int comp = task1.getMap().getId().compareTo(task2.getMap().getId());
|
|
|
|
if (comp != 0) return comp;
|
|
|
|
|
|
|
|
//sort based on the worlds spawn-point
|
|
|
|
World world = task1.getMap().getWorld();
|
|
|
|
Vector2i spawnPoint = world.getSpawnPoint().toVector2(true);
|
|
|
|
Grid regionGrid = world.getRegionGrid();
|
|
|
|
Vector2i spawnRegion = regionGrid.getCell(spawnPoint);
|
|
|
|
|
|
|
|
Vector2i task1Rel = task1.getWorldRegion().sub(spawnRegion);
|
|
|
|
Vector2i task2Rel = task2.getWorldRegion().sub(spawnRegion);
|
|
|
|
|
|
|
|
comp = tileComparator(task1Rel, task2Rel);
|
|
|
|
if (comp != 0) return comp;
|
|
|
|
|
|
|
|
if (task1.isForce() == task2.isForce()) return 0;
|
|
|
|
if (task1.isForce()) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-24 22:11:26 +02:00
|
|
|
}
|