forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 53
/
path_explorer.cc
1832 lines (1524 loc) · 53.3 KB
/
path_explorer.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) 2009 : Knightly
*
* A centralised, steppable path searching system using Floyd-Warshall Algorithm
*/
#include "path_explorer.h"
#include "tpl/slist_tpl.h"
#include "dataobj/translator.h"
#include "bauer/warenbauer.h"
#include "besch/ware_besch.h"
#include "simsys.h"
#include "simgraph.h"
#include "player/simplay.h"
#include "dataobj/umgebung.h"
#include "dataobj/fahrplan.h"
#include "simconvoi.h"
#include "simloadingscreen.h"
typedef quickstone_hashtable_tpl<haltestelle_t, haltestelle_t::connexion*> connexions_map_single_remote;
// #define DEBUG_EXPLORER_SPEED
// #define DEBUG_COMPARTMENT_STEP
///////////////////////////////////////////////
// path_explorer_t
karte_t *path_explorer_t::world = NULL;
uint8 path_explorer_t::max_categories = 0;
uint8 path_explorer_t::category_empty = 255;
path_explorer_t::compartment_t *path_explorer_t::goods_compartment = NULL;
uint8 path_explorer_t::current_compartment = 0;
bool path_explorer_t::processing = false;
void path_explorer_t::initialise(karte_t *welt)
{
if (welt)
{
world = welt;
}
max_categories = warenbauer_t::get_max_catg_index();
category_empty = warenbauer_t::nichts->get_catg_index();
goods_compartment = new compartment_t[max_categories];
for (uint8 i = 0; i < max_categories; ++i)
{
goods_compartment[i].set_category(i);
}
current_compartment = 0;
processing = false;
compartment_t::initialise();
}
void path_explorer_t::finalise()
{
delete[] goods_compartment;
goods_compartment = NULL;
current_compartment = 0;
category_empty = 255;
max_categories = 0;
processing = false;
compartment_t::finalise();
}
void path_explorer_t::step()
{
// at most check all goods categories once
for (uint8 i = 0; i < max_categories; ++i)
{
if ( current_compartment != category_empty
&& (!goods_compartment[current_compartment].is_refresh_completed()
|| goods_compartment[current_compartment].is_refresh_requested() ) )
{
processing = true; // this step performs something
// perform step
goods_compartment[current_compartment].step();
// if refresh is completed, move on to the next category
if ( goods_compartment[current_compartment].is_refresh_completed() )
{
current_compartment = (current_compartment + 1) % max_categories;
}
// each step process at most 1 goods category
return;
}
// advance to the next category only if compartment.step() is not invoked
current_compartment = (current_compartment + 1) % max_categories;
}
processing = false; // this step performs nothing
}
void path_explorer_t::full_instant_refresh()
{
// exclude empty goods (nichts)
uint16 total_steps = (max_categories - 1) * 6;
uint16 curr_step = 0;
processing = true;
// initialize progress bar
loadingscreen_t ls( translator::translate("Calculating paths ..."), total_steps, true, true);
ls.set_progress(curr_step);
// disable the iteration limits
compartment_t::enable_limits(false);
// clear all connexion hash tables and reset serving transport counters
compartment_t::reset_connexion_list();
#ifdef DEBUG_EXPLORER_SPEED
unsigned long start, diff;
start = dr_time();
#endif
for (uint8 c = 0; c < max_categories; ++c)
{
if ( c != category_empty )
{
// clear any previous leftovers
goods_compartment[c].reset(true);
#ifndef DEBUG_EXPLORER_SPEED
// go through all 6 phases
for (uint8 p = 0; p < 6; ++p)
{
// perform step
goods_compartment[c].step();
++curr_step;
ls.set_progress(curr_step);
}
#else
// one step should perform the compartment phases from the first phase till the path exploration phase
goods_compartment[c].step();
curr_step += 6;
ls.set_progress(curr_step);
#endif
}
}
#ifdef DEBUG_EXPLORER_SPEED
diff = dr_time() - start;
printf("\n\nTotal time taken : %lu ms \n", diff);
#endif
// enable iteration limits again
compartment_t::enable_limits(true);
// reset current category pointer
current_compartment = 0;
processing = false;
}
void path_explorer_t::refresh_all_categories(const bool reset_working_set)
{
if (reset_working_set)
{
for (uint8 c = 0; c < max_categories; ++c)
{
// do not remove the finished matrix and halt index map
goods_compartment[c].reset(false);
}
// clear all connexion hash tables and reset serving transport counters
compartment_t::reset_connexion_list();
// reset current category pointer : refresh will start from passengers
current_compartment = 0;
}
else
{
for (uint8 c = 0; c < max_categories; ++c)
{
// only set flag
goods_compartment[c].set_refresh();
}
}
}
///////////////////////////////////////////////
// compartment_t
const char *const path_explorer_t::compartment_t::phase_name[] =
{
"flag",
"prepare",
"rebuild",
"filter",
"matrix",
"explore",
"reroute"
};
path_explorer_t::compartment_t::connexion_list_entry_t path_explorer_t::compartment_t::connexion_list[65536];
bool path_explorer_t::compartment_t::use_limits = true;
uint32 path_explorer_t::compartment_t::limit_rebuild_connexions = default_rebuild_connexions;
uint32 path_explorer_t::compartment_t::limit_filter_eligible = default_filter_eligible;
uint32 path_explorer_t::compartment_t::limit_fill_matrix = default_fill_matrix;
uint64 path_explorer_t::compartment_t::limit_explore_paths = default_explore_paths;
uint32 path_explorer_t::compartment_t::limit_reroute_goods = default_reroute_goods;
uint32 path_explorer_t::compartment_t::local_rebuild_connexions = default_rebuild_connexions;
uint32 path_explorer_t::compartment_t::local_filter_eligible = default_filter_eligible;
uint32 path_explorer_t::compartment_t::local_fill_matrix = default_fill_matrix;
uint64 path_explorer_t::compartment_t::local_explore_paths = default_explore_paths;
uint32 path_explorer_t::compartment_t::local_reroute_goods = default_reroute_goods;
bool path_explorer_t::compartment_t::local_limits_changed = false;
uint16 path_explorer_t::compartment_t::representative_halt_count = 0;
uint8 path_explorer_t::compartment_t::representative_category = 0;
path_explorer_t::compartment_t::compartment_t()
{
refresh_start_time = 0;
finished_matrix = NULL;
finished_halt_index_map = NULL;
finished_halt_count = 0;
working_matrix = NULL;
transport_index_map = NULL;
transport_matrix = NULL;
working_halt_index_map = NULL;
working_halt_list = NULL;
working_halt_count = 0;
all_halts_list = NULL;
all_halts_count = 0;
linkages = NULL;
transfer_list = NULL;
transfer_count = 0;
catg = 255;
catg_name = NULL;
step_count = 0;
paths_available = false;
refresh_completed = true;
refresh_requested = true;
current_phase = phase_check_flag;
phase_counter = 0;
iterations = 0;
total_iterations = 0;
via_index = 0;
origin_cluster_index = 0;
target_cluster_index = 0;
origin_member_index = 0;
inbound_connections = NULL;
outbound_connections = NULL;
process_next_transfer = true;
statistic_duration = 0;
statistic_iteration = 0;
}
path_explorer_t::compartment_t::~compartment_t()
{
if (finished_matrix)
{
for (uint16 i = 0; i < finished_halt_count; ++i)
{
delete[] finished_matrix[i];
}
delete[] finished_matrix;
}
if (finished_halt_index_map)
{
delete[] finished_halt_index_map;
}
if (working_matrix)
{
for (uint16 i = 0; i < working_halt_count; ++i)
{
delete[] working_matrix[i];
}
delete[] working_matrix;
}
if (transport_index_map)
{
delete[] transport_index_map;
}
if (transport_matrix)
{
for (uint16 i = 0; i < working_halt_count; ++i)
{
delete[] transport_matrix[i];
}
delete[] transport_matrix;
}
if (working_halt_index_map)
{
delete[] working_halt_index_map;
}
if (working_halt_list)
{
delete[] working_halt_list;
}
if (all_halts_list)
{
delete[] all_halts_list;
}
if (linkages)
{
delete linkages;
}
if (transfer_list)
{
delete[] transfer_list;
}
if (inbound_connections)
{
delete inbound_connections;
}
if (outbound_connections)
{
delete outbound_connections;
}
}
void path_explorer_t::compartment_t::reset(const bool reset_finished_set)
{
refresh_start_time = 0;
if (reset_finished_set)
{
if (finished_matrix)
{
for (uint16 i = 0; i < finished_halt_count; ++i)
{
delete[] finished_matrix[i];
}
delete[] finished_matrix;
finished_matrix = NULL;
}
if (finished_halt_index_map)
{
delete[] finished_halt_index_map;
finished_halt_index_map = NULL;
}
finished_halt_count = 0;
}
if (working_matrix)
{
for (uint16 i = 0; i < working_halt_count; ++i)
{
delete[] working_matrix[i];
}
delete[] working_matrix;
working_matrix = NULL;
}
if (transport_index_map)
{
delete[] transport_index_map;
transport_index_map = NULL;
}
if (transport_matrix)
{
for (uint16 i = 0; i < working_halt_count; ++i)
{
delete[] transport_matrix[i];
}
delete[] transport_matrix;
transport_matrix = NULL;
}
if (working_halt_index_map)
{
delete[] working_halt_index_map;
working_halt_index_map = NULL;
}
if (working_halt_list)
{
delete[] working_halt_list;
working_halt_list = NULL;
}
working_halt_count = 0;
if (all_halts_list)
{
delete[] all_halts_list;
all_halts_list = NULL;
}
all_halts_count = 0;
if (linkages)
{
delete linkages;
linkages = NULL;
}
if (transfer_list)
{
delete[] transfer_list;
transfer_list = NULL;
}
transfer_count = 0;
if (inbound_connections)
{
delete inbound_connections;
inbound_connections = NULL;
}
if (outbound_connections)
{
delete outbound_connections;
outbound_connections = NULL;
}
process_next_transfer = true;
#ifdef DEBUG_COMPARTMENT_STEP
step_count = 0;
#endif
if (reset_finished_set)
{
paths_available = false;
}
refresh_completed = true;
refresh_requested = true;
current_phase = phase_check_flag;
phase_counter = 0;
iterations = 0;
total_iterations = 0;
via_index = 0;
origin_cluster_index = 0;
target_cluster_index = 0;
origin_member_index = 0;
statistic_duration = 0;
statistic_iteration = 0;
}
void path_explorer_t::compartment_t::initialise()
{
initialise_connexion_list();
}
void path_explorer_t::compartment_t::finalise()
{
finalise_connexion_list();
}
void path_explorer_t::compartment_t::step()
{
#ifdef DEBUG_COMPARTMENT_STEP
printf("\n\nCategory : %s \n", translator::translate( catg_name ) );
#endif
// For timing use
unsigned long start, diff;
switch (current_phase)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Phase 0 : Determine if a new refresh should be done, and prepare relevant flags accordingly
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case phase_check_flag :
{
if (refresh_requested)
{
refresh_requested = false; // immediately reset it so that we can take new requests
refresh_completed = false; // indicate that processing is at work
refresh_start_time = dr_time();
current_phase = phase_init_prepare; // proceed to next phase
// no return statement here, as we want to fall through to the next phase
}
else
{
#ifdef DEBUG_COMPARTMENT_STEP
printf("\t\t\tRefresh has not been requested\n");
#endif
return;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Phase 1 : Prepare a list of all halts, a halt index map, and a list of linkages for connexions reconstruction
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case phase_init_prepare :
{
#ifdef DEBUG_COMPARTMENT_STEP
++step_count;
printf("\t\tCurrent Step : %lu \n", step_count);
start = dr_time(); // start timing
#endif
slist_tpl<halthandle_t>::iterator halt_iter = haltestelle_t::get_alle_haltestellen().begin();
all_halts_count = (uint16) haltestelle_t::get_alle_haltestellen().get_count();
// create all halts list
if (all_halts_count > 0)
{
all_halts_list = new halthandle_t[all_halts_count];
}
const bool no_walking_connexions = !world->get_settings().get_allow_routing_on_foot() || catg!=warenbauer_t::passagiere->get_catg_index();
// Save the halt list in an array first to prevent the list from being modified across steps, causing bugs
for (uint16 i = 0; i < all_halts_count; ++i)
{
all_halts_list[i] = *halt_iter;
++halt_iter;
// create an empty connexion hash table if the current halt does not already have one
if ( connexion_list[ all_halts_list[i].get_id() ].connexion_table == NULL )
{
connexion_list[ all_halts_list[i].get_id() ].connexion_table = new quickstone_hashtable_tpl<haltestelle_t, haltestelle_t::connexion*>();
}
// Connect halts within walking distance of each other (for passengers only)
// @author: jamespetts, July 2011
if ( no_walking_connexions || !all_halts_list[i]->is_enabled(warenbauer_t::passagiere) )
{
continue;
}
const uint32 halts_within_walking_distance = all_halts_list[i]->get_number_of_halts_within_walking_distance();
halthandle_t walking_distance_halt;
haltestelle_t::connexion *new_connexion;
for ( uint32 x = 0; x < halts_within_walking_distance; ++x )
{
walking_distance_halt = all_halts_list[i]->get_halt_within_walking_distance(x);
if(!walking_distance_halt.is_bound() || !walking_distance_halt->is_enabled(warenbauer_t::passagiere))
{
continue;
}
const uint32 walking_journey_distance = shortest_distance(
all_halts_list[i]->get_next_pos(walking_distance_halt->get_basis_pos()),
walking_distance_halt->get_next_pos(all_halts_list[i]->get_basis_pos())
);
const uint16 journey_time = world->walking_time_tenths_from_distance(walking_journey_distance);
// Check the journey times to the connexion
new_connexion = new haltestelle_t::connexion;
new_connexion->waiting_time = 0; // People do not need to wait to walk.
new_connexion->transfer_time = walking_distance_halt->get_transfer_time();
new_connexion->best_convoy = convoihandle_t();
new_connexion->best_line = linehandle_t();
new_connexion->journey_time = journey_time;
new_connexion->alternative_seats = 0;
// These are walking connexions only. There will not be multiple possible connexions, so no need
// to check for existing connexions here.
connexion_list[ all_halts_list[i].get_id() ].connexion_table->put(walking_distance_halt, new_connexion);
connexion_list[ all_halts_list[i].get_id() ].serving_transport = 1u; // will become an interchange if served by additional transport(s)
all_halts_list[i]->prepare_goods_list(catg);
}
}
// create and initlialize a halthandle-entry to matrix-index map (halt index map)
working_halt_index_map = new uint16[65536];
for (uint32 i = 0; i < 65536; ++i)
{
// For quickstone handle, there can at most be 65535 valid entries, plus entry 0 which is reserved for null handle
// Thus, the range of quickstone entries [1, 65535] is mapped to the range of matrix index [0, 65534]
// Matrix index 65535 either means null handle or the halt has no connexion of the relevant goods category
// This is always created regardless
working_halt_index_map[i] = 65535;
}
transport_index_map = new uint16[131072](); // initialise all elements to zero
// create a list of schedules of lines and lineless convoys
linkages = new vector_tpl<linkage_t>(1024);
convoihandle_t current_convoy;
linehandle_t current_line;
linkage_t temp_linkage;
// loop through all convoys
for (vector_tpl<convoihandle_t>::const_iterator i = world->convoys().begin(), end = world->convoys().end(); i != end; i++)
{
current_convoy = *i;
// only consider lineless convoys which support this compartment's goods catetory
if ( !current_convoy->get_line().is_bound() && current_convoy->get_goods_catg_index().is_contained(catg) )
{
temp_linkage.convoy = current_convoy;
linkages->append(temp_linkage);
transport_index_map[ 65536u + current_convoy.get_id() ] = linkages->get_count();
}
}
temp_linkage.convoy = convoihandle_t(); // reset the convoy handle component
// loop through all lines of all players
for (int i = 0; i < MAX_PLAYER_COUNT; ++i)
{
spieler_t *current_player = world->get_spieler(i);
if( current_player == NULL )
{
continue;
}
for (vector_tpl<linehandle_t>::const_iterator j = current_player->simlinemgmt.get_all_lines().begin(),
end = current_player->simlinemgmt.get_all_lines().end(); j != end; j++)
{
current_line = *j;
// only consider lines which support this compartment's goods category
if ( current_line->get_goods_catg_index().is_contained(catg) )
{
temp_linkage.line = current_line;
linkages->append(temp_linkage);
transport_index_map[ current_line.get_id() ] = linkages->get_count();
}
}
}
// can have at most 65535 different lines and lineless convoys; passing this limit should be extremely unlikely
assert( linkages->get_count() <= 65535u );
#ifdef DEBUG_COMPARTMENT_STEP
diff = dr_time() - start; // stop timing
printf("\tTotal Halt Count : %lu \n", all_halts_count);
printf("\tTotal Lines/Lineless Convoys Count : %ul \n", linkages->get_count());
printf("\t\t\tInitial prepration takes : %lu ms \n", diff);
#endif
current_phase = phase_rebuild_connexions; // proceed to the next phase
#ifndef DEBUG_EXPLORER_SPEED
return;
#endif
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Phase 2 : Rebuild connexions for this compartment's goods category
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case phase_rebuild_connexions :
{
#ifdef DEBUG_COMPARTMENT_STEP
++step_count;
printf("\t\tCurrent Step : %lu \n", step_count);
#endif
const ware_besch_t *const ware_type = warenbauer_t::get_info_catg_index(catg);
linkage_t current_linkage;
schedule_t *current_schedule;
spieler_t *current_owner;
uint32 current_average_speed;
uint8 entry_count;
halthandle_t current_halt;
minivec_tpl<halthandle_t> halt_list(64);
minivec_tpl<uint16> journey_time_list(64);
minivec_tpl<bool> recurrence_list(64); // an array indicating whether certain halts have been processed already
uint32 accumulated_journey_time;
quickstone_hashtable_tpl<haltestelle_t, haltestelle_t::connexion*> *catg_connexions;
haltestelle_t::connexion *new_connexion;
start = dr_time(); // start timing
// for each schedule of line / lineless convoy
while (phase_counter < linkages->get_count())
{
current_linkage = (*linkages)[phase_counter];
// determine schedule, owner and average speed
if ( current_linkage.line.is_bound() && current_linkage.line->get_schedule() && current_linkage.line->count_convoys() )
{
// Case : a line
current_schedule = current_linkage.line->get_schedule();
current_owner = current_linkage.line->get_besitzer();
current_average_speed = (uint32) ( current_linkage.line->get_finance_history(1, LINE_AVERAGE_SPEED) > 0 ?
current_linkage.line->get_finance_history(1, LINE_AVERAGE_SPEED) :
( speed_to_kmh(current_linkage.line->get_convoy(0)->get_min_top_speed()) >> 1 ) );
}
else if ( current_linkage.convoy.is_bound() && current_linkage.convoy->get_schedule() )
{
// Case : a lineless convoy
current_schedule = current_linkage.convoy->get_schedule();
current_owner = current_linkage.convoy->get_besitzer();
current_average_speed = (uint32) ( current_linkage.convoy->get_finance_history(1, convoi_t::CONVOI_AVERAGE_SPEED) > 0 ?
current_linkage.convoy->get_finance_history(1, convoi_t::CONVOI_AVERAGE_SPEED) :
( speed_to_kmh(current_linkage.convoy->get_min_top_speed()) >> 1 ) );
}
else
{
// Case : nothing is bound -> just ignore
++phase_counter;
continue;
}
// create a list of reachable halts
bool reverse = false;
entry_count = current_schedule->is_mirrored() ? (current_schedule->get_count() * 2) - 2 : current_schedule->get_count();
halt_list.clear();
recurrence_list.clear();
uint8 index = 0;
while (entry_count-- && index < current_schedule->get_count())
{
current_halt = haltestelle_t::get_halt(world, current_schedule->eintrag[index].pos, current_owner);
// Make sure that the halt found was built before refresh started and that it supports current goods category
if ( current_halt.is_bound() && current_halt->get_inauguration_time() < refresh_start_time && current_halt->is_enabled(ware_type) )
{
// Assign to halt list only if current halt supports this compartment's goods category
halt_list.append(current_halt, 64);
// Initialise the corresponding recurrence list entry to false
recurrence_list.append(false, 64);
}
current_schedule->increment_index(&index, &reverse);
}
// precalculate journey times between consecutive halts
// This is now only a fallback in case the point to point journey time data are not available.
entry_count = halt_list.get_count();
uint16 journey_time = 0;
journey_time_list.clear();
journey_time_list.append(0); // reserve the first entry for the last journey time from last halt to first halt
for (uint8 i = 0; i < entry_count; ++i)
{
journey_time = 0;
const id_pair pair(halt_list[i].get_id(), halt_list[(i+1)%entry_count].get_id());
if ( current_linkage.line.is_bound() && current_linkage.line->get_average_journey_times()->is_contained(pair) )
{
if(!halt_list[i].is_bound() || ! halt_list[(i+1)%entry_count].is_bound())
{
current_linkage.line->get_average_journey_times()->remove(pair);
continue;
}
else
{
journey_time = current_linkage.line->get_average_journey_times()->access(pair)->reduce();
}
}
else if ( current_linkage.convoy.is_bound() && current_linkage.convoy->get_average_journey_times()->is_contained(pair) )
{
if(!halt_list[i].is_bound() || ! halt_list[(i+1)%entry_count].is_bound())
{
current_linkage.convoy->get_average_journey_times()->remove(pair);
continue;
}
else
{
journey_time = current_linkage.convoy->get_average_journey_times()->access(pair)->reduce();
}
}
if(journey_time == 0)
{
// Zero here means that there are no journey time data even if the hashtable entry exists.
// Fallback to convoy's general average speed if a point-to-point average is not available.
const uint32 distance = shortest_distance(halt_list[i]->get_basis_pos(), halt_list[(i+1)%entry_count]->get_basis_pos());
const uint32 journey_time_32 = world->travel_time_tenths_from_distance(distance, current_average_speed);
// TODO: Seriously consider using 32 bits here for all journey time data
journey_time = journey_time_32 > 65534 ? 65534 : journey_time_32;
}
// journey time from halt 0 to halt 1 is stored in journey_time_list[1]
journey_time_list.append(journey_time, 64);
}
journey_time_list[0] = journey_time_list[entry_count]; // copy the last entry to the first entry
journey_time_list.remove_at(entry_count); // remove the last entry
// rebuild connexions for all halts in halt list
// for each origin halt
for (uint8 h = 0; h < entry_count; ++h)
{
if ( recurrence_list[h] )
{
// skip this halt if it has already been processed
continue;
}
accumulated_journey_time = 0;
// use hash tables in connexion list, but not hash tables stored in the halt
catg_connexions = connexion_list[ halt_list[h].get_id() ].connexion_table;
// any serving line/lineless convoy increments serving transport count
++connexion_list[ halt_list[h].get_id() ].serving_transport;
// for each target halt (origin halt is excluded)
for (uint8 i = 1, t = (h + 1) % entry_count;
i < entry_count;
++i, t = (t + 1) % entry_count)
{
// Case : origin halt is encountered again
if ( halt_list[t] == halt_list[h] )
{
// reset and process the next
accumulated_journey_time = 0;
// mark this halt in the recurrence list to avoid duplicated processing
recurrence_list[t] = true;
continue;
}
// Case : suitable halt
accumulated_journey_time += journey_time_list[t];
// Check the journey times to the connexion
new_connexion = new haltestelle_t::connexion;
new_connexion->waiting_time = halt_list[h]->get_average_waiting_time(halt_list[t], catg);
new_connexion->transfer_time = catg != warenbauer_t::passagiere->get_catg_index() ? halt_list[h]->get_transshipment_time() : halt_list[h]->get_transfer_time();
if(current_linkage.line.is_bound())
{
average_tpl<uint16>* ave = current_linkage.line->get_average_journey_times()->access(id_pair(halt_list[h].get_id(), halt_list[t].get_id()));
average_tpl<uint16>* ave_rc = NULL;
if(current_linkage.line->get_average_journey_times_reverse_circular())
{
ave_rc = current_linkage.line->get_average_journey_times_reverse_circular()->access(id_pair(halt_list[h].get_id(), halt_list[t].get_id()));
}
if(ave && ave->count > 0)
{
// Check whether this is a bidirectional circular route.
// If it is, check whether the reverse direction gives a shorter journey time.
if(ave_rc && ave_rc->count > 0)
{
if(ave_rc->reduce() < ave->reduce())
{
ave = ave_rc;
}
}
new_connexion->journey_time = ave->reduce();
}
else
{
// Fallback - use the old method. This will be an estimate, and a somewhat generous one at that.
new_connexion->journey_time = accumulated_journey_time;
}
}
else if(current_linkage.convoy.is_bound())
{
average_tpl<uint16>* ave = current_linkage.convoy->get_average_journey_times()->access(id_pair(halt_list[h].get_id(), halt_list[t].get_id()));
if(ave && ave->count > 0)
{
new_connexion->journey_time = ave->reduce();
}
else
{
// Fallback - use the old method. This will be an estimate, and a somewhat generous one at that.
new_connexion->journey_time = accumulated_journey_time;
}
}
new_connexion->best_convoy = current_linkage.convoy;
new_connexion->best_line = current_linkage.line;
new_connexion->alternative_seats = 0;
// Check whether this is the best connexion so far, and, if so, add it.
if( !catg_connexions->put(halt_list[t], new_connexion) )
{
// The key exists in the hashtable already - check whether this entry is better.
haltestelle_t::connexion* existing_connexion = catg_connexions->get(halt_list[t]);
if( existing_connexion->journey_time > new_connexion->journey_time )
{
// The new connexion is better - replace it.
new_connexion->alternative_seats = existing_connexion->alternative_seats;
delete existing_connexion;
catg_connexions->set(halt_list[t], new_connexion);
}
else
{
delete new_connexion;
}
}
else
{
halt_list[h]->prepare_goods_list(catg);
}
}
}
++phase_counter;
// iteration control
++iterations;
++total_iterations;
if ( use_limits && iterations == limit_rebuild_connexions)
{
break;
}
}
diff = dr_time() - start; // stop timing
// iteration statistics collection
if ( catg == representative_category )
{
statistic_duration += ( diff ? diff : 1 );
statistic_iteration += iterations;
}
#ifdef DEBUG_COMPARTMENT_STEP
printf("\t\t\tRebuilding connexions takes : %lu ms \n", diff);
#endif
// check if this phase is finished
if (phase_counter == linkages->get_count())
{
// iteration limit adjustment
if ( catg == representative_category )
{
const uint32 projected_iterations = statistic_iteration * time_midpoint / statistic_duration;
if ( projected_iterations > 0 )
{
if ( umgebung_t::networkmode )
{
const uint32 percentage = projected_iterations * 100 / local_rebuild_connexions;
if ( percentage < percent_lower_limit || percentage > percent_upper_limit )
{
local_rebuild_connexions = projected_iterations;
local_limits_changed = true;
}
}
else
{
const uint32 percentage = projected_iterations * 100 / limit_rebuild_connexions;
if ( percentage < percent_lower_limit || percentage > percent_upper_limit )
{
limit_rebuild_connexions = projected_iterations;
}
}
}
}
// reset statistic variables
statistic_duration = 0;
statistic_iteration = 0;
// delete immediately after use
if (linkages)
{
delete linkages;
linkages = NULL;
}
current_phase = phase_filter_eligible; // proceed to the next phase
phase_counter = 0; // reset counter
}