Functions as Arguments: help

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
Lowkee33
Engineer
Engineer
Posts: 20
Joined: 25 Nov 2011 20:49

Functions as Arguments: help

Post by Lowkee33 »

I have an array of stations that my AI has used. I wrote a function that gets the ratings of the station (the AI only deals with passenger buses right now). The function call looks like this:

Code: Select all

function getStationRatings(stationArray, AICargo.CC_PASSENGERS) { blah, blah, blah }
Now, say I want to get the amount of such ware sitting in the station. Would I have to write a new function? Perhaps I can tweak "getStationRatings" to be more flexible. This would look something like:

Code: Select all

function getStationInfo(stationArray, AICargo.GetCargoWaiting ( ?, ? ) ){ blah, blah, blah }
I guess the main problem are the arguments in the GetCargoWaiting function. How can these be filled? Do they need to be filled? Perhaps I can pass the 2nd argument as a string and then use a SwitchCase in the actual function. This would be a messier function than I want, but would look like:

Code: Select all

function getStationInfo(stationArray, "GetWaiting") { blah, blah, blah }
Am I being clear? Eventually, I will have more arrays than just for stations. Depending on how flexible this can be perhaps I can have one function "getArrayInfo".

Thanks
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Functions as Arguments: help

Post by Yexo »

If you have a single integer as key (the StationID) and a single integer as value (either rating or amount of cargo waiting), take a look at AIList and the Valuate function. It seems to do exactly what you want.

You can implement the same behavior with arrays, use squirrels call/acall function for that.
Lowkee33
Engineer
Engineer
Posts: 20
Joined: 25 Nov 2011 20:49

Re: Functions as Arguments: help

Post by Lowkee33 »

Thanks.

Valuate does do exactly what I want. The main issue here is that I can hardly program, and was having problems with lists. I will have to be careful with sorting arrays though. Does sorting them change their order? Basically, I am going to have parallel arrays (If station 2 has low ratings, then clone a bus from group 2 at depot 2).

Moving towards lists will probably be better in the long run. I was worried about when I have other types of stations (freight/trains) that this will get complicated.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Functions as Arguments: help

Post by Zuu »

If you have two parallell arrays where eg. item 3 in array A is connected to item 3 in array B, then you could join those arrays to one array.

Eg.

Code: Select all

local array = []
array.append( { station = BuildStation(), depot = BuildDepot() } );
array.append( { station = BuildStation(), depot = BuildDepot() } );
array.append( { station = BuildStation(), depot = BuildDepot() } );
If you then reorder the array by sorting it, your station + depot pairs will still be kept together. Here I assume that BuildStation() and BuildDepot() returns the tile/id of a station/depot.

To loop over your stations/depots:

Code: Select all

foreach(item in array) // Note that when looping over Squirrel arrays, you always loop over
                       // the values. (If array would have been an AIList, you would need something like
                       // foreach(item, _ in ai_list)
{
    local station = item.station;
    local depot = item.depot;

    // do whatever you want with the station and depot
}
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Functions as Arguments: help

Post by Yexo »

Lowkee33: You probably don't like this advise, but try starting with something easier. Programming an AI is hard.
Lowkee33
Engineer
Engineer
Posts: 20
Joined: 25 Nov 2011 20:49

Re: Functions as Arguments: help

Post by Lowkee33 »

Yexo: I don't like that answer at all :lol: . I've had TT mastered for a long time now (15+ years...!). While I thoroughly enjoy OpenTTD, I quickly find the fun leave when I make money faster than I can spend it. Making my own AI certainly puts the challenge back.

Thanks for the tip Zuu. I've taken a big step in learning about lists, so they seem to be the direction I am going.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 7 guests