#include "nw_i0_generic"
#include "spawner"

void main()
{

// Initialise variables & objects

object oPC = GetPCSpeaker();
object oMod = GetModule();

object oPC2;
object oTarget;
object oTarget2;
object oSpawn;
location lTarget;
location bTarget;
string sHench;
int nInt;
string sPRes;
string sPRes2;
int iBetConfirm = GetLocalInt(oPC, "bet_confirmed");
int iMode = GetLocalInt(oMod, "gamemode");
if (iBetConfirm!=1)
   {
    SetLocalInt(oPC, "wager", 0);
    SetLocalInt(oPC, "team_bet", 0);
   //SetLocalInt(oPC, "winnings",0);
   }


// Set Holy War mode ON

SetLocalInt(oMod, "hwar_on", 1);
SetLocalInt(oMod, "war_won", 0);
SetLocalInt(oPC, "champ_won", 0);
SetLocalInt(oMod, "iFireFlag", 0);
SetLocalInt(oMod, "turn_flag", 0);
SetLocalInt(oMod, "iDfire", 1);

// Flag all monsters as being in the arena

SetLocalInt(oMod, "sw_hw", 1);
SetLocalInt(oMod, "sw_dr", 1);
SetLocalInt(oMod, "sw_bl", 1);
SetLocalInt(oMod, "sw_pf", 1);
SetLocalInt(oMod, "sw_df", 1);
SetLocalInt(oMod, "sw_pm", 1);
SetLocalInt(oMod, "sw_bm", 1);
SetLocalInt(oMod, "sw_lm", 1);
SetLocalInt(oMod, "sw_ll", 1);
SetLocalInt(oMod, "sw_xx", 1);

// Reset scoreboard death status

SetLocalString(oMod, "notime4_x", "");
SetLocalString(oMod, "death_x", "");
SetLocalString(oMod, "notime3_x", "");
SetLocalString(oMod, "mistress2_x", "");
SetLocalString(oMod, "zep_marilithb001_x", "");
SetLocalString(oMod, "bard2_x", "");
SetLocalString(oMod, "zep_halfdrafn001_x", "");
SetLocalString(oMod, "zep_pitfiend001_x", "");
SetLocalString(oMod, "dopple_x", "");
SetLocalString(oMod, "zep_balrog001_x", "");
SetLocalString(oMod, "dragon1_x", "");
SetLocalString(oMod, "darcher_x", "");

SetLocalString(oMod, "notime2_x", "");
SetLocalString(oMod, "notime1_x", "");
SetLocalString(oMod, "boss002_x", "");
SetLocalString(oMod, "beli2_x", "");
SetLocalString(oMod, "pwar3_x", "");
SetLocalString(oMod, "hdrag2_x", "");
SetLocalString(oMod, "pfiend2_x", "");
SetLocalString(oMod, "horod2_x", "");
SetLocalString(oMod, "div2_x", "");
SetLocalString(oMod, "dragon2_x", "");

// Reset match champion flag and kill comparative

SetLocalInt(oMod, "notime4_ch", 0);
SetLocalInt(oMod, "death_ch", 0);
SetLocalInt(oMod, "notime3_ch", 0);
SetLocalInt(oMod, "mistress2_ch", 0);
SetLocalInt(oMod, "zep_marilithb001_ch", 0);
SetLocalInt(oMod, "bard2_ch", 0);
SetLocalInt(oMod, "zep_halfdrafn001_ch", 0);
SetLocalInt(oMod, "zep_pitfiend001_ch", 0);
SetLocalInt(oMod, "dopple_ch", 0);
SetLocalInt(oMod, "zep_balrog001_ch", 0);
SetLocalInt(oMod, "dragon1_ch", 0);
SetLocalInt(oMod, "darcher_ch", 0);

SetLocalInt(oMod, "notime2_ch", 0);
SetLocalInt(oMod, "notime1_ch", 0);
SetLocalInt(oMod, "boss002_ch", 0);
SetLocalInt(oMod, "beli2_ch", 0);
SetLocalInt(oMod, "pwar3_ch", 0);
SetLocalInt(oMod, "hdrag2_ch", 0);
SetLocalInt(oMod, "pfiend2_ch", 0);
SetLocalInt(oMod, "horod2_ch", 0);
SetLocalInt(oMod, "div2_ch", 0);
SetLocalInt(oMod, "dragon2_ch", 0);

SetLocalInt(oMod, "notime4_HWk", 0);
SetLocalInt(oMod, "death_HWk", 0);
SetLocalInt(oMod, "notime3_HWk", 0);
SetLocalInt(oMod, "mistress2_HWk", 0);
SetLocalInt(oMod, "zep_marilithb001_HWk", 0);
SetLocalInt(oMod, "bard2_HWk", 0);
SetLocalInt(oMod, "zep_halfdrafn001_HWk", 0);
SetLocalInt(oMod, "zep_pitfiend001_HWk", 0);
SetLocalInt(oMod, "dopple_HWk", 0);
SetLocalInt(oMod, "zep_balrog001_HWk", 0);
SetLocalInt(oMod, "dragon1_HWk", 0);
SetLocalInt(oMod, "darcher_HWk", 0);

SetLocalInt(oMod, "notime2_HWk", 0);
SetLocalInt(oMod, "notime1_HWk", 0);
SetLocalInt(oMod, "boss002_HWk", 0);
SetLocalInt(oMod, "beli2_HWk", 0);
SetLocalInt(oMod, "pwar3_HWk", 0);
SetLocalInt(oMod, "hdrag2_HWk", 0);
SetLocalInt(oMod, "pfiend2_HWk", 0);
SetLocalInt(oMod, "horod2_HWk", 0);
SetLocalInt(oMod, "div2_HWk", 0);
SetLocalInt(oMod, "dragon2_HWk", 0);

// Reset Match Champ stats

SetLocalString(oMod, "sChamp", "");
SetLocalInt(oMod, "champ_count", 0);
SetLocalInt(oMod, "MostKills", 0);
SetLocalInt(oPC, "iHappy", 0);

// Reset team casualties

SetLocalInt(oMod, "hell_cas", 0);
SetLocalInt(oMod, "heav_cas", 0);

// Reset exploit flag

SetLocalInt(oMod, "xcheck", 0);
SetLocalInt(oMod, "iDfire", 0);

// Reset disabled options in spawner convo

SetLocalInt(oMod, "scre_state", 0);
SetLocalInt(oMod, "scre2_state", 0);
SetLocalInt(oMod, "scre_state3", 0);
SetLocalInt(oMod, "scre_state4", 0);
SetLocalInt(oMod, "scre_state5", 0);

// reset the player allegiance flags

SetLocalInt(oMod, "turn_flag", 0);
oPC2 = GetFirstPC();
while (GetIsObjectValid(oPC2))
      {
       sPRes2 = GetResRef(oPC);
       sPRes2+="_HWd";
       sPRes = GetResRef(oPC);
       sPRes+="_HWk";
       SetLocalInt(oMod, sPRes, 0);
       SetLocalInt(oMod, sPRes2, 0);
       SetLocalInt(oPC2, "pc_HWdam", 0);
       SetLocalInt(oPC2, "pc_HWhit", 0);
       SetLocalInt(oPC2, "pc_side", 0);
       SetLocalInt(oPC2, "join_switch", 0);
       SetLocalInt(oPC2, "champ_won", 0);
       RemoveJournalQuestEntry("war_hell", oPC2, TRUE, TRUE);
       RemoveJournalQuestEntry("war_heav", oPC2, TRUE, TRUE);
       SetLocalInt(oPC2, "pc_HWswing", 0);
       oPC2 = GetNextPC();
     }

// Reset war victory journal entry

RemoveJournalQuestEntry("war_hell", oPC, TRUE, TRUE);
RemoveJournalQuestEntry("war_heav", oPC, TRUE, TRUE);

// Lock arena gate

oTarget = GetObjectByTag("arena_gate");
SetLocked(oTarget, TRUE);

// Move PC out of the battle

oTarget = GetWaypointByTag("safe_wp");
lTarget = GetLocation(oTarget);
AssignCommand(oPC, ClearAllActions());
AssignCommand(oPC, ActionJumpToLocation(lTarget));
CastPC(0.2, SPELL_ETHEREALNESS, oPC);



// Set up spawnpoints

oTarget2 = GetWaypointByTag("big_wp");
lTarget = GetLocation(oTarget);
bTarget = GetLocation(oTarget2);
oTarget = GetWaypointByTag("monster_wp");
lTarget = GetLocation(oTarget);

// Spawn Dark team

oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "dragon1", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "zep_halfdrafn001", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "zep_marilithb001", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "bard2", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "zep_pitfiend001", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "zep_balrog001", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "dopple", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "mistress2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "notime3", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "notime4", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "darcher", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "death", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);

// Set up spawnpoints

oTarget2 = GetWaypointByTag("big_wp2");
lTarget = GetLocation(oTarget);
bTarget = GetLocation(oTarget2);
oTarget = GetWaypointByTag("monster_wp2");
lTarget = GetLocation(oTarget);

// spawn light team

oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "notime2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "div2", lTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "horod2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "pfiend2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "hdrag2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "pwar3", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "beli2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "boss002", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "notime1", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "dragon2", bTarget);
SetIsTemporaryEnemy(oPC, oSpawn);



DelayCommand(5.0, JoinBackedTeam(oPC));

// experimental 'Death' and henchman concepts

//oTarget = GetWaypointByTag("death_wp");
//lTarget = GetLocation(oTarget);
//oSpawn = CreateObject(OBJECT_TYPE_CREATURE, "death2", lTarget);

/*
int oMode = GetLocalInt(oMod, "gamemode");
if (oMode==1)
{
 sHench = "diablo";
}
else
{
sHench = "starfall2";
}

oTarget = GetWaypointByTag("star_friend");
lTarget = GetLocation(oTarget);
oSpawn = CreateObject(OBJECT_TYPE_CREATURE, sHench, lTarget);
oTarget = oSpawn;
AddHenchman(oPC, oTarget);
DelayCommand(1.0, AssignCommand(oTarget, ClearAllActions()));
DelayCommand(1.5, AssignCommand(oTarget, ActionSit(GetObjectByTag("SF_BENCH"))));
*/

// Spawn Effect

nInt = GetObjectType(oTarget);
if (nInt != OBJECT_TYPE_WAYPOINT) DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_PWKILL), oTarget));
else DelayCommand(0.5, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_PWKILL), GetLocation(oTarget)));

}