Advertisement
wifiboost

yoink'd

Sep 5th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.67 KB | None | 0 0
  1.  
  2. -----------------------------------------------------
  3. -- hook.Add("PlayerIsLoaded", "SRP_Weapon_Selector", function()
  4.  
  5. local enabled_weaponselector = CreateClientConVar("srp_hud_weapon", "1", true)
  6.  
  7. local function SRPWeaponSelector()
  8.  
  9. local scale = (ScrW() / 175 >= 6 and 1) or 0.8
  10.  
  11. surface.CreateFont("srp_wepsel", {
  12. size = 20 * scale,
  13. weight = 300 * scale,
  14. antialias = true,
  15. extended = true,
  16. font = "Roboto Bold"})
  17.  
  18. surface.CreateFont("srp_wepsel_small", {
  19. size = 20 * 0.7 * scale,
  20. weight = 300 * scale,
  21. antialias = true,
  22. extended = true,
  23. font = "Roboto Bold"})
  24.  
  25. local curTab = 0
  26. local curSlot = 1
  27. local alpha = 0
  28. local lastAction = -math.huge
  29. local loadout = {}
  30. local slide = {}
  31.  
  32. local WeaponHistory = {}
  33. local ActiveWeapon
  34. hook.Add("Think", "srp_wepsel", function()
  35. if not LocalPlayer():IsValid() then return end
  36.  
  37. local activeweapon = LocalPlayer():GetActiveWeapon()
  38.  
  39. if not activeweapon:IsValid() or activeweapon == ActiveWeapon then return end
  40. ActiveWeapon = activeweapon
  41.  
  42. activeweapon = activeweapon:GetClass()
  43.  
  44. for k, wep in next, WeaponHistory do
  45. if wep == activeweapon then
  46. table.remove(WeaponHistory, k)
  47. break
  48. end
  49. end
  50.  
  51. if #WeaponHistory >= 4 then
  52. table.remove(WeaponHistory)
  53. end
  54.  
  55. table.insert(WeaponHistory, 1, activeweapon)
  56. end)
  57.  
  58. local newinv
  59. function SelectWeapon(class)
  60. newinv = class
  61. end
  62. hook.Add("CreateMove", "srp_wepsel", function(cmd)
  63. if newinv then
  64. local wep = LocalPlayer():GetWeapon(newinv)
  65. if wep:IsValid() and LocalPlayer():GetActiveWeapon() ~= wep then
  66. cmd:SelectWeapon(wep)
  67. else
  68. newinv = nil
  69. end
  70. end
  71. end)
  72.  
  73. local CWeapons = {}
  74. for _, y in pairs(file.Find("scripts/weapon_*.txt", "MOD")) do
  75. local t = util.KeyValuesToTable(file.Read("scripts/" .. y, "MOD"))
  76. CWeapons[y:match("(.+)%.txt")] = {
  77. Slot = t.bucket,
  78. SlotPos = t.bucket_position,
  79. TextureData = t.texturedata
  80. }
  81. end
  82.  
  83. local localization = {
  84. -- gmod_camera = DarkRP.getPhrase("gmod_camera"),
  85. -- gmod_tool = DarkRP.getPhrase("gmod_tool"),
  86. -- weapon_bugbait = DarkRP.getPhrase("weapon_bugbait"),
  87. -- weapon_physcannon = DarkRP.getPhrase("weapon_physcannon"),
  88. -- weapon_physgun = DarkRP.getPhrase("weapon_physgun"),
  89. }
  90.  
  91. local function findcurrent()
  92. if alpha <= 0 then
  93. table.Empty(slide)
  94. local class = IsValid(LocalPlayer():GetActiveWeapon()) and LocalPlayer():GetActiveWeapon():GetClass()
  95. for k1, v1 in pairs(loadout) do
  96. for k2, v2 in pairs(v1) do
  97. if v2.classname == class then
  98. curTab = k1
  99. curSlot = k2
  100. return
  101. end
  102. end
  103. end
  104. end
  105. end
  106.  
  107. local function update()
  108. table.Empty(loadout)
  109.  
  110. for k, v in pairs(LocalPlayer():GetWeapons()) do
  111. local classname = v:GetClass()
  112.  
  113. local Slot = CWeapons[classname] and CWeapons[classname].Slot or v.Slot or 1
  114.  
  115. loadout[Slot] = loadout[Slot] or {}
  116.  
  117. table.insert(loadout[Slot], {
  118. classname = classname,
  119. name = localization[classname] or v:GetPrintName(),
  120. new = (CurTime() - v:GetCreationTime()) < 60,
  121. slotpos = CWeapons[classname] and CWeapons[classname].SlotPos or v.SlotPos or 1
  122. })
  123. end
  124.  
  125. for k, v in pairs(loadout) do
  126. table.sort(v, function(a, b) return a.slotpos < b.slotpos end)
  127. end
  128. end
  129.  
  130. local FKeyBinds = {
  131. ["gm_showhelp"] = "ShowHelp",
  132. ["gm_showteam"] = "ShowTeam",
  133. ["gm_showspare1"] = "ShowSpare1",
  134. ["gm_showspare2"] = "ShowSpare2"
  135. }
  136.  
  137. local function PlayerBindPress(self, ply, bind, pressed)
  138. self.BaseClass:PlayerBindPress(ply, bind, pressed)
  139.  
  140. local bnd = bind:lower():match("gm_[a-z]+[12]?")
  141. if bnd and FKeyBinds[bnd] then
  142. hook.Run(FKeyBinds[bnd])
  143. end
  144.  
  145. if not pressed then return end
  146.  
  147. bind = bind:lower()
  148.  
  149. if bind:sub(1, 4) == "slot" then
  150. local n = tonumber(bind:sub(5, 5) or 1) or 1
  151.  
  152. if n < 1 or n > 6 then return true end
  153.  
  154. n = n - 1
  155.  
  156. update()
  157.  
  158. if not loadout[n] then return true end
  159.  
  160. findcurrent()
  161.  
  162. if curTab == n and loadout[curTab] and (alpha > 0 or GetConVarNumber("hud_fastswitch") > 0) then
  163. curSlot = curSlot + 1
  164.  
  165. if curSlot > #loadout[curTab] then
  166. curSlot = 1
  167. end
  168. else
  169. curTab = n
  170. curSlot = 1
  171. end
  172.  
  173. if GetConVarNumber("hud_fastswitch") > 0 then
  174. SelectWeapon(loadout[curTab][curSlot].classname)
  175. else
  176. alpha = 1
  177. lastAction = RealTime()
  178. end
  179.  
  180. return true
  181. elseif bind:find("invnext", nil, true) and not (ply:GetActiveWeapon():IsValid() and ply:GetActiveWeapon():GetClass() == "weapon_physgun" and ply:KeyDown(IN_ATTACK)) then
  182. update()
  183.  
  184. if #loadout < 1 then
  185. return true
  186. end
  187.  
  188. findcurrent()
  189.  
  190. curSlot = curSlot + 1
  191.  
  192. if curSlot > (loadout[curTab] and #loadout[curTab] or -1) then
  193. repeat
  194. curTab = curTab + 1
  195. if curTab > 5 then
  196. curTab = 0
  197. end
  198. until loadout[curTab]
  199. curSlot = 1
  200. end
  201.  
  202. if GetConVarNumber("hud_fastswitch") > 0 then
  203. SelectWeapon(loadout[curTab][curSlot].classname)
  204. LocalPlayer():EmitSound('ui/buttonclick.wav')
  205. else
  206. lastAction = RealTime()
  207. alpha = 1
  208. LocalPlayer():EmitSound('ui/buttonrollover.wav')
  209. end
  210.  
  211. return true
  212. elseif bind:find("invprev", nil, true) and not (ply:GetActiveWeapon():IsValid() and ply:GetActiveWeapon():GetClass() == "weapon_physgun" and ply:KeyDown(IN_ATTACK)) then
  213. update()
  214.  
  215. if #loadout < 1 then
  216. return true
  217. end
  218.  
  219. findcurrent()
  220.  
  221. curSlot = curSlot - 1
  222.  
  223. if curSlot < 1 then
  224. repeat
  225. curTab = curTab - 1
  226. if curTab < 0 then
  227. curTab = 5
  228. end
  229. until loadout[curTab]
  230. curSlot = #loadout[curTab]
  231. end
  232.  
  233. if GetConVarNumber("hud_fastswitch") > 0 then
  234. SelectWeapon(loadout[curTab][curSlot].classname)
  235. LocalPlayer():EmitSound('ui/buttonclick.wav')
  236. else
  237. lastAction = RealTime()
  238. alpha = 1
  239. LocalPlayer():EmitSound('ui/buttonrollover.wav')
  240. end
  241.  
  242. return true
  243. elseif bind:find("+attack", nil, true) and alpha > 0 then
  244. if loadout[curTab] and loadout[curTab][curSlot] and not bind:find("+attack2", nil, true) then
  245. SelectWeapon(loadout[curTab][curSlot].classname)
  246. end
  247. LocalPlayer():EmitSound('ui/buttonclick.wav')
  248. alpha = 0
  249.  
  250. return true
  251. elseif bind:find("lastinv", nil, true) then
  252. if #WeaponHistory >= 2 then
  253. SelectWeapon(WeaponHistory[2])
  254. end
  255. end
  256. end
  257.  
  258. -- GAMEMODE.PlayerBindPress = PlayerBindPress
  259. timer.Simple(0, function()
  260. GAMEMODE.PlayerBindPress = PlayerBindPress
  261. end)
  262.  
  263. local width = 175 * scale
  264. local height = 22 * scale
  265. local margin = height / 4
  266. local color_dark = Color(0, 0, 0, 127)
  267. local color_bright = Color(83,104,112, 255)
  268.  
  269. hook.Add("HUDPaint", "srp_wepsel", function()
  270.  
  271. if enabled_weaponselector:GetInt() == 0 then return end
  272.  
  273. if not IsValid(LocalPlayer()) then
  274. return
  275. end
  276.  
  277. if alpha < 1e-02 then
  278. if alpha ~= 0 then
  279. alpha = 0
  280. end
  281. return
  282. end
  283.  
  284. update()
  285.  
  286. if RealTime() - lastAction > 2 then
  287. alpha = Lerp(FrameTime() * 4, alpha, 0)
  288. end
  289.  
  290. surface.SetAlphaMultiplier(alpha)
  291.  
  292. surface.SetDrawColor(color_dark)
  293. surface.SetTextColor(color_white)
  294. surface.SetFont("srp_wepsel")
  295.  
  296. local thisWidth = 0
  297.  
  298. for i, v in pairs(loadout) do
  299. thisWidth = thisWidth + width + margin
  300. end
  301.  
  302. local offx = (ScrW() - (thisWidth - margin))*0.5
  303.  
  304. for i, v in pairs(loadout) do
  305. local offy = margin
  306.  
  307. draw.SRPBackGround(0, offx, offy, height, height)
  308.  
  309. local w, h = surface.GetTextSize(i + 1)
  310. surface.SetTextPos(offx + (height - w) / 2, offy + (height - h) / 2)
  311. surface.DrawText(i + 1)
  312. offy = offy + h + margin
  313.  
  314. for j, wep in pairs(v) do
  315. local selected = curTab == i and curSlot == j
  316.  
  317. local height = height + (height + margin) * (slide[wep.classname] or 0)
  318.  
  319. slide[wep.classname] = Lerp(FrameTime() * 10, slide[wep.classname] or 0, selected and 1 or 0)
  320.  
  321. draw.SRPBackGroundColored(4, offx, offy, width, height, selected and color_bright or color_dark)
  322.  
  323. surface.SetFont("srp_wepsel")
  324. local w, h = surface.GetTextSize(wep.name)
  325. if w > width then
  326. surface.SetFont("srp_wepsel_small")
  327. w, h = surface.GetTextSize(wep.name)
  328. end
  329. surface.SetTextPos(offx + (width - w) / 2, offy + (height - h) / 2)
  330. surface.DrawText(wep.name)
  331.  
  332. offy = offy + height + margin
  333. end
  334.  
  335. surface.SetFont("srp_wepsel")
  336.  
  337. offx = offx + width + margin
  338. end
  339.  
  340. surface.SetAlphaMultiplier(1)
  341. end)
  342.  
  343. hook.Add("HUDShouldDraw", "srp_wepsel", function(name)
  344. if name == "CHudWeaponSelection" then
  345. return false
  346. end
  347. end)
  348.  
  349. end
  350. hook.Add("PlayerIsLoaded", "SRP_Weapon_Selector", SRPWeaponSelector)
  351. SRPWeaponSelector()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement