-- LUA Script - precede every function and global member with lowercase name of script + '_main' -- forcefield_blue script has been adapted from the default gg door script -- modified by Reliquia -- Script Purpose: -- Player must have key if Force-Field is locked - specified by 'USE KEY' field -- Allow player to Raise\Lower Force-Field -- Force-Field model resources: -- Two animations stored in the model: 0=opening 1=closing -- audio slot 0 - Lower Force-Field -- audio slot 1 - Raise Force-Field -- audio slot 2 - Repel player -- scriptbank\reliquia\forcefiled_blue - This script to facilitate animations: -- variables used by this script local forcefield_pressed = 0 -- state to ensure user must release E key before can open/close again local zapPlayer = 0 -- state used to zap the player local Utils = require "scriptbank\\utillib" -- so we can determine if player is looking at Force-Field or not local Q = require "scriptbank\\quatlib" local hasDecal = 1 --set to 1 if you want to display a decal on screen when forcefield is hit local decalE = nil local decalShowTime = 1000 --time to display the decal for local showingDecal = 0 function forcefield_blue_init(e) Include( "utillib.lua" ) end function forcefield_blue_main(e) if hasDecal == 1 then if decalE == nil then local tdist = 99999 for a,b in pairs (isDecal) do local tdist2 = GetDistance(e,a) if tdist2 < tdist then tdist = tdist2 decalE = a end end end end PlayerDist = GetPlayerDistance(e) if (PlayerDist < 100 ) and g_PlayerHealth > 0 then -- If Force-Field not activated check if key required if g_Entity[e]['activated'] == 0 then if g_Entity[e]['haskey'] == -1 then -- key not required activate the Force-Field SetActivated(e,1) else -- key is required - check if player has one if g_Entity[e]['haskey'] == 1 then -- player has key - prompt to use it Prompt("(E) Enable Force-Field operation") if g_KeyPressE == 1 then -- player used key - activate Force-Field SetActivated(e,1) end else -- player has no key - promt to find one Prompt("Access denied - find key to enable the Force-Field") end end else -- Force-Field is active if g_Entity[e]['activated'] == 1 then -- Force-Field is closed -- display prompt if Force-Field is not currently animating -- not animating, prompt player to open if g_Entity[e]['animating'] == 0 then Prompt("(E) Lower Force Field") end -- player pressed e, open Force-Field if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and forcefield_pressed == 0 then SetAnimation(0) -- set opening animation PlayAnimation(e) -- play the animation g_Entity[e]['animating'] = 1 -- set flag so system knows it is animatng SetActivated(e,2) -- set activated state to 2 so system knows it is in open state ActivateIfUsed(e) PlaySound(e,0) -- play sound in slot 0 forcefield_pressed = 1 -- state to ensure user must release E key before can open\close again --CollisionOff(e) -- handled by end else if g_Entity[e]['activated'] == 2 then -- Force-Field is open -- display prompt if Force-Field is not currently animating -- not animating, prompt player to close if g_Entity[e]['animating'] == 0 then Prompt("(E) Raise Force Field") end -- player pressed e, close the Force-Field if g_KeyPressE == 1 and g_Entity[e]['animating'] == 0 and forcefield_pressed == 0 then SetAnimation(1) -- set closing animation PlayAnimation(e) -- play the animation g_Entity[e]['animating'] = 1 -- set flag so system knows it is animatng SetActivated(e,1) -- set activated state to 2 so system knows it is in closed state PlaySound(e,1) -- play sound in slot 1 forcefield_pressed = 1 -- state to ensure user must release E key before can open\close again --CollisionOff(e) -- turn off collisions end end end end end if showingDecal == 1 then Show(decalE) if GetTimer(e) > decalShowTime or g_Entity[e]['activated'] == 2 then showingDecal = 0 Hide(decalE) end end if zapPlayer == 1 then zapPlayer = 0 end -- check if player has released the E key -- if so reset forcefield_pressed var so user can open/close again if g_KeyPressE == 0 then forcefield_pressed = 0 end -- Check Force-Field is OPEN -- check animation has finished if g_Entity[e]['activated'] == 2 and g_Entity[e]['animating'] == 0 then CollisionOff(e) -- turn collision off end -- Check Force-Field CLOSED -- Check if animation has finished if g_Entity[e]['activated'] == 1 and g_Entity[e]['animating'] == 0 then CollisionOn(e) -- turn collision on end if g_Entity[e]['activated'] == 1 and g_PlayerHealth > 0 or g_Entity[e]['haskey'] == 0 then PlayerDist = GetPlayerDistance(e) local FA = 0 -- to hold Force-Field angle local PLAFF = 0 -- player looking at Force-Field -- make use of the Utillib by AmenMoses if Utils.PlayerLookingAt( e, 80 ) then -- PromptLocal ( e, "Don't touch the Force-Field!" ) PLAFF = 1 end if (PlayerDist < 80 ) and zapPlayer == 0 then -- if player too close push him back --\ Call DetermineForceAngle function local x1,y1,z1 = g_PlayerPosX,g_PlayerPosY+45,g_PlayerPosZ FA = GetAngleToPoint(e,x1,z1) if hasDecal == 1 then SetPosition(decalE,x1,y1,z1) SetRotation(decalE,0,FA,0) MoveForward(decalE,60) x1,y1,z1 = GetEntityPosAng(decalE) local x2,y2,z2,_,_,_ = GetEntityPosAng(e) local ray = IntersectAll(x1,y1,z1,x2,y2,z2,0) if ray == nil then ray = 0 end if ray == 0 then x1,y1,z1 = GetIntersectCollisionX(),GetIntersectCollisionY(),GetIntersectCollisionZ() SetPosition(decalE,x1,y1,z1) RotateToPlayer(decalE) MoveForward(decalE,15) xa1,ya1,za1 = GetIntersectCollisionNX(),GetIntersectCollisionNY(),GetIntersectCollisionNZ() SetRotation(decalE,xa1,ya1,za1) showingDecal = 1 end end ForcePlayer(FA, 3) StartTimer(e) --\ zapPlayer = 1 PlaySound(e,2) -- play sound in slot 2 end end --PromptLocal (e, "Player Y =".. g_PlayerAngY ) --PromptLocal ( e, "activated=" .. g_Entity[e]['activated'] .. "PlayerDist=" .. PlayerDist ) --PromptLocal ( e, "activated=" .. g_Entity[e]['activated'] .. "PlayerDist=" .. PlayerDist .. " plrvisible=" .. g_Entity[e]['plrvisible'] .. " haskey=" .. g_Entity[e]['haskey'] ) --PromptLocal ( e, "is activated=" .. g_Entity[e]['activated'] .. " is anmimating = " .. g_Entity[e]['animating'] ) end function GetDistance(a,b) local x1,y1,z1 = GetEntityPosAng(a) local x2,y2,z2 = GetEntityPosAng(b) local dx, dy, dz = x1 - x2, y1 - y2, z1 - z2 return ( dx*dx+dy*dy+dz*dz ) end function GetAngleToPoint(e,x,z) if g_Entity[e] ~= nil and x > 0 and z > 0 then local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) if angle < 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end --SetRotation(e,0,angle,0) return angle end end