Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.40 KB | None | 0 0
  1. AddCSLuaFile("scythe.lua")
  2.  
  3. SWEP.Category = "Other"
  4. SWEP.PrintName = "Scythe"
  5. SWEP.Author = "none"
  6. SWEP.Instructions = ""
  7. SWEP.Spawnable = true
  8. SWEP.AdminOnly = false
  9. SWEP.Primary.ClipSize = -1
  10. SWEP.Primary.DefaultClip = -1
  11. SWEP.Primary.Automatic = true
  12. SWEP.Primary.Ammo = "None"
  13. SWEP.Secondary.ClipSize = -1
  14. SWEP.Secondary.DefaultClip = -1
  15. SWEP.Secondary.Automatic = false
  16. SWEP.Secondary.Ammo = "None"
  17. SWEP.Weight = 3
  18. SWEP.AutoSwitchTo = false
  19. SWEP.AutoSwitchFrom = false
  20. SWEP.Slot = 0
  21. SWEP.SlotPos = 4
  22. SWEP.DrawAmmo = false
  23. SWEP.DrawCrosshair = true
  24. SWEP.HoldType = "melee"
  25. SWEP.ViewModelFOV = 70
  26. SWEP.ViewModelFlip = false
  27. SWEP.ViewModel = "models/weapons/v_crowbar.mdl"
  28. SWEP.WorldModel = "models/weapons/w_crowbar.mdl"
  29. SWEP.ShowViewModel = false
  30. SWEP.ShowWorldModel = false
  31. SWEP.ViewModelBoneMods = {}
  32. SWEP.WElements = {
  33. ["scythew"] = { type = "Model", model = "models/voled/scythe/scythe2.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.596, 2.596, 1.557), angle = Angle(180, -85.325, -3.507), size = Vector(0.82, 0.82, 0.82), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  34. }
  35. SWEP.VElements = {
  36. ["scythev"] = { type = "Model", model = "models/voled/scythe/scythe2.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(0, 0, 8.831), angle = Angle(180, 1.169, -17.532), size = Vector(0.5, 0.432, 0.5), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  37. }
  38.  
  39. function SWEP:Initialize()
  40.  
  41. // other initialize code goes here
  42.  
  43. if CLIENT then
  44.  
  45. // Create a new table for every weapon instance
  46. self.VElements = table.FullCopy( self.VElements )
  47. self.WElements = table.FullCopy( self.WElements )
  48. self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
  49.  
  50. self:CreateModels(self.VElements) // create viewmodels
  51. self:CreateModels(self.WElements) // create worldmodels
  52.  
  53. // init view model bone build function
  54. if IsValid(self.Owner) then
  55. local vm = self.Owner:GetViewModel()
  56. if IsValid(vm) then
  57. self:ResetBonePositions(vm)
  58.  
  59. // Init viewmodel visibility
  60. if (self.ShowViewModel == nil or self.ShowViewModel) then
  61. vm:SetColor(Color(255,255,255,255))
  62. else
  63. // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
  64. vm:SetColor(Color(255,255,255,1))
  65. // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
  66. // 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
  67. vm:SetMaterial("Debug/hsv")
  68. end
  69. end
  70. end
  71.  
  72. end
  73. self:SetWeaponHoldType( self.HoldType )
  74.  
  75. end
  76.  
  77. function SWEP:PrimaryAttack()
  78.  
  79. self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 )
  80.  
  81. local trace = self.Owner:GetEyeTrace();
  82. if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
  83. self.Owner:SetAnimation( PLAYER_ATTACK1 );
  84. self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
  85. bullet = {}
  86. bullet.Num = 1
  87. bullet.Src = self.Owner:GetShootPos()
  88. bullet.Dir = self.Owner:GetAimVector()
  89. bullet.Spread = Vector(0, 0, 0)
  90. bullet.Tracer = 0
  91. bullet.Force = 1
  92. bullet.Damage = 25
  93. self.Owner:FireBullets(bullet)
  94. self.Weapon:EmitSound("Weapon_Crowbar.Melee_Hit")
  95. else
  96. self.Owner:SetAnimation( PLAYER_ATTACK1 );
  97. self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
  98. self.Weapon:EmitSound("Weapon_Crowbar.Single")
  99. end
  100.  
  101. self.Weapon:SetNextPrimaryFire( CurTime() + 0.43 )
  102. end
  103.  
  104. function SWEP:SecondaryAttack()
  105. self:PrimaryAttack()
  106. end
  107.  
  108. function SWEP:Holster()
  109.  
  110. if CLIENT and IsValid(self.Owner) then
  111. local vm = self.Owner:GetViewModel()
  112. if IsValid(vm) then
  113. self:ResetBonePositions(vm)
  114. end
  115. end
  116.  
  117. return true
  118. end
  119.  
  120. function SWEP:OnRemove()
  121. self:Holster()
  122. end
  123.  
  124. if CLIENT then
  125.  
  126. SWEP.vRenderOrder = nil
  127. function SWEP:ViewModelDrawn()
  128.  
  129. local vm = self.Owner:GetViewModel()
  130. if !IsValid(vm) then return end
  131.  
  132. if (!self.VElements) then return end
  133.  
  134. self:UpdateBonePositions(vm)
  135.  
  136. if (!self.vRenderOrder) then
  137.  
  138. // we build a render order because sprites need to be drawn after models
  139. self.vRenderOrder = {}
  140.  
  141. for k, v in pairs( self.VElements ) do
  142. if (v.type == "Model") then
  143. table.insert(self.vRenderOrder, 1, k)
  144. elseif (v.type == "Sprite" or v.type == "Quad") then
  145. table.insert(self.vRenderOrder, k)
  146. end
  147. end
  148.  
  149. end
  150.  
  151. for k, name in ipairs( self.vRenderOrder ) do
  152.  
  153. local v = self.VElements[name]
  154. if (!v) then self.vRenderOrder = nil break end
  155. if (v.hide) then continue end
  156.  
  157. local model = v.modelEnt
  158. local sprite = v.spriteMaterial
  159.  
  160. if (!v.bone) then continue end
  161.  
  162. local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
  163.  
  164. if (!pos) then continue end
  165.  
  166. if (v.type == "Model" and IsValid(model)) then
  167.  
  168. model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  169. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  170. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  171. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  172.  
  173. model:SetAngles(ang)
  174. //model:SetModelScale(v.size)
  175. local matrix = Matrix()
  176. matrix:Scale(v.size)
  177. model:EnableMatrix( "RenderMultiply", matrix )
  178.  
  179. if (v.material == "") then
  180. model:SetMaterial("")
  181. elseif (model:GetMaterial() != v.material) then
  182. model:SetMaterial( v.material )
  183. end
  184.  
  185. if (v.skin and v.skin != model:GetSkin()) then
  186. model:SetSkin(v.skin)
  187. end
  188.  
  189. if (v.bodygroup) then
  190. for k, v in pairs( v.bodygroup ) do
  191. if (model:GetBodygroup(k) != v) then
  192. model:SetBodygroup(k, v)
  193. end
  194. end
  195. end
  196.  
  197. if (v.surpresslightning) then
  198. render.SuppressEngineLighting(true)
  199. end
  200.  
  201. render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  202. render.SetBlend(v.color.a/255)
  203. model:DrawModel()
  204. render.SetBlend(1)
  205. render.SetColorModulation(1, 1, 1)
  206.  
  207. if (v.surpresslightning) then
  208. render.SuppressEngineLighting(false)
  209. end
  210.  
  211. elseif (v.type == "Sprite" and sprite) then
  212.  
  213. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  214. render.SetMaterial(sprite)
  215. render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  216.  
  217. elseif (v.type == "Quad" and v.draw_func) then
  218.  
  219. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  220. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  221. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  222. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  223.  
  224. cam.Start3D2D(drawpos, ang, v.size)
  225. v.draw_func( self )
  226. cam.End3D2D()
  227.  
  228. end
  229.  
  230. end
  231.  
  232. end
  233.  
  234. SWEP.wRenderOrder = nil
  235. function SWEP:DrawWorldModel()
  236.  
  237. if (self.ShowWorldModel == nil or self.ShowWorldModel) then
  238. self:DrawModel()
  239. end
  240.  
  241. if (!self.WElements) then return end
  242.  
  243. if (!self.wRenderOrder) then
  244.  
  245. self.wRenderOrder = {}
  246.  
  247. for k, v in pairs( self.WElements ) do
  248. if (v.type == "Model") then
  249. table.insert(self.wRenderOrder, 1, k)
  250. elseif (v.type == "Sprite" or v.type == "Quad") then
  251. table.insert(self.wRenderOrder, k)
  252. end
  253. end
  254.  
  255. end
  256.  
  257. if (IsValid(self.Owner)) then
  258. bone_ent = self.Owner
  259. else
  260. // when the weapon is dropped
  261. bone_ent = self
  262. end
  263.  
  264. for k, name in pairs( self.wRenderOrder ) do
  265.  
  266. local v = self.WElements[name]
  267. if (!v) then self.wRenderOrder = nil break end
  268. if (v.hide) then continue end
  269.  
  270. local pos, ang
  271.  
  272. if (v.bone) then
  273. pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
  274. else
  275. pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
  276. end
  277.  
  278. if (!pos) then continue end
  279.  
  280. local model = v.modelEnt
  281. local sprite = v.spriteMaterial
  282.  
  283. if (v.type == "Model" and IsValid(model)) then
  284.  
  285. model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  286. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  287. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  288. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  289.  
  290. model:SetAngles(ang)
  291. //model:SetModelScale(v.size)
  292. local matrix = Matrix()
  293. matrix:Scale(v.size)
  294. model:EnableMatrix( "RenderMultiply", matrix )
  295.  
  296. if (v.material == "") then
  297. model:SetMaterial("")
  298. elseif (model:GetMaterial() != v.material) then
  299. model:SetMaterial( v.material )
  300. end
  301.  
  302. if (v.skin and v.skin != model:GetSkin()) then
  303. model:SetSkin(v.skin)
  304. end
  305.  
  306. if (v.bodygroup) then
  307. for k, v in pairs( v.bodygroup ) do
  308. if (model:GetBodygroup(k) != v) then
  309. model:SetBodygroup(k, v)
  310. end
  311. end
  312. end
  313.  
  314. if (v.surpresslightning) then
  315. render.SuppressEngineLighting(true)
  316. end
  317.  
  318. render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  319. render.SetBlend(v.color.a/255)
  320. model:DrawModel()
  321. render.SetBlend(1)
  322. render.SetColorModulation(1, 1, 1)
  323.  
  324. if (v.surpresslightning) then
  325. render.SuppressEngineLighting(false)
  326. end
  327.  
  328. elseif (v.type == "Sprite" and sprite) then
  329.  
  330. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  331. render.SetMaterial(sprite)
  332. render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  333.  
  334. elseif (v.type == "Quad" and v.draw_func) then
  335.  
  336. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  337. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  338. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  339. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  340.  
  341. cam.Start3D2D(drawpos, ang, v.size)
  342. v.draw_func( self )
  343. cam.End3D2D()
  344.  
  345. end
  346.  
  347. end
  348.  
  349. end
  350.  
  351. function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
  352.  
  353. local bone, pos, ang
  354. if (tab.rel and tab.rel != "") then
  355.  
  356. local v = basetab[tab.rel]
  357.  
  358. if (!v) then return end
  359.  
  360. // Technically, if there exists an element with the same name as a bone
  361. // you can get in an infinite loop. Let's just hope nobody's that stupid.
  362. pos, ang = self:GetBoneOrientation( basetab, v, ent )
  363.  
  364. if (!pos) then return end
  365.  
  366. pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  367. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  368. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  369. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  370.  
  371. else
  372.  
  373. bone = ent:LookupBone(bone_override or tab.bone)
  374.  
  375. if (!bone) then return end
  376.  
  377. pos, ang = Vector(0,0,0), Angle(0,0,0)
  378. local m = ent:GetBoneMatrix(bone)
  379. if (m) then
  380. pos, ang = m:GetTranslation(), m:GetAngles()
  381. end
  382.  
  383. if (IsValid(self.Owner) and self.Owner:IsPlayer() and
  384. ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
  385. ang.r = -ang.r // Fixes mirrored models
  386. end
  387.  
  388. end
  389.  
  390. return pos, ang
  391. end
  392.  
  393. function SWEP:CreateModels( tab )
  394.  
  395. if (!tab) then return end
  396.  
  397. // Create the clientside models here because Garry says we can't do it in the render hook
  398. for k, v in pairs( tab ) do
  399. if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
  400. string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
  401.  
  402. v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
  403. if (IsValid(v.modelEnt)) then
  404. v.modelEnt:SetPos(self:GetPos())
  405. v.modelEnt:SetAngles(self:GetAngles())
  406. v.modelEnt:SetParent(self)
  407. v.modelEnt:SetNoDraw(true)
  408. v.createdModel = v.model
  409. else
  410. v.modelEnt = nil
  411. end
  412.  
  413. elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
  414. and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
  415.  
  416. local name = v.sprite.."-"
  417. local params = { ["$basetexture"] = v.sprite }
  418. // make sure we create a unique name based on the selected options
  419. local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
  420. for i, j in pairs( tocheck ) do
  421. if (v[j]) then
  422. params["$"..j] = 1
  423. name = name.."1"
  424. else
  425. name = name.."0"
  426. end
  427. end
  428.  
  429. v.createdSprite = v.sprite
  430. v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
  431.  
  432. end
  433. end
  434.  
  435. end
  436.  
  437. local allbones
  438. local hasGarryFixedBoneScalingYet = false
  439.  
  440. function SWEP:UpdateBonePositions(vm)
  441.  
  442. if self.ViewModelBoneMods then
  443.  
  444. if (!vm:GetBoneCount()) then return end
  445.  
  446. // !! WORKAROUND !! //
  447. // We need to check all model names :/
  448. local loopthrough = self.ViewModelBoneMods
  449. if (!hasGarryFixedBoneScalingYet) then
  450. allbones = {}
  451. for i=0, vm:GetBoneCount() do
  452. local bonename = vm:GetBoneName(i)
  453. if (self.ViewModelBoneMods[bonename]) then
  454. allbones[bonename] = self.ViewModelBoneMods[bonename]
  455. else
  456. allbones[bonename] = {
  457. scale = Vector(1,1,1),
  458. pos = Vector(0,0,0),
  459. angle = Angle(0,0,0)
  460. }
  461. end
  462. end
  463.  
  464. loopthrough = allbones
  465. end
  466. // !! ----------- !! //
  467.  
  468. for k, v in pairs( loopthrough ) do
  469. local bone = vm:LookupBone(k)
  470. if (!bone) then continue end
  471.  
  472. // !! WORKAROUND !! //
  473. local s = Vector(v.scale.x,v.scale.y,v.scale.z)
  474. local p = Vector(v.pos.x,v.pos.y,v.pos.z)
  475. local ms = Vector(1,1,1)
  476. if (!hasGarryFixedBoneScalingYet) then
  477. local cur = vm:GetBoneParent(bone)
  478. while(cur >= 0) do
  479. local pscale = loopthrough[vm:GetBoneName(cur)].scale
  480. ms = ms * pscale
  481. cur = vm:GetBoneParent(cur)
  482. end
  483. end
  484.  
  485. s = s * ms
  486. // !! ----------- !! //
  487.  
  488. if vm:GetManipulateBoneScale(bone) != s then
  489. vm:ManipulateBoneScale( bone, s )
  490. end
  491. if vm:GetManipulateBoneAngles(bone) != v.angle then
  492. vm:ManipulateBoneAngles( bone, v.angle )
  493. end
  494. if vm:GetManipulateBonePosition(bone) != p then
  495. vm:ManipulateBonePosition( bone, p )
  496. end
  497. end
  498. else
  499. self:ResetBonePositions(vm)
  500. end
  501.  
  502. end
  503.  
  504. function SWEP:ResetBonePositions(vm)
  505.  
  506. if (!vm:GetBoneCount()) then return end
  507. for i=0, vm:GetBoneCount() do
  508. vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
  509. vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
  510. vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
  511. end
  512.  
  513. end
  514.  
  515. /**************************
  516. Global utility code
  517. **************************/
  518.  
  519. // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
  520. // Does not copy entities of course, only copies their reference.
  521. // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
  522. function table.FullCopy( tab )
  523.  
  524. if (!tab) then return nil end
  525.  
  526. local res = {}
  527. for k, v in pairs( tab ) do
  528. if (type(v) == "table") then
  529. res[k] = table.FullCopy(v) // recursion ho!
  530. elseif (type(v) == "Vector") then
  531. res[k] = Vector(v.x, v.y, v.z)
  532. elseif (type(v) == "Angle") then
  533. res[k] = Angle(v.p, v.y, v.r)
  534. else
  535. res[k] = v
  536. end
  537. end
  538.  
  539. return res
  540.  
  541. end
  542.  
  543. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement