-------------------------------
-- SSTRP
-- Author: MNWRP
-- 
-- player_util.lua
-- Useful functions for players.
-------------------------------
DecayingRagdolls = {};

function DeathMode( ply )

	DayLog( "script.txt", "Starting death mode for " .. ply:SteamID( ) );
	local mdl = ply:GetModel( )
	
	local rag = ents.Create( "prop_ragdoll" )
	rag:SetModel( mdl )
	rag:SetPos( ply:GetPos( ) )
	rag:SetAngles( ply:GetAngles( ) )
	if(ply:Team() == 4) then
	rag:SetSkin(1)
	end
	rag.isdeathdoll = true;
	rag.ply = ply;
	rag:Spawn( )
	
	ply:SpectateEntity( rag )
	ply:Spectate( OBS_MODE_CHASE )
	
	ply.deathrag = rag;
	
	local n = #DecayingRagdolls + 1
	DecayingRagdolls[ n ] = ply.deathrag;
	
	ply:SetNWInt( "deathmode", 1 )
	
	ply.deathtime = 0;
	ply.nextsecond = CurTime( ) + 1;
	
	-- This is extremely glitchy with the physics engine, and is to be reworked later.
	
	local function Decay3( )
	
		local rag = DecayingRagdolls[ n ];
		
		if( rag and rag:IsValid() ) then
		
			rag:Remove( );
			DecayingRagdolls[ n ] = nil;
			rag = nil;
			
		end
		
	end
	
	timer.Simple( 120, Decay3 );
	
end

function SendChat( ply, msg )

	ply:PrintMessage( 3, msg );
	
end

function SendConsole( ply, msg )

	ply:PrintMessage( 2, msg );
	
end

local meta = FindMetaTable( "Player" );

function meta:MaxHealth( )

	return self:GetNWFloat("MaxHealth");
	
end

function meta:ChangeMaxHealth( amt )

	self:SetNWFloat("MaxHealth", self:MaxHealth() + amt);
	
end

function meta:MaxArmor( )

	return self:GetNWFloat("MaxArmor");
	
end

function meta:ChangeMaxArmor( amt )

	self:SetNWFloat("MaxArmor", self:MaxArmor() + amt);
	
end

function meta:MaxWalkSpeed( )

	return self:GetNWFloat("MaxWalkSpeed");
	
end

function meta:ChangeMaxWalkSpeed( amt )

	self:SetNWFloat("MaxWalkSpeed", self:MaxWalkSpeed() + amt);
	
end

function meta:MaxRunSpeed( )

	return self:GetNWFloat("MaxRunSpeed");
	
end

function meta:ChangeMaxRunSpeed( amt )

	self:SetNWFloat("MaxRunSpeed", self:MaxRunSpeed() + amt);
	
end

function meta:GiveItem( class )

	DayLog( "economy.txt", "Adding item '" .. class .. "' to " .. FormatCharString( self ) .. " inventory" );
	local inv = GetCharField( self, "inventory" );
	table.insert( inv, class );
	SetCharField( self, "inventory", inv);
	self:RefreshInventory( );

end

function meta:TakeItem( class )
	local inv = GetCharField(self, "inventory" );
	
	for k, v in pairs( inv ) do
		if( v == class ) then
			inv[ k ] = nil;
			PrintTable( inv );
			SetCharField( self, "inventory", inv);
			self:RefreshInventory( );
			DayLog( "economy.txt", "Removing item '" .. class .. "' from " .. FormatCharString( self ) .. " inventory" );
			return;
		end
	end
	
end

function meta:ClearInventory( )
	umsg.Start( "clearinventory", self )
	umsg.End( );
end

function meta:RefreshInventory( )
	self:ClearInventory( )
	
	for k, v in pairs( GetCharField( self, "inventory" ) ) do
		umsg.Start( "addinventory", self );
			umsg.String( ItemData[ v ].Name );
			umsg.String( ItemData[ v ].Class );
			umsg.String( ItemData[ v ].Description );
			umsg.String( ItemData[ v ].Model );
		umsg.End( );
	end
end

function meta:ClearBusiness( )
	umsg.Start( "clearbusiness", self )
	umsg.End( );
end

function meta:RefreshBusiness( )
	self:ClearBusiness( )
		
	if(Teams[self:Team()] == nil) then return; end -- Team not assigned
	
	for k, v in pairs( ItemData ) do
	
		if( v.Purchaseable and table.HasValue( Teams[self:Team()]["item_groups"], v.ItemGroup ) ) then
		
			umsg.Start( "addbusiness", self );
				umsg.String( v.Name );
				umsg.String( v.Class );
				umsg.String( v.Description );
				umsg.String( v.Model );
				umsg.Long( v.Price );
			umsg.End( );
			
		end
	end
end

function ChangeMoney( ply, amount ) -- Modify someone's money amount.

	-- NEVAR EVAR LET IT GO NEGATIVE.
	if( ( GetCharField( ply, "money" ) - amount ) < 0 ) then return; end
	
	DayLog( "economy.txt", "Changing " .. ply:SteamID( ) .. "-" .. ply:GetNWString( "uid" ) .. " money by " .. tostring( amount ) );
	
	SetCharField( ply, "money", GetCharField( ply, "money" ) + amount );
	ply:SetNWString("money", GetCharField( ply, "money" ));

end

function DrugPlayer( pl, mul )

	mul = mul / 10 * 2;

	pl:ConCommand("pp_motionblur 1")
	pl:ConCommand("pp_motionblur_addalpha " .. 0.05 * mul)
	pl:ConCommand("pp_motionblur_delay " .. 0.035 * mul)
	pl:ConCommand("pp_motionblur_drawalpha 1.00")
	pl:ConCommand("pp_dof 1")
	pl:ConCommand("pp_dof_initlength 9")
	pl:ConCommand("pp_dof_spacing 8")

	local IDSteam = string.gsub(pl:SteamID(), ":", "")

	timer.Create(IDSteam, 40 * mul, 1, UnDrugPlayer, pl)
end

function UnDrugPlayer(pl)
	pl:ConCommand("pp_motionblur 0")
	pl:ConCommand("pp_dof 0")
end