File: AAIAttackManager.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (422 lines) | stat: -rw-r--r-- 11,642 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the TA 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 "AAIBuildTable.h"
#include "AAIGroup.h"
#include "AAIMap.h"
#include "AAIAttack.h"

AAIAttackManager::AAIAttackManager(AAI *ai, IAICallback *cb, AAIBuildTable *bt, int continents)
{
	this->ai = ai;
	brain = ai->brain;
	this->bt = bt;
	this->cb = cb;
	this->map = ai->map;

	available_combat_cat.resize(bt->ass_categories);

	available_combat_groups_continent.resize(continents);
	available_aa_groups_continent.resize(continents);

	attack_power_continent.resize(continents, vector<float>(bt->combat_categories) );
	attack_power_global.resize(bt->combat_categories);
}

AAIAttackManager::~AAIAttackManager(void)
{
	for(list<AAIAttack*>::iterator a = attacks.begin(); a != attacks.end(); ++a)
		delete (*a);

	attacks.clear();
}


void AAIAttackManager::Update()
{
	for(list<AAIAttack*>::iterator a = attacks.begin(); a != attacks.end(); ++a)
	{
		// drop failed attacks
		if((*a)->Failed())
		{
			(*a)->StopAttack();

			delete (*a);
			attacks.erase(a);

			break;
		}

		// check if sector cleared
		if((*a)->dest)
		{
			if((*a)->dest->enemy_structures <= 0)
				GetNextDest(*a);
		}
	}

	if(attacks.size() < cfg->MAX_ATTACKS)
	{
		LaunchAttack();
	}
}

void AAIAttackManager::LaunchAttack()
{
	//////////////////////////////////////////////////////////////////////////////////////////////
	// get all available combat/aa/arty groups for attack
	//////////////////////////////////////////////////////////////////////////////////////////////
	int total_combat_groups = 0;

	for(list<UnitCategory>::iterator category = bt->assault_categories.begin(); category != bt->assault_categories.end(); ++category)
	{
		for(list<AAIGroup*>::iterator group = ai->group_list[*category].begin(); group != ai->group_list[*category].end(); ++group)
		{
			if( (*group)->AvailableForAttack() )
			{
				if((*group)->group_movement_type & MOVE_TYPE_CONTINENT_BOUND)
				{
					if((*group)->group_unit_type == ASSAULT_UNIT)
					{
						available_combat_groups_continent[(*group)->continent].push_back(*group);
						++total_combat_groups;
					}
					else
						available_aa_groups_continent[(*group)->continent].push_back(*group);
				}
				else
				{
					if((*group)->group_unit_type == ASSAULT_UNIT)
					{
						available_combat_groups_global.push_back(*group);
						++total_combat_groups;
					}
					else
						available_aa_groups_global.push_back(*group);
				}
			}
		}
	}

	// stop planing an attack if there are no combat groups available at the moment
	if(total_combat_groups == 0)
		return;

	//////////////////////////////////////////////////////////////////////////////////////////////
	// calculate max attack power for each continent
	//////////////////////////////////////////////////////////////////////////////////////////////
	fill(attack_power_global.begin(), attack_power_global.end(), 0.0f);

	for(list<AAIGroup*>::iterator group = available_combat_groups_global.begin(); group != available_combat_groups_global.end(); ++group)
		(*group)->GetCombatPower( &attack_power_global );

	for(size_t continent = 0; continent < available_combat_groups_continent.size(); ++continent)
	{
		fill(attack_power_continent[continent].begin(), attack_power_continent[continent].end(), 0.0f);

		for(list<AAIGroup*>::iterator group = available_combat_groups_continent[continent].begin(); group != available_combat_groups_continent[continent].end(); ++group)
			(*group)->GetCombatPower( &(attack_power_continent[continent]) );
	}

	// determine max lost units
	float max_lost_units = 0.0f, lost_units;

	for(int x = 0; x < map->xSectors; ++x)
	{
		for(int y = 0; y < map->ySectors; ++y)
		{
			lost_units = map->sector[x][y].GetLostUnits(1.0f, 1.0f, 1.0f, 1.0f, 1.0f);

			if(lost_units > max_lost_units)
				max_lost_units = lost_units;
		}
	}

	//////////////////////////////////////////////////////////////////////////////////////////////
	// determine attack sector
	//////////////////////////////////////////////////////////////////////////////////////////////
	float def_power, att_power;
	float best_rating = 0, my_rating;
	AAISector *dest = 0, *sector;

	for(int x = 0; x < map->xSectors; ++x)
	{
		for(int y = 0; y < map->ySectors; ++y)
		{
			sector = &map->sector[x][y];

			if(sector->distance_to_base == 0 || sector->enemy_structures < 0.0001)
				my_rating = 0;
			else
			{
				if(ai->map->continents[sector->continent].water)
				{
					def_power = sector->GetEnemyDefencePower(0.0f, 0.0f, 0.5f, 1.0f, 1.0f) + 0.01;
					att_power = attack_power_global[5] + attack_power_continent[sector->continent][5];
				}
				else
				{
					def_power = sector->GetEnemyDefencePower(1.0f, 0.0f, 0.5f, 0.0f, 0.0f) + 0.01;
					att_power = attack_power_global[5] + attack_power_continent[sector->continent][5];
				}

				my_rating = (1.0f - sector->GetLostUnits(1.0f, 1.0f, 1.0f, 1.0f, 1.0f) / max_lost_units) * sector->enemy_structures * att_power / ( def_power * (float)(2 + sector->distance_to_base) );

				//if(SufficientAttackPowerVS(dest, &combat_available, 2))
			}

			if(my_rating > best_rating)
			{
				dest = sector;
				best_rating = my_rating;
			}
		}
	}

	//////////////////////////////////////////////////////////////////////////////////////////////
	// order attack
	//////////////////////////////////////////////////////////////////////////////////////////////

	if(dest)
	{
		AAIAttack *attack = new AAIAttack(ai);
		attacks.push_back(attack);

		// add combat groups
		for(list<AAIGroup*>::iterator group = available_combat_groups_continent[dest->continent].begin(); group != available_combat_groups_continent[dest->continent].end(); ++group)
			attack->AddGroup(*group);

		for(list<AAIGroup*>::iterator group = available_combat_groups_global.begin(); group != available_combat_groups_global.end(); ++group)
			attack->AddGroup(*group);

		// add anti-air defence
		int aa_added = 0, max_aa;

		// check how much aa sensible
		if(brain->max_combat_units_spotted[1] < 0.2)
			max_aa = 0;
		else
			max_aa = 1;

		for(list<AAIGroup*>::iterator group = available_aa_groups_continent[dest->continent].begin(); group != available_aa_groups_continent[dest->continent].end(); ++group)
		{
			if(aa_added >= max_aa)
				break;

			attack->AddGroup(*group);
			++aa_added;
		}

		for(list<AAIGroup*>::iterator group = available_aa_groups_global.begin(); group != available_aa_groups_global.end(); ++group)
		{
			if(aa_added >= max_aa)
				break;

			attack->AddGroup(*group);
			++aa_added;
		}

		// rally attacking groups
		//RallyGroups(attack);

		// start the attack
		attack->AttackSector(dest);
	}
}

void AAIAttackManager::StopAttack(AAIAttack *attack)
{
	for(list<AAIAttack*>::iterator a = attacks.begin(); a != attacks.end(); ++a)
	{
		if(*a == attack)
		{
			(*a)->StopAttack();

			delete (*a);
			attacks.erase(a);

			break;
		}
	}
}

void AAIAttackManager::CheckAttack(AAIAttack *attack)
{
	// prevent command overflow
	if((cb->GetCurrentFrame() - attack->lastAttack) < 30)
		return;

	// drop failed attacks
	if(attack->Failed())
	{
		// delete attack from list
		for(list<AAIAttack*>::iterator a = attacks.begin(); a != attacks.end(); ++a)
		{
			if(*a == attack)
			{
				attacks.erase(a);

				attack->StopAttack();
				delete attack;

				break;
			}
		}
	}
}

void AAIAttackManager::GetNextDest(AAIAttack *attack)
{
	// prevent command overflow
	if((cb->GetCurrentFrame() - attack->lastAttack) < 60)
		return;

	// get new target sector
	AAISector *dest = ai->brain->GetNextAttackDest(attack->dest, attack->land, attack->water);

	//fprintf(ai->file, "Getting next dest\n");
	if(dest && SufficientAttackPowerVS(dest, &(attack->combat_groups), 2))
		attack->AttackSector(dest);
	else
		attack->StopAttack();
}

bool AAIAttackManager::SufficientAttackPowerVS(AAISector *dest, set<AAIGroup*> *combat_groups, float aggressiveness)
{
	if(dest && combat_groups->size() > 0)
	{
		// check attack power
		float attack_power = 0.5;
		float sector_defence = 0;
		int total_units = 1;

		// store ammount and category of involved groups;
		fill(available_combat_cat.begin(), available_combat_cat.end(), 0);

		// get total att power
		for(set<AAIGroup*>::iterator group = combat_groups->begin(); group != combat_groups->end(); ++group)
		{
			attack_power += (*group)->GetCombatPowerVsCategory(5);
			available_combat_cat[(*group)->combat_category] += (*group)->size;
			total_units += (*group)->size;
		}

		attack_power += (float)total_units * 0.2f;

		//  get expected def power
		for(int i = 0; i < bt->ass_categories; ++i)
			sector_defence += dest->enemy_stat_combat_power[i] * (float)available_combat_cat[i];

		sector_defence /= (float)total_units;

		//fprintf(ai->file, "Checking attack power - att power / def power %f %f\n", aggressiveness * attack_power, sector_defence);

		if(aggressiveness * attack_power >= sector_defence)
			return true;
	}

	return false;
}

bool AAIAttackManager::SufficientCombatPowerAt(AAISector *dest, set<AAIGroup*> *combat_groups, float aggressiveness)
{
	if(dest && combat_groups->size() > 0)
	{
		// store ammount and category of involved groups;
		double my_power = 0, enemy_power = 0;
		int total_units = 0;

		// reset counter
		for(int i = 0; i < bt->ass_categories; ++i)
			available_combat_cat[i] = 0;

		// get total att power
		for(int i = 0; i < bt->ass_categories; ++i)
		{
			// skip if no enemy units of that category present
			if(dest->enemy_combat_units[i] > 0)
			{
				// filter out air in normal mods
				if(i != 1 || cfg->AIR_ONLY_MOD)
				{
					for(set<AAIGroup*>::iterator group = combat_groups->begin(); group != combat_groups->end(); ++group)
						my_power += (*group)->GetCombatPowerVsCategory(i) * dest->enemy_combat_units[i];

					total_units +=  dest->enemy_combat_units[i];
				}
			}
		}

		// skip if no enemy units found
		if(total_units == 0)
			return true;

		my_power += (float) total_units * 0.20f;

		my_power /= (float)total_units;

		// get ammount of involved groups per category
		total_units = 1;

		for(set<AAIGroup*>::iterator group = combat_groups->begin(); group != combat_groups->end(); ++group)
		{
			available_combat_cat[(*group)->combat_category] += (*group)->size;
			total_units += (*group)->size;
		}

		// get total enemy power
		for(int i = 0; i < bt->ass_categories; ++i)
			enemy_power += dest->GetEnemyAreaCombatPowerVs(i, 0.25) * (float)available_combat_cat[i];

		enemy_power /= (float)total_units;

		//fprintf(ai->file, "Checking combat power - att power / def power %f %f\n", aggressiveness * my_power, enemy_power);

		if(aggressiveness * my_power >= enemy_power)
			return true;
	}

	return false;
}

bool AAIAttackManager::SufficientDefencePowerAt(AAISector *dest, float aggressiveness)
{
	// store ammount and category of involved groups;
	double my_power = 0;
	//double enemy_power = 0;
	float enemies = 0;

	// get defence power
	for(int i = 0; i < bt->ass_categories; ++i)
	{
		// only check if enemies of that category present
		if(dest->enemy_combat_units[i] > 0)
		{
			enemies += dest->enemy_combat_units[i];
			my_power += dest->my_stat_combat_power[i] * dest->enemy_combat_units[i];
		}
	}

	if(enemies > 0)
	{
		my_power /= enemies;

		if(aggressiveness * my_power >= dest->GetEnemyAreaCombatPowerVs(5, 0.5))
			return true;
	}

	return false;
}

void AAIAttackManager::RallyGroups(AAIAttack *attack)
{
}