-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleCounter.cpp
117 lines (95 loc) · 2.36 KB
/
VehicleCounter.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
#include "VehicleCounter.h"
namespace UnitHelper
{
int VehicleCounter::GetVehicleCount(map_id vehicleType)
{
std::vector<int> vehicleIndices;
GetVehicleIndices(vehicleIndices, vehicleType);
int count = 0;
for (int index : vehicleIndices)
{
count += VehicleCountVector[index].Count;
}
return count;
}
int VehicleCounter::GetVehicleCount(map_id vehicleType, map_id cargoType)
{
int index = GetVehicleIndex(vehicleType, cargoType);
if (index == -1)
{
return 0;
}
return VehicleCountVector[index].Count;
}
void VehicleCounter::PullVehiclesFromPlayer(PlayerUnitEnum playerUnitEnum)
{
Clear();
Unit unit;
while (playerUnitEnum.GetNext(unit))
{
AddVehicleToVector(unit.GetType(), unit.GetCargo());
}
}
void VehicleCounter::PullVehiclesFromRectangle(PlayerNum playerNum, InRectEnumerator inRectEnumerator)
{
Clear();
Unit unit;
while (inRectEnumerator.GetNext(unit))
{
if ((playerNum != PlayerNum::PlayerAll) && (unit.OwnerID() != (int)playerNum))
{
continue;
}
AddVehicleToVector(unit.GetType(), unit.GetCargo());
}
}
void VehicleCounter::Clear()
{
VehicleCount = 0;
VehicleCountVector.clear();
}
// PRIVATE FUNCTIONS
void VehicleCounter::GetVehicleIndices(std::vector<int>& vehicleIndices, map_id vehicleType)
{
for (std::size_t i = 0; i < VehicleCountVector.size(); ++i)
{
if (VehicleCountVector[i].UnitType == vehicleType)
{
vehicleIndices.push_back(i);
}
}
}
/*If Unit and cargo combination do not exist in vector, -1 is returned.*/
int VehicleCounter::GetVehicleIndex(map_id vehicleType, map_id cargoType)
{
for (std::size_t i = 0; i < VehicleCountVector.size(); ++i)
{
if (VehicleCountVector[i].UnitType == vehicleType &&
VehicleCountVector[i].UnitCargo == cargoType)
{
return i;
}
}
return -1;
}
void VehicleCounter::AddVehicleToVector(map_id vehicleType, map_id cargoType)
{
int index = GetVehicleIndex(vehicleType, cargoType);
if (index == -1)
{
UnitCount unitCount;
unitCount.UnitType = vehicleType;
unitCount.UnitCargo = cargoType;
unitCount.Count = 1;
VehicleCountVector.push_back(unitCount);
}
else
{
VehicleCountVector[index].Count++;
}
if (IsVehicle(vehicleType))
{
VehicleCount++;
}
}
}