Skip to content

[Minor] Optimize kick out stuck units #1806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/Ext/Building/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
{
if (const auto pUnit = abstract_cast<UnitClass*>(pThis->GetNthLink()))
{
if (!pUnit->IsTether && pUnit->GetCurrentSpeed() <= 0)
if (pUnit->Locomotor->Destination() == CoordStruct::Empty)
{
if (const auto pTeam = pUnit->Team)
pTeam->LiberateMember(pUnit);
Expand All @@ -369,19 +369,22 @@
}

auto buffer = CoordStruct::Empty;
auto pCell = MapClass::Instance.GetCellAt(*pThis->GetExitCoords(&buffer, 0));
const auto pOwner = pThis->Owner;
int i = 0;
pThis->GetExitCoords(&buffer, 0);

auto cell = CellClass::Coord2Cell(buffer);

const auto pType = pThis->Type;
const short start = static_cast<short>(pThis->Location.X / Unsorted::LeptonsPerCell + pType->GetFoundationWidth() - 2); // door

Check warning on line 377 in src/Ext/Building/Body.cpp

View workflow job for this annotation

GitHub Actions / build

'start': local variable is initialized but not referenced [D:\a\Phobos\Phobos\Phobos.vcxproj]
const short end = cell.X; // exit
auto pCell = MapClass::Instance.GetCellAt(cell);

while (true)
{
for (auto pObject = pCell->FirstObject; pObject; pObject = pObject->NextObject)
{
if (pObject->WhatAmI() == AbstractType::Unit)
if (const auto pUnit = abstract_cast<UnitClass*, true>(pObject))
{
const auto pUnit = static_cast<UnitClass*>(pObject);

if (pOwner != pUnit->Owner || pUnit->IsTether)
if (pThis->Owner != pUnit->Owner || pUnit->Locomotor->Destination() != CoordStruct::Empty)
continue;

const auto height = pUnit->GetHeight();
Expand All @@ -398,11 +401,10 @@
}
}

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

// Continue checking towards the bottom right corner
pCell = pCell->GetNeighbourCell(FacingType::East);
pCell = MapClass::Instance.GetCellAt(cell);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Ext/Building/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ DEFINE_HOOK(0x450248, BuildingClass_UpdateFactory_KickOutStuckUnits, 0x6)
{
const auto mission = pThis->CurrentMission;

if (mission == Mission::Guard && !pThis->InLimbo || mission == Mission::Unload && pThis->MissionStatus == 1) // Unloading but stuck
if (mission == Mission::Guard && !pThis->InLimbo || mission == Mission::Unload && pThis->MissionStatus == 3) // Unloading but stuck
BuildingExt::KickOutStuckUnits(pThis);
}
}
Expand Down
27 changes: 26 additions & 1 deletion src/Ext/BuildingType/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ DEFINE_HOOK(0x6D528A, TacticalClass_DrawPlacement_PlacementPreview, 0x6)

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

if (isShow)
Expand Down Expand Up @@ -404,3 +404,28 @@ DEFINE_HOOK(0x44E85F, BuildingClass_Power_DamageFactor, 0x7)

return Handled;
}

#pragma region WeaponFactoryPath

DEFINE_HOOK(0x73F5A7, UnitClass_IsCellOccupied_UnlimboDirection, 0x8)
{
enum { NextObject = 0x73FA87, ContinueCheck = 0x73F5AF };

GET(const bool, notPassable, EAX);

if (notPassable)
return ContinueCheck;

GET(BuildingClass* const, pBuilding, ESI);

const auto pType = pBuilding->Type;

if (!pType->WeaponsFactory)
return NextObject;

GET(CellClass* const, pCell, EDI);

return pCell->MapCoords.Y == pType->FoundationOutside[10].Y ? NextObject : ContinueCheck;
}

#pragma endregion
Loading