-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialPartitionGrid.cpp
More file actions
190 lines (157 loc) · 7.13 KB
/
SpatialPartitionGrid.cpp
File metadata and controls
190 lines (157 loc) · 7.13 KB
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
180
181
182
183
184
185
186
187
188
189
190
#include "SpatialPartitionGrid.h"
#include "Box.h"
#include "MovableObject.h"
#include "SceneMan.h"
using namespace RTE;
void SpatialPartitionGrid::Clear() {
m_Width = 0;
m_Height = 0;
m_CellSize = 0;
for (int team = Activity::NoTeam; team < Activity::MaxTeamCount; ++team) {
m_Cells[team + 1].clear();
m_PhysicsCells[team + 1].clear();
}
m_UsedCellIds.clear();
}
int SpatialPartitionGrid::Create(int width, int height, int cellSize) {
m_Width = width / cellSize;
m_Height = height / cellSize;
m_CellSize = cellSize;
for (int team = Activity::NoTeam; team < Activity::MaxTeamCount; ++team) {
m_Cells[team + 1].resize(m_Width * m_Height);
m_PhysicsCells[team + 1].resize(m_Width * m_Height);
}
return 0;
}
int SpatialPartitionGrid::Create(const SpatialPartitionGrid& reference) {
m_Width = reference.m_Width;
m_Height = reference.m_Height;
m_CellSize = reference.m_CellSize;
m_Cells = reference.m_Cells;
m_PhysicsCells = reference.m_PhysicsCells;
m_UsedCellIds = reference.m_UsedCellIds;
return 0;
}
void SpatialPartitionGrid::Reset() {
const Activity* activity = g_ActivityMan.GetActivity();
RTEAssert(activity, "Tried to reset spatial partition grid with no running Activity!");
for (int team = Activity::NoTeam; team < Activity::MaxTeamCount; ++team) {
if (team == Activity::NoTeam || activity->TeamActive(team)) {
for (int usedCellId: m_UsedCellIds) {
m_Cells[team + 1][usedCellId].clear();
m_PhysicsCells[team + 1][usedCellId].clear();
}
}
}
m_UsedCellIds.clear();
}
void SpatialPartitionGrid::Add(const IntRect& rect, const MovableObject& mo) {
const Activity* activity = g_ActivityMan.GetActivity();
RTEAssert(activity, "Tried to add to spatial partition grid with no running Activity!");
const MovableObject& rootParentMo = *mo.GetRootParent();
int topLeftCellX = rect.m_Left / m_CellSize;
int topLeftCellY = rect.m_Top / m_CellSize;
int bottomRightCellX = rect.m_Right / m_CellSize;
int bottomRightCellY = rect.m_Bottom / m_CellSize;
// We handle wrapping in GetCellIdForCellCoords, so make sure we've not already been passed wrapped data...
RTEAssert(topLeftCellX <= bottomRightCellX && topLeftCellY <= bottomRightCellY, "Invalidly wrapped rect passed to spatial partitioning grid!");
for (int team = Activity::NoTeam; team < Activity::MaxTeamCount; ++team) {
bool teamActive = team == Activity::NoTeam || activity->TeamActive(team);
bool ignoresThisTeam = team != Activity::NoTeam && rootParentMo.IgnoresTeamHits() && team == rootParentMo.GetTeam();
if (!teamActive || ignoresThisTeam) {
continue;
}
for (int x = topLeftCellX; x <= bottomRightCellX; x++) {
for (int y = topLeftCellY; y <= bottomRightCellY; y++) {
int cellId = GetCellIdForCellCoords(x, y);
m_Cells[team + 1][cellId].push_back(mo.GetID());
if (mo.GetsHitByMOs()) {
m_PhysicsCells[team + 1][cellId].push_back(mo.GetID());
}
}
}
}
for (int x = topLeftCellX; x <= bottomRightCellX; x++) {
for (int y = topLeftCellY; y <= bottomRightCellY; y++) {
m_UsedCellIds.insert(GetCellIdForCellCoords(x, y));
}
}
}
std::vector<MovableObject*> SpatialPartitionGrid::GetMOsInBox(const Box& box, int ignoreTeam, bool getsHitByMOsOnly) const {
RTEAssert(ignoreTeam >= Activity::NoTeam && ignoreTeam < Activity::MaxTeamCount, "Invalid ignoreTeam given to SpatialPartitioningGrid::GetMOsInBox()!");
std::unordered_set<MOID> potentialMOIDs;
Vector topLeft = box.GetCorner();
Vector bottomRight = topLeft + Vector(box.GetWidth(), box.GetHeight());
int topLeftCellX = static_cast<int>(std::floor(topLeft.m_X / static_cast<float>(m_CellSize)));
int topLeftCellY = static_cast<int>(std::floor(topLeft.m_Y / static_cast<float>(m_CellSize)));
int bottomRightCellX = static_cast<int>(std::floor(bottomRight.m_X / static_cast<float>(m_CellSize)));
int bottomRightCellY = static_cast<int>(std::floor(bottomRight.m_Y / static_cast<float>(m_CellSize)));
// Note - GetCellIdForCellCoords accounts for wrapping automatically, so we don't have to deal with it here.
auto& cells = getsHitByMOsOnly ? m_PhysicsCells : m_Cells;
for (int x = topLeftCellX; x <= bottomRightCellX; x++) {
for (int y = topLeftCellY; y <= bottomRightCellY; y++) {
const std::vector<MOID>& moidsInCell = cells[ignoreTeam + 1][GetCellIdForCellCoords(x, y)];
for (MOID moid: moidsInCell) {
potentialMOIDs.insert(moid);
}
}
}
std::list<Box> wrappedBoxes;
g_SceneMan.WrapBox(box, wrappedBoxes);
std::vector<MovableObject*> MOList;
for (MOID moid: potentialMOIDs) {
MovableObject* mo = g_MovableMan.GetMOFromID(moid);
if (mo && std::any_of(wrappedBoxes.begin(), wrappedBoxes.end(), [&mo](const Box& wrappedBox) { return wrappedBox.IsWithinBox(mo->GetPos()); })) {
MOList.push_back(mo);
}
}
return MOList;
}
std::vector<MovableObject*> SpatialPartitionGrid::GetMOsInRadius(const Vector& center, float radius, int ignoreTeam, bool getsHitByMOsOnly) const {
RTEAssert(ignoreTeam >= Activity::NoTeam && ignoreTeam < Activity::MaxTeamCount, "Invalid ignoreTeam given to SpatialPartitioningGrid::GetMOsInRadius()!");
std::unordered_set<MOID> potentialMOIDs;
int topLeftCellX = static_cast<int>(std::floor((center.m_X - radius) / static_cast<float>(m_CellSize)));
int topLeftCellY = static_cast<int>(std::floor((center.m_Y - radius) / static_cast<float>(m_CellSize)));
int bottomRightCellX = static_cast<int>(std::floor((center.m_X + radius) / static_cast<float>(m_CellSize)));
int bottomRightCellY = static_cast<int>(std::floor((center.m_Y + radius) / static_cast<float>(m_CellSize)));
// Note - GetCellIdForCellCoords accounts for wrapping automatically, so we don't have to deal with it here.
auto& cells = getsHitByMOsOnly ? m_PhysicsCells : m_Cells;
for (int x = topLeftCellX; x <= bottomRightCellX; x++) {
for (int y = topLeftCellY; y <= bottomRightCellY; y++) {
const std::vector<MOID>& moidsInCell = cells[ignoreTeam + 1][GetCellIdForCellCoords(x, y)];
for (MOID moid: moidsInCell) {
potentialMOIDs.insert(moid);
}
}
}
std::vector<MovableObject*> MOList;
for (MOID moid: potentialMOIDs) {
MovableObject* mo = g_MovableMan.GetMOFromID(moid);
if (mo && !g_SceneMan.ShortestDistance(center, mo->GetPos()).MagnitudeIsGreaterThan(radius)) {
MOList.push_back(mo);
}
}
return MOList;
}
const std::vector<MOID>& SpatialPartitionGrid::GetMOIDsAtPosition(int x, int y, int ignoreTeam, bool getsHitByMOsOnly) const {
int cellX = x / m_CellSize;
int cellY = y / m_CellSize;
// Lua sometimes decides to give SceneMan an ignoreTeam value of... -2.
// Yeah, seriously.
// So let's sanity check this shit.
ignoreTeam = ignoreTeam < Activity::NoTeam || ignoreTeam >= Activity::MaxTeamCount ? Activity::NoTeam : ignoreTeam;
auto& cells = getsHitByMOsOnly ? m_PhysicsCells : m_Cells;
return cells[ignoreTeam + 1][GetCellIdForCellCoords(cellX, cellY)];
}
int SpatialPartitionGrid::GetCellIdForCellCoords(int cellX, int cellY) const {
// We act like we wrap, even if the Scene doesn't. The only cost is some duplicate collision checks, but that's a minor cost to pay :)
int wrappedX = cellX % m_Width;
if (wrappedX < 0) {
wrappedX += m_Width;
}
int wrappedY = cellY % m_Height;
if (wrappedY < 0) {
wrappedY += m_Height;
}
return (wrappedY * m_Width) + wrappedX;
}