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 275
|
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------
#include "AAIAttack.h"
#include "AAI.h"
#include "AAIAttackManager.h"
#include "AAIMap.h"
#include "AAIGroup.h"
#include "LegacyCpp/IAICallback.h"
using namespace springLegacyAI;
AAIAttack::AAIAttack(AAI *ai, const AAISector* attackDestination):
m_lastAttackOrderInFrame(0),
m_attackDestination(attackDestination)
{
this->ai = ai;
}
AAIAttack::~AAIAttack(void)
{
for(auto group : m_combatUnitGroups)
group->SetAttack(nullptr);
for(auto group : m_antiAirUnitGroups)
group->SetAttack(nullptr);
}
bool AAIAttack::CheckIfFailed()
{
if(m_combatUnitGroups.size() > 0)
{
// check if still enough power to attack target sector
if(SufficientCombatPowerToAttackSector(m_attackDestination, AAIConstants::attackCombatPowerFactor))
{
// check if sufficient power to combat enemy units
const float3 pos = (*m_combatUnitGroups.begin())->GetGroupPosition();
const AAISector* sector = ai->Map()->GetSectorOfPos(pos);
if(sector && SufficientCombatPowerAt(sector, AAIConstants::attackCombatPowerFactor))
return false;
}
}
return true;
}
bool AAIAttack::HasTargetBeenCleared()
{
if(m_attackDestination == nullptr)
return true;
else
{
if(m_attackDestination->GetNumberOfEnemyBuildings() == 0)
return true;
// m_combatUnitGroups cannot be empty, otherwise attack would have been aborted before this function gets called
const float3& targetPosition = (*m_combatUnitGroups.begin())->GetTargetPosition();
if( ai->Map()->IsPositionInLOS(targetPosition) )
{
// if target is in LOS but no enemy units in LOS target is supposed to be cleared
const int numberOfEnemyUnits = ai->GetAICallback()->GetEnemyUnits(&(ai->Map()->UnitsInLOS().front()), targetPosition, 128.0f);
return (numberOfEnemyUnits == 0);
}
return false;
}
}
const AAISector* AAIAttack::DetermineSectorToContinueAttack()
{
AAIMovementType moveType( GetMovementTypeOfAssignedUnits() );
MobileTargetTypeValues targetTypesOfUnits;
DetermineTargetTypeOfInvolvedUnits(targetTypesOfUnits);
const AAISector *dest = ai->Map()->DetermineSectorToContinueAttack(m_attackDestination, targetTypesOfUnits, moveType);
if(dest && SufficientCombatPowerToAttackSector(dest, AAIConstants::attackCombatPowerFactor))
return dest;
else
return nullptr;
}
bool AAIAttack::SufficientCombatPowerAt(const AAISector *sector, float aggressiveness) const
{
if(sector && (m_combatUnitGroups.size() > 0) )
{
// determine target types of own units and combat efficiency against different types of enemy units
MobileTargetTypeValues numberOfMyCombatUnits;
TargetTypeValues myCombatPower;
for(auto group : m_combatUnitGroups)
{
numberOfMyCombatUnits.AddValueForTargetType(group->GetTargetType(), static_cast<float>(group->GetCurrentSize()) );
group->AddGroupCombatPower(myCombatPower);
}
numberOfMyCombatUnits.Normalize();
// determine enemy combat power (weighted by own units)
const float enemyDefencePower = sector->GetEnemyCombatPowerVsUnits(numberOfMyCombatUnits);
TargetTypeValues numberOfEnemyUnits = sector->GetNumberOfEnemyCombatUnits();
const float totalEnemyUnits = numberOfEnemyUnits.CalcuateSum();
if(totalEnemyUnits > 0.0f)
{
// normalize relative number of enemy units
numberOfEnemyUnits.MultiplyValues(1.0f / totalEnemyUnits);
const float myAttackPower = myCombatPower.CalculateWeightedSum(numberOfEnemyUnits);
/*ai->Log("My units: %f, %f, %f, %f\n", numberOfMyCombatUnits.GetValueOfTargetType(ETargetType::SURFACE),
numberOfMyCombatUnits.GetValueOfTargetType(ETargetType::AIR),
numberOfMyCombatUnits.GetValueOfTargetType(ETargetType::FLOATER),
numberOfMyCombatUnits.GetValueOfTargetType(ETargetType::SUBMERGED));
ai->Log("Enemy units: %f, %f, %f, %f, %f\n", numberOfEnemyUnits.GetValue(ETargetType::SURFACE),
numberOfEnemyUnits.GetValue(ETargetType::AIR), numberOfEnemyUnits.GetValue(ETargetType::FLOATER),
numberOfEnemyUnits.GetValue(ETargetType::SUBMERGED),numberOfEnemyUnits.GetValue(ETargetType::STATIC));
ai->Log("My attack/enemy defence power: %f / %f\n", myAttackPower, enemyDefencePower);*/
//ai->Log("Local attacker / defender combat power: %f vs %f\n", myAttackPower, enemyDefencePower);
if(aggressiveness * myAttackPower > enemyDefencePower)
return true;
}
else
return true;
}
return false;
}
bool AAIAttack::SufficientCombatPowerToAttackSector(const AAISector *sector, float aggressiveness) const
{
if(sector && (m_combatUnitGroups.size() > 0))
{
// determine total combat power vs static & how it is distributed over different target types
float combatPowerVsBuildings(0.0f);
MobileTargetTypeValues targetTypeWeights;
for(const auto group : m_combatUnitGroups)
{
const float combatPower = group->GetCombatPowerVsTargetType(ETargetType::STATIC);
targetTypeWeights.AddValueForTargetType( group->GetTargetType(), combatPower);
combatPowerVsBuildings += combatPower;
}
// determine combat power by static enemy defences with respect to target type of attacking units
const float enemyDefencePower = targetTypeWeights.GetValueOfTargetType(ETargetType::SURFACE) * sector->GetEnemyCombatPower(ETargetType::SURFACE)
+ targetTypeWeights.GetValueOfTargetType(ETargetType::FLOATER) * sector->GetEnemyCombatPower(ETargetType::FLOATER)
+ targetTypeWeights.GetValueOfTargetType(ETargetType::SUBMERGED) * sector->GetEnemyCombatPower(ETargetType::SUBMERGED);
//ai->Log("Attacker / defender combat power: %f vs %f\n", combatPowerVsBuildings, enemyDefencePower);
if(aggressiveness * combatPowerVsBuildings > enemyDefencePower)
return true;
}
return false;
}
void AAIAttack::AttackPosition(const float3& position)
{
const AAISector* sector = ai->Map()->GetSectorOfPos(position);
if(sector)
{
m_attackDestination = sector;
m_lastAttackOrderInFrame = ai->GetAICallback()->GetCurrentFrame();
for(auto group : m_combatUnitGroups)
{
group->AttackPositionInSector(position, sector, AAIConstants::attackEnemyBaseUrgency);
}
// order aa groups to guard combat units
if(!m_combatUnitGroups.empty())
{
for(auto group : m_antiAirUnitGroups)
{
const UnitId unitId = (*m_combatUnitGroups.begin())->GetRandomUnit();
if(unitId.IsValid())
group->GuardUnit(unitId);
}
}
}
}
void AAIAttack::StopAttack()
{
for(auto group : m_combatUnitGroups)
{
// get rally point somewhere between current pos an base
//group->GetNewRallyPoint();
group->RetreatToRallyPoint();
}
for(auto group : m_antiAirUnitGroups)
{
// get rally point somewhere between current pos an base
//group->GetNewRallyPoint();
group->RetreatToRallyPoint();
}
m_combatUnitGroups.clear();
m_antiAirUnitGroups.clear();
}
AAIMovementType AAIAttack::GetMovementTypeOfAssignedUnits() const
{
AAIMovementType moveType;
for(auto group : m_combatUnitGroups)
moveType.AddMovementType( group->GetMovementType() );
for(auto group : m_antiAirUnitGroups)
moveType.AddMovementType( group->GetMovementType() );
return moveType;
}
void AAIAttack::DetermineTargetTypeOfInvolvedUnits(MobileTargetTypeValues& targetTypesOfUnits) const
{
for(auto group : m_combatUnitGroups)
targetTypesOfUnits.AddValueForTargetType( group->GetTargetType(), static_cast<float>( group->GetCurrentSize() ) );
}
void AAIAttack::AddGroupsOfTargetType(const std::list<AAIGroup*>& groupList, const AAITargetType& targetType)
{
for(auto group : groupList)
{
if(group->GetTargetType() == targetType)
{
if(AddGroup(group))
group->SetAttack(this);
}
}
}
bool AAIAttack::AddGroup(AAIGroup *group)
{
if(group->GetUnitTypeOfGroup().IsAssaultUnit())
{
m_combatUnitGroups.insert(group);
return true;
}
else if(group->GetUnitTypeOfGroup().IsAntiAir())
{
m_antiAirUnitGroups.insert(group);
return true;
}
return false;
}
void AAIAttack::RemoveGroup(AAIGroup *group)
{
if(group->GetUnitTypeOfGroup().IsAssaultUnit())
{
m_combatUnitGroups.erase(group);
}
else if(group->GetUnitTypeOfGroup().IsAntiAir())
{
m_antiAirUnitGroups.erase(group);
}
}
|