Author Sticky: Post Working Scripts Here
Layrinn Posted: Monday, 22 July 09:22PM
I got tired of Linu standing in the middle of a huge melee battle trying to fire her crossbow and getting whacked by a dozen attacks of opportunity. I'm sure this has been done before, but I modified the OnCombatRoundEnd script...

This update fixes a problem with NPCs already assigned to use melee weapons switching to ranged combat.

1. Script name: nw_ch_ac3.ncs

2. What it does: Step 1... Checks the distance between the NPC and the nearest enemy and whether or not that enemy is a valid target. Step 2... If the NPC has is currently using his/her ranged weapon and there is an enemy within 4 meters, he/she will temporarily switch to his/her melee weapon. Step 3... If the NPC is temporarily using his/her melee weapon and there is no longer a creature within 4 meters, he/she will switch back to his/her ranged weapon. The end result is that the NPC fires his/her ranged weapon as long as it's safe to do so. The NPC will switch to his/her melee weapon if the need arises and switch back when it's safe to do so. The original NPC state (use ranged weapon) is preserved. The NPC will announce when changes are being made. An NPC under orders to engage in melee combat will not switch to ranged combat.

3. Notes: The script works with custom modules as well as with the default module. In order to modify the standard NPC's behavior, place the compiled script in the \override directory. You must replace the nw_ch_ac3.ncs file. I would recommend making a back up of the original file.

4. The script:

NWScript:
//::///////////////////////////////////////////////

//:: Modified Associate: End of Combat Round

//:: NW_CH_AC3

//:: Original Code Copyright (c) 2001 Bioware Corp.

//:://////////////////////////////////////////////

/*

    Calls the end of combat script every round

*/

//:://////////////////////////////////////////////

//:: Original Code Created By: Preston Watamaniuk

//:: Original Code Created On: Oct 16, 2001

//:: Modified Code Created By: Eric Konizer  (Layrinn)

//:: Modified Code Created On: July 22, 2002

//:://////////////////////////////////////////////

#include "NW_I0_GENERIC"

void main()

{

    if(!GetSpawnInCondition(NW_FLAG_SET_WARNINGS))

    {

       DetermineCombatRound();

    }

    if(GetSpawnInCondition(NW_FLAG_END_COMBAT_ROUND_EVENT))

    {

        SignalEvent(OBJECT_SELF, EventUserDefined(1003));

    }

// Begin modified code

    object oNearestEnemy = GetNearestSeenOrHeardEnemy();

    if ( (GetIsObjectValid(oNearestEnemy))  && (GetDistanceBetween(OBJECT_SELF, oNearestEnemy) < 4.0))

    {

        if (!GetLocalInt(OBJECT_SELF, "nTempMelee") && GetAssociateState(NW_ASC_USE_RANGED_WEAPON))

        {

            SpeakString("They're getting too close!!  I need to use my melee weapon.");

            SetAssociateState(NW_ASC_USE_RANGED_WEAPON, FALSE);

            SetLocalInt(OBJECT_SELF, "nTempMelee", TRUE);

            ClearAllActions();

            ActionDoCommand(EquipAppropriateWeapons(GetMaster()));

            ActionDoCommand(DetermineCombatRound());

        }

    }

    else

    {

        if (GetLocalInt(OBJECT_SELF, "nTempMelee"))

        {

            SpeakString("I'll switch back to my ranged weapon now.");

            SetAssociateState(NW_ASC_USE_RANGED_WEAPON, TRUE);

            SetLocalInt(OBJECT_SELF, "nTempMelee", FALSE);

            ClearAllActions();

            EquipAppropriateWeapons(GetMaster());

            ActionDoCommand(DetermineCombatRound());

        }

    }

// End modified code

}