Skip to content

Commit 73c2208

Browse files
authored
[Minor] Optimize kick out stuck units (#1806)
- Fix the hard coded position calculation - Changed the determination of unit movement
1 parent 937822b commit 73c2208

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/Ext/Building/Body.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ void BuildingExt::KickOutStuckUnits(BuildingClass* pThis)
357357
{
358358
if (const auto pUnit = abstract_cast<UnitClass*>(pThis->GetNthLink()))
359359
{
360-
if (!pUnit->IsTether && pUnit->GetCurrentSpeed() <= 0)
360+
if (pUnit->Locomotor->Destination() == CoordStruct::Empty)
361361
{
362362
if (const auto pTeam = pUnit->Team)
363363
pTeam->LiberateMember(pUnit);
@@ -369,19 +369,22 @@ void BuildingExt::KickOutStuckUnits(BuildingClass* pThis)
369369
}
370370

371371
auto buffer = CoordStruct::Empty;
372-
auto pCell = MapClass::Instance.GetCellAt(*pThis->GetExitCoords(&buffer, 0));
373-
const auto pOwner = pThis->Owner;
374-
int i = 0;
372+
pThis->GetExitCoords(&buffer, 0);
373+
374+
auto cell = CellClass::Coord2Cell(buffer);
375+
376+
const auto pType = pThis->Type;
377+
const short start = static_cast<short>(pThis->Location.X / Unsorted::LeptonsPerCell + pType->GetFoundationWidth() - 2); // door
378+
const short end = cell.X; // exit
379+
auto pCell = MapClass::Instance.GetCellAt(cell);
375380

376381
while (true)
377382
{
378383
for (auto pObject = pCell->FirstObject; pObject; pObject = pObject->NextObject)
379384
{
380-
if (pObject->WhatAmI() == AbstractType::Unit)
385+
if (const auto pUnit = abstract_cast<UnitClass*, true>(pObject))
381386
{
382-
const auto pUnit = static_cast<UnitClass*>(pObject);
383-
384-
if (pOwner != pUnit->Owner || pUnit->IsTether)
387+
if (pThis->Owner != pUnit->Owner || pUnit->Locomotor->Destination() != CoordStruct::Empty)
385388
continue;
386389

387390
const auto height = pUnit->GetHeight();
@@ -398,11 +401,10 @@ void BuildingExt::KickOutStuckUnits(BuildingClass* pThis)
398401
}
399402
}
400403

401-
if (++i >= 2)
404+
if (--cell.X < end)
402405
return; // no stuck
403406

404-
// Continue checking towards the bottom right corner
405-
pCell = pCell->GetNeighbourCell(FacingType::East);
407+
pCell = MapClass::Instance.GetCellAt(cell);
406408
}
407409
}
408410

src/Ext/Building/Hooks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ DEFINE_HOOK(0x450248, BuildingClass_UpdateFactory_KickOutStuckUnits, 0x6)
220220
{
221221
const auto mission = pThis->CurrentMission;
222222

223-
if (mission == Mission::Guard && !pThis->InLimbo || mission == Mission::Unload && pThis->MissionStatus == 1) // Unloading but stuck
223+
if (mission == Mission::Guard && !pThis->InLimbo || mission == Mission::Unload && pThis->MissionStatus == 3) // Unloading but stuck
224224
BuildingExt::KickOutStuckUnits(pThis);
225225
}
226226
}

src/Ext/BuildingType/Hooks.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ DEFINE_HOOK(0x6D528A, TacticalClass_DrawPlacement_PlacementPreview, 0x6)
7575

7676
const auto pBuilding = specific_cast<BuildingClass*>(DisplayClass::Instance.CurrentBuilding);
7777
const auto pType = pBuilding ? pBuilding->Type : nullptr;
78-
const auto pTypeExt = BuildingTypeExt::ExtMap.Find(pType);
78+
const auto pTypeExt = BuildingTypeExt::ExtMap.TryFind(pType);
7979
const bool isShow = pTypeExt && pTypeExt->PlacementPreview;
8080

8181
if (isShow)
@@ -404,3 +404,28 @@ DEFINE_HOOK(0x44E85F, BuildingClass_Power_DamageFactor, 0x7)
404404

405405
return Handled;
406406
}
407+
408+
#pragma region WeaponFactoryPath
409+
410+
DEFINE_HOOK(0x73F5A7, UnitClass_IsCellOccupied_UnlimboDirection, 0x8)
411+
{
412+
enum { NextObject = 0x73FA87, ContinueCheck = 0x73F5AF };
413+
414+
GET(const bool, notPassable, EAX);
415+
416+
if (notPassable)
417+
return ContinueCheck;
418+
419+
GET(BuildingClass* const, pBuilding, ESI);
420+
421+
const auto pType = pBuilding->Type;
422+
423+
if (!pType->WeaponsFactory)
424+
return NextObject;
425+
426+
GET(CellClass* const, pCell, EDI);
427+
428+
return pCell->MapCoords.Y == pType->FoundationOutside[10].Y ? NextObject : ContinueCheck;
429+
}
430+
431+
#pragma endregion

0 commit comments

Comments
 (0)