#include"aps_include"
//////////////////////////////////////
///// Created by: bushido/////////////
//////////////////////////////////////

//////////////////////////////////////
// AdjustPlayerStats()////////////////
// Developed by: Wouter Dhondt////////
// Slightly modified by: bushido//////
//////////////////////////////////////
// Adjust Player Stats function///////
// Edited For Database By Knowj///////
//////////////////////////////////////
void AdjustPlayerStats()
{
object oDied = GetLastPlayerDied();
object oKiller = GetLastHostileActor(oDied);
 if (!GetIsDM(oKiller) || !GetIsDM(oDied) )
 {
    // Increment or set the death variable
    int iDied = GetPersistentInt(oDied, "iDied", "score");
    ++iDied;
    SetPersistentInt(oDied, "iDied", iDied, 0, "score");
    // Is this object a PC?
    if (GetIsPC(oKiller) == TRUE)
    {
        // Increment or set the killer var
        int iKilled = GetPersistentInt(oKiller, "iKilled", "score");
        ++iKilled;
        SetPersistentInt(oKiller, "iKilled", iKilled, 0, "score");
    }
  }
}

// Raise OnDeath function
void Raise(object oPlayer)
{

        effect eBad = GetFirstEffect(oPlayer);
        ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectResurrection(),oPlayer);
        ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectHeal(GetMaxHitPoints(oPlayer)), oPlayer);

        //Search for negative effects
        while(GetIsEffectValid(eBad))
        {
            if (GetEffectType(eBad) == EFFECT_TYPE_ABILITY_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_AC_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_ATTACK_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_DAMAGE_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_DAMAGE_IMMUNITY_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_SAVING_THROW_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_SPELL_RESISTANCE_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_SKILL_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_BLINDNESS ||
                GetEffectType(eBad) == EFFECT_TYPE_DEAF ||
                GetEffectType(eBad) == EFFECT_TYPE_PARALYZE ||
                GetEffectType(eBad) == EFFECT_TYPE_NEGATIVELEVEL)
                {
                    //Remove effect if it is negative.
                    RemoveEffect(oPlayer, eBad);
                }
            eBad = GetNextEffect(oPlayer);
        }
        //Fire cast spell at event for the specified target
        SignalEvent(oPlayer, EventSpellCastAt(OBJECT_SELF, SPELL_RESTORATION, FALSE));
}

// Respawn/Teleporter OnDeath function
// Optionally you can create the waypoints described to send the dead player
// to their respective locations.
void Respawn(object oPlayer)
{
    string waiting = "<c � >The gods show you favor, your life is restored.";
    if (GetIsDead(oPlayer))

        {
            Raise(oPlayer);
            AssignCommand(oPlayer, ClearAllActions());
            AssignCommand(oPlayer, ActionSpeakString(waiting, TALKVOLUME_TALK));
            DelayCommand(0.5, AssignCommand(oPlayer, JumpToObject(GetWaypointByTag("w_main"))));
        }

}

void main()
{

    //Count up the kills and deaths
    AdjustPlayerStats();

    //Standard Ondeath stuff
    object oPlayer = GetLastPlayerDied();
    // * make friendly to Each of the 3 common factions
    AssignCommand(oPlayer, ClearAllActions());
/*
    if (GetStandardFactionReputation(STANDARD_FACTION_COMMONER, oPlayer) <= 10)
    {   SetLocalInt(oPlayer, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_COMMONER, 80, oPlayer);
    }
    if (GetStandardFactionReputation(STANDARD_FACTION_MERCHANT, oPlayer) <= 10)
    {   SetLocalInt(oPlayer, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_MERCHANT, 80, oPlayer);
    }
    if (GetStandardFactionReputation(STANDARD_FACTION_DEFENDER, oPlayer) <= 10)
    {   SetLocalInt(oPlayer, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_DEFENDER, 80, oPlayer);
    }
*/
    //Begin kill/death messages
    object oVictor = GetLastHostileActor(oPlayer);
    object oPC = GetFirstPC();
    int count = 0;
    int nVictims = GetPersistentInt(oVictor, "iKilled", "score");
    string sKilled = GetName(oPlayer);
    //Send Message to all PC's
    sKilled += "<c � > was slain by ";
    sKilled += GetName(oVictor);
    sKilled += "<c � >\n";
    sKilled += GetName(oVictor);
    sKilled += "<c � > has ";
    sKilled += IntToString(nVictims);
    sKilled += "<c � > kills.";

    while (GetIsPC(oPC) == TRUE)
    {
        SendMessageToPC(oPC, sKilled);
        oPC = GetNextPC();
    }

    //Respawn if noone rez'd The Player
    DelayCommand(8.0, Respawn(oPlayer));
}