forked from Whales/edigotia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_city.cpp
2345 lines (2067 loc) · 64.8 KB
/
player_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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "player_city.h"
#include "cuss.h"
#include "window.h"
#include "building.h"
#include "stringfunc.h"
#include "geometry.h"
#include "rng.h"
#include "combat.h" // For hunting
#include "globals.h"
#include "kingdom.h"
#include <sstream>
#include <vector>
#include <map>
std::string Message::save_data()
{
std::stringstream ret;
ret << int(type) << " ";
ret << date.save_data() << std::endl;
// Since text is probably multi-word, we use ! as a terminator.
ret << text << " ! ";
return ret.str();
}
bool Message::load_data(std::istream& data)
{
int tmptype;
data >> tmptype;
if (tmptype <= 0 || tmptype >= MESSAGE_MAX) {
debugmsg("Message loaded type %d (range is 1 to %d).",
tmptype, MESSAGE_MAX - 1);
return false;
}
type = Message_type(tmptype);
if (!date.load_data(data)) {
debugmsg("Message failed to load its date.");
return false;
}
std::string word;
while (word != "!") {
data >> word;
if (word != "!") {
if (!text.empty()) {
text = text + " ";
}
text = text + word;
}
}
return true;
}
Player_city::Player_city()
{
type = CITY_TYPE_CITY;
race = RACE_NULL;
for (int i = 0; i < CIT_MAX; i++) {
population[i].type = Citizen_type(i);
tax_rate[i] = 0;
}
birth_points = 0;
for (int i = 0; i < RES_MAX; i++) {
resources[i] = 0;
}
for (int i = 0; i < MINERAL_MAX; i++) {
minerals[i] = 0;
}
for (int i = 0; i < ANIMAL_MAX; i++) {
hunting_action[i] = Animal_data[i]->default_action;
}
for (int i = 0; i < AREA_MAX; i++) {
Area_datum* area_dat = Area_data[i];
// If the area is NOT unlockable, that means it starts out unlocked!
area_unlocked[i] = !(area_dat->unlockable);
}
for (int i = 0; i < BUILD_MAX; i++) {
Building_datum* build_dat = Building_data[i];
// If the building is NOT unlockable, that means it starts out unlocked!
building_unlocked[i] = !(build_dat->unlockable);
}
radius = 1;
unread_messages = 0;
show_hunting_messages = true;
show_livestock_messages = true;
hunt_record_days = -1; // We set it to 0 when we build our first camp
hunt_record_food = 0;
}
Player_city::~Player_city()
{
}
std::string Player_city::save_data()
{
std::stringstream ret;
ret << City::save_data() << std::endl;
for (int i = 0; i < CIT_MAX; i++) {
ret << tax_rate[i] << " ";
}
ret << std::endl;
ret << units_stationed.size() << " ";
for (int i = 0; i < units_stationed.size(); i++) {
ret << units_stationed[i].save_data() << " ";
}
ret << std::endl;
ret << radius << std::endl;
for (int i = 0; i < AREA_MAX; i++) {
ret << area_unlocked[i] << " ";
}
ret << std::endl;
for (int i = 0; i < BUILD_MAX; i++) {
ret << building_unlocked[i] << " ";
}
ret << std::endl;
ret << buildings.size() << " ";
for (int i = 0; i < buildings.size(); i++) {
ret << buildings[i].save_data() << " ";
}
ret << std::endl;
ret << building_queue.size() << " ";
for (int i = 0; i < building_queue.size(); i++) {
ret << building_queue[i].save_data() << " ";
}
ret << std::endl;
ret << areas.size() << " ";
for (int i = 0; i < areas.size(); i++) {
ret << areas[i].save_data() << " ";
}
ret << std::endl;
ret << area_queue.size() << " ";
for (int i = 0; i < area_queue.size(); i++) {
ret << area_queue[i].save_data() << " ";
}
ret << std::endl;
ret << hunt_record_days << " ";
ret << hunt_record_food << " ";
ret << hunt_kills.size() << " ";
for (std::map<Animal,int>::iterator it = hunt_kills.begin();
it != hunt_kills.end();
it++) {
ret << it->first << " " << it->second << " ";
}
ret << std::endl;
ret << unread_messages << " ";
ret << show_hunting_messages << " ";
ret << show_livestock_messages << " ";
ret << messages.size() << " ";
for (int i = 0; i < messages.size(); i++) {
ret << messages[i].save_data() << std::endl;
}
ret << std::endl;
ret << world_seen.save_data() << std::endl;
ret << birth_points << std::endl;
for (int i = 0; i < ANIMAL_MAX; i++) {
ret << hunting_action[i] << " ";
}
return ret.str();
}
bool Player_city::load_data(std::istream& data)
{
if (!City::load_data(data)) {
debugmsg("Player_city::load_data() failed when calling City::load_data().");
return false;
}
for (int i = 0; i < CIT_MAX; i++) {
data >> tax_rate[i];
}
int num_units;
data >> num_units;
for (int i = 0; i < num_units; i++) {
Military_unit tmp_unit;
if (!tmp_unit.load_data(data)) {
debugmsg("Player_city failed to load Military_unit %d/%d.",
i, num_units);
return false;
}
units_stationed.push_back(tmp_unit);
}
data >> radius;
for (int i = 0; i < AREA_MAX; i++) {
data >> area_unlocked[i];
}
for (int i = 0; i < BUILD_MAX; i++) {
data >> building_unlocked[i];
}
int num_buildings;
data >> num_buildings;
for (int i = 0; i < num_buildings; i++) {
Building tmp_build;
if (!tmp_build.load_data(data)) {
debugmsg("Player_city failed to load building %d/%d.", i, num_buildings);
return false;
}
buildings.push_back(tmp_build);
}
int num_bldg_queue;
data >> num_bldg_queue;
for (int i = 0; i < num_bldg_queue; i++) {
Building tmp_build;
if (!tmp_build.load_data(data)) {
debugmsg("Player_city failed to load queued building %d/%d.",
i, num_buildings);
return false;
}
building_queue.push_back(tmp_build);
}
int num_areas;
data >> num_areas;
for (int i = 0; i < num_areas; i++) {
Area tmp_area;
if (!tmp_area.load_data(data)) {
debugmsg("Player_city failed to load area %d/%d.", i, num_areas);
return false;
}
areas.push_back(tmp_area);
}
int num_area_queue;
data >> num_area_queue;
for (int i = 0; i < num_area_queue; i++) {
Area tmp_area;
if (!tmp_area.load_data(data)) {
debugmsg("Player_city failed to load area %d/%d.", i, num_areas);
return false;
}
areas.push_back(tmp_area);
}
int num_kills;
data >> hunt_record_days >> hunt_record_food >> num_kills;
for (int i = 0; i < num_kills; i++) {
int tmpani, tmpnum;
data >> tmpani >> tmpnum;
if (tmpani <= 0 || tmpani >= ANIMAL_MAX) {
debugmsg("Player_city loaded hunt_kills on animal %d (range is 1 to %d).",
tmpani, ANIMAL_MAX - 1);
return false;
}
hunt_kills[ Animal(tmpani) ] = tmpnum;
}
int num_messages;
data >> unread_messages >> show_hunting_messages >> show_livestock_messages >>
num_messages;
for (int i = 0; i < num_messages; i++) {
Message tmpmes;
if (!tmpmes.load_data(data)) {
debugmsg("Player_city failed to load message %d/%d.", i, num_messages);
return false;
}
messages.push_back(tmpmes);
}
if (!world_seen.load_data(data)) {
debugmsg("Player_city failed to load Map_seen.");
return false;
}
data >> birth_points;
for (int i = 0; i < ANIMAL_MAX; i++) {
int tmpact;
data >> tmpact;
if (tmpact <= 0 || tmpact >= ANIMAL_ACT_MAX) {
debugmsg("Player_city loaded hunting_act %d/%d; %d (range is 1 to %d).",
i, ANIMAL_MAX, tmpact, ANIMAL_ACT_MAX - 1);
return false;
}
hunting_action[i] = Animal_action(tmpact);
}
return true;
}
bool Player_city::place_keep()
{
Window w_map(0, 0, 40, 24);
cuss::interface i_map;
if (!i_map.load_from_file("cuss/city_map.cuss")) {
return false;
}
i_map.set_data("text_info", "Start town here?\n<c=magenta>Y/N<c=/>");
do {
draw_map(i_map.find_by_name("draw_map"));
i_map.set_data("draw_map", glyph('@', c_yellow, c_black),
CITY_MAP_SIZE / 2, CITY_MAP_SIZE / 2);
i_map.draw(&w_map);
w_map.refresh();
long ch = getch();
if (ch == 'Y' || ch == 'y') {
Area keep(AREA_KEEP, Point(CITY_MAP_SIZE / 2, CITY_MAP_SIZE / 2));;
areas.push_back(keep);
return true;
} else if (ch == 'n' || ch == 'N') {
return false;
}
} while (true);
return false;
}
void Player_city::pick_race()
{
// A list of options.
std::vector<std::string> options;
for (int i = 1; i < RACE_MAX; i++) {
options.push_back( Race_data[i]->plural_name );
}
int race_index = 1 + menu_vec("Pick race:", options);
race = Race(race_index);
}
void Player_city::set_name()
{
cuss::interface i_name;
if (!i_name.load_from_file("cuss/set_name.cuss")) {
name = "ERRORTOWN";
return;
}
Window w_name;
i_name.select("entry_name");
i_name.ref_data("entry_name", &name);
bool done = false;
while (!done) {
i_name.draw(&w_name);
w_name.refresh();
long ch = input();
if (ch == '!') {
set_random_name();
} else if (ch == '\n') {
done = true;
} else {
i_name.handle_keypress(ch);
}
}
}
void Player_city::start_new_city()
{
City::start_new_city();
Race_datum* race_dat = Race_data[race];
for (int i = 0; i < CIT_MAX; i++) {
if (i >= CIT_PEASANT && i <= CIT_BURGHER) {
set_tax_rate( Citizen_type(i), race_dat->high_tax_rate[i] - 5 );
} else {
tax_rate[i] = 0;
}
}
birth_points = 0;
radius = 1;
}
void Player_city::set_starting_tiles_seen()
{
Kingdom* kingdom = GAME->get_kingdom_for_race(race);
if (!kingdom) {
debugmsg("No kingdom found for Player_city::set_starting_tiles_seen().");
return;
}
// TODO: Make the bonus area vary by some property of our race?
// TODO: Round the corners of the square this produces.
int bonus = 4;
/*
for (int x = kingdom->most_west - bonus;
x <= kingdom->most_east + bonus;
x++) {
for (int y = kingdom->most_north - bonus;
y <= kingdom->most_south + bonus;
y++) {
world_seen.mark_seen(x, y);
}
}
*/
for (int x = kingdom->most_west; x <= kingdom->most_east; x++) {
for (int y = kingdom->most_north; y <= kingdom->most_south; y++) {
int id = GAME->world->get_kingdom_id(x, y);
if (id == kingdom->uid) {
for (int mx = x - bonus; mx <= x + bonus; mx++) {
for (int my = y - bonus; my <= y + bonus; my++) {
world_seen.mark_seen(mx, my);
}
}
}
}
}
// Go further afield, but only for oceans!
bonus = bonus * 10;
for (int x = kingdom->most_west - bonus;
x <= kingdom->most_east + bonus;
x++) {
for (int y = kingdom->most_north - bonus;
y <= kingdom->most_south + bonus;
y++) {
Map_type mt = GAME->world->get_map_type(x, y);
if (mt == MAP_OCEAN || mt == MAP_ICECAP) {
for (int mx = x - 1; mx <= x + 1; mx++) {
for (int my = y - 1; my <= y + 1; my++) {
world_seen.mark_seen(mx, my);
}
}
}
}
}
}
void Player_city::mark_nearby_tiles_seen(int range)
{
for (int x = location.x - range; x <= location.x + range; x++) {
for (int y = location.y - range; y <= location.y + range; y++) {
world_seen.mark_seen(x, y);
}
}
}
glyph Player_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;
}
// radius_limited and only_terrain default to false.
void Player_city::draw_map(cuss::element* e_draw, Point sel, bool radius_limited,
bool only_terrain)
{
if (!e_draw) {
debugmsg("Player_city::draw_map() called with NULL drawing area.");
return;
}
std::map<Point,glyph,Pointcomp> drawing;
// Draw any constructed areas
if (!only_terrain) {
for (int i = 0; i < areas.size(); i++) {
Area* area = &(areas[i]);
Area_datum* areadata = Area_data[area->type];
glyph sym = areadata->symbol;
if (area->pos == sel) {
sym.bg = c_blue;
} else if (!area->is_open()) {
sym.bg = c_ltgray;
}
drawing[area->pos] = sym;
}
// Draw any enqueued areas
for (int i = 0; i < area_queue.size(); i++) {
Area* area = &(area_queue[i]);
Area_datum* areadata = Area_data[area->type];
glyph gl = areadata->symbol;
if (area->pos == sel) {
gl.bg = c_blue;
} else {
gl.bg = c_brown;
}
drawing[area->pos] = gl;
}
}
// Now for any "unclaimed" points, pull the glyph from our map
for (int x = 0; x < CITY_MAP_SIZE; x++) {
for (int y = 0; y < CITY_MAP_SIZE; y++) {
Point pos(x, y);
if (drawing.count(pos) == 0) {
glyph gl = map.get_glyph(pos);
if (radius_limited && !inside_radius(pos)) {
gl.fg = c_dkgray;
}
if (pos == sel) {
gl.bg = c_blue;
}
drawing[pos] = gl;
}
}
}
// Finally, draw the glyphs to e_draw.
for (int x = 0; x < CITY_MAP_SIZE; x++) {
for (int y = 0; y < CITY_MAP_SIZE; y++) {
Point pos(x, y);
if (drawing.count(pos) == 0) {
debugmsg("ERROR - hole in city drawing at %s!", pos.str().c_str());
e_draw->set_data(glyph(), x, y);
} else {
e_draw->set_data(drawing[pos], x, y);
}
}
}
}
void Player_city::do_turn()
{
// Reduce morale modifiers for all citizens, and consume luxuries.
for (int i = CIT_PEASANT; i <= CIT_BURGHER; i++) {
population[i].decrease_morale_mods();
}
// Birth a new citizen(s)?
birth_points += get_daily_birth_points();
int births = 0;
while (birth_points >= 100) {
birth_points -= 100;
births++;
}
birth_citizens(births); // Handles the message, too
// Import resources.
for (int i = 1; i < RES_MAX; i++) {
Resource import = Resource(i);
resources[import] += get_import(import);
}
// Receive taxes.
resources[RES_GOLD] += get_taxes();
// Check for starvation.
for (int i = 1; i < CIT_MAX; i++) {
int starve_chance = population[i].get_starvation_chance();
int dead = 0;
if (starve_chance >= 100) {
dead = population[i].count; // Game over, man!
} else if (starve_chance > 0) {
for (int n = 0; n < population[i].count; n++) {
if (rng(1, 100) <= starve_chance) {
dead++;
}
}
}
// Lose morale, even if no one died.
int morale_penalty = (0 - starve_chance) / 10 - 3 * dead;
population[i].add_morale_modifier(MORALE_MOD_HUNGER, morale_penalty);
kill_citizens( Citizen_type(i), dead, DEATH_STARVATION );
} // for (int i = 0; i < CIT_MAX; i++)
// Handle livestock.
handle_livestock();
// Handle areas & buildings: do_production() does innate resource production,
// manufacturing, farming, mining, and logging. We also call do_hunt() for
// hunting camps and discover_minerals() for mines.
for (int i = 0; i < areas.size(); i++) {
Building* build = &(areas[i].building);
Building_datum* build_dat = build->get_building_datum();
build->do_production(this);
if (build_dat->base_morale > 0) {
// Give all our citizens its morale
for (int n = CIT_PEASANT; n <= CIT_BURGHER; n++) {
population[n].add_morale_modifier(MORALE_MOD_BUILDING,
build_dat->base_morale * 10,
build->get_name());
}
}
if (build->produces_resource(RES_HUNTING)) {
do_hunt( &(areas[i]) );
}
if (build->produces_resource(RES_MINING)) {
build->discover_minerals(this);
}
}
for (int i = 0; i < buildings.size(); i++) {
Building_datum* build_dat = buildings[i].get_building_datum();
buildings[i].do_production(this);
if (build_dat->base_morale > 0) {
// Give all our citizens its morale
for (int n = CIT_PEASANT; n <= CIT_BURGHER; n++) {
population[n].add_morale_modifier(MORALE_MOD_BUILDING,
build_dat->base_morale * 10,
buildings[i].get_name());
}
}
}
// Increment hunt_record_days.
if (hunt_record_days >= 0) {
hunt_record_days++;
}
// Consume food
feed_citizens();
// Pay wages.
int wages = get_total_wages();
if (!expend_resource(RES_GOLD, wages)) {
// TODO: Consequences for failure to pay wages!
resources[RES_GOLD] = 0;
}
// Lose gold to corruption.
int corruption = get_corruption_amount();
if (!expend_resource(RES_GOLD, corruption)) {
// TODO: Consequences for failure to pay corruption?
resources[RES_GOLD] = 0;
}
// We total maintenance into a single pool because we'll need to divide the gold
// by 10, and we want to lose as little to rounding as possible.
std::map<Resource,int> total_maintenance;
// Deduct maintenance for all areas
for (int i = 0; i < areas.size(); i++) {
if (areas[i].is_open()) {
std::map<Resource,int> maintenance = areas[i].get_maintenance();
for (std::map<Resource,int>::iterator it = maintenance.begin();
it != maintenance.end();
it++) {
total_maintenance[it->first] += it->second;
}
}
}
// Deduct maintenance for all buildings
for (int i = 0; i < buildings.size(); i++) {
if (buildings[i].open) {
std::map<Resource,int> maintenance = buildings[i].get_maintenance();
for (std::map<Resource,int>::iterator it = maintenance.begin();
it != maintenance.end();
it++) {
total_maintenance[it->first] += it->second;
}
}
}
if (total_maintenance.count(RES_GOLD)) {
total_maintenance[RES_GOLD] /= 10;
}
if (!expend_resources(total_maintenance)) {
// TODO: Close some areas until we CAN pay this.
}
// Let citizens consume luxuries.
for (int i = CIT_PEASANT; i <= CIT_BURGHER; i++) {
population[i].consume_luxuries(this);
}
// The last resource transaction we should do is exporting resources, since it's
// presumably the one with the least consequences.
for (int i = 1; i < RES_MAX; i++) {
Resource res_export = Resource(i);
int export_amt = get_export(res_export);
if (resources[res_export] >= export_amt) {
resources[res_export] -= export_amt;
} else {
// TODO: consequences for failure to export
}
}
// Make sure our food isn't above the cap (food goes bad y'know)
int food_cap = get_food_cap();
if (resources[RES_FOOD] > food_cap) {
resources[RES_FOOD] = food_cap;
}
// Advance progress on the first area in our queue.
if (!area_queue.empty()) {
Area* area_to_build = &(area_queue[0]);
area_to_build->building.construction_left--;
if (area_to_build->building.construction_left <= 0) {
add_message(MESSAGE_MINOR, "Our %s has finished construction.",
area_queue[0].get_name().c_str());
add_open_area(area_queue[0]);
area_queue.erase( area_queue.begin() );
}
}
// Advance progress on the first building in our queue.
if (!building_queue.empty()) {
Building* building_to_build = &(building_queue[0]);
building_to_build->construction_left--;
if (building_to_build->construction_left <= 0) {
add_message(MESSAGE_MINOR, "Our %s has finished construction.",
building_queue[0].get_name().c_str());
add_open_building(building_queue[0]);
building_queue.erase( building_queue.begin() );
}
}
// Check if we've unlocked any new areas, buildings, etc.
check_unlockables();
}
void Player_city::handle_livestock()
{
for (std::map<Animal,int>::iterator it = livestock.begin();
it != livestock.end();
it++) {
int skill = Race_data[race]->skill_level[SKILL_LIVESTOCK];
Animal animal = it->first;
Animal_datum* animal_dat = Animal_data[animal];
int animal_amount = it->second;
// Now we make any daily food gains (e.g. milk, eggs, etc)
int food_made = animal_dat->food_livestock;
if (food_made > 0) {
// food_livestock is per 100 animals!
int food_gained = (animal_amount * food_made) / 100;
// Round randomly.
if (rng(1, 100) >= (animal_amount * food_made) % 100) {
food_gained++;
}
// Adjust based on our livestock skill.
food_gained = (food_gained * (5 + skill)) / 8;
gain_resource(RES_FOOD, food_gained);
}
// Other daily resource gains.
for (int i = 0; i < animal_dat->resources_livestock.size(); i++) {
Resource_amount res = animal_dat->resources_livestock[i];
int orig_amount = res.amount; // For rounding, below.
// Again, the amount is per 100 animals.
res.amount = (animal_amount * res.amount) / 100;
// Round randomly.
if (rng(1, 100) >= (animal_amount * orig_amount) % 100) {
res.amount++;
}
// Adjust based on our livestock skill.
res.amount = (res.amount * (7 + skill)) / 10;
gain_resource(res);
}
// Do any die randomly?
int num_died = 0;
for (int i = 0; i < animal_amount; i++) {
int hardiness = animal_dat->hardiness;
// Adjust based on our livestock skill.
hardiness = (hardiness * (7 + skill)) / 10;
if (one_in(hardiness)) {
num_died++;
}
}
if (num_died > 0) {
it->second -= num_died;
if (it->second < 0) { // Shouldn't happen but let's be safe
it->second = 0;
}
animal_amount = it->second;
// At least we get to slaughter them for food!
int food_gain = num_died * animal_dat->food_killed;
kill_animals(animal, num_died);
// Add a message about it.
if (show_livestock_messages) {
std::stringstream ss_mes;
ss_mes << num_died << " ";
if (num_died == 1) {
ss_mes << animal_dat->name << " has died naturally. It was ";
} else {
ss_mes << animal_dat->name_plural << " have died naturally. They " <<
"were ";
}
ss_mes << "butchered for " << food_gain << " food.";
add_message(MESSAGE_MINOR, ss_mes.str());
}
}
// Finally, check to see if any were born.
int num_born = 0;
for (int i = 0; i < animal_amount; i++) {
int repro_chance = animal_dat->reproduction_rate;
// Adjust based on our livestock skill.
repro_chance = (repro_chance * 10) / (7 + skill);
if (one_in(repro_chance)) {
num_born++;
}
}
if (num_born > 0) {
int size = animal_dat->size;
int new_total = get_livestock_total() + num_born * size;
int capacity = get_livestock_capacity();
int overflow = 0;
// num_born is for the message, real_num_born is added to the population
// (it may change if we overflow our capacity)
int real_num_born = num_born;
if (new_total >= capacity) {
// Oh no, we're over capacity!
// Subtracting 1 means that if new_total == capacity we round down to 0.
// Then we add 1, so we're always rounding up.
overflow = 1 + (new_total - capacity - 1) / size;
// Kill the overflow rather than just setting them loose.
kill_animals(animal, overflow);
real_num_born -= overflow;
}
// Add a message.
if (show_livestock_messages) {
std::stringstream ss_mes;
ss_mes << num_born << " ";
if (num_born == 1) {
ss_mes << animal_dat->name << " was born!";
} else {
ss_mes << animal_dat->name_plural << " were born!";
}
add_message(MESSAGE_MINOR, ss_mes.str());
if (overflow > 0) {
std::stringstream ss_overflow;
ss_overflow << overflow << " ";
if (overflow == 1) {
ss_overflow << animal_dat->name;
} else {
ss_overflow << animal_dat->name_plural;
}
ss_overflow << " had to be slaughtered to make room.",
add_message(MESSAGE_MAJOR, ss_overflow.str());
}
}
if (real_num_born > 0) {
it->second += real_num_born;
}
} // if (num_born > 0)
} // for (std::map<Animal,int>::iterator it = livestock.begin(); .....
}
void Player_city::feed_citizens()
{
int food_consumed = get_food_consumption();
if (resources[RES_FOOD] >= food_consumed) {
resources[RES_FOOD] -= food_consumed;
// Everyone eats! Reduce starvation.
for (int i = 0; i < CIT_MAX; i++) {
if (population[i].starvation <= 5) {
population[i].starvation = 0;
} else {
population[i].starvation /= 2;
}
}
} else {
// We can't feed everyone! So figure out who we can feed.
// The upper classes get to eat first.
// TODO: Allow a law which changes this?
for (int i = CIT_MAX - 1; i > CIT_NULL; i--) {
Citizen_type cit_type = Citizen_type(i);
int type_consumption = get_food_consumption(cit_type);
int* ptr_starvation = &(population[i].starvation);
if (resources[RES_FOOD] >= type_consumption) {
resources[RES_FOOD] -= type_consumption;
// We ate, hooray! Reduce starvation.
if (*ptr_starvation <= 5) {
*ptr_starvation = 0;
} else {
*ptr_starvation /= 2;
}
} else {
int food_deficit = type_consumption - resources[RES_FOOD];
int citizen_consumption = citizen_food_consumption(cit_type);
int hungry_citizens = 0;
if (citizen_consumption > 0) {
// Multiply food_deficit by 100, since citizen_consumption is per 100 citizens
hungry_citizens = (100 * food_deficit) / citizen_consumption;
}
// Everyone starves. No reduction in starvation, only an increase.
if (hungry_citizens >= population[cit_type].count) {
*ptr_starvation += hungry_citizens;
} else { // Some people eat; normal increase, then a reduction
int pop = population[cit_type].count;
*ptr_starvation += hungry_citizens;
/* At best (0 hungry citizens), this is equivalent to dividing starvation by 2;
* which is exactly what we do when all citizens are fed. As hungry_citizens
* approaches pop this gets closer to dividing starvation by 1; which is exactly
* what we do when no citizens are fed.
*/
*ptr_starvation = (*ptr_starvation * (pop + hungry_citizens)) /
(pop * 2);
}
add_message(MESSAGE_URGENT, "We have run out of food!");
resources[RES_FOOD] = 0;
} // resources[RES_FOOD] < type_consumption
} // for (int i = CIT_MAX - 1; i > CIT_NULL; i--)
} // resources[RES_FOOD] < food_consumed
}
void Player_city::check_unlockables()
{
// First, do areas
for (int i = 0; i < AREA_MAX; i++) {
// Only check if it's not already unlocked.
if (!area_unlocked[i]) {
Area_datum* area_dat = Area_data[i];
City_achievement achievement = area_dat->unlock_condition;
if (meets_achievement( achievement ) ) {
// We did it!
area_unlocked[i] = true;
add_message(MESSAGE_UNLOCK, "New area unlocked: %s!",
area_dat->name.c_str());
}
}
}
// Now, buildings
for (int i = 0; i < BUILD_MAX; i++) {
// Only check if it's not already unlocked.
if (!building_unlocked[i]) {
Building_datum* build_dat = Building_data[i];
City_achievement achievement = build_dat->unlock_condition;
if (meets_achievement( achievement ) ) {
// We did it!
building_unlocked[i] = true;
add_message(MESSAGE_UNLOCK, "New building unlocked: %s!",
build_dat->name.c_str());
}
}