//::///////////////////////////////////////////////
//:: Name x2_def_spawn
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    Default On Spawn script


    2003-07-28: Georg Zoeller:

    If you set a ninteger on the creature named
    "X2_USERDEFINED_ONSPAWN_EVENTS"
    The creature will fire a pre and a post-spawn
    event on itself, depending on the value of that
    variable
    1 - Fire Userdefined Event 1510 (pre spawn)
    2 - Fire Userdefined Event 1511 (post spawn)
    3 - Fire both events

    2007-12-31: Deva Winblood
    Modified to look for X3_HORSE_OWNER_TAG and if
    it is defined look for an NPC with that tag
    nearby or in the module (checks near first).
    It will make that NPC this horse's master.

*/
//:://////////////////////////////////////////////
//:: Created By: Keith Warner, Georg Zoeller
//:: Created On: June 11/03
//:://////////////////////////////////////////////

const int EVENT_USER_DEFINED_PRESPAWN = 1510;
const int EVENT_USER_DEFINED_POSTSPAWN = 1511;


#include "x4_inc_functions"
#include "x2_inc_switches"
#include "x0_i0_henchman"

void main()
{
    string sTag;
    object oNPC;
    // User defined OnSpawn event requested?
    int nSpecEvent = GetLocalInt(OBJECT_SELF,"X2_USERDEFINED_ONSPAWN_EVENTS");


    // Pre Spawn Event requested
    if (nSpecEvent == 1  || nSpecEvent == 3  )
    {
    SignalEvent(OBJECT_SELF,EventUserDefined(EVENT_USER_DEFINED_PRESPAWN ));
    }

    sTag=GetLocalString(OBJECT_SELF,"X3_HORSE_OWNER_TAG");
    if (GetStringLength(sTag)>0)
    { // look for master
        oNPC=GetNearestObjectByTag(sTag);
        if (GetIsObjectValid(oNPC)&&GetObjectType(oNPC)==OBJECT_TYPE_CREATURE)
        { // master found
            AddHenchman(oNPC);
        } // master found
        else
        { // look in module
            oNPC=GetObjectByTag(sTag);
            if (GetIsObjectValid(oNPC)&&GetObjectType(oNPC)==OBJECT_TYPE_CREATURE)
            { // master found
                AddHenchman(oNPC);
            } // master found
            else
            { // master does not exist - remove X3_HORSE_OWNER_TAG
                DeleteLocalString(OBJECT_SELF,"X3_HORSE_OWNER_TAG");
            } // master does not exist - remove X3_HORSE_OWNER_TAG
        } // look in module
    } // look for master

    /*  Fix for the new golems to reduce their number of attacks */

    int nNumber = GetLocalInt(OBJECT_SELF,CREATURE_VAR_NUMBER_OF_ATTACKS);
    if (nNumber >0 )
    {
        SetBaseAttackBonus(nNumber);
    }

    //If the creature is a Hive Mother - spawn two hive guardians
    if (GetTag(OBJECT_SELF) == "anc_beetlemother")
        {
            CreateObject(OBJECT_TYPE_CREATURE, "anc_hiveguardian", GetLocation(OBJECT_SELF), TRUE);
            CreateObject(OBJECT_TYPE_CREATURE, "anc_hiveguardian", GetLocation(OBJECT_SELF), TRUE);
        }

    //Make it a herbivore if appropriate
    if (GetLocalInt(OBJECT_SELF, "Herbivore") == TRUE)
    SetBehaviorState(NW_FLAG_BEHAVIOR_HERBIVORE);

    //Apply penalties/bonuses based on world difficulty
    string sDifficulty = GetLocalString(GetModule(), "mDiffEnemy");
    if (sDifficulty != "ADVENTURER" && GetLevelByClass(CLASS_TYPE_COMMONER) == 0)
    {
        effect eImmunity;
        if (sDifficulty == "COMMONER")
        {
            eImmunity = EffectDamageImmunityDecrease(DAMAGE_TYPE_SLASHING, 25);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_PIERCING, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_BLUDGEONING, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_ACID, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_COLD, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_DIVINE, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_ELECTRICAL, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_FIRE, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_MAGICAL, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_NEGATIVE, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_POSITIVE, 25), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityDecrease(DAMAGE_TYPE_SONIC, 25), eImmunity);
            eImmunity = SupernaturalEffect(eImmunity);
        }
        else
        {
            int nValue;
            if (sDifficulty == "HERO") nValue = 10;
            if (sDifficulty == "LEGEND") nValue = 25;
            if (sDifficulty == "DEMIGOD") nValue = 50;

            eImmunity = EffectDamageImmunityIncrease(DAMAGE_TYPE_SLASHING, nValue);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_PIERCING, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_BLUDGEONING, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_ACID, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_COLD, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_DIVINE, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_ELECTRICAL, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_FIRE, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_MAGICAL, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_NEGATIVE, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_POSITIVE, nValue), eImmunity);
            eImmunity = EffectLinkEffects(EffectDamageImmunityIncrease(DAMAGE_TYPE_SONIC, nValue), eImmunity);
            eImmunity = SupernaturalEffect(eImmunity);
        }
        ApplyEffectToObject(DURATION_TYPE_PERMANENT, eImmunity, OBJECT_SELF);
    }

    //Random walk
    if (GetLocalInt(OBJECT_SELF, "Walk") == TRUE) ActionRandomWalk();

    //Generate loot if appropriate
    if (GetLocalString(OBJECT_SELF, "RLOOT") != "")
    {
        int nChance = GetLocalInt(OBJECT_SELF, "RCHANCE");
        if (Random(100)+1 <= nChance)
        {
            object oLoot = CreateItemOnObject(GetLocalString(OBJECT_SELF, "RLOOT"), OBJECT_SELF);
            SetDroppableFlag(oLoot, TRUE);
        }
    }

    //If the ANC_MARKS switch is set to TRUE, mark the NPC with a blue exclamation VFX
    effect eExclamation = EffectVisualEffect(VFX_EXCLAIM_RED, FALSE);
    if (GetLocalInt(GetModule(), "ANC_MARKS") == TRUE) ApplyEffectToObject(DURATION_TYPE_PERMANENT, eExclamation, OBJECT_SELF);

    // Execute default OnSpawn script.
    if(GetLocalInt(GetModule(), "X4_VANILLA_AI") == TRUE) SetLocalInt(OBJECT_SELF, "X4_VANILLA_AI", TRUE);
    if(GetLocalInt(OBJECT_SELF, "X4_VANILLA_AI") == TRUE) ExecuteScript("nw_c2_default9_v", OBJECT_SELF);
    else ExecuteScript("nw_c2_default9", OBJECT_SELF);

    //Post Spawn event requeste
    if (nSpecEvent == 2 || nSpecEvent == 3)
    {
    SignalEvent(OBJECT_SELF,EventUserDefined(EVENT_USER_DEFINED_POSTSPAWN));
    }

}
