

-- added from the 'OnInit()'
function Objective_Register()

	--[[ *** MUST INITIALIZE THE OBJ TABLE BEFORE REGISTERING *** ]]
	Initialize_OBJ_RescueWounded()

end

-- add this rule at the appropriate spot
Rule_AddInterval(OBJ_RW_Grant_PlayerProximity, 5)


-------------------------------------------------------------------------
-- [[ Rescue the Wounded ]]
-------------------------------------------------------------------------
-- The player must send a commander out to rescue some riflemen
-- pinned from the opening NIS barrage. When the commander gets 
-- near they will follow him. The objective is complete when
-- they are healed.
-------------------------------------------------------------------------
function Initialize_OBJ_RescueWounded()

	OBJ_RescueWounded = {
		
		SetupUI = function()
			
			-- show the wounded on the minimap/tacmap
			OBJ_RescueWounded.WoundedMapElementID = {}
			for i = 1, table.getn(sg_wounded) do
				
				-- keep the ping ID so that you can remove it later if necessary
				OBJ_RescueWounded.WoundedMapElementID[i] = Objective_AddUIElements(OBJ_RescueWounded, sg_wounded[i], true, false, false, false)
				
			end
			
		end,
		
		OnStart = function()
			
			-- change FOW
			World_EnablePlayerToPlayerFOW(player1, player3, false)
			
			-- announce the goal
			Util_StartIntel(EVENTS.OBJ_RW_START)
			
			-- add the detect function
			t_obj_rw_track = {
				dead = {false, false, false},
				found = {false, false, false},
				healed = {false, false, false}
			}
			Rule_AddInterval( OBJ_RW_Wounded_DetectState, 5)
			
		end,
		
		OnComplete = function()
			
			-- tell the player they did good
			Util_StartIntel(EVENTS.OBJ_RW_COMPLETE)
			
		end,
		
		OnFail = function()
			
			-- tell the player they failed
			Util_StartIntel(EVENTS.OBJ_RW_FAILED)
			
		end,
		
		OnSitRep = function()
			
			-- I'm not sure what should go here, but the game will call this function on sitreps...
			
		end,
		
		Title 			= 300200,
		Description 	= 300201,
		isPrimary 		= false,
		
	}

	--[[ Registering Objectives ]]
	Objective_Register(OBJ_RescueWounded) --[[ HERE's THE REGISTER FUNCTION ]]
	
end



-------------------------------------------------------------------------
-- [[ Rescue the Wounded - Functions ]]
-------------------------------------------------------------------------

-- grant the objective when the player gets near the wounded
function OBJ_RW_Grant_PlayerProximity()
	if Player_AreSquadsNearMarker(player1, mkr_al_woundedtrigger) then
		
		Objective_Start(OBJ_RescueWounded) --[[ HERE's THE START FUNCTION ]]
		
		Rule_RemoveMe()
	end
end


-- detect the health/status of the wounded squad memebers
function OBJ_RW_Wounded_DetectState()

	-- remove pings for fully healed sgroups
	for i = 1, table.getn(sg_wounded) do
		
		if SGroup_GetAvgHealth(sg_wounded[i]) == 1.0 then
			
			-- removing already-removed pings is safe
			Objective_RemoveUIElement(OBJ_RescueWounded, OBJ_RescueWounded.WoundedMapElementID[i])
			
		end
		
	end
	
	-- see if they all have been healed up
	if SGroup_GetAvgHealth(sg_al_wounded_all) == 1.0 then
		
		-- job complete
		Objective_Complete(OBJ_RescueWounded) --[[ HERE's THE COMPLETE FUNCTION ]]
		-- remove rule
		Rule_RemoveMe()
		
	-- see if they are all dead
	elseif SGroup_IsEmpty(sg_al_wounded_all) then
		
		--job failed
		Objective_Fail(OBJ_RescueWounded) --[[ HERE's THE FAILED FUNCTION ]]
		-- remove rule
		Rule_RemoveMe()
	end
end
