-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group.cpp
255 lines (202 loc) · 6.27 KB
/
Group.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "precomp.h"
#include "Group.h"
#include "Military.h"
#include "EntityManager.h"
#include "ColliderComponent.h"
#include "TransformComponent.h"
#include "Unit.h"
#include "TimeManager.h"
#include "SelectingComponent.h"
#include "Scene.h"
Group::Group(Scene& scene, const vector<size_t>& ids, bool ownsUnits) :
Entity(scene),
unitsInGroupIds(ids)
{
if (ownsUnits)
{
for (EntityId id : unitsInGroupIds)
{
Unit* unit = dynamic_cast<Unit*>(scene.entityManager->TryGetEntity(id).value_or(nullptr));
if (unit != nullptr)
{
unit->partOfGroup = GetId();
}
}
RemoveInvalidIds();
}
}
Group::~Group()
{
}
void Group::operator+=(Group& other)
{
vector<Unit*> otherUnits = other.GetUnitsInGroup();
for (Unit* unit : otherUnits)
{
unitsInGroupIds.push_back(unit->GetId());
unit->partOfGroup = GetId();
}
other.unitsInGroupIds.clear();
}
void Group::Tick()
{
float currentTime = TimeManager::GetTotalTimePassed();
float timeSinceLastUpdate = currentTime - timeLastUpdate;
if (timeSinceLastUpdate < updateCooldown)
{
return;
}
RemoveInvalidIds();
if (unitsInGroupIds.empty())
{
scene.entityManager->RemoveEntity(GetId());
return;
}
if (desiredFormation.has_value())
{
AssignUnitsToFormation(GetUnitsInGroup(), desiredFormation.value());
FormFormation(desiredFormation.value());
}
timeLastUpdate = currentTime;
}
float2 Group::GetGroupCentre(const vector<Unit*>& units) const
{
float2 groupCentre{};
for (Unit* unit : units)
{
groupCentre += unit->GetComponent<TransformComponent>()->GetWorldPos();
}
return groupCentre / static_cast<float>(units.size());
}
void Group::RemoveInvalidIds()
{
EntityManager* entityManager = scene.entityManager.get();
EntityId groupId = GetId();
unitsInGroupIds.erase(std::remove_if(unitsInGroupIds.begin(), unitsInGroupIds.end(),
[groupId, entityManager](const EntityId& id) -> bool
{
Unit* unit = dynamic_cast<Unit*>(entityManager->TryGetEntity(id).value_or(nullptr));
return unit == nullptr || unit->partOfGroup != groupId;
}), unitsInGroupIds.end());
}
vector<float> Group::GetStatesDistribution() const
{
vector<float> distribution(static_cast<size_t>(Unit::State::numOfStates), 0.0f);
vector<Unit*> unitsInGroup = GetUnitsInGroup();
for (Unit* unit : unitsInGroup)
{
distribution[static_cast<size_t>(unit->GetState())] += 1.0f;
}
for (float& value : distribution)
{
value /= unitsInGroup.size();
}
return distribution;
}
Group::Formation Group::CalculateFormation(const MoveTo& moveTo) const
{
vector<Unit*> unitsInGroup = GetUnitsInGroup();
if (unitsInGroup.empty())
{
return Formation{moveTo};
}
// Generate points
Unit* biggestUnit = *max_element(unitsInGroup.begin(), unitsInGroup.end(),
[](const Unit* a, const Unit* b)
{
return a->GetComponent<ColliderComponent>()->GetRadius() < b->GetComponent<ColliderComponent>()->GetRadius();
});
float spacing = biggestUnit->GetComponent<ColliderComponent>()->GetRadius() * 2.0f + biggestUnit->desiredSpacing * 1.0f;
const int groupSize = static_cast<int>(unitsInGroup.size());
Formation formation{moveTo};
float2 deltaPosGroup = moveTo.destination - GetGroupCentre(unitsInGroup);
formation.rotation = moveTo.rotationAtEnd.value_or(Float2ToAngle(deltaPosGroup));
formation.points.resize((groupSize * 2) * (groupSize * 2));
// Generating all the points can be optimized; Im creating and sorting points that are discarded.
// this would be faster if points were generated from the centre.
size_t pointIndex = 0;
for (int x = -groupSize; x < groupSize; x++)
{
for (int y = -groupSize; y < groupSize; y++, pointIndex++)
{
FormationPoint& point = formation.points[pointIndex];
point.position = (make_float2(static_cast<float>(x), static_cast<float>(y)) * spacing + moveTo.destination);
point.sqrDistToCentre = SqrdDistance(point.position, moveTo.destination);
}
}
// Sort the points by distance to the centre
sort(formation.points.begin(), formation.points.end(),
[](const Group::FormationPoint& a, const Group::FormationPoint& b) -> bool
{
return a.sqrDistToCentre < b.sqrDistToCentre;
});
formation.points.resize(groupSize);
AssignUnitsToFormation(unitsInGroup, formation);
return formation;
}
void Group::FormFormation(const Formation& formation)
{
desiredFormation = formation;
EntityManager& entityManager = *scene.entityManager;
for (const FormationPoint& fp : formation.points)
{
auto idIt = find(unitsInGroupIds.begin(), unitsInGroupIds.end(), fp.assignedUnit);
if (idIt == unitsInGroupIds.end())
{
// Unit has left the group or has been destroyed in the meantime
continue;
}
optional<Entity*> entity = entityManager.TryGetEntity(*idIt);
if (!entity.has_value())
{
continue;
}
Unit* unit = dynamic_cast<Unit*>(entity.value());
assert(unit != nullptr);
unit->CommandMoveTo(MoveTo{ fp.position, formation.givenCommand.aggroLevel, formation.rotation });
}
}
vector<Unit*> Group::GetUnitsInGroup() const
{
vector<Unit*> units;
units.reserve(unitsInGroupIds.size());
EntityManager& entityManager = *scene.entityManager;
for (size_t i = 0; i < unitsInGroupIds.size(); i++)
{
Unit* unit = dynamic_cast<Unit*>(entityManager.TryGetEntity(unitsInGroupIds[i]).value_or(nullptr));
if (unit != nullptr)
{
units.push_back(unit);
}
}
return units;
}
struct UnitToMove
{
Unit* unit{};
float2 positionAfterGoupMovement{};
};
void Group::AssignUnitsToFormation(const vector<Unit*>& units, Formation& formation) const
{
float2 deltaPosGroup = formation.givenCommand.destination - GetGroupCentre(units);
vector<UnitToMove> unitsToMove(units.size());
for (size_t i = 0; i < units.size(); i++)
{
Unit* unit = units[i];
float2 position = unit->GetComponent<TransformComponent>()->GetWorldPos();
float2 positionAfterMove = position + deltaPosGroup;
unitsToMove[i] = { unit, positionAfterMove };
}
// Units left the group, remove the furthest points.
formation.points.resize(unitsToMove.size());
for (Group::FormationPoint& fp : formation.points)
{
auto closest = min_element(unitsToMove.begin(), unitsToMove.end(),
[fp](const UnitToMove& a, const UnitToMove& b)
{
return SqrdDistance(a.positionAfterGoupMovement, fp.position) < SqrdDistance(b.positionAfterGoupMovement, fp.position);
});
fp.assignedUnit = closest->unit->GetId();
unitsToMove.erase(closest);
}
}