--called at the start of a game, makes the objects in question
function initHost()

	--create the gate foreground
	stage.createEmitter("gate", terrain.centerx, terrain.centery)
	emitter.upload(stage.newClient)
	
	--create the gate background
	stage.createEmitter("gate", terrain.centerx, terrain.centery)
	emitter.clearGFX()
	emitter.addGFX("gatebg.png")
	emitter.layer = 3
	emitter.upload(stage.newClient)

	--create the warp effect
	stage.createEmitter("gate_warp", 0, 0)
	emitter.active = 0--but it starts out inactivated
	emitter.upload(stage.newClient, terrain.vars.get("switchIDNUM"))

	--default to openable, accepts player entry
	terrain.vars.set("available", 1)
	terrain.vars.set("playercanexit", 1)
	
	--creates the gate edges
	for i = -1, 1, 2 do
		stage.createShip("GateEdge", "null", terrain.centerx + (170*i), terrain.centery-4, 0)
		ship.rot = 0
		ship.x = terrain.centerx + (170*i)--FORCE these dang things into the right place no matter what
		ship.y = terrain.centery-4
	end
	
	--create & track the switch/door
	stage.createShip("Gatedoor", "null", terrain.centerx, terrain.centery+6, 0, "switch")
	ship.rot = 0
	ship.x = terrain.centerx
	ship.y = terrain.centery+6
	terrain.trackShip(ship)
	
	--store the sprite IDNUM of the switch (in order to attach the warp effect)
	terrain.vars.set("switchIDNUM", ship.getIDNUM())
	terrain.vars.init("activated", 0)
	
	--give a hint how to get through
	terrain.vars.init("gatehint", 1)
	return 0
end


--called (by the host) every loop
function updateHost()
	--get the switch
	terrain.getTrackedShip(0)

	--if the gate is openable
	if terrain.vars.get("available") == 1 then
		--make the switch hittable
		ship.ignoreCollisions = 0
		
		--check to see of the switch is active
		--active = ship.controller.vars.get("active")
		active = 1
		
		--if we were JUST made available, make a blast of sparks
		--if ship.alpha == 0 then warpBlast(90) end
		
		--if the gate is on
		if active == 1 then
			ship.alpha = .25 + (math.random()/5)
			--ship.alpha = .5 + (math.random()/5)
			--if we JUST activated
			if terrain.vars.get("activated") == 0 then
				stage.sfx("game_gateon.wav", terrain.centerx, terrain.centery)
				setWarpEffect(1)
			end
			--use the boost to get through!
			if terrain.vars.get("gatehint") == 1 and terrain.vars.get("playercanexit") == 1 then
				if terrain.hasTimerTicked("boost", 2500) == 1 then
					stage.createText("Use the boost to get through!", terrain.centerx, terrain.centery - 80, 1800)
					stage.createText("[PRESS " .. stage.controlName("afterburn") .. "]", terrain.centerx, terrain.centery - 60, 1800)
				end
			end
		end
		
		--if the gate is off
		if active == 0 then
			ship.alpha = .2 + (math.random()/5)
			--if we JUST deactivated
			if terrain.vars.get("activated") == 1 then
				stage.sfx("game_gateoff.wav", terrain.centerx, terrain.centery)
				setWarpEffect(0)
			end
		end
	else
		ship.alpha = 0
		ship.ignoreCollisions = 1
	end
	return 0
end

--switches the warp effect on or off
function setWarpEffect(activated)
	--stage.createEmitter("gate_warp", terrain.centerx, terrain.centery)
	--emitter.active = activated
	--emitter.upload(0, terrain.vars.get("switchIDNUM"), 1)
	terrain.vars.set("activated", activated)--, -1)
end

--called (by the host) when a ship is inside this terrain tile
function shipWithin()
	--if ship.controller.vars.get("inert") == 0 then
	--if things are gateworthy
	if ship.controller.vars.get("gateworthy") == 1 then
		--if the gate is active
		if terrain.vars.get("activated") == 1 then
			--if hit the warp area
			if math.abs(ship.x - terrain.centerx) < 200 and math.abs(ship.y - terrain.centery) < 32 then
				--only afterburning stuff gets through (cryopods always are "afterburning")
				--if ship.afterburn == 1 then
					warpFX = 1
					if ship.behavior == "player" then
						if ship.afterburn == 1 and terrain.vars.get("playercanexit") == 1 then
							if ship.alpha < .5 then warpFX = 0 end--no effect if we're already exiting
							
							--"warps" the ship
							ship.ignoreCollisions = 1
							ship.alpha = 0
							--kills the trail
							ship.loseTrail()
							--sets the "destination" to this gate's waypoint
							stage.destination = stage.waypoint_at(terrain.coordx, terrain.coordy)
							--ends the current stage
							stage.finish()
							
							--tween the gate
							terrain.getTrackedShip(0)
							ship.hitTween = 1
						else
							warpFX = 0
						end
					else
						--add this ship to the stage's list of stuff that was warped
						stage.addToWarpedList(ship)
						--(give cryopods a bunch of points to drop)
						if ship.behavior == "cryopod" then
							ship.addPoints(30)
							warpFX = 0--the points are effect enough
							stage.createText(ship.name .. " retrieved!", terrain.centerx, terrain.centery - 80, 1800)
						end
						--the ship drops all of its points
						ship.dropItems()
						--"warps" the ship
						--stage.GFP(ship.name)
						ship.remove()
					end
					
					--warp blast effect
					if warpFX == 1 then
						--stage.GFP("EXITING MAP " .. ship.alpha)
						warpBlast(100)
						stage.sfx("game_warp.wav", terrain.centerx, terrain.centery)
					end
				--end
			end
		end
	end

	
	return 0
end

function warpBlast(num)
	stage.createEmitter("warpblast", ship.x, ship.y)
	emitter.num = num
	emitter.upload()
end

--called (by the host) when a ship is within 2 tiles from this terrain tile
function shipNearby()
	return 0
end

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