Adding entrance occupied penalty to Bus/Tram YPAF

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
tramrider
Engineer
Engineer
Posts: 2
Joined: 12 Mar 2024 05:24

Adding entrance occupied penalty to Bus/Tram YPAF

Post by tramrider »

Recently I have been using trams a lot, building some tram-only maps.One issue I have noticed is that if a tram or bus is loading in the first tile of a drive-through station, then it blocks the entry for other vehicles. The pathfinder does not recognize that the road stop entrance is blocked and vehicles will queue behind the loading vehicle.

Ideally this additional penalty would encourage vehicles to drive 'around the block' to enter the station from the other direction. This would be preferred because then the vehicle can unload sooner.

In this pathfinder code the route is appearing to be penalized based on the fill percentage in the road stop. This is enough to divert the vehicle to a different empty drive-through stop/line of tiles, but if there is only one drive-through station and the entrance is blocked then vehicles will queue at the closest entrance.
https://github.com/OpenTTD/OpenTTD/blob ... ad.cpp#L82

I have read through the code and considered several methods to apply this penalty. I am a webstack chump and this is my first time using C so I would like to get some feedback.

YAPF-only Solution
When the routefinder reaches the first destination station tile, check if there is a vehicle stopped in it. I cannot find any existing function to get all vehicles for a given tile and it sounds like it might involve a lot of expensive looping anyway.

Add 'entrance occupied' to Roadstop
Roadstop tracks how occupied the drive-through station is but it does not track where vehicles are inside the station. We would have to add something that represents the entrance being blocked.Could the RoadStop::Enter function be leveraged to switch an entrance_occupied flag when the vehicle stops in the entrance, then remove the flag when the vehicle leaves the entrance? More accurately, this flag would need to be set when a non-articulated vehicle stops in/leaves the first half of the tile.The YPAF would then apply the penalty if entrance_occupied of the destination station were true.
There appears to be a concept of bays in drive through stops, and a function called `AllocateDriveThroughBay` in the roadstop_base.h, although I cannot see where it is being referenced. Could checking bays are free/allocated be used?

Reserving bays (and no penalty for entrance occupied)
There is some brief discussion about implementing reserving of road station bays here but it's quite outdated:
https://github.com/OpenTTD/OpenTTD/issues/1944
This solution is probably too complicated for me as a first-time contributor, although it would seem preferred because it matches how it works with trains.
User avatar
jfs
Tycoon
Tycoon
Posts: 1759
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: Adding entrance occupied penalty to Bus/Tram YPAF

Post by jfs »

I wonder if the "fill percentage" could be changed to calculate as "backmost position occipied" rather than "number of positions occupied".

For example, if you had a row of 3 stops, which each has 8 "positions" in each lane, that makes a total of 24 "positions" in one lane. If you then have two vehicles of length 10 that stop end-to-end in that lane of the stop, you have 20 of 24 positions occupied, from position 1 up to position 20. If the front vehicle then leaves, that leaves 10 positions occupied, from position 11 to 20.
In this situation, the fill ratio would be 42%, but in practice there's only 4 positions free in the lane. So modify the calculation to determine the fill ratio to be based on the last occupied position instead of the number of occupied positions, such that until the second vehicle leaves, the fill ratio is considered 84% regardless of the free positions in front of the second vehicle.
tramrider
Engineer
Engineer
Posts: 2
Joined: 12 Mar 2024 05:24

Re: Adding entrance occupied penalty to Bus/Tram YPAF

Post by tramrider »

Yes that's a good and simple idea too, I like it. That would significantly increase the penalty while retaining the existing YAPF logic.

As far as I read the code now, there's no way to read the position vehicles are stopped at. It only remembers that they are stopped. The capability would need to be added to the code.


edit:
Just been doing some checking of the code, and I think this method can be achieved if we only set `occupied` to 0 when the last vehicle leaves the station (ie when there are 0 vehicles).
Relevant line of code here: https://github.com/OpenTTD/OpenTTD/blob ... p.cpp#L282

Now i just have to work out how many vehicles are actively loading/unloading in the station.
Last edited by tramrider on 12 Mar 2024 09:29, edited 1 time in total.
tramrider
Engineer
Engineer
Posts: 2
Joined: 12 Mar 2024 05:24

Re: Adding entrance occupied penalty to Bus/Tram YPAF

Post by tramrider »

jfs wrote: 12 Mar 2024 08:50 I wonder if the "fill percentage" could be changed to calculate as "backmost position occipied" rather than "number of positions occupied".

For example, if you had a row of 3 stops, which each has 8 "positions" in each lane, that makes a total of 24 "positions" in one lane. If you then have two vehicles of length 10 that stop end-to-end in that lane of the stop, you have 20 of 24 positions occupied, from position 1 up to position 20. If the front vehicle then leaves, that leaves 10 positions occupied, from position 11 to 20.
In this situation, the fill ratio would be 42%, but in practice there's only 4 positions free in the lane. So modify the calculation to determine the fill ratio to be based on the last occupied position instead of the number of occupied positions, such that until the second vehicle leaves, the fill ratio is considered 84% regardless of the free positions in front of the second vehicle.
I have filed a PR using your suggestion: https://github.com/OpenTTD/OpenTTD/pull/12290
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 19 guests