File: AAIBuildTask.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (101 lines) | stat: -rw-r--r-- 2,456 bytes parent folder | download | duplicates (2)
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
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------

#include <set>
using namespace std;

#include "AAIBuildTask.h"
#include "AAI.h"
#include "AAIConstructor.h"
#include "AAIBuildTable.h"
#include "AAIExecute.h"
#include "AAISector.h"

AAIBuildTask::AAIBuildTask(UnitId unitId, UnitDefId unitDefId, const float3& buildsite, UnitId constructor) :
	m_unitId(unitId),
	m_defId(unitDefId),
	m_buildsite(buildsite),
	m_constructor(constructor)
{
}

AAIBuildTask::~AAIBuildTask(void)
{
}

void AAIBuildTask::BuilderDestroyed(AAIMap* map, AAIUnitTable* unitTable)
{
	m_constructor.Invalidate();

	// com only allowed if buildpos is inside the base
	bool commander = false;

	AAISector* sector = map->GetSectorOfPos(m_buildsite);

	if(sector && (sector->GetDistanceToBase() == 0) )
		commander = true;

	// look for new builder
	AAIConstructor* nextBuilder = unitTable->FindClosestAssistant(m_buildsite, 10, commander);

	if(nextBuilder)
	{
		nextBuilder->TakeOverConstruction(this);
		m_constructor = nextBuilder->m_myUnitId;
	}
}

bool AAIBuildTask::CheckIfConstructionFailed(AAI* ai, UnitId unitId)
{
	if(m_unitId == unitId)
	{
		// cleanup buildmap etc.
		if(ai->s_buildTree.GetMovementType(m_defId).IsStatic())
			ai->Execute()->ConstructionFailed(m_buildsite, m_defId);

		AAIConstructor* constructor = GetConstructor(ai->UnitTable());

		if(constructor)
			constructor->ConstructionFinished();

		return true;
	}
	else
		return false;
}

bool AAIBuildTask::CheckIfConstructionFinished(AAIUnitTable* unitTable, UnitId unitId)
{
	if (m_unitId == unitId)
	{
		AAIConstructor* constructor = GetConstructor(unitTable);

		if(constructor)
			constructor->ConstructionFinished();

		return true;
	}
	else
		return false;	
}

bool AAIBuildTask::IsExpensiveUnitOfCategoryInSector(AAI* ai, const AAIUnitCategory& category, const AAISector* sector) const
{
	if(    (ai->s_buildTree.GetUnitCategory(m_defId) == category)
		&& sector->PosInSector(m_buildsite) )
	{
		const StatisticalData& costStatistics = ai->s_buildTree.GetUnitStatistics(ai->GetSide()).GetUnitCostStatistics(category);

		if( ai->s_buildTree.GetTotalCost(m_defId) > 0.7f * costStatistics.GetAvgValue() )
			return true;
	}

	return false;
}