Refit cargo size - API oversight?

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
TTFTCUTS
Engineer
Engineer
Posts: 24
Joined: 15 Oct 2009 00:25

Refit cargo size - API oversight?

Post by TTFTCUTS »

Hi there,

I've been working on an AI over the last couple of days (hopefully I'll make a thread for it soon), and I've noticed that while there is a way to get an engine's cargo capacity, and the capacity of a vehicle when refit to a specific cargo, there doesn't appear to be a way to find the refit capacity of an engine without constructing one and testing with the vehicle function.

This comes from finding that my AI was not building enough vehicles when transporting FIRS supplies - the eGRVTS box truck that it had chosen carries 50 crates of goods, but due to the whole "goods takes half the space" thing, they only carry 25 crates of supplies. This means that when evaluating how many trucks are needed, the result is only half of what it should be.

I have a workaround - when working out how many vehicles a route should use, it checks if there are any existing vehicles on the route already and checks their refit capacity, but this is hardly ideal. If the same thing was happening with trains, but instead of train number it was evaluating train length, it would cause a significantly greater problem.

Does an AIEngine.GetRefitCapacity( engine, cargo ) function seem like a good candidate for a new entry into the API?


Edit: I also just found that a vehicle needs to be in the depot to get its refit capacity - which makes the workaround even less ideal.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Refit cargo size - API oversight?

Post by Yexo »

TTFTCUTS wrote:Edit: I also just found that a vehicle needs to be in the depot to get its refit capacity - which makes the workaround even less ideal.
That seems like a bug.

The rest of your post is a known problem, but there is no solution for it currently. The main problem is that the capacity is changed by a NewGRF callback, which means that even OpenTTD doesn't know in advance what the capacity is going to be. As such it's can't provide AIs with that information, however badly you may need it. The only workaround is to build a vehicle, refit it to the correct cargo and do your computations again with that value. Yes, this is annoying and yes, we want a better solution too. For the short-time however, that's the best there is.

There are a few longer and more detailed explanations in this forum, but since I'm short on time I'm not going to search for them right now.
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: Refit cargo size - API oversight?

Post by krinn »

Yexo wrote:
TTFTCUTS wrote:Edit: I also just found that a vehicle needs to be in the depot to get its refit capacity - which makes the workaround even less ideal.
That seems like a bug.
This is not a bug :)
http://noai.openttd.org/docs/trunk/classAIVehicle.html#7c315d782c13df53262c083ac916af47 wrote:Precondition:
IsValidVehicle(vehicle_id).

AICargo::IsValidCargo(cargo).

You must own the vehicle.

The vehicle must be stopped in the depot.
I solve it by assigning a 255 value to any capacity for any cargos for all engines, and then record the current "native" cargo capacity of an engine to its real value.

next to that, choosing the best capacity of an engine will always return the engine that have never been built yet (as its capacity is set as 255), you build the engine, check the real capacity now that the engine was built and record it.
Now upto you: you can re-sell this one and build another one until you cannot find a different engineID or just leave that vehicle running with that engineID and let the AI learned with time what will be the best engineID for that given cargo.

Again upto you to speedup the learning process: build an engine and check any cargos refit possibilties : cost money as you will refit any unknow engine to check all cargos capacity once built (cost is total cargotype -1 "the native" * refit cost for one cargo), or just record the current cargo capacity and leave it going like that until your AI need the same engine with a different cargo.

In my tests "i" estimate safe to build any engine until i could find the "real" best engine given a cargo type, as selling the engine to rebuild it won't lost money (engine value decline, but with time: so a fast buy/sell won't let you loose money).

you endup with an easy :
- A: pickup engine for cargo X
- refit engine
- record capacity for cargo X
- B: pickup engine for cargo X
if (A==B) release the vehicle, else sell and loop to A:

you can check out (and reuse if your AI is GPLv2 or compatible ONLY please) it there : http://dev.openttdcoop.org/projects/ai- ... andler.nut
TTFTCUTS
Engineer
Engineer
Posts: 24
Joined: 15 Oct 2009 00:25

Re: Refit cargo size - API oversight?

Post by TTFTCUTS »

Yexo wrote:The main problem is that the capacity is changed by a NewGRF callback, which means that even OpenTTD doesn't know in advance what the capacity is going to be
Fair enough then - I didn't know OTTD knew that little about what NewGRFs get up to. Might have to have a "master depot" by the company HQ where experiments take place :P

On a slightly related subject, is there a way to get the AI to refit a vehicle to an alternate capacity if there is one? E.g. the HEQS trams with different lengths.


krinn - thanks for the information. I'm trying not to re-use other peoples' code for the sake of learning myself, but I think I might re-use the concept (albeit with the evil corporate lab tacked onto the side of the HQ!)
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: Refit cargo size - API oversight?

Post by krinn »

TTFTCUTS wrote: On a slightly related subject, is there a way to get the AI to refit a vehicle to an alternate capacity if there is one? E.g. the HEQS trams with different lengths.
No, same for alternate engine power or livery...
Because :) ->
TTFTCUTS wrote:OTTD knew that little about what NewGRFs get up to.
TTFTCUTS
Engineer
Engineer
Posts: 24
Joined: 15 Oct 2009 00:25

Re: Refit cargo size - API oversight?

Post by TTFTCUTS »

Thought that might be the case - thanks once again.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 8 guests