-- LUA Script - precede every function and global member with lowercase name of script + '_main' local Q = require "scriptbank\\quatlib" local U = require "scriptbank\\utillib" local P = require "scriptbank\\physlib" local rad = math.rad local deg = math.deg local atan = math.atan2 local modf = math.modf local abs = math.abs local mouse_spr = mouse_spr or CreateSprite( LoadImage( "scriptbank\\images\\mouse_cursor.png" )) SetSpritePosition( mouse_spr, 200, 200 ) function aircraft_init( e ) Include( "utillib.lua" ) Include( "quatlib.lua" ) Include( "physlib.lua" ) SetSpriteSize( mouse_spr, 2.5 ) SetSpriteOffset( mouse_spr, 1.25, 2.1 ) SetSpritePosition( mouse_spr, 200, 200 ) end local aircraft = {} local controlEnt = nil local timeLastFrame = nil local timeDiff = 0 local maxSpeed = 20 local playerPos1 = { xo = 0, yo = 18, zo = -12 } local playerPos2 = { xo = 0, yo = 30, zo = -55 } local function AttachPlayer( h, xa, ya, za ) local xo, yo, zo = U.Rotate3D( h.pp.xo, h.pp.yo, h.pp.zo, xa, ya, za ) cx, cy, cz = h.pos.x + xo, h.pos.y + yo, h.pos.z + zo SetCameraPosition ( 0, cx, cy, cz ) SetCameraAngle ( 0, deg( xa ) + 15, deg( ya ), deg( za ) ) SetFreezePosition( h.pos.x, h.pos.y + 80, h.pos.z - 80 ) TransportToFreezePositionOnly() end local function posObject( h ) local xA, yA, zA = Q.ToEuler( h.quat ) local lift = h.vec.z / 4 local vx, vy, vz = U.Rotate3D( 0, h.vec.y + lift, h.vec.z, xA, yA, zA ) h.pos.x, h.pos.y, h.pos.z = h.pos.x + vx, h.pos.y + vy, h.pos.z + vz CollisionOff( h.ent ) PositionObject( h.obj, h.pos.x, h.pos.y, h.pos.z ) RotateObject( h.obj, deg( xA ), deg( yA ), deg( zA ) ) CollisionOn( h.ent ) AttachPlayer( h, xA, yA, zA ) end local EPressed = false function aircraft_main( e ) local h = aircraft[ e ] if h == nil then local Ent = g_Entity[ e ] local x, y, z, xa, ya, za = GetObjectPosAng( Ent.obj ) aircraft[ e ] = { ent = e, state = 'init', timer = math.huge, obj = Ent.obj, pos = { x = x, y = y, z = z }, quat = Q.FromEuler( rad( xa ), rad( ya ), rad( za ) ), vec = { x = 0, y = -1, z = 0 }, height = 0, pp = playerPos2 } return end local timeNow = g_Time if h.state == 'init' then h.state = 'start' h.timer = timeNow + 1000 end if controlEnt == nil then controlEnt = e end if controlEnt == e then if timeLastFrame == nil then timeLastFrame = timeNow timeDiff = 1 else timeDiff = ( timeNow - timeLastFrame ) / 20 timeLastFrame = timeNow end end if h.state == 'start' then if h.inCab then if g_KeyPressE == 1 then if not EPressed then EPressed = true SetCameraOverride( 0 ) DeactivateMouse() SetSpritePosition( mouse_spr, 200, 200 ) SetPlayerWeapons( 1 ) h.inCab = false h.height = 0 h.yaw = 0 StopSound( e, 0 ) return end else EPressed = false end if g_KeyPressR == 1 then if not RPressed then RPressed = true if h.pp == playerPos1 then h.pp = playerPos2 else h.pp = playerPos1 end end else RPressed = false end if g_KeyPressW == 1 then PlaySoundIfSilent( e, 1 ) if h.vec.z < maxSpeed then h.vec.z = h.vec.z + 0.02 * timeDiff end elseif g_KeyPressS == 1 then if h.vec.z > 0.02 then h.vec.z = h.vec.z - 0.02 * timeDiff else h.vec.z = 0 StopSound( e, 1 ) end end local omx, omy = ( g_MouseX - 50 ) / 50, ( g_MouseY - 50 ) / 50 if abs( omx ) < 0.05 then omx = 0 end if abs( omy ) < 0.05 then omy = 0 end SetSpritePosition( mouse_spr, g_MouseX, g_MouseY ) Prompt( h.vec.z ) --Prompt( omy .. ", " .. omx ) local pitchQ = Q.FromEuler( -rad( omy ) * timeDiff, 0, 0 ) local rollQ = Q.FromEuler( 0, 0, -rad( omx ) * timeDiff ) h.quat = Q.Mul( h.quat, pitchQ ) h.quat = Q.Mul( h.quat, rollQ ) if g_KeyPressA == 1 then if h.yaw < 1 then h.yaw = h.yaw + 0.01 * timeDiff end elseif g_KeyPressD == 1 then if h.yaw > -1 then h.yaw = h.yaw - 0.01 * timeDiff end else if h.yaw > 0.01 then h.yaw = h.yaw - 0.01 * timeDiff elseif h.yaw < -0.01 then h.yaw = h.yaw + 0.01 * timeDiff else h.yaw = 0 end end if h.yaw ~= 0 then h.quat = Q.Mul( h.quat, Q.FromEuler( 0, -rad( h.yaw ) * timeDiff, 0 ) ) end local terrain = GetTerrainHeight( h.pos.x, h.pos.z ) if h.pos.y > terrain then h.vec.y = -1 else h.vec.y = 0 end posObject( h ) --CheckForCollision( h.obj ) else if U.PlayerLookingNear( e, 200, 180 ) then Prompt( "E to fly me" ) if g_KeyPressE == 1 then if not EPressed then PositionMouse( GetDeviceWidth() / 2, GetDeviceHeight() / 2 ) ActivateMouse() LoopNon3DSound( e, 0 ) EPressed = true SetPlayerWeapons( 0 ) SetCameraOverride( 3 ) h.inCab = true h.yaw = 0 return end else EPressed = false end end end end end