train depots


Makes trains go to a depot station if they dont have any other station to go to

Utilities
1 year, 1 month ago
1.1
336
Trains

g Adding Depot where no necessary

1 year, 1 month ago
(updated 1 year, 1 month ago)

Hi, here is a bug report:

Many schedules get a Depot entry despite clearly having at least 2 open stations.

https://i.imgur.com/FTwkC3y.png

1 year, 1 month ago

Hi,
Have you updated the mod recently? I have noticed some weird behaviour after updating. If you have, try running the /remake_train_table command so the mod can repopulate some of the values needed for the trains.

1 year, 1 month ago
(updated 1 year, 1 month ago)

The command did not change any behaviour.

Here is a new, simple setup which still adds a depot stop where it isn't needed:
https://i.imgur.com/LwcK1KU.png

It also happens with 3 or more open stations.

According to the /print_trainstop_table command, the stops in question are not in the train stop table

1 year, 1 month ago

This is what I could find out so far:

  1. create_stop_list() is not called after I create new stations. I called this function manually, and then the stations appeared in the train stop table. I'm not sure when this function is supposed to be called.

  2. if I create new stations, and I execute /remove_depots_from_trains before create_stop_list() is called the game crashes (index out of bounds)

1 year, 1 month ago
(updated 1 year, 1 month ago)

Ah thanks, in my testing I have not checked if adding the stops did not cause any problems. I will look into fixing it. Again thank you, I'm new to the whole modding deal so I hope you understand I still have some problems debugging.

1 year, 1 month ago

After some testing with placing and removing a lot of stations I can not seem to find why it would not correctly add stations to the list, in my testing it added them every time. I have however been able to identify and fix what I think caused the crash you were talking about.
I will do some more testing to see if I can find the problem that causes the depots being added, and add a quick check for trains to not get them or at the very least have them removed. I'm working on a larger update in which I am rewriting the logic to add the depots to the schedule since this is not the only problem I had to solve in a less than elegant way, so I hope that this will do for now.
And again, thank you for your patience with me!

1 year, 1 month ago
(updated 1 year, 1 month ago)

Here is the save game

https://www.file-upload.net/download-15213668/traindepottest42.zip.html

On accord to the fact that some of my train stops are missing from the mod's train stop table: can you explain when exactly your create_stop_list() function is called?

1 year, 1 month ago

Thanks for the save, I will look into if I can see what is wrong with the debug adapter attached tomorrow.

create_stop_list() is called every second, I tried to get it to react to the placing of train stations but that did not seem to work so I changed it into this until I would come up with a better solution. I decided it was a "good enough" compromise for now because the worst that could happen is that a depot station is added in a schedule and removed as soon as it entered another station. Having said that, the way the function is called and actually works is likely going to change when I'm done with the update.

To add at the end. I have pushed an update that I think will fix the issues that occurred to you, again it is not the most elegant code i have ever written, but it does the job. If you find any new or old issues please do reach out again, I am learning as I go!

1 year, 1 month ago
(updated 1 year, 1 month ago)

Don't need to apologize. Own your mistakes, because you learn and grow from them.

The reason I asked when create_stop_list() is called is that I tried debugging your code and manually called this function, and voila my missing stations appeared there and were handled. This probably means that the periodic call of the function did not work anymore. This is just my observation that might or might not give some insight.


What came to my mind looking at your code:

  • avoid code duplication, externalize recurring unchanged logic into small functions (e.g. the counting of active stops per schedule); this greatly increases readability and maintainability

  • avoid branching (if blocks) where it can easily be replaced with logic operations (e.g. setting a boolean: bool a = obj.prop ~= nil and obj.prop.isSomething )


if I create new stations, and I execute /remove_depots_from_trains before create_stop_list() is called the game crashes (index out of bounds)

I reckon this happens because the mod's stop list is missing the stops, where it adds depot stops, and when calling remove_depots_from_trains then tries to access them by indexer, which of course can't work. Here is the error message:

Error while running command "remove_depots_from_trains": Index out of bounds.
stack traceback:
[C]: in function 'newindex'
__train-depots
/control.lua:111: in function 'replace_train_schedule'
train-depots/control.lua:764: in function <train-depots/control.lua:758>

How about we meet in Discord and debug this together?

New response