Random element from AIAbstractList

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
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Random element from AIAbstractList

Post by Bilbo »

Is there any effective way to "get random town" (or generally "pick random object from some AIAbstractList")?
I have tried with code:

Code: Select all

  local list=AITownList();
  local count=list.Count();
  local N=RandRange(count);
But ... you can get N-th value with list.Begin() and then list.Next(), but that is very ineffective, especially with lots of towns (you have to get through entire part of list to get to Nth element)

You can also valuate with RandItem and then pick first, but that is also ineffective.

Is there some better way for random access to list elements (aside from copying the entire list to some array, which may work, but may not be always appropriate, especially if you won't need the list after that) that won't require going over entire (or significant portion of) list?

Hmm .. maybe it would be nice idea to extend AIAbstractList with some GetRandomItem() function :)
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Random element from AIAbstractList

Post by Zutty »

Bilbo wrote:You can also valuate with RandItem and then pick first, but that is also ineffective.
Thats what I do. Its not slow if thats what you're worried about. Choosing 50 random numbers (average no towns for a 256 map) isn't a big deal compared to something like pathfinding.
PathZilla - A networking AI - Now with tram support.
User avatar
Bilbo
Tycoon
Tycoon
Posts: 1710
Joined: 06 Jun 2007 21:07
Location: Czech Republic

Re: Random element from AIAbstractList

Post by Bilbo »

Zutty wrote:Choosing 50 random numbers (average no towns for a 256 map) isn't a big deal compared to something like pathfinding.
Well, it is 50 times slower than picking just one random number. And if you do it multiple times, it will make a big difference....

Especially for large lists.
If you need something, do it yourself or it will be never done.

My patches: Extra large maps (1048576 high, 1048576 wide) (FS#1059), Vehicle + Town + Industry console commands (FS#1060), few minor patches (FS#2820, FS#1521, FS#2837, FS#2843), AI debugging facility

Other: Very large ships NewGRF, Bilbo's multiplayer patch pack v5 (for OpenTTD 0.7.3)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 16 guests