#include "x2_inc_itemprop"
#include "inc_baseitem"

object ArenaSummon(string sTemplate)
{
    // do the summon
    object oOrb = GetObjectByTag("CS_ORBSUMMON");
    object oCreature = CreateObject(OBJECT_TYPE_CREATURE, sTemplate, GetLocation(GetObjectByTag("WP_ARENASUMMON")));
    SendMessageToPC(GetPCSpeaker(), "Summoned " + GetName(oCreature) + ".");

    //visual effects
    location lOrbLocation = GetLocation(oOrb);
    effect eOrbEffect = EffectVisualEffect(VFX_FNF_DISPEL);
    ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eOrbEffect, lOrbLocation, 0.5);

    location lCreatureLocation = GetLocation(oCreature);
    effect eCreatureEffect = EffectVisualEffect(VFX_FNF_SMOKE_PUFF);
    ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY, eCreatureEffect, lCreatureLocation, 1.0);

    return oCreature;
}

int RandomDamageType()
{
    //pick a random damage type
    int iDamageType;
    int iSwitch = Random(6);
    if (iSwitch == 0) iDamageType = IP_CONST_DAMAGETYPE_ACID;
    if (iSwitch == 1) iDamageType = IP_CONST_DAMAGETYPE_COLD;
    if (iSwitch == 2) iDamageType = IP_CONST_DAMAGETYPE_ELECTRICAL;
    if (iSwitch == 3) iDamageType = IP_CONST_DAMAGETYPE_FIRE;
    if (iSwitch == 4) iDamageType = IP_CONST_DAMAGETYPE_MAGICAL;
    if (iSwitch == 5) iDamageType = IP_CONST_DAMAGETYPE_SONIC;
    return iDamageType;
}

void EquipNPC(object oNPC, int iLevel)
//assigns equipment to NPC based on level
{
    object oWeapon = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oNPC);
    object oArmor = GetItemInSlot(INVENTORY_SLOT_CHEST, oNPC);
    object oOffHand = GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oNPC);

    int iWeaponBonus = iLevel/3;
        if (iWeaponBonus > 12) iWeaponBonus = 12;
    int iArmorBonus = (iLevel+2)/4;
    int iOffhandBonus = iLevel/4;

    int iDamageType = RandomDamageType();

    //apply main weapon bonuses
    IPSafeAddItemProperty(oWeapon, ItemPropertyAttackBonus(iWeaponBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);
    if (isMelee(oWeapon))
        IPSafeAddItemProperty(oWeapon, ItemPropertyDamageBonus(iDamageType, iWeaponBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);
    else
    {
        object oAmmo = GetItemInSlot(INVENTORY_SLOT_ARROWS, oNPC);
        if (GetObjectType(oAmmo)!= OBJECT_TYPE_INVALID)
            IPSafeAddItemProperty(oAmmo, ItemPropertyDamageBonus(iDamageType, iWeaponBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);

        oAmmo = GetItemInSlot(INVENTORY_SLOT_BOLTS, oNPC);
        if (GetObjectType(oAmmo)!= OBJECT_TYPE_INVALID)
            IPSafeAddItemProperty(oAmmo, ItemPropertyDamageBonus(iDamageType, iWeaponBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);

        oAmmo = GetItemInSlot(INVENTORY_SLOT_BULLETS, oNPC);
        if (GetObjectType(oAmmo)!= OBJECT_TYPE_INVALID)
            IPSafeAddItemProperty(oAmmo, ItemPropertyDamageBonus(iDamageType, iWeaponBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);
    }

    //apply armor bonus
    IPSafeAddItemProperty(oArmor, ItemPropertyACBonus(iArmorBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);

    //apply shield or offhand weapon bonus
    if (GetObjectType(oOffHand)!= OBJECT_TYPE_INVALID)
    {
        if (isArmor(oOffHand))
            IPSafeAddItemProperty(oOffHand, ItemPropertyACBonus(iOffhandBonus), 0.0, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, TRUE, TRUE);
        if (isMelee(oOffHand))
            IPSetWeaponEnhancementBonus(oOffHand, iOffhandBonus);
    }

    //AssignCommand(oNPC, SpeakString("Weapon bonus = " + IntToString(iWeaponBonus), TALKVOLUME_SHOUT));
    //AssignCommand(oNPC, SpeakString("Armor bonus = " + IntToString(iArmorBonus), TALKVOLUME_SHOUT));
    //AssignCommand(oNPC, SpeakString("Offhand bonus = " + IntToString(iOffhandBonus), TALKVOLUME_SHOUT));
}

void main()
{
    object oPC = GetPCSpeaker();
    object oCreature = ArenaSummon("cs1_fighter");
    int iPCLevel = GetLevelByPosition(1, oPC) + GetLevelByPosition(2, oPC) + GetLevelByPosition(3, oPC);
    int iLevelUp = 1;

    while (iLevelUp < iPCLevel)
    {
        iLevelUp = LevelUpHenchman(oCreature, CLASS_TYPE_FIGHTER, TRUE);
    }

    EquipNPC(oCreature, iLevelUp);

    SendMessageToPC(oPC, GetName(oCreature) + "'s level = " + IntToString(iLevelUp));
}




