
// DO NOT EDIT THIS FILE!

// Globals that we need 
local table 		= table
local Msg			= Msg
local file			= file
local tostring		= tostring
local pairs			= pairs
local util			= util
local CLIENT		= CLIENT
local hook			= hook
local LocalPlayer 	= LocalPlayer
local PrintTable	= PrintTable
local ents			= ents

/*---------------------------------------------------------
   Name: vehicles
   Desc: Handles the management of vehicles on both client and server
---------------------------------------------------------*/
module("vehicles")

local Vehicles = {}

/*---------------------------------------------------------
// Returns a table of vehicles (to populate the spawn menu)
---------------------------------------------------------*/
function GetTable()

	return Vehicles

end

/*---------------------------------------------------------
// Client specific
---------------------------------------------------------*/
if ( CLIENT ) then

	/*---------------------------------------------------------
	// Called by server to add a vehicle to the spawn list
	---------------------------------------------------------*/
	function Add( vname, name, material )

		local v = {}
		v.vname 	= vname
		v.name 		= name
		v.material 	= material

		table.insert( Vehicles, v )

	end
	
	/*---------------------------------------------------------
	// Refreshes the spawn list
	---------------------------------------------------------*/
	function RefreshList()
	
		Msg("Refreshing Spawn Menu..")
		LocalPlayer():ConCommand( "spawnmenu_reloadvehicles" )
	
	end

end


/*---------------------------------------------------------
// Server specific
---------------------------------------------------------*/
if ( !CLIENT ) then


	/*---------------------------------------------------------
	// Hook the initial spawn callback to send the player the list
	---------------------------------------------------------*/
	local function SendVehicleList( ply )

		for k, v in pairs(Vehicles) do
			ply:SendLua( "vehicles.Add( \""..tostring(k).."\", \""..tostring(v.name).."\", \""..tostring(v.material).."\" )" )
		end

		ply:SendLua( "vehicles.RefreshList()" )
	
	end
	hook.Add( "PlayerInitialSpawn", "SendVehicleList", SendVehicleList )
	
	
	/*---------------------------------------------------------
	// Player spawns a vehicle
	---------------------------------------------------------*/
	function PlayerSpawn( ply, name, endpos, normal )
	
		local tab = Vehicles[name]
		if ( !tab ) then return end
		
		local ent = ents.Create( tab.ent )
		if ( ent && ent:IsValid() ) then
		
			ent:SetPos( endpos + normal * 80 ) // This could be less shitty
		
			for k, v in pairs( tab.keyvalues ) do
				ent:SetKeyValue( k, v )
			end
			
			ent:Spawn()
			ent:Activate()
			
			ent:DropToFloor()
		
		end
		
		
		
		return ent	
	
	end
	
	
	function AddVehicle( kv )
	
		if ( !kv.vname || kv.vname == "" ) then return end
		Vehicles[ kv.vname ] = kv
	
	end
	
	
	function ReloadFiles()
	
		// Find all vehicle scripts
		local files = file.Find( "../settings/vehicles/*.txt" )
		
		// Load each one
		for k, v in pairs( files ) do
			
			// Read the plain text
			local kvfile = file.Read( "../settings/vehicles/" .. v )
			
			// Convert it to a table
			local kv = util.KeyValuesToTable( kvfile )
			
			// Throw each table in as a vehicle
			for _, t in pairs( kv ) do
			
				AddVehicle( t )
			
			end
		
		end
	
	end
	
	// Load the files now
	ReloadFiles()

end
