forked from aburch/simutrans
/
simworld.cc
4562 lines (3882 loc) · 127 KB
/
simworld.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)
*/
/*
* Hauptklasse fuer Simutrans, Datenstruktur die alles Zusammenhaelt
* Hansjoerg Malthaner, 1997
*/
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "simcity.h"
#include "simcolor.h"
#include "simconvoi.h"
#include "simdebug.h"
#include "simdepot.h"
#include "simfab.h"
#include "simgraph.h"
#include "simhalt.h"
#include "simimg.h"
#include "simintr.h"
#include "simio.h"
#include "simlinemgmt.h"
#include "simmenu.h"
#include "simmesg.h"
#include "simskin.h"
#include "simsound.h"
#include "simsys.h"
#include "simtools.h"
#include "simversion.h"
#include "simview.h"
#include "simwerkz.h"
#include "simwin.h"
#include "simworld.h"
#include "tpl/vector_tpl.h"
#include "boden/boden.h"
#include "boden/wasser.h"
#include "old_blockmanager.h"
#include "vehicle/simvehikel.h"
#include "vehicle/simverkehr.h"
#include "vehicle/movingobj.h"
#include "boden/wege/schiene.h"
#include "dings/zeiger.h"
#include "dings/baum.h"
#include "dings/signal.h"
#include "dings/roadsign.h"
#include "dings/wayobj.h"
#include "dings/groundobj.h"
#include "dings/gebaeude.h"
#include "gui/messagebox.h"
#include "gui/help_frame.h"
#include "gui/karte.h"
#include "dataobj/translator.h"
#include "dataobj/loadsave.h"
#include "dataobj/scenario.h"
#include "dataobj/einstellungen.h"
#include "dataobj/umgebung.h"
#include "dataobj/tabfile.h"
#include "dataobj/powernet.h"
#include "utils/simstring.h"
#include "bauer/brueckenbauer.h"
#include "bauer/tunnelbauer.h"
#include "bauer/fabrikbauer.h"
#include "bauer/wegbauer.h"
#include "bauer/hausbauer.h"
#include "bauer/vehikelbauer.h"
#include "besch/grund_besch.h"
#include "besch/sound_besch.h"
#include "player/simplay.h"
#include "player/ai_passenger.h"
#include "player/ai_goods.h"
//#define DEMO
//#undef DEMO
// advance 201 ms per sync_step in fast forward mode
#define MAGIC_STEP (201)
// frame per second for fast forward
#define FF_PPS (10)
static bool is_dragging = false;
// changes the snowline height (for the seasons)
bool
karte_t::recalc_snowline()
{
static int mfactor[12] = { 99, 95, 80, 50, 25, 10, 0, 5, 20, 35, 65, 85 };
static uint8 month_to_season[12] = { 2, 2, 2, 3, 3, 0, 0, 0, 0, 1, 1, 2 };
// calculate snowline with day precicion
// use linear interpolation
const long ticks_this_month = get_zeit_ms() & (karte_t::ticks_per_tag-1);
const long faktor = mfactor[letzter_monat] + ( ( (mfactor[(letzter_monat+1)%12]-mfactor[letzter_monat])*(ticks_this_month>>12) ) >> (karte_t::ticks_bits_per_tag-12) );
// just remember them
const sint16 old_snowline = snowline;
const sint16 old_season = season;
// and calculate new values
season=month_to_season[letzter_monat]; // (2+letzter_monat/3)&3; // summer always zero
const int winterline = einstellungen->get_winter_snowline();
const int summerline = einstellungen->get_climate_borders()[arctic_climate]+1;
snowline = summerline - (sint16)(((summerline-winterline)*faktor)/100);
snowline = (snowline*Z_TILE_STEP) + grundwasser;
// changed => we update all tiles ...
return (old_snowline!=snowline || old_season!=season);
}
// read height data from bmp or ppm files
bool karte_t::get_height_data_from_file( const char *filename, sint8 grundwasser, sint8 *&hfield, sint16 &ww, sint16 &hh, bool update_only_values )
{
FILE *file = fopen(filename, "rb");
if(file) {
char id[3];
// parsing the header of this mixed file format is nottrivial ...
id[0] = fgetc(file);
id[1] = fgetc(file);
id[2] = 0;
if(strcmp(id, "P6")) {
if(strcmp(id, "BM")) {
fclose(file);
dbg->error("karte_t::load_heightfield()","Heightfield has wrong image type %s instead P6/BM", id);
return false;
}
// bitmap format
fseek( file, 10, SEEK_SET );
uint32 data_offset;
sint32 w, h, format, table;
sint16 bit_depth;
#ifdef BIG_ENDIAN
uint32 l;
uint16 s;
fread( &l, 4, 1, file );
data_offset = endian_uint32(&l);
fseek( file, 18, SEEK_SET );
fread( &l, 4, 1, file );
w = (sint32)endian_uint32(&l);
fread( &l, 4, 1, file );
h = (sint32)endian_uint32(&l);
fseek( file, 28, SEEK_SET );
fread( &s, 2, 1, file );
bit_depth = (sint16)endian_uint16(&s);
fread( &l, 4, 1, file );
format = (sint32)endian_uint32(&l);
fseek( file, 46, SEEK_SET );
fread( &l, 4, 1, file );
table = (sint32)endian_uint32(&l);
#else
fread( &data_offset, 4, 1, file );
fseek( file, 18, SEEK_SET );
fread( &w, 4, 1, file );
fread( &h, 4, 1, file );
fseek( file, 28, SEEK_SET );
fread( &bit_depth, 2, 1, file );
fread( &format, 4, 1, file );
fseek( file, 46, SEEK_SET );
fread( &table, 4, 1, file );
#endif
if((bit_depth!=8 && bit_depth!=24) || format>1) {
if(!update_only_values) {
dbg->fatal("karte_t::get_height_data_from_file()","Can only use 8Bit (RLE or normal) or 24 bit bitmaps!");
}
fclose( file );
return false;
}
// skip parsing body
if(update_only_values) {
ww = w;
hh = abs(h);
return true;
}
// now read the data and convert them on the fly
hfield = new sint8[w*h];
memset( hfield, grundwasser, w*h );
if(bit_depth==8) {
// convert color tables to height levels
if(table==0) {
table = 256;
}
sint8 h_table[256];
fseek( file, 54, SEEK_SET );
for( int i=0; i<table; i++ ) {
int B = fgetc(file);
int G = fgetc(file);
int R = fgetc(file);
fgetc(file); // dummy
h_table[i] = ((((R*2+G*3+B)/4 - 224) & 0xFFF0)/16)*Z_TILE_STEP;
}
// now read the data
fseek( file, data_offset, SEEK_SET );
if(format==0) {
// uncompressed (usually mirrored, if h>0)
bool mirror = (h<0);
h = abs(h);
for( sint32 y=0; y<h; y++ ) {
sint32 offset = mirror ? y*w : (h-y-1)*w;
for( sint32 x=0; x<w; x++ ) {
hfield[x+offset] = h_table[fgetc(file)];
}
// skip line offset
if(w&1) {
fgetc(file);
}
}
}
else {
// compressed RLE (reverse y, since mirrored)
sint32 x=0, y=h-1;
while (!feof(file)) {
uint8 Count= fgetc(file);
uint8 ColorIndex = fgetc(file);
if (Count > 0) {
for( sint32 k = 0; k < Count; k++, x++ ) {
hfield[x+(y*w)] = h_table[ColorIndex];
}
} else if (Count == 0) {
sint32 Flag = ColorIndex;
if (Flag == 0) {
// goto next line
x = 0;
y--;
}
else if (Flag == 1) {
// end of bitmap
break;
}
else if (Flag == 2) {
// skip with cursor
x += (uint8)fgetc(file);
y -= (uint8)fgetc(file);
}
else {
// uncompressed run
Count = Flag;
for( sint32 k = 0; k < Count; k++, x++ ) {
hfield[x+y*w] = h_table[(uint8)fgetc(file)];
}
if (ftell(file) & 1) { // alway even offset in file
fseek(file, 1, SEEK_CUR);
}
}
}
}
}
}
else {
// uncompressed 24 bits
bool mirror = (h<0);
h = abs(h);
for( sint32 y=0; y<h; y++ ) {
sint32 offset = mirror ? y*w : (h-y-1)*w;
for( sint32 x=0; x<w; x++ ) {
int B = fgetc(file);
int G = fgetc(file);
int R = fgetc(file);
hfield[x+offset] = ((((R*2+G*3+B)/4 - 224) & 0xFFF0)/16)*Z_TILE_STEP;
}
fseek( file, (4-((w*3)&3))&3, SEEK_CUR ); // skip superfluos bytes at the end of each scanline
}
}
// success ...
fclose(file);
ww = w;
hh = h;
return true;
}
else {
// ppm format
char buf[255];
char *c = id+2;
sint32 param[3]={0,0,0};
for(int index=0; index<3; ) {
// the format is "P6[whitespace]width[whitespace]height[[whitespace bitdepth]]newline]
// however, Photoshop is the first program, that uses space for the first whitespace ...
// so we cater for Photoshop too
while(*c && *c<=32) {
c++;
}
// usually, after P6 there comes a comment with the maker
// but comments can be anywhere
if(*c==0) {
read_line(buf, 255, file);
c = buf;
continue;
}
param[index++] = atoi(c);
while(*c>='0' && *c<='9') {
c++;
}
}
// now the data
sint32 w = param[0];
sint32 h = param[1];
if(param[2]!=255) {
fclose(file);
if(!update_only_values) {
dbg->fatal("karte_t::load_heightfield()","Heightfield has wrong color depth %d", param[2] );
}
return false;
}
// report only values
if(update_only_values) {
ww = w;
hh = h;
return true;
}
// ok, now read them in
hfield = new sint8[w*h];
memset( hfield, grundwasser, w*h );
for(sint16 y=0; y<h; y++) {
for(sint16 x=0; x<w; x++) {
int R = fgetc(file);
int G = fgetc(file);
int B = fgetc(file);
hfield[x+(y*w)] = ((((R*2+G*3+B)/4 - 224) & 0xFFF0)/16)*Z_TILE_STEP;
}
}
// success ...
fclose(file);
ww = w;
hh = h;
return true;
}
}
return false;
}
/**
* Hoehe eines Punktes der Karte mit "perlin noise"
*
* @param frequency in 0..1.0 roughness, the higher the rougher
* @param amplitude in 0..160.0 top height of mountains, may not exceed 160.0!!!
* @author Hj. Malthaner
*/
sint32 karte_t::perlin_hoehe( einstellungen_t *sets, koord k, koord size )
{
switch( sets->get_rotation() ) {
// 0: do nothing
case 1: k = koord(k.y,size.x-k.x); break;
case 2: k = koord(size.x-k.x,size.y-k.y); break;
case 3: k = koord(size.y-k.y,k.x); break;
}
// double perlin_noise_2D(double x, double y, double persistence);
// return ((int)(perlin_noise_2D(x, y, 0.6)*160.0)) & 0xFFFFFFF0;
return ((int)(perlin_noise_2D(k.x, k.y, sets->get_map_roughness())*(double)sets->get_max_mountain_height())) / 16;
}
void
karte_t::calc_hoehe_mit_perlin()
{
display_set_progress_text(translator::translate("Init map ..."));
const int display_total = 16 + get_einstellungen()->get_anzahl_staedte()*4 + get_einstellungen()->get_land_industry_chains();
for(int y=0; y<=get_groesse_y(); y++) {
for(int x=0; x<=get_groesse_x(); x++) {
// Hajo: to Markus: replace the fixed values with your
// settings. Amplitude is the top highness of the
// montains, frequency is something like landscape 'roughness'
// amplitude may not be greater than 160.0 !!!
// please don't allow frequencies higher than 0.8 it'll
// break the AI's pathfinding. Frequency values of 0.5 .. 0.7
// seem to be ok, less is boring flat, more is too crumbled
// the old defaults are given here: f=0.6, a=160.0
koord pos(x,y);
const int h = perlin_hoehe( einstellungen, pos, koord::invalid );
set_grid_hgt( pos, h*Z_TILE_STEP );
// DBG_MESSAGE("karte_t::calc_hoehe_mit_perlin()","%i",h);
}
if(is_display_init()) {
display_progress((y*16)/get_groesse_y(), display_total);
}
else {
printf("X");fflush(NULL);
}
}
print(" - ok\n");fflush(NULL);
}
void karte_t::raise_clean(sint16 x, sint16 y, sint16 h)
{
if(ist_in_gittergrenzen(x, y)) {
const sint32 offset = x + y*(cached_groesse_gitter_x+1);
if( grid_hgts[offset]*Z_TILE_STEP < h ) {
grid_hgts[offset] = h/Z_TILE_STEP;
const koord k(x,y);
#ifndef DOUBLE_GROUNDS
raise_clean(x-1, y-1, h-Z_TILE_STEP);
raise_clean(x , y-1, h-Z_TILE_STEP);
raise_clean(x+1, y-1, h-Z_TILE_STEP);
raise_clean(x-1, y , h-Z_TILE_STEP);
// Punkt selbst hat schon die neue Hoehe
raise_clean(x+1, y , h-Z_TILE_STEP);
raise_clean(x-1, y+1, h-Z_TILE_STEP);
raise_clean(x , y+1, h-Z_TILE_STEP);
raise_clean(x+1, y+1, h-Z_TILE_STEP);
#else
raise_clean(x-1, y-1, h-Z_TILE_STEP*2);
raise_clean(x , y-1, h-Z_TILE_STEP*2);
raise_clean(x+1, y-1, h-Z_TILE_STEP*2);
raise_clean(x-1, y , h-Z_TILE_STEP*2);
// Punkt selbst hat schon die neue Hoehe
raise_clean(x+1, y , h-Z_TILE_STEP*2);
raise_clean(x-1, y+1, h-Z_TILE_STEP*2);
raise_clean(x , y+1, h-Z_TILE_STEP*2);
raise_clean(x+1, y+1, h-Z_TILE_STEP*2);
#endif
}
}
}
void karte_t::cleanup_karte( int xoff, int yoff )
{
// we need a copy to smoothen the map to a realistic level
const sint32 grid_size = (get_groesse_x()+1)*(get_groesse_y()+1);
sint8 *grid_hgts_cpy = new sint8[grid_size];
memcpy( grid_hgts_cpy, grid_hgts, grid_size );
// the trick for smoothing is to raise each tile by one
sint32 i,j;
for(j=0; j<=get_groesse_y(); j++) {
for(i=j>=yoff?0:xoff; i<=get_groesse_x(); i++) {
raise_clean(i,j, (grid_hgts_cpy[i+j*(get_groesse_x()+1)]*Z_TILE_STEP)+Z_TILE_STEP );
}
}
delete [] grid_hgts_cpy;
// but to leave the map unchanged, we lower the height again
for(j=0; j<=get_groesse_y(); j++) {
for(i=j>=yoff?0:xoff; i<=get_groesse_x(); i++) {
grid_hgts[i+j*(get_groesse_x()+1)] -= Z_TILE_STEP;
}
}
// now lower the corners to ground level
for(i=0; i<get_groesse_x(); i++) {
lower_to(i, 0, grundwasser,false);
lower_to(i, get_groesse_y(), grundwasser,false);
}
for(i=0; i<=get_groesse_y(); i++) {
lower_to(0, i, grundwasser,false);
lower_to(get_groesse_x(), i, grundwasser,false);
}
for(i=0; i<=get_groesse_x(); i++) {
raise_to(i, 0, grundwasser,false);
raise_to(i, get_groesse_y(), grundwasser,false);
}
for(i=0; i<=get_groesse_y(); i++) {
raise_to(0, i, grundwasser,false);
raise_to(get_groesse_x(), i, grundwasser,false);
}
// recalculate slopes and water tiles
for( j=0; j<get_groesse_y(); j++ ) {
for( i=(j>=yoff)?0:xoff; i<get_groesse_x(); i++ ) {
planquadrat_t *pl = access(i,j);
grund_t *gr = pl->get_kartenboden();
koord k(i,j);
uint8 slope = calc_natural_slope(k);
gr->set_pos(koord3d(k,min_hgt(k)));
if( gr->get_typ()!=grund_t::wasser && max_hgt(k) <= get_grundwasser() ) {
// below water but ground => convert
pl->kartenboden_setzen(new wasser_t(this, gr->get_pos()) );
}
else if( gr->get_typ()==grund_t::wasser && max_hgt(k) > get_grundwasser() ) {
// water above ground => to ground
pl->kartenboden_setzen(new boden_t(this, gr->get_pos(), slope ) );
}
else {
gr->set_grund_hang( slope );
}
pl->get_kartenboden()->calc_bild();
}
}
}
// karte_t methoden
void
karte_t::destroy()
{
DBG_MESSAGE("karte_t::destroy()", "destroying world");
// rotate the map until it can be saved
for( int i=0; i<4 && nosave; i++ ) {
DBG_MESSAGE("karte_t::destroy()", "rotating");
nosave = false;
rotate90();
}
if(nosave) {
dbg->fatal( "karte_t::destroy()","Map cannot be cleanly destroyed in any rotation!" );
}
DBG_MESSAGE("karte_t::destroy()", "label clear");
labels.clear();
if(zeiger) {
zeiger->set_pos(koord3d::invalid);
delete zeiger;
zeiger = NULL;
}
// alle convois aufräumen
while (!convoi_array.empty()) {
convoihandle_t cnv = convoi_array.back();
cnv->destroy();
}
convoi_array.clear();
DBG_MESSAGE("karte_t::destroy()", "convois destroyed");
// alle haltestellen aufräumen
haltestelle_t::destroy_all(this);
DBG_MESSAGE("karte_t::destroy()", "stops destroyed");
// entfernt alle synchronen objekte aus der liste
sync_list.clear();
DBG_MESSAGE("karte_t::destroy()", "sync list cleared");
// delete towns first (will also delete all their houses)
// for the next game we need to remember the desired number ...
sint32 no_of_cities=einstellungen->get_anzahl_staedte();
while (!stadt.empty()) {
rem_stadt(stadt.front());
}
einstellungen->set_anzahl_staedte(no_of_cities);
DBG_MESSAGE("karte_t::destroy()", "towns destroyed");
// dinge aufräumen
cached_groesse_gitter_x = cached_groesse_gitter_y = 1;
cached_groesse_karte_x = cached_groesse_karte_y = 0;
if(plan) {
delete [] plan;
plan = NULL;
}
DBG_MESSAGE("karte_t::destroy()", "planquadrat destroyed");
// gitter aufräumen
if(grid_hgts) {
delete [] grid_hgts;
grid_hgts = NULL;
}
// marker aufräumen
marker.init(0,0);
DBG_MESSAGE("karte_t::destroy()", "marker destroyed");
// spieler aufräumen
for(int i=0; i<MAX_PLAYER_COUNT; i++) {
if(spieler[i]) {
delete spieler[i];
spieler[i] = NULL;
}
}
DBG_MESSAGE("karte_t::destroy()", "player destroyed");
// clear all ids
simlinemgmt_t::init_line_ids();
DBG_MESSAGE("karte_t::destroy()", "lines destroyed");
// alle fabriken aufräumen
slist_iterator_tpl<fabrik_t*> fab_iter(fab_list);
while(fab_iter.next()) {
delete fab_iter.get_current();
}
fab_list.clear();
DBG_MESSAGE("karte_t::destroy()", "factories destroyed");
// hier nur entfernen, aber nicht löschen
ausflugsziele.clear();
DBG_MESSAGE("karte_t::destroy()", "attraction list destroyed");
delete scenario;
scenario = NULL;
assert( depot_t::get_depot_list().empty() );
DBG_MESSAGE("karte_t::destroy()", "world destroyed");
printf("World destroyed.\n");
msg->clear();
}
void karte_t::add_convoi(convoihandle_t &cnv)
{
assert(cnv.is_bound());
convoi_array.append_unique(cnv);
cnv->get_besitzer()->buche( 1, COST_ALL_CONVOIS );
}
void karte_t::rem_convoi(convoihandle_t& cnv)
{
convoi_array.remove(cnv);
cnv->get_besitzer()->buche( -1, COST_ALL_CONVOIS );
}
/**
* Zugriff auf das Städte Array.
* @author Hj. Malthaner
*/
const stadt_t *karte_t::get_random_stadt() const
{
return stadt.at_weight(simrand(stadt.get_sum_weight()));
}
void karte_t::add_stadt(stadt_t *s)
{
einstellungen->set_anzahl_staedte(einstellungen->get_anzahl_staedte()+1);
stadt.append(s, s->get_einwohner(), 64);
}
/**
* Removes town from map, houses will be left overs
* @author prissi
*/
bool karte_t::rem_stadt(stadt_t *s)
{
if(s == NULL || stadt.empty()) {
// no town there to delete ...
return false;
}
// reduce number of towns
if(s->get_name()) { DBG_MESSAGE("karte_t::rem_stadt()", s->get_name() ); }
stadt.remove(s);
DBG_MESSAGE("karte_t::rem_stadt()", "reduce city to %i", einstellungen->get_anzahl_staedte()-1 );
einstellungen->set_anzahl_staedte(einstellungen->get_anzahl_staedte()-1);
// remove all links from factories
DBG_MESSAGE("karte_t::rem_stadt()", "fab_list %i", fab_list.get_count() );
slist_iterator_tpl<fabrik_t *> iter(fab_list);
while(iter.next()) {
(iter.get_current())->remove_arbeiterziel(s);
}
// ok, we can delete this
DBG_MESSAGE("karte_t::rem_stadt()", "delete" );
delete s;
return true;
}
// just allocates space;
void karte_t::init_felder()
{
assert(plan==0);
plan = new planquadrat_t[get_groesse_x()*get_groesse_y()];
grid_hgts = new sint8[(get_groesse_x()+1)*(get_groesse_y()+1)];
memset(grid_hgts, 0, sizeof(sint8)*(get_groesse_x()+1)*(get_groesse_y()+1));
marker.init(get_groesse_x(),get_groesse_y());
simlinemgmt_t::init_line_ids();
win_set_welt( this );
reliefkarte_t::get_karte()->set_welt(this);
for(int i=0; i<MAX_PLAYER_COUNT ; i++) {
// old default: AI 3 passenger, other goods
spieler[i] = (i<2) ? new spieler_t(this,i) : NULL;
}
active_player = spieler[0];
active_player_nr = 0;
// defaults without timeline
average_speed[0] = 60;
average_speed[1] = 80;
average_speed[2] = 40;
average_speed[3] = 350;
// clear world records
max_road_speed.speed = 0;
max_rail_speed.speed = 0;
max_monorail_speed.speed = 0;
max_maglev_speed.speed = 0;
max_narrowgauge_speed.speed = 0;
max_ship_speed.speed = 0;
max_air_speed.speed = 0;
// make timer loop invalid
for( int i=0; i<32; i++ ) {
last_frame_ms[i] = 0x7FFFFFFFu;
last_step_nr[i] = 0xFFFFFFFFu;
}
last_frame_idx = 0;
pending_season_change = 0;
// init global history
for (int year=0; year<MAX_WORLD_HISTORY_YEARS; year++) {
for (int cost_type=0; cost_type<MAX_WORLD_COST; cost_type++) {
finance_history_year[year][cost_type] = 0;
}
}
for (int month=0; month<MAX_WORLD_HISTORY_MONTHS; month++) {
for (int cost_type=0; cost_type<MAX_WORLD_COST; cost_type++) {
finance_history_month[month][cost_type] = 0;
}
}
convoihandle_t::init( 1024 );
linehandle_t::init( 1024 );
halthandle_t::init( 1024 );
scenario = new scenario_t(this);
nosave = false;
}
void karte_t::create_rivers( sint16 number )
{
// First check, wether there is a canal:
const weg_besch_t* river_besch = wegbauer_t::get_besch( umgebung_t::river_type[umgebung_t::river_types-1], 0 );
if( river_besch == NULL ) {
// should never reaching here ...
dbg->warning("karte_t::create_rivers()","There is no river defined!\n");
return;
}
// create a vector of the highest points
vector_tpl<koord> water_tiles;
weighted_vector_tpl<koord> mountain_tiles;
sint8 last_height = 1;
koord last_koord(0,0);
const sint16 max_dist = cached_groesse_karte_y+cached_groesse_karte_x;
// trunk of 16 will ensure that rivers are long enough apart ...
for( sint16 y = 8; y < cached_groesse_karte_y; y+=16 ) {
for( sint16 x = 8; x < cached_groesse_karte_x; x+=16 ) {
koord k(x,y);
grund_t *gr = lookup_kartenboden(k);
const sint8 h = gr->get_hoehe()-get_grundwasser();
if( gr->ist_wasser() ) {
// may be good to start a river here
water_tiles.append(k);
}
else if( h>=last_height || koord_distance(last_koord,k)>simrand(max_dist) ) {
// something worth to add here
if( h>last_height ) {
last_height = h;
}
last_koord = k;
mountain_tiles.append( k, h, 256 );
}
}
}
if( water_tiles.get_count() == 0 ) {
dbg->message("karte_t::create_rivers()","There aren't any water tiles!\n");
return;
}
// now make rivers
uint8 retrys = 0;
while( number>0 && mountain_tiles.get_count()>0 && retrys++<100 ) {
koord start = mountain_tiles.at_weight( simrand(mountain_tiles.get_sum_weight()) );
koord end = water_tiles[ simrand(water_tiles.get_count()) ];
sint16 dist = koord_distance(start,end);
if( dist > einstellungen->get_min_river_length() && dist < einstellungen->get_max_river_length() ) {
// should be at least of decent length
wegbauer_t riverbuilder(this, spieler[1]);
riverbuilder.route_fuer(wegbauer_t::river, river_besch);
riverbuilder.set_maximum( dist*50 );
riverbuilder.calc_route( lookup_kartenboden(end)->get_pos(), lookup_kartenboden(start)->get_pos() );
if( riverbuilder.max_n >= einstellungen->get_min_river_length() ) {
// do not built too short rivers
riverbuilder.baue();
number --;
retrys = 0;
}
mountain_tiles.remove( start );
}
}
// we gave up => tell te user
if( number>0 ) {
dbg->warning( "karte_t::create_rivers()","Too many rivers requested! (%i not constructed)", number );
}
}
void karte_t::distribute_groundobjs_cities(int new_anzahl_staedte, sint16 old_x, sint16 old_y)
{
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","distributing groundobjs");
if( umgebung_t::river_types>0 && einstellungen->get_river_number()>0 ) {
create_rivers( einstellungen->get_river_number() );
}
if( umgebung_t::ground_object_probability > 0 ) {
// add eyecandy like rocky, moles, flowers, ...
koord k;
sint32 queried = simrand(umgebung_t::ground_object_probability*2);
for( k.y=0; k.y<get_groesse_y(); k.y++ ) {
for( k.x=(k.y<old_y)?old_x:0; k.x<get_groesse_x(); k.x++ ) {
grund_t *gr = lookup_kartenboden(k);
if( gr->get_typ()==grund_t::boden && !gr->hat_wege() ) {
queried --;
if( queried<0 ) {
const groundobj_besch_t *besch = groundobj_t::random_groundobj_for_climate( get_climate(gr->get_hoehe()), gr->get_grund_hang() );
if(besch) {
queried = simrand(umgebung_t::ground_object_probability*2);
gr->obj_add( new groundobj_t( this, gr->get_pos(), besch ) );
}
}
}
}
}
}
print("Creating cities ...\n");
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","prepare cities");
vector_tpl<koord> *pos = stadt_t::random_place(this, new_anzahl_staedte, old_x, old_y);
if( !pos->empty() ) {
const uint32 old_anzahl_staedte = stadt.get_count();
new_anzahl_staedte = pos->get_count();
// prissi if we could not generate enough positions ...
einstellungen->set_anzahl_staedte( old_anzahl_staedte + new_anzahl_staedte ); // new number of towns (if we did not found enough positions) ...
int old_progress = 16;
const int max_display_progress=16+2*einstellungen->get_anzahl_staedte()
+2*new_anzahl_staedte+((old_x==0)?einstellungen->get_land_industry_chains():0);
// Ansicht auf erste Stadt zentrieren
if (old_x+old_y == 0)
change_world_position( koord3d((*pos)[0], min_hgt((*pos)[0])) );
// Loop only new cities:
for( int i=0; i<new_anzahl_staedte; i++ ) {
// int citizens=(int)(einstellungen->get_mittlere_einwohnerzahl()*0.9);
// citizens = citizens/10+simrand(2*citizens+1);
int current_citicens = (2500l * einstellungen->get_mittlere_einwohnerzahl()) /(simrand(20000)+100);
stadt_t* s = new stadt_t(spieler[1], (*pos)[i], current_citicens);
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","Erzeuge stadt %i with %ld inhabitants",i,(s->get_city_history_month())[HIST_CITICENS] );
stadt.append(s, current_citicens, 64);
if(is_display_init()) {
old_progress ++;
display_progress(old_progress, max_display_progress);
}
else {
printf("*");fflush(NULL);
}
}
delete pos;
for( uint32 i=old_anzahl_staedte; i<stadt.get_count(); i++ ) {
// Hajo: do final init after world was loaded/created
stadt[i]->laden_abschliessen();
// the growth is slow, so update here the progress bar
if(is_display_init()) {
old_progress ++;
display_progress(old_progress, max_display_progress);
}
else {
printf("*");fflush(NULL);
}
}
finance_history_year[0][WORLD_TOWNS] = finance_history_month[0][WORLD_TOWNS] = stadt.get_count();
finance_history_year[0][WORLD_CITICENS] = finance_history_month[0][WORLD_CITICENS] = last_month_bev;
// Hajo: connect some cities with roads
const weg_besch_t* besch = umgebung_t::intercity_road_type ? wegbauer_t::get_besch(umgebung_t::intercity_road_type) : NULL;
if(besch == 0) {
dbg->warning("karte_t::init()", "road type '%s' not found", umgebung_t::intercity_road_type);
// Hajo: try some default (might happen with timeline ... )
besch = wegbauer_t::weg_search(road_wt,80,get_timeline_year_month(),weg_t::type_flat);
}
wegbauer_t bauigel (this, spieler[1] );
bauigel.route_fuer(wegbauer_t::strasse, besch, tunnelbauer_t::find_tunnel(road_wt,15,get_timeline_year_month()), brueckenbauer_t::find_bridge(road_wt,15,get_timeline_year_month()) );
bauigel.set_keep_existing_ways(true);
bauigel.set_maximum(umgebung_t::intercity_road_length);
// **** intercity road construction
// progress bar data
int old_progress_count = 16+2*new_anzahl_staedte;
int count = 0;
const int max_count=(einstellungen->get_anzahl_staedte()*(einstellungen->get_anzahl_staedte()-1))/2
- (old_anzahl_staedte*(old_anzahl_staedte-1))/2;
// something to do??
if( max_count > 0 ) {
// find townhall of city i and road in front of it
vector_tpl<koord3d> k;
for( int i = 0; i < einstellungen->get_anzahl_staedte(); i++ ) {
koord k1 = stadt[i]->get_pos();
const gebaeude_t* gb = dynamic_cast<gebaeude_t*>(lookup_kartenboden(k1)->first_obj());
if( gb && gb->ist_rathaus() ) {
k1.y += gb->get_tile()->get_besch()->get_h(gb->get_tile()->get_layout());
k.append( lookup_kartenboden(k1)->get_pos() );
}
else {
k.append( koord3d::invalid );
}
}
// compute all distances
uint8 conn_comp=1; // current connection component for phase 0
vector_tpl<uint8> city_flag; // city already connected to the graph? >0 nr of connection component
array2d_tpl<sint32> city_dist(einstellungen->get_anzahl_staedte(), einstellungen->get_anzahl_staedte());
for( int i = 0; i < einstellungen->get_anzahl_staedte(); i++ ) {
for( int j = max(i + 1, old_anzahl_staedte); j < einstellungen->get_anzahl_staedte(); j++ ) {
city_dist.at(i,j) = koord_distance(k[i], k[j]);
city_dist.at(j,i) = city_dist.at(i,j);
// count unbuildable connections
if( city_dist.at(i,j) >= umgebung_t::intercity_road_length ) {
count++;
}
}
city_flag.append( i < old_anzahl_staedte ? conn_comp : 0 );
// progress bar stuff
if( is_display_init() && count<=max_count ) {
int progress_count = 16+ 2*new_anzahl_staedte+ (count*einstellungen->get_anzahl_staedte()*2)/max_count;
if( old_progress_count != progress_count ) {
display_progress(progress_count, max_display_progress );
old_progress_count = progress_count;
}
}
}
// mark first town as connected
if (old_anzahl_staedte==0) {
city_flag[0]=conn_comp;
}
// get a default vehikel
route_t verbindung;
fahrer_t* test_driver;
vehikel_besch_t test_drive_besch(road_wt, 500, vehikel_besch_t::diesel );
test_driver = vehikelbauer_t::baue(koord3d(), spieler[1], NULL, &test_drive_besch);
bool ready=false;
uint8 phase=0;
// 0 - first phase: built minimum spanning tree (edge weights: city distance)
// 1 - second phase: try to complete the graph, avoid edges that
// == have similar length then already existing connection
// == lead to triangles with an angle >90 deg
while( phase < 2 ) {
ready = true;
koord conn = koord::invalid;
sint32 best = umgebung_t::intercity_road_length;
if( phase == 0 ) {
// loop over all unconnected cities
for( int i = 0; i < einstellungen->get_anzahl_staedte(); i++ ) {
if( city_flag[i] == conn_comp ) {
// loop over all connections to connected cities