|
| 1 | +/* |
| 2 | + * This file is part of BlueMap, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package de.bluecolored.bluemap.common.rendermanager; |
| 26 | + |
| 27 | +import com.flowpowered.math.vector.Vector2i; |
| 28 | +import de.bluecolored.bluemap.core.logger.Logger; |
| 29 | +import de.bluecolored.bluemap.core.map.BmMap; |
| 30 | +import de.bluecolored.bluemap.core.map.renderstate.MapTileState; |
| 31 | +import de.bluecolored.bluemap.core.map.renderstate.TileInfoRegion; |
| 32 | +import de.bluecolored.bluemap.core.map.renderstate.TileState; |
| 33 | +import de.bluecolored.bluemap.core.storage.GridStorage; |
| 34 | +import de.bluecolored.bluemap.core.storage.compression.CompressedInputStream; |
| 35 | +import de.bluecolored.bluemap.core.util.Grid; |
| 36 | +import de.bluecolored.bluemap.core.world.World; |
| 37 | +import lombok.Builder; |
| 38 | +import lombok.Getter; |
| 39 | +import lombok.NonNull; |
| 40 | +import org.jetbrains.annotations.Nullable; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Collection; |
| 45 | +import java.util.HashSet; |
| 46 | +import java.util.Set; |
| 47 | +import java.util.function.Consumer; |
| 48 | +import java.util.function.Predicate; |
| 49 | +import java.util.stream.Stream; |
| 50 | + |
| 51 | +public class MapUpdatePreparationTask implements MapRenderTask { |
| 52 | + |
| 53 | + @Getter private final BmMap map; |
| 54 | + private final @Nullable Vector2i center; |
| 55 | + private final @Nullable Integer radius; |
| 56 | + private final TileUpdateStrategy force; |
| 57 | + private final Consumer<MapUpdateTask> taskConsumer; |
| 58 | + |
| 59 | + private volatile boolean hasMoreWork, cancelled; |
| 60 | + |
| 61 | + @Builder |
| 62 | + protected MapUpdatePreparationTask( |
| 63 | + @NonNull BmMap map, |
| 64 | + @Nullable Vector2i center, |
| 65 | + @Nullable Integer radius, |
| 66 | + TileUpdateStrategy force, |
| 67 | + @NonNull Consumer<MapUpdateTask> taskConsumer |
| 68 | + ) { |
| 69 | + this.map = map; |
| 70 | + this.center = center; |
| 71 | + this.radius = radius; |
| 72 | + this.force = force != null ? force : TileUpdateStrategy.FORCE_NONE; |
| 73 | + this.taskConsumer = taskConsumer; |
| 74 | + this.hasMoreWork = true; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void doWork() { |
| 79 | + synchronized (this) { |
| 80 | + if (!hasMoreWork) return; |
| 81 | + hasMoreWork = false; |
| 82 | + } |
| 83 | + if (cancelled) return; |
| 84 | + |
| 85 | + // do work |
| 86 | + Collection<Vector2i> regions = findRegions(); |
| 87 | + Collection<RenderTask> tasks = createTasks(regions); |
| 88 | + MapUpdateTask mapUpdateTask = new MapUpdateTask(map, tasks); |
| 89 | + |
| 90 | + if (cancelled) return; |
| 91 | + |
| 92 | + // return created task |
| 93 | + taskConsumer.accept(mapUpdateTask); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean hasMoreWork() { |
| 98 | + return hasMoreWork && !cancelled; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void cancel() { |
| 103 | + cancelled = true; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getDescription() { |
| 108 | + return "preparing map '%s' update".formatted(map.getId()); |
| 109 | + } |
| 110 | + |
| 111 | + private Collection<RenderTask> createTasks(Collection<Vector2i> regions) { |
| 112 | + ArrayList<WorldRegionRenderTask> regionTasks = new ArrayList<>(regions.size()); |
| 113 | + regions.forEach(region -> regionTasks.add(new WorldRegionRenderTask(map, region, force))); |
| 114 | + |
| 115 | + // get spawn region |
| 116 | + World world = map.getWorld(); |
| 117 | + Vector2i spawnPoint = world.getSpawnPoint().toVector2(true); |
| 118 | + Grid regionGrid = world.getRegionGrid(); |
| 119 | + Vector2i spawnRegion = regionGrid.getCell(spawnPoint); |
| 120 | + |
| 121 | + // sort tasks by distance to the spawn region |
| 122 | + regionTasks.sort(WorldRegionRenderTask.defaultComparator(spawnRegion)); |
| 123 | + |
| 124 | + // save map before and after the whole update |
| 125 | + ArrayList<RenderTask> tasks = new ArrayList<>(regionTasks.size() + 2); |
| 126 | + tasks.add(new MapSaveTask(map)); |
| 127 | + tasks.addAll(regionTasks); |
| 128 | + tasks.add(new MapSaveTask(map)); |
| 129 | + |
| 130 | + return tasks; |
| 131 | + } |
| 132 | + |
| 133 | + private Collection<Vector2i> findRegions() { |
| 134 | + World world = map.getWorld(); |
| 135 | + Grid regionGrid = world.getRegionGrid(); |
| 136 | + |
| 137 | + Predicate<Vector2i> regionBoundsFilter = map.getMapSettings().getCellRenderBoundariesFilter(regionGrid, true); |
| 138 | + Predicate<Vector2i> regionRadiusFilter; |
| 139 | + if (center == null || radius == null || radius < 0) { |
| 140 | + regionRadiusFilter = r -> true; |
| 141 | + } else { |
| 142 | + Vector2i halfCell = regionGrid.getGridSize().div(2); |
| 143 | + long increasedRadiusSquared = (long) Math.pow(radius + Math.ceil(halfCell.length()), 2); |
| 144 | + regionRadiusFilter = r -> { |
| 145 | + Vector2i min = regionGrid.getCellMin(r); |
| 146 | + Vector2i regionCenter = min.add(halfCell); |
| 147 | + return regionCenter.toLong().distanceSquared(center.toLong()) <= increasedRadiusSquared; |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + Set<Vector2i> regions = new HashSet<>(); |
| 152 | + |
| 153 | + // update all regions in the world-files |
| 154 | + world.listRegions().stream() |
| 155 | + .filter(regionBoundsFilter) |
| 156 | + .filter(regionRadiusFilter) |
| 157 | + .forEach(regions::add); |
| 158 | + |
| 159 | + // also update regions that are present as map-tile-state files (they might have been rendered before but deleted now) |
| 160 | + // (a little hacky as we are operating on raw tile-state files -> maybe find a better way?) |
| 161 | + if (map.getMapSettings().isCheckForRemovedRegions()) { |
| 162 | + Grid tileGrid = map.getHiresModelManager().getTileGrid(); |
| 163 | + Grid cellGrid = MapTileState.GRID.multiply(tileGrid); |
| 164 | + try (Stream<GridStorage.Cell> stream = map.getStorage().tileState().stream()) { |
| 165 | + stream |
| 166 | + .filter(c -> { |
| 167 | + // filter out files that are fully UNKNOWN/NOT_GENERATED |
| 168 | + // this avoids unnecessarily converting UNKNOWN tiles into NOT_GENERATED tiles on force-updates |
| 169 | + try (CompressedInputStream in = c.read()) { |
| 170 | + if (in == null) return false; |
| 171 | + TileState[] states = TileInfoRegion.loadPalette(in.decompress()); |
| 172 | + for (TileState state : states) { |
| 173 | + if ( |
| 174 | + state != TileState.UNKNOWN && |
| 175 | + state != TileState.NOT_GENERATED |
| 176 | + ) return true; |
| 177 | + } |
| 178 | + return false; |
| 179 | + } catch (IOException ignore) { |
| 180 | + return true; |
| 181 | + } |
| 182 | + }) |
| 183 | + .map(c -> new Vector2i(c.getX(), c.getZ())) |
| 184 | + .flatMap(v -> cellGrid.getIntersecting(v, regionGrid).stream()) |
| 185 | + .filter(regionRadiusFilter) |
| 186 | + .forEach(regions::add); |
| 187 | + } catch (IOException ex) { |
| 188 | + Logger.global.logError("Failed to load map tile state!", ex); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return regions; |
| 193 | + } |
| 194 | + |
| 195 | + public static MapUpdatePreparationTask updateMap(BmMap map, RenderManager renderManager) { |
| 196 | + return builder() |
| 197 | + .map(map) |
| 198 | + .taskConsumer(renderManager::scheduleRenderTask) |
| 199 | + .build(); |
| 200 | + } |
| 201 | + |
| 202 | + public static MapUpdatePreparationTask updateMap(BmMap map, TileUpdateStrategy force, RenderManager renderManager) { |
| 203 | + return builder() |
| 204 | + .map(map) |
| 205 | + .force(force) |
| 206 | + .taskConsumer(renderManager::scheduleRenderTask) |
| 207 | + .build(); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments