
function init()
	--boss battle music!
	stage.setMusic("music_bosstheme.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")
	
	--create a databank
	--stage.warpShip("Databank", "", math.random(-1000,1000), math.random(600, 1000), 1, "", 100)

	--track databanks
	--stage.trackShipName("Databank")
	
	--track enemies
	stage.trackBeacons = 1		--puts arrows towards on the tracked ships
	
	--save the gate's location (to pull points towards at the end of the battle)
	stage.terrain_get("gate")
	stage.vars.set("gatex", terrain.centerx)
	stage.vars.set("gatey", terrain.centery)
	terrain.vars.set("available", 0)--deactivate it
	
	--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)
	
	--give players a directive // any initial messages
	stage.newShakyText("ENTERING GREAT SPACE REEF")
	stage.newMessage("DISRUPT DELICATE ECOLOGICAL BALANCE FOR ACTION ADVENTURE", "stolenrobot")
	
	--track if we've won yet or not
	stage.vars.set("bossdefeated", 0)
	stage.vars.set("pods", 0)
	
	
end

function update()
	--soft springy borders
	stage.borders()
	
	--(so we're not constantly checking for EVERYTHING)
	if stage.scriptTimerTick("check", 1000) == 1 then
		--if we're fighting the boss
		if stage.vars.get("bossdefeated") == 0 then 	
			--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)
						--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.factionCount(3) == 0 then
					--sets the victory music!
					stage.setMusic("music_mission2_leadsolo.ogg", 1)
					
					--activate the gate
					stage.terrain_get("gate")
					terrain.vars.set("available", 1)
					--player can't quite leave yet though
					terrain.vars.set("playercanexit", 0)
					
					--make allies gtfo
					stage.broadcast("stageend", 1)
					
					--wooo you are a winner
					roll = math.random(1, 3)
					if roll == 1 then
						stage.newMessage("GRAND GATE PARADE ACCOMPANIES GRAND REEF DEATH", "stolenrobot")
					elseif roll == 2 then
						stage.newMessage("SCRAPPY UNDERDOG WITH MASSIVE WEAPONRY DEFEATS HAPLESS ALIEN LIFE", "stolenrobot")
					elseif roll == 3 then
						stage.newMessage("ENJOY LEGITIMATE VICTORY OVER BYSTANDER ECOSYSTEM", "stolenrobot")
					end
					
					--if another game does something cool and i reference it, i'm cool as well right???
					
					
					--time to get the databank!
					stage.vars.set("bossdefeated", 1)
					
					stage.trackShipName("Databank")
					stage.vars.set("pods", stage.countTrackedShipName("Databank"))
					if stage.vars.get("pods") > 0 then--(there should have been at least one pod dropped)
						stage.newMessage("WOULD YOU KINDLY RETRIEVE DATABANK TO GATE", "stolenrobot")
						stage.trackBeacons = 3--BIG arrows towards things
						
					else--(just in case a databank doesn't drop)
						--let the player finally go through the gate
						stage.terrain_get("gate")
						terrain.vars.set("available", 1)
						terrain.vars.set("playercanexit", 1)
						stage.newMessage("WHAT DID YOU DO TO THE DATABANK?", "stolenrobot")
					end
					
					--arrow to the gate + the beacon
					stage.waypoint_setbeacon(0, 1)
				end
			end
			
		--if we've beaten the boss, time to get the databank and go home
		else
			--pull all points towards the pod/gate
			stage.pullPointsTowards(stage.vars.get("gatex"), stage.vars.get("gatey"))
		
			if stage.vars.get("pods") > 0 then
				--get the number of pods on the map
				stage.vars.set("pods", stage.countTrackedShipName("Databank"))
				--after we get the pod...
				if stage.vars.get("pods") <= 0 then
					stage.trackBeacons = 0--stop making a big deal of arrows
				
					--let the player finally go through the gate
					stage.terrain_get("gate")
					terrain.vars.set("available", 1)
					terrain.vars.set("playercanexit", 1)
					
					--make a message
					stage.newMessage("PASSWORD WAS '" .. stage.getRandomVIP() .. "'", "stolenrobot")
					stage.newMessage("THAT WAS MY NEXT GUESS", "stolenrobot")
				end
			end
		end
	end

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