-- LUA Script - precede every function and global member with lowercase name of script + '_main' local U = require "scriptbank\\utillib" local Q = require "scriptbank\\quatlib" local abs = math.abs local rad = math.rad local deg = math.deg local openingSpeed = 0.1 local offs = { x = 0, y = 20, z = 0 } local bridges ={} function bridge1_init_name( e, name ) Include( "utillib.lua" ) Include( "quatlib.lua" ) bridges[ e ] = { name = name, state = 'init' } end local function getGears( b ) for k, v in pairs( bridges ) do if v.name == 'bridge1_gears' and v.state == 'ready' and v.obj ~= nil then local x, y, z = GetObjectPosAng( v.obj ) if U.CloserThan( x, y, z, b.pos.x, b.pos.y, b.pos.z, 100 ) then v.state = 'done' return k end end end end local function positionBridge( e, b ) local rotquat = Q.FromEuler( rad( b.rotAng ), 0, 0 ) CollisionOff( e ) local aX, aY, aZ = Q.ToEuler( Q.Mul( b.quat, rotquat ) ) RotateObject( b.obj, deg( aX), deg( aY ), deg( aZ ) ) CollisionOn( e ) local g = bridges[ b.gears ] aX, aY, aZ = Q.ToEuler( Q.Mul( b.quat, Q.Conjugate( rotquat ) ) ) RotateObject( g.obj, deg( aX), deg( aY ), deg( aZ ) ) end function bridge1_main( e ) local b = bridges[ e ] if b == nil or b.state == 'done' then return end if b.state == 'init' then if b.name == 'bridge1_gears' then b.obj = g_Entity[ e ].obj b.state = 'ready' elseif b.name == 'bridge1_bed' then b.obj = g_Entity[ e ].obj local x, y, z, aX, aY, aZ = GetObjectPosAng( b.obj ) b.pos = { x = x, y = y, z = z } b.gears = getGears( b ) if b.gears == nil then PromptLocal( e, "Missing gears" ) return end b.quat = Q.FromEuler( rad( aX ), rad( aY ), rad( aZ ) ) aX, aY, aZ = Q.ToEuler( b.quat ) if abs( deg( aY ) - 90 ) < 0.001 then aY = rad( 89.999 ) elseif abs( deg( aY ) + 90 ) < 0.001 then aY = rad( -89.999 ) end b.quat = Q.FromEuler( aX, aY, aZ ) b.rotAng = 0 b.state = 'closed' end elseif b.state == 'closed' then if U.PlayerLookingAtObj( b.obj, 500 ) then Prompt( "E to open bridge" ) if g_KeyPressE == 1 then b.state = 'opening' PlaySound( e, 0 ) end end elseif b.state == 'opening' then b.rotAng = b.rotAng + openingSpeed if b.rotAng >= 90 then b.state = 'open' end positionBridge( e, b ) elseif b.state == 'open' then if U.PlayerLookingAtObj( b.obj, 500 ) then Prompt( "E to close bridge" ) if g_KeyPressE == 1 then b.state = 'closing' PlaySound( e, 1 ) end end elseif b.state == 'closing' then b.rotAng = b.rotAng - openingSpeed if b.rotAng <= 0 then b.state = 'closed' end positionBridge( e, b ) end end