////////////////////////////////////
//Created by Genisys / Guile
//1/11/08 to 8/10/08 (Big Project!)
////////////////////////////////////
/*

This is my premier OnPlayerDeath Module Event Script which I designed
specifically for all my modules, it's rather nice and is part of
multiple systems, so do not edit ANYTHING you do not know what it's for!

*/
////////////////////////////////////

//Rquired Includes..  (Don't touch!)
#include "nw_i0_plot"
#include "x2_inc_compon"
#include "setxp_inc"

///////////////////////////////////////////////////////////////////////////
/////////////////IMPORTANT OPTIONAL SETTINGS///////////////////////////////
//The constant intergal below allows you to choose where or not to utilize
//the PVP Death System I have scripted, please read below to learn more..
//FALSE = Disabled / TRUE = Enabled
const int nPVP = FALSE;

//This constant will decide whether or not you want to send a PVP message
//to all players when PVP transpires (This is for PVP Servers)
//FALSE = Disabled / TRUE = Enabled
const int nPVPMessage = FALSE;

//If you wish to restrict PVP to the arena only, then..
//Set the below setting to TRUE / FALSE = Deactivated (Default)
const int nArenaOnly = TRUE;

//This determines the level difference allowed in PVP (default 5)
//If set to 5 players within 4 levels of each other will not be
//Penalized for Killing one another. (Works both ways!)
//Add +1 for the level you want so if you want 8 level difference make it 9!
//(Set this whether you allow PVP or not!)
const int nDif = 5; //(Between 5 - 40) / (40 = Disabled)

//This is how you set the reward for XP given to the player for
//fair PVP Kills, this # is multiplied by the level difference!
const int nReward1 = 80;    //(ie. if 3 Levels difference / 80 X 3)

//This is how ytou set the reward for Gold given to the player for
//fair PVP Kills, this # is multiplied by the level difference!
const int nReward2 = 800;

/////////OPTIONS END//////////////////////////////////////////////////

////DO NOT TOUCH ANYTHING BELOW UNLESS IT SAYS - ///OPTION////

///////////////////////////////////////////////////////////////
// Raise OnDeath function (Standard Bioware Function)
void Raise(object oPlayer)
{
   
        effect eVisual = EffectVisualEffect(VFX_IMP_RESTORATION);

        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));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eVisual, oPlayer);

        DestroyObject(GetItemPossessedBy(oPlayer, "death"), 0.0f);
}

// Respawn/Teleporter OnDeath function (By Bushido / Modified By Genisys)
// Optionally you can create the waypoints described to send the dead player
// to that location.  You must create an item named arenatoken (tag & resref name)
//Then you must use give it to them & take it away from them on enter/exit of
//an area by a script, this will deem that area as the arena.
void Respawn(object oPlayer)
{

    string waiting = "<c B+>The gods show you favor, your life is restored.";

    if (GetIsDead(oPlayer))
    {
        object oPC = GetLastPlayerDied();
        object oArea = GetArea(oPC);
        string sTag = GetTag(oArea);
        if (sTag=="Arena" || sTag == "DMArena")
        {
            //Create a way point and tagname it "arenawp"
            object oTarget = GetWaypointByTag("arenaspawnpt");
            effect eTeleport = EffectVisualEffect(VFX_FNF_HOWL_WAR_CRY);

            ApplyEffectToObject(DURATION_TYPE_INSTANT, eTeleport, oPlayer);

            //Tell the player why they are being restored.
            FloatingTextStringOnCreature(waiting, oPC);

            //Resurrect the player
            Raise(oPlayer);

            //Teleport the raised player to the arena spawn point.
            DelayCommand(0.1, AssignCommand(oPlayer, ClearAllActions()));
            DelayCommand(0.2, AssignCommand(oPlayer, JumpToObject(oTarget)));
            DestroyObject(GetItemPossessedBy(oPlayer, "death"), 0.0f);
        }
        else
        {

         CreateItemOnObject("death", oPlayer, 1);

        }

    }
}

////////////////////////////////////////////////////////////////////////
////////////////////////Main Script/////////////////////////////////////
////////////////////////////////////////////////////////////////////////
void main()
{

    //Declare Major Variables...
    object oPlayer = GetLastPlayerDied();
    object oPC = GetLastPlayerDied();
    object oTarget = oPlayer;
    object oToken = GetItemPossessedBy(oPlayer, "death");
    object oItem;
    object oPP = oPlayer;
    string sArea = GetTag(GetArea(oPlayer));
    string sName = GetName(GetModule());
    location lDead = GetLocation(oPlayer);

    effect eKill = EffectDeath(TRUE);
    effect eVis = EffectVisualEffect(VFX_FNF_MYSTICAL_EXPLOSION);


   //Define who oKiller is..
   //Define the attacker & dead guy
   //PVP Variables..
    object oDead = GetLastPlayerDied();
    object oKiller = GetLastHostileActor(oDead);
    object oDied = oPlayer;
    object oVictor;
    object oAttacker;
    object oDumbo;


     //If the player is being tortured, ressurect them...
     object hell = GetArea(oPlayer);
     object helltest = GetArea(GetObjectByTag("satan1"));
     if (hell == helltest)
     {
     DelayCommand(0.1, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectResurrection(), oPlayer));
     return;
     }

///////////////////////////////////////////////////////////////////
//Determine who the killer is..

 //Define who the attacker is first!
 //Let's make sure the Killer is a PC
  if(GetIsPC(oAttacker))
  {
   oKiller = GetLastHostileActor(oDead);
  }
 if(GetIsDM(oAttacker))
  {
   //Do not define the killer!
  }
  //Let's make sure we properly identify who the Attacker is...
 else if (GetIsPC(GetMaster(oAttacker)))
 {
  oKiller = GetMaster(oAttacker);
 }

 else
 {
  //otherwise it's a monster, so don't define the killer!
 }

////////////END VARIABLES////////////////////////////////////

//Save the PC's character instantly..
if(GetLocalInt(GetModule(), "MULTI")==TRUE)
{
 ExportSingleCharacter(oPC);
}

//Clear the PC's Reputation immediately...
if(GetIsPC(oPlayer))
{

    AssignCommand(oPlayer, ClearAllActions());

    //Clear Reputation of PC
    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);
    }

 }

///////////GUILD DEATH LOCATION SAVING///////////////////////////////

    //If the pc has a Guile Pass then store their death location..
    if(GetItemPossessedBy(oDead, "guildstone") != OBJECT_INVALID)
    {
     //Store their location into the database for later as well..
     SetCampaignLocation("LOCATIONS", "dth_stored_loc", lDead, oPlayer);
    }

//(leave this alone) */
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

    //Fancy death visual effect when they die.
    location lSelf = GetLocation(oPC);
    effect eVis2 = EffectVisualEffect(234);
    ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eVis2, lSelf);

/////////PRE-RESPAWN-ACTIONS/////////

//If the player is in the arena, always respawn them..
if(GetItemPossessedBy(oPC, "arenatoken") != OBJECT_INVALID)
{
 //Lets auto ressurect anyone in the arena and give death tokens to the others.
    Respawn(oPlayer);

    // * make friendly to Each of the 3 common factions
    AssignCommand(oPlayer, ClearAllActions());

    // * Note: waiting for Sophia to make SetStandardFactionReptuation to clear all personal reputation
    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);
    }


}

/////////////////////////////////////////////////////////////////////////////

   //Have the player tell everyone around them
   //they are dying (technically they are already dead :)
   AssignCommand(oPlayer, ActionSpeakString("I'm dying, help!",
   TALKVOLUME_TALK));

////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////

 //If the player is not immortal & doesn't have an Arena Token...
 //Pop up the Death GUI Panel only if they don't meet the following..

 //If the player is not immortal
 if(GetItemPossessedBy(oPC, "immotoken") == OBJECT_INVALID)
 {
 //If the player does not have an arena token
  if(GetItemPossessedBy(oPC, "arenatoken") == OBJECT_INVALID)
  {
    //Show the player the Death Panel
    DelayCommand(2.0, PopUpGUIPanel(oPlayer,GUI_PANEL_PLAYER_DEATH));

  }
 }

//////////////////////////////////////////////////////////////////////

////////////////////DETERMINE WHO IS KILLING WHO//////////////////////
//////////////////////////////////////////////////////////////////////

//NOTE: We don't want the script to run for Monsters or DMs!

//If it's not a PC let's make sure an associate of a PC
//has not killed the PC or that's it's not a monster or DM..
if(!GetIsPC(oKiller))
{
 //Let's get the master of the asscoiate..
 if(GetIsPC(GetMaster(oKiller)))
 {
  oDumbo = GetMaster(oKiller);
 }
 //If it's a DM or Monster stop the script here!
 else
 {
  //Stop the rest of the script from firing all together!
  return;
 }

}

//Otherwise let's make sure the Attacker is a PC..
else
{

 if(GetIsPC(oKiller))
 {
  oDumbo = oKiller;
 }
 //If they haven't passed the test for PC or associate of PC stop here!
 else
 {
  return;
 }

//End else
}

//If it's not a PC or Associate of the PC STOP HERE!
if(!GetIsPC(oDumbo))
{
 return;
}

////////////////////////////////////////////////////////////////////////

////////////////////////PVP MESSAGE//////////////////////////////////////
///////See the const int nPVPMessage above to turn on/off)///////////////

//Always send this message...
if(GetIsPC(oDumbo))
{
    oVictor = oDumbo;
    ////Send PVP Stats Messages to all PCs./////////
    //This shouts whever a player has died, who killed them and also
    //alerts DMs of any pvp action going on! (Though done below as well.)
    //This can also alert DM's not playing as a DM as well too!

    string sKilled;
    //Send Message to all DM's
    sKilled = "***ATTENTION DMs****  ";
    sKilled += GetName(oPlayer);
    sKilled += " / ";
    sKilled += GetPCPlayerName(oPlayer);
    sKilled += " Was Killed by the player - ";
    sKilled += GetName(oVictor);
    sKilled += " / ";
    sKilled += GetPCPlayerName(oVictor);

   //Lets make sure the killer is a PC
   if(GetIsPC(oVictor))
   {

    //Write the PVP Information in the log file. (For those not allowing PVP)
    WriteTimestampedLogEntry("**The Following Player: ");
    WriteTimestampedLogEntry(GetPCPlayerName(oVictor));
    WriteTimestampedLogEntry(" - ");
    WriteTimestampedLogEntry(GetPCPublicCDKey(oVictor));
    WriteTimestampedLogEntry(" - ");
    WriteTimestampedLogEntry(GetPCIPAddress(oVictor));
    WriteTimestampedLogEntry(" ** Has Killed This Player: ");
    WriteTimestampedLogEntry(GetName(oPlayer));
    WriteTimestampedLogEntry(" - ");
    WriteTimestampedLogEntry(GetPCPlayerName(oPlayer));

    SendMessageToAllDMs(sKilled);
   //end PVP message function
   }
}
/////////////////////////////////////////////////////////////////////////

//Script End WOOSH!!
}