summaryrefslogtreecommitdiff
path: root/lua/entities/info_towniespawn/init.lua
blob: a78521225a459f01b8be41ee53393b4aca4a047a (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
if engine.ActiveGamemode() ~= "sandbox" then return end

AddCSLuaFile( "cl_init.lua" ) -- Make sure clientside
AddCSLuaFile( "shared.lua" ) -- and shared scripts are sent.

include('shared.lua')

ENT.default_data = {
	Model = "models/humans/Group02/Male_03.mdl",
	NavNodes = {},
	Pos = nil
}

ENT.edit_data = {
	Size = HULL_HUMAN,
	Type = "npc",
	Model = "models/editor/playerstart.mdl",
	get_default_code = function(who)
		local default_townie = [[




local function ChatGreat(ply)
	return {
		Name = "You can change the name midway!",
		Message = "That's wonderful!",
		Options = {}
	}
end

local npc = {
	["Pos"] = Vector(%f,%f,%f), --@tagpos
	["Ang"] = Angle(%f,%f,%f), --@tagang
	["Model"] = %q,
	["Name"] = "%s",
	["NavNodes"] = { --@tagnodes
		
	},
	["getDialogFor"] = function(ply)
		return {
			["Name"] = "%s",
			["Message"] = "Hey, how are you doing," .. ply:Nick(),
			["Options"] = {
				["Great!"] = ChatGreat
			}
		}
	end
}
nrequire("sv_npcsystem.lua").CreateTownie(npc)
		]]
		local pos = who:GetPos()
		local ang = who:GetAngles()
		local model = "models/humans/Group02/Male_03.mdl"
		local name = "Default Townie"
		return string.format(default_townie,pos.x,pos.y,pos.z,ang.p,ang.y,ang.r,model,name,name)
	end,
	OnSpawn = function(self)
		print("Onspawn of", self, "called")
		if not self.data then self.data = {} end
	end
}

local last_townie_use = CurTime()
hook.Add("FindUseEntity","hook_towniespawn_use",function(ply,defaultent)
	--print("Looking at", IsValid(defaultent), ":", defaultent:GetClass(), ":", last_townie_use + 2, CurTime())
	if not IsValid(defaultent) or defaultent:GetClass() ~= "info_towniespawn" or last_townie_use + 2 > CurTime() then return end
	print("Townie's use is called!")
	net.Start("edit_townie_use")
	net.WriteEntity(defaultent)
	local ntbl = {}
	for k,v in pairs(defaultent.data) do
		ntbl[#ntbl+1] = v
	end
	print("Sending data to client:")
	PrintTable(ntbl)
	net.WriteTable(ntbl)
	local allnodes = ents.FindByClass("info_edit_townienode")
	local stbl = {}
	for k,v in pairs(allnodes) do
		stbl[#stbl+1] = v.Name
	end
	net.WriteTable(stbl)
	net.Send(ply)
	last_townie_use = CurTime()
end)

util.AddNetworkString("edit_townie_navchange")
net.Receive("edit_townie_navchange",function()
	local who = net.ReadEntity()
	local newnodes = net.ReadTable()
	local thisfile = who.File
	assert(thisfile,"Failed to find file for entity!")
	local filetxt = file.Read(thisfile)
	assert(filetxt,"No such file:" .. thisfile)
	local stbl = {}
	print("newnodes is:")
	PrintTable(newnodes)
	for k,v in pairs(newnodes) do
		stbl[#stbl + 1] = string.format("[\"%s\"] = true",v)
	end
	print("nanodes to format are:")
	PrintTable(stbl)
	local nntxt = table.concat(stbl,",\n\t\t")
	print("nntxt is", nntxt)
	local subtxt = string.format([[
["NavNodes"] = { --@tagnodes
		%s
	}]],nntxt)
	local newtxt, count = filetxt:gsub("%[\"NavNodes\"%] = { %-%-@tagnodes.-}",subtxt)
	assert(count > 0, "Failed to find text to replace.")
	print("I want to write",newtxt)
	file.Write(who.File,newtxt)
	who.data = newnodes
	who:notify_file_changed(who.File)
end)

util.AddNetworkString("edit_townie_use")
--[[
local init = ENT.Initalize
function ENT:Initalize()
	print("In towniespawn's initalize")
	init(self)
end
]]