Your Lua AI is technically a spring gadget, and gadgets have callins for events. These dont need to be defined however for the gadget to work.
e.g. as shown in your AI:
function gadget:UnitFinished(unitID, unitDefID, teamID) --moveAway (unitID, 200) if (isTeamCBM (teamID)) then
There is an event UnitDamaged that Kloot has posted an example for. If you paste that in and then put your own code in, using the existing AI code as an example of how to use the parameters, then you can ge tmoving
Joined: 22 Feb 2006, 01:02 Location: cheap kitchen
Your Lua AI is technically a spring gadget
Yup. A Lua AI is not much different from other gameplay scripts, ie in CT, the resource harvesting. The only difference is that the AI has some extra lines to set up which player(s) it controlls. list of callins: http://springrts.com/wiki/LuaCallinReturn
This is the function deceleration. It is telling Lua that this function exists and that it takes a list of arguments. You can think of it kind of like opening a fast food restaurant window. The name of your restaurant is 'gadget:UnitDamaged' and you have a menu board which defines the things you need before you will take an order. We call it an argument list. (Sometimes its called a parameter list).
is your argument list. Each argument must be specified before you will start processing the order.
In this example Lua would walk up to your fast food window and specify all of the items on the menu, arguments to your function, tell you to start working and wait until you are finished.
Where Lua got these arguments really doesn't matter. You don't care where they came from, or how Lua obtained them. You only care what they are. So... what exactly are they?
unitID - The ID number of the unit that got attacked. Kind of like dogtags or a social security number. unitDefID - This is an ID number that specifies the definition of the unit. More like a resume for the unit. unitTeam - Which team the unit belongs to. damage - How much damage was done to the unit. paralyzer - (I don't really know) weaponID - The ID number for the weapon that attacked the unit with unitID. Like the serial number on a gun. attackerID - The ID number of the unit that did the attacking. attackerDefID - The ID number of the definition for the unit. attackerTeam - The team the attacker belongs to.
Now. Like I said before, we don't care how Lua got this information, we just need to assume its true. Now that we know all of the information we need to start, we can begin processing the order.
for t in pairs(myTeam) do
This is a for loop. It is a basic looping structure. It will do everything between the matching 'do' and 'end' keywords some number of times. How many times? We don't know, and frankly we don't care. What we do care about is what each part of this 'for' statement is.
t - The t in this for statement is a variable. It is going to store the data that comes out of whatever collection we are looking at. in - A keyword that tells us which collection to look in. pairs(myTeam) - pairs() is a function and myTeam is a global variable somewhere. The variable myTeam is a collection. A collection is a group of things. You can think about it as being a great big bag that we can throw things in. pairs() is a special function that allows us to look at the things in our bag in sets of two items, a key and a value.
When we look at the code "for t in pairs(myTeam) do" we need to understand that t is going to become the key for every pair of items in our bag. It is going to help us understand what happens next.
if (unitTeam == myTeam[t]) then
The if statement is fairly basic. Lets take a look at the interesting part
Here we have that same global variable myTeam again. Remember, it is a collection ( a bag full of things). 't' is our key we got from our for statement.
myTeam[t] is a statement that says: "Hey bag of stuff. give me the thing that matches to this key 't'" and magically out pops the thing that is associated with the key.
Why are we doing this? Well, we know that somewhere in that big fat sack of things is my team identifier. It tells me what my team is. Eventually when I yell at the sack long enough, and with enough keys 't', it is going to give me back my team identifier. I just have to find it first.
The 'if' statement itself checks everything that comes out of the bag against the unitTeam argument that Lua gave us. If they ever match I know that the unit Lua is telling me about is actually my unit. This is because the unit's team idenfier matches my team identifer.
The next two lines
local goto_x, goto_y, goto_z = Spring.GetUnitPosition (unitID) machTargetArea(myTeam[t], goto_x, goto_z)
Ask the spring engine to give my the x,y and z coordinate of my unit that just got attacked. (GetUnitPosition) And then send all of my other units to that location (machTargetArea).
The end statements simply tell the if, for and function to end.
In this case we don't return anything so after we are finished processing we simply walk back up to the order window and tell lua that we are finished. Lua then proceeds on to its next task.
paralyzer - (I don't really know) weaponID - The ID number for the weapon that attacked the unit with unitID. Like the serial number on a gun.
paralyzer - boolean (true of false) holding whether the weapon does 'real' or paralysis damage. weaponID - this is unitDefID for weapons, not a serial number. It identifies the type.
attackerID - The ID number of the unit that did the attacking. attackerDefID - The ID number of the definition for the unit. attackerTeam - The team the attacker belongs to.
Be wary of these as if the attacker died some will be nil (I forget if attackerDefID is nil and I am sure the other two are). Before using them do a check.
Actually before using most things it is a good idea to do a check as Spring doesn't work the way you think it would some of the time and in very rare cases something can be nil that makes no sense to be. As in if that was nil previous checks should have already failed.
Users browsing this forum: No registered users and 2 guests
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum