-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTileManager.java
More file actions
536 lines (469 loc) · 22.5 KB
/
Copy pathTileManager.java
File metadata and controls
536 lines (469 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package com.easywebmap.map;
import com.easywebmap.EasyWebMap;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.universe.Universe;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.storage.IChunkLoader;
import com.hypixel.hytale.server.core.universe.world.worldmap.WorldMapManager;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.math.vector.Transform;
import org.joml.Vector3d;
import it.unimi.dsi.fastutil.longs.LongCollection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
public class TileManager {
private final EasyWebMap plugin;
private final TileCache memoryCache;
private final DiskTileCache diskCache;
private final ConcurrentHashMap<String, CompletableFuture<byte[]>> pendingRequests;
private final ConcurrentHashMap<String, CompletableFuture<PngEncoder.TileData>> pendingPixelRequests;
private final ConcurrentHashMap<String, CachedChunkIndexes> chunkIndexCache;
private final ConcurrentHashMap<String, PngEncoder.TileData> pixelCache;
private CompositeTileGenerator compositeTileGenerator;
// Increased from 512 to 2048 to prevent cache thrashing at negative zoom levels
// (A single zoom -4 tile requires 256 base tile pixels)
private static final int MAX_PIXEL_CACHE = 2048;
// Limit concurrent tile generations to prevent CPU spikes
// Increased from 4 to 16 for faster composite tile generation at negative zoom levels
private static final int MAX_CONCURRENT_GENERATIONS = 16;
private final Semaphore generationSemaphore = new Semaphore(MAX_CONCURRENT_GENERATIONS);
// Empty tiles are ~270 bytes, real tiles are 10KB+
private static final int EMPTY_TILE_THRESHOLD = 500;
public TileManager(EasyWebMap plugin) {
this.plugin = plugin;
this.memoryCache = new TileCache(plugin.getConfig().getTileCacheSize());
this.diskCache = new DiskTileCache(plugin.getDataDirectory());
this.pendingRequests = new ConcurrentHashMap<>();
this.pendingPixelRequests = new ConcurrentHashMap<>();
this.chunkIndexCache = new ConcurrentHashMap<>();
this.pixelCache = new ConcurrentHashMap<>();
this.compositeTileGenerator = new CompositeTileGenerator(plugin, this);
}
public CompletableFuture<byte[]> getTile(String worldName, int zoom, int tileX, int tileZ) {
// Route negative zoom levels to composite tile generator
if (zoom < 0 && this.plugin.getConfig().isEnableTilePyramids()) {
return this.getCompositeTile(worldName, zoom, tileX, tileZ);
}
// For zoom >= 0, use base tile logic
return this.getBaseTile(worldName, tileX, tileZ);
}
/**
* Get a composite tile at a negative zoom level.
* Composite tiles combine multiple base tiles into one.
*/
private CompletableFuture<byte[]> getCompositeTile(String worldName, int zoom, int tileX, int tileZ) {
String cacheKey = TileCache.createKey(worldName, zoom, tileX, tileZ);
// 1. Check memory cache first
byte[] memoryCached = this.memoryCache.get(cacheKey);
if (memoryCached != null) {
return CompletableFuture.completedFuture(memoryCached);
}
// 2. Check if already generating
CompletableFuture<byte[]> pending = this.pendingRequests.get(cacheKey);
if (pending != null) {
return pending;
}
// 3. Check disk cache
if (this.plugin.getConfig().isUseDiskCache()) {
byte[] diskCached = this.diskCache.get(worldName, zoom, tileX, tileZ);
// Skip stale empty/black placeholders (e.g. cached before a fix) so
// they fall through to regeneration instead of being served forever.
if (diskCached != null && !PngEncoder.isEmptyTile(diskCached, this.plugin.getConfig().getTileSize())) {
long tileAge = this.diskCache.getTileAge(worldName, zoom, tileX, tileZ);
// Use longer refresh interval for composite tiles (they're more expensive)
long refreshInterval = this.plugin.getConfig().getTileRefreshIntervalMs() * 2;
if (tileAge < refreshInterval) {
this.memoryCache.put(cacheKey, diskCached);
return CompletableFuture.completedFuture(diskCached);
}
// Check if any players are in the area covered by this composite tile
int chunksPerAxis = this.compositeTileGenerator.getChunksPerAxis(zoom);
int baseChunkX = tileX * chunksPerAxis;
int baseChunkZ = tileZ * chunksPerAxis;
World world = Universe.get().getWorld(worldName);
if (world == null || !this.arePlayersInArea(world, baseChunkX, baseChunkZ, chunksPerAxis)) {
this.memoryCache.put(cacheKey, diskCached);
return CompletableFuture.completedFuture(diskCached);
}
}
}
// 4. Generate composite tile
CompletableFuture<byte[]> future = this.compositeTileGenerator.generateCompositeTile(worldName, zoom, tileX, tileZ);
this.pendingRequests.put(cacheKey, future);
future.whenComplete((data, ex) -> {
this.pendingRequests.remove(cacheKey);
if (data != null && ex == null && !PngEncoder.isEmptyTile(data, this.plugin.getConfig().getTileSize())) {
this.memoryCache.put(cacheKey, data);
if (this.plugin.getConfig().isUseDiskCache()) {
this.diskCache.putAsync(worldName, zoom, tileX, tileZ, data);
}
}
});
return future;
}
/**
* Get a base tile (zoom level 0) for a single chunk.
* This is called by the composite tile generator.
*/
public CompletableFuture<byte[]> getBaseTile(String worldName, int tileX, int tileZ) {
String cacheKey = TileCache.createKey(worldName, 0, tileX, tileZ);
// 1. Check memory cache first (fastest)
byte[] memoryCached = this.memoryCache.get(cacheKey);
if (memoryCached != null) {
return CompletableFuture.completedFuture(memoryCached);
}
// 2. Check if already generating
CompletableFuture<byte[]> pending = this.pendingRequests.get(cacheKey);
if (pending != null) {
return pending;
}
// 3. Check disk cache if enabled
if (this.plugin.getConfig().isUseDiskCache()) {
byte[] diskCached = this.diskCache.get(worldName, 0, tileX, tileZ);
// Skip stale empty/black placeholders so they regenerate (self-heal).
if (diskCached != null && !PngEncoder.isEmptyTile(diskCached, this.plugin.getConfig().getTileSize())) {
long tileAge = this.diskCache.getTileAge(worldName, 0, tileX, tileZ);
long refreshInterval = this.plugin.getConfig().getTileRefreshIntervalMs();
// If tile is fresh enough, serve from disk
if (tileAge < refreshInterval) {
this.memoryCache.put(cacheKey, diskCached);
return CompletableFuture.completedFuture(diskCached);
}
// Tile is old - check if players are nearby
World world = Universe.get().getWorld(worldName);
if (world == null || !this.arePlayersNearby(world, tileX, tileZ)) {
// No players nearby - terrain can't have changed, serve cached
this.memoryCache.put(cacheKey, diskCached);
return CompletableFuture.completedFuture(diskCached);
}
// Players nearby and tile is old - regenerate
}
}
// 4. Generate new tile
CompletableFuture<byte[]> future = this.generateTile(worldName, 0, tileX, tileZ);
this.pendingRequests.put(cacheKey, future);
future.whenComplete((data, ex) -> {
this.pendingRequests.remove(cacheKey);
// Don't cache empty tiles - they should regenerate when chunk gets explored
if (data != null && ex == null && !PngEncoder.isEmptyTile(data, this.plugin.getConfig().getTileSize())) {
this.memoryCache.put(cacheKey, data);
if (this.plugin.getConfig().isUseDiskCache()) {
this.diskCache.putAsync(worldName, 0, tileX, tileZ, data);
}
}
});
return future;
}
/**
* Get a base tile with raw pixels for compositing.
* Returns TileData containing both PNG bytes and raw ARGB pixels.
*/
public CompletableFuture<PngEncoder.TileData> getBaseTileWithPixels(String worldName, int tileX, int tileZ) {
String cacheKey = TileCache.createKey(worldName, 0, tileX, tileZ);
int tileSize = this.plugin.getConfig().getTileSize();
// 1. Check pixel cache
PngEncoder.TileData cached = this.pixelCache.get(cacheKey);
if (cached != null) {
return CompletableFuture.completedFuture(cached);
}
// 2. Check pending
CompletableFuture<PngEncoder.TileData> pending = this.pendingPixelRequests.get(cacheKey);
if (pending != null) {
return pending;
}
// 3. Check disk cache and decode PNG to pixels (faster than regenerating)
if (this.plugin.getConfig().isUseDiskCache()) {
byte[] diskCached = this.diskCache.get(worldName, 0, tileX, tileZ);
if (diskCached != null && !PngEncoder.isEmptyTile(diskCached, tileSize)) {
try {
int[] pixels = PngDecoder.decode(diskCached, tileSize);
if (pixels != null) {
PngEncoder.TileData tileData = new PngEncoder.TileData(diskCached, pixels, tileSize);
// Cache the pixels for future compositing
if (this.pixelCache.size() < MAX_PIXEL_CACHE) {
this.pixelCache.put(cacheKey, tileData);
}
return CompletableFuture.completedFuture(tileData);
}
} catch (Exception e) {
// Failed to decode, will regenerate
}
}
}
// 4. Generate with pixels
CompletableFuture<PngEncoder.TileData> future = this.generateTileWithPixels(worldName, tileX, tileZ);
this.pendingPixelRequests.put(cacheKey, future);
future.whenComplete((data, ex) -> {
this.pendingPixelRequests.remove(cacheKey);
// Don't cache empty tiles - they should regenerate when chunk gets explored
if (data != null && ex == null && !data.isEmpty()) {
// Cache pixels for compositing, evict if too many
if (this.pixelCache.size() < MAX_PIXEL_CACHE) {
this.pixelCache.put(cacheKey, data);
}
// Also cache PNG bytes
this.memoryCache.put(cacheKey, data.pngBytes);
if (this.plugin.getConfig().isUseDiskCache()) {
this.diskCache.putAsync(worldName, 0, tileX, tileZ, data.pngBytes);
}
}
});
return future;
}
private CompletableFuture<PngEncoder.TileData> generateTileWithPixels(String worldName, int tileX, int tileZ) {
World world = Universe.get().getWorld(worldName);
int tileSize = this.plugin.getConfig().getTileSize();
if (world == null) {
return CompletableFuture.completedFuture(new PngEncoder.TileData(
PngEncoder.encodeEmpty(tileSize), new int[0], tileSize));
}
if (this.plugin.getConfig().isRenderExploredChunksOnly()) {
if (!this.isChunkExplored(world, tileX, tileZ)) {
return CompletableFuture.completedFuture(new PngEncoder.TileData(
PngEncoder.encodeEmpty(tileSize), new int[0], tileSize));
}
}
WorldMapManager mapManager = world.getWorldMapManager();
// Acquire semaphore to limit concurrent generations
try {
this.generationSemaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.completedFuture(new PngEncoder.TileData(
PngEncoder.encodeEmpty(tileSize), new int[0], tileSize));
}
return mapManager.getImageAsync(tileX, tileZ)
.thenApply(mapImage -> {
if (mapImage == null) {
return new PngEncoder.TileData(PngEncoder.encodeEmpty(tileSize), new int[0], tileSize);
}
return PngEncoder.encodeWithPixels(mapImage, tileSize);
})
.exceptionally(ex -> {
System.err.println("[EasyWebMap] Failed to generate tile: " + ex.getMessage());
return new PngEncoder.TileData(PngEncoder.encodeEmpty(tileSize), new int[0], tileSize);
})
.whenComplete((result, ex) -> this.generationSemaphore.release());
}
private boolean arePlayersNearby(World world, int tileX, int tileZ) {
int radius = this.plugin.getConfig().getTileRefreshRadius();
for (PlayerRef playerRef : world.getPlayerRefs()) {
try {
Transform transform = playerRef.getTransform();
if (transform == null) continue;
Vector3d pos = transform.getPosition();
int playerChunkX = ChunkUtil.chunkCoordinate((int) pos.x);
int playerChunkZ = ChunkUtil.chunkCoordinate((int) pos.z);
int dx = Math.abs(playerChunkX - tileX);
int dz = Math.abs(playerChunkZ - tileZ);
if (dx <= radius && dz <= radius) {
return true;
}
} catch (Exception e) {
// Skip this player
}
}
return false;
}
/**
* Check if any players are within the area covered by a composite tile.
*/
private boolean arePlayersInArea(World world, int baseChunkX, int baseChunkZ, int chunksPerAxis) {
int radius = this.plugin.getConfig().getTileRefreshRadius();
for (PlayerRef playerRef : world.getPlayerRefs()) {
try {
Transform transform = playerRef.getTransform();
if (transform == null) continue;
Vector3d pos = transform.getPosition();
int playerChunkX = ChunkUtil.chunkCoordinate((int) pos.x);
int playerChunkZ = ChunkUtil.chunkCoordinate((int) pos.z);
// Check if player is within the area (with buffer for refresh radius)
if (playerChunkX >= baseChunkX - radius &&
playerChunkX < baseChunkX + chunksPerAxis + radius &&
playerChunkZ >= baseChunkZ - radius &&
playerChunkZ < baseChunkZ + chunksPerAxis + radius) {
return true;
}
} catch (Exception e) {
// Skip this player
}
}
return false;
}
private CompletableFuture<byte[]> generateTile(String worldName, int zoom, int tileX, int tileZ) {
World world = Universe.get().getWorld(worldName);
if (world == null) {
return CompletableFuture.completedFuture(PngEncoder.encodeEmpty(this.plugin.getConfig().getTileSize()));
}
// Check if we should only render explored chunks
if (this.plugin.getConfig().isRenderExploredChunksOnly()) {
if (!this.isChunkExplored(world, tileX, tileZ)) {
return CompletableFuture.completedFuture(PngEncoder.encodeEmpty(this.plugin.getConfig().getTileSize()));
}
}
WorldMapManager mapManager = world.getWorldMapManager();
// Acquire semaphore to limit concurrent generations
try {
this.generationSemaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return CompletableFuture.completedFuture(PngEncoder.encodeEmpty(this.plugin.getConfig().getTileSize()));
}
return mapManager.getImageAsync(tileX, tileZ)
.thenApply(mapImage -> {
if (mapImage == null) {
return PngEncoder.encodeEmpty(this.plugin.getConfig().getTileSize());
}
return PngEncoder.encode(mapImage, this.plugin.getConfig().getTileSize());
})
.exceptionally(ex -> {
System.err.println("[EasyWebMap] Failed to generate tile: " + ex.getMessage());
return PngEncoder.encodeEmpty(this.plugin.getConfig().getTileSize());
})
.whenComplete((result, ex) -> this.generationSemaphore.release());
}
private boolean isChunkExplored(World world, int chunkX, int chunkZ) {
try {
LongCollection indexes = this.getCachedChunkIndexes(world);
if (indexes == null) {
return true; // Fail open if we can't get indexes
}
long chunkIndex = ChunkUtil.indexChunk(chunkX, chunkZ);
return indexes.contains(chunkIndex);
} catch (Exception e) {
// If we can't check, allow rendering (fail open for usability)
return true;
}
}
/**
* Check if any chunks in the given area are explored.
* Used for early bail-out on composite tiles over unexplored areas.
*/
public boolean hasAnyExploredChunks(String worldName, int baseChunkX, int baseChunkZ, int chunksPerAxis) {
if (!this.plugin.getConfig().isRenderExploredChunksOnly()) {
return true; // If we're not filtering, assume there's content
}
World world = Universe.get().getWorld(worldName);
if (world == null) {
return false;
}
try {
LongCollection indexes = this.getCachedChunkIndexes(world);
if (indexes == null) {
return true; // Fail open
}
// Quick sampling - check corners and center first
int[][] samplePoints = {
{0, 0}, // top-left
{chunksPerAxis - 1, 0}, // top-right
{0, chunksPerAxis - 1}, // bottom-left
{chunksPerAxis - 1, chunksPerAxis - 1}, // bottom-right
{chunksPerAxis / 2, chunksPerAxis / 2} // center
};
for (int[] point : samplePoints) {
long idx = ChunkUtil.indexChunk(baseChunkX + point[0], baseChunkZ + point[1]);
if (indexes.contains(idx)) {
return true;
}
}
// If samples show nothing, do a full scan (but this is rare)
for (int dz = 0; dz < chunksPerAxis; dz++) {
for (int dx = 0; dx < chunksPerAxis; dx++) {
long idx = ChunkUtil.indexChunk(baseChunkX + dx, baseChunkZ + dz);
if (indexes.contains(idx)) {
return true;
}
}
}
return false;
} catch (Exception e) {
return true; // Fail open
}
}
private LongCollection getCachedChunkIndexes(World world) {
String worldName = world.getName();
CachedChunkIndexes cached = this.chunkIndexCache.get(worldName);
long now = System.currentTimeMillis();
if (cached != null && (now - cached.timestamp) < this.plugin.getConfig().getChunkIndexCacheMs()) {
return cached.indexes;
}
// Refresh the cache
try {
ChunkStore chunkStore = world.getChunkStore();
IChunkLoader loader = chunkStore.getLoader();
if (loader == null) {
return null;
}
LongCollection indexes = loader.getIndexes();
this.chunkIndexCache.put(worldName, new CachedChunkIndexes(indexes, now));
return indexes;
} catch (Exception e) {
return cached != null ? cached.indexes : null;
}
}
public CompletableFuture<Integer> pregenerateTiles(String worldName, int centerX, int centerZ, int radius) {
World world = Universe.get().getWorld(worldName);
if (world == null) {
return CompletableFuture.completedFuture(0);
}
return CompletableFuture.supplyAsync(() -> {
int count = 0;
for (int x = centerX - radius; x <= centerX + radius; x++) {
for (int z = centerZ - radius; z <= centerZ + radius; z++) {
// Skip if already cached on disk
if (this.diskCache.exists(worldName, 0, x, z)) {
continue;
}
// Skip unexplored chunks
if (this.plugin.getConfig().isRenderExploredChunksOnly()) {
if (!this.isChunkExplored(world, x, z)) {
continue;
}
}
try {
// Generate and wait
byte[] tile = this.generateTile(worldName, 0, x, z).join();
if (tile != null && tile.length > 100) {
this.diskCache.put(worldName, 0, x, z, tile);
count++;
}
// Small delay to not overload
Thread.sleep(50);
} catch (Exception e) {
// Continue with next tile
}
}
}
return count;
});
}
public void clearCache() {
this.memoryCache.clear();
this.pixelCache.clear();
this.diskCache.clear();
this.chunkIndexCache.clear();
}
public void clearMemoryCache() {
this.memoryCache.clear();
this.pixelCache.clear();
}
public void shutdown() {
this.diskCache.shutdown();
}
public int getMemoryCacheSize() {
return this.memoryCache.size();
}
public DiskTileCache getDiskCache() {
return this.diskCache;
}
private static class CachedChunkIndexes {
final LongCollection indexes;
final long timestamp;
CachedChunkIndexes(LongCollection indexes, long timestamp) {
this.indexes = indexes;
this.timestamp = timestamp;
}
}
}