
function init()
	--sets the default music of the stage
	roll = math.random(1,4)
	if roll == 1 or roll == 2 then
		stage.setMusic("music_mission1.ogg")
	elseif roll == 3 then
		stage.setMusic("music_mission4.ogg")
	elseif roll == 4 then
		stage.setMusic("music_mission5.ogg")
	end
	
	--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.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 (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)
	
	--give players a directive // any initial messages
	if stage.isMessageGroupEmpty(0) == 0 then
		stage.activateMessageGroup(0)
	elseif stage.isMessageGroupEmpty(1) == 0 then
		stage.activateMessageGroup(1)
	else
		--stage.newShakyText("DESTROY ALL ENEMIES")
		stage.newMessage("DESTROY ALIEN NEMESIS FOR GREAT HUMAN JUSTICE", "stolenrobot")
	end
	
	--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 not yet defeated all enemies
	if stage.vars.get("stagewon") == 0 and 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
			--stage.advanceSpawnCountdowns(1000)
			--if there are only a few enemies left
			if stage.factionCount(3) <= stage.vars.get("level") + 1 then
			--if stage.factionCount(3) <= 3 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)
					--since the message groups start counting at 1, it's the wave + 1
					stage.activateMessageGroup(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.enemiesAreDefeated(1) == 1 then
			if stage.factionCount(3) == 0 then
				--sets the victory music! (I was obviously quite conflicted about this)
				--stage.setMusic("music_theme_orchestral2.ogg")
				stage.setMusic("music_mission3.ogg")
				--stage.setMusic("music_theme.ogg")
				--stage.setMusic("music_mission2_leadsolo.ogg")
				--stage.setMusic("music_ghostyard.ogg")
				--stage.setMusic("music_victory_demo3.ogg")--, 1)
				--stage.setMusic("music_victory_demo4a.ogg", 1)
				--stage.setMusic("music_victory_demo4b.ogg", 1)
				--activate the gate
				stage.terrain_get("gate")
				terrain.vars.set("available", 1)
				--terrain.vars.set("activated", 1)--actually turn it on
				--beacon the gate
				stage.waypoint_setbeacon(0, 1)
				--make allies gtfo
				stage.broadcast("stageend", 1)
				--woo we won
				stage.vars.set("stagewon", 1) 
			end
		end
	end
	

	
	--boooo you are a failure
	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("ALIEN NEMESIS DEFEATED", "stolenrobot")
			stage.newMessage("CELEBRATE VICTORY WITH GRAND GATE PARADE", "stolenrobot")
			stage.newMessage("USE THE BOOST TO GET THROUGH ["..stage.controlName("afterburn").."]", "stolenrobot")
			stage.newMessage("ALL FRIENDS MADE", "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