forked from miki151/keeperrl
/
territory.cpp
83 lines (70 loc) · 2.16 KB
/
territory.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
#include "stdafx.h"
#include "territory.h"
#include "position.h"
#include "movement_type.h"
template <class Archive>
void Territory::serialize(Archive& ar, const unsigned int version) {
ar & SVAR(allSquares) & SVAR(allSquaresVec);
}
SERIALIZABLE(Territory);
void Territory::clearCache() {
extendedCache.clear();
extendedCache2.clear();
}
void Territory::insert(Position pos) {
if (!allSquares.count(pos)) {
allSquaresVec.push_back(pos);
allSquares.insert(pos);
clearCache();
}
}
void Territory::remove(Position pos) {
removeElement(allSquaresVec, pos);
allSquares.erase(pos);
clearCache();
}
bool Territory::contains(Position pos) const {
return allSquares.count(pos);
}
const vector<Position>& Territory::getAll() const {
return allSquaresVec;
}
vector<Position> Territory::calculateExtended(int minRadius, int maxRadius) const {
map<Position, int> extendedTiles;
vector<Position> extendedQueue;
for (Position pos : allSquaresVec) {
if (!extendedTiles.count(pos)) {
extendedTiles[pos] = 1;
extendedQueue.push_back(pos);
}
}
for (int i = 0; i < extendedQueue.size(); ++i) {
Position pos = extendedQueue[i];
for (Position v : pos.neighbors8())
if (!contains(v) && !extendedTiles.count(v)
&& v.canEnterEmpty({MovementTrait::WALK})) {
int a = extendedTiles[v] = extendedTiles[pos] + 1;
if (a < maxRadius)
extendedQueue.push_back(v);
}
}
if (minRadius > 0)
return filter(extendedQueue, [&] (const Position& v) { return extendedTiles.at(v) >= minRadius; });
return extendedQueue;
}
const vector<Position>& Territory::getStandardExtended() const {
return getExtended(2, 10);
}
const vector<Position>& Territory::getExtended(int min, int max) const {
if (!extendedCache.count(make_pair(min, max)))
extendedCache[make_pair(min, max)] = calculateExtended(min, max);
return extendedCache.at(make_pair(min, max));
}
const vector<Position>& Territory::getExtended(int max) const {
if (!extendedCache2.count(max))
extendedCache2[max] = calculateExtended(0, max);
return extendedCache2.at(max);
}
bool Territory::isEmpty() const {
return allSquaresVec.empty();
}