SWEP Less dmg over distance

Hi
I was wondering if there was a way to make a swep do less damage the further away the target was.

Info:
The swep fires bullets (rather than phy entities)
The swep I want to mod is using the ttt wep base (I doubt that makes difference but whatever)

All help will be greatly appreciated
Thanks :slight_smile:

Mental

1)Figure out if you are aiming at an entity
2)Get the distance of the entity from yourself
3)Scale the damage based on the distance
4)Shoot the bullet
5)???
6)Profit

You don’t have to tell us the stuff you learned in your math class yesterday, How about you tell him how to make it?

You could make it a global thing and have the damage scale based on an EntityTakeDamage hook. When I grt home I’ll make something to get you on the right track.

Edit:

Something like this could work:

[lua]local function CalcDamage( target, attacker, inflictor, amt, dmg )
if ValidEntity( target ) and ValidEntity( attacker ) then
if dmg:IsBulletDamage() then – no need to scale explosive damage or things like that
local dist = target:GetPos():Distance( attacker:GetPos() )
dmg:ScaleDamage( math.Clamp( 1.0 - ( dist / 1000 ), 0, 2 ) ) – you’re gonna have to come up with your own equation here, just remember ScaleDamage works on a decimal modifier… 0.5 = half the damage you’d normally take, anything above 1.0 is more damage.
end
end
end
hook.Add( “EntityTakeDamage”, “ScaleDamage”, CalcDamage )[/lua]

Well shame on me, I just realized I read this page that I had loaded a day ago and BL00DB4TH’s post wasn’t there :b
[lua]function GM:EntityTakeDamage(victim, weapon, killer, ammount, dmginfo)
–Killer as in attacker, just easier to type for me.
if (ammount > 0) then --No point trying anything if the damage is 0 or less

	if (weapon == "your_weapon_here") then --If the source of damage is the weapon in question
		local distance = victim:GetPos():Distance(killer:GetPos()) --Get the distance between victim and killer
		local factor = distance*0.01 --Get 0.01 per unit of distance
		dmginfo:ScaleDamage( 1-factor ) -- Substract that to 1
		--This formula reduces 1% of the damage per unit of distance (Probably waaay too much)
	end
	
end

end[/lua]

Thats a gamemode script function, if you want to use it in a weapon script, add it as a serverside hook.
If you want to say, reduce 1% damage after the distance is 1000 units just add an IF condition.
Also, important, “ammount” wont scale down but the actual damage will, so if you try printing out the amount damage of a dmginfo:ScaleDamage(0) it will show the damage before scaling. Use dmginfo:getDamage() for that.
[lua] dmginfo:ScaleDamage(0.2+level0.2+(dmg0.01))
killer:PrintMessage( HUD_PRINTCENTER, math.floor(dmginfo:GetDamage()*10)/10 )[/lua]

Just out of curiosity…

What is the purpose of multiplying it by 10, then dividing by 10?..

Different outcome after rounding;

[lua]
local x = math.floor(4.123*10)/10 – 4.1
local y = math.floor(4.123) – 4
[/lua]

Exactly :slight_smile: Obtaining only 1 decimal in a fast manner.