Request : AIAbstractList::KeepClosestTo

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
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Request : AIAbstractList::KeepClosestTo

Post by paullb »

I'd like to request an AIAbstractList::KeepClosestTo(value, number)

where value is value from which to start
and number is number of values to keep

For example, list has the following values 1,5,7,8,6,4,2,3

List.KeepClosestTo(4,5) would keep the values 5,6,4,2,3 [i.e. it keeps the 5 closest values to 4]
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: Request : AIAbstractList::KeepClosestTo

Post by TrueBrain »

Make a Squirrel Valuator which does:

Code: Select all

return abs(goal - value);
And then you can run KeepBottom(5). Much easier.
The only thing necessary for the triumph of evil is for good men to do nothing.
User avatar
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Re: Request : AIAbstractList::KeepClosestTo

Post by paullb »

TrueLight wrote:Make a Squirrel Valuator which does:

Code: Select all

return abs(goal - value);
And then you can run KeepBottom(5). Much easier.
While your solution will work. I would hardly say it is "much easier" than having a function to do it for you. :D
User avatar
paullb
Traffic Manager
Traffic Manager
Posts: 129
Joined: 19 May 2008 13:11

Re: Request : AIAbstractList::KeepClosestTo

Post by paullb »

What if the valuator is town population or something like that? How would you say get the closest 5 cities to a population of 1000?
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Request : AIAbstractList::KeepClosestTo

Post by Zutty »

It would still work...

Code: Select all

Town : Population
Before
-------
A: 1000
B: 300
C: 1200
D: 2000
E: 1100
F: 900

After
------
A: 0
E: 100
F: 100
C: 200
B: 700
D: 1000

Code: Select all

local towns = AITownList()
local goal = 1000;
local keep = 5;

towns.Valuate(function (town, goal) {
  return abs(goal - AITowm.GetPopulation(town));
}, goal);
towns.KeepBottom(keep);
PathZilla - A networking AI - Now with tram support.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests