From 70f9cb535eed5ad99fdeb895b6d6b83a3e2db6d8 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 13 Apr 2018 08:37:06 +0300 Subject: [PATCH] [General] Use RecordHelper methods to create and update DedicatedPlayers --- apps/openmw/mwmp/DedicatedPlayer.cpp | 138 +++++++++--------- apps/openmw/mwmp/DedicatedPlayer.hpp | 7 +- apps/openmw/mwmp/RecordHelper.cpp | 7 + apps/openmw/mwmp/RecordHelper.hpp | 1 + .../player/ProcessorPlayerBaseInfo.hpp | 2 +- .../player/ProcessorPlayerShapeshift.hpp | 4 + components/openmw-mp/Base/BasePlayer.hpp | 2 + 7 files changed, 90 insertions(+), 71 deletions(-) diff --git a/apps/openmw/mwmp/DedicatedPlayer.cpp b/apps/openmw/mwmp/DedicatedPlayer.cpp index 78a068a7d..80f6216ac 100644 --- a/apps/openmw/mwmp/DedicatedPlayer.cpp +++ b/apps/openmw/mwmp/DedicatedPlayer.cpp @@ -34,6 +34,7 @@ #include "GUIController.hpp" #include "CellController.hpp" #include "MechanicsHelper.hpp" +#include "RecordHelper.hpp" using namespace mwmp; @@ -55,6 +56,10 @@ DedicatedPlayer::DedicatedPlayer(RakNet::RakNetGUID guid) : BasePlayer(guid) cell.blank(); position.pos[0] = position.pos[1] = Main::get().getCellController()->getCellSize() / 2; position.pos[2] = 0; + + MWBase::World *world = MWBase::Environment::get().getWorld(); + npc = *world->getPlayerPtr().get()->mBase; + npc.mId = "Dedicated Player"; } DedicatedPlayer::~DedicatedPlayer() { @@ -137,45 +142,85 @@ void DedicatedPlayer::move(float dt) void DedicatedPlayer::setBaseInfo() { - MWBase::World *world = MWBase::Environment::get().getWorld(); + static std::string previousRace; - if (reference) + // Use the previous race if the new one doesn't exist + if (!RecordHelper::doesRaceExist(npc.mRace)) + npc.mRace = previousRace; + + if (!reference) { - deleteReference(); + npc.mId = RecordHelper::createNpcRecord(npc); + createReference(npc.mId); + } + else + { + RecordHelper::updateNpcRecord(npc); + reloadPtr(); } - - std::string recId = getNpcRecordId(); - createReference(recId); setEquipment(); + + previousRace = npc.mRace; } void DedicatedPlayer::setShapeshift() { MWBase::World *world = MWBase::Environment::get().getWorld(); - if (reference) - { - deleteReference(); - } + bool isNpc = false; - std::string recId; + if (reference) + isNpc = ptr.getTypeName() == typeid(ESM::NPC).name(); - if (!creatureRefId.empty()) + if (!creatureRefId.empty() && RecordHelper::doesCreatureExist(creatureRefId)) { + if (isNpc) + { + deleteReference(); + } + const ESM::Creature *tmpCreature = world->getStore().get().search(creatureRefId); - if (tmpCreature != 0) + creature = *tmpCreature; + creature.mScript = ""; + if (!displayCreatureName) + creature.mName = npc.mName; + LOG_APPEND(Log::LOG_INFO, "- %s is disguised as %s", npc.mName.c_str(), creatureRefId.c_str()); + + // Is this our first time creating a creature record id for this player? If so, keep it around + // and reuse it + if (creatureRecordId.empty()) { - recId = getCreatureRecordId(); + creature.mId = "Dedicated Player"; + creatureRecordId = RecordHelper::createCreatureRecord(creature); + LOG_APPEND(Log::LOG_INFO, "- Creating new creature record %s", creatureRecordId.c_str()); } - } - if (recId.empty()) - { - recId = getNpcRecordId(); + creature.mId = creatureRecordId; + + if (!reference) + { + createReference(creature.mId); + } + else + { + RecordHelper::updateCreatureRecord(creature); + reloadPtr(); + } } + // This player was already a creature, but the new creature refId was empty or + // invalid, so we'll turn this player into their NPC self again as a result + else if (!isNpc) + { + if (reference) + { + deleteReference(); + } - createReference(recId); + RecordHelper::updateNpcRecord(npc); + createReference(npc.mId); + reloadPtr(); + } if (ptr.getTypeName() == typeid(ESM::NPC).name()) { @@ -353,54 +398,6 @@ void DedicatedPlayer::playSpeech() winMgr->messageBox(MWBase::Environment::get().getDialogueManager()->getVoiceCaption(sound), MWGui::ShowInDialogueMode_Never); } -std::string DedicatedPlayer::getNpcRecordId() -{ - MWBase::World *world = MWBase::Environment::get().getWorld(); - - MWWorld::Ptr player = world->getPlayerPtr(); - - ESM::NPC newNpc = *player.get()->mBase; - - // To avoid freezes caused by invalid races, only set race if we find it - // on our client - if (world->getStore().get().search(npc.mRace) != 0) - newNpc.mRace = npc.mRace; - - newNpc.mHead = npc.mHead; - newNpc.mHair = npc.mHair; - newNpc.mClass = npc.mClass; - newNpc.mName = npc.mName; - newNpc.mFlags = npc.mFlags; - - LOG_APPEND(Log::LOG_INFO, "- Creating new NPC record"); - newNpc.mId = "Dedicated Player"; - std::string recId = world->createRecord(newNpc)->mId; - - return recId; -} - -std::string DedicatedPlayer::getCreatureRecordId() -{ - MWBase::World *world = MWBase::Environment::get().getWorld(); - - ESM::Creature creature; - - const ESM::Creature *tmpCreature = world->getStore().get().search(creatureRefId); - - creature = *tmpCreature; - creature.mScript = ""; - if (!displayCreatureName) - creature.mName = npc.mName; - LOG_APPEND(Log::LOG_INFO, "Player %s looks like %s", npc.mName.c_str(), creatureRefId.c_str()); - - LOG_APPEND(Log::LOG_INFO, "- Creating new NPC record"); - creature.mId = "Dedicated Player"; - - std::string recId = world->createRecord(creature)->mId; - - return recId; -} - void DedicatedPlayer::createReference(const std::string& recId) { MWBase::World *world = MWBase::Environment::get().getWorld(); @@ -440,3 +437,10 @@ void DedicatedPlayer::setPtr(const MWWorld::Ptr& newPtr) { ptr = newPtr; } + +void DedicatedPlayer::reloadPtr() +{ + MWBase::World *world = MWBase::Environment::get().getWorld(); + world->disable(ptr); + world->enable(ptr); +} diff --git a/apps/openmw/mwmp/DedicatedPlayer.hpp b/apps/openmw/mwmp/DedicatedPlayer.hpp index bb3718a0a..aefa08b76 100644 --- a/apps/openmw/mwmp/DedicatedPlayer.hpp +++ b/apps/openmw/mwmp/DedicatedPlayer.hpp @@ -50,9 +50,6 @@ namespace mwmp void playAnimation(); void playSpeech(); - std::string getNpcRecordId(); - std::string getCreatureRecordId(); - void createReference(const std::string& recId); void deleteReference(); @@ -60,6 +57,7 @@ namespace mwmp MWWorld::ManualRef* getRef(); void setPtr(const MWWorld::Ptr& newPtr); + void reloadPtr(); private: @@ -72,6 +70,9 @@ namespace mwmp ESM::CustomMarker marker; bool markerEnabled; + + std::string npcRecordId; + std::string creatureRecordId; }; } #endif //OPENMW_DEDICATEDPLAYER_HPP diff --git a/apps/openmw/mwmp/RecordHelper.cpp b/apps/openmw/mwmp/RecordHelper.cpp index 032afcb0a..186558d18 100644 --- a/apps/openmw/mwmp/RecordHelper.cpp +++ b/apps/openmw/mwmp/RecordHelper.cpp @@ -13,6 +13,13 @@ bool RecordHelper::doesCreatureExist(const std::string& refId) return world->getStore().get().search(refId); } +bool RecordHelper::doesRaceExist(const std::string& raceId) +{ + MWBase::World *world = MWBase::Environment::get().getWorld(); + + return world->getStore().get().search(raceId); +} + std::string RecordHelper::createCreatureRecord(const ESM::Creature& creature) { MWBase::World *world = MWBase::Environment::get().getWorld(); diff --git a/apps/openmw/mwmp/RecordHelper.hpp b/apps/openmw/mwmp/RecordHelper.hpp index b66580e37..314438332 100644 --- a/apps/openmw/mwmp/RecordHelper.hpp +++ b/apps/openmw/mwmp/RecordHelper.hpp @@ -7,6 +7,7 @@ namespace RecordHelper { bool doesCreatureExist(const std::string& refId); + bool doesRaceExist(const std::string& raceId); std::string createCreatureRecord(const ESM::Creature& creature); std::string createNpcRecord(const ESM::NPC& npc); diff --git a/apps/openmw/mwmp/processors/player/ProcessorPlayerBaseInfo.hpp b/apps/openmw/mwmp/processors/player/ProcessorPlayerBaseInfo.hpp index eab9e8182..35e3669f4 100644 --- a/apps/openmw/mwmp/processors/player/ProcessorPlayerBaseInfo.hpp +++ b/apps/openmw/mwmp/processors/player/ProcessorPlayerBaseInfo.hpp @@ -23,7 +23,7 @@ namespace mwmp if (isLocal()) { - LOG_APPEND(Log::LOG_INFO, "- Packet was about my id"); + LOG_APPEND(Log::LOG_INFO, "- Packet was about LocalPlayer"); if (isRequest()) { diff --git a/apps/openmw/mwmp/processors/player/ProcessorPlayerShapeshift.hpp b/apps/openmw/mwmp/processors/player/ProcessorPlayerShapeshift.hpp index 70479221f..448cab9cb 100644 --- a/apps/openmw/mwmp/processors/player/ProcessorPlayerShapeshift.hpp +++ b/apps/openmw/mwmp/processors/player/ProcessorPlayerShapeshift.hpp @@ -15,8 +15,12 @@ namespace mwmp virtual void Do(PlayerPacket &packet, BasePlayer *player) { + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_PLAYER_SHAPESHIFT from server"); + if (isLocal()) { + LOG_APPEND(Log::LOG_INFO, "- Packet was about LocalPlayer"); + static_cast(player)->setShapeshift(); } else if (player != 0) diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 9186a383c..119db0eca 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -6,6 +6,7 @@ #define OPENMW_BASEPLAYER_HPP #include +#include #include #include #include @@ -274,6 +275,7 @@ namespace mwmp ESM::Cell cell; ESM::NPC npc; ESM::NpcStats npcStats; + ESM::Creature creature; ESM::CreatureStats creatureStats; ESM::Class charClass; Item equipedItems[19];