News:

Do you need help?
Simutrans Wiki Manual can help you to play and extend Simutrans. In 9 languages.

Clarification needed on convoy reversing code

Started by Nazalassa, April 07, 2026, 04:40:39 PM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Nazalassa

I noticed that under certain circumstances, upon reversing convoys shift themselves by some amount, most noticeably when they contain vehicles of non-standard length (that is, not 8 car-units).

teleport.gif

Having looked at the code I found the following in vorfahren() (simconvoi.cc):

Code (C++) Select
                if(  steps_driven>0  ||  !can_go_alte_richtung()  ) {
                        // start route from the beginning at index 0, place everything on start
                        uint32 train_length = move_to(0);

                        // move one train length to the start position ...
                        // in north/west direction, we leave the vehicle away to start as much back as possible
                        ribi_t::ribi neue_richtung = fahr[0]->get_direction();
                        if(neue_richtung==ribi_t::south  ||  neue_richtung==ribi_t::east) {
                                // drive the convoi to the same position, but do not hop into next tile!
                                if(  train_length%16==0  ) {
                                        // any space we need => just add
                                        train_length += fahr[vehicle_count-1]->get_desc()->get_length();
                                }
                                else {
                                        // limit train to front of tile
                                        train_length += min( (train_length%CARUNITS_PER_TILE)-1, fahr[vehicle_count-1]->get_desc()->get_length() );
                                }
                        }
                        else {
                                train_length += 1;
                        }
                        train_length = max(1,train_length);
                        /* ... */
                }

I don't understand what the purpose of the "if (train_length%16 == 0)" condition is; I tried to force it to happen, and this stops the convoi from "jumping", so what is its purpose?
Making paks since October 2023  |  pak48.bitlit | pak32.box | MLM for pak64 | Empire F7 cars | Pneumatic tubes | More pak64 vehicles and industries

Life is like a multi-tasking OS: you know you'll eventually get back to everything, but you don't know when.

poppo

When a convoy goes back direction, we must be careful not to go beyond its original position! If it sticks out, it will interfere with the switch on the adjacent tile and cause a traffic jam (so, we must move only limited tiles when train_length%16>0).

Furthermore, if you want to remove the "jumping" when reversing, there are two additional points to keep in mind.
First, the image of opposite direction is not same position. So, you must care about the offset of reversing (but there are no information about it)
Second, stopping position is not the end of the tile(especially for North and West direction). so, when we reposition convoys, convoy can be jumping.

Nazalassa

Why then does it only happen when the convoy is going north- or westwards, can't the convoy "stick out" of the tile when going in other directions as well? Also, I noticed that convoys which vehicles all have length 8 don't "jump", so why would slightly shorter/longer convoys have to?

Besides, I do not see how the convoy would "stick out" (assuming the new position computation is done correctly) on an unreserved tile, since all tiles with vehicles in them are reserved - in the following situation, both platform tiles are actually reserved when the train reverses:

teleport.gif

(of course, computing the new position needs to be done correctly... As I understand it, train_length should at the end be <total length of train> when facing south/east, and <total length of train> - <length of first vehicle> when facing north/west, for the convoy to reverse in place.)
Making paks since October 2023  |  pak48.bitlit | pak32.box | MLM for pak64 | Empire F7 cars | Pneumatic tubes | More pak64 vehicles and industries

Life is like a multi-tasking OS: you know you'll eventually get back to everything, but you don't know when.

poppo

A Vehicle's position is determined by its front position. So, the last car's end position can be "stick out" for the rear from reserved tiles.
When reversing, we put vehicles spacing out by the length of vehicles. However, as mentioned above, the length of the last car was not taken into account before reversing. So, vehicles cannot advance a distance equal to the last car's length.
But, the total length of convoy except the last one is a multiple of 16, convoys can advance the length of the last car without any problem. If all cars' length are 8, the total length except the last car is 16 or the last car length is equal to the remaining length of the tile. So, This isn't an issue for vehicles that length are 8.

In addition, convoy does not go the end of the tile(steps=255) when going north- or westwards. please see vehicle.cc or rail_vehicle.cc (but this "stopping at the middle of the tile" can cause some problems, imo).

Nazalassa

Quote from: poppo on April 08, 2026, 11:47:59 PMA Vehicle's position is determined by its front position. So, the last car's end position can be "stick out" for the rear from reserved tiles.
When reversing, we put vehicles spacing out by the length of vehicles. However, as mentioned above, the length of the last car was not taken into account before reversing. So, vehicles cannot advance a distance equal to the last car's length.

From what I understood, a vehicle's position represents its south/east end (which is not necessarily the front one). So I only see one possibility for the vehicle to stick out: when it is going south/eastwards and its last vehicle is longer than its first vehicle, in this fashion:

<- ###### ###### ###### ###### ############
S |+------+------+|-----+------+--|---------------| N
                         ||
                         \/
   ############ ###### ###### ###### ######  ->
S |+------------+-|----+------+---|--+------------| N

where |-----| is a tile, ###### are the convoy's vehicles, and + denotes the position of each vehicle.

Is that correct?
Making paks since October 2023  |  pak48.bitlit | pak32.box | MLM for pak64 | Empire F7 cars | Pneumatic tubes | More pak64 vehicles and industries

Life is like a multi-tasking OS: you know you'll eventually get back to everything, but you don't know when.

poppo

not correct...
For all directions, the vehicle position is at the front of the vehicle. And the second car and subsequent cars are positioned behind the preceding car by a distance equal to the length of that car (not their own length), see convoi_t::vorfahren().

In addition, your case the convoy reserved only 2 tiles(the 3rd tile was "sticked out" by last vehicle, but not reserved because last car's position is already in the 2nd tile). so, after reversing, this convoy cannot move to the 3rd tile before check clearance.

Furthermore, for the north and south direction, convoy stop at not the end of the tile but at the middle of the tile(see vehicle_t::hop()).

In this case:

<-        ###### ###### ###### ###### ############
N |-------+------+|-----+------+--|---+-----------| S
                         ||
                         \/
   ############ ###### ###### ###### ######  ->
N  xxxxxxxx|--+------+-----|+------+------+|---------------| S

and

<- ###### ###### ###### ###### ############
S |+------+------+|-----+------+--|---------------| N
                         ||
                         \/
   ############ ###### ###### ###### ######  ->
S  xxxxxxxxxx|+------+------+|-----+------+--|---------------| N


prissi

I think your vehiles is an unfortunate case of jumping 14 units (which happens because the third vehicle starts a 14 on the last tile.) You can verify this by looking at the reserved tiles. In some directions, the convoi occupies two tiles, in other three tiles in a station. (For loading, the actual train length is calculated independently whether the car occupy station tiles or not).

That is also the reason, why map rotation by only rotating coordinates does not work.

poppo

so, Why the convoy to north- or eastward stop in steps=128, only half of the tile? Today, the vehicle length is not only 8.