aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/npc
diff options
context:
space:
mode:
Diffstat (limited to 'gamemode/core/npc')
-rw-r--r--gamemode/core/npc/sv_common.lua38
-rw-r--r--gamemode/core/npc/sv_huntingspawner.lua118
-rw-r--r--gamemode/core/npc/sv_npcsystem.lua40
3 files changed, 181 insertions, 15 deletions
diff --git a/gamemode/core/npc/sv_common.lua b/gamemode/core/npc/sv_common.lua
new file mode 100644
index 0000000..e37a5f8
--- /dev/null
+++ b/gamemode/core/npc/sv_common.lua
@@ -0,0 +1,38 @@
+--[[
+ Some common functions that a lot of npcs use, take out here to make fixing bugs easier.
+]]
+
+local com = {}
+
+com.pausefor10sec = function(npc)
+ npc.StartActionTime = CurTime() + 10
+ npc:SetSequence(npc:LookupSequence("idle"))
+ npc.loco:FaceTowards(Vector(-343, 148, 565))
+ local oyaw,oacc = npc.loco:GetMaxYawRate(), npc.loco:GetAcceleration()
+ timer.Simple(0,function()
+ npc.loco:SetMaxYawRate(0)
+ npc.loco:SetAcceleration(0)
+ npc.loco:SetVelocity(Vector(0,0,0))
+ end)
+ timer.Simple(10, function()
+ npc.loco:SetMaxYawRate(oyaw)
+ npc.loco:SetAcceleration(oacc)
+ end)
+end
+
+com.is10secdone = function(npc)
+ return npc.StartActionTime < CurTime()
+end
+
+com.Rumors = {
+ "This is a rumor!",
+ "Here is another!",
+ "And yet another!",
+}
+
+com.GetRumor = function()
+ local rng = math.random(#com.Rumors)
+ return com.Rumors[rng]
+end
+
+return com
diff --git a/gamemode/core/npc/sv_huntingspawner.lua b/gamemode/core/npc/sv_huntingspawner.lua
new file mode 100644
index 0000000..283b27f
--- /dev/null
+++ b/gamemode/core/npc/sv_huntingspawner.lua
@@ -0,0 +1,118 @@
+--[[
+ This file spawns the huntable npc's in their zones.
+]]
+local track = nrequire("core/inventory/sv_invtracker.lua")
+local itm = nrequire("core/inventory/item.lua")
+local o = {}
+
+local huntablenodes = {}
+function o.CreateSpawnNode(tbl)
+ huntablenodes[#huntablenodes+1] = tbl
+ local e = ents.Create("info_huntablespawn")
+ e:SetPos(tbl.Position)
+end
+
+local function SpawnMonsterFor(ply,zone)
+ --Check what hunting ground we're in
+ print("I want to attack",ply)
+ if zone == nil then return end
+ print("I am in a zone!")
+ local possiblenpcs = zone.npctbl
+ PrintTable(possiblenpcs)
+
+ local randnum = math.random(0,100)
+ print("Random num was",randnum)
+ local npctype
+ for k,v in pairs(possiblenpcs) do
+ randnum = randnum - v
+ print("Subtracting ", v , " for ",k)
+ if randnum < 0 then
+ npctype = k
+ break
+ end
+ end
+
+ if npctype == nil then
+ print(ply,"got lucky this time...")
+ return
+ end
+ print("I will spawn a ",npctype,"to attack ",ply,"!")
+
+ --Find a place for the npc to spawn that's out of sight!
+ local potentialspots = ents.FindInSphere( ply:GetPos(), 4000 )
+ for k,v in pairs(potentialspots) do
+ print("Checking spot",v)
+ if v:GetClass() ~= "info_huntablespawn" then
+ print("Was not an info_huntablespawn")
+ potentialspots[k] = nil
+ else
+ local tr = util.TraceLine({
+ start = v:GetPos() + Vector(0,0,50),
+ endpos = ply:GetPos() + Vector(0,0,64),
+ })
+ if tr.Hit and tr.Entity == ply then
+ potentialspots[k] = nil
+ print("Player could see this point")
+ end
+ end
+ end
+
+ local a = {}
+ for k,v in pairs(potentialspots) do a[#a+1] = v end
+
+ --Choose a random spot!
+ local spawnpos = a[math.random(1,#a)]
+
+ print("I want to spawn a monster at", spawnpos)
+ if spawnpos == nil then
+ print("Couldn't find a spot to spawn an NPC around",ply,"at",ply:GetPos(),"make sure there are enough info_huntablespawn entities around in little corners and stuff")
+ return
+ end
+ --Do a trace up to hit the skybox,
+ local npc = ents.Create(npctype)
+ npc:SetPos(spawnpos:GetPos())
+ npc:SetEnemy(ply)
+ npc:Spawn()
+end
+
+local lastspawned = {}
+for k,v in pairs(player.GetAll()) do lastspawned[v] = CurTime() end
+hook.Add("PlayerInitialSpawn","add_to_huntable_tbl",function(ply)
+ lastspawned[ply] = CurTime()
+end)
+hook.Add("Tick","occasionally_spawn_monsters",function()
+ for _,ply in pairs(player.GetAll()) do
+ local zone = ply:GetCurrentZone( "artery_huntingground" )
+ if zone == nil then continue end
+ if CurTime() - lastspawned[ply] > zone.SpawnRate then
+ SpawnMonsterFor(ply,zone)
+ lastspawned[ply] = CurTime()
+ end
+ end
+end)
+
+local external_drops = {}
+function o.RegisterDrops(npcname,tbl)
+ assert(external_drops[npcname] == nil, string.format("Tried to register 2 drop tables for the npc %q",npcname))
+ external_drops[npcname] = tbl
+end
+
+hook.Add("OnNPCKilled","droplootforexnpcs",function(npc,attacker,inflictor)
+ local d = external_drops[npc:GetClass()]
+ if d == nil then return end
+
+ for k, v in pairs(d) do
+ local rng = math.random(0, 100)
+ local itemname = v[1]
+ local itemchance = v[2]
+ local heightoffset = 10
+
+ if rng < itemchance then
+ local drop = itm.GetItemByName(itemname)
+ print("Createing a drop of",drop)
+ track.CreateDroppedItem(drop, self:GetPos()+Vector(math.random(20),math.random(20),20))
+ end
+ end
+end)
+
+return o
diff --git a/gamemode/core/npc/sv_npcsystem.lua b/gamemode/core/npc/sv_npcsystem.lua
index 1abbc67..78b041f 100644
--- a/gamemode/core/npc/sv_npcsystem.lua
+++ b/gamemode/core/npc/sv_npcsystem.lua
@@ -11,6 +11,7 @@ function n.RegisterNPC(npc)
end
function n.CreateNPCByName(npcname, pos)
+ assert(npcs[npcname],string.format("No npc named %q, valid names are:\n%s",npcname,table.concat(table.GetKeys(npcs),"\n")))
print("Createing a ", npcname, " at ", pos)
local npctbl = npcs[npcname]
local npc = ents.Create("npc_huntable")
@@ -66,23 +67,32 @@ for k, v in pairs(removeents) do
end
end
-local mapfields = {"navnodes", "npcs"}
+local function ExecuteOnFolder(dir, recursive, func)
+ local path = ""
+ local fpath = table.concat({path,dir,"/*"})
+ local files, directories = file.Find(fpath,"DATA")
+ for k,v in pairs(files) do
+ local callpath = table.concat({path,dir,"/",v})
+ func(callpath)
+ end
+ if not recursive then return end
+ for k,v in pairs(directories) do
+ local npath = table.concat({dir,"/",v})
+ ExecuteOnFolder(npath,true,func)
+ end
+end
--- "chests",
local function loadMap()
- for k, v in ipairs(mapfields) do
- local mapname = game.GetMap()
- local fpath = string.format("artery/maps/%s/%s/*", mapname, v)
- local files, dirs = file.Find(fpath, "DATA")
-
- for i, j in pairs(files) do
- if string.GetExtensionFromFilename(j) ~= "lua" then continue end
- local itempath = string.format("artery/maps/%s/%s/%s", mapname, v, j)
- local itemtxt = file.Read(itempath, "DATA")
- assert(itemtxt ~= nil, "Found a file, but it looks like it can't be compiled:" .. itempath)
- CompileString(itemtxt, itempath)()
- end
- end
+ local mapname = game.GetMap()
+
+ local foldername = "artery/maps/" .. mapname
+ ExecuteOnFolder(foldername,true,function(path)
+ print("I want to run",path)
+ local filetxt = file.Read(path,"DATA")
+ print("File text is", filetxt)
+ CompileString(filetxt,path)()
+ print("I want to execute",path)
+ end)
end
hook.Add("InitPostEntity", "artery_spawnmapnpcs", function()