Merge pull request #1236 from madsboddum/static_city_loader

Extracted static city loading from CityService into StaticCityLoader,…
This commit is contained in:
Josh
2023-04-05 18:23:05 -05:00
committed by GitHub
3 changed files with 79 additions and 80 deletions
@@ -105,6 +105,7 @@ object ServerData {
val destroyMissions by SoftDataLoaderDelegate(::DestroyMissionLoader)
val dynamicLairs by SoftDataLoaderDelegate(::DynamicLairLoader)
val rawResources by SoftDataLoaderDelegate(::RawResourceLoader)
val staticCities by SoftDataLoaderDelegate(::StaticCityLoader)
private class WeakDataLoaderDelegate<T: DataLoader>(loaderCreator: () -> T): DataLoaderDelegate<T>(::WeakReference, loaderCreator)
private class SoftDataLoaderDelegate<T: DataLoader>(loaderCreator: () -> T): DataLoaderDelegate<T>(::SoftReference, loaderCreator)
@@ -0,0 +1,65 @@
/***********************************************************************************
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* --------------------------------------------------------------------------------*
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
***********************************************************************************/
package com.projectswg.holocore.resources.support.data.server_info.loader
import com.projectswg.common.data.location.Terrain
import com.projectswg.holocore.resources.support.data.server_info.SdbLoader
import com.projectswg.holocore.resources.support.objects.swg.SWGObject
import java.io.File
class StaticCityLoader : DataLoader() {
private val cities = mutableMapOf<Terrain, MutableList<City>>()
fun getCities(terrain: Terrain): Collection<City> {
return cities.getOrElse(terrain) { emptyList() }
}
override fun load() {
SdbLoader.load(File("serverdata/map/cities.sdb")).use { set ->
while (set.next()) {
val t = Terrain.getTerrainFromName(set.getText("terrain"))
val list: MutableList<City> = cities.computeIfAbsent(t) { mutableListOf() }
list.add(City(set.getText("city"), set.getInt("x").toInt(), set.getInt("z").toInt(), set.getInt("radius").toInt()))
}
}
}
class City(val name: String, private val x: Int, private val z: Int, private val radius: Int) {
fun isWithinRange(obj: SWGObject): Boolean {
return square(obj.x.toInt() - x) + square(obj.z.toInt() - z) <= square(radius)
}
override fun toString(): String {
return String.format("City[%s, (%d, %d), radius=%d]", name, x, z, radius)
}
private fun square(x: Int): Int {
return x * x
}
}
}
@@ -1,5 +1,5 @@
/***********************************************************************************
* Copyright (c) 2018 /// Project SWG /// www.projectswg.com *
* Copyright (c) 2023 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
@@ -26,14 +26,13 @@
***********************************************************************************/
package com.projectswg.holocore.services.gameplay.structures.housing;
import com.projectswg.common.data.location.Terrain;
import com.projectswg.common.network.packets.SWGPacket;
import com.projectswg.common.network.packets.swg.zone.object_controller.DataTransform;
import com.projectswg.holocore.intents.support.global.network.InboundPacketIntent;
import com.projectswg.holocore.intents.support.global.zone.PlayerEventIntent;
import com.projectswg.holocore.intents.support.objects.swg.ObjectCreatedIntent;
import com.projectswg.holocore.resources.support.data.server_info.SdbLoader;
import com.projectswg.holocore.resources.support.data.server_info.SdbLoader.SdbResultSet;
import com.projectswg.holocore.resources.support.data.server_info.loader.ServerData;
import com.projectswg.holocore.resources.support.data.server_info.loader.StaticCityLoader.City;
import com.projectswg.holocore.resources.support.global.player.Player;
import com.projectswg.holocore.resources.support.global.player.PlayerEvent;
import com.projectswg.holocore.resources.support.objects.swg.SWGObject;
@@ -41,29 +40,11 @@ import com.projectswg.holocore.resources.support.objects.swg.creature.CreatureOb
import com.projectswg.holocore.resources.support.objects.swg.tangible.TangibleObject;
import me.joshlarson.jlcommon.control.IntentHandler;
import me.joshlarson.jlcommon.control.Service;
import me.joshlarson.jlcommon.log.Log;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
public class CityService extends Service {
private final Map<Terrain, List<City>> cities;
public CityService() {
cities = new EnumMap<>(Terrain.class);
}
@Override
public boolean initialize() {
loadCities();
return true;
}
@IntentHandler
private void handleInboundPacketIntent(InboundPacketIntent gpi) {
SWGPacket p = gpi.getPacket();
@@ -71,7 +52,7 @@ public class CityService extends Service {
performLocationUpdate(gpi.getPlayer().getCreatureObject());
}
}
@IntentHandler
private void handlePlayerEventIntent(PlayerEventIntent i) {
Player player = i.getPlayer();
@@ -80,36 +61,21 @@ public class CityService extends Service {
performLocationUpdate(creature);
}
}
@IntentHandler
private void handleObjectCreatedIntent(ObjectCreatedIntent i) {
SWGObject object = i.getObject();
if (!(object instanceof TangibleObject)) {
return;
}
performLocationUpdate((TangibleObject) object);
}
private void loadCities() {
cities.clear();
try (SdbResultSet set = SdbLoader.load(new File("serverdata/map/cities.sdb"))) {
while (set.next()) {
Terrain t = Terrain.getTerrainFromName(set.getText("terrain"));
List<City> list = cities.computeIfAbsent(t, k -> new ArrayList<>());
list.add(new City(set.getText("city"), (int) set.getInt("x"), (int) set.getInt("z"), (int) set.getInt("radius")));
}
} catch (IOException e) {
Log.e(e);
}
}
private void performLocationUpdate(TangibleObject object) {
List<City> list = cities.get(object.getTerrain());
if (list == null)
return; // No cities on that planet
for (City city : list) {
Collection<City> cities = ServerData.INSTANCE.getStaticCities().getCities(object.getTerrain());
for (City city : cities) {
if (city.isWithinRange(object)) {
object.setCurrentCity(city.getName());
return;
@@ -117,38 +83,5 @@ public class CityService extends Service {
}
object.setCurrentCity("");
}
private static class City {
private String name;
private int x;
private int z;
private int radius;
public City(String name, int x, int z, int radius) {
this.name = name;
this.x = x;
this.z = z;
this.radius = radius;
}
public String getName() {
return name;
}
public boolean isWithinRange(SWGObject obj) {
return square((int) obj.getX() - x) + square((int) obj.getZ() - z) <= square(radius);
}
@Override
public String toString() {
return String.format("City[%s, (%d, %d), radius=%d]", name, x, z, radius);
}
private int square(int x) {
return x * x;
}
}
}