-------------------------------
-- SSTRP
-- Author: MNWRP
--
-- charactercreate.lua
-- Contains the character creation concommands.
-------------------------------	
	
-- Set Model
function ccSetModel( ply, cmd, args )

	local mdl = args[ 1 ];
	
	if( ply:GetNWInt( "charactercreate" ) == 1 ) then
	
		if( table.HasValue( ValidModels, string.lower( mdl ) ) ) then
			
			CallHook( "CharacterCreation_SetModel", ply, mdl );
			SetCharField(ply, "model", mdl );

		else
		
			CallHook( "CharacterCreation_SetModel", ply, "models/humans/group01/male_01.mdl" );
			SetCharField(ply, "model", "models/humans/group01/male_01.mdl" );
			
		end
		
		
	end
	
	return;
end
concommand.Add( "rp_setmodel", ccSetModel );

-- Start Creation
function ccStartCreate( ply, cmd, args )
	
	local PlyCharTable = PlayerData[ FormatSteamID( ply:SteamID() ) ][ "characters" ]
	
	-- Find the highest Unique ID
	local high = 0;
	
	for k, v in pairs( PlyCharTable ) do
	
		k = tonumber( k );
		high = tonumber( high );
		
		if( k > high ) then 
		
			high = k;
			
		end
		
	end
	
	high = high + 1;
	ply:SetNWString( "uid", tostring(high) );
	
	ply:SetNWInt( "charactercreate", 1 );
	PlayerData[ FormatSteamID( ply:SteamID() ) ][ "characters" ][ tostring(high) ] = {  }
	
	CallHook( "CharacterCreation_Start", ply );
	
end
concommand.Add( "rp_startcreate", ccStartCreate );

-- Finish Creation
function ccFinishCreate( ply, cmd, args )

	if( ply:GetNWInt( "charactercreate" ) == 1 ) then
		
		ply:SetNWInt( "charactercreate", 0 )
		
		local SteamID = FormatSteamID( ply:SteamID() );
		
		for fieldname, default in pairs( CharacterDataFields ) do
		
			if( PlayerData[ SteamID ][ "characters" ][ ply:GetNWString( "uid" ) ][ fieldname ] == nil) then

				PlayerData[ SteamID ][ "characters" ][ ply:GetNWString( "uid" ) ][ fieldname ] = ReferenceFix(default);
		
			end
			
		end
		
		ResendCharData( ply );

		ply:RefreshInventory( )
		ply:RefreshBusiness( )
		
		ply:SetTeam( 1 );
		
		ply:Spawn( );
		
		ply:ConCommand( "fadein" );
		
		CallHook( "CharacterCreation_Finished", ply, ply:GetNWString( "uid" ) );
		
	end
	
end
concommand.Add( "rp_finishcreate", ccFinishCreate );

function ccSelectChar( ply, cmd, args )

	local uid = args[ 1 ];
	local SteamID = FormatSteamID(ply:SteamID());
	
	if( PlayerData[ SteamID ][ "characters" ][ uid ] != nil ) then
	
		ply:SetNWString( "uid", uid );
		ResendCharData( ply );
		
		ply:SetNWInt( "charactercreate", 0 );
	
		ply:SetTeam( 1 );
		CallHook( "CharacterSelect_PostSetTeam", ply, PlayerData[ SteamID ][ "characters" ][ uid ] );
		
		ply:RefreshInventory( )
		ply:RefreshBusiness( )
		
		ply:ConCommand( "fadein" );
		
		ply:Spawn( );
		
		CallHook( "CharacterSelected", ply, PlayerData[ SteamID ][ "characters" ][ uid ] );
		
	else
		
		return;
		
	end

end
concommand.Add( "rp_selectchar", ccSelectChar );

function ccReady( ply, cmd, args )

	if( ply.Ready == false ) then

		ply.Ready = true;
	
		-- Find the highest Unique ID and set it - just in case they want to create a character.
		local high = 0;
		
		local PlyCharTable = PlayerData[ FormatSteamID( ply:SteamID() ) ]["characters"];
		
		for k, v in pairs( PlyCharTable ) do
		
			k = tonumber( k );
			high = tonumber( high );
			
			if( k > high ) then 
			
				high = k;
				
			end
			
		end
		
		high = high + 1;
		ply:SetNWString( "uid", tostring(high) );
		
		for k, v in pairs( PlyCharTable ) do -- Send them all their characters for selection
	
			umsg.Start( "ReceiveChar", ply );
				umsg.Long( tonumber(k) );
				umsg.String( v[ "name" ] );
				umsg.String( v[ "model" ] );
			umsg.End( );
			
		end
		
		ply:SetNWInt( "charactercreate", 1 )
		
		umsg.Start( "charactercreate", ply );
		umsg.End( );
		
		CallHook( "PlayerReady", ply );
		
	end
	
end
concommand.Add( "rp_ready", ccReady );