Page 1 of 1

Brave but dumb, the AI are. [Solved]

Posted: Fri Jun 19, 2009 9:04 pm
by bobfinkl
So I've been messing around with AI properties for a while trying to get them to do things that real soldiers would do, and I thought they would run away if enough of their comrades died right? So I went ahead and made a script to give them to "defend" an object outside the maps boundary, and whaddaya know they didn't run.

Here is my script:

Code: Select all

    ATT_ReinforcementCount = GetReinforcementCount(ATT)
    if ATT_ReinforcementCount < 50 then
        AddAIGoal(ATT, "Defend", 600, "Russian_retreat")
    end

    DEF_ReinforcementCount = GetReinforcementCount(DEF)
    if DEF_ReinforcementCount < 30 then
        AddAIGoal(DEF, "Defend", 200, "German_retreat")
    end
Nothing in the error log is there that wasn't there before, the "German_retreat" and "Russian_retreat" are objects on the map (a yavin object I placed underground and set to be on the side that would retreat to them) that I named that.

Thanks in advance.

Re: Brave but dumb, the AI are.

Posted: Fri Jun 19, 2009 10:01 pm
by Null_1138
Very ingenious, but I don't know why it wouldn't work. Would your answer lie in the Kashyyyk campaign script? Or was that retreat fudged?

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 1:10 am
by AQT
Most likely fudged; they all were killed and then respawned behind the fort.

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 4:24 am
by MercuryNoodles
This is just a suggestion, since I don't really do scripting, but maybe you should clear or delete the other AI goals for the team in question, so that the defend goal for the retreat carries all of the weight. As far as I can tell, so long as a goal has any weight, the AI will be assigned to that goal.

It also might be worth it to try changing the CP strategic values via the script as well. The docs don't seem to state specifically, but it appears these values determine what priorites are given to attacking and defending the individual CPs.

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 8:38 am
by bobfinkl
AQT wrote:Most likely fudged; they all were killed and then respawned behind the fort.
Actually it wasn't, the AI were given an objective to "Defend" the refinery objects and I'm trying to get the AI to do the same to objects outside the boundary.

MercuryNoodles I doubt the 2nd thing you suggested would work, but perhaps clearing the AI goals would give me a tip as to what the problem is.

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 8:47 am
by YaNkFaN
do the objects have health or are they just props i'm not sure AI defend just props (maybe they're smarter than you think) because if you were told to defend something that couldn't be destroyed or captured would you do it...

I'm trying to say that the only things in BF2 that have an AI defend goal attached to them is destructable things or flags (in ctf and 1flag) maybe possibly giving those objects (german retreat and russian retreat health's (probably a huge one like 1000000) the AI may be more responsive

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 8:56 am
by bobfinkl
That is probably true, but I've found the main source of the problem. I'm 80% sure that the problem is that you can't constantly get the reinforcement count of a team, and I have no way that I can think of that would trigger it to go off when it reaches the number of reinforcements.

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 11:30 am
by Frisbeetarian
Use OnCharacterDeath and check the reinforcements inside it.

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 12:20 pm
by bobfinkl
Sorry for my lack of knowledge I haven't scripted in a long time so I'm a bit rusty. Could you write out the code for me?

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 1:25 pm
by Fiodis
I'm only a newbie at this myself, but something along the lines of:

Code: Select all

OnCharacterDeath(
function (player, killer)
GetReinforcementCount (team)
end
)

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 3:52 pm
by bobfinkl
Still doesn't work, again nothing in the error log that wasn't there before.

Here is my code:
Hidden/Spoiler:
OnCharacterDeath(
function (player, killer)
ATT_ReinforcementCount = GetReinforcementCount(ATT)
end
)

if ATT_ReinforcementCount < 50 then
ClearAIGoals(ATT)
--AddAIGoal(ATT, "Defend", 600, "Russian_retreat")
end

OnCharacterDeath(
function (player, killer)
DEF_ReinforcementCount = GetReinforcementCount(DEF)
end
)

if DEF_ReinforcementCount < 30 then
ClearAIGoals(DEF)
--AddAIGoal(DEF, "Defend", 200, "German_retreat")
end

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 6:27 pm
by [RDH]Zerted
You want this:

Code: Select all

ATT_Retreat = OnCharacterDeath(
    function(player, killer)
        --only care when dead player is on the ATT team
        if GetCharacterTeam(player) ~= ATT then return end

        if GetReinforcementCount(ATT) < 50 then
           --tell ATT to retreat
           ClearAIGoals(ATT)
           AddAIGoal(ATT, "Defend", 600, "Russian_retreat")

           --stop watching this OnCharacterDeath so we don't waste game resources
           ReleaseOnCharacterDeath(ATT_Retreat)
        end
    end
)

DEF_Retreat = OnCharacterDeath(
    function(player, killer)
        --only care when dead player is on the DEF team
        if GetCharacterTeam(player) ~= DEF then return end

        if GetReinforcementCount(ATT) < 30 then
           --tell DEF to retreat
           ClearAIGoals(DEF)
           AddAIGoal(DEF, "Defend", 600, "German_retreat")

           --stop watching this OnCharacterDeath so we don't waste game resources
           ReleaseOnCharacterDeath(DEF_Retreat)
        end
    end
)
It may have typos, I didn't check it.

Your original problem was that your code was only running once. You have to put it in an OnCharacterDeath event callback so that every time someone dies, the code is run again.

The AI goal Follow should give a tighter/faster retreat if Defend isn't good enough.

You could add in a ShowMessageText("Run for your lives!", ATT) to tell the humans that the AI are retreating. It would go after adding the new AI goal.

Re: Brave but dumb, the AI are.

Posted: Sat Jun 20, 2009 7:56 pm
by bobfinkl
Thanks Zerted that worked, oh and just for anyone else who wants to get their AI to retreat you must have a invisible CP for the AI to retreat to.

Re: Brave but dumb, the AI are.

Posted: Mon Jun 22, 2009 4:09 pm
by jangoisbaddest
[RDH]Zerted wrote:The AI goal Follow should give a tighter/faster retreat if Defend isn't good enough.
Woah! Sorry to derail, but I was wondering about this kind of thing. How does this work? Do you specify, say, a path or planning arc for the AI to follow? What is the difference between this and "patrol"?

Re: Brave but dumb, the AI are.

Posted: Mon Jun 22, 2009 6:13 pm
by Frisbeetarian

Re: Brave but dumb, the AI are.

Posted: Mon Jun 22, 2009 6:32 pm
by [RDH]Zerted
bobfinkl wrote:...you must have a invisible CP for the AI to retreat to.
You shouldn't have to use a CP. Check that ScriptAIGoals.txt doc. It says you can use any game object pointer (gameObjectPtr) or any integer character index (integerChrIdx) for Defend. The pointers are the game objects and the index is the number you get as the character value for all those character event callbacks, such as OnCharacterSpawn.

Re: Brave but dumb, the AI are.

Posted: Mon Jun 22, 2009 6:47 pm
by bobfinkl
Odd, because once I switched it to a cp it worked and before it refused to. I'll try some other objects and see if they work.

Re: Brave but dumb, the AI are.

Posted: Mon Jun 22, 2009 7:42 pm
by [RDH]Zerted
Well if you're not over the CP limit, theres no problem in using another CP.

Re: Brave but dumb, the AI are.

Posted: Mon Jun 22, 2009 8:21 pm
by bobfinkl
Then I guess this is solved then, admins go ahead and lock this.