Advertisement
Auxiliare

A swep

Oct 23rd, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.48 KB | None | 0 0
  1. /**************************************************
  2. SWEP Construction Kit base code
  3. Created by Clavus
  4. Available for public use, thread at:
  5. facepunch.com/threads/1032378
  6. **************************************************/
  7.  
  8. function SWEP:Initialize()
  9.  
  10. // other initialize code goes here
  11.  
  12. if CLIENT then
  13.  
  14. self:CreateModels(self.VElements) // create viewmodels
  15. self:CreateModels(self.WElements) // create worldmodels
  16.  
  17. // init view model bone build function
  18. self.BuildViewModelBones = function( s )
  19. if LocalPlayer():GetActiveWeapon() == self and self.ViewModelBoneMods then
  20. for k, v in pairs( self.ViewModelBoneMods ) do
  21. local bone = s:LookupBone(k)
  22. if (!bone) then continue end
  23. local m = s:GetBoneMatrix(bone)
  24. if (!m) then continue end
  25. m:Scale(v.scale)
  26. m:Rotate(v.angle)
  27. m:Translate(v.pos)
  28. s:SetBoneMatrix(bone, m)
  29. end
  30. end
  31. end
  32.  
  33. end
  34.  
  35. end
  36.  
  37.  
  38. function SWEP:OnRemove()
  39.  
  40. // other onremove code goes here
  41.  
  42. if CLIENT then
  43. self:RemoveModels()
  44. end
  45.  
  46. end
  47.  
  48.  
  49. if CLIENT then
  50.  
  51. SWEP.vRenderOrder = nil
  52. function SWEP:ViewModelDrawn()
  53.  
  54. local vm = self.Owner:GetViewModel()
  55. if !ValidEntity(vm) then return end
  56.  
  57. if (!self.VElements) then return end
  58.  
  59. if vm.BuildBonePositions ~= self.BuildViewModelBones then
  60. vm.BuildBonePositions = self.BuildViewModelBones
  61. end
  62.  
  63. if (self.ShowViewModel == nil or self.ShowViewModel) then
  64. vm:SetColor(255,255,255,255)
  65. else
  66. // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
  67. vm:SetColor(255,255,255,1)
  68. end
  69.  
  70. if (!self.vRenderOrder) then
  71.  
  72. // we build a render order because sprites need to be drawn after models
  73. self.vRenderOrder = {}
  74.  
  75. for k, v in pairs( self.VElements ) do
  76. if (v.type == "Model") then
  77. table.insert(self.vRenderOrder, 1, k)
  78. elseif (v.type == "Sprite" or v.type == "Quad") then
  79. table.insert(self.vRenderOrder, k)
  80. end
  81. end
  82.  
  83. end
  84.  
  85. for k, name in ipairs( self.vRenderOrder ) do
  86.  
  87. local v = self.VElements[name]
  88. if (!v) then self.vRenderOrder = nil break end
  89.  
  90. local model = v.modelEnt
  91. local sprite = v.spriteMaterial
  92.  
  93. if (!v.bone) then continue end
  94.  
  95. local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
  96.  
  97. if (!pos) then continue end
  98.  
  99. if (v.type == "Model" and ValidEntity(model)) then
  100.  
  101. model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  102. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  103. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  104. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  105.  
  106. model:SetAngles(ang)
  107. model:SetModelScale(v.size)
  108.  
  109. if (v.material == "") then
  110. model:SetMaterial("")
  111. elseif (model:GetMaterial() != v.material) then
  112. model:SetMaterial( v.material )
  113. end
  114.  
  115. if (v.skin and v.skin != model:GetSkin()) then
  116. model:SetSkin(v.skin)
  117. end
  118.  
  119. if (v.bodygroup) then
  120. for k, v in pairs( v.bodygroup ) do
  121. if (model:GetBodygroup(k) != v) then
  122. model:SetBodygroup(k, v)
  123. end
  124. end
  125. end
  126.  
  127. if (v.surpresslightning) then
  128. render.SuppressEngineLighting(true)
  129. end
  130.  
  131. render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  132. render.SetBlend(v.color.a/255)
  133. model:DrawModel()
  134. render.SetBlend(1)
  135. render.SetColorModulation(1, 1, 1)
  136.  
  137. if (v.surpresslightning) then
  138. render.SuppressEngineLighting(false)
  139. end
  140.  
  141. elseif (v.type == "Sprite" and sprite) then
  142.  
  143. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  144. render.SetMaterial(sprite)
  145. render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  146.  
  147. elseif (v.type == "Quad" and v.draw_func) then
  148.  
  149. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  150. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  151. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  152. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  153.  
  154. cam.Start3D2D(drawpos, ang, v.size)
  155. v.draw_func( self )
  156. cam.End3D2D()
  157.  
  158. end
  159.  
  160. end
  161.  
  162. end
  163.  
  164. SWEP.wRenderOrder = nil
  165. function SWEP:DrawWorldModel()
  166.  
  167. if (self.ShowWorldModel == nil or self.ShowWorldModel) then
  168. self:DrawModel()
  169. end
  170.  
  171. if (!self.WElements) then return end
  172.  
  173. if (!self.wRenderOrder) then
  174.  
  175. self.wRenderOrder = {}
  176.  
  177. for k, v in pairs( self.WElements ) do
  178. if (v.type == "Model") then
  179. table.insert(self.wRenderOrder, 1, k)
  180. elseif (v.type == "Sprite" or v.type == "Quad") then
  181. table.insert(self.wRenderOrder, k)
  182. end
  183. end
  184.  
  185. end
  186.  
  187. if (ValidEntity(self.Owner)) then
  188. bone_ent = self.Owner
  189. else
  190. // when the weapon is dropped
  191. bone_ent = self
  192. end
  193.  
  194. for k, name in pairs( self.wRenderOrder ) do
  195.  
  196. local v = self.WElements[name]
  197. if (!v) then self.wRenderOrder = nil break end
  198.  
  199. local pos, ang
  200.  
  201. if (v.bone) then
  202. pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
  203. else
  204. pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
  205. end
  206.  
  207. if (!pos) then continue end
  208.  
  209. local model = v.modelEnt
  210. local sprite = v.spriteMaterial
  211.  
  212. if (v.type == "Model" and ValidEntity(model)) then
  213.  
  214. model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
  215. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  216. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  217. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  218.  
  219. model:SetAngles(ang)
  220. model:SetModelScale(v.size)
  221.  
  222. if (v.material == "") then
  223. model:SetMaterial("")
  224. elseif (model:GetMaterial() != v.material) then
  225. model:SetMaterial( v.material )
  226. end
  227.  
  228. if (v.skin and v.skin != model:GetSkin()) then
  229. model:SetSkin(v.skin)
  230. end
  231.  
  232. if (v.bodygroup) then
  233. for k, v in pairs( v.bodygroup ) do
  234. if (model:GetBodygroup(k) != v) then
  235. model:SetBodygroup(k, v)
  236. end
  237. end
  238. end
  239.  
  240. if (v.surpresslightning) then
  241. render.SuppressEngineLighting(true)
  242. end
  243.  
  244. render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
  245. render.SetBlend(v.color.a/255)
  246. model:DrawModel()
  247. render.SetBlend(1)
  248. render.SetColorModulation(1, 1, 1)
  249.  
  250. if (v.surpresslightning) then
  251. render.SuppressEngineLighting(false)
  252. end
  253.  
  254. elseif (v.type == "Sprite" and sprite) then
  255.  
  256. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  257. render.SetMaterial(sprite)
  258. render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
  259.  
  260. elseif (v.type == "Quad" and v.draw_func) then
  261.  
  262. local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  263. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  264. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  265. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  266.  
  267. cam.Start3D2D(drawpos, ang, v.size)
  268. v.draw_func( self )
  269. cam.End3D2D()
  270.  
  271. end
  272.  
  273. end
  274.  
  275. end
  276.  
  277. function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
  278.  
  279. local bone, pos, ang
  280. if (tab.rel and tab.rel != "") then
  281.  
  282. local v = basetab[tab.rel]
  283.  
  284. if (!v) then return end
  285.  
  286. // Technically, if there exists an element with the same name as a bone
  287. // you can get in an infinite loop. Let's just hope nobody's that stupid.
  288. pos, ang = self:GetBoneOrientation( basetab, v, ent )
  289.  
  290. if (!pos) then return end
  291.  
  292. pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
  293. ang:RotateAroundAxis(ang:Up(), v.angle.y)
  294. ang:RotateAroundAxis(ang:Right(), v.angle.p)
  295. ang:RotateAroundAxis(ang:Forward(), v.angle.r)
  296.  
  297. else
  298.  
  299. bone = ent:LookupBone(bone_override or tab.bone)
  300.  
  301. if (!bone) then return end
  302.  
  303. pos, ang = Vector(0,0,0), Angle(0,0,0)
  304. local m = ent:GetBoneMatrix(bone)
  305. if (m) then
  306. pos, ang = m:GetTranslation(), m:GetAngle()
  307. end
  308.  
  309. if (ValidEntity(self.Owner) and self.Owner:IsPlayer() and
  310. ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
  311. ang.r = -ang.r // Fixes mirrored models
  312. end
  313.  
  314. end
  315.  
  316. return pos, ang
  317. end
  318.  
  319. function SWEP:CreateModels( tab )
  320.  
  321. if (!tab) then return end
  322.  
  323. // Create the clientside models here because Garry says we can't do it in the render hook
  324. for k, v in pairs( tab ) do
  325. if (v.type == "Model" and v.model and v.model != "" and (!ValidEntity(v.modelEnt) or v.createdModel != v.model) and
  326. string.find(v.model, ".mdl") and file.Exists ("../"..v.model) ) then
  327.  
  328. v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
  329. if (ValidEntity(v.modelEnt)) then
  330. v.modelEnt:SetPos(self:GetPos())
  331. v.modelEnt:SetAngles(self:GetAngles())
  332. v.modelEnt:SetParent(self)
  333. v.modelEnt:SetNoDraw(true)
  334. v.createdModel = v.model
  335. else
  336. v.modelEnt = nil
  337. end
  338.  
  339. elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
  340. and file.Exists ("../materials/"..v.sprite..".vmt")) then
  341.  
  342. local name = v.sprite.."-"
  343. local params = { ["$basetexture"] = v.sprite }
  344. // make sure we create a unique name based on the selected options
  345. local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
  346. for i, j in pairs( tocheck ) do
  347. if (v[j]) then
  348. params["$"..j] = 1
  349. name = name.."1"
  350. else
  351. name = name.."0"
  352. end
  353. end
  354.  
  355. v.createdSprite = v.sprite
  356. v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
  357.  
  358. end
  359. end
  360.  
  361. end
  362.  
  363. function SWEP:OnRemove()
  364. self:RemoveModels()
  365. end
  366.  
  367. function SWEP:RemoveModels()
  368. if (self.VElements) then
  369. for k, v in pairs( self.VElements ) do
  370. if (ValidEntity( v.modelEnt )) then v.modelEnt:Remove() end
  371. end
  372. end
  373. if (self.WElements) then
  374. for k, v in pairs( self.WElements ) do
  375. if (ValidEntity( v.modelEnt )) then v.modelEnt:Remove() end
  376. end
  377. end
  378. self.VElements = nil
  379. self.WElements = nil
  380. end
  381. SWEP.ViewModelBoneMods = {
  382. ["bip_lowerArm_L"] = { scale = Vector(1, 1, 1), pos = Vector(0, 30, 0), angle = Angle(0, 0, 0) }
  383. }
  384. SWEP.VElements = {
  385. ["qqqqwqw"] = { type = "Model", model = "models/weapons/w_smg_mac10.mdl", bone = "Gun", rel = "", pos = Vector(-0.419, -15.176, -1.719), angle = Angle(89.8, -95.638, 7.83), size = Vector(0.5, 0.5, 0.5), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
  386. ["wqfdsda"] = { type = "Model", model = "models/props_c17/oildrum001_explosive.mdl", bone = "Gun", rel = "", pos = Vector(0, -8.245, 0), angle = Angle(0, 2.381, -90.47), size = Vector(0.05, 0.05, 0.168), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
  387. }
  388. end
  389.  
  390. //General Settings \\
  391. SWEP.AdminSpawnable = true // Is the swep spawnable for admin
  392. SWEP.HoldType = "pistol"
  393. SWEP.ViewModelFOV = 50
  394. SWEP.ViewModelFlip = false
  395. SWEP.ViewModel = "models/weapons/v_models/v_distol_scout.mdl"
  396. SWEP.WorldModel = "models/weapons/w_models/w_distol.mdl"
  397. SWEP.ShowViewModel = true
  398. SWEP.ShowWorldModel = true
  399.  
  400. SWEP.AutoSwitchTo = false // when someone walks over the swep, chould i automatectly change to your swep ?
  401. SWEP.Slot = 0 // Deside wich slot you want your swep do be in 1 2 3 4 5 6
  402. SWEP.PrintName = "Custom1" // your sweps name
  403. SWEP.Author = "Auxiliare" // Your name
  404. SWEP.Spawnable = true // Can everybody spawn this swep ? - If you want only admin keep this false and adminsapwnable true.
  405. SWEP.AutoSwitchFrom = false // Does the weapon get changed by other sweps if you pick them up ?
  406. SWEP.FiresUnderwater = false // Does your swep fire under water ?
  407. SWEP.Weight = 5 // Chose the weight of the Swep
  408. SWEP.DrawCrosshair = true // Do you want it to have a crosshair ?
  409. SWEP.Category = "SwepCatogory" // Make your own catogory for the swep
  410. SWEP.SlotPos = 0 // Deside wich slot you want your swep do be in 1 2 3 4 5 6
  411. SWEP.DrawAmmo = true // Does the ammo show up when you are using it ? True / False
  412. SWEP.ReloadSound = "Weapon_Pistol.Reload" // Reload sound, you can use the default ones, or you can use your one; Example; "sound/myswepreload.waw"
  413. SWEP.Instructions = "SwepInstructions" // How do pepole use your swep ?
  414. SWEP.Contact = "YourMailAdress" // How Pepole chould contact you if they find bugs, errors, etc
  415. SWEP.Purpose = "WhatsThePurposeOfThisSwep" // What is the purpose with this swep ?
  416. SWEP.base = "weapon_base"
  417. //General settings\\
  418.  
  419. //PrimaryFire Settings\\
  420. SWEP.Primary.Sound = "Weapon_Pistol.Single" // The sound that plays when you shoot :]
  421. SWEP.Primary.Damage = 10 // How much damage the swep is doing
  422. SWEP.Primary.TakeAmmo = 1 // How much ammo does it take for each shot ?
  423. SWEP.Primary.ClipSize = 16 // The clipsize
  424. SWEP.Primary.Ammo = "Pistol" // ammmo type pistol/ smg1
  425. SWEP.Primary.DefaultClip = 32 // How much ammo does the swep come with `?
  426. SWEP.Primary.Spread = 0.1 // Does the bullets spread all over, if you want it fire exactly where you are aiming leave it o.1
  427. SWEP.Primary.NumberofShots = 1 // How many bullets you are firing each shot.
  428. SWEP.Primary.Automatic = false // Is the swep automatic ?
  429. SWEP.Primary.Recoil = 1 // How much we should punch the view
  430. SWEP.Primary.Delay = 0.1 // How long time before you can fire again
  431. SWEP.Primary.Force = 10 // The force of the shot
  432. //PrimaryFire settings\\
  433. SWEP.IronSightsPos = Vector(-7.381, 0, -3.77)
  434. SWEP.IronSightsAng = Vector(2.868, 0.574, -70)
  435. //Secondary Fire Variables\\
  436. SWEP.Secondary.NumberofShots = 1 // How many explosions for each shot
  437. SWEP.Secondary.Force = 10 // Explosions Force
  438. SWEP.Secondary.Spread = 0.1 // How much the explosion does spread
  439. SWEP.Secondary.Sound = "Weapon_RPG.Single" // Fire sound
  440. SWEP.Secondary.DefaultClip = 32 // How much ammo the secoindary swep comes with
  441. SWEP.Secondary.Automatic = false // Is it automactic ?
  442. SWEP.Secondary.Ammo = "Pistol" // Leave as Pistol !
  443. SWEP.Secondary.Recoil = 1 // How uch we should punch the view
  444. SWEP.Secondary.Delay = 0.2 // How long you have to wait before fire a new shot
  445. SWEP.Secondary.TakeAmmo = 1 // How much ammo Does it take ?
  446. SWEP.Secondary.ClipSize = 16 // ClipSize
  447. SWEP.Secondary.Damage = 10 -- The damage the explosion does.
  448. SWEP.Secondary.Magnitude = "175" -- How big its the explosion ?
  449. //Secondary Fire Variables\\
  450.  
  451.  
  452.  
  453. //SWEP:PrimaryFire()\\
  454. function SWEP:PrimaryAttack()
  455. if ( !self:CanPrimaryAttack() ) then return end
  456. local bullet = {}
  457. bullet.Num = self.Primary.NumberofShots
  458. bullet.Src = self.Owner:GetShootPos()
  459. bullet.Dir = self.Owner:GetAimVector()
  460. bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0)
  461. bullet.Tracer = 0
  462. bullet.Force = self.Primary.Force
  463. bullet.Damage = self.Primary.Damage
  464. bullet.AmmoType = self.Primary.Ammo
  465. local rnda = self.Primary.Recoil * -1
  466. local rndb = self.Primary.Recoil * math.random(-1, 1)
  467. self:ShootEffects()
  468. self.Owner:FireBullets( bullet )
  469. self.Weapon:EmitSound(Sound(self.Primary.Sound))
  470. self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) )
  471. self:TakePrimaryAmmo(self.Primary.TakeAmmo)
  472. self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
  473. self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
  474. end
  475. //SWEP:PrimaryFire()\\
  476.  
  477. //SWEP:SecondaryFire()\\
  478. function SWEP:SecondaryAttack()
  479. if ( !self:CanSecondaryAttack() ) then return end
  480. local rnda = -self.Secondary.Recoil
  481. local rndb = self.Secondary.Recoil * math.random(-1, 1)
  482. self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) )
  483. local eyetrace = self.Owner:GetEyeTrace()
  484. self.Weapon:EmitSound ( self.Secondary.Sound )
  485. self:ShootEffects()
  486. local explode = ents.Create("env_explosion")
  487. explode:SetPos( eyetrace.HitPos )
  488. explode:SetOwner( self.Owner )
  489. explode:Spawn()
  490. explode:SetKeyValue("iMagnitude","175")
  491. explode:Fire("Explode", 0, 0 )
  492. explode:EmitSound("weapon_AWP.Single", 400, 400 )
  493. self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
  494. self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
  495. self:TakePrimaryAmmo(self.Secondary.TakeAmmo)
  496. end
  497. //SWEP:SecondaryFire()\\
  498.  
  499.  
  500.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement