From 10be52d40d5ca083cf228f66ff1427f1c86c281f Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 7 Apr 2017 08:12:50 +0300 Subject: [PATCH] [Client] Add localActorsToCells map to more easily find LocalActors --- apps/openmw/mwmp/Cell.cpp | 42 ++++++++--------------- apps/openmw/mwmp/Cell.hpp | 3 -- apps/openmw/mwmp/CellController.cpp | 53 ++++++++++++++++++++++++----- apps/openmw/mwmp/CellController.hpp | 8 +++++ 4 files changed, 67 insertions(+), 39 deletions(-) diff --git a/apps/openmw/mwmp/Cell.cpp b/apps/openmw/mwmp/Cell.cpp index fa29dee7e..3b4228581 100644 --- a/apps/openmw/mwmp/Cell.cpp +++ b/apps/openmw/mwmp/Cell.cpp @@ -1,9 +1,9 @@ #include "../mwworld/worldimp.hpp" #include #include -#include #include "Cell.hpp" +#include "CellController.hpp" #include "Main.hpp" #include "Networking.hpp" #include "LocalPlayer.hpp" @@ -26,7 +26,7 @@ void Cell::updateLocal() { if (localActors.empty()) return; - mwmp::WorldEvent *worldEvent = mwmp::Main::get().getNetworking()->getWorldEvent(); + WorldEvent *worldEvent = mwmp::Main::get().getNetworking()->getWorldEvent(); worldEvent->reset(); worldEvent->cell = *store->getCell(); @@ -47,7 +47,7 @@ void Cell::updateLocal() actor->update(); MWWorld::Ptr ptr = actor->getPtr(); - mwmp::WorldObject worldObject; + WorldObject worldObject; worldObject.refId = ptr.getCellRef().getRefId(); worldObject.refNumIndex = ptr.getCellRef().getRefNum().mIndex; worldObject.mpNum = ptr.getCellRef().getMpNum(); @@ -64,8 +64,8 @@ void Cell::updateLocal() } } - mwmp::Main::get().getNetworking()->getWorldPacket(ID_ACTOR_FRAME)->setEvent(worldEvent); - mwmp::Main::get().getNetworking()->getWorldPacket(ID_ACTOR_FRAME)->Send(); + Main::get().getNetworking()->getWorldPacket(ID_ACTOR_FRAME)->setEvent(worldEvent); + Main::get().getNetworking()->getWorldPacket(ID_ACTOR_FRAME)->Send(); } void Cell::initializeLocalActors() @@ -83,8 +83,10 @@ void Cell::initializeLocalActors() ptr.getBase()->isLocalActor = true; actor->setPtr(ptr); - std::string mapIndex = generateMapIndex(ptr); + std::string mapIndex = Main::get().getCellController()->generateMapIndex(ptr); localActors[mapIndex] = actor; + + Main::get().getCellController()->setLocalActorRecord(mapIndex, getDescription()); LOG_APPEND(Log::LOG_INFO, "- Initialized LocalActor %s", mapIndex.c_str()); } @@ -96,19 +98,21 @@ void Cell::uninitializeLocalActors() { LocalActor *actor = it->second; actor->getPtr().getBase()->isLocalActor = false; + + Main::get().getCellController()->removeLocalActorRecord(it->first); } localActors.clear(); } -void Cell::readCellFrame(mwmp::WorldEvent& worldEvent) +void Cell::readCellFrame(WorldEvent& worldEvent) { WorldObject worldObject; for (unsigned int i = 0; i < worldEvent.objectChanges.count; i++) { worldObject = worldEvent.objectChanges.objects.at(i); - std::string mapIndex = generateMapIndex(worldObject); + std::string mapIndex = Main::get().getCellController()->generateMapIndex(worldObject); // If this key doesn't exist, create it if (dedicatedActors.count(mapIndex) == 0) @@ -143,30 +147,12 @@ void Cell::readCellFrame(mwmp::WorldEvent& worldEvent) } } -std::string Cell::generateMapIndex(MWWorld::Ptr ptr) -{ - std::string mapIndex = ""; - mapIndex += ptr.getCellRef().getRefId(); - mapIndex += "-" + Utils::toString(ptr.getCellRef().getRefNum().mIndex); - mapIndex += "-" + Utils::toString(ptr.getCellRef().getMpNum()); - return mapIndex; -} - -std::string Cell::generateMapIndex(mwmp::WorldObject object) -{ - std::string mapIndex = ""; - mapIndex += object.refId; - mapIndex += "-" + Utils::toString(object.refNumIndex); - mapIndex += "-" + Utils::toString(object.mpNum); - return mapIndex; -} - -MWWorld::CellStore *mwmp::Cell::getCellStore() +MWWorld::CellStore *Cell::getCellStore() { return store; } -std::string mwmp::Cell::getDescription() +std::string Cell::getDescription() { return store->getCell()->getDescription(); } diff --git a/apps/openmw/mwmp/Cell.hpp b/apps/openmw/mwmp/Cell.hpp index f867f4d77..c470df236 100644 --- a/apps/openmw/mwmp/Cell.hpp +++ b/apps/openmw/mwmp/Cell.hpp @@ -20,9 +20,6 @@ namespace mwmp void uninitializeLocalActors(); void readCellFrame(mwmp::WorldEvent& worldEvent); - std::string generateMapIndex(MWWorld::Ptr ptr); - std::string generateMapIndex(mwmp::WorldObject object); - MWWorld::CellStore* getCellStore(); std::string getDescription(); diff --git a/apps/openmw/mwmp/CellController.cpp b/apps/openmw/mwmp/CellController.cpp index 6a8d65e34..dcdf65d38 100644 --- a/apps/openmw/mwmp/CellController.cpp +++ b/apps/openmw/mwmp/CellController.cpp @@ -2,6 +2,7 @@ #include "../mwworld/worldimp.hpp" #include #include +#include #include "../mwworld/containerstore.hpp" #include "../mwworld/class.hpp" @@ -11,6 +12,7 @@ using namespace mwmp; std::map CellController::cellsActive; +std::map CellController::localActorsToCells; mwmp::CellController::CellController() { @@ -58,7 +60,7 @@ void CellController::initializeCellLocal(const ESM::Cell& cell) cellsActive[mapIndex] = mpCell; } -void CellController::readCellFrame(mwmp::WorldEvent& worldEvent) +void CellController::readCellFrame(WorldEvent& worldEvent) { std::string mapIndex = worldEvent.cell.getDescription(); @@ -80,12 +82,47 @@ void CellController::readCellFrame(mwmp::WorldEvent& worldEvent) } } -int mwmp::CellController::getCellSize() const +void CellController::setLocalActorRecord(std::string actorIndex, std::string cellIndex) +{ + localActorsToCells[actorIndex] = cellIndex; +} + +void CellController::removeLocalActorRecord(std::string actorIndex) +{ + localActorsToCells.erase(actorIndex); +} + +bool CellController::hasLocalActorRecord(MWWorld::Ptr ptr) +{ + std::string mapIndex = generateMapIndex(ptr); + + return (localActorsToCells.count(mapIndex) > 0); +} + +std::string CellController::generateMapIndex(MWWorld::Ptr ptr) +{ + std::string mapIndex = ""; + mapIndex += ptr.getCellRef().getRefId(); + mapIndex += "-" + Utils::toString(ptr.getCellRef().getRefNum().mIndex); + mapIndex += "-" + Utils::toString(ptr.getCellRef().getMpNum()); + return mapIndex; +} + +std::string CellController::generateMapIndex(WorldObject object) +{ + std::string mapIndex = ""; + mapIndex += object.refId; + mapIndex += "-" + Utils::toString(object.refNumIndex); + mapIndex += "-" + Utils::toString(object.mpNum); + return mapIndex; +} + +int CellController::getCellSize() const { return 8192; } -MWWorld::CellStore *mwmp::CellController::getCell(const ESM::Cell& cell) +MWWorld::CellStore *CellController::getCell(const ESM::Cell& cell) { MWWorld::CellStore *cellStore; @@ -107,10 +144,10 @@ MWWorld::CellStore *mwmp::CellController::getCell(const ESM::Cell& cell) } -void mwmp::CellController::openContainer(const MWWorld::Ptr &container, bool loot) +void CellController::openContainer(const MWWorld::Ptr &container, bool loot) { // Record this as the player's current open container - mwmp::Main::get().getLocalPlayer()->storeCurrentContainer(container, loot); + Main::get().getLocalPlayer()->storeCurrentContainer(container, loot); LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Container \"%s\" (%d) is opened. Loot: %s", container.getCellRef().getRefId().c_str(), container.getCellRef().getRefNum().mIndex, @@ -130,9 +167,9 @@ void mwmp::CellController::openContainer(const MWWorld::Ptr &container, bool loo } -void mwmp::CellController::closeContainer(const MWWorld::Ptr &container) +void CellController::closeContainer(const MWWorld::Ptr &container) { - mwmp::Main::get().getLocalPlayer()->clearCurrentContainer(); + Main::get().getLocalPlayer()->clearCurrentContainer(); // If the player died while in a container, the container's Ptr could be invalid now if (!container.isEmpty()) @@ -148,5 +185,5 @@ void mwmp::CellController::closeContainer(const MWWorld::Ptr &container) } } - mwmp::Main::get().getLocalPlayer()->updateInventory(); + Main::get().getLocalPlayer()->updateInventory(); } diff --git a/apps/openmw/mwmp/CellController.hpp b/apps/openmw/mwmp/CellController.hpp index 77dc01c93..da6e5e4ff 100644 --- a/apps/openmw/mwmp/CellController.hpp +++ b/apps/openmw/mwmp/CellController.hpp @@ -20,6 +20,13 @@ namespace mwmp void initializeCellLocal(const ESM::Cell& cell); void readCellFrame(mwmp::WorldEvent& worldEvent); + void setLocalActorRecord(std::string actorIndex, std::string cellIndex); + void removeLocalActorRecord(std::string actorIndex); + bool hasLocalActorRecord(MWWorld::Ptr ptr); + + std::string generateMapIndex(MWWorld::Ptr ptr); + std::string generateMapIndex(mwmp::WorldObject object); + int getCellSize() const; virtual MWWorld::CellStore *getCell(const ESM::Cell& cell); @@ -28,6 +35,7 @@ namespace mwmp private: static std::map cellsActive; + static std::map localActorsToCells; }; }