The International Simutrans Forum

 

Author Topic: Getting convoy on the tile  (Read 728 times)

0 Members and 1 Guest are viewing this topic.

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Getting convoy on the tile
« on: August 17, 2021, 12:14:19 PM »
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?

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #1 on: August 17, 2021, 12:48:22 PM »
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

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Re: Getting convoy on the tile
« Reply #2 on: August 17, 2021, 01:00:34 PM »
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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #3 on: August 17, 2021, 01:09:56 PM »
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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #4 on: August 17, 2021, 01:13:49 PM »
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:

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10905
  • Languages: De,EN,JP
Re: Getting convoy on the tile
« Reply #5 on: August 17, 2021, 01:25:27 PM »
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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #6 on: August 17, 2021, 01:29:26 PM »
The 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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #7 on: August 17, 2021, 01:36:54 PM »
veh->get_convoi()->self
I assume this refers to the convoy selected by the cursor?.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10905
  • Languages: De,EN,JP
Re: Getting convoy on the tile
« Reply #8 on: August 17, 2021, 02:10:56 PM »
Yes thi needs to be exposed to the api. Unfortunately I have very little knowledge on the scripting.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #9 on: August 17, 2021, 02:13:57 PM »
It seems like something easy to do, I'm just not sure if that would be a boolean function to identify the convoy.  :P

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10905
  • Languages: De,EN,JP
Re: Getting convoy on the tile
« Reply #10 on: August 18, 2021, 12:10:00 PM »
It should return a covoi handle. That is never zero.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #11 on: August 18, 2021, 12:14:46 PM »
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.  :-[ 

Offline Freahk

  • Devotee
  • *
  • Posts: 1624
  • Languages: DE, EN
Re: Getting convoy on the tile
« Reply #12 on: August 18, 2021, 12:49:22 PM »
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.

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Re: Getting convoy on the tile
« Reply #13 on: August 18, 2021, 03:07:42 PM »
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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #14 on: August 18, 2021, 03:10:18 PM »
get_convoi()->self
I'm going to experiment with a get_convoi () -> self to get the selected convoy. :P

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #15 on: August 18, 2021, 06:43:00 PM »
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:
Code: [Select]
    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())
    }
« Last Edit: August 18, 2021, 10:57:13 PM by Yona-TYT »

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10905
  • Languages: De,EN,JP
Re: Getting convoy on the tile
« Reply #16 on: August 19, 2021, 02:06:26 AM »
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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: Getting convoy on the tile
« Reply #17 on: August 20, 2021, 06:29:06 PM »
@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.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10905
  • Languages: De,EN,JP
Re: Getting convoy on the tile
« Reply #18 on: August 21, 2021, 11:56:26 AM »
r10036 should do this.

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Re: Getting convoy on the tile
« Reply #19 on: August 24, 2021, 10:20:18 AM »
Thank you  8)