-- LUA Script - precede every function and global member with lowercase name of script + '_main'
--DESCRIPTION: smallg's fishing script - to work with fishing_bait.lua, fishing_rod.lua & fish.lua
--DESCRIPTION: [WATERHEIGHT=550] height of the shoreline is 
--DESCRIPTION: [FIGHTBITEDELAY=500] how long the player reels in the fish for after a successful prompt 
--DESCRIPTION: [FIGHTBITETIMER=2000] how long the player has to press the correct key when prompted 
--DESCRIPTION: [BITEDIRECTIONWIDTH=10] width of the button prompt icon 
--DESCRIPTION: [BITEDIRECTIONHEIGHT=20] height of the button prompt icon 
--DESCRIPTION: [RODXOFFSET=-10] adjusts where the rod is placed while fishing
--DESCRIPTION: [RODYOFFSET=0] adjusts where the rod is placed while fishing
--DESCRIPTION: [RODZOFFSET=0] adjusts where the rod is placed while fishing
--DESCRIPTION: [RODROTXOFFSET=-50] adjusts how the rod is placed while fishing
--DESCRIPTION: [RODROTYOFFSET=0] adjusts how the rod is placed while fishing
--DESCRIPTION: [RODROTZOFFSET=10] adjusts how the rod is placed while fishing
--DESCRIPTION: [CASTRODROTATESPEED=30] the rotation of the rod when casting 
--DESCRIPTION: [RODSPEED=8] how much the rod moves while reeling fish in 
--DESCRIPTION: [FLOATFLOATSPEED=50] how quickly the float comes back up after being cast 
--DESCRIPTION: [FLOATBOBSPEED=200] how quickly the float moves up and down while in the water 
--DESCRIPTION: [BITECHECKDELAY=1001] how quick to check for fish bites 
--DESCRIPTION: [RODICONX=80] x position of the rod icon
--DESCRIPTION: [RODICONY=1] y position of the rod icon
--DESCRIPTION: [RODICONWIDTH=8] width of the rod icon
--DESCRIPTION: [RODICONHEIGHT=10] height of the rod icon
--DESCRIPTION: [RODSELECTICONY=40] where the selection icons are on screen 
--DESCRIPTION: [RODSELECTICONWIDTH=10] how big the selection icons are on screen
--DESCRIPTION: [RODSELECTICONHEIGHT=15] how big the selection icons are on screen
--DESCRIPTION: bait selection uses same settings as rod selection 
--DESCRIPTION: [CASTBARSPEED=100] how quickly the cast bar fills up while choosing cast power 
--DESCRIPTION: [CASTBARX=30] where the cast bar is located on screen
--DESCRIPTION: [CASTBARY=45] where the cast bar is located on screen
--DESCRIPTION: [CASTBARWIDTH=40] how big the cast bar is on screen
--DESCRIPTION: [CASTBARHEIGHT=10] how big the cast bar is on screen
--DESCRIPTION: [CURSORWIDTH=3] how big the cursor is on screen
--DESCRIPTION: [CURSORHEIGHT=3] how big the cusor is on screen
--DESCRIPTION: Sounds: 1 = caught fish, 2 = fish got away

local catchprompt = "Yay, you caught a fish!"
local failedcatchprompt = "Oh no the fish got away.."
local prompttime = 2000
-- adjusts how much the rod wobbles while held
local rodwobbletimer = 1500
local rodwobblespeedx = 2
local rodwobblespeedy = 5
--how far away the fish can detect the bait from
local hookrange = 144000


U = U or require "scriptbank\\utillib"
canfish = 0
local state = ""
local cursorimg, cursorspr = nil, nil
local rodiconsimgs, baiticonsimgs = {}, {}
local rodiconssprs, baiticonssprs = {}, {}
local rodicons, rodE, baitE = {}, {}, {}
local timer = {}
local rodiconimg, baiticonimg, castbarbgimg, castbarimg = nil, nil, nil, nil, nil
local rodiconspr, baiticonspr, castbarbgspr, castbarspr, bitedirectionspr = nil, nil, nil, nil, nil
local bitedirectionupimg, bitedirectionleftimg, bitedirectiondownimg, bitedirectionrightimg = nil, nil, nil, nil
local maxrods = 0
local maxbaits = 0
local castpower = 0.1
local castdirection = 1
local startcastpower = 0
local bobdirection, bitedirection = 1, 0
local castdistance = 0
local hookedfishE = 0
local oldcx, oldcy = 0, 0
local cursormovedx, cursormovedy = 0, 0
local goodreel = 0
local bitechance, nobaitbitechance = 0, 1
local rodwobbledirection = 1
local newrodwobblex, newrodwobbley
local placedrod = 0
local fishbitedistance = 0




fishbaitE = nil 
fishrodE = nil 
presse, mclick, pressq = 0, 0, 0
g_fishing = {}
function fishing_properties(e, waterheight, fightbitedelay, fightbitetimer, bitedirectionwidth, bitedirectionheight, 
															rodxoffset, rodyoffset, rodzoffset, rodrotxoffset, rodrotyoffset, rodrotzoffset, 
															castrodrotatespeed, rodspeed, floatfloatspeed, floatbobspeed, bitecheckdelay,
															rodiconx, rodicony, rodiconwidth, rodiconheight,
															rodselecticony, rodselecticonwidth, rodselecticonheight,
															castbarspeed, castbarx, castbary, castbarwidth, castbarheight,
															cursorwidth, cursorheight)
	local f = g_fishing[e]
	f.waterheight = waterheight
	f.fightbitedelay = fightbitedelay
	f.fightbitetimer = fightbitetimer
	f.bitedirectionwidth = bitedirectionwidth
	f.bitedirectionheight = bitedirectionheight
	f.rodxoffset = rodxoffset
	f.rodyoffset = rodyoffset
	f.rodzoffset = rodzoffset
	f.rodrotxoffset = rodrotxoffset
	f.rodrotyoffset = rodrotyoffset
	f.rodrotzoffset = rodrotzoffset
	f.castrodrotatespeed = castrodrotatespeed
	f.rodspeed = rodspeed
	f.floatfloatspeed = floatfloatspeed
	f.floatbobspeed = floatbobspeed
	f.bitecheckdelay = bitecheckdelay
	f.rodiconx = rodiconx 
	f.rodicony = rodicony
	f.rodiconwidth = rodiconwidth
	f.rodiconheight = rodiconheight
	f.rodselecticony = rodselecticony
	f.rodselecticonwidth = rodselecticonwidth
	f.rodselecticonheight = rodselecticonheight
	f.castbarspeed = castbarspeed
	f.castbarx = castbarx 
	f.castbary = castbary 
	f.castbarwidth = castbarwidth
	f.castbarheight = castbarheight
	f.cursorwidth = cursorwidth
	f.cursorheight = cursorheight
	
end 

function fishing_init(e)
	g_fishing[e] = {}
	local f = g_fishing[e]
	f.waterheight = 510
	f.fightbitedelay = 500
	f.fightbitetimer = 2000
	f.bitedirectionwidth = 10
	f.bitedirectionheight = 20
	f.rodxoffset = -10
	f.rodyoffset = 0
	f.rodzoffset = 0
	f.rodrotxoffset = -50
	f.rodrotyoffset = 0
	f.rodrotzoffset = 10
	f.castrodrotatespeed = 30
	f.rodspeed = 8
	f.floatfloatspeed = 50
	f.floatbobspeed = 200
	f.bitecheckdelay = 1001
	f.rodiconx = 80
	f.rodicony = 1
	f.rodiconwidth = 8
	f.rodiconheight = 15
	f.rodselecticony = 30
	f.rodselecticonwidth = 10
	f.rodselecticonheight = 40
	f.castbarspeed = 100
	f.castbarx = 30
	f.castbary = 45
	f.castbarwidth = 40
	f.castbarheight = 10
	f.cursorwidth = 3
	f.cursorheight = 3
	
	timer[e] = {}
	Hide(e)
	state = "wait"
end 

function fishing_main(e)
	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 ShowSprite(id, x, y)
		PasteSpritePosition(id, x, y)
	end 
	local function MoveOffScreen(id)
		SetSpritePosition(id, 3000, 3000)
	end 
	local function GetDistance(e,v)
		local dx = g_Entity[e]['x'] - g_Entity[v]['x']
		local dy = g_Entity[e]['y'] - g_Entity[v]['y']
		local dz = g_Entity[e]['z'] - g_Entity[v]['z']
		local tdist = (dx^2)+(dy^2)+(dz^2)
		return tdist
	end 
	local function GetFlatDistance(e,v)
		local dx = g_Entity[e]['x'] - g_Entity[v]['x']
		local dz = g_Entity[e]['z'] - g_Entity[v]['z']
		local tdist = (dx^2)+(dz^2)
		return tdist
	end 
	local function GetPlayerFlatDistance(e)
		local dx = g_Entity[e]['x'] - g_PlayerPosX
		local dz = g_Entity[e]['z'] - g_PlayerPosZ
		local tdist = (dx^2)+(dz^2)
		return tdist
	end 
	local function AngleToPoint(e,x,z)
		if g_Entity[e] ~= nil and x > 0 and z > 0 then
			local destx = x - g_Entity[e]['x']
			local destz = z - g_Entity[e]['z']
			local angle = math.atan2(destx,destz)
			angle = angle * (180.0 / math.pi)
			--angle = angle + math.random(-1,1)
			if angle < 0 then
				angle = 360 + angle
			elseif angle > 360 then
				angle = angle - 360
			end
			return angle
		end
	end
	
	local f = g_fishing[e]
	
	if state == "wait" then 
		if canfish > 0 then 
			if g_PlayerPosY > f.waterheight then 
				Prompt("Press E to start fishing")
				if g_KeyPressE == 1 then 
					if presse == 0 then 
						presse = 1 
						CollisionOff(e)
						SetPosition(e, g_PlayerPosX, g_PlayerPosY + 100, g_PlayerPosZ)						
						state = "choose rod"
					end 
				else 
					presse = 0
				end 
			end
		end 
	elseif state == "choose rod" then 
		SetFreezePosition(g_PlayerPosX, g_PlayerPosY, g_PlayerPosZ)
		SetFreezeAngle(g_PlayerAngX, g_PlayerAngY, g_PlayerAngZ)
		TransportToFreezePosition()		
		FreezePlayer()		
		ActivateMouse()		
		local fp = "scriptbank\\smallg\\fishing\\images\\"
		local fn = "rod"
		local tempi = 0
		for a = 1, 9 do 
			local nfn = fp..fn..a..".png"
			if file_exists(nfn) == true then 
				for b,c in pairs (g_fishing_rod) do
					if g_fishing_rod[b].rodcollected == 2 then 
						if g_fishing_rod[b].rodimage == a then 
							tempi = tempi + 1
							rodiconsimgs[tempi] = LoadImage(nfn) 
							rodiconssprs[tempi] = CreateSprite(rodiconsimgs[tempi])
							SetSpriteSize(rodiconssprs[tempi], f.rodselecticonwidth, f.rodselecticonheight)
							MoveOffScreen(rodiconssprs[tempi])
							rodE[tempi] = b 
							break 
						end 
					end 
				end 
			end 
		end 
		maxrods = tempi
		if maxrods == 0 then
			state = "stop fishing"
			return 
		end 
		cursorimg = LoadImage(fp.."cursor.png")
		cursorspr = CreateSprite(cursorimg)
		SetSpriteSize(cursorspr, f.cursorwidth, f.cursorheight)
		MoveOffScreen(cursorspr)		
		state = "choosing rod" 
	elseif state == "choosing rod" then 		
		local cx, cy = g_MouseX, g_MouseY
		local posy = f.rodselecticony
		local posx = 50
		TextCenterOnX(posx, posy - 8, 5, "Choose Your Fishing Rod")
		TextCenterOnX(50, 80, 3, "[Q] Stop Fishing")
		if g_KeyPressQ == 1 then 
			if pressq == 0 then 
				pressq = 1 
				state = "stop fishing"
			end 
		else 
			pressq = 0
		end 
		posx = posx - ((maxrods/2)*f.rodselecticonwidth)
		posx = posx - (maxrods / 4)
		local rodselected = 0
		for a = 1, maxrods do 
			PasteSpritePosition(rodiconssprs[a], posx, posy)			
			posx = posx + f.rodselecticonwidth + 1
		end 
		PasteSpritePosition(cursorspr, cx, cy)
		if g_MouseClick == 1 then 
			if mclick == 0 then 
				posx = 50 
				posx = posx - ((maxrods/2)*f.rodselecticonwidth)
				posx = posx - (maxrods / 4)
				for a = 1, maxrods do 				
					if cy >= posy and cy <= posy + f.rodselecticonheight then 
						if cx >= posx and cx <= posx + f.rodselecticonwidth then 
							rodselected = a 
							break 
						end 
					end 
					posx = posx + f.rodselecticonwidth + 1
				end 
				mclick = 1
			end 
		else 
			mclick = 0
		end 
		if rodselected > 0 then 	
			fishrodE = nil 
			for a = 1, maxrods do 
				DeleteSprite(rodiconssprs[a])
				rodiconssprs[a] = nil
				if a ~= rodselected then 					
					DeleteImage(rodiconsimgs[a])
					rodiconsimgs[a] = nil					
				else 
					for b,c in pairs (rodE) do 
						if a == b then 
							fishrodE = c 
							break 
						end 
					end 
				end 
			end 
			rodiconimg = rodiconsimgs[rodselected]
			rodiconspr = CreateSprite(rodiconimg)
			SetSpriteSize(rodiconspr, f.rodiconwidth, f.rodiconheight)
			MoveOffScreen(rodiconspr)
			placedrod = 0
			state = "choose bait"
		end 
	elseif state == "choose bait" then 	
		if placedrod == 0 then 
			if fishrodE == nil then 
				fishrodE = e 
			end 
			CollisionOff(fishrodE)
			Show(fishrodE)
			local rad = math.rad
			local ox,oy,oz = U.Rotate3D(f.rodxoffset,f.rodyoffset,f.rodzoffset,rad(g_PlayerAngX),rad(g_PlayerAngY),rad(g_PlayerAngZ))
			SetPosition(fishrodE, g_PlayerPosX + ox, g_PlayerPosY + oy, g_PlayerPosZ + oz)
			SetRotation(fishrodE, g_PlayerAngX + f.rodrotxoffset, g_PlayerAngY + f.rodrotyoffset, g_PlayerAngZ + f.rodrotzoffset)		-- -50, 0, +10
			placedrod = 1
		end 
		local fp = "scriptbank\\smallg\\fishing\\images\\"
		local fn = "bait"
		local tempi = 0
		for a = 1, 9 do 
			local nfn = fp..fn..a..".png"
			if file_exists(nfn) == true then 
				for b,c in pairs (g_fishing_bait) do 
					if baittotal[g_fishing_bait[b]['baitimage']] > 0 then 
						if g_fishing_bait[b].baitimage == a then 
							tempi = tempi + 1
							baiticonsimgs[tempi] = LoadImage(nfn) 
							baiticonssprs[tempi] = CreateSprite(baiticonsimgs[tempi])
							SetSpriteSize(baiticonssprs[tempi], f.rodselecticonwidth, f.rodselecticonheight)
							MoveOffScreen(baiticonssprs[tempi])
							baitE[tempi] = b 
							break 
						end 
					end 
				end 
			end 
		end 
		maxbaits = tempi
		if maxbaits == 0 then 
			state = "stop fishing"
			return 
		end 
		state = "choosing bait"
	elseif state == "choosing bait" then 
		placedrod = 0
		local cx, cy = g_MouseX, g_MouseY
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		local posy = f.rodselecticony
		local posx = 50
		TextCenterOnX(posx, posy - 8, 5, "Choose Your Fishing Bait")
		TextCenterOnX(50, 80, 3, "[Q] Stop Fishing")
		if g_KeyPressQ == 1 then 
			if pressq == 0 then 
				pressq = 1 
				state = "stop fishing"
			end 
		else 
			pressq = 0
		end 
		posx = posx - ((maxbaits/2)*f.rodselecticonwidth)
		posx = posx - (maxbaits / 4)
		local baitselected = 0
		for a = 1, maxbaits do 
			PasteSpritePosition(baiticonssprs[a], posx, posy)			
			TextCenterOnX(posx + (f.rodselecticonwidth / 2), posy + f.rodselecticonheight + 1, 3, "x"..baittotal[g_fishing_bait[baitE[a]]['baitimage']])
			posx = posx + f.rodselecticonwidth + 1
		end 
		PasteSpritePosition(cursorspr, cx, cy)
		if g_MouseClick == 1 then 
			if mclick == 0 then 
				posx = 50 
				posx = posx - ((maxbaits/2)*f.rodselecticonwidth)
				posx = posx - (maxbaits / 4)
				for a = 1, maxbaits do 				
					if cy >= posy and cy <= posy + f.rodselecticonheight then 
						if cx >= posx and cx <= posx + f.rodselecticonwidth then 
							baitselected = a 
							break 
						end 
					end 
					posx = posx + f.rodselecticonwidth + 1
				end 
				mclick = 1
			end 
		else 
			mclick = 0
		end 
		if baitselected > 0 then 	
			fishbaitE = nil 
			for a = 1, maxbaits do 
				DeleteSprite(baiticonssprs[a])
				baiticonssprs[a] = nil 
				if a ~= baitselected then 					
					DeleteImage(baiticonsimgs[a])
					baiticonsimgs[a] = nil 
				else 
					for b,c in pairs (baitE) do 
						if a == b then 
							fishbaitE = c 							
							break 
						end 
					end 
				end 
			end 
			baiticonimg = baiticonsimgs[baitselected]
			baiticonspr = CreateSprite(baiticonimg)
			SetSpriteSize(baiticonspr, f.rodiconwidth, f.rodiconheight)
			MoveOffScreen(baiticonspr)
			state = "start cast"
		end 
	elseif state == "start cast" then 
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)
		local fp = "scriptbank\\smallg\\fishing\\images\\"
		castbarbgimg = LoadImage(fp.."castbarbg.png")
		castbarbgspr = CreateSprite(castbarbgimg)
		SetSpriteSize(castbarbgspr, f.castbarwidth, f.castbarheight)
		MoveOffScreen(castbarbgspr)
		castbarimg = LoadImage(fp.."castbar.png")
		castbarspr = CreateSprite(castbarimg)
		SetSpriteSize(castbarspr, f.castbarwidth, f.castbarheight)
		MoveOffScreen(castbarspr)
		castpower = 0.1
		if fishbaitE == nil then 
			fishbaitE = e 
			bitechance = nobaitbitechance
		else 
			bitechance = g_fishing_bait[fishbaitE]['catchchance']
		end 				
		CollisionOff(fishbaitE)
		SetPosition(fishbaitE, g_PlayerPosX, g_PlayerPosY, g_PlayerPosZ)
		SetRotation(fishbaitE, 0, g_PlayerAngY, 0)		
		Show(fishbaitE)
		state = "casting"
	elseif state == "casting" then 	
		TextCenterOnX(50, 80, 3, "[Q] Stop Fishing")
		if g_KeyPressQ == 1 then 
			if pressq == 0 then 
				pressq = 1 
				state = "stop fishing"
			end 
		else 
			pressq = 0
		end 
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)
		ShowSprite(castbarbgspr, f.castbarx, f.castbary)
		ShowSprite(castbarspr, f.castbarx, f.castbary)		
		castpower = castpower + (GetElapsedTime() * f.castbarspeed * castdirection)
		if castpower > 100 then 
			castpower = 100
			castdirection = -1
		elseif castpower < 0.1 then 
			castpower = 0.1
			castdirection = 1
		end 
		local sx = f.castbarwidth * (castpower / 100)
		SetSpriteSize(castbarspr, sx, f.castbarheight)
		if (g_KeyPressE == 1 and presse == 0) or (g_MouseClick == 1 and mclick == 0) then 
			mclick = 1
			presse = 1 
			startcastpower = castpower
			Show(fishbaitE)
			PlaySound(fishrodE,2)
			state = "cast line"
		else
			if g_KeyPressE == 0 then 
				presse = 0
			end 
			if g_MouseClick == 0 then 
				mclick = 0
			end 
		end 
	elseif state == "cast line" then 		
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)
		if castpower > 0.1 or g_Entity[fishbaitE]['y'] > f.waterheight - 3 then 
			RotateX(fishrodE, f.castrodrotatespeed)
			ShowSprite(castbarbgspr, f.castbarx, f.castbary)
			ShowSprite(castbarspr, f.castbarx, f.castbary)		
			castpower = castpower - (GetElapsedTime() * f.castbarspeed)
			if castpower < 0.1 then castpower = 0.1 end
			local sx = f.castbarwidth * (castpower / 100)
			SetSpriteSize(castbarspr, sx, f.castbarheight)
			local castspeed = g_fishing_rod[fishrodE]['castspeed']
			if castpower > startcastpower * 0.6 then 
				MoveUp(fishbaitE,castspeed * 1.5)
			else 
				if g_Entity[fishbaitE]['y'] > f.waterheight - 2 then 
					MoveUp(fishbaitE,-castspeed * 1.6)
				end 
			end 
			if castpower > 0.1 then 
				MoveForward(fishbaitE,castspeed * 3)
			end
		else 
			DeleteSprite(castbarbgspr)
			castbarbgspr = nil 
			DeleteSprite(castbarspr)
			castbarspr = nil 
			DeleteImage(castbarbgimg)
			castbarbgimg = nil 
			DeleteImage(castbarimg)
			castbarimg = nil 
			StopSound(fishrodE,2)
			TriggerWaterRipple(g_Entity[fishbaitE]['x'],g_Entity[fishbaitE]['y'],g_Entity[fishbaitE]['z'])
			state = "wait for float"
		end 
	elseif state == "wait for float" then 
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)		
		if g_Entity[fishbaitE]['y'] < f.waterheight - 2 then 
			MoveUp(fishbaitE,f.floatfloatspeed * 3)
		else 
			TriggerWaterRipple(g_Entity[fishbaitE]['x'],g_Entity[fishbaitE]['y'],g_Entity[fishbaitE]['z'])
			bobdirection = 1
			newrodwobblex = math.random(0,rodwobblespeedx)
			newrodwobbley = math.random(0,rodwobblespeedy)
			timer[e][3] = g_Time + rodwobbletimer
			timer[e][4] = g_Time + f.bitecheckdelay * 3
			state = "wait for bite"
		end 
	elseif state == "wait for bite" then 
		TextCenterOnX(50, 80, 3, "[Q] Stop Fishing")
		if g_KeyPressQ == 1 then 
			if pressq == 0 then 
				pressq = 1 
				state = "stop fishing"
			end 
		else 
			pressq = 0
		end 
		if g_Time > timer[e][3] then 
			timer[e][3] = g_Time + rodwobbletimer
			timer[e][3] = g_Time + rodwobbletimer
			newrodwobblex = math.random(0,rodwobblespeedx)
			newrodwobbley = math.random(0,rodwobblespeedy)
			rodwobbledirection = rodwobbledirection * -1
		end 
		RotateX(fishrodE, newrodwobblex * rodwobbledirection * 0.3)
		RotateY(fishrodE, newrodwobbley * rodwobbledirection * 0.3)
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)
		local bobspeed = GetElapsedTime() * f.floatbobspeed * bobdirection
		MoveUp(fishbaitE, bobspeed)
		if g_Entity[fishbaitE]['y'] > f.waterheight - 2 then 
			if bobdirection == 1 then 
				TriggerWaterRipple(g_Entity[fishbaitE]['x'],g_Entity[fishbaitE]['y'],g_Entity[fishbaitE]['z'])
				bobdirection = -1			
			end 
		elseif g_Entity[fishbaitE]['y'] < f.waterheight - 6 then 
			if bobdirection == -1 then 
				bobdirection = 1 
			end
		end 
		if g_Time > timer[e][4] then 
			timer[e][4] = g_Time + f.bitecheckdelay
			for a,b in pairs (isfish) do 			
				if GetFlatDistance(fishbaitE, b) < hookrange then 
					if math.random(1,100) <= bitechance then 
						fishstate[b] = "hooked"
						hookedfishE = b
						local fp = "scriptbank\\smallg\\fishing\\images\\"
						bitedirectionupimg = LoadImage(fp.."bitedirectionup.png")
						bitedirectiondownimg = LoadImage(fp.."bitedirectiondown.png")
						bitedirectionleftimg = LoadImage(fp.."bitedirectionleft.png")
						bitedirectionrightimg = LoadImage(fp.."bitedirectionright.png")
						bitedirectionspr = CreateSprite(bitedirectionupimg)
						SetSpriteSize(bitedirectionspr, f.bitedirectionwidth, f.bitedirectionheight)
						MoveOffScreen(bitedirectionspr)
						timer[e][1] = g_Time + f.fightbitedelay
						castdistance = GetPlayerFlatDistance(fishbaitE)
						fishbitedistance = GetFlatDistance(fishbaitE, hookedfishE)
						timer[e][5] = g_Time + 9000
						state = "biting"
						break 
					end
				end 
			end 
		end 
	elseif state == "biting" then
		--fishstate[hookedfishE] = "hooked"
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)				
		local dist = GetFlatDistance(fishbaitE, hookedfishE)
		if dist > fishbitedistance * 1.1 or g_Time > timer[e][5] then 
			--fish got away 
			state = "fish escaped"
		else 
			local x1,y1,z1 = GetEntityPosAng(fishbaitE)
			local x2, y2, z2 = GetEntityPosAng(hookedfishE)
			if dist > 80 or math.abs(y1-y2) > 20 then 					
				SetRotation(hookedfishE, 0, AngleToPoint(hookedfishE, x1, z1), 0)
				local reelingspeed = g_fishing_rod[fishrodE]['reelspeed']
				if y2 < y1 - 10 then 
					MoveUp(hookedfishE, reelingspeed * 0.95)
				elseif y2 > y1 - 5 then 
					MoveUp(hookedfishE, -reelingspeed * 0.95)
				end		
				local tdist = 1
				if dist < 500 then 
					 tdist = dist / 500 
				end 
				MoveForward(hookedfishE, reelingspeed * 1 * tdist)
			else 
				state = "bite"
			end 
		end 
	elseif state == "bite" then 
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)				
		local pdist = GetPlayerFlatDistance(fishbaitE)
		if pdist > 13000 then 			
			if g_Time > timer[e][1] then 
				local tempbd = 0
				repeat 
					tempbd = math.random(1,4)
				until tempbd ~= bitedirection
				bitedirection = tempbd
				if bitedirection == 1 then 
					SetSpriteImage(bitedirectionspr, bitedirectionupimg)
				elseif bitedirection == 2 then 
					SetSpriteImage(bitedirectionspr, bitedirectionrightimg)
				elseif bitedirection == 3 then 
					SetSpriteImage(bitedirectionspr, bitedirectiondownimg)
				else 
					SetSpriteImage(bitedirectionspr, bitedirectionleftimg)
				end 
				timer[e][2] = g_Time + f.fightbitetimer
				state = "fight bite"
			else 		
				CollisionOff(fishbaitE)
				CollisionOff(hookedfishE)
				local angleToPlayer = AngleToPoint(hookedfishE, g_PlayerPosX, g_PlayerPosZ)				
				local nangle = angleToPlayer
				local reelingspeed = g_fishing_rod[fishrodE]['reelspeed']
				if pdist < castdistance * 0.15 then 					
					--Prompt("moving up")
					SetRotation(hookedfishE, 0, nangle, 0)
					SetRotation(fishbaitE, 0, nangle, 0)
					MoveForward(fishbaitE, reelingspeed * 0.95 * goodreel)
					MoveForward(hookedfishE, reelingspeed * 0.95 * goodreel)
					MoveUp(fishbaitE, reelingspeed * 0.8 * goodreel)
					MoveUp(hookedfishE, reelingspeed * 0.8 * goodreel)
					SetRotation(hookedfishE, -90, g_Entity[hookedfishE]['angley'], 0)
					RotateX(fishrodE, -f.rodspeed * goodreel * 0.5)
				else 		
					TriggerWaterRipple(g_Entity[hookedfishE]['x'],g_Entity[hookedfishE]['y'],g_Entity[hookedfishE]['z'])
					if bitedirection == 1 then 
						if g_Entity[fishbaitE]['y'] < f.waterheight - 10 or goodreel == -1 then 
							MoveUp(fishbaitE, reelingspeed * 0.24 * goodreel)
							MoveUp(hookedfishE, reelingspeed * 0.24 * goodreel)							
						end
						RotateX(fishrodE, -f.rodspeed * goodreel)
					elseif bitedirection == 2 then 
						nangle = nangle - (20 * goodreel)
						RotateY(fishrodE, f.rodspeed * goodreel)
					elseif bitedirection == 3 then 
						if g_Entity[fishbaitE]['y'] < f.waterheight - 2 then 
							MoveUp(fishbaitE, reelingspeed * -0.24 * goodreel)
							MoveUp(hookedfishE, reelingspeed * -0.24 * goodreel)
						end
						RotateX(fishrodE, f.rodspeed * goodreel)
					else 
						nangle = nangle + (20 * goodreel)
						RotateY(fishrodE, -f.rodspeed * goodreel)
					end 
					SetRotation(hookedfishE, 0, nangle, 0)
					SetRotation(fishbaitE, 0, nangle, 0)
					MoveForward(fishbaitE, reelingspeed * goodreel)
					MoveForward(hookedfishE, reelingspeed * goodreel)
					if pdist > castdistance * 1.2 then --line broke cos too far away 
						state = "fish escaped"
					end 
				end 				
			end 
		else 
			--caught the fish!
			state = "caught fish"
		end 			
	elseif state == "fight bite" then 
		ShowSprite(rodiconspr, f.rodiconx, f.rodicony)
		ShowSprite(baiticonspr, f.rodiconx + f.rodiconwidth + 1, f.rodicony)
		goodreel = 0
		if bitedirection == 1 then --up				
			if g_KeyPressW == 1 then 
				goodreel = 1 
			elseif g_KeyPressA == 1 or g_KeyPressD == 1 or g_KeyPressS == 1 then 
				goodreel = -1
			end 
		elseif bitedirection == 2 then --right
			if g_KeyPressD == 1 then 
				goodreel = 1 
			elseif g_KeyPressA == 1 or g_KeyPressW == 1 or g_KeyPressS == 1 then 
				goodreel = -1
			end 
		elseif bitedirection == 3 then --down 
			if g_KeyPressS == 1 then 
				goodreel = 1 
			elseif g_KeyPressA == 1 or g_KeyPressD == 1 or g_KeyPressW == 1 then 
				goodreel = -1
			end 
		else --left 
			if g_KeyPressA == 1 then 
				goodreel = 1 
			elseif g_KeyPressW == 1 or g_KeyPressD == 1 or g_KeyPressS == 1 then 
				goodreel = -1
			end 
		end 
		ShowSprite(bitedirectionspr, 50 - (f.bitedirectionwidth / 2), 50 - (f.bitedirectionheight / 2))
		if goodreel ~= 0 then 
			timer[e][1] = g_Time + f.fightbitedelay			
			state = "bite"
		elseif g_Time > timer[e][2] then 
			--fish got away
			state = "fish escaped"
		end 
	elseif state == "caught fish" then 
		PromptDuration(catchprompt, prompttime)
		PlaySound(e,1)
		fishstate[hookedfishE] = "caught"
		Hide(fishbaitE)
		Destroy(hookedfishE)
		DeleteSprite(baiticonspr)
		baiticonspr = nil 
		DeleteImage(baiticonimg)
		baiticonimg = nil 
		baittotal[g_fishing_bait[fishbaitE]['baitimage']] = baittotal[g_fishing_bait[fishbaitE]['baitimage']] - 1
		state = "choose bait"
	elseif state == "fish escaped" then 
		baittotal[g_fishing_bait[fishbaitE]['baitimage']] = baittotal[g_fishing_bait[fishbaitE]['baitimage']] - 1
		PromptDuration(failedcatchprompt, prompttime)
		PlaySound(e,2)
		Hide(fishbaitE)
		fishstate[hookedfishE] = "got away"
		DeleteSprite(baiticonspr)
		baiticonspr = nil 
		DeleteImage(baiticonimg)
		baiticonimg = nil 
		state = "choose bait"
	elseif state == "stop fishing" then 		
		if cursorspr ~= nil then 
			DeleteSprite(cursorspr)
			cursorspr = nil 
		end 
		if cursorimg ~= nil then 
			DeleteImage(cursorimg)
			cursorimg = nil 
		end 
		for a = 1, maxrods do 
			if rodiconssprs[a] ~= nil then 
				DeleteSprite(rodiconssprs[a])
				rodiconssprs[a] = nil 
			end 
			if rodiconsimgs[a] ~= nil then 
				DeleteImage(rodiconsimgs[a])
				rodiconsimgs[a] = nil 
			end 
		end
		if maxbaits > 0 then 
			for a = 1, maxbaits do 
				if baiticonssprs[a] ~= nil then 
					DeleteSprite(baiticonssprs[a])
					baiticonssprs[a] = nil 
				end 
				if baiticonsimgs[a] ~= nil then 
					DeleteImage(baiticonsimgs[a])
					baiticonsimgs[a] = nil 
				end 
			end 
		end 
		if rodiconspr ~= nil then 
			DeleteSprite(rodiconspr)
			rodiconspr = nil 
		end 
		if rodiconimg ~= nil then 
			rodiconimg = nil 
		end 
		if baiticonspr ~= nil then 
			DeleteSprite(baiticonspr)
			baiticonspr = nil 
		end 
		if baiticonspr ~= nil then 
			baiticonspr = nil 
		end 
		if castbarbgspr ~= nil then 
			DeleteSprite(castbarbgspr)
			castbarbgspr = nil 
		end 
		if castbarbgimg ~= nil then 
			DeleteImage(castbarbgimg)
			castbarbgimg = nil 
		end 
		if castbarspr ~= nil then 
			DeleteSprite(castbarspr)
			castbarspr = nil 
		end 
		if castbarimg ~= nil then 
			DeleteImage(castbarimg)
			castbarimg = nil 
		end 
		SetPosition(fishbaitE, g_PlayerPosX, g_PlayerPosY + 3000, g_PlayerPosZ)
		Hide(fishbaitE)
		Hide(fishrodE)		
		DeactivateMouse()
		UnFreezePlayer()				
		TransportToFreezePosition()
		state = "wait"
	end 
	--Prompt(state)
end 