-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcity.cpp
721 lines (638 loc) · 16.7 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "city.h"
#include "rng.h"
#include "world.h"
#include "globals.h"
#include <sstream>
City::City()
{
uid = -1;
type = CITY_TYPE_CITY;
race = RACE_NULL;
for (int i = 0; i < CIT_MAX; i++) {
population[i].type = Citizen_type(i);
}
for (int i = 0; i < RES_MAX; i++) {
resources[i] = 0;
resource_price[i] = Resource_data[i]->value;
}
for (int i = 0; i < MINERAL_MAX; i++) {
minerals[i] = 0;
mineral_price[i] = Mineral_data[i]->value;
}
}
City::~City()
{
}
void City::clear_data()
{
uid = -1;
name = "";
type = CITY_TYPE_CITY;
race = RACE_NULL;
location = Point(-1, -1);
for (int i = 0; i < CIT_MAX; i++) {
population[i].type = Citizen_type(i);
}
for (int i = 0; i < RES_MAX; i++) {
resources[i] = 0;
}
for (int i = 0; i < MINERAL_MAX; i++) {
minerals[i] = 0;
}
livestock.clear();
trade_routes.clear();
// TODO: Clear the map?
}
std::string City::save_data()
{
std::stringstream ret;
ret << uid << " ";
// Since some names are multiple words, we need the ! to mark the end.
ret << name << " ! ";
ret << int(type) << " " << int(race) << std::endl;
ret << location.x << " " << location.y << std::endl;
ret << map.save_data() << std::endl;
ret << trade_routes.size() << std::endl;
for (std::map<int,Trade_route>::iterator it = trade_routes.begin();
it != trade_routes.end();
it++) {
ret << it->first << " " << it->second.save_data() << std::endl;
}
for (int i = 0; i < CIT_MAX; i++) {
ret << population[i].save_data() << std::endl;
}
for (int i = 0; i < RES_MAX; i++) {
ret << resources[i] << " ";
}
ret << std::endl;
for (int i = 0; i < MINERAL_MAX; i++) {
ret << minerals[i] << " ";
}
ret << std::endl;
ret << livestock.size() << " ";
for (std::map<Animal,int>::iterator it = livestock.begin();
it != livestock.end();
it++) {
ret << int(it->first) << " " << it->second << " ";
}
ret << std::endl;
return ret.str();
}
bool City::load_data(std::istream& data)
{
clear_data();
data >> uid;
std::string tmpstr;
while (tmpstr != "!") {
data >> tmpstr;
if (tmpstr != "!") {
if (!name.empty()) {
name = name + " ";
}
name = name + tmpstr;
}
}
int tmptype, tmprace;
data >> tmptype >> tmprace;
if (tmptype >= CITY_TYPE_MAX || tmptype <= 0) {
debugmsg("City '%s' has type value of %d (range is 1 - %d)!",
name.c_str(), tmptype, CITY_TYPE_MAX - 1);
return false;
}
if (tmprace >= RACE_MAX || tmptype <= 0) {
debugmsg("City '%s' has race value of %d (range is 1 - %d)!",
name.c_str(), tmprace, RACE_MAX - 1);
return false;
}
type = City_type(tmptype);
race = Race(tmprace);
data >> location.x >> location.y;
if (!map.load_data(data)) {
debugmsg("City '%s' failed to load map.", name.c_str());
return false;
}
int num_routes;
data >> num_routes;
for (int i = 0; i < num_routes; i++) {
int tmpuid;
Trade_route tmproute;
data >> tmpuid;
if (!tmproute.load_data(data)) {
debugmsg("City '%s' failed to load trade route to City %d.",
name.c_str(), tmpuid);
return false;
}
trade_routes[tmpuid] = tmproute;
}
for (int i = 0; i < CIT_MAX; i++) {
if (!population[i].load_data(data)) {
debugmsg("City '%s' failed to load %s data.",
name.c_str(), citizen_type_name(Citizen_type(i), true).c_str());
return false;
}
}
for (int i = 0; i < RES_MAX; i++) {
data >> resources[i];
}
for (int i = 0; i < MINERAL_MAX; i++) {
data >> minerals[i];
}
int num_animals;
data >> num_animals;
for (int i = 0; i < num_animals; i++) {
int tmpanimal, tmpnum;
data >> tmpanimal >> tmpnum;
if (tmpanimal <= 0 || tmpanimal >= ANIMAL_MAX) {
debugmsg("City '%s' had animal of index %d (Range is 1 to %d)",
name.c_str(), tmpanimal, ANIMAL_MAX - 1);
return false;
}
livestock[ Animal(tmpanimal) ] = tmpnum;
}
return true;
}
void City::start_new_city()
{
Race_datum* race_dat = Race_data[race];
type = CITY_TYPE_CITY;
for (int i = 0; i < CIT_MAX; i++) {
population[i].type = Citizen_type(i);
population[i].add_citizens( race_dat->starting_population[i] );
}
for (int i = 0; i < RES_MAX; i++) {
resources[i] = race_dat->starting_resources[i];
}
for (int i = 0; i < MINERAL_MAX; i++) {
minerals[i] = 0;
}
// We always start with the maximum amount of food, regardless of our race.
resources[RES_FOOD] = get_food_cap();
}
// loc defaults to (-1, -1)
void City::generate_map(Point loc)
{
if (!GAME->world->OOB(loc)) {
location = loc;
}
std::vector<Crop> crops = GAME->world->crops_at (location);
std::vector<Mineral> minerals = GAME->world->minerals_at(location);
std::vector<Animal> animals = GAME->world->animals_at (location);
map.generate( GAME->world->get_map_type(location),
crops, minerals, animals,
GAME->world->coast_from(location),
GAME->world->river_start_for(location),
GAME->world->river_end_for(location)
);
}
void City::do_turn()
{
// TODO: Stick some stuff in here?
}
void City::set_random_name()
{
if (race == RACE_NULL) {
name = "Abandoned City";
} else {
Race_datum* race_dat = Race_data[race];
name = race_dat->get_city_name();
}
}
void City::set_race(Race new_race)
{
race = new_race;
}
void City::set_city_type(City_type new_type)
{
type = new_type;
}
// range defaults to 100, progress_bar defaults to true
void City::setup_trade_routes(int range, bool progress_bar)
{
trade_routes.clear();
for (int i = 0; i < GAME->world->city_list.size(); i++) {
int percent = (100 * i) / GAME->world->city_list.size();
if (progress_bar) {
popup_nowait("Establishing trade routes... [%d%%%%%%%%]", percent);
}
City* target = GAME->world->city_list[i];
// Don't consider cities that are more than <range> tiles away.
if (target != this && manhattan_dist(location, target->location) <= range) {
int dist = GAME->world->get_trade_distance(race, location,
target->location);
int overhd = dist + 500;
if (target->race != race) {
Race_datum* our_race_dat = Race_data[race];
Race_datum* their_race_dat = Race_data[target->race];
int our_relation = our_race_dat->relations[target->race];
int their_relation = their_race_dat->relations[race];
// Positive relations have a smaller/no impact on this penalty.
if (our_relation > 0) {
our_relation /= 2;
}
if (their_relation > 0) {
their_relation /= 2;
}
int penalty = 11 - our_relation - their_relation;
// If our races like each other, there might be no penalty!
if (penalty > 10) {
overhd = (overhd * penalty) / 10;
}
}
overhd /= 100; // Since dist is 100 times the days-of-travel!
if (dist >= 0 && dist <= 5000) { // 50 days' travel
trade_routes[target->uid] = Trade_route(target->location, dist, overhd);
// Bug net
if (trade_routes[target->uid].overhead > 100) {
debugmsg("Trade_route overhead %d (calc'd %d)! %s <=> %s, dist %s",
trade_routes[target->uid].overhead, overhd,
location.str().c_str(), target->location.str().c_str(),
dist);
}
}
}
}
}
int City::get_total_population(Citizen_type type)
{
if (type == CIT_NULL) {
int ret = 0;
for (int i = 0; i < CIT_MAX; i++) {
ret += population[i].count;
}
return ret;
}
return population[type].count;
}
std::string City::get_name()
{
return name;
}
glyph City::get_glyph()
{
glyph ret;
ret.bg = c_black;
int pop = get_total_population();
switch (type) {
case CITY_TYPE_CAPITAL:
ret.symbol = '@';
break;
case CITY_TYPE_DUCHY:
ret.symbol = 'D';
break;
case CITY_TYPE_CITY:
default:
if (pop >= CITY_POPULATION_LARGE) {
ret.symbol = 'O';
} else {
ret.symbol = 'o';
}
break;
}
// Now color us based on our population.
int pop_level = 0;
if (pop >= CITY_POPULATION_LARGE) {
// 100 if we're barely large, 200 if we're double-large, etc
pop_level = (100 * pop) / CITY_POPULATION_LARGE;
} else {
// 100 if we're half-large, 200 if we're nearly large, 0 if we're 0
pop_level = (200 * pop) / CITY_POPULATION_LARGE;
}
if (pop_level >= 200) {
ret.fg = c_white;
} else if (pop_level >= 175) {
ret.fg = c_ltred;
} else if (pop_level >= 150) {
ret.fg = c_yellow;
} else if (pop_level >= 125) {
ret.fg = c_ltgreen;
} else if (pop_level >= 100) {
ret.fg = c_ltblue;
} else if (pop_level >= 75) {
ret.fg = c_cyan;
} else if (pop_level >= 50) {
ret.fg = c_magenta;
} else if (pop_level >= 25) {
ret.fg = c_brown;
} else {
ret.fg = c_ltgray;
}
return ret;
}
City_type City::get_city_type()
{
return type;
}
Race City::get_race()
{
return race;
}
std::vector<Trade_route> City::find_sellers_of(Resource res)
{
std::vector<Trade_route> ret;
for (std::map<int,Trade_route>::iterator it = trade_routes.begin();
it != trade_routes.end();
it++) {
City* seller = GAME->world->lookup_city_uid(it->first);
if (seller) {
int avail = seller->get_net_resource_production(res);
if (avail > 0) {
// Insert into our return vector, sorted by distance.
// TODO: Sort by unit price (including overhead)!
int dist = it->second.distance;
bool found = false;
for (int i = 0; !found && i < ret.size(); i++) {
if (dist <= ret[i].distance) {
found = true;
ret.insert( ret.begin() + i, it->second );
}
}
if (!found) {
ret.push_back(it->second);
}
} // if (avail > 0)
} // if (seller)
} // for (std::map<int,Trade_route>::iterator it = trade_routes.begin(); ... )
return ret;
}
std::vector<Trade_route> City::find_buyers_for(Resource res)
{
std::vector<Trade_route> ret;
for (std::map<int,Trade_route>::iterator it = trade_routes.begin();
it != trade_routes.end();
it++) {
City* buyer = GAME->world->lookup_city_uid(it->first);
if (buyer && buyer->get_daily_demand(res) > 0) {
ret.push_back(it->second);
}
}
return ret;
}
int City::get_price(Resource res)
{
return resource_price[res];
}
int City::get_price(Mineral min)
{
return mineral_price[min];
}
void City::set_all_prices()
{
}
// get_daily_demand() (both versions) are overridden for AI_city and Player_city
// and as such, the City version just returns 0 (it's not used).
int City::get_daily_demand(Resource res)
{
return 0;
}
int City::get_daily_demand(Mineral min)
{
return 0;
}
std::map<Resource,int> City::get_luxuries(Luxury_type type)
{
std::map<Resource,int> ret;
for (int i = 0; i < RES_MAX; i++) {
if (resources[i] > 1 && Resource_data[i]->luxury_type == type) {
ret[ Resource(i) ] = resources[i];
}
}
return ret;
}
// Since generic City doesn't have any Areas or Buildings or imports, we don't
// actually produce resources. Overloaded for Player_city and AI_city.
int City::get_gross_resource_production(Resource res)
{
return 0;
}
int City::get_resource_consumption(Resource res)
{
int ret = 0;
for (int i = CIT_PEASANT; i <= CIT_BURGHER; i++) {
ret += population[i].consumption[res];
if (res == RES_FOOD) { // Special case
ret += get_food_consumption( Citizen_type(i) );
}
}
return ret;
}
int City::get_net_resource_production(Resource res)
{
return get_gross_resource_production(res) - get_resource_consumption(res);
}
int City::get_resource_amount(Resource res)
{
if (res < 0 || res >= RES_MAX) {
return 0;
}
return resources[res];
}
int City::get_mineral_amount(Mineral min)
{
if (min < 0 || min >= MINERAL_MAX) {
return 0;
}
return minerals[min];
}
int City::get_food_cap()
{
int food_expiration = 25; // How many days does it take our food to go bad?
// TODO: Allow modifiers to food_expiration.
return food_expiration * get_food_consumption();
}
// type defaults to CIT_NULL
int City::get_food_consumption(Citizen_type type)
{
// TODO: Modify this based on our race.
if (type == CIT_NULL) {
int ret = 0;
for (int i = 1; i < CIT_MAX; i++) {
int a = get_food_consumption( Citizen_type(i) );
ret += a;
}
return ret;
}
// citizen_food_consumption() is defined in citizen.cpp. It returns the amount
// per 100 citizens, so naturally we need to divide by 100.
int ret = (population[type].count * citizen_food_consumption( type )) / 100;
// Modify based on race - its food_consumption is a percentage.
ret = (ret * Race_data[race]->food_consumption) / 100;
return ret;
}
// amount defaults to 1
bool City::has_resource(Resource res, int amount)
{
return (get_resource_amount(res) >= amount);
}
bool City::has_resource(Resource_amount res)
{
return has_resource(res.type, res.amount);
}
bool City::has_resources(std::vector<Resource_amount> res_used)
{
for (int i = 0; i < res_used.size(); i++) {
if (!has_resource( res_used[i] )) {
return false;
}
}
return true;
}
bool City::has_resources(std::map<Resource,int> res_used)
{
for (std::map<Resource,int>::iterator it = res_used.begin();
it != res_used.end();
it++) {
if (!has_resource(it->first, it->second)) {
return false;
}
}
return true;
}
// amount defaults to 1
bool City::has_mineral(Mineral res, int amount)
{
return (get_mineral_amount(res) >= amount);
}
bool City::has_mineral(Mineral_amount res)
{
return has_mineral(res.type, res.amount);
}
bool City::has_minerals(std::vector<Mineral_amount> min_used)
{
for (int i = 0; i < min_used.size(); i++) {
if (!has_mineral( min_used[i] )) {
return false;
}
}
return true;
}
bool City::has_minerals(std::map<Mineral,int> min_used)
{
for (std::map<Mineral,int>::iterator it = min_used.begin();
it != min_used.end();
it++) {
if (!has_mineral(it->first, it->second)) {
return false;
}
}
return true;
}
bool City::expend_resource(Resource res, int amount)
{
if (!has_resource(res, amount)) {
return false;
}
resources[res] -= amount;
return true;
}
bool City::expend_resource(Resource_amount res)
{
if (!has_resource(res)) {
return false;
}
resources[res.type] -= res.amount;
return true;
}
bool City::expend_resources(std::vector<Resource_amount> res_used)
{
if (!has_resources(res_used)) {
return false;
}
for (int i = 0; i < res_used.size(); i++) {
resources[ res_used[i].type ] -= res_used[i].amount;
}
return true;
}
bool City::expend_resources(std::map<Resource,int> res_used)
{
if (!has_resources(res_used)) {
return false;
}
for (std::map<Resource,int>::iterator it = res_used.begin();
it != res_used.end();
it++) {
resources[it->first] -= it->second;
}
return true;
}
bool City::expend_mineral(Mineral res, int amount)
{
if (!has_mineral(res, amount)) {
return false;
}
minerals[res] -= amount;
return true;
}
bool City::expend_mineral(Mineral_amount res)
{
if (!has_mineral(res)) {
return false;
}
minerals[res.type] -= res.amount;
return true;
}
bool City::expend_minerals(std::vector<Mineral_amount> min_used)
{
if (!has_minerals(min_used)) {
return false;
}
for (int i = 0; i < min_used.size(); i++) {
minerals[ min_used[i].type ] -= min_used[i].amount;
}
return true;
}
bool City::expend_minerals(std::map<Mineral,int> min_used)
{
if (!has_minerals(min_used)) {
return false;
}
for (std::map<Mineral,int>::iterator it = min_used.begin();
it != min_used.end();
it++) {
minerals[it->first] -= it->second;
}
return true;
}
void City::gain_resource(Resource res, int amount)
{
resources[res] += amount;
}
void City::gain_resource(Resource_amount res)
{
gain_resource(res.type, res.amount);
}
void City::gain_resources(std::vector<Resource_amount> res_used)
{
for (int i = 0; i < res_used.size(); i++) {
gain_resource( res_used[i] );
}
}
void City::gain_resources(std::map<Resource,int> res_used)
{
for (std::map<Resource,int>::iterator it = res_used.begin();
it != res_used.end();
it++) {
gain_resource( it->first, it->second );
}
}
void City::gain_mineral(Mineral min, int amount)
{
minerals[min] += amount;
}
void City::gain_mineral(Mineral_amount min)
{
gain_mineral(min.type, min.amount);
}
void City::gain_minerals(std::vector<Mineral_amount> min_used)
{
for (int i = 0; i < min_used.size(); i++) {
gain_mineral( min_used[i] );
}
}
void City::gain_minerals(std::map<Mineral,int> min_used)
{
for (std::map<Mineral,int>::iterator it = min_used.begin();
it != min_used.end();
it++) {
gain_mineral( it->first, it->second );
}
}