// Module : OnHeartbeat by Brian "spilth" Kelly
// For Neverwinter Nights - Bleeding Tutorial

#include "bleeding_config"

// Dying check inspired by Mitchell M. Evans (gonecamping@cox.net)

void main() {
    // See if the module thinks anybody is dying
    object oModule      = GetModule();
    int bEverybodyAlive = GetLocalInt(oModule, "EverybodyAlive");

    // Only bother looping through players if they 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;
                    }
                    // See if they've been wounded again
                    if (GetCurrentHitPoints(oPC) < GetLocalInt(oPC,"LastHitPoints"))
                    {
                        SetLocalInt(oPC, "PlayerHealth", PC_HEALTH_DYING);
                        iPlayerHealth = PC_HEALTH_DYING;
                    }

                    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, bAllowWaitForHelpWhenStable, 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);
                            //effect eDamage = EffectHitPointChangeWhenDying(-1.0);
                            //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

}

