
	-- add every second, reduce if work load is too heavy
	Rule_AddInterval(Rule_DetectUnderAttack, 1)
	
	t_Group1 = {
		sgroup = 		{ sg_name1, sg_name2, sg_name3 },
		targetcount = 	{ 10, 20, 15 },
		recovercount = 	{ 60, 50, 45 },
		doicare = 		{true, false, true},
		-- the 'Under Attack' plan and move data
		planA = 		{"move", 		"pathattack", 		"path"},
		valueA = 		{mkr_name1, 	"pathname1", 		"pathname2"},
		-- the 'No Longer Under Attack' plan and data
		planB = 		{"attack", 		"move", 		"pathattack"},
		valueB = 		{mkr_name1, 	mkr_name2, 		"pathname3"},
	}
	
	-- for generic data use a loop to generate
	for i = 1, table.getn(t_Group1.sgroup) do 
		t_Group1.active[i] = false
		t_Group1.recovered[i] = true
		t_Group1.counter[i] = 0
	end
	
function Rule_DetectUnderAttack()
	
	-- just to shorten the name :)
	local t = t_Group1
	
	for i = 1, table.getn(t.sgroup) do 
		
		-- is the group alive? empty? etc.
		if SGroup_IsEmpty(t.sgroup[i]) == false then
			
			-- is the group 'inactive' and 'recovered' AND 'Under Attack'
			if t.isactive[i] == false and t.recovered[i] == true and SGroup_IsUnderAttack(t.sgroup[i], false, 1) then
				
				-- initiate the state checking
				t.isactive[i] = true
				t.counter[i] = 0
				
				-- spawn reinforcements 
				
				-- do stuff whatever you want
				
			-- is the group 'active' and is the time spent active greater than the target AND do I care if its still under attack
			elseif t.isactive[i] == true and t.counter[i] > t.targetcount[i] and DoICareIfImUnderAttack( t.sgroup[i], t.doicare[i] )  then
				
				-- if its still under attack do plan A
				if SGroup_IsUnderAttack(t.sgroup[i], false, 1) then
					-- take an 'Under Attack' action
					doaction = {t.sgroup[i], t.planA[i], t.valueA[i]}
					DoTankAction( doaction )
				else
					-- otherwise do plan B
					doaction = {t.sgroup[i], t.planB[i], t.valueB[i]}
					DoTankAction( doaction )
				end
				
				-- change state checking flags
				t.isactive[i] = false
				t.recovered[i] = false
				t.counter[i] = 0
				
			end
			
		end
		
		-- is the sgroup active?
		if t.isactive[i] == true then
			
			-- increment counter
			t.counter[i] = t.counter[i]+1
			
		-- is the sgroup inactive and still recovering?
		elseif t.isactive[i] == false and t.recovered[i] == false then
			
			-- increment the counter
			t.counter[i] = t.counter[i]+1
			
			-- is the group recovered?
			if t.counter[i] > t.recovercount[i] then
				
				-- yes the group is recovered
				t.recovered[i] = true
				t.counter[i] = 0
				
			end
			
		end
		
	end

end

function DoICareIfImUnderAttack(sgroup, boolean)
	if boolean == true and SGroup_IsUnderAttack(sgroup, false, 5) then
		return true -- if you care if its under attack AND its under attack then return TRUE
	elseif boolean == false then
		return true -- if you DON'T CARE if its under attack then return TRUE
	end
end

function DoTankAction( tablename )
	
	if tablename[2] == "move" then
		Cmd_MoveToMarker(tablename[1], tablename[3])
	elseif tablename[2] == "attack" then
		Cmd_AttackMoveMarker(tablename[1], tablename[3])
	elseif tablename[2] == "pathmove" then
		Cmd_SquadPath(tablename[1], tablename[3], true, true, false, 0)
	elseif tablename[2] == "pathattack" then
		Cmd_SquadPath(tablename[1], tablename[3], true, true, true, 20)
	end
	
end
