Advertisement
wifiboost

DarkRP/master/entities/weapons/lockpick/shared.lua

Sep 7th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.84 KB | None | 0 0
  1. AddCSLuaFile()
  2.  
  3. if CLIENT then
  4.     SWEP.PrintName = "Lock Pick"
  5.     SWEP.Slot = 5
  6.     SWEP.SlotPos = 1
  7.     SWEP.DrawAmmo = false
  8.     SWEP.DrawCrosshair = false
  9. end
  10.  
  11. -- Variables that are used on both client and server
  12.  
  13. SWEP.Author = "DarkRP Developers"
  14. SWEP.Instructions = "Left click to pick a lock                               Right Click to Advert Raid"
  15. SWEP.Contact = ""
  16. SWEP.Purpose = ""
  17. SWEP.IsDarkRPLockpick = true
  18.  
  19. SWEP.ViewModelFOV = 62
  20. SWEP.ViewModelFlip = false
  21. SWEP.ViewModel = Model("models/weapons/c_crowbar.mdl")
  22. SWEP.WorldModel = Model("models/weapons/w_crowbar.mdl")
  23.  
  24. SWEP.UseHands = true
  25.  
  26. SWEP.Spawnable = true
  27. SWEP.AdminOnly = true
  28. SWEP.Category = "DarkRP (Utility)"
  29.  
  30. SWEP.Sound = Sound("physics/wood/wood_box_impact_hard3.wav")
  31.  
  32. SWEP.Primary.ClipSize = -1      -- Size of a clip
  33. SWEP.Primary.DefaultClip = 0        -- Default number of bullets in a clip
  34. SWEP.Primary.Automatic = false      -- Automatic/Semi Auto
  35. SWEP.Primary.Ammo = ""
  36.  
  37. SWEP.Secondary.ClipSize = -1        -- Size of a clip
  38. SWEP.Secondary.DefaultClip = -1     -- Default number of bullets in a clip
  39. SWEP.Secondary.Automatic = false        -- Automatic/Semi Auto
  40. SWEP.Secondary.Ammo = ""
  41.  
  42. --[[-------------------------------------------------------
  43. Name: SWEP:Initialize()
  44. Desc: Called when the weapon is first loaded
  45. ---------------------------------------------------------]]
  46. function SWEP:Initialize()
  47.     self:SetHoldType("normal")
  48. end
  49.  
  50. function SWEP:SetupDataTables()
  51.     self:NetworkVar("Bool", 0, "IsLockpicking")
  52.     self:NetworkVar("Float", 0, "LockpickStartTime")
  53.     self:NetworkVar("Float", 1, "LockpickEndTime")
  54.     self:NetworkVar("Float", 2, "NextSoundTime")
  55.     self:NetworkVar("Int", 0, "TotalLockpicks")
  56.     self:NetworkVar("Entity", 0, "LockpickEnt")
  57. end
  58.  
  59. --[[-------------------------------------------------------
  60. Name: SWEP:PrimaryAttack()
  61. Desc: +attack1 has been pressed
  62. ---------------------------------------------------------]]
  63. function SWEP:PrimaryAttack()
  64.     self:SetNextPrimaryFire(CurTime() + 2)
  65.     if self:GetIsLockpicking() then return end
  66.  
  67.     self:GetOwner():LagCompensation(true)
  68.     local trace = self:GetOwner():GetEyeTrace()
  69.     self:GetOwner():LagCompensation(false)
  70.     local ent = trace.Entity
  71.  
  72.     if not IsValid(ent) or ent.DarkRPCanLockpick == false then return end
  73.     local canLockpick = hook.Call("canLockpick", nil, self:GetOwner(), ent, trace)
  74.  
  75.     if canLockpick == false then return end
  76.     if canLockpick ~= true and (
  77.             trace.HitPos:DistToSqr(self:GetOwner():GetShootPos()) > 10000 or
  78.             (not GAMEMODE.Config.canforcedooropen and ent:getKeysNonOwnable()) or
  79.             (not ent:isDoor() and not ent:IsVehicle() and not string.find(string.lower(ent:GetClass()), "vehicle") and (not GAMEMODE.Config.lockpickfading or not ent.isFadingDoor))
  80.         ) then
  81.         return
  82.     end
  83.  
  84.     self:SetHoldType("pistol")
  85.  
  86.     self:SetIsLockpicking(true)
  87.     self:SetLockpickEnt(ent)
  88.     self:SetLockpickStartTime(CurTime())
  89.     local endDelta = hook.Call("lockpickTime", nil, self:GetOwner(), ent) or util.SharedRandom("DarkRP_Lockpick" .. self:EntIndex() .. "_" .. self:GetTotalLockpicks(), 10, 30)
  90.     self:SetLockpickEndTime(CurTime() + endDelta)
  91.     self:SetTotalLockpicks(self:GetTotalLockpicks() + 1)
  92.  
  93.  
  94.     if IsFirstTimePredicted() then
  95.         hook.Call("lockpickStarted", nil, self:GetOwner(), ent, trace)
  96.     end
  97.  
  98.     if CLIENT then
  99.         self.Dots = ""
  100.         self.NextDotsTime = SysTime() + 0.5
  101.         return
  102.     end
  103.  
  104.     local onFail = function(ply) if ply == self:GetOwner() then hook.Call("onLockpickCompleted", nil, ply, false, ent) end end
  105.  
  106.     -- Lockpick fails when dying or disconnecting
  107.     hook.Add("PlayerDeath", self, fc{onFail, fn.Flip(fn.Const)})
  108.     hook.Add("PlayerDisconnected", self, fc{onFail, fn.Flip(fn.Const)})
  109.     -- Remove hooks when finished
  110.     hook.Add("onLockpickCompleted", self, fc{fp{hook.Remove, "PlayerDisconnected", self}, fp{hook.Remove, "PlayerDeath", self}})
  111. end
  112.  
  113. function SWEP:Holster()
  114.     self:SetIsLockpicking(false)
  115.     self:SetLockpickEnt(nil)
  116.     return true
  117. end
  118.  
  119. function SWEP:Succeed()
  120.     self:SetHoldType("normal")
  121.  
  122.     local ent = self:GetLockpickEnt()
  123.     self:SetIsLockpicking(false)
  124.     self:SetLockpickEnt(nil)
  125.  
  126.     if not IsValid(ent) then return end
  127.  
  128.     local override = hook.Call("onLockpickCompleted", nil, self:GetOwner(), true, ent)
  129.  
  130.     if override then return end
  131.  
  132.     if ent.isFadingDoor and ent.fadeActivate and not ent.fadeActive then
  133.         ent:fadeActivate()
  134.         if IsFirstTimePredicted() then timer.Simple(5, function() if IsValid(ent) and ent.fadeActive then ent:fadeDeactivate() end end) end
  135.     elseif ent.Fire then
  136.         ent:keysUnLock()
  137.         ent:Fire("open", "", .6)
  138.         ent:Fire("setanimation", "open", .6)
  139.     end
  140. end
  141.  
  142. function SWEP:Fail()
  143.     self:SetIsLockpicking(false)
  144.     self:SetHoldType("normal")
  145.  
  146.     hook.Call("onLockpickCompleted", nil, self:GetOwner(), false, self:GetLockpickEnt())
  147.     self:SetLockpickEnt(nil)
  148. end
  149.  
  150. local dots = {
  151.     [0] = ".",
  152.     [1] = "..",
  153.     [2] = "...",
  154.     [3] = ""
  155. }
  156. function SWEP:Think()
  157.     if not self:GetIsLockpicking() or self:GetLockpickEndTime() == 0 then return end
  158.  
  159.     if CurTime() >= self:GetNextSoundTime() then
  160.         self:SetNextSoundTime(CurTime() + 1)
  161.         local snd = {1,3,4}
  162.         self:EmitSound("weapons/357/357_reload" .. tostring(snd[math.Round(util.SharedRandom("DarkRP_LockpickSnd" .. CurTime(), 1, #snd))]) .. ".wav", 50, 100)
  163.     end
  164.     if CLIENT and (not self.NextDotsTime or SysTime() >= self.NextDotsTime) then
  165.         self.NextDotsTime = SysTime() + 0.5
  166.         self.Dots = self.Dots or ""
  167.         local len = string.len(self.Dots)
  168.  
  169.         self.Dots = dots[len]
  170.     end
  171.  
  172.     local trace = self:GetOwner():GetEyeTrace()
  173.     if not IsValid(trace.Entity) or trace.Entity ~= self:GetLockpickEnt() or trace.HitPos:DistToSqr(self:GetOwner():GetShootPos()) > 10000 then
  174.         self:Fail()
  175.     elseif self:GetLockpickEndTime() <= CurTime() then
  176.         self:Succeed()
  177.     end
  178. end
  179.  
  180. function SWEP:DrawHUD()
  181.     if not self:GetIsLockpicking() or self:GetLockpickEndTime() == 0 then return end
  182.  
  183.     self.Dots = self.Dots or ""
  184.     local w = ScrW()
  185.     local h = ScrH()
  186.     local x, y, width, height = w / 2 - w / 10, h / 2 - 60, w / 5, h / 15
  187.     draw.RoundedBox(8, x, y, width, height, Color(10,10,10,120))
  188.  
  189.     local time = self:GetLockpickEndTime() - self:GetLockpickStartTime()
  190.     local curtime = CurTime() - self:GetLockpickStartTime()
  191.     local status = math.Clamp(curtime / time, 0, 1)
  192.     local BarWidth = status * (width - 16)
  193.     local cornerRadius = math.Min(8, BarWidth / 3 * 2 - BarWidth / 3 * 2 % 2)
  194.     draw.RoundedBox(cornerRadius, x + 8, y + 8, BarWidth, height - 16, Color(255 - (status * 255), 0 + (status * 255), 0, 255))
  195.  
  196.     draw.DrawNonParsedSimpleText(DarkRP.getPhrase("picking_lock") .. self.Dots, "Trebuchet24", w / 2, y + height / 2, Color(255, 255, 255, 255), 1, 1)
  197. end
  198.  
  199. function SWEP:SecondaryAttack()
  200. RunConsoleCommand("say","/advert Raid")
  201. end
  202.  
  203.  
  204. DarkRP.hookStub{
  205.     name = "canLockpick",
  206.     description = "Whether an entity can be lockpicked.",
  207.     parameters = {
  208.         {
  209.             name = "ply",
  210.             description = "The player attempting to lockpick an entity.",
  211.             type = "Player"
  212.         },
  213.         {
  214.             name = "ent",
  215.             description = "The entity being lockpicked.",
  216.             type = "Entity"
  217.         },
  218.         {
  219.             name = "trace",
  220.             description = "The trace result.",
  221.             type = "table"
  222.         }
  223.     },
  224.     returns = {
  225.         {
  226.             name = "allowed",
  227.             description = "Whether the entity can be lockpicked",
  228.             type = "boolean"
  229.         }
  230.     },
  231.     realm = "Shared"
  232. }
  233.  
  234. DarkRP.hookStub{
  235.     name = "lockpickStarted",
  236.     description = "Called when a player is about to pick a lock.",
  237.     parameters = {
  238.         {
  239.             name = "ply",
  240.             description = "The player that is about to pick a lock.",
  241.             type = "Player"
  242.         },
  243.         {
  244.             name = "ent",
  245.             description = "The entity being lockpicked.",
  246.             type = "Entity"
  247.         },
  248.         {
  249.             name = "trace",
  250.             description = "The trace result.",
  251.             type = "table"
  252.         }
  253.     },
  254.     returns = {},
  255.     realm = "Shared"
  256. }
  257.  
  258. DarkRP.hookStub{
  259.     name = "onLockpickCompleted",
  260.     description = "Result of a player attempting to lockpick an entity.",
  261.     parameters = {
  262.         {
  263.             name = "ply",
  264.             description = "The player attempting to lockpick the entity.",
  265.             type = "Player"
  266.         },
  267.         {
  268.             name = "success",
  269.             description = "Whether the player succeeded in lockpicking the entity.",
  270.             type = "boolean"
  271.         },
  272.         {
  273.             name = "ent",
  274.             description = "The entity that was lockpicked.",
  275.             type = "Entity"
  276.         },
  277.     },
  278.     returns = {
  279.         {
  280.             name = "override",
  281.             description = "Return true to override default behaviour, which is opening the (fading) door.",
  282.             type = "boolean"
  283.         }
  284.     },
  285.     realm = "Shared"
  286. }
  287.  
  288. DarkRP.hookStub{
  289.     name = "lockpickTime",
  290.     description = "The length of time, in seconds, it takes to lockpick an entity.",
  291.     parameters = {
  292.         {
  293.             name = "ply",
  294.             description = "The player attempting to lockpick an entity.",
  295.             type = "Player"
  296.         },
  297.         {
  298.             name = "ent",
  299.             description = "The entity being lockpicked.",
  300.             type = "Entity"
  301.         },
  302.     },
  303.     returns = {
  304.         {
  305.             name = "time",
  306.             description = "Seconds in which it takes a player to lockpick an entity",
  307.             type = "number"
  308.         }
  309.     },
  310.     realm = "Shared"
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement