-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolony.cpp
123 lines (103 loc) · 2.77 KB
/
colony.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
#include "colony.hpp"
namespace TerraNova {
Colony::Colony(SDL_Renderer* ren, const int faction):
ren(ren), name(Faction::GenerateColonyName(faction)), faction(faction)
{
resources.fill(5*RESCAP/5);
resourceCap.fill(RESCAP);
}
void Colony::ChangeName(const std::string name){
this->name = name;
}
int Colony::AddResource(const resource_t resource, int amount){
if(resource < 0 || resource >= LAST_RESOURCE) return -1;
if(amount < 0) return 0;
if(resources[resource] + amount > resourceCap[resource]){
amount -= resourceCap[resource]-resources[resource];
resources[resource] = resourceCap[resource];
} else {
resources[resource] += amount;
amount = 0;
}
return amount;
}
std::array<int, LAST_RESOURCE> Colony::AddResources(
const std::array<int, LAST_RESOURCE>& income){
std::array<int, LAST_RESOURCE> leftovers;
for(unsigned int i = 0; i < income.size(); ++i){
if(income[i] <= 0){
leftovers[i] = income[i];
continue;
}
if(resources[i] + income[i] > resourceCap[i]){
leftovers[i] = income[i] - resourceCap[i] + resources[i];
resources[i] = resourceCap[i];
} else {
resources[i] += income[i];
leftovers[i] = 0;
}
}
return leftovers;
}
int Colony::TakeResource(const resource_t resource, int amount){
if(resource < 0 || resource >= LAST_RESOURCE) return -1;
if(amount < 0) return 0;
if(amount > resources[resource]){
amount = resources[resource];
resources[resource] = 0;
} else {
resources[resource] -= amount;
}
return amount;
}
std::array<int, LAST_RESOURCE> Colony::TakeResources(
const std::array<int, LAST_RESOURCE>& amounts) {
std::array<int, LAST_RESOURCE> ret;
for (auto i = 0u; i < LAST_RESOURCE; ++i) {
ret[i] = TakeResource(static_cast<resource_t>(i), amounts[i]);
}
return ret;
}
std::string Colony::Name() const{
return name;
}
int Colony::Row() const{
return row;
}
int Colony::Column() const{
return colm;
}
const std::array<int, LAST_RESOURCE>& Colony::Resources() const {
return resources;
}
const int& Colony::Resource(const resource_t resource) const{
return resources[resource];
}
const int& Colony::Resource(const int resource) const{
return Resource(static_cast<resource_t>(resource));
}
const int& Colony::AvailablePower() const {
return powerGrid.AvailablePower();
}
const int& Colony::MaximumPower() const {
return powerGrid.MaximumPower();
}
void Colony::AddBuilding(std::shared_ptr<Building> toAdd) {
powerGrid.AddBuilding(toAdd);
}
void Colony::StartTurn(){
powerGrid.StartTurn();
}
void Colony::EndTurn(){
powerGrid.EndTurn();
}
std::string Colony::ResourceName(const resource_t resource){
switch(resource){
case FOOD: return "food";
case CARBON: return "carbon";
case SILICON: return "silicon";
case IRON: return "iron";
default: return "RESOURCE NOT FOUND";
}
}
} // namespace TerraNova