
function init()
	--sets the default music of the stage
	stage.setMusic("music_bosstheme_orchestral.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")
	
	--setup the waves
	stage.vars.init("end", 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)
	
	--give players a directive // any initial messages
	stage.activateMessageGroup(0)
	
	--track if we've won yet or not
	stage.vars.set("stagewon", 0) 
	
	--stage.newMessage(stage.vars.get("wavecount") .. " WAVES TOTAL", "stolenrobot")
end

function update()
	--soft springy borders
	stage.borders()
	
	--revive dead players at waypoint 0
	--stage.reviveDeadPlayers(0)
	
	
	
	--if we've defeated all enemies
	if stage.scriptTimerTick("check", 500) == 1 then--(so we're not _constantly_ going through all ships)

		--check to see if we've gotten the boss yet
		stage.trackShipName("AngelPod")
		if stage.countTrackedShipName("AngelPod") >= 1 then
			stage.newMessage("FINISH HIM AND A WINNER IS YOU", "stolenrobot")
			stage.newMessage("VICTORY IS WITHIN OUR GRASP", "stolenrobot")
			stage.newMessage("VILE ALIEN NEMESIS NEARLY DEFEATED", "stolenrobot")
			
			--sets the default music of the stage
			stage.setMusic("music_theme.ogg")
			--activate the gate (no helpful hint text)
			stage.terrain_get("gate")
			terrain.vars.set("available", 1)
			terrain.vars.set("gatehint", 0)
			--beacon the gate
			--stage.waypoint_setbeacon(0, 1)
			
			--make allies stand down
			stage.broadcast("weaponshot", 0)
			
			--kill remaining peons in faction 3
			if stage.vars.get("end") == 0 then stage.killUntrackedShips(3) end
			
			--start checking for victory
			stage.vars.set("end", 1)
		end
			
		--have we neared the end yet?
		if stage.vars.get("end") > 0 and stage.failure == 0 then
			--the player killed the pod
			if stage.factionCount(3) == 0 then
				if stage.vars.get("end") == 1 then
					stage.vars.set("end", 2)
					stage.clearMessages()
					--start the helpful hint text
					stage.terrain_get("gate")
					terrain.vars.set("gatehint", 1)
				end
				--wooo you are a winner
				if stage.scriptTimerTick("gatereminder", 10000) == 1 or stage.vars.get("firstreminder") == 0 then
					stage.newMessage("ALIEN NEMESIS DEFEATED", "stolenrobot")
					stage.newMessage("CELEBRATE VICTORY WITH GRAND GATE PARADE", "stolenrobot")
					stage.newMessage("USE THE BOOST TO GET THROUGH [LEFT SHIFT]", "stolenrobot")
					stage.vars.set("firstreminder", 1)
				end
			end
		end
	end

	if stage.failure == 1 then stage.playDeathMessages() end
	
end