Author Topic: Animal Companion AI Scripts  (Read 1274 times)

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« on: October 27, 2013, 01:24:12 am »


               I'm having trouble altering the animal companion scripts.

Specifically, I'm editing ac5, ac6, and ac8 (Attacked, Damaged, and Disturbed) and compiling them/recompiling them/rebuilding the whole doesn't seem to make any changes.  Originally I couldn't even get a debugging string to go off at the start of each script.

Then (and I still have no idea HOW I managed to do this) the string magically started going off.  But when I then changed the string to be specific to the individual script I'm back in the same problem of it not having any effect (it's "stuck" on the original identical debugging strings).

Aka, originally, nothing would go off.

Now it will finally say (somehow): "Firing OnAttacked."

But it won't update to "Firing OnAttacked"/"Firing OnDamaged"/"Firing OnDisturbed" now.

Am I missing something obvious here?
               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #1 on: October 27, 2013, 04:59:44 pm »


               Are you using new PCs or old PCs?
It could be that you are having trouble with a character file identifying scripts by means of the ID the compiler uses (not sure if this is true, but it is worth checking).

EDIT: Looking at a few character files, it does not look like there is a field for the familiar/animal companions script.  The only data I see are for a name and the type of creature.
               
               

               


                     Modifié par WhiZard, 27 octobre 2013 - 05:18 .
                     
                  


            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #2 on: October 27, 2013, 05:58:14 pm »


               After doing some more testing, both have the same issue.  Additionally, I can consistently get OnAttacked to fire (and can change what happens in OnAttacked) -- but OnAttacked appears to fire for at least OnDamaged as well (swing at the companion and hit it and one message appears when the swing starts and another when the swing hits).

Why would OnAttacked fire for OnDamaged for Animal Companions?

The reason I'm fiddling with these at all is to improve the controllability of the companions with a script I'm using.  Trying to make it so that if I tell the companion to attack a certain creature that its AI doesn't make it change its mind -- setting a local int for a short time period when it is ordered to attack and then trying to make the AI script simply return if the local int is detected.

Basically a way of trying to say "I don't care what you think you want to do, just do what I just told you to do."
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #3 on: October 27, 2013, 06:34:11 pm »


               Wondering if I might be able to use SetCommandable for this...I'll probably fiddle with it a bit.
               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #4 on: October 27, 2013, 06:43:25 pm »


               From the templates, ac5 appears to serve for both OnAttacked and OnDamaged for all animal companions and familiars
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #5 on: October 27, 2013, 08:17:12 pm »


               Interesting.  Is there an easier/better way than trying to adjust those scripts or trying to use SetCommandable to make it so animal companions won't change their target once they get it initially?  Like "I don't care if that tank with a tower shield swung at you, I told you to go for the ARCHER!"
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #6 on: October 28, 2013, 06:11:46 am »


               So, I discovered something interesting with SetCommandable.  OnSpellCast at always automatically sets Commandable to true.  This means either that behavior has to be edited or another method must be used.

I decided to use another method -- basically detects whether the master has commanded the companion within the last nine seconds (round and a half) and if so the relevant AI scripts simply immediately return.

Currently have this applied to...

OnAttacked
OnHeartbeat
OnPerceived
OnSpellCastAt
OnCombatEnd
OnDialogue

I also set it up for OnDamaged but obviously this is irrelevant for the Animal Companion specifically since it uses the OnAttacked script twice.  I may be avoid to avoid returning on some of those scripts but I played it safe as it doesn't seem to hurt anything and all were firing in combat and appear to have the potential to change the behavior of the creature.

This method appears to have solved my issues -- effectively shuts the AI combat decision making down for nine seconds each time the player gives a command and allows a LOT of custom control over the pet.

This can obviously be applied to any henchmen/animal companion/familiar that isn't reliant on spells or other special abilities quite easily.  Probably would be possible to script something and tell casters to cast a spell instead of attack.  But thought people might find it interesting.
               
               

               


                     Modifié par MagicalMaster, 28 octobre 2013 - 06:12 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #7 on: October 28, 2013, 08:14:28 pm »


               Perhaps it would be simpler to just add a  SPECIAL_COMBAT_AI_SCRIP


AI Behavior Priority  
               
               

               


                     Modifié par Lightfoot8, 28 octobre 2013 - 08:16 .
                     
                  


            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #8 on: October 28, 2013, 08:23:04 pm »


               Well, effectively seems I took the four lines of code (including two lines of curly braces) that would go into the special combat AI script and placed them in the other scripts themselves.  So instead of the special AI script firing and preventing the other AI scripts from firing it fires the individual scripts then stops.

Which means my question would be, what's more efficient?  Is calling the special AI script less work for the computer than calling the normal scripts but simply stopping?
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #9 on: October 28, 2013, 08:52:14 pm »


               

MagicalMaster wrote...

Well, effectively seems I took the four lines of code (including two lines of curly braces) that would go into the special combat AI script and placed them in the other scripts themselves.  So instead of the special AI script firing and preventing the other AI scripts from firing it fires the individual scripts then stops.

Which means my question would be, what's more efficient?  Is calling the special AI script less work for the computer than calling the normal scripts but simply stopping?

special AI is more efficient if you are ok with short answer, explaining why could take hours...
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #10 on: October 28, 2013, 09:27:40 pm »


               I'm okay with it.  I've just been told that, for example, a psuedo-heartbeat takes up much more resources than a standard heartbeat and I didn't know if a similar issue would apply here.

Assuming there are no other issues, is there any downside to using that special AI?
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #11 on: October 29, 2013, 12:15:03 am »


               I tried this and had issues.  Specifically, the animal companion, even once the AI was supposed the reactivate, would not seek out new targets (would respond if attacked).

Here's the code I used for the new script:


void main()
{
        // Don't do anything if we have have been recently commanded
        if (GetLocalInt(OBJECT_SELF, "commandstatus"))
        {
                SetLocalInt(OBJECT_SELF,"X2_SPECIAL_COMBAT_AI_SCRIPT_OK",TRUE);
        }
}

               
               

               


                     Modifié par MagicalMaster, 29 octobre 2013 - 12:15 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #12 on: October 29, 2013, 01:59:26 pm »


               keep inmind that the script is ran at the top of the core combat function DetermineCombatRound

it basicly stops the core AI when X2_SPECIAL_COMBAT_AI_SCRIPT_OK is set.   So within the script you need to tell the creature what to do if the flag is set.  

void main()
{
    // Don't do anything if we have have been recently commanded
    if (GetLocalInt(OBJECT_SELF, "commandstatus"))
    {
        ActoinAttack( ??);
         SetLocalInt(OBJECT_SELF,"X2_SPECIAL_COMBAT_AI_SCRIPT_OK",TRUE);
    }
}
Allong with whatever checks you want to use to make sure the Target is still valid.    

I Have many times just called DetermineCombatRound again with a direct attack target.  In doing that though you will need to place controls so that the functiont does not just keep calling itself and creating an endless loop.
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #13 on: October 29, 2013, 04:11:56 pm »


               The creature has already BEEN told what to do -- all I'm trying to do is prevent it from changing its mind when the player does not want it to.

But it seemed to suppress the AI even when commandstatus was 0 and thus the normal AI should have run.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Animal Companion AI Scripts
« Reply #14 on: October 29, 2013, 04:53:33 pm »


               ok, Just to make sure we are on the same page.

The script above is not on any EVENT.
it is connected to the creature by setting a local on the creature. i.e.
SetLocalString(OBJECT_SELF,"X2_SPECIAL_COMBAT_AI_SCRIPT", "Name of AI script" );

Is that how you have it set up?

AND for the record the function DetermineCombatRound  runs at least once every combat round.   Many time it runs more then once.
               
               

               


                     Modifié par Lightfoot8, 29 octobre 2013 - 04:55 .