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

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

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

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

Post by yanom »

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 114 times
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: LUA AI - call-out to detect when a unit is attacked.

Post by Kloot »

Code: Select all

function gadget:UnitDamaged(
    unitID, unitDefID, unitTeam,
    damage, paralyzer, weaponID,
    attackerID, attackerDefID, attackerTeam
)
    -- your code
end
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: LUA AI - call-out to detect when a unit is attacked.

Post by yanom »

Mmm. How do I use that exactly? Im new at this
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: LUA AI - call-out to detect when a unit is attacked.

Post by AF »

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: Select all

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
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LUA AI - call-out to detect when a unit is attacked.

Post by knorke »

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
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: LUA AI - call-out to detect when a unit is attacked.

Post by yanom »

ok, i need knorke's or some lua coder's help with this. Right now the code says:

Code: Select all

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 111 times
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: LUA AI - call-out to detect when a unit is attacked.

Post by yanom »

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 124 times
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: LUA AI - call-out to detect when a unit is attacked.

Post by yanom »

yanom wrote: Anyway, I still need a way to find the location of the unit being attacked.
solved that, again with more borrowed code.

Maaan, lua is weird
Truer than ever.

Latest source is attached
Attachments
tp_schwarmAI.lua
(21.38 KiB) Downloaded 116 times
slind
Posts: 29
Joined: 26 May 2011, 01:01

Re: LUA AI - call-out to detect when a unit is attacked.

Post by slind »

Maaan, lua is weird

Code: Select all

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: Select all

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: Select all

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: Select all

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: Select all

if (unitTeam == myTeam[t]) then
The if statement is fairly basic. Lets take a look at the interesting part

Code: Select all

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
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.
yanom
Posts: 323
Joined: 10 Jul 2009, 23:34

Re: LUA AI - call-out to detect when a unit is attacked.

Post by yanom »

ahh, thanks
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: LUA AI - call-out to detect when a unit is attacked.

Post by Google_Frog »

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.
Post Reply

Return to “AI”