forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimworld.cc
6840 lines (5900 loc) · 202 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 Hj. Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
/*
* Hauptklasse fuer Simutrans, Datenstruktur die alles Zusammenhaelt
* Hj. Malthaner, 1997
*/
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
#include "simcity.h"
#include "simcolor.h"
#include "simconvoi.h"
#include "simdebug.h"
#include "simdepot.h"
#include "simfab.h"
#include "display/simgraph.h"
#include "display/viewport.h"
#include "simhalt.h"
#include "display/simimg.h"
#include "siminteraction.h"
#include "simintr.h"
#include "simio.h"
#include "simlinemgmt.h"
#include "simloadingscreen.h"
#include "simmenu.h"
#include "simmesg.h"
#include "simskin.h"
#include "simsound.h"
#include "simsys.h"
#include "simticker.h"
#include "simunits.h"
#include "simversion.h"
#include "display/simview.h"
#include "simtool.h"
#include "gui/simwin.h"
#include "simworld.h"
#include "tpl/vector_tpl.h"
#include "tpl/binary_heap_tpl.h"
#include "boden/boden.h"
#include "boden/wasser.h"
#include "old_blockmanager.h"
#include "vehicle/simvehicle.h"
#include "vehicle/simroadtraffic.h"
#include "vehicle/movingobj.h"
#include "boden/wege/schiene.h"
#include "obj/zeiger.h"
#include "obj/baum.h"
#include "obj/signal.h"
#include "obj/roadsign.h"
#include "obj/wayobj.h"
#include "obj/groundobj.h"
#include "obj/gebaeude.h"
#include "obj/leitung2.h"
#include "gui/password_frame.h"
#include "gui/messagebox.h"
#include "gui/help_frame.h"
#include "gui/karte.h"
#include "gui/player_frame_t.h"
#include "network/network.h"
#include "network/network_file_transfer.h"
#include "network/network_socket_list.h"
#include "network/network_cmd_ingame.h"
#include "dataobj/height_map_loader.h"
#include "dataobj/ribi.h"
#include "dataobj/translator.h"
#include "dataobj/loadsave.h"
#include "dataobj/scenario.h"
#include "dataobj/settings.h"
#include "dataobj/environment.h"
#include "dataobj/powernet.h"
#include "dataobj/records.h"
#include "utils/cbuffer_t.h"
#include "utils/simrandom.h"
#include "utils/simstring.h"
#include "network/memory_rw.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 "player/simplay.h"
#include "player/finance.h"
#include "player/ai_passenger.h"
#include "player/ai_goods.h"
#include "player/ai_scripted.h"
// forward declaration - management of rotation for scripting
namespace script_api
{
void rotate90();
void new_world();
};
// 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 uint32 last_clients = -1;
static uint8 last_active_player_nr = 0;
static std::string last_network_game;
karte_t* karte_t::world = NULL;
stringhashtable_tpl<karte_t::missing_level_t>missing_pak_names;
#ifdef MULTI_THREAD
#include "utils/simthread.h"
#include <semaphore.h>
bool spawned_world_threads=false; // global job indicator array
static simthread_barrier_t world_barrier_start;
static simthread_barrier_t world_barrier_end;
// to start a thread
typedef struct{
karte_t *welt;
int thread_num;
sint16 x_step;
sint16 x_world_max;
sint16 y_min;
sint16 y_max;
sem_t* wait_for_previous;
sem_t* signal_to_next;
xy_loop_func function;
bool keep_running;
} world_thread_param_t;
// now the paramters
static world_thread_param_t world_thread_param[MAX_THREADS];
void *karte_t::world_xy_loop_thread(void *ptr)
{
world_thread_param_t *param = reinterpret_cast<world_thread_param_t *>(ptr);
while(true) {
if(param->keep_running) {
simthread_barrier_wait( &world_barrier_start ); // wait for all to start
}
sint16 x_min = 0;
sint16 x_max = param->x_step;
while( x_min < param->x_world_max ) {
// wait for predecessor to finish its block
if( param->wait_for_previous ) {
sem_wait( param->wait_for_previous );
}
(param->welt->*(param->function))(x_min, x_max, param->y_min, param->y_max);
// signal to next thread that we finished one block
if( param->signal_to_next ) {
sem_post( param->signal_to_next );
}
x_min = x_max;
x_max = min(x_max + param->x_step, param->x_world_max);
}
if(param->keep_running) {
simthread_barrier_wait( &world_barrier_end ); // wait for all to finish
}
else {
return NULL;
}
}
return ptr;
}
#endif
void karte_t::world_xy_loop(xy_loop_func function, uint8 flags)
{
const bool use_grids = (flags & GRIDS_FLAG) == GRIDS_FLAG;
uint16 max_x = use_grids?(cached_grid_size.x+1):cached_grid_size.x;
uint16 max_y = use_grids?(cached_grid_size.y+1):cached_grid_size.y;
#ifdef MULTI_THREAD
set_random_mode( INTERACTIVE_RANDOM ); // do not allow simrand() here!
const bool sync_x_steps = (flags & SYNCX_FLAG) == SYNCX_FLAG;
// semaphores to synchronize progress in x direction
sem_t sems[MAX_THREADS-1];
for( int t = 0; t < env_t::num_threads; t++ ) {
if( sync_x_steps && t < env_t::num_threads - 1 ) {
sem_init(&sems[t], 0, 0);
}
world_thread_param[t].welt = this;
world_thread_param[t].thread_num = t;
world_thread_param[t].x_step = min( 64, max_x / env_t::num_threads );
world_thread_param[t].x_world_max = max_x;
world_thread_param[t].y_min = (t * max_y) / env_t::num_threads;
world_thread_param[t].y_max = ((t + 1) * max_y) / env_t::num_threads;
world_thread_param[t].function = function;
world_thread_param[t].wait_for_previous = sync_x_steps && t > 0 ? &sems[t-1] : NULL;
world_thread_param[t].signal_to_next = sync_x_steps && t < env_t::num_threads - 1 ? &sems[t] : NULL;
world_thread_param[t].keep_running = t < env_t::num_threads - 1;
}
if( !spawned_world_threads ) {
// we can do the parallel display using posix threads ...
pthread_t thread[MAX_THREADS];
/* Initialize and set thread detached attribute */
pthread_attr_t attr;
pthread_attr_init( &attr );
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
// init barrier
simthread_barrier_init( &world_barrier_start, NULL, env_t::num_threads );
simthread_barrier_init( &world_barrier_end, NULL, env_t::num_threads );
for( int t = 0; t < env_t::num_threads - 1; t++ ) {
if( pthread_create( &thread[t], &attr, world_xy_loop_thread, (void *)&world_thread_param[t] ) ) {
dbg->fatal( "karte_t::world_xy_loop()", "cannot multithread, error at thread #%i", t+1 );
return;
}
}
spawned_world_threads = true;
pthread_attr_destroy( &attr );
}
// and start processing
simthread_barrier_wait( &world_barrier_start );
// the last we can run ourselves
world_xy_loop_thread(&world_thread_param[env_t::num_threads-1]);
simthread_barrier_wait( &world_barrier_end );
// return from thread
for( int t = 0; t < env_t::num_threads - 1; t++ ) {
if( sync_x_steps ) {
sem_destroy(&sems[t]);
}
}
clear_random_mode( INTERACTIVE_RANDOM ); // do not allow simrand() here!
#else
// slow serial way of display
(this->*function)( 0, max_x, 0, max_y );
#endif
}
void checklist_t::rdwr(memory_rw_t *buffer)
{
buffer->rdwr_long(random_seed);
buffer->rdwr_short(halt_entry);
buffer->rdwr_short(line_entry);
buffer->rdwr_short(convoy_entry);
}
int checklist_t::print(char *buffer, const char *entity) const
{
return sprintf(buffer, "%s=[rand=%u halt=%u line=%u cnvy=%u] ", entity, random_seed, halt_entry, line_entry, convoy_entry);
}
void karte_t::recalc_season_snowline(bool set_pending)
{
static const sint8 mfactor[12] = { 99, 95, 80, 50, 25, 10, 0, 5, 20, 35, 65, 85 };
static const uint8 month_to_season[12] = { 2, 2, 2, 3, 3, 0, 0, 0, 0, 1, 1, 2 };
// calculate snowline with day precision
// use linear interpolation
const sint32 ticks_this_month = get_zeit_ms() & (karte_t::ticks_per_world_month - 1);
const sint32 factor = mfactor[last_month] + ( ( (mfactor[(last_month + 1) % 12] - mfactor[last_month]) * (ticks_this_month >> 12) ) >> (karte_t::ticks_per_world_month_shift - 12) );
// just remember them
const uint8 old_season = season;
const sint16 old_snowline = snowline;
// and calculate new values
season = month_to_season[last_month]; // (2+last_month/3)&3; // summer always zero
if( old_season != season && set_pending ) {
pending_season_change++;
}
const sint16 winterline = settings.get_winter_snowline();
const sint16 summerline = settings.get_climate_borders()[arctic_climate] + 1;
snowline = summerline - (sint16)(((summerline-winterline)*factor)/100) + grundwasser;
if( old_snowline != snowline && set_pending ) {
pending_snowline_change++;
}
}
void karte_t::perlin_hoehe_loop( sint16 x_min, sint16 x_max, sint16 y_min, sint16 y_max )
{
for( int y = y_min; y < y_max; y++ ) {
for( int x = x_min; x < x_max; x++ ) {
// loop all tiles
koord k(x,y);
sint16 const h = perlin_hoehe(&settings, k, koord(0, 0));
set_grid_hgt( k, (sint8) h);
}
}
}
/**
* 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(settings_t const* const sets, koord k, koord const size)
{
// 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
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;
k = k + koord(sets->get_origin_x(), sets->get_origin_y());
return ((int)(perlin_noise_2D(k.x, k.y, sets->get_map_roughness())*(double)sets->get_max_mountain_height())) / 16;
}
void karte_t::cleanup_grounds_loop( sint16 x_min, sint16 x_max, sint16 y_min, sint16 y_max )
{
for( int y = y_min; y < y_max; y++ ) {
for( int x = x_min; x < x_max; x++ ) {
planquadrat_t *pl = access_nocheck(x,y);
grund_t *gr = pl->get_kartenboden();
koord k(x,y);
uint8 slope = calc_natural_slope(k);
sint8 height = min_hgt_nocheck(k);
sint8 water_hgt = get_water_hgt_nocheck(k);
if( height < water_hgt) {
const sint8 disp_hn_sw = max( height + corner_sw(slope), water_hgt );
const sint8 disp_hn_se = max( height + corner_se(slope), water_hgt );
const sint8 disp_hn_ne = max( height + corner_ne(slope), water_hgt );
const sint8 disp_hn_nw = max( height + corner_nw(slope), water_hgt );
height = water_hgt;
slope = (disp_hn_sw - height) + ((disp_hn_se - height) * 3) + ((disp_hn_ne - height) * 9) + ((disp_hn_nw - height) * 27);
}
gr->set_pos( koord3d( k, height) );
if( gr->get_typ() != grund_t::wasser && max_hgt_nocheck(k) <= water_hgt ) {
// below water but ground => convert
pl->kartenboden_setzen( new wasser_t(gr->get_pos()) );
}
else if( gr->get_typ() == grund_t::wasser && max_hgt_nocheck(k) > water_hgt ) {
// water above ground => to ground
pl->kartenboden_setzen( new boden_t(gr->get_pos(), slope ) );
}
else {
gr->set_grund_hang( slope );
}
if( max_hgt_nocheck(k) > water_hgt ) {
set_water_hgt(k, grundwasser-4);
}
}
}
}
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_size().x+1)*(sint32)(get_size().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_size().y; j++) {
for(i=j>=yoff?0:xoff; i<=get_size().x; i++) {
raise_grid_to(i,j, grid_hgts_cpy[i+j*(get_size().x+1)] + 1);
}
}
delete [] grid_hgts_cpy;
// but to leave the map unchanged, we lower the height again
for(j=0; j<=get_size().y; j++) {
for(i=j>=yoff?0:xoff; i<=get_size().x; i++) {
grid_hgts[i+j*(get_size().x+1)] --;
}
}
if( xoff==0 && yoff==0 ) {
world_xy_loop(&karte_t::cleanup_grounds_loop, 0);
}
else {
cleanup_grounds_loop( 0, get_size().x, yoff, get_size().y );
cleanup_grounds_loop( xoff, get_size().x, 0, yoff );
}
}
void karte_t::destroy()
{
is_sound = false; // karte_t::play_sound_area_clipped needs valid zeiger
destroying = true;
DBG_MESSAGE("karte_t::destroy()", "destroying world");
uint32 max_display_progress = 256+stadt.get_count()*10 + haltestelle_t::get_alle_haltestellen().get_count() + convoi_array.get_count() + (cached_size.x*cached_size.y)*2;
uint32 old_progress = 0;
loadingscreen_t ls( translator::translate("Destroying map ..."), max_display_progress, true );
// rotate the map until it can be saved
nosave_warning = false;
if( nosave ) {
max_display_progress += 256;
for( int i=0; i<4 && nosave; i++ ) {
DBG_MESSAGE("karte_t::destroy()", "rotating");
rotate90();
}
old_progress += 256;
ls.set_max( max_display_progress );
ls.set_progress( old_progress );
}
if(nosave) {
dbg->fatal( "karte_t::destroy()","Map cannot be cleanly destroyed in any rotation!" );
}
goods_in_game.clear();
DBG_MESSAGE("karte_t::destroy()", "label clear");
labels.clear();
if(zeiger) {
zeiger->set_pos(koord3d::invalid);
delete zeiger;
zeiger = NULL;
}
old_progress += 256;
ls.set_progress( old_progress );
// removes all moving stuff from the sync_step
sync.clear();
sync_eyecandy.clear();
sync_way_eyecandy.clear();
old_progress += cached_size.x*cached_size.y;
ls.set_progress( old_progress );
DBG_MESSAGE("karte_t::destroy()", "sync list cleared");
// alle convois aufraeumen
while (!convoi_array.empty()) {
convoihandle_t cnv = convoi_array.back();
cnv->destroy();
old_progress ++;
if( (old_progress&0x00FF) == 0 ) {
ls.set_progress( old_progress );
}
}
convoi_array.clear();
DBG_MESSAGE("karte_t::destroy()", "convois destroyed");
// alle haltestellen aufraeumen
old_progress += haltestelle_t::get_alle_haltestellen().get_count();
haltestelle_t::destroy_all();
DBG_MESSAGE("karte_t::destroy()", "stops destroyed");
ls.set_progress( old_progress );
// remove all target cities (we can skip recalculation anyway)
FOR(slist_tpl<fabrik_t*>, const f, fab_list) {
f->clear_target_cities();
}
// delete towns first (will also delete all their houses)
// for the next game we need to remember the desired number ...
sint32 const no_of_cities = settings.get_city_count();
for( uint32 i=0; !stadt.empty(); i++ ) {
rem_stadt(stadt.front());
old_progress += 10;
if( (i&0x00F) == 0 ) {
ls.set_progress( old_progress );
}
}
settings.set_city_count(no_of_cities);
DBG_MESSAGE("karte_t::destroy()", "towns destroyed");
ls.set_progress( old_progress );
// dinge aufraeumen
cached_grid_size.x = cached_grid_size.y = 1;
cached_size.x = cached_size.y = 0;
delete [] plan;
plan = NULL;
DBG_MESSAGE("karte_t::destroy()", "planquadrat destroyed");
old_progress += (cached_size.x*cached_size.y)/2;
ls.set_progress( old_progress );
// gitter aufraeumen
delete [] grid_hgts;
grid_hgts = NULL;
delete [] water_hgts;
water_hgts = NULL;
// player cleanup
for(int i=0; i<MAX_PLAYER_COUNT; i++) {
delete players[i];
players[i] = NULL;
}
DBG_MESSAGE("karte_t::destroy()", "player destroyed");
old_progress += (cached_size.x*cached_size.y)/4;
ls.set_progress( old_progress );
// alle fabriken aufraeumen
FOR(slist_tpl<fabrik_t*>, const f, fab_list) {
delete f;
}
fab_list.clear();
DBG_MESSAGE("karte_t::destroy()", "factories destroyed");
// hier nur entfernen, aber nicht loeschen
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");
dbg->important("World destroyed.");
destroying = false;
}
void karte_t::add_convoi(convoihandle_t const cnv)
{
assert(cnv.is_bound());
convoi_array.append_unique(cnv);
}
void karte_t::rem_convoi(convoihandle_t const cnv)
{
convoi_array.remove(cnv);
}
void karte_t::add_stadt(stadt_t *s)
{
settings.set_city_count(settings.get_city_count() + 1);
stadt.append(s, s->get_einwohner());
// Knightly : add links between this city and other cities as well as attractions
FOR(weighted_vector_tpl<stadt_t*>, const c, stadt) {
c->add_target_city(s);
}
s->recalc_target_cities();
s->recalc_target_attractions();
}
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", s->get_name());
}
stadt.remove(s);
DBG_DEBUG4("karte_t::rem_stadt()", "reduce city to %i", settings.get_city_count() - 1);
settings.set_city_count(settings.get_city_count() - 1);
// Knightly : remove links between this city and other cities
FOR(weighted_vector_tpl<stadt_t*>, const c, stadt) {
c->remove_target_city(s);
}
// remove all links from factories
DBG_DEBUG4("karte_t::rem_stadt()", "fab_list %i", fab_list.get_count() );
FOR(slist_tpl<fabrik_t*>, const f, fab_list) {
f->remove_target_city(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);
uint32 const x = get_size().x;
uint32 const y = get_size().y;
plan = new planquadrat_t[x * y];
grid_hgts = new sint8[(x + 1) * (y + 1)];
max_height = min_height = 0;
MEMZERON(grid_hgts, (x + 1) * (y + 1));
water_hgts = new sint8[x * y];
MEMZERON(water_hgts, x * y);
win_set_world( this );
reliefkarte_t::get_karte()->init();
for(int i=0; i<MAX_PLAYER_COUNT ; i++) {
// old default: AI 3 passenger, other goods
players[i] = (i<2) ? new player_t(i) : NULL;
}
active_player = players[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
records->clear_speed_records();
// 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;
pending_snowline_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;
}
}
last_month_bev = 0;
tile_counter = 0;
convoihandle_t::init( 1024 );
linehandle_t::init( 1024 );
halthandle_t::init( 1024 );
vehicle_base_t::set_overtaking_offsets( get_settings().is_drive_left() );
scenario = new scenario_t(this);
nosave_warning = nosave = false;
if (env_t::server) {
nwc_auth_player_t::init_player_lock_server(this);
}
}
void karte_t::set_scenario(scenario_t *s)
{
if (scenario != s) {
delete scenario;
}
scenario = s;
}
void karte_t::create_rivers( sint16 number )
{
// First check, wether there is a canal:
const way_desc_t* river_desc = way_builder_t::get_desc( env_t::river_type[env_t::river_types-1], 0 );
if( river_desc == 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_size.y+cached_size.x;
// trunk of 16 will ensure that rivers are long enough apart ...
for( sint16 y = 8; y < cached_size.y; y+=16 ) {
for( sint16 x = 8; x < cached_size.x; x+=16 ) {
koord k(x,y);
grund_t *gr = lookup_kartenboden_nocheck(k);
const sint8 h = gr->get_hoehe() - get_water_hgt_nocheck(k);
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;
// using h*h as weight would give mountian sources more preferences
// on the other hand most rivers do not string near summits ...
mountain_tiles.append( k, h );
}
}
}
if (water_tiles.empty()) {
dbg->message("karte_t::create_rivers()","There aren't any water tiles!\n");
return;
}
// now make rivers
sint16 retrys = number*2;
while( number > 0 && !mountain_tiles.empty() && retrys>0 ) {
// start with random coordinates
koord const start = pick_any_weighted(mountain_tiles);
mountain_tiles.remove( start );
// build a list of matchin targets
vector_tpl<koord> valid_water_tiles;
for( uint32 i=0; i<water_tiles.get_count(); i++ ) {
sint16 dist = koord_distance(start,water_tiles[i]);
if( settings.get_min_river_length() < dist && dist < settings.get_max_river_length() ) {
valid_water_tiles.append( water_tiles[i] );
}
}
// now try 256 random locations
for( sint32 i=0; i<256 && !valid_water_tiles.empty(); i++ ) {
koord const end = pick_any(valid_water_tiles);
valid_water_tiles.remove( end );
way_builder_t riverbuilder(players[1]);
riverbuilder.init_builder(way_builder_t::river, river_desc);
sint16 dist = koord_distance(start,end);
riverbuilder.set_maximum( dist*50 );
riverbuilder.calc_route( lookup_kartenboden(end)->get_pos(), lookup_kartenboden(start)->get_pos() );
if( riverbuilder.get_count() >= (uint32)settings.get_min_river_length() ) {
// do not built too short rivers
riverbuilder.build();
number --;
retrys++;
break;
}
}
retrys--;
}
// 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_city_count, sint32 new_mean_citizen_count, sint16 old_x, sint16 old_y )
{
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","distributing rivers");
if( env_t::river_types > 0 && settings.get_river_number() > 0 ) {
create_rivers( settings.get_river_number() );
}
dbg->important("Creating cities ...");
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","prepare cities");
vector_tpl<koord> *pos = stadt_t::random_place(new_city_count, old_x, old_y);
if( !pos->empty() ) {
const sint32 old_city_count = stadt.get_count();
new_city_count = pos->get_count();
dbg->important("Creating cities: %d", new_city_count);
// prissi if we could not generate enough positions ...
settings.set_city_count(old_city_count);
int old_progress = 16;
loadingscreen_t ls( translator::translate( "distributing cities" ), 16 + 2 * (old_city_count + new_city_count) + 2 * new_city_count + (old_x == 0 ? settings.get_factory_count() : 0), true, true );
{
// Loop only new cities:
#ifdef DEBUG
uint32 tbegin = dr_time();
#endif
for( int i=0; i<new_city_count; i++ ) {
stadt_t* s = new stadt_t(players[1], (*pos)[i], 1 );
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","Erzeuge stadt %i with %ld inhabitants",i,(s->get_city_history_month())[HIST_CITICENS] );
if (s->get_buildings() > 0) {
add_stadt(s);
}
else {
delete(s);
}
}
// center on first city
if( old_x+old_y == 0 && stadt.get_count()>0) {
viewport->change_world_position( stadt[0]->get_pos() );
}
delete pos;
DBG_DEBUG("karte_t::distribute_groundobjs_cities()","took %lu ms for all towns", dr_time()-tbegin );
uint32 game_start = current_month;
// townhalls available since?
FOR(vector_tpl<building_desc_t const*>, const desc, *hausbauer_t::get_list(building_desc_t::townhall)) {
uint32 intro_year_month = desc->get_intro_year_month();
if( intro_year_month<game_start ) {
game_start = intro_year_month;
}
}
// streets since when?
game_start = max( game_start, way_builder_t::get_earliest_way(road_wt)->get_intro_year_month() );
uint32 original_start_year = current_month;
uint32 original_industry_growth = settings.get_industry_increase_every();
settings.set_industry_increase_every( 0 );
for( uint32 i=old_city_count; i<stadt.get_count(); i++ ) {
// Hajo: do final init after world was loaded/created
stadt[i]->finish_rd();
// int citizens=(int)(new_mean_citizen_count*0.9);
// citizens = citizens/10+simrand(2*citizens+1);
const sint32 citizens = (2500l * new_mean_citizen_count) /(simrand(20000)+100);
sint32 diff = (original_start_year-game_start)/2;
sint32 growth = 32;
sint32 current_bev = 1;
/* grow gradually while aging
* the difference to the current end year will be halved,
* while the growth step is doubled
*/
current_month = game_start;
bool not_updated = false;
bool new_town = true;
while( current_bev < citizens ) {
growth = min( citizens-current_bev, growth*2 );
current_bev += growth;
stadt[i]->change_size( growth, new_town );
// Only "new" for the first change_size call
new_town = false;
if( current_bev > citizens/2 && not_updated ) {
ls.set_progress( ++old_progress );
not_updated = true;
}
current_month += diff;
diff >>= 1;
}
// the growth is slow, so update here the progress bar
ls.set_progress( ++old_progress );
}
current_month = original_start_year;
settings.set_industry_increase_every( original_industry_growth );
msg->clear();
}
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
way_desc_t const* desc = settings.get_intercity_road_type(get_timeline_year_month());
if(desc == 0) {
// Hajo: try some default (might happen with timeline ... )
desc = way_builder_t::weg_search(road_wt,80,get_timeline_year_month(),type_flat);
}
way_builder_t bauigel (players[1] );
bauigel.init_builder(way_builder_t::strasse | way_builder_t::terraform_flag, desc, tunnel_builder_t::get_tunnel_desc(road_wt,15,get_timeline_year_month()), bridge_builder_t::find_bridge(road_wt,15,get_timeline_year_month()) );
bauigel.set_keep_existing_ways(true);
bauigel.set_maximum(env_t::intercity_road_length);
// **** intercity road construction
int count = 0;
sint32 const n_cities = settings.get_city_count();
int const max_count = n_cities * (n_cities - 1) / 2 - old_city_count * (old_city_count - 1) / 2;
// something to do??
if( max_count > 0 ) {
// print("Building intercity roads ...\n");
ls.set_max( 16 + 2 * (old_city_count + new_city_count) + 2 * new_city_count + (old_x == 0 ? settings.get_factory_count() : 0) );
// find townhall of city i and road in front of it
vector_tpl<koord3d> k;
for (int i = 0; i < settings.get_city_count(); ++i) {
koord k1(stadt[i]->get_townhall_road());
if (lookup_kartenboden(k1) && lookup_kartenboden(k1)->hat_weg(road_wt)) {
k.append(lookup_kartenboden(k1)->get_pos());
}
else {
// look for a road near the townhall
gebaeude_t const* const gb = obj_cast<gebaeude_t>(lookup_kartenboden(stadt[i]->get_pos())->first_obj());
bool ok = false;
if( gb && gb->is_townhall() ) {
koord k_check = stadt[i]->get_pos() + koord(-1,-1);
const koord size = gb->get_tile()->get_desc()->get_size(gb->get_tile()->get_layout());
koord inc(1,0);
// scan all adjacent tiles, take the first that has a road
for(sint32 i=0; i<2*size.x+2*size.y+4 && !ok; i++) {
grund_t *gr = lookup_kartenboden(k_check);
if (gr && gr->hat_weg(road_wt)) {
k.append(gr->get_pos());
ok = true;
}
k_check = k_check + inc;
if (i==size.x+1) {
inc = koord(0,1);
}
else if (i==size.x+size.y+2) {
inc = koord(-1,0);
}
else if (i==2*size.x+size.y+3) {
inc = koord(0,-1);
}
}
}
if (!ok) {
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(settings.get_city_count(), settings.get_city_count());
for (sint32 i = 0; i < settings.get_city_count(); ++i) {
city_dist.at(i,i) = 0;
for (sint32 j = i + 1; j < settings.get_city_count(); ++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 to new cities
if( j>=old_city_count && city_dist.at(i,j) >= env_t::intercity_road_length ) {
count++;
}
}
city_flag.append( i < old_city_count ? conn_comp : 0 );
// progress bar stuff
ls.set_progress( 16 + 2 * new_city_count + count * settings.get_city_count() * 2 / max_count );
}
// mark first town as connected
if (old_city_count==0) {
city_flag[0]=conn_comp;
}
// get a default vehikel
route_t verbindung;
vehicle_t* test_driver;
vehicle_desc_t test_drive_desc(road_wt, 500, vehicle_desc_t::diesel );
test_driver = vehicle_builder_t::build(koord3d(), players[1], NULL, &test_drive_desc);
test_driver->set_flag( obj_t::not_on_map );
bool ready=false;