forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simcity.cc
3133 lines (2769 loc) · 99.9 KB
/
simcity.cc
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
/*
* Copyright (c) 1997 - 2001 Hj. Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*
* construction of cities, creation of passengers
*
*/
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "boden/wege/strasse.h"
#include "boden/grund.h"
#include "boden/boden.h"
#include "gui/simwin.h"
#include "simworld.h"
#include "simware.h"
#include "player/simplay.h"
#include "simplan.h"
#include "display/simimg.h"
#include "vehicle/simverkehr.h"
#include "simtools.h"
#include "simhalt.h"
#include "simfab.h"
#include "simcity.h"
#include "simmesg.h"
#include "simcolor.h"
#include "gui/karte.h"
#include "gui/stadt_info.h"
#include "besch/haus_besch.h"
#include "simintr.h"
#include "simdebug.h"
#include "obj/gebaeude.h"
#include "dataobj/translator.h"
#include "dataobj/settings.h"
#include "dataobj/loadsave.h"
#include "dataobj/tabfile.h"
#include "dataobj/environment.h"
#include "sucher/bauplatz_sucher.h"
#include "bauer/wegbauer.h"
#include "bauer/brueckenbauer.h"
#include "bauer/hausbauer.h"
#include "bauer/fabrikbauer.h"
#include "utils/cbuffer_t.h"
#include "utils/simstring.h"
#define PACKET_SIZE (7)
// since we use 32 bit per growth steps, we use this varible to take care of the remaining sub citicen growth
#define CITYGROWTH_PER_CITICEN (0x0000000100000000ll)
karte_ptr_t stadt_t::welt; // one is enough ...
/********************************* From here on cityrules stuff *****************************************/
/**
* in this fixed interval, construction will happen
* 21s = 21000 per house
*/
const uint32 stadt_t::city_growth_step = 21000;
/**
* this is the default faktor to prefer clustering
*/
uint32 stadt_t::cluster_factor = 10;
/*
* chance to do renovation instead new building (in percent)
* @author prissi
*/
static uint32 renovation_percentage = 12;
/*
* minimum ratio of city area to building area to allow expansion
* the higher this value, the slower the city expansion if there are still "holes"
* @author prissi
*/
static uint32 min_building_density = 25;
// the following are the scores for the different building types
static sint16 ind_start_score = 0;
static sint16 com_start_score = -10;
static sint16 res_start_score = 0;
// order: res com, ind, given by gebaeude_t::typ
static sint16 ind_neighbour_score[] = { -8, 0, 8 };
static sint16 com_neighbour_score[] = { 1, 8, 1 };
static sint16 res_neighbour_score[] = { 8, 0, -8 };
/**
* Rule data structure
* maximum 7x7 rules
* @author Hj. Malthaner
*/
class rule_entry_t {
public:
uint8 x,y;
char flag;
rule_entry_t(uint8 x_=0, uint8 y_=0, char f_='.') : x(x_), y(y_), flag(f_) {}
void rdwr(loadsave_t* file)
{
file->rdwr_byte(x);
file->rdwr_byte(y);
uint8 c = flag;
file->rdwr_byte(c);
flag = c;
}
};
class rule_t {
public:
sint16 chance;
vector_tpl<rule_entry_t> rule;
rule_t(uint32 count=0) : chance(0), rule(count) {}
void rdwr(loadsave_t* file)
{
file->rdwr_short(chance);
if (file->is_loading()) {
rule.clear();
}
uint32 count = rule.get_count();
file->rdwr_long(count);
for(uint32 i=0; i<count; i++) {
if (file->is_loading()) {
rule.append(rule_entry_t());
}
rule[i].rdwr(file);
}
}
};
// house rules
static vector_tpl<rule_t *> house_rules;
// and road rules
static vector_tpl<rule_t *> road_rules;
/**
* Symbols in rules:
* S = darf keine Strasse sein
* s = muss Strasse sein
* n = muss Natur sein
* H = darf kein Haus sein
* h = muss Haus sein
* T = not a stop // added in 88.03.3
* t = is a stop // added in 88.03.3
* u = good slope for way
* U = not a slope for ways
* . = beliebig
*/
// here '.' is ignored, since it will not be tested anyway
static char const* const allowed_chars_in_rule = "SsnHhTtUu";
/*
* @param pos position to check
* @param regel the rule to evaluate
* @return true on match, false otherwise
* @author Hj. Malthaner
*/
bool stadt_t::bewerte_loc(const koord pos, const rule_t ®el, int rotation)
{
//printf("Test for (%s) in rotation %d\n", pos.get_str(), rotation);
koord k;
FOR(vector_tpl<rule_entry_t>, const& r, regel.rule) {
uint8 x,y;
switch (rotation) {
default:
case 0: x=r.x; y=r.y; break;
case 90: x=r.y; y=6-r.x; break;
case 180: x=6-r.x; y=6-r.y; break;
case 270: x=6-r.y; y=r.x; break;
}
const koord k(pos.x+x-3, pos.y+y-3);
const grund_t* gr = welt->lookup_kartenboden(k);
if (gr == NULL) {
// outside of the map => cannot apply this rule
return false;
}
switch (r.flag) {
case 's':
// road?
if (!gr->hat_weg(road_wt)) return false;
break;
case 'S':
// not road?
if (gr->hat_weg(road_wt)) return false;
break;
case 'h':
// is house
if (gr->get_typ() != grund_t::fundament || gr->obj_bei(0)->get_typ()!=obj_t::gebaeude) return false;
break;
case 'H':
// no house
if (gr->get_typ() == grund_t::fundament) return false;
break;
case 'n':
// nature/empty
if (!gr->ist_natur() || gr->kann_alle_obj_entfernen(NULL) != NULL) return false;
break;
case 'U':
// unbuildable for road
if (!hang_t::ist_wegbar(gr->get_grund_hang())) return false;
break;
case 'u':
// road may be buildable
if (hang_t::ist_wegbar(gr->get_grund_hang())) return false;
break;
case 't':
// here is a stop/extension building
if (!gr->is_halt()) return false;
break;
case 'T':
// no stop
if (gr->is_halt()) return false;
break;
default: ;
// ignore
}
}
return true;
}
/**
* Check rule in all transformations at given position
* prissi: but the rules should explicitly forbid building then?!?
* @author Hj. Malthaner
*/
sint32 stadt_t::bewerte_pos(const koord pos, const rule_t ®el)
{
// will be called only a single time, so we can stop after a single match
if(bewerte_loc(pos, regel, 0) ||
bewerte_loc(pos, regel, 90) ||
bewerte_loc(pos, regel, 180) ||
bewerte_loc(pos, regel, 270)) {
return 1;
}
return 0;
}
void stadt_t::bewerte_strasse(koord k, sint32 rd, const rule_t ®el)
{
if (simrand(rd) == 0) {
best_strasse.check(k, bewerte_pos(k, regel));
}
}
void stadt_t::bewerte_haus(koord k, sint32 rd, const rule_t ®el)
{
if (simrand(rd) == 0) {
best_haus.check(k, bewerte_pos(k, regel));
}
}
/**
* Reads city configuration data
* @author Hj. Malthaner
*/
bool stadt_t::cityrules_init(const std::string &objfilename)
{
tabfile_t cityconf;
// first take user data, then user global data
const std::string user_dir=env_t::user_dir;
if (!cityconf.open((user_dir+"cityrules.tab").c_str())) {
if (!cityconf.open((objfilename+"config/cityrules.tab").c_str())) {
dbg->fatal("stadt_t::init()", "Can't read cityrules.tab" );
return false;
}
}
tabfileobj_t contents;
cityconf.read(contents);
char buf[128];
cluster_factor = (uint32)contents.get_int("cluster_factor", 10);
renovation_percentage = (uint32)contents.get_int("renovation_percentage", 25);
// to keep compatible with the typo, here both are ok
min_building_density = (uint32)contents.get_int("minimum_building_desity", 25);
min_building_density = (uint32)contents.get_int("minimum_building_density", min_building_density);
// init the building value tables
ind_start_score = contents.get_int("ind_start_score", 0);
ind_neighbour_score[0] = contents.get_int("ind_near_res", -8);
ind_neighbour_score[1] = contents.get_int("ind_near_com", 0);
ind_neighbour_score[2] = contents.get_int("ind_near_ind", 8);
com_start_score = contents.get_int("com_start_score", -10);
com_neighbour_score[0] = contents.get_int("com_near_res", 1);
com_neighbour_score[1] = contents.get_int("com_near_com", 8);
com_neighbour_score[2] = contents.get_int("com_near_ind", 1);
res_start_score = contents.get_int("res_start_score", 0);
res_neighbour_score[0] = contents.get_int("res_near_res", 8);
res_neighbour_score[1] = contents.get_int("res_near_com", 0);
res_neighbour_score[2] = contents.get_int("res_near_ind", -8);
uint32 num_house_rules = 0;
for (;;) {
sprintf(buf, "house_%d", num_house_rules + 1);
if (contents.get_string(buf, 0)) {
num_house_rules++;
} else {
break;
}
}
DBG_MESSAGE("stadt_t::init()", "Read %d house building rules", num_house_rules);
uint32 num_road_rules = 0;
for (;;) {
sprintf(buf, "road_%d", num_road_rules + 1);
if (contents.get_string(buf, 0)) {
num_road_rules++;
} else {
break;
}
}
DBG_MESSAGE("stadt_t::init()", "Read %d road building rules", num_road_rules);
clear_ptr_vector( house_rules );
for (uint32 i = 0; i < num_house_rules; i++) {
house_rules.append(new rule_t());
sprintf(buf, "house_%d.chance", i + 1);
house_rules[i]->chance = contents.get_int(buf, 0);
sprintf(buf, "house_%d", i + 1);
const char* rule = contents.get_string(buf, "");
// skip leading spaces (use . for padding)
while (*rule == ' ') {
rule++;
}
// find out rule size
size_t size = 0;
size_t maxlen = strlen(rule);
while (size < maxlen && rule[size]!=' ') {
size++;
}
if (size > 7 || maxlen < size * (size + 1) - 1 || (size & 1) == 0 || size <= 2 ) {
dbg->fatal("stadt_t::cityrules_init()", "house rule %d has bad format!", i + 1);
}
// put rule into memory
const uint8 offset = (7 - (uint)size) / 2;
for (uint y = 0; y < size; y++) {
for (uint x = 0; x < size; x++) {
const char flag = rule[x + y * (size + 1)];
// check for allowed characters; ignore '.';
// leave midpoint out, should be 'n', which is checked in baue() anyway
if ((x+offset!=3 || y+offset!=3) && (flag!=0 && strchr(allowed_chars_in_rule, flag))) {
house_rules[i]->rule.append(rule_entry_t(x+offset,y+offset,flag));
}
else {
if ((x+offset!=3 || y+offset!=3) && flag!='.') {
dbg->warning("stadt_t::cityrules_init()", "house rule %d entry (%d,%d) is '%c' and will be ignored", i + 1, x+offset, y+offset, flag);
}
}
}
}
}
clear_ptr_vector( road_rules );
for (uint32 i = 0; i < num_road_rules; i++) {
road_rules.append(new rule_t());
sprintf(buf, "road_%d.chance", i + 1);
road_rules[i]->chance = contents.get_int(buf, 0);
sprintf(buf, "road_%d", i + 1);
const char* rule = contents.get_string(buf, "");
// skip leading spaces (use . for padding)
while (*rule == ' ') {
rule++;
}
// find out rule size
size_t size = 0;
size_t maxlen = strlen(rule);
while (size < maxlen && rule[size] != ' ') {
size++;
}
if ( size > 7 || maxlen < size * (size + 1) - 1 || (size & 1) == 0 || size <= 2 ) {
dbg->fatal("stadt_t::cityrules_init()", "road rule %d has bad format!", i + 1);
}
// put rule into memory
const uint8 offset = (7 - (uint)size) / 2;
for (uint y = 0; y < size; y++) {
for (uint x = 0; x < size; x++) {
const char flag = rule[x + y * (size + 1)];
// check for allowed characters; ignore '.';
// leave midpoint out, should be 'n', which is checked in baue() anyway
if ((x+offset!=3 || y+offset!=3) && (flag!=0 && strchr(allowed_chars_in_rule, flag))) {
road_rules[i]->rule.append(rule_entry_t(x+offset,y+offset,flag));
}
else {
if ((x+offset!=3 || y+offset!=3) && flag!='.') {
dbg->warning("stadt_t::cityrules_init()", "road rule %d entry (%d,%d) is '%c' and will be ignored", i + 1, x+offset, y+offset, flag);
}
}
}
}
}
return true;
}
/**
* Reads/writes city configuration data from/to a savegame
* called from karte_t::speichern and karte_t::laden
* only written for networkgames
* @author Dwachs
*/
void stadt_t::cityrules_rdwr(loadsave_t *file)
{
if( file->get_version() >= 112008 ) {
file->rdwr_long( cluster_factor );
}
file->rdwr_long(renovation_percentage);
file->rdwr_long(min_building_density);
file->rdwr_short(ind_start_score);
file->rdwr_short(ind_neighbour_score[0]);
file->rdwr_short(ind_neighbour_score[1]);
file->rdwr_short(ind_neighbour_score[2]);
file->rdwr_short(com_start_score);
file->rdwr_short(com_neighbour_score[0]);
file->rdwr_short(com_neighbour_score[1]);
file->rdwr_short(com_neighbour_score[2]);
file->rdwr_short(res_start_score);
file->rdwr_short(res_neighbour_score[0]);
file->rdwr_short(res_neighbour_score[1]);
file->rdwr_short(res_neighbour_score[2]);
// house rules
if (file->is_loading()) {
clear_ptr_vector( house_rules );
}
uint32 count = house_rules.get_count();
file->rdwr_long(count);
for(uint32 i=0; i<count; i++) {
if (file->is_loading()) {
house_rules.append(new rule_t());
}
house_rules[i]->rdwr(file);
}
// road rules
if (file->is_loading()) {
clear_ptr_vector( road_rules );
}
count = road_rules.get_count();
file->rdwr_long(count);
for(uint32 i=0; i<count; i++) {
if (file->is_loading()) {
road_rules.append(new rule_t());
}
road_rules[i]->rdwr(file);
}
}
/**
* denkmal_platz_sucher_t:
*
* Sucht einen freien Bauplatz
* Im Gegensatz zum bauplatz_sucher_t werden Strassen auf den Raendern
* toleriert.
*
* 22-Dec-02: Hajo: added safety checks for gr != 0 and plan != 0
*
* @author V. Meyer
*/
class denkmal_platz_sucher_t : public platzsucher_t {
public:
denkmal_platz_sucher_t(karte_t* welt, sint16 radius) : platzsucher_t(welt, radius) {}
virtual bool ist_feld_ok(koord pos, koord d, climate_bits cl) const
{
const planquadrat_t* plan = welt->access(pos + d);
// Hajo: can't build here
if (plan == NULL) return false;
const grund_t* gr = plan->get_kartenboden();
if( ((1 << welt->get_climate( gr->get_pos().get_2d() )) & cl) == 0 ) {
return false;
}
if (ist_randfeld(d)) {
return
gr->get_grund_hang() == hang_t::flach && // Flach
gr->get_typ() == grund_t::boden && // Boden -> no building
(!gr->hat_wege() || gr->hat_weg(road_wt)) && // only roads
gr->kann_alle_obj_entfernen(NULL) == NULL; // Irgendwas verbaut den Platz?
} else {
return
gr->get_grund_hang() == hang_t::flach &&
gr->get_typ() == grund_t::boden &&
gr->ist_natur() && // Keine Wege hier
gr->kann_alle_obj_entfernen(NULL) == NULL; // Irgendwas verbaut den Platz?
}
}
};
/**
* rathausplatz_sucher_t:
*
* 22-Dec-02: Hajo: added safety checks for gr != 0 and plan != 0
*
* @author V. Meyer
*/
class rathausplatz_sucher_t : public platzsucher_t {
public:
rathausplatz_sucher_t(karte_t* welt, uint8 dir_) : platzsucher_t(welt), dir(dir_) {}
virtual bool ist_feld_ok(koord pos, koord d, climate_bits cl) const
{
const grund_t* gr = welt->lookup_kartenboden(pos + d);
if (gr == NULL || gr->get_grund_hang() != hang_t::flach) return false;
if( ((1 << welt->get_climate( gr->get_pos().get_2d() )) & cl) == 0 ) {
return false;
}
if (d.x > 0 || d.y > 0) {
if (welt->lookup_kartenboden(pos)->get_hoehe() != gr->get_hoehe()) {
// height wrong!
return false;
}
}
if ( ((dir & ribi_t::sued)!=0 && d.y == h - 1) ||
((dir & ribi_t::west)!=0 && d.x == 0) ||
((dir & ribi_t::nord)!=0 && d.y == 0) ||
((dir & ribi_t::ost)!=0 && d.x == b - 1)) {
// we want to build a road here:
return
gr->get_typ() == grund_t::boden &&
(!gr->hat_wege() || (gr->hat_weg(road_wt) && !gr->has_two_ways())) && // build only on roads, no other ways
!gr->is_halt() &&
gr->kann_alle_obj_entfernen(NULL) == NULL;
} else {
// we want to build the townhall here: maybe replace existing buildings
return ((gr->get_typ()==grund_t::boden && gr->ist_natur()) || gr->get_typ()==grund_t::fundament) &&
gr->kann_alle_obj_entfernen(NULL) == NULL;
}
}
private:
uint8 dir;
};
static bool compare_gebaeude_pos(const gebaeude_t* a, const gebaeude_t* b)
{
const uint32 pos_a = (a->get_pos().y<<16)+a->get_pos().x;
const uint32 pos_b = (b->get_pos().y<<16)+b->get_pos().x;
return pos_a<pos_b;
}
// this function adds houses to the city house list
void stadt_t::add_gebaeude_to_stadt(const gebaeude_t* gb, bool ordered)
{
if (gb != NULL) {
const haus_tile_besch_t* tile = gb->get_tile();
koord size = tile->get_besch()->get_groesse(tile->get_layout());
const koord pos = gb->get_pos().get_2d() - tile->get_offset();
koord k;
// add all tiles
for (k.y = 0; k.y < size.y; k.y++) {
for (k.x = 0; k.x < size.x; k.x++) {
if (gebaeude_t* const add_gb = obj_cast<gebaeude_t>(welt->lookup_kartenboden(pos + k)->first_obj())) {
if(add_gb->get_tile()->get_besch()!=gb->get_tile()->get_besch()) {
dbg->error( "stadt_t::add_gebaeude_to_stadt()","two buildings \"%s\" and \"%s\" at (%i,%i): Game will crash during deletion", add_gb->get_tile()->get_besch()->get_name(), gb->get_tile()->get_besch()->get_name(), pos.x + k.x, pos.y + k.y);
buildings.remove(add_gb);
}
else {
if( ordered ) {
buildings.insert_ordered(add_gb, tile->get_besch()->get_level() + 1, compare_gebaeude_pos);
}
else {
buildings.append(add_gb, tile->get_besch()->get_level() + 1);
}
}
add_gb->set_stadt(this);
}
}
}
// no update of city limits
// as has_low_density may depend on the order the buildings list is filled
if (!ordered) {
// check borders
pruefe_grenzen(pos);
if(size!=koord(1,1)) {
pruefe_grenzen(pos+size-koord(1,1));
}
}
}
}
// this function removes houses from the city house list
void stadt_t::remove_gebaeude_from_stadt(gebaeude_t* gb)
{
buildings.remove(gb);
gb->set_stadt(NULL);
recalc_city_size();
}
// just updates the weight count of this building (after a renovation)
void stadt_t::update_gebaeude_from_stadt(gebaeude_t* gb)
{
buildings.remove(gb);
buildings.append(gb, gb->get_tile()->get_besch()->get_level() + 1);
}
void stadt_t::pruefe_grenzen(koord k)
{
// WARNING: do not call this during multithreaded loading,
// as has_low_density may depend on the order the buildings list is filled
if( has_low_density ) {
// has extra wide borders => change density calculation
has_low_density = (buildings.get_count()<10 || (buildings.get_count()*100l)/(abs(ur.x-lo.x-4)*abs(ur.y-lo.y-4)+1) > min_building_density);
if(!has_low_density) {
// full recalc needed due to map borders ...
recalc_city_size();
return;
}
}
else {
has_low_density = (buildings.get_count()<10 || (buildings.get_count()*100l)/((ur.x-lo.x)*(ur.y-lo.y)+1) > min_building_density);
if(has_low_density) {
// wide borders again ..
lo -= koord(2,2);
ur += koord(2,2);
}
}
// now just add single coordinates
if( has_low_density ) {
lo.clip_max(k-koord(2,2));
ur.clip_min(k+koord(2,2));
}
else {
// first grow within ...
lo.clip_max(k);
ur.clip_min(k);
}
lo.clip_min(koord(0,0));
ur.clip_max(koord(welt->get_size().x-1,welt->get_size().y-1));
}
// recalculate the spreading of a city
// will be updated also after house deletion
void stadt_t::recalc_city_size()
{
// WARNING: do not call this during multithreaded loading,
// as has_low_density may depend on the order the buildings list is filled
lo = pos;
ur = pos;
FOR(weighted_vector_tpl<gebaeude_t*>, const i, buildings) {
if (i->get_tile()->get_besch()->get_utyp() != haus_besch_t::firmensitz) {
koord const& gb_pos = i->get_pos().get_2d();
lo.clip_max(gb_pos);
ur.clip_min(gb_pos);
}
}
has_low_density = (buildings.get_count()<10 || (buildings.get_count()*100l)/((ur.x-lo.x)*(ur.y-lo.y)+1) > min_building_density);
if( has_low_density ) {
// wider borders for faster growth of sparse small towns
lo -= koord(2,2);
ur += koord(2,2);
}
lo.clip_min(koord(0,0));
ur.clip_max(koord(welt->get_size().x-1,welt->get_size().y-1));
}
void stadt_t::init_pax_destinations()
{
pax_destinations_old.clear();
pax_destinations_new.clear();
pax_destinations_new_change = 0;
}
void stadt_t::factory_entry_t::rdwr(loadsave_t *file)
{
if( file->get_version()>=110005 ) {
koord factory_pos;
if( file->is_saving() ) {
factory_pos = factory->get_pos().get_2d();
}
factory_pos.rdwr( file );
if( file->is_loading() ) {
// position will be resolved back into fabrik_t* later
factory_pos_x = factory_pos.x;
factory_pos_y = factory_pos.y;
}
file->rdwr_long( demand );
file->rdwr_long( supply );
file->rdwr_long( remaining );
}
}
void stadt_t::factory_entry_t::resolve_factory()
{
factory = fabrik_t::get_fab( koord(factory_pos_x, factory_pos_y) );
}
const stadt_t::factory_entry_t* stadt_t::factory_set_t::get_entry(const fabrik_t *const factory) const
{
FOR(vector_tpl<factory_entry_t>, const& e, entries) {
if (e.factory == factory) {
return &e;
}
}
return NULL; // not found
}
stadt_t::factory_entry_t* stadt_t::factory_set_t::get_random_entry()
{
if( total_remaining>0 ) {
sint32 weight = simrand(total_remaining);
FOR(vector_tpl<factory_entry_t>, & entry, entries) {
if( entry.remaining>0 ) {
if( weight<entry.remaining ) {
return &entry;
}
weight -= entry.remaining;
}
}
}
return NULL;
}
void stadt_t::factory_set_t::update_factory(fabrik_t *const factory, const sint32 demand)
{
if( entries.is_contained( factory_entry_t(factory) ) ) {
// existing target factory
factory_entry_t &entry = entries[ entries.index_of( factory_entry_t(factory) ) ];
total_demand += demand - entry.demand;
entry.demand = demand;
// entry.supply, entry.remaining and total_remaining will be adjusted in recalc_generation_ratio()
}
else {
// new target factory
entries.append( factory_entry_t(factory, demand) );
total_demand += demand;
}
ratio_stale = true; // always trigger recalculation of ratio
}
void stadt_t::factory_set_t::remove_factory(fabrik_t *const factory)
{
if( entries.is_contained( factory_entry_t(factory) ) ) {
factory_entry_t &entry = entries[ entries.index_of( factory_entry_t(factory) ) ];
total_demand -= entry.demand;
total_remaining -= entry.remaining;
entries.remove( entry );
ratio_stale = true;
}
}
#define SUPPLY_BITS (3)
#define SUPPLY_FACTOR (9) // out of 2^SUPPLY_BITS
void stadt_t::factory_set_t::recalc_generation_ratio(const sint32 default_percent, const sint64 *city_stats, const int stats_count, const int stat_type)
{
ratio_stale = false; // reset flag
// calculate an average of at most 3 previous months' pax/mail generation amounts
uint32 months = 0;
sint64 average_generated = 0;
sint64 month_generated;
while( months<3 && (month_generated=city_stats[(months+1)*stats_count+stat_type])>0 ) {
average_generated += month_generated;
++months;
}
if( months>1 ) {
average_generated /= months;
}
/* ratio formula -> ((supply * 100) / total) shifted by RATIO_BITS */
// we supply 1/8 more than demand
const sint32 target_supply = (total_demand * SUPPLY_FACTOR + ((1<<(DEMAND_BITS+SUPPLY_BITS))-1)) >> (DEMAND_BITS+SUPPLY_BITS);
if( total_demand==0 ) {
// no demand -> zero ratio
generation_ratio = 0;
} else if (!welt->get_settings().get_factory_enforce_demand() || average_generated == 0) {
// demand not enforced or no pax generation data from previous month(s) -> simply use default ratio
generation_ratio = (uint32)default_percent << RATIO_BITS;
}
else {
// ratio of target supply (plus allowances for rounding up) to previous months' average generated pax (less 6.25% or 1/16 allowance for fluctuation), capped by default ratio
const sint64 default_ratio = (sint64)default_percent << RATIO_BITS;
const sint64 supply_ratio = ((sint64)((target_supply+(sint32)entries.get_count())*100)<<RATIO_BITS) / (average_generated-(average_generated>>4)+1);
generation_ratio = (uint32)( default_ratio<supply_ratio ? default_ratio : supply_ratio );
}
// adjust supply and remaining figures
if (welt->get_settings().get_factory_enforce_demand() && (generation_ratio >> RATIO_BITS) == (uint32)default_percent && average_generated > 0 && total_demand > 0) {
const sint64 supply_promille = ( ( (average_generated << 10) * (sint64)default_percent ) / 100 ) / (sint64)target_supply;
if( supply_promille<1024 ) {
// expected supply is really smaller than target supply
FOR(vector_tpl<factory_entry_t>, & entry, entries) {
const sint32 new_supply = (sint32)( ( (sint64)entry.demand * SUPPLY_FACTOR * supply_promille + ((1<<(DEMAND_BITS+SUPPLY_BITS+10))-1) ) >> (DEMAND_BITS+SUPPLY_BITS+10) );
const sint32 delta_supply = new_supply - entry.supply;
if( delta_supply==0 ) {
continue;
}
else if( delta_supply>0 || (entry.remaining+delta_supply)>=0 ) {
// adjust remaining figures by the change in supply
total_remaining += delta_supply;
entry.remaining += delta_supply;
}
else {
// avoid deducting more than allowed
total_remaining -= entry.remaining;
entry.remaining = 0;
}
entry.supply = new_supply;
}
return;
}
}
// expected supply is unknown or sufficient to meet target supply
FOR(vector_tpl<factory_entry_t>, & entry, entries) {
const sint32 new_supply = ( entry.demand * SUPPLY_FACTOR + ((1<<(DEMAND_BITS+SUPPLY_BITS))-1) ) >> (DEMAND_BITS+SUPPLY_BITS);
const sint32 delta_supply = new_supply - entry.supply;
if( delta_supply==0 ) {
continue;
}
else if( delta_supply>0 || (entry.remaining+delta_supply)>=0 ) {
// adjust remaining figures by the change in supply
total_remaining += delta_supply;
entry.remaining += delta_supply;
}
else {
// avoid deducting more than allowed
total_remaining -= entry.remaining;
entry.remaining = 0;
}
entry.supply = new_supply;
}
}
void stadt_t::factory_set_t::new_month()
{
FOR(vector_tpl<factory_entry_t>, & e, entries) {
e.new_month();
}
total_remaining = 0;
total_generated = 0;
ratio_stale = true;
}
void stadt_t::factory_set_t::rdwr(loadsave_t *file)
{
if( file->get_version()>=110005 ) {
uint32 entry_count = entries.get_count();
file->rdwr_long(entry_count);
if( file->is_loading() ) {
entries.resize( entry_count );
factory_entry_t entry;
for( uint32 e=0; e<entry_count; ++e ) {
entry.rdwr( file );
total_demand += entry.demand;
total_remaining += entry.remaining;
entries.append( entry );
}
}
else {
for( uint32 e=0; e<entry_count; ++e ) {
entries[e].rdwr( file );
}
}
file->rdwr_long( total_generated );
}
}
void stadt_t::factory_set_t::resolve_factories()
{
uint32 remove_count = 0;
FOR(vector_tpl<factory_entry_t>, & e, entries) {
e.resolve_factory();
if (!e.factory) {
remove_count ++;
}
}
for( uint32 e=0; e<remove_count; ++e ) {
this->remove_factory( NULL );
}
}
stadt_t::~stadt_t()
{
// close info win
destroy_win((ptrdiff_t)this);
if( reliefkarte_t::get_karte()->get_city() == this ) {
reliefkarte_t::get_karte()->set_city(NULL);
}
// olny if there is still a world left to delete from
if( welt->get_size().x > 1 ) {
welt->lookup_kartenboden(pos)->set_text(NULL);
// remove city info and houses
while (!buildings.empty()) {
// old buildings are not where they think they are, so we ask for map floor
gebaeude_t* const gb = buildings.front();
buildings.remove(gb);
assert( gb!=NULL && !buildings.is_contained(gb) );
if(gb->get_tile()->get_besch()->get_utyp()==haus_besch_t::firmensitz) {
stadt_t *city = welt->suche_naechste_stadt(gb->get_pos().get_2d());
gb->set_stadt( city );
if(city) {
city->buildings.append(gb, gb->get_passagier_level());
}
}
else {
gb->set_stadt( NULL );
hausbauer_t::remove(welt->get_spieler(1),gb);
}
}
}
}
static bool name_used(weighted_vector_tpl<stadt_t*> const& cities, char const* const name)
{
FOR(weighted_vector_tpl<stadt_t*>, const i, cities) {
if (strcmp(i->get_name(), name) == 0) {
return true;
}
}
return false;
}
stadt_t::stadt_t(spieler_t* sp, koord pos, sint32 citizens) :
buildings(16),
pax_destinations_old(koord(PAX_DESTINATIONS_SIZE, PAX_DESTINATIONS_SIZE)),
pax_destinations_new(koord(PAX_DESTINATIONS_SIZE, PAX_DESTINATIONS_SIZE))
{
assert(welt->is_within_limits(pos));
step_count = 0;
pax_destinations_new_change = 0;
next_step = 0;
step_interval = 1;
next_growth_step = 0;
has_low_density = false;
stadtinfo_options = 3; // citizen and growth