Aimbot and keyvalues.

At the moment I’m trying to make an aimbot system (for strictly educational purposes, of course). Basically, the idea is that if you aim at an npc/player, if your aim is close enough (a few degrees leeway) and you fire, your aim snaps to their head immediately.

Here’s the code



if ply:GetActiveWeapon() and ply:GetActiveWeapon():IsValid() then

	local ReflexWeapons_Attack1 = {
									"weapon_357",
									"weapon_ar2",
									"weapon_crossbow",
									"weapon_pistol",
									"weapon_rpg",
									"weapon_shotgun",
									"weapon_smg1",
									"weapon_stunstick"								
								}
								
	local ReflexWeapons_Attack2 = {
									"weapon_ar2",
									"weapon_crossbow",
									"weapon_shotgun",
									"weapon_smg1",							
								}

	if key == IN_ATTACK or key == IN_ATTACK2 then

		for k, v in pairs( ents.FindInCone( ply:EyePos(), ply:GetAimVector(), 9999, 2 ) ) do
			
			local trace = { }  
			trace.start = ply:EyePos()  
			trace.endpos = v:EyePos()
			trace.filter = ply  
			 
			local tr = util.TraceLine( trace ) 
		
			if ( v:IsPlayer() or v:IsNPC() ) and v != ply and tr.HitWorld == false then
			
				table.insert( perks_AimTable, v )
				
			end		
		
		end
		
	perks_AimTarget = table.GetFirstValue( perks_AimTable )

	table.Empty( perks_AimTable )
	
	end
								
	if key == IN_ATTACK and table.HasValue( ReflexWeapons_Attack1, ply:GetActiveWeapon():GetClass() ) and perks_AimTarget and perks_AimTarget:IsValid() then

		local TargetAng = ( perks_AimTarget:EyePos() - ply:EyePos() ):Angle()
		
		ply:SetEyeAngles( TargetAng )
			
	end

end


I’ve tried a few different ways of doing it, but they all use ents.FindInCone(), which seems to be buggy as hell. As I understand it (going by the descriptions on the two wikis), what I’ve done should grab entities on a very narrow cone around my aiming vector, then aim at them. What happens in practice is that sometimes it’ll kinda work, and sometimes I’ll fire and suddenly spin 180 degrees and shoot something on the other side of the map. Am I defining the angle of the cone properly? Does anyone have a diagram? :v:

On a completely unrelated note, I’ve almost finished making a gamemode, and I’m looking at creating adjustable settings for the gamemode selection screen.

In ninjas.txt



"ninjas"
{
	"base"		"base"
	"title"		"Ninjas!"
	"maps"		"^gm_|^gmod_"
	"menusystem"	"1"

	"settings"
	{
	
		1
		{
			"name"		"n_startmoney"
			"text"		"Starting Money"
			"help"		"Amount of money you start with."
			"type"		"numeric"
			"default"	"16"
		}
	
		2
		{
			"name"		"population_citizen"
			"text"		"Citizen Population"
			"help"		"The number of citizens on the map."
			"type"		"numeric"
			"default"	"40"
		}
	
		3
		{
			"name"		"population_target"
			"text"		"Target Population"
			"help"		"The number of targets on the map."
			"type"		"numeric"
			"default"	"2"
		}
		
		4
		{
			"name"		"population_metropolice"
			"text"		"Metrocop Population"
			"help"		"The number of metropolice on the map."
			"type"		"numeric"
			"default"	"15"
		}
		
		5
		{
			"name"		"population_scanner"
			"text"		"Scanner Population"
			"help"		"The number of combine scanners on the map."
			"type"		"numeric"
			"default"	"5"
		}

	}
	
}


The CVars are set in the initialisation hook of init.lua.



	--#########################################--
	--#########################################--
	--					CVARS		   		     --
	--#########################################--
	--#########################################--
		
	CreateConVar( "population_citizen", "40", 128, "Number of neutral citizens." )
	CreateConVar( "population_target", "2", 128, "Number of marked citizens." )
	CreateConVar( "population_metropolice", "15", 128, "Number of AI metrocops." )	
	CreateConVar( "population_scanner", "5", 128, "Number of combine scanners." )
	
	CreateConVar( "n_startmoney", "8000", 128, "Amount of money a player will start with." )


These do work, since I can adjust them and stuff in game, but they do not seem to be linked to ninjas.txt, because no options appear. I’ve had a dig around in the code for sandbox and ttt, and I can’t really tell how what I’ve done differs.

I realise that this has been quite a long post. Any help would be appreciated!

find in cone is pretty expensive, especially if used on a frame-by-frame basis such as an aimbot.

Hmm, okay. What would you recommend?

I would actually recommend converting the positions to screen coords and then calculating how close they are to the center of the screen and dealing with them that way.

Okay, I’ll have a look at that. Thanks :smile:

Aimbot has such a negative meaning. Why not Autoaim, or Aim Assist, like the consoles call it?

I guess ‘Educational Vector-Screen-Space Aim Assist’ has a certain ring to it…

Ah, okay, just a quick problem, I’m trying to get this to work serverside and I’ve just realised that all this screen stuff is clientside… is there a way to do this on the server? Or am I being silly?

Best way would be clientside, there’s no server way, as ToScreen() is resolution/FOV dependant.

Go take a look at actual lua aimbots. Those all (or should) contain a check to see if the target is within X degrees of the player.
[editline]9th July 2014[/editline]

That sounds disgusting, just use some trig and vector math, it’s not hard.

As someone interested in learning C++ for Gmod, could you post it anyways?

[lua]
local goodang = Angle(0,0,0);
FindMetaTable(“Angle”).Difference = function(x, y)
return math.abs(math.NormalizeAngle(x.p, y.p)) + math.abs(math.NormalizeAngle(x.y, y.y)) + math.abs(math.NormalizeAngle(x.r, y.r));
end
hook.Add(“CreateMove”, “aaa”, function(cmd)
if(cmd:KeyDown(IN_ATTACK) and cmd:GetViewAngles():Difference(goodang) < 2^2) then
cmd:SetViewAngles(goodang);
end
end);
[/lua]

It’s integrated in with the rest of my educational-purposes-only target acquisition assistant, but here you go I guess.



bool CAimboat::IsWithinFov(targetinfo_t& targetinfo)
{
    Vector aim = _me->angles().MakeVector();
    Vector ang = targetinfo.angles_to_target.MakeVector();

    float mag_s = sqrt(aim.Magnitude());
    float mag_d = sqrt(ang.Magnitude());

    float u_dot_v = aim.x*ang.x + aim.y*ang.y + aim.z*ang.z;

    _fov_current = (float)(acos(u_dot_v / (mag_s*mag_d)) * (180.0f / (float)M_PI));

    return (_fov_current < cvars::aimbot_fov->get_float());
}


Hmm, okay, but for Educational-Vector-Aim-Assist, I wanted it to run on the server to prevent it being exploited (as dumb as that sounds) as I was going to integrate it into a gamemode somewhere down the line possibly.

You can’t run such things on the server, it needs to be run on the client. So however you do it, at some point it needs to be sent to the client and at that point whoever has the knowledge can take the code and exploit it if they wish to.

you could make it synced between client and server if you wanted to (hooks on server & client), and send a net message to the client to toggle aimbot hook

That means the aimbot would still be running on the client, and people could exploit it by just calling the hook.

That’s why you don’t let it run when the aimbot isn’t toggled by the net message… and if someone could exploit it, why would they even want to toggle the aimbot? They could just make their own if they wanted to…

OP is the one who wants to prevent people from exploiting it by running it server side. Not sure why, seeing as GM aimbots already exist clientside

How about using a CalcView hook instead of CreateMove clientside? That would eliminate the possibility of people messing with the createmove hook toggle and it would still be synced.