-- LUA Script - precede every function and global member with lowercase name of script + '_main' local U = require "scriptbank\\utillib" local rad = math.rad local bookcases = {} local function assignBookcasePart( e ) -- find another bookcase part that is close by local x1, y1, z1 = GetEntityPosAng( e ) for k, v in pairs( bookcases ) do if k ~= e and not v.used then local x2, y2, z2 = GetEntityPosAng( k ) if U.CloserThan( x1, y1, z1, x2, y2, z2, 20 ) then v.used = true v.otherSide = e bookcases[ e ].used = true bookcases[ e ].otherSide = k return end end end end local function moveBookcase( e, bc ) if bc.state == 'opening' then if U.CloserThan( bc.x, bc.y, bc.z, bc.ex, bc.ey, bc.ez, 0.1 ) then return false else bc.x, bc.y, bc.z = bc.x + bc.vx, bc.y + bc.vy, bc.z + bc.vz end elseif bc.state == 'closing' then if U.CloserThan( bc.x, bc.y, bc.z, bc.sx, bc.sy, bc.sz, 0.1 ) then return false else bc.x, bc.y, bc.z = bc.x - bc.vx, bc.y - bc.vy, bc.z - bc.vz end end CollisionOff( e ) ResetPosition( e , bc.x, bc.y, bc.z ) CollisionOn( e ) if BC_Update ~= nil then BC_Update( e, bc.x, bc.y, bc.z ) end return true end function new_bookcase_init_name( e, name ) Include( "utillib.lua" ) if name == "Oak Cabinet (Right Side)" then bookcases[ e ] = { side = "R", used = false, state = 'init' } elseif name == "Oak Cabinet (Left Side)" then bookcases[ e ] = { side = "L", used = false, state = 'init' } end end local keyPressed = false function new_bookcase_main( e ) local bc = bookcases[ e ] if bc == nil then PromptLocal( e, "Error!" ) return end if not bc.used then assignBookcasePart( e ) PromptLocal( e, "Assigning Bookcase" ) return end if bc.vx == nil then bc.x, bc.y, bc.z, ax, ay, az = GetEntityPosAng( e ) -- store 'closed' position bc.sx, bc.sy, bc.sz = bc.x, bc.y, bc.z local movInc = 0.1 -- number of units to move in a single frame local movTot = 30 -- total distance to move -- negate for LHS if bc.side == "L" then movInc = -movInc movTot = -movTot end bc.vx, bc.vy, bc.vz = U.Rotate3D( movInc, 0, 0, rad(ax), rad(ay), rad(az) ) -- calculate 'open' position local ex, ey, ez = U.Rotate3D( movTot, 0, 0, rad(ax), rad(ay), rad(az) ) bc.ex, bc.ey, bc.ez = bc.x + ex, bc.y + ey, bc.z + ez end --PromptLocal( e, bc.state ) if bc.state == 'init' then if BC_Register ~= nil then BC_Register( e, 'BC_Item' .. bc.side ) end bc.state = 'closed' -- RHS of bookcase controls the action elseif bc.state == 'closed' and bc.side == "R" then -- initial state with bookcase 'closed' if U.PlayerLookingNear( e, 150, 45 ) then Prompt( "Press E key to open" ) if g_KeyPressE == 1 then if not keyPressed then keyPressed = true bc.state = 'opening' bookcases[ bc.otherSide ].state = 'opening' end else keyPressed = false end end elseif bc.state == 'opening' then if not moveBookcase( e, bc ) then bc.state = 'open' end elseif bc.state == 'open' and bc.side == "R" then if U.PlayerLookingNear( e, 150, 45 ) then Prompt( "Press E key to close" ) if g_KeyPressE == 1 then if not keyPressed then keyPressed = true bc.state = 'closing' bookcases[ bc.otherSide ].state = 'closing' end else keyPressed = false end end elseif bc.state == 'closing' then if not moveBookcase( e, bc ) then bc.state = 'closed' end end end