-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.cpp
More file actions
351 lines (310 loc) · 9.43 KB
/
tile.cpp
File metadata and controls
351 lines (310 loc) · 9.43 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "tile.hpp"
namespace TerraNova {
Tile::Tile(TileType* type, SDL_Renderer* ren,
const int row, const int colm): GFXObject(ren,
File::SpritePath() / type->Path(), 0, 0),
type(type), row(row), colm(colm) {}
void Tile::Render() const{
SDL_Rect renderLayout = layout;
renderLayout.x += MAPDISP_ORIGIN_X;
renderLayout.y += MAPDISP_ORIGIN_Y;
sprite->RenderTo(ren, renderLayout);
if(bldg) bldg->Render();
for(auto& occ : occupants){
if(!occ){
std::cerr << "Error: attempted to render a non-existent occupant."
<< std::endl;
continue;
}
occ->Render();
}
}
void Tile::StartTurn(){
occupants = CheckAndLock(weakOccupants);
/*if(occupants.size() > 0){
std::cout << "The Tile at (" << row << "," << colm << ") has "
<< occupants.size() << " occupants." << std::endl;
}*/
if (linkedColony /*&& (Faction() == linkedColony->Faction())*/) {
linkedColony->AddResources(this->Income());
}
if (bldg && bldg->TurnsLeft() > 0) {
bldg->BuildTurn();
if (bldg->Finished() && linkedColony) linkedColony->AddBuilding(bldg);
}
}
void Tile::EndTurn(){
occupants.clear();
}
void Tile::MoveTo(int x, int y){
//std::cout << "A Tile has been moved to (" << x << "," << y << ")." << std::endl;
GFXObject::MoveTo(x,y);
if(bldg) bldg->MoveTo(MAPDISP_ORIGIN_X + x + (TILE_WIDTH - BUILDING_WIDTH)/2,
MAPDISP_ORIGIN_Y + y + (4*TILE_HEIGHT/3 - BUILDING_HEIGHT)/2);
for(auto& occ : occupants){
occ->MoveTo(MAPDISP_ORIGIN_X + x + (TILE_WIDTH - PERSON_WIDTH)/2,
MAPDISP_ORIGIN_Y + y + (4*TILE_HEIGHT/3 - PERSON_HEIGHT)/2);
}
}
// this takes an entire SDL_Rect but only uses the positions, not the sizes
void Tile::MoveTo(SDL_Rect newLayout){
MoveTo(newLayout.x, newLayout.y);
}
void Tile::Resize(int w, int h){
if(bldg) bldg->Resize(bldg->W()*w/this->W(), bldg->H()*h/this->H());
for(auto& occ : occupants){
occ->Resize(occ->W()*w/this->W(), occ->H()*h/this->H());
}
GFXObject::Resize(w,h);
}
// this takes an entire SDL_Rect but only uses the sizes, not the positions
void Tile::Resize(SDL_Rect newLayout){
Resize(newLayout.w, newLayout.h);
}
bool Tile::InsideQ(const int x, const int y){
/*std::cout << "Testing to see if a click at (" << x << "," << y << ") is "
<< "inside of a Tile at (" << layout.x << "," << layout.y << ")." << std::endl;*/
int relX = x - layout.x - MAPDISP_ORIGIN_X;
int relY = y - layout.y - MAPDISP_ORIGIN_Y;
if(relX < 0 || relY < 0 || relX > layout.w || relY > layout.h ||
(4*relY < layout.h &&
((2*relX < layout.w - 3.464*relY) ||
(2*relX > layout.w + 3.464*relY))) ||
(4*relY > 3*layout.h &&
((4*relX < 1.732*(4*relY - 3*layout.h)) ||
(4*relX > 4*layout.w - 1.732*(4*relY - 3*layout.h)))))
return false;
return true;
}
GFXObject* Tile::SelectAt(const int x, const int y) {
if (bldg && bldg->InsideQ(x, y)) return bldg.get();
if (InsideQ(x, y)) return this;
return nullptr;
}
bool Tile::Click() {
/*if(HasColony()){
//EnterColony(linkedColony);
return true;
}
return false;*/
return HasColony();
}
TileType* Tile::Type() const{
return type;
}
void Tile::SetTileType(TileType* newType){
ChangeSprite(File::SpritePath() / newType->Path());
type = newType;
}
std::array<int, LAST_RESOURCE> Tile::Income() const{
std::array<int, LAST_RESOURCE> inc = {{0}};
if(bldg && !bldg->CanHarvest()) return inc;
bool hasHarvester = false;
for(auto& occ : occupants){
if(occ->Orders() == ORDER_HARVEST) hasHarvester = true;
}
if(bldg && bldg->Finished() && bldg->Automatic()) hasHarvester = true;
if(!hasHarvester) return inc;
inc = type->Yield();
//std::cout << "Yield[0]: " << type->Yield()[0] << std::endl;
if(bldg){
for(unsigned int i = 0; i < inc.size(); ++i)
inc[i] += bldg->BonusResources()[i];
}
return inc;
}
bool Tile::HasColony() const{
return hasColony;
}
Colony* Tile::LinkedColony() const{
return linkedColony;
}
void Tile::SetColony(Colony* newColony){
if(newColony){
RemoveBuilding();
//selectable = true;
ChangeSprite(File::SpritePath() /
("terrain/colony_p" + std::to_string(newColony->Faction())) );
newColony->SetRow(this->Row());
newColony->SetColumn(this->Colm());
LinkColony(newColony);
hasColony = true;
} else {
ChangeSprite(File::SpritePath() / type->Path());
LinkColony(nullptr);
hasColony = false;
}
}
void Tile::LinkColony(Colony* colonyToLink){
linkedColony = colonyToLink;
}
void Tile::SetLocation(const int row, const int colm){
this->row = row;
this->colm = colm;
}
int Tile::Row() const{
return row;
}
int Tile::Colm() const{
return colm;
}
void Tile::AddBuilding(std::shared_ptr<Building> newBldg) {
bldg = newBldg;
SDL_Rect bldgLayout = layout;
bldgLayout.x += MAPDISP_ORIGIN_X + (TILE_WIDTH - BUILDING_WIDTH)/2;
bldgLayout.y += MAPDISP_ORIGIN_Y + (4*TILE_HEIGHT/3 - BUILDING_HEIGHT)/2;
bldg->MoveTo(bldgLayout);
}
void Tile::AddBuilding(const BuildingType* type, const char faction) {
auto newBuilding = std::make_shared<Building>(ren, 0, 0, type);
newBuilding->SetFaction(faction);
AddBuilding(newBuilding);
}
void Tile::RemoveBuilding(){
bldg.reset();
}
// with an empty argument or no argument, this returns whether tile has ANY
// building or not. With an argument, it returns whether tile has THAT building
bool Tile::HasBuilding(const std::string& buildingName) const {
if (buildingName.empty()) return bldg != nullptr;
return bldg != nullptr && bldg->Name() == buildingName && bldg->PoweredOn();
}
bool Tile::AddOccupant(std::shared_ptr<Unit> newOccupant){
if(!newOccupant){
std::cerr << "Error: attempted to add a blank occupant to a Tile."
<< std::endl;
return false;
}
if((bldg && bldg->MaxOccupants() <= NumberOfOccupants()) ||
(!bldg && NumberOfOccupants() > 0)) return false;
for(auto& oldOccupant : occupants){
if(oldOccupant == newOccupant.get()){
std::cerr << "Error: attempted to add an occupant to a Tile who was"
<< " already occupying it." << std::endl;
return false;
}
}
if(bldg && !bldg->CanTrain().empty()){
if(!newOccupant->CanRespec()){
std::cout << "This character is unique and can not be retrained."
<< std::endl;
return false;
} else {
bldg->StartTraining(bldg->CanTrain()[0]);
}
}
weakOccupants.push_back(newOccupant);
occupants.push_back(newOccupant.get());
/*newOccupant->MoveTo(MAPDISP_ORIGIN_X + layout.x + (TILE_WIDTH - PERSON_WIDTH)/2,
MAPDISP_ORIGIN_Y + layout.y + (4*TILE_HEIGHT/3 - PERSON_HEIGHT)/2);*/
return true;
}
bool Tile::RemoveOccupant(Unit* removeThis){
/*std::cout << "Removing an occupant from the Tile at (" << row << "," << colm
<< "), which has " << occupants.size() << " occupants." << std::endl;*/
if(!removeThis){
std::cerr << "Error: attempted to remove a blank occupant from a Tile."
<< std::endl;
return false;
}
// doing this instead of remove_if so that we can error if they're not found
for(unsigned int i = occupants.size()-1; i <= occupants.size(); --i){
if(occupants[i] == removeThis){
occupants.erase(occupants.begin()+i);
weakOccupants.erase(weakOccupants.begin()+i);
if(bldg && occupants.empty()) bldg->FinishTraining();
return true;
}
}
std::cerr << "Error: attempted to remove an occupant from the Tile at ("
<< row << "," << colm << "), but they were not found among its "
<< occupants.size() << " occupant(s)." << std::endl;
return false;
}
std::vector<Unit*> Tile::Occupants() const{
return occupants;
}
std::shared_ptr<Unit> Tile::SharedOccupant(const unsigned int i) const {
if(i >= NumberOfOccupants()){
std::cerr << "Error: someone asked to take ownership of a tile's "
<< "occupant #" << i << ", but it only has " << NumberOfOccupants()
<< "." << std::endl;
return nullptr;
}
return weakOccupants[i].lock();
}
unsigned int Tile::NumberOfLivingOccupants() const{
unsigned int ret = 0u;
for(auto& occ : occupants) if(!occ->Dead()) ++ret;
return ret;
}
// eventually we want this to return the strongest occupant, not the first one
Unit* Tile::Defender() const{
if(NumberOfOccupants() == 0) return nullptr;
return Occupants()[0];
}
int Tile::Faction() const{
if(NumberOfOccupants() == 0) return 0;
return Occupants()[0]->Faction();
}
void Tile::Training(){
if(bldg && bldg->NowTraining()){
bldg->TrainingTurn();
if(bldg->TurnsToTrain() == 0){
if(occupants.empty()){
std::cerr << "Error: training was completed at a Building but "
<< "there were no occupants to receive it." << std::endl;
return;
} else {
if(!occupants[0]->CanRespec()){
std::cerr << "Error: training was completed at a Building "
<< "but the occupant was unique and could not be "
<< "retrained. This should not happen." << std::endl;
} else {
occupants[0]->ChangeSpec(bldg->NowTraining());
}
bldg->FinishTraining();
}
}
}
}
unsigned int Tile::MoveCost(const Tile& destination,
const MoveCostTable moveCosts){
//std::cout << destination.Name() << " move cost:";
unsigned int cost = moveCosts.base;
if(destination.Wooded()){
if(moveCosts.wooded < 0){
//std::cout << -1u << std::endl;
return -1u;
} else {
cost += moveCosts.wooded;
}
}
if(destination.Aquatic()){
if(moveCosts.aquatic < 0){
//std::cout << -1u << std::endl;
return -1u;
} else {
cost += moveCosts.aquatic;
}
}
if(destination.Cold()){
if(moveCosts.cold < 0){
//std::cout << -1u << std::endl;
return -1u;
} else {
cost += moveCosts.cold;
}
}
if(destination.Hilly()){
if(moveCosts.hilly < 0){
//std::cout << -1u << std::endl;
return -1u;
} else {
cost += moveCosts.hilly;
}
}
//std::cout << cost << std::endl;
return cost;
}
} // namespace TerraNova