File: AAIAirForceManager.cpp

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (247 lines) | stat: -rw-r--r-- 5,840 bytes parent folder | download | duplicates (3)
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
// -------------------------------------------------------------------------
// 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;

	my_team = ai->Getcb()->GetMyTeam();
	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;

	air_groups = &ai->Getgroup_list()[AIR_ASSAULT];
}

AAIAirForceManager::~AAIAirForceManager(void)
{

}

void AAIAirForceManager::CheckTarget(int unit_id, const UnitDef *def)
{
	// do not attack own units
	if(my_team != ai->Getcb()->GetUnitTeam(unit_id))
	{
		float3 pos = ai->Getcb()->GetUnitPos(unit_id);

		// calculate in which sector unit is located
		int x = pos.x/ai->Getmap()->xSectorSize;
		int y = pos.z/ai->Getmap()->ySectorSize;

		// check if unit is within the map
		if(x >= 0 && x < ai->Getmap()->xSectors && y >= 0 && y < ai->Getmap()->ySectors)
		{
			// check for anti air defences if low on units
			if(ai->Getmap()->sector[x][y].lost_units[AIR_ASSAULT-COMMANDER] >= cfg->MAX_AIR_GROUP_SIZE && ai->Getgroup_list()[AIR_ASSAULT].size() < 5)
				return;

			AAIGroup *group;
			int max_groups;

			UnitCategory category = ai->Getbt()->units_static[def->id].category;

			if(ai->Getbt()->GetUnitDef(def->id).health > 8000)
				max_groups = 3;
			else if(ai->Getbt()->GetUnitDef(def->id).health > 4000)
				max_groups = 2;
			else
				max_groups = 1;

			for(int i = 0; i < max_groups; ++i)
			{
				if(category == AIR_ASSAULT)
				{
					group = GetAirGroup(100.0, ANTI_AIR_UNIT);

					if(group)
						group->DefendAirSpace(&pos);
				}
				else if(category <= METAL_MAKER)
				{
					group = GetAirGroup(100.0, BOMBER_UNIT);

					if(group)
						group->BombTarget(unit_id, &pos);
				}
				else
				{
					group = GetAirGroup(100.0, ASSAULT_UNIT);

					if(group)
						group->AirRaidUnit(unit_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(my_team != ai->Getcb()->GetUnitTeam(unit_id) && !IsTarget(unit_id))
	{
		float3 pos = ai->Getcb()->GetUnitPos(unit_id);

		// calculate in which sector unit is located
		int x = pos.x/ai->Getmap()->xSectorSize;
		int y = pos.z/ai->Getmap()->ySectorSize;

		// check if unit is within the map
		if(x >= 0 && x < ai->Getmap()->xSectors && y >= 0 && y < ai->Getmap()->ySectors)
		{
			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->Getcb()->GetUnitPos(unit_id);
			targets[i].def_id = def_id;
			targets[i].cost = ai->Getbt()->units_static[def_id].cost;
			targets[i].health = ai->Getcb()->GetUnitHealth(unit_id);
			targets[i].category = ai->Getbt()->units_static[def_id].category;

			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 best = 0, current;
	int best_target = -1;
	int x, y, i;

	for(i = 0; i < cfg->MAX_AIR_TARGETS; ++i)
	{
		if(targets[i].unit_id != -1)
		{
			x = targets[i].pos.x/ai->Getmap()->xSectorSize;
			y = targets[i].pos.z/ai->Getmap()->ySectorSize;

			current = pow(targets[i].cost, cost) / (1.0f + ai->Getmap()->sector[x][y].enemy_stat_combat_power[1] * danger) * targets[i].health / ai->Getbt()->GetUnitDef(targets[i].def_id).health ;

			if(current > best)
			{
				best = current;
				best_target = i;
			}
		}
	}

	if(best_target != -1)
	{
		AAIGroup *group = GetAirGroup(100.0, BOMBER_UNIT);

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

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

AAIGroup* AAIAirForceManager::GetAirGroup(float importance, UnitType group_type)
{
	if(cfg->AIR_ONLY_MOD)
	{
		for(list<AAIGroup*>::iterator group = air_groups->begin(); group != air_groups->end(); ++group)
		{
			if((*group)->task_importance < importance && group_type == (*group)->group_unit_type  && (*group)->units.size() > (*group)->maxSize/2)
				return *group;
		}
	}
	else
	{
		for(list<AAIGroup*>::iterator group = air_groups->begin(); group != air_groups->end(); ++group)
		{
			if((*group)->task_importance < importance && group_type == (*group)->group_unit_type && (*group)->units.size() >= (*group)->maxSize)
				return *group;
		}
	}

	return 0;
}

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;
}