File: AAIAirForceManager.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (228 lines) | stat: -rw-r--r-- 5,344 bytes parent folder | download
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
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------

#include <list>

#include "AAIAirForceManager.h"

#include "AAIMap.h"
#include "AAI.h"
#include "AAIBuildTable.h"
#include "AAIUnitTable.h"
#include "AAIConfig.h"
#include "AAIGroup.h"
#include "AAISector.h"

#include "LegacyCpp/UnitDef.h"
using namespace springLegacyAI;


AAIAirForceManager::AAIAirForceManager(AAI *ai)
{
	this->ai = ai;

	num_of_targets = 0;

	targets.resize(cfg->MAX_AIR_TARGETS);

	for(int i = 0; i < cfg->MAX_AIR_TARGETS; ++i)
		targets[i].unit_id = -1;
}

AAIAirForceManager::~AAIAirForceManager(void)
{

}

void AAIAirForceManager::CheckTarget(const UnitId& unitId, const AAIUnitCategory& category, float health)
{
	// do not attack own units
	if(ai->GetAICallback()->GetUnitTeam(unitId.id) != ai->GetMyTeamId()) 
	{
		float3 pos = ai->GetAICallback()->GetUnitPos(unitId.id);

		// calculate in which sector unit is located
		AAISector* sector = ai->Getmap()->GetSectorOfPos(pos);

		// check if unit is within the map
		if(sector)
		{
			// check for anti air defences if low on units
			if( (sector->GetLostAirUnits() > 2.5f) && (ai->GetUnitGroupsList(EUnitCategory::AIR_COMBAT).size() < 5) )
				return;

			AAIGroup *group(nullptr);
			int max_groups;

			if(health > 8000)
				max_groups = 3;
			else if(health > 4000)
				max_groups = 2;
			else
				max_groups = 1;

			for(int i = 0; i < max_groups; ++i)
			{
				if(category.IsAirCombat() == true)
				{
					group = GetAirGroup(100.0, EUnitType::ANTI_AIR);

					if(group)
						group->DefendAirSpace(&pos);
				}
				else if(category.IsBuilding() == true)
				{
					group = GetAirGroup(100.0, EUnitType::ANTI_STATIC);

					if(group)
						group->BombTarget(unitId.id, &pos);
				}
				else
				{
					group = GetAirGroup(100.0, EUnitType::ANTI_SURFACE);

					if(group)
						group->AirRaidUnit(unitId.id);
				}
			}
		}
	}
}

void AAIAirForceManager::CheckBombTarget(int unit_id, int def_id)
{
	// dont continue if target list already full
	if(num_of_targets >= cfg->MAX_AIR_TARGETS)
		return;

	// do not add own units or units that ar already on target list
	if( (ai->GetAICallback()->GetUnitTeam(unit_id) != ai->GetMyTeamId()) && !IsTarget(unit_id))
	{
		const float3 pos = ai->GetAICallback()->GetUnitPos(unit_id);

		// check if unit is within the map
		if( ai->Getmap()->IsPositionWithinMap(pos) )
		{
			AddTarget(unit_id, def_id);
		}
	}
}

void AAIAirForceManager::AddTarget(int unit_id, int def_id)
{
	for(int i = 0; i < cfg->MAX_AIR_TARGETS; ++i)
	{
		if(targets[i].unit_id == -1)
		{
			ai->LogConsole("Target added...");

			targets[i].pos = ai->GetAICallback()->GetUnitPos(unit_id);
			targets[i].def_id = def_id;
			targets[i].cost = ai->s_buildTree.GetTotalCost(UnitDefId(def_id));
			targets[i].health = ai->GetAICallback()->GetUnitHealth(unit_id);

			ai->Getut()->units[unit_id].status = BOMB_TARGET;

			++num_of_targets;

			return;
		}
	}

	// could not add target, randomly overwrite one of the existing targets
	/*i = rand()%cfg->MAX_AIR_TARGETS;
	targets[i].pos.x = pos.x;
	targets[i].pos.z = pos.z;
	targets[i].def_id = def_id;
	targets[i].cost = cost;
	targets[i].health = health;
	targets[i].category = category;*/
}

void AAIAirForceManager::RemoveTarget(int unit_id)
{
	for(int i = 0; i < cfg->MAX_AIR_TARGETS; ++i)
	{
		if(targets[i].unit_id == unit_id)
		{
			ai->LogConsole("Target removed...");

			targets[i].unit_id = -1;

			ai->Getut()->units[unit_id].status = ENEMY_UNIT;

			--num_of_targets;

			return;
		}
	}
}

void AAIAirForceManager::BombBestUnit(float cost, float danger)
{
	float highestRating(0.0f);
	int   selectedTargetId(-1);

	for(int i = 0; i < cfg->MAX_AIR_TARGETS; ++i)
	{
		if(targets[i].unit_id != -1)
		{
			AAISector* sector = ai->Getmap()->GetSectorOfPos(targets[i].pos);

			if(sector)
			{
				const float healthRating = ai->Getbt()->GetUnitDef(targets[i].def_id).health / targets[i].health; // favor already damaged targets
				//! @todo Check this formula
				const float rating = pow(targets[i].cost, cost) / (1.0f + sector->GetEnemyCombatPower(ETargetType::AIR) * danger) * healthRating;

				if(rating > highestRating)
				{
					highestRating = rating;
					selectedTargetId = i;
				}
			}
		}
	}

	if(selectedTargetId != -1)
	{
		AAIGroup *group = GetAirGroup(100.0, EUnitType::ANTI_STATIC);

		if(group)
		{
			//ai->LogConsole("Bombing...");
			group->BombTarget(targets[selectedTargetId].unit_id, &targets[selectedTargetId].pos);

			targets[selectedTargetId].unit_id = -1;
			--num_of_targets;
		}
	}
}

AAIGroup* AAIAirForceManager::GetAirGroup(float importance, EUnitType groupType) const
{
	for(auto group = ai->GetUnitGroupsList(EUnitCategory::AIR_COMBAT).begin(); group != ai->GetUnitGroupsList(EUnitCategory::AIR_COMBAT).end(); ++group)
	{
		if( ((*group)->task_importance < importance) && (*group)->GetUnitTypeOfGroup().IsUnitTypeSet(groupType) )
			return *group;
	}
	
	return nullptr;
}

bool AAIAirForceManager::IsTarget(int unit_id)
{
	for(int i = 0; i < cfg->MAX_AIR_TARGETS; ++i)
	{
		if(targets[i].unit_id == unit_id)
			return true;
	}

	return false;
}