Patch: Gradual loading at stations

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
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Patch: Gradual loading at stations

Post by Maedhros »

I've got a patch here to allow gradual loading at stations. which is a really nice feature of ttdpatch. It's controllable by a patch setting, so if you don't want it after all it's easy to turn off. :)

Historical comments:
In theory, this supports the loading speed newgrf attribute (Action0, property 7), but I'm not convinced it actually does. As far as I can work out, the code in newgrf.c around line 1198 isn't actually doing anything, but it might just be the bit I changed that's wrong.

Edit: New version attached, which actually uses the load_amount of the wagons rather than that of the engines. :roll: The values are still confusing me a bit though - those from the UK Replacement Set seem reasonable, but those from the DBSetXL seem to be doubled, and somehow the Patch knows what to do with both of them...
Attachments
gradual_loading-r6474.diff
Patch against SVN trunk r6474.
(22.99 KiB) Downloaded 200 times
Last edited by Maedhros on 21 Sep 2006 08:13, edited 3 times in total.
No-one's more important than the earthworm.
User avatar
mart3p
Tycoon
Tycoon
Posts: 1030
Joined: 31 Oct 2005 21:00
Location: UK

Post by mart3p »

Good patch Maedhros :) I've only given it a quick test with UKRS and so far it seems to work ok.

Having had a bit of experience with the NewGRF code, I had a look at your change in newgrf.c. It seems to work fine, action 0 property 7 is being set correctly.
Image
Patchman
Tycoon
Tycoon
Posts: 7575
Joined: 02 Oct 2002 18:57
Location: Ithaca, New York
Contact:

Post by Patchman »

Loading speeds may be modified by callback 12, this is what you need to take into account if you want full compatibility.
Josef Drexler

TTDPatch main | alpha/beta | nightly | manual | FAQ | tracker
No private messages please, you'll only get the answering machine there. Send email instead.
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

mart3p wrote:Good patch Maedhros :) I've only given it a quick test with UKRS and so far it seems to work ok.

Having had a bit of experience with the NewGRF code, I had a look at your change in newgrf.c. It seems to work fine, action 0 property 7 is being set correctly.
Thanks! I was using the value for the engine before, rather than for every wagon, which is why I wasn't seeing any changes happening. :oops:
Patchman wrote:Loading speeds may be modified by callback 12, this is what you need to take into account if you want full compatibility.
Ooh, thankyou. And here I thought I could get away without knowing much about newgrfs... So much for that idea, huh. ^_^


I've updated the patch again by the way - I've added MS and MA macros in table/engines.h rather than hardcoding all the default values. It's also halved the size of the patch.
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan »

Maedhros wrote:
Patchman wrote:Loading speeds may be modified by callback 12, this is what you need to take into account if you want full compatibility.
Ooh, thankyou. And here I thought I could get away without knowing much about newgrfs... So much for that idea, huh. ^_^
I don't know how Open does callbacks, but it seems like (especially for 12, which is called on a vehicle, with no special variables) a simple call to GetCallbackResult(0x12, vehicle); ought to do the trick, followed by a check for whatever flag value means "callback failed"
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
User avatar
mart3p
Tycoon
Tycoon
Posts: 1030
Joined: 31 Oct 2005 21:00
Location: UK

Post by mart3p »

DaleStan wrote:I don't know how Open does callbacks, but it seems like (especially for 12, which is called on a vehicle, with no special variables) a simple call to GetCallbackResult(0x12, vehicle); ought to do the trick, followed by a check for whatever flag value means "callback failed"
The function you want is GetVehicleCallback in newgrf_engine.c . You'll also need to add a new value to the CallbackID enum in newgrf_callbacks.h. The callback failed value, is the constant CALLBACK_FAILED. ;)

As an example, have a look a peter1138’s changes at r5822 where he added support for callback 31.
Image
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan »

Maedhros, be aware that I think there's a bug in there. Callback 31 is an 8-bit callback, but I don't see any code that obviously masks off the high 7 bits of the return from GetVehicleCallback.

Callback 12 is also 8 bits, so make sure you do an "& 0xFF" before using the return value (but after checking for CALLBACK_FAILED).

/me wonders how hard it would be to create a patch to fix this class of bugs once and for all; several people have had trouble with it.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

Thanks a lot, mart3p and DaleStan, with your help it was much easier than I was expecting. :D I've updated the patch in the first post again so it now supports callback 12.
No-one's more important than the earthworm.
User avatar
Born Acorn
Tycoon
Tycoon
Posts: 7596
Joined: 10 Dec 2002 20:36
Skype: bornacorn
Location: Wrexham, Wales
Contact:

Post by Born Acorn »

Oooh. Even more newgrf support! Yay!
Image
Nappe1
Traffic Manager
Traffic Manager
Posts: 175
Joined: 19 Jan 2004 17:25
Location: kotka or Savitaipale, Finland
Contact:

Post by Nappe1 »

more New GRF support is always a good thing(tm.) :)
good patch indeed.
User avatar
mart3p
Tycoon
Tycoon
Posts: 1030
Joined: 31 Oct 2005 21:00
Location: UK

Post by mart3p »

DaleStan wrote:/me wonders how hard it would be to create a patch to fix this class of bugs once and for all; several people have had trouble with it.
Yes, me being one of those "several people". I see you've already posted a fix, nice one! While looking again at the r5288 changes, I spotted another problem. See my bug report here.
Maedhros wrote:I've updated the patch in the first post again so it now supports callback 12.
Excellent, I'll do some more testing. :)
Image
iNVERTED
Route Supervisor
Route Supervisor
Posts: 387
Joined: 24 Apr 2005 21:21
Location: Torquay, England
Contact:

Post by iNVERTED »

Maedhros, the patch breaks any "Transfer and leave empty" order. :(
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

iNVERTED wrote:Maedhros, the patch breaks any "Transfer and leave empty" order. :(
So it did.

The patch has been updated again, which now fixes the following:
  • Unloading to a station that didn't accept the cargo didn't work past the first $load_amount, and then it added the wrong amount to the station anyway.
  • If the "Improved loading algorithm" patch was enabled, vehicles wouldn't load while another train was in the station. It now checks to see if Full Load is in the orders so vehicles without it can load if another is in the station.
  • If the "Leave station when any cargo is full" patch was enabled, vehicles would leave the station when one cargo type was full. It now checks if Full Load is in the orders so they carry on loading the other cargo if it's available and they're not set to Full Load.
BTW, iNVERTED, please can you provide some more information next time you report a bug, like for instance what breaks, and what was supposed to happen... :wink:
No-one's more important than the earthworm.
iNVERTED
Route Supervisor
Route Supervisor
Posts: 387
Joined: 24 Apr 2005 21:21
Location: Torquay, England
Contact:

Post by iNVERTED »

I usually do, but this one was so obvious that I only needed to tell you what had broken. ;)

Yay, more practice with applying patches for me. *shot*
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

iNVERTED wrote:I usually do, but this one was so obvious that I only needed to tell you what had broken. ;)
Hehe, this is true. :)

Right. I think I've sorted out the transfers once and for all. There is now a cargo_unloading boolean in the Vehicle struct, so once the vehicle has unloaded all its cargo it won't try to unload anything that was just loaded.
No-one's more important than the earthworm.
User avatar
mart3p
Tycoon
Tycoon
Posts: 1030
Joined: 31 Oct 2005 21:00
Location: UK

Post by mart3p »

Maedhros: while looking at your code, I noticed you are calling GetVehicleCallback for callback 12 from LoadUnloadVehicle in economy.c. This means the callback is being repeatedly called for all vehicles (and wagons) while they are loading. Although this works, it is unnecessary. The NewGRF spec says:
This callback is used instead of property 07 when loading a game or rearranging a train in a depot (or when buying other vehicles).
I think it would be better if you only used the callback at these times. Although it probably means adding a byte to the Vehicle structure to cache the callback result.
Image
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

mart3p wrote:Maedhros: while looking at your code, I noticed you are calling GetVehicleCallback for callback 12 from LoadUnloadVehicle in economy.c. This means the callback is being repeatedly called for all vehicles (and wagons) while they are loading. Although this works, it is unnecessary. The NewGRF spec says:
This callback is used instead of property 07 when loading a game or rearranging a train in a depot (or when buying other vehicles).
I think it would be better if you only used the callback at these times. Although it probably means adding a byte to the Vehicle structure to cache the callback result.
I'm going to have to go back and read the newgrf spec more closely, obviously. It'll make the patch a bit more complicated to calculate the value for each type of vehicle when it's being bought or rearranged, but I do agree that's what should be done.
No-one's more important than the earthworm.
Patchman
Tycoon
Tycoon
Posts: 7575
Joined: 02 Oct 2002 18:57
Location: Ithaca, New York
Contact:

Post by Patchman »

I should note that I wrote this "limitation" in the very beginning of the callback system, this was only the third callback, after all. Back then I overestimated the performance impact of doing a callback... but from what I can tell, at least in the patch, the cost of a callback is negligible unless it's called for every vehicle in every engine tick.

So unless callbacks are much more expensive in OTTD, it's probably fine to leave it as is.
Josef Drexler

TTDPatch main | alpha/beta | nightly | manual | FAQ | tracker
No private messages please, you'll only get the answering machine there. Send email instead.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 7 guests