-
Notifications
You must be signed in to change notification settings - Fork 1
/
AAIUnitTable.cpp
436 lines (357 loc) · 11.8 KB
/
AAIUnitTable.cpp
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------
#include "AAI.h"
#include "AAIUnitTable.h"
#include "AAIExecute.h"
#include "AAIConstructor.h"
#include "AAIBuildTable.h"
#include "AAIAirForceManager.h"
#include "AAIConfig.h"
#include "AAIMap.h"
#include "AAIGroup.h"
#include "AAIConstructor.h"
#include "LegacyCpp/UnitDef.h"
using namespace springLegacyAI;
AAIUnitTable::AAIUnitTable(AAI *ai)
{
this->ai = ai;
units.resize(cfg->MAX_UNITS);
// fill buildtable
for(int i = 0; i < cfg->MAX_UNITS; ++i)
{
units[i].def_id = 0;
units[i].group = nullptr;
units[i].cons = nullptr;
units[i].status = UNIT_KILLED;
units[i].last_order = 0;
}
m_activeUnitsOfCategory.resize(AAIUnitCategory::numberOfUnitCategories, 0);
m_underConstructionUnitsOfCategory.resize(AAIUnitCategory::numberOfUnitCategories, 0);
m_requestedUnitsOfCategory.resize(AAIUnitCategory::numberOfUnitCategories, 0);
activeFactories = futureFactories = 0;
}
AAIUnitTable::~AAIUnitTable(void)
{
// delete constructors
for(auto constructor : m_constructors)
{
delete units[constructor.id].cons;
}
m_activeUnitsOfCategory.clear();
m_underConstructionUnitsOfCategory.clear();
m_requestedUnitsOfCategory.clear();
}
bool AAIUnitTable::AddUnit(int unit_id, int def_id, AAIGroup *group, AAIConstructor *cons)
{
if(unit_id < cfg->MAX_UNITS)
{
// clear possible enemies that are still listed (since they had been killed outside of los)
if(units[unit_id].status == ENEMY_UNIT)
{
if(units[unit_id].group)
units[unit_id].group->TargetUnitKilled();
}
else if(units[unit_id].status == BOMB_TARGET)
{
ai->AirForceMgr()->RemoveTarget(UnitId(unit_id));
if(units[unit_id].group)
ai->AirForceMgr()->FindNextBombTarget(units[unit_id].group);
//if(units[unit_id].group)
// units[unit_id].group->TargetUnitKilled();
}
units[unit_id].def_id = def_id;
units[unit_id].group = group;
units[unit_id].cons = cons;
units[unit_id].status = UNIT_IDLE;
return true;
}
else
{
ai->Log("ERROR: AAIUnitTable::AddUnit() index %i out of range", unit_id);
return false;
}
}
void AAIUnitTable::RemoveUnit(int unit_id)
{
if(unit_id < cfg->MAX_UNITS)
{
units[unit_id].def_id = 0;
units[unit_id].group = 0;
units[unit_id].cons = nullptr;
units[unit_id].status = UNIT_KILLED;
}
else
{
ai->Log("ERROR: AAIUnitTable::RemoveUnit() index %i out of range", unit_id);
}
}
void AAIUnitTable::AddConstructor(UnitId unitId, UnitDefId unitDefId)
{
const AAIUnitType& unitType = ai->s_buildTree.GetUnitType(unitDefId);
AAIConstructor *cons = new AAIConstructor(ai, unitId, unitDefId, unitType.IsFactory(), unitType.IsBuilder(), unitType.IsConstructionAssist(), ai->BuildTable()->GetBuildqueueOfFactory(unitDefId));
m_constructors.insert(unitId);
units[unitId.id].cons = cons;
// commander has not been requested before -> increase "requested constructors" counter as it is decreased by ConstructorFinished(...)
const bool commander = ai->s_buildTree.GetUnitCategory(unitDefId).IsCommander();
if(commander)
ai->BuildTable()->ConstructorRequested(unitDefId);
ai->BuildTable()->ConstructorFinished(unitDefId);
if( unitType.IsFactory() && ai->s_buildTree.GetMovementType(unitDefId).IsStatic() )
{
if(commander == false)
--futureFactories;
++activeFactories;
}
}
void AAIUnitTable::RemoveConstructor(UnitId unitId, UnitDefId unitDefId)
{
if( ai->s_buildTree.GetUnitType(unitDefId).IsFactory() && ai->s_buildTree.GetMovementType(unitDefId).IsStatic() )
activeFactories -= 1;
// decrease number of available builders for all buildoptions of the builder
ai->BuildTable()->ConstructorKilled(unitDefId);
// erase from builders list
m_constructors.erase(unitId);
// clean up memory
units[unitId.id].cons->Killed();
delete units[unitId.id].cons;
units[unitId.id].cons = nullptr;
}
void AAIUnitTable::AddExtractor(int unit_id)
{
extractors.insert(unit_id);
}
void AAIUnitTable::RemoveExtractor(int unit_id)
{
extractors.erase(unit_id);
}
void AAIUnitTable::AddScout(int unit_id)
{
scouts.insert(unit_id);
}
void AAIUnitTable::RemoveScout(int unit_id)
{
scouts.erase(unit_id);
}
void AAIUnitTable::AddPowerPlant(UnitId unitId, UnitDefId unitDefId)
{
power_plants.insert(unitId.id);
}
void AAIUnitTable::RemovePowerPlant(int unit_id)
{
power_plants.erase(unit_id);
}
void AAIUnitTable::AddMetalMaker(int unit_id, int def_id)
{
metal_makers.insert(unit_id);
}
void AAIUnitTable::RemoveMetalMaker(int unit_id)
{
metal_makers.erase(unit_id);
}
void AAIUnitTable::AddStaticSensor(UnitId unitId)
{
m_staticSensors.insert(unitId);
}
void AAIUnitTable::RemoveStaticSensor(UnitId unitId)
{
m_staticSensors.erase(unitId);
}
void AAIUnitTable::AddJammer(int unit_id, int def_id)
{
jammers.insert(unit_id);
}
void AAIUnitTable::RemoveJammer(int unit_id)
{
jammers.erase(unit_id);
}
void AAIUnitTable::AddStationaryArty(int unit_id, int /*def_id*/)
{
stationary_arty.insert(unit_id);
}
void AAIUnitTable::RemoveStationaryArty(int unit_id)
{
stationary_arty.erase(unit_id);
}
AAIConstructor* AAIUnitTable::FindBuilder(UnitDefId building, bool commander)
{
// look for idle builder
for(auto constructorUnitId : m_constructors)
{
// check all builders
if( ai->s_buildTree.GetUnitType(units[constructorUnitId.id].cons->m_myDefId).IsBuilder() )
{
AAIConstructor *constructor = units[constructorUnitId.id].cons;
// find unit that can directly build that building
if( constructor->IsAvailableForConstruction() && ai->s_buildTree.CanBuildUnitType(constructor->m_myDefId, building) )
{
// filter out commander (if not allowed)
if(! (!commander && ai->s_buildTree.GetUnitCategory(constructor->m_myDefId).IsCommander() ) )
return constructor;
}
}
}
// no builder found
return nullptr;
}
AvailableConstructor AAIUnitTable::FindClosestBuilder(UnitDefId building, const float3& position, bool commander)
{
const int continent = AAIMap::GetContinentID(position);
AvailableConstructor selectedBuilder;
// look for idle builder
for(auto constructor : m_constructors)
{
// check all builders
if(ai->s_buildTree.GetUnitType(units[constructor.id].cons->m_myDefId).IsBuilder())
{
AAIConstructor* builder = units[constructor.id].cons;
// find idle or assisting builder, who can build this building
if( builder->IsAvailableForConstruction()
&& ai->s_buildTree.CanBuildUnitType(builder->m_myDefId, building) )
{
const float3 builderPosition = ai->GetAICallback()->GetUnitPos(builder->m_myUnitId.id);
const bool continentCheckPassed = (ai->s_buildTree.GetMovementType(builder->m_myDefId).CannotMoveToOtherContinents() == false)
|| (AAIMap::GetContinentID(builderPosition) == continent);
const bool commanderCheckPassed = commander
|| ! ai->s_buildTree.GetUnitCategory(builder->m_myDefId).IsCommander();
// filter out commander
if(continentCheckPassed && commanderCheckPassed)
{
const float dx = builderPosition.x - position.x;
const float dy = builderPosition.z - position.z;
const float maxSpeed = std::max(0.1f, ai->s_buildTree.GetMaxSpeed(builder->m_myDefId));
const float travelTime = fastmath::apxsqrt( dx * dx + dy * dy ) / maxSpeed;
if( (travelTime < selectedBuilder.TravelTimeToBuildSite()) || (selectedBuilder.IsValid() == false))
selectedBuilder.SetAvailableConstructor(builder, travelTime);
}
}
}
}
return selectedBuilder;
}
AAIConstructor* AAIUnitTable::FindClosestAssistant(const float3& pos, int /*importance*/, bool commander)
{
const int continent = AAIMap::GetContinentID(pos);
AAIConstructor *selectedAssistant(nullptr);
float maxDist(0.0f);
// find idle builder
for(auto constructor : m_constructors)
{
// check all assisters
if( ai->s_buildTree.GetUnitType(units[constructor.id].cons->m_myDefId).IsConstructionAssist() )
{
AAIConstructor* assistant = units[constructor.id].cons;
// find idle assister
if(assistant->IsIdle())
{
const float3 assistantPosition = ai->GetAICallback()->GetUnitPos(assistant->m_myUnitId.id);
const AAIMovementType& moveType = ai->s_buildTree.GetMovementType(assistant->m_myDefId.id);
const bool continentCheckPassed = (moveType.CannotMoveToOtherContinents() == false) || (AAIMap::GetContinentID(assistantPosition) == continent);
const bool commanderCheckPassed = (commander || (ai->s_buildTree.GetUnitCategory(assistant->m_myDefId).IsCommander() == false) );
// filter out commander
if(continentCheckPassed && commanderCheckPassed)
{
const float dx = (pos.x - assistantPosition.x);
const float dy = (pos.z - assistantPosition.z);
const float squaredDist = dx * dx + dy * dy;
if( (squaredDist < maxDist) || (maxDist == 0.0f) )
{
maxDist = squaredDist;
selectedAssistant = assistant;
}
}
}
}
}
// no assister found -> request one
/*if(!best_assistant)
{
uint32_t allowedMovementTypes = static_cast<uint32_t>(EMovementType::MOVEMENT_TYPE_AIR)
+ static_cast<uint32_t>(EMovementType::MOVEMENT_TYPE_HOVER);
if(ai->GetAICallback()->GetElevation(pos.x, pos.z) < 0.0f)
{
allowedMovementTypes |= static_cast<uint32_t>(EMovementType::MOVEMENT_TYPE_SEA_FLOATER);
allowedMovementTypes |= static_cast<uint32_t>(EMovementType::MOVEMENT_TYPE_SEA_SUBMERGED);
}
else
{
allowedMovementTypes |= static_cast<uint32_t>(EMovementType::MOVEMENT_TYPE_GROUND);
}
ai->Getbt()->AddAssistant(allowedMovementTypes, true);
}*/
return selectedAssistant;
}
void AAIUnitTable::EnemyKilled(int unit)
{
if(units[unit].status == BOMB_TARGET)
{
ai->AirForceMgr()->RemoveTarget(UnitId(unit));
units[unit].status = ENEMY_UNIT;
if(units[unit].group)
ai->AirForceMgr()->FindNextBombTarget(units[unit].group);
}
else
{
if(units[unit].group)
units[unit].group->TargetUnitKilled();
}
RemoveUnit(unit);
}
void AAIUnitTable::SetEnemyUnitAsTargetOfGroup(UnitId unitId, AAIGroup *group)
{
units[unitId.id].group = group;
// only mark units as enemy if they are not already marked as potential bomb target (and thus on corresponding list of airforce manager)
if(units[unitId.id].status != BOMB_TARGET)
units[unitId.id].status = ENEMY_UNIT;
}
void AAIUnitTable::SetUnitStatus(int unit, UnitTask status)
{
units[unit].status = status;
}
void AAIUnitTable::UnitRequested(const AAIUnitCategory& category, int number)
{
m_requestedUnitsOfCategory[category.GetArrayIndex()] += number;
}
void AAIUnitTable::UnitRequestFailed(const AAIUnitCategory& category)
{
--m_requestedUnitsOfCategory[category.GetArrayIndex()];
}
void AAIUnitTable::ConstructionStarted(const AAIUnitCategory& category)
{
--m_requestedUnitsOfCategory[category.GetArrayIndex()];
++m_underConstructionUnitsOfCategory[category.GetArrayIndex()];
}
void AAIUnitTable::UnitUnderConstructionKilled(const AAIUnitCategory& category)
{
--m_underConstructionUnitsOfCategory[category.GetArrayIndex()];
}
void AAIUnitTable::UnitFinished(const AAIUnitCategory& category)
{
--m_underConstructionUnitsOfCategory[category.GetArrayIndex()];
++m_activeUnitsOfCategory[category.GetArrayIndex()];
}
void AAIUnitTable::ActiveUnitKilled(const AAIUnitCategory& category)
{
--m_activeUnitsOfCategory[category.GetArrayIndex()];
}
void AAIUnitTable::UpdateConstructors()
{
for(auto constructor : m_constructors)
{
units[constructor.id].cons->Update();
}
}
void AAIUnitTable::CheckBombTarget(UnitId unitId, UnitDefId defId, const AAIUnitCategory& category, const float3& position)
{
if(category.IsBuilding() && (units[unitId.id].status != BOMB_TARGET) )
{
const bool addedToTargets = ai->AirForceMgr()->CheckIfStaticBombTarget(unitId, defId, position);
if(addedToTargets)
units[unitId.id].status = BOMB_TARGET;
}
}