//::///////////////////////////////////////////////
//:: NW_C2_DIMDOOR.nss
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
     Creature randomly hops around
     to enemies during combat.
*/
//:://////////////////////////////////////////////
//:: Created By:  Brent
//:: Created On:  January 2002
//:://////////////////////////////////////////////

void JumpToWeakestEnemy(object oTarget)
{
    object oTargetVictim = GetFactionMostDamagedMember(oTarget);
    // * won't jump if closer than 4 meters to victim
    if ((GetDistanceToObject(oTargetVictim) > 4.0)   && (GetObjectSeen(oTargetVictim) == TRUE))
    {
        ClearAllActions();
        effect eVis = EffectVisualEffect(VFX_FNF_SUMMON_UNDEAD);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, OBJECT_SELF);

//        SpeakString("Jump to " + GetName(oTargetVictim));
        DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, OBJECT_SELF));
        DelayCommand(0.3,ActionJumpToObject(oTargetVictim));
        DelayCommand(0.5,ActionAttack(oTargetVictim));
    }
}
void main()
{
    // * During Combat try teleporting around
    if (GetUserDefinedEventNumber() == 1003)
    {
        // * if random OR heavily wounded then teleport to next enemy
        if ((Random(100) < 50)  ||  ( (GetCurrentHitPoints() / GetMaxHitPoints()) * 100 < 50) )
        {
           JumpToWeakestEnemy(GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY));
        }
    }


  if (GetUserDefinedEventNumber() == 1006) // OnDamaged event
  {
    object oAttacker = GetLastDamager();
    object oPC = GetFactionLeader(oAttacker);
    int nMaxHP = GetMaxHitPoints();
    int nCurHP = GetCurrentHitPoints();
    // if at less than half hit points
    if (nCurHP < (nMaxHP / 10) && !(GetLocalInt(OBJECT_SELF, "fired") == 1))
    {
      object oFirstMember = GetFactionLeader(oAttacker);
      object oPartyMember = GetFirstFactionMember(oFirstMember, TRUE);

      ClearAllActions(TRUE);

      {
        while (GetIsObjectValid (oPartyMember))
            {
            object oPartyMemberArea = GetArea(oPartyMember);
            if (!GetIsDead (oPartyMember))// Dead members will be skipped
            {
            // Send the party member to the destination
            AssignCommand(oPartyMember, ClearAllActions(TRUE));
            SetIsTemporaryFriend(oPartyMember, OBJECT_SELF, FALSE);
            }
        oPartyMember = GetNextFactionMember (oFirstMember, TRUE); //Set oPartyMember to the next person in the party.
        }
    // Last but not least, send the PC who clicked in the first place.
    // The script will go here right away if the party only has one person in it.
    AssignCommand(oPC, ClearAllActions(TRUE));
    SurrenderToEnemies();
    SetIsTemporaryFriend(oPC, OBJECT_SELF, FALSE);
    ChangeFaction(OBJECT_SELF, oPC);
    SetLocalInt(OBJECT_SELF, "fired", 1);
    ActionStartConversation(oPC, "",FALSE, FALSE);

        object oMonster=GetFirstObjectInArea(GetArea(OBJECT_SELF));
        while (oMonster != OBJECT_INVALID)
        {
        if (GetTag(oMonster)=="specy")
        {
        DestroyObject(oMonster);
        }
        oMonster=GetNextObjectInArea(GetArea(OBJECT_SELF));
        }

      }
    }
  }

    if (GetUserDefinedEventNumber() == 1003) // OnCombat round - Double check incase above never worked
  {
    object oAttacker = GetLastDamager();
    object oPC = GetFactionLeader(oAttacker);
    int nMaxHP = GetMaxHitPoints();
    int nCurHP = GetCurrentHitPoints();
    // if at less than half hit points
    if (nCurHP < (nMaxHP / 10) && !(GetLocalInt(OBJECT_SELF, "fired") == 1))
    {
      object oFirstMember = GetFactionLeader(oAttacker);
      object oPartyMember = GetFirstFactionMember(oFirstMember, TRUE);

      ClearAllActions(TRUE);

      {
        while (GetIsObjectValid (oPartyMember))
            {
            object oPartyMemberArea = GetArea(oPartyMember);
            if (!GetIsDead (oPartyMember))// Dead members will be skipped
            {
            // Send the party member to the destination
            AssignCommand(oPartyMember, ClearAllActions(TRUE));
            SetIsTemporaryFriend(oPartyMember, OBJECT_SELF, FALSE);
            }
        oPartyMember = GetNextFactionMember (oFirstMember, TRUE); //Set oPartyMember to the next person in the party.
        }
    // Last but not least, send the PC who clicked in the first place.
    // The script will go here right away if the party only has one person in it.
    AssignCommand(oPC, ClearAllActions(TRUE));
    SurrenderToEnemies();
    SetIsTemporaryFriend(oPC, OBJECT_SELF, FALSE);
    ChangeFaction(OBJECT_SELF, oPC);
    SetLocalInt(OBJECT_SELF, "fired", 1);
    ActionStartConversation(oPC, "",FALSE, FALSE);

        object oMonster=GetFirstObjectInArea(GetArea(OBJECT_SELF));
        while (oMonster != OBJECT_INVALID)
        {
        if (GetTag(oMonster)=="specy")
        {
        DestroyObject(oMonster);
        }
        oMonster=GetNextObjectInArea(GetArea(OBJECT_SELF));
        }

      }
  }


    }
  }



