summaryrefslogtreecommitdiff
path: root/lua/autorun/town.lua
blob: 40131df710c02655699c5f68b05747dcca9a4c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
--[[
	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",function(ply,cmd,args)
	if not ply:IsAdmin() then return end
	loadtownies()
end)
hook.Add("InitPostEntity","load_lua",loadtownies)