forked from jamespetts/simutrans-extended
/
simvehicle.cc
9417 lines (8492 loc) · 318 KB
/
simvehicle.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) 1997 - 2001 Hj. Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*
* All moving stuff (vehicle_base_t) and all player vehicle (derived from vehicle_t)
*
* 01.11.99 Moved from simobj.cc
*
* Hansjoerg Malthaner, Nov. 1999
*/
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include "../boden/grund.h"
#include "../boden/wege/runway.h"
#include "../boden/wege/kanal.h"
#include "../boden/wege/schiene.h"
#include "../boden/wege/monorail.h"
#include "../boden/wege/strasse.h"
#include "../bauer/goods_manager.h"
#include "../simworld.h"
#include "../simdebug.h"
#include "../simdepot.h"
#include "../simunits.h"
#include "../player/simplay.h"
#include "../player/finance.h"
#include "../simfab.h"
#include "../simware.h"
#include "../simhalt.h"
#include "../simsound.h"
#include "../simcity.h"
#include "../simsignalbox.h"
#include "../display/simimg.h"
#include "../simmesg.h"
#include "../simcolor.h"
#include "../display/simgraph.h"
#include "../display/viewport.h"
#include "../simline.h"
#include "../simintr.h"
#include "../obj/wolke.h"
#include "../obj/signal.h"
#include "../obj/roadsign.h"
#include "../obj/crossing.h"
#include "../obj/zeiger.h"
#include "../gui/karte.h"
#include "../descriptor/citycar_desc.h"
#include "../descriptor/goods_desc.h"
#include "../descriptor/skin_desc.h"
#include "../descriptor/roadsign_desc.h"
#include "../dataobj/schedule.h"
#include "../dataobj/translator.h"
#include "../dataobj/loadsave.h"
#include "../dataobj/environment.h"
#include "../dataobj/way_constraints.h"
#include "../utils/simstring.h"
#include "../utils/cbuffer_t.h"
#include "../utils/simrandom.h"
#include "../descriptor/goods_desc.h"
#include "../convoy.h"
#include "../bauer/vehikelbauer.h"
#include "simvehicle.h"
#include "simroadtraffic.h"
#include "../path_explorer.h"
#include "../freight_list_sorter.h"
/* get dx and dy from dir (just to remind you)
* any vehicle (including city cars and pedestrians)
* will go this distance per sync step.
* (however, the real dirs are only calculated during display, these are the old ones)
*/
sint8 vehicle_base_t::dxdy[ 8*2 ] = {
-2, 1, // s
-2, -1, // w
-4, 0, // sw
0, 2, //se
2, -1, // n
2, 1, // e
4, 0, // ne
0, -2 // nw
};
// Constants
uint8 vehicle_base_t::old_diagonal_vehicle_steps_per_tile = 128;
uint8 vehicle_base_t::diagonal_vehicle_steps_per_tile = 181;
uint16 vehicle_base_t::diagonal_multiplier = 724;
// set only once, before loading!
void vehicle_base_t::set_diagonal_multiplier( uint32 multiplier, uint32 old_diagonal_multiplier )
{
diagonal_multiplier = (uint16)multiplier;
diagonal_vehicle_steps_per_tile = (uint8)(130560u/diagonal_multiplier) + 1;
if(old_diagonal_multiplier == 0)
{
old_diagonal_vehicle_steps_per_tile = diagonal_vehicle_steps_per_tile;
}
else
{
old_diagonal_vehicle_steps_per_tile = (uint8)(130560u/old_diagonal_multiplier) + 1;
}
}
// if true, convoi, must restart!
bool vehicle_base_t::need_realignment() const
{
return old_diagonal_vehicle_steps_per_tile!=diagonal_vehicle_steps_per_tile && ribi_t::is_bend(direction);
}
// [0]=xoff [1]=yoff
static sint8 driveleft_base_offsets[8][2] =
{
{ 12, 6 },
{ -12, 6 },
{ 0, 6 },
{ 12, 0 },
{ -12, -6 },
{ 12, -6 },
{ 0, -6 },
{ -12, 0 }
// { 12, -12, 0, 12, -12, 12, 0 -12 },
// { 6, 6, 6, 0, -6, -6, -6, 0 }
};
// [0]=xoff [1]=yoff
sint8 vehicle_base_t::overtaking_base_offsets[8][2];
// recalc offsets for overtaking
void vehicle_base_t::set_overtaking_offsets( bool driving_on_the_left )
{
sint8 sign = driving_on_the_left ? -1 : 1;
// a tile has the internal size of
const sint8 XOFF=12;
const sint8 YOFF=6;
overtaking_base_offsets[0][0] = sign * XOFF;
overtaking_base_offsets[1][0] = -sign * XOFF;
overtaking_base_offsets[2][0] = 0;
overtaking_base_offsets[3][0] = sign * XOFF;
overtaking_base_offsets[4][0] = -sign * XOFF;
overtaking_base_offsets[5][0] = sign * XOFF;
overtaking_base_offsets[6][0] = 0;
overtaking_base_offsets[7][0] = sign * (-XOFF-YOFF);
overtaking_base_offsets[0][1] = sign * YOFF;
overtaking_base_offsets[1][1] = sign * YOFF;
overtaking_base_offsets[2][1] = sign * YOFF;
overtaking_base_offsets[3][1] = 0;
overtaking_base_offsets[4][1] = -sign * YOFF;
overtaking_base_offsets[5][1] = -sign * YOFF;
overtaking_base_offsets[6][1] = -sign * YOFF;
overtaking_base_offsets[7][1] = 0;
}
/**
* Checks if this vehicle must change the square upon next move
* THIS IS ONLY THERE FOR LOADING OLD SAVES!
* @author Hj. Malthaner
*/
bool vehicle_base_t::is_about_to_hop( const sint8 neu_xoff, const sint8 neu_yoff ) const
{
const sint8 y_off_2 = 2*neu_yoff;
const sint8 c_plus = y_off_2 + neu_xoff;
const sint8 c_minus = y_off_2 - neu_xoff;
return ! (c_plus < OBJECT_OFFSET_STEPS*2 && c_minus < OBJECT_OFFSET_STEPS*2 && c_plus > -OBJECT_OFFSET_STEPS*2 && c_minus > -OBJECT_OFFSET_STEPS*2);
}
#ifdef INLINE_OBJ_TYPE
vehicle_base_t::vehicle_base_t(typ type):
obj_t(type)
#else
vehicle_base_t::vehicle_base_t():
obj_t()
#endif
{
image = IMG_EMPTY;
set_flag( obj_t::is_vehicle );
steps = 0;
steps_next = VEHICLE_STEPS_PER_TILE - 1;
use_calc_height = true;
drives_on_left = false;
dx = 0;
dy = 0;
zoff_start = zoff_end = 0;
next_lane = 0;
gr = NULL;
weg = NULL;
disp_lane = 2;
}
#ifdef INLINE_OBJ_TYPE
vehicle_base_t::vehicle_base_t(typ type, koord3d pos):
obj_t(type, pos)
#else
vehicle_base_t::vehicle_base_t(koord3d pos):
obj_t(pos)
#endif
{
image = IMG_EMPTY;
set_flag( obj_t::is_vehicle );
pos_next = pos;
steps = 0;
steps_next = VEHICLE_STEPS_PER_TILE - 1;
use_calc_height = true;
drives_on_left = false;
dx = 0;
dy = 0;
zoff_start = zoff_end = 0;
next_lane = 0;
gr = NULL;
weg = NULL;
disp_lane = 2;
}
void vehicle_base_t::rotate90()
{
koord3d pos_cur = get_pos();
pos_cur.rotate90( welt->get_size().y-1 );
set_pos( pos_cur );
// directions are counterclockwise to ribis!
direction = ribi_t::rotate90( direction );
pos_next.rotate90( welt->get_size().y-1 );
// new offsets: very tricky ...
sint8 new_dx = -dy*2;
dy = dx/2;
dx = new_dx;
// new pos + step offsets (only possible, since we know the height!
sint8 neu_yoff = get_xoff()/2;
set_xoff( -get_yoff()*2 );
set_yoff( neu_yoff );
// adjust disp_lane individually
}
void vehicle_base_t::leave_tile()
{
// first: release crossing
// This needs to be refreshed here even though this value is cached:
// otherwise, it can sometimes be incorrect, which could lead to vehicles failing to release crossings.
gr = welt->lookup(get_pos());
weg = NULL;
if( gr && gr->ist_uebergang() ) {
crossing_t *cr = gr->find<crossing_t>(2);
grund_t *gr2 = welt->lookup(pos_next);
if( gr2==NULL || gr2==gr || !gr2->ist_uebergang() || cr->get_logic()!=gr2->find<crossing_t>(2)->get_logic() ) {
cr->release_crossing(this);
}
}
// then remove from ground (or search whole map, if failed)
if(!get_flag(not_on_map) && (gr==NULL || !gr->obj_remove(this)) ) {
// was not removed (not found?)
dbg->error("vehicle_base_t::leave_tile()","'typ %i' %p could not be removed from %d %d", get_typ(), this, get_pos().x, get_pos().y);
DBG_MESSAGE("vehicle_base_t::leave_tile()","checking all plan squares");
// check, whether it is on another height ...
const planquadrat_t *pl = welt->access( get_pos().get_2d() );
if( pl ) {
gr = pl->get_boden_von_obj(this);
if( gr ) {
gr->obj_remove(this);
dbg->warning("vehicle_base_t::leave_tile()","removed vehicle typ %i (%p) from %d %d",get_typ(), this, get_pos().x, get_pos().y);
}
gr = NULL;
return;
}
koord k;
bool ok = false;
for(k.y=0; k.y<welt->get_size().y; k.y++) {
for(k.x=0; k.x<welt->get_size().x; k.x++) {
grund_t *gr = welt->access( k )->get_boden_von_obj(this);
if(gr && gr->obj_remove(this)) {
dbg->warning("vehicle_base_t::leave_tile()","removed vehicle typ %i (%p) from %d %d",get_name(), this, k.x, k.y);
ok = true;
}
}
}
if(!ok) {
dbg->error("vehicle_base_t::leave_tile()","'%s' %p was not found on any map square!",get_name(), this);
}
}
gr = NULL;
}
void vehicle_base_t::enter_tile(grund_t* gr)
{
if(!gr) {
dbg->error("vehicle_base_t::enter_tile()","'%s' new position (%i,%i,%i)!",get_name(), get_pos().x, get_pos().y, get_pos().z );
gr = welt->lookup_kartenboden(get_pos().get_2d());
set_pos( gr->get_pos() );
}
if(!gr->obj_add(this))
{
dbg->error("vehicle_base_t::enter_tile()","'%s failed to be added to the object list", get_name());
}
weg = gr->get_weg(get_waytype());
}
/* THE routine for moving vehicles
* it will drive on as long as it can
* @return the distance actually travelled
*/
uint32 vehicle_base_t::do_drive(uint32 distance)
{
uint32 steps_to_do = distance >> YARDS_PER_VEHICLE_STEP_SHIFT;
if( steps_to_do == 0 ) {
// ok, we will not move in this steps
return 0;
}
// ok, so moving ...
if( !get_flag(obj_t::dirty) ) {
mark_image_dirty( image, 0 );
set_flag( obj_t::dirty );
}
uint32 distance_travelled; // Return value
grund_t *gr = NULL; // if hopped, then this is new position
uint32 steps_target = steps_to_do + (uint32)steps;
if( steps_target > (uint32)steps_next ) {
// We are going far enough to hop.
// We'll be adding steps_next+1 for each hop, as if we
// started at the beginning of this tile, so for an accurate
// count of steps done we must subtract the location we started with.
sint32 steps_done = -steps;
bool has_hopped = false;
koord3d pos_prev;
// Hop as many times as possible.
while( steps_target > steps_next && (gr = hop_check()) ) {
// now do the update for hopping
steps_target -= steps_next+1;
steps_done += steps_next+1;
pos_prev = get_pos();
hop(gr);
use_calc_height = true;
has_hopped = true;
}
if (has_hopped)
{
if (dx == 0)
{
if (dy > 0)
{
set_xoff( pos_prev.x != get_pos().x ? -OBJECT_OFFSET_STEPS : OBJECT_OFFSET_STEPS );
}
else
{
set_xoff( pos_prev.x != get_pos().x ? OBJECT_OFFSET_STEPS : -OBJECT_OFFSET_STEPS );
}
}
else
{
set_xoff( dx < 0 ? OBJECT_OFFSET_STEPS : -OBJECT_OFFSET_STEPS );
}
if (dy == 0)
{
if (dx > 0)
{
set_yoff( pos_prev.y != get_pos().y ? OBJECT_OFFSET_STEPS/2 : -OBJECT_OFFSET_STEPS/2 );
}
else
{
set_yoff( pos_prev.y != get_pos().y ? -OBJECT_OFFSET_STEPS/2 : OBJECT_OFFSET_STEPS/2 );
}
}
else
{
set_yoff( dy < 0 ? OBJECT_OFFSET_STEPS/2 : -OBJECT_OFFSET_STEPS/2 );
}
}
if( steps_next == 0 ) {
// only needed for aircrafts, which can turn on the same tile
// the indicate the turn with this here
steps_next = VEHICLE_STEPS_PER_TILE - 1;
steps_target = VEHICLE_STEPS_PER_TILE - 1;
steps_done -= VEHICLE_STEPS_PER_TILE - 1;
}
// Update internal status, how far we got within the tile.
if( steps_target <= steps_next ) {
steps = steps_target;
}
else {
// could not go as far as we wanted (hop_check failed) => stop at end of tile
steps = steps_next;
}
steps_done += steps;
distance_travelled = steps_done << YARDS_PER_VEHICLE_STEP_SHIFT;
}
else {
// Just travel to target, it's on same tile
steps = steps_target;
distance_travelled = distance & YARDS_VEHICLE_STEP_MASK; // round down to nearest step
}
if(use_calc_height) {
calc_height(gr);
}
// remaining steps
update_bookkeeping(distance_travelled >> YARDS_PER_VEHICLE_STEP_SHIFT );
return distance_travelled;
}
// For reversing: length-8*(paksize/16)
// to make smaller steps than the tile granularity, we have to use this trick
void vehicle_base_t::get_screen_offset( int &xoff, int &yoff, const sint16 raster_width ) const
{
sint32 adjusted_steps = steps;
const vehicle_t* veh = obj_cast<vehicle_t>(this);
if (veh && veh->is_reversed())
{
adjusted_steps += (VEHICLE_STEPS_PER_TILE / 2 - veh->get_desc()->get_length_in_steps());
}
// vehicles needs finer steps to appear smoother
sint32 display_steps = (uint32)adjusted_steps*(uint16)raster_width;
if(dx && dy) {
display_steps &= 0xFFFFFC00;
}
else {
display_steps = (display_steps*diagonal_multiplier)>>10;
}
xoff += (display_steps*dx) >> 10;
yoff += ((display_steps*dy) >> 10) + (get_hoff(raster_width)) / (4 * 16);
if( drives_on_left ) {
const int drive_left_dir = ribi_t::get_dir(get_direction());
xoff += tile_raster_scale_x( driveleft_base_offsets[drive_left_dir][0], raster_width );
yoff += tile_raster_scale_y( driveleft_base_offsets[drive_left_dir][1], raster_width );
}
}
uint16 vehicle_base_t::get_tile_steps(const koord &start, const koord &ende, /*out*/ ribi_t::ribi &direction) const
{
static const ribi_t::ribi ribis[3][3] = {
{ribi_t::northwest, ribi_t::north, ribi_t::northeast},
{ribi_t::west, ribi_t::none, ribi_t::east},
{ribi_t::southwest, ribi_t::south, ribi_t::southeast}
};
const sint8 di = sgn(ende.x - start.x);
const sint8 dj = sgn(ende.y - start.y);
direction = ribis[dj + 1][di + 1];
return di == 0 || dj == 0 ? VEHICLE_STEPS_PER_TILE : diagonal_vehicle_steps_per_tile;
}
// calcs new direction and applies it to the vehicles
ribi_t::ribi vehicle_base_t::calc_set_direction(const koord3d& start, const koord3d& ende)
{
ribi_t::ribi direction = ribi_t::none; //"direction" = direction (Google); "keine" = none (Google)
const sint8 di = ende.x - start.x;
const sint8 dj = ende.y - start.y;
if(dj < 0 && di == 0) {
direction = ribi_t::north;
dx = 2;
dy = -1;
steps_next = VEHICLE_STEPS_PER_TILE - 1;
} else if(dj > 0 && di == 0) {
direction = ribi_t::south;
dx = -2;
dy = 1;
steps_next = VEHICLE_STEPS_PER_TILE - 1;
} else if(di < 0 && dj == 0) {
direction = ribi_t::west;
dx = -2;
dy = -1;
steps_next = VEHICLE_STEPS_PER_TILE - 1;
} else if(di >0 && dj == 0) {
direction = ribi_t::east;
dx = 2;
dy = 1;
steps_next = VEHICLE_STEPS_PER_TILE - 1;
} else if(di > 0 && dj > 0) {
direction = ribi_t::southeast;
dx = 0;
dy = 2;
steps_next = diagonal_vehicle_steps_per_tile - 1;
} else if(di < 0 && dj < 0) {
direction = ribi_t::northwest;
dx = 0;
dy = -2;
steps_next = diagonal_vehicle_steps_per_tile - 1;
} else if(di > 0 && dj < 0) {
direction = ribi_t::northeast;
dx = 4;
dy = 0;
steps_next = diagonal_vehicle_steps_per_tile - 1;
} else {
direction = ribi_t::southwest;
dx = -4;
dy = 0;
steps_next = diagonal_vehicle_steps_per_tile - 1;
}
// we could artificially make diagonals shorter: but this would break existing game behaviour
return direction;
}
// this routine calculates the new height
// beware of bridges, tunnels, slopes, ...
void vehicle_base_t::calc_height(grund_t *gr)
{
use_calc_height = false; // assume, we are only needed after next hop
zoff_start = zoff_end = 0; // assume flat way
if(gr==NULL) {
gr = welt->lookup(get_pos());
}
if(gr==NULL) {
// slope changed below a moving thing?!?
return;
}
else if (gr->ist_tunnel() && gr->ist_karten_boden() && !is_flying()) {
use_calc_height = true; // to avoid errors if underground mode is switched
if( grund_t::underground_mode == grund_t::ugm_none ||
(grund_t::underground_mode == grund_t::ugm_level && gr->get_hoehe() < grund_t::underground_level)
) {
// need hiding? One of the few uses of XOR: not half driven XOR exiting => not hide!
ribi_t::ribi hang_ribi = ribi_type( gr->get_grund_hang() );
if((steps<(steps_next/2)) ^ ((hang_ribi&direction)!=0) ) {
set_image(IMG_EMPTY);
}
else {
calc_image();
}
}
}
else if( !gr->is_visible() ) {
set_image(IMG_EMPTY);
}
else {
// force a valid image above ground, with special handling of tunnel entraces
if (get_image() == IMG_EMPTY) {
if (!gr->ist_tunnel() && gr->ist_karten_boden()) {
calc_image();
}
}
// will not work great with ways, but is very short!
slope_t::type hang = gr->get_weg_hang();
if( hang ) {
const uint slope_height = (hang & 7) ? 1 : 2;
ribi_t::ribi hang_ribi = ribi_type(hang);
if( ribi_t::doubles(hang_ribi) == ribi_t::doubles(direction)) {
zoff_start = hang_ribi & direction ? 2*slope_height : 0; // 0 .. 4
zoff_end = hang_ribi & ribi_t::backward(direction) ? 2*slope_height : 0; // 0 .. 4
}
else {
// only for shadows and movingobjs ...
zoff_start = hang_ribi & direction ? slope_height+1 : 0; // 0 .. 3
zoff_end = hang_ribi & ribi_t::backward(direction) ? slope_height+1 : 0; // 0 .. 3
}
}
else {
zoff_start = (gr->get_weg_yoff() * 2) / TILE_HEIGHT_STEP;
zoff_end = zoff_start;
}
}
}
sint16 vehicle_base_t::get_hoff(const sint16 raster_width) const
{
sint16 h_start = -(sint8)TILE_HEIGHT_STEP * (sint8)zoff_start;
sint16 h_end = -(sint8)TILE_HEIGHT_STEP * (sint8)zoff_end;
return ((h_start*steps + h_end*(256 - steps))*raster_width) >> 9;
}
/* true, if one could pass through this field
* also used for citycars, thus defined here
*/
vehicle_base_t *vehicle_base_t::no_cars_blocking( const grund_t *gr, const convoi_t *cnv, const uint8 current_direction, const uint8 next_direction, const uint8 next_90direction, const private_car_t *pcar, sint8 lane_on_the_tile )
{
bool cnv_overtaking = false; //whether this convoi is on passing lane.
if( cnv ) {
cnv_overtaking = cnv -> is_overtaking();
}
if( pcar ) {
cnv_overtaking = pcar -> is_overtaking();
}
switch (lane_on_the_tile) {
case 1:
cnv_overtaking = true; //treat as convoi is overtaking.
break;
case -1:
cnv_overtaking = false; //treat as convoi is not overtaking.
break;
}
// Search vehicle
for( uint8 pos=1; pos < gr->get_top(); pos++ ) {
if( vehicle_base_t* const v = obj_cast<vehicle_base_t>(gr->obj_bei(pos)) ) {
if( v->get_typ()==obj_t::pedestrian ) {
continue;
}
// check for car
uint8 other_direction=255;
bool other_moving = false;
bool other_overtaking = false; //whether the other convoi is on passing lane.
if( road_vehicle_t const* const at = obj_cast<road_vehicle_t>(v) ) {
// ignore ourself
if( cnv == at->get_convoi() ) {
continue;
}
other_direction = at->get_direction();
other_moving = at->get_convoi()->get_akt_speed() > kmh_to_speed(1);
other_overtaking = at->get_convoi()->is_overtaking();
}
// check for city car
else if( v->get_waytype() == road_wt ) {
other_direction = v->get_direction();
if( private_car_t const* const sa = obj_cast<private_car_t>(v) ){
if( pcar == sa ) {
continue; // ignore ourself
}
other_moving = sa->get_current_speed() > 1;
other_overtaking = sa->is_overtaking();
}
}
// ok, there is another car ...
if( other_direction != 255 ) {
if( next_direction == other_direction && !ribi_t::is_threeway(gr->get_weg_ribi(road_wt)) && cnv_overtaking == other_overtaking ) {
// only consider cars on same lane.
// cars going in the same direction and no crossing => that mean blocking ...
return v;
}
const ribi_t::ribi other_90direction = (gr->get_pos().get_2d() == v->get_pos_next().get_2d()) ? other_direction : calc_direction(gr->get_pos(),v->get_pos_next());
if( other_90direction == next_90direction && cnv_overtaking == other_overtaking ) {
// Want to exit in same as other ~50% of the time
return v;
}
const bool across = next_direction == (drives_on_left ? ribi_t::rotate45l(next_90direction) : ribi_t::rotate45(next_90direction)); // turning across the opposite directions lane
const bool other_across = other_direction == (drives_on_left ? ribi_t::rotate45l(other_90direction) : ribi_t::rotate45(other_90direction)); // other is turning across the opposite directions lane
if( other_direction == next_direction && !(other_across || across) && cnv_overtaking == other_overtaking ) {
// only consider cars on same lane.
// entering same straight waypoint as other ~18%
return v;
}
const bool straight = next_direction == next_90direction; // driving straight
const ribi_t::ribi current_90direction = straight ? ribi_t::backward(next_90direction) : (~(next_direction|ribi_t::backward(next_90direction)))&0x0F;
const bool other_straight = other_direction == other_90direction; // other is driving straight
const bool other_exit_same_side = current_90direction == other_90direction; // other is exiting same side as we're entering
const bool other_exit_opposite_side = ribi_t::backward(current_90direction) == other_90direction; // other is exiting side across from where we're entering
if( across && ((ribi_t::is_perpendicular(current_90direction,other_direction) && other_moving) || (other_across && other_exit_opposite_side) || ((other_across || other_straight) && other_exit_same_side && other_moving) ) ) {
// other turning across in front of us from orth entry dir'n ~4%
return v;
}
const bool headon = ribi_t::backward(current_direction) == other_direction; // we're meeting the other headon
const bool other_exit_across = (drives_on_left ? ribi_t::rotate90l(next_90direction) : ribi_t::rotate90(next_90direction)) == other_90direction; // other is exiting by turning across the opposite directions lane
if( straight && (ribi_t::is_perpendicular(current_90direction,other_direction) || (other_across && other_moving && (other_exit_across || (other_exit_same_side && !headon))) ) ) {
// other turning across in front of us, but allow if other is stopped - duplicating historic behaviour ~2%
return v;
}
else if( other_direction == current_direction && current_90direction == ribi_t::none && cnv_overtaking == other_overtaking ) {
// entering same diagonal waypoint as other ~1%
return v;
}
// else other car is not blocking ~25%
}
}
}
// way is free
return NULL;
}
bool vehicle_base_t::judge_lane_crossing( const uint8 current_direction, const uint8 next_direction, const uint8 other_next_direction, const bool is_overtaking, const bool forced_to_change_lane ) const
{
bool on_left = !(is_overtaking==welt->get_settings().is_drive_left());
// go straight = 0, turn right = -1, turn left = 1.
sint8 this_turn;
if( next_direction == ribi_t::rotate90(current_direction) ) {
this_turn = -1;
}
else if( next_direction == ribi_t::rotate90l(current_direction) ) {
this_turn = 1;
}
else {
// go straight?
this_turn = 0;
}
sint8 other_turn;
if( other_next_direction == ribi_t::rotate90(current_direction) ) {
other_turn = -1;
}
else if( other_next_direction == ribi_t::rotate90l(current_direction) ) {
other_turn = 1;
}
else {
// go straight?
other_turn = 0;
}
if( on_left ) {
if( forced_to_change_lane && other_turn - this_turn >= 0 ) {
return true;
}
else if( other_turn - this_turn > 0 ) {
return true;
}
}
else {
if( forced_to_change_lane && other_turn - this_turn <= 0 ) {
return true;
}
else if( other_turn - this_turn < 0 ) {
return true;
}
}
return false;
}
sint16 vehicle_t::compare_directions(sint16 first_direction, sint16 second_direction) const
{
//Returns difference between two directions in degrees
sint16 difference = 0;
if(first_direction > 360 || second_direction > 360 || first_direction < 0 || second_direction < 0) return 0;
//If directions > 360, this means that they are not supposed to be checked.
if(first_direction > 180 && second_direction < 180)
{
sint16 tmp = first_direction - second_direction;
if(tmp > 180) difference = 360 - tmp;
else difference = tmp;
}
else if(first_direction < 180 && second_direction > 180)
{
sint16 tmp = second_direction - first_direction;
if(tmp > 180) difference = 360 - tmp;
else difference = tmp;
}
else if(first_direction > second_direction)
{
difference = first_direction - second_direction;
}
else
{
difference = second_direction - first_direction;
}
return difference;
}
void vehicle_t::rotate90()
{
vehicle_base_t::rotate90();
previous_direction = ribi_t::rotate90( previous_direction );
last_stop_pos.rotate90( welt->get_size().y-1 );
}
void vehicle_t::rotate90_freight_destinations(const sint16 y_size)
{
// now rotate the freight
for (uint8 i = 0; i < number_of_classes; i++)
{
FOR(slist_tpl<ware_t>, &tmp, fracht[i])
{
tmp.rotate90(y_size);
}
}
}
void vehicle_t::set_convoi(convoi_t *c)
{
/* cnv can have three values:
* NULL: not previously assigned
* 1 (only during loading): convoi wants to reserve the whole route
* other: previous convoi (in this case, currently always c==cnv)
*
* if c is NULL, then the vehicle is removed from the convoi
* (the rail_vehicle_t::set_convoi etc. routines must then remove a
* possibly pending reservation of stops/tracks)
*/
assert( c==NULL || cnv==NULL || cnv==(convoi_t *)1 || c==cnv);
cnv = c;
if(cnv) {
// we need to re-establish the finish flag after loading
if(leading) {
route_t const& r = *cnv->get_route();
check_for_finish = r.empty() || route_index >= r.get_count() || get_pos() == r.at(route_index);
}
if( pos_next != koord3d::invalid ) {
route_t const& r = *cnv->get_route();
if (!r.empty() && route_index < r.get_count() - 1) {
grund_t const* const gr = welt->lookup(pos_next);
if (!gr || !gr->get_weg(get_waytype())) {
if (!(water_wt == get_waytype() && gr && gr->is_water())) { // ships on the open sea are valid
pos_next = r.at(route_index + 1U);
}
}
}
}
// just correct freight destinations
for (uint8 i = 0; i < number_of_classes; i++)
{
FOR(slist_tpl<ware_t>, &c, fracht[i])
{
c.finish_rd(welt);
}
}
}
}
/**
* Unload freight to halt
* @return sum of unloaded goods
* @author Hj. Malthaner
*/
uint16 vehicle_t::unload_cargo(halthandle_t halt, sint64 & revenue_from_unloading, array_tpl<sint64> & apportioned_revenues)
{
uint16 sum_menge = 0, sum_delivered = 0, index = 0;
revenue_from_unloading = 0;
if( !halt.is_bound() ) {
return 0;
}
if(halt->is_enabled(get_cargo_type()))
{
for (uint8 j = 0; j < number_of_classes; j++)
{
if (!fracht[j].empty())
{
for (slist_tpl<ware_t>::iterator i = fracht[j].begin(), end = fracht[j].end(); i != end; )
{
const ware_t& tmp = *i;
halthandle_t end_halt = tmp.get_ziel();
halthandle_t via_halt = tmp.get_zwischenziel();
// check if destination or transfer is still valid
if (!end_halt.is_bound() || !via_halt.is_bound())
{
DBG_MESSAGE("vehicle_t::unload_cargo()", "destination of %d %s is no longer reachable", tmp.menge, translator::translate(tmp.get_name()));
total_freight -= tmp.menge;
sum_weight -= tmp.menge * tmp.get_desc()->get_weight_per_unit();
i = fracht[j].erase(i);
}
else if (end_halt == halt || via_halt == halt)
{
// here, only ordinary goods should be processed
if (halt != end_halt && welt->get_settings().is_avoid_overcrowding() && tmp.is_passenger() && !halt->is_within_walking_distance_of(via_halt) && halt->is_overcrowded(tmp.get_desc()->get_index()))
{
// The avoid_overcrowding setting is activated
// Halt overcrowded - discard passengers, and collect no revenue.
// Experimetal 7.2 - also calculate a refund.
if (tmp.get_origin().is_bound() && get_owner()->get_finance()->get_account_balance() > 0)
{
// Cannot refund unless we know the origin.
// Also, ought not refund unless the player is solvent.
// Players ought not be put out of business by refunds, as this makes gameplay too unpredictable,
// especially in online games, where joining one player's network to another might lead to a large
// influx of passengers which one of the networks cannot cope with.
const uint16 distance = shortest_distance(halt->get_basis_pos(), tmp.get_origin()->get_basis_pos());
const uint32 distance_meters = (uint32)distance * welt->get_settings().get_meters_per_tile();
// Refund is approximation.
const sint64 refund_amount = (tmp.menge * tmp.get_desc()->get_refund(distance_meters) + 2048ll) / 4096ll;
revenue_from_unloading -= refund_amount;
// Book refund to the convoi & line
// (the revenue accounting in simconvoi.cc will take care of the profit booking)
cnv->book(-refund_amount, convoi_t::CONVOI_REFUNDS);
}
// Add passengers to unhappy (due to overcrowding) passengers.
halt->add_pax_unhappy(tmp.menge);
total_freight -= tmp.menge;
}
else
{
assert(halt.is_bound());
const uint32 menge = tmp.menge;
halt->liefere_an(tmp); //"supply" (Babelfish)
sum_menge += menge;
index = tmp.get_index(); // Note that there is only one freight type per vehicle
total_freight -= tmp.menge;
cnv->invalidate_weight_summary();
// Calculate the revenue for each packet.
// Also, add to the "apportioned revenues" for way tolls.
// Don't charge a higher class that the passenger is willing to pay, in case the accommodation was reassigned enroute.
const uint8 class_of_accommodation = min(class_reassignments[j], tmp.get_class());
revenue_from_unloading += menge > 0 ? cnv->calc_revenue(tmp, apportioned_revenues, class_of_accommodation) : 0;
// book delivered goods to destination
if (end_halt == halt)
{
sum_delivered += menge;
if (tmp.is_passenger())
{
// New for Extended 7.2 - add happy passengers
// to the origin station and transported passengers/mail
// to the origin city only *after* they arrive at their
// destinations.
if (tmp.get_origin().is_bound())
{
// Check required because Simutrans-Standard saved games
// do not have origins. Also, the start halt might not
// be in (or be fully in) a city.
tmp.get_origin()->add_pax_happy(menge);
koord origin_pos = tmp.get_origin()->get_basis_pos();
stadt_t* origin_city = welt->get_city(origin_pos);
if (!origin_city)
{
// The origin stop is not within a city.
// If the stop is located outside the city, but the passengers
// come from a city, they will not record as transported.
origin_pos = tmp.get_origin()->get_init_pos();
origin_city = welt->get_city(origin_pos);
}
if (!origin_city)
{
for (uint8 i = 0; i < 16; i++)
{
koord pos(origin_pos + origin_pos.second_neighbours[i]);
origin_city = welt->get_city(pos);
if (origin_city)
{
break;
}
}
}
if (origin_city)
{
origin_city->add_transported_passengers(menge);
}
}
}