/// <summary>
    /// Executes the melee attack after the attack has been released.
    /// Casts a ray from the centre of the screen in mouselook mode or from the cursor position
    /// Waits a period of time after the release before casting the ray.
    /// </summary>
    public override IEnumerator ExecuteMelee(string StrikeType, float StrikeCharge)
    {
        yield return(new WaitForSeconds(0.4f));

        Ray ray;

        if (UWCharacter.Instance.MouseLookEnabled == true)
        {
            ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        }
        else
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        }

        RaycastHit hit = new RaycastHit();

        if (Physics.Raycast(ray, out hit, weaponRange))
        {
            if (!hit.transform.Equals(this.transform))
            {
                ObjectInteraction objInt = hit.transform.gameObject.GetComponent <ObjectInteraction>();
                if (objInt != null)
                {
                    //Debug.Log("Hitting " + objInt.name);
                    switch (objInt.GetItemType())
                    {
                    case ObjectInteraction.NPC_TYPE:
                        PC_Hits_NPC(UWCharacter.Instance, currWeapon, CurrentStrike, StrikeCharge, objInt.GetComponent <NPC>(), hit);
                        break;

                    default:
                        Impact.SpawnHitImpact(Impact.ImpactDamage(), (ray.origin + hit.point) / 2f, DamageImpactStart, DamageImpactEnd);
                        objInt.Attack((short)GetPlayerBaseDamage(currWeapon, CurrentStrike), UWCharacter.Instance.gameObject);
                        break;
                    }
                }
                else
                {//Probably hit a wall or tile floor
                    Impact.SpawnHitImpact(Impact.ImpactDamage(), (ray.origin + hit.point) / 2f, DamageImpactStart, DamageImpactEnd);
                    if (currWeapon != null)
                    {
                        short durability = currWeapon.getDurability();
                        if (durability <= 30)
                        {
                            currWeapon.SelfDamage((short)(Mathf.Max(0, Random.Range(1, durability + 1) - durability)));
                        }
                    }

                    if (ObjectInteraction.PlaySoundEffects)
                    {
                        UWCharacter.Instance.aud.clip = MusicController.instance.SoundEffects[MusicController.SOUND_EFFECT_MELEE_MISS_1];
                        UWCharacter.Instance.aud.Play();
                    }
                }
            }
        }
        else
        {
            if (ObjectInteraction.PlaySoundEffects)
            {
                UWCharacter.Instance.aud.clip = MusicController.instance.SoundEffects[MusicController.SOUND_EFFECT_MELEE_MISS_1];
                UWCharacter.Instance.aud.Play();
            }
            if (currWeapon != null)
            {
                currWeapon.onHit(null);
            }
        }

        ///Ends the attack and wait for a period before allowing another action.
        AttackExecuting = false;
        UWHUD.instance.window.UWWindowWait(1.0f);
    }
    /// <summary>
    /// Combat calculations for PC hitting an NPC
    /// </summary>
    /// <param name="playerUW">Player Character</param>
    /// <param name="npc">Npc.</param>
    /// <param name="hit">Hit.</param>
    public static void PC_Hits_NPC(UWCharacter playerUW, WeaponMelee currentWeapon, string StrikeName, float StrikeCharge, NPC npc, RaycastHit hit)
    {
        int attackScore = 0;

        int   flankingbonus = 0; //need to calcu but depending on relative headings between attacker and defender.
        int   magicbonus    = 0; //currently unknown what provides this but is calculated by casting/5 + 0xA when set.
        short MinCharge;
        short MaxCharge;

        if (npc != null)
        {
            //recalc position to get the correct heading for the flanking bonus
            npc.objInt().UpdatePosition();
            flankingbonus = CalcFlankingBonus(playerUW.currentHeading, npc.objInt().heading);
        }


        if (currentWeapon != null)
        {
            attackScore = (playerUW.PlayerSkills.GetSkill(Skills.SkillAttack) >> 1) + playerUW.PlayerSkills.GetSkill(currentWeapon.GetSkill()) + (playerUW.PlayerSkills.DEX / 7) + magicbonus + flankingbonus;
            if (GameWorldController.instance.difficulty == 0)
            {//bonus of 7 for easy difficulty
                attackScore += 7;
            }
            attackScore += currentWeapon.AccuracyBonus();//Given by the accuracy enchantments. Note in vanilla UW2 accuracy actually increases damage and not attack score. This is fixed here.

            MinCharge = currentWeapon.GetMinCharge();
            MaxCharge = currentWeapon.GetMaxCharge();
        }
        else
        {
            //use the unarmed calculations
            attackScore = (playerUW.PlayerSkills.GetSkill(Skills.SkillAttack) >> 1) + playerUW.PlayerSkills.GetSkill(Skills.SkillUnarmed) + (playerUW.PlayerSkills.DEX / 7) + magicbonus + flankingbonus;
            if (GameWorldController.instance.difficulty == 0)
            {//bonus of 7 for easy difficulty
                attackScore += 7;
            }
            MinCharge = WeaponMelee.GetMeleeMinCharge();
            MaxCharge = WeaponMelee.GetMeleeMaxCharge();
        }


        //Get base damage
        int BaseSwingDamage   = GetPlayerBaseDamage(currentWeapon, StrikeName);
        int basePower         = (short)(GameWorldController.instance.objDat.critterStats[63].Strength / 9); //the player is actually a critter so power in this case is their STR.
        int WeaponBonusDamage = 0;

        if (currentWeapon != null)
        {
            WeaponBonusDamage = currentWeapon.DamageBonus();
        }
        else
        {                                                                                            //unarmed
            basePower  = (short)(GameWorldController.instance.objDat.critterStats[63].Strength / 6); //the player is actually a critter so power in this case is their STR.
            basePower += (playerUW.PlayerSkills.GetSkill(Skills.SkillUnarmed) / 5);
        }


        //scale base damage by charge. Min damage is always 2.
        short Damage = (short)(basePower + BaseSwingDamage + WeaponBonusDamage);

        // damage % 6 no of 1D6s to calculate the actual damage and then add the remainder as a final roll 1-remainder
        Damage = (short)(DamageRoll((short)(Damage / 6), 6) + DamageRoll(1, (short)(Damage % 6)));

        //Damage is scaled by the amount of weapon charge built up.
        Damage = CalcAttackChargeDamage((short)StrikeCharge, MinCharge, MaxCharge, Damage);

        //And a flat flanking bonus
        Damage += (short)flankingbonus;

        //Min damage will be 2.
        Damage = (short)(Mathf.Max(Damage, 2));

        Skills.SkillRollResult rollresult = Skills.SkillRoll(attackScore, npc.Defence());

        bool CriticalHit = RollForCritDamage(ref Damage, rollresult);

        switch (rollresult)
        {
        case Skills.SkillRollResult.CriticalSuccess:
        case Skills.SkillRollResult.Success:
        {
            Debug.Log("Base Damage = " + ((short)(basePower + BaseSwingDamage + WeaponBonusDamage)) + " Final Damage = " + Damage);
            if (CriticalHit)
            {
                Impact.SpawnHitImpact(Impact.ImpactBlood(), npc.GetImpactPoint() + Vector3.up * 0.1f, npc.objInt().GetHitFrameStart(), npc.objInt().GetHitFrameEnd());
            }
            npc.ApplyAttack(Damage, playerUW.gameObject);
            Impact.SpawnHitImpact(Impact.ImpactBlood(), npc.GetImpactPoint(), npc.objInt().GetHitFrameStart(), npc.objInt().GetHitFrameEnd());
            if (currentWeapon != null)
            {        ///Performs the onHit action of the melee weapon.
                currentWeapon.onHit(hit.transform.gameObject);
            }
            break;
        }

        case Skills.SkillRollResult.Failure:
            npc.ApplyAttack(0, playerUW.gameObject);
            npc.npc_aud.PlayCombatMissed();
            break;

        case Skills.SkillRollResult.CriticalFailure:
        {
            npc.ApplyAttack(0, playerUW.gameObject);
            npc.npc_aud.PlayCombatMissed();
            //Damage weapon
            short durability = currentWeapon.getDurability();
            if (durability <= 30)
            {
                currentWeapon.SelfDamage((short)Mathf.Max(0, Random.Range(0, npc.EquipDamage + 1) - durability));
            }
            break;
        }
        }
    }
    //Combat calcs


    /// <summary>
    /// Combat calculations for PC hitting an NPC
    /// </summary>
    /// <param name="playerUW">Player Character</param>
    /// <param name="npc">Npc.</param>
    /// <param name="hit">Hit.</param>
    public static void PC_Hits_NPC(UWCharacter playerUW, WeaponMelee currentWeapon, string StrikeName, float StrikeCharge, NPC npc, RaycastHit hit)
    {
        int attackScore = 0;
        int BaseDamage  = GetPlayerBaseDamage(currentWeapon, StrikeName);

        if (currentWeapon != null)
        {
            attackScore  = playerUW.PlayerSkills.GetSkill(Skills.SkillAttack) / 2 + playerUW.PlayerSkills.GetSkill(currentWeapon.GetSkill() + 1);
            attackScore += currentWeapon.AccuracyBonus();
        }
        else
        {
            attackScore = playerUW.PlayerSkills.GetSkill(Skills.SkillAttack) / 2 + playerUW.PlayerSkills.GetSkill(Skills.SkillUnarmed);
        }


        int toHit = Mathf.Max(npc.GetDefence() - attackScore, 0);
        //Difficulty is either 1 for standard or 0 for easy.
        int roll          = Mathf.Max(30, Random.Range(-1, 31) + (5 * (1 - GameWorldController.instance.difficulty)));        //-1 is critical miss. 30 is always hit
        int HitRollResult = 0;

        if (((roll >= toHit) || (roll >= 30)) && (roll > -1))
        {
            short Damage = (short)(Mathf.Max(((float)(BaseDamage)) * (StrikeCharge / 100f), 1));
            npc.ApplyAttack(Damage, playerUW.gameObject);
            HitRollResult = 1;
            if (roll == 30)
            {
                HitRollResult = 2;                      //crit. apply double damage
                npc.ApplyAttack(Damage, playerUW.gameObject);
            }
            if (roll >= 27)
            {
                HitRollResult = 2;                      //critical
            }
        }
        else
        {
            HitRollResult = 0;
            npc.ApplyAttack(0, playerUW.gameObject);                   //A zero damage attack to update ai if needed
            if (currentWeapon != null)
            {
                //Apply equipment damage to weapon if NPC Defence is 1.5 times attackscore
                if ((float)npc.GetDefence() > (float)attackScore * 1.5f)
                {
                    short durability = currentWeapon.getDurability();
                    //currentWeapon.WeaponSelfDamage();
                    if (durability <= 30)
                    {
                        currentWeapon.SelfDamage((short)Mathf.Max(0, Random.Range(0, npc.GetArmourDamage() + 1) - durability));
                    }
                }
            }
        }


        ///Creates a blood splatter at the point of impact
        switch (HitRollResult)
        {
        case 0:                 //Miss
                                //Impact.SpawnHitImpact(hit.transform.name + "_impact", npc.objInt().GetImpactPoint(),46,50);
            if (ObjectInteraction.PlaySoundEffects)
            {
                npc.objInt().aud.clip = GameWorldController.instance.getMus().SoundEffects[MusicController.SOUND_EFFECT_MELEE_MISS_2];
                npc.objInt().aud.Play();
            }
            break;

        case 1:                //Hit
            Impact.SpawnHitImpact(Impact.ImpactBlood(), npc.objInt().GetImpactPoint(), npc.objInt().GetHitFrameStart(), npc.objInt().GetHitFrameEnd());
            if (ObjectInteraction.PlaySoundEffects)
            {
                npc.objInt().aud.clip = GameWorldController.instance.getMus().SoundEffects[MusicController.SOUND_EFFECT_MELEE_HIT_1];
                npc.objInt().aud.Play();
            }
            break;

        case 2:                //Crit
            Impact.SpawnHitImpact(Impact.ImpactBlood(), npc.objInt().GetImpactPoint(), npc.objInt().GetHitFrameStart(), npc.objInt().GetHitFrameEnd());
            Impact.SpawnHitImpact(Impact.ImpactBlood(), npc.objInt().GetImpactPoint() + Vector3.up * 0.1f, npc.objInt().GetHitFrameStart(), npc.objInt().GetHitFrameEnd());
            if (ObjectInteraction.PlaySoundEffects)
            {
                npc.objInt().aud.clip = GameWorldController.instance.getMus().SoundEffects[MusicController.SOUND_EFFECT_MELEE_HIT_2];
                npc.objInt().aud.Play();
            }
            break;
        }

        if (currentWeapon != null)
        {                ///Performs the onHit action of the melee weapon.
            currentWeapon.onHit(hit.transform.gameObject);
        }
    }