--called at the start of a game, makes the objects in question
function initHost()
	--start the spawn timer at the top of its countdown
	terrain.vars.init("spawntimer", terrain.vars.get("spawndelay"))
	
	terrain.callWithin = 0--skip calling the within function
	return 0
end

--called (by the host) every loop
function updateHost()

	--if we trigger off a waypoint
	if terrain.vars.get("trigger") == 1 then
		--get our group's # waypoint
		stage.waypoint_get(terrain.vars.get("group"))
	
		--if we're waiting to be activated
		if terrain.vars.get("state") == 0 then
			--and our corresponding waypoint has been turned on
			if waypoint.on == 1 then
				--activate ourselves
				terrain.vars.set("state", 1)
			end
		--if we're happily chugging out ships
		elseif terrain.vars.get("state") == 1 then
			--and our corresponding waypoint has been turned off
			if waypoint.on == 0 then
				--dectivate ourselves
				terrain.vars.set("state", 0)
			end
		end
	end
	
	--if we trigger off the countdown
	if terrain.vars.get("trigger") == 2 then
		--if we're waiting to be activated
		if terrain.vars.get("state") == 0 then
			--count down spawntimer
			if terrain.vars.get("spawntimer") > 0 then
				--stage.GFP("COUNTDOWN : " .. terrain.vars.get("spawntimer"))
				terrain.vars.set("spawntimer", terrain.vars.get("spawntimer") - 20)
			--activate ourselves
			elseif terrain.vars.get("spawntimer") <= 0 then
				stage.GFP("ACTIVATED countdown spawner")
				terrain.vars.set("state", 1)
			end
		end
	end
	
	
	--check to see if we've spawned the enough ships
	if terrain.vars.get("spawncount") >= terrain.vars.get("spawnnum") then
		--if we only spawn one wave and then call it quits:
		if terrain.vars.get("spawnmode") == 0 then
			--inactivate self (won't re-trigger)
			terrain.vars.set("state", -1)
			--reset spawn count, in case we reactivate
			terrain.vars.set("spawncount", 0)
			
		--if we spawn the ships EVERY spawndelay interval
		elseif terrain.vars.get("trigger") == 2 then
			--just reset countdown timer
			terrain.vars.set("spawntimer", terrain.vars.get("spawndelay"))
			--inactivate self (will re-trigger)
			terrain.vars.set("state", 0)
			--reset spawn count, for when we reactivate
			terrain.vars.set("spawncount", 0)
		end
	end
	
	--are we active?
	if terrain.vars.get("state") == 1 then
		respawnShips()
	end

	return 0
end

--called (by the host) when a ship is inside this terrain tile
function shipWithin()
	return 0
end

--checks to see if we need to spawn any ships & does so as needed
function respawnShips()

	--if we're currently under the amount of ships to spawn (plus one due to initial reference ship)
	--if terrain.trackCount < terrain.vars.get("spawnnum") + 1 then
	if terrain.vars.get("spawncount") < terrain.vars.get("spawnnum") then
	
		--count down spawntimer
		terrain.vars.set("spawntimer", terrain.vars.get("spawntimer") - 20)
	
		--if spawntimer has counted down
		if terrain.vars.get("spawntimer") <= 0 then
			if terrain.vars.get("spawnnum") > 1 then
				--randomize placement
				sx = terrain.x + math.random(0, terrain.wid)
				sy = terrain.y + math.random(0, terrain.het)
			else
				--place in center
				sx = terrain.centerx
				sy = terrain.centery
			end
			
			--get our reference ship
			terrain.getTrackedShip(0)
			--make a new ship
			stage.warpShip(ship.name, ship.config, sx, sy, ship.faction, ship.behavior, 1500)
			--track the newly-created ship
			--terrain.trackShip(ship)
			--count up on the number of spawned ships
			terrain.vars.set("spawncount", terrain.vars.get("spawncount") + 1)
			
			--reset the timer (countdown timer trigger spawns them rapidly)
			if terrain.vars.get("trigger") == 2 then
				terrain.vars.set("spawntimer", 500)
			else
				terrain.vars.set("spawntimer", terrain.vars.get("spawndelay"))
			end
		end
		
	end

end

--sets the editor/minimap display icon and color
function setIcon()
	icon.frame = 0	--from map_icons.png, which frame to use.
	icon.color = 0	--brown
	return 0
end