#include "x3_inc_horse"

////////////////////////////////////////////////////////////////////////////////
int GOLD_REWARD_AMOUNT = 0;
int XP_REWARD_AMMOUNT = 0;
////////////////////////////////////////////////////////////////////////////////


object oPC1 = GetLocalObject(GetModule(),"Player1" );
object oPC2 = GetLocalObject(GetModule(),"Player2" );
int GetArmorType(object oItem)
{
    // Make sure the item is valid and is an armor.
    if (!GetIsObjectValid(oItem))
        return -1;
    if (GetBaseItemType(oItem) != BASE_ITEM_ARMOR)
        return -1;

    // Get the identified flag for safe keeping.
    int bIdentified = GetIdentified(oItem);
    SetIdentified(oItem,FALSE);

    int nType = -1;
    switch (GetGoldPieceValue(oItem))
    {
        case    1: nType = 0; break; // None
        case    5: nType = 1; break; // Padded
        case   10: nType = 2; break; // Leather
        case   15: nType = 3; break; // Studded Leather / Hide
        case  100: nType = 4; break; // Chain Shirt / Scale Mail
        case  150: nType = 5; break; // Chainmail / Breastplate
        case  200: nType = 6; break; // Splint Mail / Banded Mail
        case  600: nType = 7; break; // Half-Plate
        case 1500: nType = 8; break; // Full Plate
    }
    // Restore the identified flag, and return armor type.
    SetIdentified(oItem,bIdentified);
    return nType;
}

void DealDamage(object oLoser, int nSerious)
{
    int nProtected = TRUE;
    if (GetBaseItemType(GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oLoser)) != BASE_ITEM_LARGESHIELD &&
        GetBaseItemType(GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oLoser)) != BASE_ITEM_SMALLSHIELD &&
        GetBaseItemType(GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oLoser)) != BASE_ITEM_TOWERSHIELD)
        nProtected = FALSE;
    if (GetArmorType(GetItemInSlot(INVENTORY_SLOT_HEAD, oLoser)) != BASE_ITEM_HELMET)
        nProtected = FALSE;

    int nDamage = GetMaxHitPoints(oLoser)/5;
    if (nSerious == TRUE) nDamage = GetMaxHitPoints(oLoser)/2;
    effect eDamage = EffectDamage(nDamage, DAMAGE_TYPE_POSITIVE);
    effect ePenalty = EffectSkillDecrease(SKILL_RIDE, 2);
    ePenalty = SupernaturalEffect(ePenalty);
    if (nProtected == FALSE)
    {
        if (nSerious == TRUE) ApplyEffectToObject(DURATION_TYPE_TEMPORARY, ePenalty, oLoser, 600.0);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oLoser);
    }
}

void GiveRide()
{
    int nRide = GetLocalInt(OBJECT_SELF, "RIDE");
    ApplyEffectToObject(DURATION_TYPE_PERMANENT, EffectSkillIncrease(SKILL_RIDE, nRide), OBJECT_SELF);
}

void EndCutscene(object oPlayer)
{
DelayCommand(9.0 , FadeToBlack(oPlayer , FADE_SPEED_FAST));
DelayCommand(12.0 , StopFade(oPlayer));
DelayCommand(12.0 , SetCutsceneMode(oPlayer, FALSE));
DelayCommand(12.0 , SetLocalObject(GetModule(), "Player1" , OBJECT_INVALID));
DelayCommand(12.0 , SetLocalObject(GetModule(), "Player2" , OBJECT_INVALID));
DelayCommand(12.0 , HorseSetPhenotype(oPlayer, FALSE));
}

void BreakWeapon(object oPlayer)
{
int BreakRoll = d3(1);
if (BreakRoll == 3)
    {
    /*string LanceTag = GetTag(GetItemInSlot(INVENTORY_SLOT_RIGHTHAND ,oPC1));
    object MyLance = GetItemInSlot( INVENTORY_SLOT_RIGHTHAND, oPC1);
    object oItem = CopyItemAndModify( MyLance ,ITEM_APPR_TYPE_WEAPON_MODEL, ITEM_APPR_WEAPON_MODEL_TOP, 2);
    AssignCommand(oPlayer , ActionEquipItem(oItem , INVENTORY_SLOT_RIGHTHAND));
    DestroyObject(MyLance);
    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(621), oPlayer);
    FloatingTextStringOnCreature("Your Lance Shattered!",oPlayer);*/
    }
}
void main()
{
object oPC = GetPCSpeaker();

GiveRide();

if ( GetLocalObject(GetModule(), "Player1") != OBJECT_INVALID)
{ FloatingTextStringOnCreature("There is already someone signed up for a joust! Please wait until they are done." , oPC);
  return;}

    //Make sure the joust dosent start without logical items equpped
    if (HorseGetIsMounted(oPC)!= TRUE)
        {FloatingTextStringOnCreature("You must be mounted to joust!", oPC);
         return;}
    if (GetBaseItemType(GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC)) != 92)
        {FloatingTextStringOnCreature("You must have a lance equipped to joust!", oPC);
         return;}
    if (GetItemAppearance(GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC), ITEM_APPR_TYPE_WEAPON_MODEL, ITEM_APPR_WEAPON_MODEL_TOP) == 2)
        {FloatingTextStringOnCreature("You cannot joust with a broken lance!", oPC);
         return;}

    //Make sure we have jousting in this area before jousting.
    if (GetObjectByTag("RedStart") == OBJECT_INVALID)
        {FloatingTextStringOnCreature("There is no jousting in this Area", oPC);
         return;}
    else if (GetObjectByTag("RedDecide") == OBJECT_INVALID)
        {FloatingTextStringOnCreature("There is no jousting in this Area", oPC);
         return;}
    else if (GetObjectByTag("RedEnd") == OBJECT_INVALID)
        {FloatingTextStringOnCreature("There is no jousting in this Area", oPC);
         return;}
    else if (GetObjectByTag("BlueStart") == OBJECT_INVALID)
        {FloatingTextStringOnCreature("There is no jousting in this Area", oPC);
         return;}
    else if (GetObjectByTag("BlueDecide") == OBJECT_INVALID)
        {FloatingTextStringOnCreature("There is no jousting in this Area", oPC);
         return;}
    else if (GetObjectByTag("BlueEnd") == OBJECT_INVALID)
        {FloatingTextStringOnCreature("There is no Jousting in this Area", oPC);
         return;}

if (GetLocalObject(GetModule(),"Player1" ) == OBJECT_INVALID && GetLocalObject(GetModule(),"Player2") == OBJECT_INVALID)
{SetLocalObject(GetModule(), "Player2" , OBJECT_SELF);
SetLocalObject(GetModule(), "Player1" , oPC);}

//Refresh the Variables... or the PLAYERS in this matter.
            object oPC1 = GetLocalObject(GetModule(),"Player1" );
            object oPC2 = GetLocalObject(GetModule(),"Player2" );

            //Start The Cutscene:
            int PC1Ride = GetSkillRank(SKILL_RIDE, oPC1);
            int PC1Roll = d20() + PC1Ride;
            int PC2Ride = GetSkillRank(SKILL_RIDE, oPC2);
            int PC2Roll = d20() + PC2Ride;

            //Set the Jousting Position
            HorseSetPhenotype(oPC2, TRUE);
            HorseSetPhenotype(oPC1, TRUE);

            //Set the players up for the Joust
            AssignCommand(oPC1, JumpToObject(GetObjectByTag("RedStart")));
            AssignCommand(oPC1, SetFacingPoint(GetPosition(GetObjectByTag("RedDecide"))));
            AssignCommand(oPC2, JumpToObject(GetObjectByTag("BlueStart")));
            AssignCommand(oPC2, SetFacingPoint(GetPosition(GetObjectByTag("BlueDecide"))));

            //Take a random "Shatter" effect for the Lance:


            //Handle The Red Player's Animations
            //Script By: Patrick Lobre
            //PC 1

            FadeFromBlack(oPC1);
            SetCutsceneMode(oPC1, TRUE);
            AssignCommand(oPC1, ActionWait(4.0f));
            AssignCommand(oPC1, ActionMoveToObject(GetObjectByTag("RedDecide"),TRUE));

            if (PC1Roll <= PC2Roll)
            {
                if (PC2Roll - PC1Roll >= 10) //Loose by 10 or more
                    {AssignCommand(oPC1, ActionPlayAnimation(HORSE_ANIMATION_LOOPING_JOUST_VIOLENT_FALL, 1.0f, 3.0f));
                     DelayCommand(8.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(671), oPC1));}
                else if (PC2Roll - PC1Roll <=  5) //Loose by 5 or less
                    {AssignCommand(oPC1, ActionPlayAnimation(HORSE_ANIMATION_LOOPING_JOUST_GLANCE, 1.0f, 3.0f));
                     DelayCommand(8.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(672), oPC1));}
                else if (PC2Roll - PC1Roll <=  9) //Loose by 9 or less
                    {AssignCommand(oPC1, ActionPlayAnimation(HORSE_ANIMATION_LOOPING_JOUST_HELMOFF, 1.0f, 3.0f));
                     DelayCommand(8.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(671), oPC1));}

            }
            else
            {
                AssignCommand(oPC1, ActionMoveToObject(GetObjectByTag("RedEnd"),TRUE));
                //Only break the lance on a heavy hit:
                if (PC1Roll - PC2Roll > 5) DelayCommand(8.0, BreakWeapon(oPC1));
                GiveXPToCreature(oPC1 , XP_REWARD_AMMOUNT);
                GiveGoldToCreature(oPC1 , GOLD_REWARD_AMOUNT);
            }

            SendMessageToPC(oPC1, "Your roll was " + IntToString(PC1Roll)+ ". Your opponent's roll was " + IntToString(PC2Roll));
            EndCutscene(oPC1);
            DelayCommand(11.0 , AssignCommand(oPC1, JumpToObject(GetObjectByTag("RedEnd"))));


            //Handle The Blue Player's Animations
            //Script By: Patrick Lobre
            //PC 2

            FadeFromBlack(oPC2);
            SetCutsceneMode(oPC2, TRUE);
            AssignCommand(oPC2, ActionWait(4.0f));
            AssignCommand(oPC2, ActionMoveToObject(GetObjectByTag("BlueDecide"),TRUE));

            if (PC2Roll <= PC1Roll)
            {
                if (PC1Roll - PC2Roll >= 10) //Loose by 10 or more
                    {AssignCommand(oPC2, ActionPlayAnimation(HORSE_ANIMATION_LOOPING_JOUST_VIOLENT_FALL, 1.0f, 3.0f));
                     DelayCommand(8.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(671), oPC2));}
                else if (PC1Roll - PC2Roll <=  5) //Loose by 5 or less
                    {AssignCommand(oPC2, ActionPlayAnimation(HORSE_ANIMATION_LOOPING_JOUST_GLANCE, 1.0f, 3.0f));
                     DelayCommand(8.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(672), oPC2));}
                else if (PC1Roll - PC2Roll <=  9) //Loose by 9 or less
                    {AssignCommand(oPC2, ActionPlayAnimation(HORSE_ANIMATION_LOOPING_JOUST_HELMOFF, 1.0f, 3.0f));
                     DelayCommand(8.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(671), oPC2));}
            }
            else
            {   AssignCommand(oPC2, ActionMoveToObject(GetObjectByTag("BlueEnd"),TRUE));
                //Only break the lance on a heavy hit:
                if (PC2Roll - PC1Roll > 5) DelayCommand(8.0, BreakWeapon(oPC2));
                GiveXPToCreature(oPC2 , GOLD_REWARD_AMOUNT);
                GiveGoldToCreature(oPC2 , GOLD_REWARD_AMOUNT);
            }

            EndCutscene(oPC2);
            SendMessageToPC(oPC2, "Your roll was " + IntToString(PC2Roll)+ ". Your opponent's roll was " + IntToString(PC1Roll));
            DelayCommand(11.0 , AssignCommand(oPC2, JumpToObject(GetObjectByTag("BlueEnd"))));
            }