summaryrefslogtreecommitdiff
path: root/lua/entities/info_towniespawn
diff options
context:
space:
mode:
Diffstat (limited to 'lua/entities/info_towniespawn')
-rw-r--r--lua/entities/info_towniespawn/cl_init.lua109
-rw-r--r--lua/entities/info_towniespawn/init.lua125
-rw-r--r--lua/entities/info_towniespawn/shared.lua12
3 files changed, 246 insertions, 0 deletions
diff --git a/lua/entities/info_towniespawn/cl_init.lua b/lua/entities/info_towniespawn/cl_init.lua
new file mode 100644
index 0000000..178160c
--- /dev/null
+++ b/lua/entities/info_towniespawn/cl_init.lua
@@ -0,0 +1,109 @@
+if engine.ActiveGamemode() ~= "sandbox" then return end
+
+include('shared.lua')
+
+function ENT:Draw()
+ --self:DrawModel() -- Draws Model Client Side
+end
+
+function ENT:Hint()
+ AddWorldTip( self:EntIndex(), "Spawnpoint", 0.5, self:GetPos(), self )
+end
+
+local navnodes = {}
+local selectors = {}
+
+local function sync_navnode_table(who)
+ print("Synching navnodes, table is")
+ PrintTable(navnodes)
+ net.Start("edit_townie_navchange")
+ net.WriteEntity(who)
+ net.WriteTable(navnodes)
+ net.SendToServer()
+end
+
+local function add_nav_item(layout,name,elist,who)
+ print("Adding nav item",layout,name,elist)
+ local holder = vgui.Create("DPanel",layout)
+
+ local selector = vgui.Create("DComboBox",holder)
+ selector.num = #navnodes + 1
+ print("adding the ", selector.num, "selector")
+ selectors[selector.num] = selector
+ selector:Dock(FILL)
+ for k,v in pairs(elist) do
+ print("Adding option",v)
+ selector:AddChoice(v)
+ end
+ if name ~= nil then
+ selector:SetValue(name)
+ navnodes[#navnodes+1] = name
+ end
+
+ selector:SetWidth(150)
+
+ selector.OnSelect = function(self,index,value)
+ print(value,"selected","on",selector.num)
+ navnodes[selector.num] = value
+ sync_navnode_table(who)
+ end
+
+ local delete = vgui.Create("DButton",holder)
+ delete:Dock(RIGHT)
+ delete:SetText("-")
+ delete.DoClick = function(self)
+ for i = selector.num,#navnodes do
+ navnodes[i] = navnodes[i+1]
+ selectors[i] = selectors[i+1]
+ if IsValid(selectors[i]) then
+ selectors[i].num = selectors[i].num - 1
+ end
+ end
+ holder:Remove()
+ sync_navnode_table(who)
+ end
+
+ layout:Add(holder)
+end
+
+net.Receive("edit_townie_use",function()
+ print("I want to make the edit panel for a townie")
+ navnodes = {}
+ local who = net.ReadEntity()
+ local tbl = net.ReadTable() --Current nav nodes for this townie
+ local posi = net.ReadTable() --A list of all nav nodes in the map
+ local editorframe = vgui.Create( "DFrame" )
+ editorframe:SetPos( 100, 100 )
+ editorframe:SetSize( 300, 200 )
+ editorframe:SetTitle( "Edit" .. tostring(who) )
+ editorframe:SetDraggable( true )
+ editorframe:MakePopup()
+
+ local editor = vgui.Create("DPanel",editorframe)
+ editor:Dock(FILL)
+
+ local layout = vgui.Create( "DListLayout", editor )
+ layout:Dock(FILL)
+
+ print("Already setup navnodes:",tbl)
+ PrintTable(tbl)
+
+ for k,v in pairs(tbl) do
+ print("adding",layout,v,slist)
+ add_nav_item(layout,v,posi,who)
+ end
+
+ print("After creation, navnodes are")
+ PrintTable(navnodes)
+
+ local addbutton = vgui.Create("DButton",layout)
+ addbutton:Dock(BOTTOM)
+ addbutton:SetText("+")
+ addbutton.DoClick = function()
+ add_nav_item(layout,nil,posi,who)
+ end
+
+ layout:SetSize( 100, 100 )
+ layout:SetPos( 20, 50 )
+
+end)
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
+]]
diff --git a/lua/entities/info_towniespawn/shared.lua b/lua/entities/info_towniespawn/shared.lua
new file mode 100644
index 0000000..ea7ac99
--- /dev/null
+++ b/lua/entities/info_towniespawn/shared.lua
@@ -0,0 +1,12 @@
+if engine.ActiveGamemode() ~= "sandbox" then return end
+
+ENT.Base = "info_programmable_base"
+
+ENT.PrintName= "Townie Spawn"
+ENT.Author= "Apickx"
+ENT.Contact= "cogarr.net"
+ENT.Purpose= "Set a spawn point"
+ENT.Instructions= "Make sure there's a 32x32 box for players to spawn!"
+ENT.Spawnable = true
+ENT.AdminSpawnable = false
+ENT.Category = "Artery"