diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 814ed026b..6a851576b 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -96,7 +96,7 @@ add_openmw_dir (mwbase inputmanager windowmanager statemanager ) -add_openmw_dir (mwmp Main Networking LocalPlayer DedicatedPlayer LocalActor DedicatedActor WorldEvent CellController GUIChat GUILogin GUIController PlayerMarkerCollection GUIDialogList) +add_openmw_dir (mwmp Main Networking LocalPlayer DedicatedPlayer LocalActor DedicatedActor WorldEvent Cell CellController GUIChat GUILogin GUIController PlayerMarkerCollection GUIDialogList) # Main executable diff --git a/apps/openmw/mwmp/Cell.cpp b/apps/openmw/mwmp/Cell.cpp new file mode 100644 index 000000000..d26586e17 --- /dev/null +++ b/apps/openmw/mwmp/Cell.cpp @@ -0,0 +1,79 @@ +#include "../mwworld/worldimp.hpp" +#include +#include + +#include "Cell.hpp" +#include "Main.hpp" +#include "LocalPlayer.hpp" +using namespace mwmp; + +mwmp::Cell::Cell(MWWorld::CellStore* cellStore) +{ + store = cellStore; + + std::map localActors; + std::map dedicatedActors; +} + +mwmp::Cell::~Cell() +{ + +} + +void Cell::update() +{ + for (std::map::iterator it = localActors.begin(); it != localActors.end();) + { + LocalActor *actor = it->second; + + // TODO:: Make sure this condition actually works + if (actor->getPtr().getCell() != nullptr && actor->getPtr().getCell() != store) + { + LOG_APPEND(Log::LOG_INFO, "- Removing LocalActor %s which is no longer in this cell", it->first.c_str()); + localActors.erase(it++); + } + else + { + LOG_APPEND(Log::LOG_VERBOSE, "- Updating LocalActor %s", it->first.c_str()); + actor->update(); + ++it; + } + } +} + +void Cell::initializeLocalActors() +{ + ESM::Cell esmCell = *store->getCell(); + MWWorld::CellRefList *npcList = store->getNpcs(); + + for (typename MWWorld::CellRefList::List::iterator listIter(npcList->mList.begin()); + listIter != npcList->mList.end(); ++listIter) + { + MWWorld::Ptr ptr(&*listIter, 0); + + std::string mapIndex = generateMapIndex(ptr); + localActors[mapIndex] = new LocalActor(); + localActors[mapIndex]->cell = esmCell; + localActors[mapIndex]->setPtr(ptr); + LOG_APPEND(Log::LOG_INFO, "- Initialized LocalActor %s", mapIndex.c_str()); + } +} + +std::string Cell::generateMapIndex(MWWorld::Ptr ptr) +{ + std::string mapIndex = ""; + mapIndex += ptr.getCellRef().getRefId(); + mapIndex += "-" + std::to_string(ptr.getCellRef().getRefNum().mIndex); + mapIndex += "-" + std::to_string(ptr.getCellRef().getMpNum()); + return mapIndex; +} + +MWWorld::CellStore *mwmp::Cell::getCellStore() +{ + return store; +} + +std::string mwmp::Cell::getDescription() +{ + return store->getCell()->getDescription(); +} diff --git a/apps/openmw/mwmp/Cell.hpp b/apps/openmw/mwmp/Cell.hpp new file mode 100644 index 000000000..34db2ab9f --- /dev/null +++ b/apps/openmw/mwmp/Cell.hpp @@ -0,0 +1,32 @@ +#ifndef OPENMW_CELL_HPP +#define OPENMW_CELL_HPP + +#include "LocalActor.hpp" +#include "DedicatedActor.hpp" +#include "../mwworld/cellstore.hpp" + +namespace mwmp +{ + class Cell + { + public: + + Cell(MWWorld::CellStore* cellStore); + ~Cell(); + + void update(); + void initializeLocalActors(); + + std::string generateMapIndex(MWWorld::Ptr ptr); + + MWWorld::CellStore* getCellStore(); + std::string getDescription(); + + private: + MWWorld::CellStore* store; + std::map localActors; + std::map dedicatedActors; + }; +} + +#endif //OPENMW_CELL_HPP diff --git a/apps/openmw/mwmp/CellController.cpp b/apps/openmw/mwmp/CellController.cpp index 725edecb9..7d7205abd 100644 --- a/apps/openmw/mwmp/CellController.cpp +++ b/apps/openmw/mwmp/CellController.cpp @@ -10,8 +10,7 @@ #include "LocalPlayer.hpp" using namespace mwmp; -std::map CellController::localActors; -std::map CellController::dedicatedActors; +std::deque CellController::cellsActive; mwmp::CellController::CellController() { @@ -25,49 +24,36 @@ mwmp::CellController::~CellController() void CellController::update() { - for (std::map::iterator it = localActors.begin(); it != localActors.end();) + for (std::deque::iterator it = cellsActive.begin(); it != cellsActive.end();) { - LocalActor *actor = it->second; + mwmp::Cell *mpCell = *it; - if (!MWBase::Environment::get().getWorld()->isCellActive(getCell(actor->cell))) + if (!MWBase::Environment::get().getWorld()->isCellActive(mpCell->getCellStore())) { - localActors.erase(it++); + it = cellsActive.erase(it); } else { - printf("Updating %s\n", it->first.c_str()); + //LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Updating mwmp::Cell %s", mpCell->getDescription().c_str()); + mpCell->update(); ++it; } } } -void CellController::initializeLocalActors(const ESM::Cell& cell) +void CellController::initializeCell(const ESM::Cell& cell) { + MWWorld::CellStore *cellStore = getCell(cell); if (!cellStore) return; - MWWorld::CellRefList *npcList = cellStore->getNpcs(); + mwmp::Cell *mpCell = new mwmp::Cell(cellStore); + + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Initialized mwmp::Cell %s", mpCell->getDescription().c_str()); - for (typename MWWorld::CellRefList::List::iterator listIter(npcList->mList.begin()); - listIter != npcList->mList.end(); ++listIter) - { - MWWorld::Ptr ptr(&*listIter, 0); - - std::string mapIndex = generateMapIndex(ptr); - localActors[mapIndex] = new LocalActor(); - localActors[mapIndex]->cell = cell; - printf("Initialized local actor %s\n", mapIndex.c_str()); - } -} - -std::string CellController::generateMapIndex(MWWorld::Ptr ptr) -{ - std::string mapIndex = ""; - mapIndex += ptr.getCellRef().getRefId(); - mapIndex += "-" + std::to_string(ptr.getCellRef().getRefNum().mIndex); - mapIndex += "-" + std::to_string(ptr.getCellRef().getMpNum()); - return mapIndex; + mpCell->initializeLocalActors(); + cellsActive.push_back(mpCell); } int mwmp::CellController::getCellSize() const diff --git a/apps/openmw/mwmp/CellController.hpp b/apps/openmw/mwmp/CellController.hpp index dc72e9dfc..9077ca53c 100644 --- a/apps/openmw/mwmp/CellController.hpp +++ b/apps/openmw/mwmp/CellController.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_CELLCONTROLLER_HPP #define OPENMW_CELLCONTROLLER_HPP +#include "Cell.hpp" #include "LocalActor.hpp" #include "DedicatedActor.hpp" #include "../mwworld/cellstore.hpp" @@ -15,9 +16,8 @@ namespace mwmp ~CellController(); void update(); - void initializeLocalActors(const ESM::Cell& cell); + void initializeCell(const ESM::Cell& cell); - std::string generateMapIndex(MWWorld::Ptr ptr); int getCellSize() const; virtual MWWorld::CellStore *getCell(const ESM::Cell& cell); @@ -25,8 +25,7 @@ namespace mwmp void closeContainer(const MWWorld::Ptr& container); private: - static std::map localActors; - static std::map dedicatedActors; + static std::deque cellsActive; }; } diff --git a/apps/openmw/mwmp/LocalActor.cpp b/apps/openmw/mwmp/LocalActor.cpp index c278acf7e..dd6e215f3 100644 --- a/apps/openmw/mwmp/LocalActor.cpp +++ b/apps/openmw/mwmp/LocalActor.cpp @@ -1,3 +1,5 @@ +#include + #include "LocalActor.hpp" using namespace mwmp; @@ -17,3 +19,13 @@ void LocalActor::update() { } + +MWWorld::Ptr LocalActor::getPtr() +{ + return ptr; +} + +void LocalActor::setPtr(const MWWorld::Ptr& newPtr) +{ + ptr = newPtr; +} diff --git a/apps/openmw/mwmp/LocalActor.hpp b/apps/openmw/mwmp/LocalActor.hpp index 9b3565153..3eab1a8d7 100644 --- a/apps/openmw/mwmp/LocalActor.hpp +++ b/apps/openmw/mwmp/LocalActor.hpp @@ -2,6 +2,7 @@ #define OPENMW_LOCALACTOR_HPP #include +#include "../mwworld/manualref.hpp" namespace mwmp { @@ -13,6 +14,12 @@ namespace mwmp virtual ~LocalActor(); void update(); + + MWWorld::Ptr getPtr(); + void setPtr(const MWWorld::Ptr& newPtr); + + private: + MWWorld::Ptr ptr; }; } diff --git a/apps/openmw/mwmp/Networking.cpp b/apps/openmw/mwmp/Networking.cpp index f6f9226c4..3de347111 100644 --- a/apps/openmw/mwmp/Networking.cpp +++ b/apps/openmw/mwmp/Networking.cpp @@ -859,7 +859,7 @@ void Networking::processWorldPacket(RakNet::Packet *packet) { LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Received ID_ACTOR_AUTHORITY about %s", worldEvent.cell.getDescription().c_str()); - Main::get().getCellController()->initializeLocalActors(worldEvent.cell); + Main::get().getCellController()->initializeCell(worldEvent.cell); break; } diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp index cf25c0ebb..d2ae702d8 100644 --- a/apps/openmw/mwworld/worldimp.hpp +++ b/apps/openmw/mwworld/worldimp.hpp @@ -539,7 +539,7 @@ namespace MWWorld Make it possible to check whether a cell is active */ - virtual bool isCellActive(MWWorld::CellStore* cell); + virtual bool isCellActive(MWWorld::CellStore* cell); /* End of tes3mp addition */