-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolony.cpp
More file actions
271 lines (250 loc) · 6.63 KB
/
colony.cpp
File metadata and controls
271 lines (250 loc) · 6.63 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "math.h"
#include "game.h"
#include "ship.h"
Colony::Colony(int o, Planet *p, int initial) {
planet = p;
owner = o;
Init(initial);
}
void Colony::Init(int initial) {
population = 0;
pop_minor = 0;
if(initial) population = 500;
for(int ctr=0; ctr<cur_tree->NumTechs(); ++ctr) {
Tech *tc = cur_tree->GetTech(ctr);
if(tc->type == TECH_STRUCTURE && tc->known[owner]) {
objs.push_back(ctr);
oqty.push_back(initial * cur_tree->Homeworld(ctr));
population += initial * tc->crew * cur_tree->Homeworld(ctr);
}
}
for(int ctr=0; ctr<cur_tree->NumTechs(); ++ctr) {
Tech *tc = cur_tree->GetTech(ctr);
if(tc->type == TECH_PROJECT && tc->known[owner]) {
objs.push_back(ctr);
oqty.push_back(initial * cur_tree->Homeworld(ctr));
population += initial * tc->crew * cur_tree->Homeworld(ctr);
}
}
int fn = 0;
if(!planet) return;
planet->See(owner);
planet->Know(owner);
for(int ctr=0; ctr<cur_tree->NumTechs(); ++ctr) {
Tech *tc = cur_tree->GetTech(ctr);
if(tc->type == TECH_SHIP) {
objs.push_back(ctr);
oqty.push_back(0);
if(initial && cur_tree->Homeworld(ctr) > 0) {
Fleet *flt = new Fleet(planet, tc->names);
planet->Sys()->objects.push_back(flt);
for(int nshp=0 ; nshp < cur_tree->Homeworld(ctr); ++nshp) {
Ship *shp = new Ship(ctr, owner);
flt->AddShip(shp);
shp->AddCrew(shp->MaxCrew());
}
++fn;
}
}
}
}
Colony::~Colony() {
}
int Colony::Industry() {
int ind = Population()/1000;
for(int ctr=0; ctr<(int)objs.size(); ++ctr) {
ind += cur_tree->Industry(objs[ctr], oqty[ctr], this);
}
if(planet) {
for(int flt=0; flt<int(planet->Sys()->objects.size()); ++flt) {
if(planet->Sys()->objects[flt]->SType() == SOBJECT_FLEET) {
for(int shp=0; shp<((Fleet*)planet->Sys()->objects[flt])->NumShips(); ++shp) {
ind += cur_tree->Industry(((Fleet*)planet->Sys()->objects[flt])->GetShip(shp)->tech, 1, this);
}
}
}
}
long long free;
free = FreePop();
free *= 1000000;
free += FreePopM();
if(free < 0) {
long long tmp = ind, need, busy;
busy = Population();
busy *= 1000000;
busy += PopulationM();
need = busy + (-free);
tmp *= busy;
tmp /= need;
ind = tmp;
}
return max(0, ind);
}
int Colony::SpareIndustry() {
int ind = Industry();
for(int ctr=0; ctr<(int)objs.size(); ++ctr) {
ind -= cur_tree->Upkeep(objs[ctr], oqty[ctr], this);
}
return max(0, ind);
}
int Colony::Population() {
return population;
}
int Colony::PopulationM() {
return pop_minor;
}
int Colony::FreePop() {
int pop = population;
for(int ctr=0; ctr<(int)objs.size(); ++ctr) {
pop -= cur_tree->Crew(objs[ctr], oqty[ctr], this);
}
return pop;
}
int Colony::FreePopM() {
if(pop_minor == 0) return 0;
else if(FreePop() < 0) return -1000000 + pop_minor;
else return pop_minor;
}
int Colony::Growth() {
long long ret = population;
ret *= Happiness();
ret *= Security();
ret /= 100;
ret /= 1000000;
return ret;
}
int Colony::GrowthM() {
long long ret = population;
ret *= Happiness();
ret *= Security();
ret /= 100;
ret %= 1000000;
return ret;
}
int Colony::Happiness() {
if(Population() == 0) return 0;
int hap = FreePop()*100/Population();
for(int ctr=0; ctr<(int)objs.size(); ++ctr) {
hap += cur_tree->Happiness(objs[ctr], oqty[ctr], this);
}
if(planet) {
for(int flt=0; flt<int(planet->Sys()->objects.size()); ++flt) {
if(planet->Sys()->objects[flt]->SType() == SOBJECT_FLEET) {
for(int shp=0; shp<((Fleet*)planet->Sys()->objects[flt])->NumShips(); ++shp) {
hap += cur_tree->Happiness(((Fleet*)planet->Sys()->objects[flt])->GetShip(shp)->tech, 1, this);
}
}
}
}
return min(max(0, hap), 1000);
}
int Colony::Security() {
int sec = 0;
for(int ctr=0; ctr<int(objs.size()); ++ctr) {
sec += cur_tree->Security(objs[ctr], oqty[ctr], this);
}
if(planet) {
for(int flt=0; flt<int(planet->Sys()->objects.size()); ++flt) {
if(planet->Sys()->objects[flt]->SType() == SOBJECT_FLEET) {
for(int shp=0; shp<((Fleet*)planet->Sys()->objects[flt])->NumShips(); ++shp) {
sec += cur_tree->Security(((Fleet*)planet->Sys()->objects[flt])->GetShip(shp)->tech, 1, this);
}
}
}
}
return min(max(0, sec), 1000);
}
int Colony::Loyalty() {
int loy = 0;
for(int ctr=0; ctr<(int)objs.size(); ++ctr) {
loy += cur_tree->Loyalty(objs[ctr], oqty[ctr], this);
}
if(planet) {
for(int flt=0; flt<int(planet->Sys()->objects.size()); ++flt) {
if(planet->Sys()->objects[flt]->SType() == SOBJECT_FLEET) {
for(int shp=0; shp<((Fleet*)planet->Sys()->objects[flt])->NumShips(); ++shp) {
loy += cur_tree->Loyalty(((Fleet*)planet->Sys()->objects[flt])->GetShip(shp)->tech, 1, this);
}
}
}
}
return loy;
}
void Colony::TakeTurn() {
pop_minor += GrowthM();
population += pop_minor / 1000000;
pop_minor %= 1000000;
population += Growth();
int ind = Industry();
for(int ctr=0; ctr<(int)objs.size(); ++ctr) {
int diff = cur_tree->Upkeep(objs[ctr], oqty[ctr], this);
if(diff > ind) {
--oqty[ctr];
--ctr;
continue;
}
ind -= diff;
}
if(SpareIndustry() > 0 && projs.size() > 0) {
int indus = SpareIndustry(), need, ctr;
Tech *tc;
while(projs.size() > 0 && indus > 0) {
tc = cur_tree->GetTech(projs[0]);
need = tc->icost;
if(prog[0]+indus >= need) {
indus -= need-prog[0];
if(tc->type == TECH_SHIP) {
if(planet) {
Fleet *flt = new Fleet(planet, tc->names);
planet->Sys()->objects.push_back(flt);
flt->AddShip(new Ship(projs[0], owner));
flt->GetShip(0)->AddCrew(flt->GetShip(0)->MaxCrew());
population -= flt->GetShip(0)->MaxCrew();
//flt->location = planet;
}
}
else {
for(ctr=0; ctr < (int)objs.size(); ++ctr) {
if(objs[ctr] == projs[0]) { ++oqty[ctr]; break; }
}
if(ctr == (int)objs.size()) {
objs.push_back(projs[0]);
oqty.push_back(1);
}
}
projs.erase(projs.begin());
prog.erase(prog.begin());
}
else {
prog[0] += indus;
indus = 0;
}
}
}
}
void Colony::LandShip(Ship *s) {
switch(s->Class()) {
case(SCLASS_COLONYSHIP):
case(SCLASS_COLONIZER): {
for(int ctr=0; ctr < int(objs.size()); ++ctr) {
if(objs[ctr] == 0) { // 0 == CITY - FIXME!
++oqty[ctr];
break;
}
}
}break;
case(SCLASS_TRANSPORT): {
}break;
default: {
fprintf(stderr, "How the fuck did you land THAT on a planet?!?\n");
}break;
}
population += s->Crew();
}
void Colony::SaveTo(FILE *f) {
}
void Colony::LoadFrom(FILE *f) {
}