File: CCoverageCell.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (105 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download | duplicates (4)
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
#include "CCoverageCell.h"

#include "CAI.h"
#include "CUnitTable.h"
#include "CCoverageHandler.h"


std::map<CCoverageCell::NType, std::string> CCoverageCell::type2str;

bool CCoverageCell::isInRange(const float3& pos)  const {
	return getCenter().distance2D(pos) <= range;
}

void CCoverageCell::update(std::list<CUnit*>& uncovered) {
	if (isVacant())	return;

	float newRange = ai->coverage->getCoreRange(type, unit->type);
	if (newRange < range) {
		const float3 center = getCenter();
		for (std::map<int, CUnit*>::iterator it = units.begin(); it != units.end(); ) {
			const float3 pos = it->second->pos();
			if (center.distance2D(pos) > newRange) {
				uncovered.push_back(it->second);
				it->second->unreg(*this);
				units.erase(it++);
			}
			else
				++it;
		}

		range = newRange;
	}

	// TODO: if core is mobile then update it when position has changed
}

void CCoverageCell::setCore(CUnit* u) {
	assert(unit == NULL);

	u->reg(*this);

	unit = u;
	range = ai->coverage->getCoreRange(type, unit->type);
}

void CCoverageCell::remove() {
	LOG_II("CCoverageCell::remove " << (*this))

	// remove this object from CCoverageHandler...
	for (std::list<ARegistrar*>::iterator it = records.begin(); it != records.end(); ) {
		(*(it++))->remove(*this);
	}

	assert(records.empty());

	if (unit)
		unit->unreg(*this);

	if (!units.empty()) {
		for (std::map<int, CUnit*>::iterator it = units.begin(); it != units.end(); ++it) {
			it->second->unreg(*this);
		}
		units.clear();
	}

	unit = NULL;
	range = 0.0f;
}

void CCoverageCell::remove(ARegistrar& u) {
	if (unit->key == u.key)
		remove();
	else {
		units.erase(u.key);
		u.unreg(*this);
	}
}

bool CCoverageCell::addUnit(CUnit* u) {
	if (unit && unit->key == u->key)
		return false; // do not add itself
	if (ai->coverage->getCoreType(u->type) == type)
		return false; // do not cover neighbour cores
	assert(units.find(u->key) == units.end());
	units[u->key] = u;
	u->reg(*this);
	return true;
}

std::ostream& operator<<(std::ostream& out, const CCoverageCell& obj) {
	std::stringstream ss;

	if (obj.unit)
		ss << "CoverageCell(" << obj.unit->def->humanName;
	else
		ss << "CoverageCell(Unknown";

	ss << "):" << " type(" << obj.type2str[obj.type] << "), range(" << obj.range << "), amount(" << obj.units.size() << ")";

	std::string s = ss.str();

	out << s;

	return out;
}