#include "nw_i0_generic"
#include "x0_i0_petrify"
#include "onpercept_dark"
#include "prc_inc_util"

void Normalize(object oPC)
{
effect eEffect1 = EffectEthereal();
RemoveEffectOfType(oPC, GetEffectType(eEffect1));
}


void JoinBackedTeam(object oPC)
{
 int iDone = 0;
 object oCreature;
 object oCreature2;
 object oTeam;
 string sSide;
 int iTeam = GetLocalInt(oPC, "team_bet");

oCreature = GetFirstObjectInArea(OBJECT_SELF);
while (GetIsObjectValid(oCreature))
      {
       if ((GetObjectType(oCreature)==OBJECT_TYPE_CREATURE)&&(!GetIsPC(oCreature))&&
          (GetTag(oCreature)!="starfall")&&(GetTag(oCreature)!="reaper"))
          {
           SetIsTemporaryEnemy(oPC, oCreature);
          }
      oCreature =GetNextObjectInArea(OBJECT_SELF);
     }

oCreature = GetFirstObjectInArea(OBJECT_SELF);
while ((GetObjectType(oCreature)!=OBJECT_TYPE_CREATURE)||
      (!GetIsEnemy(oCreature, oPC))||(GetIsPC(oCreature))||
      (GetTag(oCreature)=="starfall")||(GetTag(oCreature)=="reaper"))
      {
       oCreature =GetNextObjectInArea(OBJECT_SELF);
      }
//FloatingTextStringOnCreature(GetName(OBJECT_SELF), oPC);
//FloatingTextStringOnCreature(GetName(oCreature), oPC);

if (((GetResRef(oCreature)=="dragon1")||(GetResRef(oCreature)=="zep_halfdrafn001")||
   (GetResRef(oCreature)=="zep_marilithb001")||(GetResRef(oCreature)=="bard2")||
   (GetResRef(oCreature)=="zep_pitfiend001")||(GetResRef(oCreature)=="zep_balrog001")||
   (GetResRef(oCreature)=="dopple")||(GetResRef(oCreature)=="mistress2")||
   (GetResRef(oCreature)=="notime3")||(GetResRef(oCreature)=="notime4")||
   (GetResRef(oCreature)=="darcher")||(GetResRef(oCreature)=="death"))&&(iTeam==2))
   {
    AdjustReputation(oPC, oCreature, 100);
    SetIsTemporaryFriend(oPC, oCreature);
    oTeam = GetFirstFactionMember(oCreature, FALSE);
    while (GetIsObjectValid(oTeam))
        {
       SetIsTemporaryFriend(oPC, oTeam, FALSE);
       oTeam = GetNextFactionMember(oCreature, FALSE);
      }
   }
else if (((GetResRef(oCreature)=="dragon1")||(GetResRef(oCreature)=="zep_halfdrafn001")||
   (GetResRef(oCreature)=="zep_marilithb001")||(GetResRef(oCreature)=="bard2")||
   (GetResRef(oCreature)=="zep_pitfiend001")||(GetResRef(oCreature)=="zep_balrog001")||
   (GetResRef(oCreature)=="dopple")||(GetResRef(oCreature)=="mistress2")||
   (GetResRef(oCreature)=="notime3")||(GetResRef(oCreature)=="notime4")||
   (GetResRef(oCreature)=="darcher")||(GetResRef(oCreature)=="death"))&&(iTeam==1))
   {
    oCreature2 = GetFirstObjectInArea(OBJECT_SELF);
    while ((GetObjectType(oCreature2)!=OBJECT_TYPE_CREATURE)||
          (!GetIsEnemy(oCreature, oCreature2))||(GetIsPC(oCreature2))||
      (GetTag(oCreature2)=="starfall")||(GetTag(oCreature2)=="reaper"))
      {
       oCreature2 =GetNextObjectInArea(OBJECT_SELF);
      }
      AdjustReputation(oPC, oCreature2, 100);
    SetIsTemporaryFriend(oPC, oCreature2);
    oTeam = GetFirstFactionMember(oCreature2, FALSE);
    while (GetIsObjectValid(oTeam))
        {
       SetIsTemporaryFriend(oPC, oTeam, FALSE);
       oTeam = GetNextFactionMember(oCreature2, FALSE);
      }
   }
else if (((GetResRef(oCreature)!="dragon1")&&(GetResRef(oCreature)!="zep_halfdrafn001")&&
   (GetResRef(oCreature)!="zep_marilithb001")&&(GetResRef(oCreature)!="bard2")&&
   (GetResRef(oCreature)!="zep_pitfiend001")&&(GetResRef(oCreature)!="zep_balrog001")&&
   (GetResRef(oCreature)!="dopple")&&(GetResRef(oCreature)!="mistress2")&&
   (GetResRef(oCreature)!="notime3")&&(GetResRef(oCreature)!="notime4")&&
   (GetResRef(oCreature)!="darcher")&&(GetResRef(oCreature)!="death"))&&(iTeam==1))
   {
    AdjustReputation(oPC, oCreature, 100);
    SetIsTemporaryFriend(oPC, oCreature);
    oTeam = GetFirstFactionMember(oCreature, FALSE);
    while (GetIsObjectValid(oTeam))
      {
       SetIsTemporaryFriend(oPC, oTeam, FALSE);
       oTeam = GetNextFactionMember(oCreature, FALSE);
      }
   }
else if (((GetResRef(oCreature)!="dragon1")&&(GetResRef(oCreature)!="zep_halfdrafn001")&&
   (GetResRef(oCreature)!="zep_marilithb001")&&(GetResRef(oCreature)!="bard2")&&
   (GetResRef(oCreature)!="zep_pitfiend001")&&(GetResRef(oCreature)!="zep_balrog001")&&
   (GetResRef(oCreature)!="dopple")&&(GetResRef(oCreature)!="mistress2")&&
   (GetResRef(oCreature)!="notime3")&&(GetResRef(oCreature)!="notime4")&&
   (GetResRef(oCreature)!="darcher")&&(GetResRef(oCreature)!="death"))&&(iTeam==2))
   {
    oCreature2 = GetFirstObjectInArea(OBJECT_SELF);
    while ((GetObjectType(oCreature2)!=OBJECT_TYPE_CREATURE)||
          (!GetIsEnemy(oCreature, oCreature2))||(GetIsPC(oCreature2))||
          (GetTag(oCreature2)=="starfall")||(GetTag(oCreature2)=="reaper"))
          {
           oCreature2 =GetNextObjectInArea(OBJECT_SELF);
          }
    AdjustReputation(oPC, oCreature2, 100);
    SetIsTemporaryFriend(oPC, oCreature2);
    oTeam = GetFirstFactionMember(oCreature2, FALSE);
    while (GetIsObjectValid(oTeam))
        {
         SetIsTemporaryFriend(oPC, oTeam, FALSE);
         oTeam = GetNextFactionMember(oCreature2, FALSE);
        }
    //FloatingTextStringOnCreature("fell through", oPC);
   }
}



void JoinTeam2(object oSelf, object oPC)
{
 int iDone = 0;
 object oCreature;
 object oTeam;
 string sSide;

oCreature = GetFirstObjectInArea(OBJECT_SELF);
while ((GetObjectType(oCreature)!=OBJECT_TYPE_CREATURE)||
      (!GetIsEnemy(oCreature, oSelf))||(GetIsPC(oCreature))||
      (GetTag(oCreature)=="starfall")||(GetTag(oCreature)=="reaper"))
      {
       oCreature =GetNextObjectInArea(OBJECT_SELF);
      }
//FloatingTextStringOnCreature(GetName(OBJECT_SELF), oPC);
//FloatingTextStringOnCreature(GetName(oCreature), oPC);

if ((GetResRef(oCreature)=="dragon1")||(GetResRef(oCreature)=="zep_halfdrafn001")||
   (GetResRef(oCreature)=="zep_marilithb001")||(GetResRef(oCreature)=="bard2")||
   (GetResRef(oCreature)=="zep_pitfiend001")||(GetResRef(oCreature)=="zep_balrog001")||
   (GetResRef(oCreature)=="dopple")||(GetResRef(oCreature)=="mistress2")||
   (GetResRef(oCreature)=="notime3")||(GetResRef(oCreature)=="notime4")||
   (GetResRef(oCreature)=="darcher")||(GetResRef(oCreature)=="death"))
   {
    sSide = "You have joined the Champions of Darkness";
    SetLocalInt(oPC, "pc_side", 2);
   }
else
   {
    sSide = "You have joined the Champions of Light";
    SetLocalInt(oPC, "pc_side", 1);
   }

AdjustReputation(oPC, oCreature, 100);
SetIsTemporaryFriend(oPC, oCreature);

oTeam = GetFirstFactionMember(oCreature, FALSE);
while (GetIsObjectValid(oTeam))
      {
       //AdjustReputation(oPC, oTeam, 100);
       SetIsTemporaryFriend(oPC, oTeam, FALSE);
       oTeam = GetNextFactionMember(oCreature, FALSE);
      }
FloatingTextStringOnCreature(sSide, oPC);
}

void LastHit2()
{

    object oMod = GetModule();
    object oPC = GetLastDamager(OBJECT_SELF);
    int oDam = GetTotalDamageDealt();
    string oAtt;
    string oSelf;
    string oMon;
    object oDest;
    object oLast;
    int oHit;
    int oSwing;
    int oMhit;
    int oPCdam;
    int oSDTaken;
    int oUDTaken;
    int oSDDealt;
    int oUDDealt;
    int iSFhit;
    int iSFdam;
    int iFBhit;
    int iFBdam;
    int oHWdam;
    int oHWhit;
    int iCHdam;
    int iCHhit;
    int mCount;
    int iCHswing;
    int iHWswing;
    int iPCswing;
    int iPChit;
    int iSFswing;
    int iFBswing;
    int iArenaChall =GetLocalInt(oMod, "challenge");
    int iSFDuel =GetLocalInt(oMod, "duel_on");
    int iFinalBattle = GetLocalInt(oPC, "final_on");
    int iWarWon = GetLocalInt(oMod, "war_won");
    int iJoinSwitch = GetLocalInt(oPC, "join_switch");
    int iHwar = GetLocalInt(oMod, "hwar_on");
    //FloatingTextStringOnCreature("duel on:  "+IntToString(GetLocalInt(oMod, "duel_on")), oPC);

    oSwing = GetLocalInt(oPC, "pc_swing");
    iPChit = GetLocalInt(oPC, "pc_hit");
    iCHswing = GetLocalInt(oPC, "pc_CHswing");
    iSFswing = GetLocalInt(oPC, "pc_SFswing");
    iFBswing = GetLocalInt(oPC, "pc_FBswing");
    iHWswing = GetLocalInt(oPC, "pc_HWswing");





    if (GetIsPC(oPC))
       {
        oHit = GetLocalInt(oPC, "pc_hit");
        oPCdam = GetLocalInt(oPC, "player_dd");
        ++oHit;
        oPCdam+=oDam;
        if (oHit>oSwing){oHit=oSwing;}
        SetLocalInt(oPC, "pc_hit", oHit);
        SetLocalInt(oPC, "player_dd", oPCdam);
        if (GetName(GetLastWeaponUsed(oPC))=="")
               {
                iPCswing = GetLocalInt(oPC, "pc_swing");
                ++iPCswing;
                if (oHit>iPCswing){oHit=iPCswing;}
                SetLocalInt(oPC, "pc_hit", oHit);
                SetLocalInt(oPC, "pc_swing", iPCswing);
               }
        //FloatingTextStringOnCreature("hwar on:"+IntToString(iHwar), oPC);
        //FloatingTextStringOnCreature("war won:"+IntToString(iWarWon), oPC);
        if (iArenaChall==1)
           {
            iCHdam = GetLocalInt(oPC, "pc_CHdam");
            iCHhit = GetLocalInt(oPC, "pc_CHhit");
            iCHdam+=oDam;
            ++iCHhit;
            if (iCHhit>iCHswing){iCHhit=iCHswing;}
            SetLocalInt(oPC, "pc_CHhit", iCHhit);
            SetLocalInt(oPC, "pc_CHdam", iCHdam);
            if (GetName(GetLastWeaponUsed(oPC))=="")
               {
                iCHswing = GetLocalInt(oPC, "pc_CHswing");
                iCHhit = GetLocalInt(oPC, "pc_CHhit");
                ++iCHswing;
                if (iCHhit>iCHswing){iCHhit=iCHswing;}
                SetLocalInt(oPC, "pc_CHhit", iCHhit);
                SetLocalInt(oPC, "pc_CHswing", iCHswing);
               }
           }
        if (iSFDuel==1)
           {
            iSFdam = GetLocalInt(oPC, "pc_SFdam");
            iSFhit = GetLocalInt(oPC, "pc_SFhit");
            iSFdam+=oDam;
            ++iSFhit;
            if (iSFhit>iSFswing){iSFhit=iSFswing;}
            SetLocalInt(oPC, "pc_SFhit", iSFhit);
            SetLocalInt(oPC, "pc_SFdam", iSFdam);
            if (GetName(GetLastWeaponUsed(oPC))=="")
               {
                iSFswing = GetLocalInt(oPC, "pc_SFswing");
                iSFhit = GetLocalInt(oPC, "pc_SFhit");
                ++iSFswing;
                if (iSFhit>iSFswing){iSFhit=iSFswing;}
                SetLocalInt(oPC, "pc_SFhit", iSFhit);
                SetLocalInt(oPC, "pc_SFswing", iSFswing);
               }
           }
        if (iFinalBattle==1)
           {
            iFBdam = GetLocalInt(oPC, "pc_FBdam");
            iFBhit = GetLocalInt(oPC, "pc_FBhit");
            iFBdam+=oDam;
            ++iFBhit;
            if (iFBhit>iFBswing){iFBhit=iFBswing;}
            SetLocalInt(oPC, "pc_FBhit", iFBhit);
            SetLocalInt(oPC, "pc_FBdam", iFBdam);
            if (GetName(GetLastWeaponUsed(oPC))=="")
               {
                iFBswing = GetLocalInt(oPC, "pc_FBswing");
                iFBhit = GetLocalInt(oPC, "pc_FBhit");
                ++iFBswing;
                if (iFBhit>iFBswing){iFBhit=iFBswing;}
                SetLocalInt(oPC, "pc_FBhit", iFBhit);
                SetLocalInt(oPC, "pc_FBswing", iFBswing);
               }
           }
        if (iHwar==1)
           {
            mCount = 0;
            oHWdam = GetLocalInt(oPC, "pc_HWdam");
            oHWhit = GetLocalInt(oPC, "pc_HWhit");
            ++oHWhit;
            oHWdam+=oDam;
            if (oHWhit>iHWswing){oHWhit=iHWswing;}
            SetLocalInt(oPC, "pc_HWhit", oHWhit);
            SetLocalInt(oPC, "pc_HWdam", oHWdam);
            if (GetName(GetLastWeaponUsed(oPC))=="")
               {
                iHWswing = GetLocalInt(oPC, "pc_HWswing");
                ++iHWswing;
                if (oHWhit>iHWswing){oHWhit=iHWswing;}
                SetLocalInt(oPC, "pc_HWhit", oHWhit);
                SetLocalInt(oPC, "pc_HWswing", iHWswing);
               }
           if (iWarWon==1)
              {
               oLast = GetFirstObjectInArea(GetArea(oPC));
               while (GetIsObjectValid(oLast))
                     {
                      if ((GetObjectType(oLast)==OBJECT_TYPE_CREATURE)&&(!GetIsPC(oLast))&&(!GetIsDead(oLast))
                         &&(GetTag(oLast)!="starfall")&&(GetTag(oLast)!="reaper"))
                          {
                           ++mCount;
                          }
                      oLast = GetNextObjectInArea(GetArea(oPC));
                     }
              //FloatingTextStringOnCreature("m count:"+IntToString(mCount), oPC);
              if (mCount==0)
                 {
                  SetLocalInt(oMod, "challenge", 0);
                  SetLocalInt(oMod, "d1", 0);
                  SetLocalInt(oMod, "d2", 0);
                  SetLocalInt(oMod, "d2a", 0);
                  SetLocalInt(oMod, "d3", 0);
                  SetLocalInt(oMod, "d4", 0);
                  SetLocalInt(oMod, "d5", 0);
                  SetLocalInt(oMod, "ch_hw", 0);
                  SetLocalInt(oMod, "ch_dr", 0);
                  SetLocalInt(oMod, "ch_bl", 0);
                  SetLocalInt(oMod, "ch_pf", 0);
                  SetLocalInt(oMod, "ch_df", 0);
                  SetLocalInt(oMod, "ch_pm", 0);
                  SetLocalInt(oMod, "ch_bm", 0);
                  SetLocalInt(oMod, "ch_lm", 0);
                  SetLocalInt(oMod, "ch_ll", 0);
                  SetLocalInt(oMod, "ch_xx", 0);
                  SetLocalInt(oMod, "gr_xx", 0);
                  SetLocalInt(oMod, "sw_hw", 0);
                  SetLocalInt(oMod, "sw_dr", 0);
                  SetLocalInt(oMod, "sw_bl", 0);
                  SetLocalInt(oMod, "sw_pf", 0);
                  SetLocalInt(oMod, "sw_df", 0);
                  SetLocalInt(oMod, "sw_pm", 0);
                  SetLocalInt(oMod, "sw_bm", 0);
                  SetLocalInt(oMod, "sw_lm", 0);
                  SetLocalInt(oMod, "sw_ll", 0);
                  SetLocalInt(oMod, "sw_xx", 0);
                  SetLocalInt(oMod, "hwar_on", 0);
                  DelayCommand(3.0, FloatingTextStringOnCreature
                  ("** You are the last one standing **", oPC));
                 }
             }
           }


       }
}

void LastHit()
{
 object oKiller = GetLastKiller();
 object oSelf = OBJECT_SELF;
 string sKiller = GetName(oKiller);
 string sVictim = GetName(oSelf);
 string sAnnounce;

if (sKiller ==""){sKiller = "An unseen force";}

// generate a random death message

int iDice = d10();
switch(iDice)
      {
       case 1: {sAnnounce = sKiller+" sends "+sVictim+" into early retirement!";}break;
       case 2: {sAnnounce = sKiller+" paints the pavement red with "+sVictim+"!";}break;
       case 3: {sAnnounce = sKiller+" dices "+sVictim+" into small pieces!";}break;
       case 4: {sAnnounce = sKiller+"'s face is the last thing that "+sVictim+" sees!";}break;
       case 5: {sAnnounce = sVictim+" doesn't survive "+sKiller+"'s brutal attack!";}break;
       case 6: {sAnnounce = sKiller+" decapitates "+sVictim+"!";}break;
       case 7: {sAnnounce = sKiller+" cuts "+sVictim+" clean in half!";}break;
       case 8: {sAnnounce = sKiller+" helps "+sVictim+" to the pavement!";}break;
       case 9: {sAnnounce = sKiller+" tears "+sVictim+" a new asshole!";}break;
       case 10: {sAnnounce = sKiller+" slices "+sVictim+" like an onion!";}break;
      }
if (oKiller==OBJECT_INVALID){sAnnounce = sVictim+" dies from a fatal wound!";}
// send message to all players

object oPlayer = GetFirstPC();
while (GetIsObjectValid(oPlayer))
      {
       SendMessageToPC(oPlayer, sAnnounce);
       oPlayer = GetNextPC();
      }
 DelayCommand(3.2, LastHit2());
}


void QuickRestore(object oPC)
{
object oCaster;
oCaster = oPC;

object oTarget;
oTarget = oPC;

AssignCommand(oCaster, ActionCastSpellAtObject(SPELL_GREATER_RESTORATION, oTarget, METAMAGIC_ANY, TRUE, 20, PROJECTILE_PATH_TYPE_DEFAULT, TRUE));
}




void DeathStats(object oPC)
{
object oMod = GetModule();
int oDam = GetTotalDamageDealt();

int oHWhit = GetLocalInt(oPC, "pc_HWhit");
int oHit = GetLocalInt(oPC, "pc_hit");
int oPCdam = GetLocalInt(oPC, "player_dd");
int oHWdam = GetLocalInt(oPC, "pc_HWdam");
int iCHhit = GetLocalInt(oPC, "pc_CHhit");
int iCHdam = GetLocalInt(oPC, "pc_CHdam");
int iSFhit = GetLocalInt(oPC, "pc_SFhit");
int iSFdam = GetLocalInt(oPC, "pc_SFdam");
int iFBhit = GetLocalInt(oPC, "pc_FBhit");
int iFBdam = GetLocalInt(oPC, "pc_FBdam");
int iArenaChall = GetLocalInt(oMod, "challenge");
int iSFDuel = GetLocalInt(oMod, "duel_on");
int iFinalBattle = GetLocalInt(oPC, "final_on");

SetLocalInt(oPC, "pc_HWhit", ++oHWhit);
SetLocalInt(oPC, "pc_hit", ++oHit);
SetLocalInt(oPC, "player_dd", oPCdam+oDam);
SetLocalInt(oPC, "pc_HWdam", oHWdam+oDam);

if (iArenaChall==1)
   {
    SetLocalInt(oPC, "pc_CHhit", ++iCHhit);
    SetLocalInt(oPC, "pc_CHdam", iCHdam+oDam);
   }
if (iSFDuel==1)
   {
    SetLocalInt(oPC, "pc_SFhit", ++iSFhit);
    SetLocalInt(oPC, "pc_SFdam", iSFdam+oDam);

   }
if (iFinalBattle==1)
   {
    SetLocalInt(oPC, "pc_FBhit", ++iFBhit);
    SetLocalInt(oPC, "pc_FBdam", iFBdam+oDam);
   }
}


void spawner(object oPC, string oCreature, int oDrag)
{

    object oTarget;
    if (oDrag==1){oTarget=GetWaypointByTag("big_wp");}
    if (oDrag==0){oTarget = GetWaypointByTag("monster_wp");}
    if (oDrag==2){oTarget = GetWaypointByTag("starfall_wp");}
    location lTarget = GetLocation(oTarget);
    object oSpawn = CreateObject(OBJECT_TYPE_CREATURE, oCreature, lTarget);
    oTarget = oSpawn;
    if (oDrag!=2)
    {
    SetIsTemporaryEnemy(oPC, oTarget);
    AssignCommand(oTarget, ActionAttack(oPC));
    AssignCommand(oTarget, DetermineCombatRound(oPC));

   oTarget = oSpawn;
   int 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)));
   }
}


void DoEffect(float oDelay, int oEffect, string oWaypoint)
{
object oTarget;
oTarget = GetObjectByTag(oWaypoint);
int nInt;
nInt = GetObjectType(oTarget);
effect eEffect;
eEffect = EffectVisualEffect(oEffect);
if (nInt != OBJECT_TYPE_WAYPOINT)
   DelayCommand(oDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT,
   eEffect, oTarget));
else
   DelayCommand(oDelay, ApplyEffectAtLocation(DURATION_TYPE_INSTANT,
   eEffect, GetLocation(oTarget)));
}

 void ResetPools(object oPC)
{

   object oMod = GetModule();
   object oP1 = GetObjectByTag("pool1");
   object oP2 = GetObjectByTag("pool2");
   object oP3 = GetObjectByTag("fountain1");
   object oP4 = GetObjectByTag("fountain2");
   object oLever1 = GetObjectByTag("pool_lever");
   object oLever2 = GetObjectByTag("fount_lever");
   effect eEffect = EffectVisualEffect(VFX_DUR_GLOBE_INVULNERABILITY);
   AssignCommand(oPC, PlaySound("sce_negative"));
   AssignCommand(oLever1, ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE));
   AssignCommand(oLever2, ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE));
   RemoveEffectOfType(oP1, GetEffectType(eEffect));
   RemoveEffectOfType(oP2, GetEffectType(eEffect));
   RemoveEffectOfType(oP3, GetEffectType(eEffect));
   RemoveEffectOfType(oP4, GetEffectType(eEffect));
   SetLocalInt(oMod, "fountain_state", 0);
   SetLocalInt(oMod, "pool_state", 0);
}
void PCEffect(float oDelay, int oEffect, object oPC)
{
effect eEffect;
eEffect = EffectVisualEffect(oEffect);
DelayCommand(oDelay, ApplyEffectAtLocation(DURATION_TYPE_INSTANT,
             eEffect, GetLocation(oPC)));
}

void SoundPlay(float oDelay, string oSound, object oPC)
{
DelayCommand(oDelay, AssignCommand(oPC, PlaySound(oSound)));
}


void Message (float oDelay, string oMSG, object oPC)
{
DelayCommand(oDelay, FloatingTextStringOnCreature(oMSG, oPC));
}

void CastPC(float oDelay, int eEffect, object oPC)
{
DelayCommand(oDelay, AssignCommand(oPC, ActionCastSpellAtObject
             (eEffect, oPC, METAMAGIC_ANY, TRUE, 20,
              PROJECTILE_PATH_TYPE_DEFAULT, TRUE)));
}

void BattleRest (object oPC)
{
    DelayCommand(5.0, PRCForceRest(oPC));
    PCEffect(5.0,VFX_FNF_NATURES_BALANCE, oPC);
    Message(7.0, "Rest Granted", oPC);
    //CastPC(9.0, SPELL_ETHEREALNESS, oPC);
    //Message(9.0, "Buff phase initiated: Buff or attack now", oPC);
}

void Place(string oPlaceable, string oWaypoint)
{
location lTarget;
object oTarget = GetWaypointByTag(oWaypoint);
lTarget = GetLocation(oTarget);
object oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, oPlaceable, lTarget);
}

void PlaceCreature(string oPlaceable, string oWaypoint)
{
location lTarget;
object oTarget = GetWaypointByTag(oWaypoint);
lTarget = GetLocation(oTarget);
object oSpawn = CreateObject(OBJECT_TYPE_CREATURE, oPlaceable, lTarget);
}


void Cast(int nSpell, object oPlayer)
{
AssignCommand(oPlayer,
ActionCastSpellAtObject(
nSpell,
oPlayer,
METAMAGIC_ANY,
TRUE,
20,
PROJECTILE_PATH_TYPE_DEFAULT,
TRUE));
}

void Sound (float oDelay, string oSound, object oPC)
{
DelayCommand(oDelay, AssignCommand(oPC, PlaySound(oSound)));
}


void NPCvoice(object oSelf, object oPC, int iSet)
{

int iVoice;
float oDice;

 switch (iSet)
      {
       case 1: iVoice = VOICE_CHAT_ATTACK;break;
       case 2: iVoice = VOICE_CHAT_BATTLECRY1;break;
       case 3: iVoice = VOICE_CHAT_BATTLECRY2;break;
       case 4: iVoice = VOICE_CHAT_BATTLECRY3;break;
       case 5: iVoice = VOICE_CHAT_THREATEN;break;
       case 6: iVoice = VOICE_CHAT_CHEER;break;
       case 7: iVoice = VOICE_CHAT_CUSS;break;
       case 8: iVoice = VOICE_CHAT_FLEE;break;
       case 9: iVoice = VOICE_CHAT_GOODBYE;break;
       case 10: iVoice = VOICE_CHAT_THANKS;break;
       case 11: iVoice = VOICE_CHAT_WEAPONSUCKS;break;
       case 12: iVoice = VOICE_CHAT_LAUGH;break;
      }
oDice = IntToFloat(d2());
oDice +=0.2;
AssignCommand(oSelf, SetFacingObject(oPC));
DelayCommand(oDice+0.5, AssignCommand(oSelf, PlayVoiceChat(iVoice)));
}



void LeaveTeam(object oPC)
{
object oCreature;
object oPlayer;

oCreature = GetFirstObjectInArea(OBJECT_SELF);

while ((GetObjectType(oCreature)!=OBJECT_TYPE_CREATURE)||
      (GetIsPC(oCreature))||(GetTag(oCreature)=="starfall")||
       (GetTag(oCreature)=="reaper"))
      {
       oCreature = GetNextObjectInArea(OBJECT_SELF);
      }
oPlayer = GetFirstPC();
while (GetIsObjectValid(oPlayer))
    {
     //AdjustReputation(oPC, oCreature, -100);
     SetIsTemporaryEnemy(oPC, oCreature, FALSE);
     SetLocalInt(oPC, "join_switch", 0);
     oPlayer = GetNextPC();
    }
}


void TeamCheer()
{
object oCreature;
object oTeam;
float oDelay;
float oDelay2;

oCreature = GetFirstObjectInArea(OBJECT_SELF);

while ((GetObjectType(oCreature)!=OBJECT_TYPE_CREATURE)||
       (GetIsPC(oCreature))||(GetTag(oCreature)=="starfall")||
       (GetTag(oCreature)=="reaper"))
      {
       oCreature =GetNextObjectInArea(OBJECT_SELF);
      }

oTeam = GetFirstFactionMember(oCreature, FALSE);

while (GetIsObjectValid(oTeam))
      {
       if (!GetIsDead(oTeam))
          {
           if ((GetResRef(OBJECT_SELF)=="death")|| (GetResRef(OBJECT_SELF)=="bard2")||
              (GetResRef(OBJECT_SELF)=="darcher")||(GetResRef(OBJECT_SELF)=="notime2")
              ||(GetResRef(OBJECT_SELF)=="div2")||(GetResRef(OBJECT_SELF)=="pfiend2")
              ||(GetResRef(OBJECT_SELF)=="horod2")||(GetResRef(OBJECT_SELF)=="pwar3")
              ||(GetResRef(OBJECT_SELF)=="notime1"))
              {
               oDelay = IntToFloat(d3()+3);
               oDelay2 = oDelay - 0.2;
               DelayCommand(oDelay, AssignCommand(oTeam, ClearAllActions()));
               DelayCommand(oDelay, AssignCommand(oTeam, ActionPlayAnimation
               (ANIMATION_FIREFORGET_VICTORY3)));
               DelayCommand(oDelay2, PlayVoiceChat(VOICE_CHAT_CHEER, oTeam));
              }
           else
              {
               oDelay = IntToFloat(d3()+3);
               oDelay2 = oDelay - 0.2;
               DelayCommand(oDelay, AssignCommand(oTeam, ClearAllActions()));
               DelayCommand(oDelay, AssignCommand(oTeam, ActionPlayAnimation
               (ANIMATION_FIREFORGET_TAUNT)));
               DelayCommand(oDelay2, PlayVoiceChat(VOICE_CHAT_CHEER, oTeam));
              }
          }
       oTeam = GetNextFactionMember(oCreature, FALSE);
      }
}


void HolyScore(object oSelf)
{

// initialiise objects

object oMod = GetModule();
object oPC = GetFirstPC();
object oCreature = oSelf;
object oKiller = GetLastKiller();
object oHost = GetObjectByTag("starfall");
int oDam = GetTotalDamageDealt();
object oPlayer;
object oTeam;
object oPC2;
// initialise integers

int iDice;
int iKills;
int iSCount = 0;
int iPCSide;
int oBonus;
int iModCount;
int iHWkills;
int oHWhit;
int oHWdam;
int oHeaven = GetLocalInt(oMod, "heav_score");
int oHell = GetLocalInt(oMod, "hell_score");
int oHeavloss = GetLocalInt(oMod, "heav_loss");
int oHellloss = GetLocalInt(oMod, "hell_loss");
int iHvcas = GetLocalInt(oMod, "heav_cas");
int iHlcas = GetLocalInt(oMod, "hell_cas");
int iHvTotCas = GetLocalInt(oMod, "iHvTotCas");
int iHlTotCas = GetLocalInt(oMod, "iHlTotCas");
int iMostKills = GetLocalInt(oMod, "MostKills");
int iHWPCkillTot = GetLocalInt(oPC, "iHWPCkillTot");
int iDfire = GetLocalInt(oMod, "iDfire");
int iTeamBet = GetLocalInt(oPC, "team_bet");
int iWager = GetLocalInt(oPC, "wager");
int iTWwager = GetLocalInt(oPC, "TWwager");
int iTLwager = GetLocalInt(oPC, "TLwager");
int iOdds = GetLocalInt(oPC, "iOdds");
int iOdds2 = GetLocalInt(oPC, "iOdds2");
int iNPCduel = GetLocalInt(oPC, "npc_duel");
int iWager2;

// initialise strings

string sCHbet = GetLocalString(oPC, "CHbet");
string sKiller;
string sVictim;
string sChamp;
string sTop;
string sAnnounce;
string sRes;
string sRes2;
string sRes3;
string mRes;

int iXploit = GetLocalInt(oMod, "xcheck");

// get module variables for stat tracking
if  (iXploit==1){return;}


sChamp = GetLocalString(oMod, "sChamp");
sKiller = GetName(oKiller);
sVictim = GetName(oSelf);

if (sKiller ==""){sKiller = "An unseen God";}
if (sKiller =="The Arena of Champions"){sKiller = "The Phoenix-Dragon";}
// generate a random death message

iDice = d8();
switch(iDice)
      {
       case 1: {sAnnounce = sKiller+" sends "+sVictim+" into early retirement!";}break;
       case 2: {sAnnounce = sKiller+" paints the pavement red with "+sVictim+"!";}break;
       case 3: {sAnnounce = sKiller+" dices "+sVictim+" into small pieces!";}break;
       case 4: {sAnnounce = sKiller+"'s face is the last thing that "+sVictim+" sees!";}break;
       case 5: {sAnnounce = sVictim+" doesn't survive "+sKiller+"'s brutal attack!";}break;
       case 6: {sAnnounce = sKiller+" decapitates "+sVictim+"!";}break;
       case 7: {sAnnounce = sKiller+" cuts "+sVictim+" clean in half!";}break;
       case 8: {sAnnounce = sKiller+" helps "+sVictim+" to the pavement!";}break;
      }
if (oKiller==OBJECT_INVALID){sAnnounce = sVictim+" dies from a fatal wound.";}
// send message to all players

oPlayer = GetFirstPC();
while (GetIsObjectValid(oPlayer))
      {
       SendMessageToPC(oPlayer, sAnnounce);
       oPlayer = GetNextPC();
      }

// add to holy war kill totals for PC scoreboard

if (GetIsPC(oKiller))
   {
    ++iHWPCkillTot;
    SetLocalInt(oKiller, "iHWPCkillTot", iHWPCkillTot);
   }

// assign integers to each monsters resref for stat tracking

sRes = GetResRef(OBJECT_SELF);
sRes2 = GetResRef(oKiller);
sRes+="_x";
sRes2+="_k";
sRes3 = GetResRef(oKiller)+"_HWk";
iKills = GetLocalInt(oMod, sRes2);
iHWkills = GetLocalInt(oMod, sRes3);
++iKills;
++iHWkills;
SetLocalInt(oMod, sRes3, iHWkills);
SetLocalInt(oMod, sRes2, iKills);
SetLocalString(oMod, sRes, "  [Dead]");

// check to see if the highest kill score has been beaten

if (iHWkills>iMostKills)
   {
    SetLocalInt(oMod, "MostKills", iKills);
    sTop = GetName(oKiller);
    iModCount = GetLocalInt(oMod, "champ_count");
    ++iModCount;
    SetLocalInt(oMod, "champ_count", iModCount);
    if (!GetIsPC(oKiller))
       {
        oHWdam = GetLocalInt(oPC, "pc_HWdam");
        oHWhit = GetLocalInt(oPC, "pc_HWhit");
        ++oHWhit;
        oHWdam+=oDam;
        SetLocalInt(oPC, "pc_HWhit", oHWhit);
        SetLocalInt(oPC, "pc_HWdam", oHWdam);
        mRes = GetResRef(oKiller);
        mRes +="_ch";
        SetLocalInt(oMod, mRes,iModCount);
       }

    oPC2 = GetFirstPC();
    while (GetIsObjectValid(oPC2))
          {
           if (sTop==GetName(oPC2))
              {
               SetLocalInt(oPC2, "champ_won", 1);
              }
           else
              {
               SetLocalInt(oPC2, "champ_won", 0);
              }
          oPC2 = GetNextPC();
         }
    sChamp = sTop+" is the match champion with ";
    sChamp += IntToString(iHWkills);
    sChamp += " kills!";
    SetLocalString(oMod, "sChamp", sChamp);
    if (iHWkills==1){sChamp = "The top kill-count was only "+IntToString(iHWkills)+
       " kill. "+sTop+" scored the first kill and is therefore awarded the championship.";}


   }

// count the remaining members of the creature just killed's faction

oTeam = GetFirstFactionMember(oSelf, FALSE);
while (GetIsObjectValid(oTeam))
      {
       if (!GetIsDead(oTeam))
          {
           ++iSCount;
          }
       oTeam = GetNextFactionMember(oSelf, FALSE);
      }

// if there are no survivors on the team of the victim - finish the match
//Message(3.0, IntToString(iSCount), oPC);
//Message(3.0, IntToString(iDfire), oPC);

if ((iSCount==0)&&(iDfire!=1))
   {

    if ((GetResRef(oCreature)=="dragon1")||(GetResRef(oCreature)=="zep_halfdrafn001")||
    (GetResRef(oCreature)=="zep_marilithb001")||(GetResRef(oCreature)=="bard2")||
    (GetResRef(oCreature)=="zep_pitfiend001")||(GetResRef(oCreature)=="zep_balrog001")||
    (GetResRef(oCreature)=="dopple")||(GetResRef(oCreature)=="mistress2")||
    (GetResRef(oCreature)=="notime3")||(GetResRef(oCreature)=="notime4")||
    (GetResRef(oCreature)=="darcher")||(GetResRef(oCreature)=="death"))
       {
        SetLocalInt(oMod, "iDfire", 1);
        oPC = GetFirstPC();
        while (GetIsObjectValid(oPC))
        {
         iPCSide = GetLocalInt(oPC, "pc_side");
         switch (iPCSide)
         {
          case 0: {
                   if (iTeamBet==1)
                      {
                       Normalize(oPC);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       iWager2=iWager*2;
                       iTWwager+=iWager2-iWager;
                       SetLocalInt(oPC, "TWwager", iTWwager);
                       SetLocalInt(oPC, "winnings", iWager);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       AssignCommand(oPC, ClearAllActions());
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_FIREFORGET_VICTORY3));
                       Message(6.0, "** You won "+IntToString(iWager2)+" gold: Collect at the tote **", oPC);
                      }
                   else if (iTeamBet==2)
                      {
                       iTLwager+=iWager;
                       SetLocalInt(oPC, "TLwager", iTLwager);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       SetLocalInt(oPC, "winnings", 0);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       AssignCommand(oPC, ClearAllActions());
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                       Message(6.0, "** You lost the bet **", oPC);
                      }
                    else if (iTeamBet==3)
                      {
                       if (sTop==sCHbet)
                          {
                            SetLocalInt(oPC, "bet_confirmed", 0);
                            Normalize(oPC);
                            iWager2 =(iWager*iOdds);
                            iTWwager+=iWager2-iWager;
                            SetLocalInt(oPC, "TWwager", iTWwager);
                            SetLocalInt(oPC, "winnings", iWager2);
                            SetLocalInt(oPC, "wager", 0);
                            SetLocalInt(oPC, "team_bet", 0);
                            AssignCommand(oPC, ClearAllActions());
                            AssignCommand(oPC, ActionPlayAnimation
                            (ANIMATION_FIREFORGET_VICTORY3));
                            Message(6.0, "** You won "+IntToString(iWager2)+" gold: Collect at the tote **", oPC);
                          }
                       else
                          {
                           iTLwager+=iWager;
                           SetLocalInt(oPC, "TLwager", iTLwager);
                           SetLocalInt(oPC, "bet_confirmed", 0);
                           SetLocalInt(oPC, "winnings", 0);
                           SetLocalInt(oPC, "wager", 0);
                           SetLocalInt(oPC, "team_bet", 0);
                           AssignCommand(oPC, ClearAllActions());
                           AssignCommand(oPC, ActionPlayAnimation
                           (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                           Message(6.0, "** You lost the bet **", oPC);
                          }
                      }
                    else if (iTeamBet==4)
                      {
                       if (sTop==sCHbet)
                          {
                           Normalize(oPC);
                           SetLocalInt(oPC, "bet_confirmed", 0);
                           iWager2 =(iWager*iOdds2);
                           iTWwager+=iWager2-iWager;
                           SetLocalInt(oPC, "TWwager", iTWwager);
                           SetLocalInt(oPC, "winnings", iWager2);
                           SetLocalInt(oPC, "wager", 0);
                           SetLocalInt(oPC, "team_bet", 0);
                           AssignCommand(oPC, ClearAllActions());
                           AssignCommand(oPC, ActionPlayAnimation
                           (ANIMATION_FIREFORGET_VICTORY3));
                           Message(6.0, "** You won "+IntToString(iWager2)+" gold: Collect at the tote **", oPC);
                          }
                       else
                          {
                           iTLwager+=iWager;
                           SetLocalInt(oPC, "TLwager", iTLwager);
                           SetLocalInt(oPC, "bet_confirmed", 0);
                           SetLocalInt(oPC, "winnings", 0);
                           SetLocalInt(oPC, "wager", 0);
                           SetLocalInt(oPC, "team_bet", 0);
                           AssignCommand(oPC, ClearAllActions());
                           AssignCommand(oPC, ActionPlayAnimation
                           (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                           Message(6.0, "** You lost the bet **", oPC);
                          }
                      }
                   else
                      {
                       AssignCommand(oPC, ClearAllActions());
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_LOOPING_TALK_LAUGHING, 1.0f, 4.0f));
                      }
                  }break;

          case 1: {
                   if (iTeamBet!=0)
                      {
                       Normalize(oPC);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       Message(6.0, "** You forfeited the bet by joining a team**", oPC);
                       iTLwager+=iWager;
                       SetLocalInt(oPC, "TLwager", iTLwager);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       SetLocalInt(oPC, "winnings",0);
                      }
                    AssignCommand(oPC, ActionPlayAnimation
                   (ANIMATION_FIREFORGET_VICTORY2));
                  }break;
          case 2: {
                   if (iTeamBet!=0)
                      {
                       Normalize(oPC);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       Message(6.0, "** You forfeited the bet by joining a team**", oPC);
                       iTLwager+=iWager;
                       SetLocalInt(oPC, "TLwager", iTLwager);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       SetLocalInt(oPC, "winnings",0);
                      }
                   AssignCommand(oPC, ClearAllActions());
                   AssignCommand(oPC, ActionPlayAnimation
                   (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                  }break;
         }
         if ((iTeamBet==4)||(iNPCduel==1))
            {
             //SetLocalInt(oPC, "npc_duel", 0);
             Message(3.0, "** "+sTop+" has prevailed **", oPC);
            }
         else
            {
             Message(2.0, "*** The army of light has prevailed ***", oPC);
             Message(4.0, sChamp, oPC);
            }
         if ((GetLocalInt(oPC, "champ_won")==1)&&(iTeamBet==0)&&(iNPCduel!=1))
            {
             oBonus = 50000;
             SendMessageToPC(oPC, "Match championship prize money: "+IntToString(oBonus)+" gold");
             GiveGoldToCreature(oPC, oBonus);
             PlaySound("it_coins");
            }
         else {SetLocalInt(oPC, "champ_won", 0);}
         oPC = GetNextPC();
        }
        ++oHeaven;
        ++oHellloss;
        ++iHlcas;
        ++iHlTotCas;
        SetLocalInt(oMod, "hell_cas", iHlcas);
        SetLocalInt(oMod, "iHlTotCas", iHlTotCas);
        SetLocalInt(oMod, "heav_score", oHeaven);
        SetLocalInt(oMod, "hell_loss", oHellloss);
        SetLocalInt(oMod, "war_won", 1);
        //AssignCommand(oHost, SpeakString("The divine Tyriel and his holy warriors have been victorious!", TALKVOLUME_SHOUT));
        DelayCommand(1.0, TeamCheer());
        DelayCommand(2.5, AddJournalQuestEntry("war_heav", 1, oPC, FALSE, FALSE));
        return;
       }
    else
       {
         SetLocalInt(oMod, "iDfire", 1);
         oPC = GetFirstPC();
        while (GetIsObjectValid(oPC))
        {
         iPCSide = GetLocalInt(oPC, "pc_side");
         switch (iPCSide)
         {
          case 0: {
                   if (iTeamBet==2)
                      {
                       Normalize(oPC);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       iWager2=iWager*2;
                       iTWwager+=iWager2-iWager;
                       SetLocalInt(oPC, "TWwager", iTWwager);
                       SetLocalInt(oPC, "winnings", iWager);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       AssignCommand(oPC, ClearAllActions());
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_FIREFORGET_VICTORY3));
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_FIREFORGET_VICTORY3));
                       Message(6.0, "** You won "+IntToString(iWager2)+" gold: Collect at the tote **", oPC);
                      }
                   else if (iTeamBet==1)
                      {
                       iTLwager+=iWager;
                       SetLocalInt(oPC, "TLwager", iTLwager);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       SetLocalInt(oPC, "winnings", 0);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       AssignCommand(oPC, ClearAllActions());
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                       Message(6.0, "** You lost the bet **", oPC);
                      }
                    else if (iTeamBet==3)
                      {
                       if (sTop==sCHbet)
                          {
                            Normalize(oPC);
                            SetLocalInt(oPC, "bet_confirmed", 0);
                            iWager2 = (iWager*iOdds);
                            iTWwager+=iWager2-iWager;
                            SetLocalInt(oPC, "TWwager", iTWwager);
                            SetLocalInt(oPC, "winnings", iWager2);
                            SetLocalInt(oPC, "wager", 0);
                            SetLocalInt(oPC, "team_bet", 0);
                            AssignCommand(oPC, ClearAllActions());
                            AssignCommand(oPC, ActionPlayAnimation
                            (ANIMATION_FIREFORGET_VICTORY3));
                            Message(6.0, "** You won "+IntToString(iWager2)+" gold: Collect at the tote **", oPC);
                          }
                       else
                          {
                           iTLwager+=iWager;
                           SetLocalInt(oPC, "TLwager", iTLwager);
                           SetLocalInt(oPC, "bet_confirmed", 0);
                           SetLocalInt(oPC, "winnings", 0);
                           SetLocalInt(oPC, "wager", 0);
                           SetLocalInt(oPC, "team_bet", 0);
                           AssignCommand(oPC, ClearAllActions());
                           AssignCommand(oPC, ActionPlayAnimation
                           (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                           Message(6.0, "** You lost the bet **", oPC);
                          }
                      }
                    else if (iTeamBet==4)
                      {
                       if (sTop==sCHbet)
                          {
                           Normalize(oPC);
                           SetLocalInt(oPC, "bet_confirmed", 0);
                           iWager2 =(iWager*iOdds2);
                           iTWwager+=iWager2-iWager;
                           SetLocalInt(oPC, "TWwager", iTWwager);
                           SetLocalInt(oPC, "winnings", iWager2);
                           SetLocalInt(oPC, "wager", 0);
                           SetLocalInt(oPC, "team_bet", 0);
                           AssignCommand(oPC, ClearAllActions());
                           AssignCommand(oPC, ActionPlayAnimation
                           (ANIMATION_FIREFORGET_VICTORY3));
                           Message(6.0, "** You won "+IntToString(iWager2)+" gold: Collect at the tote **", oPC);
                          }
                       else
                          {
                           iTLwager+=iWager;
                           SetLocalInt(oPC, "TLwager", iTLwager);
                           SetLocalInt(oPC, "bet_confirmed", 0);
                           SetLocalInt(oPC, "winnings", 0);
                           SetLocalInt(oPC, "wager", 0);
                           SetLocalInt(oPC, "team_bet", 0);
                           AssignCommand(oPC, ClearAllActions());
                           AssignCommand(oPC, ActionPlayAnimation
                           (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                           Message(6.0, "** You lost the bet **", oPC);
                          }
                      }
                   else
                      {
                       AssignCommand(oPC, ClearAllActions());
                       AssignCommand(oPC, ActionPlayAnimation
                       (ANIMATION_LOOPING_TALK_LAUGHING, 1.0f, 4.0f));
                      }
                  }break;
          case 1: {
                   if (iTeamBet!=0)
                      {
                       Normalize(oPC);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       Message(6.0, "** You forfeited the bet by joining a team**", oPC);
                       iTLwager+=iWager;
                       SetLocalInt(oPC, "TLwager", iTLwager);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       SetLocalInt(oPC, "winnings",0);
                      }
                   AssignCommand(oPC, ClearAllActions());
                   AssignCommand(oPC, ActionPlayAnimation
                   (ANIMATION_LOOPING_TALK_FORCEFUL, 1.0f, 8.0f));
                  }break;
          case 2: {
                   if (iTeamBet!=0)
                      {
                       Normalize(oPC);
                       SetLocalInt(oPC, "bet_confirmed", 0);
                       Message(6.0, "** You forfeited the bet by joining a team**", oPC);
                       iTLwager+=iWager;
                       SetLocalInt(oPC, "TLwager", iTLwager);
                       SetLocalInt(oPC, "wager", 0);
                       SetLocalInt(oPC, "team_bet", 0);
                       SetLocalInt(oPC, "winnings",0);
                      }
                   AssignCommand(oPC, ClearAllActions());
                   AssignCommand(oPC, ActionPlayAnimation
                   (ANIMATION_FIREFORGET_VICTORY2));
                   }break;

         }
         if ((iTeamBet==4)||(iNPCduel==1))
            {
             //SetLocalInt(oPC, "npc_duel", 0);
             Message(3.0, "** "+sTop+" has prevailed **", oPC);
            }
         else
            {
             Message(2.0, "*** The army of darkness has prevailed ***", oPC);
             Message(4.0, sChamp, oPC);
            }
         if ((GetLocalInt(oPC, "champ_won")==1)&&(iTeamBet==0)&&(iNPCduel!=1))
            {
             oBonus = 50000;
             SendMessageToPC(oPC, "Match championship prize money: "+IntToString(oBonus)+" gold");
             GiveGoldToCreature(oPC, oBonus);
             PlaySound("it_coins");
            }
         else {SetLocalInt(oPC, "champ_won", 0);}
         oPC = GetNextPC();
        }
        ++oHell;
        ++oHeavloss;
        ++iHvcas;
        ++iHvTotCas;
        SetLocalInt(oMod, "heav_cas", iHvcas);
        SetLocalInt(oMod, "iHvTotCas", iHvTotCas);
        SetLocalInt(oMod, "hell_score", oHell);
        SetLocalInt(oMod, "heav_loss", oHeavloss);
        SetLocalInt(oMod, "war_won", 1);
        //AssignCommand(oHost, SpeakString("The Dark Lord and his minions have been victorious!", TALKVOLUME_SHOUT));
        DelayCommand(1.0, TeamCheer());
        DelayCommand(2.5, AddJournalQuestEntry("war_hell", 1, oPC, FALSE, FALSE));
        return;
       }
   }
else
   {
    if ((GetResRef(oCreature)=="dragon1")||(GetResRef(oCreature)=="zep_halfdrafn001")||
    (GetResRef(oCreature)=="zep_marilithb001")||(GetResRef(oCreature)=="bard2")||
    (GetResRef(oCreature)=="zep_pitfiend001")||(GetResRef(oCreature)=="zep_balrog001")||
    (GetResRef(oCreature)=="dopple")||(GetResRef(oCreature)=="mistress2")||
    (GetResRef(oCreature)=="notime3")||(GetResRef(oCreature)=="notime4")||
    (GetResRef(oCreature)=="darcher")||(GetResRef(oCreature)=="death"))
       {
        ++iHlTotCas;
        ++iHlcas;
        SetLocalInt(oMod, "hell_cas", iHlcas);
        SetLocalInt(oMod, "iHlTotCas", iHlTotCas);
        return;
       }
    else
       {
        ++iHvcas;
        ++iHvTotCas;
        SetLocalInt(oMod, "iHvTotCas", iHvTotCas);
        SetLocalInt(oMod, "heav_cas", iHvcas);
        return;
       }
   }

}



//void main () {}