forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
city.cpp
504 lines (425 loc) · 12.9 KB
/
city.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "city.hpp"
#include <iostream>
#include <set>
#include "building_data.hpp"
#include "path_finding.hpp"
#include "exception.hpp"
#include "oc3_emigrant.h"
#include "oc3_positioni.h"
City::City()
{
_time = 0;
_month = 0;
_roadEntryI = 0;
_roadEntryJ = 0;
_roadExitI = 0;
_roadExitJ = 0;
_boatEntryI = 0;
_boatEntryJ = 0;
_boatExitI = 0;
_boatExitJ = 0;
_funds = 1000;
_population = 0;
_taxRate = 700;
_climate = C_CENTRAL;
}
void City::timeStep()
{
// CALLED 11 time/second
_time += 1;
if( _time % 22 == 1 )
{
_createImigrants();
}
if( _time % 110 == 1 )
{
// every X seconds
_month++;
monthStep();
}
std::list<Walker*>::iterator walkerIt = _walkerList.begin();
while (walkerIt != _walkerList.end())
{
Walker &walker = **walkerIt;
walker.timeStep(_time);
if (walker.isDeleted())
{
// remove the walker from the walkers list
walkerIt = _walkerList.erase(walkerIt);
}
else
{
++walkerIt;
}
}
std::list<LandOverlay*>::iterator overlayIt = _overlayList.begin();
while (overlayIt != _overlayList.end())
{
LandOverlay &overlay = **overlayIt;
overlay.timeStep(_time);
if (overlay.isDeleted())
{
// remove the overlay from the overlay list
overlayIt = _overlayList.erase(overlayIt);
}
else
{
++overlayIt;
}
}
}
void City::monthStep()
{
collectTaxes();
calculatePopulation();
}
void City::_createImigrants()
{
Uint32 vacantPop=0;
std::list<LandOverlay*> houses = getBuildingList(B_HOUSE);
for( std::list<LandOverlay*>::iterator itHouse = houses.begin(); itHouse != houses.end(); ++itHouse )
{
House* house = dynamic_cast<House*>(*itHouse);
if( house && house->getAccessRoads().size() > 0 )
{
vacantPop += house->getMaxHabitants() - house->getNbHabitants();
}
}
if( vacantPop == 0 )
{
return;
}
std::list<Walker*> walkers = getWalkerList( WT_EMIGRANT );
if( vacantPop <= walkers.size() )
{
return;
}
Tile& roadTile = _tilemap.at( _roadEntryI, _roadEntryJ );
Road* roadEntry = dynamic_cast< Road* >( roadTile.get_terrain().getOverlay() );
if( roadEntry )
{
vacantPop = std::max<Uint32>( 1, rand() % std::max<Uint32>( 1, vacantPop / 2 ) );
Emigrant* ni = Emigrant::create( *roadEntry );
_walkerList.push_back( ni );
}
}
std::list<Walker*>& City::getWalkerList()
{
return _walkerList;
}
std::list<Walker*> City::getWalkerList( const WalkerType type )
{
std::list<Walker*> res;
Walker* walker = 0;
for (std::list<Walker*>::iterator itWalker = _walkerList.begin(); itWalker != _walkerList.end(); ++itWalker )
{
// for each walker
walker = *itWalker;
if( walker != NULL && walker->getType() == type )
{
res.push_back(walker);
}
}
return res;
}
std::list<LandOverlay*>& City::getOverlayList()
{
return _overlayList;
}
unsigned long City::getTime()
{
return _time;
}
std::list<LandOverlay*> City::getBuildingList(const BuildingType buildingType)
{
std::list<LandOverlay*> res;
for (std::list<LandOverlay*>::iterator itOverlay = _overlayList.begin(); itOverlay!=_overlayList.end(); ++itOverlay)
{
// for each overlay
LandOverlay *overlay = *itOverlay;
Construction *construction = dynamic_cast<Construction*>(overlay);
if (construction != NULL && construction->getType() == buildingType)
{
// overlay matches the filter
res.push_back(overlay);
}
}
return res;
}
void City::recomputeRoadsForAll()
{
for (std::list<LandOverlay*>::iterator itOverlay = _overlayList.begin(); itOverlay!=_overlayList.end(); ++itOverlay)
{
// for each overlay
LandOverlay *overlay = *itOverlay;
Construction *construction = dynamic_cast<Construction*>(overlay);
if (construction != NULL)
{
// overlay matches the filter
construction->computeAccessRoads();
// for some constructions we need to update picture
if (construction->getType() == B_ROAD) construction->setPicture(dynamic_cast<Road*>(construction)->computePicture());
}
}
}
Tilemap& City::getTilemap()
{
return _tilemap;
}
int City::getRoadEntryI() const { return _roadEntryI; }
int City::getRoadEntryJ() const { return _roadEntryJ; }
int City::getRoadExitI() const { return _roadExitI; }
int City::getRoadExitJ() const { return _roadExitJ; }
int City::getBoatEntryI() const { return _boatEntryI; }
int City::getBoatEntryJ() const { return _boatEntryJ; }
int City::getBoatExitI() const { return _boatExitI; }
int City::getBoatExitJ() const { return _boatExitJ; }
ClimateType City::getClimate() const
{
return _climate;
}
void City::setClimate(const ClimateType climate)
{
_climate = climate;
}
void City::setRoadEntryIJ(const int i, const int j)
{
_roadEntryI = i;
_roadEntryJ = j;
}
void City::setRoadExitIJ(const int i, const int j)
{
_roadExitI = i;
_roadExitJ = j;
}
void City::setBoatEntryIJ(const int i, const int j)
{
_boatEntryI = i;
_boatEntryJ = j;
}
void City::setBoatExitIJ(const int i, const int j)
{
_boatExitI = i;
_boatExitJ = j;
}
int City::getTaxRate() const
{
return _taxRate;
}
void City::setTaxRate(const int taxRate)
{
_taxRate = taxRate;
}
long City::getFunds() const
{
return _funds;
}
void City::setFunds(const long funds)
{
_funds = funds;
}
long City::getPopulation() const
{
/* here we need to calculate population ??? */
return _population;
}
void City::setPopulation(const long population)
{
_population = population;
}
void City::build(Construction &buildInstance, const int i, const int j)
{
BuildingData &buildingData = BuildingDataHolder::instance().getData(buildInstance.getType());
// make new building
Construction *building = (Construction *) buildInstance.clone();
building->build(i, j);
_overlayList.push_back(building);
_funds -= buildingData.getCost();
}
void City::clearLand(const int i, const int j)
{
Tile& cursorTile = _tilemap.at(i, j);
TerrainTile& terrain = cursorTile.get_terrain();
if (terrain.isDestructible())
{
int size = 1;
int i1 = i;
int j1 = j;
LandOverlay* overlay = terrain.getOverlay();
bool deleteRoad = false;
if (terrain.isRoad()) deleteRoad = true;
if (overlay != NULL)
{
size = overlay->getSize();
i1 = overlay->getTile().getI();
j1 = overlay->getTile().getJ();
overlay->destroy();
_overlayList.remove(overlay);
delete overlay;
}
std::list<Tile*> clearedTiles = _tilemap.getFilledRectangle(i1, j1, i1+size-1, j1+size-1);
for (std::list<Tile*>::iterator itTile = clearedTiles.begin(); itTile!=clearedTiles.end(); ++itTile)
{
Tile &tile = **itTile;
tile.set_master_tile(NULL);
TerrainTile &terrain = tile.get_terrain();
terrain.setTree(false);
terrain.setBuilding(false);
terrain.setRoad(false);
terrain.setGarden(false);
terrain.setOverlay(NULL);
// choose a random landscape picture:
// flat land1a 2-9;
// wheat: land1a 18-29;
// green_something: land1a 62-119; => 58
// green_flat: land1a 232-289; => 58
// choose a random background image, green_something 62-119 or green_flat 232-240
std::string res_pfx = "land1a";
int res_id = 0;
if (rand() % 10 > 6)
{
// 30% => choose green_sth 62-119
res_id = 62 + rand() % (119 - 62 + 1);
}
else
{
// 70% => choose green_flat 232-289
res_id = 232 + rand() % (289 - 232 + 1);
}
tile.set_picture(&PicLoader::instance().get_picture(res_pfx, res_id));
}
// recompute roads;
// there is problem that we NEED to recompute all roads map for all buildings
// because MaxDistance2Road can be any number
if (deleteRoad) recomputeRoadsForAll();
}
}
void City::collectTaxes()
{
long taxes = 0;
std::list<LandOverlay*> houseList = getBuildingList(B_HOUSE);
for (std::list<LandOverlay*>::iterator itHouse = houseList.begin(); itHouse != houseList.end(); ++itHouse)
{
House &house = dynamic_cast<House&>(**itHouse);
taxes += house.collectTaxes();
}
_funds += taxes;
std::cout << "Monthly Taxes=" << taxes << std::endl;
}
void City::calculatePopulation()
{
long pop = 0; /* population can't be negative - should be unsigned long long*/
std::list<LandOverlay*> houseList = getBuildingList(B_HOUSE);
for (std::list<LandOverlay*>::iterator itHouse = houseList.begin(); itHouse != houseList.end(); ++itHouse)
{
//check for error on dyncast
if( House* house = dynamic_cast<House*>(*itHouse) )
{
pop += house->getNbHabitants();
}
}
setPopulation(pop);
}
void City::serialize(OutputSerialStream &stream)
{
// std::cout << "WRITE TILEMAP @" << stream.tell() << std::endl;
getTilemap().serialize(stream);
// std::cout << "WRITE CITY @" << stream.tell() << std::endl;
stream.write_int(_roadEntryI, 2, 0, 1000);
stream.write_int(_roadEntryJ, 2, 0, 1000);
stream.write_int(_roadExitI, 2, 0, 1000);
stream.write_int(_roadExitJ, 2, 0, 1000);
stream.write_int(_boatEntryI, 2, 0, 1000);
stream.write_int(_boatEntryJ, 2, 0, 1000);
stream.write_int(_boatExitI, 2, 0, 1000);
stream.write_int(_boatExitJ, 2, 0, 1000);
stream.write_int((int) _climate, 2, 0, C_MAX);
stream.write_int(_time, 4, 0, 1000000);
stream.write_int(_funds, 4, 0, 1000000);
stream.write_int(_population, 4, 0, 1000000);
// walkers
stream.write_int(_walkerList.size(), 2, 0, 65535);
for (std::list<Walker*>::iterator itWalker = _walkerList.begin(); itWalker != _walkerList.end(); ++itWalker)
{
// std::cout << "WRITE WALKER @" << stream.tell() << std::endl;
Walker &walker = **itWalker;
walker.serialize(stream);
}
// overlays
stream.write_int(_overlayList.size(), 2, 0, 65535);
for (std::list<LandOverlay*>::iterator itOverlay = _overlayList.begin(); itOverlay != _overlayList.end(); ++itOverlay)
{
// std::cout << "WRITE OVERLAY @" << stream.tell() << std::endl;
LandOverlay &overlay = **itOverlay;
overlay.serialize(stream);
}
}
void City::unserialize(InputSerialStream &stream)
{
// std::cout << "READ TILEMAP @" << stream.tell() << std::endl;
_tilemap.unserialize(stream);
// std::cout << "READ CITY @" << stream.tell() << std::endl;
_roadEntryI = stream.read_int(2, 0, 1000);
_roadEntryJ = stream.read_int(2, 0, 1000);
_roadExitI = stream.read_int(2, 0, 1000);
_roadExitJ = stream.read_int(2, 0, 1000);
_boatEntryI = stream.read_int(2, 0, 1000);
_boatEntryJ = stream.read_int(2, 0, 1000);
_boatExitI = stream.read_int(2, 0, 1000);
_boatExitJ = stream.read_int(2, 0, 1000);
_climate = (ClimateType) stream.read_int(2, 0, 1000);
_time = stream.read_int(4, 0, 1000000);
_funds = stream.read_int(4, 0, 1000000);
_population = stream.read_int(4, 0, 1000000);
// walkers
int nbItems = stream.read_int(2, 0, 65535);
for (int i = 0; i < nbItems; ++i)
{
// std::cout << "READ WALKER @" << stream.tell() << std::endl;
Walker &walker = Walker::unserialize_all(stream);
_walkerList.push_back(&walker);
}
// overlays
nbItems = stream.read_int(2, 0, 65535);
for (int i = 0; i < nbItems; ++i)
{
// std::cout << "READ OVERLAY @" << stream.tell() << std::endl;
LandOverlay &overlay = LandOverlay::unserialize_all(stream);
_overlayList.push_back(&overlay);
}
// set all pointers to overlays&walkers
stream.set_dangling_pointers(false); // ignore missing pointers
// finalize the buildings
std::list<LandOverlay*> llo = _overlayList;
for (std::list<LandOverlay*>::iterator itLLO = llo.begin(); itLLO!=llo.end(); ++itLLO)
{
LandOverlay &overlay = **itLLO;
int i = overlay.getTile().getI();
int j = overlay.getTile().getJ();
overlay.build(i, j);
}
}
TilePos City::getRoadEntryIJ() const
{
return TilePos( _roadEntryI, _roadEntryJ );
}
TilePos City::getRoadExitIJ() const
{
return TilePos( _roadExitI, _roadExitJ );
}