CitiesSkylinesMods / TMPE

Cities: Skylines Traffic Manager: President Edition
https://steamcommunity.com/sharedfiles/filedetails/?id=1637663252
MIT License
577 stars 85 forks source link

Intercity trains despawn instead of returning to an outside connection #180

Open VictorPhilipp opened 5 years ago

VictorPhilipp commented 5 years ago

As @aubergine18 reported here (#161), intercity trains cannot return to an outside connection after unloading. However, it is currently unclear what is causing this and whether it is a bug in TMPE or not.

https://github.com/VictorPhilipp/Cities-Skylines-Traffic-Manager-President-Edition/issues/165

VictorPhilipp commented 5 years ago

It's happening with some, but not all, cargo trains too - in some situations.

Should be reproducible on the save linked in #140 which if memory serves has two cargo stations (one in Garnet district, one in Forestry district) both served by a the same cargo track which loops round in Forestry (ensuring the forward-only train can turn round and leave the map).

After an external train deposits it's cargo:

I've not checked, but maybe passenger trains are showing same trait - despawning if there's no passengers for the "leaving city" journey?

VictorPhilipp commented 5 years ago

It seems to also be happening with cargo ships (and possibly passenger ships too, but not tested those yet). If an external cargo ship offloads goods, it then despawns rather than returning to whence it came.

VictorPhilipp commented 5 years ago

I finally found the reasons why this is happening. It's going to be a bit technical here.

Passenger trains

Passenger trains may despawn if the the train station where they arrived at is located too close to the edge of the map. Of course, this is possible if the 81 tile mod is enabled for example, and in general it should be unproblematic.

When passenger train stations are built next to the edge of the map the game assumes that the train reached an outside connection. If there are no passengers to pick up the game assumes that the train may safely despawn because

However, the map bound condition CO once wrote uses a rather rough metric. [technical stuff ahead] It checks whether the following condition is true for the last known world position (lastFramePosition) of the train in question: max(abs(lastFramePosition.x), abs(lastFramePosition.z)) > 4800 (code reference: PassengerTrainAI.ArriveAtTarget) Since the map center corresponds to the world position (0, 0, 0) the term above (uniform/maximum norm) increases towards map bounds.

A possible solution would be therefore to reduce the threshold (4800) to some higher value or to replace the check with something better. I would first have to check if other mods already provide "old-fashioned" (=non-"Harmony") detours for the PassengerTrainAI.ArriveAtTarget method (@bloodypenguin I have not checked but I strongly assume that public transport related mods like Improved Public Transport 2 override it). If that's the case this would lead to further mod incompatibilities which I do not want to accept.

Cargo trains

I have not tested cargo trains as extensively as passenger trains but a different condition seems to apply here. Cargo trains waiting at a cargo hub despawn when the hub is not able to provide any goods within 16 simulation steps. [technical stuff ahead] The check is performed in the method CargoTrainAI.ArriveAtDestination() (starting with the call to CargoTruckAI.FindNextCargoParent).

I guess the same is also happening for cargo ships.

Same story: If other mods override the ArriveAtDestination method for cargo trains I will not take any action here.

I will postpone the issue for at least version 1.11.

VictorPhilipp commented 5 years ago

That's a really nice investigration! Yes, IPT2 indeed overrides ArriveAtTarget methods, and I can try to implement the fixes there

VictorPhilipp commented 5 years ago

Issue affects cargo road vehicles too.

VictorPhilipp commented 5 years ago

I've noticed this is happening a lot less after the recent updates - maybe due to improved path finding increasing the likelihood that there's cargo/cims to load on to trains/ships/etc?