forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimconvoi.cc
2217 lines (1863 loc) · 54 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 "simplay.h"
#include "simconvoi.h"
#include "simvehikel.h"
#include "simhalt.h"
#include "simdepot.h"
#include "simwin.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 "dataobj/fahrplan.h"
#include "dataobj/route.h"
#include "dataobj/loadsave.h"
#include "dataobj/translator.h"
#include "dataobj/umgebung.h"
#include "dings/crossing.h"
#include "utils/tocstring.h"
#include "utils/simstring.h"
#include "utils/cbuffer_t.h"
// zeige debugging info in infofenster wenn definiert
// #define DEBUG 1
/*
* Waiting time for loading (ms)
* @author Hj- Malthaner
*/
#define WTT_LOADING 2000
/*
* Debugging helper - translate state value to human readable name
* @author Hj- Malthaner
*/
static const char * state_names[] =
{
"INITIAL",
"FAHRPLANEINGABE",
"ROUTING_1",
"DUMMY",
"DUMMY",
"ROUTING_2",
"DRIVING",
"LOADING",
"WAITING_FOR_CLEARANCE",
"WAITING_FOR_CLEARANCE_ONE_MONTH",
"CAN_START",
"CAN_START_ONE_MONTH",
"", // self destruct
"WAITING_FOR_CLEARANCE_TWO_MONTHS",
"CAN_START_TWO_MONTHS",
};
/**
* Calculates speed of slowest vehicle in the given array
* @author Hj. Matthaner
*/
static int calc_min_top_speed(const array_tpl<vehikel_t*>& fahr, int anz_vehikel)
{
int min_top_speed = 9999999;
for(int i=0; i<anz_vehikel; i++) {
min_top_speed = min(min_top_speed, kmh_to_speed( fahr[i]->gib_besch()->gib_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;
jahresgewinn = 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 = INVALID_LINE_ID;
home_depot = koord3d::invalid;
last_stop_pos = koord3d::invalid;
}
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);
setze_name( "Unnamed" );
welt->add_convoi( self );
init_financial_history();
}
convoi_t::~convoi_t()
{
assert(self.is_bound());
assert(anz_vehikel==0);
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 );
// @author hsiegeln - deregister from line
unset_line();
self.detach();
// force asynchronous recalculation
if(fpl) {
if(fpl->maxi()>0) {
welt->set_schedule_counter();
}
delete fpl;
}
destroy_win((long)this);
}
void
convoi_t::laden_abschliessen()
{
if(anz_vehikel>0) {
DBG_MESSAGE("convoi_t::laden_abschliessen()","state=%s, next_stop_index=%d", state_names[state] );
for( unsigned i=0; i<anz_vehikel; i++ ) {
vehikel_t* v = fahr[i];
v->setze_erstes(i == 0);
v->setze_letztes(i == anz_vehikel - 1U);
// this sets the convoi and will renew the block reservation, if needed!
v->setze_convoi(this);
}
DBG_MESSAGE("convoi_t::laden_abschliessen()","next_stop_index=%d", next_stop_index );
// lines are still unknown during loading!
if (line_id != INVALID_LINE_ID) {
// if a line is assigned, set line!
register_with_line(line_id);
}
}
else {
// no vehicles in this convoi?!?
destroy();
}
}
/**
* Gibt die Position des Convois zurück.
* @return Position des Convois
* @author Hj. Malthaner
*/
koord3d
convoi_t::gib_pos() const
{
if(anz_vehikel > 0 && fahr[0]) {
return state==INITIAL ? home_depot : fahr[0]->gib_pos();
} else {
return koord3d::invalid;
}
}
/**
* Sets the name. Creates a copy of name.
* @author Hj. Malthaner
*/
void
convoi_t::setze_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));
}
/**
* Vehicles of the convoi add their running cost by using this
* method
* @author Hj. Malthaner
*/
void convoi_t::add_running_cost(sint32 cost)
{
// Fahrtkosten
jahresgewinn += cost;
gib_besitzer()->buche(cost, COST_VEHICLE_RUN);
// hsiegeln
book(cost, CONVOI_OPERATIONS);
book(cost, CONVOI_PROFIT);
}
/* Calculates (and sets) new akt_speed
* needed for driving, entering and leaving a depot)
*/
void convoi_t::calc_acceleration(long delta_t)
{
// Prissi: more pleasant and a little more "physical" model *
int sum_friction_weight = 0;
sum_gesamtgewicht = 0;
// calculate total friction
for(unsigned i=0; i<anz_vehikel; i++) {
const vehikel_t* v = fahr[i];
int total_vehicle_weight = v->gib_gesamtgewicht();
sum_friction_weight += v->gib_frictionfactor() * total_vehicle_weight;
sum_gesamtgewicht += total_vehicle_weight;
}
// 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]->gib_pos().gib_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]->gib_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:
// schedule window closed?
if(fpl!=NULL && fpl->ist_abgeschlossen()) {
setze_fahrplan(fpl);
welt->set_schedule_counter();
if(fpl->maxi()==0) {
// no entry => no route ...
state = ROUTING_2;
wait_lock = 0;
}
else {
// Schedule changed at station
// this station? then complete loading task else drive on
state = (gib_pos() == fpl->eintrag[fpl->aktuell].pos ? LOADING : ROUTING_2);
}
}
break;
case ROUTING_1:
case DUMMY4:
case DUMMY5:
case ROUTING_2:
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(65536<sp_soll) {
sp_soll -= 65536;
int v_nr = get_vehicle_at_length(steps_driven++);
// until all are moving
if(v_nr==anz_vehikel) {
steps_driven = -1;
state = DRIVING;
return true;
}
// now only the right numbers
for(int i=0; i<=v_nr; i++) {
fahr[i]->sync_step();
}
}
// maybe we have been stopped be something => avoid jumps
sp_soll &= (65536-1);
// 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);
while(65536<sp_soll) {
sp_soll -= 65536;
fahr[0]->sync_step();
// state may have change
if(state!=DRIVING) {
return true;
}
// now move the rest (so all vehikel are moving synchroniously)
for(unsigned i=1; i<anz_vehikel; i++) {
fahr[i]->sync_step();
}
}
// maybe we have been stopped be something => avoid jumps
sp_soll &= (65536-1);
// 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
*/
int convoi_t::drive_to(koord3d start, koord3d ziel)
{
INT_CHECK("simconvoi 293");
if (anz_vehikel == 0 || !fahr[0]->calc_route(start, ziel, speed_to_kmh(min_top_speed), &route)) {
gib_besitzer()->bescheid_vehikel_problem(self,ziel);
// wait 10s before next attempt
wait_lock = 10000;
return false;
}
return true;
}
/**
* Berechne route zum nächsten Halt
* @author Hanjsörg Malthaner
*/
void convoi_t::drive_to_next_stop()
{
fahrplan_t *fpl = gib_fahrplan();
assert(fpl != NULL);
if(fpl->aktuell+1 < fpl->maxi()) {
fpl->aktuell ++;
}
else {
fpl->aktuell = 0;
}
}
/**
* 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_2;
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 != INVALID_LINE_ID) {
check_pending_updates();
}
if(wait_lock!=0) {
return;
}
switch(state) {
case LOADING:
laden();
break;
case DUMMY4:
case DUMMY5:
case ROUTING_1:
case ROUTING_2:
{
vehikel_t* v = fahr[0];
if(fpl->maxi()==0) {
// no entries => no route ...
gib_besitzer()->bescheid_vehikel_problem(self, v->gib_pos());
// wait 10s before next attempt
wait_lock = 10000;
}
else {
// check first, if we are already there:
if(fpl->aktuell>=fpl->maxi() || v->gib_pos()==fpl->eintrag[fpl->get_aktuell()].pos) {
drive_to_next_stop();
}
// Hajo: now calculate a new route
drive_to(v->gib_pos(), fpl->eintrag[fpl->get_aktuell()].pos);
if(route.gib_max_n() > 0) {
vorfahren();
}
}
// finally, was there a record last time?
if(max_record_speed>welt->get_record_speed(fahr[0]->gib_waytype())) {
welt->notify_record(self, max_record_speed, record_pos);
}
}
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::gib_halt(welt,v->gib_pos()).is_bound()) {
v->play_sound();
}
}
if(restart_speed>=0) {
akt_speed = restart_speed;
}
// wait a little before next try
if(state==CAN_START || state==CAN_START_ONE_MONTH) {
wait_lock = 1000;
}
else if(state==CAN_START_TWO_MONTHS) {
wait_lock = 2500;
}
}
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;
}
// wait a little before next try
if(state==WAITING_FOR_CLEARANCE_ONE_MONTH || state==WAITING_FOR_CLEARANCE_TWO_MONTHS) {
wait_lock = 2500;
}
}
break;
// must be here; may otherwise confuse window management
case SELF_DESTRUCT:
besitzer_p->buche( calc_restwert(), gib_pos().gib_2d(), COST_NEW_VEHICLE );
welt->setze_dirty();
destroy();
break;
default: /* keeps compiler silent*/
break;
}
}
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) {
gib_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) {
gib_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<gib_vehikel_anzahl(); j++ ) {
if (fahr[j]->gib_besch()->is_retired(month_now)) {
has_obsolete = true;
break;
}
}
}
}
void
convoi_t::betrete_depot(depot_t *dep)
{
// Hajo: remove vehicles from world data structure
for(unsigned i=0; i<anz_vehikel; i++) {
vehikel_t* v = fahr[i];
grund_t* gr = welt->lookup(v->gib_pos());
if(gr) {
// remove from blockstrecke
v->setze_letztes(true);
v->verlasse_feld();
}
}
dep->convoi_arrived(self, self->gib_fahrplan()!=0);
destroy_win((long)this);
// Hajo: since 0.81.5exp it's safe to
// remove the current sync object from
// the sync list from inside sync_step()
welt->sync_remove(this);
state = INITIAL;
}
void convoi_t::start()
{
if(state == INITIAL || state == ROUTING_1) {
// set home depot to location of depot convoi is leaving
if(route.gib_max_n()>0) {
home_depot = route.position_bei(0);
fahr[0]->setze_pos( home_depot );
}
else {
home_depot = fahr[0]->gib_pos();
}
alte_richtung = ribi_t::keine;
state = ROUTING_2;
calc_loading();
DBG_MESSAGE("convoi_t::start()","Convoi %s wechselt von INITIAL nach ROUTING_1", name_and_id);
} else {
dbg->warning("convoi_t::start()","called with state=%s\n",state_names[state]);
}
}
/* called, when at a destination
* can be waypoint, depot or a stop
* called from the first vehikel_t of a convoi */
void convoi_t::ziel_erreicht()
{
const vehikel_t* v = fahr[0];
alte_richtung = v->gib_fahrtrichtung();
// check, what is at destination!
const grund_t *gr = welt->lookup(v->gib_pos());
depot_t *dp = gr->gib_depot();
if(dp) {
// ok, we are entering a depot
char buf[128];
// we still book the money for the trip; however, the frieght will be lost
calc_gewinn();
akt_speed = 0;
sprintf(buf, translator::translate("!1_DEPOT_REACHED"), gib_name());
message_t::get_instance()->add_message(buf, v->gib_pos().gib_2d(),message_t::convoi, gib_besitzer()->get_player_nr(), IMG_LEER);
betrete_depot(dp);
}
else {
// no depot reached, check for stop!
halthandle_t halt = gr->ist_wasser() ? haltestelle_t::gib_halt(welt, v->gib_pos()) : gr->gib_halt();
if( halt.is_bound() && gr->gib_weg_ribi(v->gib_waytype())!=0 ) {
// seems to be a stop, so book the money for the trip
akt_speed = 0;
halt->book(1, HALT_CONVOIS_ARRIVED);
state = LOADING;
}
else {
// Neither depot nor station: waypoint
drive_to_next_stop();
state = ROUTING_2;
}
}
wait_lock = 0;
}
/**
* Wartet bis Fahrzeug 0 freie Fahrt meldet
* @author Hj. Malthaner
*/
void convoi_t::warten_bis_weg_frei(int restart_speed)
{
if(!is_waiting()) {
state = WAITING_FOR_CLEARANCE;
wait_lock = 0;
}
if(restart_speed>=0) {
// langsam anfahren
akt_speed = restart_speed;
}
}
bool
convoi_t::add_vehikel(vehikel_t *v, bool infront)
{
DBG_MESSAGE("convoi_t::add_vehikel()","at pos %i of %i totals.",anz_vehikel,max_vehicle);
// extend array if requested (only needed for trains)
if(anz_vehikel == max_vehicle) {
DBG_MESSAGE("convoi_t::add_vehikel()","extend array_tpl to %i totals.",max_rail_vehicle);
fahr.resize(max_rail_vehicle, NULL);
}
// now append
if (anz_vehikel < fahr.get_size()) {
v->setze_convoi(this);
if(infront) {
for(unsigned i = anz_vehikel; i > 0; i--) {
fahr[i] = fahr[i - 1];
}
fahr[0] = v;
} else {
fahr[anz_vehikel] = v;
}
anz_vehikel ++;
const vehikel_besch_t *info = v->gib_besch();
if(info->gib_leistung()) {
is_electric |= info->get_engine_type()==vehikel_besch_t::electric;
}
sum_leistung += info->gib_leistung();
sum_gear_und_leistung += info->gib_leistung()*info->get_gear();
sum_gewicht += info->gib_gewicht();
min_top_speed = min( min_top_speed, kmh_to_speed( v->gib_besch()->gib_geschw() ) );
sum_gesamtgewicht = sum_gewicht;
calc_loading();
freight_info_resort = true;
// check for obsolete
if(!has_obsolete && welt->use_timeline()) {
has_obsolete = v->gib_besch()->is_retired( welt->get_timeline_year_month() );
}
}
else {
return false;
}
// der convoi hat jetzt ein neues ende
setze_erstes_letztes();
DBG_MESSAGE("convoi_t::add_vehikel()","now %i of %i total vehikels.",anz_vehikel,max_vehicle);
return true;
}
vehikel_t *
convoi_t::remove_vehikel_bei(uint16 i)
{
vehikel_t *v = NULL;
if(i<anz_vehikel) {
v = fahr[i];
if(v != NULL) {
for(unsigned j=i; j<anz_vehikel-1u; j++) {
fahr[j] = fahr[j + 1];
}
v->setze_convoi(NULL);
--anz_vehikel;
fahr[anz_vehikel] = NULL;
const vehikel_besch_t *info = v->gib_besch();
sum_leistung -= info->gib_leistung();
sum_gear_und_leistung -= info->gib_leistung()*info->get_gear();
sum_gewicht -= info->gib_gewicht();
}
sum_gesamtgewicht = sum_gewicht;
calc_loading();
freight_info_resort = true;
// der convoi hat jetzt ein neues ende
if(anz_vehikel > 0) {
setze_erstes_letztes();
}
// Hajo: calculate new minimum top speed
min_top_speed = calc_min_top_speed(fahr, anz_vehikel);
// check for obsolete
if(has_obsolete) {
has_obsolete = false;
const int month_now = welt->get_timeline_year_month();
for(unsigned i=0; i<anz_vehikel; i++) {
has_obsolete |= fahr[i]->gib_besch()->is_retired(month_now);
}
}
// still requires electrifications?
if(is_electric) {
is_electric = false;
for(unsigned i=0; i<anz_vehikel; i++) {
if(fahr[i]->gib_besch()->gib_leistung()) {
is_electric |= fahr[i]->gib_besch()->get_engine_type()==vehikel_besch_t::electric;
}
}
}
}
return v;
}
void
convoi_t::setze_erstes_letztes()
{
// anz_vehikel muss korrekt init sein
if(anz_vehikel>0) {
fahr[0]->setze_erstes(true);
for(unsigned i=1; i<anz_vehikel; i++) {
fahr[i]->setze_erstes(false);
fahr[i - 1]->setze_letztes(false);
}
fahr[anz_vehikel - 1]->setze_letztes(true);
}
else {
dbg->warning("convoi_t::setze_erstes_letzes()", "called with anz_vehikel==0!");
}
}
bool convoi_t::setze_fahrplan(fahrplan_t * f)
{
enum states old_state = state;
state = INITIAL; // because during a sync-step we might be called twice ...
DBG_DEBUG("convoi_t::setze_fahrplan()", "new=%p, old=%p", f, fpl);
if(f == NULL) {
return false;
}
// happens to be identical?
if(fpl!=f) {
// delete, if not equal
if(fpl) {
delete fpl;
}
}
fpl = NULL;
// rebuild destination for the new schedule
fpl = f;
// asynchronous recalculation of routes
welt->set_schedule_counter();
// remove wrong freight
for(unsigned i=0; i<anz_vehikel; i++) {
fahr[i]->remove_stale_freight();
}
// ok, now we have a schedule
if(old_state!=INITIAL) {
state = FAHRPLANEINGABE;
}
return true;
}