Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.25 KB | None | 0 0
  1. if SERVER then return end
  2.  
  3. local boneEnabled, boneDisabled = Vector(1, 1, 1), Vector(0, 0, 0)
  4.  
  5. function SWEP:HandleBoneMods2(vm)
  6.  
  7. end
  8.  
  9. function SWEP:HandleBoneMods()
  10.     local vm = self.VModel
  11.  
  12.     if not vm then
  13.         return
  14.     end
  15.  
  16.     if self.WeaponAngle then
  17.         local weaponBone = vm:LookupBone("lead_gun")
  18.  
  19.         if weaponBone then
  20.             if vm:GetBoneMatrix(weaponBone) then
  21.                 vm:ManipulateBoneAngles(weaponBone, self.WeaponAngle)
  22.             end
  23.         end
  24.     end
  25.  
  26.     if self.BoneMatrix then
  27.         for k, v in pairs(self.BoneMatrix) do
  28.             local boneID = vm:LookupBone(k)
  29.  
  30.             if boneID then
  31.                 if vm:GetBoneMatrix(boneID) then
  32.                     vm:ManipulateBonePosition(boneID, v.Pos)
  33.                     vm:ManipulateBoneAngles(boneID, v.Ang)
  34.                 end
  35.             end
  36.         end
  37.     end
  38.  
  39.     if self.SilencerBone then
  40.         local silencerBone = vm:LookupBone(self.SilencerBone)
  41.  
  42.         if silencerBone then
  43.             if vm:GetBoneMatrix(silencerBone) then
  44.                 local b = self:GetSilenced()
  45.  
  46.                 if self.AttachType == "silencer" and self.AttachTime > CurTime() then
  47.                     b = not b
  48.                 end
  49.  
  50.                 vm:ManipulateBoneScale(silencerBone, b and boneEnabled or boneDisabled)
  51.             end
  52.         end
  53.     end
  54.  
  55.     if self.ScopeBone then
  56.         local scopeBone = vm:LookupBone(self.ScopeBone)
  57.  
  58.         if scopeBone then
  59.             if vm:GetBoneMatrix(scopeBone) then
  60.                 local b = self:GetScoped()
  61.  
  62.                 if self.AttachType == "scope" and self.AttachTime > CurTime() then
  63.                     b = not b
  64.                 end
  65.  
  66.                 vm:ManipulateBoneScale(scopeBone, b and boneEnabled or boneDisabled)
  67.             end
  68.         end
  69.     end
  70.  
  71.     if self.SlideBone then
  72.         local boneID = vm:LookupBone(self.SlideBone.name)
  73.  
  74.         if boneID and vm:GetBoneMatrix(boneID) then
  75.             local desired_pos = self:Clip1() == 0 and self.SlideBone.pos or boneDisabled
  76.             local current_pos = vm:GetManipulateBonePosition(boneID)
  77.  
  78.             local FT = FrameTime()
  79.  
  80.             current_pos.x = math.Approach(current_pos.x, desired_pos.x, FT * 15)
  81.             current_pos.y = math.Approach(current_pos.y, desired_pos.y, FT * 15)
  82.             current_pos.z = math.Approach(current_pos.z, desired_pos.z, FT * 15)
  83.  
  84.             vm:ManipulateBonePosition(boneID, current_pos)
  85.         end
  86.     end
  87.  
  88.     local hiddenBones = self.HiddenBones
  89.  
  90.     if istable(hiddenBones) then
  91.         for i = 1, #hiddenBones do
  92.             local boneName = hiddenBones[i]
  93.  
  94.             if boneName then
  95.                 local boneID = vm:LookupBone(boneName)
  96.  
  97.                 if boneID and vm:GetBoneMatrix(boneID) then
  98.                     vm:ManipulateBoneScale(boneID, boneDisabled)
  99.                 end
  100.             end
  101.         end
  102.     end
  103.  
  104.     self:HandleBoneMods2(vm)
  105. end
  106.  
  107. function SWEP:DrawViewModel()
  108.     local viewModel  = self.VModel
  109.     local handsModel = self.Owner.COP_Hands
  110.  
  111.     local FT = FrameTime()
  112.  
  113.     if self.ViewModelFlip then
  114.         render.CullMode(MATERIAL_CULLMODE_CW)
  115.     end
  116.  
  117.     viewModel:FrameAdvance(FT)
  118.     viewModel:SetupBones()
  119.     viewModel:DrawModel()
  120.  
  121.     if handsModel then
  122.         handsModel:SetParent(viewModel)
  123.         handsModel:AddEffects(EF_BONEMERGE)
  124.         handsModel:DrawModel()
  125.     end
  126.  
  127.     if self.ViewModelFlip then
  128.         render.CullMode(MATERIAL_CULLMODE_CCW)
  129.     end
  130.  
  131.     if self.ShellTable then
  132.         for k, v in pairs(self.ShellTable) do
  133.             if !IsValid(v) then
  134.                 self.ShellTable[k] = nil
  135.             else
  136.                 v:DrawModel()
  137.             end
  138.         end
  139.     end
  140. end
  141.  
  142. local normalizeAngle = math.NormalizeAngle
  143.  
  144. function util_NormalizeAngles(a)
  145.     a.p = normalizeAngle(a.p)
  146.     a.y = normalizeAngle(a.y)
  147.     a.r = normalizeAngle(a.r)
  148.     return a
  149. end
  150.  
  151. local clamp = math.Clamp
  152. local function mix(from, to, dt)
  153.     local dt = clamp(dt, 0, 1)
  154.  
  155.     if type(from) == "Angle" then
  156.         from = util_NormalizeAngles(from)
  157.         to   = util_NormalizeAngles(to)
  158.     end
  159.  
  160.     return from + (to - from) * dt
  161. end
  162.  
  163. SWEP.AimMul = 0
  164. SWEP.SmoothAimMul = 0
  165. SWEP.SwayAngle = Angle()
  166. SWEP.BobPos = Vector()
  167.  
  168. SWEP.PitchMul = 0
  169.  
  170. local angTab, oldDelta = Angle(), Angle()
  171.  
  172. function SWEP:GetDesiredFOV(fov)
  173.     local desiredFOV = self.ZoomFOV or fov
  174.  
  175.     if self:GetScoped() then
  176.         if self:ShouldDrawViewModel_COP() then
  177.             return fov
  178.         end
  179.  
  180.         desiredFOV = self.ScopeFOV or desiredFOV
  181.     end
  182.  
  183.     return fov - (fov - desiredFOV) * self.AimMul
  184. end
  185.  
  186. SWEP.SwayPos = 0
  187.  
  188. SWEP.AttachTime = 0
  189. SWEP.AttachAng = 0
  190. SWEP.AttachAng2 = 0
  191.  
  192. SWEP.ImpulseMul = 0
  193. SWEP.HitImpulse = 0
  194. SWEP.ImpulseTime = 0
  195.  
  196. function SWEP:CalcViewModelPos(origin, angles)
  197.     local vm = self.VModel
  198.  
  199.     origin = origin or EyePos()
  200.     angles = angles or (self.Owner:EyeAngles() + self.Owner:GetViewPunchAngles())
  201.  
  202.     local FT, CT = FrameTime(), CurTime()
  203.  
  204.     self.AimMul = math.Approach(self.AimMul, self:GetAiming() and 1 or 0, FT * (1 / self.AimTime))
  205.  
  206.     local AimMul = 1 - self.AimMul
  207.     self.PitchMul = mix(self.PitchMul, angles.p / 90, FT * 5)
  208.  
  209.     angTab.p = angles.p
  210.     angTab.y = angles.y
  211.  
  212.     self.AttachAng  = math.Approach(self.AttachAng, self.AttachTime > CT and 1 or 0, FT * 4)
  213.     self.AttachAng2 = mix(self.AttachAng2, self.AttachAng, FT * 15)
  214.  
  215.     local delta = util_NormalizeAngles(angTab - oldDelta)
  216.  
  217.     oldDelta.p = angles.p
  218.     oldDelta.y = angles.y
  219.  
  220.     local ang = util_NormalizeAngles(delta * math.pi)
  221.  
  222.     ang.p = clamp(ang.p, -math.pi, math.pi)
  223.     ang.y = clamp(ang.y, -math.pi, math.pi)
  224.  
  225.     self.SwayAngle = mix(self.SwayAngle, ang, FT * 3)
  226.  
  227.     local angles_p = self.OriginAng.x * AimMul - self.BobPos.y
  228.     local angles_y = self.OriginAng.y * AimMul + self.BobPos.x
  229.     local angles_r = self.OriginAng.z * AimMul + self.BobPos.x * 0.5
  230.  
  231.     local AimPos = self:GetScoped() and self.ScopeAimPos or self.AimPos
  232.     local AimAng = self:GetScoped() and self.ScopeAimAng or self.AimAng
  233.  
  234.     angles:RotateAroundAxis(angles:Right(),   angles_p + AimAng.x * self.AimMul - self.AttachAng2 * 45)
  235.     angles:RotateAroundAxis(angles:Up(),      angles_y + AimAng.y * self.AimMul + self.AttachAng2 * 32)
  236.     angles:RotateAroundAxis(angles:Forward(), angles_r + AimAng.z * self.AimMul)
  237.  
  238.     local right   = angles:Right()
  239.     local up      = angles:Up()
  240.     local forward = angles:Forward()
  241.  
  242.     local origin_x = (self.OriginPos.x * AimMul + (AimPos.x) * self.AimMul) * (self.ViewModelFlip and -1 or 1)
  243.     local origin_y = (self.OriginPos.y * AimMul + (AimPos.y) * self.AimMul)
  244.     local origin_z = (self.OriginPos.z * AimMul + (AimPos.z) * self.AimMul)
  245.  
  246.     local onGround = self.Owner:IsOnGround()
  247.     if onGround and self.LastVelocity then
  248.         self.ImpulseTime = CurTime() + 0.25
  249.         self.ImpulseMul  = math.Clamp(-self.LastVelocity.z / 200, 0, 2)
  250.  
  251.         self.LastVelocity = nil
  252.     elseif not onGround then
  253.         self.LastVelocity = self.Owner:GetVelocity()
  254.     end
  255.  
  256.     if self.SwayTime then
  257.         local desired = math.sin((self.SwayTime - CT) * math.pi) * self.SwayDir * 2 * (0.25 + AimMul * 0.75)
  258.         self.SwayPos = mix(self.SwayPos, desired, FT * 8)
  259.     else
  260.         self.SwayPos = mix(self.SwayPos, 0, FT * 8)
  261.     end
  262.  
  263.     local impulse   = math.Clamp(-(CT - self.ImpulseTime), 0, 0.25) * 4
  264.     self.HitImpulse = math.sin(impulse * math.pi) * self.ImpulseMul
  265.  
  266.     origin = origin + (origin_x + self.PitchMul * AimMul + self.SwayPos) * right
  267.     origin = origin + (origin_y) * forward
  268.     origin = origin + (origin_z + self.PitchMul * AimMul) * up
  269.  
  270.     origin.z = origin.z - self.HitImpulse
  271.  
  272.     local swayP = self.SwayAngle.p * AimMul
  273.     local swayY = self.SwayAngle.y * AimMul
  274.  
  275.     origin = origin + swayP * up
  276.  
  277.     local c = math.cos(swayY) - 1
  278.     local s = math.sin(swayY)
  279.  
  280.     origin = origin + s * right
  281.     origin = origin + c * forward
  282.  
  283.     return origin, angles
  284. end
  285.  
  286. local cop_viewbob_scale = CreateClientConVar("cop_viewbob_scale", 1, true, false)
  287. local view = {}
  288.  
  289. SWEP.DoMuzzleAnim = false
  290. SWEP.MuzzleAng    = Angle(0, 0, 0)
  291.  
  292. function SWEP:CalcView(ply, origin, angles, fov)
  293.     view.origin = origin
  294.     view.angles = angles
  295.  
  296.     if ply:GetViewEntity() ~= ply then
  297.         return origin, angles, fov
  298.     end
  299.  
  300.     local vm = self.VModel
  301.  
  302.     local up      = angles:Up()
  303.     local right   = angles:Right()
  304.     local forward = angles:Forward()
  305.  
  306.     local CT = CurTime()
  307.     local FT = FrameTime()
  308.  
  309.     local mul = math.Clamp(cop_viewbob_scale:GetFloat(), 0, 2)
  310.  
  311.     if vm then
  312.         local att = vm:GetAttachment(1)
  313.  
  314.         if (self.DoMuzzleAnim or self.boreAnim) and att then
  315.             if self:GetNextPrimaryFire() <= CT or self.AttachTime > CT then
  316.                 self.DoMuzzleAnim = false
  317.             end
  318.  
  319.             self.MuzzleAng.p = math.ApproachAngle(self.MuzzleAng.p, -att.Ang.p * 0.15 * mul, FT * 15)
  320.             self.MuzzleAng.y = math.ApproachAngle(self.MuzzleAng.y, -math.NormalizeAngle(att.Ang.y - math.NormalizeAngle(angles.y - 90)) * 0.15 * mul, FT * 15)
  321.             self.MuzzleAng.r = math.ApproachAngle(self.MuzzleAng.r, math.NormalizeAngle(att.Ang.r + math.NormalizeAngle(angles.p)) * 0.033 * mul, FT * 15)
  322.         else
  323.             if self:GetReloading() then
  324.                 self.DoMuzzleAnim = true
  325.             end
  326.  
  327.             self.MuzzleAng.p = math.ApproachAngle(self.MuzzleAng.p, 0, FT * 15)
  328.             self.MuzzleAng.y = math.ApproachAngle(self.MuzzleAng.y, 0, FT * 15)
  329.             self.MuzzleAng.r = math.ApproachAngle(self.MuzzleAng.r, 0, FT * 15)
  330.         end
  331.     end
  332.  
  333.     view.angles:RotateAroundAxis(right, self.MuzzleAng.p)
  334.     view.angles:RotateAroundAxis(up, self.MuzzleAng.y)
  335.     view.angles:RotateAroundAxis(forward, self.MuzzleAng.r)
  336.  
  337.     local vel = clamp( self.Owner:GetVelocity():Length2D() / 200 * (1.25 - self.AimMul), 0, 1 ) * mul
  338.  
  339.     if self.Owner:GetMoveType() ~= MOVETYPE_WALK or not self.Owner:OnGround() then
  340.         vel = 0
  341.     end
  342.  
  343.     self.BobPos.x = math.sin(CT * 8) * vel
  344.     self.BobPos.y = (math.abs(math.cos(CT * 8)) - 0.5) * vel
  345.  
  346.     view.angles:RotateAroundAxis(right,   -self.BobPos.y)
  347.     view.angles:RotateAroundAxis(up,      self.BobPos.x)
  348.     view.angles:RotateAroundAxis(forward, self.BobPos.x * 0.5)
  349.  
  350.     view.origin   = view.origin - (self.SwayPos) * view.angles:Right()
  351.     view.origin.z = view.origin.z - self.HitImpulse
  352.  
  353.     local desiredFOV = self:GetDesiredFOV(fov)
  354.     view.fov = mix(view.fov or fov, desiredFOV, FT * 15)
  355.  
  356.     return view.origin, view.angles, view.fov
  357. end
  358.  
  359. function SWEP:AdjustMouseSensitivity()
  360.     return (view.fov or 90) / self.Owner:GetFOV()
  361. end
  362.  
  363. function SWEP:PreDrawViewModel(vm, weapon, ply)
  364.     local pos = self.Owner:GetShootPos()
  365.     local FT  = FrameTime()
  366.  
  367.     if self.VModel then
  368.         render.SetBlend(0)
  369.         local origin, angles = self:CalcViewModelPos()
  370.  
  371.         self.VModel:SetPos(origin)
  372.         self.VModel:SetAngles(angles)
  373.  
  374.         self:HandleBoneMods()
  375.     end
  376. end
  377.  
  378. function SWEP:ShouldDrawViewModel_COP()
  379.     return not (self:GetScoped() and self.AimMul >= 0.99)
  380. end
  381.  
  382. function SWEP:PostDrawViewModel(vm, weapon, ply)
  383.     if self.VModel then
  384.         render.SetBlend(1)
  385.         cam.IgnoreZ(true)
  386.  
  387.         if self:ShouldDrawViewModel_COP() then
  388.             self:DrawViewModel()
  389.         end
  390.  
  391.         cam.IgnoreZ(false)
  392.     end
  393. end
  394.  
  395. local function COP_PlayAnimation(um)
  396.     local seq   = um:ReadString()
  397.     local speed = um:ReadFloat()
  398.     local cycle = um:ReadFloat()
  399.  
  400.     local ply = LocalPlayer()
  401.     local wep = ply:GetActiveWeapon()
  402.  
  403.     if IsValid(wep) and wep.IsCOPWeapon then
  404.         wep:PlayAnimation(seq, speed, cycle)
  405.     end
  406. end
  407.  
  408. usermessage.Hook("COP_PLAYANIMATION", COP_PlayAnimation)
  409.  
  410. local function COP_EmitShell()
  411.     local ply = LocalPlayer()
  412.     local wep = ply:GetActiveWeapon()
  413.  
  414.     if IsValid(wep) and wep.IsCOPWeapon then
  415.         wep:EmitShell()
  416.     end
  417. end
  418.  
  419. usermessage.Hook("COP_EMITSHELL", COP_EmitShell)
  420.  
  421. local function COP_AddonAttach(um)
  422.     local ply = LocalPlayer()
  423.     local wep = ply:GetActiveWeapon()
  424.  
  425.     local addon_type = um:ReadString()
  426.  
  427.     if IsValid(wep) and wep.IsCOPWeapon then
  428.         wep.AttachTime = CurTime() + 1
  429.         wep.AttachType = addon_type
  430.  
  431.         if addon_type == "silencer" then
  432.             wep:EmitSound(wep:GetSilenced() and "COP_Addon.Detach" or "COP_Addon.Attach")
  433.         else
  434.             wep:EmitSound(wep:GetScoped() and "COP_Addon.Detach" or "COP_Addon.Attach")
  435.         end
  436.     end
  437. end
  438.  
  439. usermessage.Hook("COP_ADDON_ATTACH", COP_AddonAttach)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement