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

91 lines
2.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-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;
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;
}
}