Skip to content

Custom Gun Lua: Lock on tracking rockets

Vuthakral edited this page Mar 15, 2022 · 4 revisions

About

Author: Vuthakral
Originally From: Draconic Halo UNSC SWEPs
Description: Solution #1 provides a code which can lock onto targets while zoomed in using a weapon's scope, and the potential target is within radius of the weapon's aim assist cone. Solution #2 provides a similar solution, but does not require the use of a scope.

Solution #1:

function SWEP:DoCustomInitialize()
	self.LockingSound = CreateSound(self, "drc.spnkr_locking")
	self.LockedSound = CreateSound(self, "drc.spnkr_locked")
end

local NPCTargets = {
	"npc_combinegunship",
	"npc_combinedropship",
	"npc_helicopter",
	"npc_strider",
	"npc_turret_floor",
	"npc_cscanner",
	"npc_clawscanner",
	"monster_turret",
	"monster_apache",
	"monster_osprey",
	"monster_alien_controller",
	"monster_nihilanth",
	"monster_bigmomma"
}

SWEP.TargetLocked = false
SWEP.LockTime = 0
SWEP.LockTick = 0
SWEP.LastTarget = nil
SWEP.LastTarget = nil
function SWEP:DoCustomThink()
	if !IsValid(self) or !IsValid(self:GetOwner()) then return end
	if CurTime() < self.LockTick then return end
	if !self.SightsDown or self:Clip1() < 1 then 
		self.LockTime = 0
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	self.LockTick = CurTime() + 0.1
	
	local ply = self:GetOwner()
	local tgt = self:GetConeTarget()
	if !IsValid(tgt) then return end
	local class = tgt:GetClass()
	if tgt:IsNPC() && !table.HasValue(NPCTargets, class) then return end
	if tgt:IsPlayer() then return end
	if tgt:IsNextBot() then return end
	
	self.AntiWallhack_TR = util.TraceLine({
		start = ply:EyePos(),
		endpos = tgt:GetPos() + tgt:OBBCenter()
	})
	
	if self.AntiWallhack_TR.Hit && self.AntiWallhack_TR.Entity:IsWorld() then
		self.LockTime = 0
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	
	if IsValid(tgt) then
		if !self.LockingSound:IsPlaying() then self.LockingSound:Play() end
		self.TargetLocked = true
		self.LockTime = self.LockTime + 0.1
		if tgt:EntIndex() != self.LastTarget then self.LockTime = 0 end
		self.LastTarget = tgt:EntIndex()
		
		if self.LockTime > 1.4 then
			self.LockingSound:Stop()
			if !self.LockedSound:IsPlaying() then self.LockedSound:Play() end
		else
			self.LockedSound:Stop()
		end
	else
		self.LockingSound:Stop()
		self.LockedSound:Stop()
		self.TargetLocked = false
		self.LockTime = 0
	end
	self.LockTime = math.Clamp(self.LockTime, 0, 1.5)
end

function SWEP:PassToProjectile(proj)
	if self.LockTime > 1.49 == true then
		proj.Tracking 		= true
		proj.TrackType 		= "Tracking"
		proj.TrackFraction 	= 0.5
		proj.TrackTarget	= self:GetConeTarget()
	end
end

SWEP.LockFlashTime = 0
local CTU = f1
function SWEP:DrawCustom2DScopeElements()
	local tgt = self:GetConeTarget()
	if !IsValid(tgt) then return end
	local class = tgt:GetClass()
	if tgt:IsNPC() && !table.HasValue(NPCTargets, class) then return end
	if tgt:IsPlayer() then return end
	if tgt:IsNextBot() then return end
	
	if self.AntiWallhack_TR.Hit && self.AntiWallhack_TR.Entity:IsWorld() then
		self.LockTime = 0
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	
	local f1 = Color(255, 0, 0)
	local f2 = Color(255, 100, 0)
	
	if CurTime() > self.LockFlashTime then
		if CTU == f1 then
			CTU = f2
			self.LockFlashTime = CurTime() + 0.1
		else
			CTU = f1
			self.LockFlashTime = CurTime() + 0.2
		end
	end

	if IsValid(tgt) then
		local icon = Material("models/vuthakral/halo/HUD/reticles/lock2.png")
		surface.SetMaterial(icon)
		if self.LockTime > 1.49 then surface.SetDrawColor(CTU) else surface.SetDrawColor(Color(127, 127, 127)) end
		surface.DrawTexturedRectRotated((tgt:GetPos() + tgt:OBBCenter()):ToScreen().x, (tgt:GetPos() + tgt:OBBCenter()):ToScreen().y, ScrH() / 6, ScrH() / 6, 0)
	end
end

Solution #2

function SWEP:DoCustomInitialize()
	self.LockingSound = CreateSound(self, "drc.spnkr_locking")
	self.LockedSound = CreateSound(self, "drc.spnkr_locked")
end

local NPCTargets = {
	"npc_combinegunship",
	"npc_combinedropship",
	"npc_helicopter",
	"npc_strider",
	"npc_turret_floor",
	"npc_cscanner",
	"npc_clawscanner",
	"monster_turret",
	"monster_apache",
	"monster_osprey",
	"monster_alien_controller",
	"monster_nihilanth",
	"monster_bigmomma"
}

SWEP.TargetLocked = false
SWEP.LockTime = 0
SWEP.LockTick = 0
SWEP.LastTarget = nil
SWEP.LastTarget = nil
function SWEP:DoCustomThink()
	if !IsValid(self) or !IsValid(self:GetOwner()) then return end
	if CurTime() < self.LockTick then return end
	if self:Clip1() < 1 then 
		self.LockTime = 0
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	self.LockTick = CurTime() + 0.1
	if self.TargetLocked == false then self.LockTime = 0 end
	
	local ply = self:GetOwner()
	local tgt = self:GetConeTarget()
	if !IsValid(tgt) then 
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	local class = tgt:GetClass()
	if tgt:IsNPC() && !table.HasValue(NPCTargets, class) then return end
	if tgt:IsPlayer() then return end
	if tgt:IsNextBot() then return end
	
	self.AntiWallhack_TR = util.TraceLine({
		start = ply:EyePos(),
		endpos = tgt:GetPos() + tgt:OBBCenter()
	})
	
	if self.AntiWallhack_TR.Hit && self.AntiWallhack_TR.Entity:IsWorld() then
		self.LockTime = 0
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	
	if IsValid(tgt) then
		if !self.LockingSound:IsPlaying() then self.LockingSound:Play() end
		self.TargetLocked = true
		self.LockTime = self.LockTime + 0.1
		if tgt:EntIndex() != self.LastTarget then self.LockTime = 0 end
		self.LastTarget = tgt:EntIndex()
		
		if self.LockTime > 1.4 then
			self.LockingSound:Stop()
			if !self.LockedSound:IsPlaying() then self.LockedSound:Play() end
		else
			self.LockedSound:Stop()
		end
	else
		self.LockingSound:Stop()
		self.LockedSound:Stop()
		self.TargetLocked = false
		self.LockTime = 0
	end
	self.LockTime = math.Clamp(self.LockTime, 0, 1.5)
end

function SWEP:PassToProjectile(proj)
	if self.LockTime > 1.49 == true then
		proj.Tracking 		= true
		proj.TrackType 		= "Tracking"
		proj.TrackFraction 	= 0.5
		proj.TrackTarget	= self:GetConeTarget()
	end
end

SWEP.LockFlashTime = 0
local CTU = f1
function SWEP:DrawCustom2DScopeElements()
	local tgt = self:GetConeTarget()
	if !IsValid(tgt) then return end
	local class = tgt:GetClass()
	if tgt:IsNPC() && !table.HasValue(NPCTargets, class) then return end
	if tgt:IsPlayer() then return end
	if tgt:IsNextBot() then return end
	
	if self.AntiWallhack_TR.Hit && self.AntiWallhack_TR.Entity:IsWorld() then
		self.LockTime = 0
		self.LockingSound:Stop()
		self.LockedSound:Stop()
	return end
	
	local f1 = Color(255, 0, 0)
	local f2 = Color(255, 100, 0)
	
	if CurTime() > self.LockFlashTime then
		if CTU == f1 then
			CTU = f2
			self.LockFlashTime = CurTime() + 0.1
		else
			CTU = f1
			self.LockFlashTime = CurTime() + 0.2
		end
	end

	if IsValid(tgt) then
		local icon = Material("models/vuthakral/halo/HUD/reticles/lock2.png")
		surface.SetMaterial(icon)
		if self.LockTime > 1.49 then surface.SetDrawColor(CTU) else surface.SetDrawColor(Color(127, 127, 127)) end
		surface.DrawTexturedRectRotated((tgt:GetPos() + tgt:OBBCenter()):ToScreen().x, (tgt:GetPos() + tgt:OBBCenter()):ToScreen().y, ScrH() / 6, ScrH() / 6, 0)
	end
end
Clone this wiki locally