-- LUA Script - precede every function and global member with lowercase name of script + '_main' -- Door Prompts 'Closed' can be opened with entity collected specified by 'USE KEY' -- state to ensure user must release E key before can open/close again local U = require "scriptbank\\utillib" local rad = math.rad local doorDistance = 50 local doors = {} local door_pressed = 0 function clever_door_init( e ) Include( "utillib.lua" ) doors[ e ] = { entered = false } end local function calcDoorSide( obj ) local x, y, z, xa, ya, za = GetObjectPosAng( obj ) local osx, osy, osz = U.Rotate3D( 0, 0, -doorDistance, rad( xa ), rad( ya ), rad( za ) ) -- are we 'in front' of the door? if U.PlayerCloserThanPos( x + osx, y + osy, z + osz, doorDistance ) then return -doorDistance end osx, osy, osz = U.Rotate3D( 0, 0, doorDistance, rad( xa ), rad( ya ), rad( za ) ) -- are we 'behind' the door? if U.PlayerCloserThanPos( x + osx, y + osy, z + osz, doorDistance ) then return doorDistance end end function clever_door_main( e ) local door = doors[ e ] if door == nil then return end local Ent = g_Entity[ e ] if door.entered then PromptLocal( e, 'entered' ) elseif door.opened then PromptLocal( e, 'Opened but not entered yet' ) else PromptLocal( e, 'Still closed' ) end if door.opened and not door.entered then local x, y, z, xa, ya, za = GetObjectPosAng( Ent.obj ) local osx, osy, osz = U.Rotate3D( 0, 0, -doors[ e ].side, rad( xa ), rad( ya ), rad( za ) ) osx, osy, osz = x + osx, y + osy, z + osz if U.PlayerCloserThanPos( osx, osy, osz, doorDistance ) then door.entered = true end end if g_PlayerHealth > 0 and U.PlayerCloserThan( e, 100 ) then GetEntityPlayerVisibility( e ) if Ent.activated == 0 then if Ent.haskey == -1 then SetActivated( e, 1 ) else if Ent.plrvisible == 1 then if Ent.haskey == 1 then if GetGamePlayerStateXBOX() == 1 then Prompt( "The door is locked. Press Y button to unlock door" ) else Prompt( "The door is locked. Press E key to unlock door" ) end if g_KeyPressE == 1 then SetActivated( e, 1 ) end else Prompt( "The door is locked. Find a key to unlock door" ) end end end else if Ent.activated == 1 then -- door is unlocked and closed if Ent.plrvisible == 1 then if GetGamePlayerStateXBOX() == 1 then Prompt( "Press Y button to open door" ) else Prompt( "Press E to open door" ) end if g_KeyPressE == 1 and Ent.animating == 0 and door_pressed == 0 then SetAnimation( 0 ) PlayAnimation( e ) Ent.animating = 1 SetActivated( e, 2 ) ActivateIfUsed( e ) PlaySound( e, 0 ) StartTimer( e ) Ent.timer = g_Time door_pressed = 1 end end else if Ent.activated == 2 then -- door is open if Ent.plrvisible == 1 then if g_KeyPressE == 1 and Ent.animating == 0 and door_pressed == 0 then SetAnimation( 1 ) PlayAnimation( e ) Ent.animating = 1 SetActivated( e, 1 ) PlaySound( e, 1 ) CollisionOn( e ) door_pressed = 1 end end end end end end if Ent.activated == 2 then if door.side == nil then -- add code to detect 'side' of door we are on and store it door.side = calcDoorSide( Ent.obj ) end -- door collision off after 1 second if GetTimer( e ) > 1000 then CollisionOff( e ) door.opened = true end end if g_KeyPressE == 0 then door_pressed = 0 end end