Skip to content

Commit

Permalink
Speedboost: cache CvPlayer::getYieldEquipmentAmount()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightinggale committed Jun 21, 2013
1 parent 19a54e5 commit 9e433ec
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 61 deletions.
Binary file modified Assets/CvGameCoreDLL.dll
Binary file not shown.
21 changes: 12 additions & 9 deletions DLL_Sources/CvCityAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1761,17 +1761,20 @@ int CvCityAI::AI_numPotentialDefenders() const
//int iEquipable = getPopulation();
int iEquipable = std::max(0, getPopulation() - 1);
// TAC - AI City Defense - koma13 - END
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
if (kOwner.hasContentsYieldEquipmentAmount(eLoopProfession)) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
int iAmount = kOwner.getYieldEquipmentAmount(eLoopProfession, (YieldTypes)iYield);

if (iAmount > 0)
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
// TAC - AI Buildings - koma13 - START
//iEquipable = std::min(iEquipable, getYieldStored((YieldTypes)iYield) / iAmount);
int iYieldsAvailabe = std::min(getYieldStored((YieldTypes)iYield), getMaxYieldCapacity() * 60 / 100);
iEquipable = std::min(iEquipable, iYieldsAvailabe / iAmount);
// TAC - AI Buildings - koma13 - END
int iAmount = kOwner.getYieldEquipmentAmount(eLoopProfession, (YieldTypes)iYield);

if (iAmount > 0)
{
// TAC - AI Buildings - koma13 - START
//iEquipable = std::min(iEquipable, getYieldStored((YieldTypes)iYield) / iAmount);
int iYieldsAvailabe = std::min(getYieldStored((YieldTypes)iYield), getMaxYieldCapacity() * 60 / 100);
iEquipable = std::min(iEquipable, iYieldsAvailabe / iAmount);
// TAC - AI Buildings - koma13 - END
}
}
}

Expand Down
1 change: 1 addition & 0 deletions DLL_Sources/CvEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ enum DllExport ProfessionTypes
{
INVALID_PROFESSION = -2,
NO_PROFESSION = -1,
NUM_PROFESSION_TYPES = 69, // precompile XML values - Nightinggale
};

enum DllExport SpecialUnitTypes
Expand Down
39 changes: 38 additions & 1 deletion DLL_Sources/CvPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ void CvPlayer::init(PlayerTypes eID)
}

AI_init();
Update_cache_YieldEquipmentAmount(); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}


Expand Down Expand Up @@ -594,6 +595,7 @@ void CvPlayer::reset(PlayerTypes eID, bool bConstructorCall)
{
AI_reset();
}
Update_cache_YieldEquipmentAmount(); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}


Expand Down Expand Up @@ -1994,6 +1996,7 @@ bool CvPlayer::isHuman() const

void CvPlayer::updateHuman()
{
bool old_m_bHuman = m_bHuman; // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
if (getID() == NO_PLAYER)
{
m_bHuman = false;
Expand All @@ -2009,6 +2012,13 @@ void CvPlayer::updateHuman()
m_bHuman = false;
}
// Dale - AoD: AI Autoplay END

// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
if (old_m_bHuman != m_bHuman)
{
Update_cache_YieldEquipmentAmount();
}
// cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}

bool CvPlayer::isNative() const
Expand Down Expand Up @@ -12073,6 +12083,8 @@ void CvPlayer::read(FDataStreamBase* pStream)
pStream->Read(&m_iRevolutionEuropeTradeCount);
pStream->Read(&m_iFatherPointMultiplier);
pStream->Read(&m_iMissionaryRateModifier);

Update_cache_YieldEquipmentAmount(); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}

//
Expand Down Expand Up @@ -15890,10 +15902,12 @@ void CvPlayer::setProfessionEquipmentModifier(ProfessionTypes eProfession, int i
}

FAssert(getProfessionEquipmentModifier(eProfession) >= -100);
Update_cache_YieldEquipmentAmount(eProfession); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}
}

int CvPlayer::getYieldEquipmentAmount(ProfessionTypes eProfession, YieldTypes eYield) const
// cache CvPlayer::getYieldEquipmentAmount - function rename - Nightinggale
int CvPlayer::getYieldEquipmentAmountUncached(ProfessionTypes eProfession, YieldTypes eYield) const
{
FAssert(eProfession >= 0 && eProfession < GC.getNumProfessionInfos());
FAssert(eYield >= 0 && eYield < NUM_YIELD_TYPES);
Expand All @@ -15915,6 +15929,29 @@ int CvPlayer::getYieldEquipmentAmount(ProfessionTypes eProfession, YieldTypes eY
return std::max(0, iAmount);
}

// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
void CvPlayer::Update_cache_YieldEquipmentAmount(ProfessionTypes eProfession)
{
for (int iYield = 0; iYield < NUM_YIELD_TYPES; iYield++) {
m_cache_YieldEquipmentAmount[eProfession].set(getYieldEquipmentAmountUncached(eProfession, (YieldTypes)iYield), iYield);
}
m_cache_YieldEquipmentAmount[eProfession].isEmpty(); // This will release the array if it's empty
}

void CvPlayer::Update_cache_YieldEquipmentAmount()
{
if (m_eID <= NO_PLAYER || m_aiProfessionEquipmentModifier == NULL)
{
// Some update calls gets triggered during player init. They can safely be ignored.
return;
}

for (int iProfession = 0; iProfession < NUM_PROFESSION_TYPES; iProfession++) {
Update_cache_YieldEquipmentAmount((ProfessionTypes)iProfession);
}
}
// cache CvPlayer::getYieldEquipmentAmount - end - Nightinggale

bool CvPlayer::isProfessionValid(ProfessionTypes eProfession, UnitTypes eUnit) const
{
if (eProfession != NO_PROFESSION)
Expand Down
40 changes: 40 additions & 0 deletions DLL_Sources/CvPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,11 @@ class CvPlayer
int getProfessionEquipmentModifier(ProfessionTypes eProfession) const;
void setProfessionEquipmentModifier(ProfessionTypes eProfession, int iValue);
int getYieldEquipmentAmount(ProfessionTypes eProfession, YieldTypes eYield) const;
// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
bool hasContentsYieldEquipmentAmount(ProfessionTypes eProfession) const;
int getYieldEquipmentAmountSecure(ProfessionTypes eProfession, YieldTypes eYield) const;
bool hasContentsYieldEquipmentAmountSecure(ProfessionTypes eProfession) const;
// cache CvPlayer::getYieldEquipmentAmount - end - Nightinggale
bool isProfessionValid(ProfessionTypes eProfession, UnitTypes eUnit) const;
void changeProfessionEurope(int iUnitId, ProfessionTypes eProfession);

Expand Down Expand Up @@ -934,6 +939,13 @@ class CvPlayer
int* m_aiProfessionEquipmentModifier;
int* m_aiTraitCount;

// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
YieldArray<int> m_cache_YieldEquipmentAmount[NUM_PROFESSION_TYPES];
void Update_cache_YieldEquipmentAmount();
void Update_cache_YieldEquipmentAmount(ProfessionTypes eProfession);
int getYieldEquipmentAmountUncached(ProfessionTypes eProfession, YieldTypes eYield) const;
// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale

std::vector<EventTriggerTypes> m_triggersFired;
CivicTypes* m_paeCivics;
int** m_ppiImprovementYieldChange;
Expand Down Expand Up @@ -991,4 +1003,32 @@ class CvPlayer
void doUpdateCacheOnTurn();
};

// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
inline int CvPlayer::getYieldEquipmentAmount(ProfessionTypes eProfession, YieldTypes eYield) const
{
FAssert(eProfession >= 0 && eProfession < NUM_PROFESSION_TYPES);
return m_cache_YieldEquipmentAmount[eProfession].get(eYield);
}

inline bool CvPlayer::hasContentsYieldEquipmentAmount(ProfessionTypes eProfession) const
{
// strictly speaking it returns true if the array is allocated without considering the content of the array.
// The reason why it works is because Update_cache_YieldEquipmentAmount() will only allocate arrays if they contain anything
// and deallocate them if it is changed to contain only 0.
FAssert(eProfession >= 0 && eProfession < NUM_PROFESSION_TYPES);
return m_cache_YieldEquipmentAmount[eProfession].isAllocated();
}

// same functions, but with the added return 0 if professions is NO_PROFESSION or INVALID_PROFESSION
inline int CvPlayer::getYieldEquipmentAmountSecure(ProfessionTypes eProfession, YieldTypes eYield) const
{
return eProfession > NO_PROFESSION ? getYieldEquipmentAmount(eProfession, eYield) : 0;
}

inline bool CvPlayer::hasContentsYieldEquipmentAmountSecure(ProfessionTypes eProfession) const
{
return eProfession > NO_PROFESSION ? hasContentsYieldEquipmentAmount(eProfession) : false;
}
// cache CvPlayer::getYieldEquipmentAmount - end - Nightinggale

#endif
13 changes: 8 additions & 5 deletions DLL_Sources/CvPlayerAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11115,15 +11115,18 @@ int CvPlayerAI::AI_professionSuitability(UnitTypes eUnit, ProfessionTypes eProfe
}
else
{
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
if (hasContentsYieldEquipmentAmount((ProfessionTypes)kUnit.getDefaultProfession())) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
if (getYieldEquipmentAmount((ProfessionTypes)kUnit.getDefaultProfession(), (YieldTypes) iYield) > 0)
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
if (getYieldEquipmentAmount(eProfession, (YieldTypes) iYield) == 0)
if (getYieldEquipmentAmount((ProfessionTypes)kUnit.getDefaultProfession(), (YieldTypes) iYield) > 0)
{
iConModifiers += 50;
if (getYieldEquipmentAmount(eProfession, (YieldTypes) iYield) == 0)
{
iConModifiers += 50;
}
break;
}
break;
}
}
}
Expand Down
78 changes: 46 additions & 32 deletions DLL_Sources/CvUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7623,10 +7623,14 @@ int CvUnit::getPower() const
if (getProfession() != NO_PROFESSION)
{
iPower += GC.getProfessionInfo(getProfession()).getPowerValue();
for (int i = 0; i < NUM_YIELD_TYPES; ++i)

if (GET_PLAYER(getOwnerINLINE()).hasContentsYieldEquipmentAmount(getProfession())) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
YieldTypes eYield = (YieldTypes) i;
iPower += GC.getYieldInfo(eYield).getPowerValue() * GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(getProfession(), eYield);
for (int i = 0; i < NUM_YIELD_TYPES; ++i)
{
YieldTypes eYield = (YieldTypes) i;
iPower += GC.getYieldInfo(eYield).getPowerValue() * GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(getProfession(), eYield);
}
}
}

Expand All @@ -7645,10 +7649,14 @@ int CvUnit::getAsset() const
if (getProfession() != NO_PROFESSION)
{
iAsset += GC.getProfessionInfo(getProfession()).getAssetValue();
for (int i = 0; i < NUM_YIELD_TYPES; ++i)

if (GET_PLAYER(getOwnerINLINE()).hasContentsYieldEquipmentAmount(getProfession())) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
YieldTypes eYield = (YieldTypes) i;
iAsset += GC.getYieldInfo(eYield).getAssetValue() * GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(getProfession(), eYield);
for (int i = 0; i < NUM_YIELD_TYPES; ++i)
{
YieldTypes eYield = (YieldTypes) i;
iAsset += GC.getYieldInfo(eYield).getAssetValue() * GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(getProfession(), eYield);
}
}
}
YieldTypes eYield = getYield();
Expand Down Expand Up @@ -10032,21 +10040,20 @@ bool CvUnit::canHaveProfession(ProfessionTypes eProfession, bool bBumpOther, con
//make sure all equipment is available
if (!pCity->AI_isWorkforceHack())
{
for (int i=0; i < NUM_YIELD_TYPES; ++i)
if (kOwner.hasContentsYieldEquipmentAmountSecure(getProfession()) || kOwner.hasContentsYieldEquipmentAmount(eProfession)) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
YieldTypes eYieldType = (YieldTypes) i;
int iYieldCarried = 0;
if (getProfession() != NO_PROFESSION)
{
iYieldCarried += kOwner.getYieldEquipmentAmount(getProfession(), eYieldType);
}
int iYieldRequired = kOwner.getYieldEquipmentAmount(eProfession, eYieldType);
if (iYieldRequired > 0)
for (int i=0; i < NUM_YIELD_TYPES; ++i)
{
int iMissing = iYieldRequired - iYieldCarried;
if (iMissing > pCity->getYieldStored(eYieldType))
YieldTypes eYieldType = (YieldTypes) i;
int iYieldCarried = kOwner.getYieldEquipmentAmountSecure(getProfession(), eYieldType); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
int iYieldRequired = kOwner.getYieldEquipmentAmount(eProfession, eYieldType);
if (iYieldRequired > 0)
{
return false;
int iMissing = iYieldRequired - iYieldCarried;
if (iMissing > pCity->getYieldStored(eYieldType))
{
return false;
}
}
}
}
Expand All @@ -10068,14 +10075,17 @@ bool CvUnit::canHaveProfession(ProfessionTypes eProfession, bool bBumpOther, con
return false;
}

for (int i=0; i < NUM_YIELD_TYPES; ++i)
if (kOwner.hasContentsYieldEquipmentAmount(eProfession)) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
YieldTypes eYield = (YieldTypes) i;
if (!kOwner.isYieldEuropeTradable(eYield))
for (int i=0; i < NUM_YIELD_TYPES; ++i)
{
if (kOwner.getYieldEquipmentAmount(eProfession, eYield) > kOwner.getYieldEquipmentAmount(getProfession(), eYield))
YieldTypes eYield = (YieldTypes) i;
if (!kOwner.isYieldEuropeTradable(eYield))
{
return false;
if (kOwner.getYieldEquipmentAmount(eProfession, eYield) > kOwner.getYieldEquipmentAmount(getProfession(), eYield))
{
return false;
}
}
}
}
Expand Down Expand Up @@ -10197,12 +10207,16 @@ void CvUnit::processProfession(ProfessionTypes eProfession, int iChange, bool bU
kOwner.changeAssets(iChange * kProfession.getAssetValue());

int iPower = iChange * kProfession.getPowerValue();
for (int i = 0; i < NUM_YIELD_TYPES; ++i)

if (GET_PLAYER(getOwnerINLINE()).hasContentsYieldEquipmentAmount(eProfession)) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
YieldTypes eYield = (YieldTypes) i;
int iYieldAmount = GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(eProfession, eYield);
iPower += iChange * GC.getYieldInfo(eYield).getPowerValue() * iYieldAmount;
kOwner.changeAssets(iChange * GC.getYieldInfo(eYield).getAssetValue() * iYieldAmount);
for (int i = 0; i < NUM_YIELD_TYPES; ++i)
{
YieldTypes eYield = (YieldTypes) i;
int iYieldAmount = GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(eProfession, eYield);
iPower += iChange * GC.getYieldInfo(eYield).getPowerValue() * iYieldAmount;
kOwner.changeAssets(iChange * GC.getYieldInfo(eYield).getAssetValue() * iYieldAmount);
}
}

kOwner.changePower(iPower);
Expand All @@ -10228,7 +10242,7 @@ void CvUnit::processProfession(ProfessionTypes eProfession, int iChange, bool bU
{
if (iChange != 0)
{
if (eProfession != NO_PROFESSION && (pCity->getPopulation() > 0 || GC.getDefineINT("CONSUME_EQUIPMENT_ON_FOUND") != 0))
if (GET_PLAYER(getOwnerINLINE()).hasContentsYieldEquipmentAmountSecure(eProfession) && (pCity->getPopulation() > 0 || GC.getDefineINT("CONSUME_EQUIPMENT_ON_FOUND") != 0)) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
for (int i = 0; i < NUM_YIELD_TYPES; i++)
{
Expand Down Expand Up @@ -12840,10 +12854,10 @@ bool CvUnit::raidWeapons(CvUnit* pUnit)
}

std::vector<int> aYields(NUM_YIELD_TYPES, 0);
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
CvPlayer& kOwner = GET_PLAYER(pUnit->getOwnerINLINE());
if (kOwner.hasContentsYieldEquipmentAmountSecure(pUnit->getProfession())) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
CvPlayer& kOwner = GET_PLAYER(pUnit->getOwnerINLINE());
if (pUnit->getProfession() != NO_PROFESSION)
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
aYields[iYield] += kOwner.getYieldEquipmentAmount(pUnit->getProfession(), (YieldTypes) iYield);
}
Expand Down
31 changes: 17 additions & 14 deletions DLL_Sources/CvUnitAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14442,29 +14442,32 @@ bool CvUnitAI::AI_joinCityDefender()
CvProfessionInfo& kProfession = GC.getProfessionInfo(getProfession());

int iCapacity = pCity->getMaxYieldCapacity();
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
if (GET_PLAYER(getOwnerINLINE()).hasContentsYieldEquipmentAmount(getProfession())) // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
{
int iAmount = GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(getProfession(), (YieldTypes)iYield);
if (iAmount > 0)
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
//VET NewCapacity - begin 3/4
if (GC.getNEW_CAPACITY())
int iAmount = GET_PLAYER(getOwnerINLINE()).getYieldEquipmentAmount(getProfession(), (YieldTypes)iYield);
if (iAmount > 0)
{
if ((pCity->getTotalYieldStored() + (iAmount * 2)) > iCapacity)
{return false;}
}
else
//VET NewCapacity - begin 3/4
if (GC.getNEW_CAPACITY())
{
if ((pCity->getTotalYieldStored() + (iAmount * 2)) > iCapacity)
{return false;}
}
else
//VET NewCapacity - begin 3/4 (ray fix)
{
//VET NewCapacity - end 3/4 (ray fix)
//VET NewCapacity - end 3/4
if ((pCity->getYieldStored((YieldTypes)iYield) + (iAmount * 2)) > iCapacity)
{
return false;
}
if ((pCity->getYieldStored((YieldTypes)iYield) + (iAmount * 2)) > iCapacity)
{
return false;
}
//VET NewCapacity - begin 3/4 (ray fix)
}
}
//VET NewCapacity - end 3/4 (ray fix)
}
}
}

Expand Down

0 comments on commit 9e433ec

Please sign in to comment.