Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from siege import game
- from siege.util import minutes, Timer
- from siege.network import NetworkManager, Message, MessageHandler
- from siege.io import DataStream, File, FileOnCompleteHandler
- from siege.world import World
- from siege import Locale
- from siege.log import Log
- class AnomalySystem(object):
- UPDATE_FREQUENCY = minutes(5) # Only the update frequency for starting anomalies, everything else updates on update.
- FILE_VERSION = 3
- NAME = 'anomaly'
- def getName(self):
- return self.NAME
- def __init__(self):
- self.anomalies = dict()
- self.updateTimer = Timer(self.UPDATE_FREQUENCY)
- self.world = None
- def add(self, anomaly):
- self.anomalies[anomaly.name] = anomaly
- def getAnomalies(self):
- return self.anomalies
- def getAnomaly(self, anomalyName):
- return self.anomalies[anomalyName]
- def update(self, frametime):
- if World.get() is not None: # Make sure the game is being played, not that a world is being generated
- if World.get().getPlayer() is not None or NetworkManager.isStandalone(): # Make sure that the player isn't idling on the character select screen
- if NetworkManager.isHost():
- self.updateTimer.update(frametime)
- for anomalyName, anomaly in self.anomalies.iteritems():
- if anomaly.active:
- anomaly.update(frametime)
- if self.updateTimer.expired():
- self.updateTimer.reset(self.UPDATE_FREQUENCY)
- for anomalyName, anomaly in self.anomalies.iteritems():
- if anomaly.shouldOccur(self.UPDATE_FREQUENCY):
- anomaly.occur()
- else:
- for anomalyName, anomaly in self.anomalies.iteritems():
- if anomaly.active:
- anomaly.clientUpdate(frametime)
- def handleClientSync(self, client, packet):
- packetAnomalyKey = packet.readString()
- if packetAnomalyKey in self.anomalies.keys():
- self.anomalies[packetAnomalyKey].recieveSync(client, packet)
- else:
- Log.error("No anomaly with name {} registered on client".format(packetAnomalyKey))
- def save(self, world, asynchronous):
- stream = DataStream()
- stream.writeUint16(self.FILE_VERSION)
- stream.writeUint8(len(self.anomalies))
- anomalySaveOrder = []
- for anomalyName in self.anomalies.keys():
- stream.writeString(anomalyName)
- anomalySaveOrder.append(anomalyName)
- for i in range(len(anomalySaveOrder)):
- stream.diverge()
- anomaly = self.getAnomaly(anomalySaveOrder[i])
- stream.writeUint8(anomaly.VERSION)
- anomaly.save(stream)
- stream.converge()
- if asynchronous:
- game.file.asyncWrite(self.getPath(world), FileOnCompleteHandler.create(), stream, useCompression=False)
- else:
- File.save(self.getPath(world), stream)
- def load(self, world):
- if File.exists(self.getPath(world)):
- stream = DataStream()
- File.load(self.getPath(world), stream)
- systemVersion = stream.readUint16() # Read anomaly file systemVersion
- if systemVersion >= 2:
- anomalyAmount = stream.readUint8()
- anomalySaveOrder = []
- for i in range(anomalyAmount):
- anomalySaveOrder.append(stream.readString())
- for i in range(anomalyAmount):
- stream.begin()
- try:
- anomaly = self.getAnomaly(anomalySaveOrder[i])
- anomalyVersion = stream.readUint8()
- anomaly.load(stream, systemVersion, anomalyVersion)
- stream.end()
- except Exception as error:
- Log.warn("Anomaly of type {} failed to load".format(type(anomaly).__name__))
- Log.warn(str(error))
- anomaly.loadFailed()
- stream.skip()
- if systemVersion == 2:
- for anomaly in self.anomalies.values():
- if anomaly.realm:
- anomaly.removeMarker(anomaly.realm)
- markerName = Locale.get(anomaly.markerName)
- anomaly.purgeMarkers(anomaly.realm, markerName)
- anomaly.addMarker(anomaly.realm, markerName)
- elif systemVersion == 1:
- self.loadV1(stream)
- else:
- for anomalyName, anomaly in self.anomalies.iteritems():
- anomaly.loadFailed()
- Log.warn("Resetting Anomaly Timers due to file version change")
- def loadV1(self, stream):
- Log.info("Anomaly data is V1, attempting to load from V1.")
- for anomalyName, anomaly in self.anomalies.iteritems():
- Log.debug("Attempting to load {} from V1".format(anomalyName))
- if anomalyName not in ["apex", "darkness", "silvan", "swarm", "chaos_crystal"]:
- continue
- stream.begin()
- try:
- anomaly.load(stream, 1, 1)
- stream.end()
- Log.debug("Succeeded in loading {} from V1".format(anomalyName))
- except Exception as error:
- Log.warn("Anomaly of type {} failed to load from V1".format(type(anomaly).__name__))
- Log.warn(str(error))
- anomaly.loadFailed()
- stream.skip()
- def getPath(self, world):
- return os.path.join(world.path, "anomaly")
- @staticmethod
- def playerJoined(player=None):
- if player is None and not NetworkManager.isStandalone():
- player = World.get().getPlayer()
- if player is not None:
- player.entity.legend.enableMarker("Anomaly")
- @staticmethod
- def worldLoaded(world):
- system = game.anomaly
- system.load(world)
- game.events['world_saved'].listen(system.save)
- @staticmethod
- def worldUnloaded(world):
- if game.hasSystem(AnomalySystem.NAME):
- system = game.anomaly
- game.events["world_saved"].remove(system.save)
- @staticmethod
- def register():
- game.events['player_joined'].listen(AnomalySystem.playerJoined)
- if not NetworkManager.isStandalone():
- # Need to do this because player_joined isn't called in time for the host.
- game.events['map_loaded'].listen(AnomalySystem.playerJoined)
- system = AnomalySystem()
- game.registerSystem(AnomalySystem.NAME, system)
- if NetworkManager.isHost():
- game.events['world_loaded'].listen(AnomalySystem.worldLoaded)
- game.events['world_unloaded'].listen(AnomalySystem.worldUnloaded)
- else:
- # Check if the client exists since the first system initialization occurs while in the menu
- if game.network.getClient() is not None:
- game.network.getClient().register(Message.ANOMALY, MessageHandler.create(system.handleClientSync))
- @staticmethod
- def unregister():
- game.events['player_joined'].remove(AnomalySystem.playerJoined)
- game.events['map_loaded'].remove(AnomalySystem.playerJoined)
Advertisement
Add Comment
Please, Sign In to add comment