-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Default script - does nothing.

local xpos = {}
local ypos = {}
local zpos = {}
local setpos = {}
local outOfBounds = {}
local started = {}
local range = {}
local tooClose = {}
local timer = {}
local timer2 = {}
local timer3 = {}
local timer4 = {}
local terrainHeight = {}
local waterHeight = {}
local moveAmount = {}
local moveAmount2 = {}
local xRotation = {}
local changeHeight = {}
local swimming = {}
local approaching = {}
local attacking = {}
local state = {}
local swimanim = {}
local aggressive = {}
local attacktype = {}
local animationcomplete = {}
local hitting = {}
local deathAnim = {}
local waterHeight = {}
local speed = {}
local checkHealth = {}
local healthSet = {}
local fixedHeight = {}
local goalx = {}
local goalz = {}

function whiteshark_init(e)
xpos[e] = 0
ypos[e] = 0
zpos[e] = 0
setpos[e] = 0
goalx[e] = 0
goalz[e] = 0
started[e] = 0
range[e] = 200
tooClose[e] = 0
timer[e] = 0
timer2[e] = 0
timer3[e] = 0
timer4[e] = 0
terrainHeight[e] = 0
waterHeight[e] = 0
moveAmount[e] = 0
moveAmount2[e] = 0
xRotation[e] = 0
changeHeight[e] = 0
swimming[e] = 1
approaching[e] = 0
attacking[e] = 0
state[e] = ""
swimanim[e] = 0
aggressive[e] = 0
attacktype[e] = 0
animationcomplete[e] = 0
hitting[e] = 0
speed[e] = 50
deathAnim[e] = 0
checkHealth[e] = g_Entity[e]['health'] + 2000
SetEntityHealth(e,g_Entity[e]['health'] + 2000)
healthSet[e] = 0
fixedHeight[e] = 0
end

function whiteshark_main(e)

if  g_PlayerHealth < 1 then
	whiteshark_init(e)
end

terrainHeight[e] = GetTerrainHeight(g_Entity[e]['x'],g_Entity[e]['z'])
waterHeight[e] = GetWaterHeight() - 100

if g_Entity[e]['health'] > 1000 then
	checkHealth[e] = g_Entity[e]['health']
end
	
if checkHealth[e] > 2000 then

if setpos[e] == 0 then
	xpos[e] = g_Entity[e]['x']
	zpos[e] = g_Entity[e]['z']
	ypos[e] = g_Entity[e]['y']
	setpos[e] = 1
end

if GetPlayerDistance(e) > 1000 and aggressive[e] == 0 then
	if state[e] ~= "relaxed" then
		g_Entity[e]['animating'] = 0
		state[e] ="relaxed"
	end
	swimming[e] = 1
	approaching[e] = 0
	attacking[e] = 0
end

if GetPlayerDistance(e) < 1000 and GetPlayerDistance(e) > 300 and animationcomplete[e] == 1 then
	if state[e] ~= "approaching" then
		g_Entity[e]['animating'] = 0
		state[e] ="approaching"
	end
	swimming[e] = 0
	approaching[e] = 1
	attacking[e] = 0
	aggressive[e] = 1
end
--and g_Entity[e]['y'] < (g_PlayerPosY - 14) and g_Entity[e]['y'] > (g_PlayerPosY -16)
if GetPlayerDistance(e) < 300  then
	if state[e] ~= "attacking" then
		g_Entity[e]['animating'] = 0
		state[e] ="attacking"
	end
	swimming[e] = 0
	approaching[e] = 0
	attacking[e] = 1
end

if GetPlayerDistance(e) > 2000 then
	aggressive[e] = 0
end

xRotation[e] = g_Entity[e]['anglex']


if aggressive[e] == 0 then
	if fixedHeight[e] == 0 then
		if g_Entity[e]['y'] - terrainHeight[e] < 0 then
			CollisionOff(e)
			MoveUp(e,20)
			CollisionOn(e)
			if g_Entity[e]['anglex'] >-10 then
				RotateX(e,-10)
			end
		end
	
		if g_Entity[e]['y'] > (waterHeight[e]) then
			CollisionOff(e)
			MoveUp(e,-20)
			CollisionOn(e)
			if g_Entity[e]['anglex'] < 10 then
				RotateX(e,10)
			end
		end
	end
	if fixedHeight[e] == 1 then 
		if g_Entity[e]['y'] < waterHeight[e] then
			CollisionOff(e)
			MoveUp(e,20)
			CollisionOn(e)
			
		elseif g_Entity[e]['y'] > waterHeight[e] then
			CollisionOff(e)
			MoveUp(e,-20)
			CollisionOn(e)
		end
	end
end
if whitesharkGoalDistance(e) < 100 then
	tooClose[e] = 1
	else
	tooClose[e] = 0
end

if GetTimer(e) - timer[e] > (10000 + math.random(-2000,2000)) or tooClose[e] == 1 then 
	goalx[e] = xpos[e] + math.random(-range[e],range[e])
	goalz[e] = zpos[e] + math.random(-range[e],range[e])
	timer[e] = GetTimer(e)
end

if swimming[e] == 1 then
	SetAnimationSpeed(e,1)
	if speed[e] < 50 then
		speed[e] = speed[e] + 1
	end
	RotateToPointSlowly(e,goalx[e],goalz[e],1)
	CollisionOff(e)
	MoveForward(e,speed[e])
	CollisionOn(e)
	if swimanim[e] == 0 then
		SetAnimationFrames(10,50)
		LoopAnimation(e)
		swimanim[e] = 1
	end
	
	if GetAnimationFrame(e) > 48 then
		animationcomplete[e] = 1
	end
	if GetAnimationFrame(e) < 12 then
		animationcomplete[e] = 0
	end

	if g_Entity[e]['y'] - terrainHeight[e] > 0 and g_Entity[e]['y'] < (waterHeight[e]) and fixedHeight[e] == 0 then
	
		if GetTimer(e) - timer3[e] > 5000 then
			if changeHeight[e] == 1 then
				changeHeight[e] = 0
			elseif 
			changeHeight[e] == 0 then
			changeHeight[e] = 1
			end
		moveAmount[e] = math.random(-10,10)
		timer3[e] = GetTimer(e)
		end
		if changeHeight[e] == 1 then
			CollisionOff(e)
			MoveUp(e,moveAmount[e])
			CollisionOn(e)
			if moveAmount[e] < -5 then
				if g_Entity[e]['anglex'] < 10 then
				RotateX(e,10)
				end
			end
			
			if moveAmount[e] > 5 then
				if g_Entity[e]['anglex'] >-10 then
				RotateX(e,-10)
				end
			end
			
			if moveAmount[e] < 5 and moveAmount[e] >-5 then
				if g_Entity[e]['anglex'] < -1 then
					RotateX(e,5)
				elseif g_Entity[e]['anglex'] > 1 then
					RotateX(e,-5)
				end
			end
			elseif changeHeight[e] == 0 then
			if g_Entity[e]['anglex'] < -1 then
					RotateX(e,5)
				elseif g_Entity[e]['anglex'] > 1 then
					RotateX(e,-5)
			end
			 
		end		
	end
end

if approaching[e] == 1 then
	SetAnimationSpeed(e,2)
	speed[e] = 100
	if g_Entity[e]['anglex'] > 1 then
		RotateX(e,-10)
	elseif g_Entity[e]['anglex'] < -1 then
		RotateX(e,10)
	end
	
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(10,50)
		LoopAnimation(e)
		g_Entity[e]['animating'] = 1
	end
	
	if g_Entity[e]['y'] < waterHeight[e] and g_Entity[e]['y'] > terrainHeight[e] then
		if g_Entity[e]['y'] - (g_PlayerPosY - 15) < -50 then
			CollisionOff(e)
			MoveUp(e,50)
			CollisionOn(e)
		end
		if g_Entity[e]['y'] - (g_PlayerPosY - 15) < 0 and  g_Entity[e]['y'] - (g_PlayerPosY - 15) > -50 then
			CollisionOff(e)
			MoveUp(e,10)
			CollisionOn(e)
		end
		
		if g_Entity[e]['y'] - (g_PlayerPosY - 15) > 50  then
			CollisionOff(e)
			MoveUp(e,-50)
			CollisionOn(e)
		end
		
		if g_Entity[e]['y'] - (g_PlayerPosY - 15) < 50 and g_Entity[e]['y'] - (g_PlayerPosY - 15) > 0 then
			CollisionOff(e)
			MoveUp(e,-10)
			CollisionOn(e)
		end
	end
	
	if g_Entity[e]['y'] > waterHeight[e] then
		CollisionOff(e)
		MoveUp(e,-30)
		CollisionOn(e)
	end
	
	if g_Entity[e]['y'] < terrainHeight[e] then
		CollisionOff(e)
		MoveUp(e,30)
		CollisionOn(e)
	end
	
	RotateToPlayerSlowly(e,5)
	CollisionOff(e)
	MoveForward(e,speed[e])
	CollisionOn(e)
end

if attacking[e] == 1 then
	SetAnimationSpeed(e,1)
	speed[e] = 100
	RotateToPlayerSlowly(e,8)
	if g_Entity[e]['y'] < waterHeight[e] then
		if g_Entity[e]['y'] < (g_PlayerPosY - 15) then
			CollisionOff(e)
			MoveUp(e,0)
			CollisionOn(e)
		end
	end
	
	if g_Entity[e]['y'] > terrainHeight[e] then
		if g_Entity[e]['y'] > (g_PlayerPosY -15)  then
			CollisionOff(e)
			MoveUp(e,-0)
			CollisionOn(e)
		end
	end
	
	if GetAnimationFrame(e) == 80  then
		hitting[e] = 1
	else
		hitting[e] = 0
	end	
	if hitting[e] == 1 and GetPlayerDistance(e) < 200 then
		HurtPlayer(e,20)
		PlaySound(e,0)
	end
	
	
	if GetPlayerDistance(e) > 200 then
		CollisionOff(e)
		MoveForward(e,100)
		CollisionOn(e)
	end
	
	if GetPlayerDistance(e) < 200 and GetPlayerDistance(e) >150 then
		CollisionOff(e)
		MoveForward(e,30)
		CollisionOn(e)
	end
				
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(55,105)
		LoopAnimation(e)
		g_Entity[e]['animating'] = 1
	end
		
	if GetAnimationFrame(e) == 55 then
		animationcomplete[e] = 0
	end
	if GetAnimationFrame(e) == 105 then
		animationcomplete[e] = 1
	end
end

if GetTimer(e) > timer2[e] + (5000 +math.random(-2000,2000))	 then
	moveAmount[e] = math.random(-10,10)
	timer2[e] = GetTimer(e)
end
end

if checkHealth[e] < 2001  then
	if g_Entity[e]['health'] < 1000 then
		SetEntityHealth(e,1000)
	end
	if g_Entity[e]['y']-terrainHeight[e] > 0 then
		CollisionOff(e)
		MoveUp(e,-30)
		CollisionOn(e)
	end
	if g_Entity[e]['anglex'] >1 then
		RotateX(e,-10)
	elseif g_Entity[e]['anglex'] < -1 then
		RotateX(e,10)
	end
		if deathAnim[e] ~= 1 then
			if g_Entity[e]['animating'] == 0 then
				SetAnimation(3)
				PlayAnimation(e)
				g_Entity[e]['animating'] = 1
			end
			if GetAnimationFrame(e) > 210 then
				deathAnim[e] =1
				StopAnimation(e)
			end
		--elseif deathAnim[e] == 1 then
		--	SetAnimationFrames(215,215)
		--	PlayAnimation(e)
		end

end

end


function whitesharkDistance(e)
  flockDX = (g_Entity[e]['x'] - xpos[e])
  flockDY = (g_Entity[e]['y'] - ypos[e])
  flockDZ = (g_Entity[e]['z'] - zpos[e])
  
  return math.sqrt(math.abs(flockDX*flockDX)+math.abs(flockDY*flockDY)+math.abs(flockDZ*flockDZ));
end 

function RotateToPointSlowly(e,x,z,v)
if g_Entity[e] ~= nil and x > 0 and z > 0 then
if v == nil then
v = 1 
end
local destx = x - g_Entity[e]['x']
local destz = z - g_Entity[e]['z']
local angleneeded = math.atan2(destx,destz)
angleneeded = angleneeded * (180.0 / math.pi)
if angleneeded < 0 then
angleneeded = 360 + angleneeded
elseif angleneeded > 360 then
angleneeded = angleneeded - 360
end
local current_angy = g_Entity[e]['angley']
while current_angy < 0 or current_angy > 360 do
if current_angy <= 0 then
current_angy = 360 + current_angy
elseif current_angy > 360 then
current_angy = current_angy - 360
end
end
local L = angleneeded - v
local R = angleneeded + v
if L <= 0 then
L = 360 + L 
elseif L > 360 then
L = L - 360
end
if R <= 0 then
R = 360 + R
elseif R > 360 then
R = R - 360
end
 
--if not already facing the target direction (+/- the rotation speed)
if current_angy < L or current_angy > R then
--do it in 2 halfs so we can account for 0/360 wrap
if current_angy > 180 then
if angleneeded > current_angy - 180 and angleneeded < current_angy then
current_angy = current_angy - v
else
current_angy = current_angy + v
end
else
if angleneeded < current_angy + 180 and angleneeded > current_angy then
current_angy = current_angy + v
else
current_angy = current_angy - v
end
end
SetRotation(e,xRotation[e],current_angy,0)
return current_angy
end
end
end

function whitesharkGoalDistance(e)
if g_Entity[e] ~= nil and g_Entity[e] ~= 0 then
local disx = g_Entity[e]['x'] - goalx[e]
local disz = g_Entity[e]['z'] - goalz[e]
return math.sqrt(disx^2 + disz^2)
end
end



