----------------------------------------------------------------------------------
-- This is an example gameplay override script
----------------------------------------------------------------------------------


_OpenScript( "Events.lua" )


_Msg("--------------------------------------------------------\n")
_Msg("--------------------------------------------------------\n")
_Msg("--------------------------------------------------------\n")
_Msg("-[ The example gameplay script has been launched! ]-----\n")
_Msg("--------------------------------------------------------\n")
_Msg("--------------------------------------------------------\n")
_Msg("--------------------------------------------------------\n")
_Msg("---------------+- mAy gOd hAvE mErcy On OUr sOUls -+----\n")
_Msg("--------------------------------------------------------\n")
_Msg("--------------------------------------------------------\n")


BestRunnerName = " "
BestRunnerKills = 0
CurrentRunnerKills = 0
iRunningMan = 0
RunnersOldHealth = 0
bWaitingRunnerRespawn = false;

--  Called every frame from: CHL2MPRules::Think( void ) -----------------------


PosUpdate = 0;

	function gamerulesThink ()


		if ( false and iRunningMan > 0 and PosUpdate < _fCurrentTime ) then

			local InfoString = ""
			
			local vPos = _EntGetPos( iRunningMan );
			
			InfoString = InfoString .. "pos: " .. vecString( vPos )
			InfoString = InfoString .. "\nvel: " .. vecString( _EntGetVelocity( iRunningMan) )
			

			_ScreenText( 0, InfoString,  0.1,0.1,   100,200,255,255,  0,0, 0.50, 0, 4 )


			PosUpdate = _fCurrentTime + 0.05

		end


		if (iRunningMan == 0 and timeChooseRunner < _fCurrentTime ) then
			
			local iMostFrags = -100
			local iNewRunner = 0
			
			for i=0, maxplayers do

				if ( _PlayerInfo( i, "connected" ) ) then
					
					if ( _PlayerInfo( i, "kills" ) >= iMostFrags ) then
						
						iMostFrags = _PlayerInfo( i, "kills" )
						iNewRunner = i

					end
					
				end					
				
			end
			
			
			if ( iNewRunner > 0 ) then

				NewRunner( iNewRunner )
				
			end
			
			
			timeChooseRunner = _fCurrentTime + 5
			
		end
		
		
		
		if ( iRunningMan > 0 and _PlayerInfo( iRunningMan, "connected" ) ) then
			
			_PlayerSetMaxSpeed( iRunningMan, 500 )
			
		end


	end 
	
	
	
	function GiveDefaultItems( playerid )
		
		if (playerid == iRunningMan) then
			
			_PlayerGiveItem( playerid, "weapon_pistol" )
			_PlayerGiveItem( playerid, "weapon_smg1" )
			_PlayerGiveItem( playerid, "weapon_crowbar" )
			
			_PlayerGiveAmmo( playerid, 255, "SMG1", false )
			_PlayerGiveAmmo( playerid, 3, "smg1_grenade", false )
			
		else
		
			_PlayerGiveItem( playerid, "weapon_pistol" )
			_PlayerGiveItem( playerid, "weapon_shotgun" )
			_PlayerGiveItem( playerid, "weapon_smg1" )
			_PlayerGiveItem( playerid, "weapon_crowbar" )
			_PlayerGiveItem( playerid, "weapon_physcannon" )		
			
			_PlayerGiveAmmo( playerid, 255, "SMG1", false )
			_PlayerGiveAmmo( playerid, 128, "Buckshot", false )	
		
		end
		
	end
		
	
--  Called right before the new map starts ------------------------------------

	          
	function gamerulesStartMap ()
	
		PlayerFreezeAll( false )
		
		BestRunnerName = ""
		timeChooseRunner = 0
		iRunningMan = 0
		
		
		_TeamSetName( TEAM_BLUE, "Running Man" )
		_TeamSetName( TEAM_GREEN, "Team Mossman" )
			
	end
	


-- Player chooses a model (and team in this case)

	function PlayerSpawnChooseModel ( playerid )	
	
	
		if ( iRunningMan == playerid ) then
			_PlayerSetModel( playerid, "models/player/breen.mdl" )
			_PlayerChangeTeam( playerid, TEAM_BLUE )		
			
			if (bWaitingRunnerRespawn) then
				
				_PlayerSetHealth( playerid, 999 )
				RunnersOldHealth = 999		
				bWaitingRunnerRespawn = false
			end
		else
			_PlayerSetModel( playerid, "models/player/mossman.mdl" )
			_PlayerChangeTeam( playerid, TEAM_GREEN )
		end
			
		
	end

	
-- Event overrides

	function eventPlayerConnect ( name, userid, address, steamid )

		local iRand = math.random( 10, 30 )
		_PlaySound( "vo/npc/female01/question"..iRand..".wav" )
		
	
		_PrintMessageAll( HUD_PRINTCENTER, "Holy shit! ".. name .. " has joined the game!"  );

	end


	function eventPlayerDisconnect ( name, userid, address, steamid, reason )

		local iRand = math.random( 10, 17 )
		_PlaySound( "vo/npc/male01/gordead_ques"..iRand..".wav" )

	end
	
	
	
	
	function eventPlayerKilled ( killed, attacker, weapon )
		
		_PlayerAddDeath( killed, 1 )
				
		if ( attacker == iRunningMan and attacker ~= killed and attacker > 0) then
			
			CurrentRunnerKills = CurrentRunnerKills + 1
			
			if ( CurrentRunnerKills > BestRunnerKills ) then
				
				BestRunnerName = " " .._PlayerInfo( attacker, "name" )
				BestRunnerKills = CurrentRunnerKills
				
			end
				
			_PlayerAddScore( attacker, 1 )
					
		end
		
		-- The runner is dead - long live the runner!
		if ( killed == iRunningMan) then
					
			-- If they killed themselves then they lose
			iRunningMan = 0
			
			-- The running man killed himself! PUNISH HIM
			if ( attacker == killed ) then
				
				_PlayerAddScore( killed, -5 )
				
			elseif ( attacker > 0) then
				
				_PlayerAddDeath( killed, -1 )
				_PlayerAddScore( attacker, 1 )
				NewRunner( attacker )
				
			end
						
		end
		
	end
	
	
	function eventPlayerHurt ( userid, newhealth, attacker )
		
		
		if ( userid == iRunningMan ) then
			
			
			_ScreenText( 0, "Runner Health: " .. newhealth,  0.02,0.8,   100,200,255,255,  0.1,5, 10, 2, 3 )
			
			if ( newhealth < 800 and RunnersOldHealth >= 800 ) then
				
				_PrintMessageAll( HUD_PRINTCENTER, "The runner has " .. newhealth .. " Health left!" );
				
			end
			
			if ( newhealth < 500 and RunnersOldHealth >= 500 ) then
				
				_PrintMessageAll( HUD_PRINTCENTER, newhealth .. " Health left!" );
				
			end
			
			if ( newhealth < 200 and RunnersOldHealth >= 200 ) then
				
				_PrintMessageAll( HUD_PRINTCENTER, "The runner has " .. newhealth .. " Health left - Come on!" );
				
			end
			
			if ( newhealth < 100 and RunnersOldHealth >= 100 ) then
				
				_PrintMessageAll( HUD_PRINTCENTER, "The runner has " .. newhealth .. " Health left - Nearly dead!" );
				
			end
			
			RunnersOldHealth = newhealth
			
		end
		
	end
	
	
-- Custom functions for this mode


	function NewRunner( playerid )
	
		CurrentRunnerKills = 0
		iRunningMan = playerid
		
		local BestRunnerString = ""
				
		if ( BestRunnerKills > 0 ) then
			
			BestRunnerString = "Best Runner: "..BestRunnerName.." with ".. BestRunnerKills .." kills!"
			
			_ScreenText( 0, BestRunnerString,  -1,0.3,   100,200,255,255,  0.05,5, 10, 2, 2 )
			
		end
		
		
		_ScreenText( 0, _PlayerInfo( playerid, "name" ) .. " is the new runner!",  -1,0.2,   255,100,0,255,  1,3, 6, 1, 1 )	
		
		_PlayerSetModel( playerid, "models/player/breen.mdl" )
		_PlayerChangeTeam( playerid, TEAM_BLUE )
		
		_PlayerSilentKill( playerid, 5, true )
		bWaitingRunnerRespawn = true
				
	end


	function canPlayerHaveItem( playerid, itemname )
			
		-- weapons the runner is allowed to pickup.. aka spawn with
		if ( playerid == iRunningMan and _PlayerInfo( playerid, "alive") ) then
			
			if ( itemname == "weapon_pistol" ) then return true end
			if ( itemname == "weapon_smg1" ) then return true end
			if ( itemname == "weapon_crowbar" ) then return true end
			
			return false
		end
		
		return true
		
	end
	
	
	function eventPlayerSpawn ( userid )
		
		-- Team circles for everyone!
		_PlayerSetDrawTeamCircle( userid, true )

	end
