Advertisement
RadioactvePixels

Untitled

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