Files
Server/src/engine/util/MapLoader.java
T

146 lines
4.5 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
/*
* Copyright MagicBane 2013
*/
package engine.util;
import engine.InterestManagement.RealmMap;
2023-10-08 09:49:49 -04:00
import engine.InterestManagement.Terrain;
2023-06-07 14:53:15 -04:00
import engine.gameManager.ConfigManager;
2022-04-30 09:41:17 -04:00
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
2023-10-08 09:49:49 -04:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
2022-04-30 09:41:17 -04:00
public enum MapLoader {
MAPLOADER;
public static int[][] loadMap() {
BufferedImage image;
int[][] realmMap;
long timeToLoad = System.currentTimeMillis();
long bytesRead = 0;
long realmsWritten = 0;
2023-02-08 09:27:46 -05:00
int realmUUID;
2023-07-23 08:16:36 -04:00
String mapPath = null;
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
// Load image from disk
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
try {
2023-07-23 08:19:15 -04:00
mapPath = ConfigManager.DEFAULT_DATA_DIR + "realmmaps/" + ConfigManager.MB_WORLD_REALMMAP.getValue() + ".png";
2023-07-23 08:15:39 -04:00
image = ImageIO.read(new File(mapPath));
2022-04-30 09:41:17 -04:00
// Array size determined by image size
MBServerStatics.SPATIAL_HASH_BUCKETSX = image.getWidth();
MBServerStatics.SPATIAL_HASH_BUCKETSY = image.getHeight();
realmMap = new int[MBServerStatics.SPATIAL_HASH_BUCKETSX][MBServerStatics.SPATIAL_HASH_BUCKETSY];
} catch (IOException e) {
2023-07-23 08:16:36 -04:00
Logger.error("Error loading realm map: " + mapPath);
2022-04-30 09:41:17 -04:00
return null;
}
// Flip image on the y axis
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
image = flipImage(image);
// Load spatial imageMap with color data from file
for (int i = 0; i < MBServerStatics.SPATIAL_HASH_BUCKETSY; i++) {
for (int j = 0; j < MBServerStatics.SPATIAL_HASH_BUCKETSX; j++) {
2023-05-15 06:08:16 -04:00
Color pixelColor = new Color(image.getRGB(j, i));
2023-07-15 09:23:48 -04:00
realmUUID = RealmMap.getRealmIDByColor(pixelColor);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
realmMap[j][i] = realmUUID;
2023-05-15 06:12:35 -04:00
bytesRead++;
2022-04-30 09:41:17 -04:00
2023-05-15 06:12:35 -04:00
if (realmUUID != 0)
realmsWritten++;
2023-05-15 05:48:41 -04:00
2022-04-30 09:41:17 -04:00
}
}
2023-05-15 06:08:16 -04:00
2022-04-30 09:41:17 -04:00
timeToLoad = System.currentTimeMillis() - timeToLoad;
2023-07-23 08:22:17 -04:00
Logger.info("Realmmap: " + mapPath);
2023-05-15 06:17:16 -04:00
Logger.info(bytesRead + " pixels processed in " + timeToLoad + " milis");
2023-05-15 06:12:35 -04:00
Logger.info(realmsWritten + " realm pixels written ");
2022-04-30 09:41:17 -04:00
return realmMap;
}
public static BufferedImage flipImage(BufferedImage img) {
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
int w = img.getWidth();
int h = img.getHeight();
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
BufferedImage dimg = new BufferedImage(w, h, img.getColorModel()
.getTransparency());
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
Graphics2D g = dimg.createGraphics();
g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
g.dispose();
return dimg;
}
2023-10-08 09:49:49 -04:00
public static void loadAlHeightMaps() {
// Load pixel data for heightmaps
try (Stream<Path> filePathStream = Files.walk(Paths.get(ConfigManager.DEFAULT_DATA_DIR + "heightmaps/TARGA/"))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
File imageFile = filePath.toFile();
2023-10-09 05:44:38 -04:00
BufferedImage heightmapImage;
2023-10-08 09:49:49 -04:00
try {
2023-10-09 05:44:38 -04:00
heightmapImage = ImageIO.read(imageFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
int fileName = Integer.parseInt(imageFile.getName().substring(0, imageFile.getName().lastIndexOf(".")));
boolean singleBandRaster = heightmapImage.getRaster().getNumBands() == 1;
int color;
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
// Generate pixel data for this heightmap.
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
short[][] colorData = new short[heightmapImage.getWidth()][heightmapImage.getHeight()];
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
for (int y = 0; y < heightmapImage.getHeight(); y++)
for (int x = 0; x < heightmapImage.getWidth(); x++) {
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
if (singleBandRaster)
color = heightmapImage.getRaster().getSample(x, y, 0);
else
color = new Color(heightmapImage.getRGB(x, y)).getRed();
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
colorData[x][y] = (short) color;
}
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
// Add pixel for this TGA image into the collection
2023-10-08 09:49:49 -04:00
2023-10-09 05:44:38 -04:00
Terrain._heightmap_pixel_cache.put(fileName, colorData);
2023-10-08 09:49:49 -04:00
}
2023-10-09 05:44:38 -04:00
2023-10-08 09:49:49 -04:00
}); // Try with resources block
} catch (IOException e) {
Logger.error(e);
}
}
2022-04-30 09:41:17 -04:00
}