1 // -------------------------------------------------------------------------
2 // AAI
3 //
4 // A skirmish AI for the Spring engine.
5 // Copyright Alexander Seizinger
6 //
7 // Released under GPL license: see LICENSE.html for more information.
8 // -------------------------------------------------------------------------
9 
10 #include <set>
11 using namespace std;
12 
13 #include "AAIBuildTask.h"
14 #include "AAI.h"
15 #include "AAIConstructor.h"
16 #include "AAIUnitTable.h"
17 #include "AAIBuildTable.h"
18 #include "AAIExecute.h"
19 #include "AAIMap.h"
20 #include "AAISector.h"
21 
AAIBuildTask(AAI * ai,int unit_id,int def_id,float3 * pos,int tick)22 AAIBuildTask::AAIBuildTask(AAI *ai, int unit_id, int def_id, float3 *pos, int tick):
23 	build_pos(*pos)
24 {
25 	this->ai = ai;
26 	this->unit_id = unit_id;
27 	this->def_id = def_id;
28 
29 	order_tick = tick;
30 
31 	builder_id = -1;
32 
33 }
34 
~AAIBuildTask(void)35 AAIBuildTask::~AAIBuildTask(void)
36 {
37 }
38 
BuilderDestroyed()39 void AAIBuildTask::BuilderDestroyed()
40 {
41 	builder_id = -1;
42 
43 	// com only allowed if buildpos is inside the base
44 	bool commander = false;
45 
46 	int x = build_pos.x / ai->Getmap()->xSectorSize;
47 	int y = build_pos.z / ai->Getmap()->ySectorSize;
48 
49 	if(x >= 0 && y >= 0 && x < ai->Getmap()->xSectors && y < ai->Getmap()->ySectors)
50 	{
51 		if(ai->Getmap()->sector[x][y].distance_to_base == 0)
52 			commander = true;
53 	}
54 
55 	// look for new builder
56 	AAIConstructor* new_builder = ai->Getut()->FindClosestAssistant(build_pos, 10, commander);
57 
58 	if(new_builder)
59 	{
60 		new_builder->TakeOverConstruction(this);
61 		builder_id = new_builder->unit_id;
62 	}
63 }
64 
BuildtaskFailed()65 void AAIBuildTask::BuildtaskFailed()
66 {
67 	// cleanup buildmap etc.
68 	if(ai->Getbt()->units_static[def_id].category <= METAL_MAKER)
69 		ai->Getexecute()->ConstructionFailed(build_pos, def_id);
70 
71 	// tell builder to stop construction (and release assisters) (if still alive)
72 	if(builder_id >= 0 && ai->Getut()->units[builder_id].cons)
73 		ai->Getut()->units[builder_id].cons->ConstructionFinished();
74 }
75