#include "script/wheels.lua"

function init()
	enabled = true
	if GetBool("savegame.mission.mall_intro") then
		enabled = false
		local loc = FindLocation("defaultplayer", true)
		SetPlayerSpawnTransform(GetLocationTransform(loc))
		Delete(FindBody("bus"))
		return
	end

	gateLocation = FindLocation("gate")
	gateTrigger = FindTrigger("gate")
	gateNearTrigger = FindTrigger("gatenear")
	gateDoneTrigger = FindTrigger("gatedone")
	gateNear = false
	gateDone = false

	doorLocation = FindLocation("door")
	doorTrigger = FindTrigger("door")
	doorRubbleTrigger = FindTrigger("rubble")
	doorDoneTrigger = FindTrigger("doordone")
	doorRubbleDone = false
	doorDone = false
	
	toolsLocation = FindLocation("tools")
	toolsTrigger = FindTrigger("tools")
	toolsDone = false

	fuseLocation = FindLocation("fuse")
	fuseOutsideTrigger = FindTrigger("fuseoutside")
	fuseInsideTrigger = FindTrigger("fuseinside")
	fuseLight = FindShape("fuselight")
	fuseBody = FindBody("fuse")
	fuseDone = false

	terminalLocation = FindLocation("terminal")
	terminalTrigger = FindTrigger("terminal")
	terminalDone = false
	
	powerOn = LoadSound("LEVEL/script/tutorial/poweron.ogg")
	arrow = LoadSprite("gfx/arrowdown.png")
	
	--Turn off tv and terminal and make them non-interactive until power is on
	tv = FindScreen("tv", true)
	terminal = FindScreen("terminal", true)
	SetScreenEnabled(tv, false)
	SetScreenEnabled(terminal, false)
	RemoveTag(GetShapeBody(GetScreenShape(tv)), "interact")
	RemoveTag(GetShapeBody(GetScreenShape(terminal)), "interact")
	
	--Turn off all the lights
	lights = FindShapes("light", true)
	for i=1, #lights do
		SetShapeEmissiveScale(lights[i], 0)
	end
	
	--Disable all tools
	local tools = ListKeys("game.tool")
	for i=1,#tools do
		local k = "game.tool."..tools[i]..".enabled"
		if GetBool(k) then
			SetBool(k, false)
		end
	end
	SetString("game.player.tool", "none")	
	
	--gateDone = true
	--doorDone = true
	--toolsDone = true
	
	bus = FindBody("bus")
	busVel = Vec(0,0,0)
	busTransform = GetBodyTransform(bus)
	busSound = LoadSound("LEVEL/script/tutorial/bus.ogg")
	busActive = false
	
	wheelInit(bus)
end


function drawArrow(location)
	local t = GetLocationTransform(location)
	t.rot = QuatLookAt(t.pos, GetCameraTransform().pos)
	local offset = 1.5 + math.sin(4.0*GetTime())*0.5
	t.pos[2] = t.pos[2] + offset
	DrawSprite(arrow, t, 2, 2, .3, .3, .3, 3, false, true)
	DrawSprite(arrow, t, 2, 2, 1, 1, 1, 1, true, true)
end


function tick(dt)
	if not enabled then
		return
	end

	if GetTime() > 2.5 then
		if not busActive then
			PlaySound(busSound)
			busActive = true
		end
		local fwd = TransformToParentVec(busTransform, Vec(0, 0, 1))
		busVel = VecAdd(busVel, VecScale(fwd, dt*2.0))
		busTransform.pos = VecAdd(busTransform.pos, VecScale(busVel, dt))
		wheelUpdate(-VecLength(busVel))
		SetBodyTransform(bus, busTransform)
	end

	local curinput = GetString("game.input.curdevice")
	local p = GetPlayerPos()
	if not gateDone then
		if IsPointInTrigger(gateTrigger, p) then
			if curinput == "mouse" then
				SetString("hud.hintimage", "ui/hud/tutorial/opendoor.png")
				SetString("hud.hintimage.input", "")
			else
				SetString("hud.hintimage", "ui/hud/tutorial/opendoor_empty.png")
				local str = string.upper(GetString("game.input.grab.icon"))
				if str ~= "" then
					SetString("hud.hintimage.input", str)
					SetInt("hud.hintimage.offsetx", 212)
					SetInt("hud.hintimage.offsety", 120)
				end
			end
		else
			if not gateNear and IsPointInTrigger(gateNearTrigger, p) then
				gateNear = true
			end
			if gateNear then
				SetString("hud.hintinfo", "Ïöîôòí åòôòöã")
				drawArrow(gateLocation)
			else
				SetString("hud.hintinfo", "Éèì èòðòí")
			end
		end
		if IsPointInTrigger(gateDoneTrigger, p) then
			gateDone = true
		end
	elseif not doorDone then
		if IsPointInTrigger(doorTrigger, p) then
			if not doorRubbleDone and IsTriggerEmpty(doorRubbleTrigger) then
				doorRubbleDone = true
			end
			if doorRubbleDone then
				if curinput == "mouse" then
					SetString("hud.hintimage", "ui/hud/tutorial/opendoor.png")
					SetString("hud.hintimage.input", "")
				else
					SetString("hud.hintimage", "ui/hud/tutorial/opendoor_empty.png")
					local str = string.upper(GetString("game.input.grab.icon"))
					if str ~= "" then
						SetString("hud.hintimage.input", str)
						SetInt("hud.hintimage.offsetx", 212)
						SetInt("hud.hintimage.offsety", 120)
					end
				end
			else
				if curinput == "mouse" then
					SetString("hud.hintimage", "ui/hud/tutorial/hold.png")
					SetString("hud.hintimage.input", "")
				else
					SetString("hud.hintimage", "ui/hud/tutorial/hold_empty.png")
					local str = string.upper(GetString("game.input.grab.icon"))
					if str ~= "" then
						SetString("hud.hintimage.input", str)
						SetInt("hud.hintimage.offsetx", 154)
						SetInt("hud.hintimage.offsety", 120)
					end
				end
			end
		else
			SetString("hud.hintinfo", "Èãûòèì åñùöô¼")
			drawArrow(doorLocation)
		end
		if IsPointInTrigger(doorDoneTrigger, p) then
			doorDone = true
		end
	elseif not toolsDone then
		if not IsPointInTrigger(toolsTrigger, p) then
			SetString("hud.hintinfo", "Ðòèäéôì ìñõöôùðéñöº")
			drawArrow(toolsLocation)
		end
		if GetBool("game.tool.sledge.enabled") and GetBool("game.tool.spraycan.enabled") and GetBool("game.tool.extinguisher.enabled") then
			toolsDone = true
		end
	elseif not fuseDone then
		SetTag(fuseBody, "interact", "Èãðéñìö¼ óôéèòûôãñìöéï¼")
		if IsPointInTrigger(fuseOutsideTrigger, p) then
			if curinput == "mouse" then
				SetString("hud.hintimage", "ui/hud/tutorial/usetool.png")
				SetString("hud.hintimage.input", "")
			else
				SetString("hud.hintimage", "ui/hud/tutorial/usetool_empty.png")
				local str = string.upper(GetString("game.input.usetool.icon"))
				if str ~= "" then
					SetString("hud.hintimage.input", str)
					SetInt("hud.hintimage.offsetx", 154)
					SetInt("hud.hintimage.offsety", 120)
				end
			end
		elseif IsPointInTrigger(fuseInsideTrigger, p) then
			if curinput == "mouse" then
				SetString("hud.hintimage", "ui/hud/tutorial/flashlight.png")
				SetString("hud.hintimage.input", "")
			else
				SetString("hud.hintimage", "ui/hud/tutorial/flashlight_empty.png")
				local str = string.upper(GetString("game.input.flashlight.icon"))
				if str ~= "" then
					SetString("hud.hintimage.input", str)
					SetInt("hud.hintimage.offsetx", 75)
					SetInt("hud.hintimage.offsety", 121)
				end
			end
		else
			SetString("hud.hintinfo", "Èãðéñì óôéèòûôãñìöéï¼, ýöòäº åîï¾ýìö¼ óìöãñìé")
			drawArrow(fuseLocation)
		end
		if GetPlayerInteractBody() == fuseBody and InputPressed("interact") then
			PlaySound(powerOn)
			RemoveTag(fuseBody, "interact")
			fuseDone = true
			SetShapeEmissiveScale(fuseLight, 0)
			for i=1, #lights do
				SetShapeEmissiveScale(lights[i], 1)
			end
			SetScreenEnabled(tv, true)
			SetScreenEnabled(terminal, true)
			SetTag(GetShapeBody(GetScreenShape(tv)), "interact")
			SetTag(GetShapeBody(GetScreenShape(terminal)), "interact")
		end
	elseif not terminalDone then
		if not IsPointInTrigger(fuseInsideTrigger, p) then
			if not IsPointInTrigger(terminalTrigger, p) then
				SetString("hud.hintinfo", "Èãíèì åñùöô¼ ì óôòåéô¼ õòòäÿéñì¿")
				drawArrow(terminalLocation)
			end
			if GetBool("game.player.usescreen") then
				terminalDone = true
			end
		end
	end
end
