
// DO NOT EDIT THIS FILE!

// Globals that we need 
//local gmod = gmod
local pairs = pairs
local unpack = unpack
local type = type
local Msg = Msg
local table = table

/*---------------------------------------------------------
   Name: scripted_ents
   Desc: Scripted Entity factory
---------------------------------------------------------*/
module("scripted_ents")

local SEntList = {}


/*---------------------------------------------------------
   Name: TableInherit( t, base )
   Desc: Copies any missing data from base to t
---------------------------------------------------------*/
local function TableInherit( t, base )

	for k, v in pairs( base ) do 
		
		if ( t[k] == nil ) then	
			t[k] = v 
		elseif ( type(t[k]) == "table" ) then
			TableInherit( t[k], v )
		end
		
	end
	
	t["BaseClass"] = base
	
	return t

end


/*---------------------------------------------------------
   Name: Register( table, string, bool )
---------------------------------------------------------*/
function Register( t, name, reload )

	// Don't load it twice unless we're reloading
	if (!reload && SEntList[ name ] != nil ) then
		return
	end
	
	local type = t.Type
	
	if (type == nil) then
		Msg("Couldn't register Scripted Entity "..name.." - the Type field is empty!\n")
		return
	end
	
	local isBaseType = true
	if ( name != "base_"..type ) then
		t = TableInherit( t, Get( "base_"..type ) )
		isBaseType = false
	end

	SEntList[ name ] = {}
	SEntList[ name ].type = type
	SEntList[ name ].t = t
	SEntList[ name ].isBaseType = isBaseType
	
	t.Classname = name
end


/*---------------------------------------------------------
   Name: Get( string )
---------------------------------------------------------*/
function Get( name )

	if (SEntList[ name ] == nil) then return nil end

	// Create/copy a new table
	local retval = {}
	for k, v in pairs( SEntList[ name ].t ) do 
		retval[k] = v
	end

	return retval
end


/*---------------------------------------------------------
   Name: GetType( string )
---------------------------------------------------------*/
function GetType( name )

	if (SEntList[ name ] == nil) then return nil end
	return SEntList[ name ].type
	
end

/*---------------------------------------------------------
   Name: GetStored( string )
   Desc: Gets the REAL weapon table, not a copy
---------------------------------------------------------*/
function GetStored( name )
	return SEntList[ name ]
end

/*---------------------------------------------------------
   Name: GetList( string )
   Desc: Get a list of all the registered SWEPs
---------------------------------------------------------*/
function GetList()
	local result = {}
	
	for k,v in pairs(SEntList) do
		table.insert(result, v)
	end
	
	return result
end