﻿//==============================================================================
// Hailmary v. 1.4
//   by *XYZ*SaYnt
// $Id$
//==============================================================================
//
// Update History:
//
// Version 1.1
// * allow the T's to set the wire color connected to the detonator.
//
// Version 1.2
// * user configurable odds for wire pulling
// * reduced chat spam
// * made printouts prettier
//
// Version 1.3
// * compatibility with CheapBombTimer
//
// Version 1.4
// * A little cleanup of the code
// * removed an annoying extra message to chat
// * added a "verbosity" user setting to control how much hailmary talks to you
// * user customizable chat prefix

block config
{
   // set this variable to 1 if you want the terrorists to be able to 
   // choose the wire connected to the detonator (in other words, the
   // wire that CT's have to cut to quick-defuse the bomb)
   // set this variable to 0 if you just want a simple random number
   // generator to pick the wire.
   es_xsetinfo HM_TSELECT 1

   // multiplier for the odds when defusing without a defuse kit (pulling a wire).  The odds
   // of success become 1:(4*HM_MULTIPLIER).
   // so, if you want a CT to be able to have the same odds whether pulling or cutting, set
   // this to 1.  To make it nearly impossible for CTs to successfully pull a wire, set this
   // to 100.  By default it is 2, making the odds 1:8.

   es_xsetinfo HM_MULTIPLIER 2
   
   // set the verbosity level.  If you feel like HailMary spams too much, change
   // this setting appropriately.
   //
   // 0 = never say anything at all
   // 1 = just the bare necessities - remind the CT's when the bomb is planted
   //     that they can cut a wire to defuse (1 line), and also tell info on
   //     wire correctness when a wire is cut.
   // 2 = all printouts in (1), but with two lines for the CT reminder, and
   //     a notice when a terrorist selects a wire.
   // 3 = all printouts in (2), but with notices when the bomb planter or
   //     defuser are killed.
   //
   es_xsetinfo HM_VERBOSITY 3
   
   // the prefix to place in front of chat from the hailmary addon
   es_xsetinfo HM_CHATPREFIX [HM]
}
//==============================================================================








 
// !!!!!!!!!! DO NOT EDIT BELOW THIS LINE !!!!!!!!!!!!

// TODO: is there a way to make bots cut a wire, if there is not enough time to defuse, and
// make them do it intelligently?
 
 
block load
{
  es_xsetinfo HM_VERSION 1.4BETA

  es_xsetinfo hailmary_version 1.5
  es_makepublic hailmary_version

	// import the user's configs
	es_xdoblock hailmary/config

	// needed for popup menus
	es_load corelib
	es_load popup

	// create the popup menu for the CTs
	popup create HM_MENU
	popup addline HM_MENU "Válasz egy drótot, vagy folytasd a hatástalanítást:"
	popup addline HM_MENU "[0] Semmi"
	popup addline HM_MENU "[1] Piros"
	popup addline HM_MENU "[2] Zöld"
	popup addline HM_MENU "[3] Kék"
	popup addline HM_MENU "[4] Sárga"
	popup select HM_MENU 0 hailmary/nowire
	popup select HM_MENU 1 hailmary/redwire
	popup select HM_MENU 2 hailmary/greenwire
	popup select HM_MENU 3 hailmary/bluewire
	popup select HM_MENU 4 hailmary/yellowwire

        // create a popup menu for the Ts
	popup create HM_MENU_T
	popup addline HM_MENU_T "Válassz egy drótot, hogy élesítsd a detonátort:"
	popup addline HM_MENU_T "[0] Véletlenszerű"
	popup addline HM_MENU_T "[1] Piros"
	popup addline HM_MENU_T "[2] Zöld"
	popup addline HM_MENU_T "[3] Kék"
	popup addline HM_MENU_T "[4] Sárga"
	popup select HM_MENU_T 0 hailmary/nowire_t
	popup select HM_MENU_T 1 hailmary/redwire_t
	popup select HM_MENU_T 2 hailmary/greenwire_t
	popup select HM_MENU_T 3 hailmary/bluewire_t
	popup select HM_MENU_T 4 hailmary/yellowwire_t

	//es_xmsg #green Hailmary addon loaded.

        es_xdoblock hailmary/inithm

}


block unload
{
	es_xdoblock hailmary/killpopup 
	popup delete HM_MENU
	popup delete HM_MENU_T
}


block inithm
{
	es_xsetinfo HM_BOMBENTITY 0
	es_xsetinfo HM_DEFUSERID -1
	es_xsetinfo HM_PLANTERID -1
        es_xsetinfo HM_DEFUSERNAME "NONAMETAS"
        es_xsetinfo HM_PLANTERNAME "NONAMETAS"
	es_xsetinfo HM_BOMBPOS 0
	es_xsetinfo HM_CUTWIRE 0
	es_xsetinfo HM_WIRE 0 
	es_xsetinfo HM_WIRECOLOR "none" 
	es_xsetinfo HM_ISDEAD 0
	es_xsetinfo HM_DEFUSERHASKIT 0
	es_xsetinfo HM_CUTWIRECOLOR "none"
	es_xsetinfo HM_ACTION "cut"
	es_xsetinfo HM_EXPLODING 0
	es_xsetinfo HM_PULLODDS 4
	es_math HM_PULLODDS * server_var(HM_MULTIPLIER)
}

event round_start
{
  es_xdoblock hailmary/inithm
}


event round_end
{
  // just an extra cleanup; probably not needed but....
  es_xdoblock hailmary/killpopup 
}


event bomb_beginplant
{
	// use a random number generator to set the wire color that will successfully defuse the bomb.  This
  // is so that if the planter does not choose a wire, one is randomly
  // chosen for him.
	es_xrand HM_WIRE 1 4
  es_xdoblock hailmary/setHM_WIRECOLOR

  // save off who is doing the planting
  es_setinfo HM_PLANTERNAME event_var(es_username)
  es_setinfo HM_PLANTERID event_var(userid)

  // send a menu to the planter, to let them choose the wire color that
  // will detonate  
  if (server_var(HM_TSELECT) != 0) do
    {
        es popup send HM_MENU_T event_var(userid)
    }
}

event bomb_abortplant
{
  es_xsetinfo HM_PLANTERNAME "noname"
  es_xsetinfo HM_PLANTERID -1
  es_xdoblock hailmary/killpopup
}


event bomb_planted
{
        // take down the popup; the T has lost his chance to choose wires.
        es_xsetinfo HM_PLANTERNAME "noname"
        es_xsetinfo HM_PLANTERID -1 
        es_xdoblock hailmary/killpopup

        // remind the CTs that they can defuse by cutting or pulling wires.
        if (server_var(HM_VERBOSITY) > 0) do
        {
          es_msg #multi #green server_var(HM_CHATPREFIX) #default CTk: hatástalanítanotok kell a bombát a detonátor elvágásával.
        }
        if (server_var(HM_VERBOSITY) > 1) do
        {
          es_msg #multi #green server_var(HM_CHATPREFIX) #default Ha rossz drótot vágtok el a bomba felrobban.
        }
        
        // DEBUG
        //es_msg You should cut the server_var(HM_WIRECOLOR) wire, wire server_var(HM_WIRE)
}


event bomb_begindefuse
{
  es_xgetentityindex HM_BOMBENTITY planted_c4	
  es_getindexprop HM_BOMBPOS server_var(HM_BOMBENTITY) "CPlantedC4.baseclass.baseclass.m_vecOrigin"

  // save the player ID that is doing the defusing.
  es_setinfo HM_DEFUSERID event_var(userid)
  es_setinfo HM_DEFUSERNAME event_var(es_username)
  es_setinfo HM_DEFUSERHASKIT event_var(haskit)

  // here, pop the menu that allows the player to choose whether or not 
  // to cut a wire.  If they have a kit, they can cut the wire.  If they do
  // not, they have to pull the wire, which gives a 50/50 chance of setting off
  // the bomb even if it is the right wire.
  if (event_var(haskit) == 0) do
  {
    popup modline HM_MENU 1 "Válasz egy drótot a kihúzáshoz, vagy folytasd a hatástalanítást:"
    es_xsetinfo HM_ACTION "kihúzva"
  }
  else do
  {
     popup modline HM_MENU 1 "Válasz egy drótot az elvágáshoz, vagy folytasd a hatástalanítást:"
     es_xsetinfo HM_ACTION "elvágva"
  }

  es popup send HM_MENU event_var(userid)

}




event bomb_abortdefuse
{
   es_getplayerprop HM_ISDEAD event_var(es_username) "CCSPlayer.baseclass.pl.deadflag"
   if (server_var(HM_ISDEAD) == 0) do
    {
      if (server_var(HM_EXPLODING) == 0) do 
      {
        if (server_var(HM_VERBOSITY) > 2) do
        {
          es_msg #multi #green server_var(HM_CHATPREFIX) #default event_var(es_username) leállította a bomba felszedését.
        }
      }
    }

  // take down the menu.
  
  es_xdoblock hailmary/killpopup 

  es_xsetinfo HM_DEFUSERID -1
  es_xsetinfo HM_DEFUSERNAME "NONAMETAS"
  es_xsetinfo HM_DEFUSERHASKIT 0

}


event player_death
{
	if (event_var(euserid) == server_var(HM_DEFUSERID)) do
  {
     if (server_var(HM_VERBOSITY) > 2) do
     {
	      es_msg #multi #green server_var(HM_CHATPREFIX) #default A hatástalanító event_var(es_username) meghalt! 	
	   }
     es_xdoblock hailmary/killpopup 
  }

	if (event_var(userid) == server_var(HM_PLANTERID)) do
  {
    if (server_var(HM_VERBOSITY) > 2) do
     {
	     es_msg #multi #green server_var(HM_CHATPREFIX) #default Az élesítő event_var(es_username) meghalt! 	
	   }
    es_xdoblock hailmary/killpopup 
  }
  
}
 

 


block nowire_t
{
  es_xdoblock hailmary/killpopup 
  es_xdoblock hailmary/setwire
}


block redwire_t
{
  es_xsetinfo HM_WIRE 1
  es_xdoblock hailmary/setHM_WIRECOLOR
  es_xdoblock hailmary/setwire
}

block greenwire_t
{
  es_xsetinfo HM_WIRE 2
  es_xdoblock hailmary/setHM_WIRECOLOR
  es_xdoblock hailmary/setwire
}

block bluewire_t
{
  es_xsetinfo HM_WIRE 3
  es_xdoblock hailmary/setHM_WIRECOLOR
  es_xdoblock hailmary/setwire
}

block yellowwire_t
{
  es_xsetinfo HM_WIRE 4
  es_xdoblock hailmary/setHM_WIRECOLOR
  es_xdoblock hailmary/setwire
}


block nowire
{
  es_xdoblock hailmary/killpopup 
}

block redwire
{
  es_xsetinfo HM_CUTWIRE 1
  es_xsetinfo HM_CUTWIRECOLOR "piros"
  es_xdoblock hailmary/cutwire
}

block greenwire
{
  es_xsetinfo HM_CUTWIRE 2
  es_xsetinfo HM_CUTWIRECOLOR "zöld"
  es_xdoblock hailmary/cutwire
}

block bluewire
{
  es_xsetinfo HM_CUTWIRE 3
  es_xsetinfo HM_CUTWIRECOLOR "kék"
  es_xdoblock hailmary/cutwire
}

block yellowwire
{
  es_xsetinfo HM_CUTWIRE 4
  es_xsetinfo HM_CUTWIRECOLOR "sárga"
  es_xdoblock hailmary/cutwire
}


block setwire
{
  if (server_var(HM_VERBOSITY) > 1) do
  {
    es_msg #multi #green server_var(HM_CHATPREFIX) #default server_var(HM_PLANTERNAME) a drótot kiválasztotta.
  }
  //es_msg and that wire is server_var(HM_WIRECOLOR)  
}



block cutwire 
{
   if (server_var(HM_DEFUSERID) > -1) do  // if the defuser is defined
   {
      es_getplayerprop HM_ISDEAD server_var(HM_DEFUSERID) "CCSPlayer.baseclass.pl.deadflag"
      if (server_var(HM_ISDEAD) == 0) do  // and if the defuser is not dead
      { 
         if (server_var(HM_VERBOSITY) > 0) do
         {
           es_msg #multi #green server_var(HM_CHATPREFIX) #default server_var(HM_DEFUSERNAME) server_var(HM_ACTION) a server_var(HM_CUTWIRECOLOR) drótot.
         }
         //es_msg right wire is server_var(HM_WIRE), cut wire is server_var(HM_CUTWIRE)

         // if the wire that was cut and the detonator wire match, then the
         // bomb is instantly defused, UNLESS the defuser does not have a kit.
         // In that case, the defuser pulled the wire, which means that there
         // is a 50-50 chance it will still detonate, even though he chose the
         // correct wire.
         if (server_var(HM_CUTWIRE) == server_var(HM_WIRE)) do
          {
            es_xsetinfo HM_DICEROLL 1
            es_xsetinfo HM_DESCRIBE "a wire cut (1:4 odds)"
            if (server_var(HM_DEFUSERHASKIT) == 0) do
             {                       
               es_rand HM_DICEROLL 1 server_var(HM_MULTIPLIER)      
               //es_msg rolling the dice... server_var(HM_DICEROLL)
 	             //es_format HM_DESCRIBE a gutsy wire pull (1:server_var(HM_PULLODDS) odds)  
               es_format HM_DESCRIBE "a gutsy wire pull (1:%1 odds)" server_var(HM_PULLODDS) 
             }
            if (server_var(HM_DICEROLL) == 1) do
               {
                 // defuse the bomb immediately
                if (server_var(HM_VERBOSITY) > 0) do
                {
                   es_msg #multi #green server_var(HM_CHATPREFIX) #default A bomba hatástalanítva server_var(HM_DESCRIBE) a server_var(HM_DEFUSERNAME)
                }
                es_setindexprop server_var(HM_BOMBENTITY) "CPlantedC4.m_flDefuseCountDown" 1.0
               }
            else do
            {
 	            // explode the bomb
              es_xsetinfo CheapBombTimer_TotalBeeps -1
 	            es_xsetinfo HM_EXPLODING 1
 	            if (server_var(HM_VERBOSITY) > 0) do
 	            {
                es_msg #multi #green server_var(HM_CHATPREFIX) #default Ez volt a helyes drót, de a drót további húzásával váltotta ki a bombát.  BOOOOOM!
              }
              es_setindexprop server_var(HM_BOMBENTITY) "CPlantedC4.m_flC4Blow" 1.0
            }
          }
          else do
          {
            // explode the bomb
            es_xsetinfo HM_EXPLODING 1
            es_xsetinfo CheapBombTimer_TotalBeeps -1  
            if (server_var(HM_VERBOSITY) > 0) do
            {          
              es_msg #multi #green server_var(HM_CHATPREFIX) #default A server_var(HM_WIRECOLOR) drót volt a detonátor.   BOOOOOM!
            }
            es_setindexprop server_var(HM_BOMBENTITY) "CPlantedC4.m_flC4Blow" 1.0
          }
      }
  }
}





// the only purpose of this function is to map a numerical color id to
// a text color, so that we can output to screen which color was the
// right one.
block setHM_WIRECOLOR
{
  if (server_var(HM_WIRE) == 1) do
  {
    es_xsetinfo HM_WIRECOLOR piros
  }
  else do
  {
    if (server_var(HM_WIRE) == 2) do
    {
      es_xsetinfo HM_WIRECOLOR zöld
    }
    else do
    {
      if (server_var(HM_WIRE) == 3) do
      {
        es_xsetinfo HM_WIRECOLOR kék
      }
      else do
      {
        if (server_var(HM_WIRE) == 4) do
        {
          es_xsetinfo HM_WIRECOLOR sárga
        }
        else do 
        {
           es_xsetinfo HM_WIRECOLOR Unknown
        }
      }
    }
  }
}




block killpopup
{
  //es popup close HM_MENU server_var(HM_DEFUSERNAME)
  es popup unsendname HM_MENU #all
  es popup unsendname HM_MENU_T #all
}
