[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 "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<ESM::NPC>()->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();
bool isNpc = false;
if (reference)
isNpc = ptr.getTypeName() == typeid(ESM::NPC).name();
if (!creatureRefId.empty() && RecordHelper::doesCreatureExist(creatureRefId))
{
if (isNpc)
{
deleteReference();
}
std::string recId;
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, "- %s is disguised as %s", npc.mName.c_str(), creatureRefId.c_str());
if (!creatureRefId.empty())
// Is this our first time creating a creature record id for this player? If so, keep it around
// and reuse it
if (creatureRecordId.empty())
{
const ESM::Creature *tmpCreature = world->getStore().get<ESM::Creature>().search(creatureRefId);
if (tmpCreature != 0)
creature.mId = "Dedicated Player";
creatureRecordId = RecordHelper::createCreatureRecord(creature);
LOG_APPEND(Log::LOG_INFO, "- Creating new creature record %s", creatureRecordId.c_str());
}
creature.mId = creatureRecordId;
if (!reference)
{
recId = getCreatureRecordId();
createReference(creature.mId);
}
else
{
RecordHelper::updateCreatureRecord(creature);
reloadPtr();
}
if (recId.empty())
}
// 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)
{
recId = getNpcRecordId();
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<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)
{
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);
}

@ -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

@ -13,6 +13,13 @@ bool RecordHelper::doesCreatureExist(const std::string& 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)
{
MWBase::World *world = MWBase::Environment::get().getWorld();

@ -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);

@ -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())
{

@ -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<LocalPlayer *>(player)->setShapeshift();
}
else if (player != 0)

@ -6,6 +6,7 @@
#define OPENMW_BASEPLAYER_HPP
#include <components/esm/loadcell.hpp>
#include <components/esm/loadcrea.hpp>
#include <components/esm/loadnpc.hpp>
#include <components/esm/npcstats.hpp>
#include <components/esm/creaturestats.hpp>
@ -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];

Loading…
Cancel
Save