Page 1 of 1

Formations?

Posted: Fri May 30, 2014 11:18 pm
by MileHighGuy
Is it possible to make the ai go into formations via lua? Or at least travel in groups? For example, can I have clones follow the first clone commander they see? How complex can we go with this?

Re: Formations?

Posted: Fri May 30, 2014 11:42 pm
by Marth8880
You could possibly create a table with a for...while loop that compiles all of the currently-spawned Commander units and then create an AI goal that's set to defend or follow (defend is a loose follow, follow is a tight follow) the entity ptrs stored in that table. Or something like that. I'm not very familiar with tables, so I can't help you very much with it. However, I think the AI goal bit would probably be something along the lines of this:

Code: Select all

AddAIGoal(REP, "Defend", 80, [commandertable])

Re: Formations?

Posted: Sat May 31, 2014 12:43 am
by MileHighGuy
Are there any examples of this being used in the stock files? I will have to look up how to use tables in lua.

Re: Formations?

Posted: Sat May 31, 2014 6:13 am
by razac920
AIGoals seems like a good idea. I don't know of any examples like this in the stock files, but here's how I would do it:
Hidden/Spoiler:
[code]
players = {}
goals = {}
count = 0
addgoal = OnCharacterSpawn(function(player)
if GetCharacterClass(player) == 4 and GetCharacterTeam(player) == REP then // replace 4 with the index of your commander, starting with 0
local seen = false
for i = 1,count do
if players == player then
seen = true
goals = AddAIGoal(REP,"Defend",80,GetCharacterUnit(player)) // or "Follow" if you want them to follow very closely, and change the weight too
end
end
if not seen then
count = count + 1
players[count] = player
goals[count] = AddAIGoal(REP,"Defend",80,GetCharacterUnit(player)) // or "Follow" if you want them to follow very closely, and change the weight
end
end
end)

removegoal = OnCharacterDeath(function(player,killer)
for j = 1,count do
if players == player and not goals == nil then
DeleteAIGoal(goals)
goals = nil
end
end
end)
[/code]


Since there is a maximum of 20 AI Goals, make sure you don't have too many commanders on the map at the same time.

Re: Formations?

Posted: Sat May 31, 2014 9:47 am
by Marth8880
Looks great! The syntax for the defend/follow goal is AddAIGoal( team, "Defend", weight, gameObjectPtr ), though. ;)

Re: Formations?

Posted: Sat May 31, 2014 9:52 am
by razac920
Fixed, good catch!