--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- SYNC WEAPON FUNCTIONS
-- Provides an interface for dealing with sync weapons
--
-- (c) 2005 Relic Entertainment
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


--? @shortdesc Registers the sync weapon in the egroup and returns a SyncWeaponID you can use to keep track of it
--? @result SyncWeaponID
--? @args EGroupID egroup
function SyncWeapon_GetFromEGroup(egroup)

	local syncweapon
	
	-- takes the first sync weapon in an egroup... 
	local _CheckEntity = function(gid, idx, eid)
		if (Entity_IsSyncWeapon(eid) == true) then
			syncweapon = eid
			return true
		end
	end
	EGroup_ForEachEx(egroup, _CheckEntity, true, true)
	
	if syncweapon ~= nil then
		return Entity_GetGameID(syncweapon)
	end
	
end


--? @shortdesc Registers the sync weapon in the sgroup and returns a SyncWeaponID you can use to keep track of it
--? @result SyncWeaponID
--? @args SGroupID sgroup
function SyncWeapon_GetFromSGroup(sgroup)

	local syncweapon
	
	-- takes the first sync weapon in an sgroup... 
	local _CheckSquad = function(gid, idx, sid)
		
		for n = 1, Squad_Count(sid) do
			local eid = Squad_EntityAt(sid, n-1)
			if (Entity_IsSyncWeapon(eid) == true) then
				syncweapon = eid
				break
			end
		end
		
		if (syncweapon ~= nil) then
			return true
		end
		
	end
	SGroup_ForEachEx(sgroup, _CheckSquad, true, true)
	
	if syncweapon ~= nil then
		return Entity_GetGameID(syncweapon)
	end
	
end


--? @shortdesc Returns true if the specified player owns the sync weapon. Use a playerid of nil to see if it's unonwed.
--? @result Boolean
--? @args SyncWeaponID weapon, PlayerID player
function SyncWeapon_IsOwnedByPlayer(swid, player)

	-- not valid anymore?
	if not SyncWeapon_Exists(swid) then
		return false
	end
	
	local eid = Entity_FromWorldID(swid)
	local swp = nil
	
	if (eid ~= nil) then
		if (World_OwnsEntity(eid) == false) then
			swp = Entity_GetPlayerOwner(eid)
		end
	end
	
	if player == nil then
		return swp == nil
	else
		if swp == nil then
			return false
		else
			return World_GetPlayerIndex(swp) == World_GetPlayerIndex(player)
		end
	end
	
end


--? @shortdesc Returns the EntityID of a sync weapon, or nil if it's been destroyed
--? @result EntityID
--? @args SyncWeaponID weapon
function SyncWeapon_GetEntity(swid)
	if SyncWeapon_Exists(swid) then
		return Entity_FromWorldID(swid)
	end
end


--? @shortdesc Returns the position of a sync weapon, or nil if it's been destroyed
--? @result Position
--? @args SyncWeaponID weapon
function SyncWeapon_GetPosition(swid)
	if SyncWeapon_Exists(swid) then
		return Entity_GetPosition(Entity_FromWorldID(swid))
	end
end


--? @shortdesc Returns true if a sync weapon still exists in the game world
--? @result Boolean
--? @args SyncWeaponID weapon
function SyncWeapon_Exists(swid)
	return swid ~= nil and Entity_IsValid(swid) and Entity_IsSyncWeapon(Entity_FromWorldID(swid))
end


--? @shortdesc Sets whether a weapon to auto-target things or not
--? @args SyncWeaponID weapon, String hardpoint, Bool enable
--? @result Void
function SyncWeapon_SetAutoTargetting(swid, hardpoint, enable)

	if SyncWeapon_Exists(swid) then
		
		-- create the appropriate modifier
		local modifier = 1
		if (enable == true) then
			modifier = Modifier_Create(MAT_Weapon, "modifiers\\auto_target_enable_weapon_modifier.lua", MUT_Enable, true, 1, hardpoint)
		else
			modifier = Modifier_Create(MAT_Weapon, "modifiers\\auto_target_enable_weapon_modifier.lua", MUT_Enable, true, -1, hardpoint)
		end
		
		local eid = Entity_FromWorldID(swid)
		Modifier_ApplyToEntity(modifier, eid)
		
	end
	
end
