#include "bleeding_config"
#include "x3_inc_horse"

void main() 
{
    object oPC=GetFirstPC();
    int nRoll;
    int bNoCombat=GetLocalInt(GetModule(),"X3_NO_MOUNTED_COMBAT_FEAT");
    while(GetIsObjectValid(oPC))
    { // PC traversal
        if (GetLocalInt(oPC,"bX3_STORE_MOUNT_INFO"))
        { // store
            DeleteLocalInt(oPC,"bX3_STORE_MOUNT_INFO");
            HorseSaveToDatabase(oPC,X3_HORSE_DATABASE);
        } // store
        if (!bNoCombat&&GetHasFeat(FEAT_MOUNTED_COMBAT,oPC)&&HorseGetIsMounted(oPC))
        { // check for AC increase
            nRoll=d20()+GetSkillRank(SKILL_RIDE,oPC);
            nRoll=nRoll-10;
            if (nRoll>4)
            { // ac increase
                nRoll=nRoll/5;
                ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectACIncrease(nRoll),oPC,7.0);
            } // ac increase
        } // check for AC increase
        oPC=GetNextPC();
    } // PC traversal
	
    object oCurrentPC = GetFirstPC();

    while(oCurrentPC!=OBJECT_INVALID)
     {
        int nRestingCounter = GetLocalInt(oCurrentPC, "nRestingCounterPC")+1;
        SetLocalInt(oCurrentPC, "nRestingCounterPC", nRestingCounter);
        oCurrentPC=GetNextPC();
     }
   // **********************************************************
   // Begin Bleeding to -10 section
   // **********************************************************
      // See if the module thinks anybody is dying
    object oModule      = GetModule();
    int bEverybodyAlive = GetLocalInt(oModule, "EverybodyAlive");

    // Only bother looping through players if there are disabled, dying, stable or dead players
    if (!bEverybodyAlive) {
        object oPC          = GetFirstPC();
        int iPlayerHealth   = PC_HEALTH_ALIVE;

        // Assume everybody really is alive
        int bEverybodyReallyAlive = TRUE;

        // Loop through the players and check their health
        while (GetIsObjectValid(oPC)) {
            iPlayerHealth = GetLocalInt(oPC, "PlayerHealth");

            if (iPlayerHealth != PC_HEALTH_ALIVE && iPlayerHealth != PC_HEALTH_DEAD) {

                // Double check their hit points
                if (GetCurrentHitPoints(oPC) <= 0) {
                    // They're really not alive

                    // See if they've been healed
                    if (GetCurrentHitPoints(oPC) > GetLocalInt(oPC,"LastHitPoints")) {
                        SetLocalInt(oPC, "PlayerHealth", PC_HEALTH_STABLE);
                        iPlayerHealth = PC_HEALTH_STABLE;
                    }

                    if (iPlayerHealth == PC_HEALTH_DYING) {
                        bEverybodyReallyAlive = FALSE;

                        // Make a stabilization check
                        if (d100() <= iStabilizationChance) {
                            // They stabilized
                            SetLocalInt(oPC, "PlayerHealth", PC_HEALTH_STABLE);

                            // See if they want to wait for help
                            PopUpDeathGUIPanel(oPC, bAllowRespawnWhenStable, bAllowRespawnWhenStable, 0, sStableGUIMessage);

                        } else {
                            // Make a random groan
                            switch (d6()) {
                                case 1:
                                    PlayVoiceChat (VOICE_CHAT_PAIN1, oPC);
                                    break;

                                case 2:
                                    PlayVoiceChat (VOICE_CHAT_PAIN2, oPC);
                                    break;

                                case 3:
                                    PlayVoiceChat (VOICE_CHAT_PAIN3, oPC);
                                    break;

                                case 4:
                                    PlayVoiceChat (VOICE_CHAT_HEALME, oPC);
                                    break;

                                case 5:
                                    PlayVoiceChat (VOICE_CHAT_NEARDEATH, oPC);
                                    break;

                                case 6:
                                    PlayVoiceChat (VOICE_CHAT_HELP, oPC);
                                    break;

                            }

                            // They bleed!
                            effect eDamage = EffectDamage(1, DAMAGE_TYPE_MAGICAL, DAMAGE_POWER_PLUS_FIVE);
                            ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oPC);

                            // Save their hit points in case they disconnect
                            SetLocalInt(oPC, "LastHitPoints", GetCurrentHitPoints(oPC));

                        } // End Stabilization If

                    } else if (iPlayerHealth == PC_HEALTH_STABLE) {
                        bEverybodyReallyAlive = FALSE;
                        PopUpDeathGUIPanel(oPC, bAllowRespawnWhenStable, bAllowWaitForHelpWhenStable, 0, sStableGUIMessage);

                    } else if (iPlayerHealth == PC_HEALTH_DISABLED) {
                        bEverybodyReallyAlive = FALSE;
                        PopUpDeathGUIPanel(oPC, bAllowRespawnWhenDisabled, bAllowWaitForHelpWhenDisabled, 0, sDisabledGUIMessage);

                    }

                } else {
                    // Must've been healed or something...
                    SetLocalInt(oPC, "PlayerHealth", PC_HEALTH_ALIVE);

                } // End HitPoints <= 0 If

            }

            oPC = GetNextPC();

        } // End while

        if (bEverybodyReallyAlive) {
            // We didn't find any dead players, so assume everybody is alive
            SetLocalInt(oModule, "EverybodyAlive", TRUE);
        }

    } // End if

}
