function OnInit()
	SobGroup_Create("sg_Survivors")
	Player_SetDefaultShipTactic(0, AggressiveTactics)
	Player_SetDefaultShipTactic(1, AggressiveTactics)
	Rule_AddInterval("Rule_SurvivorCount", 3)
	Rule_AddInterval("Rule_Briefing", 8)
	Rule_AddInterval("Rule_StartPlay", 13)
	Camera_SetLetterboxStateNoUI(1, 2)
end

function Rule_SurvivorCount()
	DisplaySurvivorCount()
	Rule_Remove("Rule_SurvivorCount")
end

function DisplaySurvivorCount()
	local iSurvivorCount = GetSurvivorCount()
	local message = strsub(tostring(iSurvivorCount), 1, 2)..","..strsub(tostring(iSurvivorCount), 3, 5).." SURVIVORS"
	LocationCard(message, 4)
end

function GetSurvivorCount()
	local tSurvivors = { { name = "hur_galactica", number = 2200, }, {name = "hur_containership", number = 250,}, {name = "hur_raptor", number =2,}, { name = "hur_vipermkii", number = 1,}, {name = "hur_vipermkxii", number = 1,}, }
	local iSurvivorCount = 0
	for i, iCount in tSurvivors do
		SobGroup_Clear("sg_Survivors")
		Player_FillShipsByType("sg_Survivors", 0, iCount.name)
		iSurvivorCount = iSurvivorCount + iCount.number*SobGroup_Count("sg_Survivors")
	end
	return iSurvivorCount
end

function Rule_Briefing()
	Subtitle_Add(Actor_FleetIntel, "To All Viper Pilots: Engage enemy raider squadron enroute to Virgon orbit.", 5)
	Rule_Remove("Rule_Briefing")
end

function Rule_StartPlay()
	Camera_SetLetterboxStateNoUI(0, 2)
	iCountdown = 5*60
	Rule_AddInterval("Rule_FTLCountdown", 1)
	Rule_AddInterval("Rule_WatchGalactica", 1)
	CPU_Enable(1, 0)
	local tVol1 = randomVector(0, 360, 0, 360, 15000)
	Volume_AddSphere("vol_Cylon1", {tVol1[1], tVol1[2], tVol1[3],}, 2000)
	SobGroup_ExitHyperSpace("sg_Cylon_1", "vol_Cylon1")
	SobGroup_AttackPlayer("sg_Cylon_1", 0)
	local tVol2 = randomVector(0, 360, 0, 360, 15000)
	Volume_AddSphere("vol_Cylon2", {tVol2[1], tVol2[2], tVol2[3],}, 2000)
	SobGroup_ExitHyperSpace("sg_Cylon_2", "vol_Cylon2")
	SobGroup_AttackPlayer("sg_Cylon_2", 0)
	SobGroup_SetSpeed("sg_Cylon_Base1", 0.8)
	SobGroup_SetSpeed("sg_Cylon_Base2", 0.8)
	SobGroup_Attack(1, "sg_Cylon_Base1", "sg_Galactica")
	SobGroup_Attack(1, "sg_Cylon_Base2", "sg_Galactica")
	Rule_Remove("Rule_StartPlay")
end

function Rule_FTLCountdown()
	iCountdown = iCountdown - 1
	if iCountdown > 0 then
		local m = floor(iCountdown/60)
		local s = iCountdown - m*60
		local message = format("Remaining time: %02d:%02d", m, s)
		LocationCard(message,1)
	else
		LocationCard("JUMP PREP COMPLETE - PREPARE FOR JUMP TO RAGNAR ANCHORAGE", 3)
		Rule_AddInterval("Rule_FTLJump", 5)
		Rule_Remove("Rule_FTLCountdown")
	end
end

function Rule_FTLJump()
	SobGroup_EnterHyperSpaceOffMap("Player_Ships0")
	Camera_SetLetterboxStateNoUI(1, 2)
	DisplaySurvivorCount()
	Rule_AddInterval("Rule_End", 5)
	Rule_Remove("Rule_FTLJump")
end

function Rule_WatchGalactica()
	if SobGroup_Empty("sg_Galactica") == 1 then
		Rule_Remove("Rule_FTLCountdown")
		LocationCard("BATTLESTAR GALACTICA HAS BEEN DESTROYED", 3)
		Rule_AddInterval("Rule_End", 5)
	end
end


function Rule_End()
	setGameOver()
	Rule_Remove("Rule_End")
end


function randomVector(azimuth, azimuth_var, site, site_var, length)
	local li_si = (site - site_var) + random(site_var*2)
	local li_az = (azimuth - azimuth_var) + random(azimuth_var*2)
	local t_vector = {}
	t_vector[1] = floor(length * cos(li_si) * cos(li_az))
	t_vector[3] = floor(length * cos(li_si) * sin(li_az))
	t_vector[2] = floor(length * sin(li_si))
	return t_vector
end
