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 --- gamemode/npcsystem/npcs/bird.lua | 124 +++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 70 deletions(-) (limited to 'gamemode/npcsystem/npcs/bird.lua') diff --git a/gamemode/npcsystem/npcs/bird.lua b/gamemode/npcsystem/npcs/bird.lua index fa3ad8d..0b05e62 100644 --- a/gamemode/npcsystem/npcs/bird.lua +++ b/gamemode/npcsystem/npcs/bird.lua @@ -6,17 +6,58 @@ NPC.Icon = Material("wintersurvival2/hud/ws1_icons/icon_rock") NPC.Social = "Pack" --Solo, Pack -NPC.Vitality = 10 -NPC.Speed = 100 +NPC.Stats = { + ["Vitality"] = 10, + ["Speed"] = 50, + ["AwareDist"] = 800, +} + +--Some npc's like birds have diffent names for their idle sequences +NPC.IdleSequences = { + [0] = "Idle01", + [1] = "Eat_A", +} + --Drops should be formated as [index]={["item name"], percent_drop} where percent_drop is a number from 0 to 100 NPC.Drops = { - [0] = {"Meat",100},--Birds will drop at least 1 meat, and have a 50% chance of dropping 2 - [1] = {"Meat",50}, - [2] = {"Feather",50}, + [0] = {"Meat",100}, + [1] = {"Meat",100}, + [2] = {"Meat",100}, + [3] = {"Meat",100}, + [4] = {"Meat",80}, + [5] = {"Meat",40}, + [6] = {"Meat",10}, } --Attacks should be formated as [i]={function attackpriority() = function doattack()} -NPC.Attacks = nil +local checkrun = function(self,ply) + --If we're awayre of any enemies, run away! + return 1 +end +local dorun = function(self,ply) + self:StartActivity(ACT_FLY) + self:PlaySequenceAndWait( "Fly01" ) + + --Find a position in roughly the oposite direction of the player + local tpos = self:GetPos() + local ppos = ply:GetPos() + local direction = Vector(tpos.x - ppos.x, tpos.y - ppos.y, tpos.z - ppos.z) + direction:Normalize() + local addition = direction * 1000 + local topos = self:GetPos() + addition + local navarea = navmesh.GetNavArea(self:GetPos(), 100) + if navarea:IsValid() then + self:MoveToPos(topos) + else + print("Suicideing a bird to prevent server crash!") + self:BecomeRagdoll(DamageInfo()) + end +end +NPC.Attacks = { + [1] = {--run away from the player + [checkrun] = dorun + }, +} --A function that takes a position and returns true if this is an acceptable place to spawn NPC.SpawnLocations = nil @@ -25,7 +66,13 @@ NPC.SpawnLocations = nil NPC.Target = nil --All enemies that this NPC is aware of -NPC.AwareEnemies = nil +NPC.AwareEnemies = {} + +--Attack priority is a fucntion that takes a player, and returns an int describing it's prority to attack (higher = more important) NPC will always attack the player with the highest priority +function NPC:AttackPriority(ply) + local dist = ply:GetPos():Distance(self:GetPos()) + return self.Stats["AwareDist"] - dist +end --What to replace the ENT:BehaveAct with function NPC:Act() @@ -36,69 +83,6 @@ function NPC:Stuck() end ---What to replace ENT:RunBehaviour with -function NPC:Behave() - --print("Going into bird's custom behaviour") - while ( true ) do - self:StartActivity( ACT_IDLE ) -- walk anims - self:PlaySequenceAndWait( "Idle01" ) -- Sit on the floor - --Check if there are any players nearby - local players = ents.FindByClass("Player") - local playernearby = false - for k,v in pairs(players) do - local fardist = 800 - local closedist = 300 - local removedist = 2000 - local iscrouched = v:Crouching() - local dist = v:GetPos():Distance(self:GetPos()) - --Remove ourselves if there's noone nearby - if(dist > 2000) then - playernearby = true - end - --Keep flying away as long as we're being chased - while((dist < fardist and not iscrouched) or (dist < closedist)) do - self:StartActivity(ACT_FLY) - self:PlaySequenceAndWait( "Fly01" ) - - --Find a position in roughly the oposite direction of the player - local tpos = self:GetPos() - local ppos = v:GetPos() - local direction = Vector(tpos.x - ppos.x, tpos.y - ppos.y, tpos.z - ppos.z) - direction:Normalize() - local addition = direction * 1000 - local topos = self:GetPos() + addition - - --Check to make sure we can make it somewhere, so we don't crash - --[[ - if not self.loco:IsAreaTraversable(navmesh.GetNavArea(self:GetPos(),100)) then - print("No way to go forward!") - break - end - --]] - local navarea = navmesh.GetNavArea(self:GetPos(), 100) - - if navarea:IsValid() then - --print("Looks like nav area is valid") - self:MoveToPos(topos) - else - --print("Looks like nav area is invalid") - self:BecomeRagdoll(DamageInfo()) - end - - - - --Check to see if we're being chased - iscrouched = v:Crouching() - dist = v:GetPos():Distance(self:GetPos()) - end - end - if not playernearby then - self:BecomeRagdoll(DamageInfo()) - end - end - coroutine.yield() -end - --These are just here to tell the editors/develoeprs what functions are available.. dont un-comment them out, as this could affect all the items. /* function NPC:OnSpawn() -- cgit v1.2.3-70-g09d2 From a7a1e3a2d23e4aa10d874ad1b663d2116c1f62cb Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 2 Jan 2016 22:23:17 -0500 Subject: More work on ai overhaul --- gamemode/npcsystem/aidirector.lua | 2 ++ gamemode/npcsystem/npcs/bird.lua | 4 +++- gamemode/npcsystem/npcs/zombie.lua | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) (limited to 'gamemode/npcsystem/npcs/bird.lua') diff --git a/gamemode/npcsystem/aidirector.lua b/gamemode/npcsystem/aidirector.lua index 4051204..f5cf2b4 100644 --- a/gamemode/npcsystem/aidirector.lua +++ b/gamemode/npcsystem/aidirector.lua @@ -93,6 +93,8 @@ hook.Add("Tick","SpawnAmbient",function() if (C != CONTENTS_WATER and C != CONTENTS_WATER+CONTENTS_TRANSLUCENT) then --print("Appropriate place found, spawning bird)") local randnpcnum = math.Round(math.Rand(0, #ambientnpcs)) + local npc = GetNpcByName(ambientnpcs[randnpcnum]) + if(npc:) SpawnNpcByName(ambientnpcs[randnpcnum],Pos) break end diff --git a/gamemode/npcsystem/npcs/bird.lua b/gamemode/npcsystem/npcs/bird.lua index 0b05e62..463b8bd 100644 --- a/gamemode/npcsystem/npcs/bird.lua +++ b/gamemode/npcsystem/npcs/bird.lua @@ -60,7 +60,9 @@ NPC.Attacks = { } --A function that takes a position and returns true if this is an acceptable place to spawn -NPC.SpawnLocations = nil +function NPC:SpawnLocations(pos) + return true +end --The entity that is this npc's current target, if it has one. Nil otherwise NPC.Target = nil diff --git a/gamemode/npcsystem/npcs/zombie.lua b/gamemode/npcsystem/npcs/zombie.lua index 7c598fd..216ca43 100644 --- a/gamemode/npcsystem/npcs/zombie.lua +++ b/gamemode/npcsystem/npcs/zombie.lua @@ -89,7 +89,9 @@ function NPC:AttackPriority(ply) end --A function that takes a position and returns true if this is an acceptable place to spawn -NPC.SpawnLocations = nil +function NPC:SpawnLocations(pos) + return true +end --The entity that is this npc's current target, if it has one. Nil otherwise NPC.Target = nil -- cgit v1.2.3-70-g09d2 From f11d53ab5eca356b2eadb0e75b7c1ae94870e033 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 2 Jan 2016 22:31:14 -0500 Subject: Fixed mixup between bird and zombie drops --- gamemode/npcsystem/npcs/base.lua | 10 +++++++--- gamemode/npcsystem/npcs/bird.lua | 21 +++++++++++---------- gamemode/npcsystem/npcs/zombie.lua | 10 +++++++--- 3 files changed, 25 insertions(+), 16 deletions(-) (limited to 'gamemode/npcsystem/npcs/bird.lua') diff --git a/gamemode/npcsystem/npcs/base.lua b/gamemode/npcsystem/npcs/base.lua index 670c476..0e8ea53 100644 --- a/gamemode/npcsystem/npcs/base.lua +++ b/gamemode/npcsystem/npcs/base.lua @@ -7,9 +7,13 @@ NPC.Icon = Material("wintersurvival2/hud/ws1_icons/icon_rock") NPC.Social = "Pack" --Solo, Pack NPC.Stats = { - ["Vitality"] = 100, - ["Speed"] = 50, - ["AwareDist"] = 1000, + ["Vitality"] = 1, + ["Speed"] = 1, + ["AwareDist"] = 1, + ["Accel"] = 1, + ["Decel"] = 1, + ["Step"] = 1, --Step height + ["Hull"] = HULL_HUMAN } --Some npc's like birds have diffent names for their idle sequence diff --git a/gamemode/npcsystem/npcs/bird.lua b/gamemode/npcsystem/npcs/bird.lua index 463b8bd..14eef6a 100644 --- a/gamemode/npcsystem/npcs/bird.lua +++ b/gamemode/npcsystem/npcs/bird.lua @@ -7,9 +7,13 @@ NPC.Icon = Material("wintersurvival2/hud/ws1_icons/icon_rock") NPC.Social = "Pack" --Solo, Pack NPC.Stats = { - ["Vitality"] = 10, - ["Speed"] = 50, - ["AwareDist"] = 800, + ["Vitality"] = 100, + ["Speed"] = 400, + ["AwareDist"] = 1000, + ["Accel"] = 200, + ["Decel"] = 200, + ["Step"] = 20, --Step height + ["Hull"] = HULL_TINY } --Some npc's like birds have diffent names for their idle sequences @@ -19,14 +23,11 @@ NPC.IdleSequences = { } --Drops should be formated as [index]={["item name"], percent_drop} where percent_drop is a number from 0 to 100 + NPC.Drops = { - [0] = {"Meat",100}, - [1] = {"Meat",100}, - [2] = {"Meat",100}, - [3] = {"Meat",100}, - [4] = {"Meat",80}, - [5] = {"Meat",40}, - [6] = {"Meat",10}, + [0] = {"Meat",100},--Birds will drop at least 1 meat, and have a 50% chance of dropping 2 + [1] = {"Meat",50}, + [2] = {"Feather",50}, } --Attacks should be formated as [i]={function attackpriority() = function doattack()} diff --git a/gamemode/npcsystem/npcs/zombie.lua b/gamemode/npcsystem/npcs/zombie.lua index a725a04..f07bd5a 100644 --- a/gamemode/npcsystem/npcs/zombie.lua +++ b/gamemode/npcsystem/npcs/zombie.lua @@ -18,9 +18,13 @@ NPC.Stats = { --Drops should be formated as [index]={["item name"], percent_drop} where percent_drop is a number from 0 to 100 NPC.Drops = { - [0] = {"Meat",100},--Birds will drop at least 1 meat, and have a 50% chance of dropping 2 - [1] = {"Meat",50}, - [2] = {"Feather",50}, + [0] = {"Meat",100}, + [1] = {"Meat",100}, + [2] = {"Meat",100}, + [3] = {"Meat",100}, + [4] = {"Meat",80}, + [5] = {"Meat",40}, + [6] = {"Meat",10}, } --Some npc's like birds have diffent names for their idle sequences -- cgit v1.2.3-70-g09d2 From 8e0e1e34108c1ba8909b186772596f8b1aae9f03 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 2 Jan 2016 23:29:30 -0500 Subject: Fixed up more bugs with the ai overhaul --- entities/entities/ws_arrow/init.lua | 8 ++++ entities/entities/ws_npc_ambient/init.lua | 57 ++++++++++++++++++----------- entities/entities/ws_npc_ambient/shared.lua | 1 - gamemode/npcsystem/npcs/bird.lua | 2 +- 4 files changed, 45 insertions(+), 23 deletions(-) (limited to 'gamemode/npcsystem/npcs/bird.lua') diff --git a/entities/entities/ws_arrow/init.lua b/entities/entities/ws_arrow/init.lua index db5adb6..d87924a 100644 --- a/entities/entities/ws_arrow/init.lua +++ b/entities/entities/ws_arrow/init.lua @@ -24,8 +24,16 @@ function ENT:Think() return true end +function ENT:PhysicsCollide(data, phys) + if phys:GetEntity():IsPlayer() or phys:GetEntity():IsNPC() then + print("We hit a player!") + else + end +end + function ENT:Use(activator, caller, usetype, value) if caller:IsPigeon() then return end + print("Trying to pick up an arrow!") caller:AddItem("Arrow",1) self:Remove() end diff --git a/entities/entities/ws_npc_ambient/init.lua b/entities/entities/ws_npc_ambient/init.lua index a0497dd..6df03a0 100644 --- a/entities/entities/ws_npc_ambient/init.lua +++ b/entities/entities/ws_npc_ambient/init.lua @@ -6,9 +6,15 @@ include('shared.lua') function ENT:Initialize() --print("NPC spawned!") + self:SetMoveType(MOVETYPE_STEP) + self:SetSolid(SOLID_OBB) + self:SetCollisionGroup(COLLISION_GROUP_INTERACTIVE) + 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"] + if(self.Stats["Vitality"]) then + self:SetHealth(self.Stats["Vitality"]) + print("Helath set to " .. 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 @@ -30,27 +36,36 @@ end function ENT:OnInjured(dmg) --print("Taking some dammage") local itempos = self:GetPos() - self:SetHealth(self:Health() - dmg:GetDamage()) + print("Takeing " .. dmg:GetDamage() .. " health from our " .. self:Health()) + --self:SetHealth(self:Health() - dmg:GetDamage()) + print("Health is now" .. self:Health()) if self.OnDammage != nil then self:OnDammage(dmg) end - if self:Health() <= 0 then //run on death - if(CLIENT) then return end - if not self.Drops then return end - --print("Looks like we have some drops") +end + +function ENT:OnContact(ent) + if(ent:GetClass() == "ws_arrow") then + self:TakeDamage(30) + ent:SetParent(self) + end +end + +function ENT:OnKilled(dmg) + if(CLIENT) then return end + if not self.Drops then return end + --print("Looks like we have some drops") + for k,v in pairs(self.Drops) do local rng = math.random(0,100) - for k,v in pairs(self.Drops) do - local itemname = self.Drops[k][1] - local itemchance = self.Drops[k][2] - local heightoffset = 10 - if rng < itemchance then - --print("Createing a " .. itemname) - local drop = ents.Create("ws_item") - drop.Item = GetItemByName(itemname) - drop:SetModel(drop.Item.Model) - drop:SetPos(itempos + (self:GetUp()*heightoffset)) - drop:Spawn() - heightoffset = heightoffset + 10 - end + local itemname = self.Drops[k][1] + local itemchance = self.Drops[k][2] + local heightoffset = 10 + if rng < itemchance then + local drop = ents.Create("ws_item") + drop.Item = GetItemByName(itemname) + drop:SetModel(drop.Item.Model) + drop:SetPos(self:GetPos() + (self:GetUp()*heightoffset)) + drop:Spawn() + heightoffset = heightoffset + 10 end - --self:SetSchedule( SCHED_FALL_TO_GROUND ) - end + end + self:BecomeRagdoll( dmg ) end diff --git a/entities/entities/ws_npc_ambient/shared.lua b/entities/entities/ws_npc_ambient/shared.lua index 7fa3661..c84245c 100644 --- a/entities/entities/ws_npc_ambient/shared.lua +++ b/entities/entities/ws_npc_ambient/shared.lua @@ -3,7 +3,6 @@ ENT.Base = "base_nextbot" //WS stuff ENT.Drops = nil ENT.OnDammage = nil -ENT.Vitality = 1 ENT.Speed = 0 ENT.Model = nil diff --git a/gamemode/npcsystem/npcs/bird.lua b/gamemode/npcsystem/npcs/bird.lua index 14eef6a..ff4c2f2 100644 --- a/gamemode/npcsystem/npcs/bird.lua +++ b/gamemode/npcsystem/npcs/bird.lua @@ -7,7 +7,7 @@ NPC.Icon = Material("wintersurvival2/hud/ws1_icons/icon_rock") NPC.Social = "Pack" --Solo, Pack NPC.Stats = { - ["Vitality"] = 100, + ["Vitality"] = 10, ["Speed"] = 400, ["AwareDist"] = 1000, ["Accel"] = 200, -- cgit v1.2.3-70-g09d2 From d55c70375be34c740cee4a8a86dc3a81249d42f1 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sun, 3 Jan 2016 22:48:10 -0500 Subject: spelling fix in zombie.lua --- gamemode/npcsystem/npcs/bird.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gamemode/npcsystem/npcs/bird.lua') diff --git a/gamemode/npcsystem/npcs/bird.lua b/gamemode/npcsystem/npcs/bird.lua index ff4c2f2..7b19457 100644 --- a/gamemode/npcsystem/npcs/bird.lua +++ b/gamemode/npcsystem/npcs/bird.lua @@ -32,7 +32,7 @@ NPC.Drops = { --Attacks should be formated as [i]={function attackpriority() = function doattack()} local checkrun = function(self,ply) - --If we're awayre of any enemies, run away! + --If we're aware of any enemies, run away! return 1 end local dorun = function(self,ply) -- cgit v1.2.3-70-g09d2