mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-10 22:31:40 +02:00
Add caching for commonly accessed values
This commit is contained in:
@@ -670,14 +670,18 @@ void CCreatureSet::serializeJson(JsonSerializeFormat & handler, const std::strin
|
||||
}
|
||||
}
|
||||
|
||||
CStackInstance::CStackInstance()
|
||||
: armyObj(_armyObj)
|
||||
CStackInstance::CStackInstance(bool isHypothetic)
|
||||
: CBonusSystemNode(isHypothetic),
|
||||
armyObj(_armyObj),
|
||||
nativeTerrain(this, Selector::type()(BonusType::TERRAIN_NATIVE)),
|
||||
initiative(this, Selector::type()(BonusType::STACKS_SPEED))
|
||||
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
CStackInstance::CStackInstance(const CreatureID & id, TQuantity Count, bool isHypothetic):
|
||||
CBonusSystemNode(isHypothetic), armyObj(_armyObj)
|
||||
CStackInstance::CStackInstance(const CreatureID & id, TQuantity Count, bool isHypothetic)
|
||||
: CStackInstance(false)
|
||||
{
|
||||
init();
|
||||
setType(id);
|
||||
@@ -685,7 +689,7 @@ CStackInstance::CStackInstance(const CreatureID & id, TQuantity Count, bool isHy
|
||||
}
|
||||
|
||||
CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count, bool isHypothetic)
|
||||
: CBonusSystemNode(isHypothetic), armyObj(_armyObj)
|
||||
: CStackInstance(false)
|
||||
{
|
||||
init();
|
||||
setType(cre);
|
||||
@@ -834,6 +838,22 @@ PlayerColor CStackInstance::getOwner() const
|
||||
return _armyObj ? _armyObj->getOwner() : PlayerColor::NEUTRAL;
|
||||
}
|
||||
|
||||
int32_t CStackInstance::getInitiative(int turn) const
|
||||
{
|
||||
if (turn == 0)
|
||||
return initiative.getValue();
|
||||
|
||||
return ACreature::getInitiative(turn);
|
||||
}
|
||||
|
||||
TerrainId CStackInstance::getNativeTerrain() const
|
||||
{
|
||||
if (nativeTerrain.hasBonus())
|
||||
return TerrainId::ANY_TERRAIN;
|
||||
|
||||
return getFactionID().toEntity(VLC)->getNativeTerrain();
|
||||
}
|
||||
|
||||
void CStackInstance::deserializationFix()
|
||||
{
|
||||
const CArmedInstance *armyBackup = _armyObj;
|
||||
|
@@ -10,6 +10,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "bonuses/Bonus.h"
|
||||
#include "bonuses/BonusCache.h"
|
||||
#include "bonuses/CBonusSystemNode.h"
|
||||
#include "serializer/Serializeable.h"
|
||||
#include "GameConstants.h"
|
||||
@@ -71,6 +72,9 @@ public:
|
||||
|
||||
class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet, public ACreature
|
||||
{
|
||||
BonusValueCache nativeTerrain;
|
||||
BonusValueCache initiative;
|
||||
|
||||
protected:
|
||||
const CArmedInstance *_armyObj; //stack must be part of some army, army must be part of some object
|
||||
|
||||
@@ -119,7 +123,7 @@ public:
|
||||
CreatureID getCreatureID() const; //-1 if not available
|
||||
std::string getName() const; //plural or singular
|
||||
virtual void init();
|
||||
CStackInstance();
|
||||
CStackInstance(bool isHypothetic = false);
|
||||
CStackInstance(const CreatureID & id, TQuantity count, bool isHypothetic = false);
|
||||
CStackInstance(const CCreature *cre, TQuantity count, bool isHypothetic = false);
|
||||
virtual ~CStackInstance() = default;
|
||||
@@ -135,6 +139,9 @@ public:
|
||||
std::string nodeName() const override; //from CBonusSystemnode
|
||||
void deserializationFix();
|
||||
PlayerColor getOwner() const override;
|
||||
|
||||
int32_t getInitiative(int turn = 0) const final;
|
||||
TerrainId getNativeTerrain() const final;
|
||||
};
|
||||
|
||||
class DLL_LINKAGE CCommanderInstance : public CStackInstance
|
||||
|
Reference in New Issue
Block a user