diff --git a/src/Ext/Script/Body.cpp b/src/Ext/Script/Body.cpp index 8c9a85082a..9d52c081dc 100644 --- a/src/Ext/Script/Body.cpp +++ b/src/Ext/Script/Body.cpp @@ -379,7 +379,17 @@ void ScriptExt::Mission_Gather_NearTheLeader(TeamClass *pTeam, int countdown = - double closeEnough; // Find the Leader - pLeaderUnit = FindTheTeamLeader(pTeam); + pLeaderUnit = pExt->TeamLeader; + if (!pLeaderUnit + || !pLeaderUnit->IsAlive + || pLeaderUnit->Health <= 0 + || pLeaderUnit->InLimbo + || !pLeaderUnit->IsOnMap + || pLeaderUnit->Absorbed) + { + pLeaderUnit = FindTheTeamLeader(pTeam); + pExt->TeamLeader = pLeaderUnit; + } if (!pLeaderUnit) { @@ -643,7 +653,17 @@ void ScriptExt::Mission_Attack(TeamClass *pTeam, bool repeatAction = true, int c } // Find the Leader - pLeaderUnit = FindTheTeamLeader(pTeam); + pLeaderUnit = pTeamData->TeamLeader; + if (!pLeaderUnit + || !pLeaderUnit->IsAlive + || pLeaderUnit->Health <= 0 + || pLeaderUnit->InLimbo + || !pLeaderUnit->IsOnMap + || pLeaderUnit->Absorbed) + { + pLeaderUnit = FindTheTeamLeader(pTeam); + pTeamData->TeamLeader = pLeaderUnit; + } if (!pLeaderUnit || bAircraftsWithoutAmmo || (pacifistTeam && !agentMode)) { @@ -2052,7 +2072,17 @@ void ScriptExt::Mission_Move(TeamClass *pTeam, int calcThreatMode = 0, bool pick } // Find the Leader - pLeaderUnit = FindTheTeamLeader(pTeam); + pLeaderUnit = pTeamData->TeamLeader; + if (!pLeaderUnit + || !pLeaderUnit->IsAlive + || pLeaderUnit->Health <= 0 + || pLeaderUnit->InLimbo + || !pLeaderUnit->IsOnMap + || pLeaderUnit->Absorbed) + { + pLeaderUnit = FindTheTeamLeader(pTeam); + pTeamData->TeamLeader = pLeaderUnit; + } if (!pLeaderUnit || bAircraftsWithoutAmmo) { @@ -2705,7 +2735,7 @@ void ScriptExt::SkipNextAction(TeamClass* pTeam, int successPercentage = 0) if (percentage <= successPercentage) { - Debug::Log("DEBUG: ScripType: [%s] [%s] (line: %d = %d,%d) Next script line skipped successfuly. Next line will be: %d = %d,%d\n", + Debug::Log("DEBUG: [%s] [%s] (line: %d = %d,%d) Next script line skipped successfuly. Next line will be: %d = %d,%d\n", pTeam->Type->ID, pTeam->CurrentScript->Type->ID, pTeam->CurrentScript->CurrentMission, pTeam->CurrentScript->Type->ScriptActions[pTeam->CurrentScript->CurrentMission].Action, pTeam->CurrentScript->Type->ScriptActions[pTeam->CurrentScript->CurrentMission].Argument, pTeam->CurrentScript->CurrentMission + 2, pTeam->CurrentScript->Type->ScriptActions[pTeam->CurrentScript->CurrentMission + 2].Action, pTeam->CurrentScript->Type->ScriptActions[pTeam->CurrentScript->CurrentMission + 2].Argument); pTeam->CurrentScript->CurrentMission++; @@ -2911,6 +2941,7 @@ FootClass* ScriptExt::FindTheTeamLeader(TeamClass* pTeam) { FootClass* pLeaderUnit = nullptr; int bestUnitLeadershipValue = -1; + bool teamLeaderFound = false; if (!pTeam) { @@ -2920,7 +2951,18 @@ FootClass* ScriptExt::FindTheTeamLeader(TeamClass* pTeam) // Find the Leader or promote a new one for (auto pUnit = pTeam->FirstUnit; pUnit; pUnit = pUnit->NextTeamMember) { - if (pUnit && pUnit->IsAlive + if (!pUnit) + continue; + + // Preventing >1 leaders in teams + if (teamLeaderFound) + { + pUnit->IsTeamLeader = false; + continue; + } + + if (pUnit->IsAlive + && pUnit->Health > 0 && !pUnit->InLimbo && pUnit->IsOnMap && !pUnit->Absorbed) @@ -2928,11 +2970,11 @@ FootClass* ScriptExt::FindTheTeamLeader(TeamClass* pTeam) if (pUnit->IsTeamLeader) { pLeaderUnit = pUnit; - break; + teamLeaderFound = true; + continue; } auto pUnitType = pUnit->GetTechnoType(); - if (pUnitType) { // The team Leader will be used for selecting targets, if there are living Team Members then always exists 1 Leader. @@ -2944,6 +2986,10 @@ FootClass* ScriptExt::FindTheTeamLeader(TeamClass* pTeam) } } } + else + { + pUnit->IsTeamLeader = false; + } } if (pLeaderUnit) diff --git a/src/Ext/Team/Body.cpp b/src/Ext/Team/Body.cpp index 07e8009030..54f359782c 100644 --- a/src/Ext/Team/Body.cpp +++ b/src/Ext/Team/Body.cpp @@ -18,6 +18,7 @@ void TeamExt::ExtData::Serialize(T& Stm) .Process(this->MoveMissionEndMode) .Process(this->WaitNoTargetCounter) .Process(this->WaitNoTargetTimer) + .Process(this->TeamLeader) ; } diff --git a/src/Ext/Team/Body.h b/src/Ext/Team/Body.h index 783a2b5110..a3ad86116f 100644 --- a/src/Ext/Team/Body.h +++ b/src/Ext/Team/Body.h @@ -23,6 +23,7 @@ class TeamExt int MoveMissionEndMode; int WaitNoTargetCounter; TimerStruct WaitNoTargetTimer; + FootClass* TeamLeader; ExtData(TeamClass* OwnerObject) : Extension(OwnerObject) , WaitNoTargetAttempts { 0 } @@ -33,6 +34,7 @@ class TeamExt , MoveMissionEndMode { 0 } , WaitNoTargetCounter { 0 } , WaitNoTargetTimer { 0 } + , TeamLeader { nullptr } { } virtual ~ExtData() = default;