Skip to content
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
60 changes: 53 additions & 7 deletions src/Ext/Script/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -2911,6 +2941,7 @@ FootClass* ScriptExt::FindTheTeamLeader(TeamClass* pTeam)
{
FootClass* pLeaderUnit = nullptr;
int bestUnitLeadershipValue = -1;
bool teamLeaderFound = false;

if (!pTeam)
{
Expand All @@ -2920,19 +2951,30 @@ 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)
{
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.
Expand All @@ -2944,6 +2986,10 @@ FootClass* ScriptExt::FindTheTeamLeader(TeamClass* pTeam)
}
}
}
else
{
pUnit->IsTeamLeader = false;
}
}

if (pLeaderUnit)
Expand Down
1 change: 1 addition & 0 deletions src/Ext/Team/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void TeamExt::ExtData::Serialize(T& Stm)
.Process(this->MoveMissionEndMode)
.Process(this->WaitNoTargetCounter)
.Process(this->WaitNoTargetTimer)
.Process(this->TeamLeader)
;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Ext/Team/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TeamExt
int MoveMissionEndMode;
int WaitNoTargetCounter;
TimerStruct WaitNoTargetTimer;
FootClass* TeamLeader;

ExtData(TeamClass* OwnerObject) : Extension<TeamClass>(OwnerObject)
, WaitNoTargetAttempts { 0 }
Expand All @@ -33,6 +34,7 @@ class TeamExt
, MoveMissionEndMode { 0 }
, WaitNoTargetCounter { 0 }
, WaitNoTargetTimer { 0 }
, TeamLeader { nullptr }
{ }

virtual ~ExtData() = default;
Expand Down