Skip to content

Commit 4460004

Browse files
authored
[Minor] Optimize GeneralUtils::GetWarheadVersusArmor() (#1700)
1 parent d0a94e4 commit 4460004

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Utilities/GeneralUtils.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <Ext/Techno/Body.h>
99
#include <Misc/FlyingStrings.h>
1010
#include <Utilities/Constructs.h>
11+
#include "AresHelper.h"
1112

1213
bool GeneralUtils::IsValidString(const char* str)
1314
{
@@ -70,23 +71,38 @@ const double GeneralUtils::GetRangedRandomOrSingleValue(PartialVector2D<double>
7071
return range.X >= range.Y || range.ValueCount < 2 ? range.X : (ScenarioClass::Instance->Random.RandomRanged(min, max) / 100.0);
7172
}
7273

73-
const double GeneralUtils::GetWarheadVersusArmor(WarheadTypeClass* pWH, Armor ArmorType)
74+
struct VersesData
7475
{
75-
return double(MapClass::GetTotalDamage(100, pWH, ArmorType, 0)) / 100.0;
76+
double Verses;
77+
WarheadFlags Flags;
78+
};
79+
80+
struct DummyTypeExtHere
81+
{
82+
char _[0x24];
83+
std::vector<VersesData> Verses;
84+
};
85+
86+
const double GeneralUtils::GetWarheadVersusArmor(WarheadTypeClass* pWH, Armor armorType)
87+
{
88+
if (!AresHelper::CanUseAres)
89+
return pWH->Verses[static_cast<int>(armorType)];
90+
91+
return reinterpret_cast<DummyTypeExtHere*>(*(uintptr_t*)((char*)pWH + 0x1CC))->Verses[static_cast<int>(armorType)].Verses;
7692
}
7793

7894
const double GeneralUtils::GetWarheadVersusArmor(WarheadTypeClass* pWH, TechnoClass* pThis, TechnoTypeClass* pType)
7995
{
8096
if (!pType)
8197
pType = pThis->GetTechnoType();
8298

83-
auto ArmorType = pType->Armor;
99+
auto armorType = pType->Armor;
84100
auto const pShield = TechnoExt::ExtMap.Find(pThis)->Shield.get();
85101

86102
if (pShield && pShield->IsActive() && !pShield->CanBePenetrated(pWH))
87-
ArmorType = pShield->GetArmorType();
103+
armorType = pShield->GetArmorType();
88104

89-
return double(MapClass::GetTotalDamage(100, pWH, ArmorType, 0)) / 100.0;
105+
return GeneralUtils::GetWarheadVersusArmor(pWH, armorType);
90106
}
91107

92108
// Weighted random element choice (weight) - roll for one.

src/Utilities/GeneralUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GeneralUtils
2828
static std::vector<CellStruct> AdjacentCellsInRange(unsigned int range);
2929
static const int GetRangedRandomOrSingleValue(PartialVector2D<int> range);
3030
static const double GetRangedRandomOrSingleValue(PartialVector2D<double> range);
31-
static const double GetWarheadVersusArmor(WarheadTypeClass* pWH, Armor ArmorType);
31+
static const double GetWarheadVersusArmor(WarheadTypeClass* pWH, Armor armorType);
3232
static const double GetWarheadVersusArmor(WarheadTypeClass* pWH, TechnoClass* pThis, TechnoTypeClass* pType = nullptr);
3333
static int ChooseOneWeighted(const double dice, const std::vector<int>* weights);
3434
static bool HasHealthRatioThresholdChanged(double oldRatio, double newRatio);

0 commit comments

Comments
 (0)