[Bug]: CTD when all cargos are disabled by NewGRF, and cargo payment graph opened
Version of OpenTTD
Master (7469f00)
Expected result
When all cargos are disabled by a NewGRF like Feature Disabler GRF, starting a new game without any cargos prompts the following error message on map generation:
No vehicles will be available at all Change your NewGRF configuration
I would expect opening the "Cargo payment rates" graph to show an empty graph with no cargos.
Actual result
Opening the "Cargo payment rates" graph causes a crash to desktop.
Steps to reproduce
- Enable the NewGRF Feature Disabler GRF with the parameter enabled to disable all cargos
- Start a game
- Open the "Cargo payment rates" graph
- CTD
I would expect the game to abort world generation if there are no cargo types available. If there aren't any cargo types there isn't any game to play at all, since you have nothing to transport.
It's perfect! You can build eye-candy objects all day without the distraction of pointless transportation! :p
The game crashes in this case due to a division by 0 inside the following function: https://github.com/OpenTTD/OpenTTD/blob/41c40f130b907e73475f41706510a54a519b78e4/src/widget.cpp#L2172-L2180
I was able to reproduce the crash on master (2022-02-10) and in jgrpp (0.46.0). The stack trace of the latter crash was used to find the crashing function.
Crash files from the former crash: crash20220214021730.zip
I'm sure there's more places around the code that assumes that at least one cargo type is available too. That function is just the first that happens to be hit here.
I think the proper fix is to abort map generation if no cargos are available, not to find and fix the crashes.