NPC's not attacking.

Refer to title for problem.

Trying to get an npc that I’m creating to attack me (a player) but I’m not having any real success. They just go straight to T-Pose, facing me (you can tell they’re trying to attack).

My only real function, located in init.lua:



function ENT:Initialize()
	self:SetModel("models/Humans/Group01/Female_01.mdl")
	
	self:SetHullType(HULL_HUMAN)
	self:SetHullSizeNormal()
	
	self:SetSolid(SOLID_BBOX)
	self:SetMoveType(MOVETYPE_STEP)
	
	self:CapabilitiesAdd( CAP_SIMPLE_RADIUS_DAMAGE | CAP_MOVE_GROUND | CAP_MOVE_SHOOT | CAP_USE | CAP_OPEN_DOORS | CAP_TURN_HEAD | CAP_WEAPON_RANGE_ATTACK1 | CAP_WEAPON_RANGE_ATTACK2 | CAP_WEAPON_MELEE_ATTACK1 | CAP_WEAPON_MELEE_ATTACK2 | CAP_INNATE_RANGE_ATTACK1 | CAP_INNATE_RANGE_ATTACK2 | CAP_INNATE_MELEE_ATTACK1 | CAP_INNATE_MELEE_ATTACK2 | CAP_USE_WEAPONS | CAP_ANIMATEDFACE | CAP_AIM_GUN)
	
	self:SetMaxYawSpeed(5000)
	
	self:SetHealth(100)
	self:Give("weapon_ar2")
	
	self:AddRelationship("player D_HT 99")
end


(Used [.code][./code] due to line 10)

Am I missing/doing something wrong? Is everyone having this problem? Are there any solutions? Do I sound desperate? (Don’t answer that last one…)

Because your not telling it to do anything. look into other people’s SNPCs specifically at SelectSchedule() and ai_schedule

[lua]
local schdFireWeapon = ai_schedule.New(“Fire At Player”)
schdFireWeapon:AddTask(“FindEnemy”, {Class = “player”, Radius = 2000})
schdFireWeapon:EngTask(“TASK_GET_PATH_TO_RANGE_ENEMY_LKP_LOS”,0)
schdFireWeapon:EngTask(“TASK_RUN_PATH”,0)
schdFireWeapon:EngTask(“TASK_WAIT_FOR_MOVEMENT”,0)
schdFireWeapon:EngTask(“TASK_STOP_MOVING”,0)
schdFireWeapon:EngTask(“TASK_FACE_ENEMY”,0)
schdFireWeapon:EngTask(“TASK_RANGE_ATTACK1”,0)
schdFireWeapon:EngTask(“TASK_RELOAD”,0)
[/lua]
[lua]
function ENT:SelectSchedule()
self:StartSchedule(schdFireWeapon)
end
[/lua]

This is my schedule, which gives the same result as before. T-pose.

um, you can’t tell it to do everything in one schedule you have to break it up.

[LUA]
local schdChase = ai_schedule.New(“Chase Enemy”)
schdChase:AddTask(“FindEnemy”, {Class = “player”, Radius = 2000})
schdChase:EngTask(“TASK_GET_PATH_TO_ENEMY”,0)
schdChase:EngTask( “TASK_RUN_PATH_TIMED”, 0.1 )
schdChase:EngTask( “TASK_WAIT”, 0.1 )

local schdMeleeAttack = ai_schedule.New( “Attack Enemy” )
schdMeleeAttack:EngTask( “TASK_STOP_MOVING”, 0 )
schdMeleeAttack:EngTask( “TASK_WAIT_FACE_ENEMY”, 0.001 )
schdMeleeAttack:EngTask( “TASK_PLAY_SEQUENCE”, ACT_MELEE_ATTACK1 )

function ENT:SelectSchedule()
if(!IsValid(self:GetEnemy()))then
self:StartSchedule(schdChase)
return
end
if(self:GetEnemy():GetPos():Distance(self:GetPos())<=75)then
self:StartSchedule(schdMeleeAttack)
else
self:StartSchedule(schdChase)
end
end[/LUA]

Ok, so I tried my initial modification of yours, as well as yours in verbatim, but I still get an npc trying to flap its wings. (going from idle to T-pose back to idle; repeat)

Are there nodes on this map you are using it on?

There should be.
I’m on gm_bigcity.

Why would it matter though? It’s failing to attack, not moving to a random node.

bigcity doesn’t have nodes. because it has to get the path to get to you to attack.

Same exact thing occurs on gm_construct.

And Note that I have gotten movement to work fine, even on non-noded maps, it’s just the attacking part.

How does the NPC move on a non-noded map?

Because it’s not moving to a node, it’s moving to what I specify it to. (To my understanding, at least.)

I’ve gotten it to move on gm_big city, although apparently it’s not noded.

NPC’s can navigate without a nodegraph, however, not very well, and if they have nothing really close to interest them they’ll just stand around. here’s the code for zombie i’m working on, it moves around this might help.

shared.lua
[lua]
ENT.Base = “base_ai”
ENT.Type = “ai”

ENT.AutomaticFrameAdvance = true

function ENT:SetAutomaticFrameAdvance( bUsingAnim )
self.AutomaticFrameAdvance = bUsingAnim
end

[/lua]

cl_init.lua
[lua]
include(‘shared.lua’)

ENT.RenderGroup = RENDERGROUP_BOTH

function ENT:Draw()

self:DrawModel()

end

function ENT:DrawTranslucent()

self:Draw()

end
[/lua]

init.lua
[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

function ENT:Initialize()

self:SetModel( "models/zombie/classic.mdl" )
 
self:SetHullSizeNormal()
self:SetHullType( HULL_HUMAN )

self:SetNPCState( NPC_STATE_ALERT )

self:SetSolid( SOLID_BBOX ) 
self:SetMoveType( MOVETYPE_STEP )
self:CapabilitiesAdd( CAP_ANIMATEDFACE | 
                      CAP_TURN_HEAD | 
					  CAP_MOVE_GROUND | 
					  CAP_USE | 
					  CAP_AUTO_DOORS | 
					  CAP_OPEN_DOORS | 
					  CAP_WEAPON_MELEE_ATTACK1 |
					  CAP_WEAPON_MELEE_ATTACK2 |
					  CAP_INNATE_MELEE_ATTACK1 |
					  CAP_INNATE_MELEE_ATTACK2 |
					  CAP_SQUAD 						  
					 )
					 


self:DropToFloor()
self:SetHealth(120)
self:SetMaxHealth(120)

end

ENT.Seek = ai_schedule.New( “eozSeek” )
ENT.Seek:EngTask(“TASK_GET_PATH_TO_RANDOM_NODE”, 1024)
ENT.Seek:EngTask(“TASK_WALK_PATH”, 0)

ENT.SeekEnemy = ai_schedule.New( “eozSeekEnemy” )
ENT.SeekEnemy:EngTask(“TASK_GET_PATH_TO_ENEMY”, 0)
ENT.SeekEnemy:EngTask(“TASK_FACE_ENEMY”, 0)
ENT.SeekEnemy:EngTask(“TASK_RUN_PATH”, 0)

ENT.Attack = ai_schedule.New( “eozAttack” )
ENT.Attack:EngTask(“TASK_STOP_MOVING”, 0)
ENT.Attack:EngTask(“TASK_FACE_ENEMY”, 0)
ENT.Attack:EngTask(“TASK_MELEE_ATTACK1”, 0)

function ENT:Think()

end

function ENT:OnTakeDamage( dmginfo)

print(tostring(self) .. " I'm Hurt")
self:SetNPCState( NPC_STATE_COMBAT )

end

function ENT:OnCondition( iCondition )

--print(iCondition)
print(self:ConditionName(iCondition))
--COND_CAN_MELEE_ATTACK1

if self:ConditionName(iCondition) == "COND_TOO_FAR_TO_ATTACK" then		

	print("Too Far!!! I'll Get You!")
	if (self:IsCurrentSchedule( SCHED_NONE )) then
		self:StartSchedule( self.SeekEnemy )
	end
	
end

if self:ConditionName(iCondition) == "COND_CAN_MELEE_ATTACK1" then		

	print("Attack!!")
	if (self:IsCurrentSchedule( SCHED_NONE )) then
		self:StartSchedule( self.Attack )
	end
	
end

end

[/lua]

I’m aware they can move on maps that are not noded, http://www.facepunch.com/threads/725556-Npc-ai?highlight= Unless you were talking to him. I’m sure he will be able to learn from your code.

If not Clevin, it would be usefull if you would just post your entire npc code.

Use gm_navigation by Spacetech for maps that are not noded. Works like a charm.

client:
[lua]
include(‘sh_init.lua’)

ENT.RenderGroup = RENDERGROUP_BOTH

function ENT:Draw()
self:DrawModel()
end

function ENT:BuildBonePositions(NumBones, NumPhysBones)
end

function ENT:DoRagdollBone(PhysBoneNum, BoneNum)
end
[/lua]

server:
[lua]
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“sh_init.lua”)

include(‘sh_init.lua’)

–[[
local schdWalk = ai_schedule.New(“Walking Randomly”) – Walk towards set entity --works!!!
schdWalk:AddTask(“FindNode”, {Class = “node_test”, Radius = 2000})
schdWalk:EngTask(“TASK_GET_PATH_TO_ENEMY”, 0)
schdWalk:EngTask(“TASK_WALK_PATH”,0)
schdWalk:EngTask(“TASK_WAIT_FOR_MOVEMENT”,0)
–]]

local schdFindTarget = ai_schedule.New(“Find Target”)
schdFindTarget:AddTask(“FindEnemy”, {Class = “player”, Radius = 2000})
schdFindTarget:EngTask(“TASK_GET_PATH_TO_RANGE_ENEMY_LKP_LOS”,0)
schdFindTarget:EngTask(“TASK_RUN_PATH”,0)
schdFindTarget:EngTask(“TASK_WAIT_FOR_MOVEMENT”,0)

local schdFireWeapon = ai_schedule.New(“Attack Enemy”)
schdFireWeapon:EngTask(“TASK_STOP_MOVING”,0)
schdFireWeapon:EngTask(“TASK_FACE_ENEMY”,0)
schdFireWeapon:EngTask(“TASK_PLAY_SEQUENCE”, ACT_RANGE_ATTACK1)

function ENT:TaskStart_FindNode(data)
–[[
local et = ents.FindInSphere(self:GetPos(), data.Radius or 512)
for k,v in ipairs(et) do
if (v != self && v:GetClass() == data.Class) then
self:SetEnemy(v, true)
self:UpdateEnemyMemory(v, v:GetPos())
self:TaskComplete()
return
end
end
self:SetEnemy(NULL)
–]]
end
function ENT:Task_FindNode(data)
local et = ents.FindInSphere(self:GetPos(), data.Radius or 512)
for k,v in ipairs(et) do
if (v != self && v:GetClass() == data.Class) then
self:SetEnemy(v, true)
self:UpdateEnemyMemory(v, v:GetPos())
self:TaskComplete()
return
end
end
self:SetEnemy(NULL)
end

function ENT:Think()
self:NextThink(CurTime())
return true
end

function ENT:SpawnFunction(ply, tr)
if !tr.HitWorld then return end

local ent = ents.Create("npc_test")
ent:SetPos(tr.HitPos)
ent:Spawn()

return ent

end

function ENT:Initialize()
self:SetModel(“models/Humans/Group01/Female_01.mdl”)

self:SetHullType(HULL_HUMAN)
self:SetHullSizeNormal()

self:SetSolid(SOLID_BBOX)
self:SetMoveType(MOVETYPE_STEP)

self:CapabilitiesAdd( 	CAP_SIMPLE_RADIUS_DAMAGE |
						CAP_MOVE_GROUND |
						CAP_MOVE_SHOOT |
						CAP_USE |
						CAP_OPEN_DOORS |
						CAP_TURN_HEAD |
						CAP_WEAPON_RANGE_ATTACK1 |
						CAP_WEAPON_RANGE_ATTACK2 |
						CAP_WEAPON_MELEE_ATTACK1 |
						CAP_WEAPON_MELEE_ATTACK2 |
						CAP_INNATE_RANGE_ATTACK1 |
						CAP_INNATE_RANGE_ATTACK2 | 
						CAP_INNATE_MELEE_ATTACK1 |
						CAP_INNATE_MELEE_ATTACK2 |
						CAP_USE_WEAPONS |
						CAP_ANIMATEDFACE |
						CAP_AIM_GUN)

self:SetMaxYawSpeed(5000)

self:SetHealth(100)
self:Give("weapon_ar2")

– self:AddRelationship(“player D_HT 99”)
end

function ENT:SelectSchedule()
if(!IsValid(self:GetEnemy()))then
self:StartSchedule(schdChase)
return
end
if(self:GetEnemy():GetPos():Distance(self:GetPos())<=75)then
self:StartSchedule(schdMeleeAttack)
else
self:StartSchedule(schdChase)
end
end

function SpawnNPC(ply)
local tr = ply:GetEyeTrace()
local npc = ents.Create(“npc_test”)
npc:SetPos(tr.HitPos)
npc:Spawn()
end
concommand.Add(“spawntestnpc”,SpawnNPC)
[/lua]

shared:
[lua]
ENT.Base = “base_ai”
ENT.Type = “ai”

ENT.PrintName = “TestNPC”
ENT.Author = “Clevin”
ENT.Contact = “Clevin”
ENT.Purpose = “Testing.”
ENT.Instructions = “Testing.”

ENT.AutomaticFrameAdvance = true

function ENT:OnRemove()
end

function ENT:PhysicsCollide(data, physobj)
end

function ENT:PhysicsUpdate(physobj)
end

function ENT:SetAutomaticFrameAdvance(bUsingAnim)
self.AutomaticFrameAdvance = bUsingAnim
end
[/lua]

It’s purely the attacking part, not the moving part that is causing me trouble.

Have you tried using TASK_RANGE_ATTACK1 instead of schdFireWeapon:EngTask(“TASK_PLAY_SEQUENCE”, ACT_RANGE_ATTACK1)

that may change things, also I don’t know if you’ve used it but here’s a list of source engine tasks from the valve wiki, http://developer.valvesoftware.com/wiki/Shared_tasks

Yes, I was originally using that, but had changed it to what you see from one of -TB-'s posts above.

-snip-

is this in single player or on a server?

Edit:

Ok, well I was just asking because I’m having issues now with some NPC’s on a dedicated server, thought it might be related, but guess not, well good luck

Single player.

I had trouble with them in multiplayer too Fanty.