Skip to content

Commit 0054c0d

Browse files
authored
Merge pull request #1576 from OutpostUniverse/miscTransitiveHeaders
Reduce misc transitive headers
2 parents e434faa + ba4e84a commit 0054c0d

12 files changed

+203
-160
lines changed

appOPHD/Map/TileMap.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <algorithm>
1515
#include <functional>
16+
#include <numeric>
1617
#include <array>
1718

1819

appOPHD/MapObjects/Structures/StorageTanks.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ StringTable StorageTanks::createInspectorViewTable()
3030
1,
3131
{
3232
std::to_string(storage().total()) + " / " + std::to_string(storageCapacity()),
33-
storage().resources[0],
34-
storage().resources[1],
35-
storage().resources[2],
36-
storage().resources[3]
33+
std::to_string(storage().resources[0]),
34+
std::to_string(storage().resources[1]),
35+
std::to_string(storage().resources[2]),
36+
std::to_string(storage().resources[3]),
3737
});
3838

3939
return stringTable;

appOPHD/ProductPool.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Constants/Numbers.h"
44
#include "Constants/Strings.h"
55

6+
#include <NAS2D/Dictionary.h>
67
#include <NAS2D/ParserHelper.h>
78

89
#include <algorithm>

appOPHD/ProductPool.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
#include <libOPHD/EnumProductType.h>
44

5-
#include <NAS2D/Dictionary.h>
6-
75
#include <array>
86

97

8+
namespace NAS2D
9+
{
10+
class Dictionary;
11+
}
12+
13+
1014
int storageRequiredPerUnit(ProductType type);
1115

1216
class ProductPool

appOPHD/States/CrimeExecution.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ namespace
3939
}
4040

4141

42+
std::vector<std::size_t> getIndicesWithStock(const StorableResources& storabeResources)
43+
{
44+
std::vector<std::size_t> indicesWithStock;
45+
46+
for (std::size_t i = 0; i < storabeResources.resources.size(); ++i)
47+
{
48+
if (storabeResources.resources[i] > 0)
49+
{
50+
indicesWithStock.push_back(i);
51+
}
52+
}
53+
54+
return indicesWithStock;
55+
}
56+
57+
4258
int calcAmountForStealing(Difficulty difficulty, int low, int high, int max)
4359
{
4460
auto stealRandom = randomNumber.generate(low, high);
@@ -118,16 +134,17 @@ void CrimeExecution::stealRawResources(Structure& structure)
118134

119135
void CrimeExecution::stealResources(Structure& structure, const std::array<std::string, 4>& resourceNames)
120136
{
121-
if (structure.storage().isEmpty())
137+
auto& storage = structure.storage();
138+
if (storage.isEmpty())
122139
{
123140
return;
124141
}
125142

126-
auto resourceIndicesWithStock = structure.storage().getIndicesWithStock();
143+
auto resourceIndicesWithStock = getIndicesWithStock(storage);
127144
auto indexToStealFrom = randomNumber.generate<std::size_t>(0, resourceIndicesWithStock.size() - 1);
128145

129-
int amountStolen = calcAmountForStealing(mDifficulty, 2, 5, structure.storage().resources[indexToStealFrom]);
130-
structure.storage().resources[indexToStealFrom] -= amountStolen;
146+
int amountStolen = calcAmountForStealing(mDifficulty, 2, 5, storage.resources[indexToStealFrom]);
147+
storage.resources[indexToStealFrom] -= amountStolen;
131148

132149
mCrimeEventSignal.emit(
133150
"Resources Stolen",

appOPHD/StorableResources.cpp

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include "StorableResources.h"
2+
3+
#include <algorithm>
4+
#include <numeric>
5+
6+
7+
StorableResources StorableResources::operator*(int multiplier) const
8+
{
9+
StorableResources result = *this;
10+
for (size_t i = 0; i < resources.size(); ++i)
11+
{
12+
result.resources[i] *= multiplier;
13+
}
14+
return result;
15+
}
16+
17+
18+
StorableResources StorableResources::operator/(int divisor) const
19+
{
20+
StorableResources result = *this;
21+
for (size_t i = 0; i < resources.size(); ++i)
22+
{
23+
result.resources[i] /= divisor;
24+
}
25+
return result;
26+
}
27+
28+
29+
StorableResources& StorableResources::operator+=(const StorableResources& other)
30+
{
31+
for (size_t i = 0; i < resources.size(); ++i)
32+
{
33+
resources[i] += other.resources[i];
34+
}
35+
36+
return *this;
37+
}
38+
39+
40+
StorableResources& StorableResources::operator-=(const StorableResources& other)
41+
{
42+
for (size_t i = 0; i < resources.size(); ++i)
43+
{
44+
resources[i] -= other.resources[i];
45+
}
46+
47+
return *this;
48+
}
49+
50+
51+
bool StorableResources::operator<=(const StorableResources& other) const
52+
{
53+
for (size_t i = 0; i < resources.size(); ++i)
54+
{
55+
if (!(resources[i] <= other.resources[i]))
56+
{
57+
return false;
58+
}
59+
}
60+
return true;
61+
}
62+
63+
64+
bool StorableResources::operator<(const StorableResources& other) const
65+
{
66+
for (size_t i = 0; i < resources.size(); ++i)
67+
{
68+
if (!(resources[i] < other.resources[i]))
69+
{
70+
return false;
71+
}
72+
}
73+
return true;
74+
}
75+
76+
77+
bool StorableResources::operator>=(const StorableResources& other) const
78+
{
79+
return other <= *this;
80+
}
81+
82+
83+
bool StorableResources::operator>(const StorableResources& other) const
84+
{
85+
return other < *this;
86+
}
87+
88+
89+
StorableResources StorableResources::cap(const StorableResources& other) const
90+
{
91+
StorableResources out;
92+
for (std::size_t i = 0; i < resources.size(); ++i)
93+
{
94+
out.resources[i] = std::clamp(resources[i], 0, other.resources[i]);
95+
}
96+
97+
return out;
98+
}
99+
100+
101+
StorableResources StorableResources::cap(int max) const
102+
{
103+
StorableResources out;
104+
for (std::size_t i = 0; i < resources.size(); ++i)
105+
{
106+
out.resources[i] = std::clamp(resources[i], 0, max);
107+
}
108+
109+
return out;
110+
}
111+
112+
113+
bool StorableResources::isEmpty() const
114+
{
115+
for (size_t i = 0; i < resources.size(); ++i)
116+
{
117+
if (resources[i] > 0)
118+
{
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
124+
125+
126+
// Sum of all resource types
127+
int StorableResources::total() const
128+
{
129+
return std::accumulate(resources.begin(), resources.end(), 0);
130+
}
131+
132+
133+
StorableResources operator+(StorableResources lhs, const StorableResources& rhs)
134+
{
135+
return lhs += rhs;
136+
}
137+
138+
139+
StorableResources operator-(StorableResources lhs, const StorableResources& rhs)
140+
{
141+
return lhs -= rhs;
142+
}

0 commit comments

Comments
 (0)