AI Goal: Follow Class

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
User avatar
Lagomorphia
Major
Major
Posts: 502
Joined: Sun Dec 13, 2009 10:02 am

AI Goal: Follow Class

Post by Lagomorphia »

For some reason, the search function doesn't allow two letter words, so despite the fact that this has probably been answered already (as I've seen it done before) I can't find the topic explaining how.

How do you set an AI goal so a team follows a certain class of unit? I'm using this script to spawn a small team of units, and I want them to follow the unit class that spawns them. I've successfully spawned them and they obey their current conquest goal to the letter, but it's a pain having to use the follow command every single time, the AI never tell them to follow, and I plan to make the unit that spawns them a hero, which means I need them coded to follow anyway. There is only one of the class that spawns them on the battlefield at once. Ideally, I want them to follow whoever is spawned as that one unit of that class, be they AI or human.

Also, how do I modify that script to trigger when the character spawns rather than when they use a certain weapon? I tried something involving OnCharacterSpawn a few days ago, but my ineptitude at LUA coding defeated me.
THEWULFMAN
Space Ranger
Posts: 5557
Joined: Tue Aug 17, 2010 3:30 pm
Projects :: Evolved 2
Location: Columbus, Ohio
Contact:

Re: AI Goal: Follow Class

Post by THEWULFMAN »

your not going to believe this, but I was coming to GT to start a near indentical thread :shock:

no, like, really :shock:

Anyway, I am looking into this myself,because I want my 2 clones to follow their commander or captain.

I am looking into maybe trying to trick the AI into thinking you have a flag. Stand by.

still :shock: about this thread.
User avatar
Lagomorphia
Major
Major
Posts: 502
Joined: Sun Dec 13, 2009 10:02 am

Re: AI Goal: Follow Class

Post by Lagomorphia »

Probably because both ideas were spawned off of the idea I told you two days ago about those droid squads.
User avatar
DarthD.U.C.K.
Master of the Force
Master of the Force
Posts: 6027
Joined: Wed Sep 27, 2006 11:05 am
Location: Duckburg, Germany

Re: AI Goal: Follow Class

Post by DarthD.U.C.K. »

if you want them to follow the unit that spawned them:

Code: Select all

			Supportgoal = AddAIGoal (SupportTeam, "Follow", 100, GetEntityPtr(character))
put it in the "OnCharacterDispenseControllableTeam"-function

im not sure how to tell them to follow one class, im not sure if that possible.

edit: the last parameter doesnt work, im not sure which function to use but because the weapon is used primarily by the player you can just put a zero as alast argument and theyll follow him.
Last edited by DarthD.U.C.K. on Mon Apr 18, 2011 4:23 pm, edited 1 time in total.
User avatar
Lagomorphia
Major
Major
Posts: 502
Joined: Sun Dec 13, 2009 10:02 am

Re: AI Goal: Follow Class

Post by Lagomorphia »

Thanks!

By the way, if I give them a Conquest goal weighted at, say, 50, will they proceed with Conquest objectives after their 'master' dies?

EDIT: Where in the code do I put it?
Hidden/Spoiler:
TroopSpawnerWeapon = "cis_weap_inf_remotedroid_ord"

OnCharacterDispenseControllableTeam(
function(character,controlled)
if GetEntityClass(controlled) == GetEntityClassPtr(TroopSpawnerWeapon) then
local SupportTeam = 3 --Team the new spawned character is in (change this)
local teamSize = GetTeamSize(SupportTeam)
for i = 0, teamSize-1 do
local characterIndex = GetTeamMember(SupportTeam, i)
local charUnit = GetCharacterUnit(characterIndex)
if not charUnit then
local destination = GetEntityMatrix(GetCharacterUnit(character))
SpawnCharacter(characterIndex,destination)
end
end
end
end,
CIS --User team (change this)
)
User avatar
DarthD.U.C.K.
Master of the Force
Master of the Force
Posts: 6027
Joined: Wed Sep 27, 2006 11:05 am
Location: Duckburg, Germany

Re: AI Goal: Follow Class

Post by DarthD.U.C.K. »

you can put it under local supportteam.

about conquest: put a conquesgoal with a priority of 1 above/below the other goal
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: AI Goal: Follow Class

Post by [RDH]Zerted »

You can't tell the AI to follow a class, unless you add a new goal each time a unit of that class spawns (don't do that). What you want to do is have the AI follow the character's unit.
weapon is used primarily by the player you can just put a zero as alast argument and theyll follow him
Only in SP. The human's id is always zero in single-player, so the AI would always follow the human. If you want it to work in MP, use the character's unit.

Make sure to clean out old AI goals as the game has a max goal limit (19). Store the goal in a variable (followGoal = AddAIGoal(...)) and delete it whenever you change it (DeleteAIGoal(followGoal)). If you're always wiping out all of a team's AI goals, forget about the variable and just use ClearAIGoals(<team number>).

The AI attempts to do all AI goals. If there are 8 AI units, a Conquest goal of 75, a Follow goal of 25, and a Deathmatch goal of 100:
* 3 units doing Conquest (75 is 3/8th of the total goal count of 200)
* 1 unit doing Follow (25 is 1/8th of 200)
* 4 units doing Deathmatch (100 is half of 200 and 4 units are half of the 8 total units)
If you don't want AI units doing Conquest until after their master dies, then don't add that goal until after the master dies (and don't forget to delete/clear the follow master goal).
User avatar
Lagomorphia
Major
Major
Posts: 502
Joined: Sun Dec 13, 2009 10:02 am

Re: AI Goal: Follow Class

Post by Lagomorphia »

I sort of understand, but I'll need an example script if I'm going to get it working.
kinetosimpetus
Imperial Systems Expert
Imperial Systems Expert
Posts: 2381
Joined: Wed Mar 25, 2009 4:15 pm
Projects :: A secret project
Games I'm Playing :: Warframe STO

Re: AI Goal: Follow Class

Post by kinetosimpetus »

I wanted this a long time ago, so I'm really interested in how this turns out, and I'm writing up some pseudo-code right now that, once turned into actual code, should effectively make AI follow every unit that spawns as a certain class or classes.

EDIT:

Code: Select all

--AI Follow Officers -- pseudocode

local officerCount = 0

local officerUnit1
local officerUnit2
local officerUnit3
...
local officerUnit7
local officerUnit8

local followGoal1
...
local followGoal8

OnCharacterSpawn(character)

        if (character.getUnit().getUnitClass() == "rep_inf_ep2_officer") then
                addOfficer(character)
                return
        end

end

function addOfficer(character)

        if (officerCount == 0) then
                officerUnit1 = character.getUnit()
                followGoal1 = AddAIGoal(character.getTeam(), "follow", 50)
        elseif(officerCount == 2)
        ...
        end
        officerCount = officerCount+1

end

OnCharacterDeath(character)

        if (character.getUnit().getUnitClass() == "rep_inf_ep2_officer" then
                removeOfficer(character)
        end

end

function removeOfficer(character)

        If (character.getUnit() == officerUnit1) then
                DeleteAIGoal(followGoal1)
        Elseif...
        end

        officerCount = officerCount-1
        
        if followgoal1 == null then
                cleanupOfficers(1)
        End
        If followgoal2 == null then
                cleanupOffucers(2)
        end
        ...
        If followgoal7 == null then
                cleanupOffucers(7)
        end

end

function cleanupOfficers(index)

        While index is less than 8
                officerUnit<index> = officerUnit<index+1>
                Index = index + 1
        End

End
THEWULFMAN
Space Ranger
Posts: 5557
Joined: Tue Aug 17, 2010 3:30 pm
Projects :: Evolved 2
Location: Columbus, Ohio
Contact:

Re: AI Goal: Follow Class

Post by THEWULFMAN »

kinetosimpetus wrote:I wanted this a long time ago, so I'm really interested in how this turns out, and I'm writing up some pseudo-code right now that, once turned into actual code, should effectively make AI follow every unit that spawns as a certain class or classes.

EDIT:
Hidden/Spoiler:
--AI Follow Officers -- pseudocode

local officerCount = 0

local officerUnit1
local officerUnit2
local officerUnit3
...
local officerUnit7
local officerUnit8

local followGoal1
...
local followGoal8

OnCharacterSpawn(character)

        if (character.getUnit().getUnitClass() == "rep_inf_ep2_officer") then
                addOfficer(character)
                return
        end

end

function addOfficer(character)

        if (officerCount == 0) then
                officerUnit1 = character.getUnit()
                followGoal1 = AddAIGoal(character.getTeam(), "follow", 50)
        elseif(officerCount == 2)
        ...
        end
        officerCount = officerCount+1

end

OnCharacterDeath(character)

        if (character.getUnit().getUnitClass() == "rep_inf_ep2_officer" then
                removeOfficer(character)
        end

end

function removeOfficer(character)

        If (character.getUnit() == officerUnit1) then
                DeleteAIGoal(followGoal1)
        Elseif...
        end

        officerCount = officerCount-1
        
        if followgoal1 == null then
                cleanupOfficers(1)
        End
        If followgoal2 == null then
                cleanupOfficers(2)
        end
        ...
        If followgoal7 == null then
                cleanupOfficers(7)
        end

end

function cleanupOfficers(index)

        While index is less than 8
                officerUnit<index> = officerUnit<index+1>
                Index = index + 1
        End

End
Looks interesting enough to make it into my mod. I fixed the 2 spelling errors btw. I will test asap.(That includes getting it turned into a proper script)
kinetosimpetus
Imperial Systems Expert
Imperial Systems Expert
Posts: 2381
Joined: Wed Mar 25, 2009 4:15 pm
Projects :: A secret project
Games I'm Playing :: Warframe STO

Re: AI Goal: Follow Class

Post by kinetosimpetus »

Here's what I have now, untested, but it munged ok.
Hidden/Spoiler:
[code]

------------------------
-- AI Follow Officers --
------------------------

--kinetosimpetus 4/19/2011

local officerCount = 0
local officerMax = 8

local officerCharacter1
local officerCharacter2
local officerCharacter3
local officerCharacter4
local officerCharacter5
local officerCharacter6
local officerCharacter7
local officerCharacter8

local followGoal1
local followGoal2
local followGoal3
local followGoal4
local followGoal5
local followGoal6
local followGoal7
local followGoal8

officer_follow_spawn = OnCharacterSpawn(
function(character)

print("K15: Debug: officer_follow_spawn: character spawned")

if (GetUnitClass(GetCharacterUnit(character)) == "rep_inf_ep3_officer") then

print("K15: Debug: officer_follow_spawn: character is ARC Captain")

addOfficer(character)

elseif(GetUnitClass(GetCharacterUnit(character)) == "all_inf_officer") then

print("K15: Debug: officer_follow_spawn: character is Rebel Officer")

addOfficer(character)

elseif(GetUnitClass(GetCharacterUnit(character)) == "imp_inf_officer") then

print("K15: Debug: officer_follow_spawn: character is Imperial Officer")

addOfficer(character)

elseif(GetUnitClass(GetCharacterUnit(character)) == "cis_inf_engineer") then

print("K15: Debug: officer_follow_spawn: character is OOM Commander")

addOfficer(character)
end

end)

function addOfficer(character)

print("K15: Debug: addOfficer: entered")

if (officerCount == 0) then
officerCharacter1 = character
followGoal1 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 1) then
officerCharacter2 = character
followGoal2 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 2) then
officerCharacter3 = character
followGoal3 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 3) then
officerCharacter4 = character
followGoal4 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 4) then
officerCharacter5 = character
followGoal5 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 5) then
officerCharacter6 = character
followGoal6 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 6) then
officerCharacter7 = character
followGoal7 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

elseif(officerCount == 7) then
officerCharacter8 = character
followGoal8 = AddAIGoal(ATT, "Follow", 50, GetEntityPtr(character))

print("K15: Debug: addOfficer: goal added. count: "..(officerCount + 1))

else

print("K15: Debug: addOfficer: goal -NOT- added. count: "..(officerCount + 1))

end

officerCount = officerCount + 1

print("K15: Debug: addOfficer: leaving. count: "..officerCount)

end

officer_follow_death = OnCharacterDeath(
function(character)

print("K15: Debug: officer_follow_death: character died")

if (GetUnitClass(GetCharacterUnit(character)) == "rep_inf_ep3_officer") then

print("K15: Debug: officer_follow_death: character was ARC Captain")

removeOfficer(character)

end

if(GetUnitClass(GetCharacterUnit(character)) == "all_inf_officer") then

print("K15: Debug: officer_follow_death: character was Rebel Officer")

removeOfficer(character)

end

if(GetUnitClass(GetCharacterUnit(character)) == "imp_inf_officer") then

print("K15: Debug: officer_follow_death: character was Imperial Officer")

removeOfficer(character)

end

if(GetUnitClass(GetCharacterUnit(character)) == "cis_inf_engineer") then

print("K15: Debug: officer_follow_death: character was OOM Commander")

removeOfficer(character)
end

end)


function removeOfficer(character)

identified = 0

print("K15: Debug: removeOfficer: entered")

if (character == officerCharacter1) then

print("K15: Debug: removeOfficer: character was officer 1")

DeleteAIGoal(followGoal1)
officerCount = officerCount-1

identified = 1

end

if (character == officerCharacter2) then

print("K15: Debug: removeOfficer: character was officer 2")

DeleteAIGoal(followGoal2)
officerCount = officerCount-1

identified = 1

end

if (character == officerCharacter3) then

print("K15: Debug: removeOfficer: character was officer 3")

DeleteAIGoal(followGoal3)
officerCount = officerCount-1

identified = 1

end

if (character == officerCharacter4) then

print("K15: Debug: removeOfficer: character was officer 4")

DeleteAIGoal(followGoal4)
officerCount = officerCount-1

identified = 1

end

if (character == officerCharacter5) then

print("K15: Debug: removeOfficer: character was officer 5")

officerCount = officerCount-1
DeleteAIGoal(followGoal5)

identified = 1

end

if (character == officerCharacter6) then

print("K15: Debug: removeOfficer: character was officer 6")

DeleteAIGoal(followGoal6)
officerCount = officerCount-1

identified = 1

end

if (character == officerCharacter7) then

print("K15: Debug: removeOfficer: character was officer 7")

DeleteAIGoal(followGoal7)
officerCount = officerCount-1

identified = 1

end

if (character == officerCharacter8) then

print("K15: Debug: removeOfficer: character was officer 8")

DeleteAIGoal(followGoal8)
officerCount = officerCount-1

identified = 1

end

if (identified == 0) then
print("K15: Debug: removeOfficer: character unidentified as officer")
end


if followgoal1 == null then
cleanupOfficers(1)
end
if followgoal2 == null then
cleanupOffucers(2)
end
if followgoal3 == null then
cleanupOffucers(3)
end
if followgoal4 == null then
cleanupOffucers(4)
end
if followgoal5 == null then
cleanupOffucers(5)
end
if followgoal6 == null then
cleanupOffucers(6)
end
if followgoal7 == null then
cleanupOffucers(7)
end

print("K15: Debug: removeOfficer: leaving. count: "..officerCount)


end

function cleanupOfficers(index)

identified = 0

print("K15: Debug: cleanupOfficers: entered")

if(index == 1) then

print("K15: Debug: cleanupOfficers: shifting officer 2 into officer 1")

officerCharacter1 = officerCharacter2
followGoal1 = followGoal2

officerCharacter2 = null
followGoal2 = null

identified = 1

end
if (index == 2) then

print("K15: Debug: cleanupOfficers: shifting officer 3 into officer 2")

officerCharacter2 = officerCharacter3
followGoal2 = followGoal3

officerCharacter3 = null
followGoal3 = null

identified = 1

end
if (index == 3) then

print("K15: Debug: cleanupOfficers: shifting officer 4 into officer 3")

officerCharacter3 = officerCharacter4
followGoal3 = followGoal4

officerCharacter4 = null
followGoal4 = null

identified = 1

end
if (index == 4) then

print("K15: Debug: cleanupOfficers: shifting officer 5 into officer 4")

officerCharacter4 = officerCharacter5
followGoal4 = followGoal5

officerCharacter5 = null
followGoal5 = null

identified = 1

end
if (index == 5) then

print("K15: Debug: cleanupOfficers: shifting officer 6 into officer 5")

officerCharacter5 = officerCharacter6
followGoal5 = followGoal6

officerCharacter6 = null
followGoal6 = null

identified = 1

end
if (index == 6) then

print("K15: Debug: cleanupOfficers: shifting officer 7 into officer 6")

officerCharacter6 = officerCharacter7
followGoal6 = followGoal7

officerCharacter7 = null
followGoal7 = null

identified = 1

end
if (index == 7) then

print("K15: Debug: cleanupOfficers: shifting officer 8 into officer 7")

officerCharacter7 = officerCharacter8
followGoal7 = followGoal8

officerCharacter8 = null
followGoal8 = null

identified = 1

end
if (identified == 0) then
print("K15: Debug: cleanupOfficers: index not recognised, not shifting")
end

end

----------------------------
-- End AI Follow Officers --
----------------------------


[/code]
It's a little wordy...
It could be made smaller (and better in many other ways) if it was done in a more "Object Oriented" way, but idk how to do that with lua...
User avatar
DarthD.U.C.K.
Master of the Force
Master of the Force
Posts: 6027
Joined: Wed Sep 27, 2006 11:05 am
Location: Duckburg, Germany

Re: AI Goal: Follow Class

Post by DarthD.U.C.K. »

is there really nothing like an array in lua? that would really shrink the code.
kinetosimpetus
Imperial Systems Expert
Imperial Systems Expert
Posts: 2381
Joined: Wed Mar 25, 2009 4:15 pm
Projects :: A secret project
Games I'm Playing :: Warframe STO

Re: AI Goal: Follow Class

Post by kinetosimpetus »

I think there is, but idk how to use it.
THEWULFMAN
Space Ranger
Posts: 5557
Joined: Tue Aug 17, 2010 3:30 pm
Projects :: Evolved 2
Location: Columbus, Ohio
Contact:

Re: AI Goal: Follow Class

Post by THEWULFMAN »

Am I the only one who doesnt care about the length of their lua? I would be willing to put that in every one of my scipts, and more, oh wait, thats right, I am off the walls crazy dedicated to modding this game :runaway:

I am testing now, will edit the results in later
kinetosimpetus
Imperial Systems Expert
Imperial Systems Expert
Posts: 2381
Joined: Wed Mar 25, 2009 4:15 pm
Projects :: A secret project
Games I'm Playing :: Warframe STO

Re: AI Goal: Follow Class

Post by kinetosimpetus »

well it would be easier to modify and maintain if it were smaller, especially if you could have it in its own script and call it with 50 other scripts instead of making the same edits to 50 scripts if there was a bug or you just wanted to change something.


EDIT: doesn't work yet. debug log shows this...

Code: Select all

K15: Debug: officer_follow_spawn: character spawned

Message Severity: 3
.\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to call global `GetUnitClass' (a nil value)
stack traceback:
	(none): in function <(none):70>
User avatar
Lagomorphia
Major
Major
Posts: 502
Joined: Sun Dec 13, 2009 10:02 am

Re: AI Goal: Follow Class

Post by Lagomorphia »

Couldn't you have it as a seperate script that the LUA calls for, like AI heroes?
kinetosimpetus
Imperial Systems Expert
Imperial Systems Expert
Posts: 2381
Joined: Wed Mar 25, 2009 4:15 pm
Projects :: A secret project
Games I'm Playing :: Warframe STO

Re: AI Goal: Follow Class

Post by kinetosimpetus »

Yes, but I haven't successfully made that type of script.

Edit: It works now, kinda, the AI will follow each other.

However, I once saw a group of AI with one officer running around in circles, actually, there were 2 groups like that.

another thing, the debug output shows that the code does not run in sync, if 2 officers spawn really close together, they might both get an AIGoal even if the max is reached already (8 supposedly, if the code ran in sync) but afaik, there's no crashes right now unless it crashes when AI goals totally max out.

continuing testing...
Post Reply