
function init()
	--sets the default music of the stage
	--stage.setMusic("music_mission1_redux.ogg")
	stage.setMusic("music_cryopod.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")
	
	--count the number of possible spawn groups
	stage.vars.set("groupnum", stage.countSpawnGroups())
	
	--track cryopods
	stage.trackShipName("Cryopod")
	stage.vars.set("pods", stage.countTrackedShipName("Cryopod"))
	stage.trackBeacons = 1
	
	--beacon the gate
	stage.terrain_get("gate")
	stage.waypoint_setbeacon(0, 1)
	--player can't quite leave yet though
	terrain.vars.set("playercanexit", 0)
	
	--spawn couple enemies
	for i = 1, 2 do
		wave = math.random(1, stage.vars.get("groupnum"))
		stage.activateSpawnGroup(wave, 1)
	end
	
	--track if we've won yet or not
	stage.vars.set("stagewon", 0) 
	
	--what i do?
	stage.newShakyText("ENDLESS NEMESIS FLEET")
	stage.newShakyText("QUICKLY RETRIEVE ALL CRYOPODS")
	stage.newMessage("TOW TO GATE ALL FRIENDS-TO-BE", "stolenrobot")
end

function update()
	--soft springy borders
	stage.borders()
	
	spawn = 0
	
	--spawn on a timer
	if stage.scriptTimerTick("waveinterval", 14000) == 1 then spawn = 1 end
	
	--spawn if there aren't enough enemies
	if stage.scriptTimerTick("checkenemies", 3000) == 1 then
		if stage.enemyCount(1) <= 4 then spawn = 1 end
	end
	
	--spawn enemies every interval (if there isn't a ridiculous number already)
	if spawn == 1 then
		if stage.enemyCount(1) <= 20 then
			--select a spawn group to activate
			wave = math.random(1, stage.vars.get("groupnum"))
			--force-activate that group
			stage.activateSpawnGroup(wave, 1)
		end
		
		-- don't encourage them to stick around after all pods are rescued
		if stage.vars.get("endstage") == 1 then
			stage.GFP("with the landlord cussin right behind")
			for i = 1, 3 do
				wave = math.random(1, stage.vars.get("groupnum"))
				stage.activateSpawnGroup(wave, 1)
			end
		end
	end
	
	--check for rescued cryopods
	if stage.scriptTimerTick("targetcheck", 1000) == 1 then
		oldpods = stage.vars.get("pods")
		--update the number of pods
		stage.vars.set("pods", stage.countTrackedShipName("Cryopod"))
		--when we go down to a single pod, send a message
		if oldpods > 1 and stage.vars.get("pods") == 1 then
			stage.newMessage("WE LEAVE NO ONE BEHIND", "stolenrobot")
		end
	end
	
	--if no cryopods are left
	if stage.vars.get("pods") <= 0 then
		if stage.vars.get("endstage") == 0 then
			--sets the victory music!
			stage.setMusic("music_mission3.ogg")
			--stage.setMusic("music_victory_demo.ogg")
			
			--player can leave
			stage.terrain_get("gate")
			terrain.vars.set("playercanexit", 1)
			
			--make allies gtfo
			stage.broadcast("stageend", 1)

			--woo we won
			stage.vars.set("stagewon", 1) 
			
			--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
	
	--gentle reminder to get the pod
	if stage.scriptTimerTick("databankreminder", 60000) == 1 then
		stage.newMessage("NEMESIS FLEET UNENDING", "stolenrobot")
		stage.newMessage("RETRIEVE ALL PODS AND RETURN TO GATE", "stolenrobot")
	end
	
	if stage.failure == 1 then
		stage.playDeathMessages()
	--wooo you are a winner
	elseif stage.vars.get("stagewon") == 1 then
		--wooo you are a winner
		if stage.scriptTimerTick("gatereminder", 10000) == 1 or stage.vars.get("firstreminder") == 0 then
			stage.newShakyText("NO REMAINING CRYOPODS")
			stage.newShakyText("GATE PARADE NOW!")
			--stage.newMessage("NO REMAINING CRYOPODS", "stolenrobot")
			stage.newMessage("TIME TO TAKE PERSONAL GRAND GATE PARADE", "stolenrobot")
			stage.vars.set("firstreminder", 1)
		end
	end

end