From 3b604a432e558047cd6eea2b0769855ca21d934e Mon Sep 17 00:00:00 2001 From: David Cernat Date: Wed, 15 Feb 2017 20:14:25 +0200 Subject: [PATCH] [General] Restructure ID_CONTAINER to allow multiple containers at once --- apps/openmw-mp/Script/Functions/World.cpp | 4 +- apps/openmw-mp/Script/Functions/World.hpp | 62 +++++++++--------- apps/openmw/mwgui/container.cpp | 16 +++-- apps/openmw/mwmp/Networking.cpp | 2 +- apps/openmw/mwmp/WorldEvent.cpp | 16 ++--- apps/openmw/mwmp/WorldEvent.hpp | 1 - components/openmw-mp/Base/BaseEvent.hpp | 65 ++++++++++--------- .../Packets/World/PacketContainer.cpp | 56 ++++++++-------- 8 files changed, 110 insertions(+), 112 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/World.cpp b/apps/openmw-mp/Script/Functions/World.cpp index 8ccc77f43..c21354af2 100644 --- a/apps/openmw-mp/Script/Functions/World.cpp +++ b/apps/openmw-mp/Script/Functions/World.cpp @@ -67,9 +67,9 @@ void WorldFunctions::SetBaseEventCell(const char* cellDescription) noexcept } } -void WorldFunctions::SetContainerChangesAction(int action) noexcept +void WorldFunctions::SetBaseEventAction(int action) noexcept { - baseEvent->containerChanges.action = action; + baseEvent->action = action; } void WorldFunctions::SetObjectRefId(const char* refId) noexcept diff --git a/apps/openmw-mp/Script/Functions/World.hpp b/apps/openmw-mp/Script/Functions/World.hpp index 4cda3a16e..6f1b95092 100644 --- a/apps/openmw-mp/Script/Functions/World.hpp +++ b/apps/openmw-mp/Script/Functions/World.hpp @@ -2,41 +2,41 @@ #define OPENMW_WORLD_HPP #define WORLDFUNCTIONS \ - {"CreateBaseEvent", WorldFunctions::CreateBaseEvent},\ + {"CreateBaseEvent", WorldFunctions::CreateBaseEvent},\ \ - {"AddWorldObject", WorldFunctions::AddWorldObject},\ - {"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\ - {"SetContainerChangesAction", WorldFunctions::SetContainerChangesAction},\ + {"AddWorldObject", WorldFunctions::AddWorldObject},\ + {"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\ + {"SetBaseEventAction", WorldFunctions::SetBaseEventAction},\ \ - {"SetObjectRefId", WorldFunctions::SetObjectRefId},\ - {"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\ - {"SetObjectCharge", WorldFunctions::SetObjectCharge},\ - {"SetObjectCount", WorldFunctions::SetObjectCount},\ - {"SetObjectGoldValue", WorldFunctions::SetObjectGoldValue},\ - {"SetObjectScale", WorldFunctions::SetObjectScale},\ - {"SetObjectDoorState", WorldFunctions::SetObjectDoorState},\ - {"SetObjectLockLevel", WorldFunctions::SetObjectLockLevel},\ - {"SetObjectPosition", WorldFunctions::SetObjectPosition},\ - {"SetObjectRotation", WorldFunctions::SetObjectRotation},\ + {"SetObjectRefId", WorldFunctions::SetObjectRefId},\ + {"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\ + {"SetObjectCharge", WorldFunctions::SetObjectCharge},\ + {"SetObjectCount", WorldFunctions::SetObjectCount},\ + {"SetObjectGoldValue", WorldFunctions::SetObjectGoldValue},\ + {"SetObjectScale", WorldFunctions::SetObjectScale},\ + {"SetObjectDoorState", WorldFunctions::SetObjectDoorState},\ + {"SetObjectLockLevel", WorldFunctions::SetObjectLockLevel},\ + {"SetObjectPosition", WorldFunctions::SetObjectPosition},\ + {"SetObjectRotation", WorldFunctions::SetObjectRotation},\ \ - {"GetObjectChangesSize", WorldFunctions::GetObjectChangesSize},\ + {"GetObjectChangesSize", WorldFunctions::GetObjectChangesSize},\ \ - {"GetObjectRefId", WorldFunctions::GetObjectRefId},\ - {"GetObjectRefNumIndex", WorldFunctions::GetObjectRefNumIndex},\ - {"GetObjectCharge", WorldFunctions::GetObjectCharge},\ - {"GetObjectCount", WorldFunctions::GetObjectCount},\ - {"GetObjectGoldValue", WorldFunctions::GetObjectGoldValue},\ - {"GetObjectScale", WorldFunctions::GetObjectScale},\ - {"GetObjectDoorState", WorldFunctions::GetObjectDoorState},\ - {"GetObjectLockLevel", WorldFunctions::GetObjectLockLevel},\ - {"GetObjectPosX", WorldFunctions::GetObjectPosX},\ - {"GetObjectPosY", WorldFunctions::GetObjectPosY},\ - {"GetObjectPosZ", WorldFunctions::GetObjectPosZ},\ - {"GetObjectRotX", WorldFunctions::GetObjectRotX},\ - {"GetObjectRotY", WorldFunctions::GetObjectRotY},\ - {"GetObjectRotZ", WorldFunctions::GetObjectRotZ},\ + {"GetObjectRefId", WorldFunctions::GetObjectRefId},\ + {"GetObjectRefNumIndex", WorldFunctions::GetObjectRefNumIndex},\ + {"GetObjectCharge", WorldFunctions::GetObjectCharge},\ + {"GetObjectCount", WorldFunctions::GetObjectCount},\ + {"GetObjectGoldValue", WorldFunctions::GetObjectGoldValue},\ + {"GetObjectScale", WorldFunctions::GetObjectScale},\ + {"GetObjectDoorState", WorldFunctions::GetObjectDoorState},\ + {"GetObjectLockLevel", WorldFunctions::GetObjectLockLevel},\ + {"GetObjectPosX", WorldFunctions::GetObjectPosX},\ + {"GetObjectPosY", WorldFunctions::GetObjectPosY},\ + {"GetObjectPosZ", WorldFunctions::GetObjectPosZ},\ + {"GetObjectRotX", WorldFunctions::GetObjectRotX},\ + {"GetObjectRotY", WorldFunctions::GetObjectRotY},\ + {"GetObjectRotZ", WorldFunctions::GetObjectRotZ},\ \ - {"SendContainer", WorldFunctions::SendContainer},\ + {"SendContainer", WorldFunctions::SendContainer},\ \ {"SendObjectDelete", WorldFunctions::SendObjectDelete},\ {"SendObjectPlace", WorldFunctions::SendObjectPlace},\ @@ -57,7 +57,7 @@ public: static void AddWorldObject() noexcept; static void SetBaseEventCell(const char* cellDescription) noexcept; - static void SetContainerChangesAction(int action) noexcept; + static void SetBaseEventAction(int action) noexcept; static void SetObjectRefId(const char* refId) noexcept; static void SetObjectRefNumIndex(int refNumIndex) noexcept; diff --git a/apps/openmw/mwgui/container.cpp b/apps/openmw/mwgui/container.cpp index a5e2cae87..1a38e000d 100644 --- a/apps/openmw/mwgui/container.cpp +++ b/apps/openmw/mwgui/container.cpp @@ -104,11 +104,11 @@ namespace MWGui // Added by tes3mp mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent(); event->cell = *mPtr.getCell()->getCell(); + event->action = mwmp::BaseEvent::REMOVE; mwmp::WorldObject worldObject; worldObject.refId = mPtr.getCellRef().getRefId(); worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex; - event->addObject(worldObject); MWWorld::Ptr itemPtr = mModel->getItem(mSelectedItem).mBase; @@ -119,8 +119,9 @@ namespace MWGui containerItem.goldValue = itemPtr.getCellRef().getGoldValue(); containerItem.owner = itemPtr.getCellRef().getOwner(); containerItem.actionCount = count; - event->addContainerItem(containerItem); - event->containerChanges.action = mwmp::ContainerChanges::REMOVE; + + worldObject.containerChanges.items.push_back(containerItem); + event->addObject(worldObject); mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event); @@ -163,11 +164,11 @@ namespace MWGui // Added by tes3mp mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent(); event->cell = *mPtr.getCell()->getCell(); + event->action = mwmp::BaseEvent::ADD; mwmp::WorldObject worldObject; worldObject.refId = mPtr.getCellRef().getRefId(); worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex; - event->addObject(worldObject); MWWorld::Ptr itemPtr = mDragAndDrop->mItem.mBase; @@ -180,8 +181,9 @@ namespace MWGui containerItem.charge = itemPtr.getCellRef().getCharge(); containerItem.goldValue = itemPtr.getCellRef().getGoldValue(); containerItem.owner = itemPtr.getCellRef().getOwner(); - event->addContainerItem(containerItem); - event->containerChanges.action = mwmp::ContainerChanges::ADD; + + worldObject.containerChanges.items.push_back(containerItem); + event->addObject(worldObject); mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event); @@ -317,12 +319,12 @@ namespace MWGui // Added by tes3mp mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent(); event->cell = *mPtr.getCell()->getCell(); + event->action = mwmp::BaseEvent::SET; mwmp::WorldObject worldObject; worldObject.refId = mPtr.getCellRef().getRefId(); worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex; event->addObject(worldObject); - event->containerChanges.action = mwmp::ContainerChanges::SET; mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event); diff --git a/apps/openmw/mwmp/Networking.cpp b/apps/openmw/mwmp/Networking.cpp index b26b617ce..b592f9fc3 100644 --- a/apps/openmw/mwmp/Networking.cpp +++ b/apps/openmw/mwmp/Networking.cpp @@ -749,7 +749,7 @@ void Networking::processWorldPacket(RakNet::Packet *packet) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_CONTAINER"); // If we've received a request for information, comply with it - if (event->containerChanges.action == mwmp::ContainerChanges::REQUEST) + if (event->action == mwmp::BaseEvent::REQUEST) event->sendContainers(ptrCellStore); // Otherwise, edit containers based on the information received else diff --git a/apps/openmw/mwmp/WorldEvent.cpp b/apps/openmw/mwmp/WorldEvent.cpp index 1faed8b3d..b44eda4ab 100644 --- a/apps/openmw/mwmp/WorldEvent.cpp +++ b/apps/openmw/mwmp/WorldEvent.cpp @@ -40,11 +40,6 @@ void WorldEvent::addObject(WorldObject worldObject) objectChanges.objects.push_back(worldObject); } -void WorldEvent::addContainerItem(ContainerItem containerItem) -{ - containerChanges.items.push_back(containerItem); -} - void WorldEvent::sendContainers(MWWorld::CellStore* cellStore) { MWWorld::CellRefList *containerList = cellStore->getContainers(); @@ -86,21 +81,20 @@ void WorldEvent::editContainers(MWWorld::CellStore* cellStore) ptrFound.getCellRef().getRefNum()); MWWorld::ContainerStore& containerStore = ptrFound.getClass().getContainerStore(ptrFound); - int action = containerChanges.action; // If we are setting the entire contents, clear the current ones - if (action == ContainerChanges::SET) + if (action == BaseEvent::SET) containerStore.clear(); - for (unsigned int i = 0; i < containerChanges.count; i++) + for (unsigned int i = 0; i < worldObject.containerChanges.count; i++) { - ContainerItem containerItem = containerChanges.items.at(i); + ContainerItem containerItem = worldObject.containerChanges.items.at(i); MWWorld::Ptr ownerPtr = MWBase::Environment::get().getWorld()->searchPtr(containerItem.owner, false); if (ownerPtr.isEmpty()) ownerPtr = MWBase::Environment::get().getWorld()->getPlayerPtr(); - if (action == ContainerChanges::ADD || action == ContainerChanges::SET) + if (action == BaseEvent::ADD || action == BaseEvent::SET) { // Create a ManualRef to be able to set item charge MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), containerItem.refId, 1); @@ -116,7 +110,7 @@ void WorldEvent::editContainers(MWWorld::CellStore* cellStore) containerStore.add(newPtr, containerItem.count, ownerPtr, true); } - else if (action == ContainerChanges::REMOVE) + else if (action == BaseEvent::REMOVE) { // We have to find the right item ourselves because ContainerStore has no method // accounting for charge diff --git a/apps/openmw/mwmp/WorldEvent.hpp b/apps/openmw/mwmp/WorldEvent.hpp index 22edc527e..83690bce7 100644 --- a/apps/openmw/mwmp/WorldEvent.hpp +++ b/apps/openmw/mwmp/WorldEvent.hpp @@ -16,7 +16,6 @@ namespace mwmp virtual ~WorldEvent(); void addObject(WorldObject worldObject); - void addContainerItem(ContainerItem containerItem); void sendContainers(MWWorld::CellStore* cellStore); diff --git a/components/openmw-mp/Base/BaseEvent.hpp b/components/openmw-mp/Base/BaseEvent.hpp index 66368b518..094b6cebc 100644 --- a/components/openmw-mp/Base/BaseEvent.hpp +++ b/components/openmw-mp/Base/BaseEvent.hpp @@ -7,6 +7,28 @@ namespace mwmp { + struct ContainerItem + { + std::string refId; + int count; + int charge; + int goldValue; + + std::string owner; + int actionCount; + + inline bool operator==(const ContainerItem& rhs) + { + return refId == rhs.refId && count == rhs.count && charge == rhs.charge && goldValue && rhs.goldValue; + } + }; + + struct ContainerChanges + { + std::vector items; + unsigned int count; + }; + struct WorldObject { std::string refId; @@ -30,22 +52,8 @@ namespace mwmp int shortVal; float floatVal; std::string varName; - }; - - struct ContainerItem - { - std::string refId; - int count; - int charge; - int goldValue; - std::string owner; - int actionCount; - - inline bool operator==(const ContainerItem& rhs) - { - return refId == rhs.refId && count == rhs.count && charge == rhs.charge && goldValue && rhs.goldValue; - } + ContainerChanges containerChanges; }; struct ObjectChanges @@ -54,22 +62,6 @@ namespace mwmp unsigned int count; }; - struct ContainerChanges - { - std::vector items; - unsigned int count; - - enum CONTAINER_ACTION - { - SET = 0, - ADD = 1, - REMOVE = 2, - REQUEST = 3 - }; - - int action; // 0 - Clear and set in entirety, 1 - Add item, 2 - Remove item, 3 - Request items - }; - class BaseEvent { public: @@ -84,11 +76,20 @@ namespace mwmp } + enum WORLD_ACTION + { + SET = 0, + ADD = 1, + REMOVE = 2, + REQUEST = 3 + }; + RakNet::RakNetGUID guid; ObjectChanges objectChanges; - ContainerChanges containerChanges; ESM::Cell cell; + + int action; // 0 - Clear and set in entirety, 1 - Add item, 2 - Remove item, 3 - Request items }; } diff --git a/components/openmw-mp/Packets/World/PacketContainer.cpp b/components/openmw-mp/Packets/World/PacketContainer.cpp index 5ac903af4..553ce4650 100644 --- a/components/openmw-mp/Packets/World/PacketContainer.cpp +++ b/components/openmw-mp/Packets/World/PacketContainer.cpp @@ -12,21 +12,18 @@ void PacketContainer::Packet(RakNet::BitStream *bs, BaseEvent *event, bool send) { WorldPacket::Packet(bs, event, send); - RW(event->containerChanges.action, send); + RW(event->action, send); - if (!send) + if (send) { - event->objectChanges.objects.clear(); - event->containerChanges.items.clear(); + event->objectChanges.count = (unsigned int)(event->objectChanges.objects.size()); } else { - event->objectChanges.count = (unsigned int)(event->objectChanges.objects.size()); - event->containerChanges.count = (unsigned int)(event->containerChanges.items.size()); + event->objectChanges.objects.clear(); } RW(event->objectChanges.count, send); - RW(event->containerChanges.count, send); RW(event->cell.mData.mFlags, send); RW(event->cell.mData.mX, send); @@ -40,37 +37,42 @@ void PacketContainer::Packet(RakNet::BitStream *bs, BaseEvent *event, bool send) if (send) { worldObject = event->objectChanges.objects.at(i); + worldObject.containerChanges.count = (unsigned int)(worldObject.containerChanges.items.size()); + } + else + { + worldObject.containerChanges.items.clear(); } RW(worldObject.refId, send); RW(worldObject.refNumIndex, send); + RW(worldObject.containerChanges.count, send); - if (!send) - { - event->objectChanges.objects.push_back(worldObject); - } - } + ContainerItem containerItem; - ContainerItem containerItem; - - for (unsigned int i = 0; i < event->containerChanges.count; i++) - { - if (send) + for (unsigned int i = 0; i < worldObject.containerChanges.count; i++) { - containerItem = event->containerChanges.items.at(i); + if (send) + { + containerItem = worldObject.containerChanges.items.at(i); + } + + RW(containerItem.refId, send); + RW(containerItem.count, send); + RW(containerItem.charge, send); + RW(containerItem.goldValue, send); + RW(containerItem.owner, send); + RW(containerItem.actionCount, send); + + if (!send) + { + worldObject.containerChanges.items.push_back(containerItem); + } } - RW(containerItem.refId, send); - RW(containerItem.count, send); - RW(containerItem.charge, send); - RW(containerItem.goldValue, send); - RW(containerItem.owner, send); - RW(containerItem.actionCount, send); - if (!send) { - event->containerChanges.items.push_back(containerItem); + event->objectChanges.objects.push_back(worldObject); } } - }