Skip to content

Commit 0698da6

Browse files
CoroniaCrimRecya
andauthored
[Minor] optimization & fix batch 2 (#1685)
- fixed edgy crash caused by `FirstPassenger` being nullptr - attempted to fix #1600 making paradropped units can't enter buildings - only call `RecalculateStatMultiplier` when an AE is expired, discarded or deactivated - passenger `AutoDeath` inside a transport can now trigger `Gunner` change properly - adjusted `abstract_cast` where sanity check is not needed - other optimization in logic and code style --------- Co-authored-by: CrimRecya <[email protected]>
1 parent 75ecfd9 commit 0698da6

Some content is hidden

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

42 files changed

+317
-353
lines changed

src/Commands/ObjectInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ void ObjectInfoCommandClass::Execute(WWKey eInput) const
113113
display();
114114
}
115115

116-
if (pFoot->Passengers.NumPassengers > 0)
116+
if (FootClass* pCurrent = pFoot->Passengers.GetFirstPassenger())
117117
{
118-
FootClass* pCurrent = pFoot->Passengers.FirstPassenger;
119118
append("%d Passengers: %s", pFoot->Passengers.NumPassengers, pCurrent->GetTechnoType()->ID);
120119
for (pCurrent = abstract_cast<FootClass*>(pCurrent->NextObject); pCurrent; pCurrent = abstract_cast<FootClass*>(pCurrent->NextObject))
121120
append(", %s", pCurrent->GetTechnoType()->ID);

src/Ext/Aircraft/Hooks.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@ DEFINE_HOOK(0x4197F3, AircraftClass_GetFireLocation_Strafing, 0x5)
5757
GET(AircraftClass*, pThis, EDI);
5858
GET(AbstractClass*, pTarget, EAX);
5959

60-
if (!pTarget)
61-
return 0;
62-
60+
// pTarget can be nullptr
6361
auto const pObject = abstract_cast<ObjectClass*>(pTarget);
6462

6563
if (!pObject || !pObject->IsInAir())
6664
return 0;
6765

68-
auto const pExt = TechnoExt::ExtMap.Find(pThis);
69-
int weaponIndex = pExt->CurrentAircraftWeaponIndex;
66+
int weaponIndex = TechnoExt::ExtMap.Find(pThis)->CurrentAircraftWeaponIndex;
7067

7168
if (weaponIndex < 0)
7269
weaponIndex = pThis->SelectWeapon(pTarget);
@@ -295,7 +292,7 @@ DEFINE_HOOK(0x4CF31C, FlyLocomotionClass_FlightUpdate_LandingDir, 0x9)
295292

296293
if (pLinkedTo->CurrentMission == Mission::Enter || pLinkedTo->GetMapCoords() == CellClass::Coord2Cell(pLinkedTo->Locomotor->Destination()))
297294
{
298-
if (auto const pAircraft = abstract_cast<AircraftClass*>(pLinkedTo))
295+
if (auto const pAircraft = abstract_cast<AircraftClass*, true>(pLinkedTo))
299296
dir = DirStruct(AircraftExt::GetLandingDir(pAircraft)).Raw;
300297
}
301298

@@ -603,12 +600,13 @@ DEFINE_HOOK(0x4C72F2, EventClass_Execute_AircraftAreaGuard_Untether, 0x6)
603600

604601
static __forceinline bool CheckSpyPlaneCameraCount(AircraftClass* pThis)
605602
{
606-
auto const pExt = TechnoExt::ExtMap.Find(pThis);
607603
auto const pWeaponExt = WeaponTypeExt::ExtMap.Find(pThis->GetWeapon(0)->WeaponType);
608604

609605
if (!pWeaponExt->Strafing_Shots.isset())
610606
return true;
611607

608+
auto const pExt = TechnoExt::ExtMap.Find(pThis);
609+
612610
if (pExt->Strafe_BombsDroppedThisRound >= pWeaponExt->Strafing_Shots)
613611
return false;
614612

src/Ext/Anim/Body.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void AnimExt::VeinAttackAI(AnimClass* pAnim)
141141
{
142142
ObjectClass* pNext = pOccupier->NextObject;
143143
int damage = RulesClass::Instance->VeinDamage;
144-
TechnoClass* pTechno = abstract_cast<TechnoClass*>(pOccupier);
144+
TechnoClass* pTechno = abstract_cast<TechnoClass*, true>(pOccupier);
145145

146146
if (pTechno && !pTechno->GetTechnoType()->ImmuneToVeins && !pTechno->HasAbility(Ability::VeinProof)
147147
&& pTechno->Health > 0 && pTechno->IsAlive && pTechno->GetHeight() <= 5)

src/Ext/Building/Hooks.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,15 @@ DEFINE_HOOK(0x4403D4, BuildingClass_AI_ChronoSparkle, 0x6)
7676
DEFINE_HOOK(0x443C81, BuildingClass_ExitObject_InitialClonedHealth, 0x7)
7777
{
7878
GET(BuildingClass*, pBuilding, ESI);
79+
7980
if (auto const pInf = abstract_cast<InfantryClass*>(R->EDI<FootClass*>()))
8081
{
8182
if (pBuilding && pBuilding->Type->Cloning)
8283
{
83-
if (auto pTypeExt = BuildingTypeExt::ExtMap.Find(pBuilding->Type))
84-
{
85-
double percentage = GeneralUtils::GetRangedRandomOrSingleValue(pTypeExt->InitialStrength_Cloning);
86-
int strength = Math::clamp(static_cast<int>(pInf->Type->Strength * percentage), 1, pInf->Type->Strength);
87-
88-
pInf->Health = strength;
89-
pInf->EstimatedHealth = strength;
90-
}
84+
double percentage = GeneralUtils::GetRangedRandomOrSingleValue(BuildingTypeExt::ExtMap.Find(pBuilding->Type)->InitialStrength_Cloning);
85+
int strength = Math::clamp(static_cast<int>(pInf->Type->Strength * percentage), 1, pInf->Type->Strength);
86+
pInf->Health = strength;
87+
pInf->EstimatedHealth = strength;
9188
}
9289
}
9390

@@ -99,7 +96,8 @@ DEFINE_HOOK(0x449ADA, BuildingClass_MissionConstruction_DeployToFireFix, 0x0)
9996
GET(BuildingClass*, pThis, ESI);
10097

10198
auto pExt = BuildingExt::ExtMap.Find(pThis);
102-
if (pExt && pExt->DeployedTechno && pThis->LastTarget)
99+
100+
if (pExt->DeployedTechno && pThis->LastTarget)
103101
{
104102
pThis->Target = pThis->LastTarget;
105103
pThis->QueueMission(Mission::Attack, false);
@@ -249,10 +247,11 @@ DEFINE_HOOK(0x44FBBF, CreateBuildingFromINIFile_AfterCTOR_BeforeUnlimbo, 0x8)
249247
DEFINE_HOOK(0x440B4F, BuildingClass_Unlimbo_SetShouldRebuild, 0x5)
250248
{
251249
enum { ContinueCheck = 0x440B58, SkipSetShouldRebuild = 0x440B81 };
252-
GET(BuildingClass* const, pThis, ESI);
253250

254251
if (SessionClass::IsCampaign())
255252
{
253+
GET(BuildingClass* const, pThis, ESI);
254+
256255
// Preplaced structures are already managed before
257256
if (BuildingExt::ExtMap.Find(pThis)->IsCreatedFromMapFile)
258257
return SkipSetShouldRebuild;
@@ -277,8 +276,7 @@ DEFINE_HOOK(0x4519A2, BuildingClass_UpdateAnim_SetParentBuilding, 0x6)
277276
GET(BuildingClass*, pThis, ESI);
278277
GET(AnimClass*, pAnim, EBP);
279278

280-
auto const pAnimExt = AnimExt::ExtMap.Find(pAnim);
281-
pAnimExt->ParentBuilding = pThis;
279+
AnimExt::ExtMap.Find(pAnim)->ParentBuilding = pThis;
282280
TechnoExt::ExtMap.Find(pThis)->AnimRefCount++;
283281

284282
return 0;
@@ -288,10 +286,14 @@ DEFINE_HOOK(0x43D6E5, BuildingClass_Draw_ZShapePointMove, 0x5)
288286
{
289287
enum { Apply = 0x43D6EF, Skip = 0x43D712 };
290288

291-
GET(BuildingClass*, pThis, ESI);
292289
GET(Mission, mission, EAX);
293290

294-
if ((mission != Mission::Selling && mission != Mission::Construction) || BuildingTypeExt::ExtMap.Find(pThis->Type)->ZShapePointMove_OnBuildup)
291+
if ((mission != Mission::Selling && mission != Mission::Construction))
292+
return Apply;
293+
294+
GET(BuildingClass*, pThis, ESI);
295+
296+
if (BuildingTypeExt::ExtMap.Find(pThis->Type)->ZShapePointMove_OnBuildup)
295297
return Apply;
296298

297299
return Skip;
@@ -303,9 +305,7 @@ DEFINE_HOOK(0x4511D6, BuildingClass_AnimationAI_SellBuildup, 0x7)
303305

304306
GET(BuildingClass*, pThis, ESI);
305307

306-
auto const pTypeExt = BuildingTypeExt::ExtMap.Find(pThis->Type);
307-
308-
return pTypeExt->SellBuildupLength == pThis->Animation.Value ? Continue : Skip;
308+
return BuildingTypeExt::ExtMap.Find(pThis->Type)->SellBuildupLength == pThis->Animation.Value ? Continue : Skip;
309309
}
310310

311311
#pragma region FactoryPlant
@@ -358,12 +358,13 @@ DEFINE_HOOK(0x449149, BuildingClass_Captured_FactoryPlant2, 0x6)
358358
enum { Skip = 0x449197 };
359359

360360
GET(BuildingClass*, pThis, ESI);
361-
GET(HouseClass*, pNewOwner, EBP);
362361

363362
auto const pTypeExt = BuildingTypeExt::ExtMap.Find(pThis->Type);
364363

365364
if (pTypeExt->FactoryPlant_AllowTypes.size() > 0 || pTypeExt->FactoryPlant_DisallowTypes.size() > 0)
366365
{
366+
GET(HouseClass*, pNewOwner, EBP);
367+
367368
auto const pHouseExt = HouseExt::ExtMap.Find(pNewOwner);
368369
pHouseExt->RestrictedFactoryPlants.push_back(pThis);
369370

@@ -405,9 +406,7 @@ DEFINE_HOOK(0x440D01, BuildingClass_Unlimbo_DestroyableObstacle, 0x6)
405406
{
406407
GET(BuildingClass*, pThis, ESI);
407408

408-
auto const pTypeExt = BuildingTypeExt::ExtMap.Find(pThis->Type);
409-
410-
if (pTypeExt->IsDestroyableObstacle)
409+
if (BuildingTypeExt::ExtMap.Find(pThis->Type)->IsDestroyableObstacle)
411410
RecalculateCells(pThis);
412411

413412
return 0;
@@ -417,9 +416,7 @@ DEFINE_HOOK(0x445D87, BuildingClass_Limbo_DestroyableObstacle, 0x6)
417416
{
418417
GET(BuildingClass*, pThis, ESI);
419418

420-
auto const pTypeExt = BuildingTypeExt::ExtMap.Find(pThis->Type);
421-
422-
if (pTypeExt->IsDestroyableObstacle)
419+
if (BuildingTypeExt::ExtMap.Find(pThis->Type)->IsDestroyableObstacle)
423420
RecalculateCells<true>(pThis);
424421

425422
// only remove animation when the building is destroyed or sold
@@ -444,9 +441,7 @@ DEFINE_HOOK(0x483D8E, CellClass_CheckPassability_DestroyableObstacle, 0x6)
444441

445442
GET(BuildingClass*, pBuilding, ESI);
446443

447-
auto const pTypeExt = BuildingTypeExt::ExtMap.Find(pBuilding->Type);
448-
449-
if (pTypeExt->IsDestroyableObstacle)
444+
if (BuildingTypeExt::ExtMap.Find(pBuilding->Type)->IsDestroyableObstacle)
450445
return IsBlockage;
451446

452447
return 0;
@@ -529,12 +524,12 @@ DEFINE_HOOK(0x6F4D1A, TechnoClass_ReceiveCommand_Repair, 0x5)
529524
{
530525
enum { AnswerNegative = 0x6F4CB4 };
531526

532-
GET(TechnoClass*, pThis, ESI);
533-
GET(int, repairStep, EAX);
534527
GET_STACK(TechnoClass*, pFrom, STACK_OFFSET(0x18, 0x4));
535528

536529
if (auto const pBuilding = abstract_cast<BuildingClass*>(pFrom))
537530
{
531+
GET(TechnoClass*, pThis, ESI);
532+
GET(int, repairStep, EAX);
538533
auto const pTypeExt = BuildingTypeExt::ExtMap.Find(pBuilding->Type);
539534

540535
if (pBuilding->Type->UnitReload && pTypeExt->Units_RepairRate.isset() && !UnitRepairTemp::SeparateRepair)
@@ -632,10 +627,12 @@ DEFINE_HOOK(0x4FAAD8, HouseClass_AbandonProduction_RewriteForBuilding, 0x8)
632627
DEFINE_HOOK(0x6A9C54, StripClass_DrawStrip_FindFactoryDehardCode, 0x6)
633628
{
634629
GET(TechnoTypeClass* const, pType, ECX);
635-
LEA_STACK(BuildCat*, pBuildCat, STACK_OFFSET(0x490, -0x490));
636630

637631
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*>(pType))
632+
{
633+
LEA_STACK(BuildCat*, pBuildCat, STACK_OFFSET(0x490, -0x490));
638634
*pBuildCat = pBuildingType->BuildCat;
635+
}
639636

640637
return 0;
641638
}

src/Ext/Bullet/Hooks.DetonateLogics.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,14 @@ DEFINE_HOOK(0x469E34, BulletClass_Logics_DebrisAnims, 0x5)
163163
enum { SkipGameCode = 0x469EBA };
164164

165165
GET(BulletClass*, pThis, ESI);
166+
GET(int, debrisCount, EBX);
166167

167168
auto const pWHExt = WarheadTypeExt::ExtMap.Find(pThis->WH);
168169
auto const debrisAnims = pWHExt->DebrisAnims.GetElements(RulesClass::Instance->MetallicDebris);
169170

170171
if (debrisAnims.size() < 1)
171172
return SkipGameCode;
172173

173-
GET(int, debrisCount, EBX);
174-
175174
while (debrisCount > 0)
176175
{
177176
int debrisIndex = ScenarioClass::Instance->Random.RandomRanged(0, debrisAnims.size() - 1);
@@ -204,14 +203,14 @@ DEFINE_HOOK(0x469C46, BulletClass_Logics_DamageAnimSelected, 0x8)
204203
{
205204
enum { SkipGameCode = 0x469C98 };
206205

206+
GET(BulletClass*, pThis, ESI);
207207
GET(AnimTypeClass*, pAnimType, EBX);
208+
LEA_STACK(CoordStruct*, coords, STACK_OFFSET(0xA4, -0x40));
208209

209210
bool createdAnim = false;
210211

211212
if (pAnimType)
212213
{
213-
GET(BulletClass*, pThis, ESI);
214-
LEA_STACK(CoordStruct*, coords, STACK_OFFSET(0xA4, -0x40));
215214
auto const pWHExt = WarheadTypeExt::ExtMap.Find(pThis->WH);
216215
const bool splashed = pWHExt->Splashed;
217216
int creationInterval = splashed ? pWHExt->SplashList_CreationInterval : pWHExt->AnimList_CreationInterval;
@@ -296,13 +295,13 @@ DEFINE_HOOK(0x469C46, BulletClass_Logics_DamageAnimSelected, 0x8)
296295
DEFINE_HOOK(0x469AA4, BulletClass_Logics_Extras, 0x5)
297296
{
298297
GET(BulletClass*, pThis, ESI);
298+
GET_BASE(CoordStruct*, coords, 0x8);
299299

300300
auto const pOwner = pThis->Owner ? pThis->Owner->Owner : BulletExt::ExtMap.Find(pThis)->FirerHouse;
301301

302302
// Extra warheads
303303
if (pThis->WeaponType)
304304
{
305-
GET_BASE(CoordStruct*, coords, 0x8);
306305
auto const pWeaponExt = WeaponTypeExt::ExtMap.Find(pThis->WeaponType);
307306
int defaultDamage = pThis->WeaponType->Damage;
308307

src/Ext/Bullet/Hooks.Obstacles.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ DEFINE_HOOK(0x6F737F, TechnoClass_InRange_WeaponMinimumRange, 0x6)
213213

214214
DEFINE_HOOK(0x6F7647, TechnoClass_InRange_Obstacles, 0x5)
215215
{
216-
GET(CellClass*, pResult, EAX);
216+
GET_BASE(WeaponTypeClass*, pWeapon, 0x10);
217217
GET(CoordStruct const* const, pSourceCoords, ESI);
218+
REF_STACK(CoordStruct const, targetCoords, STACK_OFFSET(0x3C, -0x1C));
219+
GET_BASE(AbstractClass* const, pTarget, 0xC);
220+
GET(CellClass*, pResult, EAX);
218221

219222
auto pObstacleCell = pResult;
220223
auto pTechno = InRangeTemp::Techno;
221224

222225
if (!pObstacleCell)
223226
{
224-
GET_BASE(WeaponTypeClass*, pWeapon, 0x10);
225-
REF_STACK(CoordStruct const, targetCoords, STACK_OFFSET(0x3C, -0x1C));
226-
GET_BASE(AbstractClass* const, pTarget, 0xC);
227227
auto subjectToGround = BulletTypeExt::ExtMap.Find(pWeapon->Projectile)->SubjectToGround.Get();
228228
const auto newSourceCoords = subjectToGround ? BulletObstacleHelper::AddFLHToSourceCoords(*pSourceCoords, targetCoords, pTechno, pTarget, pWeapon, subjectToGround) : *pSourceCoords;
229229
pObstacleCell = BulletObstacleHelper::FindFirstImpenetrableObstacle(newSourceCoords, targetCoords, pTechno, pTarget, pTechno->Owner, pWeapon, true, subjectToGround);

src/Ext/Bullet/Hooks.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,13 @@ DEFINE_HOOK(0x468E61, BulletClass_Explode_TargetSnapChecks1, 0x6)
345345
{
346346
return 0;
347347
}
348-
else if (auto const pExt = BulletExt::ExtMap.Find(pThis))
348+
349+
auto const pExt = BulletExt::ExtMap.Find(pThis);
350+
351+
if (pExt->Trajectory && CheckTrajectoryCanNotAlwaysSnap(pExt->Trajectory->Flag()) && !pExt->SnappedToTarget)
349352
{
350-
if (pExt->Trajectory && CheckTrajectoryCanNotAlwaysSnap(pExt->Trajectory->Flag()) && !pExt->SnappedToTarget)
351-
{
352-
R->EAX(pThis->Type);
353-
return SkipChecks;
354-
}
353+
R->EAX(pThis->Type);
354+
return SkipChecks;
355355
}
356356

357357
return 0;
@@ -376,11 +376,10 @@ DEFINE_HOOK(0x468E9F, BulletClass_Explode_TargetSnapChecks2, 0x6)
376376

377377
// Do not force Trajectory=Straight projectiles to detonate at target coordinates under certain circumstances.
378378
// Fixes issues with walls etc.
379-
if (auto const pExt = BulletExt::ExtMap.Find(pThis))
380-
{
381-
if (pExt->Trajectory && CheckTrajectoryCanNotAlwaysSnap(pExt->Trajectory->Flag()) && !pExt->SnappedToTarget)
382-
return SkipSetCoordinate;
383-
}
379+
auto const pExt = BulletExt::ExtMap.Find(pThis);
380+
381+
if (pExt->Trajectory && CheckTrajectoryCanNotAlwaysSnap(pExt->Trajectory->Flag()) && !pExt->SnappedToTarget)
382+
return SkipSetCoordinate;
384383

385384
return 0;
386385
}
@@ -391,22 +390,21 @@ DEFINE_HOOK(0x468D3F, BulletClass_ShouldExplode_AirTarget, 0x6)
391390

392391
GET(BulletClass*, pThis, ESI);
393392

394-
if (auto const pExt = BulletExt::ExtMap.Find(pThis))
395-
{
396-
if (pExt->Trajectory && CheckTrajectoryCanNotAlwaysSnap(pExt->Trajectory->Flag()))
397-
return SkipCheck;
398-
}
393+
auto const pExt = BulletExt::ExtMap.Find(pThis);
394+
395+
if (pExt->Trajectory && CheckTrajectoryCanNotAlwaysSnap(pExt->Trajectory->Flag()))
396+
return SkipCheck;
399397

400398
return 0;
401399
}
402400

403401
DEFINE_HOOK(0x4687F8, BulletClass_Unlimbo_FlakScatter, 0x6)
404402
{
405403
GET(BulletClass*, pThis, EBX);
404+
GET_STACK(float, mult, STACK_OFFSET(0x5C, -0x44));
406405

407406
if (pThis->WeaponType)
408407
{
409-
GET_STACK(float, mult, STACK_OFFSET(0x5C, -0x44));
410408
auto const pTypeExt = BulletTypeExt::ExtMap.Find(pThis->Type);
411409
int defaultValue = RulesClass::Instance->BallisticScatter;
412410
int min = pTypeExt->BallisticScatter_Min.Get(Leptons(0));

src/Ext/Bullet/Trajectories/StraightTrajectory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ bool StraightTrajectory::CheckThroughAndSubjectInCell(BulletClass* pBullet, Cell
662662

663663
while (pObject)
664664
{
665-
const auto pTechno = abstract_cast<TechnoClass*>(pObject);
665+
const auto pTechno = abstract_cast<TechnoClass*, true>(pObject);
666666
pObject = pObject->NextObject;
667667

668668
// Non technos and not target friendly forces will be excluded
@@ -798,7 +798,7 @@ void StraightTrajectory::PrepareForDetonateAt(BulletClass* pBullet, HouseClass*
798798

799799
while (pObject)
800800
{
801-
const auto pTechno = abstract_cast<TechnoClass*>(pObject);
801+
const auto pTechno = abstract_cast<TechnoClass*, true>(pObject);
802802
pObject = pObject->NextObject;
803803

804804
if (!pTechno || !pTechno->IsAlive || !pTechno->IsOnMap || pTechno->Health <= 0 || pTechno->InLimbo || pTechno->IsSinking)

src/Ext/CaptureManager/Body.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool CaptureManagerExt::CaptureUnit(CaptureManagerClass* pManager, TechnoClass*
100100

101101
pManager->DecideUnitFate(pTarget);
102102

103-
auto const pBld = abstract_cast<BuildingClass*>(pTarget);
103+
auto const pBld = abstract_cast<BuildingClass*, true>(pTarget);
104104
auto const pType = pTarget->GetTechnoType();
105105
CoordStruct location = pTarget->GetCoords();
106106

0 commit comments

Comments
 (0)