forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimhalt.cc
2111 lines (1729 loc) · 50.6 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
/*
* simhalt.cc
*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project and may not be used
* in other projects without written permission of the author.
*/
/* simhalt.cc
*
* Haltestellen fuer Simutrans
* 03.2000 getrennt von simfab.cc
*
* Hj. Malthaner
*/
#include "simdebug.h"
#include "simmem.h"
#include "simplan.h"
#include "boden/grund.h"
#include "boden/boden.h"
#include "boden/wasser.h"
#include "boden/wege/weg.h"
#include "boden/wege/strasse.h"
#include "simhalt.h"
#include "simfab.h"
#include "simplay.h"
#include "simconvoi.h"
#include "simwin.h"
#include "simworld.h"
#include "simintr.h"
#include "simpeople.h"
#include "freight_list_sorter.h"
#include "simtime.h"
#include "simcolor.h"
#include "simgraph.h"
#include "freight_list_sorter.h"
#include "gui/halt_info.h"
#include "gui/halt_detail.h"
#include "dings/gebaeude.h"
#ifdef LAGER_NOT_IN_USE
#include "dings/lagerhaus.h"
#endif
#include "dataobj/fahrplan.h"
#include "dataobj/warenziel.h"
#include "dataobj/einstellungen.h"
#include "dataobj/umgebung.h"
#include "dataobj/loadsave.h"
#include "dataobj/translator.h"
#include "utils/tocstring.h"
#include "utils/simstring.h"
#include "besch/ware_besch.h"
#include "bauer/warenbauer.h"
//#include "tpl/minivec_tpl.h"
/**
* Max number of hops in route calculation
* @author Hj. Malthaner
*/
static int max_hops = 300;
/**
* Sets max number of hops in route calculation
* @author Hj. Malthaner
*/
void haltestelle_t::set_max_hops(int hops)
{
if(hops > 9994) {
hops = 9994;
}
max_hops = hops;
}
// Helfer Klassen
class HNode {
public:
halthandle_t halt;
int depth;
HNode *link;
};
// Klassenvariablen
slist_tpl<halthandle_t> haltestelle_t::alle_haltestellen;
halthandle_t
haltestelle_t::gib_halt(karte_t *welt, const koord pos)
{
const planquadrat_t *plan = welt->lookup(pos);
if(plan) {
if(plan->gib_halt().is_bound()) {
return plan->gib_halt();
}
// no halt? => we do the water check
if(plan->gib_kartenboden()->ist_wasser()) {
// may catch bus stops close to water ...
if(plan->get_haltlist_count()>0) {
return plan->get_haltlist()[0];
}
}
}
return halthandle_t();
}
koord
haltestelle_t::gib_basis_pos() const
{
if (!grund.empty()) {
return grund.front()->gib_pos().gib_2d();
}
else {
return koord::invalid;
}
}
koord3d
haltestelle_t::gib_basis_pos3d() const
{
if (!grund.empty()) {
return grund.front()->gib_pos();
}
else {
return koord3d::invalid;
}
}
/**
* Station factory method. Returns handles instead of pointers.
* @author Hj. Malthaner
*/
halthandle_t haltestelle_t::create(karte_t *welt, koord pos, spieler_t *sp)
{
haltestelle_t * p = new haltestelle_t(welt, pos, sp);
return p->self;
}
/*
* removes a ground tile from a station
* @author prissi
*/
bool
haltestelle_t::remove(karte_t *welt, spieler_t *sp, koord3d pos, const char *&msg)
{
msg = NULL;
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 = gib_halt(welt,pos);
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);
// remove station building?
gebaeude_t *gb=dynamic_cast<gebaeude_t *>(bd->suche_obj(ding_t::gebaeude));
if(gb) {
DBG_MESSAGE("haltestelle_t::remove()", "removing building" );
const haus_tile_besch_t *tile = gb->gib_tile();
koord size = tile->gib_besch()->gib_groesse( tile->gib_layout() );
const sint32 costs = tile->gib_besch()->gib_level()*(sint32)umgebung_t::cst_multiply_post;
// get startpos
koord k=tile->gib_offset();
if(k != koord(0,0)) {
return haltestelle_t::remove(welt, sp, pos-koord3d(k,0),msg);
}
else {
for(k.y = 0; k.y < size.y; k.y ++) {
for(k.x = 0; k.x < size.x; k.x ++) {
grund_t *gr = welt->lookup(koord3d(k,0)+pos);
if(welt->max_hgt(k+pos.gib_2d())<=welt->gib_grundwasser() || gr->gib_typ()==grund_t::fundament) {
msg=gr->kann_alle_obj_entfernen(sp);
if(msg) {
return false;
}
}
}
}
// remove all gound
DBG_MESSAGE("haltestelle_t::remove()", "removing building: cleanup");
for(k.y = 0; k.y < size.y; k.y ++) {
for(k.x = 0; k.x < size.x; k.x ++) {
grund_t *gr = welt->lookup(koord3d(k,0)+pos);
sp->buche(costs, k, COST_CONSTRUCTION);
if(gr) {
halt->rem_grund(gr);
gebaeude_t *gb=static_cast<gebaeude_t *>(gr->suche_obj(ding_t::gebaeude));
gr->obj_remove(gb); // remove building
delete gb;
gr->setze_text(NULL);
if(gr->gib_typ()==grund_t::fundament) {
uint8 new_slope = gr->gib_hoehe()==welt->min_hgt(k+pos.gib_2d()) ? 0 : welt->calc_natural_slope(k+pos.gib_2d());
welt->access(k+pos.gib_2d())->kartenboden_setzen(new boden_t(welt, koord3d(k+pos.gib_2d(),welt->min_hgt(k+pos.gib_2d())), new_slope) );
}
else if(welt->max_hgt(k+pos.gib_2d())<=welt->gib_grundwasser()) {
welt->access(k+pos.gib_2d())->kartenboden_setzen(new wasser_t(welt, k+pos.gib_2d()) );
}
else {
weg_t *weg = bd->gib_weg(road_wt);
if(weg && static_cast<strasse_t *>(weg)->hat_gehweg()) {
// Stadtstrassen sollten entfernbar sein, deshalb
// dürfen sie keinen Besitzer haben.
bd->setze_besitzer( NULL );
}
}
sp->buche(costs, pos.gib_2d()+k, COST_CONSTRUCTION);
}
}
}
bd = NULL; // no need to do things twice ...
}
}
else {
halt->rem_grund(bd);
}
if(!halt->existiert_in_welt()) {
DBG_DEBUG("haltestelle_t::remove()","remove last");
// all deleted?
halt->gib_besitzer()->halt_remove( halt );
DBG_DEBUG("haltestelle_t::remove()","destroy");
haltestelle_t::destroy( halt );
}
else {
DBG_DEBUG("haltestelle_t::remove()","not last");
// 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_bild();
DBG_DEBUG("haltestelle_t::remove()","reset city way owner");
weg_t *weg = bd->gib_weg(road_wt);
if(weg && static_cast<strasse_t *>(weg)->hat_gehweg()) {
// Stadtstrassen sollten entfernbar sein, deshalb
// dürfen sie keinen Besitzer haben.
bd->setze_besitzer( NULL );
}
bd->setze_text( NULL );
}
return true;
}
/**
* Station factory method. Returns handles instead of pointers.
* @author Hj. Malthaner
*/
halthandle_t haltestelle_t::create(karte_t *welt, loadsave_t *file)
{
haltestelle_t *p = new haltestelle_t(welt, file);
return p->self;
}
/**
* Station destruction method.
* @author Hj. Malthaner
*/
void haltestelle_t::destroy(halthandle_t &halt)
{
haltestelle_t * p = halt.detach();
delete p;
}
/**
* 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.front();
destroy(halt);
}
alle_haltestellen.clear();
}
// Konstruktoren
haltestelle_t::haltestelle_t(karte_t *wl, loadsave_t *file)
: reservation(0), registered_lines(0)
{
self = halthandle_t(this);
alle_haltestellen.insert(self);
grund.clear();
welt = wl;
marke = 0;
pax_happy = 0;
pax_unhappy = 0;
pax_no_route = 0;
status_color = COL_YELLOW;
reroute_counter = welt->get_schedule_counter()-1;
rebuilt_destination_counter = reroute_counter;
enables = NOT_ENABLED;
// @author hsiegeln
sortierung = freight_list_sorter_t::by_name;
resort_freight_info = true;
// Lazy init at opening!
halt_info = NULL;
rdwr(file);
}
haltestelle_t::haltestelle_t(karte_t *wl, koord pos, spieler_t *sp)
: reservation(0), registered_lines(0)
{
self = halthandle_t(this);
alle_haltestellen.insert(self);
welt = wl;
marke = 0;
this->pos = pos;
besitzer_p = sp;
#ifdef LAGER_NOT_IN_USE
lager = NULL;
#endif
enables = NOT_ENABLED;
reroute_counter = welt->get_schedule_counter()-1;
rebuilt_destination_counter = reroute_counter;
pax_happy = 0;
pax_unhappy = 0;
pax_no_route = 0;
status_color = COL_YELLOW;
sortierung = freight_list_sorter_t::by_name;
init_financial_history();
// Lazy init at opening!
halt_info = NULL;
}
haltestelle_t::~haltestelle_t()
{
while (!grund.empty()) {
rem_grund(grund.front());
}
if(halt_info) {
destroy_win(halt_info);
delete halt_info;
halt_info = 0;
}
alle_haltestellen.remove(self);
self.unbind();
for(unsigned int i=0; i<warenbauer_t::gib_waren_anzahl(); i++) {
const ware_besch_t *ware = warenbauer_t::gib_info(i);
slist_tpl<ware_t> *wliste = waren.get(ware);
if(wliste) {
waren.remove(ware);
delete wliste;
}
}
// route may have changed without this station ...
welt->set_schedule_counter();
}
/**
* Sets the name. Creates a copy of name.
* @author Hj. Malthaner
*/
void
haltestelle_t::setze_name(const char *name)
{
tstrncpy(this->name, translator::translate(name), 128);
if (!grund.empty()) {
grund.front()->setze_text(this->name);
}
}
void
haltestelle_t::step()
{
// DBG_MESSAGE("haltestelle_t::step()","%s (cnt %i)",gib_name(),reroute_counter);
// hsiegeln: update amount of waiting ware
financial_history[0][HALT_WAITING] = sum_all_waiting_goods();
if(rebuilt_destination_counter!=welt->get_schedule_counter()) {
// schedule has changed ...
rebuild_destinations();
rebuilt_destination_counter = welt->get_schedule_counter();
}
else {
// all new connection updated => recalc routes
if(reroute_counter!=welt->get_schedule_counter()) {
reroute_goods();
reroute_counter = welt->get_schedule_counter();
// DBG_MESSAGE("haltestelle_t::step()","rerouting goods at %s",gib_name());
}
}
recalc_status();
}
/**
* Called every month
* @author Hj. Malthaner
*/
void haltestelle_t::neuer_monat()
{
if(enables&CROWDED) {
besitzer_p->bescheid_station_voll(self);
enables &= (PAX|POST|WARE);
}
// Hajo: reset passenger statistics
pax_happy = 0;
pax_no_route = 0;
pax_unhappy = 0;
// 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;
}
}
/**
* Called every 255 steps
* will distribute the goods to changed routes (if there are any)
* @author Hj. Malthaner
*/
void haltestelle_t::reroute_goods()
{
// reroute only monthly!
ptrhashtable_iterator_tpl<const ware_besch_t *, slist_tpl<ware_t> *> waren_iter(waren);
while(waren_iter.next()) {
static slist_tpl<ware_t> waren_kill_queue;
slist_tpl<ware_t> * wliste = waren_iter.get_current_value();
slist_iterator_tpl <ware_t> ware_iter(wliste);
waren_kill_queue.clear();
// Hajo:
// Step 1: re-route goods now and then to adapt to changes in
// world layout, remove all goods which destination was removed
// from the map
while(ware_iter.next()) {
ware_t & ware = ware_iter.access_current();
// since also the factory halt list is added to the ground, we can use just this ...
if(welt->lookup(ware.gib_zielpos())->is_connected(self)) {
// we are already there!
if(warenbauer_t::ist_fabrik_ware(ware.gib_typ())) {
liefere_an_fabrik(ware);
}
waren_kill_queue.insert(ware);
}
else {
suche_route(ware);
}
INT_CHECK("simhalt 457");
// check if this good can still reach its destination
if(!gib_halt(welt,ware.gib_ziel()).is_bound() || !gib_halt(welt,ware.gib_zwischenziel()).is_bound()) {
// schedule it for removal
waren_kill_queue.insert(ware);
}
}
while( waren_kill_queue.count() ) {
ware_t w = waren_kill_queue.remove_first();
DBG_MESSAGE("haltestelle_t::reroute_goods()","removing %i %s",w.menge,w.gib_name() );
wliste->remove(w);
}
}
}
/**
* Calculates a status color for status bars
* @author Hj. Malthaner
*/
void haltestelle_t::recalc_status()
{
status_color = financial_history[0][HALT_CONVOIS_ARRIVED] > 0 ? COL_GREEN : COL_YELLOW;
// has passengers
if(get_pax_happy() > 0 || get_pax_no_route() > 0) {
if(get_pax_unhappy() > 200 ) {
status_color = COL_RED;
} else if(get_pax_unhappy() > 40) {
status_color = COL_ORANGE;
}
}
// check for goods
if(status_color!=COL_RED && get_ware_enabled()) {
const int count = warenbauer_t::gib_waren_anzahl();
const int max_ware = get_capacity();
for( int i=0; i+1<count; i++) {
const ware_besch_t *wtyp = warenbauer_t::gib_info(i+1);
if( gib_ware_summe(wtyp)>max_ware ) {
status_color = COL_RED;
break;
}
}
}
}
/**
* Draws some nice colored bars giving some status information
* @author Hj. Malthaner
*/
void haltestelle_t::display_status(int xpos, int ypos) const
{
const int count = warenbauer_t::gib_waren_anzahl();
ypos -= 11;
// all variables in the bracket MUST be signed, otherwise nothing may be drawn at all
xpos -= (count*4 - get_tile_raster_width())/2;
for( int i=0; i+1<count; i++) {
const ware_besch_t *wtyp = warenbauer_t::gib_info(i+1);
const int v = min((gib_ware_summe(wtyp) >> 2) + 2, 128);
display_fillbox_wh_clip(xpos+i*4, ypos-v-1, 1, v, COL_GREY4, true);
display_fillbox_wh_clip(xpos+i*4+1, ypos-v-1, 2, v,
// (i & 7) * 4 + 1,
255 - i*4, true);
display_fillbox_wh_clip(xpos+i*4+3, ypos-v-1, 1, v, COL_GREY1, true);
// Hajo: show up arrow for capped values
if(v == 128) {
display_fillbox_wh_clip(xpos+i*4+1, ypos-v-6, 2, 4, COL_WHITE, true);
display_fillbox_wh_clip(xpos+i*4, ypos-v-5, 4, 1, COL_WHITE, true);
}
}
const int color = gib_status_farbe();
display_fillbox_wh_clip(xpos-1, ypos, count*4-2, 4, color, true);
}
/*
* connects a factory to a halt
*/
void
haltestelle_t::verbinde_fabriken()
{
if(!grund.empty()) {
{ // unlink all
slist_iterator_tpl <fabrik_t *> fab_iter(fab_list);
while( fab_iter.next() ) {
fab_iter.get_current()->unlink_halt(self);
}
}
fab_list.clear();
slist_iterator_tpl<grund_t *> iter( grund );
while(iter.next()) {
grund_t *gb = iter.get_current();
koord p = gb->gib_pos().gib_2d();
vector_tpl<fabrik_t *> &fablist = fabrik_t::sind_da_welche( welt,
p-koord( welt->gib_einstellungen()->gib_station_coverage(), welt->gib_einstellungen()->gib_station_coverage()),
p+koord( welt->gib_einstellungen()->gib_station_coverage(), welt->gib_einstellungen()->gib_station_coverage())
);
for(unsigned i=0; i<fablist.get_count(); i++) {
fabrik_t* fab = fablist[i];
if(!fab_list.contains(fab)) {
fab_list.insert(fab);
fab->link_halt(self);
}
}
}
}
}
/*
* removes factory to a halt
*/
void
haltestelle_t::remove_fabriken(fabrik_t *fab)
{
DBG_MESSAGE("haltestelle_t::remove_fabriken()","removing %p",fab);
for(unsigned i=0; i<fab_list.count(); i++ ) {
DBG_MESSAGE("haltestelle_t::remove_fabriken()","fab_list at(%i) = %p",i,fab_list.at(i));
}
DBG_MESSAGE("haltestelle_t::remove_fabriken()","removing %s",fab->gib_name());
bool ok=fab_list.remove(fab);
DBG_MESSAGE("karte_t::remove_fabriken()","fab_list now %i(%i)",fab_list.count(),ok);
for(unsigned i=0; i<fab_list.count(); i++ ) {
DBG_MESSAGE("haltestelle_t::remove_fabriken()","fab_list at(%i) = %p",i,fab_list.at(i));
}
}
/**
* Rebuilds the list of reachable destinations
*
* @author Hj. Malthaner
*/
void haltestelle_t::rebuild_destinations()
{
// Hajo: first, remove all old entries
warenziele.clear();
// DBG_MESSAGE("haltestelle_t::rebuild_destinations()", "Adding new table entries");
// Hajo: second, calculate new entries
for(unsigned i=0; i<welt->get_convoi_count(); i++ ) {
convoihandle_t cnv = welt->get_convoi_array()[i];
// DBG_MESSAGE("haltestelle_t::rebuild_destinations()", "convoi %d %p", cnv.get_id(), cnv.get_rep());
if(gib_besitzer()==welt->gib_spieler(1) || cnv->gib_besitzer()==gib_besitzer()) {
INT_CHECK("simhalt.cc 612");
fahrplan_t *fpl = cnv->gib_fahrplan();
if(fpl) {
for(int i=0; i<fpl->maxi(); i++) {
// Hajo: Hält dieser convoi hier?
if (gib_halt(welt, fpl->eintrag[i].pos) == self) {
const int anz = cnv->gib_vehikel_anzahl();
for(int j=0; j<anz; j++) {
vehikel_t *v = cnv->gib_vehikel(j);
hat_gehalten(0,v->gib_fracht_typ(), fpl );
}
}
}
}
}
}
}
void
haltestelle_t::liefere_an_fabrik(const ware_t ware)
{
slist_iterator_tpl<fabrik_t *> fab_iter(fab_list);
while(fab_iter.next()) {
fabrik_t * fab = fab_iter.get_current();
const vector_tpl<ware_t>& eingang = fab->gib_eingang();
for (uint32 i = 0; i < eingang.get_count(); i++) {
if (eingang[i].gib_typ() == ware.gib_typ() && ware.gib_zielpos() == fab->gib_pos().gib_2d()) {
fab->liefere_an(ware.gib_typ(), ware.menge);
return;
}
}
}
}
/**
* Kann die Ware nicht zum Ziel geroutet werden (keine Route), dann werden
* Ziel und Zwischenziel auf koord::invalid gesetzt.
*
* @param ware die zu routende Ware
* @param start die Starthaltestelle
* @author Hj. Malthaner
*/
void
haltestelle_t::suche_route(ware_t &ware, koord *next_to_ziel)
{
const ware_besch_t * warentyp = ware.gib_typ();
const koord ziel = ware.gib_zielpos();
// since also the factory halt list is added to the ground, we can use just this ...
const planquadrat_t *plan = welt->lookup(ziel);
const halthandle_t *halt_list = plan->get_haltlist();
// but we can only use a subset of these
minivec_tpl <halthandle_t> ziel_list (plan->get_haltlist_count());
for( unsigned h=0; h<plan->get_haltlist_count(); h++ ) {
halthandle_t halt = halt_list[h];
if( halt->is_enabled(warentyp) ) {
ziel_list.append( halt );
}
else {
//DBG_MESSAGE("suche_route()","halt %s near (%i,%i) does not accept %s!",halt->gib_name(),ziel.x,ziel.y,warentyp->gib_name());
}
}
if (ziel_list.empty()) {
ware.setze_ziel(koord::invalid);
ware.setze_zwischenziel(koord::invalid);
// printf("keine route zu %d,%d nach %d steps\n", ziel.x, ziel.y, step);
if(next_to_ziel!=NULL) {
*next_to_ziel = koord::invalid;
}
//DBG_MESSAGE("suche_route()","no target near (%i,%i) out of %i stations!",ziel.x,ziel.y,halt_list.get_count());
return;
}
// check, if the shortest connection is not right to us ...
if(ziel_list.is_contained(self)) {
ware.setze_ziel(pos);
ware.setze_zwischenziel(koord::invalid);
if(next_to_ziel!=NULL) {
*next_to_ziel = koord::invalid;
}
}
static HNode nodes[10000];
static uint32 current_mark = 0;
const int max_transfers = umgebung_t::max_transfers;
INT_CHECK("simhalt 452");
// Need to clean up ?
if(current_mark > (1u<<31)) {
slist_iterator_tpl<halthandle_t > halt_iter (alle_haltestellen);
while(halt_iter.next()) {
halt_iter.get_current()->marke = 0;
}
current_mark = 0;
}
// alle alten markierungen ungültig machen
current_mark++;
// die Berechnung erfolgt durch eine Breitensuche fuer Graphen
// Warteschlange fuer Breitensuche
static slist_tpl <HNode *> queue;
queue.clear();
int step = 1;
HNode *tmp;
nodes[0].halt = self;
nodes[0].link = 0;
nodes[0].depth = 0;
queue.insert( &nodes[0] ); // init queue mit erstem feld
self->marke = current_mark;
// Breitensuche
// long t0 = get_current_time_millis();
do {
tmp = queue.remove_first();
const halthandle_t halt = tmp->halt;
if(ziel_list.is_contained(halt)) {
// ziel gefunden
goto found;
}
// Hajo: check for max transfers -> don't add more stations
// to queue if the limit is reached
if(tmp->depth < max_transfers) {
// ziele prüfen
slist_iterator_tpl<warenziel_t> iter(halt->warenziele);
while(iter.next() && step<max_hops) {
// check if destination if for the goods type
warenziel_t wz = iter.get_current();
if(wz.gib_typ()->is_interchangeable(warentyp)) {
// since these are precalculated, they should be always pointing to a valid ground
// (if not, we were just under construction, and will be fine after 16 steps)
const halthandle_t tmp_halt = welt->lookup(wz.gib_ziel())->gib_halt();
if(tmp_halt.is_bound() && tmp_halt->marke != current_mark && tmp_halt->is_enabled(warentyp)) {
HNode *node = &nodes[step++];
node->halt = tmp_halt;
node->depth = tmp->depth + 1;
node->link = tmp;
queue.append( node );
// betretene Haltestellen markieren
tmp_halt->marke = current_mark;
}
}
}
} // max transfers
/*
else {
printf("routing %s to %s -> transfer limit reached\n",
ware.gib_name(),
gib_halt(ware.gib_ziel())->gib_name());
}
*/
} while(queue.count() && step < max_hops);
// if the loop ends, nothing was found
tmp = 0;
// printf("No route found in %d steps\n", step);
found:
// long t1 = get_current_time_millis();
// printf("Route calc took %ld ms, %d steps\n", t1-t0, step);
INT_CHECK("simhalt 606");
// long t2 = get_current_time_millis();
if(tmp) {
// ziel gefunden
ware.setze_ziel( tmp->halt->gib_basis_pos() );
if(tmp->link == NULL) {
// kein zwischenziel
ware.setze_zwischenziel(ware.gib_ziel());
if(next_to_ziel!=NULL) {
// for reverse route the next hop, but not hop => enter start
//DBG_DEBUG("route","zwischenziel %s",tmp->halt->gib_name() );
*next_to_ziel = self->gib_basis_pos();
}
}
else {
// next to start
if(next_to_ziel!=NULL) {
// for reverse route the next hop
*next_to_ziel = tmp->link->halt->gib_basis_pos();
//DBG_DEBUG("route","zwischenziel %s",tmp->halt->gib_name(), start->gib_name() );
}
// zwischenziel ermitteln
while(tmp->link->link) {
tmp = tmp->link;
}
ware.setze_zwischenziel(tmp->halt->gib_basis_pos());
}
/*
printf("route %s to %s via %s in %d steps\n",
ware.gib_name(),
gib_halt(ware.gib_ziel())->gib_name(),
gib_halt(ware.gib_zwischenziel())->gib_name(),
step);
*/
}
else {
// Kein Ziel gefunden
ware.setze_ziel(koord::invalid);
ware.setze_zwischenziel(koord::invalid);
// printf("keine route zu %d,%d nach %d steps\n", ziel.x, ziel.y, step);
if(next_to_ziel!=NULL) {
*next_to_ziel = koord::invalid;
}
}
}
/**
* Found route and station uncrowded
* @author Hj. Malthaner
*/
void haltestelle_t::add_pax_happy(int n)
{
pax_happy += n;
book(n, HALT_HAPPY);
}
/**
* Found no route
* @author Hj. Malthaner
*/
void haltestelle_t::add_pax_no_route(int n)
{
pax_no_route += n;
book(n, HALT_NOROUTE);
}
/**
* Station crowded
* @author Hj. Malthaner
*/
void haltestelle_t::add_pax_unhappy(int n)
{
pax_unhappy += n;
book(n, HALT_UNHAPPY);
}
bool
haltestelle_t::add_grund(grund_t *gr)
{
assert(gr!=NULL);
// neu halt?
if(!grund.contains(gr)) {
koord pos=gr->gib_pos().gib_2d();
gr->setze_halt(self);
grund.append(gr);
reservation.append(0,16);
// appends this to the ground
// after that, the surrounding ground will know of this station
for( int y=-welt->gib_einstellungen()->gib_station_coverage(); y<=welt->gib_einstellungen()->gib_station_coverage(); y++ ) {
for( int x=-welt->gib_einstellungen()->gib_station_coverage(); x<=welt->gib_einstellungen()->gib_station_coverage(); x++ ) {
koord p=pos+koord(x,y);
if(welt->ist_in_kartengrenzen(p)) {
welt->access(p)->add_to_haltlist( self );
welt->lookup(p)->gib_kartenboden()->set_flag(grund_t::dirty);
}
}
}
welt->access(pos)->setze_halt(self);
//DBG_MESSAGE("haltestelle_t::add_grund()","pos %i,%i,%i to %s added.",pos.x,pos.y,pos.z,gib_name());
vector_tpl<fabrik_t *> &fablist = fabrik_t::sind_da_welche( welt,
pos-koord(welt->gib_einstellungen()->gib_station_coverage(), welt->gib_einstellungen()->gib_station_coverage()),
pos+koord(welt->gib_einstellungen()->gib_station_coverage(), welt->gib_einstellungen()->gib_station_coverage())
);
for(unsigned i=0; i<fablist.get_count(); i++) {
fabrik_t* fab = fablist[i];
if(!fab_list.contains(fab)) {
fab_list.insert(fab);
fab->link_halt(self);
}
}
assert(welt->lookup(pos)->gib_halt() == self && gr->is_halt());
return true;
}
else {
return false;
}