
function init()
	--sets the default music of the stage
	stage.setMusic("music_mission1_corrosive.ogg")
	
	--start the player at the correct waypoint
	stage.waypointStart = 0
	
	--track all created ships of this faction
	stage.trackFaction = 3
	
	--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")
	
	--should we be spawning waves? AKA have we contacted the survivor yet
	stage.vars.set("started", 0)
	
	--setup the waves
	--stage.vars.set('wavecount', 3)		--now set in generated setup script
	stage.vars.set("wave", 0)		--current wave (before the first wave)
	stage.vars.init("phase", 0)		--[0:waiting on a spawn | 1:waiting for a spawn wave to be destroyed)
	stage.trackBeacons = 1		--puts arrows towards on the tracked ships
	
	--deactivate the gate
	stage.terrain_get("gate")
	terrain.vars.set("available", 0)

	--track survivors (but not ones that are pre-placed)
	stage.trackShipBehavior("survivor")

	--give players a directive // any initial messages
	stage.newShakyText("FIND AND ASSIST ALLY")
	
	if stage.isMessageGroupEmpty(0) == 0 then
		stage.activateMessageGroup(0)
	--elseif stage.isMessageGroupEmpty(1) == 0 then
	--	stage.activateMessageGroup(1)
	else--motivational poetry
		stage.newMessage("IS NOBLE CAUSE INDEED TO ASSIST THOSE IN NEED", "stolenrobot")
	end
	
	--track if we've won yet or not
	stage.vars.set("stagewon", 0) 
	
end

function update()
	--soft springy borders
	stage.borders()
	
	--if we have not yet started the map
	if stage.vars.get("started") == 0 then
		--if the survivor we're tracking has activated
		if stage.getTrackedShip(0) == 1 and ship.behavior == "survivor" then
			if ship.controller.vars.get("jumpstarted") == 1 then
				stage.vars.set("started", 1)
				stage.clearMessages()
				stage.activateMessageGroup(1)
			end
		else--if we couldn't find the survivor (ARE THEY DED???)
			stage.GFP("survivor dead or missing, starting waves")
			stage.vars.set("started", 1)
		end
	else
		--if we've defeated all enemies
		if stage.scriptTimerTick("check", 2000) == 1 then--(so we're not constantly going through all ships)
			--if there are still things to spawn
			if stage.respawnsStillActive() == 1 then
				--if there are only a few enemies left
				if stage.factionCount(3) <= stage.vars.get("level") + 1 then
					--if the current spawn group isn't already activated
					if stage.isSpawnGroupActivated(stage.vars.get("wave")) == 0 then
						stage.GFP("level " .. stage.vars.get("level") .. "  next group activated! " .. stage.vars.get("wave"))
						--activate the next spawn countdown (forcing the activation)
						stage.activateSpawnGroup(stage.vars.get("wave"), 1)
						--count up the spawn counter
						stage.vars.set("wave", stage.vars.get("wave") + 1)
					end
				end
			--if all waves have spawned
			else
				--if there are no enemies left on the map
				if stage.factionCount(3) == 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)
					--tell the survivor to go through the gate
					if stage.getTrackedShip(0) == 1 then
						if ship.behavior == "survivor" then ship.controller.vars.set("stageend", 1) end
					end
					--make allies gtfo
					stage.broadcast("stageend", 1)
					--woo we won
					stage.vars.set("stagewon", 1) 
				end
			end
		end
	end
	
	if stage.failure == 1 then
		stage.playDeathMessages(1)
		
	--wooo you are a winner
	elseif stage.vars.get("stagewon") == 1 then
		if stage.scriptTimerTick("gatereminder", 10000) == 1 or stage.vars.get("firstreminder") == 0 then
			stage.newMessage("GATE IS ACTIVE", "stolenrobot")
			if stage.getTrackedShip(0) == 1 then
				stage.newMessage("CONDUCT GRAND GATE PARADE WITH NEW FRIEND", "stolenrobot")
			end
			stage.newMessage("USE THE BOOST TO GET THROUGH ["..stage.controlName("afterburn").."]", "stolenrobot")
			stage.vars.set("firstreminder", 1)
		end
		
		
		--pull all points towards the center gate
		stage.pullPointsTowards(stage.vars.get("gatex"), stage.vars.get("gatey"))

	end
end