diff --git a/CREDITS.md b/CREDITS.md index b04dd710cc..35aebe7fc2 100644 --- a/CREDITS.md +++ b/CREDITS.md @@ -635,6 +635,7 @@ This page lists all the individual contributions to the project by their author. - Fix an issue that the AI would enter a combat state when its building receiving damage from friendly units or damage not greater than 0 - Fix an issue that the techno with weapon with `AA=yes` and `AG=no` would not auto targeting units that are falling, such as paratroopers - Dehardcode the `ZAdjust` of warhead anim + - Fix an issue where some effects pointing to a unit were not properly cleared when the unit changed its owner - **solar-III (凤九歌)** - Target scanning delay customization (documentation) - Skip target scanning function calling for unarmed technos (documentation) diff --git a/docs/Fixed-or-Improved-Logics.md b/docs/Fixed-or-Improved-Logics.md index 5a61fb6e08..1b852578f4 100644 --- a/docs/Fixed-or-Improved-Logics.md +++ b/docs/Fixed-or-Improved-Logics.md @@ -271,6 +271,7 @@ This page describes all ingame logics that are fixed or improved in Phobos witho - Reactivate unused trigger events 2, 53, and 54. - Fixed the bug that vehicle fall on infantry will make all cell content has been removed. - Fixed `MovementZone=Subterannean` harvesters being unable to find docks if in area enclosed by water, cliffs etc. +- Fixed an issue where some effects pointing to a unit were not properly cleared when the unit changed its owner. ## Fixes / interactions with other extensions diff --git a/docs/Whats-New.md b/docs/Whats-New.md index bb745beab6..a504ab9a97 100644 --- a/docs/Whats-New.md +++ b/docs/Whats-New.md @@ -458,6 +458,7 @@ New: - Fast access structure (by FlyStar) - Toggle off laser trail and shake effects (by Ollerus) - [Dehardcode the `ZAdjust` of warhead anim](Fixed-or-Improved-Logics.md#dehardcode-the-zadjust-of-warhead-anim) (by TaranDahl) +- Fixed an issue where some effects pointing to a unit were not properly cleared when the unit changed its owner (by TaranDahl) Vanilla fixes: - Fixed sidebar not updating queued unit numbers when adding or removing units when the production is on hold (by CrimRecya) diff --git a/src/Misc/Hooks.BugFixes.cpp b/src/Misc/Hooks.BugFixes.cpp index 8b4bf8e4fe..c0cfe5a51c 100644 --- a/src/Misc/Hooks.BugFixes.cpp +++ b/src/Misc/Hooks.BugFixes.cpp @@ -2818,3 +2818,52 @@ DEFINE_HOOK(0x54CC9C, JumpjetLocomotionClass_ProcessCrashing_DropFix, 0x5) return fallOnSomething ? SkipGameCode2 : SkipGameCode; } + +#pragma region ClearTargetOnOwnerChanged + +DEFINE_HOOK(0x70D4A0, AbstractClass_ClearTargetToMe_ClearManagerTarget, 0x5) +{ + GET(AbstractClass*, pThis, ECX); + + for (const auto pTemporal : TemporalClass::Array) + { + if (pTemporal->Target == pThis) + pTemporal->LetGo(); + } + + // WW don't clear target if the techno has airstrike manager. + // No idea why, but for now we respect it and don't handle the airstrike target. + //for (const auto pAirstrike : AirstrikeClass::Array) + //{ + // if (pAirstrike->Target == pThis) + // pAirstrike->ClearTarget(); + //} + + for (const auto pSpawn : SpawnManagerClass::Array) + { + if (pSpawn->Target == pThis) + pSpawn->ResetTarget(); + } + + if (const auto pTechno = abstract_cast(pThis)) + pTechno->LastTarget = nullptr; + + if (const auto pFoot = abstract_cast(pThis)) + pFoot->LastDestination = nullptr; + + return 0; +} + +DEFINE_HOOK(0x70D4FD, AbstractClass_ClearTargetToMe_ClearLastTarget, 0x6) +{ + GET(TechnoClass*, pTechno, ESI); + GET(const bool, shouldClear, ECX); + GET(AbstractClass*, pThis, EBP); + + if (pTechno->LastTarget == pThis && shouldClear) + pTechno->LastTarget = nullptr; + + return 0; +} + +#pragma endregion