//==============================================================================
// 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 "Продолжайте или обрежте провод:"
	popup addline HM_MENU "[0] Продолжить"
	popup addline HM_MENU "[1] Красный"
	popup addline HM_MENU "[2] Зеленый"
	popup addline HM_MENU "[3] Голубой"
	popup addline HM_MENU "[4] Желтый"
	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 "Выберите провод:"
	popup addline HM_MENU_T "[0] Случайный выбор"
	popup addline HM_MENU_T "[1] Красный"
	popup addline HM_MENU_T "[2] Зеленый"
	popup addline HM_MENU_T "[3] Голубой"
	popup addline HM_MENU_T "[4] Желтый"
	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 "никакой" 
	es_xsetinfo HM_ISDEAD 0
	es_xsetinfo HM_DEFUSERHASKIT 0
	es_xsetinfo HM_CUTWIRECOLOR "никакой"
	es_xsetinfo HM_ACTION "разрез"
	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 CTs: Вы можете быстро обезвредить бомбу, обрезав провод.
        }
        if (server_var(HM_VERBOSITY) > 1) do
        {
          es_msg #multi #green server_var(HM_CHATPREFIX) #default Если вы обрежете не тот провод, бомба сразу же взорвется.
        }
        
        // 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 "Продолжайте или обрежте провод:"
    es_xsetinfo HM_ACTION "обрезать"
  }
  else do
  {
     popup modline HM_MENU 1 "Продолжайте или обрежте провод:"
     es_xsetinfo HM_ACTION "обрезать"
  }

  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) только что перестал обезвреживать бомбу.
        }
      }
    }

  // 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 Тот, кто разряжал бомбу event_var(es_username), был убит! 	
	   }
     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 Тот, кто установил бомбу event_var(es_username), был убит! 	
	   }
    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 "red"
  es_xdoblock hailmary/cutwire
}

block greenwire
{
  es_xsetinfo HM_CUTWIRE 2
  es_xsetinfo HM_CUTWIRECOLOR "green"
  es_xdoblock hailmary/cutwire
}

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

block yellowwire
{
  es_xsetinfo HM_CUTWIRE 4
  es_xsetinfo HM_CUTWIRECOLOR "yellow"
  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) выбрал провод для детонатора.
  }
  //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) server_var(HM_CUTWIRECOLOR) провод.
         }
         //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 The bomb has been defused by server_var(HM_DESCRIBE) by 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 Это был правильный выбор, но вы не успели!
              }
              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 server_var(HM_WIRECOLOR) провод был проводом детонатора ;(
            }
            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 red
  }
  else do
  {
    if (server_var(HM_WIRE) == 2) do
    {
      es_xsetinfo HM_WIRECOLOR green
    }
    else do
    {
      if (server_var(HM_WIRE) == 3) do
      {
        es_xsetinfo HM_WIRECOLOR blue
      }
      else do
      {
        if (server_var(HM_WIRE) == 4) do
        {
          es_xsetinfo HM_WIRECOLOR yellow
        }
        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
}
