1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------
#include "AAIAttackManager.h"
#include "AAI.h"
#include "AAIBrain.h"
#include "AAIAttack.h"
#include "AAIConfig.h"
#include "AAIGroup.h"
#include "AAIMap.h"
#include "AAISector.h"
AAIAttackManager::AAIAttackManager(AAI *ai) :
ai(ai),
m_activeAttacks(AAIConstants::maxNumberOfAttacks, nullptr)
{
}
AAIAttackManager::~AAIAttackManager(void)
{
for(auto attack = m_activeAttacks.begin(); attack != m_activeAttacks.end(); ++attack)
{
if(*attack)
delete (*attack);
}
m_activeAttacks.clear();
}
void AAIAttackManager::Update(AAIThreatMap& threatMap)
{
int availableAttackId(-1);
for(int attackId = 0; attackId < m_activeAttacks.size(); ++attackId)
{
AAIAttack* attack = m_activeAttacks[attackId];
if(attack)
{
// drop failed attacks
if( AbortAttackIfFailed(attack) )
availableAttackId = attackId;
// check if sector cleared
else if( attack->HasTargetBeenCleared() )
AttackNextSectorOrAbort(attack);
}
else
availableAttackId = attackId;
}
// at least one attack id is available -> check if new attack should be launched
if(availableAttackId >= 0)
TryToLaunchAttack(availableAttackId, threatMap);
}
void AAIAttackManager::TryToLaunchAttack(int availableAttackId, AAIThreatMap& threatMap)
{
//////////////////////////////////////////////////////////////////////////////////////////////
// get all available combat/aa/arty groups for attack
//////////////////////////////////////////////////////////////////////////////////////////////
const int numberOfContinents( AAIMap::GetNumberOfContinents() );
std::vector< std::list<AAIGroup*> > availableAssaultGroupsOnContinent(numberOfContinents);
std::vector< std::list<AAIGroup*> > availableAAGroupsOnContinent(numberOfContinents);
std::list<AAIGroup*> availableAssaultGroupsGlobal;
std::list<AAIGroup*> availableAAGroupsGlobal;
const int numberOfAssaultUnitGroups = DetermineCombatUnitGroupsAvailableForattack(availableAssaultGroupsGlobal, availableAAGroupsGlobal,
availableAssaultGroupsOnContinent, availableAAGroupsOnContinent);
// stop planning an attack if there are no combat groups available at the moment
if(numberOfAssaultUnitGroups == 0)
return;
//////////////////////////////////////////////////////////////////////////////////////////////
// calculate max attack power vs the different target types for each continent
//////////////////////////////////////////////////////////////////////////////////////////////
MobileTargetTypeValues numberOfAssaultGroupsOfTargetType;
for(const auto group : availableAssaultGroupsGlobal)
{
numberOfAssaultGroupsOfTargetType.AddValueForTargetType(group->GetTargetType(), 1.0f);
}
for(size_t continent = 0; continent < availableAssaultGroupsOnContinent.size(); ++continent)
{
for(const auto group : availableAssaultGroupsOnContinent[continent])
{
numberOfAssaultGroupsOfTargetType.AddValueForTargetType(group->GetTargetType(), 1.0f);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// determine target types of attackers
//////////////////////////////////////////////////////////////////////////////////////////////
std::list<AAITargetType> attackerTargetTypes;
for(auto targetType : AAITargetType::m_mobileTargetTypes)
{
if(numberOfAssaultGroupsOfTargetType.GetValueOfTargetType(targetType) > 0)
attackerTargetTypes.push_back(targetType);
}
//////////////////////////////////////////////////////////////////////////////////////////////
// for every possible attacker target type, determine whether suitable target is available and
// order attack
//////////////////////////////////////////////////////////////////////////////////////////////
for(auto targetType : attackerTargetTypes)
{
threatMap.UpdateLocalEnemyCombatPower(targetType, ai->Map()->GetSectorMap());
const MapPos baseCenter = ai->Brain()->GetCenterOfBase();
const AAISector* targetSector = threatMap.DetermineSectorToAttack(targetType, baseCenter, ai->Map()->GetSectorMap());
// order groups of given target type to attack
if(targetSector)
{
const float3 targetPosition = targetSector->DetermineAttackPosition();
const int continentId = AAIMap::GetContinentID(targetPosition);
AAIAttack *attack = new AAIAttack(ai, targetSector);
// add combat unit groups
attack->AddGroupsOfTargetType(availableAssaultGroupsOnContinent[continentId], targetType);
attack->AddGroupsOfTargetType(availableAssaultGroupsGlobal, targetType);
// add anti air units if necessary
if( (ai->Brain()->m_maxSpottedCombatUnitsOfTargetType.GetValueOfTargetType(ETargetType::AIR) > 0.2f)
|| (ai->Brain()->GetRecentAttacksBy(ETargetType::AIR) > 0.9f) )
{
std::list<AAIGroup*> antiAirGroups;
SelectNumberOfGroups(antiAirGroups, 1, availableAAGroupsOnContinent[continentId], availableAAGroupsGlobal);
attack->AddGroupsOfTargetType(antiAirGroups, targetType);
}
if( attack->CheckIfFailed() )
{
// insufficient combat power of attacking units -> abort attack
delete attack;
}
else
{
// start the attack
m_activeAttacks[availableAttackId] = attack;
attack->AttackPosition(targetPosition);
}
}
}
}
void AAIAttackManager::SelectNumberOfGroups(std::list<AAIGroup*> selectedGroupList, int maxNumberOfGroups, std::list<AAIGroup*> groupList1, std::list<AAIGroup*> groupList2)
{
int numberOfSelectedGroups(0);
for(auto group = groupList1.begin(); group != groupList1.end(); ++group)
{
if(numberOfSelectedGroups >= maxNumberOfGroups)
break;
selectedGroupList.push_back(*group);
++numberOfSelectedGroups;
}
for(auto group = groupList2.begin(); group != groupList2.end(); ++group)
{
if(numberOfSelectedGroups >= maxNumberOfGroups)
break;
selectedGroupList.push_back(*group);
++numberOfSelectedGroups;
}
}
int AAIAttackManager::DetermineCombatUnitGroupsAvailableForattack( std::list<AAIGroup*>& availableAssaultGroupsGlobal,
std::list<AAIGroup*>& availableAAGroupsGlobal,
std::vector< std::list<AAIGroup*> >& availableAssaultGroupsOnContinent,
std::vector< std::list<AAIGroup*> >& availableAAGroupsOnContinent) const
{
const std::vector<AAIUnitCategory> combatUnitCategories = { AAIUnitCategory(EUnitCategory::GROUND_COMBAT),
AAIUnitCategory(EUnitCategory::HOVER_COMBAT),
AAIUnitCategory(EUnitCategory::SEA_COMBAT),
AAIUnitCategory(EUnitCategory::SUBMARINE_COMBAT) };
int numberOfAssaultUnitGroups(0);
for(auto category = combatUnitCategories.begin(); category != combatUnitCategories.end(); ++category)
{
for(auto group = ai->GetUnitGroupsList(*category).begin(); group != ai->GetUnitGroupsList(*category).end(); ++group)
{
if( (*group)->IsAvailableForAttack() )
{
const AAIUnitType& unitType = (*group)->GetUnitTypeOfGroup();
if(unitType.IsAssaultUnit())
{
if( (*group)->GetMovementType().CannotMoveToOtherContinents() )
availableAssaultGroupsOnContinent[(*group)->GetContinentId()].push_back(*group);
else
availableAssaultGroupsGlobal.push_back(*group);
++numberOfAssaultUnitGroups;
}
else if(unitType.IsAntiAir())
{
if( (*group)->GetMovementType().CannotMoveToOtherContinents() )
availableAAGroupsOnContinent[(*group)->GetContinentId()].push_back(*group);
else
availableAAGroupsGlobal.push_back(*group);
}
}
}
}
return numberOfAssaultUnitGroups;
}
void AAIAttackManager::AbortAttack(AAIAttack* attack)
{
attack->StopAttack();
for(auto a = m_activeAttacks.begin(); a != m_activeAttacks.end(); ++a)
{
if(*a == attack)
{
*a = nullptr;
break;
}
}
delete attack;
}
bool AAIAttackManager::AbortAttackIfFailed(AAIAttack *attack)
{
if((ai->GetAICallback()->GetCurrentFrame() - attack->m_lastAttackOrderInFrame) < 30) // prevent command overflow
return false;
else if(attack->CheckIfFailed())
{
AbortAttack(attack);
return true;
}
else
return false;
}
void AAIAttackManager::AttackNextSectorOrAbort(AAIAttack* attack)
{
// prevent command overflow
if((ai->GetAICallback()->GetCurrentFrame() - attack->m_lastAttackOrderInFrame) < 60)
return;
const AAISector* sector = attack->DetermineSectorToContinueAttack();
if(sector)
{
const float3 position = sector->DetermineAttackPosition();
attack->AttackPosition(position);
}
else
{
AbortAttack(attack);
}
}
|