--LUA Script - precede every function and global member with lowercase name of script + '_main'
--DESCRIPTION: smallg's quick travel / teleport script 
--DESCRIPTION: [DESTINATIONS=2(1,9)] (how many exit portals are on the map)
--DESCRIPTION: [BASENAME$="teleport"] (i.e. if teleport1, teleport2 etc use "teleport")
--DESCRIPTION: [TEXTX=50] (destination list text position)
--DESCRIPTION: [TEXTY=20]
--DESCRIPTION: [SCALE=5(2,5)] (list size)
--DESCRIPTION: [TEXTGAP=5] (gap between the destinations)
--DESCRIPTION: [USEPROMPT$="use teleporter (E)"] (text to show when approaching the entity)
--DESCRIPTION: [USERANGE=120] (interaction range)
--DESCRIPTION: [USEANGLE=15]
--DESCRIPTION: [USEANYWHERE!=0] (can press Q to bring up the quick travel menu)
--DESCRIPTION: [OVERLAYX=0] (position and size of the images)
--DESCRIPTION: [OVERLAYY=0]
--DESCRIPTION: [OVERLAYWIDTH=100]
--DESCRIPTION: [OVERLAYHEIGHT=100]
--DESCRIPTION: [PANELWIDTH=1]
--DESCRIPTION: [PANELHEIGHT=3]
--DESCRIPTION: [CURSORWIDTH=3]
--DESCRIPTION: [CURSORHEIGHT=3]
--DESCRIPTION: [CLOSEX=90]
--DESCRIPTION: [CLOSEY=0]
--DESCRIPTION: [CLOSEWIDTH=5]
--DESCRIPTION: [CLOSEHEIGHT=5]
--DESCRIPTION: [ANIMATIONEND=19] (if no animation images set to 0 to skip the animation)
--DESCRIPTION: [ANIMATIONX=0]
--DESCRIPTION: [ANIMATIONY=0]
--DESCRIPTION: [ANIMATIONWIDTH=100]
--DESCRIPTION: [ANIMATIONHEIGHT=100]
--DESCRIPTION: [ANIMATIONTIME=2000]
--DESCRIPTION: Sounds: 0 = porting
U = U or require "scriptbank\\utillib"

g_quicktravel = {}
local mouse, presse, pressq, animationframe, porting =  0,0,0,0,0
local state, timer, portimg, destinationname, destinatione = {},{},{},{},{}
local overlayimg, panelleftimg, panelrightimg, hoverimg, cursorimg, closeimg
local overlayspr, panelleftspr, panelrightspr, hoverspr, cursorspr, portspr, closespr
function quicktravel_properties(e, destinations, basename, textx, texty, scale, textgap, useprompt, userange, useangle, useanywhere, 
																		overlayx, overlayy, overlaywidth, overlayheight, panelwidth, panelheight, cursorwidth, cursorheight, closex, closey, closewidth, closeheight, 
																		animationend, animationx, animationy, animationwidth, animationheight, animationtime)
	local qt = g_quicktravel[e]
	qt.destinations = destinations
	qt.basename = basename 
	qt.textx = textx
	qt.texty = texty
	qt.scale = scale
	qt.textgap = textgap
	qt.useprompt = useprompt
	qt.useangle = useangle
	qt.useanywhere = useanywhere
	qt.overlayx = overlayx
	qt.overlayy = overlayy
	qt.overlaywidth = overlaywidth
	qt.overlayheight = overlayheight
	qt.panelwidth = panelwidth
	qt.panelheight = panelheight
	qt.cursorwidth = cursorwidth
	qt.cursorheight = cursorheight
	qt.closex = closex
	qt.closey = closey
	qt.closewidth = closewidth
	qt.closeheight = closeheight
	qt.animationend = animationend
	qt.animationx = animationx
	qt.animationy = animationy
	qt.animationwidth = animationwidth
	qt.animationheight = animationheight
	qt.animationtime = animationtime
end 

function quicktravel_init(e)
	g_quicktravel[e] = {}
	local qt = g_quicktravel[e]
	qt.destinations = 1
	qt.basename = "teleport" 
	qt.textx = 50
	qt.texty = 20 
	qt.scale = 5
	qt.textgap = 5
	qt.useprompt = "Use teleporter? (E)"
	qt.userange = 120
	qt.useangle = 15
	qt.useanywhere = 0
	qt.overlayx = 0
	qt.overlayy = 0
	qt.overlaywidth = 100
	qt.overlayheight = 100
	qt.panelwidth = 1
	qt.panelheight = 3
	qt.cursorwidth = 3
	qt.cursorheight = 3
	qt.closex = 90
	qt.closey = 0
	qt.closewidth = 5
	qt.closeheight = 5
	qt.animationend = 19
	qt.animationx = 0
	qt.animationy = 0
	qt.animationwidth = 100
	qt.animationheight = 100
	qt.animationtime = 2000
	state[e] = "wait"
	timer[e] = {}
end 

function quicktravel_main(e)
	local function MoveOffScreen(v)
		SetSpritePosition(v, 2000, 2000)
	end 
	local function round(num, numDecimalPlaces)
		local mult = 10^(numDecimalPlaces or 0)
		return math.floor(num * mult + 0.5) / mult
	end	
	local function file_exists(name)
		 local f=io.open(name,"r")
		 if f~=nil then io.close(f) return true else return false end
	end
	local function CleanUp(e)
		local qt = g_quicktravel[e]
		DeleteImage(cursorimg)
		DeleteImage(overlayimg)
		DeleteImage(closeimg)
		DeleteImage(panelleftimg)
		DeleteImage(panelrightimg)
		DeleteImage(hoverimg)
		if qt.animationend > 0 then 
			for a = 0, qt.animationend do 
				if portimg[a] ~= nil then 
					DeleteImage(portimg[a])
				end 
			end 
			DeleteSprite(portspr)
			portspr = nil 
		end 
		DeleteSprite(overlayspr)
		DeleteSprite(cursorspr)
		DeleteSprite(closespr)
		DeleteSprite(panelleftspr)
		DeleteSprite(panelrightspr)
		DeleteSprite(hoverspr)	
	end 
	
	local qt = g_quicktravel[e]
	if state[e] == "wait" then 
		if U.PlayerLookingNear(e, qt.userange, qt.useangle) == true then 
			PromptLocal(e, qt.useprompt)
			if g_KeyPressE == 1 then 
				if presse == 0 then 
					presse = 1 
					state[e] = "opening"
				end 
			else 
				presse = 0
			end 
		elseif qt.useanywhere == 1 then 
			if g_KeyPressQ == 1 then 
				if pressq == 0 then 
					pressq = 1 
					state[e] = "opening"
				end 
			else 
				pressq = 0 
			end 
		end 
	elseif state[e] == "opening" then 
		local imagepath =  "scriptbank\\smallg\\images\\quicktravel\\"
		overlayimg = LoadImage(imagepath.."overlay.png")
		closeimg = LoadImage(imagepath.."exit.png")
		panelleftimg = LoadImage(imagepath.."left.png")
		panelrightimg = LoadImage(imagepath.."right.png")
		hoverimg = LoadImage(imagepath.."hover.png")
		cursorimg = LoadImage(imagepath.."cursor.png")
		if qt.animationend > 0 then 
			local temp = 0
			for a = 0, qt.animationend do 
				local fn = imagepath.."quicktravel_"
				if a < 10 then 
					fn = fn.."00"
				elseif a < 100 then 
					fn = fn.."0"
				end 
				fn = fn..a..".png"
				if file_exists(fn) == true then 
					portimg[a] = LoadImage(fn)
					temp = temp + 1
					if portspr == nil then 
						portspr = CreateSprite(portimg[a])
						SetSpriteSize(portspr, qt.animationwidth, qt.animationheight)
						MoveOffScreen(portspr)
					end 
				end 
			end 
		end 
		overlayspr = CreateSprite(overlayimg)
		SetSpriteSize(overlayspr, qt.overlaywidth, qt.overlayheight)
		MoveOffScreen(overlayspr)		
		closespr = CreateSprite(closeimg)
		SetSpriteSize(closespr, qt.closewidth, qt.closeheight)
		MoveOffScreen(closespr)
		panelleftspr = CreateSprite(panelleftimg)
		SetSpriteSize(panelleftspr, qt.panelwidth * qt.scale, qt.panelheight * qt.scale)
		MoveOffScreen(panelleftspr)
		panelrightspr = CreateSprite(panelrightimg)
		SetSpriteSize(panelrightspr, qt.panelwidth * qt.scale, qt.panelheight * qt.scale)
		MoveOffScreen(panelrightspr)
		hoverspr = CreateSprite(hoverimg)
		SetSpriteSize(hoverspr, qt.textsize, qt.textsize)
		MoveOffScreen(hoverspr)
		cursorspr = CreateSprite(cursorimg)
		SetSpriteSize(cursorspr, qt.cursorwidth, qt.cursorheight)
		MoveOffScreen(cursorspr)
		local name1 = qt.basename
		for a = 1, g_EntityElementMax do 
			if a ~= nil then 
				if g_Entity[a] ~= nil and a ~= e then 
					for b = 1, qt.destinations do 					
						local name2 = GetEntityName(a)
						local name3, name4 = "",""
						name3 = string.sub(name2, 1, string.len(name1)) --get name with no numbers
						name4 = string.sub(name2, 1, string.len(name1) + 1) --get name with destination number
						if name3 == name1 then 	
							if name1..b == name4 then 
								destinatione[b] = a
								destinationname[b] = string.sub(name2, string.len(name4) + 2, string.len(name2))
							end 
						end 
					end 
				end 
			end 
		end 
		SetFreezePosition(g_PlayerPosX, g_PlayerPosY, g_PlayerPosZ)
		SetFreezeAngle(g_PlayerAngX, g_PlayerAngY, g_PlayerAngZ)
		FreezePlayer()
		ActivateMouse()		
		state[e] = "open"
	elseif state[e] == "open" then 
		local mx, my = g_MouseX, g_MouseY
		porting = 0
		PasteSpritePosition(overlayspr, qt.overlayx, qt.overlayy)		
		local x1,x2,y1,y1,x3,y3
		y1 = qt.texty 		
		y2 = qt.texty + qt.scale
		local halfwidth
		local halfheight = qt.scale / 2 
		for a = 1, qt.destinations do 
			if destinatione[a] ~= nil then 		
				halfwidth = (string.len(destinationname[a]) * (qt.scale / 2.5)) / 2 
				halfheight = qt.scale / 2 
				x1 = qt.textx 
				y1 = qt.texty + (qt.textgap * (a-1))
				local offset = qt.scale - 2 - (qt.scale * 0.2)
				TextCenterOnX(x1, y1 - offset, qt.scale, destinationname[a])	
				x1 = x1 - halfwidth - 1 
				y1 = y1 - halfheight
				x2 = x1 + (halfwidth * 2) + 1.5
				y2 = y1 + (halfheight * 3)
				y3 = y1 - (((y2 - y1) +  (0.2 * qt.scale))/2)
				PasteSpritePosition(panelleftspr, x1 - (qt.panelwidth * qt.scale), y3)
				PasteSpritePosition(panelrightspr, x2, y3)
				y1 = y1 - qt.scale
				if mx >= x1 and mx <= x2 and my >= y1 and my <= y2 then 
					SetSpriteSize(hoverspr, x2 - x1, (qt.panelheight * qt.scale) - 3)
					PasteSpritePosition(hoverspr, x1, y3 + 1.5)
					if g_MouseClick == 1 then 
						if mouse == 0 then 
							mouse = 1
							porting = a 
						end 
					end 
				end 
				y1 = qt.texty
				y1 = y1 + (qt.textgap * a)
			end 
		end 
		--Prompt("destinations = "..#destinatione)		
		PasteSpritePosition(closespr, qt.closex, qt.closey)
		if g_MouseClick == 1 then 
			if mouse == 0 then 
				mouse = 1				
				x1 = qt.closex 
				x2 = x1 + qt.closewidth
				y1 = qt.closey
				y2 = y1 + qt.closeheight				
				if mx >= x1 and mx <= x2 and my >= y1 and my <= y2 then 
					state[e] = "closing"
				end 
			end 
		else
			mouse = 0
		end 
		if qt.useanywhere == 1 then 
			if g_KeyPressQ == 1 then 
				if pressq == 0 then 
					pressq = 1 
					state[e] = "closing"
				end 
			else 
				pressq = 0 
			end 
		end 
		PasteSpritePosition(cursorspr, mx, my)
		if porting > 0 then 
			state[e] = "porting"
			local etimer = GetTimer(e)
			timer[e][2] = etimer - 1
			if qt.animationend > 0 then 
				LoopSound(e,0)
				animationframe = -1
				--StartTimer(e)
				local etimer = GetTimer(e)
				timer[e][1] = etimer + 1
				timer[e][2] = etimer + qt.animationtime
				for a = 0, qt.animationend do 
					if portimg[a] ~= nil then 
						if a >= animationframe or animationframe == -1 then 
							animationframe = a 
							SetSpriteImage(portspr, portimg[animationframe])
							break
						end 
					end 
				end 
			else 
				PlaySound(e,0)
			end 
		end 
	elseif state[e] == "porting" then 
		local etimer = GetTimer(e)
		if etimer < timer[e][2] then 
			if etimer > timer[e][1] then 
				timer[e][1] = etimer + 1
				animationframe = animationframe + 1 
				if animationframe > qt.animationend then animationframe = 0 end 
				for a = 0, qt.animationend do 
					if portimg[a] ~= nil then 
						if a >= animationframe or animationframe == -1 then 
							animationframe = a 
							SetSpriteImage(portspr, portimg[animationframe])
							break
						end 
					end 
				end 
			end 
			PasteSpritePosition(portspr, qt.animationx, qt.animationy)
		else 
			if qt.animationend > 0 then 
				StopSound(e,0)
			end
			local port = g_Entity[destinatione[porting]]
			SetFreezePosition(port.x, port.y, port.z)
			SetFreezeAngle(port.anglex, port.angley, port.anglez)
			TransportToFreezePosition()
			state[e] = "closing"
		end 
	elseif state[e] == "closing" then 
		CleanUp(e)
		DeactivateMouse()
		UnFreezePlayer()
		state[e] = "wait"
	end 
end 