[General] Use RecordHelper methods to create and update DedicatedPlayers

0.6.3
David Cernat 7 years ago
parent 49e94725aa
commit 70f9cb535e

@ -34,6 +34,7 @@
#include "GUIController.hpp" #include "GUIController.hpp"
#include "CellController.hpp" #include "CellController.hpp"
#include "MechanicsHelper.hpp" #include "MechanicsHelper.hpp"
#include "RecordHelper.hpp"
using namespace mwmp; using namespace mwmp;
@ -55,6 +56,10 @@ DedicatedPlayer::DedicatedPlayer(RakNet::RakNetGUID guid) : BasePlayer(guid)
cell.blank(); cell.blank();
position.pos[0] = position.pos[1] = Main::get().getCellController()->getCellSize() / 2; position.pos[0] = position.pos[1] = Main::get().getCellController()->getCellSize() / 2;
position.pos[2] = 0; position.pos[2] = 0;
MWBase::World *world = MWBase::Environment::get().getWorld();
npc = *world->getPlayerPtr().get<ESM::NPC>()->mBase;
npc.mId = "Dedicated Player";
} }
DedicatedPlayer::~DedicatedPlayer() DedicatedPlayer::~DedicatedPlayer()
{ {
@ -137,45 +142,85 @@ void DedicatedPlayer::move(float dt)
void DedicatedPlayer::setBaseInfo() 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(); setEquipment();
previousRace = npc.mRace;
} }
void DedicatedPlayer::setShapeshift() void DedicatedPlayer::setShapeshift()
{ {
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
if (reference) bool isNpc = false;
{
deleteReference();
}
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<ESM::Creature>().search(creatureRefId); const ESM::Creature *tmpCreature = world->getStore().get<ESM::Creature>().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()) creature.mId = creatureRecordId;
{
recId = getNpcRecordId(); 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()) 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); 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<ESM::NPC>()->mBase;
// To avoid freezes caused by invalid races, only set race if we find it
// on our client
if (world->getStore().get<ESM::Race>().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<ESM::Creature>().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) void DedicatedPlayer::createReference(const std::string& recId)
{ {
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
@ -440,3 +437,10 @@ void DedicatedPlayer::setPtr(const MWWorld::Ptr& newPtr)
{ {
ptr = newPtr; ptr = newPtr;
} }
void DedicatedPlayer::reloadPtr()
{
MWBase::World *world = MWBase::Environment::get().getWorld();
world->disable(ptr);
world->enable(ptr);
}

@ -50,9 +50,6 @@ namespace mwmp
void playAnimation(); void playAnimation();
void playSpeech(); void playSpeech();
std::string getNpcRecordId();
std::string getCreatureRecordId();
void createReference(const std::string& recId); void createReference(const std::string& recId);
void deleteReference(); void deleteReference();
@ -60,6 +57,7 @@ namespace mwmp
MWWorld::ManualRef* getRef(); MWWorld::ManualRef* getRef();
void setPtr(const MWWorld::Ptr& newPtr); void setPtr(const MWWorld::Ptr& newPtr);
void reloadPtr();
private: private:
@ -72,6 +70,9 @@ namespace mwmp
ESM::CustomMarker marker; ESM::CustomMarker marker;
bool markerEnabled; bool markerEnabled;
std::string npcRecordId;
std::string creatureRecordId;
}; };
} }
#endif //OPENMW_DEDICATEDPLAYER_HPP #endif //OPENMW_DEDICATEDPLAYER_HPP

@ -13,6 +13,13 @@ bool RecordHelper::doesCreatureExist(const std::string& refId)
return world->getStore().get<ESM::Creature>().search(refId); return world->getStore().get<ESM::Creature>().search(refId);
} }
bool RecordHelper::doesRaceExist(const std::string& raceId)
{
MWBase::World *world = MWBase::Environment::get().getWorld();
return world->getStore().get<ESM::Race>().search(raceId);
}
std::string RecordHelper::createCreatureRecord(const ESM::Creature& creature) std::string RecordHelper::createCreatureRecord(const ESM::Creature& creature)
{ {
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();

@ -7,6 +7,7 @@
namespace RecordHelper namespace RecordHelper
{ {
bool doesCreatureExist(const std::string& refId); bool doesCreatureExist(const std::string& refId);
bool doesRaceExist(const std::string& raceId);
std::string createCreatureRecord(const ESM::Creature& creature); std::string createCreatureRecord(const ESM::Creature& creature);
std::string createNpcRecord(const ESM::NPC& npc); std::string createNpcRecord(const ESM::NPC& npc);

@ -23,7 +23,7 @@ namespace mwmp
if (isLocal()) if (isLocal())
{ {
LOG_APPEND(Log::LOG_INFO, "- Packet was about my id"); LOG_APPEND(Log::LOG_INFO, "- Packet was about LocalPlayer");
if (isRequest()) if (isRequest())
{ {

@ -15,8 +15,12 @@ namespace mwmp
virtual void Do(PlayerPacket &packet, BasePlayer *player) virtual void Do(PlayerPacket &packet, BasePlayer *player)
{ {
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_PLAYER_SHAPESHIFT from server");
if (isLocal()) if (isLocal())
{ {
LOG_APPEND(Log::LOG_INFO, "- Packet was about LocalPlayer");
static_cast<LocalPlayer *>(player)->setShapeshift(); static_cast<LocalPlayer *>(player)->setShapeshift();
} }
else if (player != 0) else if (player != 0)

@ -6,6 +6,7 @@
#define OPENMW_BASEPLAYER_HPP #define OPENMW_BASEPLAYER_HPP
#include <components/esm/loadcell.hpp> #include <components/esm/loadcell.hpp>
#include <components/esm/loadcrea.hpp>
#include <components/esm/loadnpc.hpp> #include <components/esm/loadnpc.hpp>
#include <components/esm/npcstats.hpp> #include <components/esm/npcstats.hpp>
#include <components/esm/creaturestats.hpp> #include <components/esm/creaturestats.hpp>
@ -274,6 +275,7 @@ namespace mwmp
ESM::Cell cell; ESM::Cell cell;
ESM::NPC npc; ESM::NPC npc;
ESM::NpcStats npcStats; ESM::NpcStats npcStats;
ESM::Creature creature;
ESM::CreatureStats creatureStats; ESM::CreatureStats creatureStats;
ESM::Class charClass; ESM::Class charClass;
Item equipedItems[19]; Item equipedItems[19];

Loading…
Cancel
Save