From f1e99d19a5aa6e5fa61518366235e3da09689d0b Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 2 Jan 2016 22:10:06 -0500 Subject: Ai overhual now working --- entities/entities/ws_npc_ambient/init.lua | 5 ++ entities/entities/ws_npc_ambient/shared.lua | 75 ++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) (limited to 'entities') diff --git a/entities/entities/ws_npc_ambient/init.lua b/entities/entities/ws_npc_ambient/init.lua index 485b6c9..a0497dd 100644 --- a/entities/entities/ws_npc_ambient/init.lua +++ b/entities/entities/ws_npc_ambient/init.lua @@ -8,6 +8,11 @@ function ENT:Initialize() --print("NPC spawned!") if(self.Model) then self:SetModel(self.Model) else print("NPC created without model, this might be a bug!") end + if(self.Stats["Vitality"]) then self.Vitality = self.Stats["Vitality"] + else print("NPC created with no stat for vitality, this might be a bug!")end + if(self.Stats["Accel"]) then self.loco:SetAcceleration(self.Stats["Accel"])end + if(self.Stats["Decel"]) then self.loco:SetDeceleration(self.Stats["Decel"]) end + if(self.Stats["Step"]) then self.loco:SetJumpHeight(self.Stats["Step"]) end --self:SetModel( "models/Humans/Group01/Female_01.mdl" ) --[[ self:SetHullType( HULL_HUMAN ); diff --git a/entities/entities/ws_npc_ambient/shared.lua b/entities/entities/ws_npc_ambient/shared.lua index ba65e2b..7fa3661 100644 --- a/entities/entities/ws_npc_ambient/shared.lua +++ b/entities/entities/ws_npc_ambient/shared.lua @@ -17,6 +17,79 @@ ENT.Act = nil function ENT:OnRemove() end +function ENT:DefaultBehaviour() + while ( true ) do + --Main loop for ai + + --Update aware enemies + local players = ents.FindByClass("Player") + for k,v in pairs(players) do + if(v:IsPigeon()) then continue end + local dist = v:GetPos():Distance(self:GetPos()) + if( dist < self.Stats["AwareDist"]) then + table.insert(self.AwareEnemies,v) + end + end + + --Find the enemy with the highest priority + local maxpriority = -1 + local maxprioritytarget = nil + for k,v in pairs(self.AwareEnemies) do + local priority = self:AttackPriority(v) + if(priority > maxpriority) then + maxpriority = priority + maxprioritytarget = v + end + end + self.Target = maxprioritytarget + + --If we can't find anyone to attack, just stay idle + if(self.Target == nil) then + print("Couldn't find anyone to attack!") + --Play an idle sequence + local randanim = math.Round(math.Rand(0,#self.IdleSequences)) + self:PlaySequenceAndWait( self.IdleSequences[randanim] ) + self:StartActivity( ACT_IDLE ) + --If there's noone within 4000 units, just remove ourselves to save server resources + local closest = 5000 + for k,v in pairs(player.GetAll()) do + local thisdist = self:GetPos():Distance(v:GetPos()) + if(thisdist > closest) then + closest = thisdist + end + end + if(closest > 4000) then self:BecomeRagdoll(DamageInfo()) end + else + --We have a target to attack! + + --Find which attack will do the most dammage + local maxdammage = -1 + local maxdammagefunc = nil + for k,v in pairs(self.Attacks) do + local dammagefunc = nil + local attackfunc = nil + for i,j in pairs(v) do + dammagefunc = i + attackfunc = j + end + local dammage = dammagefunc(self, self.Target) + if(dammage > maxdammage) then + maxdammage = dammage + maxdammagefunc = attackfunc + end + end + + --Do that attack + if(maxdammagefunc) then + maxdammagefunc(self, self.Target) + end + end + coroutine.yield() + end + + coroutine.yield() +end + function ENT:BehaveAct() if(self.Act) then self:Act() @@ -29,6 +102,6 @@ function ENT:RunBehaviour() if(self.Behave) then self:Behave() else - print("NPC spawned without a Behave function, this might be an error!") + self:DefaultBehaviour() end end -- cgit v1.2.3-70-g09d2