-- TEMPLATE MISSION SCRIPT : HUNT --
-- Tracks all ships of a specific type, activates the gate when they're all gone.

function init()
	--sets the default music of the stage
	stage.setMusic("music_mission4.ogg")
	
	--start the player at the correct waypoint
	stage.waypointStart = 0
	
	--loads a map in this mission folder
	stage.loadMap("asteroidbelt")
	
	--track barnicles (TO CHANGE SHIP TYPE: change the names here and in the one place down in update() )
	stage.trackShipName("Barnacle")
	targetCount = stage.countTrackedShipName("Barnacle")
	stage.vars.set("targets", targetCount)
	
	--set number of barnicles you actually have to kill
	--stage.vars.set("killTarget", math.max(2, targetCount-2))	--all but two
	stage.vars.set("killTarget", targetCount)			--ALL
	stage.vars.set("killCount", 0)
	
	--count the number of possible spawn groups (to activate them randomly)
	stage.vars.set("groupnum", stage.countSpawnGroups())
	
	--deactivate the gate
	stage.terrain_get("gate")
	terrain.vars.set("available", 0)
	
	--what i do?
	stage.newShakyText("DESTROY BARNACLES")
	stage.newMessage("TIME FOR BARNACLE SPRING CLEANING", "stolenrobot")

end

function update()
	--soft springy borders
	stage.borders()
	
	if stage.scriptTimerTick("targetcheck", 1000) == 1 then
		--count the number of current targets
		targetCount = stage.countTrackedShipName("Barnacle")
		--whenever a target is killed
		if targetCount < stage.vars.get("targets") then
			--update the counts--
			stage.vars.set("targets", targetCount)
			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 TARGETS", "stolenrobot")
			elseif remaining == 1 then
				stage.newMessage("EXTERMINATION PROGRESS :       DESTROY ONE MORE TARGET", "stolenrobot")
			end
		end
	end
	
	--if all/enough targets are killed
	if stage.vars.get("targets") <= 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)

			--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
end