forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimconvoi.cc
2935 lines (2511 loc) · 75 KB
/
simconvoi.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
/**
* convoi_t Klasse für Fahrzeugverbände
* von Hansjörg Malthaner
*/
#include <math.h>
#include <stdlib.h>
#include "simdebug.h"
#include "simworld.h"
#include "simware.h"
#include "player/simplay.h"
#include "simconvoi.h"
#include "simhalt.h"
#include "simdepot.h"
#include "simwin.h"
#include "simmenu.h"
#include "simcolor.h"
#include "simmesg.h"
#include "simintr.h"
#include "simlinemgmt.h"
#include "simline.h"
#include "freight_list_sorter.h"
#include "gui/karte.h"
#include "gui/convoi_info_t.h"
#include "gui/fahrplan_gui.h"
#include "gui/messagebox.h"
#include "boden/grund.h"
#include "boden/wege/schiene.h" // for railblocks
#include "besch/vehikel_besch.h"
#include "besch/roadsign_besch.h"
#include "dataobj/fahrplan.h"
#include "dataobj/route.h"
#include "dataobj/loadsave.h"
#include "dataobj/translator.h"
#include "dataobj/umgebung.h"
#include "dings/crossing.h"
#include "dings/roadsign.h"
#include "vehicle/simvehikel.h"
#include "vehicle/overtaker.h"
#include "utils/simstring.h"
#include "utils/cbuffer_t.h"
/*
* Waiting time for loading (ms)
* @author Hj- Malthaner
*/
#define WTT_LOADING 2000
/*
* Waiting time for infinite loading (ms)
* @author Hj- Malthaner
*/
#define WAIT_INFINITE 0xFFFFFFFFu
karte_t *convoi_t::welt = NULL;
/*
* Debugging helper - translate state value to human readable name
* @author Hj- Malthaner
*/
static const char * state_names[convoi_t::MAX_STATES] =
{
"INITIAL",
"FAHRPLANEINGABE",
"ROUTING_1",
"",
"",
"NO_ROUTE",
"DRIVING",
"LOADING",
"WAITING_FOR_CLEARANCE",
"WAITING_FOR_CLEARANCE_ONE_MONTH",
"CAN_START",
"CAN_START_ONE_MONTH",
"SELF_DESTRUCT", // self destruct
"WAITING_FOR_CLEARANCE_TWO_MONTHS",
"CAN_START_TWO_MONTHS",
"LEAVING_DEPOT",
"ENTERING_DEPOT"
};
/**
* Calculates speed of slowest vehicle in the given array
* @author Hj. Matthaner
*/
static int calc_min_top_speed(const array_tpl<vehikel_t*>& fahr, uint8 anz_vehikel)
{
int min_top_speed = 9999999;
for(uint8 i=0; i<anz_vehikel; i++) {
min_top_speed = min(min_top_speed, kmh_to_speed( fahr[i]->get_besch()->get_geschw() ) );
}
return min_top_speed;
}
void convoi_t::init(karte_t *wl, spieler_t *sp)
{
welt = wl;
besitzer_p = sp;
is_electric = false;
sum_gesamtgewicht = sum_gewicht = sum_gear_und_leistung = sum_leistung = 0;
previous_delta_v = 0;
min_top_speed = 9999999;
fpl = NULL;
line = linehandle_t();
line_id = INVALID_LINE_ID;
anz_vehikel = 0;
steps_driven = -1;
withdraw = false;
has_obsolete = false;
no_load = false;
wait_lock = 0;
go_on_ticks = WAIT_INFINITE;
jahresgewinn = 0;
total_distance_traveled = 0;
alte_richtung = ribi_t::keine;
next_wolke = 0;
state = INITIAL;
*name_and_id = 0;
name_offset = 0;
freight_info_resort = true;
freight_info_order = 0;
loading_level = 0;
loading_limit = 0;
max_record_speed = 0;
akt_speed_soll = 0; // Sollgeschwindigkeit
akt_speed = 0; // momentane Geschwindigkeit
sp_soll = 0;
next_stop_index = 65535;
line_update_pending = linehandle_t();
home_depot = koord3d::invalid;
last_stop_pos = koord3d::invalid;
recalc_data = true;
}
convoi_t::convoi_t(karte_t* wl, loadsave_t* file) : fahr(max_vehicle, NULL)
{
self = convoihandle_t(this);
init(wl, 0);
rdwr(file);
}
convoi_t::convoi_t(spieler_t* sp) : fahr(max_vehicle, NULL)
{
self = convoihandle_t(this);
init(sp->get_welt(), sp);
set_name( "Unnamed" );
welt->add_convoi( self );
init_financial_history();
}
convoi_t::~convoi_t()
{
assert(self.is_bound());
assert(anz_vehikel==0);
// close windows
destroy_win( magic_convoi_info+self.get_id() );
destroy_win( magic_convoi_detail+self.get_id() );
DBG_MESSAGE("convoi_t::~convoi_t()", "destroying %d, %p", self.get_id(), this);
// stop following
if(welt->get_follow_convoi()==self) {
welt->set_follow_convoi( convoihandle_t() );
}
welt->sync_remove( this );
welt->rem_convoi( self );
// force asynchronous recalculation
if(fpl) {
if(!fpl->ist_abgeschlossen()) {
destroy_win((long)fpl);
}
if (!fpl->empty() && !line.is_bound()) {
welt->set_schedule_counter();
}
delete fpl;
}
// @author hsiegeln - deregister from line (again) ...
unset_line();
self.detach();
}
uint32 convoi_t::move_to(karte_t const& welt, koord3d const& k, uint16 const start_index)
{
uint32 train_length = 0;
for (unsigned i = 0; i != anz_vehikel; ++i) {
vehikel_t& v = *fahr[i];
steps_driven = -1;
if (grund_t const* const gr = welt.lookup(v.get_pos())) {
v.mark_image_dirty(v.get_bild(), v.get_hoff());
v.verlasse_feld();
// maybe unreserve this
if (schiene_t* const rails = dynamic_cast<schiene_t*>(gr->get_weg(v.get_waytype()))) {
rails->unreserve(&v);
}
}
/* Set pos_prev to the starting point this way. Otherwise it may be
* elsewhere, especially on curves and with already broken convois. */
v.set_pos(k);
v.neue_fahrt(start_index, true);
if (welt.lookup(v.get_pos())) {
v.set_pos(k);
v.betrete_feld();
}
if (i != anz_vehikel - 1U) train_length += v.get_besch()->get_length();
}
return train_length;
}
void convoi_t::laden_abschliessen()
{
if(fpl==NULL) {
if( state!=INITIAL ) {
grund_t *gr = welt->lookup(home_depot);
if(gr && gr->get_depot()) {
dbg->warning( "convoi_t::laden_abschliessen()","No schedule during loading convoi %i: State will be initial!", self.get_id() );
for( uint8 i=0; i<anz_vehikel; i++ ) {
fahr[i]->get_pos() = home_depot;
}
state = INITIAL;
}
else {
dbg->error( "convoi_t::laden_abschliessen()","No schedule during loading convoi %i: Convoi will be destroyed!", self.get_id() );
for( uint8 i=0; i<anz_vehikel; i++ ) {
fahr[i]->get_pos() = koord3d::invalid;
}
destroy();
}
}
// anyway reassign convoi pointer ...
for( uint8 i=0; i<anz_vehikel; i++ ) {
vehikel_t* v = fahr[i];
v->set_convoi(this);
}
return;
}
bool realing_position = false;
if(anz_vehikel>0) {
DBG_MESSAGE("convoi_t::laden_abschliessen()","state=%s, next_stop_index=%d", state_names[state] );
for( uint8 i=0; i<anz_vehikel; i++ ) {
vehikel_t* v = fahr[i];
v->set_erstes( i==0 );
v->set_letztes( i+1==anz_vehikel );
// this sets the convoi and will renew the block reservation, if needed!
v->set_convoi(this);
// wrong alingmant here => must relocate
if(v->need_realignment()) {
// diagonal => convoi must restart
realing_position |= (v->get_fahrtrichtung()&0x0A)!=0 && (state==DRIVING || is_waiting());
}
}
DBG_MESSAGE("convoi_t::laden_abschliessen()","next_stop_index=%d", next_stop_index );
// lines have been still unknown during loading for old savegames!
if (line_id != INVALID_LINE_ID) {
// if a line is assigned, set line!
linehandle_t new_line = besitzer_p->simlinemgmt.get_line_by_id(line_id);
if( new_line.is_bound() && !fpl->matches( welt, new_line->get_schedule() ) ) {
// 101 version produced broken line ids => we have to find our line the hard way ...
vector_tpl<linehandle_t> lines;
get_besitzer()->simlinemgmt.get_lines(fpl->get_type(), &lines);
new_line = linehandle_t();
for (vector_tpl<linehandle_t>::const_iterator i = lines.begin(), end = lines.end(); i != end; i++) {
linehandle_t l = *i;
if( fpl->matches( welt, l->get_schedule() ) ) {
// if a line is assigned, set line!
new_line = l;
break;
}
}
}
// now the line should match our schedule or else ...
if(new_line.is_bound()) {
line = new_line;
line_id = new_line->get_line_id();
line->add_convoy(self);
DBG_DEBUG("convoi_t::laden_abschliessen()","%s registers for %d", name_and_id, line_id);
}
else {
line_id = INVALID_LINE_ID;
line = linehandle_t();
}
}
}
else {
// no vehicles in this convoi?!?
dbg->error( "convoi_t::laden_abschliessen()","No vehicles in Convoi %i: will be destroyed!", self.get_id() );
destroy();
}
// put convoi agian right on track?
if(realing_position && anz_vehikel>1) {
// display just a warning
dbg->warning("convoi_t::laden_abschliessen()","cnv %i is currently too long.",self.get_id());
// since start may have been changed
uint16 start_index = max(2,fahr[anz_vehikel-1]->get_route_index())-2;
koord3d k0 = fahr[anz_vehikel-1]->get_pos();
uint32 train_length = move_to(*welt, k0, start_index) + 1;
// now advance all convoi until it is completely on the track
fahr[0]->set_erstes(false); // switches off signal checks ...
for(unsigned i=0; i<anz_vehikel; i++) {
vehikel_t* v = fahr[i];
v->darf_rauchen(false);
fahr[i]->fahre_basis( ((TILE_STEPS)*train_length)<<12 );
train_length -= v->get_besch()->get_length();
v->darf_rauchen(true);
// eventually reserve this again
grund_t *gr=welt->lookup(v->get_pos());
// airplanes may have no ground ...
schiene_t *sch0 = dynamic_cast<schiene_t *>( gr->get_weg(fahr[i]->get_waytype()) );
if(sch0) {
sch0->reserve(self,ribi_t::keine);
}
}
fahr[0]->set_erstes(true);
}
}
// since now convoi states go via werkzeug_t
void convoi_t::call_convoi_tool( const char function, const char *extra ) const
{
werkzeug_t *w = create_tool( WKZ_CONVOI_TOOL | SIMPLE_TOOL );
char cmd[3] = { function, ',', 0 };
cbuffer_t param(8192);
param.append( cmd );
param.append( self.get_id() );
if( extra && *extra ) {
param.append( "," );
param.append( extra );
}
w->set_default_param(param);
welt->set_werkzeug( w, get_besitzer() );
// since init always returns false, it is save to delete immediately
delete w;
}
void convoi_t::rotate90( const sint16 y_size )
{
last_stop_pos.rotate90( y_size );
record_pos.rotate90( y_size );
home_depot.rotate90( y_size );
route.rotate90( y_size );
if(fpl) {
fpl->rotate90( y_size );
}
// eventually correct freight destinations (and remove all stale freight)
for( int i=0; i<anz_vehikel; i++ ) {
fahr[i]->remove_stale_freight();
}
}
/**
* Gibt die Position des Convois zurück.
* @return Position des Convois
* @author Hj. Malthaner
*/
koord3d convoi_t::get_pos() const
{
if(anz_vehikel > 0 && fahr[0]) {
return state==INITIAL ? home_depot : fahr[0]->get_pos();
}
else {
return koord3d::invalid;
}
}
/**
* Sets the name. Creates a copy of name.
* @author Hj. Malthaner
*/
void convoi_t::set_name(const char *name)
{
char buf[128];
name_offset = sprintf(buf,"(%i) ",self.get_id() );
tstrncpy(buf+name_offset, translator::translate(name), 116);
tstrncpy(name_and_id, buf, lengthof(name_and_id));
}
// length of convoi (16 is one tile)
uint32 convoi_t::get_length() const
{
uint32 len = 0;
for( uint8 i=0; i<anz_vehikel; i++ ) {
len += fahr[i]->get_besch()->get_length();
}
return len;
}
/**
* convoi add their running cost for traveling one tile
* @author Hj. Malthaner
*/
void convoi_t::add_running_cost()
{
sint64 cost = 0;
for( uint8 i=0; i<anz_vehikel; i++ ) {
cost -= fahr[i]->get_besch()->get_betriebskosten();
}
jahresgewinn += cost;
get_besitzer()->buche(cost, COST_VEHICLE_RUN);
book( cost, CONVOI_OPERATIONS );
book( cost, CONVOI_PROFIT );
total_distance_traveled ++;
book( 1, CONVOI_DISTANCE );
}
/* Calculates (and sets) new akt_speed
* needed for driving, entering and leaving a depot)
*/
void convoi_t::calc_acceleration(long delta_t)
{
// Dwachs: only compute this if a vehicle in the convoi hopped
if (recalc_data) {
sum_friction_weight = 0;
sum_gesamtgewicht = 0;
akt_speed_soll = min_top_speed;
// calculate total friction
for(unsigned i=0; i<anz_vehikel; i++) {
const vehikel_t* v = fahr[i];
int total_vehicle_weight = v->get_gesamtgewicht();
sum_friction_weight += v->get_frictionfactor() * total_vehicle_weight;
sum_gesamtgewicht += total_vehicle_weight;
akt_speed_soll = min(akt_speed_soll, v->get_speed_limit());
}
recalc_data = false;
}
// Prissi: more pleasant and a little more "physical" model *
// try to simulate quadratic friction
if(sum_gesamtgewicht != 0) {
/*
* The parameter consist of two parts (optimized for good looking):
* - every vehicle in a convoi has a the friction of its weight
* - the dynamic friction is calculated that way, that v^2*weight*frictionfactor = 200 kW
* => heavier loaded and faster traveling => less power for acceleration is available!
* since delta_t can have any value, we have to scale the step size by this value.
* however, there is a quadratic friction term => if delta_t is too large the calculation may get weird results
* @author prissi
*/
/* but for integer, we have to use the order below and calculate actually 64*deccel, like the sum_gear_und_leistung
* since akt_speed=10/128 km/h and we want 64*200kW=(100km/h)^2*100t, we must multiply by (128*2)/100
* But since the acceleration was too fast, we just deccelerate 4x more => >>6 instead >>8 */
sint32 deccel = ( ( (akt_speed*sum_friction_weight)>>6 )*(akt_speed>>2) ) / 25 + (sum_gesamtgewicht*64); // this order is needed to prevent overflows!
// prissi:
// integer sucks with planes => using floats ...
sint32 delta_v = (sint32)( ( (double)( (akt_speed>akt_speed_soll?0l:sum_gear_und_leistung) - deccel)*(double)delta_t)/(double)sum_gesamtgewicht);
// we normalize delta_t to 1/64th and check for speed limit */
// sint32 delta_v = ( ( (akt_speed>akt_speed_soll?0l:sum_gear_und_leistung) - deccel) * delta_t)/sum_gesamtgewicht;
// we need more accurate arithmetic, so we store the previous value
delta_v += previous_delta_v;
previous_delta_v = delta_v & 0x0FFF;
// and finally calculate new speed
akt_speed = max(akt_speed_soll>>4, akt_speed+(sint32)(delta_v>>12l) );
}
else {
// very old vehicle ...
akt_speed += 16;
}
// obey speed maximum with additional const brake ...
if(akt_speed > akt_speed_soll) {
akt_speed -= 24;
if(akt_speed > akt_speed_soll+kmh_to_speed(20)) {
akt_speed = akt_speed_soll+kmh_to_speed(20);
}
}
// new record?
if(akt_speed > max_record_speed) {
max_record_speed = akt_speed;
record_pos = fahr[0]->get_pos().get_2d();
}
}
int convoi_t::get_vehicle_at_length(uint16 length)
{
int current_length = 0;
for( int i=0; i<anz_vehikel; i++ ) {
current_length += fahr[i]->get_besch()->get_length();
if(length<current_length) {
return i;
}
}
return anz_vehikel;
}
// moves all vehicles of a convoi
bool convoi_t::sync_step(long delta_t)
{
// still have to wait before next action?
wait_lock -= delta_t;
if(wait_lock <= 0) {
wait_lock = 0;
}
else {
return true;
}
switch(state) {
case INITIAL:
// jemand muß start aufrufen, damit der convoi von INITIAL
// nach ROUTING_1 geht, das kann nicht automatisch gehen
break;
case FAHRPLANEINGABE:
case ROUTING_1:
case DUMMY4:
case DUMMY5:
case NO_ROUTE:
case CAN_START:
case CAN_START_ONE_MONTH:
case CAN_START_TWO_MONTHS:
// Hajo: this is an async task, see step()
break;
case ENTERING_DEPOT:
break;
case LEAVING_DEPOT:
{
// ok, so we will accelerate
akt_speed_soll = max( akt_speed_soll, kmh_to_speed(30) );
calc_acceleration(delta_t);
sp_soll += (akt_speed*delta_t);
// now actually move the units
while(sp_soll>>12) {
uint32 sp_hat = fahr[0]->fahre_basis(1<<12);
int v_nr = get_vehicle_at_length((++steps_driven)>>4);
// stop when depot reached
if(state==INITIAL) {
break;
}
// until all are moving or something went wrong (sp_hat==0)
if(sp_hat==0 || v_nr==anz_vehikel) {
steps_driven = -1;
state = DRIVING;
return true;
}
// now only the right numbers
for(int i=1; i<=v_nr; i++) {
fahr[i]->fahre_basis(sp_hat);
}
sp_soll -= sp_hat;
}
// smoke for the engines
next_wolke += delta_t;
if(next_wolke>500) {
next_wolke = 0;
for(int i=0; i<anz_vehikel; i++ ) {
fahr[i]->rauche();
}
}
}
break; // LEAVING_DEPOT
case DRIVING:
{
calc_acceleration(delta_t);
// now actually move the units
sp_soll += (akt_speed*delta_t);
uint32 sp_hat = fahr[0]->fahre_basis(sp_soll);
// stop when depot reached ...
if(state==INITIAL) {
break;
}
// now move the rest (so all vehikel are moving synchroniously)
for(unsigned i=1; i<anz_vehikel; i++) {
fahr[i]->fahre_basis(sp_hat);
}
// maybe we have been stopped be something => avoid wide jumps
sp_soll = (sp_soll-sp_hat) & 0x0FFF;
// smoke for the engines
next_wolke += delta_t;
if(next_wolke>500) {
next_wolke = 0;
for(int i=0; i<anz_vehikel; i++ ) {
fahr[i]->rauche();
}
}
}
break; // DRIVING
case LOADING:
// Hajo: loading is an async task, see laden()
break;
case WAITING_FOR_CLEARANCE:
case WAITING_FOR_CLEARANCE_ONE_MONTH:
case WAITING_FOR_CLEARANCE_TWO_MONTHS:
// Hajo: waiting is asynchronous => fixed waiting order and route search
break;
case SELF_DESTRUCT:
// see step, since destruction during a screen update ma give stange effects
break;
default:
dbg->fatal("convoi_t::sync_step()", "Wrong state %d!\n", state);
break;
}
return true;
}
/**
* Berechne route von Start- zu Zielkoordinate
* @author Hanjsörg Malthaner
*/
bool convoi_t::drive_to()
{
if( anz_vehikel>0 ) {
koord3d start = fahr[0]->get_pos();
koord3d ziel = fpl->get_current_eintrag().pos;
// avoid stopping midhalt
if( start==ziel ) {
halthandle_t halt = haltestelle_t::get_halt(welt,ziel,get_besitzer());
if( halt.is_bound() && route.is_contained(start) ) {
for( uint32 i=route.index_of(start); i<route.get_count(); i++ ) {
grund_t *gr = welt->lookup(route.position_bei(i));
if( gr && gr->get_halt()==halt ) {
ziel = gr->get_pos();
}
else {
break;
}
}
}
}
if( !fahr[0]->calc_route(start, ziel, speed_to_kmh(min_top_speed), &route) ) {
state = NO_ROUTE;
get_besitzer()->bescheid_vehikel_problem(self,ziel);
// wait 10s before next attempt
wait_lock = 25000;
}
else {
vorfahren();
return true;
}
}
return false;
}
/**
* Ein Fahrzeug hat ein Problem erkannt und erzwingt die
* Berechnung einer neuen Route
* @author Hanjsörg Malthaner
*/
void convoi_t::suche_neue_route()
{
state = ROUTING_1;
wait_lock = 0;
}
/**
* Asynchrne step methode des Convois
* @author Hj. Malthaner
*/
void convoi_t::step()
{
// moved check to here, as this will apply the same update
// logic/constraints convois have for manual schedule manipulation
if (line_update_pending.is_bound()) {
check_pending_updates();
}
if(wait_lock!=0) {
return;
}
switch(state) {
case LOADING:
laden();
break;
case DUMMY4:
case DUMMY5:
break;
case FAHRPLANEINGABE:
// schedule window closed?
if(fpl!=NULL && fpl->ist_abgeschlossen()) {
set_schedule(fpl);
if (fpl->empty()) {
// no entry => no route ...
state = NO_ROUTE;
}
else {
// Schedule changed at station
// this station? then complete loading task else drive on
halthandle_t h = haltestelle_t::get_halt( welt, get_pos(), get_besitzer() );
if( h.is_bound() && h==haltestelle_t::get_halt( welt, fpl->get_current_eintrag().pos, get_besitzer() ) ) {
if (route.get_count() > 0) {
koord3d const& pos = route.back();
if (h == haltestelle_t::get_halt(welt, pos, get_besitzer())) {
state = get_pos() == pos ? LOADING : DRIVING;
break;
}
}
else {
if( drive_to() ) {
state = DRIVING;
break;
}
}
}
if( fpl->get_current_eintrag().pos==get_pos() ) {
// position in depot: waiting
grund_t *gr = welt->lookup(fpl->get_current_eintrag().pos);
if( gr && gr->get_depot() ) {
state = INITIAL;
}
else {
state = ROUTING_1;
}
}
else {
// go to next
state = ROUTING_1;
}
}
}
break;
case ROUTING_1:
{
vehikel_t* v = fahr[0];
if (fpl->empty()) {
state = NO_ROUTE;
}
else {
// check first, if we are already there:
assert( fpl->get_aktuell()<fpl->get_count() );
if( v->get_pos()==fpl->get_current_eintrag().pos ) {
fpl->advance();
}
// Hajo: now calculate a new route
drive_to();
// finally, was there a record last time?
if(max_record_speed>welt->get_record_speed(fahr[0]->get_waytype())) {
welt->notify_record(self, max_record_speed, record_pos);
}
}
}
break;
case NO_ROUTE:
// stucked vehicles
{
vehikel_t* v = fahr[0];
if (fpl->empty()) {
// no entries => no route ...
get_besitzer()->bescheid_vehikel_problem(self, v->get_pos());
}
else {
// Hajo: now calculate a new route
drive_to();
}
}
break;
case CAN_START:
case CAN_START_ONE_MONTH:
case CAN_START_TWO_MONTHS:
{
vehikel_t* v = fahr[0];
int restart_speed=-1;
if (v->ist_weg_frei(restart_speed)) {
// can reserve new block => drive on
state = (steps_driven>=0) ? LEAVING_DEPOT : DRIVING;
if(haltestelle_t::get_halt(welt,v->get_pos(),besitzer_p).is_bound()) {
v->play_sound();
}
}
if(restart_speed>=0) {
akt_speed = restart_speed;
}
if(state==CAN_START || state==CAN_START_ONE_MONTH) {
set_tiles_overtaking( 0 );
}
}
break;
case WAITING_FOR_CLEARANCE_ONE_MONTH:
case WAITING_FOR_CLEARANCE_TWO_MONTHS:
case WAITING_FOR_CLEARANCE:
{
int restart_speed=-1;
if (fahr[0]->ist_weg_frei(restart_speed)) {
state = (steps_driven>=0) ? LEAVING_DEPOT : DRIVING;
}
if(restart_speed>=0) {
akt_speed = restart_speed;
}
if(state!=DRIVING) {
set_tiles_overtaking( 0 );
}
}
break;
// must be here; may otherwise confuse window management
case SELF_DESTRUCT:
welt->set_dirty();
destroy();
return; // must not continue method after deleting this object
default: /* keeps compiler silent*/
break;
}
// calculate new waiting time
switch( state ) {
// handled by routine
case LOADING:
break;
// immediate action needed
case SELF_DESTRUCT:
case LEAVING_DEPOT:
case ENTERING_DEPOT:
case DRIVING:
case DUMMY4:
case DUMMY5:
wait_lock = 0;
break;
// just waiting for action here
case INITIAL:
case FAHRPLANEINGABE:
case NO_ROUTE:
wait_lock = 25000;
break;
// action soon needed
case ROUTING_1:
case CAN_START:
case WAITING_FOR_CLEARANCE:
wait_lock = 500;
break;
// waiting for free way, not too heavy, not to slow
case CAN_START_ONE_MONTH:
case WAITING_FOR_CLEARANCE_ONE_MONTH:
case CAN_START_TWO_MONTHS:
case WAITING_FOR_CLEARANCE_TWO_MONTHS:
wait_lock = 2500;
break;
default: ;
}
}
void convoi_t::neues_jahr()
{
jahresgewinn = 0;
}
void convoi_t::new_month()
{
// should not happen: leftover convoi without vehicles ...
if(anz_vehikel==0) {
DBG_DEBUG("convoi_t::new_month()","no vehicles => self destruct!");
self_destruct();
return;
}
// everything normal: update histroy
for (int j = 0; j<MAX_CONVOI_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;
}
// check for traffic jam
if(state==WAITING_FOR_CLEARANCE) {
state = WAITING_FOR_CLEARANCE_ONE_MONTH;
// check, if now free ...
// migh also reset the state!
int restart_speed=-1;
if (fahr[0]->ist_weg_frei(restart_speed)) {
state = DRIVING;
}
if(restart_speed>=0) {
akt_speed = restart_speed;
}
}
else if(state==WAITING_FOR_CLEARANCE_ONE_MONTH) {
get_besitzer()->bescheid_vehikel_problem(self,koord3d::invalid);
state = WAITING_FOR_CLEARANCE_TWO_MONTHS;
}
// check for traffic jam
if(state==CAN_START) {
state = CAN_START_ONE_MONTH;
}
else if(state==CAN_START_ONE_MONTH) {
get_besitzer()->bescheid_vehikel_problem(self,koord3d::invalid);
state = CAN_START_TWO_MONTHS;
}
// check for obsolete vehicles in the convoi
if(!has_obsolete && welt->use_timeline()) {
// convoi has obsolete vehicles?
const int month_now = welt->get_timeline_year_month();
has_obsolete = false;
for(unsigned j=0; j<get_vehikel_anzahl(); j++ ) {
if (fahr[j]->get_besch()->is_retired(month_now)) {
has_obsolete = true;
break;
}
}
}
}