Page 1 of 1

Functions as Arguments: help

Posted: 02 Dec 2011 19:06
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

Re: Functions as Arguments: help

Posted: 02 Dec 2011 19:11
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.

Re: Functions as Arguments: help

Posted: 02 Dec 2011 19:44
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.

Re: Functions as Arguments: help

Posted: 02 Dec 2011 20:52
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
}

Re: Functions as Arguments: help

Posted: 02 Dec 2011 22:16
by Yexo
Lowkee33: You probably don't like this advise, but try starting with something easier. Programming an AI is hard.

Re: Functions as Arguments: help

Posted: 03 Dec 2011 22:08
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.