----------------------------------------------------------------------------------
-- This script defines the default game rules.
-- These rules are the same as the default game rules for Half-Life 2 deathmatch.
-- fraglimit and timelimit are read from the console variables.
----------------------------------------------------------------------------------

fIntermissionEnd = 0
bEndGame = false

_OpenScript( "Events.lua" ) -- Use the default (empty) events


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


	function gamerulesThink ()
	
	
		-- gameover is true when the game has ended and everyone
		-- is looking at the scoreboard blaming lag for their score
		
		if ( bEndGame ) then
			
			if ( fIntermissionEnd < _fCurrentTime ) then
				
				_StartNextLevel()
				
			end
						
			return
			
		end;
	
		
		
		
		local TimeLimit = _GetConVar_Float( "mp_timelimit" ) * 60 -- Minutes to seconds!
		local FragLimit = _GetConVar_Float( "mp_fraglimit" )
		
		if (FragLimit > 0) then -- We have a fraglimit!
		
			if ( _IsTeamplay() ) then
				
				local NumTeams = _TeamCount();
				
				for i=0, NumTeams do
					
					if ( _TeamScore(i) >= FragLimit ) then
					
						StartIntermission()
					
					end
					
				end
		
			else -- not teamplay
		
				for i=0, maxplayers do
				
					if ( _PlayerInfo( i, "connected" ) and  _PlayerInfo( i, "kills" ) >= FragLimit ) then
						
						StartIntermission()
						
					end
				
				end
		
			end
			
		end
	 
	 

		if ( TimeLimit > 0 ) then
			
			if ( TimeLimit < _fCurrentTime ) then
			
				StartIntermission()
				
			end;
			
		end;
		
		
	
	end --gamerulesThink
	
	
	
	
	
-- Give the players the default weapons --

	function GiveDefaultItems( playerid )
		
		
		if ( _GetRule( "SpawnWithAllWeapons" ) ) then
			
			_PlayerGiveItem( playerid, "weapon_stunstick" )
			_PlayerGiveItem( playerid, "weapon_357" )
			_PlayerGiveItem( playerid, "weapon_smg1" )
			_PlayerGiveItem( playerid, "weapon_ar2" )
			_PlayerGiveItem( playerid, "weapon_shotgun" )
			_PlayerGiveItem( playerid, "weapon_frag" )
			_PlayerGiveItem( playerid, "weapon_crossbow" )
			_PlayerGiveItem( playerid, "weapon_rpg" )
			_PlayerGiveItem( playerid, "weapon_slam" )
			_PlayerGiveItem( playerid, "weapon_bugbait" )
			
			_PlayerGiveAmmo( playerid, 255, "Pistol", false )
			_PlayerGiveAmmo( playerid, 255, "Buckshot", false )	
			_PlayerGiveAmmo( playerid, 255, "SMG1", false )
			_PlayerGiveAmmo( playerid, 255, "AR2", false )	
			_PlayerGiveAmmo( playerid, 255, "AR2AltFire", false )
			_PlayerGiveAmmo( playerid, 255, "smg1_grenade", false )	
			_PlayerGiveAmmo( playerid, 255, "357", false )
			_PlayerGiveAmmo( playerid, 5, "rpg_round", false )				
			_PlayerGiveAmmo( playerid, 2, "grenade", false )
			_PlayerGiveAmmo( playerid, 2, "slam", false )	
			_PlayerGiveAmmo( playerid, 10, "XBowBolt", false )	
			
				
		end
	
		_PlayerGiveItem( playerid, "weapon_crowbar" )
		_PlayerGiveItem( playerid, "weapon_pistol" )
		_PlayerGiveItem( playerid, "weapon_physcannon" )
		
		_PlayerGiveAmmo( playerid, 100, "SMG1", false )
		_PlayerGiveAmmo( playerid, 25, "Buckshot", false )	
		
		if ( _GetRule( "AllowPhysgun" ) ) then
			
			_PlayerGiveItem( playerid, "weapon_physgun" )
		end
	
		if ( _GetRule( "AllowMultigun" ) ) then
			
			_PlayerGiveItem( playerid, "weapon_tool" )
		end
		
	end
	

--  The current map has ended, show the scoreboard ----------------------------

	
	function StartIntermission ()
	

		bEndGame = true;
		
		fIntermissionEnd = _fCurrentTime + _GetConVar_Float( "mp_chattime" )
		
		-- Loop through all players
		for i=0, maxplayers do
		
			_PlayerShowScoreboard( i )
			_PlayerFreeze( i, true )
		
		end
	
	end
	
	
	
	
	
--  Called right before the new map starts ------------------------------------

	          
	function gamerulesStartMap ()
	
		-- Anything to imitialize?	
	
		bEndGame = false
		fIntermissionEnd = 0
		PlayerFreezeAll( false )
		
		
		-- Set the default team names
		_TeamSetName( TEAM_BLUE, "Blue Team" )
		_TeamSetName( TEAM_GREEN, "Green Team" )
		_TeamSetName( TEAM_YELLOW, "Yellow Team" )
		_TeamSetName( TEAM_RED, "Red Team" )
			
	end



-- This is very important, it will crash if a player doesn't have a model! --
	function PlayerSpawnChooseModel ( playerid )	
	
		if ( _PlayerInfo( playerid, "model" ) == DEFAULT_PLAYER_MODEL ) then
					
			-- The player doesn't have a preferred model, set it randomly
			if ( _PlayerPreferredModel( playerid ) == "" ) then
	
				_PlayerSetModel( playerid, _PlayerGetRandomAllowedModel() )
						
			-- The player has a preferred model, use that!		
			else
			
				_PlayerSetModel( playerid, _PlayerPreferredModel( playerid ) )
			
			end
			
		end
		
	end


-- Events --



	function eventPlayerKilled ( killed, attacker, weapon )
		
		
		_PlayerAddDeath( killed, 1 )
		
		
		-- Player killed himself, what a top hat
		if ( killed == attacker ) then
			
			_PlayerAddScore( killed, -1 )
			
		-- Was killed by another player!
		elseif ( attacker > 0 ) then
		
			_PlayerAddScore( attacker, 1 )
			_PlayerAllowDecalPaint( attacker ); -- Let the player spraypaint the body

		-- The soft sod was killed by the world
		else
		
			_PlayerAddScore( killed, -1 )
			
		end	
		
		
	end


	function eventPlayerSpawn ( userid )
		
		-- If we're in teamplay mode switch team circles on for everyone
		_PlayerSetDrawTeamCircle( userid, _GetRule( "Teamplay" ) )

	end

	
	
