2014-05-25 14:13:07 +02:00
|
|
|
#include "cellref.hpp"
|
|
|
|
|
2022-01-22 22:44:02 +01:00
|
|
|
#include <cassert>
|
2021-01-22 15:48:37 +01:00
|
|
|
|
|
|
|
#include <components/debug/debuglog.hpp>
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/objectstate.hpp>
|
2014-05-25 14:13:07 +02:00
|
|
|
|
|
|
|
namespace MWWorld
|
|
|
|
{
|
|
|
|
|
2021-01-22 15:48:37 +01:00
|
|
|
const ESM::RefNum& CellRef::getOrAssignRefNum(ESM::RefNum& lastAssignedRefNum)
|
|
|
|
{
|
|
|
|
if (!mCellRef.mRefNum.isSet())
|
|
|
|
{
|
|
|
|
// Generated RefNums have negative mContentFile
|
|
|
|
assert(lastAssignedRefNum.mContentFile < 0);
|
|
|
|
lastAssignedRefNum.mIndex++;
|
|
|
|
if (lastAssignedRefNum.mIndex == 0) // mIndex overflow, so mContentFile should be changed
|
|
|
|
{
|
2021-04-23 02:49:12 +02:00
|
|
|
if (lastAssignedRefNum.mContentFile > std::numeric_limits<int32_t>::min())
|
|
|
|
lastAssignedRefNum.mContentFile--;
|
|
|
|
else
|
2021-01-22 15:48:37 +01:00
|
|
|
Log(Debug::Error) << "RefNum counter overflow in CellRef::getOrAssignRefNum";
|
|
|
|
}
|
|
|
|
mCellRef.mRefNum = lastAssignedRefNum;
|
|
|
|
mChanged = true;
|
|
|
|
}
|
|
|
|
return mCellRef.mRefNum;
|
|
|
|
}
|
|
|
|
|
2014-07-29 15:55:58 +02:00
|
|
|
void CellRef::unsetRefNum()
|
|
|
|
{
|
2015-02-03 23:05:06 +01:00
|
|
|
mCellRef.mRefNum.unset();
|
2014-07-29 15:55:58 +02:00
|
|
|
}
|
|
|
|
|
2014-05-25 14:13:07 +02:00
|
|
|
void CellRef::setScale(float scale)
|
|
|
|
{
|
|
|
|
if (scale != mCellRef.mScale)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mScale = scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::setPosition(const ESM::Position& position)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mPos = position;
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:09:07 +03:00
|
|
|
float CellRef::getNormalizedEnchantmentCharge(int maxCharge) const
|
|
|
|
{
|
|
|
|
if (maxCharge == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (mCellRef.mEnchantmentCharge == -1)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return mCellRef.mEnchantmentCharge / static_cast<float>(maxCharge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 14:13:07 +02:00
|
|
|
void CellRef::setEnchantmentCharge(float charge)
|
|
|
|
{
|
|
|
|
if (charge != mCellRef.mEnchantmentCharge)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mEnchantmentCharge = charge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::setCharge(int charge)
|
|
|
|
{
|
2015-01-23 15:33:39 +01:00
|
|
|
if (charge != mCellRef.mChargeInt)
|
2014-05-25 14:13:07 +02:00
|
|
|
{
|
|
|
|
mChanged = true;
|
2015-01-23 15:33:39 +01:00
|
|
|
mCellRef.mChargeInt = charge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:48:56 -07:00
|
|
|
void CellRef::applyChargeRemainderToBeSubtracted(float chargeRemainder)
|
|
|
|
{
|
|
|
|
mCellRef.mChargeIntRemainder += std::abs(chargeRemainder);
|
|
|
|
if (mCellRef.mChargeIntRemainder > 1.0f)
|
|
|
|
{
|
|
|
|
float newChargeRemainder = (mCellRef.mChargeIntRemainder - std::floor(mCellRef.mChargeIntRemainder));
|
|
|
|
if (mCellRef.mChargeInt <= static_cast<int>(mCellRef.mChargeIntRemainder))
|
|
|
|
{
|
|
|
|
mCellRef.mChargeInt = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mCellRef.mChargeInt -= static_cast<int>(mCellRef.mChargeIntRemainder);
|
|
|
|
}
|
|
|
|
mCellRef.mChargeIntRemainder = newChargeRemainder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 15:33:39 +01:00
|
|
|
void CellRef::setChargeFloat(float charge)
|
|
|
|
{
|
|
|
|
if (charge != mCellRef.mChargeFloat)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mChargeFloat = charge;
|
2014-05-25 14:13:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:18:43 +01:00
|
|
|
void CellRef::resetGlobalVariable()
|
|
|
|
{
|
|
|
|
if (!mCellRef.mGlobalVariable.empty())
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mGlobalVariable.erase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 22:00:45 +01:00
|
|
|
void CellRef::setFactionRank(int factionRank)
|
|
|
|
{
|
|
|
|
if (factionRank != mCellRef.mFactionRank)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mFactionRank = factionRank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 14:13:07 +02:00
|
|
|
void CellRef::setOwner(const std::string& owner)
|
|
|
|
{
|
|
|
|
if (owner != mCellRef.mOwner)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mOwner = owner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 13:07:59 +02:00
|
|
|
void CellRef::setSoul(std::string_view soul)
|
2014-05-25 14:13:07 +02:00
|
|
|
{
|
|
|
|
if (soul != mCellRef.mSoul)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mSoul = soul;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::setFaction(const std::string& faction)
|
|
|
|
{
|
|
|
|
if (faction != mCellRef.mFaction)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mFaction = faction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::setLockLevel(int lockLevel)
|
|
|
|
{
|
|
|
|
if (lockLevel != mCellRef.mLockLevel)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mLockLevel = lockLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 20:30:37 +02:00
|
|
|
void CellRef::lock(int lockLevel)
|
|
|
|
{
|
|
|
|
if (lockLevel != 0)
|
|
|
|
setLockLevel(abs(lockLevel)); // Changes lock to locklevel, if positive
|
|
|
|
else
|
|
|
|
setLockLevel(ESM::UnbreakableLock); // If zero, set to max lock level
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::unlock()
|
|
|
|
{
|
|
|
|
setLockLevel(-abs(mCellRef.mLockLevel)); // Makes lockLevel negative
|
|
|
|
}
|
|
|
|
|
2014-05-25 14:13:07 +02:00
|
|
|
void CellRef::setTrap(const std::string& trap)
|
|
|
|
{
|
|
|
|
if (trap != mCellRef.mTrap)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mTrap = trap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::setGoldValue(int value)
|
|
|
|
{
|
|
|
|
if (value != mCellRef.mGoldValue)
|
|
|
|
{
|
|
|
|
mChanged = true;
|
|
|
|
mCellRef.mGoldValue = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CellRef::writeState(ESM::ObjectState& state) const
|
|
|
|
{
|
|
|
|
state.mRef = mCellRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|