View topic - LUA AI - call-out to detect when a unit is attacked.



All times are UTC + 1 hour


Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: 08 Jun 2011, 03:00 

Joined: 10 Jul 2009, 22:34
So i got into Lua AI stuff by tinkering with knorke's AI for Conflict Terra (great mod, btw). Still don't know much about Lua or Spring AI's.

but is there a call out (something like Spring.getUnitsAttacked, idk, maybe) that could somehow detect when a unit is attacked and report the location of that unit?

Source code of the AI is attatched.


Attachments:
tp_schwarmAI.lua [21 KiB]
Downloaded 111 times
Top
 Offline Profile  
 
PostPosted: 08 Jun 2011, 09:58 
Spring Developer

Joined: 08 Oct 2006, 15:58
Code:
function gadget:UnitDamaged(
    unitID, unitDefID, unitTeam,
    damage, paralyzer, weaponID,
    attackerID, attackerDefID, attackerTeam
)
    -- your code
end


Top
 Offline Profile  
 
PostPosted: 08 Jun 2011, 15:19 

Joined: 10 Jul 2009, 22:34
Mmm. How do I use that exactly? Im new at this


Top
 Offline Profile  
 
PostPosted: 08 Jun 2011, 15:29 
AI Coder
User avatar

Joined: 14 Sep 2004, 10:32
Location: Cookieland
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:

Code:
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


Top
 Offline Profile  
 
PostPosted: 08 Jun 2011, 15:39 
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
AF wrote:
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


Top
 Offline Profile  
 
PostPosted: 08 Jun 2011, 19:13 

Joined: 10 Jul 2009, 22:34
ok, i need knorke's or some lua coder's help with this. Right now the code says:

Code:
function gadget:UnitDamaged(
    unitID, unitDefID, unitTeam,
    damage, paralyzer, weaponID,
    attackerID, attackerDefID, attackerTeam
)

   Spring.Echo(myTeam[1])
   
   if (unitTeam == myTeam[1]) then
      machTargetArea(unitTeam, 2, 2);
      Spring.Echo("Unit under fire!!!")
   end
end   


problem is, myTeam[1] always is "nil". Isn't myTeam[1] supposed to be the first in the list of teams that Schwarm is playing for?

EDIT: Also, I need a way to detect where the unit being attacked is (location on the map)


Latest source is attatched


Attachments:
tp_schwarmAI.lua [21.29 KiB]
Downloaded 108 times
Top
 Offline Profile  
 
PostPosted: 09 Jun 2011, 02:59 

Joined: 10 Jul 2009, 22:34
yanom wrote:
ok, i need knorke's or some lua coder's help with this. Right now the code says:


problem is, myTeam[1] always is "nil". Isn't myTeam[1] supposed to be the first in the list of teams that Schwarm is playing for?



ok, I managed to solve that problem (using a bit of lua that I don't fully understand, borrowed from another function. Maaan, lua is weird )

Anyway, I still need a way to find the location of the unit being attacked.

Latest source is attached


Attachments:
tp_schwarmAI.lua [21.32 KiB]
Downloaded 121 times
Top
 Offline Profile  
 
PostPosted: 09 Jun 2011, 03:31 

Joined: 10 Jul 2009, 22:34
yanom wrote:
Anyway, I still need a way to find the location of the unit being attacked.

solved that, again with more borrowed code.


Quote:
Maaan, lua is weird

Truer than ever.

Latest source is attached


Attachments:
tp_schwarmAI.lua [21.38 KiB]
Downloaded 113 times
Top
 Offline Profile  
 
PostPosted: 09 Jun 2011, 18:42 

Joined: 26 May 2011, 00:01
Quote:
Maaan, lua is weird


Code:
function gadget:UnitDamaged(
    unitID, unitDefID, unitTeam,
    damage, paralyzer, weaponID,
    attackerID, attackerDefID, attackerTeam
)
   
   for t in pairs(myTeam) do
      if (unitTeam == myTeam[t]) then
         local goto_x, goto_y, goto_z = Spring.GetUnitPosition (unitID)
         machTargetArea(myTeam[t], goto_x, goto_z)
      end
   end
end   


Lets break it down for you.
Code:
function gadget:UnitDamaged(
    unitID, unitDefID, unitTeam,
    damage, paralyzer, weaponID,
    attackerID, attackerDefID, attackerTeam
)

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).

Code:
unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam


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.

Code:
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.

Code:
if (unitTeam == myTeam[t]) then


The if statement is fairly basic. Lets take a look at the interesting part
Code:
myTeam[t]

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
Quote:
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.


Top
 Offline Profile  
 
PostPosted: 10 Jun 2011, 19:47 

Joined: 10 Jul 2009, 22:34
ahh, thanks


Top
 Offline Profile  
 
PostPosted: 15 Jun 2011, 13:17 
Moderator

Joined: 12 Oct 2007, 08:24
Quote:
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.

Quote:
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.


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.