diff --git a/apps/openmw/mwmp/Cell.cpp b/apps/openmw/mwmp/Cell.cpp index 38e62b694..fa29dee7e 100644 --- a/apps/openmw/mwmp/Cell.cpp +++ b/apps/openmw/mwmp/Cell.cpp @@ -78,11 +78,14 @@ void Cell::initializeLocalActors() { MWWorld::Ptr ptr(&*listIter, 0); - std::string mapIndex = generateMapIndex(ptr); - localActors[mapIndex] = new LocalActor(); - localActors[mapIndex]->cell = esmCell; + LocalActor *actor = new LocalActor(); + actor->cell = esmCell; ptr.getBase()->isLocalActor = true; - localActors[mapIndex]->setPtr(ptr); + actor->setPtr(ptr); + + std::string mapIndex = generateMapIndex(ptr); + localActors[mapIndex] = actor; + LOG_APPEND(Log::LOG_INFO, "- Initialized LocalActor %s", mapIndex.c_str()); } } @@ -112,13 +115,14 @@ void Cell::readCellFrame(mwmp::WorldEvent& worldEvent) { MWWorld::Ptr ptrFound = store->searchExact(worldObject.refId, worldObject.refNumIndex, worldObject.mpNum); - if (ptrFound) - { - dedicatedActors[mapIndex] = new DedicatedActor(); - dedicatedActors[mapIndex]->cell = worldEvent.cell; - dedicatedActors[mapIndex]->setPtr(ptrFound); - LOG_APPEND(Log::LOG_INFO, "- Initialized DedicatedActor %s", mapIndex.c_str()); - } + if (!ptrFound) return; + + DedicatedActor *actor = new DedicatedActor(); + actor->cell = worldEvent.cell; + actor->setPtr(ptrFound); + dedicatedActors[mapIndex] = actor; + + LOG_APPEND(Log::LOG_INFO, "- Initialized DedicatedActor %s", mapIndex.c_str()); } // If this now exists, set its details diff --git a/apps/openmw/mwmp/CellController.cpp b/apps/openmw/mwmp/CellController.cpp index 90fedcb78..6a8d65e34 100644 --- a/apps/openmw/mwmp/CellController.cpp +++ b/apps/openmw/mwmp/CellController.cpp @@ -10,7 +10,7 @@ #include "LocalPlayer.hpp" using namespace mwmp; -std::deque CellController::cellsActive; +std::map CellController::cellsActive; mwmp::CellController::CellController() { @@ -24,14 +24,14 @@ mwmp::CellController::~CellController() void CellController::updateLocal() { - for (std::deque::iterator it = cellsActive.begin(); it != cellsActive.end();) + for (std::map::iterator it = cellsActive.begin(); it != cellsActive.end();) { - mwmp::Cell *mpCell = *it; + mwmp::Cell *mpCell = it->second; if (!MWBase::Environment::get().getWorld()->isCellActive(mpCell->getCellStore())) { mpCell->uninitializeLocalActors(); - it = cellsActive.erase(it); + cellsActive.erase(it++); } else { @@ -49,42 +49,34 @@ void CellController::initializeCellLocal(const ESM::Cell& cell) if (!cellStore) return; mwmp::Cell *mpCell = new mwmp::Cell(cellStore); - + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Initialized mwmp::Cell %s", mpCell->getDescription().c_str()); mpCell->initializeLocalActors(); - cellsActive.push_back(mpCell); + + std::string mapIndex = mpCell->getDescription(); + cellsActive[mapIndex] = mpCell; } void CellController::readCellFrame(mwmp::WorldEvent& worldEvent) { - bool cellExisted = false; + std::string mapIndex = worldEvent.cell.getDescription(); - // Check if this cell already exists - for (std::deque::iterator it = cellsActive.begin(); it != cellsActive.end(); ++it) - { - mwmp::Cell *mpCell = *it; - - if (worldEvent.cell.getDescription() == mpCell->getDescription()) - { - mpCell->readCellFrame(worldEvent); - cellExisted = true; - break; - } - } - - if (!cellExisted) + // If this key doesn't exist, create it + if (cellsActive.count(mapIndex) == 0) { MWWorld::CellStore *cellStore = getCell(worldEvent.cell); if (!cellStore) return; mwmp::Cell *mpCell = new mwmp::Cell(cellStore); + cellsActive[mapIndex] = mpCell; + } - LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Initialized mwmp::Cell %s", mpCell->getDescription().c_str()); - - cellsActive.push_back(mpCell); - mpCell->readCellFrame(worldEvent); + // If this now exists, send it the data + if (cellsActive.count(mapIndex) > 0) + { + cellsActive[mapIndex]->readCellFrame(worldEvent); } } diff --git a/apps/openmw/mwmp/CellController.hpp b/apps/openmw/mwmp/CellController.hpp index 517344927..77dc01c93 100644 --- a/apps/openmw/mwmp/CellController.hpp +++ b/apps/openmw/mwmp/CellController.hpp @@ -27,7 +27,7 @@ namespace mwmp void closeContainer(const MWWorld::Ptr& container); private: - static std::deque cellsActive; + static std::map cellsActive; }; }