#include "prc_inc_spells"

void SummonTitan(location lLocation, int nTeam)
{

  object oAlly = CreateObject(OBJECT_TYPE_CREATURE, "titan", lLocation, FALSE);
  SetLocalInt(oAlly, "m_nTeamToAttack", nTeam);

}

void SummonLocusts(location lLocation, int nTeam)
{

  object oAlly = CreateObject(OBJECT_TYPE_CREATURE, "locust", lLocation, FALSE);
  SetLocalInt(oAlly, "m_nTeamToAttack", nTeam);
  object oAlly2 = CreateObject(OBJECT_TYPE_CREATURE, "locust", lLocation, FALSE);
  SetLocalInt(oAlly2, "m_nTeamToAttack", nTeam);
  object oAlly3 = CreateObject(OBJECT_TYPE_CREATURE, "locust", lLocation, FALSE);
  SetLocalInt(oAlly3, "m_nTeamToAttack", nTeam);
  object oAlly4 = CreateObject(OBJECT_TYPE_CREATURE, "locust", lLocation, FALSE);
  SetLocalInt(oAlly4, "m_nTeamToAttack", nTeam);

}

void RemoveAllEffects( object oPlayer )
{
    effect eEffect;
    eEffect = GetFirstEffect(oPlayer);

    while ( GetIsEffectValid(eEffect) == TRUE )
    {
        if ( GetEffectDurationType(eEffect) == DURATION_TYPE_TEMPORARY ||
             GetEffectDurationType(eEffect) == DURATION_TYPE_PERMANENT )
        {
            RemoveEffect(oPlayer,eEffect);
        }

        eEffect = GetNextEffect(oPlayer);
    }
}

void ExplodeAtLocation(location lTarget, int nDamage, int nSaveDC = 30, float fRadius = 3.) {
  ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_FIREBALL), lTarget);
  object oObject = GetFirstObjectInShape(SHAPE_SPHERE, fRadius, lTarget);
  do {
    int nDamageAfterSave = PRCGetReflexAdjustedDamage(nDamage, oObject, nSaveDC);
    ApplyEffectToObject( DURATION_TYPE_INSTANT, EffectDamage(nDamageAfterSave, DAMAGE_TYPE_FIRE), oObject);
   } while ((oObject = GetNextObjectInShape(SHAPE_SPHERE, fRadius, lTarget)) != OBJECT_INVALID);
}

void NegativeAtLocation(location lTarget, int nDamage, int nSaveDC = 18, float fRadius = 5.) {
  ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_HORRID_WILTING), lTarget);
  object oObject = GetFirstObjectInShape(SHAPE_SPHERE, fRadius, lTarget);
  do {
//    int nDamageAfterSave = GetReflexAdjustedDamage(nDamage, oObject, nSaveDC);
    ApplyEffectToObject( DURATION_TYPE_INSTANT, EffectDamage(nDamage, DAMAGE_TYPE_NEGATIVE), oObject);
   } while ((oObject = GetNextObjectInShape(SHAPE_SPHERE, fRadius, lTarget)) != OBJECT_INVALID);
}

void DiseaseAtLocation(location lTarget, int nDamage, int nSaveDC = 30, float fRadius = 3.) {
  ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_GAS_EXPLOSION_EVIL), lTarget);
  object oObject = GetFirstObjectInShape(SHAPE_SPHERE, fRadius, lTarget);
  do {
//     if (FortitudeSave(oObject, nSaveDC, SAVING_THROW_TYPE_DISEASE, OBJECT_SELF)==0) {
       ApplyEffectToObject( DURATION_TYPE_PERMANENT, EffectDisease(DISEASE_MINDFIRE), oObject);
//     }
//    int nDamageAfterSave = GetReflexAdjustedDamage(nDamage, oObject, nSaveDC);
//    ApplyEffectToObject( DURATION_TYPE_INSTANT, EffectDamage(nDamageAfterSave, DAMAGE_TYPE_FIRE), oObject);

   } while ((oObject = GetNextObjectInShape(SHAPE_SPHERE, fRadius, lTarget)) != OBJECT_INVALID);
}

void IcestormAtLocation(location lTarget, int nDamage, int nSaveDC = 30, float fRadius = 4.) {
  ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_ICESTORM), lTarget);
  object oObject = GetFirstObjectInShape(SHAPE_SPHERE, fRadius, lTarget);
  do {
    int nDamageAfterSave = PRCGetReflexAdjustedDamage(nDamage, oObject, nSaveDC);
    ApplyEffectToObject( DURATION_TYPE_INSTANT, EffectDamage(nDamageAfterSave, DAMAGE_TYPE_COLD), oObject);
   } while ((oObject = GetNextObjectInShape(SHAPE_SPHERE, fRadius, lTarget)) != OBJECT_INVALID);
}


void WrathOfTheGods( int nTeamHit )
{

  object oPC = GetFirstPC();
  while ( GetIsObjectValid(oPC) == TRUE )
  {
      int nPCTeam = GetLocalInt(oPC, "m_nTeam");
      if(nPCTeam == nTeamHit) {

        ExplodeAtLocation(GetLocation(oPC), d6(12));
        DelayCommand(2.0, NegativeAtLocation(GetLocation(oPC), d6(6)));
        DelayCommand(4.0, DiseaseAtLocation(GetLocation(oPC), d6(12)));
        DelayCommand(6.0, IcestormAtLocation(GetLocation(oPC), d6(12)));
        DelayCommand(7.0, ExplodeAtLocation(GetLocation(oPC), d6(12)));
        DelayCommand(8.0, NegativeAtLocation(GetLocation(oPC), d6(6)));
        DelayCommand(9.0, IcestormAtLocation(GetLocation(oPC), d6(12)));

      }
      oPC = GetNextPC();
  }

}

void FuryOfTheTitans( int nTeamHit )
{

  object oPC = GetFirstPC();
  while ( GetIsObjectValid(oPC) == TRUE )
  {
      int nPCTeam = GetLocalInt(oPC, "m_nTeam");
      if(nPCTeam == nTeamHit && GetHitDice(oPC) > 10) {

        ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SUMMON_CELESTIAL), GetLocation(oPC));
        DelayCommand(3.0, SummonTitan(GetLocation(oPC), nTeamHit));

      }
      oPC = GetNextPC();
  }

}

void PlagueOfLocusts( int nTeamHit )
{

  object oPC = GetFirstPC();
  while ( GetIsObjectValid(oPC) == TRUE )
  {
      int nPCTeam = GetLocalInt(oPC, "m_nTeam");
      if(nPCTeam == nTeamHit && GetHitDice(oPC) > 5) {

        ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_DISPEL_DISJUNCTION), GetLocation(oPC));
        DelayCommand(3.0, SummonLocusts(GetLocation(oPC), nTeamHit));

      }
      oPC = GetNextPC();
  }

}

void CheckScoreForEvents( int nCScore, int nCPreviousScore, int nCTeam, string sCTeam )
{

  int nDamagedTeam, nPCTeam;
  string sSpeak = "";
  if(nCTeam == 1) {
    nDamagedTeam = 2;
  }
  if(nCTeam == 2) {
    nDamagedTeam = 1;
  }

  if(nCScore > 9 && nCPreviousScore < 10) {

    sSpeak = sCTeam + " team has scored 10 points.  All team members receive 5,000 gold.";
    object oPC = GetFirstPC();
    while ( GetIsObjectValid(oPC) == TRUE )
    {
        nPCTeam = GetLocalInt(oPC, "m_nTeam");
        if(nPCTeam == nCTeam) {
          GiveGoldToCreature(oPC, 5000);
        }
        oPC = GetNextPC();
    }

  }

  if(nCScore > 19 && nCPreviousScore < 20) {

    sSpeak = sCTeam + " team has scored 20 points.  All team members receive 10,000 gold.";
    object oPC = GetFirstPC();
    while ( GetIsObjectValid(oPC) == TRUE )
    {
        nPCTeam = GetLocalInt(oPC, "m_nTeam");
        if(nPCTeam == nCTeam) {
          GiveGoldToCreature(oPC, 10000);
        }
        oPC = GetNextPC();
    }
  }

  if(nCScore > 29 && nCPreviousScore < 30) {

    sSpeak = sCTeam + " team has scored 30 points.  All team members receive 10,000 gold.";
    object oPC = GetFirstPC();
    while ( GetIsObjectValid(oPC) == TRUE )
    {
        nPCTeam = GetLocalInt(oPC, "m_nTeam");
        if(nPCTeam == nCTeam) {
          GiveGoldToCreature(oPC, 10000);
        }
        oPC = GetNextPC();
    }
  }

  if(nCScore > 39 && nCPreviousScore < 40) {

    sSpeak = sCTeam + " team has scored 40 points.  Enemy team members receive Plague of Locusts.";
    PlagueOfLocusts(nDamagedTeam);

  }

  if(nCScore > 49 && nCPreviousScore < 50) {

    sSpeak = sCTeam + " team has scored 50 points.  All team members above level 10 receive a Rod of Allies.";
    object oPC = GetFirstPC();
    while ( GetIsObjectValid(oPC) == TRUE )
    {
        nPCTeam = GetLocalInt(oPC, "m_nTeam");
        if(nPCTeam == nCTeam && GetHitDice(oPC) > 10) {
          if(nCTeam == 1)
            CreateItemOnObject("celestial", oPC, 1);
          if(nCTeam == 2)
            CreateItemOnObject("demonicsceptor", oPC, 1);
        }
        oPC = GetNextPC();
    }
  }

  if(nCScore > 74 && nCPreviousScore < 75) {

    sSpeak = sCTeam + " team has scored 75 points.  All team members receive 20,000 gold.  " +
             "All enemy team members above level 5 receive Wrath of the Gods.";
    object oPC = GetFirstPC();
    while ( GetIsObjectValid(oPC) == TRUE )
    {
        nPCTeam = GetLocalInt(oPC, "m_nTeam");
        if(nPCTeam == nCTeam) {
          GiveGoldToCreature(oPC, 20000);
        }
        oPC = GetNextPC();
    }

    WrathOfTheGods(nDamagedTeam);

  }

  if(nCScore > 84 && nCPreviousScore < 85) {

    sSpeak = sCTeam + " team has scored 85 points.  Enemy team members receive Wrath of the Gods and Plague of Locusts.";
    WrathOfTheGods(nDamagedTeam);
    PlagueOfLocusts(nDamagedTeam);

  }

  if(nCScore > 99 && nCPreviousScore < 100) {

    sSpeak = sCTeam + " team has scored 100 points.  Scores are reset to zero. " +
    "All team members receive a Rod of Allies and a Victory Gem.  " +
    "All enemy team members receive Wrath of the Gods and Fury of the Titans.";
    object oPC = GetFirstPC();
    while ( GetIsObjectValid(oPC) == TRUE )
    {
        nPCTeam = GetLocalInt(oPC, "m_nTeam");
        if(nPCTeam == nCTeam && GetHitDice(oPC) > 10) {
          if(nCTeam == 1)
            CreateItemOnObject("celestial", oPC, 1);
          if(nCTeam == 2)
            CreateItemOnObject("demonicsceptor", oPC, 1);
        }

        if(nPCTeam == nCTeam) {
          CreateItemOnObject("victorygem", oPC, 1);
        }

        oPC = GetNextPC();
    }

    WrathOfTheGods(nDamagedTeam);
    FuryOfTheTitans(nDamagedTeam);
    SetLocalInt(OBJECT_SELF, "m_nGoodScore", 0);
    SetLocalInt(OBJECT_SELF, "m_nEvilScore", 0);

  }

  if(sSpeak != "")
    ActionSpeakString(sSpeak, TALKVOLUME_SHOUT);

}