From 871d8c8308642d7324c8172aec60848481853e14 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 23 Apr 2017 13:59:15 +0300 Subject: [PATCH] [Client] Send and read ActorCellChange packets --- apps/openmw/mwmp/ActorList.cpp | 16 +++ apps/openmw/mwmp/ActorList.hpp | 3 + apps/openmw/mwmp/Cell.cpp | 103 +++++++++++++----- apps/openmw/mwmp/Cell.hpp | 10 +- apps/openmw/mwmp/CellController.cpp | 13 +++ apps/openmw/mwmp/CellController.hpp | 1 + apps/openmw/mwmp/DedicatedActor.cpp | 10 ++ apps/openmw/mwmp/DedicatedActor.hpp | 1 + apps/openmw/mwmp/LocalActor.cpp | 12 ++ apps/openmw/mwmp/LocalActor.hpp | 1 + .../actor/ProcessorActorCellChange.hpp | 2 +- 11 files changed, 138 insertions(+), 34 deletions(-) diff --git a/apps/openmw/mwmp/ActorList.cpp b/apps/openmw/mwmp/ActorList.cpp index d1b01acad..28ceb7003 100644 --- a/apps/openmw/mwmp/ActorList.cpp +++ b/apps/openmw/mwmp/ActorList.cpp @@ -33,6 +33,7 @@ void ActorList::reset() speechActors.clear(); statsDynamicActors.clear(); attackActors.clear(); + cellChangeActors.clear(); guid = mwmp::Main::get().getNetworking()->getLocalPlayer()->guid; } @@ -76,6 +77,11 @@ void ActorList::addAttackActor(LocalActor localActor) attackActors.push_back(localActor); } +void ActorList::addCellChangeActor(LocalActor localActor) +{ + cellChangeActors.push_back(localActor); +} + void ActorList::sendPositionActors() { if (positionActors.size() > 0) @@ -136,6 +142,16 @@ void ActorList::sendAttackActors() } } +void ActorList::sendCellChangeActors() +{ + if (cellChangeActors.size() > 0) + { + baseActors = cellChangeActors; + Main::get().getNetworking()->getActorPacket(ID_ACTOR_CELL_CHANGE)->setActorList(this); + Main::get().getNetworking()->getActorPacket(ID_ACTOR_CELL_CHANGE)->Send(); + } +} + // TODO: Finish this void ActorList::editActorsInCell(MWWorld::CellStore* cellStore) { diff --git a/apps/openmw/mwmp/ActorList.hpp b/apps/openmw/mwmp/ActorList.hpp index c75332368..e99f5733f 100644 --- a/apps/openmw/mwmp/ActorList.hpp +++ b/apps/openmw/mwmp/ActorList.hpp @@ -27,6 +27,7 @@ namespace mwmp void addSpeechActor(LocalActor localActor); void addStatsDynamicActor(LocalActor localActor); void addAttackActor(LocalActor localActor); + void addCellChangeActor(LocalActor localActor); void sendPositionActors(); void sendAnimFlagsActors(); @@ -34,6 +35,7 @@ namespace mwmp void sendSpeechActors(); void sendStatsDynamicActors(); void sendAttackActors(); + void sendCellChangeActors(); void editActorsInCell(MWWorld::CellStore* cellStore); @@ -48,6 +50,7 @@ namespace mwmp std::vector speechActors; std::vector statsDynamicActors; std::vector attackActors; + std::vector cellChangeActors; }; } diff --git a/apps/openmw/mwmp/Cell.cpp b/apps/openmw/mwmp/Cell.cpp index 9c29019fa..a85ca2f27 100644 --- a/apps/openmw/mwmp/Cell.cpp +++ b/apps/openmw/mwmp/Cell.cpp @@ -6,7 +6,6 @@ #include "Cell.hpp" #include "Main.hpp" #include "Networking.hpp" -#include "LocalPlayer.hpp" #include "CellController.hpp" #include "MechanicsHelper.hpp" @@ -38,12 +37,20 @@ void Cell::updateLocal(bool forceUpdate) { LocalActor *actor = it->second; - // TODO:: Make sure this condition actually works - if (actor->getPtr().getCell() != store) + MWWorld::CellStore *newStore = actor->getPtr().getCell(); + + if (newStore != store) { + actor->updateCell(); + LOG_APPEND(Log::LOG_INFO, "- Removing LocalActor %s which is no longer in this cell", it->first.c_str()); Main::get().getCellController()->removeLocalActorRecord(it->first); + + // If the cell this actor has moved to is active, initialize them in it + if (Main::get().getCellController()->isActiveCell(*newStore->getCell())) + Main::get().getCellController()->getCell(*newStore->getCell())->initializeLocalActor(actor->getPtr()); + localActors.erase(it++); } else @@ -61,6 +68,7 @@ void Cell::updateLocal(bool forceUpdate) actorList->sendSpeechActors(); actorList->sendStatsDynamicActors(); actorList->sendAttackActors(); + actorList->sendCellChangeActors(); } void Cell::updateDedicated(float dt) @@ -97,8 +105,6 @@ void Cell::readPositions(ActorList& actorList) void Cell::readAnimFlags(ActorList& actorList) { - initializeDedicatedActors(actorList); - BaseActor baseActor; for (unsigned int i = 0; i < actorList.count; i++) @@ -118,8 +124,6 @@ void Cell::readAnimFlags(ActorList& actorList) void Cell::readAnimPlay(ActorList& actorList) { - initializeDedicatedActors(actorList); - BaseActor baseActor; for (unsigned int i = 0; i < actorList.count; i++) @@ -140,8 +144,6 @@ void Cell::readAnimPlay(ActorList& actorList) void Cell::readStatsDynamic(ActorList& actorList) { - initializeDedicatedActors(actorList); - BaseActor baseActor; for (unsigned int i = 0; i < actorList.count; i++) @@ -159,8 +161,6 @@ void Cell::readStatsDynamic(ActorList& actorList) void Cell::readSpeech(ActorList& actorList) { - initializeDedicatedActors(actorList); - BaseActor baseActor; for (unsigned int i = 0; i < actorList.count; i++) @@ -179,8 +179,6 @@ void Cell::readSpeech(ActorList& actorList) void Cell::readAttack(ActorList& actorList) { - initializeDedicatedActors(actorList); - BaseActor baseActor; for (unsigned int i = 0; i < actorList.count; i++) @@ -197,9 +195,56 @@ void Cell::readAttack(ActorList& actorList) } } +void Cell::readCellChange(ActorList& actorList) +{ + initializeDedicatedActors(actorList); + + BaseActor baseActor; + + for (unsigned int i = 0; i < actorList.count; i++) + { + baseActor = actorList.baseActors.at(i); + std::string mapIndex = Main::get().getCellController()->generateMapIndex(baseActor); + + if (dedicatedActors.count(mapIndex) > 0) + { + DedicatedActor *actor = dedicatedActors[mapIndex]; + actor->cell = baseActor.cell; + actor->position = baseActor.position; + + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Server says DedicatedActor %s, %i, %i moved to %s", + actor->refId.c_str(), actor->refNumIndex, actor->mpNum, actor->cell.getDescription().c_str()); + + MWWorld::CellStore *newStore = Main::get().getCellController()->getCellStore(actor->cell); + actor->setCell(newStore); + + Main::get().getCellController()->removeDedicatedActorRecord(mapIndex); + + // If the cell this actor has moved to is active, initialize them in it + if (Main::get().getCellController()->isActiveCell(actor->cell)) + Main::get().getCellController()->getCell(actor->cell)->initializeDedicatedActor(actor->getPtr()); + + dedicatedActors.erase(mapIndex); + } + } +} + +void Cell::initializeLocalActor(const MWWorld::Ptr& ptr) +{ + LocalActor *actor = new LocalActor(); + actor->cell = *store->getCell(); + actor->setPtr(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()); +} + void Cell::initializeLocalActors() { - ESM::Cell esmCell = *store->getCell(); MWWorld::CellRefList *npcList = store->getNpcs(); for (typename MWWorld::CellRefList::List::iterator listIter(npcList->mList.begin()); @@ -207,17 +252,22 @@ void Cell::initializeLocalActors() { MWWorld::Ptr ptr(&*listIter, store); - LocalActor *actor = new LocalActor(); - actor->cell = esmCell; - actor->setPtr(ptr); + initializeLocalActor(ptr); + } +} + +void Cell::initializeDedicatedActor(const MWWorld::Ptr& ptr) +{ + DedicatedActor *actor = new DedicatedActor(); + actor->cell = *store->getCell(); + actor->setPtr(ptr); - std::string mapIndex = Main::get().getCellController()->generateMapIndex(ptr); - localActors[mapIndex] = actor; + std::string mapIndex = Main::get().getCellController()->generateMapIndex(ptr); + dedicatedActors[mapIndex] = actor; - Main::get().getCellController()->setLocalActorRecord(mapIndex, getDescription()); + Main::get().getCellController()->setDedicatedActorRecord(mapIndex, getDescription()); - LOG_APPEND(Log::LOG_INFO, "- Initialized LocalActor %s", mapIndex.c_str()); - } + LOG_APPEND(Log::LOG_INFO, "- Initialized DedicatedActor %s", mapIndex.c_str()); } void Cell::initializeDedicatedActors(ActorList& actorList) @@ -236,14 +286,7 @@ void Cell::initializeDedicatedActors(ActorList& actorList) if (!ptrFound) return; - DedicatedActor *actor = new DedicatedActor(); - actor->cell = actorList.cell; - actor->setPtr(ptrFound); - dedicatedActors[mapIndex] = actor; - - Main::get().getCellController()->setDedicatedActorRecord(mapIndex, getDescription()); - - LOG_APPEND(Log::LOG_INFO, "- Initialized DedicatedActor %s", mapIndex.c_str()); + initializeDedicatedActor(ptrFound); } } } diff --git a/apps/openmw/mwmp/Cell.hpp b/apps/openmw/mwmp/Cell.hpp index 4e49d683a..d673ed415 100644 --- a/apps/openmw/mwmp/Cell.hpp +++ b/apps/openmw/mwmp/Cell.hpp @@ -1,5 +1,5 @@ -#ifndef OPENMW_CELL_HPP -#define OPENMW_CELL_HPP +#ifndef OPENMW_MPCELL_HPP +#define OPENMW_MPCELL_HPP #include "ActorList.hpp" #include "LocalActor.hpp" @@ -24,8 +24,12 @@ namespace mwmp void readStatsDynamic(ActorList& actorList); void readSpeech(ActorList& actorList); void readAttack(ActorList& actorList); + void readCellChange(ActorList& actorList); + void initializeLocalActor(const MWWorld::Ptr& ptr); void initializeLocalActors(); + + void initializeDedicatedActor(const MWWorld::Ptr& ptr); void initializeDedicatedActors(ActorList& actorList); void uninitializeLocalActors(); @@ -44,4 +48,4 @@ namespace mwmp }; } -#endif //OPENMW_CELL_HPP +#endif //OPENMW_MPCELL_HPP diff --git a/apps/openmw/mwmp/CellController.cpp b/apps/openmw/mwmp/CellController.cpp index 6ded044b2..73e40baf2 100644 --- a/apps/openmw/mwmp/CellController.cpp +++ b/apps/openmw/mwmp/CellController.cpp @@ -167,6 +167,19 @@ void CellController::readAttack(ActorList& actorList) } } +void CellController::readCellChange(ActorList& actorList) +{ + std::string mapIndex = actorList.cell.getDescription(); + + initializeCell(actorList.cell); + + // If this now exists, send it the data + if (cellsActive.count(mapIndex) > 0) + { + cellsActive[mapIndex]->readCellChange(actorList); + } +} + void CellController::setLocalActorRecord(std::string actorIndex, std::string cellIndex) { localActorsToCells[actorIndex] = cellIndex; diff --git a/apps/openmw/mwmp/CellController.hpp b/apps/openmw/mwmp/CellController.hpp index d1a799a1c..53f97cc5e 100644 --- a/apps/openmw/mwmp/CellController.hpp +++ b/apps/openmw/mwmp/CellController.hpp @@ -28,6 +28,7 @@ namespace mwmp void readStatsDynamic(mwmp::ActorList& actorList); void readSpeech(mwmp::ActorList& actorList); void readAttack(mwmp::ActorList& actorList); + void readCellChange(mwmp::ActorList& actorList); void setLocalActorRecord(std::string actorIndex, std::string cellIndex); void removeLocalActorRecord(std::string actorIndex); diff --git a/apps/openmw/mwmp/DedicatedActor.cpp b/apps/openmw/mwmp/DedicatedActor.cpp index f61735e97..b82507a19 100644 --- a/apps/openmw/mwmp/DedicatedActor.cpp +++ b/apps/openmw/mwmp/DedicatedActor.cpp @@ -10,10 +10,13 @@ #include "../mwrender/animation.hpp" +#include "../mwworld/cellstore.hpp" #include "../mwworld/class.hpp" #include "../mwworld/worldimp.hpp" #include "DedicatedActor.hpp" +#include "Main.hpp" +#include "CellController.hpp" using namespace mwmp; using namespace std; @@ -44,6 +47,13 @@ void DedicatedActor::update(float dt) setStatsDynamic(); } +void DedicatedActor::setCell(MWWorld::CellStore *cellStore) +{ + MWBase::World *world = MWBase::Environment::get().getWorld(); + + ptr = world->moveObject(ptr, cellStore, position.pos[0], position.pos[1], position.pos[2]); +} + void DedicatedActor::move(float dt) { MWBase::World *world = MWBase::Environment::get().getWorld(); diff --git a/apps/openmw/mwmp/DedicatedActor.hpp b/apps/openmw/mwmp/DedicatedActor.hpp index 306a2960a..7dc5c67c0 100644 --- a/apps/openmw/mwmp/DedicatedActor.hpp +++ b/apps/openmw/mwmp/DedicatedActor.hpp @@ -15,6 +15,7 @@ namespace mwmp virtual ~DedicatedActor(); void update(float dt); + void setCell(MWWorld::CellStore *cellStore); void move(float dt); void setAnimFlags(); void playAnimation(); diff --git a/apps/openmw/mwmp/LocalActor.cpp b/apps/openmw/mwmp/LocalActor.cpp index db5e2ee0a..e1a56fb5f 100644 --- a/apps/openmw/mwmp/LocalActor.cpp +++ b/apps/openmw/mwmp/LocalActor.cpp @@ -54,6 +54,18 @@ void LocalActor::update(bool forceUpdate) updateAttack(); } +void LocalActor::updateCell() +{ + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_ACTOR_CELL_CHANGE about %s, %i, %i to server", + refId.c_str(), refNumIndex, mpNum); + + LOG_APPEND(Log::LOG_INFO, "- Moved from %s to %s", cell.getDescription().c_str(), ptr.getCell()->getCell()->getDescription().c_str()); + + cell = *ptr.getCell()->getCell(); + + mwmp::Main::get().getNetworking()->getActorList()->addCellChangeActor(*this); +} + void LocalActor::updatePosition(bool forceUpdate) { bool posIsChanging = (direction.pos[0] != 0 || direction.pos[1] != 0 || direction.pos[2] != 0 || diff --git a/apps/openmw/mwmp/LocalActor.hpp b/apps/openmw/mwmp/LocalActor.hpp index f4ec95bca..78aab33e8 100644 --- a/apps/openmw/mwmp/LocalActor.hpp +++ b/apps/openmw/mwmp/LocalActor.hpp @@ -16,6 +16,7 @@ namespace mwmp void update(bool forceUpdate); + void updateCell(); void updatePosition(bool forceUpdate); void updateAnimFlags(bool forceUpdate); void updateAnimPlay(); diff --git a/apps/openmw/mwmp/processors/actor/ProcessorActorCellChange.hpp b/apps/openmw/mwmp/processors/actor/ProcessorActorCellChange.hpp index 75bec092c..11fa4fb90 100644 --- a/apps/openmw/mwmp/processors/actor/ProcessorActorCellChange.hpp +++ b/apps/openmw/mwmp/processors/actor/ProcessorActorCellChange.hpp @@ -22,7 +22,7 @@ namespace mwmp virtual void Do(ActorPacket &packet, ActorList &actorList) { - + //Main::get().getCellController()->readCellChange(actorList); } }; }