News:

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

Getting convoy on the tile

Started by THLeaderH, August 17, 2021, 12:14:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

THLeaderH

I tried to implement a scripted tool to copy a convoy on the selected tile to a specific depot. However, it seems that there is no way to get the convoy on the selected tile. tile_x.find_object() returns the vehicle on the tile, but it is an instance of map_object_x and we cannot get the convoy which the vehicle belongs to. How can we solve this?

Yona-TYT

I think there is no direct way to do this, you would have to get the complete list of vehicles, then "get.pos ()" and compare its position with that of the tool whoever finds that first. :P

THLeaderH

There is no way to get the complete list of vehicles, since vehicle_x is not provided. A method to get the complete list of convoys is provided, but that will not be a solution because convoy_x.get_pos() returns only the pos of the front vehicle.

One of the solution may be implementing an API function for tile_x to return the convoy which is on the tile. The more straightforward solution would be implementing vehicle_x and get_vehicle() function for tile_x but this seems to be overspec.

Yona-TYT

It is difficult, because we do not have how to choose which convoy is the correct one on a tile (I do not know if it is possible).
What occurs to me here is to fill in a type of tool with a mini window, so a list of convoys of the selected tile would be generated, then the user can see and choose the correct one.

Yona-TYT

My idea is a new window that works alongside the script tool, and that can create a list like my example.
This is how I check the convoys available in my script:

prissi

The is the function vehicle->get_convoi() which give a direct point (not a handle) to the convoi. However, it might be better to check for zero and otherwise return veh->get_convoi()->self That of course require a tiny code change to expose this to the scripting.

But you window could be generated from the convoi list.

Yona-TYT

Quote from: prissi on August 17, 2021, 01:25:27 PMThe is the function vehicle->get_convoi() which give a direct point (not a handle) to the convoi.
It seems to me that it is not implemented.

Yona-TYT

Quote from: prissi on August 17, 2021, 01:25:27 PMveh->get_convoi()->self
I assume this refers to the convoy selected by the cursor?.

prissi

Yes thi needs to be exposed to the api. Unfortunately I have very little knowledge on the scripting.

Yona-TYT

It seems like something easy to do, I'm just not sure if that would be a boolean function to identify the convoy.  :P

prissi

It should return a covoi handle. That is never zero.

Yona-TYT

I have been analyzing, but I did not find any method to obtain lists of convoys by tile, so the only way is to obtain the complete list of convoys.

Using vehicles_desc to get comvoy_x is also not feasible.  :-[ 

Mariculous

Not sure about the script API, but in the game code there is no direct way to get all convoys from a tile. (at least none that I know of)
It had to be implemented first. objlist_t::get_convoi_vehicle() seems to be related, but not exactly what you are looking for.
It returns the first vehicle found on the tile.
A function might be created that returns all vehicles on that tile.
vehicle_t::get_convoi() can be used to retrieve the convoy of a vehicle, if is is part of a convoy! Vehicles can exist without being part of a convoy.
Then you could discard duplicated convoys.
Alternatively, a function to directly return convoys on that tile uniquely might be used.

THLeaderH

The return value of tile_x.get_convoy() should be array<convoy_x> since one tile can have up to two convoys, tram convoy and road convoy. If there is no convoy on the tile, the return value array<convoy_x> should be empty.

Yona-TYT

Quote from: THLeaderH on August 18, 2021, 03:07:42 PMget_convoi()->self
I'm going to experiment with a get_convoi () -> self to get the selected convoy. :P

Yona-TYT

#15
Well, here is my final experiment, this time you get an array of the convoys.  :P
This cost me a lot, since I don't know much about programming in c ++.
Edit.
Update!. I change the function to the "tile_x" class, since it makes more sense there.
Example:

    local t = tile_x(pos.x,pos.y,pos.z)
    local cov = t.get_convoys()
   
    for(local nr = 0; cov && nr<cov.len(); nr++){
        gui.add_message(""+cov[nr].get_name()+" :: "+cov.len())
    }

prissi

Well, the convoi pointer of vehicles can be zero, and the self access will be crashing. But I will look into this, since now I know where to change it.

Yona-TYT

@Prissi
I also found that in a convoy with trailers it is usually against 2 times the same convoy, I can't solve that no matter how hard I try.

prissi


THLeaderH