|
| 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