Skip to content

Commit fc80cbb

Browse files
CoroniachaserliStarkku
authored
[Minor] Optimize codestyle (#1740)
Split from #1535. Despite editing many files, it only contains 3 changes mostly: - added const to variables that's not supposed to be changed, so the compiler might optimize it during building the dll - cached some variables that would be used multiple time to reduce pointer call - remove sanity check from `ExtMap.Find` and add `ExtMap.TryFind` --------- Co-authored-by: Trsdy <[email protected]> Co-authored-by: Starkku <[email protected]>
1 parent 8d3fca3 commit fc80cbb

File tree

122 files changed

+2104
-1805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+2104
-1805
lines changed

CREDITS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ This page lists all the individual contributions to the project by their author.
544544
- Allow faking digital display for `InfoType=Health` at disguise
545545
- Display banner improvement and doc
546546
- Damage multiplier for health percentage
547+
- Linked superweapons tweak
547548
- **NaotoYuuki** - Vertical & meteor trajectory projectile prototypes
548549
- **handama** - AI script action to `16005 Jump Back To Previous Script`
549550
- **TaranDahl (航味麻酱)**:

src/Commands/ObjectInfo.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,27 @@ void ObjectInfoCommandClass::Execute(WWKey eInput) const
7575
auto printFoots = [&append, &display, &getTargetInfo](FootClass* pFoot)
7676
{
7777
append("[Phobos] Dump ObjectInfo runs.\n");
78-
auto pType = pFoot->GetTechnoType();
78+
auto const pType = pFoot->GetTechnoType();
7979
append("ID = %s, ", pType->ID);
8080
append("Owner = %s (%s), ", pFoot->Owner->get_ID(), pFoot->Owner->PlainName);
8181
append("Location = (%d, %d), ", pFoot->GetMapCoords().X, pFoot->GetMapCoords().Y);
8282
append("Mission = %d (%s), Status = %d\n", pFoot->CurrentMission, MissionControlClass::FindName(pFoot->CurrentMission), pFoot->MissionStatus);
8383

8484
if (pFoot->BelongsToATeam())
8585
{
86-
auto pTeam = pFoot->Team;
86+
auto const pTeam = pFoot->Team;
8787

88-
auto pTeamType = pFoot->Team->Type;
88+
auto const pTeamType = pFoot->Team->Type;
8989
bool found = false;
9090
for (int i = 0; i < AITriggerTypeClass::Array.Count && !found; i++)
9191
{
92-
auto pTriggerTeam1Type = AITriggerTypeClass::Array.GetItem(i)->Team1;
93-
auto pTriggerTeam2Type = AITriggerTypeClass::Array.GetItem(i)->Team2;
92+
auto const pTriggerTeam1Type = AITriggerTypeClass::Array.GetItem(i)->Team1;
93+
auto const pTriggerTeam2Type = AITriggerTypeClass::Array.GetItem(i)->Team2;
9494

9595
if (pTeamType && ((pTriggerTeam1Type && pTriggerTeam1Type == pTeamType) || (pTriggerTeam2Type && pTriggerTeam2Type == pTeamType)))
9696
{
9797
found = true;
98-
auto pTriggerType = AITriggerTypeClass::Array.GetItem(i);
98+
auto const pTriggerType = AITriggerTypeClass::Array.GetItem(i);
9999
append("Trigger ID = %s, weights [Current, Min, Max]: %f, %f, %f", pTriggerType->ID, pTriggerType->Weight_Current, pTriggerType->Weight_Minimum, pTriggerType->Weight_Maximum);
100100
}
101101
}
@@ -141,8 +141,8 @@ void ObjectInfoCommandClass::Execute(WWKey eInput) const
141141

142142
append("Current HP = (%d / %d)", pFoot->Health, pType->Strength);
143143

144-
auto pTechnoExt = TechnoExt::ExtMap.Find(pFoot);
145-
auto pShieldData = pTechnoExt->Shield.get();
144+
auto const pTechnoExt = TechnoExt::ExtMap.Find(pFoot);
145+
auto const pShieldData = pTechnoExt->Shield.get();
146146

147147
if (pTechnoExt->CurrentShieldType && pShieldData)
148148
append(", Current Shield HP = (%d / %d)", pShieldData->GetHP(), pTechnoExt->CurrentShieldType->Strength);
@@ -157,7 +157,7 @@ void ObjectInfoCommandClass::Execute(WWKey eInput) const
157157
auto printBuilding = [&append, &display, &getTargetInfo](BuildingClass* pBuilding)
158158
{
159159
append("[Phobos] Dump ObjectInfo runs.\n");
160-
auto pType = pBuilding->Type;
160+
auto const pType = pBuilding->Type;
161161
append("ID = %s, ", pType->ID);
162162
append("Owner = %s (%s), ", pBuilding->Owner->get_ID(), pBuilding->Owner->PlainName);
163163
append("Location = (%d, %d), ", pBuilding->GetMapCoords().X, pBuilding->GetMapCoords().Y);
@@ -210,8 +210,8 @@ void ObjectInfoCommandClass::Execute(WWKey eInput) const
210210

211211
append("Current HP = (%d / %d)\n", pBuilding->Health, pBuilding->Type->Strength);
212212

213-
auto pTechnoExt = TechnoExt::ExtMap.Find(pBuilding);
214-
auto pShieldData = pTechnoExt->Shield.get();
213+
auto const pTechnoExt = TechnoExt::ExtMap.Find(pBuilding);
214+
auto const pShieldData = pTechnoExt->Shield.get();
215215

216216
if (pTechnoExt->CurrentShieldType && pShieldData)
217217
append("Current Shield HP = (%d / %d)\n", pShieldData->GetHP(), pTechnoExt->CurrentShieldType->Strength);
@@ -240,7 +240,7 @@ void ObjectInfoCommandClass::Execute(WWKey eInput) const
240240
dumped = true;
241241
};
242242

243-
for (auto pTechno : TechnoClass::Array)
243+
for (auto const pTechno : TechnoClass::Array)
244244
{
245245
if (dumped) break;
246246
if (pTechno->IsMouseHovering)

src/Ext/Aircraft/Body.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@ void AircraftExt::FireWeapon(AircraftClass* pThis, AbstractClass* pTarget)
1515
if (weaponIndex < 0)
1616
weaponIndex = pThis->SelectWeapon(pTarget);
1717

18-
bool isStrafe = pThis->Is_Strafe();
1918
auto const pExt = TechnoExt::ExtMap.Find(pThis);
2019
auto const pWeapon = pThis->GetWeapon(weaponIndex)->WeaponType;
2120
auto const pWeaponExt = WeaponTypeExt::ExtMap.Find(pWeapon);
21+
const int burstCount = pWeapon->Burst;
22+
const bool isStrafe = pThis->Is_Strafe();
2223

23-
if (pWeapon->Burst > 0)
24+
if (burstCount > 0)
2425
{
25-
for (int i = 0; i < pWeapon->Burst; i++)
26+
int& bombDropCount = pExt->Strafe_BombsDroppedThisRound;
27+
int& currentBurstIndex = pThis->CurrentBurstIndex;
28+
const bool simulateBurst = pWeaponExt->Strafing_SimulateBurst;
29+
30+
for (int i = 0; i < burstCount; i++)
2631
{
27-
if (isStrafe && pWeapon->Burst < 2 && pWeaponExt->Strafing_SimulateBurst)
28-
pThis->CurrentBurstIndex = pExt->Strafe_BombsDroppedThisRound % 2 == 0;
32+
if (isStrafe && burstCount < 2 && simulateBurst)
33+
currentBurstIndex = bombDropCount % 2 == 0;
2934

3035
pThis->Fire(pTarget, weaponIndex);
3136
}
3237

3338
if (isStrafe)
3439
{
35-
pExt->Strafe_BombsDroppedThisRound++;
40+
bombDropCount++;
3641

3742
if (pWeaponExt->Strafing_UseAmmoPerShot)
3843
{
@@ -52,7 +57,8 @@ void AircraftExt::FireWeapon(AircraftClass* pThis, AbstractClass* pTarget)
5257
// Spy plane, airstrike etc.
5358
bool AircraftExt::PlaceReinforcementAircraft(AircraftClass* pThis, CellStruct edgeCell)
5459
{
55-
auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->Type);
60+
auto const pType = pThis->Type;
61+
auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pType);
5662
auto coords = CellClass::Cell2Coord(edgeCell);
5763
coords.Z = 0;
5864
AbstractClass* pTarget = nullptr;
@@ -66,10 +72,10 @@ bool AircraftExt::PlaceReinforcementAircraft(AircraftClass* pThis, CellStruct ed
6672
}
6773

6874
++Unsorted::ScenarioInit;
69-
bool result = pThis->Unlimbo(coords, DirType::North);
75+
const bool result = pThis->Unlimbo(coords, DirType::North);
7076
--Unsorted::ScenarioInit;
7177

72-
pThis->SetHeight(pTypeExt->SpawnHeight.Get(pThis->Type->GetFlightLevel()));
78+
pThis->SetHeight(pTypeExt->SpawnHeight.Get(pType->GetFlightLevel()));
7379

7480
if (pTarget)
7581
pThis->PrimaryFacing.SetDesired(pThis->GetTargetDirection(pTarget));
@@ -85,18 +91,21 @@ DirType AircraftExt::GetLandingDir(AircraftClass* pThis, BuildingClass* pDock)
8591
return poseDir;
8692

8793
// If this is a spawnee, use the spawner's facing.
88-
if (auto pOwner = pThis->SpawnOwner)
94+
if (auto const pOwner = pThis->SpawnOwner)
8995
return pOwner->PrimaryFacing.Current().GetDir();
9096

97+
auto const pType = pThis->Type;
98+
9199
if (pDock || pThis->HasAnyLink())
92100
{
93-
auto pLink = pThis->GetNthLink(0);
101+
auto const pLink = pThis->GetNthLink(0);
94102

95-
if (auto pBuilding = pDock ? pDock : abstract_cast<BuildingClass*>(pLink))
103+
if (auto const pBuilding = pDock ? pDock : abstract_cast<BuildingClass*, true>(pLink))
96104
{
97-
auto const pBuildingTypeExt = BuildingTypeExt::ExtMap.Find(pBuilding->Type);
98-
int docks = pBuilding->Type->NumberOfDocks;
99-
int linkIndex = pBuilding->FindLinkIndex(pThis);
105+
auto const pBuildingType = pBuilding->Type;
106+
auto const pBuildingTypeExt = BuildingTypeExt::ExtMap.Find(pBuildingType);
107+
const int docks = pBuildingType->NumberOfDocks;
108+
const int linkIndex = pBuilding->FindLinkIndex(pThis);
100109

101110
if (docks > 0 && linkIndex >= 0 && linkIndex < docks)
102111
{
@@ -106,13 +115,13 @@ DirType AircraftExt::GetLandingDir(AircraftClass* pThis, BuildingClass* pDock)
106115
else if (docks > 0 && pBuildingTypeExt->AircraftDockingDirs[0].has_value())
107116
return *pBuildingTypeExt->AircraftDockingDirs[0];
108117
}
109-
else if (!pThis->Type->AirportBound)
118+
else if (!pType->AirportBound)
110119
return pLink->PrimaryFacing.Current().GetDir();
111120
}
112121

113-
int landingDir = TechnoTypeExt::ExtMap.Find(pThis->Type)->LandingDir.Get((int)poseDir);
122+
const int landingDir = TechnoTypeExt::ExtMap.Find(pType)->LandingDir.Get((int)poseDir);
114123

115-
if (!pThis->Type->AirportBound && landingDir < 0)
124+
if (!pType->AirportBound && landingDir < 0)
116125
return pThis->PrimaryFacing.Current().GetDir();
117126

118127
return static_cast<DirType>(std::clamp(landingDir, 0, 255));

src/Ext/Aircraft/Hooks.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ DEFINE_HOOK(0x417FF1, AircraftClass_Mission_Attack_StrafeShots, 0x6)
3535
return 0;
3636
}
3737

38-
int strafingShots = pWeaponExt->Strafing_Shots.Get(5);
38+
const int strafingShots = pWeaponExt->Strafing_Shots.Get(5);
3939

4040
if (strafingShots > 5)
4141
{
4242
if (state == AirAttackStatus::FireAtTarget3_Strafe)
4343
{
44-
int remainingShots = strafingShots - 3 - pExt->Strafe_BombsDroppedThisRound;
44+
const int remainingShots = strafingShots - 3 - pExt->Strafe_BombsDroppedThisRound;
4545

4646
if (remainingShots > 0)
4747
pThis->MissionStatus = (int)AirAttackStatus::FireAtTarget2_Strafe;
@@ -68,7 +68,7 @@ DEFINE_HOOK(0x4197F3, AircraftClass_GetFireLocation_Strafing, 0x5)
6868
if (weaponIndex < 0)
6969
weaponIndex = pThis->SelectWeapon(pTarget);
7070

71-
auto fireError = pThis->GetFireError(pTarget, weaponIndex, false);
71+
auto const fireError = pThis->GetFireError(pTarget, weaponIndex, false);
7272

7373
if (fireError == FireError::ILLEGAL || fireError == FireError::CANT)
7474
return 0;
@@ -168,7 +168,7 @@ DEFINE_HOOK(0x418B1F, AircraftClass_Mission_Attack_FireAtTarget5Strafe_BurstFix,
168168
static int GetDelay(AircraftClass* pThis, bool isLastShot)
169169
{
170170
auto const pExt = TechnoExt::ExtMap.Find(pThis);
171-
int weaponIndex = pExt->CurrentAircraftWeaponIndex >= 0 ? pExt->CurrentAircraftWeaponIndex : pThis->SelectWeapon(pThis->Target);
171+
const int weaponIndex = pExt->CurrentAircraftWeaponIndex >= 0 ? pExt->CurrentAircraftWeaponIndex : pThis->SelectWeapon(pThis->Target);
172172
auto const pWeapon = pThis->GetWeapon(weaponIndex)->WeaponType;
173173
auto const pWeaponExt = WeaponTypeExt::ExtMap.Find(pWeapon);
174174
int delay = pWeapon->ROF;
@@ -356,7 +356,7 @@ DEFINE_HOOK(0x44402E, BuildingClass_ExitObject_PoseDir2, 0x5)
356356
GET(BuildingClass*, pThis, ESI);
357357
GET(AircraftClass*, pAircraft, EBP);
358358

359-
auto dir = DirStruct(AircraftExt::GetLandingDir(pAircraft, pThis));
359+
auto const dir = DirStruct(AircraftExt::GetLandingDir(pAircraft, pThis));
360360

361361
if (RulesExt::Global()->ExtendedAircraftMissions)
362362
pAircraft->PrimaryFacing.SetCurrent(dir);
@@ -373,7 +373,7 @@ DEFINE_HOOK(0x415EEE, AircraftClass_Fire_KickOutPassengers, 0x6)
373373
enum { SkipKickOutPassengers = 0x415F08 };
374374

375375
GET(AircraftClass*, pThis, EDI);
376-
GET_BASE(int, weaponIdx, 0xC);
376+
GET_BASE(const int, weaponIdx, 0xC);
377377

378378
auto const pWeapon = pThis->GetWeapon(weaponIdx)->WeaponType;
379379

@@ -405,7 +405,7 @@ DEFINE_HOOK(0x416A0A, AircraftClass_Mission_Move_SmoothMoving, 0x5)
405405
enum { EnterIdleAndReturn = 0x416AC0, ContinueMoving1 = 0x416908, ContinueMoving2 = 0x416A47 };
406406

407407
GET(AircraftClass* const, pThis, ESI);
408-
GET(CoordStruct* const, pCoords, EAX);
408+
GET(CoordStruct const* const, pCoords, EAX);
409409

410410
if (pThis->Team || pThis->Airstrike || pThis->Spawned)
411411
return 0;
@@ -420,7 +420,7 @@ DEFINE_HOOK(0x416A0A, AircraftClass_Mission_Move_SmoothMoving, 0x5)
420420
if (!TechnoTypeExt::ExtMap.Find(pType)->ExtendedAircraftMissions_SmoothMoving.Get(extendedMissions))
421421
return 0;
422422

423-
const int distance = Game::F2I(Point2D { pCoords->X, pCoords->Y }.DistanceFrom(Point2D { pThis->Location.X, pThis->Location.Y }));
423+
const int distance = static_cast<int>(Point2D { pCoords->X, pCoords->Y }.DistanceFrom(Point2D { pThis->Location.X, pThis->Location.Y }));
424424

425425
// When the horizontal distance between the aircraft and its destination is greater than half of its deceleration distance
426426
// or its turning radius, continue to move forward, otherwise return to airbase or execute the next planning waypoint
@@ -833,10 +833,12 @@ DEFINE_HOOK(0x708FC0, TechnoClass_ResponseMove_Pickup, 0x5)
833833

834834
if (auto const pAircraft = abstract_cast<AircraftClass*>(pThis))
835835
{
836-
if (pAircraft->Type->Carryall && pAircraft->HasAnyLink() &&
836+
auto const pType = pAircraft->Type;
837+
838+
if (pType->Carryall && pAircraft->HasAnyLink() &&
837839
generic_cast<FootClass*>(pAircraft->Destination))
838840
{
839-
auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pAircraft->Type);
841+
auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pType);
840842

841843
if (pTypeExt->VoicePickup.isset())
842844
{

0 commit comments

Comments
 (0)