forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simhalt.cc
3339 lines (2884 loc) · 95.7 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)
*/
/*
* Stations for Simutrans
* 03.2000 moved from simfab.cc
*
* Hj. Malthaner
*/
#include <algorithm>
#include "freight_list_sorter.h"
#include "simcity.h"
#include "simcolor.h"
#include "simconvoi.h"
#include "simdebug.h"
#include "simfab.h"
#include "simhalt.h"
#include "simintr.h"
#include "simline.h"
#include "simmem.h"
#include "simmesg.h"
#include "simplan.h"
#include "player/simplay.h"
#include "gui/simwin.h"
#include "simworld.h"
#include "simware.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 "gui/halt_info.h"
#include "gui/halt_detail.h"
#include "gui/karte.h"
#include "utils/simrandom.h"
#include "utils/simstring.h"
#include "vehicle/simpeople.h"
karte_ptr_t haltestelle_t::welt;
vector_tpl<halthandle_t> haltestelle_t::alle_haltestellen;
stringhashtable_tpl<halthandle_t> haltestelle_t::all_names;
// 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 vector_tpl<convoihandle_t>stale_convois;
static vector_tpl<linehandle_t>stale_lines;
void haltestelle_t::reset_routing()
{
reconnect_counter = welt->get_schedule_counter()-1;
}
void haltestelle_t::step_all()
{
// tell all stale convois to reroute their goods
if( !stale_convois.empty() ) {
convoihandle_t cnv = stale_convois.pop_back();
if( cnv.is_bound() ) {
cnv->check_freight();
}
}
// same for stale lines
if( !stale_lines.empty() ) {
linehandle_t line = stale_lines.pop_back();
if( line.is_bound() ) {
line->check_freight();
}
}
static vector_tpl<halthandle_t>::iterator iter( alle_haltestellen.begin() );
if (alle_haltestellen.empty()) {
return;
}
const uint8 schedule_counter = welt->get_schedule_counter();
if (reconnect_counter != schedule_counter) {
// always start with reconnection, re-routing will happen after complete reconnection
status_step = RECONNECTING;
reconnect_counter = schedule_counter;
iter = alle_haltestellen.begin();
}
sint16 units_remaining = 128;
for (; iter != alle_haltestellen.end(); ++iter) {
if (units_remaining <= 0) return;
// iterate until the specified number of units were handled
if( !(*iter)->step(status_step, units_remaining) ) {
// too much rerouted => needs to continue at next round!
return;
}
}
if (status_step == RECONNECTING) {
// reconnecting finished, compute connected components in one sweep
rebuild_connected_components();
// reroute in next call
status_step = REROUTING;
}
else if (status_step == REROUTING) {
status_step = 0;
}
iter = alle_haltestellen.begin();
}
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 belongs to same player or is public, or is a dock (when on water)
*/
halthandle_t haltestelle_t::get_halt(const koord3d pos, const player_t *player )
{
const grund_t *gr = welt->lookup(pos);
if(gr) {
if(gr->get_halt().is_bound() && player_t::check_owner(player,gr->get_halt()->get_owner()) ) {
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];
if( halt->get_owner()==player && halt->get_station_type()&dock ) {
return halt;
}
}
// then for public stop
for( uint8 i=0; i<cnt; i++ ) {
halthandle_t halt = plan->get_haltlist()[i];
if( halt->get_owner()==welt->get_public_player() && halt->get_station_type()&dock ) {
return halt;
}
}
// so: nothing found
}
}
return halthandle_t();
}
koord haltestelle_t::get_basis_pos() const
{
return get_basis_pos3d().get_2d();
}
koord3d haltestelle_t::get_basis_pos3d() const
{
if (tiles.empty()) {
return koord3d::invalid;
}
assert(tiles.front().grund->get_pos().get_2d() == init_pos);
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" );
hausbauer_t::remove( player, gb );
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 );
}
// 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)
{
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);
}
delete all_koords;
all_koords = NULL;
status_step = 0;
}
haltestelle_t::haltestelle_t(loadsave_t* file)
{
last_loading_step = welt->get_steps();
cargo = (vector_tpl<ware_t> **)calloc( goods_manager_t::get_max_catg_index(), sizeof(vector_tpl<ware_t> *) );
all_links = new link_t[ goods_manager_t::get_max_catg_index() ];
status_color = SYSCOL_TEXT_UNUSED;
last_status_color = color_idx_to_rgb(COL_PURPLE);
last_bar_count = 0;
reconnect_counter = welt->get_schedule_counter()-1;
enables = NOT_ENABLED;
// @author hsiegeln
sortierung = freight_list_sorter_t::by_name;
resort_freight_info = true;
rdwr(file);
markers[ self.get_id() ] = current_marker;
alle_haltestellen.append(self);
}
haltestelle_t::haltestelle_t(koord k, player_t* player_)
{
self = halthandle_t(this);
assert( !alle_haltestellen.is_contained(self) );
alle_haltestellen.append(self);
markers[ self.get_id() ] = current_marker;
last_loading_step = welt->get_steps();
this->init_pos = k;
owner_p = player_;
enables = NOT_ENABLED;
// force total re-routing
reconnect_counter = welt->get_schedule_counter()-1;
last_catg_index = 255;
cargo = (vector_tpl<ware_t> **)calloc( goods_manager_t::get_max_catg_index(), sizeof(vector_tpl<ware_t> *) );
all_links = new link_t[ goods_manager_t::get_max_catg_index() ];
status_color = SYSCOL_TEXT_UNUSED;
last_status_color = color_idx_to_rgb(COL_PURPLE);
last_bar_count = 0;
sortierung = freight_list_sorter_t::by_name;
init_financial_history();
}
haltestelle_t::~haltestelle_t()
{
assert(self.is_bound());
// first: remove halt from all lists
int i=0;
while(alle_haltestellen.is_contained(self)) {
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 );
}
// free name
set_name(NULL);
// remove from ground and planquadrat (tile) haltlists
koord ul(32767,32767);
koord lr(0,0);
while( !tiles.empty() ) {
grund_t *gr = tiles.remove_first().grund;
koord pos = gr->get_pos().get_2d();
gr->set_halt( halthandle_t() );
// 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();
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(x,y);
if(plan->get_haltlist_count()>0) {
plan->remove_from_haltlist(self);
}
}
}
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();
for(unsigned i=0; i<goods_manager_t::get_max_catg_index(); 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 );
delete[] all_links;
// routes may have changed without this station ...
verbinde_fabriken();
}
void haltestelle_t::rotate90( const sint16 y_size )
{
init_pos.rotate90( y_size );
// rotate cargo (good) destinations
// iterate over all different categories
for(unsigned 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);
}
}
}
}
// re-linking factories
verbinde_fabriken();
}
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;
// strings for intown / outside of town
const bool inside = (li_gr < k.x && re_gr > k.x && ob_gr < k.y && un_gr > k.y);
const bool suburb = !inside && (li_gr - 6 < k.x && re_gr + 6 > k.x && ob_gr - 6 < k.y && un_gr + 6 > k.y);
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), // northeast
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, tourist 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();
}
}
// if there are street names, use them
if( inside || suburb ) {
const vector_tpl<char*>& street_names( translator::get_street_name_list() );
// make sure we do only ONE random call regardless of how many names are available (to avoid desyncs in network games)
if( const uint32 count = street_names.get_count() ) {
uint32 idx = simrand( count );
static const uint32 some_primes[] = { 19, 31, 109, 199, 409, 571, 631, 829, 1489, 1999, 2341, 2971, 3529, 4621, 4789, 7039, 7669, 8779, 9721 };
// find prime that does not divide count
uint32 offset = 1;
for(uint8 i=0; i<lengthof(some_primes); i++) {
if (count % some_primes[i]!=0) {
offset = some_primes[i];
break;
}
}
// as count % offset != 0 we are guaranteed to test all street names
for(uint32 i=0; i<count; i++) {
buf.clear();
if (cbuffer_t::check_format_strings("%s %s", street_names[idx])) {
buf.printf( street_names[idx], city_name, stop );
if( !all_names.get(buf).is_bound() ) {
return strdup(buf);
}
}
idx = (idx+offset) % count;
}
buf.clear();
}
else {
/* the one random call to avoid desyncs */
simrand(5);
}
}
// still all names taken => then try the normal naming scheme ...
char numbername[10];
if(inside) {
strcpy( numbername, "0center" );
} else if(suburb) {
// close to the city we use a different scheme, with suburbs
strcpy( numbername, "0suburb" );
}
else {
strcpy( numbername, "0extern" );
}
const char *dirname = NULL;
static const char *diagonal_name[4] = { "nordwest", "nordost", "suedost", "suedwest" };
static const char *direction_name[4] = { "nord", "ost", "sued", "west" };
uint8 const rot = welt->get_settings().get_rotation();
if (k.y < ob_gr || (inside && k.y*3 < (un_gr+ob_gr+ob_gr)) ) {
if (k.x < li_gr) {
dirname = diagonal_name[(4 - rot) % 4];
}
else if (k.x > re_gr) {
dirname = diagonal_name[(5 - rot) % 4];
}
else {
dirname = direction_name[(4 - rot) % 4];
}
} else if (k.y > un_gr || (inside && k.y*3 > (un_gr+un_gr+ob_gr)) ) {
if (k.x < li_gr) {
dirname = diagonal_name[(3 - rot) % 4];
}
else if (k.x > re_gr) {
dirname = diagonal_name[(6 - rot) % 4];
}
else {
dirname = direction_name[(6 - rot) % 4];
}
} else {
if (k.x <= stadt->get_pos().x) {
dirname = direction_name[(3 - rot) % 4];
}
else {
dirname = direction_name[(5 - rot) % 4];
}
}
dirname = translator::translate(dirname,lang);
// Try everything to get a unique name
while(true) {
// well now try them all from "0..." over "9..." to "A..." to "Z..."
for( int i=0; i<10+26; i++ ) {
numbername[0] = i<10 ? '0'+i : 'A'+i-10;
const char *base_name = translator::translate(numbername,lang);
if(base_name==numbername) {
// not translated ... try next
continue;
}
// allow for names without direction
uint8 count_s = count_printf_param( base_name );
if(count_s==3) {
if (cbuffer_t::check_format_strings("%s %s %s", base_name) ) {
// ok, try this name, if free ...
buf.printf( base_name, city_name, dirname, stop );
}
}
else {
if (cbuffer_t::check_format_strings("%s %s", base_name) ) {
// ok, try this name, if free ...
buf.printf( base_name, city_name, stop );
}
}
if( buf.len()>0 && !all_names.get(buf).is_bound() ) {
return strdup(buf);
}
buf.clear();
}
// here we did not find a suitable name ...
// ok, no suitable city names, try the suburb ones ...
if( strcmp(numbername+1,"center")==0 ) {
strcpy( numbername, "0suburb" );
}
// ok, no suitable suburb names, try the external ones (if not inside city) ...
else if( strcmp(numbername+1,"suburb")==0 && !inside ) {
strcpy( numbername, "0extern" );
}
else {
// no suitable unique name found at all ...
break;
}
}
}
/* so far we did not found a matching station name
* as a last resort, we will try numbered names
* (or the user requested this anyway)
*/
// strings for intown / outside of town
const char *base_name = translator::translate( inside ? "%s city %d %s" : "%s land %d %s", lang);
// finally: is there a stop with this name already?
for( uint32 i=1; i<65536; i++ ) {
buf.printf( base_name, city_name, i, stop );
if( !all_names.get(buf).is_bound() ) {
return strdup(buf);
}
buf.clear();
}
// emergency measure: But before we should run out of handles anyway ...
assert(0);
return strdup("Unnamed");
}
// add convoi to loading
void haltestelle_t::request_loading( convoihandle_t cnv )
{
if( !loading_here.is_contained( cnv ) ) {
loading_here.append( cnv );
}
if( last_loading_step != welt->get_steps() ) {
last_loading_step = welt->get_steps();
// now iterate over all convois
for( slist_tpl<convoihandle_t>::iterator i = loading_here.begin(), end = loading_here.end(); i != end; ) {
convoihandle_t const c = *i;
if (c.is_bound() && c->get_state() == convoi_t::LOADING) {
// now we load into convoi
c->hat_gehalten(self);
}
if (c.is_bound() && c->get_state() == convoi_t::LOADING) {
++i;
}
else {
i = loading_here.erase( i );
}
}
}
}
bool haltestelle_t::step(uint8 what, sint16 &units_remaining)
{
switch(what) {
case RECONNECTING:
units_remaining -= (rebuild_connections()/256)+2;
break;
case REROUTING:
if( !reroute_goods(units_remaining) ) {
return false;
}
recalc_status();
break;
default:
break;
}
return true;
}
/**
* Called every month
* @author Hj. Malthaner
*/
void haltestelle_t::new_month()
{
if( welt->get_active_player()==owner_p && status_color==color_idx_to_rgb(COL_RED) ) {
cbuffer_t buf;
buf.printf( translator::translate("%s\nis crowded."), get_name() );
welt->get_message()->add_message(buf, get_basis_pos(),message_t::full, PLAYER_FLAG|owner_p->get_player_nr(), IMG_EMPTY );
enables &= (PAX|POST|WARE);
}
// hsiegeln: roll financial history
for (int j = 0; j<MAX_HALT_COST; j++) {
for (int k = MAX_MONTHS-1; k>0; k--) {
financial_history[k][j] = financial_history[k-1][j];
}
financial_history[0][j] = 0;
}
// number of waiting should be constant ...
financial_history[0][HALT_WAITING] = financial_history[1][HALT_WAITING];
}
/**
* Called after schedule calculation of all stations is finished
* will distribute the goods to changed routes (if there are any)
* returns true upon completion
* @author Hj. Malthaner
*/
bool haltestelle_t::reroute_goods(sint16 &units_remaining)
{
if( last_catg_index==255 ) {
last_catg_index = 0;
}
for( ; last_catg_index<goods_manager_t::get_max_catg_index(); last_catg_index++) {
if( units_remaining<=0 ) {
return false;
}
if(cargo[last_catg_index]) {
// first: clean out the array
vector_tpl<ware_t> * warray = cargo[last_catg_index];
vector_tpl<ware_t> * new_warray = new vector_tpl<ware_t>(warray->get_count());
for (size_t j = warray->get_count(); j-- != 0;) {
ware_t & ware = (*warray)[j];
if(ware.menge==0) {
continue;
}
// since also the factory halt list is added to the ground, we can use just this ...
if( welt->access(ware.get_zielpos())->is_connected(self) ) {
// we are already there!
if( ware.to_factory ) {
liefere_an_fabrik(ware);
}
continue;
}
// add to new array
new_warray->append( ware );
}
// delete, if nothing connects here
if( new_warray->empty() ) {
if( all_links[last_catg_index].connections.empty() ) {
// no connections from here => delete
delete new_warray;
new_warray = NULL;
}
}
// replace the array
delete cargo[last_catg_index];
cargo[last_catg_index] = new_warray;
// if something left
// re-route goods to adapt to changes in world layout,
// remove all goods whose destination was removed from the map
if (cargo[last_catg_index] && !cargo[last_catg_index]->empty()) {
vector_tpl<ware_t> &warray = *cargo[last_catg_index];
uint32 last_goods_index = 0;
units_remaining -= warray.get_count();
while( last_goods_index<warray.get_count() ) {
search_route_resumable(warray[last_goods_index]);
if( warray[last_goods_index].get_ziel()==halthandle_t() ) {
// remove invalid destinations
fabrik_t::update_transit( &warray[last_goods_index], false);
warray.remove_at(last_goods_index);
}
else {
++last_goods_index;
}
}
}
}
}