forked from jamespetts/simutrans-extended
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simcity.cc
3490 lines (3027 loc) · 104 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 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic licence.
* (see licence.txt)
*
* construction of cities, creation of passengers
*
*/
#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 "simworld.h"
#include "simware.h"
#include "player/simplay.h"
#include "simplan.h"
#include "simimg.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 "dings/gebaeude.h"
#include "dataobj/translator.h"
#include "dataobj/einstellungen.h"
#include "dataobj/loadsave.h"
#include "dataobj/tabfile.h"
#include "dataobj/umgebung.h"
#include "sucher/bauplatz_sucher.h"
#include "bauer/warenbauer.h"
#include "bauer/wegbauer.h"
#include "bauer/hausbauer.h"
#include "bauer/fabrikbauer.h"
#include "utils/cbuffer_t.h"
#include "utils/simstring.h"
#include "tpl/ptrhashtable_tpl.h"
#include "tpl/minivec_tpl.h"
karte_t* stadt_t::welt = NULL; // one is enough ...
sint16 number_of_cars;
// Private car ownership information.
// @author: jamespetts
// (But much of this code is adapted from the speed bonus code,
// written by Prissi).
class car_ownership_record_t {
public:
sint32 year;
sint16 ownership_percent;
car_ownership_record_t( sint32 y = 0, sint16 ownership = 0 ) {
year = y*12;
ownership_percent = ownership;
};
};
static sint16 default_car_ownership_percent = 25;
static vector_tpl<car_ownership_record_t> car_ownership[1];
void stadt_t::privatecar_init(cstring_t objfilename)
{
tabfile_t ownership_file;
// first take user data, then user global data
if (!ownership_file.open(objfilename+"config/privatecar.tab"))
{
dbg->message("stadt_t::privatecar_init()", "Error opening config/privatecar.tab.\nWill use default value." );
return;
}
tabfileobj_t contents;
ownership_file.read(contents);
/* init the values from line with the form year, proportion, year, proportion
* must be increasing order!
*/
int *tracks = contents.get_ints("car_ownership");
if((tracks[0]&1)==1)
{
dbg->message("stadt_t::privatecar_init()", "Ill formed line in config/privatecar.tab.\nWill use default value. Format is year,ownership percentage[ year,ownership percentage]!" );
car_ownership->clear();
return;
}
car_ownership[0].resize( tracks[0]/2 );
for( int i=1; i<tracks[0]; i+=2 )
{
car_ownership_record_t c( tracks[i], tracks[i+1] );
car_ownership[0].append( c );
}
delete [] tracks;
}
sint16 stadt_t::get_private_car_ownership(sint32 monthyear)
{
if(monthyear == 0)
{
return default_car_ownership_percent;
}
// ok, now lets see if we have data for this
if(car_ownership->get_count())
{
uint i=0;
while( i<car_ownership->get_count() && monthyear>=car_ownership[0][i].year ) {
i++;
}
if( i==car_ownership->get_count() )
{
// maxspeed already?
return car_ownership[0][i-1].ownership_percent;
}
else if(i==0)
{
// minspeed below
return car_ownership[0][0].ownership_percent;
}
else
{
// interpolate linear
const sint32 delta_ownership_percent = car_ownership[0][i].ownership_percent - car_ownership[0][i-1].ownership_percent;
const sint32 delta_years = car_ownership[0][i].year - car_ownership[0][i-1].year;
return ( (delta_ownership_percent*(monthyear-car_ownership[0][i-1].year)) / delta_years ) + car_ownership[0][i-1].ownership_percent;
}
}
else
{
return default_car_ownership_percent;
}
}
// Private car ownership information.
// @author: jamespetts
// (But much of this code is adapted from the speed bonus code,
// written by Prissi).
class electric_consumption_record_t {
public:
sint32 year;
sint16 consumption_percent;
electric_consumption_record_t( sint32 y = 0, sint16 consumption = 0 ) {
year = y*12;
consumption_percent = consumption;
};
};
static float default_electricity_consumption = 1.0F;
static vector_tpl<electric_consumption_record_t> electricity_consumption[1];
void stadt_t::electricity_consumption_init(cstring_t objfilename)
{
tabfile_t consumption_file;
// first take user data, then user global data
if (!consumption_file.open(objfilename+"config/electricity.tab"))
{
dbg->message("stadt_t::electricity_consumption_init()", "Error opening config/electricity.tab.\nWill use default value." );
return;
}
tabfileobj_t contents;
consumption_file.read(contents);
/* init the values from line with the form year, proportion, year, proportion
* must be increasing order!
*/
int *tracks = contents.get_ints("electricity_consumption");
if((tracks[0]&1)==1)
{
dbg->message("stadt_t::electricity_consumption_init()", "Ill formed line in config/electricity.tab.\nWill use default value. Format is year,ownership percentage[ year,ownership percentage]!" );
car_ownership->clear();
return;
}
electricity_consumption[0].resize( tracks[0]/2 );
for( int i=1; i<tracks[0]; i+=2 )
{
electric_consumption_record_t c( tracks[i], tracks[i+1] );
electricity_consumption[0].append( c );
}
delete [] tracks;
}
float stadt_t::get_electricity_consumption(sint32 monthyear) const
{
if(monthyear == 0)
{
return default_electricity_consumption;
}
// ok, now lets see if we have data for this
if(electricity_consumption->get_count())
{
uint i=0;
while( i<electricity_consumption->get_count() && monthyear>=electricity_consumption[0][i].year )
{
i++;
}
if( i==electricity_consumption->get_count() )
{
// maxspeed already?
return electricity_consumption[0][i-1].consumption_percent;
}
else if(i==0)
{
// minspeed below
return electricity_consumption[0][0].consumption_percent;
}
else
{
// interpolate linear
const sint32 delta_ownership_percent = electricity_consumption[0][i].consumption_percent - electricity_consumption[0][i-1].consumption_percent;
const sint32 delta_years = electricity_consumption[0][i].year - electricity_consumption[0][i-1].year;
return (((float)(delta_ownership_percent*(monthyear-electricity_consumption[0][i-1].year)) / delta_years ) + electricity_consumption[0][i-1].consumption_percent) / 100.0F;
}
}
else
{
return default_electricity_consumption;
}
}
/********************************* From here on cityrules stuff *****************************************/
/**
* in this fixed interval, construction will happen
* 21s = 21000 per house
*/
const uint32 stadt_t::step_bau_interval = 21000;
/**
* try to built cities at least this distance apart
* @author prissi
*/
static int minimum_city_distance = 16;
/*
* 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_desity = 25;
/**
* add a new consumer every % people increase
* @author prissi
*/
static int industry_increase_every[8];
// the following are the scores for the different building types
static int ind_start_score = 0;
static int com_start_score = -10;
static int res_start_score = 0;
// order: res com, ind, given by gebaeude_t::typ
static int ind_neighbour_score[] = { -8, 0, 8 };
static int com_neighbour_score[] = { 1, 8, 1 };
static int res_neighbour_score[] = { 8, 0, -8 };
/**
* Rule data structure
* maximum 7x7 rules
* @author Hj. Malthaner
*/
struct rule_t {
int chance;
char rule[49];
};
// house rules
static int num_house_rules = 0;
static struct rule_t* house_rules = 0;
// and road rules
static int num_road_rules = 0;
static struct rule_t* road_rules = 0;
// rotation of the rules ...
static const uint8 rotate_rules_0[] = {
0, 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
};
static const uint8 rotate_rules_90[] = {
6, 13, 20, 27, 34, 41, 48,
5, 12, 19, 26, 33, 40, 47,
4, 11, 18, 25, 32, 39, 46,
3, 10, 17, 24, 31, 38, 45,
2, 9, 16, 23, 30, 37, 44,
1, 8, 15, 22, 29, 36, 43,
0, 7, 14, 21, 28, 35, 42
};
static const uint8 rotate_rules_180[] = {
48, 47, 46, 45, 44, 43, 42,
41, 40, 39, 38, 37, 36, 35,
34, 33, 32, 31, 30, 29, 28,
27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14,
13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1, 0
};
static const uint8 rotate_rules_270[] = {
42, 35, 28, 21, 14, 7, 0,
43, 36, 29, 22, 15, 8, 1,
44, 37, 30, 23, 16, 9, 2,
45, 38, 31, 24, 17, 10, 3,
46, 39, 32, 25, 18, 11, 4,
47, 40, 33, 26, 19, 12, 5,
48, 41, 34, 27, 20, 13, 6
};
/**
* 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
*
* @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 char* regel, int rotation)
{
koord k;
const uint8* index_to_rule = 0;
switch (rotation) {
case 0: index_to_rule = rotate_rules_0; break;
case 90: index_to_rule = rotate_rules_90; break;
case 180: index_to_rule = rotate_rules_180; break;
case 270: index_to_rule = rotate_rules_270; break;
}
uint8 rule_index = 0;
for (k.y = pos.y - 3; k.y <= pos.y + 3; k.y++) {
for (k.x = pos.x - 3; k.x <= pos.x + 3; k.x++) {
const char rule = regel[index_to_rule[rule_index++]];
if (rule != 0) {
const grund_t* gr = welt->lookup_kartenboden(k);
if (gr == NULL) {
// outside of the map => cannot apply this rule
return false;
}
switch (rule) {
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()!=ding_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;
}
}
}
}
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 char* regel)
{
// 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 char* regel)
{
if (simrand(rd) == 0) {
best_strasse.check(k, bewerte_pos(k, regel));
}
}
void stadt_t::bewerte_haus(koord k, sint32 rd, const char* regel)
{
if (simrand(rd) == 0) {
best_haus.check(k, bewerte_pos(k, regel));
}
}
/**
* Reads city configuration data
* @author Hj. Malthaner
*/
bool stadt_t::cityrules_init(cstring_t objfilename)
{
tabfile_t cityconf;
// first take user data, then user global data
cstring_t user_dir=umgebung_t::user_dir;
if (!cityconf.open(user_dir+"cityrules.tab")) {
if (!cityconf.open(objfilename+"config/cityrules.tab")) {
dbg->fatal("stadt_t::init()", "Can't read cityrules.tab" );
return false;
}
}
tabfileobj_t contents;
cityconf.read(contents);
char buf[128];
minimum_city_distance = contents.get_int("minimum_city_distance", 16);
renovation_percentage = (uint32)contents.get_int("renovation_percentage", 25);
min_building_desity = (uint32)contents.get_int("minimum_building_desity", 25);
int ind_increase = contents.get_int("industry_increase_every", 0);
for (int i = 0; i < 8; i++) {
industry_increase_every[i] = ind_increase << i;
}
// 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);
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);
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);
house_rules = new struct rule_t[num_house_rules];
for (int i = 0; i < num_house_rules; i++) {
sprintf(buf, "house_%d.chance", i + 1);
house_rules[i].chance = contents.get_int(buf, 0);
memset(house_rules[i].rule, 0, sizeof(house_rules[i].rule));
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 uint offset = (7 - size) / 2;
for (uint y = 0; y < size; y++) {
for (uint x = 0; x < size; x++) {
house_rules[i].rule[(offset + y) * 7 + x + offset] = rule[x + y * (size + 1)];
}
}
}
road_rules = new struct rule_t[num_road_rules];
for (int i = 0; i < num_road_rules; i++) {
sprintf(buf, "road_%d.chance", i + 1);
road_rules[i].chance = contents.get_int(buf, 0);
memset(road_rules[i].rule, 0, sizeof(road_rules[i].rule));
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 - size) / 2;
for (uint y = 0; y < size; y++) {
for (uint x = 0; x < size; x++) {
road_rules[i].rule[(offset + y) * 7 + x + offset] = rule[x + y * (size + 1)];
}
}
}
return true;
}
/**
* denkmal_platz_sucher_t:
*
* Sucht einen freien Bauplatz
* Im Gegensatz zum bauplatz_sucher_t werden Strassen auf den Rändern
* 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) : platzsucher_t(welt) {}
virtual bool ist_feld_ok(koord pos, koord d, climate_bits cl) const
{
const planquadrat_t* plan = welt->lookup(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_hoehe())) & cl) == 0) {
return false;
}
if (ist_randfeld(d)) {
return
gr->get_grund_hang() == hang_t::flach && // Flach
gr->get_typ() == grund_t::boden && // Boden -> keine GEbäude
(!gr->hat_wege() || gr->hat_weg(road_wt)) && // Höchstens Strassen
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) : platzsucher_t(welt) {}
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) return false;
if (((1 << welt->get_climate(gr->get_hoehe())) & cl) == 0) {
return false;
}
if (d.x > 0 || d.y > 0) {
if (welt->max_hgt(pos) != welt->max_hgt(pos + d)) {
// height wrong!
return false;
}
}
if (d.y == h - 1) {
// Hier soll eine Strasse hin
return
gr->get_grund_hang() == hang_t::flach &&
gr->get_typ() == grund_t::boden &&
(!gr->hat_wege() || gr->hat_weg(road_wt)) && // Höchstens Strassen
!gr->is_halt() &&
gr->kann_alle_obj_entfernen(NULL) == NULL;
} else {
// Hier soll das Haus hin - wir ersetzen auch andere Gebäude, aber keine Wege!
return
gr->get_grund_hang()==hang_t::flach && (
(gr->get_typ()==grund_t::boden && gr->ist_natur()) ||
gr->get_typ()==grund_t::fundament
) &&
gr->kann_alle_obj_entfernen(NULL) == NULL;
}
}
};
// this function adds houses to the city house list
void stadt_t::add_gebaeude_to_stadt(const gebaeude_t* gb)
{
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++) {
gebaeude_t* add_gb = dynamic_cast<gebaeude_t*>(welt->lookup_kartenboden(pos + k)->first_obj());
if(add_gb) {
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 {
buildings.append(add_gb, tile->get_besch()->get_level() + 1, 16);
}
add_gb->set_stadt(this);
}
}
}
// 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, 16);
}
// recalculate house informations (used for target selection)
void stadt_t::recount_houses()
{
DBG_MESSAGE("stadt_t::rdwr()", "borders (%i,%i) -> (%i,%i)", lo.x, lo.y, ur.x, ur.y);
buildings.clear();
for (sint16 y = lo.y; y <= ur.y; y++) {
for (sint16 x = lo.x; x <= ur.x; x++) {
const grund_t* gr = welt->lookup_kartenboden(koord(x, y));
gebaeude_t* gb = dynamic_cast<gebaeude_t*>(gr->first_obj());
if (gb!=NULL && gb->get_tile()->get_besch()->is_connected_with_town() && welt->suche_naechste_stadt(koord(x, y)) == this) {
// no attraction, just normal buidlings or townhall
buildings.append(gb, gb->get_tile()->get_besch()->get_level() + 1, 16);
gb->set_stadt(this);
}
}
}
DBG_MESSAGE("recount_houses()", "%s has %i bev", get_name(), get_einwohner());
}
void stadt_t::pruefe_grenzen(koord k)
{
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_desity);
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_desity);
if(has_low_density) {
// wide borders again ..
lo -= koord(2,2);
ur += koord(2,2);
}
}
// now just add single coordinates
if( has_low_density ) {
if (k.x < lo.x+2) {
lo.x = k.x - 2;
}
if (k.y < lo.y+2) {
lo.y = k.y - 2;
}
if (k.x > ur.x-2) {
ur.x = k.x + 2;
}
if (k.y > ur.y-2) {
ur.y = k.y + 2;
}
}
else {
// first grow within ...
if (k.x < lo.x) {
lo.x = k.x;
}
if (k.y < lo.y) {
lo.y = k.y;
}
if (k.x > ur.x) {
ur.x = k.x;
}
if (k.y > ur.y) {
ur.y = k.y;
}
}
if (lo.x < 0) {
lo.x = 0;
}
if (lo.y < 0) {
lo.y = 0;
}
if (ur.x >= welt->get_groesse_x()) {
ur.x = welt->get_groesse_x()-1;
}
if (ur.y >= welt->get_groesse_y()) {
ur.y = welt->get_groesse_y()-1;
}
}
bool stadt_t::is_within_city_limits(koord k) const
{
const sint16 li_gr = lo.x - 2;
const sint16 re_gr = ur.x + 2;
const sint16 ob_gr = lo.y - 2;
const sint16 un_gr = ur.y + 2;
bool inside = li_gr < k.x && re_gr > k.x && ob_gr < k.y && un_gr > k.y;
return inside;
}
// recalculate the spreading of a city
// will be updated also after house deletion
void stadt_t::recalc_city_size()
{
lo = pos;
ur = pos;
for( uint32 i=0; i<buildings.get_count(); i++ ) {
if(buildings[i]->get_tile()->get_besch()->get_utyp()!=haus_besch_t::firmensitz) {
const koord gb_pos = buildings[i]->get_pos().get_2d();
if (lo.x > gb_pos.x) {
lo.x = gb_pos.x;
}
if (lo.y > gb_pos.y) {
lo.y = gb_pos.y;
}
if (ur.x < gb_pos.x) {
ur.x = gb_pos.x;
}
if (ur.y < gb_pos.y) {
ur.y = gb_pos.y;
}
}
}
has_low_density = (buildings.get_count()<10 || (buildings.get_count()*100l)/((ur.x-lo.x)*(ur.y-lo.y)+1) > min_building_desity);
if( has_low_density ) {
// wider borders for faster growth of sparse small towns
lo.x -= 2;
lo.y -= 2;
ur.x += 2;
ur.y += 2;
}
if (lo.x < 0) {
lo.x = 0;
}
if (lo.y < 0) {
lo.y = 0;
}
if (ur.x >= welt->get_groesse_x()) {
ur.x = welt->get_groesse_x()-1;
}
if (ur.y >= welt->get_groesse_y()) {
ur.y = welt->get_groesse_y()-1;
}
}
void stadt_t::init_pax_destinations()
{
pax_destinations_old.clear();
pax_destinations_new.clear();
pax_destinations_new_change = 0;
}
stadt_t::~stadt_t()
{
// close info win
destroy_win((long)this);
// Empty the list of city cars
current_cars.clear();
// Remove references to this city from factories.
ITERATE(city_factories, i)
{
city_factories[i]->clear_city();
}
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_groesse_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* gb = (gebaeude_t *)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(),16);
}
}
else
{
gb->set_stadt( NULL );
hausbauer_t::remove(welt,welt->get_spieler(1),gb);
}
}
}
free( (void *)name );
}
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)),
arbeiterziele(4)
{
welt = sp->get_welt();
assert(welt->ist_in_kartengrenzen(pos));
step_count = 0;
pax_destinations_new_change = 0;
next_step = 0;
step_interval = 1;
next_bau_step = 0;
has_low_density = false;
stadtinfo_options = 3; // citicens and growth
besitzer_p = sp;
this->pos = pos;
bev = 0;
arb = 0;
won = 0;
lo = ur = pos;
DBG_MESSAGE("stadt_t::stadt_t()", "Welt %p", welt);
fflush(NULL);
/* get a unique cityname */
/* 9.1.2005, prissi */
const weighted_vector_tpl<stadt_t*>& staedte = welt->get_staedte();
const int name_list_count = translator::get_count_city_name();
fflush(NULL);
// start at random position
int start_cont = simrand(name_list_count);
// get a unique name
const char* list_name;
list_name = translator::get_city_name(start_cont);