Page 1 of 2

[Patch] Train autoconversion v0.2

Posted: 19 Mar 2008 01:33
by jez
Probably the most stress-free train network upgrading you'll ever experience. :)

When you're upgrading your track network using the track conversion tool, to something like monorail or maglev, you'll usually get a 'train in the way' error if you try to upgrade a depot whilst a train is stopped in there. This patch autoconverts all stopped trains, and their wagons, to the first compatible train/wagon of the new track type. This vastly increases the speed of upgrading your train network - simply build up money, send all your trains to the depots, and convert the depots along with the track and all the trains will also be converted!

Any comments/code improvements/suggestions welcome!
Version history:
v0.1 (for SVN trunk r12347):
- Initial release.

v0.2 (for SVN trunk r12421):
- Now you have to hold CTRL when converting rail to autoconvert depots with trains in.
- Extra options added to the autoreplacement code (thanks Bjarni!), now we only upgrade a depot if we can afford to upgrade ALL trains in depot.
- Significant code overhaul, split up CmdConvertRail into 2 functions as we now need to iterate through all selected tiles twice, to check whether we have the money necessary for the conversion first; we must do this manually because we had to cause this command to trigger 'notest' in DoCommand.
- When querying the cost of converting track, if only depots with trains are selected, gives a cost estimation of upgrading each depot (ie. max possible cost).
- When querying to cost of converting track, if any track or empty depots are selected, gives a cost estimation of just upgrading any selected track (ie. min possible cost).
- When actually doing the replacement, always tries to convert any selected track or empty depots first. If that was successful, goes through each found depot with trains in, and tries to convert it; does so if we can afford the conversion.
- The cost animation near the cursor will reflect the cost of upgrding any empty depots and track tiles (INCLUDING the track tiles of the upgraded depots that had trains in them!). Each upgraded depot will have its own cost animation too, indicating the cost of upgrading the trains and wagons in that depot.



Known bugs:
- Group handling probably isn't very good; currently we just get the EngineReplacement for the group ID of the train (ie. engV->group_id), but set it again using the ALL_GROUP group.
- We might need one or two more custom strings for this patch (although I haven't found the need for any more yet...)
- I'm told there may also be problems with NewGRF, as some GRF sets disallow certain cargo types for certain track types. As my patch basically uses the autoreplace code, I'm hoping that as long as autoreplace is maintained to work with NewGRF, so this patch will work.
- Some code cleanup is needed.
- Currently, if we're estimating the cost of autoreplacing trains in a depot, the command will fail if we don't have enough money. The command needs not to fail, but to return the cost estimation of replacement. This patch currently hacks around this by charging 100 million for replacement if the cost estimation fails, but this should be fixed (Bjarni needs to fix this one in the autoreplace code ;-) )
- Doesn't handle very well replacing wagons for NewGRF sets that might not allow certain cargo types on certain track types (what is the best way to handle this? Hmm.)
- Doesn't handle very well replacing train engines that can hold cargo - maybe (maybe not?) we should try to replace them with train engines that handle the same type of cargo.
- Doesn't have a GUI to specify replacements (do we want this? It would seem very complex, perhaps unnecessarily so.)

Re: [Patch] Train autoconversion

Posted: 19 Mar 2008 12:17
by Bilbo
First compatible? What about allowing specifying train replacement orders across railtypes (like SH-40 -> Pegasus, Asiastar -> Chimaera) and then using these for replacement instead of "first available"? This would be more clean solution IMHO

Re: [Patch] Train autoconversion

Posted: 19 Mar 2008 12:43
by TheJosh
Good idea, although your implementation needs many issues ironed out.

Re: [Patch] Train autoconversion

Posted: 19 Mar 2008 12:44
by jez
Bilbo wrote:First compatible? What about allowing specifying train replacement orders across railtypes (like SH-40 -> Pegasus, Asiastar -> Chimaera) and then using these for replacement instead of "first available"? This would be more clean solution IMHO
Yep, but that would need a relatively involved GUI, which could be done I guess but I don't really need it for my games. You could also do the autoconversion with the first available vehicle, then figure out which vehicle had been used as the replacement, then setup autoreplace to replace that vehicle with the one you wanted.

Re: [Patch] Train autoconversion

Posted: 19 Mar 2008 18:37
by NukeBuster
Isn't there already a replace vehicle gui? Couldn't that be used for replacements?

Or let the tool convert just the depo, and use the replace gui to replace the vehicles?

Re: [Patch] Train autoconversion

Posted: 19 Mar 2008 18:56
by jez
There is an autoreplace GUI, but it can currently only replace vehicles with others of the same track type; it can't convert between track types.

Re: [Patch] Train autoconversion

Posted: 19 Mar 2008 21:16
by FooBar
jez wrote:There is an autoreplace GUI...
We know :wink: But one could expand upon the current GUI.

Anyways, I like your feature. Personally, I wouldn't mind upgrading all trains, and then running another replace sequence to get the trains I like. Specifying beforehand would be a more sensible solution though.

Re: [Patch] Train autoconversion

Posted: 20 Mar 2008 00:33
by athanasios
Still better this patch than manually replacing. :)

Re: [Patch] Train autoconversion

Posted: 20 Mar 2008 23:35
by Bilbo
Current GUI could be used and it would be expanded to allow entering replace orders across track types. When coverting between track typess, these cross-track orders would then be performed. When the vehicle gets into the depot, only orders for same track type would be done.

Re: [Patch] Train autoconversion

Posted: 21 Mar 2008 06:07
by leno4
thanks

Re: [Patch] Train autoconversion

Posted: 22 Mar 2008 21:22
by libik
amazing :), thx a lot...

I didn't try it, because I dont have night build yet, but I will buy it as soon as possible...

question > I dont really understand this bug with Groups of trains... It means, that it is not able to convert more trains in one depot or it means, that we shouldnt convert more depots together?

Re: [Patch] Train autoconversion

Posted: 22 Mar 2008 21:38
by leno4
Could someone please post there openttd.exe with the patch already installed?

Thanks

Re: [Patch] Train autoconversion

Posted: 22 Mar 2008 21:49
by fabca2
This is very good !!!

I like the idea very much.

it's very fast, and if you are not happy you can change train by replacing tools (GUI) as you want.

for thoses who say "you must extend GUI to allow replacing across rail type", I would reply :
- it's longer : you have to select each train type (imagine you have 5 differents train types...)
- you will change every train on your network, you cannot work on a part of it (or you had to manage groups etc...)
so, this is not a good idea.

Re: [Patch] Train autoconversion

Posted: 22 Mar 2008 22:21
by jez
libik wrote:question > I dont really understand this bug with Groups of trains... It means, that it is not able to convert more trains in one depot or it means, that we shouldnt convert more depots together?
I'm going to investigate this one a bit more. There are groups of trains, and I need to find out more but I think it's possible to group them and treat different groups differently when it comes to autoconversion. We need to handle that properly with this patch.
leno4 wrote:Could someone please post there openttd.exe with the patch already installed?
Thanks
When I have a newer version that's more robust with fewer bugs I will post such an exe. :-)

Re: [Patch] Train autoconversion

Posted: 22 Mar 2008 22:29
by leno4
jez wrote:
libik wrote:question > I dont really understand this bug with Groups of trains... It means, that it is not able to convert more trains in one depot or it means, that we shouldnt convert more depots together?
I'm going to investigate this one a bit more. There are groups of trains, and I need to find out more but I think it's possible to group them and treat different groups differently when it comes to autoconversion. We need to handle that properly with this patch.
leno4 wrote:Could someone please post there openttd.exe with the patch already installed?
Thanks
When I have a newer version that's more robust with fewer bugs I will post such an exe. :-)
Thanks a lot

Re: [Patch] Train autoconversion

Posted: 23 Mar 2008 00:06
by Bilbo
fabca2 wrote:for thoses who say "you must extend GUI to allow replacing across rail type", I would reply :
- it's longer : you have to select each train type (imagine you have 5 differents train types...)
You specify 5 replacements, then you can proceed in converting entire network. Much easier than deleting 100 trains by hand and then creating 100 new one.
If you use some newGRF's, you may want to specify which replacements you want, instead of relying on some automatic selection (which may or may not be good).
fabca2 wrote: - you will change every train on your network, you cannot work on a part of it (or you had to manage groups etc...)
No. Only trains in depots that are hit by the "convert track" tool gets changed, so you have very precise selection of what parts get converted. Cross-type replace orders would be ignored when train just enters the depot. These would be only for the autoconverter.

Re: [Patch] Train autoconversion

Posted: 23 Mar 2008 17:32
by libik
how to install this patch?

Re: [Patch] Train autoconversion

Posted: 23 Mar 2008 18:33
by Yexo
libik wrote:how to install this patch?
Read http://www.tt-forums.net/viewtopic.php?f=33&t=21678

Re: [Patch] Train autoconversion

Posted: 27 Mar 2008 00:07
by jez
I'm releasing my new version of this patch. :-) As you can see from the v0.2 version history, I've made quite a few changes and things are a bit nicer now, especially thanks to Bjarni's hard work in improving the train autoreplace code. Still one or two bugs to contend with, though.

Note that you now need to hold CTRL when you convert track for this autoreplacement to happen; otherwise you still get the old behaviour.

This new version isn't very well tested, so suggestions and bug reports are welcome.

OpenTTD devs, I'd appreciate starting to get some code reviews from you if you feel like it, too. :-)

Re: [Patch] Train autoconversion v0.2

Posted: 04 Apr 2008 16:14
by brupje
I get

About to do DepotCost... tileindex: 161454
Illegal instruction (core dumped)