1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-02-02 08:45:32 +00:00
openmw-tes3mp/components/esm/cellref.cpp
cc9cii abe6904a94 Minor performance gains on loading cell references.
- Use integers where possible
- Unfortunately many functions are simply duplicated for now, but over time the deprecated ones will be removed
2015-12-19 17:54:45 +11:00

284 lines
7.6 KiB
C++

#include "cellref.hpp"
#include "esmreader.hpp"
#include "esmwriter.hpp"
void ESM::RefNum::load (ESMReader& esm, bool wide)
{
if (wide)
esm.getHNT (*this, SREC_FRMR, 8);
else
esm.getHNT (mIndex, SREC_FRMR);
}
void ESM::RefNum::save (ESMWriter &esm, bool wide, const std::string& tag) const
{
if (wide)
esm.writeHNT (tag, *this, 8);
else
{
int refNum = (mIndex & 0xffffff) | ((hasContentFile() ? mContentFile : 0xff)<<24);
esm.writeHNT (tag, refNum, 4);
}
}
void ESM::CellRef::load (ESMReader& esm, bool &isDeleted, bool wideRefNum)
{
loadId(esm, wideRefNum);
loadData(esm, isDeleted);
}
void ESM::CellRef::loadId (ESMReader& esm, bool wideRefNum)
{
// According to Hrnchamd, this does not belong to the actual ref. Instead, it is a marker indicating that
// the following refs are part of a "temp refs" section. A temp ref is not being tracked by the moved references system.
// Its only purpose is a performance optimization for "immovable" things. We don't need this, and it's problematic anyway,
// because any item can theoretically be moved by a script.
if (esm.isNextSub (SREC_NAM0))
esm.skipHSub();
blank();
mRefNum.load (esm, wideRefNum);
esm.getHNString (SREC_NAME, mRefID);
}
void ESM::CellRef::loadData(ESMReader &esm, bool &isDeleted)
{
isDeleted = false;
bool isLoaded = false;
while (!isLoaded && esm.hasMoreSubs())
{
esm.getSubName();
switch (esm.retSubName().val)
{
case ESM::FourCC<'U','N','A','M'>::value:
esm.getHT(mReferenceBlocked);
break;
case ESM::FourCC<'X','S','C','L'>::value:
esm.getHT(mScale);
break;
case ESM::FourCC<'A','N','A','M'>::value:
mOwner = esm.getHString();
break;
case ESM::FourCC<'B','N','A','M'>::value:
mGlobalVariable = esm.getHString();
break;
case ESM::FourCC<'X','S','O','L'>::value:
mSoul = esm.getHString();
break;
case ESM::FourCC<'C','N','A','M'>::value:
mFaction = esm.getHString();
break;
case ESM::FourCC<'I','N','D','X'>::value:
esm.getHT(mFactionRank);
break;
case ESM::FourCC<'X','C','H','G'>::value:
esm.getHT(mEnchantmentCharge);
break;
case ESM::FourCC<'I','N','T','V'>::value:
esm.getHT(mChargeInt);
break;
case ESM::FourCC<'N','A','M','9'>::value:
esm.getHT(mGoldValue);
break;
case ESM::FourCC<'D','O','D','T'>::value:
esm.getHT(mDoorDest);
mTeleport = true;
break;
case ESM::FourCC<'D','N','A','M'>::value:
mDestCell = esm.getHString();
break;
case ESM::FourCC<'F','L','T','V'>::value:
esm.getHT(mLockLevel);
break;
case ESM::FourCC<'K','N','A','M'>::value:
mKey = esm.getHString();
break;
case ESM::FourCC<'T','N','A','M'>::value:
mTrap = esm.getHString();
break;
case ESM::FourCC<'D','A','T','A'>::value:
esm.getHT(mPos, 24);
break;
case ESM::FourCC<'N','A','M','0'>::value:
esm.skipHSub();
break;
case ESM::SREC_DELE:
esm.skipHSub();
isDeleted = true;
break;
default:
esm.cacheSubName();
isLoaded = true;
break;
}
}
}
void ESM::CellRef::save (ESMWriter &esm, bool wideRefNum, bool inInventory, bool isDeleted) const
{
mRefNum.save (esm, wideRefNum);
esm.writeHNCString("NAME", mRefID);
if (isDeleted) {
esm.writeHNCString("DELE", "");
return;
}
if (mScale != 1.0) {
esm.writeHNT("XSCL", mScale);
}
esm.writeHNOCString("ANAM", mOwner);
esm.writeHNOCString("BNAM", mGlobalVariable);
esm.writeHNOCString("XSOL", mSoul);
esm.writeHNOCString("CNAM", mFaction);
if (mFactionRank != -2) {
esm.writeHNT("INDX", mFactionRank);
}
if (mEnchantmentCharge != -1)
esm.writeHNT("XCHG", mEnchantmentCharge);
if (mChargeInt != -1)
esm.writeHNT("INTV", mChargeInt);
if (mGoldValue != 1) {
esm.writeHNT("NAM9", mGoldValue);
}
if (!inInventory && mTeleport)
{
esm.writeHNT("DODT", mDoorDest);
esm.writeHNOCString("DNAM", mDestCell);
}
if (!inInventory && mLockLevel != 0) {
esm.writeHNT("FLTV", mLockLevel);
}
if (!inInventory)
esm.writeHNOCString ("KNAM", mKey);
if (!inInventory)
esm.writeHNOCString ("TNAM", mTrap);
if (mReferenceBlocked != -1)
esm.writeHNT("UNAM", mReferenceBlocked);
if (!inInventory)
esm.writeHNT("DATA", mPos, 24);
}
void ESM::CellRef::blank()
{
mRefNum.unset();
mRefID.clear();
mScale = 1;
mOwner.clear();
mGlobalVariable.clear();
mSoul.clear();
mFaction.clear();
mFactionRank = -2;
mChargeInt = -1;
mEnchantmentCharge = -1;
mGoldValue = 0;
mDestCell.clear();
mLockLevel = 0;
mKey.clear();
mTrap.clear();
mReferenceBlocked = -1;
mTeleport = false;
for (int i=0; i<3; ++i)
{
mDoorDest.pos[i] = 0;
mDoorDest.rot[i] = 0;
mPos.pos[i] = 0;
mPos.rot[i] = 0;
}
}
ESM::CellRef::CellRef ()
{
blank();
}
ESM::CellRef::CellRef (const CellRef& other)
: mRefNum(other.mRefNum)
, mRefID(other.mRefID)
, mScale(other.mScale)
, mOwner(other.mOwner)
, mGlobalVariable(other.mGlobalVariable)
, mSoul(other.mSoul)
, mFaction(other.mFaction)
, mFactionRank(other.mFactionRank)
, mChargeInt(other.mChargeInt)
, mEnchantmentCharge(other.mEnchantmentCharge)
, mGoldValue(other.mGoldValue)
, mTeleport(other.mTeleport)
, mDoorDest(other.mDoorDest)
, mDestCell(other.mDestCell)
, mLockLevel(other.mLockLevel)
, mKey(other.mKey)
, mTrap(other.mTrap)
, mReferenceBlocked(other.mReferenceBlocked)
, mPos(other.mPos)
{
}
ESM::CellRef::CellRef (CellRef&& other)
{
*this = std::move(other);
}
ESM::CellRef& ESM::CellRef::operator= (CellRef&& other)
{
if (this != &other)
{
mRefNum = other.mRefNum; // RefNum
mRefID = std::move(other.mRefID);
mScale = other.mScale;
mOwner = std::move(other.mOwner);
mGlobalVariable = std::move(other.mGlobalVariable);
mSoul = std::move(other.mSoul);
mFaction = std::move(other.mFaction);
mFactionRank = other.mFactionRank;
mChargeInt = other.mChargeInt;
mEnchantmentCharge = other.mEnchantmentCharge;
mGoldValue = other.mGoldValue;
mTeleport = other.mTeleport;
mDoorDest = other.mDoorDest; // Position
mDestCell = std::move(other.mDestCell);
mLockLevel = other.mLockLevel;
mKey = std::move(other.mKey);
mTrap = std::move(other.mTrap);
mReferenceBlocked = other.mReferenceBlocked;
mPos = other.mPos; // Position
}
return *this;
}
bool ESM::operator== (const RefNum& left, const RefNum& right)
{
return left.mIndex==right.mIndex && left.mContentFile==right.mContentFile;
}
bool ESM::operator< (const RefNum& left, const RefNum& right)
{
if (left.mIndex<right.mIndex)
return true;
if (left.mIndex>right.mIndex)
return false;
return left.mContentFile<right.mContentFile;
}