From e7350687dbb0fde8a1f47a72f38978c34dcd98de Mon Sep 17 00:00:00 2001 From: Light2 Date: Thu, 21 Nov 2013 15:17:32 +0100 Subject: [PATCH] Added weather --- scripts/weather.py | 16 +++++ src/main/NGECore.java | 6 ++ src/protocol/swg/ServerWeatherMessage.java | 4 +- src/services/SimulationService.java | 35 ++++++++++ src/services/WeatherService.java | 80 ++++++++++++++++++++++ 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 scripts/weather.py diff --git a/scripts/weather.py b/scripts/weather.py new file mode 100644 index 00000000..397986ae --- /dev/null +++ b/scripts/weather.py @@ -0,0 +1,16 @@ +import sys + +def init(core): + weatherSvc = core.weatherService + weatherSvc.addPlanetSettings('tatooine', 75, 0) + weatherSvc.addPlanetSettings('naboo', 85, 0) + weatherSvc.addPlanetSettings('corellia', 85, 1) + weatherSvc.addPlanetSettings('dantooine', 80, 1) + weatherSvc.addPlanetSettings('rori', 75, 2) + weatherSvc.addPlanetSettings('talus', 75, 2) + weatherSvc.addPlanetSettings('lok', 75, 2) + weatherSvc.addPlanetSettings('dathomir', 50, 3) + weatherSvc.addPlanetSettings('endor', 80, 0) + weatherSvc.addPlanetSettings('yavin4', 65, 2) + weatherSvc.addPlanetSettings('kashyyyk_main', 80, 0) + diff --git a/src/main/NGECore.java b/src/main/NGECore.java index 728cd62a..03c0d448 100644 --- a/src/main/NGECore.java +++ b/src/main/NGECore.java @@ -52,6 +52,7 @@ import services.SkillModService; import services.SkillService; import services.StaticService; import services.TerrainService; +import services.WeatherService; import services.chat.ChatService; import services.combat.CombatService; import services.command.CombatCommand; @@ -132,6 +133,7 @@ public class NGECore { public EquipmentService equipmentService; public TravelService travelService; public EntertainmentService entertainmentService; + public WeatherService weatherService; // Login Server public NetworkDispatch loginDispatch; @@ -143,6 +145,7 @@ public class NGECore { private ObjectDatabase creatureODB; private ObjectDatabase mailODB; + public NGECore() { @@ -275,6 +278,9 @@ public class NGECore { travelService.startShuttleSchedule(); + weatherService = new WeatherService(this); + weatherService.loadPlanetSettings(); + didServerCrash = false; System.out.println("Started Server."); setGalaxyStatus(2); diff --git a/src/protocol/swg/ServerWeatherMessage.java b/src/protocol/swg/ServerWeatherMessage.java index dfe21b2d..e8dc0955 100644 --- a/src/protocol/swg/ServerWeatherMessage.java +++ b/src/protocol/swg/ServerWeatherMessage.java @@ -45,7 +45,7 @@ public class ServerWeatherMessage extends SWGMessage { } public IoBuffer serialize() { - IoBuffer result = IoBuffer.allocate(26).order(ByteOrder.LITTLE_ENDIAN);; + IoBuffer result = IoBuffer.allocate(22).order(ByteOrder.LITTLE_ENDIAN); result.putShort((short)3); result.putInt(0x486356EA); @@ -54,6 +54,6 @@ public class ServerWeatherMessage extends SWGMessage { result.putFloat(cloudY); result.putFloat(cloudZ); - return result; + return result.flip(); } } diff --git a/src/services/SimulationService.java b/src/services/SimulationService.java index 5f8b3ae7..b0ffc16b 100644 --- a/src/services/SimulationService.java +++ b/src/services/SimulationService.java @@ -553,6 +553,8 @@ public class SimulationService implements INetworkDispatch { } } + core.weatherService.sendWeather(object); + if (!object.hasSkill(ghost.getProfessionWheelPosition())) { object.showFlyText("cbt_spam", "skill_up", (float) 2.5, new RGB(154, 205, 50), 0); object.playEffectObject("clienteffect/skill_granted.cef", ""); @@ -872,5 +874,38 @@ public class SimulationService implements INetworkDispatch { return height; } + + public void notifyPlanet(Planet planet, IoBuffer packet) { + + ConcurrentHashMap clients = core.getActiveConnectionsMap(); + + for(Client client : clients.values()) { + + if(client.getParent() == null) + continue; + + if(client.getParent().getPlanet() == null) + continue; + else if(client.getParent().getPlanet() == planet) + client.getSession().write(packet); + + } + + } + + public void notifyAllClients(IoBuffer packet) { + + ConcurrentHashMap clients = core.getActiveConnectionsMap(); + + for(Client client : clients.values()) { + + if(client.getParent() == null) + continue; + + client.getSession().write(packet); + + } + + } } diff --git a/src/services/WeatherService.java b/src/services/WeatherService.java index 501cf77b..79e22d3d 100644 --- a/src/services/WeatherService.java +++ b/src/services/WeatherService.java @@ -21,17 +21,97 @@ ******************************************************************************/ package services; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import protocol.swg.ServerWeatherMessage; +import resources.objects.creature.CreatureObject; +import engine.resources.scene.Planet; import main.NGECore; public class WeatherService { private NGECore core; + private Map weatherStability = new ConcurrentHashMap(); + private Map currentWeatherMap = new ConcurrentHashMap(); + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + // TODO: randomise cloud vectors, proper algorithm for weather type rolls + public WeatherService(NGECore core) { this.core = core; } + public void loadPlanetSettings(){ + core.scriptService.callScript("scripts/", "weather", "init", core); + } + public void addPlanetSettings(String planetName, int stability, byte defaultWeather) { + + final Planet planet = core.terrainService.getPlanetByName(planetName); + + if(planet == null) + return; + + if(stability > 100) + stability = 100; + if(defaultWeather > 4 || defaultWeather < 0) + defaultWeather = 0; + + weatherStability.put(planet, stability); + currentWeatherMap.put(planet, defaultWeather); + + scheduler.scheduleAtFixedRate(new Runnable() { + + @Override + public void run() { + runWeatherCycle(planet); + } + + }, 30, 30, TimeUnit.MINUTES); + + } + + public void runWeatherCycle(Planet planet) { + + int stability = weatherStability.get(planet); + + Random rand = new Random(); + int weatherRoll = rand.nextInt(100); + byte weatherType; + + if(weatherRoll < stability) + weatherType = 0; + else { + weatherType = (byte) (rand.nextInt(4) + 1); + } + + currentWeatherMap.put(planet, weatherType); + core.simulationService.notifyPlanet(planet, new ServerWeatherMessage(weatherType, 1, 0, 0).serialize()); + //System.out.println("Weather type changed to: " + weatherType + " on: " + planet.getName()); + + } + + public void sendWeather(CreatureObject player) { + + if(player.getClient() == null || player.getPlanet() == null) + return; + + byte weatherType; + + if(!currentWeatherMap.containsKey(player.getPlanet())) { + weatherType = 0; + } else { + weatherType = currentWeatherMap.get(player.getPlanet()); + } + + ServerWeatherMessage weatherMsg = new ServerWeatherMessage(weatherType, 1, 0, 0); + player.getClient().getSession().write(weatherMsg.serialize()); + + } }