LUA AI - call-out to detect when a unit is attacked.
Moderators: hoijui, Moderators
LUA AI - call-out to detect when a unit is attacked.
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.
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
Re: LUA AI - call-out to detect when a unit is attacked.
Code: Select all
function gadget:UnitDamaged(
unitID, unitDefID, unitTeam,
damage, paralyzer, weaponID,
attackerID, attackerDefID, attackerTeam
)
-- your code
end
Re: LUA AI - call-out to detect when a unit is attacked.
Mmm. How do I use that exactly? Im new at this
Re: LUA AI - call-out to detect when a unit is attacked.
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:
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
e.g. as shown in your AI:
Code: Select all
function gadget:UnitFinished(unitID, unitDefID, teamID)
--moveAway (unitID, 200)
if (isTeamCBM (teamID)) then
Re: LUA AI - call-out to detect when a unit is attacked.
Yup. A Lua AI is not much different from other gameplay scripts, ie in CT, the resource harvesting.AF wrote:Your Lua AI is technically a spring gadget
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
Re: LUA AI - call-out to detect when a unit is attacked.
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?
EDIT: Also, I need a way to detect where the unit being attacked is (location on the map)
Latest source is attatched
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
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
Re: LUA AI - call-out to detect when a unit is attacked.
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 )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?
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
Re: LUA AI - call-out to detect when a unit is attacked.
solved that, again with more borrowed code.yanom wrote: Anyway, I still need a way to find the location of the unit being attacked.
Truer than ever.Maaan, lua is weird
Latest source is attached
- Attachments
-
- tp_schwarmAI.lua
- (21.38 KiB) Downloaded 116 times
Re: LUA AI - call-out to detect when a unit is attacked.
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
Code: Select all
function gadget:UnitDamaged(
unitID, unitDefID, unitTeam,
damage, paralyzer, weaponID,
attackerID, attackerDefID, attackerTeam
)
Code: Select all
unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam
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
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
Code: Select all
myTeam[t]
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
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).local goto_x, goto_y, goto_z = Spring.GetUnitPosition (unitID)
machTargetArea(myTeam[t], goto_x, goto_z)
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.
-
- Moderator
- Posts: 2464
- Joined: 12 Oct 2007, 09:24
Re: LUA AI - call-out to detect when a unit is attacked.
paralyzer - boolean (true of false) holding whether the weapon does 'real' or paralysis damage.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.
weaponID - this is unitDefID for weapons, not a serial number. It identifies the type.
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.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.
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.