News:

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

What controls the order in which vehicles are drawn in a convoy?

Started by jamespetts, February 28, 2009, 04:43:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jamespetts

I am looking to add a feature to Simutrans-Experimental allowing trains to run backwards (i.e., with the last vehicle first), and have differential delays for reversing trains depending on whether they can run backwards or not. In order to do so, I need to know how the code determines the order in which to draw vehicles in a convoy; can anyone point me in the right direction?
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

Dwachs

In Simutrans everything is drawn tile-based. Individual members of a convoy are saved on different tiles. All vehicles are sorted on a tile to minimize drawing errors, see dingliste::intern_insert_moving().

Parsley, sage, rosemary, and maggikraut.

jamespetts

Dwachs,

thank you for the information - most helpful. I am not quite sure what you mean by "all vehicles are sorted on a tile", though - can you elaborate a little?

Edit: I cannot find dingliste::intern_insert_moving(), or indeed any string in the entire project containing "insert_moving"; is that the correct name?

Edit 2: I have found


bool dingliste_t::intern_add_moving(ding_t* ding)


but no call is made to that method in either simvehikel.cc or simconvoi.cc - are you sure that this method is used for ordering of vehicles in convoys? The comments in the code refer to citycars.
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

prissi

This code add things to a tile. Moving things belong to convois, but could be citycars or pedetrians. This routine handles it all. It will do right also if you go backewards, as long as the ribis are ok. (However, the convoi routines would do much better, if you just reaarange the convoi when revering and set the first and last flag correctly.)

jamespetts

Prissi,

thank you very much! I had already worked out about using a reversing algorithm to reverse the order of the vehicles in the convoy array, but was having many problems because I was not aware of the first and last flags, which I have now solved thanks to your post. You have been most helpful. For reference, here is the code:


void
convoi_t::reverse_order()
{
// Code snippet obtained and adapted from:
// http://www.cprogramming.com/snippets/show.php?tip=15&count=30&page=0
// by John Shao (public domain work)

    uint8 a = 0;
    vehikel_t* reverse;
uint8 b = anz_vehikel;

fahr[0]->set_erstes(false);
fahr[anz_vehikel - 1]->set_letztes(false);
   
    for(a;a<--b;a++) //increment a and decrement b until they meet eachother
    {
        reverse = fahr[a]; //put what's in a into swap space
        fahr[a] = fahr[b]; //put what's in b into a
        fahr[b] = reverse; //put what's in the swap (a) into b
    }

fahr[0]->set_erstes(true);
fahr[anz_vehikel - 1]->set_letztes(true);
}


Thank you again :-)
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

prissi

Before you do this, you should clear all reservations, because only the last vehicel will clear it. With this, reservations might be left. (But hard to guess from pseudocode.)

jamespetts

Thank you for that suggestion :-) I did initially have problems with reservations, but they went away when I reset the flags for leading and trailing vehicles.
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.