Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. AddCSLuaFile()
  2.  
  3. local TTT = ( GAMEMODE_NAME == "terrortown" or cvars.Bool("csgo_knives_force_ttt", false) )
  4.  
  5. DEFINE_BASECLASS( TTT and "weapon_tttbase" or "weapon_base" )
  6.  
  7. if ( SERVER ) then
  8.  
  9. SWEP.Weight = 5
  10. SWEP.AutoSwitchTo = false
  11. SWEP.AutoSwitchFrom = false
  12.  
  13. CreateConVar("csgo_knives_oldsounds", 0, FCVAR_ARCHIVE, "Play old sounds when swinging knife or hitting wall")
  14. CreateConVar("csgo_knives_backstabs", 1, FCVAR_ARCHIVE, "Allow backstabs")
  15. CreateConVar("csgo_knives_primary", 1, FCVAR_ARCHIVE, "Allow primary attacks")
  16. CreateConVar("csgo_knives_secondary", 1, FCVAR_ARCHIVE, "Allow secondary attacks")
  17. CreateConVar("csgo_knives_inspecting", 1, FCVAR_ARCHIVE, "Allow inspecting")
  18. CreateConVar("csgo_knives_force_ttt", 0, FCVAR_ARCHIVE, "Forces knives to enable TTT mode. For debug purposes. Normally you shouldn't enable it unless you haven't any trouble getting it work in ttt")
  19.  
  20. CreateConVar("csgo_knives_dmg_sec_back", 180, FCVAR_ARCHIVE, "How much damage deal when hit with secondary attack from behind")
  21. CreateConVar("csgo_knives_dmg_sec_front", 65, FCVAR_ARCHIVE, "How much damage deal when hit with secondary attack in front or from side")
  22. CreateConVar("csgo_knives_dmg_prim_back", 40, FCVAR_ARCHIVE, "How much damage deal when hit with primary attack from behind")
  23. CreateConVar("csgo_knives_dmg_prim_front1", 40, FCVAR_ARCHIVE, "How much damage deal when hit with primary attack in front or from side (value randomly being picked between *_front1 and *_front2)")
  24. CreateConVar("csgo_knives_dmg_prim_front2", 25, FCVAR_ARCHIVE, "How much damage deal when hit with primary attack in front or from side (value randomly being picked between *_front1 and *_front2)")
  25. end
  26.  
  27.  
  28.  
  29. if ( CLIENT ) then
  30. -- CreateClientConVar( "csgo_knives_cl_lefthanded", "0", true, false, "Flip knives viewmodel and hold knives on left hand" ) -- ToDo
  31.  
  32. SWEP.PrintName = "CS:GO baseknife"
  33. SWEP.Slot = 1
  34. SWEP.SlotPos = 1
  35. SWEP.DrawAmmo = false
  36. SWEP.DrawCrosshair = true
  37. SWEP.ViewModelFOV = 65
  38. SWEP.ViewModelFlip = false
  39. SWEP.CSMuzzleFlashes = true
  40. SWEP.UseHands = true
  41. SWEP.ViewModelFlip = false -- cvars.Bool("csgo_knives_cl_lefthanded", false) -- ToDo
  42. end
  43.  
  44. SWEP.Category = "CS:GO Knives"
  45.  
  46. SWEP.Spawnable = false
  47. SWEP.AdminSpawnable = false
  48.  
  49. --SWEP.ViewModel = "models/weapons/v_csgo_default.mdl"
  50. --SWEP.WorldModel = "models/weapons/W_csgo_default.mdl"
  51.  
  52. SWEP.DrawWeaponInfoBox = false
  53.  
  54. SWEP.Weight = 5
  55. SWEP.AutoSwitchTo = false
  56. SWEP.AutoSwitchFrom = false
  57.  
  58. SWEP.Primary.ClipSize = -1
  59. SWEP.Primary.Damage = -1
  60. SWEP.Primary.DefaultClip = -1
  61. SWEP.Primary.Automatic = true
  62. SWEP.Primary.Ammo ="none"
  63.  
  64.  
  65. SWEP.Secondary.ClipSize = -1
  66. SWEP.Secondary.DefaultClip = -1
  67. SWEP.Secondary.Damage = -1
  68. SWEP.Secondary.Automatic = true
  69. SWEP.Secondary.Ammo ="none"
  70.  
  71. --- TTT config values
  72.  
  73.  
  74.  
  75. -- If AutoSpawnable is true and SWEP.Kind is not WEAPON_EQUIP1/2, then this gun can
  76. -- be spawned as a random weapon.
  77. SWEP.AutoSpawnable = false
  78.  
  79. -- The AmmoEnt is the ammo entity that can be picked up when carrying this gun.
  80. -- SWEP.AmmoEnt = "item_ammo_smg1_ttt"
  81.  
  82. -- CanBuy is a table of ROLE_* entries like ROLE_TRAITOR and ROLE_DETECTIVE. If
  83. -- a role is in this table, those players can buy this.
  84. SWEP.CanBuy = nil -- We are not supposed to buy base knife
  85.  
  86. -- InLoadoutFor is a table of ROLE_* entries that specifies which roles should
  87. -- receive this weapon as soon as the round starts. In this case, none.
  88. SWEP.InLoadoutFor = nil
  89.  
  90. -- If LimitedStock is true, you can only buy one per round.
  91. SWEP.LimitedStock = false
  92.  
  93. -- If AllowDrop is false, players can't manually drop the gun with Q
  94. SWEP.AllowDrop = true
  95.  
  96. -- If IsSilent is true, victims will not scream upon death.
  97. SWEP.IsSilent = true
  98.  
  99. -- If NoSights is true, the weapon won't have ironsights
  100. SWEP.NoSights = true
  101.  
  102. -- This sets the icon shown for the weapon in the DNA sampler, search window,
  103. -- equipment menu (if buyable), etc.
  104. SWEP.Icon = "vgui/ttt/icon_nades" -- most generic icon I guess
  105.  
  106. function SWEP:SetupDataTables() --This also used for variable declaration and SetVar/GetVar getting work
  107. self:NetworkVar( "Float", 0, "InspectTime" )
  108. self:NetworkVar( "Float", 1, "IdleTime" )
  109. end
  110.  
  111.  
  112.  
  113. function SWEP:Initialize()
  114. self:SetSkin(self.SkinIndex or 0) -- Sets worldmodel skin
  115. self:SetHoldType( self.AreDaggers and "fist" or "knife" ) -- Avoid using SetWeaponHoldType! Otherwise the players could hold it wrong!
  116. end
  117.  
  118.  
  119.  
  120. function SWEP:Think()
  121. self.Owner:GetViewModel():SetSkin(self.SkinIndex or 0)
  122. if CurTime()>=self:GetIdleTime() then
  123. self.Weapon:SendWeaponAnim( ACT_VM_IDLE )
  124. self:SetIdleTime( CurTime() + self.Owner:GetViewModel():SequenceDuration() )
  125. end
  126. end
  127.  
  128.  
  129.  
  130. function SWEP:Deploy()
  131. self:SetInspectTime( 0 )
  132. self:SetIdleTime( CurTime() + self.Owner:GetViewModel():SequenceDuration() )
  133. self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
  134. local NextAttack = 1
  135. self.Weapon:SetNextPrimaryFire( CurTime() + NextAttack )
  136. self.Weapon:SetNextSecondaryFire( CurTime() + NextAttack )
  137. -- self.Weapon:EmitSound( "Weapon_Knife.Deploy" ) -- The deploy sound should be played only from v_ model
  138. return true
  139. end
  140.  
  141.  
  142.  
  143. function SWEP:EntityFaceBack(ent)
  144. local angle = self.Owner:GetAngles().y -ent:GetAngles().y
  145. if angle < -180 then angle = 360 +angle end
  146. if angle <= 90 and angle >= -90 then return true end
  147. return false
  148. end
  149.  
  150.  
  151.  
  152. function SWEP:FindHullIntersection(VecSrc, tr, Mins, Maxs, pEntity)
  153.  
  154. local VecHullEnd = VecSrc + ((tr.HitPos - VecSrc) * 2)
  155.  
  156. local tracedata = {}
  157.  
  158. tracedata.start = VecSrc
  159. tracedata.endpos = VecHullEnd
  160. tracedata.filter = pEntity
  161. tracedata.mask = MASK_SOLID
  162. tracedata.mins = Mins
  163. tracedata.maxs = Maxs
  164.  
  165. local tmpTrace = util.TraceLine( tracedata )
  166.  
  167. if tmpTrace.Hit then
  168. tr = tmpTrace
  169. return tr
  170. end
  171.  
  172. local Distance = 999999
  173. --local MinMaxs = { Mins, Maxs }
  174.  
  175. for i = 0, 1 do
  176. for j = 0, 1 do
  177. for k = 0, 1 do
  178.  
  179. local VecEnd = Vector()
  180.  
  181. --VecEnd.x = VecHullEnd.x + MinMaxs[i].x -- Attempt to index a nil value :\
  182. --VecEnd.y = VecHullEnd.y + MinMaxs[j].y
  183. --VecEnd.z = VecHullEnd.z + MinMaxs[k].z
  184. VecEnd.x = VecHullEnd.x + (i>0 and Maxs.x or Mins.x)
  185. VecEnd.y = VecHullEnd.y + (j>0 and Maxs.y or Mins.y)
  186. VecEnd.z = VecHullEnd.z + (k>0 and Maxs.z or Mins.z)
  187.  
  188. tracedata.endpos = VecEnd
  189.  
  190. tmpTrace = util.TraceLine( tracedata )
  191.  
  192. if tmpTrace.Hit then
  193. ThisDistance = (tmpTrace.HitPos - VecSrc):Length()
  194.  
  195. if (ThisDistance < Distance) then
  196. tr = tmpTrace
  197. Distance = ThisDistance
  198. end
  199. end
  200. end -- for k
  201. end -- for j
  202. end --for i
  203.  
  204. return tr
  205. end
  206.  
  207.  
  208.  
  209. function SWEP:PrimaryAttack()
  210. if not cvars.Bool("csgo_knives_primary", true) or ( CurTime() < self.Weapon:GetNextPrimaryFire() ) then return end
  211. self:DoAttack( false )
  212. end
  213.  
  214.  
  215.  
  216. function SWEP:SecondaryAttack()
  217. if not cvars.Bool("csgo_knives_secondary", true) or ( CurTime() < self.Weapon:GetNextSecondaryFire() ) then return end
  218. self:DoAttack( true )
  219. end
  220.  
  221.  
  222.  
  223. function SWEP:DoAttack( Altfire )
  224. local Weapon = self.Weapon
  225. local Attacker = self:GetOwner()
  226. local Range = Altfire and 48 or 64
  227.  
  228. Attacker:LagCompensation(true)
  229.  
  230. local Forward = Attacker:GetAimVector()
  231. local AttackSrc = Attacker:GetShootPos()
  232. local AttackEnd = AttackSrc + Forward * Range
  233.  
  234. local tracedata = {}
  235.  
  236. tracedata.start = AttackSrc
  237. tracedata.endpos = AttackEnd
  238. tracedata.filter = Attacker
  239. tracedata.mask = MASK_SOLID
  240. tracedata.mins = Vector( -16, -16, -18 ) -- head_hull_mins
  241. tracedata.maxs = Vector( 16, 16, 18 ) -- head_hull_maxs
  242.  
  243. local tr = util.TraceLine( tracedata )
  244. if not tr.Hit then tr = util.TraceHull( tracedata ) end
  245. if tr.Hit and ( not (IsValid(tr.Entity) and tr.Entity) or tr.HitWorld ) then
  246. -- Calculate the point of intersection of the line (or hull) and the object we hit
  247. -- This is and approximation of the "best" intersection
  248. local HullDuckMins, HullDuckMaxs = Attacker:GetHullDuck()
  249. tr = self:FindHullIntersection(AttackSrc, tr, HullDuckMins, HullDuckMaxs, Attacker)
  250. AttackEnd = tr.HitPos -- This is the point on the actual surface (the hull could have hit space)
  251. end
  252.  
  253. local DidHit = tr.Hit and not tr.HitSky
  254. local HitEntity = IsValid(tr.Entity) and tr.Entity or Entity(0) -- Ugly hack to destroy glass surf. 0 is worldspawn.
  255. local DidHitPlrOrNPC = HitEntity and ( HitEntity:IsPlayer() or HitEntity:IsNPC() ) and IsValid( HitEntity )
  256.  
  257. Attacker:LagCompensation(false) -- Don't forget to disable it!
  258.  
  259. -- Calculate damage and deal hurt if we can
  260. local Backstab = cvars.Bool("csgo_knives_backstabs", true) and DidHitPlrOrNPC and self:EntityFaceBack( HitEntity ) -- Because we can only backstab creatures
  261. local RMB_BACK = cvars.Number("csgo_knives_dmg_sec_back", 180)
  262. local RMB_FRONT = cvars.Number("csgo_knives_dmg_sec_front", 65)
  263. local LMB_BACK = cvars.Number("csgo_knives_dmg_prim_back", 90)
  264. local LMB_FRONT1 = cvars.Number("csgo_knives_dmg_prim_front1", 40)
  265. local LMB_FRONT2 = cvars.Number("csgo_knives_dmg_prim_front2", 25)
  266.  
  267. local Damage = ( Altfire and (Backstab and RMB_BACK or RMB_FRONT) ) or ( Backstab and LMB_BACK ) or ( ( math.random(0,5) == 3 ) and LMB_FRONT1 ) or LMB_FRONT2 -- This is random it chooses between LMB_FRONT1 AND LMB_FRONT2
  268.  
  269. local damageinfo = DamageInfo()
  270. damageinfo:SetAttacker( Attacker )
  271. damageinfo:SetInflictor( self )
  272. damageinfo:SetDamage( Damage )
  273. damageinfo:SetDamageType( bit.bor( DMG_BULLET , DMG_NEVERGIB ) )
  274. damageinfo:SetDamageForce( Forward * 1000 )
  275. damageinfo:SetDamagePosition( AttackEnd )
  276. HitEntity:DispatchTraceAttack( damageinfo, tr, Forward )
  277.  
  278. if tr.HitWorld then util.Decal("ManhackCut", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal) end
  279.  
  280. --Change next attack time
  281. local NextAttack = Altfire and 1.0 or DidHit and 0.5 or 0.4
  282. Weapon:SetNextPrimaryFire( CurTime() + NextAttack )
  283. Weapon:SetNextSecondaryFire( CurTime() + NextAttack )
  284.  
  285. --Send animation to attacker
  286. Attacker:SetAnimation( PLAYER_ATTACK1 )
  287.  
  288. --Send animation to viewmodel
  289. local Act = DidHit and ( Altfire and ( Backstab and ACT_VM_SWINGHARD or ACT_VM_HITCENTER2 ) or ( Backstab and ACT_VM_SWINGHIT or ACT_VM_HITCENTER ) ) or ( Altfire and ACT_VM_MISSCENTER2 or ACT_VM_MISSCENTER )
  290. if Act then
  291. Weapon:SendWeaponAnim( Act )
  292. self:SetIdleTime( CurTime() + self.Owner:GetViewModel():SequenceDuration() )
  293. end
  294.  
  295. --Play sound
  296. -- Sound("...") were added to precache sounds
  297. local Oldsounds = cvars.Bool("csgo_knives_oldsounds", false)
  298. local StabSnd = Sound("csgo_knife.Stab")
  299. local HitSnd = Sound("csgo_knife.Hit")
  300. local HitwallSnd = Oldsounds and Sound("csgo_knife.HitWall_old") or Sound("csgo_knife.HitWall")
  301. local SlashSnd = Oldsounds and Sound("csgo_knife.Slash_old") or Sound("csgo_knife.Slash")
  302.  
  303. local Snd = DidHitPlrOrNPC and ( Altfire and StabSnd or HitSnd ) or DidHit and HitwallSnd or SlashSnd
  304. -- if Snd then Weapon:EmitSound( Snd ) end -- Probably causes sound missing
  305. Weapon:EmitSound( Snd )
  306.  
  307. return true
  308. end
  309.  
  310.  
  311.  
  312. function SWEP:Reload()
  313. if self.Owner:IsNPC() then return end -- NPCs aren't supposed to reload it
  314.  
  315. local keydown = self.Owner:KeyDown(IN_ATTACK) or self.Owner:KeyDown(IN_ATTACK2) or self.Owner:KeyDown(IN_ZOOM)
  316. if not cvars.Bool("csgo_knives_inspecting", true) or keydown then return end
  317.  
  318. local getseq = self:GetSequence()
  319. local act = self:GetSequenceActivity(getseq) --GetActivity() method doesn't work :\
  320. if ( act == ACT_VM_IDLE_LOWERED and CurTime() < self:GetInspectTime() ) then
  321. self:SetInspectTime( CurTime() + 0.1 ) -- We should press R repeately instead of holding it to loop
  322. return end
  323.  
  324. self.Weapon:SendWeaponAnim(ACT_VM_IDLE_LOWERED)
  325. self:SetIdleTime( CurTime() + self.Owner:GetViewModel():SequenceDuration() )
  326. self:SetInspectTime( CurTime() + 0.1 )
  327. end
  328.  
  329. --YOU'RE WINNER!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement