--script by smallg
-- Modification par AlgoStep Company (DevCore35)
--further modification by Honkeyboy for spacecraft,speeders ect
-- Script V1.1

local state = {}
local use_range = {}
speed = {}
local max_speed = {}
local turn_rate = {}
local accleration = {}
local pressed = 0
-- ??? Bug stopcar and decelerationship and freinship... Idea ?
--local stopcar = audiobank\music\cars\stop.ogg

local cx = 0
local turning = 0
playership = 0
function flyspeederbike_init(e)

state[e] = "init"
speed[e] = 0
-- Adjust the speed of the vehicle here
max_speed[e] = 2000
turn_rate[e] =100
use_range[e] = 300
accleration[e] = 90
GravityOn(e)
playership = e 
CollisionOn(e)

end

function flyspeederbike_main(e)

if state[e] == "init" then
if GetPlayerDistance(e) <= use_range[e] then
Prompt("Press E to enter the Speeder")
--press enter/return to use car
if GetScancode() == 18 and pressed == 0 then
--FreezePlayer()
pressed = 1
LoopSound(e,0)
PromptDuration("W, S, A, D to Fly. X to brake.",5000)
state[e] = "start"
--SetRotation(e,0,g_PlayerAngY,0)
GravityOn(e)
end
end
elseif state[e] == "start" then
CollisionOff(e)
--FreezePlayer()
if GetPlayerDistance(e) < 300 then
outside = 0
end
if g_MouseClick > 0 then
if fired_gun[g_MouseClick] ~= nil then
if fired_gun[g_MouseClick] == 0 then
fired_gun[g_MouseClick] = 1 
end 
end 
end 

--[[
if fired_gun[1] ~= nil and fired_gun[2] ~= nil then
Prompt("Fired gun 1 = "..fired_gun[1].." / Fired gun 2 = "..fired_gun[2].."   "..g_MouseClick)
end
--]]
turning = 0
if g_KeyPressA == 1 then
turning = 1 
RotateY(e,-turn_rate[e])
--GravityOn(e)
elseif g_KeyPressD == 1 then
turning = 1 
RotateY(e, turn_rate[e])
--GravityOn(e)
end

-- stop ship test (V1.1) - X TOUCH
if GetScancode() == 45 and pressed == 0 then
speed[e] = 0
--PlaySound(e,)
--PlaySound(e,4)
end

if g_KeyPressS == 1 then
if speed[e] < (max_speed[e]/2) then
--if speed[e] < (max_speed[e]) then
speed[e] = speed[e] + accleration[e]
--GravityOn(e)
PlaySound(e,1)
end
elseif g_KeyPressW == 1 then
--if speed[e] > -(max_speed[e]/2) then
if speed[e] > -(max_speed[e]) then
speed[e] = speed[e] - accleration[e]
--GravityOn(e)
--PlaySound(e,decelerationship)
--PlaySound(e,2)
end

else
if speed[e] > -(max_speed[e] /10) and speed[e] < max_speed[e] /10 then
speed[e] = 0

if GetScancode() == 18 and pressed == 0 then
--UnFreezePlayer()
pressed = 1
StopSound(e,0)
StopSound(e,1)
--PlaySound(e,stopship)
--PlaySound(e,3)
CollisionOn(e)
state[e] = "init"
end

end
end
MoveForward(e, speed[e])
--UnFreezePlayer()
--SetFreezeAngle(g_PlayerAngX,g_PlayerAngY,g_PlayerAngZ)
--SetFreezePosition(g_Entity[e]['x'],g_Entity[e]['y']+60,g_Entity[e]['z'])
--TransportToFreezePosition()
if g_Entity[e]['y'] > 600 then 
MoveUp(e,-(g_Entity[e]['y']-600)*10)
end
TransportToIfUsed(e)
if cx > 20 then 
cx = 0
CollisionOn(e)
else 
cx = cx+1 
end 
if turning == 0 then 
SetRotation(e,0,g_Entity[e]['angley'],0)
end 

GravityOn(e)
--
--if pressed == 0 then
--if GetScancode() == 203 or GetScancode() == 205 then
--pressed = 1
--UnFreezePlayer()
--end
--end

end


if pressed == 1 and GetScancode() ~= 18 then
pressed = 0
end
end


----------------custom functions
function RotateToEntity(e,v)
if g_Entity[e] ~= nil and g_Entity[e] ~= 0 and g_Entity[v] ~= nil and g_Entity[v] ~= 0 then
local x = g_Entity[v]['x'] - g_Entity[e]['x']
local z = g_Entity[v]['z'] - g_Entity[e]['z']
local angle = math.atan2(x,z)
angle = angle * (180.0 / math.pi)
if angle < 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
SetRotation(e,0,angle,0)
return angle
end
end

function RotateToPoint(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)
if angle < 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
SetRotation(e,0,angle,0)
return angle
end
end

function GetDistance(e,v)
if g_Entity[e] ~= nil and g_Entity[e] ~= 0 and g_Entity[v] ~= nil and g_Entity[v] ~= 0 then
local disx = g_Entity[e]['x'] - g_Entity[v]['x']
local disz = g_Entity[e]['z'] - g_Entity[v]['z']
local disy = g_Entity[e]['y'] - g_Entity[v]['y']
return math.sqrt(disx^2 + disz^2 + disy^2)
end
end

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


function GetFlatDistance(e,v)
if g_Entity[e] ~= nil and g_Entity[v] ~= nil then
  local distDX = g_Entity[e]['x'] - g_Entity[v]['x']
  local distDZ = g_Entity[e]['z'] - g_Entity[v]['z']
  return math.sqrt((distDX*distDX)+(distDZ*distDZ));
end
end

function GetPlayerDistance(e)
  tPlayerDX = (g_Entity[e]['x'] - g_PlayerPosX)
  tPlayerDY = (g_Entity[e]['y'] - g_PlayerPosY)
  tPlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ)
  if math.abs(tPlayerDY) > 100 then 
   tPlayerDY = tPlayerDY * 4
  end
  return math.sqrt(math.abs(tPlayerDX*tPlayerDX)+math.abs(tPlayerDY*tPlayerDY)+math.abs(tPlayerDZ*tPlayerDZ));
end

function GetPlayerFlatDistance(e)
  tPlayerDX = (g_Entity[e]['x'] - g_PlayerPosX)
  tPlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ)
  return math.sqrt(math.abs(tPlayerDX*tPlayerDX)+math.abs(tPlayerDZ*tPlayerDZ));
end

function PlayerLooking(e,dis,v)
if g_Entity[e] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
if GetPlayerDistance(e) <= dis then
local destx = g_Entity[e]['x'] - g_PlayerPosX
local destz = g_Entity[e]['z'] - g_PlayerPosZ
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)

if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_PlayerAngY < 0 or g_PlayerAngY > 360 do
if g_PlayerAngY <= 0 then
g_PlayerAngY = 360 + g_PlayerAngY
elseif g_PlayerAngY > 360 then
g_PlayerAngY = g_PlayerAngY - 360
end
end
local L = angle - v
local R = angle + 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 (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then
return 1
elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end

function EntityLooking(e,t,dis,v)
if g_Entity[e] ~= nil and g_Entity[t] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
local destx = g_Entity[t]['x'] - g_Entity[e]['x']
local destz = g_Entity[t]['z'] - g_Entity[e]['z']
if math.sqrt((destx*destx)+(destz*destz)) <= dis then
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
   
if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_Entity[e]['angley'] < 0 or g_Entity[e]['angley'] > 360 do
if g_Entity[e]['angley'] <= 0 then
g_Entity[e]['angley'] = 360 + g_Entity[e]['angley']
elseif g_Entity[e]['angley'] > 360 then
g_Entity[e]['angley'] = g_Entity[e]['angley'] - 360
end
end
local L = angle - v
local R = angle + 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 (L < R and math.abs(g_Entity[e]['angley']) > L and math.abs(g_Entity[e]['angley']) < R) then
return 1
elseif (L > R and (math.abs(g_Entity[e]['angley']) > L or math.abs(g_Entity[e]['angley']) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end

function EntityLookingAtPlayer(e,dis,v)
if g_Entity[e] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
local destx = g_PlayerPosX - g_Entity[e]['x']
local destz = g_PlayerPosZ - g_Entity[e]['z']
if math.sqrt((destx*destx)+(destz*destz)) <= dis then
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
   
if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_Entity[e]['angley'] < 0 or g_Entity[e]['angley'] > 360 do
if g_Entity[e]['angley'] <= 0 then
g_Entity[e]['angley'] = 360 + g_Entity[e]['angley']
elseif g_Entity[e]['angley'] > 360 then
g_Entity[e]['angley'] = g_Entity[e]['angley'] - 360
end
end
local L = angle - v
local R = angle + 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 (L < R and math.abs(g_Entity[e]['angley']) > L and math.abs(g_Entity[e]['angley']) < R) then
return 1
elseif (L > R and (math.abs(g_Entity[e]['angley']) > L or math.abs(g_Entity[e]['angley']) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end