diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-05-07 04:49:52 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-05-07 04:49:52 +1000 |
commit | 3a54621bd8a55b672ba986f02ab094bfb4ba6faf (patch) | |
tree | 4cba881b3c8518b7be01fc8225a0877406aee16e /source/game | |
parent | 37fab128ceccfafac99d33e84c768e85aead6d60 (diff) |
add world.template, world.setTemplate
Diffstat (limited to 'source/game')
-rw-r--r-- | source/game/StarNetPackets.cpp | 19 | ||||
-rw-r--r-- | source/game/StarNetPackets.hpp | 13 | ||||
-rw-r--r-- | source/game/StarUniverseClient.cpp | 2 | ||||
-rw-r--r-- | source/game/StarWorldClient.cpp | 4 | ||||
-rw-r--r-- | source/game/StarWorldClient.hpp | 1 | ||||
-rw-r--r-- | source/game/StarWorldServer.cpp | 25 | ||||
-rw-r--r-- | source/game/StarWorldServer.hpp | 1 | ||||
-rw-r--r-- | source/game/scripting/StarWorldLuaBindings.cpp | 13 |
8 files changed, 73 insertions, 5 deletions
diff --git a/source/game/StarNetPackets.cpp b/source/game/StarNetPackets.cpp index 953743c..cf0beca 100644 --- a/source/game/StarNetPackets.cpp +++ b/source/game/StarNetPackets.cpp @@ -77,7 +77,8 @@ EnumMap<PacketType> const PacketTypeNames{ {PacketType::SystemShipDestroy, "SystemShipDestroy"}, {PacketType::SystemObjectSpawn, "SystemObjectSpawn"}, // OpenStarbound packets - {PacketType::ReplaceTileList, "ReplaceTileList"} + {PacketType::ReplaceTileList, "ReplaceTileList"}, + {PacketType::UpdateWorldTemplate, "UpdateWorldTemplate"} }; EnumMap<NetCompressionMode> const NetCompressionModeNames { @@ -139,7 +140,6 @@ PacketPtr createPacket(PacketType type) { case PacketType::FindUniqueEntityResponse: return make_shared<FindUniqueEntityResponsePacket>(); case PacketType::Pong: return make_shared<PongPacket>(); case PacketType::ModifyTileList: return make_shared<ModifyTileListPacket>(); - case PacketType::ReplaceTileList: return make_shared<ReplaceTileListPacket>(); case PacketType::DamageTileGroup: return make_shared<DamageTileGroupPacket>(); case PacketType::CollectLiquid: return make_shared<CollectLiquidPacket>(); case PacketType::RequestDrop: return make_shared<RequestDropPacket>(); @@ -169,6 +169,9 @@ PacketPtr createPacket(PacketType type) { case PacketType::SystemShipCreate: return make_shared<SystemShipCreatePacket>(); case PacketType::SystemShipDestroy: return make_shared<SystemShipDestroyPacket>(); case PacketType::SystemObjectSpawn: return make_shared<SystemObjectSpawnPacket>(); + // OpenStarbound + case PacketType::ReplaceTileList: return make_shared<ReplaceTileListPacket>(); + case PacketType::UpdateWorldTemplate: return make_shared<UpdateWorldTemplatePacket>(); default: throw StarPacketException(strf("Unrecognized packet type {}", (unsigned int)type)); } @@ -1430,4 +1433,16 @@ void SystemObjectSpawnPacket::write(DataStream& ds) const { ds.write(parameters); } +UpdateWorldTemplatePacket::UpdateWorldTemplatePacket() {} + +UpdateWorldTemplatePacket::UpdateWorldTemplatePacket(Json templateData) : templateData(std::move(templateData)) {} + +void UpdateWorldTemplatePacket::read(DataStream& ds) { + ds.read(templateData); +} + +void UpdateWorldTemplatePacket::write(DataStream& ds) const { + ds.write(templateData); +} + } diff --git a/source/game/StarNetPackets.hpp b/source/game/StarNetPackets.hpp index 63a7ee8..5dbb822 100644 --- a/source/game/StarNetPackets.hpp +++ b/source/game/StarNetPackets.hpp @@ -116,7 +116,8 @@ enum class PacketType : uint8_t { SystemObjectSpawn, // OpenStarbound packets - ReplaceTileList + ReplaceTileList, + UpdateWorldTemplate }; extern EnumMap<PacketType> const PacketTypeNames; @@ -970,4 +971,14 @@ struct SystemObjectSpawnPacket : PacketBase<PacketType::SystemObjectSpawn> { Maybe<Vec2F> position; JsonObject parameters; }; + +struct UpdateWorldTemplatePacket : PacketBase<PacketType::UpdateWorldTemplate> { + UpdateWorldTemplatePacket(); + UpdateWorldTemplatePacket(Json templateData); + + void read(DataStream& ds) override; + void write(DataStream& ds) const override; + + Json templateData; +}; } diff --git a/source/game/StarUniverseClient.cpp b/source/game/StarUniverseClient.cpp index b41d19e..9a1f31b 100644 --- a/source/game/StarUniverseClient.cpp +++ b/source/game/StarUniverseClient.cpp @@ -423,7 +423,7 @@ void UniverseClient::warpPlayer(WarpAction const& warpAction, bool animate, Stri if (auto warpToWorld = warpAction.ptr<WarpToWorld>()) { if (warpToWorld->world.empty() || warpToWorld->world == playerWorld()) { if (auto pos = warpToWorld->target.ptr<SpawnTargetPosition>()) { - m_mainPlayer->moveTo(*pos); // Add a little space to up + m_mainPlayer->moveTo(*pos); return; } } diff --git a/source/game/StarWorldClient.cpp b/source/game/StarWorldClient.cpp index 51fe883..8db6a62 100644 --- a/source/game/StarWorldClient.cpp +++ b/source/game/StarWorldClient.cpp @@ -186,6 +186,10 @@ WorldTemplateConstPtr WorldClient::currentTemplate() const { return m_worldTemplate; } +void WorldClient::setTemplate(Json newTemplate) { + m_outgoingPackets.push_back(make_shared<UpdateWorldTemplatePacket>(newTemplate)); +} + SkyConstPtr WorldClient::currentSky() const { return m_sky; } diff --git a/source/game/StarWorldClient.hpp b/source/game/StarWorldClient.hpp index 5ce3997..6c1f89c 100644 --- a/source/game/StarWorldClient.hpp +++ b/source/game/StarWorldClient.hpp @@ -115,6 +115,7 @@ public: void removeEntity(EntityId entityId, bool andDie); WorldTemplateConstPtr currentTemplate() const; + void setTemplate(Json newTemplate); SkyConstPtr currentSky() const; void dimWorld(); diff --git a/source/game/StarWorldServer.cpp b/source/game/StarWorldServer.cpp index 3bc067a..a9dc1f0 100644 --- a/source/game/StarWorldServer.cpp +++ b/source/game/StarWorldServer.cpp @@ -340,7 +340,7 @@ List<EntityId> WorldServer::players() const { } void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> const& packets) { - auto const& clientInfo = m_clientInfo.get(clientId); + shared_ptr<ClientInfo> clientInfo = m_clientInfo.get(clientId); auto& root = Root::singleton(); auto entityFactory = root.entityFactory(); auto itemDatabase = root.itemDatabase(); @@ -542,6 +542,14 @@ void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> c for (auto const& pair : m_clientInfo) pair.second->outgoingPackets.append(make_shared<UpdateWorldPropertiesPacket>(updateWorldProperties->updatedProperties)); + } else if (auto updateWorldTemplate = as<UpdateWorldTemplatePacket>(packet)) { + if (!clientInfo->admin) + continue; // nuh-uh! + + auto newWorldTemplate = make_shared<WorldTemplate>(updateWorldTemplate->templateData); + setTemplate(newWorldTemplate); + // setTemplate re-adds all clients currently, update clientInfo + clientInfo = m_clientInfo.get(clientId); } else { throw WorldServerException::format("Improper packet type {} received by client", (int)packet->type()); } @@ -2587,4 +2595,19 @@ void WorldServer::setupForceRegions() { } } +void WorldServer::setTemplate(WorldTemplatePtr newTemplate) { + m_worldTemplate = std::move(newTemplate); + for (auto& client : clientIds()) { + auto& info = m_clientInfo.get(client); + bool local = info->local; + bool isAdmin = info->admin; + auto netRules = info->clientState.netCompatibilityRules(); + SpawnTarget spawnTarget; + if (auto player = clientPlayer(client)) + spawnTarget = SpawnTargetPosition(player->position() + player->feetOffset()); + removeClient(client); + addClient(client, spawnTarget, local, isAdmin, netRules); + } +} + } diff --git a/source/game/StarWorldServer.hpp b/source/game/StarWorldServer.hpp index 4a0e87a..3c60b40 100644 --- a/source/game/StarWorldServer.hpp +++ b/source/game/StarWorldServer.hpp @@ -217,6 +217,7 @@ public: EntityId loadUniqueEntity(String const& uniqueId); WorldTemplatePtr worldTemplate() const; + void setTemplate(WorldTemplatePtr newTemplate); SkyPtr sky() const; void modifyLiquid(Vec2I const& pos, LiquidId liquid, float quantity, bool additive = false); void setLiquid(Vec2I const& pos, LiquidId liquid, float level, float pressure); diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index b2c94a4..2d952ea 100644 --- a/source/game/scripting/StarWorldLuaBindings.cpp +++ b/source/game/scripting/StarWorldLuaBindings.cpp @@ -371,6 +371,12 @@ namespace LuaBindings { return playerIds; }); + callbacks.registerCallback("template", [clientWorld]() { + return clientWorld->currentTemplate()->store(); + }); + callbacks.registerCallback("setTemplate", [clientWorld](Json worldTemplate) { + clientWorld->setTemplate(worldTemplate); + }); } if (auto serverWorld = as<WorldServer>(world)) { @@ -455,6 +461,13 @@ namespace LuaBindings { }); return serverWorld->enqueuePlacement(std::move(distributions), id); }); + callbacks.registerCallback("template", [serverWorld]() { + return serverWorld->worldTemplate()->store(); + }); + callbacks.registerCallback("setTemplate", [serverWorld](Json worldTemplate) { + auto newTemplate = make_shared<WorldTemplate>(worldTemplate); + serverWorld->setTemplate(newTemplate); + }); } return callbacks; |