AIEngineList() problems

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
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

AIEngineList() problems

Post by MinchinWeb »

I'm trying to get a ship that will transport a given cargo, has less than a maximum capacity, and is the fastest among those left. The code I'm suing is:

Code: Select all

//	Pick an engine (ship)
local Engines = AIEngineList(AIVehicle.VT_WATER);

//	Keep only buildable engines
Engines.Valuate(AIEngine.IsBuildable);
Engines.KeepValue(true.tointeger());

//	TO-DO:	Keep only engines we can afford  AIEngine.GetPrice(EngineID)

//	Keep only ships for this cargo
Engines.Valuate(AIEngine.CanRefitCargo, CargoNo);
Engines.KeepValue(true.tointeger());

//	Keep only ships under max capacity
//		"In case it can transport multiple cargoes, it returns the first/main."
Engines.Valuate(AIEngine.GetCapacity);
Engines.RemoveAboveValue((AIIndustry.GetLastMonthProduction(MetaLib.Industry.GetIndustryID(BuildPair[0]), CargoNo) * this._CapacityDays)/365);

//	Pick the fastest one
Engines.Valuate(AIEngine.GetMaxSpeed);
Engines.Sort(AIList.SORT_BY_VALUE, AIList.SORT_DESCENDING);

local PickedEngine = Engines.Begin();
Log.Note("Picked engine: " + AIEngine.GetName(PickedEngine), 2);
For OIL_, it works fiine, returning "?Fitzroy Utility Tug", but for PASS (passengers), it returned "Kirby Paul Tank (Steam)", which is a train. Did I somehow add trains to my list or is AIEngine.GetName() failing or is it something else?

(I do have FISH 0.9.2, and OpenGFX+ Trains 0.2.5, among others NewGRF active)
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: AIEngineList() problems

Post by Yexo »

Most likely the list is empty (check the size before calling Begin()). Begin() returns 0 in case the list is empty, which incidentally is the engine id of the kirby paul tank.
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: AIEngineList() problems

Post by MinchinWeb »

Thanks for the quick reply Yexo. I had my capacity cutoff set too low, so the list was indeed empty.
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: AIEngineList() problems

Post by krinn »

MinchinWeb wrote: Engines.RemoveAboveValue((AIIndustry.GetLastMonthProduction(MetaLib.Industry.GetIndustryID(BuildPair[0]), CargoNo) * this._CapacityDays)/365);
You might end with a real and not an integer no ?
And seeing your true.tointeger() you know already the function doesn't like something that is not integer.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: AIEngineList() problems

Post by Zuu »

[integer value] / [integer value] => [integer value]

So the /365 at the end will not cause the result to become a float.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
MinchinWeb
Traffic Manager
Traffic Manager
Posts: 225
Joined: 01 Feb 2011 12:41
Contact:

Re: AIEngineList() problems

Post by MinchinWeb »

You have to ask Squirrel very nicely to give you floats (i.e. floating point numbers, a.k.a. real numbers). Further to what Zuu metions, it seems that if you multiply or divide two numbers, one of them being a float and the other being an integer, you get an integer back. That can cause other problem if you division returns results less than 1. For example:

Code: Select all

(1 / 5) * 10
will give you 0 rather than 2...

In this case, all three numbers are integers, and so an integer is returned.
Alberta Town Names - 1500+ real names from 'Acme' to 'Zama City'
MinchinWeb's Random Town Name Generator - providing 2 million plus names...
WmDOT v13 - An AI that doubles as your highway department
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: AIEngineList() problems

Post by Yexo »

MinchinWeb wrote: it seems that if you multiply or divide two numbers, one of them being a float and the other being an integer, you get an integer back.
This is incorrect. If you multiply/divide/add/subtract two numbers where at least one of them is a float, the result will also be a float.
That can cause other problem if you division returns results less than 1. For example:

Code: Select all

(1 / 5) * 10
will give you 0 rather than 2...

In this case, all three numbers are integers, and so an integer is returned.
Your example doesn't demonstrate int/float.

Code: Select all

(1 / 5.0) * 10
will give you 2.0.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 8 guests