Added weather

This commit is contained in:
Light2
2013-11-21 15:17:32 +01:00
parent 3f721f876f
commit e7350687db
5 changed files with 139 additions and 2 deletions
+16
View File
@@ -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)
+6
View File
@@ -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);
+2 -2
View File
@@ -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();
}
}
+35
View File
@@ -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<Integer, Client> 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<Integer, Client> clients = core.getActiveConnectionsMap();
for(Client client : clients.values()) {
if(client.getParent() == null)
continue;
client.getSession().write(packet);
}
}
}
+80
View File
@@ -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<Planet, Integer> weatherStability = new ConcurrentHashMap<Planet, Integer>();
private Map<Planet, Byte> currentWeatherMap = new ConcurrentHashMap<Planet, Byte>();
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());
}
}