Page 1 of 1

AIEngineList() problems

Posted: 06 Jan 2012 19:03
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)

Re: AIEngineList() problems

Posted: 06 Jan 2012 19:12
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.

Re: AIEngineList() problems

Posted: 07 Jan 2012 07:06
by MinchinWeb
Thanks for the quick reply Yexo. I had my capacity cutoff set too low, so the list was indeed empty.

Re: AIEngineList() problems

Posted: 09 Jan 2012 01:29
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.

Re: AIEngineList() problems

Posted: 09 Jan 2012 19:15
by Zuu
[integer value] / [integer value] => [integer value]

So the /365 at the end will not cause the result to become a float.

Re: AIEngineList() problems

Posted: 09 Jan 2012 19:26
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.

Re: AIEngineList() problems

Posted: 09 Jan 2012 21:15
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.