Files
prestonbane/src/engine/InterestManagement/RealmMap.java
T

126 lines
4.6 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.InterestManagement;
import engine.math.Vector3fImmutable;
import engine.mbEnums;
2022-04-30 09:41:17 -04:00
import engine.net.Dispatch;
import engine.net.DispatchMessage;
import engine.net.client.msg.TerritoryChangeMessage;
import engine.objects.City;
import engine.objects.PlayerCharacter;
import engine.objects.Realm;
import engine.server.MBServerStatics;
import engine.util.MapLoader;
import org.pmw.tinylog.Logger;
import java.awt.*;
import java.util.HashMap;
2022-04-30 09:41:17 -04:00
import static engine.objects.Realm.getRealm;
public enum RealmMap {
2024-04-26 08:09:07 -04:00
// MB Dev Notes:
// This class loads and caches realm maps used by each
// map set for its realm overlay. The RealmMap loaded is
// controlled by config entry MB_WORLD_REALMMAP
//
// Unlike a Heightmap this is a just color lookup; identical to
// the old image maps used in 90s web technology.
//
// Realm Map images are stored on disk in /mb.data/realmmaps/
2024-04-26 08:09:07 -04:00
REALM_MAP;
2022-04-30 09:41:17 -04:00
2023-05-15 05:48:41 -04:00
private static final HashMap<Color, Integer> _rgbToIDMap = new HashMap<>();
2023-07-15 09:23:48 -04:00
public static int[][] _realmImageMap;
2023-05-15 05:48:41 -04:00
public static int getRealmIDByColor(Color color) {
2022-04-30 09:41:17 -04:00
2023-05-15 06:03:13 -04:00
return _rgbToIDMap.getOrDefault(color, 0);
}
2022-04-30 09:41:17 -04:00
public static int getRealmIDAtLocation(Vector3fImmutable pos) {
int xBuckets = (int) ((pos.getX() / MBServerStatics.MAX_WORLD_WIDTH) * MBServerStatics.SPATIAL_HASH_BUCKETSX);
int yBuckets = (int) ((pos.getZ() / MBServerStatics.MAX_WORLD_HEIGHT) * MBServerStatics.SPATIAL_HASH_BUCKETSY);
if (yBuckets < 0 || yBuckets >= MBServerStatics.SPATIAL_HASH_BUCKETSY
|| xBuckets < 0 || xBuckets >= MBServerStatics.SPATIAL_HASH_BUCKETSX) {
2023-02-08 09:27:46 -05:00
Logger.error("Invalid range; Z: " + yBuckets + ", X: " + xBuckets);
2022-04-30 09:41:17 -04:00
return 255;
}
return RealmMap._realmImageMap[xBuckets][yBuckets];
}
public static void addToColorMap(Color color, int realmID) {
2023-05-15 05:48:41 -04:00
_rgbToIDMap.put(color, realmID);
}
2022-04-30 09:41:17 -04:00
public static Realm getRealmForCity(City city) {
Realm outRealm = null;
2024-04-27 11:32:05 -04:00
outRealm = city.realm;
2022-04-30 09:41:17 -04:00
return outRealm;
}
public static Realm getRealmAtLocation(Vector3fImmutable worldVector) {
return getRealm(RealmMap.getRealmIDAtLocation(worldVector));
}
public static void updateRealm(PlayerCharacter player) {
2022-04-30 09:41:17 -04:00
int realmID = RealmMap.getRealmIDAtLocation(player.getLoc());
if (realmID != player.getLastRealmID()) {
2022-04-30 09:41:17 -04:00
player.setLastRealmID(realmID);
Realm realm = Realm.getRealm(realmID);
if (realm != null) {
if (realm.isRuled()) {
2022-04-30 09:41:17 -04:00
City city = realm.getRulingCity();
if (city != null) {
TerritoryChangeMessage tcm = new TerritoryChangeMessage((PlayerCharacter) realm.getRulingCity().getOwner(), realm);
2022-04-30 09:41:17 -04:00
Dispatch dispatch = Dispatch.borrow(player, tcm);
DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);
} else {
TerritoryChangeMessage tcm = new TerritoryChangeMessage(null, realm);
2022-04-30 09:41:17 -04:00
Dispatch dispatch = Dispatch.borrow(player, tcm);
DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);
2022-04-30 09:41:17 -04:00
}
} else {
TerritoryChangeMessage tcm = new TerritoryChangeMessage(null, realm);
2022-04-30 09:41:17 -04:00
Dispatch dispatch = Dispatch.borrow(player, tcm);
DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.PRIMARY);
2022-04-30 09:41:17 -04:00
}
}
}
}
public static void loadRealmImageMap() {
2023-02-08 09:27:46 -05:00
// Build color lookup map for realms from database
for (Realm realm : Realm._realms.values()) {
RealmMap.addToColorMap(realm.mapColor, realm.realmID);
}
2022-04-30 09:41:17 -04:00
RealmMap._realmImageMap = MapLoader.loadMap();
}
}