CapsAdmin

Untitled

Nov 12th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.64 KB | None | 0 0
  1. AddCSLuaFile()
  2.  
  3. wowozela = {}
  4.  
  5. wowozela.ValidNotes =
  6. {
  7.     IN_ATTACK,
  8.     IN_ATTACK2,
  9. }
  10.  
  11. wowozela.ValidKeys =
  12. {
  13.     IN_ATTACK,
  14.     IN_ATTACK2,
  15.     IN_WALK,
  16.     IN_SPEED,
  17.     IN_USE,
  18. }
  19.  
  20. for key, val in pairs(wowozela.ValidNotes) do
  21.     concommand.Add("wowozela_select_" .. key, function(ply, _, args)
  22.         local wep = ply:GetActiveWeapon()
  23.        
  24.         if wep:IsValid() and wep:GetClass() == "wowozela" then
  25.             local key, val = tonumber(args[1]) or 1, tonumber(args[2]) or 0
  26.             wep.dt[key] = val
  27.         end    
  28.     end)
  29. end
  30.  
  31. wowozela.Samples = {}
  32.  
  33. for _, file_name in pairs(file.Find("sound/wowozela/samples/*.wav", "GAME")) do
  34.    
  35.     table.insert(wowozela.Samples, "wowozela/samples/" .. file_name)
  36.  
  37.     if SERVER then
  38.         resource.AddFile("sound/wowozela/samples/" .. file_name)
  39.     end
  40. end
  41.  
  42. table.sort(wowozela.Samples, function(a,b) return a < b end)
  43.  
  44.  
  45. function wowozela.New(ply)
  46.     local sampler = setmetatable({}, wowozela.SamplerMeta)
  47.     ply.sampler = sampler
  48.  
  49.     sampler.Player = NULL
  50.  
  51.     sampler.Pitch = 100
  52.     sampler.Volume = 1
  53.  
  54.     sampler.Keys = {}
  55.     sampler.CSP = {}
  56.  
  57.     sampler:Initialize(ply)
  58.  
  59.     return sampler
  60. end
  61.  
  62. function wowozela.IsValidKey(key)
  63.     return table.HasValue(wowozela.ValidKeys, key)
  64. end
  65.  
  66. function wowozela.IsValidNote(key)
  67.     return table.HasValue(wowozela.ValidNotes, key)
  68. end
  69.  
  70. do -- swep meta
  71.     local SWEP = {Primary = {}, Secondary = {}}
  72.  
  73.     SWEP.Base = "weapon_base"
  74.  
  75.     SWEP.Author = ""
  76.     SWEP.Contact = ""
  77.     SWEP.Purpose = ""
  78.     SWEP.Instructions = ""
  79.     SWEP.PrintName = "Wowozela"
  80.  
  81.     SWEP.SlotPos = 1
  82.     SWEP.Slot = 1
  83.  
  84.     SWEP.Spawnable = false
  85.     SWEP.AdminSpawnable = true
  86.  
  87.     SWEP.AutoSwitchTo = true
  88.     SWEP.AutoSwitchFrom = true
  89.     SWEP.HoldType = "normal"
  90.  
  91.     SWEP.Primary.ClipSize = -1
  92.     SWEP.Primary.DefaultClip = -1
  93.     SWEP.Primary.Automatic = false
  94.     SWEP.Primary.Ammo = "none"
  95.  
  96.     SWEP.Secondary.ClipSize = -1
  97.     SWEP.Secondary.DefaultClip = -1
  98.     SWEP.Secondary.Automatic = false
  99.     SWEP.Secondary.Ammo = "none"
  100.  
  101.     SWEP.DrawAmmo = false
  102.     SWEP.DrawCrosshair = true
  103.     SWEP.ViewModel = "models/weapons/v_hands.mdl"
  104.     SWEP.WorldModel = "models/weapons/w_bugbait.mdl"
  105.     SWEP.DrawWeaponInfoBox = true
  106.  
  107.     function SWEP:SetupDataTables()
  108.         for i, key in ipairs(wowozela.ValidNotes) do
  109.             self:DTVar("Int", i, key)
  110.         end
  111.     end
  112.  
  113.     function SWEP:PrintWeaponInfo() end
  114.     function SWEP:DrawWeaponSelection() end
  115.     function SWEP:DrawWorldModel() return true end
  116.     function SWEP:CanPrimaryAttack() return false end
  117.     function SWEP:CanSecondaryAttack() return false end
  118.     function SWEP:ShouldDropOnDie() return false end
  119.     function SWEP:Reload() return false end
  120.  
  121.     function SWEP:Initialize()
  122.        self:SetWeaponHoldType("normal")
  123.     end
  124.  
  125.     if SERVER then
  126.        function SWEP:OnDrop()
  127.           self:Remove()
  128.        end
  129.     end
  130.  
  131.     function SWEP:Deploy()
  132.        self.Think = self._Think
  133.        return true
  134.     end
  135.  
  136.     function SWEP:Holster()
  137.         if not self.Owner:KeyDown(IN_RELOAD) then
  138.             return true
  139.         end
  140.        
  141.         return false
  142.     end
  143.  
  144.     function SWEP:OnKeyEvent(key, press)
  145.         if press and wowozela.IsValidNote(key) and self.Owner:KeyDown(IN_RELOAD) then
  146.             self.dt[key] = math.Clamp((self.dt[key] + 1)%#wowozela.Samples, 1, #wowozela.Samples)
  147.             wowozela.BroadcastKeyEvent(self.Owner, key, press, true)
  148.             wowozela.BroadcastKeyEvent(self.Owner, key, press, false)
  149.         end
  150.     end
  151.  
  152.     function SWEP:_Think()
  153.         if self.Owner and self.Owner:IsValid() and self.Owner:GetViewModel():IsValid() then
  154.             self.Owner:GetViewModel():SetNoDraw(true)
  155.             self.Think = nil
  156.         end
  157.     end
  158.  
  159.     function SWEP:GetViewModelPosition(pos, ang)
  160.        pos.x = 35575
  161.        pos.y = 35575
  162.        pos.z = 35575
  163.  
  164.        return pos, ang
  165.     end
  166.    
  167.     if CLIENT then
  168.         local size = 80
  169.         local count = 4
  170.        
  171.         surface.CreateFont(
  172.             "WowozelaFont",
  173.             {
  174.                 font        = "Roboto Bk",
  175.                 size        = size,
  176.                 weight      = 1000,
  177.             }
  178.         )
  179.        
  180.         local names = {}
  181.        
  182.         function SWEP:DrawHUD()
  183.             local in1 = self.dt and self.dt[IN_ATTACK] ~= 0 and self.dt[IN_ATTACK] or 1
  184.             local in2 = self.dt and self.dt[IN_ATTACK2] ~= 0 and self.dt[IN_ATTACK2] or 1
  185.             local total = #wowozela.Samples
  186.  
  187.             if self.Owner:KeyDown(IN_RELOAD) then
  188.                 for i = -count, count do
  189.                
  190.                     in1 = math.max((in1+i)%total, 1)
  191.                     in2 = math.max((in2+i)%total, 1)
  192.                    
  193.                     if not names[in1] then
  194.                         names[in1] = wowozela.Samples[in1]:match("^wowozela/samples/(.+)%.wav")
  195.                     end
  196.                    
  197.                     if not names[in2] then
  198.                         names[in2] = wowozela.Samples[in2]:match("^wowozela/samples/(.+)%.wav")
  199.                     end
  200.                    
  201.                     --local alpha = -math.abs(i / count) + 1
  202.                     alpha = 255 --alpha * 255
  203.                    
  204.                     local col = HSVToColor((in1/total)*360, 1, 1)
  205.                     col.a = alpha
  206.                    
  207.                     draw.SimpleText(
  208.                         names[in1],
  209.                         "WowozelaFont",
  210.                         16,
  211.                         (i + count) * size,
  212.                         col,
  213.                         TEXT_ALIGN_LEFT,
  214.                         TEXT_ALIGN_CENTER
  215.                     )
  216.                    
  217.                     local col = HSVToColor((in2/total)*360, 1, 1)
  218.                     col.a = alpha
  219.                    
  220.                     draw.SimpleText(
  221.                         names[in2],
  222.                         "WowozelaFont",
  223.                         ScrW() - 16,
  224.                         (i + count) * size,
  225.                         col,
  226.                         TEXT_ALIGN_RIGHT,
  227.                         TEXT_ALIGN_CENTER
  228.                     )
  229.                 end
  230.             else
  231.                 if not names[in1] then
  232.                     names[in1] = wowozela.Samples[in1]:match("^wowozela/samples/(.+)%.wav")
  233.                 end
  234.                
  235.                 if not names[in2] then
  236.                     names[in2] = wowozela.Samples[in2]:match("^wowozela/samples/(.+)%.wav")
  237.                 end            
  238.            
  239.                 draw.SimpleText(
  240.                     names[in1],
  241.                     "WowozelaFont",
  242.                     16,
  243.                     ScrH() - ScrH()/2,
  244.                     HSVToColor((in1/total)*360, 1, 1),
  245.                     TEXT_ALIGN_LEFT,
  246.                     TEXT_ALIGN_CENTER
  247.                 )
  248.                    
  249.                 draw.SimpleText(
  250.                     names[in2],
  251.                     "WowozelaFont",
  252.                     ScrW() - 16,
  253.                     ScrH() - ScrH()/2,
  254.                     HSVToColor((in2/total)*360, 1, 1),
  255.                     TEXT_ALIGN_RIGHT,
  256.                     TEXT_ALIGN_CENTER
  257.                 )
  258.             end
  259.         end
  260.     end
  261.     weapons.Register(SWEP, "wowozela", true)
  262. end
  263.  
  264. do -- sample meta
  265.     local META = {}
  266.     META.__index = META
  267.  
  268.     META.Weapon = NULL
  269.  
  270.     function META:Initialize(ply)
  271.         self.Player = ply
  272.  
  273.         for i, path in pairs(wowozela.Samples) do
  274.             self:SetSample(i, path)
  275.         end
  276.  
  277.         self.IDs = {}
  278.     end
  279.  
  280.     function META:GetSampleIndex(key)
  281.         if wowozela.IsValidNote(key) then
  282.             local wep = self.Player:GetActiveWeapon()
  283.  
  284.             if wep:IsWeapon() and wep:GetClass() == "wowozela" then
  285.                 return math.Clamp(wep.dt[key], 1, #wowozela.Samples)
  286.             end
  287.         end
  288.     end
  289.  
  290.     function META:CanPlay()
  291.         local wep = self.Player:GetActiveWeapon()
  292.         if wep:IsWeapon() and wep:GetClass() == "wowozela" then
  293.             self.Weapon = wep
  294.             return true
  295.         end
  296.  
  297.         return false
  298.     end
  299.  
  300.     function META:GetPos()
  301.         if self.Player == LocalPlayer() and not self.Player:ShouldDrawLocalPlayer() then
  302.             return self.Player:EyePos()
  303.         end
  304.        
  305.         return self.Player:GetBonePosition(self.Player:LookupBone("ValveBiped.Bip01_Head1"))
  306.     end
  307.  
  308.     function META:GetAngles()
  309.         local ang = self.Player:GetAimVector():Angle()
  310.        
  311.         ang.p = math.NormalizeAngle(ang.p)
  312.         ang.y = math.NormalizeAngle(ang.y)
  313.         ang.r = 0
  314.        
  315.         return ang
  316.     end
  317.  
  318.     function META:IsPlaying() -- hm
  319.         for _, on in pairs(self.Keys) do
  320.             if on then
  321.                 return true
  322.             end
  323.         end
  324.  
  325.         return false
  326.     end
  327.  
  328.     function META:SetSample(i, path)
  329.         self.CSP[i] = CreateSound(self.Player, path or wowozela.DefaultSound)
  330.         self.CSP[i]:SetSoundLevel(100)
  331.     end
  332.  
  333.     function META:ChangeVolume(i, num)
  334.         if self.CSP[i] then
  335.             self.CSP[i]:ChangeVolume(self.Volume, 0)
  336.         end
  337.     end
  338.  
  339.     function META:ChangePitch(i, num)
  340.         if self.CSP[i] then
  341.             self.CSP[i]:ChangePitch(self.Pitch, 0)
  342.         end
  343.     end
  344.  
  345.     function META:SetPitch(num) -- ???
  346.         num = num or 1
  347.  
  348.         if self:IsKeyDown(IN_WALK) then
  349.             num = num - 1
  350.         end
  351.  
  352.         if self:IsKeyDown(IN_SPEED) then
  353.             num = num - 2
  354.         end
  355.  
  356.         self.Pitch = math.Clamp(math.floor(100 * 2 ^ num), 1, 255)
  357.  
  358.         for i in pairs(wowozela.Samples) do
  359.             self:ChangePitch(i, self.Pitch)
  360.         end
  361.     end
  362.  
  363.     function META:SetVolume(num)
  364.         self.Volume = math.Clamp(num or self.Volume, 0.0001, 1)
  365.  
  366.         for i in pairs(wowozela.Samples) do
  367.             self:ChangeVolume(i, self.Volume)
  368.         end
  369.     end
  370.  
  371.     function META:Start(i, id)
  372.         if not self:CanPlay() then return end
  373.  
  374.         if self.CSP[i] then
  375.             if id then
  376.                 local snd = self.IDs[id]
  377.                 if snd then
  378.                     snd:Stop()
  379.                 end
  380.                 snd = self.CSP[i]
  381.                 snd:PlayEx(self.Volume, self.Pitch)
  382.                 self.IDs[id] = snd
  383.             else
  384.                 self.CSP[i]:PlayEx(self.Volume, self.Pitch)
  385.             end
  386.         end
  387.     end
  388.  
  389.     function META:Stop(i, id)
  390.         if self.CSP[i] then
  391.             if id then
  392.                 local snd = self.IDs[id]
  393.                 if snd then
  394.                     snd:Stop()
  395.                 end
  396.                 self.IDs[id] = self.CSP[i]
  397.             else
  398.                 self.CSP[i]:Stop()
  399.             end
  400.         end
  401.     end
  402.  
  403.     function META:IsKeyDown(key)
  404.         return self.Keys[key] == true
  405.     end
  406.  
  407.     function META:OnKeyEvent(key, press)
  408.         local id = self:GetSampleIndex(key)
  409.  
  410.         if id then
  411.             if press then
  412.                 self:Start(id, key)
  413.                 self:SetVolume(1)
  414.             else
  415.                 self:Stop(id, key)
  416.             end
  417.         end
  418.     end
  419.  
  420.     function META:Think()
  421.         if not self:CanPlay() then
  422.             for _, csp in pairs(self.CSP) do
  423.                 csp:Stop()
  424.             end
  425.             return
  426.         end
  427.    
  428.         local ang = self:GetAngles()
  429.  
  430.         if self:IsKeyDown(IN_USE) then
  431.             if self.using then
  432.                 self:SetVolume(math.abs(ang.y - self.using) / 20)
  433.             else
  434.                 self.using = ang.y
  435.             end
  436.         else
  437.             self.using = false
  438.             self:SetVolume(1)
  439.         end
  440.  
  441.         self:SetPitch(-ang.p/89)
  442.  
  443.         if self:IsKeyDown(IN_ATTACK) or self:IsKeyDown(IN_ATTACK2) then
  444.             self:MakeParticle()
  445.         end
  446.     end
  447.  
  448.     local emitter
  449.  
  450.     function META:MakeParticle()
  451.         local pitch = self.Pitch
  452.        
  453.         emitter = emitter or ParticleEmitter(Vector())
  454.        
  455.         local scale = self.Player:GetModelScale()
  456.        
  457.         local forward = self:GetAngles():Forward()
  458.         local particle = emitter:Add("particle/particle_glow_04_additive", self:GetPos() + forward * 10 * scale)
  459.        
  460.         if particle then
  461.             local col = HSVToColor(pitch*2.55, self.Volume, 1)
  462.             particle:SetColor(col.r, col.g, col.b, self.Volume)
  463.  
  464.             particle:SetVelocity(self.Volume * self:GetAngles():Forward() * 500 * scale)
  465.  
  466.             particle:SetDieTime(20)
  467.             particle:SetLifeTime(0)
  468.  
  469.             local size = ((-pitch + 255) / 250) + 1
  470.  
  471.             particle:SetAngles(AngleRand())
  472.             particle:SetStartSize(math.max(size*2*scale, 1))
  473.             particle:SetEndSize(0)
  474.  
  475.             particle:SetStartAlpha(255*self.Volume)
  476.             particle:SetEndAlpha(0)
  477.  
  478.             --particle:SetRollDelta(math.Rand(-1,1)*20)
  479.             particle:SetAirResistance(500)
  480.             --particle:SetGravity(Vector(math.Rand(-1,1), math.Rand(-1,1), math.Rand(-1, 1)) * 8 )
  481.         end
  482.     end
  483.  
  484.     wowozela.SamplerMeta = META
  485. end
  486.  
  487. do -- player meta
  488.     local PLAYER = FindMetaTable("Player")
  489.  
  490.     function PLAYER:GetSampler()
  491.         return self.sampler
  492.     end
  493. end
  494.  
  495. do -- hooks
  496.  
  497.     local hack = {}
  498.  
  499.     function wowozela.KeyEvent(ply, key, press)
  500.         --WHAT
  501.         local id = ply:UniqueID() .. key
  502.         if hack[id] == press then return end
  503.         hack[id] = press
  504.         --WHAT
  505.  
  506.         local sampler = ply:GetSampler()
  507.  
  508.         if sampler and sampler.OnKeyEvent and ply == sampler.Player then
  509.  
  510.             sampler.Keys[key] = press
  511.  
  512.             return sampler:OnKeyEvent(key, press)
  513.         end
  514.     end
  515.  
  516.     function wowozela.Think()
  517.         for key, ply in pairs(player.GetAll()) do
  518.             local sampler = ply:GetSampler()
  519.  
  520.             if not sampler then sampler = wowozela.New(ply) end
  521.  
  522.             if sampler and sampler.Think then
  523.                 sampler:Think()
  524.             end
  525.         end
  526.     end
  527.  
  528.     hook.Add("Think", "wowozela_think", wowozela.Think)
  529.  
  530.     function wowozela.Draw()
  531.         for key, ply in pairs(player.GetAll()) do
  532.             local sampler = ply:GetSampler()
  533.  
  534.             if sampler and sampler.Draw then
  535.                 sampler:Draw()
  536.             end
  537.         end
  538.     end
  539.  
  540.     hook.Add("PostDrawOpaqueRenderables", "wowozela_draw", wowozela.Draw)
  541.  
  542.     function wowozela.BroadcastKeyEvent(ply, key, press, filter)
  543.         local rp = RecipientFilter()
  544.         rp:AddAllPlayers()
  545.         if not filter then
  546.             rp:RemovePlayer(ply)
  547.         end
  548.  
  549.         umsg.Start("wowozela_keyevent", rp)
  550.             umsg.Entity(ply)
  551.             umsg.Long(key) -- or short?
  552.             umsg.Bool(press)
  553.         umsg.End()
  554.     end
  555.  
  556.     hook.Add("KeyPress", "wowozela_keypress", function(ply, key)
  557.         local wep = ply:GetActiveWeapon()
  558.         if wep:IsValid() and wep:GetClass() == "wowozela" and wowozela.IsValidKey(key) then
  559.             if SERVER then
  560.                 wowozela.BroadcastKeyEvent(ply, key, true)
  561.                 wep:OnKeyEvent(key, true)
  562.             end
  563.  
  564.             if CLIENT then
  565.                 wowozela.KeyEvent(ply, key, true)
  566.             end
  567.         end
  568.     end)
  569.  
  570.     hook.Add("KeyRelease", "wowozela_keyrelease", function(ply, key)
  571.         local wep = ply:GetActiveWeapon()
  572.         if wep:IsValid() and wep:GetClass() == "wowozela" and wowozela.IsValidKey(key) then
  573.             if SERVER then
  574.                 wowozela.BroadcastKeyEvent(ply, key, false)
  575.                 wep:OnKeyEvent(key, false)
  576.             end
  577.  
  578.             if CLIENT then
  579.                 wowozela.KeyEvent(ply, key, false)
  580.             end
  581.         end
  582.     end)
  583.  
  584.     if CLIENT then
  585.         usermessage.Hook("wowozela_keyevent", function(umr)
  586.             local ply = umr:ReadEntity()
  587.             local key = umr:ReadLong()
  588.             local press = umr:ReadBool()
  589.  
  590.             if ply:IsPlayer() then
  591.                 wowozela.KeyEvent(ply, key, press)
  592.             end
  593.         end)
  594.     end
  595.  
  596. end
Advertisement
Add Comment
Please, Sign In to add comment