#pragma once #include "StarSystemWorld.hpp" #include "StarUuid.hpp" namespace Star { STAR_CLASS(SystemWorldServer); STAR_STRUCT(Packet); class SystemWorldServer : public SystemWorld { public: // create new system world server SystemWorldServer(Vec3I location, ClockConstPtr universeClock, CelestialDatabasePtr celestialDatabase); // load system world server from storage SystemWorldServer(Json const& diskStore, ClockConstPtr universeClock, CelestialDatabasePtr celestialDatabase); void setClientDestination(ConnectionId const& clientId, SystemLocation const& destination); // null while flying, system coordinate when in space or at a system object // planet coordinates while orbiting a planet SystemClientShipPtr clientShip(ConnectionId clientId) const; SystemLocation clientShipLocation(ConnectionId clientId) const; Maybe> clientWarpAction(ConnectionId clientId) const; SkyParameters clientSkyParameters(ConnectionId clientId) const; List clients() const; void addClientShip(ConnectionId clientId, Uuid const& uuid, float shipSpeed, SystemLocation location); void removeClientShip(ConnectionId clientId); List shipsAtLocation(SystemLocation const& location) const; List activeInstanceWorlds() const; // removeObject queues up object for destruction, any ships at the location // are moved away void removeObject(Uuid objectUuid); bool addObject(SystemObjectPtr object, bool doRangeCheck = false); void update(float dt); List objects() const override; List objectKeys() const override; SystemObjectPtr getObject(Uuid const& uuid) const override; List pullShipFlights(); void handleIncomingPacket(ConnectionId clientId, PacketPtr packet); List pullOutgoingPackets(ConnectionId clientId); bool triggeredStorage(); Json diskStore(); private: struct ClientNetVersions { HashMap ships; HashMap objects; }; void queueUpdatePackets(); void setClientShipDestination(ConnectionId clientId, SystemLocation const& destination); // random position for new ships entering the system void placeInitialObjects(); void spawnObjects(); Vec2F randomObjectSpawnPosition(RandomSource& rand) const; SkyParameters locationSkyParameters(SystemLocation const& location) const; // setting this to true asynchronously triggers storage from the server thread bool m_triggerStorage; double m_lastSpawn; double m_objectSpawnTime; // objects to be destroyed as soon as there are no ships at the location List m_objectDestroyQueue; // ships to be destroyed after update packets have been queued List m_shipDestroyQueue; HashMap m_clientNetVersions; HashMap m_clientShips; HashMap m_objects; HashMap m_ships; // client ID and flight start position List m_shipFlights; HashMap> m_outgoingPackets; }; }