// ******************************
// Blade's Anticamp
//
// * Description:
//	- camping is allowed for cts if the Bomb has been droped
//	- camping is allowed for Terrors on Hostage maps
//	- camping is allowed for Terrors if the Bomb has been planted 
//	- camper will loose healthpoints every 2 sec.
//	- camping is allowed for player with 15 healthpoints
//
// * Install instructions:
//       1. Install Mattie's EventScripts plugins:
//            http://mattie.info/cs
//
//       2. Extract the file in:
//           /addons/eventscripts/anticamp/es_anticamp.txt
//
//       3. Add the following line somewhere in autoexec.cfg:
//           es_load anticamp
//
block config
{	
// *********************************************************************

	// This sound will be emitted from camper with the camp message
	// Leave blank to disable this option
	detect_sound "HL1/fvox/bell.wav"
	
	// Beacon sound
	// Leave blank to disable this option
	beacon_sound "buttons/button17.wav" 
	
	// 0 = No player movement, with player movement
	slap_mode 0
	
	// Minimum health where camping is allowed
	min_health 15

	// Amount of cash decrease during camping every 2 sec
	takecash 100

	// Amount of health decrease during camping every 2 sec
	health_slap 6

// *********************************************************************

	// Set the timer when the punishments should be triggered -
	// after the camptime has passed. Do not use floats! 
	// To disable set to -1
	slap "3" 	
	cash "5" 
	blind "10" 	
	strip "-1"
	beacon "0" 	
	credits "-1"	
	message "0" // can be fired several times	

// *********************************************************************

	// Define camp permission for CT on desired maps. Leave blank to revoke
	allow_t_camp ""
	
	// Define camp permission for T on desired maps. Leave bland to revoke
	allow_ct_camp "ad,eg"
	
	// Exclude from anticamp
	// 0 = nothing, 1 = humans, 2 = bots
	exclude 0
	
	// Set this value to change the camptime in seconds
	// Do not use floats!
	camptime 10 	

	// Default radius is 200 ~ 2.5m = 8ft
	campradius 200 	
	
	// Activates anticamp on round start, if specified time has elapsed.
	start_timer 0
}	

block load
{
	// init vars
	es_xset x 0	
	es_xset y 0		
	es_xset z 0	
	es_xset fA 0
	es_xset tpx 0		
	es_xset tpy 0		
	es_xset tpz 0	
	es_xset _str 0
	es_xset slap 0
	es_xset cash 0
	es_xset ac_id 0
	es_xset _temp 0
	es_xset blind 0
	es_xset strip 0
	es_xset _index 0
	es_xset _rand1 0
	es_xset _rand2 0
	es_xset beacon 0
	es_xset message 0	
	es_xset exclude 0
	es_xset credits 0		
	es_xset ac_name 0
	es_xset ac_team 0		
	es_xset ac_timer 0
	es_xset ac_class 0
	es_xset camptime 0
	es_xset _tempent 0
	es_xset ac_isbot 0
	es_xset ac_sound 0
	es_xset takecash 0
	es_xset ac_mapcat 0
	es_xset slap_mode 0
	es_xset ac_weapon 0
	es_xset ac_return 0
	es_xset ac_health 0
	es_xset campradius 0
	es_xset ac_counter 0	
	es_xset minplayer 0
	es_xset min_health 0
	es_xset _punishment 0
	es_xset health_slap 0
	es_xset start_timer 0
	es_xset _start_timer 0
	es_xset beacon_sound 0	
	es_xset detect_sound 0	
	es_xset allow_t_camp 0
	es_xset allow_ct_camp 0
	es_xset anticamp_mode 0
	
	// load tables
	es_xset anticamp "1.4.53"
	es_xmakepublic anticamp
	es_xkeygroupcreate anticamp
	es_xkeygroupload mapconfig |anticamp

	// init punishments	
	alias _slap "es_xdoblock anticamp/slap"
	alias _cash "es_xdoblock anticamp/cash"
	alias _blind "es_xdoblock anticamp/blind"
	alias _strip "es_xdoblock anticamp/strip"
	alias _beacon "es_xdoblock anticamp/beacon"
	alias _credits "es_xdoblock anticamp/credits"
	alias _message "es_xdoblock anticamp/message"	
	
        // format msg
	es_xset campmsg "\x01Player:\x03 %s1\x01 is camping with\x04 %s2" 
	es_xstring campmsg replace "\x01" "" 
	es_xstring campmsg replace "\x03" "" 
        es_xstring campmsg replace "\x04" ""	
}

block unload
{
	es_xexists _temp keygroup anticamp
	ifx true(_temp) do
	{
	  es_xkeygroupdelete anticamp
	}	
	es_xexists _temp keygroup userinfo
	ifx true(_temp) do
	{
	  es_xkeygroupdelete userinfo
	}			
}

event es_map_start
{	
	es_xset ac_team 0
	es_token ac_mapcat server_var(eventscripts_currentmap) 1 _
	
	// allow terrors to camp on hostage maps
	if (server_var(ac_mapcat) in server_var(allow_t_camp)) do
	{
	  es_xset ac_team 2
	}

	// allow ct's to camp on demolition maps
	if (server_var(ac_mapcat) in server_var(allow_ct_camp)) do
	{
	  es_xset ac_team 3
	}

	// reset main loop
	es_xexists _temp keygroup userinfo
	ifx false(_temp) do
	{
	  es_xcreateplayerlist userinfo
	}
	es_xkeygroupdelete anticamp
	es_xkeygroupcreate anticamp
	
	// map configs
	es_xkeygroupdelete mapconfig
	es_xkeygroupload mapconfig |anticamp
	
	// load config
	es_xdoblock anticamp/config
	es_xmathparse camptime "camptime*-1"

	// load punishment sequence
	es_xset _punishment ";_slap;_cash;_blind;_strip;_beacon;_credits;_message"
	if (server_var(slap) == -1)  then es_xstring _punishment replace ";_slap"
	if (server_var(cash) == -1) then es_xstring _punishment replace ";_cash"
	if (server_var(blind) == -1) then es_xstring _punishment replace ";_blind"
	if (server_var(strip) == -1) then es_xstring _punishment replace ";_strip"
	if (server_var(beacon) == -1) then es_xstring _punishment replace ";_beacon"
	if (server_var(credits) == -1) then es_xstring _punishment replace ";_credits"
	if (server_var(message) == -1) then es_xstring _punishment replace ";_message"
	
	// load map config
	es_xset _temp 0
	es_xset anticamp_mode 1
	es_exists _temp key mapconfig server_var(eventscripts_currentmap)
	ifx true(_temp) do
	{
	  es_foreachval _temp in mapconfig server_var(eventscripts_currentmap) "es_xdoblock anticamp/map_config"	  
	}
	
	ifx parse("camptime >= 1") do
	{
	  es_xmathparse camptime "camptime*-1"
	}

	// start anticamp
	es alias do_punish server_var(_punishment)
	es_xdoblock anticamp/check_player
}

block map_config
{
	es_keygetvalue _str mapconfig server_var(eventscripts_currentmap) server_var(_temp)
	es_xformatv _str "%1 %2" _temp _str
	es_xcommandv _str
}

block check_player
{
	// command order
	ifx true(anticamp_mode) do
	{
	  ifx true(_start_timer) do
	  {
	    es_xmath _start_timer - 1
	  }
	  else do
	  {
	    es_xkeygroupdelete userinfo
	    es_xcreateplayerlist userinfo
	    es_xforeachkey ac_id in anticamp "es_xdoblock anticamp/verify_vector"  
	  }
	}
	es_xdelayed 1 es_xdoblock anticamp/check_player
}

block verify_vector
{		
	es_keygetvalue ac_class userinfo server_var(ac_id) teamid
	ifx parse("ac_class != ac_team") do
	{
	  // get user health
	  es_keygetvalue ac_health userinfo server_var(ac_id) health
	  ifx parse("ac_health > min_health") do
	  {
	    // get current location
	    es_getplayerprop fA server_var(ac_id) "CBaseEntity.m_vecOrigin"
	    es_splitvectorstring x y z server_var(fA)	  

	    // get old vector
	    es_keygetvalue _str anticamp server_var(ac_id) vectorstr
	    es_splitvectorstring tpx tpy tpz server_var(_str)
	    
	    // check old vector
	    ifx parse("x == tpx") do
	    {
	      es_xdoblock anticamp/get_user
	    }
	    else do
	    {
              es_xmathparse x "x-tpx"
	      es_xmathparse y "y-tpy"
	      es_xmathparse z "z-tpz"
	      
	      es_xstring x replace -
	      es_xstring y replace -
	      es_xstring z replace -
	     	      
	      // check if camping
	      ifx parse("(x < campradius) and (y < campradius) and (z < campradius)") do
	      {
                es_xdoblock anticamp/get_user
	      }
	    }
	    ifx false(ac_return) do
            {
	      es_keysetvalue anticamp server_var(ac_id) timer server_var(camptime)
	      es_keysetvalue anticamp server_var(ac_id) vectorstr server_var(fA)
	    }
	    es_xset ac_return 0
          }
        }
}

block get_user
{
	// get total camps
	es_keygetvalue ac_counter anticamp server_var(ac_id) timer
	es_xmathparse ac_counter "ac_counter+1"
	es_keysetvalue anticamp server_var(ac_id) timer server_var(ac_counter)
	
	// punish if camptime has passed
	ifx parse("ac_counter >= 0") do
	{
	  es_xmathparse _temp "ac_counter/2" 
	  if ("." notin server_var(_temp)) do
	  {
	    es_xset ac_timer 1
	  }
	  else do
	  {
	    es_xset ac_timer 0
	  }
	  do_punish
	}
	es_xset ac_return 1
}

block credits
{
	ifx true(ac_timer) do
	{
	  ifx parse("ac_counter <= credits") do
	  {	  
	    es_set _temp server_var(credits)
	    es_xmathparse _temp "_temp - ac_counter"
	    es_xformatv _str "credits: %1/%2" credits _temp
	    es_tell server_var(ac_id) #green Camping will be punished server_var(_str)
	  }
	}
}

block message
{
	if (server_var(ac_counter) in server_var(message)) do
	{
	  // get user detail
	  es_keygetvalue ac_weapon userinfo server_var(ac_id) weapon
	  es_xstring ac_weapon replace weapon_
	  es_keygetvalue ac_name userinfo server_var(ac_id) name
	  es_keygetvalue _index userinfo server_var(ac_id) index

	  // write msg
     	  es_xusermsg create campmsg SayText2 
     	  es_usermsg write byte campmsg server_var(_index) 
     	  es_xusermsg write byte campmsg true 
      	  es_usermsg write string campmsg server_var(campmsg)
     	  es_usermsg write string campmsg server_var(ac_name) 
          es_usermsg write string campmsg server_var(ac_weapon) 
          es_xcreateplayerlist _playerlist
          es_xforeachkey _tmp in _playerlist "es_usermsg send campmsg server_var(_tmp)"
     	  es_xkeygroupdelete _playerlist
     	  es_xusermsg delete campmsg
   
	  ifx true(detect_sound) do
	  {
	    es_emitsound player server_var(ac_id) server_var(detect_sound) 1.0 0.6
	  }
	}   
}

block cash
{
	ifx true(ac_timer) do
	{
	  ifx parse("ac_counter >= cash") do
	  {
 	    es_getplayerprop _temp server_var(ac_id) "CCSPlayer.m_iAccount"
 	    ifx true(_temp) do
 	    {
 	      es_xmathparse _temp "_temp-takecash"
 	      ifx parse("_temp > 0") do
 	      {
 	        es_setplayerprop server_var(ac_id) "CCSPlayer.m_iAccount" server_var(_temp)
 	      }
 	    }
 	  }
 	}
}

block strip
{
	ifx parse("ac_counter == strip") do
	{
	  es_give server_var(ac_id) player_weaponstrip
	  es_fire server_var(ac_id) player_weaponstrip strip
	  es_give server_var(ac_id) weapon_knife
	}
}

block blind
{
	ifx parse("ac_counter >= blind") do
	{
	  es_give server_var(ac_id) env_fade
	  es_fire server_var(ac_id) env_fade addoutput "spawnflags 4"
	  es_fire server_var(ac_id) env_fade addoutput "duration 1"
	  es_fire server_var(ac_id) env_fade addoutput "holdtime 1.2"
	  es_fire server_var(ac_id) env_fade addoutput "rendercolor 200 200 200"
	  es_fire server_var(ac_id) env_fade addoutput "OnUser1 !self,Fade,-1,0"
	  es_fire server_var(ac_id) env_fade FireUser1	
	}
}

block slap
{
	ifx true(ac_timer) do
	{
	  ifx parse("ac_counter >= slap") do
	  {
	    // emit hurt sounds
  	    es_xrand _temp 1 3
            es_xformatv ac_sound "player/damage%1.wav" _temp
            es_emitsound player server_var(ac_id) server_var(ac_sound) 1.0 0.7	
         
            // slap player
            es_xmathparse ac_health "ac_health-health_slap"
            ifx parse("ac_health > min_health") do
            {
              es_setplayerprop server_var(ac_id) "CCSPlayer.baseclass.m_iHealth" server_var(ac_health)
            }
            else do
            {
              es_setplayerprop server_var(ac_id) "CCSPlayer.baseclass.m_iHealth" server_var(min_health)
            }
	    ifx true(slap_mode) do
	    {
	      es_xrand _rand1 -200 200
	      es_xrand _rand2 -200 200
	      es_xformatv _temp "%1,%2,170" _rand1 _rand2
	      es_setplayerprop server_var(ac_id) "CCSPlayer.baseclass.localdata.m_vecBaseVelocity" server_var(_temp)          
	    }
            ifx parse("ac_health <= 0") do
            {
              es_sexec server_var(ac_id) kill
              es bot_kill server_var(ac_name)
            }
          }
        }
}

block beacon
{
	ifx true(ac_timer) do
	{
          ifx parse("ac_counter >= beacon") do
          {
    	    ifx true(beacon_sound) do
    	    {
    	      es_emitsound player server_var(ac_id) server_var(beacon_sound) 1.0 0.7
    	    }
    	    es_splitvectorstring x y z server_var(fA)
    	    es_xmathparse z "z+10"
	    es est_effect 10 #a 0 "sprites/lgtning.vmt" server_var(x) server_var(y) server_var(z) 1 400 1 10 50 0 220 10 111 200 0
    	  }
    	}
}

event player_spawn
{
	// check if alive
	if (event_var(es_userteam) != 0) do
	{
	  ifx false(exclude) do
	  {
	    es_xdoblock anticamp/add_player
	  }
	  else do
	  {
	    es_isbot ac_isbot event_var(userid) 
	    ifx parse("exclude == 2") do
	    {
	      ifx false(ac_isbot) do
	      {
	        es_xdoblock anticamp/add_player    
	      }
	    }	  
	    ifx parse("exclude == 1") do
	    {
	      ifx true(ac_isbot) do
	      {
	        es_xdoblock anticamp/add_player    
	      }
	    }	   	  
	  }
	}	
}

block add_player
{
	es_keycreate anticamp event_var(userid)
	es_keysetvalue anticamp event_var(userid) timer "-222"			
	es_getplayerprop fA event_var(userid) "CBaseEntity.m_vecOrigin"
	es_keysetvalue anticamp event_var(userid) vectorstr server_var(fA)
}

event bomb_abortdefuse
{
	es_keycreate anticamp event_var(userid)
      	es_keysetvalue anticamp event_var(userid) timer server_var(camptime)
	es_getplayerprop fA event_var(userid) "CBaseEntity.m_vecOrigin"
	es_keysetvalue anticamp event_var(userid) vectorstr server_var(fA)
}

event round_start
{
	es_xcopy _start_timer start_timer
}

event round_end
{
	es_xset ac_team 0
	if (server_var(ac_mapcat) in server_var(allow_t_camp)) do
	{
	  es_xset ac_team 2
	}
	if (server_var(ac_mapcat) in server_var(allow_ct_camp)) do
	{
	  es_xset ac_team 3
	}	
}

event player_death
{
	// delete user from loop
	es_exists _temp key anticamp event_var(userid)
	ifx true(_temp) do
	{
	  es_keydelete anticamp event_var(userid)
	}
}
	
event player_disconnect
{
	// delete user from loop
	es_exists _temp key anticamp event_var(userid)
	ifx true(_temp) do
	{
	  es_keydelete anticamp event_var(userid)
	}
}

event bomb_begindefuse
{
	// delete user from loop
	es_exists _temp key anticamp event_var(userid)
	ifx true(_temp) do
	{
	  es_keydelete anticamp event_var(userid)
	}
}


event bomb_droped
{
	// allow ct's to camp
	if (server_var(ac_mapcat) notin server_var(allow_ct_camp)) do
	{
	  es_xset ac_team 3
	}
}

event bomb_pickup
{
	es_xset ac_team 0
	if (server_var(ac_mapcat) in server_var(allow_t_camp)) do
	{
	  es_xset ac_team 2
	}
	if (server_var(ac_mapcat) in server_var(allow_ct_camp)) do
	{
	  es_xset ac_team 3
	}
}
	
event bomb_planted
{
	// allow terrors to camp
	if (server_var(ac_mapcat) notin server_var(allow_t_camp)) do
	{
	  es_xset ac_team 2
	}
}	



		

