Author Sticky: Post Working Scripts Here
TheFirstBigW Posted: Thursday, 27 June 06:13AM
1. Script name: Improved henchman AI
2. What it does: a) When a henchman armed with a ranged weapon is damaged by an enemy wielding a melee weapon, this script causes the henchman to switch to a melee weapon. b) When a henchman is getting low on HP [I currently have it set to fire at 33.3% of their max HP], this script causes the henchman to run away from their attacker, hopefully long enough and far away enough to have time to heal themselves.
3. Notes: While these modifications are far from perfect and do not guarantee the survival of your henchman, I have found that they significantly increase my henchman's chances of making it out alive.

4. The script itself:

Section # 1: Open the standard henchman OnDamaged script [the script file named "nw_ch_ac6" in "Chapter1.nwm"], and then insert the following script just before the last right-brace (looks like a curly bracket) at the end of the file:
NWScript:
// * The following block of code has henchmen run away from their

    // *   attacker if their HP fall below 33.3% of max.

    float fHenchmanHPpctg;

    fHenchmanHPpctg = ( IntToFloat( GetCurrentHitPoints(OBJECT_SELF) ) / IntToFloat( GetMaxHitPoints(OBJECT_SELF) ) ) * 100;

    if(fHenchmanHPpctg < 33.3)

    {

        ClearAllActions();

        ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 );

        ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 );

        ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 );

        DelayCommand(1.5, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

        DelayCommand(1.5, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

        DelayCommand(1.5, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

        DelayCommand(3.0, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

        DelayCommand(3.0, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

        DelayCommand(3.0, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

    }

    // * The following block of code has henchmen switch to melee weapons if they

    // *   are using a ranged weapon and are attacked in melee combat.

    if(GetAssociateState( NW_ASC_USE_RANGED_WEAPON ))

    {

        if(GetNumberOfMeleeAttackers() > 0)

        {

            ClearAllActions();

            SetAssociateState( NW_ASC_USE_RANGED_WEAPON , FALSE);

            EquipAppropriateWeapons( GetMaster() );

            ActionAttack( GetLastAttacker() );

        }

    }



Section # 2: Open the standard henchman OnHeartbeat script [the script file named "nw_ch_ac1" in "Chapter1.nwm"], and then once again insert the following script just before the last right-brace at the end of the file:

NWScript:
// * The following block of code does a check every 6 seconds to have henchmen

    // *   run away from their attacker if their HP fall below 33.3% of max.

    if (GetLastAttacker() != OBJECT_INVALID)

    {

        float fHenchmanHPpctg;

        fHenchmanHPpctg = ( IntToFloat( GetCurrentHitPoints(OBJECT_SELF) ) / IntToFloat( GetMaxHitPoints(OBJECT_SELF) ) ) * 100;

        if(fHenchmanHPpctg < 33.3)

        {

            ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 );

            ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 );

            ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 );

            if(GetDistanceBetween( OBJECT_SELF, GetLastAttacker() ) < 20.0)

            {

                DelayCommand(1.5, ClearAllActions());

                DelayCommand(1.5, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

                DelayCommand(1.5, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

                DelayCommand(1.5, ActionMoveAwayFromObject( GetLastAttacker(), TRUE, 40.0 ));

            }

        }

    }