forked from aburch/simutrans
/
simhalt.cc
5893 lines (5231 loc) · 170 KB
/
simhalt.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. Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
/*
* Haltestellen fuer Simutrans
* 03.2000 derived from simfab.cc
*
* Hj. Malthaner
*/
#include <algorithm>
#include "freight_list_sorter.h"
#include "path_explorer.h"
#include "simcity.h"
#include "simcolor.h"
#include "simconvoi.h"
#include "simdebug.h"
#include "simdepot.h"
#include "simfab.h"
#include "simhalt.h"
#include "simintr.h"
#include "simline.h"
#include "simmem.h"
#include "simmesg.h"
#include "simplan.h"
#include "utils/simrandom.h"
#include "player/simplay.h"
#include "player/finance.h"
#include "gui/simwin.h"
#include "simworld.h"
#include "simware.h"
#include "simsys.h"
#include "bauer/hausbauer.h"
#include "bauer/goods_manager.h"
#include "descriptor/goods_desc.h"
#include "boden/boden.h"
#include "boden/grund.h"
#include "boden/wasser.h"
#include "boden/wege/strasse.h"
#include "dataobj/settings.h"
#include "dataobj/schedule.h"
#include "dataobj/loadsave.h"
#include "dataobj/translator.h"
#include "dataobj/environment.h"
#include "obj/gebaeude.h"
#include "obj/label.h"
#include "obj/signal.h"
#include "gui/halt_info.h"
#include "gui/halt_detail.h"
#include "gui/karte.h"
#include "utils/simstring.h"
#include "vehicle/simpeople.h"
#include "descriptor/goods_desc.h"
karte_ptr_t haltestelle_t::welt;
vector_tpl<halthandle_t> haltestelle_t::alle_haltestellen;
stringhashtable_tpl<halthandle_t> haltestelle_t::all_names;
vector_tpl<lines_loaded_t> haltestelle_t::lines_loaded;
uint8 haltestelle_t::pedestrian_limit = 0;
// hash table only used during loading
inthashtable_tpl<sint32,halthandle_t> *haltestelle_t::all_koords = NULL;
// since size_x*size_y < 0x1000000, we have just to shift the high bits
#define get_halt_key(k,width) ( ((k).x*(width)+(k).y) /*+ ((k).z << 25)*/ )
//uint8 haltestelle_t::status_step = 0;
uint8 haltestelle_t::reconnect_counter = 0;
static const uint8 pedestrian_generate_max = 16;
// controls the halt iterator in step_all():
static bool restart_halt_iterator = true;
void haltestelle_t::step_all()
{
const uint32 count = alle_haltestellen.get_count();
if (count)
{
const uint32 loops = min(count, 256u);
static vector_tpl<halthandle_t>::iterator iter;
for (uint32 i = 0; i < loops; ++i)
{
if (restart_halt_iterator || iter == alle_haltestellen.end())
{
restart_halt_iterator = false;
iter = alle_haltestellen.begin();
}
(*iter++)->step();
}
}
}
halthandle_t haltestelle_t::get_halt(const koord pos, const player_t *player )
{
const planquadrat_t *plan = welt->access(pos);
if(plan)
{
for( uint8 i=0; i < plan->get_boden_count(); i++ ) {
halthandle_t my_halt = plan->get_boden_bei(i)->get_halt();
if( my_halt.is_bound() && my_halt->check_access(player) ) {
// Stop at first halt found (always prefer ground level)
return my_halt;
}
}
// no halt? => we do the water check
if(plan->get_kartenboden()->is_water())
{
// may catch bus stops close to water ...
const uint8 cnt = plan->get_haltlist_count();
// first check for own stop
for(uint8 i = 0; i < cnt; i++)
{
if(plan->get_haltlist()[i].halt->get_owner() == player)
{
const uint8 distance_to_dock = plan->get_haltlist()[i].distance;
if(distance_to_dock <= 1)
{
return plan->get_haltlist()[i].halt;
}
}
}
// then for other stops to which access is allowed
// (This is second because it is preferable to dock at one's own
// port to avoid charges)
for(uint8 i = 0; i < cnt; i++)
{
if(plan->get_haltlist()[i].halt->check_access(player))
{
const uint8 distance_to_dock = plan->get_haltlist()[i].distance;
if(distance_to_dock <= 1)
{
return plan->get_haltlist()[i].halt;
}
}
}
// so: nothing found
}
}
return halthandle_t();
}
static vector_tpl<convoihandle_t>stale_convois;
static vector_tpl<linehandle_t>stale_lines;
void haltestelle_t::start_load_game()
{
all_koords = new inthashtable_tpl<sint32,halthandle_t>;
}
void haltestelle_t::end_load_game()
{
delete all_koords;
all_koords = NULL;
}
/**
* return an index to a halt; it is only used for old games
* by default create a new halt if none found
*/
halthandle_t haltestelle_t::get_halt_koord_index(koord k)
{
if(!welt->is_within_limits(k)) {
return halthandle_t();
}
// check in hashtable
halthandle_t h;
const sint32 n = get_halt_key(koord3d(k,-128), welt->get_size().y);
assert(all_koords);
h = all_koords->get( n );
if( !h.is_bound() ) {
// No halts found => create one
h = haltestelle_t::create(k, NULL );
all_koords->set( n, h );
}
return h;
}
/* we allow only for a single stop per grund
* this will only return something if this stop is accessible by the player passed in the second parameter
*/
halthandle_t haltestelle_t::get_halt(const koord3d pos, const player_t *player )
{
const grund_t *gr = welt->lookup(pos);
if(gr)
{
weg_t *w = gr->get_weg_nr(0);
if(!w)
{
w = gr->get_weg_nr(1);
}
// Stops on public roads, even those belonging to other players, should be able to be used by all players.
if(gr->get_halt().is_bound() && (gr->get_halt()->check_access(player) ||
(w && player_t::check_owner(w->get_owner(), player))) ||
(w && (w->get_waytype() == road_wt || w->get_waytype() == tram_wt) && (w->get_owner() == NULL || w->get_owner()->is_public_service())))
{
return gr->get_halt();
}
// no halt? => we do the water check
if(gr->is_water())
{
// may catch bus stops close to water ...
const planquadrat_t *plan = welt->access(pos.get_2d());
const uint8 cnt = plan->get_haltlist_count();
// first check for own stop
for(uint8 i = 0; i < cnt; i++)
{
halthandle_t halt = plan->get_haltlist()[i].halt;
const uint8 distance_to_dock = plan->get_haltlist()[i].distance;
if(halt->get_owner() == player && distance_to_dock <= 1 && halt->get_station_type() & dock)
{
return halt;
}
}
// then for other stops to which access is allowed
// (This is second because it is preferable to dock at one's own
// port to avoid charges)
for(uint8 i = 0; i < cnt; i++)
{
halthandle_t halt = plan->get_haltlist()[i].halt;
const uint8 distance_to_dock = plan->get_haltlist()[i].distance;
if(halt->check_access(player) && distance_to_dock <= 1 && halt->get_station_type() & dock)
{
return halt;
}
}
// so: nothing found
}
}
return halthandle_t();
}
koord haltestelle_t::get_basis_pos() const
{
if (tiles.empty()) return koord::invalid;
assert(tiles.front().grund->get_pos().get_2d() == init_pos);
return tiles.front().grund->get_pos().get_2d();
}
koord3d haltestelle_t::get_basis_pos3d() const
{
if (tiles.empty()) return koord3d::invalid;
return tiles.front().grund->get_pos();
}
/**
* Station factory method. Returns handles instead of pointers.
* @author Hj. Malthaner
*/
halthandle_t haltestelle_t::create(koord pos, player_t *player)
{
haltestelle_t * p = new haltestelle_t(pos, player);
return p->self;
}
/*
* removes a ground tile from a station
* @author prissi
*/
bool haltestelle_t::remove(player_t *player, koord3d pos)
{
grund_t *bd = welt->lookup(pos);
// wrong ground?
if(bd==NULL) {
dbg->error("haltestelle_t::remove()","illegal ground at %d,%d,%d", pos.x, pos.y, pos.z);
return false;
}
halthandle_t halt = bd->get_halt();
if(!halt.is_bound()) {
dbg->error("haltestelle_t::remove()","no halt at %d,%d,%d", pos.x, pos.y, pos.z);
return false;
}
DBG_MESSAGE("haltestelle_t::remove()","removing segment from %d,%d,%d", pos.x, pos.y, pos.z);
// otherwise there will be marked tiles left ...
halt->mark_unmark_coverage(false);
// only try to remove connected buildings, when still in list to avoid infinite loops
if( halt->rem_grund(bd) ) {
// remove station building?
gebaeude_t* gb = bd->find<gebaeude_t>();
if(gb)
{
DBG_MESSAGE("haltestelle_t::remove()", "removing building" );
if(gb->get_tile()->get_desc()->get_is_control_tower())
{
halt->remove_control_tower();
halt->recalc_status();
}
hausbauer_t::remove( player, gb, false );
bd = NULL; // no need to recalc image
// removing the building could have destroyed this halt already
if (!halt.is_bound()){
return true;
}
}
}
if(!halt->existiert_in_welt()) {
DBG_DEBUG("haltestelle_t::remove()","remove last");
// all deleted?
DBG_DEBUG("haltestelle_t::remove()","destroy");
haltestelle_t::destroy( halt );
}
else {
DBG_DEBUG("haltestelle_t::remove()","not last");
// acceptance and type may have been changed ... (due to post office/dock/railways station deletion)
halt->recalc_station_type();
}
// if building was removed this is false!
if(bd) {
bd->calc_image();
reliefkarte_t::get_karte()->calc_map_pixel(pos.get_2d());
}
return true;
}
/**
* Station factory method. Returns handles instead of pointers.
* @author Hj. Malthaner
*/
halthandle_t haltestelle_t::create(loadsave_t *file)
{
haltestelle_t *p = new haltestelle_t(file);
return p->self;
}
/**
* Station destruction method.
* @author Hj. Malthaner
*/
void haltestelle_t::destroy(halthandle_t const halt)
{
// just play safe: restart iterator at zero ...
restart_halt_iterator = true;
delete halt.get_rep();
}
/**
* Station destruction method.
* Da destroy() alle_haltestellen modifiziert kann kein Iterator benutzt
* werden! V. Meyer
* @author Hj. Malthaner
*/
void haltestelle_t::destroy_all()
{
while (!alle_haltestellen.empty()) {
halthandle_t halt = alle_haltestellen.back();
destroy(halt);
}
if (all_koords) {
delete all_koords;
all_koords = NULL;
}
//status_step = 0;
}
haltestelle_t::haltestelle_t(loadsave_t* file)
{
last_loading_step = welt->get_steps();
const uint8 max_categories = goods_manager_t::get_max_catg_index();
const uint8 max_classes = max(goods_manager_t::passengers->get_number_of_classes(), goods_manager_t::mail->get_number_of_classes());
cargo = (vector_tpl<ware_t> **)calloc( max_categories, sizeof(vector_tpl<ware_t> *) );
non_identical_schedules = new uint8[ max_categories ];
for ( uint8 i = 0; i < max_categories; i++ ) {
non_identical_schedules[i] = 0;
}
connexions = new quickstone_hashtable_tpl<haltestelle_t, connexion*>*[max_categories];
// See here for an explanation of the below: http://stackoverflow.com/questions/29375797/copy-2d-array-using-memcpy/29375830#29375830
// This is not a true 2d array as the number of categories is not fixed.
waiting_times = new inthashtable_tpl<uint32, waiting_time_set >*[max_categories];
waiting_times[0] = new inthashtable_tpl<uint32, waiting_time_set >[max_categories * max_classes];
for (uint8 i = 1; i < max_categories; i++)
{
waiting_times[i] = waiting_times[i - 1] + max_classes;
}
#ifdef MULTI_THREAD
transferring_cargoes = new vector_tpl<transferring_cargo_t>[world()->get_parallel_operations() + 2];
#else
transferring_cargoes = new vector_tpl<transferring_cargo_t>[1];
#endif
// Knightly : create the actual connexion hash tables
for(uint8 i = 0; i < max_categories; i ++)
{
connexions[i] = new quickstone_hashtable_tpl<haltestelle_t, connexion*>();
}
do_alternative_seats_calculation = true;
status_color = COL_YELLOW;
last_status_color = COL_PURPLE;
last_bar_count = 0;
enables = NOT_ENABLED;
// @author hsiegeln
sortierung = freight_list_sorter_t::by_name;
resort_freight_info = true;
init_financial_history();
rdwr(file);
alle_haltestellen.append(self);
restart_halt_iterator = true;
// Added by : Knightly
inauguration_time = 0;
}
haltestelle_t::haltestelle_t(koord k, player_t* player)
{
self = halthandle_t(this);
assert( !alle_haltestellen.is_contained(self) );
alle_haltestellen.append(self);
restart_halt_iterator = true;
//markers[ self.get_id() ] = current_marker;
last_loading_step = welt->get_steps();
this->init_pos = k;
owner = player;
enables = NOT_ENABLED;
last_catg_index = 255; // force total rerouting
const uint8 max_categories = goods_manager_t::get_max_catg_index();
const uint8 max_classes = max(goods_manager_t::passengers->get_number_of_classes(), goods_manager_t::mail->get_number_of_classes());
cargo = (vector_tpl<ware_t> **)calloc( max_categories, sizeof(vector_tpl<ware_t> *) );
non_identical_schedules = new uint8[ max_categories ];
for ( uint8 i = 0; i < max_categories; i++ ) {
non_identical_schedules[i] = 0;
}
connexions = new quickstone_hashtable_tpl<haltestelle_t, connexion*>*[max_categories];
// See here for an explanation of the below: http://stackoverflow.com/questions/29375797/copy-2d-array-using-memcpy/29375830#29375830
// This is not a true 2d array as the number of categories is not fixed.
waiting_times = new inthashtable_tpl<uint32, waiting_time_set >*[max_categories];
waiting_times[0] = new inthashtable_tpl<uint32, waiting_time_set >[max_categories * max_classes];
for (uint8 i = 1; i < max_categories; i++)
{
waiting_times[i] = waiting_times[i - 1] + max_classes;
}
// Knightly : create the actual connexion hash tables
for(uint8 i = 0; i < max_categories; i ++)
{
connexions[i] = new quickstone_hashtable_tpl<haltestelle_t, connexion*>();
}
do_alternative_seats_calculation = true;
status_color = COL_YELLOW;
last_status_color = COL_PURPLE;
last_bar_count = 0;
sortierung = freight_list_sorter_t::by_name;
init_financial_history();
check_waiting = 0;
check_nearby_halts();
// Added by : Knightly
//inauguration_time = dr_time();
inauguration_time = welt->get_ticks(); // Possibly more network safe than the original (commented out above)
control_towers = 0;
transfer_time = 0;
for(sint32 i = 0; i < 4; i ++)
{
train_last_departed[i] = 0;
}
#ifdef MULTI_THREAD
transferring_cargoes = new vector_tpl<transferring_cargo_t>[world()->get_parallel_operations() + 2];
#else
transferring_cargoes = new vector_tpl<transferring_cargo_t>[1];
#endif
}
haltestelle_t::~haltestelle_t()
{
assert(self.is_bound());
// first: remove halt from all lists
int i=0;
while(alle_haltestellen.remove(self)) {
i++;
}
if (i != 1) {
dbg->error("haltestelle_t::~haltestelle_t()", "handle %i found %i times in haltlist!", self.get_id(), i );
}
if(!welt->is_destroying())
{
ITERATE(halts_within_walking_distance, n)
{
halthandle_t walking_distance_halt = get_halt_within_walking_distance(n);
if ( walking_distance_halt.is_bound() )
{
walking_distance_halt->remove_halt_within_walking_distance(self);
}
}
// At every other stop which may have waiting times for going here, clean this stop out
// of that stop's waiting times list
FOR(vector_tpl<halthandle_t>, & current_halt, alle_haltestellen)
{
// If it's not bound, or waiting_times isn't initialized, this could crash
if(current_halt.is_bound() && current_halt->waiting_times)
{
for(uint8 category = 0; category < goods_manager_t::get_max_catg_index(); category++)
{
uint8 number_of_classes;
if (category == goods_manager_t::INDEX_PAS)
{
number_of_classes = goods_manager_t::passengers->get_number_of_classes();
}
else if (category == goods_manager_t::INDEX_MAIL)
{
number_of_classes = goods_manager_t::mail->get_number_of_classes();
}
else
{
number_of_classes = 1;
}
for (uint8 g_class = 0; g_class < number_of_classes; g_class++ )
{
current_halt->waiting_times[category][g_class].remove(self.get_id());
}
}
}
}
}
// free name
set_name(NULL);
if(!welt->is_destroying())
{
// remove from ground and planquadrat haltlists
koord ul(32767,32767);
koord lr(0,0);
while( !tiles.empty() ) {
koord pos = tiles.remove_first().grund->get_pos().get_2d();
planquadrat_t *pl = welt->access_nocheck(pos);
assert(pl);
for( uint8 i=0; i<pl->get_boden_count(); i++ ) {
pl->get_boden_bei(i)->set_halt( halthandle_t() );
}
// bounding box for adjustments
// bounding box for adjustments
ul.clip_max(pos);
lr.clip_min(pos);
}
// remove from all haltlists
uint16 const cov = welt->get_settings().get_station_coverage();
vector_tpl<fabrik_t*> affected_fab_list;
ul.x = max(0, ul.x - cov);
ul.y = max(0, ul.y - cov);
lr.x = min(welt->get_size().x, lr.x + 1 + cov);
lr.y = min(welt->get_size().y, lr.y + 1 + cov);
for( int y=ul.y; y<lr.y; y++ ) {
for( int x=ul.x; x<lr.x; x++ ) {
planquadrat_t *plan = welt->access_nocheck(x,y);
if(plan->get_haltlist_count()>0) {
plan->remove_from_haltlist(self);
}
const grund_t* gr = plan->get_kartenboden();
// If there's a factory here, add it to the working list
const gebaeude_t* gb = gr->find<gebaeude_t>();
if (gb) {
fabrik_t* fab = gb->get_fabrik();
if (fab && !affected_fab_list.is_contained(fab) ) {
affected_fab_list.append(fab);
}
}
}
}
// Update nearby factories' lists of connected halts.
// Must be done AFTER updating the planquadrats
FOR (vector_tpl<fabrik_t*>, fab, affected_fab_list)
{
fab->recalc_nearby_halts();
}
}
destroy_win( magic_halt_info + self.get_id() );
destroy_win( magic_halt_detail + self.get_id() );
// finally detach handle
// before it is needed for clearing up the planqudrat and tiles
self.detach();
const uint8 max_categories = goods_manager_t::get_max_catg_index();
for(uint8 i = 0; i < max_categories; i++) {
if (cargo[i]) {
FOR(vector_tpl<ware_t>, const &w, *cargo[i]) {
fabrik_t::update_transit(w, false);
}
delete cargo[i];
cargo[i] = NULL;
}
}
free(cargo);
if(!welt->is_destroying())
{
#ifdef MULTI_THREAD
welt->stop_path_explorer();
#endif
for(uint8 i = 0; i < max_categories; i++)
{
reset_connexions(i);
path_explorer_t::refresh_category(i);
}
delete connexions[i];
}
delete[] connexions;
// See here for an explanation of the below: http://stackoverflow.com/questions/29375797/copy-2d-array-using-memcpy/29375830#29375830
// This is not a true 2d array as the number of categories is not fixed.
delete[] waiting_times[0];
delete[] waiting_times;
delete[] non_identical_schedules;
// delete[] all_links;
delete[] transferring_cargoes;
}
void haltestelle_t::rotate90( const sint16 y_size )
{
init_pos.rotate90( y_size );
// rotate goods destinations
// iterate over all different categories
for(uint8 i=0; i<goods_manager_t::get_max_catg_index(); i++) {
if(cargo[i]) {
vector_tpl<ware_t>& warray = *cargo[i];
for (size_t j = warray.get_count(); j-- > 0;) {
ware_t& ware = warray[j];
if(ware.menge>0) {
ware.rotate90(y_size);
}
else
{
// empty => remove
warray.remove_at(j);
}
}
}
}
for (uint32 i = 0; i < world()->get_parallel_operations(); i++)
{
vector_tpl<transferring_cargo_t>& tcarray = transferring_cargoes[i];
for (size_t j = tcarray.get_count(); j-- > 0;)
{
transferring_cargo_t& tc = tcarray[j];
if (tc.ware.menge>0)
{
tc.ware.rotate90(y_size);
}
else
{
// empty => remove
tcarray.remove_at(j);
}
}
}
vector_tpl<koord3d> rotated_station_signals;
FOR(vector_tpl<koord3d>, i, station_signals)
{
i.rotate90(y_size);
rotated_station_signals.append(i);
}
station_signals.clear();
FOR(vector_tpl<koord3d>, i, rotated_station_signals)
{
station_signals.append(i);
}
sint64 temp_last_departed[4];
// Rotate station signal timings
for(uint32 i = 0; i < 4; i ++)
{
temp_last_departed[i] = train_last_departed[i];
}
// Rotation is clockwise.
// North becomes East
train_last_departed[2] = temp_last_departed[0];
// South becomes West
train_last_departed[3] = temp_last_departed[1];
// East becomes South
train_last_departed[1] = temp_last_departed[2];
// West becomes North
train_last_departed[0] = temp_last_departed[3];
// Simworld will update nearby factories' lists of connected halts.
}
const char* haltestelle_t::get_name() const
{
const char *name = "Unknown";
if (tiles.empty()) {
name = "Unnamed";
}
else {
grund_t* bd = welt->lookup(get_basis_pos3d());
if(bd && bd->get_flag(grund_t::has_text)) {
name = bd->get_text();
}
}
return name;
}
/**
* Sets the name. Creates a copy of name.
* @author Hj. Malthaner
*/
void haltestelle_t::set_name(const char *new_name)
{
grund_t *gr = welt->lookup(get_basis_pos3d());
if(gr) {
if(gr->get_flag(grund_t::has_text)) {
halthandle_t h = all_names.remove(gr->get_text());
if(h!=self) {
DBG_MESSAGE("haltestelle_t::set_name()","removing name %s already used!",gr->get_text());
}
}
if(!gr->find<label_t>()) {
gr->set_text( new_name );
if(new_name && all_names.set(gr->get_text(),self).is_bound() ) {
DBG_MESSAGE("haltestelle_t::set_name()","name %s already used!",new_name);
}
}
// Knightly : need to update the title text of the associated halt detail and info dialogs, if present
halt_detail_t *const details_frame = dynamic_cast<halt_detail_t *>( win_get_magic( magic_halt_detail + self.get_id() ) );
if( details_frame ) {
details_frame->set_name( get_name() );
}
halt_info_t *const info_frame = dynamic_cast<halt_info_t *>( win_get_magic( magic_halt_info + self.get_id() ) );
if( info_frame ) {
info_frame->set_name( get_name() );
}
}
}
// returns the number of % in a printf string ....
static int count_printf_param( const char *str )
{
int count = 0;
while( *str!=0 ) {
if( *str=='%' ) {
str++;
if( *str!='%' ) {
count++;
}
}
str ++;
}
return count;
}
// creates stops with unique! names
char* haltestelle_t::create_name(koord const k, char const* const typ)
{
int const lang = welt->get_settings().get_name_language_id();
stadt_t *stadt = welt->find_nearest_city(k);
const char *stop = translator::translate(typ,lang);
cbuffer_t buf;
// this fails only, if there are no towns at all!
if(stadt==NULL) {
for( uint32 i=1; i<65536; i++ ) {
// get a default name
buf.printf( translator::translate("land stop %i %s",lang), i, stop );
if( !all_names.get(buf).is_bound() ) {
return strdup(buf);
}
buf.clear();
}
return strdup("Unnamed");
}
// now we have a city
const char *city_name = stadt->get_name();
sint16 li_gr = stadt->get_linksoben().x - 2;
sint16 re_gr = stadt->get_rechtsunten().x + 2;
sint16 ob_gr = stadt->get_linksoben().y - 2;
sint16 un_gr = stadt->get_rechtsunten().y + 2;
// The location is anywhere inside the town boundary
const bool inside = (li_gr < k.x && re_gr > k.x && ob_gr < k.y && un_gr > k.y);
// The location is a short distance outside the town boundary
const bool suburb = !inside && (li_gr - 6 < k.x && re_gr + 6 > k.x && ob_gr - 6 < k.y && un_gr + 6 > k.y);
const koord townhall_pos = stadt->get_townhall_road();
//const uint16 tiles_to_edge = inside ? max(max(k.x - li_gr, k.y - ob_gr), max(un_gr - k.y, re_gr - k.x)) : 1;
const uint16 townhall_tiles_to_edge = max(max(townhall_pos.x - li_gr, townhall_pos.y - ob_gr), max(un_gr - townhall_pos.y, re_gr - townhall_pos.x));
// This location is in the geographical centre of the town
const bool inner = shortest_distance(k, townhall_pos) < townhall_tiles_to_edge / 2;
if (!welt->get_settings().get_numbered_stations()) {
static const koord next_building[24] = {
koord( 0, -1), // north
koord( 1, 0), // east
koord( 0, 1), // south
koord(-1, 0), // west
koord( 1, -1), // nordost
koord( 1, 1), // southeast
koord(-1, 1), // southwest
koord(-1, -1), // northwest
koord( 0, -2), // double nswo
koord( 2, 0),
koord( 0, 2),
koord(-2, 0),
koord( 1, -2), // all the remaining 3s
koord( 2, -1),
koord( 2, 1),
koord( 1, 2),
koord(-1, 2),
koord(-2, 1),
koord(-2, -1),
koord(-1, -2),
koord( 2, -2), // and now all buildings with distance 4
koord( 2, 2),
koord(-2, 2),
koord(-2, -2)
};
// standard names:
// order: factory, attraction, direction, normal name
// prissi: first we try a factory name
// is there a translation for factory defined?
const char *fab_base_text = "%s factory %s %s";
const char *fab_base = translator::translate(fab_base_text,lang);
if( fab_base_text != fab_base ) {
slist_tpl<fabrik_t *>fabs;
if (self.is_bound()) {
// first factories (so with same distance, they have priority)
int this_distance = 999;
FOR(slist_tpl<fabrik_t*>, const f, get_fab_list()) {
int distance = koord_distance(f->get_pos().get_2d(), k);
if( distance < this_distance ) {
fabs.insert(f);
this_distance = distance;
}
else {
fabs.append(f);
}
}
}
else {
// since the distance are presorted, we can just append for a good choice ...
for( int test=0; test<24; test++ ) {
fabrik_t *fab = fabrik_t::get_fab(k+next_building[test]);
if(fab && fabs.is_contained(fab)) {
fabs.append(fab);
}
}
}
// are there fabs?
FOR(slist_tpl<fabrik_t*>, const f, fabs) {
// with factories
buf.printf(fab_base, city_name, f->get_name(), stop);
if( !all_names.get(buf).is_bound() ) {
return strdup(buf);
}
buf.clear();
}
}
// no fabs or all names used up already
// is there a translation for buildings defined?
const char *building_base_text = "%s building %s %s";
const char *building_base = translator::translate(building_base_text,lang);
if( building_base_text != building_base ) {
// check for other special building (townhall, monument, tourst attraction)
for (int i=0; i<24; i++) {
grund_t *gr = welt->lookup_kartenboden( next_building[i] + k);
if(gr==NULL || gr->get_typ()!=grund_t::fundament) {
// no building here
continue;
}
// since closes coordinates are tested first, we do not need to not sort this
const char *building_name = NULL;
const gebaeude_t* gb = gr->find<gebaeude_t>();
if(gb==NULL) {
// field may have foundations but no building
continue;
}
// now we have a building here
if (gb->is_monument()) {
building_name = translator::translate(gb->get_name(),lang);
}
else if (gb->is_townhall() ||
gb->get_tile()->get_desc()->get_type() == building_desc_t::attraction_land || // land attraction
gb->get_tile()->get_desc()->get_type() == building_desc_t::attraction_city) { // town attraction
building_name = make_single_line_string(translator::translate(gb->get_tile()->get_desc()->get_name(),lang), 2);
}
else {
// normal town house => not suitable for naming
continue;
}
// now we have a name: try it
buf.printf( building_base, city_name, building_name, stop );
if( !all_names.get(buf).is_bound() ) {
return strdup(buf);
}
buf.clear();
}
}
// Use the standard naming scheme before reverting to street names:
// this way, the major stops in a town will be likely to be distinguished
// automatically from the minor stops by name automatically.
char numbername[10];
if(inside)
{
if (inner)
{
// Names for town centre locations