-- 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' local U = require "scriptbank\\utillib" local rad = math.rad local doorDistance = 100 local checkDelay = 100 local doors = {} local eKeyPressed = false function clever_door_init_name( e, name ) Include( "utillib.lua" ) doors[ e ] = { name = name, state = 'initialise', timer = 0 } SetAnimationSpeed( e, 2 + math.random() ) 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 * 1.42 ) 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 * 1.42 ) then return -doorDistance end end function doorEntered( name ) for _, door in pairs( doors ) do if door.name == name then return ( door.state == 'closed but has been entered' or door.state == 'open and entered' ) end end end local function crossedOver( obj, side ) local x, y, z, xa, ya, za = GetObjectPosAng( obj ) local osx, osy, osz = U.Rotate3D( 0, 0, side, rad( xa ), rad( ya ), rad( za ) ) osx, osy, osz = x + osx, y + osy, z + osz return U.PlayerCloserThanPos( osx, osy, osz, doorDistance ) end function clever_door_main( e ) local door = doors[ e ] if door == nil then return end local timeNow = g_Time if timeNow < door.timer then return end door.timer = timeNow + checkDelay local Ent = g_Entity[ e ] PromptLocal( e, door.name .. ", " .. door.state .. ", " .. ( door.count or 'nil' ) ) if g_PlayerHealth > 0 and U.PlayerCloserThan( e, doorDistance ) then if door.state == 'initialise' then if Ent.haskey == -1 then door.state = 'closed and not entered' else door.state = 'locked' end CollisionOn( e ) door.collOn = true elseif door.state == 'locked' then if U.PlayerLookingAtObj( Ent.obj, doorDistance ) then playerLookingAt = e 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 door.state = 'closed and not entered' end else Prompt( "The door is locked. Find a key to unlock door" ) end end elseif door.state == 'closed and not entered' or door.state == 'closed but has been entered' then if U.PlayerLookingAtObj( Ent.obj, doorDistance ) 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 not eKeyPressed then eKeyPressed = true SetAnimation( 0 ) PlayAnimation( e ) Ent.animating = 1 ActivateIfUsed( e ) PlaySound( e, 0 ) if door.state == 'closed and not entered' then door.state = 'open but not entered' else door.state = 'open and entered' end end end elseif door.state == 'open but not entered' or door.state == 'open and entered' then if U.PlayerLookingNear( e, doorDistance, 100 ) then if g_KeyPressE == 1 and Ent.animating == 0 and not eKeyPressed then eKeyPressed = true SetAnimation( 1 ) PlayAnimation( e ) Ent.animating = 1 PlaySound( e, 1 ) CollisionOn( e ) door.collOn = true if door.state == 'open but not entered' then door.state = 'closed and not entered' else door.state = 'closed but has been entered' end end end end end -- the following code needs to be executed even if the player is some -- distance from the door if door.state == 'open but not entered' then if door.otherSide == nil then door.count = 0 door.otherSide = calcDoorSide( Ent.obj ) else if crossedOver( Ent.obj, door.otherSide ) then door.state = 'open and entered' door.count = 1 door.otherSide = -door.otherSide end end if door.collOn and Ent.animating == 0 then CollisionOff( e ) door.collOn = false end elseif door.state == 'open and entered' then if door.collOn and Ent.animating == 0 then CollisionOff( e ) door.collOn = false end if crossedOver( Ent.obj, door.otherSide ) then door.count = door.count + 1 door.otherSide = -door.otherSide end end if eKeyPressed and g_KeyPressE == 0 then eKeyPressed = false end end