summaryrefslogtreecommitdiff
path: root/lua/autorun/sv_npc.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-08-07 18:22:29 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-08-07 18:22:29 -0400
commitdaa59a7835c350a09dcb207c714acf57828137f3 (patch)
treeae2c00da0e546447ca17a9c5d8492310e5e93f27 /lua/autorun/sv_npc.lua
downloadartery_editor-daa59a7835c350a09dcb207c714acf57828137f3.tar.gz
artery_editor-daa59a7835c350a09dcb207c714acf57828137f3.tar.bz2
artery_editor-daa59a7835c350a09dcb207c714acf57828137f3.zip
Inital Commit
Diffstat (limited to 'lua/autorun/sv_npc.lua')
-rw-r--r--lua/autorun/sv_npc.lua121
1 files changed, 121 insertions, 0 deletions
diff --git a/lua/autorun/sv_npc.lua b/lua/autorun/sv_npc.lua
new file mode 100644
index 0000000..2d7322d
--- /dev/null
+++ b/lua/autorun/sv_npc.lua
@@ -0,0 +1,121 @@
+if CLIENT then return end
+include("town.lua")
+local f = nrequire("concommands.lua")
+local npcs = {} --Master table of npcs
+local autocompletef
+
+function RegisterNPC(npc)
+ assert(npc ~= nil, "Attempted to register a nil npc")
+ assert(npc.Name ~= nil, "Attempted to register an npc without a name")
+ npcs[npc.Name] = npc
+ autocompletef = f.AutocompleteFunction(npcs)
+end
+
+function 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")
+ npc:SetPos(pos)
+
+ for k, v in pairs(npctbl) do
+ npc[k] = v
+ end
+
+ npc:Spawn()
+
+ return npc
+end
+
+--Creates a shop npc with this tbl
+function n.CreateShop(npc)
+ --print("Createing shop npc")
+ local npcent = ents.Create("npc_shop")
+ for k,v in pairs(npc) do
+ npcent[k] = v
+ end
+ npcent:Spawn()
+ --print("Called spawn")
+end
+
+--Creates a townie npc with this tbl
+function n.CreateTownie(tbl)
+ local npcent = ents.Create("npc_townie")
+ for k, v in pairs(tbl) do
+ npcent[k] = v
+ end
+ npcent:Spawn()
+end
+
+--Creates a new navigation node for npc's
+function n.CreateNavNode(tbl)
+ local nodeent = ents.Create("info_townienode")
+ assert(tbl ~= nil, "Tried to create a nil navnode")
+ for k, v in pairs(tbl) do
+ nodeent[k] = v
+ end
+ nodeent:Spawn()
+end
+
+--Ents to remove when refreshing the npc map
+local removeents = {"npc_townie", "info_townienode", "npc_shop"}
+
+-- "art_chest",
+for k, v in pairs(removeents) do
+ local eot = ents.FindByClass(v)
+ for i, j in pairs(eot) do
+ j:Remove()
+ end
+end
+
+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
+
+local function loadMap()
+ 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()
+ loadMap()
+end)
+
+concommand.Add("artery_reloadmap", function()
+ for k, v in pairs(removeents) do
+ local eot = ents.FindByClass(v)
+
+ for i, j in pairs(eot) do
+ j:Remove()
+ end
+ end
+
+ loadMap()
+end)
+
+concommand.Add("artery_makenpc", function(ply, cmd, args)
+ if not ply:IsAdmin() then return end
+ local na = args[1]
+ n.CreateNPCByName(na, ply:GetEyeTrace().HitPos)
+end, autocompletef)
+
+return n