Advertisement
Guest User

policebadge_v2

a guest
Jul 1st, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.56 KB | None | 0 0
  1. if ( SERVER ) then
  2. AddCSLuaFile( "shared.lua" );
  3. end
  4.  
  5. if ( CLIENT ) then
  6. SWEP.PrintName = ( "Police Badge" );
  7. SWEP.DrawCrosshair = ( false );
  8. SWEP.Slot = ( 4 ) ;
  9. SWEP.SlotPos = ( 6 );
  10. SWEP.DrawAmmo = ( false );
  11. end
  12.  
  13. SWEP.Base = ( "weapon_gta_base" );
  14. SWEP.Category = ( "CSS" );
  15. SWEP.Weight = ( 5 );
  16. SWEP.AutoSwitchTo = ( true );
  17. SWEP.AutoSwitchFrom = ( true );
  18. SWEP.IconLetter = ( "C" );
  19.  
  20. SWEP.HoldType = "pistol"
  21.  
  22. SWEP.Primary = {}
  23. SWEP.Primary.ClipSize = -1
  24. SWEP.Primary.DefaultClip = 0
  25. SWEP.Primary.Automatic = false
  26. SWEP.Primary.Ammo = ""
  27.  
  28. SWEP.Secondary.ClipSize = -1
  29. SWEP.Secondary.DefaultClip = 0
  30. SWEP.Secondary.Automatic = false
  31. SWEP.Secondary.Ammo = ""
  32.  
  33. function SWEP:Initialize()
  34. self:SetWeaponHoldType( "" )
  35. // if CLIENT then killicon.AddFont( self.ClassName, "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) ) end
  36. end
  37.  
  38. if ( SERVER ) then
  39. AddCSLuaFile( "shared.lua" );
  40. end
  41.  
  42. if ( CLIENT ) then
  43. SWEP.PrintName = ( "Police Badge" );
  44. SWEP.DrawCrosshair = ( false );
  45. SWEP.Slot = ( 4 ) ;
  46. SWEP.SlotPos = ( 6 );
  47. SWEP.DrawAmmo = ( false );
  48. end
  49.  
  50. SWEP.Base = ( "weapon_gta_base" );
  51. SWEP.Category = ( "CSS" );
  52. SWEP.Weight = ( 5 );
  53. SWEP.AutoSwitchTo = ( true );
  54. SWEP.AutoSwitchFrom = ( true );
  55. SWEP.IconLetter = ( "C" );
  56.  
  57.  
  58. SWEP.ViewModel = "models/perp2/v_fists.mdl"
  59. SWEP.WorldModel = "models/perp2/w_fists.mdl"
  60.  
  61. SWEP.Primary = {}
  62. SWEP.Primary.ClipSize = -1
  63. SWEP.Primary.DefaultClip = 0
  64. SWEP.Primary.Automatic = false
  65. SWEP.Primary.Ammo = ""
  66.  
  67. SWEP.Secondary.ClipSize = -1
  68. SWEP.Secondary.DefaultClip = 0
  69. SWEP.Secondary.Automatic = false
  70. SWEP.Secondary.Ammo = ""
  71.  
  72. function SWEP:Initialize()
  73. self:SetWeaponHoldType( "" )
  74. // if CLIENT then killicon.AddFont( self.ClassName, "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) ) end
  75. end
  76.  
  77. function SWEP:PrimaryAttack()
  78. if ( CLIENT ) then return; end
  79. local eyepos = self.Owner:GetEyeTrace();
  80. if ( eyepos.Entity && eyepos.Entity:IsValid() && eyepos.Entity:IsPlayer() ) then
  81. if ( self.Owner.lastUse && self.Owner.lastUse + 1 > CurTime() ) then return; end
  82. eyepos.Entity:ChatPrint( "Officer name: " .. self.Owner:GetRPName() .. ", Occupation: Undercover police officer." );
  83. self.Owner:Notify( "Notified " .. eyepos.Entity:Nick() );
  84. self.Owner.lastUse = CurTime();
  85. end
  86. end
  87.  
  88. SWEP.ShowViewModel = true
  89. SWEP.ShowWorldModel = true
  90. SWEP.ViewModelBoneMods = {
  91. ["Bip01 L Arm2"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
  92. ["Bip01 L Hand"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
  93. ["Bip01 L Arm1"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) },
  94. ["Bip01 L Arm"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(-0.556, 0, 0), angle = Angle(0, 0, 0) },
  95. ["Bip01"] = { scale = Vector(0.887, 0.887, 0.887), pos = Vector(0.555, 0.925, 0), angle = Angle(0, 0, 0) }
  96. }
  97. SWEP.ViewModelBoneMods = {
  98. ["Bip01 L Arm"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(-0.556, 0, 0), angle = Angle(0, 0, 0) },
  99. ["Bip01"] = { scale = Vector(1, 1, 1), pos = Vector(0.185, 0, 0), angle = Angle(0, 0, 0) }
  100. }
  101. SWEP.IronSightsPos = Vector(0, 0, 0)
  102. SWEP.IronSightsAng = Vector(0, 0, 0)
  103. SWEP.VElements = {
  104. ["badge"] = { type = "Model", model = "models/freeman/policebadge.mdl", bone = "Bip01", rel = "", pos = Vector(19, 1.557, 25.454), angle = Angle(8.182, 153.117, 0), size = Vector(1.08, 1.08, 1.08), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  105. }
  106. SWEP.WElements = {
  107. ["policebadge"] = { type = "Model", model = "models/freeman/policebadge.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(3.635, 2.596, -0.519), angle = Angle(171.817, -180, -3.507), size = Vector(1.34, 1.34, 1.34), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  108. }
  109.  
  110. /********************************************************
  111. SWEP Construction Kit base code
  112. Created by Clavus
  113. Available for public use, thread at:
  114. facepunch.com/threads/1032378
  115.  
  116.  
  117. DESCRIPTION:
  118. This script is meant for experienced scripters
  119. that KNOW WHAT THEY ARE DOING. Don't come to me
  120. with basic Lua questions.
  121.  
  122. Just copy into your SWEP or SWEP base of choice
  123. and merge with your own code.
  124.  
  125. The SWEP.VElements, SWEP.WElements and
  126. SWEP.ViewModelBoneMods tables are all optional
  127. and only have to be visible to the client.
  128. ********************************************************/
  129.  
  130. function SWEP:Initialize()
  131.  
  132. // other initialize code goes here
  133.  
  134. if CLIENT then
  135.  
  136. // Create a new table for every weapon instance
  137. self.VElements = table.FullCopy( self.VElements )
  138. self.WElements = table.FullCopy( self.WElements )
  139. self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
  140.  
  141. self:CreateModels(self.VElements) // create viewmodels
  142. self:CreateModels(self.WElements) // create worldmodels
  143.  
  144. // init view model bone build function
  145. if IsValid(self.Owner) then
  146. local vm = self.Owner:GetViewModel()
  147. if IsValid(vm) then
  148. self:ResetBonePositions(vm)
  149.  
  150. // Init viewmodel visibility
  151. if (self.ShowViewModel == nil or self.ShowViewModel) then
  152. vm:SetColor(Color(255,255,255,255))
  153. else
  154. // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
  155. vm:SetColor(Color(255,255,255,1))
  156. // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
  157. // however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
  158. vm:SetMaterial("Debug/hsv")
  159. end
  160. end
  161. end
  162.  
  163. end
  164.  
  165. end
  166.  
  167. function SWEP:Holster()
  168.  
  169. if CLIENT and IsValid(self.Owner) then
  170. local vm = self.Owner:GetViewModel()
  171. if IsValid(vm) then
  172. self:ResetBonePositions(vm)
  173. end
  174. end
  175.  
  176. return true
  177. end
  178.  
  179. function SWEP:OnRemove()
  180. self:Holster()
  181. end
  182.  
  183. if CLIENT then
  184.  
  185. SWEP.vRenderOrder = nil
  186. function SWEP:ViewModelDrawn()
  187.  
  188. local vm = self.Owner:GetViewModel()
  189. if !IsValid(vm) then return end
  190.  
  191. if (!self.VElements) then return end
  192.  
  193. self:UpdateBonePositions(vm)
  194.  
  195. if (!self.vRenderOrder) then
  196.  
  197. // we build a render order because sprites need to be drawn after models
  198. self.vRenderOrder = {}
  199.  
  200. for k, v in pairs( self.VElements ) do
  201. if (v.type == "Model") then
  202. table.insert(self.vRenderOrder, 1, k)
  203. elseif (v.type == "Sprite" or v.type == "Quad") then
  204. table.insert(self.vRenderOrder, k)
  205. end
  206. end
  207.  
  208. end
  209.  
  210. for k, name in ipairs( self.vRenderOrder ) do
  211.  
  212. local v = self.VElements[name]
  213. if (!v) then self.vRenderOrder = nil break end
  214. if (v.hide) then continue end
  215.  
  216. local model = v.modelEnt
  217. local sprite = v.spriteMaterial
  218.  
  219. if (!v.bone) then continue end
  220.  
  221. local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
  222.  
  223. if (!pos) then continue end
  224.  
  225. if (v.type == "Model" and IsValid(model)) then
  226.  
  227. model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  228. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  229. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  230. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  231.  
  232. model:SetAngles(ang)
  233. //model:SetModelScale(v.size)
  234. local matrix = Matrix()
  235. matrix:Scale(v.size)
  236. model:EnableMatrix( "RenderMultiply", matrix )
  237.  
  238. if (v.material == "") then
  239. model:SetMaterial("")
  240. elseif (model:GetMaterial() != v.material) then
  241. model:SetMaterial( v.material )
  242. end
  243.  
  244. if (v.skin and v.skin != model:GetSkin()) then
  245. model:SetSkin(v.skin)
  246. end
  247.  
  248. if (v.bodygroup) then
  249. for k, v in pairs( v.bodygroup ) do
  250. if (model:GetBodygroup(k) != v) then
  251. model:SetBodygroup(k, v)
  252. end
  253. end
  254. end
  255.  
  256. if (v.surpresslightning) then
  257. render.SuppressEngineLighting(true)
  258. end
  259.  
  260. render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  261. render.SetBlend(v.color.a/255)
  262. model:DrawModel()
  263. render.SetBlend(1)
  264. render.SetColorModulation(1, 1, 1)
  265.  
  266. if (v.surpresslightning) then
  267. render.SuppressEngineLighting(false)
  268. end
  269.  
  270. elseif (v.type == "Sprite" and sprite) then
  271.  
  272. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  273. render.SetMaterial(sprite)
  274. render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  275.  
  276. elseif (v.type == "Quad" and v.draw_func) then
  277.  
  278. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  279. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  280. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  281. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  282.  
  283. cam.Start3D2D(drawpos, ang, v.size)
  284. v.draw_func( self )
  285. cam.End3D2D()
  286.  
  287. end
  288.  
  289. end
  290.  
  291. end
  292.  
  293. SWEP.wRenderOrder = nil
  294. function SWEP:DrawWorldModel()
  295.  
  296. if (self.ShowWorldModel == nil or self.ShowWorldModel) then
  297. self:DrawModel()
  298. end
  299.  
  300. if (!self.WElements) then return end
  301.  
  302. if (!self.wRenderOrder) then
  303.  
  304. self.wRenderOrder = {}
  305.  
  306. for k, v in pairs( self.WElements ) do
  307. if (v.type == "Model") then
  308. table.insert(self.wRenderOrder, 1, k)
  309. elseif (v.type == "Sprite" or v.type == "Quad") then
  310. table.insert(self.wRenderOrder, k)
  311. end
  312. end
  313.  
  314. end
  315.  
  316. if (IsValid(self.Owner)) then
  317. bone_ent = self.Owner
  318. else
  319. // when the weapon is dropped
  320. bone_ent = self
  321. end
  322.  
  323. for k, name in pairs( self.wRenderOrder ) do
  324.  
  325. local v = self.WElements[name]
  326. if (!v) then self.wRenderOrder = nil break end
  327. if (v.hide) then continue end
  328.  
  329. local pos, ang
  330.  
  331. if (v.bone) then
  332. pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
  333. else
  334. pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
  335. end
  336.  
  337. if (!pos) then continue end
  338.  
  339. local model = v.modelEnt
  340. local sprite = v.spriteMaterial
  341.  
  342. if (v.type == "Model" and IsValid(model)) then
  343.  
  344. model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  345. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  346. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  347. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  348.  
  349. model:SetAngles(ang)
  350. //model:SetModelScale(v.size)
  351. local matrix = Matrix()
  352. matrix:Scale(v.size)
  353. model:EnableMatrix( "RenderMultiply", matrix )
  354.  
  355. if (v.material == "") then
  356. model:SetMaterial("")
  357. elseif (model:GetMaterial() != v.material) then
  358. model:SetMaterial( v.material )
  359. end
  360.  
  361. if (v.skin and v.skin != model:GetSkin()) then
  362. model:SetSkin(v.skin)
  363. end
  364.  
  365. if (v.bodygroup) then
  366. for k, v in pairs( v.bodygroup ) do
  367. if (model:GetBodygroup(k) != v) then
  368. model:SetBodygroup(k, v)
  369. end
  370. end
  371. end
  372.  
  373. if (v.surpresslightning) then
  374. render.SuppressEngineLighting(true)
  375. end
  376.  
  377. render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  378. render.SetBlend(v.color.a/255)
  379. model:DrawModel()
  380. render.SetBlend(1)
  381. render.SetColorModulation(1, 1, 1)
  382.  
  383. if (v.surpresslightning) then
  384. render.SuppressEngineLighting(false)
  385. end
  386.  
  387. elseif (v.type == "Sprite" and sprite) then
  388.  
  389. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  390. render.SetMaterial(sprite)
  391. render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  392.  
  393. elseif (v.type == "Quad" and v.draw_func) then
  394.  
  395. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  396. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  397. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  398. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  399.  
  400. cam.Start3D2D(drawpos, ang, v.size)
  401. v.draw_func( self )
  402. cam.End3D2D()
  403.  
  404. end
  405.  
  406. end
  407.  
  408. end
  409.  
  410. function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
  411.  
  412. local bone, pos, ang
  413. if (tab.rel and tab.rel != "") then
  414.  
  415. local v = basetab[tab.rel]
  416.  
  417. if (!v) then return end
  418.  
  419. // Technically, if there exists an element with the same name as a bone
  420. // you can get in an infinite loop. Let's just hope nobody's that stupid.
  421. pos, ang = self:GetBoneOrientation( basetab, v, ent )
  422.  
  423. if (!pos) then return end
  424.  
  425. pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  426. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  427. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  428. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  429.  
  430. else
  431.  
  432. bone = ent:LookupBone(bone_override or tab.bone)
  433.  
  434. if (!bone) then return end
  435.  
  436. pos, ang = Vector(0,0,0), Angle(0,0,0)
  437. local m = ent:GetBoneMatrix(bone)
  438. if (m) then
  439. pos, ang = m:GetTranslation(), m:GetAngles()
  440. end
  441.  
  442. if (IsValid(self.Owner) and self.Owner:IsPlayer() and
  443. ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
  444. ang.r = -ang.r // Fixes mirrored models
  445. end
  446.  
  447. end
  448.  
  449. return pos, ang
  450. end
  451.  
  452. function SWEP:CreateModels( tab )
  453.  
  454. if (!tab) then return end
  455.  
  456. // Create the clientside models here because Garry says we can't do it in the render hook
  457. for k, v in pairs( tab ) do
  458. if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
  459. string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
  460.  
  461. v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
  462. if (IsValid(v.modelEnt)) then
  463. v.modelEnt:SetPos(self:GetPos())
  464. v.modelEnt:SetAngles(self:GetAngles())
  465. v.modelEnt:SetParent(self)
  466. v.modelEnt:SetNoDraw(true)
  467. v.createdModel = v.model
  468. else
  469. v.modelEnt = nil
  470. end
  471.  
  472. elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
  473. and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
  474.  
  475. local name = v.sprite.."-"
  476. local params = { ["$basetexture"] = v.sprite }
  477. // make sure we create a unique name based on the selected options
  478. local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
  479. for i, j in pairs( tocheck ) do
  480. if (v[j]) then
  481. params["$"..j] = 1
  482. name = name.."1"
  483. else
  484. name = name.."0"
  485. end
  486. end
  487.  
  488. v.createdSprite = v.sprite
  489. v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
  490.  
  491. end
  492. end
  493.  
  494. end
  495.  
  496. local allbones
  497. local hasGarryFixedBoneScalingYet = false
  498.  
  499. function SWEP:UpdateBonePositions(vm)
  500.  
  501. if self.ViewModelBoneMods then
  502.  
  503. if (!vm:GetBoneCount()) then return end
  504.  
  505. // !! WORKAROUND !! //
  506. // We need to check all model names :/
  507. local loopthrough = self.ViewModelBoneMods
  508. if (!hasGarryFixedBoneScalingYet) then
  509. allbones = {}
  510. for i=0, vm:GetBoneCount() do
  511. local bonename = vm:GetBoneName(i)
  512. if (self.ViewModelBoneMods[bonename]) then
  513. allbones[bonename] = self.ViewModelBoneMods[bonename]
  514. else
  515. allbones[bonename] = {
  516. scale = Vector(1,1,1),
  517. pos = Vector(0,0,0),
  518. angle = Angle(0,0,0)
  519. }
  520. end
  521. end
  522.  
  523. loopthrough = allbones
  524. end
  525. // !! ----------- !! //
  526.  
  527. for k, v in pairs( loopthrough ) do
  528. local bone = vm:LookupBone(k)
  529. if (!bone) then continue end
  530.  
  531. // !! WORKAROUND !! //
  532. local s = Vector(v.scale.x,v.scale.y,v.scale.z)
  533. local p = Vector(v.pos.x,v.pos.y,v.pos.z)
  534. local ms = Vector(1,1,1)
  535. if (!hasGarryFixedBoneScalingYet) then
  536. local cur = vm:GetBoneParent(bone)
  537. while(cur >= 0) do
  538. local pscale = loopthrough[vm:GetBoneName(cur)].scale
  539. ms = ms * pscale
  540. cur = vm:GetBoneParent(cur)
  541. end
  542. end
  543.  
  544. s = s * ms
  545. // !! ----------- !! //
  546.  
  547. if vm:GetManipulateBoneScale(bone) != s then
  548. vm:ManipulateBoneScale( bone, s )
  549. end
  550. if vm:GetManipulateBoneAngles(bone) != v.angle then
  551. vm:ManipulateBoneAngles( bone, v.angle )
  552. end
  553. if vm:GetManipulateBonePosition(bone) != p then
  554. vm:ManipulateBonePosition( bone, p )
  555. end
  556. end
  557. else
  558. self:ResetBonePositions(vm)
  559. end
  560.  
  561. end
  562.  
  563. function SWEP:ResetBonePositions(vm)
  564.  
  565. if (!vm:GetBoneCount()) then return end
  566. for i=0, vm:GetBoneCount() do
  567. vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
  568. vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
  569. vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
  570. end
  571.  
  572. end
  573.  
  574. /**************************
  575. Global utility code
  576. **************************/
  577.  
  578. // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
  579. // Does not copy entities of course, only copies their reference.
  580. // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
  581. function table.FullCopy( tab )
  582.  
  583. if (!tab) then return nil end
  584.  
  585. local res = {}
  586. for k, v in pairs( tab ) do
  587. if (type(v) == "table") then
  588. res[k] = table.FullCopy(v) // recursion ho!
  589. elseif (type(v) == "Vector") then
  590. res[k] = Vector(v.x, v.y, v.z)
  591. elseif (type(v) == "Angle") then
  592. res[k] = Angle(v.p, v.y, v.r)
  593. else
  594. res[k] = v
  595. end
  596. end
  597.  
  598. return res
  599.  
  600. end
  601.  
  602. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement