local rad   = math.rad
local deg   = math.deg 
local floor = math.floor
local atan  = math.atan2
local abs   = math.abs
local sqrt  = math.sqrt
local U = require "scriptbank\\utillib"
epressed = 0
local init_next_beep = 0
local nextbeep = 0
function wind_turbine_bomb_init(e)
	SetActivated(e,19)
end 

function wind_turbine_bomb_main(e)
	local function round(num, idp)
		local mult = 10^(idp or 0)
		return math.floor(num * mult + 0.5) / mult
	end
	--first we're just going to start in an 'activated' state somewhere outside GG's default range
	if g_Entity[e]['activated'] == 19 then 
		--check the player is looking at the bomb to show a prompt while collecting it
		if U.PlayerLookingNear(e,250,90) then
			PromptLocal(e,"Collect the wind turbine bomb? (e)")
			--collect the bomb by pressing E key
			if g_KeyPressE == 1 then 
				if epressed == 0 then 
					epressed = 1
					--hide it and turn collision off so we can store it for later use
					Hide(e)
					CollisionOff(e)
					--change state to activate the next part of the logic
					SetActivated(e,20)
					PlaySound(e,0)
				end 
			else 
				epressed = 0
			end 
		end 
	--in this state we place the bomb at the player's location at all times 
	--(a bit above his head to ensure it doesn't interfere with AI / getting shot etc)
	--and wait for the player to hold down the E key for a while to plant the bomb
	elseif g_Entity[e]['activated'] == 20 then 
		SetPosition(e,g_PlayerPosX,g_PlayerPosY+500,g_PlayerPosZ)
		if g_KeyPressE == 0 then 
			StartTimer(e)
			epressed = 0
		--planting the bomb 
		elseif epressed == 0 then 
			local timer = GetTimer(e)
			Prompt("Placing bomb "..round(1000-timer,1).."s")
			--check if the E key was held down long enough 
			if timer > 1000 then 
				--now we place the bomb on the map and show it again
				SetPosition(e,g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ)
				CollisionOn(e)
				Show(e)
				SetActivated(e,21)
				PlaySound(e,0)
				StartTimer(e)		
				init_next_beep = 1000
				nextbeep = init_next_beep
			end 
		end 
	--in this state we're going to wait for the timer to countdown til the bomb explodes
	elseif g_Entity[e]['activated'] == 21 then 
		local timer = GetTimer(e)
		Prompt("Bomb explodes in "..round(3000-timer,1).."s")
		if timer > nextbeep then 
			init_next_beep = init_next_beep * 0.68
			nextbeep = init_next_beep+timer
			PlaySound(e,1)
		end
		if timer > 3000 then 
			--this bit is a bit more advanced but basically we're just checking for any script that 
			--has isBride[] in it
			--if it does then we check it is nearby and store that value briefly as 'a'
			--we can then use 'a' to change things in that script 
			--in this case we tell the bridges that are nearby to change their activated state to 98
			local x1,y1,z1 = GetEntityPosAng(e)
			for a,b in pairs (isBridge) do
				local x2,y2,z2 = GetEntityPosAng(a)
				if U.CloserThan(x1,y1,z1,x2,y2,z2,900) then 
					SetActivated(a,98)
				end 
			end 
			--after we find all nearby bridges we go to the next state
			StartTimer(e)
			SetActivated(e,22)
		end 
	--this state is simply to allow the bridge to begin animating a little before the explosion takes place (to better sync the two)
	elseif g_Entity[e]['activated'] == 22 then 
		if GetTimer(e) > 60 then 
			SetEntityHealth(e,0)
			Destroy(e)
		end
	end 
end 