
// DO NOT EDIT THIS FILE!

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

/*---------------------------------------------------------
   Name: weapons
   Desc: A module to manage adding and fetching SWEPS
---------------------------------------------------------*/
module("weapons")

local WeaponList = {}


/*---------------------------------------------------------
   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 ( k != "BaseClass" && type(t[k]) == "table" ) then
			TableInherit( t[k], v )
		end
		
	end
	
	t["BaseClass"] = base
	
	return t

end


/*---------------------------------------------------------
   Name: Register( table, string, bool )
   Desc: Used to register your SWEP with the engine
---------------------------------------------------------*/
function Register( t, name, reload )

	// Don't load it twice unless we're reloading
	if (!reload && WeaponList[ name ] != nil ) then
		return
	end
	
	// This gives the illusion of inheritence
	//if ( name != "weapon_base" ) then
	//	t = TableInherit( t, Get( "weapon_base" ) )
	//end
	
	t.Classname = name

	WeaponList[ name ] = t	

end

/*---------------------------------------------------------
   Name: Get( string )
   Desc: Get a weapon by name.
---------------------------------------------------------*/
function Get( name )

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

	// Create/copy a new table
	local retval = {}
	for k, v in pairs( WeaponList[ name ] ) do 
		retval[k] = v
	end
	
	if ( retval.Base != name ) then
	
		if ( !GetStored( name ) ) then
			Msg("Error: Weapon "..name.." is derived from non existant weapon - "..retval.Base.."!\n")
		else
			retval = TableInherit( retval, Get( retval.Base ) )
		end
		
	end

	return retval
end

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

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