
function init()
	--sets the default music of the stage
	stage.setMusic("music_mission2_leadsolo.ogg")
	
	--start the player at the correct waypoint
	stage.waypointStart = 0
	
	--loads a map in this mission folder
	stage.loadMap("vector_battle")
	
	--blank background
	--stage.setBackdropName(0, "")
	--stage.setBackdropName(1, "")
	stage.minimalHUD = 1
	
	--setup the waves
	stage.vars.set("wave", 0)		--current wave (before the first wave)
	stage.vars.set("wavenum", 6)
	stage.trackBeacons = 1		--puts arrows towards on the tracked ships
	
	--number of lives
	stage.vars.set("lives", 3)
	
	--player plays as CLASSIC SHIP
	--stage.setPlayerShips("VectorClassic", "ALPHA")
	
	stage.newShakyText("HACK SYSTEM AND SURVIVE")
	
	--stage.newMessage("IS NOBLE CAUSE INDEED TO ASSIST THOSE IN NEED", "stolenrobot")
end

function update()
	--cheaty way to set stage size to screen size
	stage.setSizeToScreen()

	--soft springy borders
	--stage.borders()
	stage.wrap()
	
	--revive if die
	if stage.vars.get("lives") > 0 then
		if stage.reviveDeadPlayers(0) == 1 then
			stage.vars.set("lives", stage.vars.get("lives") - 1)
			stage.createText(stage.vars.get("lives") .. " lives left!", 0, 0, 1800)
		end
	end
	
	--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.vars.get("wave") < stage.vars.get("wavenum") then
			--if we've cleared the enemies
			if stage.factionCount(3) == 0 then
				--get another life
				if math.mod(stage.vars.get("wave"), 2) == 1 then
					stage.vars.set("lives", stage.vars.get("lives") + 1)
					stage.createText("+1 life!", 0, 0, 1800)
				end
				--spawn a couple of asteroids, enemies
				turretnum = 0
				shipnum = 0
				asteroidnum = 0
				spawn = math.random(0, math.min(stage.vars.get("wave")+2, 10))
				if spawn == 0 then
					asteroidnum = 6
				elseif spawn == 1 then
					asteroidnum = 4
					turretnum = 1
				elseif spawn == 2 then
					asteroidnum = 4
					turretnum = 3
				elseif spawn == 3 then
					asteroidnum = 3
					shipnum = 1
				elseif spawn == 4 then
					asteroidnum = 5
					turretnum = 1
					shipnum = 1
				elseif spawn == 5 then
					asteroidnum = 2
					turretnum = 3
					shipnum = 1
				elseif spawn == 6 then
					asteroidnum = 4
					shipnum = 3
				elseif spawn == 7 then
					asteroidnum = 5
					turretnum = 6
				elseif spawn == 8 then
					asteroidnum = 1
					shipnum = 4
				else
					asteroidnum = 8
				end
				
				edgex = stage.width * .8
				edgey = stage.height * .8
				
				for i = 1, asteroidnum do
					ax = math.random(-edgex, edgex)
					ay = math.random(-edgey, edgey)
					atype = math.random(0,2)
					aspin = math.random() - .5
					if atype == 0 then
						stage.warpShip("VectorAsteroid2", "", ax, ay, 3, "", 500)
						warp.spin = aspin * 3
					else
						stage.warpShip("VectorAsteroid1", "", ax, ay, 3, "", 500)
						warp.spin = aspin * 6
					end
				end
				
				for i = 1, turretnum do
					ax = math.random(-edgex, edgex)
					ay = math.random(-edgey, edgey)
					stage.warpShip("VectorTurret", "", ax, ay, 3, "", 500)
					ship.bump(math.random(0,359), math.random(500, 3000))
				end
				
				for i = 1, shipnum do
					ax = math.random(-edgex, edgex)
					ay = math.random(-edgey, edgey)
					stage.warpShip("VectorShip", "", ax, ay, 3, "", 500)
					ship.bump(math.random(0,359), math.random(500, 3000))
				end
				
				--count up the spawn counter
				stage.vars.set("wave", stage.vars.get("wave") + 1)
			end
		
		--if all waves have spawned
		else
			--if there are no enemies left on the map
			if stage.factionCount(3) == 0 then
				stage.haltMusic()
				--ends the current stage
				stage.finish()
			end
		end
	end
	
	--boooo you are a failure
	if stage.failure == 1 then

	end
	
	
end