File: AirBaseHandler.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (179 lines) | stat: -rwxr-xr-x 4,246 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "System/mmgr.h"
#include "AirBaseHandler.h"

#include "GlobalSynced.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/Scripts/CobInstance.h"
#include "Sim/Units/UnitDef.h"
#include "System/creg/STL_List.h"
#include "System/creg/STL_Set.h"

#include <limits>

CAirBaseHandler* airBaseHandler = NULL;

CR_BIND(CAirBaseHandler, )
CR_REG_METADATA(CAirBaseHandler,(
	CR_MEMBER(bases),
	CR_MEMBER(airBaseIDs),
	CR_RESERVED(16)
));

CR_BIND_DERIVED(CAirBaseHandler::LandingPad, CObject, (0, 0, NULL));
CR_REG_METADATA_SUB(CAirBaseHandler, LandingPad, (
	CR_MEMBER(unit),
	CR_MEMBER(piece),
	CR_MEMBER(base),
	CR_RESERVED(8)
));

CR_BIND(CAirBaseHandler::AirBase, (NULL))
CR_REG_METADATA_SUB(CAirBaseHandler, AirBase, (
	CR_MEMBER(unit),
	CR_MEMBER(freePads),
	CR_MEMBER(pads),
	CR_RESERVED(8)
));


CAirBaseHandler::CAirBaseHandler() : bases(teamHandler->ActiveAllyTeams())
{
}


CAirBaseHandler::~CAirBaseHandler()
{
	// should not be any bases left here...
	for (int a = 0; a < teamHandler->ActiveAllyTeams(); ++a) {
		for (AirBaseLstIt bi = bases[a].begin(); bi != bases[a].end(); ++bi) {
			for (PadLstIt pi = (*bi)->pads.begin(); pi != (*bi)->pads.end(); ++pi) {
				delete *pi;
			}
			delete *bi;
		}
	}
}

void CAirBaseHandler::RegisterAirBase(CUnit* owner)
{
	// prevent a unit from being registered as a base more than
	// once (this can happen eg. when an airbase unit is damaged
	// and then repaired back to full health, which causes it to
	// re-register itself via FinishedBuilding())
	if (airBaseIDs.find(owner->id) != airBaseIDs.end()) {
		return;
	}

	AirBase* ab = new AirBase(owner);
	std::vector<int> args;

	owner->script->QueryLandingPads(/*out*/ args);

	// FIXME: use a set to avoid multiple bases per piece?
	for (unsigned int p = 0; p < args.size(); p++) {
		const int piece = args[p];

		if (!owner->script->PieceExists(piece)) {
			continue;
		}

		LandingPad* pad = new LandingPad(piece, owner, ab);

		ab->pads.push_back(pad);
		ab->freePads.push_back(pad);
	}

	bases[owner->allyteam].push_back(ab);
	airBaseIDs.insert(owner->id);
}


void CAirBaseHandler::DeregisterAirBase(CUnit* base)
{
	if (airBaseIDs.find(base->id) == airBaseIDs.end()) {
		return;
	}

	for (AirBaseLstIt bi = bases[base->allyteam].begin(); bi != bases[base->allyteam].end(); ++bi) {
		if ((*bi)->unit == base) {
			for (PadLstIt pi = (*bi)->pads.begin(); pi != (*bi)->pads.end(); ++pi) {
				// the unit that has reserved a pad is responsible to see if the pad is gone so just delete it
				delete *pi;
			}
			delete *bi;
			bases[base->allyteam].erase(bi);
			break;
		}
	}

	airBaseIDs.erase(base->id);
}

void CAirBaseHandler::LeaveLandingPad(LandingPad* pad)
{
	pad->GetBase()->freePads.push_back(pad);
}



CAirBaseHandler::LandingPad* CAirBaseHandler::FindAirBase(CUnit* unit, float minPower, bool wantFreePad)
{
	float minDist = std::numeric_limits<float>::max();

	AirBaseLstIt foundBaseIt = bases[unit->allyteam].end();

	for (AirBaseLstIt bi = bases[unit->allyteam].begin(); bi != bases[unit->allyteam].end(); ++bi) {
		AirBase* base = *bi;
		CUnit* baseUnit = base->unit;

		if (unit == baseUnit) {
			// do not pick ourselves as a landing pad
			continue;
		}
		if (baseUnit->beingBuilt || baseUnit->stunned) {
			continue;
		}

		if (baseUnit->pos.SqDistance(unit->pos) >= minDist || baseUnit->unitDef->buildSpeed < minPower) {
			continue;
		}

		if (wantFreePad && base->freePads.empty()) {
			continue;
		}

		minDist = baseUnit->pos.SqDistance(unit->pos);
		foundBaseIt = bi;
	}

	if (foundBaseIt != bases[unit->allyteam].end()) {
		AirBase* foundBase = *foundBaseIt;

		if (wantFreePad) {
			LandingPad* foundPad = foundBase->freePads.front();
			foundBase->freePads.pop_front();
			return foundPad;
		} else {
			if (!foundBase->pads.empty())
				return foundBase->pads.front();
		}
	}

	return NULL;
}

float3 CAirBaseHandler::FindClosestAirBasePos(CUnit* unit, float minPower)
{
	const CAirBaseHandler::LandingPad* pad = FindAirBase(unit, minPower, false);

	if (pad == NULL)
		return ZeroVector;

	const AirBase* base = pad->GetBase();
	const CUnit* baseUnit = base->unit;
	return baseUnit->pos;
}