//ALL Credit goes to Big Black for making this base, it's very simple and very easy to use!

//...since you can read this, I'm assuming you've extracted the .gma

//in that case, have fun with it. You'll see comments from time to time describing features.

//Look for comments!

//And have fun with RUBY ROSE'S SCYTHE!



function SWEP:IsEquipment()

return WEPS.IsEquipment(self)

end

function SWEP:OnDrop()

	self:Remove()

end

SWEP.Base = "weapon_tttbase"

SWEP.ViewModelFOV	= 60

SWEP.ViewModelFlip	= false

SWEP.Spawnable			= false

SWEP.AdminSpawnable		= true

SWEP.Weight			= 5

SWEP.Kind = WEAPON_MELEE

SWEP.WeaponID = AMMO_CROWBAR

SWEP.AllowDrop = false

SWEP.AllowDelete = true



if (SERVER) then



	AddCSLuaFile( "shared.lua" )

	SWEP.Weight				= 5

	SWEP.AutoSwitchTo			= false

	SWEP.AutoSwitchFrom		= false



	local ActIndex = {}

		ActIndex[ "knife" ]		= ACT_HL2MP_IDLE_KNIFE	//I wouldn't touch this.



	function SWEP:SetWeaponHoldType( t )	//Or this.



		local index 								= ActIndex[ t ]	//Or any of this. Okay you get the point.

			

		if (index == nil) then

			return

		end



		self.ActivityTranslate 							= {}

		self.ActivityTranslate [ ACT_HL2MP_IDLE ] 			= index

		self.ActivityTranslate [ ACT_HL2MP_WALK ] 			= index + 1

		self.ActivityTranslate [ ACT_HL2MP_RUN ] 				= index + 2

		self.ActivityTranslate [ ACT_HL2MP_IDLE_CROUCH ] 		= index + 3

		self.ActivityTranslate [ ACT_HL2MP_WALK_CROUCH ] 		= index + 4

		self.ActivityTranslate [ ACT_HL2MP_GESTURE_RANGE_ATTACK ] 	= index + 5

		self.ActivityTranslate [ ACT_HL2MP_GESTURE_RELOAD ] 		= index + 6

		self.ActivityTranslate [ ACT_HL2MP_JUMP ] 			= index + 7

		self.ActivityTranslate [ ACT_RANGE_ATTACK1 ] 			= index + 8

	//Because we need animations

		self:SetupWeaponHoldTypeForAI( t )

	end

end



if ( CLIENT ) then

	SWEP.PrintName			= "Crescent Rose"	//Name of the SWEP

	SWEP.DrawAmmo 			= false	//Knives don't use ammo!

	SWEP.DrawCrosshair 		= false	//Because we already got one.

	SWEP.ViewModelFOV		= 60	//Field of View, lower is closer, higher is farther.

	SWEP.ViewModelFlip		= false //Flip the model? Generally used for CS:S models but that's obsolete now.

	SWEP.CSMuzzleFlashes	= false	//Does a knife have a gun barrel? I think not.

	

	SWEP.Slot				= 1	//Where does this weapon go? Where the crowbar goes, that's where!

	SWEP.SlotPos			= 1	//Place it at slot 1, if possible.

	SWEP.IconLetter		= "j"	//What letter this uses for a killicon. Don't touch this, unless you know what letters are bound to what guns.



	killicon.AddFont("weapon_rubyrose_scythe", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ))	//Sexy KillIcon. Too bad I don't know how to make a custom one work.

	surface.CreateFont("CSKillIcons", {font = "csd", size = ScreenScale(30), weight = 500, antialias = true, additive = true})

	surface.CreateFont("CSSelectIcons", {font = "csd", size = ScreenScale(60), weight = 500, antialias = true, additive = true})

end

SWEP.Category				= "RWBY" //Where the SWEP is in the spawn menu.



SWEP.HoldType			= "melee2"	//How we hold this weapon.



SWEP.Spawnable				= true	//Can anyone spawn it?

SWEP.AdminSpawnable			= true	//Can admins only spawn it?



SWEP.ViewModel 				= "models/weapons/rubyscythe.mdl" //The model shown on your screen in first person.

SWEP.WorldModel 			= "models/weapons/rubyscythe.mdl" //The model everyone else sees on screen. No point changing this, the model is replaced on obtaining this weapon.

SWEP.UseHands				= true	//Use C Models, basically hands that look like your model's hands. Custom models require custom hands.

SWEP.ShowViewModel = true	//Show the view model? KEEP THIS TRUE AT ALL TIMES, IF DISABLED, ON WEAPON SWITCH ALL VIEWMODELS, INCLUDING EVERY GUN, WILL BE INVISIBLE!!

SWEP.ShowWorldModel = false	//Show the world model?

SWEP.Weight					= 5	//How tough this weapon is, for auto weapon switching. No point enabling this, autoswitch to and from is disabled.

SWEP.AutoSwitchTo				= false	//Switch to this weapon if weight is higher then the one we have in hand?

SWEP.AutoSwitchFrom			= false	//Allow other weapons to auto switch this gun, if it's weight is higher?



SWEP.Primary.ClipSize			= -1	//This is a knife. No need for ammo.

SWEP.Primary.Damage			= 25	//Don't touch this, instead goto line 144.

SWEP.Primary.DefaultClip		= -1	//This is a knife, how could it hold clips?

SWEP.Primary.Automatic			= true	//Instead of clicking like a maniac, you can hold the mouse button.

SWEP.Primary.Ammo			="none"	//Once again, this is a knife...



SWEP.WElements = {

	["element_name"] = { type = "Model", model = "models/rubyscythe.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.596, 0, 0), angle = Angle(180, -180, 0), size = Vector(1.149, 1.149, 1.149), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }

}

//WElements puts the scythe on the world model, so others see it.

SWEP.VElements = {

	["scythe"] = { type = "Model", model = "models/rubyscythe.mdl", bone = "v_weapon.Knife_Handle", rel = "", pos = Vector(-0.151, -1, -1), angle = Angle(0, -90, 0), size = Vector(0.699, 0.699, 0.699), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }

}

//VElements puts the scythe on the view model, so you can see it.



SWEP.Secondary.ClipSize			= -1 //These are all obsolete, so don't fiddle with these.

SWEP.Secondary.DefaultClip		= -1

SWEP.Secondary.Damage			= 0

SWEP.Secondary.Automatic		= false



SWEP.MissSound 				= Sound("weapons/knife/knife_slash1.wav") //Play this sound when we miss.

SWEP.WallSound 				= Sound("weapons/knife/knife_hitwall1.wav")	//Play this sound when we hit a wall. No, knives can't break down walls, silly.

SWEP.DeploySound				= Sound("weapons/knife/knife_deploy1.wav")	//Play this sound when we take out this bad boy!





/*---------------------------------------------------------

Think does nothing

---------------------------------------------------------*/

function SWEP:Think()

end



/*---------------------------------------------------------

Deploy plays the animation and draw sound

---------------------------------------------------------*/

function SWEP:Deploy()

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )

	self.Weapon:SetNextPrimaryFire(CurTime() + 1)	//Delay using this weapon for one second so we can play the animation.

	self.Weapon:EmitSound( self.DeploySound, 50, 100 )

	return true

end



/*---------------------------------------------------------

PrimaryAttack activated when we attack!

---------------------------------------------------------*/

function SWEP:PrimaryAttack()



	vm = self.Owner:GetViewModel()	//This is in place so we can always do a slashing animation. Do you want to hit an enemy with the back side of your scythe? I don't think so.



	local tr = {}

	tr.start = self.Owner:GetShootPos()

	tr.endpos = self.Owner:GetShootPos() + ( self.Owner:GetAimVector() * 90 ) //90 is our range, higher means farther, lower is closer.

	tr.filter = self.Owner

	tr.mask = MASK_SHOT

	local trace = util.TraceLine( tr )



	self.Weapon:SetNextPrimaryFire(CurTime() + 0.70)	//Add delay when we swing. If this wasn't in place, we'd swing 64 times a second, one per tick!

	self.Owner:SetAnimation( PLAYER_ATTACK1 ) //Plays the attack animation on the player's thirdperson model.



	if ( trace.Hit ) then



		if trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(),"npc") or string.find(trace.Entity:GetClass(),"prop_ragdoll") then	//Find what we're hitting. If it matches any of these, run this snippet of code.

			self.Weapon:SendWeaponAnim( ACT_VM_IDLE )	//Act all idle and stuff.

			vm:SetSequence(vm:LookupSequence("midslash2"))	//Slash at it!

			bullet = {}

			bullet.Num    = 1

			bullet.Src    = self.Owner:GetShootPos()

			bullet.Dir    = self.Owner:GetAimVector()

			bullet.Spread = Vector(0, 0, 0)

			bullet.Tracer = 0

			bullet.Force  = 1

			bullet.Damage = 95 //Damage it!

			

			self.Owner:FireBullets(bullet) 

			self.Weapon:EmitSound( "weapons/knife/knife_hit" .. math.random(1, 4) .. ".wav" )	//Play the slash sound!

		else

			self.Weapon:SendWeaponAnim( ACT_VM_IDLE )

			vm:SetSequence(vm:LookupSequence("midslash2"))

			bullet = {}

			bullet.Num    = 1

			bullet.Src    = self.Owner:GetShootPos()

			bullet.Dir    = self.Owner:GetAimVector()

			bullet.Spread = Vector(0, 0, 0)

			bullet.Tracer = 0

			bullet.Force  = 1

			bullet.Damage = 95

			self.Owner:FireBullets(bullet) 

//			self.Weapon:Emitd the mouse button.

end

end

end

SWEP.Primary.Ammo			="none"	//Once again, this is a knife...



SWEP.WElements = {

	["element_name"] = { type = "Model", model = "models/rubyscythe.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.596, 0, 0), angle = Angle(180, -180, 0), size = Vector(1.149, 1.149, 1.149), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }

}

//WElements puts the scythe on the world model, so others see it.

SWEP.VElements = {

	["scythe"] = { type = "Model", model = "models/rubyscythe.mdl", bone = "v_weapon.Knife_Handle", rel = "", pos = Vector(-0.151, -1, -1), angle = Angle(0, -90, 0), size = Vector(0.699, 0.699, 0.699), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }

}

//VElements puts the scythe on the view model, so you can see it.



SWEP.Secondary.ClipSize			= -1 //These are all obsolete, so don't fiddle with these.

SWEP.Secondary.DefaultClip		= -1

SWEP.Secondary.Damage			= 75

SWEP.Secondary.Automatic		= false



SWEP.MissSound 				= Sound("weapons/knife/knife_slash1.wav") //Play this sound when we miss.

SWEP.WallSound 				= Sound("weapons/knife/knife_hitwall1.wav")	//Play this sound when we hit a wall. No, knives can't break down walls, silly.

SWEP.DeploySound				= Sound("weapons/knife/knife_deploy1.wav")	//Play this sound when we take out this bad boy!





/*---------------------------------------------------------

Think does nothing

---------------------------------------------------------*/

function SWEP:Think()

end



/*---------------------------------------------------------

Deploy plays the animation and draw sound

---------------------------------------------------------*/

function SWEP:Deploy()

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )

	self.Weapon:SetNextPrimaryFire(CurTime() + 1)	//Delay using this weapon for one second so we can play the animation.

	self.Weapon:EmitSound( self.DeploySound, 50, 100 )

	return true

end



/*---------------------------------------------------------

PrimaryAttack activated when we attack!

---------------------------------------------------------*/

function SWEP:PrimaryAttack()



	vm = self.Owner:GetViewModel()	//This is in place so we can always do a slashing animation. Do you want to hit an enemy with the back side of your scythe? I don't think so.



	local tr = {}

	tr.start = self.Owner:GetShootPos()

	tr.endpos = self.Owner:GetShootPos() + ( self.Owner:GetAimVector() * 90 ) //90 is our range, higher means farther, lower is closer.

	tr.filter = self.Owner

	tr.mask = MASK_SHOT

	local trace = util.TraceLine( tr )



	self.Weapon:SetNextPrimaryFire(CurTime() + 0.70)	//Add delay when we swing. If this wasn't in place, we'd swing 64 times a second, one per tick!

	self.Owner:SetAnimation( PLAYER_ATTACK1 ) //Plays the attack animation on the player's thirdperson model.



	if ( trace.Hit ) then



		if trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(),"npc") or string.find(trace.Entity:GetClass(),"prop_ragdoll") then	//Find what we're hitting. If it matches any of these, run this snippet of code.

			self.Weapon:SendWeaponAnim( ACT_VM_IDLE )	//Act all idle and stuff.

			vm:SetSequence(vm:LookupSequence("midslash2"))	//Slash at it!

			bullet = {}

			bullet.Num    = 1

			bullet.Src    = self.Owner:GetShootPos()

			bullet.Dir    = self.Owner:GetAimVector()

			bullet.Spread = Vector(0, 0, 0)

			bullet.Tracer = 0

			bullet.Force  = 1

			bullet.Damage = 95 //Damage it!

			

			self.Owner:FireBullets(bullet) 

			self.Weapon:EmitSound( "weapons/knife/knife_hit" .. math.random(1, 4) .. ".wav" )	//Play the slash sound!

		else

			self.Weapon:SendWeaponAnim( ACT_VM_IDLE )

			vm:SetSequence(vm:LookupSequence("midslash2"))

			bullet = {}

			bullet.Num    = 1

			bullet.Src    = self.Owner:GetShootPos()

			bullet.Dir    = self.Owner:GetAimVector()

			bullet.Spread = Vector(0, 0, 0)

			bullet.Tracer = 0

			bullet.Force  = 1

			bullet.Damage = 95

			self.Owner:FireBullets(bullet) 

			self.Weapon:EmitSound( self.WallSound )		

			util.Decal("ManhackCut", trace.HitPos + trace.HitNormal, trace.HitPos - trace.HitNormal)	//Paint a nice cut on where we slashed at.

		end

	else

		self.Weapon:EmitSound(self.MissSound,100,math.random(90,120))	//Play a miss sound.

		self.Weapon:SendWeaponAnim( ACT_VM_IDLE )

			vm:SetSequence(vm:LookupSequence("midslash2"))	//Always do the slashing animation when attacking, makes it so we don't hit it with the butt of our scythe!

	end

end



/*---------------------------------------------------------

SecondaryAttack Does nothing

---------------------------------------------------------*/

function SWEP:SecondaryAttack()



	end

/*---------------------------------------------------------

Reload Does nothing

---------------------------------------------------------*/

function SWEP:Reload()



	return false

end



/*---------------------------------------------------------

OnRemove

---------------------------------------------------------*/

function SWEP:OnRemove()



return true

end



/*---------------------------------------------------------

Holster

---------------------------------------------------------*/

function SWEP:Holster()



	return true

end



/*---------------------------------------------------------

ShootEffects

---------------------------------------------------------*/

function SWEP:ShootEffects()



end



local IRONSIGHT_TIME = 0.15





/*---------------------------------------------------------

DrawWeaponSelection shows the weapon icon when we highlight it.

---------------------------------------------------------*/

function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)



	draw.SimpleText(self.IconLetter, "CSSelectIcons", x + wide / 2, y + tall * 0.2, Color(255, 210, 0, 255), TEXT_ALIGN_CENTER)

	-- Draw a CS:S select icon





end



/*---------------------------------------------------------

	DrawHUD

	

	Just a rough mock up showing how to draw your own crosshair.

	

---------------------------------------------------------*/

function SWEP:DrawHUD()





	local x, y



	// If we're drawing the local player, draw the crosshair where they're aiming,

	// instead of in the center of the screen.

	if ( self.Owner == LocalPlayer() && self.Owner:ShouldDrawLocalPlayer() ) then



		local tr = util.GetPlayerTrace( self.Owner )

		tr.mask = bit.bor( CONTENTS_SOLID,CONTENTS_MOVEABLE,CONTENTS_MONSTER,CONTENTS_WINDOW,CONTENTS_DEBRIS,CONTENTS_GRATE,CONTENTS_AUX )

		local trace = util.TraceLine( tr )

		

		local coords = trace.HitPos:ToScreen()

		x, y = coords.x, coords.y



	else

		x, y = ScrW() / 2.0, ScrH() / 2.0

	end

	

	local scale = 1 

	

	// Scale the size of the crosshair according to how long ago we fired our weapon

	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )

	scale = scale * (2 - math.Clamp( (CurTime() - LastShootTime) * 5, 0.0, 1.0 ))



	surface.SetDrawColor( 0, 255, 0, 255 )

	

	

	// Draw an awesome crosshair

	local gap = scale

	local length = gap + 6 * scale

	surface.DrawLine( x - length, y, x - gap, y )

	surface.DrawLine( x + length, y, x + gap, y )

	surface.DrawLine( x, y - length, x, y - gap )

	surface.DrawLine( x, y + length, x, y + gap )



end



//This is all coding to make the scythe's looks tick! Do NOT edit this, or you can expect loads of errors.

//Thanks to Clavus for creating this awesome tool.



/********************************************************

	SWEP Construction Kit base code

		Created by Clavus

	Available for public use, thread at:

	   facepunch.com/threads/1032378

	   

	   

	DESCRIPTION:

		This script is meant for experienced scripters 

		that KNOW WHAT THEY ARE DOING. Don't come to me 

		with basic Lua questions.

		

		Just copy into your SWEP or SWEP base of choice

		and merge with your own code.

		

		The SWEP.VElements, SWEP.WElements and

		SWEP.ViewModelBoneMods tables are all optional

		and only have to be visible to the client.

********************************************************/



function SWEP:Initialize()



	self:SetWeaponHoldType( self.HoldType ) 

	util.PrecacheSound("weapons/iceaxe/iceaxe_swing1.wav")

	util.PrecacheSound("weapons/knife/knife_hit1.wav")

	util.PrecacheSound("weapons/knife/knife_hit2.wav")

	util.PrecacheSound("weapons/knife/knife_hit3.wav")

	util.PrecacheSound("weapons/knife/knife_hit4.wav")



	if CLIENT then

	

		// Create a new table for every weapon instance

		self.VElements = table.FullCopy( self.VElements )

		self.WElements = table.FullCopy( self.WElements )

		self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )



		self:CreateModels(self.VElements) // create viewmodels

		self:CreateModels(self.WElements) // create worldmodels

		

		// init view model bone build function

		if IsValid(self.Owner) then

			local vm = self.Owner:GetViewModel()

			if IsValid(vm) then

				self:ResetBonePositions(vm)

				

				// Init viewmodel visibility

				if (self.ShowViewModel == nil or self.ShowViewModel) then

					vm:SetColor(Color(255,255,255,255))

				else

					// we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called

					vm:SetColor(Color(255,255,255,1))

					// ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in

					// 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

					vm:SetMaterial("Debug/hsv")			

				end

			end

		end

		

	end



end



function SWEP:Holster()

	

	if CLIENT and IsValid(self.Owner) then

		local vm = self.Owner:GetViewModel()

		if IsValid(vm) then

			self:ResetBonePositions(vm)

		end

	end

	

	return true

end



function SWEP:OnRemove()

	self:Holster()

end



if CLIENT then



	SWEP.vRenderOrder = nil

	function SWEP:ViewModelDrawn()

		

		local vm = self.Owner:GetViewModel()

		if !IsValid(vm) then return end

		

		if (!self.VElements) then return end

		

		self:UpdateBonePositions(vm)



		if (!self.vRenderOrder) then

			

			// we build a render order because sprites need to be drawn after models

			self.vRenderOrder = {}



			for k, v in pairs( self.VElements ) do

				if (v.type == "Model") then

					table.insert(self.vRenderOrder, 1, k)

				elseif (v.type == "Sprite" or v.type == "Quad") then

					table.insert(self.vRenderOrder, k)

				end

			end

			

		end



		for k, name in ipairs( self.vRenderOrder ) do

		

			local v = self.VElements[name]

			if (!v) then self.vRenderOrder = nil break end

			if (v.hide) then continue end

			

			local model = v.modelEnt

			local sprite = v.spriteMaterial

			

			if (!v.bone) then continue end

			

//			local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )

			

			if (!pos) then continue end

			

			if (v.type == "Model" and IsValid(model)) then



				model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )

				ang:RotateAroundAxis(ang:Up(), v.angle.y)

				ang:RotateAroundAxis(ang:Right(), v.angle.p)

				ang:RotateAroundAxis(ang:Forward(), v.angle.r)



				model:SetAngles(ang)

				//model:SetModelScale(v.size)

				local matrix = Matrix()

				matrix:Scale(v.size)

				model:EnableMatrix( "RenderMultiply", matrix )

				

				if (v.material == "") then

					model:SetMaterial("")

				elseif (model:GetMaterial() != v.material) then

					model:SetMaterial( v.material )

				end

				

				if (v.skin and v.skin != model:GetSkin()) then

					model:SetSkin(v.skin)

				end

				

				if (v.bodygroup) then

					for k, v in pairs( v.bodygroup ) do

						if (model:GetBodygroup(k) != v) then

							model:SetBodygroup(k, v)

						end

					end

				end

				

				if (v.surpresslightning) then

					render.SuppressEngineLighting(true)

				end

				

				render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)

				render.SetBlend(v.color.a/255)

				model:DrawModel()

				render.SetBlend(1)

				render.SetColorModulation(1, 1, 1)

				

				if (v.surpresslightning) then

					render.SuppressEngineLighting(false)

				end

				

			elseif (v.type == "Sprite" and sprite) then

				oop. //Lets just hope nobodys that stupid.

			pos, ang = self:GetBoneOrientation( basetab, v, ent )

			

			if (!pos) then return end

			

			pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z

			ang:RotateAroundAxis(ang:Up(), v.angle.y)

			ang:RotateAroundAxis(ang:Right(), v.angle.p)

			ang:RotateAroundAxis(ang:Forward(), v.angle.r)

				

		else

		

			bone = ent:LookupBone(bone_override or tab.bone)



			if (!bone) then return end

			

			pos, ang = Vector(0,0,0), Angle(0,0,0)

			local m = ent:GetBoneMatrix(bone)

			if (m) then

				pos, ang = m:GetTranslation(), m:GetAngles()

			end

			

			if (IsValid(self.Owner) and self.Owner:IsPlayer() and 

				ent == self.Owner:GetViewModel() and self.ViewModelFlip) then

				ang.r = -ang.r // Fixes mirrored models

			end

		

		end

		

		return pos, ang

	end

end

	function SWEP:CreateModels( tab )



		if (!tab) then return end



		// Create the clientside models here because Garry says we can't do it in the render hook

		for k, v in pairs( tab ) do

			if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and 

					string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then

				

				v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)

				if (IsValid(v.modelEnt)) then

					v.modelEnt:SetPos(self:GetPos())

					v.modelEnt:SetAngles(self:GetAngles())

					v.modelEnt:SetParent(self)

					v.modelEnt:SetNoDraw(true)

					v.createdModel = v.model

				else

					v.modelEnt = nil

				end

				

			elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite) 

				and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then

				

				local name = v.sprite.."-"

				local params = { ["$basetexture"] = v.sprite }

				// make sure we create a unique name based on the selected options

				local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }

				for i, j in pairs( tocheck ) do

					if (v[j]) then

						params["$"..j] = 1

						name = name.."1"

					else

						name = name.."0"

					end

				end



				v.createdSprite = v.sprite

				v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)

				

			end

		end

		

	end

	

	local allbones

	local hasGarryFixedBoneScalingYet = false



	function SWEP:UpdateBonePositions(vm)

		

		if self.ViewModelBoneMods then

			

			if (!vm:GetBoneCount()) then return end

			

			// !! WORKAROUND !! //

			// We need to check all model names :/

			local loopthrough = self.ViewModelBoneMods

			if (!hasGarryFixedBoneScalingYet) then

				allbones = {}

				for i=0, vm:GetBoneCount() do

					local bonename = vm:GetBoneName(i)

					if (self.ViewModelBoneMods[bonename]) then 

						allbones[bonename] = self.ViewModelBoneMods[bonename]

					else

						allbones[bonename] = { 

							scale = Vector(1,1,1),

							pos = Vector(0,0,0),

							angle = Angle(0,0,0)

						}

					end

				end

				

				loopthrough = allbones

			end

			// !! ----------- !! //

			

			for k, v in pairs( loopthrough ) do

				local bone = vm:LookupBone(k)

				if (!bone) then continue end

				

				// !! WORKAROUND !! //

				local s = Vector(v.scale.x,v.scale.y,v.scale.z)

				local p = Vector(v.pos.x,v.pos.y,v.pos.z)

				local ms = Vector(1,1,1)

				if (!hasGarryFixedBoneScalingYet) then

					local cur = vm:GetBoneParent(bone)

					while(cur >= 0) do

						local pscale = loopthrough[vm:GetBoneName(cur)].scale

						ms = ms * pscale

						cur = vm:GetBoneParent(cur)

					end

				end

				

				s = s * ms

				// !! ----------- !! //

				

				if vm:GetManipulateBoneScale(bone) != s then

					vm:ManipulateBoneScale( bone, s )

				end

				if vm:GetManipulateBoneAngles(bone) != v.angle then

					vm:ManipulateBoneAngles( bone, v.angle )

				end

				if vm:GetManipulateBonePosition(bone) != p then

					vm:ManipulateBonePosition( bone, p )

				end

			end

		else

			self:ResetBonePositions(vm)

		end

		   

	end

	 

	function SWEP:ResetBonePositions(vm)

		

		if (!vm:GetBoneCount()) then return end

		for i=0, vm:GetBoneCount() do

			vm:ManipulateBoneScale( i, Vector(1, 1, 1) )

			vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )

			vm:ManipulateBonePosition( i, Vector(0, 0, 0) )

		end

		

	end



	/**************************

		Global utility code

	**************************/



	// Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).

	// Does not copy entities of course, only copies their reference.

	// WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop

	function table.FullCopy( tab )



		if (!tab) then return nil end

		

		local res = {}

		for k, v in pairs( tab ) do

			if (type(v) == "table") then

				res[k] = table.FullCopy(v) // recursion ho!

			elseif (type(v) == "Vector") then

				res[k] = Vector(v.x, v.y, v.z)

			elseif (type(v) == "Angle") then

				res[k] = Angle(v.p, v.y, v.r)

			else

				res[k] = v

			end

		end

		

		return res

		

	end

	

end



