summaryrefslogtreecommitdiff
path: root/lua/autorun/town.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/autorun/town.lua')
-rw-r--r--lua/autorun/town.lua141
1 files changed, 141 insertions, 0 deletions
diff --git a/lua/autorun/town.lua b/lua/autorun/town.lua
new file mode 100644
index 0000000..2805f7c
--- /dev/null
+++ b/lua/autorun/town.lua
@@ -0,0 +1,141 @@
+--[[
+ This file loads and saves towns and stuff
+]]
+if engine.ActiveGamemode() ~= "sandbox" then return end
+print("Hello from town.lua!")
+local currentfile
+--A fake nrequire
+local npcs = {}
+local fakes = {
+ ["sv_npcsystem.lua"] = {
+ CreateTownie = function(tbl)
+ print("got create townie table:")
+ PrintTable(tbl)
+ print("Spawning townie!")
+ --Make sure this townie dosen't already exist
+ for k,v in pairs(ents.FindByClass("info_towniespawn")) do
+ if v.File == currentfile then return end
+ end
+ --Spawn the townie editor
+ local e = ents.Create("info_towniespawn")
+ e:SetPos(tbl.Pos)
+ e:Spawn()
+ e.File = currentfile
+ e.data = {}
+ for k,v in pairs(tbl.NavNodes) do
+ e.data[#e.data + 1] = k
+ end
+ print("Ent's data is")
+ PrintTable(e.data)
+ end,
+ CreateNavNode = function(tbl)
+ print("got navnode table:",tbl)
+ PrintTable(tbl)
+ --Make sure this nav node dosen't already exist
+ for k,v in pairs(ents.FindByClass("info_edit_townienode")) do
+ print("Looking for nodes that connect to ", currentfile, v.File)
+ if v.File == currentfile then return end
+ end
+ --Spawn this navnode
+ local e = ents.Create("info_edit_townienode")
+ e:SetPos(tbl.Position)
+ e:Spawn()
+ e.File = currentfile
+ e.Name = tbl.Name
+ end,
+ CreateShop = function(tbl)
+ --Make sure this townie dosen't already exist
+ for k,v in pairs(ents.FindByClass("info_townieshop")) do
+ if v.File == currentfile then return end
+ end
+ --Spawn the townie editor
+ local e = ents.Create("info_towniespawn")
+ e:SetPos(tbl.Pos)
+ e:Spawn()
+ e.File = currentfile
+ end,
+ RegisterNPC = function(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,
+ CreateNPCByName = function(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
+ },
+ ["inventory/item.lua"] = {
+ GetItemByName = function(string)
+ return {}
+ end
+ }
+}
+fakes["core/npc/sv_npcsystem.lua"] = fakes["sv_npcsystem.lua"]
+local calls = {}
+function nrequire(string)
+ print("nrequire called")
+
+ local ntbl = {}
+ local nmeta = {}
+ nmeta.__index = function(self,key)
+ print("a table from nrequire was called",string,key)
+ local record = calls[string]
+ record[#record+1] = key
+ print("REturning", fakes[string][key])
+ return fakes[string][key]
+ end
+ setmetatable(ntbl,nmeta)
+ calls[string] = ntbl
+ return ntbl
+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
+
+function loadtownies()
+ --Remove any current townie things
+ for k,v in pairs(fakes) do
+ for i,j in pairs(ents.FindByClass(k)) do
+ j:Remove()
+ 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)
+ currentfile = path
+ CompileString(filetxt,path)()
+ print("I want to execute",path)
+ end)
+end
+
+concommand.Add("artery_loadtownies",loadtownies)
+hook.Add("InitPostEntity","load_lua",loadtownies)