summaryrefslogtreecommitdiff
path: root/lua/entities/info_towniespawn/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/entities/info_towniespawn/init.lua')
-rw-r--r--lua/entities/info_towniespawn/init.lua125
1 files changed, 125 insertions, 0 deletions
diff --git a/lua/entities/info_towniespawn/init.lua b/lua/entities/info_towniespawn/init.lua
new file mode 100644
index 0000000..a785212
--- /dev/null
+++ b/lua/entities/info_towniespawn/init.lua
@@ -0,0 +1,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
+]]