forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simconvoi.cc
2017 lines (1698 loc) · 49.8 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
/**
* simconvoi.cc
*
* convoi_t Klasse für Fahrzeugverbände
* von Hansjörg Malthaner
*/
#include <math.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include <malloc.h> // for alloca
#endif
#include "simdebug.h"
#include "simworld.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/loadsave.h"
#include "dataobj/translator.h"
#include "dataobj/umgebung.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
};
/**
* Calculates speed of slowest vehicle in the given array
* @author Hj. Matthaner
*/
static int calc_min_top_speed(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, fahr->at(i)->gib_speed());
}
return min_top_speed;
}
/**
* Initialize all variables with default values.
* Each constructor must call this method first!
* @author Hj. Malthaner
*/
void convoi_t::init(karte_t *wl, spieler_t *sp)
{
welt = wl;
besitzer_p = sp;
sum_gesamtgewicht = sum_gewicht = sum_gear_und_leistung = sum_leistung = 0;
previous_delta_v = 0;
min_top_speed = 9999999;
fahr = new array_tpl<vehikel_t *> (max_vehicle);
old_fpl = fpl = NULL;
line = linehandle_t();
line_id = UNVALID_LINE_ID;
for(unsigned i=0; i<fahr->get_size(); i++) {
fahr->at(i) = NULL;
}
convoi_info = NULL;
anz_vehikel = 0;
anz_ready = 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;
akt_speed_soll = 0; // Sollgeschwindigkeit
akt_speed = 0; // momentane Geschwindigkeit
sp_soll = 0;
next_stop_index = 65535;
line_update_pending = UNVALID_LINE_ID;
home_depot = koord3d(0,0,0);
last_stop_pos = koord3d(0,0,0);
}
convoi_t::convoi_t(karte_t *wl, loadsave_t *file)
{
self = convoihandle_t(this);
init(wl, 0);
rdwr(file);
}
convoi_t::convoi_t(karte_t *wl, spieler_t *sp)
{
self = convoihandle_t(this);
init(wl, sp);
setze_name( "Unnamed" );
welt->add_convoi( self );
init_financial_history();
}
convoi_t::~convoi_t()
{
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() );
}
if(convoi_info) {
destroy_win(convoi_info);
delete convoi_info;
}
convoi_info = 0;
// @author hsiegeln - deregister from line
unset_line();
welt->sync_remove( this );
welt->rem_convoi( self );
for(int i=anz_vehikel-1; i>=0; i--) {
delete fahr->at(i);
}
delete fahr;
// force asynchronous recalculation
welt->set_schedule_counter();
// Hajo: finally clean up
self.detach();
fahr = 0;
fpl = 0;
}
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++ ) {
fahr->at(i)->setze_erstes( i==0 );
fahr->at(i)->setze_letztes( i==(anz_vehikel-1u) );
// this sets the convoi and will renew the block reservation, if needed!
fahr->at(i)->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!=UNVALID_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->get(0)) {
return fahr->get(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,128);
}
/**
* wird vom ersten Fahrzeug des Convois aufgerufen, um Aenderungen
* der Grundgeschwindigkeit zu melden. Berechnet (Brems-) Beschleunigung
* @author Hj. Malthaner
*/
void
convoi_t::setze_akt_speed_soll(int as)
{
// first set speed limit
akt_speed_soll=min(as,min_top_speed);
}
/**
* 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);
}
/**
* Vorbereitungsmethode für Echtzeitfunktionen eines Objekts.
* @author Hj. Malthaner
*/
void
convoi_t::sync_prepare()
{
if(wait_lock > 0) {
// Hajo: got a saved game that had wait_lock set to 345891
// I have no idea how that can happen, but we can work
// around such problems here. -> wait at most 1 minute
if(wait_lock > 60000) {
dbg->warning("convoi_t::sync_prepre()",
"Convoi %d: wait lock out of bounds:"
"wait_lock = %d, setting to 60000",
self.get_id(), wait_lock);
wait_lock = 60000;
}
} else {
wait_lock = 0;
}
}
/**
* Methode für Echtzeitfunktionen eines Objekts.
* @author Hj. Malthaner
*/
bool
convoi_t::sync_step(long delta_t)
{
// DBG_MESSAGE("convoi_t::sync_step()", "%p, state %d", this, state);
// moved check to here, as this will apply the same update
// logic/constraints convois have for manual schedule manipulation
if (line_update_pending != UNVALID_LINE_ID) {
// if ((state != CAN_START) && (state != CAN_START_ONE_MONTH)) {
check_pending_updates();
// }
}
wait_lock -= delta_t;
if(wait_lock <= 0) {
wait_lock = 0;
// step ++;
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:
if((fpl != NULL) && (fpl->ist_abgeschlossen())) {
setze_fahrplan(fpl);
welt->set_schedule_counter();
if(fpl->maxi()==0) {
// no entry => no route ...
state = ROUTING_2;
}
else {
// V.Meyer: If we are at destination - complete loading task!
state = gib_pos() == fpl->eintrag.get(fpl->aktuell).pos ? LOADING : ROUTING_1;
calc_loading();
}
}
break;
case ROUTING_1:
wait_lock += WTT_LOADING;
state = ROUTING_2;
// DBG_MESSAGE("convoi_t::sync_step()","Convoi wechselt von ROUTING_1 nach ROUTING_2\n");
break;
case DUMMY4:
case DUMMY5:
case ROUTING_2:
case CAN_START:
case CAN_START_ONE_MONTH:
// Hajo: this is an async task, see step()
break;
case DRIVING:
// teste, ob wir neu ansetzen müssen ?
if(!anz_ready) {
// 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++) {
int total_vehicle_weight;
total_vehicle_weight = fahr->at(i)->gib_gesamtgewicht();
sum_friction_weight += fahr->at(i)->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
* => the more heavy and the more fast the 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 arithmetik, 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) );
//DBG_MESSAGE("convoi_t::sync_step","accel %d, deccel %d, akt_speed %d, delta_t %d, delta_v %d, previous_delta_v %d",sum_gear_und_leistung,deccel,akt_speed,delta_t,delta_v, previous_delta_v );
}
else {
// very old vehicle ...
akt_speed += 16;
}
// obey speed maximum with additional const brake ...
if(akt_speed > akt_speed_soll) {
//DBG_MESSAGE("convoi_t::sync_step","akt_speed=%d, akt_speed_soll=%d",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);
//DBG_MESSAGE("convoi_t::sync_step","akt_speed=%d, akt_speed_soll=%d",akt_speed,akt_speed_soll);
}
}
// now actually move the units
sp_soll += (akt_speed*delta_t) / 64;
while(1024 < sp_soll && !anz_ready) {
sp_soll -= 1024;
fahr->at(0)->sync_step();
// stopped by something!
if(state!=DRIVING) {
sp_soll &= 1023;
return true;
}
for(unsigned i=1; i<anz_vehikel; i++) {
fahr->at(i)->sync_step();
}
}
// smoke for the engines
next_wolke += delta_t;
if(next_wolke>500) {
next_wolke = 0;
for(int i=0; i<anz_vehikel; i++ ) {
fahr->at(i)->rauche();
}
}
} // end if(anz_ready==0)
else {
// Ziel erreicht
alte_richtung = fahr->at(0)->gib_fahrtrichtung();
// pruefen ob wir ein depot erreicht haben
const grund_t *gr = welt->lookup(fahr->at(0)->gib_pos());
depot_t * dp = gr->gib_depot();
if(dp) {
// ok, we are entering a depot
char buf[128];
// Gewinn für transport einstreichen
calc_gewinn(false);
akt_speed = 0;
sprintf(buf, translator::translate("!1_DEPOT_REACHED"), gib_name());
message_t::get_instance()->add_message(buf,fahr->at(0)->gib_pos().gib_2d(),message_t::convoi,gib_besitzer()->get_player_nr(),IMG_LEER);
betrete_depot(dp);
// 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;
return true; // Hajo: convoi is still alive
}
else {
// no depot reached, so book values for stop!
const grund_t *gr = welt->lookup(fahr->at(0)->gib_pos());
halthandle_t halt = haltestelle_t::gib_halt(welt, fahr->at(0)->gib_pos());
// we could have reached a non-haltestelle stop, so check before booking!
if(halt.is_bound() && gr->gib_weg_ribi(fahr->at(0)->gib_wegtyp())!=0) {
// Gewinn für transport einstreichen
calc_gewinn(true);
akt_speed = 0;
halt->book(1, HALT_CONVOIS_ARRIVED);
//DBG_MESSAGE("convoi_t::sync_step()","reached station at (%i,%i)",gr->gib_pos().x,gr->gib_pos().y);
state = LOADING;
calc_loading();
}
else {
//DBG_MESSAGE("convoi_t::sync_step()","passed waypoint at (%i,%i)",gr->gib_pos().x,gr->gib_pos().y);
// Neither depot nor station: waypoint
drive_to_next_stop();
state = ROUTING_2;
}
}
}
break;
case LOADING:
// Hajo: loading is an async task, see laden()
break;
case WAITING_FOR_CLEARANCE:
case WAITING_FOR_CLEARANCE_ONE_MONTH:
// Hajo: waiting is asynchronous => fixed waiting order and route search
break;
case SELF_DESTRUCT:
{
for(unsigned f=0; f<anz_vehikel; f++) {
besitzer_p->buche(fahr->at(f)->calc_restwert(), fahr->at(f)->gib_pos().gib_2d(), COST_NEW_VEHICLE);
}
destroy();
}
break;
default:
dbg->fatal("convoi_t::sync_step()", "Wrong state %d!\n", state);
}
}
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->at(0)->calc_route(welt, start, ziel, speed_to_kmh(min_top_speed), &route )) {
gib_besitzer()->bescheid_vehikel_problem(self,ziel);
return false;
}
INT_CHECK("simconvoi 297");
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;
}
/**
* Asynchrne step methode des Convois
* @author Hj. Malthaner
*/
void convoi_t::step()
{
switch(state) {
case LOADING:
laden();
break;
case DUMMY4:
case DUMMY5:
case ROUTING_2:
if(fpl->maxi()==0) {
// no entries => no route ...
gib_besitzer()->bescheid_vehikel_problem(self,fahr->at(0)->gib_pos());
}
else {
// check first, if we are already there:
if(fahr->at(0)->gib_pos()==fpl->eintrag.at(fpl->get_aktuell()).pos) {
drive_to_next_stop();
}
// Hajo: now calculate a new route
drive_to(fahr->at(0)->gib_pos(),fpl->eintrag.at(fpl->get_aktuell()).pos);
if(route.gib_max_n() > 0) {
// Hajo: ROUTING_3 is no more, go to CAN_START directly
vorfahren();
state = CAN_START;
}
}
break;
case CAN_START:
case CAN_START_ONE_MONTH:
if((self.get_id()&1)==(welt->gib_steps()&1)) {
int restart_speed=-1;
if(fahr->at(0)->ist_weg_frei(restart_speed)) {
// can reserve new block => drive on
state = DRIVING;
fahr->at(0)->play_sound();
}
if(restart_speed>=0) {
akt_speed = restart_speed;
}
}
break;
case WAITING_FOR_CLEARANCE:
case WAITING_FOR_CLEARANCE_ONE_MONTH:
// only every fourth step after the first try ...
if(akt_speed>=0 || (self.get_id()&3)==(welt->gib_steps()&3)) {
int restart_speed=-1;
if(fahr->at(0)->ist_weg_frei(restart_speed)) {
state = DRIVING;
}
if(restart_speed>=0) {
akt_speed = restart_speed;
}
}
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;
}
else if(state==WAITING_FOR_CLEARANCE_ONE_MONTH) {
gib_besitzer()->bescheid_vehikel_problem(self,koord3d::invalid);
}
// 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);
}
// 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->at(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++) {
grund_t *gr=welt->lookup(fahr->at(i)->gib_pos());
if(gr) {
// remove from blockstrecke
gr->obj_remove(fahr->at(i),gib_besitzer());
if(fahr->at(i)->gib_wegtyp()==weg_t::schiene || fahr->at(i)->gib_wegtyp()==weg_t::monorail) {
schiene_t *sch=(schiene_t *)gr->gib_weg(fahr->at(i)->gib_wegtyp());
sch->unreserve(self);
}
}
}
dep->convoi_arrived(self, true);
if(convoi_info) {
// V.Meyer: destroy convoi info when entering the depot
destroy_win(convoi_info);
delete convoi_info;
convoi_info = NULL;
}
}
void
convoi_t::start()
{
if(state == INITIAL || state == ROUTING_1) {
// set home depot to location of depot convoi is leaving
set_home_depot(gib_pos());
/*
for(unsigned i=0; i<anz_vehikel; i++) {
grund_t * gr = welt->lookup(fahr->at(i)->gib_pos());
if(!gr->obj_ist_da(fahr->at(i))) {
fahr->at(i)->betrete_feld();
}
}
*/
alte_richtung = ribi_t::keine;
state = ROUTING_1;
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 the first vehicle reaches the target
* @author Hj. Malthaner
*/
void convoi_t::ziel_erreicht(vehikel_t *)
{
wait_lock += 8*50;
anz_ready |= true;
}
/**
* 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;
}
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);
array_tpl <vehikel_t *> *f = new array_tpl<vehikel_t *> (max_rail_vehicle);
for(unsigned i=0; i<max_vehicle; i++) {
f->at(i) = fahr->at(i);
}
for(unsigned j=max_vehicle; j<max_rail_vehicle; j++) {
f->at(j) = NULL;
}
delete fahr;
fahr = f;
}
// now append
if(anz_vehikel < fahr->get_size()) {
v->setze_convoi(this);
if(infront) {
for(unsigned i = anz_vehikel; i > 0; i--) {
fahr->at(i) = fahr->at(i - 1);
}
fahr->at(0) = v;
} else {
fahr->at(anz_vehikel) = v;
}
anz_vehikel ++;
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();
min_top_speed = min(min_top_speed, v->gib_speed());
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->at(i);
if(v != NULL) {
for(unsigned j=i; j<anz_vehikel-1u; j++) {
fahr->at(j) = fahr->at(j+1);
}
--anz_vehikel;
fahr->at(anz_vehikel) = NULL;
v->setze_convoi(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->at(i)->gib_besch()->is_retired( month_now );
}
}
}
return v;
}
void
convoi_t::setze_erstes_letztes()
{
// anz_vehikel muss korrekt init sein
if(anz_vehikel>0) {
fahr->at(0)->setze_erstes(true);
for(unsigned i=1; i<anz_vehikel; i++) {
fahr->at(i)->setze_erstes(false);
fahr->at(i-1)->setze_letztes(false);
}
fahr->at(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;
}
if(old_fpl!=NULL) {
delete old_fpl;
}
old_fpl = NULL;
// 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->at(i)->remove_stale_freight();
}
// ok, now we have a schedule
if(old_state!=INITIAL) {
state = FAHRPLANEINGABE;
}
return true;
}
fahrplan_t *
convoi_t::erzeuge_fahrplan()
{
if(fpl == NULL) {
if(fahr->at(0) != NULL) {
fpl = fahr->at(0)->erzeuge_neuen_fahrplan();
fpl->eingabe_abschliessen();
}
}
return fpl;
}
/* checks, if we go in the same direction;
* true: convoy prepared
* false: must recalculate position
* on all error we better use the normal starting procedure ...
*/
bool
convoi_t::go_alte_richtung()
{
// invalid route?
if(route.gib_max_n()<=2) {
return false;
}
// going backwards? then recalculate all
sint8 dummy;
ribi_t::ribi neue_richtung_rwr = ribi_t::rueckwaerts( fahr->at(0)->calc_richtung(route.position_bei(0).gib_2d(), route.position_bei(2).gib_2d(), dummy, dummy) );