Advertisement
MoNoLidThZ

BAN HAMMER SAUCE

Mar 23rd, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.51 KB | None | 0 0
  1. SWEP.Spawnable = true
  2. SWEP.AdminOnly = true
  3.  
  4. SWEP.BounceWeaponIcon = true
  5.  
  6. SWEP.Author = ""
  7. SWEP.Instructions = ""
  8. SWEP.Purpose = "Ban bad things"
  9. SWEP.Contact = ""
  10.  
  11. SWEP.Base = "weapon_tttbase"
  12. SWEP.ViewModelFlip = false
  13. SWEP.DrawCrosshair      = false
  14. SWEP.Primary.Damage         = 1337
  15. SWEP.Primary.ClipSize       = 10
  16. SWEP.Primary.DefaultClip    = 10
  17. SWEP.Primary.Automatic      = true
  18. SWEP.Primary.Delay = 0.25
  19. SWEP.Primary.Ammo       = "none"
  20. SWEP.Secondary.ClipSize     = 10
  21. SWEP.Secondary.DefaultClip  = 10
  22. SWEP.Secondary.Automatic    = false
  23. SWEP.Secondary.Ammo     = "none"
  24. SWEP.Secondary.Delay = 0.1
  25.  
  26. SWEP.AllowDelete = false -- never removed for weapon reduction
  27. SWEP.AllowDrop = false
  28. SWEP.Kind = WEAPON_EQUIP2
  29. //SWEP.CanBuy = {ROLE_DETECTIVE}
  30. //SWEP.LimitedStock = true -- only buyable once
  31. //SWEP.WeaponID = AMMO_TASER
  32. SWEP.WeaponMode = {"Kill Only","Kill & Ban","Throw","Kill & Remove","Kill & Breach"}
  33. SWEP.WeaponModeHT = {"grenade","grenade","grenade","grenade","melee2"}
  34. if CLIENT then
  35.  
  36.     SWEP.PrintName = "Ban-Hammer"
  37.     SWEP.HoldType = "grenade"  
  38.     SWEP.ViewModelFOV = 69
  39.     SWEP.Slot = 7
  40.     SWEP.SlotPos = 5
  41.     SWEP.DrawCrosshair = true
  42.     SWEP.UseHands = true
  43.    
  44.     SWEP.WepSelectIcon      = surface.GetTextureID( "weapons/banhammer" )
  45.    
  46.     killicon.Add( "banhammer", "hammer/hammer", color_white )
  47.    
  48.     SWEP.ViewModelBoneMods = {}
  49.    
  50.     SWEP.VElements = {
  51.     ["hammer"] = { type = "Model", model = "models/banhammer/banhammer.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.599, 1.179, 4), angle = Angle(174.886, 168.75, -1.024), size = Vector(0.549, 0.549, 0.549), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  52. }
  53.  
  54.     SWEP.WElements = {
  55.     ["hammer"] = { type = "Model", model = "models/banhammer/banhammer.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.273, 1.363, 3.181), angle = Angle(0, -113.524, -164.659), size = Vector(0.492, 0.492, 0.492), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  56. }
  57. end
  58.  
  59. SWEP.ViewModel = "models/weapons/c_stunstick.mdl"
  60. SWEP.ShowWorldModel = false
  61.    
  62. SWEP.IronSightsPos = Vector(0, 0, 0)
  63. SWEP.IronSightsAng = Vector(0, 0, 0)
  64.  
  65. local randomsounds = {
  66.   "weapons/hammer/hammer_impact1.wav",
  67.   "weapons/hammer/hammer_impact2.wav",
  68.   "weapons/hammer/hammer_impact3.wav",
  69.   "weapons/hammer/hammer_impact4.wav",
  70.   "weapons/hammer/hammer_impact5.wav",
  71.   "weapons/hammer/hammer_impact6.wav",
  72. }
  73. function SWEP:PrimaryAttack()
  74. if SERVER then
  75. self.Owner:EmitSound("weapons/hammer/swing" .. math.random( 1, 2 ) .. ".wav")
  76. self.Owner:EmitSound("weapons/hammer/hammer_impact_ban1.wav")
  77. end
  78. self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
  79.  
  80. self.Owner:ViewPunch(Angle( -2, -3, 0 ))
  81.  
  82. local trace = {}
  83.         trace.start = self.Owner:GetShootPos()
  84.         trace.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 24^14
  85.         trace.filter = self.Owner
  86.     local tr = util.TraceLine(trace)
  87.  
  88. //Mode 1: Kill
  89. if self:GetNWInt("WeaponMode",1) == 1 then
  90. if (tr.HitPos:Distance(self.Owner:GetShootPos()) <= 300) then
  91.     bullet = {}
  92.     bullet.Num    = 1
  93.     bullet.Src    = self.Owner:GetShootPos()
  94.     bullet.Dir    = self.Owner:GetAimVector()
  95.     bullet.Spread = Vector(0, 0, 0)    --0,0,0
  96.     bullet.Tracer = 1
  97.     bullet.Force  = 31337
  98.     bullet.Damage = self.Primary.Damage  
  99.     bullet.AmmoType = "AR2Altfire"
  100. local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
  101. self.Owner:SetAnimation( PLAYER_ATTACK1 ); 
  102.     if SERVER then
  103.     local random = math.random(1, #randomsounds)
  104.     self.Owner:EmitSound(randomsounds[random])
  105.     util.Decal("FadingScorch", hit1, hit2)
  106.     util.ScreenShake( Vector(0,0,0), 5, 5, 0.6, 5000 )
  107.     self.Owner:ViewPunch(Angle( -5, -3, 4 )) end
  108.     local effect = EffectData()
  109.     effect:SetOrigin(tr.HitPos)
  110.     effect:SetNormal(tr.HitNormal)
  111.     effect:SetScale(10)
  112.     util.Effect("StunstickImpact", effect)
  113.     self:ShootEffects()
  114.     self.Owner:FireBullets( bullet )
  115.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  116.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  117. else
  118.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  119.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  120. end
  121. //Mode 2: Ban (1 wk)
  122. elseif self:GetNWInt("WeaponMode",1) == 2 then
  123. if (tr.HitPos:Distance(self.Owner:GetShootPos()) <= 300) then
  124.     bullet = {}
  125.     bullet.Num    = 1
  126.     bullet.Src    = self.Owner:GetShootPos()
  127.     bullet.Dir    = self.Owner:GetAimVector()
  128.     bullet.Spread = Vector(0, 0, 0)    --0,0,0
  129.     bullet.Tracer = 1
  130.     bullet.Force  = 0
  131.     bullet.Damage = self.Primary.Damage  
  132.     bullet.AmmoType = "AR2Altfire"
  133.     bullet.Callback = function(att, tr, dmginfo)
  134.                         if SERVER or (CLIENT and IsFirstTimePredicted()) then
  135.                            local ent = tr.Entity
  136.                            if (not tr.HitWorld) and IsValid(ent) then
  137.  
  138.                               if CLIENT and ent:IsPlayer() and not ent:IsBot() then
  139.                                 RunConsoleCommand( "ulx", "banid", ent:SteamID(), "10080", "The Ban Hammer Has Spoken!" )
  140.                                 //ulx.ban( self.Owner, ent, 10080, "The Ban Hammer Has Spoken!" )
  141.                               end
  142.                            end
  143.                         end
  144.                      end
  145. local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
  146. self.Owner:SetAnimation( PLAYER_ATTACK1 );
  147.     if SERVER then
  148.     local random = math.random(1, #randomsounds)
  149.     self.Owner:EmitSound(randomsounds[random])
  150.     util.Decal("FadingScorch", hit1, hit2)
  151.     util.ScreenShake( Vector(0,0,0), 5, 5, 0.6, 5000 )
  152.     self.Owner:ViewPunch(Angle( -5, -3, 4 )) end
  153.     local effect = EffectData()
  154.     effect:SetOrigin(tr.HitPos)
  155.     effect:SetNormal(tr.HitNormal)
  156.     effect:SetScale(10)
  157.     util.Effect("StunstickImpact", effect)
  158.     self:ShootEffects()
  159.     self.Owner:FireBullets( bullet )
  160.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  161.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  162. else
  163.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  164.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  165. end
  166. //Mode 3: Throw Hammer
  167. elseif self:GetNWInt("WeaponMode",1) == 3 then
  168.     self:ThrowProp("models/banhammer/banhammer.mdl",100000)
  169. //Mode 4: Remove Shit that has been hammer'd
  170. elseif self:GetNWInt("WeaponMode",1) == 4 then
  171. if (tr.HitPos:Distance(self.Owner:GetShootPos()) <= 300) then
  172.     bullet = {}
  173.     bullet.Num    = 1
  174.     bullet.Src    = self.Owner:GetShootPos()
  175.     bullet.Dir    = self.Owner:GetAimVector()
  176.     bullet.Spread = Vector(0, 0, 0)    --0,0,0
  177.     bullet.Tracer = 1
  178.     bullet.Force  = 0
  179.     bullet.Damage = self.Primary.Damage  
  180.     bullet.AmmoType = "AR2Altfire"
  181.     bullet.Callback = function(att, tr, dmginfo)
  182.                        if SERVER then
  183.                           local ent = tr.Entity
  184.                           if (not tr.HitWorld) and IsValid(ent) then
  185.                                 if(ent:IsPlayer()) then
  186.                                     if ent:IsSuperAdmin() then return end
  187.                                     ulx.fancyLogAdmin( self.Owner, "#A removed #T from the game.", ent )
  188.                                     ent:SendLua("cam.End3D()")
  189.                                 end
  190.                                 SafeRemoveEntity(ent)
  191.                            end
  192.                        end
  193.                     end
  194. local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
  195. self.Owner:SetAnimation( PLAYER_ATTACK1 );
  196.     if SERVER then
  197.    local random = math.random(1, #randomsounds)
  198.    self.Owner:EmitSound(randomsounds[random])
  199.     util.Decal("FadingScorch", hit1, hit2)
  200.     util.ScreenShake( Vector(0,0,0), 5, 5, 0.6, 5000 )
  201.     self.Owner:ViewPunch(Angle( -5, -3, 4 )) end
  202.     local effect = EffectData()
  203.     effect:SetOrigin(tr.HitPos)
  204.     effect:SetNormal(tr.HitNormal)
  205.     effect:SetScale(10)
  206.     util.Effect("StunstickImpact", effect)
  207.     self:ShootEffects()
  208.     self.Owner:FireBullets( bullet )
  209.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  210.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  211. else
  212.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  213.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  214. end
  215. //Mode 5: Unlock Doors, Break Props.
  216. elseif self:GetNWInt("WeaponMode",1) == 5 then
  217.     if (tr.HitPos:Distance(self.Owner:GetShootPos()) <= 300) then
  218.     bullet = {}
  219.     bullet.Num    = 1
  220.     bullet.Src    = self.Owner:GetShootPos()
  221.     bullet.Dir    = self.Owner:GetAimVector()
  222.     bullet.Spread = Vector(0, 0, 0)    --0,0,0
  223.     bullet.Tracer = 1
  224.     bullet.Force  = 0
  225.     bullet.Damage = self.Primary.Damage  
  226.     bullet.AmmoType = "AR2Altfire"
  227.     bullet.Callback = function(att, tr, dmginfo)
  228.                        if SERVER or (CLIENT and IsFirstTimePredicted()) then
  229.                           local ent = tr.Entity
  230.                           if (not tr.HitWorld) and IsValid(ent) then
  231.                             self:OpenEnt(ent)
  232.                            end
  233.                        end
  234.                     end
  235. local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
  236. self.Owner:SetAnimation( PLAYER_ATTACK1 );
  237.     if SERVER then
  238.    local random = math.random(1, #randomsounds)
  239.    self.Owner:EmitSound(randomsounds[random])
  240.     util.Decal("FadingScorch", hit1, hit2)
  241.     util.ScreenShake( Vector(0,0,0), 5, 5, 0.6, 5000 )
  242.     self.Owner:ViewPunch(Angle( -5, -3, 4 )) end
  243.     local effect = EffectData()
  244.     effect:SetOrigin(tr.HitPos)
  245.     effect:SetNormal(tr.HitNormal)
  246.     effect:SetScale(10)
  247.     util.Effect("StunstickImpact", effect)
  248.     self:ShootEffects()
  249.     self.Owner:FireBullets( bullet )
  250.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  251.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  252. else
  253.     self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
  254.     self.Owner:SetAnimation( PLAYER_ATTACK1 )
  255. end
  256. end
  257. end
  258.    
  259. function SWEP:SecondaryAttack()
  260. self.Weapon:SetNextSecondaryFire(CurTime() + self.Secondary.Delay)
  261. //self.Owner:EmitSound("weapons/hammer/hammer_impact_ban1.wav")
  262.     self:EmitSound("weapons/shotgun_empty.wav")
  263.     if CLIENT then
  264.         --chat.AddText(Color(0,128,0),"BANHAMMER Mode Set To: "..self.WeaponMode[self:GetNWInt("WeaponMode",1)].." ("..self:GetNWInt("WeaponMode",1)..")")
  265.     else
  266.         self:SetNWInt("WeaponMode",(#self.WeaponMode-1 < self:GetNWInt("WeaponMode",1)) and 1 or self:GetNWInt("WeaponMode",1) + 1)
  267.         if self:GetNWInt("WeaponMode",1) == 2 then
  268.             self.Owner:GodEnable()
  269.         else
  270.             self.Owner:GodDisable()
  271.         end
  272.         self:SetWeaponHoldType(self.WeaponModeHT[self:GetNWInt("WeaponMode",1)])
  273.     end
  274. end
  275. function SWEP:Reload()
  276. end
  277.  
  278. function SWEP:OnDrop()
  279.     self:Remove()
  280. end
  281.  
  282. function SWEP:Deploy()
  283.     self.Owner:EmitSound("weapons/hammer/hammer_impact_special"..math.random(1,2)..".wav" )
  284.     return true
  285. end
  286. function SWEP:DrawHUD()
  287.    
  288.      local client = LocalPlayer()
  289.      //if disable_crosshair:GetBool() or (not IsValid(client)) then return end
  290.         draw.SimpleTextOutlined( self.WeaponMode[self:GetNWInt("WeaponMode",1)], "TabLarge", ScrW()*0.5, 25, Color(255,0,0), 1, 1, 10, Color(0,0,0) )
  291.      local sights = false
  292.  
  293.      local x = ScrW() / 2.0
  294.      local y = ScrH() / 2.0
  295.      local scale = math.max(0.2,  10 * 0)
  296.  
  297.      local LastShootTime = self.Weapon:LastShootTime()
  298.      scale = scale * (2 - math.Clamp( (CurTime() - LastShootTime) * 5, 0.0, 1.0 ))
  299.  
  300.      local alpha =  1
  301.      local bright =  1
  302.  
  303.      -- somehow it seems this can be called before my player metatable
  304.      -- additions have loaded
  305.      if client.IsTraitor and client:IsTraitor() then
  306.         surface.SetDrawColor(255 * bright,
  307.                              50 * bright,
  308.                              50 * bright,
  309.                              255 * alpha)
  310.      else
  311.         surface.SetDrawColor(0,
  312.                              255 * bright,
  313.                              0,
  314.                              255 * alpha)
  315.      end
  316.  
  317.      local gap = 20 * scale * (sights and 0.8 or 1)
  318.      local length = gap + (25 * 1) * scale
  319.      surface.DrawLine( x - length, y, x - gap, y )
  320.      surface.DrawLine( x + length, y, x + gap, y )
  321.      surface.DrawLine( x, y - length, x, y - gap )
  322.      surface.DrawLine( x, y + length, x, y + gap )
  323.  
  324.     local tr = LocalPlayer():GetEyeTrace()
  325.     if IsValid(tr.Entity) then
  326.     if tr.Entity:GetClass() == "player" then
  327.    
  328.             draw.DrawText("Player: ".. tr.Entity:Nick(), "Trebuchet24", ScrW() * 0.5, 63, color_white,TEXT_ALIGN_CENTER)
  329.     else tr = tr.Entity:GetClass()
  330.     draw.DrawText(tr, "Trebuchet24", ScrW() * 0.5, 63, color_white,TEXT_ALIGN_CENTER)
  331.     end
  332.     end
  333.     --chat.AddText(Color(0,255,0,255) , LocalPlayer():GetEyeTrace().Entity:GetClass())
  334. local totalply,livingply = 0,0
  335. for k,v in ipairs(player.GetAll()) do
  336. totalply = totalply+1
  337. if v:Alive() then
  338. livingply = livingply+1
  339.     if v != LocalPlayer() then
  340.        local x1,y1,x2,y2 = coordinates(v)
  341.         --print(tostring(team.GetColor(v:Team())))
  342.         surface.SetDrawColor(Color(255,255,255))
  343.  
  344.         --surface.DrawText()
  345.         draw.DrawText(v:Nick(), "TabLarge", (x1+x2)*0.5, y1-15, color_white,TEXT_ALIGN_CENTER)
  346.        surface.DrawLine( x1, y1, math.min( x1 + 5, x2 ), y1 )
  347.        surface.DrawLine( x1, y1, x1, math.min( y1 + 5, y2 ) )
  348.  
  349.  
  350.        surface.DrawLine( x2, y1, math.max( x2 - 5, x1 ), y1 )
  351.        surface.DrawLine( x2, y1, x2, math.min( y1 + 5, y2 ) )
  352.  
  353.  
  354.        surface.DrawLine( x1, y2, math.min( x1 + 5, x2 ), y2 )
  355.        surface.DrawLine( x1, y2, x1, math.max( y2 - 5, y1 ) )
  356.  
  357.  
  358.        surface.DrawLine( x2, y2, math.max( x2 - 5, x1 ), y2 )
  359.        surface.DrawLine( x2, y2, x2, math.max( y2 - 5, y1 ) )
  360.     end
  361. end
  362. end
  363. draw.DrawText("Living Players: " .. livingply .. "/" .. totalply, "Trebuchet24", 63, 63, Color(255, 255, 255, 255),TEXT_ALIGN_LEFT)
  364.  
  365.      
  366. end
  367. function SWEP:Initialize()
  368.  
  369.     // other initialize code goes here
  370.  
  371.     if CLIENT then
  372.    
  373.         // Create a new table for every weapon instance
  374.         self.VElements = table.FullCopy( self.VElements )
  375.         self.WElements = table.FullCopy( self.WElements )
  376.         self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
  377.        self:SetWeaponHoldType( self.HoldType )
  378.        
  379.         self:CreateModels(self.VElements) // create viewmodels
  380.         self:CreateModels(self.WElements) // create worldmodels
  381.        
  382.         // init view model bone build function
  383.         if IsValid(self.Owner) then
  384.             local vm = self.Owner:GetViewModel()
  385.             if IsValid(vm) then
  386.                 self:ResetBonePositions(vm)
  387.                
  388.                 // Init viewmodel visibility
  389.                 if (self.ShowViewModel == nil or self.ShowViewModel) then
  390.                     vm:SetColor(Color(255,255,255,255))
  391.                 else
  392.                     // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
  393.                     vm:SetColor(Color(255,255,255,1))
  394.                     // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
  395.                     // 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
  396.                     vm:SetMaterial("Debug/hsv")        
  397.                 end
  398.             end
  399.         end
  400.        
  401.     end
  402.  
  403. end
  404.  
  405. function SWEP:Holster()
  406.    
  407.     if CLIENT and IsValid(self.Owner) then
  408.         local vm = self.Owner:GetViewModel()
  409.         if IsValid(vm) then
  410.             self:ResetBonePositions(vm)
  411.         end
  412.     end
  413.    
  414.     return true
  415. end
  416.  
  417. function SWEP:OnRemove()
  418.     self:Holster()
  419. end
  420.  
  421. function SWEP:Ammo1()
  422.     return self.Owner:GetAmmoCount( self.Weapon:GetPrimaryAmmoType() )
  423. end
  424.  
  425. function SWEP:OpenEnt(enthit)
  426. if CLIENT then return end
  427. // If "enthit" is a "normal" door, open it.
  428. if (enthit:GetClass() == "func_door_rotating" or enthit:GetClass() == "prop_door_rotating") then
  429.  
  430. enthit:Fire("Unlock", "", 0)
  431. enthit:Fire("Open", "", 0.01)
  432. end
  433.  
  434. // If "enthit" is like a garage door, do this
  435. if (enthit:GetClass() == "func_door") then
  436.  
  437. enthit:Fire("Unlock", "", 0)
  438. enthit:Fire("Open", "", 0.01)
  439. enthit:Fire("Toggle", "", 0.01)
  440. end
  441.  
  442. // If "enthit" is a combine/elevator door, do this
  443. if (enthit:GetClass() == "prop_dynamic" and enthit:GetModel() != "models/props_lab/RavenDoor.mdl") then
  444.  
  445. enthit:Fire("SetAnimation", "Open", 0)
  446. end
  447.  
  448. // If "enthit" is a rollermine, do this
  449. if (enthit:GetClass() == "NPC_rollermine") then
  450.  
  451. enthit:Fire("Powerdown", "", 0)
  452. end
  453.  
  454. // If "enthit" is a sliding door, do this
  455. if (enthit:GetClass() == "func_movelinear") then
  456.  
  457. enthit:Fire("SetAnimation", "Open", 0)
  458. end
  459.  
  460. // If "enthit" is an elevator, do this
  461. if (enthit:GetClass() == "func_tracktrain") then
  462.  
  463. enthit:Fire("Toggle", "", 0)
  464. end
  465.  
  466. // If "enthit" is a prop, do this
  467. if (enthit:GetClass() == "prop_physics") then
  468.  
  469. enthit:Fire("EnableMotion", "", 0)
  470. end
  471.  
  472. // If "enthit" is the ravenholm door, do this
  473. if (enthit:GetClass() == "prop_dynamic" and enthit:GetModel() == "models/props_lab/RavenDoor.mdl") then
  474.  
  475. enthit:Fire("SetAnimation", "RavenDoor_Open", 0)
  476. end
  477.  
  478. // If "enthit" is a forcefield, do this
  479. if (enthit:GetClass() == "func_wall_toggle") then
  480.  
  481. enthit:Fire("Toggle", "", 0)
  482. end
  483.  
  484. // If "enthit" is like a hl2 citidel tele door, do this
  485. if (enthit:GetClass() == "momentary_rot_button") then
  486.  
  487. enthit:Fire("Unlock", "", 0)
  488. enthit:Fire("Press", "", 0.01)
  489. end
  490. end
  491.  
  492. function SWEP:ThrowProp( model_file,velocitymul )
  493.  
  494.     --
  495.     -- Play the shoot sound we precached earlier!
  496.     --
  497.  
  498.  
  499.     --
  500.     -- If we're the client ) then this is as much as we want to do.
  501.     -- We play the sound above on the client due to prediction.
  502.     -- ( if ( we didn't they would feel a ping delay during multiplayer )
  503.     --
  504.     if ( CLIENT ) then return end
  505.     if !util.IsValidModel( model_file ) then return end
  506.     --
  507.     -- Create a prop_physics entity
  508.     --
  509.     local ent = ents.Create( "prop_physics" )
  510.  
  511.     --
  512.     -- Always make sure that created entities are actually created!
  513.     --
  514.     if (  !IsValid( ent ) ) then return end
  515.  
  516.     --
  517.     -- Set the entity's model to the passed in model
  518.     --
  519.     ent:SetModel( model_file )
  520.  
  521.     --
  522.     -- Set the position to the player's eye position plus 16 units forward.
  523.     -- Set the angles to the player'e eye angles. Then spawn it.
  524.     --
  525.     ent:SetModelScale(0.492, 0.492, 0.492)
  526.     ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 50 ) )
  527.     ent:SetAngles( self.Owner:EyeAngles() )
  528.     ent:Spawn()
  529.  
  530.  
  531.     --
  532.     -- Now get the physics object. Whenever we get a physics object
  533.     -- we need to test to make sure its valid before using it.
  534.     -- If it isn't ) then we'll remove the entity.
  535.     --
  536.     local phys = ent:GetPhysicsObject()
  537.     if (  !IsValid( phys ) ) then ent:Remove() return end
  538.  
  539.  
  540.     --
  541.     -- Now we apply the force - so the chair actually throws instead
  542.     -- of just falling to the ground. You can play with this value here
  543.     -- to adjust how fast we throw it.
  544.     --
  545.     local velocity = self.Owner:GetAimVector()
  546.     velocity = velocity * velocitymul
  547.     phys:ApplyForceCenter( velocity )
  548.     phys:AddAngleVelocity( Vector(0,1000,0) )
  549.     phys:SetMass(1337)
  550.  
  551.     --
  552.     -- Assuming we're playing in Sandbox mode we want to add this
  553.     -- entity to the cleanup and undo lists. This is done like so.
  554.     --
  555.     timer.Simple(10,function()
  556.        
  557.     if  IsValid(ent) then
  558.         ent:Remove()
  559.     end
  560.     end)
  561. end
  562. if CLIENT then
  563.  
  564.     SWEP.vRenderOrder = nil
  565.     function SWEP:ViewModelDrawn()
  566.        
  567.         local vm = self.Owner:GetViewModel()
  568.         if !IsValid(vm) then return end
  569.        
  570.         if (!self.VElements) then return end
  571.        
  572.         self:UpdateBonePositions(vm)
  573.  
  574.         if (!self.vRenderOrder) then
  575.            
  576.             // we build a render order because sprites need to be drawn after models
  577.             self.vRenderOrder = {}
  578.  
  579.             for k, v in pairs( self.VElements ) do
  580.                 if (v.type == "Model") then
  581.                     table.insert(self.vRenderOrder, 1, k)
  582.                 elseif (v.type == "Sprite" or v.type == "Quad") then
  583.                     table.insert(self.vRenderOrder, k)
  584.                 end
  585.             end
  586.            
  587.         end
  588.  
  589.         for k, name in ipairs( self.vRenderOrder ) do
  590.        
  591.             local v = self.VElements[name]
  592.             if (!v) then self.vRenderOrder = nil break end
  593.             if (v.hide) then continue end
  594.            
  595.             local model = v.modelEnt
  596.             local sprite = v.spriteMaterial
  597.            
  598.             if (!v.bone) then continue end
  599.            
  600.             local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
  601.            
  602.             if (!pos) then continue end
  603.            
  604.             if (v.type == "Model" and IsValid(model)) then
  605.  
  606.                 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  607.                 ang:RotateAroundAxis(ang:Up(), v.angle.y)
  608.                 ang:RotateAroundAxis(ang:Right(), v.angle.p)
  609.                 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  610.  
  611.                 model:SetAngles(ang)
  612.                 //model:SetModelScale(v.size)
  613.                 local matrix = Matrix()
  614.                 matrix:Scale(v.size)
  615.                 model:EnableMatrix( "RenderMultiply", matrix )
  616.                
  617.                 if (v.material == "") then
  618.                     model:SetMaterial("")
  619.                 elseif (model:GetMaterial() != v.material) then
  620.                     model:SetMaterial( v.material )
  621.                 end
  622.                
  623.                 if (v.skin and v.skin != model:GetSkin()) then
  624.                     model:SetSkin(v.skin)
  625.                 end
  626.                
  627.                 if (v.bodygroup) then
  628.                     for k, v in pairs( v.bodygroup ) do
  629.                         if (model:GetBodygroup(k) != v) then
  630.                             model:SetBodygroup(k, v)
  631.                         end
  632.                     end
  633.                 end
  634.                
  635.                 if (v.surpresslightning) then
  636.                     render.SuppressEngineLighting(true)
  637.                 end
  638.                
  639.                 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  640.                 render.SetBlend(v.color.a/255)
  641.                 model:DrawModel()
  642.                 render.SetBlend(1)
  643.                 render.SetColorModulation(1, 1, 1)
  644.                
  645.                 if (v.surpresslightning) then
  646.                     render.SuppressEngineLighting(false)
  647.                 end
  648.                
  649.             elseif (v.type == "Sprite" and sprite) then
  650.                
  651.                 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  652.                 render.SetMaterial(sprite)
  653.                 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  654.                
  655.             elseif (v.type == "Quad" and v.draw_func) then
  656.                
  657.                 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  658.                 ang:RotateAroundAxis(ang:Up(), v.angle.y)
  659.                 ang:RotateAroundAxis(ang:Right(), v.angle.p)
  660.                 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  661.                
  662.                 cam.Start3D2D(drawpos, ang, v.size)
  663.                     v.draw_func( self )
  664.                 cam.End3D2D()
  665.  
  666.             end
  667.            
  668.         end
  669.        
  670.     end
  671.  
  672.     SWEP.wRenderOrder = nil
  673.     function SWEP:DrawWorldModel()
  674.        
  675.         if (self.ShowWorldModel == nil or self.ShowWorldModel) then
  676.             self:DrawModel()
  677.         end
  678.        
  679.         if (!self.WElements) then return end
  680.        
  681.         if (!self.wRenderOrder) then
  682.  
  683.             self.wRenderOrder = {}
  684.  
  685.             for k, v in pairs( self.WElements ) do
  686.                 if (v.type == "Model") then
  687.                     table.insert(self.wRenderOrder, 1, k)
  688.                 elseif (v.type == "Sprite" or v.type == "Quad") then
  689.                     table.insert(self.wRenderOrder, k)
  690.                 end
  691.             end
  692.  
  693.         end
  694.        
  695.         if (IsValid(self.Owner)) then
  696.             bone_ent = self.Owner
  697.         else
  698.             // when the weapon is dropped
  699.             bone_ent = self
  700.         end
  701.        
  702.         for k, name in pairs( self.wRenderOrder ) do
  703.        
  704.             local v = self.WElements[name]
  705.             if (!v) then self.wRenderOrder = nil break end
  706.             if (v.hide) then continue end
  707.            
  708.             local pos, ang
  709.            
  710.             if (v.bone) then
  711.                 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
  712.             else
  713.                 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
  714.             end
  715.            
  716.             if (!pos) then continue end
  717.            
  718.             local model = v.modelEnt
  719.             local sprite = v.spriteMaterial
  720.            
  721.             if (v.type == "Model" and IsValid(model)) then
  722.  
  723.                 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  724.                 ang:RotateAroundAxis(ang:Up(), v.angle.y)
  725.                 ang:RotateAroundAxis(ang:Right(), v.angle.p)
  726.                 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  727.  
  728.                 model:SetAngles(ang)
  729.                 //model:SetModelScale(v.size)
  730.                 local matrix = Matrix()
  731.                 matrix:Scale(v.size)
  732.                 model:EnableMatrix( "RenderMultiply", matrix )
  733.                
  734.                 if (v.material == "") then
  735.                     model:SetMaterial("")
  736.                 elseif (model:GetMaterial() != v.material) then
  737.                     model:SetMaterial( v.material )
  738.                 end
  739.                
  740.                 if (v.skin and v.skin != model:GetSkin()) then
  741.                     model:SetSkin(v.skin)
  742.                 end
  743.                
  744.                 if (v.bodygroup) then
  745.                     for k, v in pairs( v.bodygroup ) do
  746.                         if (model:GetBodygroup(k) != v) then
  747.                             model:SetBodygroup(k, v)
  748.                         end
  749.                     end
  750.                 end
  751.                
  752.                 if (v.surpresslightning) then
  753.                     render.SuppressEngineLighting(true)
  754.                 end
  755.                
  756.                 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  757.                 render.SetBlend(v.color.a/255)
  758.                 model:DrawModel()
  759.                 render.SetBlend(1)
  760.                 render.SetColorModulation(1, 1, 1)
  761.                
  762.                 if (v.surpresslightning) then
  763.                     render.SuppressEngineLighting(false)
  764.                 end
  765.                
  766.             elseif (v.type == "Sprite" and sprite) then
  767.                
  768.                 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  769.                 render.SetMaterial(sprite)
  770.                 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  771.                
  772.             elseif (v.type == "Quad" and v.draw_func) then
  773.                
  774.                 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  775.                 ang:RotateAroundAxis(ang:Up(), v.angle.y)
  776.                 ang:RotateAroundAxis(ang:Right(), v.angle.p)
  777.                 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  778.                
  779.                 cam.Start3D2D(drawpos, ang, v.size)
  780.                     v.draw_func( self )
  781.                 cam.End3D2D()
  782.  
  783.             end
  784.            
  785.         end
  786.        
  787.     end
  788.  
  789.     function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
  790.        
  791.         local bone, pos, ang
  792.         if (tab.rel and tab.rel != "") then
  793.            
  794.             local v = basetab[tab.rel]
  795.            
  796.             if (!v) then return end
  797.            
  798.             // Technically, if there exists an element with the same name as a bone
  799.             // you can get in an infinite loop. Let's just hope nobody's that stupid.
  800.             pos, ang = self:GetBoneOrientation( basetab, v, ent )
  801.            
  802.             if (!pos) then return end
  803.            
  804.             pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  805.             ang:RotateAroundAxis(ang:Up(), v.angle.y)
  806.             ang:RotateAroundAxis(ang:Right(), v.angle.p)
  807.             ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  808.                
  809.         else
  810.        
  811.             bone = ent:LookupBone(bone_override or tab.bone)
  812.  
  813.             if (!bone) then return end
  814.            
  815.             pos, ang = Vector(0,0,0), Angle(0,0,0)
  816.             local m = ent:GetBoneMatrix(bone)
  817.             if (m) then
  818.                 pos, ang = m:GetTranslation(), m:GetAngles()
  819.             end
  820.            
  821.             if (IsValid(self.Owner) and self.Owner:IsPlayer() and
  822.                 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
  823.                 ang.r = -ang.r // Fixes mirrored models
  824.             end
  825.        
  826.         end
  827.        
  828.         return pos, ang
  829.     end
  830.  
  831.     function SWEP:CreateModels( tab )
  832.  
  833.         if (!tab) then return end
  834.  
  835.         // Create the clientside models here because Garry says we can't do it in the render hook
  836.         for k, v in pairs( tab ) do
  837.             if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
  838.                     string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
  839.                
  840.                 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
  841.                 if (IsValid(v.modelEnt)) then
  842.                     v.modelEnt:SetPos(self:GetPos())
  843.                     v.modelEnt:SetAngles(self:GetAngles())
  844.                     v.modelEnt:SetParent(self)
  845.                     v.modelEnt:SetNoDraw(true)
  846.                     v.createdModel = v.model
  847.                 else
  848.                     v.modelEnt = nil
  849.                 end
  850.                
  851.             elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
  852.                 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
  853.                
  854.                 local name = v.sprite.."-"
  855.                 local params = { ["$basetexture"] = v.sprite }
  856.                 // make sure we create a unique name based on the selected options
  857.                 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
  858.                 for i, j in pairs( tocheck ) do
  859.                     if (v[j]) then
  860.                         params["$"..j] = 1
  861.                         name = name.."1"
  862.                     else
  863.                         name = name.."0"
  864.                     end
  865.                 end
  866.  
  867.                 v.createdSprite = v.sprite
  868.                 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
  869.                
  870.             end
  871.         end
  872.        
  873.     end
  874.    
  875.     local allbones
  876.     local hasGarryFixedBoneScalingYet = false
  877.  
  878.     function SWEP:UpdateBonePositions(vm)
  879.        
  880.         if self.ViewModelBoneMods then
  881.            
  882.             if (!vm:GetBoneCount()) then return end
  883.            
  884.             // !! WORKAROUND !! //
  885.             // We need to check all model names :/
  886.             local loopthrough = self.ViewModelBoneMods
  887.             if (!hasGarryFixedBoneScalingYet) then
  888.                 allbones = {}
  889.                 for i=0, vm:GetBoneCount() do
  890.                     local bonename = vm:GetBoneName(i)
  891.                     if (self.ViewModelBoneMods[bonename]) then
  892.                         allbones[bonename] = self.ViewModelBoneMods[bonename]
  893.                     else
  894.                         allbones[bonename] = {
  895.                             scale = Vector(1,1,1),
  896.                             pos = Vector(0,0,0),
  897.                             angle = Angle(0,0,0)
  898.                         }
  899.                     end
  900.                 end
  901.                
  902.                 loopthrough = allbones
  903.             end
  904.             // !! ----------- !! //
  905.            
  906.             for k, v in pairs( loopthrough ) do
  907.                 local bone = vm:LookupBone(k)
  908.                 if (!bone) then continue end
  909.                
  910.                 // !! WORKAROUND !! //
  911.                 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
  912.                 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
  913.                 local ms = Vector(1,1,1)
  914.                 if (!hasGarryFixedBoneScalingYet) then
  915.                     local cur = vm:GetBoneParent(bone)
  916.                     while(cur >= 0) do
  917.                         local pscale = loopthrough[vm:GetBoneName(cur)].scale
  918.                         ms = ms * pscale
  919.                         cur = vm:GetBoneParent(cur)
  920.                     end
  921.                 end
  922.                
  923.                 s = s * ms
  924.                 // !! ----------- !! //
  925.                
  926.                 if vm:GetManipulateBoneScale(bone) != s then
  927.                     vm:ManipulateBoneScale( bone, s )
  928.                 end
  929.                 if vm:GetManipulateBoneAngles(bone) != v.angle then
  930.                     vm:ManipulateBoneAngles( bone, v.angle )
  931.                 end
  932.                 if vm:GetManipulateBonePosition(bone) != p then
  933.                     vm:ManipulateBonePosition( bone, p )
  934.                 end
  935.             end
  936.         else
  937.             self:ResetBonePositions(vm)
  938.         end
  939.            
  940.     end
  941.      
  942.     function SWEP:ResetBonePositions(vm)
  943.        
  944.         if (!vm:GetBoneCount()) then return end
  945.         for i=0, vm:GetBoneCount() do
  946.             vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
  947.             vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
  948.             vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
  949.         end
  950.        
  951.     end
  952.  
  953.     /**************************
  954.         Global utility code
  955.     **************************/
  956.  
  957.     // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
  958.     // Does not copy entities of course, only copies their reference.
  959.     // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
  960.     function table.FullCopy( tab )
  961.  
  962.         if (!tab) then return nil end
  963.        
  964.         local res = {}
  965.         for k, v in pairs( tab ) do
  966.             if (type(v) == "table") then
  967.                 res[k] = table.FullCopy(v) // recursion ho!
  968.             elseif (type(v) == "Vector") then
  969.                 res[k] = Vector(v.x, v.y, v.z)
  970.             elseif (type(v) == "Angle") then
  971.                 res[k] = Angle(v.p, v.y, v.r)
  972.             else
  973.                 res[k] = v
  974.             end
  975.         end
  976.        
  977.         return res
  978.        
  979.     end
  980.    
  981. end
  982.  
  983. function coordinates( ent )
  984. local min, max = ent:OBBMins(), ent:OBBMaxs()
  985. local corners = {
  986.         Vector( min.x, min.y, min.z ),
  987.         Vector( min.x, min.y, max.z ),
  988.         Vector( min.x, max.y, min.z ),
  989.         Vector( min.x, max.y, max.z ),
  990.         Vector( max.x, min.y, min.z ),
  991.         Vector( max.x, min.y, max.z ),
  992.         Vector( max.x, max.y, min.z ),
  993.         Vector( max.x, max.y, max.z )
  994. }
  995.  
  996. local minX, minY, maxX, maxY = ScrW() * 2, ScrH() * 2, 0, 0
  997. for _, corner in ipairs( corners ) do
  998.         local onScreen = ent:LocalToWorld( corner ):ToScreen()
  999.         minX, minY = math.min( minX, onScreen.x ), math.min( minY, onScreen.y )
  1000.         maxX, maxY = math.max( maxX, onScreen.x ), math.max( maxY, onScreen.y )
  1001. end
  1002.  
  1003. return minX, minY, maxX, maxY
  1004. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement