
function init()
	--sets the default music of the stage
	stage.setMusic("music_mission3.ogg")
	
	--start the player at the correct waypoint
	stage.waypointStart = 0
	
	--any battle-specific values are dynamically generated (music, text, bosses...)
	stage.callLuaFile("galaxy_battle_setup", "setup")
	
	--loads a map in this mission folder
	stage.loadMap("galaxy_battle")
	
	--track barnicles
	stage.trackShipName("Barnacle")
	barnacleCount = stage.countTrackedShipName("Barnacle")
	stage.vars.set("barnacles", barnacleCount)
	
	--set number of barnicles you actually have to kill
	stage.vars.set("killTarget", math.max(2, barnacleCount-2))
	stage.vars.set("killCount", 0)
	
	--count the number of possible spawn groups
	stage.vars.set("groupnum", stage.countSpawnGroups())
	
	--deactivate the gate (and save its location)
	stage.terrain_get("gate")
	terrain.vars.set("available", 0)
	stage.vars.set("gatex", terrain.centerx)
	stage.vars.set("gatey", terrain.centery)
	
	--what i do?
	stage.newMessage("SEARCH AND DESTROY ALIEN BARNACLES", "stolenrobot")

end

function update()
	--soft springy borders
	stage.borders()
	
	if stage.scriptTimerTick("targetcheck", 1000) == 1 then
		--count the number of current barnacles
		barnacleCount = stage.countTrackedShipName("Barnacle")
		--whenever a barnacle is killed
		if barnacleCount < stage.vars.get("barnacles") then
			--update the counts--
			stage.vars.set("barnacles", barnacleCount)
			stage.vars.set("killCount", stage.vars.get("killCount") + 1)
			remaining = stage.vars.get("killTarget") - stage.vars.get("killCount")
		
			--spawn some enemies--
			--select a spawn group to activate
			wave = math.random(1, stage.vars.get("groupnum"))
			--force-activate that group
			stage.activateSpawnGroup(wave, 1)
			
			--give a message about how many are left--
			if remaining > 1 then
				stage.newMessage("EXTERMINATION PROGRESS :       DESTROY " .. remaining .. " ADDITIONAL BARNACLES", "stolenrobot")
			elseif remaining == 1 then
				stage.newMessage("EXTERMINATION PROGRESS :       DESTROY ONE MORE BARNACLE", "stolenrobot")
			end
		end
	end
	
	--if all/enough barnacles are killed
	if stage.vars.get("barnacles") <= 0 or stage.vars.get("killCount") >= stage.vars.get("killTarget") then
		if stage.vars.get("endstage") == 0 then
			--sets the victory music!
			stage.setMusic("music_mission3.ogg")
			--activate the gate
			stage.terrain_get("gate")
			terrain.vars.set("available", 1)
			--beacon the gate
			stage.waypoint_setbeacon(0, 1)
			--make allies gtfo
			stage.broadcast("stageend", 0)
			--wooo you are a winner
			stage.newMessage("GATE PORTAL FREE FROM INHIBITION", "stolenrobot")
			stage.newMessage("CELEBRATE EXTERMINATION WITH GRAND GATE ESCAPE", "stolenrobot")
			stage.newMessage("USE THE BOOST TO GET THROUGH [LEFT SHIFT]", "stolenrobot")
			
			--spawn a bunch of enemies
			for i = 1, 4 do
				wave = math.random(1, stage.vars.get("groupnum"))
				stage.activateSpawnGroup(wave, 1)
			end
			stage.vars.set("endstage", 1)
		end
	end
	
	if stage.failure == 1 then stage.playDeathMessages() end
	
	--gentle reminder of objective
	--if stage.scriptTimerTick("hintcheck", 45000) == 1 and stage.countTrackedShips() > 0 then
	--	hintMessage()
	--end
end