summaryrefslogtreecommitdiff
path: root/lua/entities/info_programmable_base
diff options
context:
space:
mode:
Diffstat (limited to 'lua/entities/info_programmable_base')
-rw-r--r--lua/entities/info_programmable_base/cl_init.lua124
-rw-r--r--lua/entities/info_programmable_base/init.lua148
-rw-r--r--lua/entities/info_programmable_base/shared.lua12
3 files changed, 284 insertions, 0 deletions
diff --git a/lua/entities/info_programmable_base/cl_init.lua b/lua/entities/info_programmable_base/cl_init.lua
new file mode 100644
index 0000000..fc1ada0
--- /dev/null
+++ b/lua/entities/info_programmable_base/cl_init.lua
@@ -0,0 +1,124 @@
+if engine.ActiveGamemode() ~= "sandbox" then return end
+
+include('shared.lua')
+
+--Don't draw our hull model
+function ENT:Draw()
+end
+
+local function draw_edit_panel(filename,who)
+ if not GCompute then error("GCompute not installed!") end
+ if not GCompute.IDE then error("GCompute was initalized wrong or has changed, update this file!") end
+ local inst = GCompute.IDE.GetInstance()
+ inst:SetVisible (true)
+
+ print("Opening file browser!")
+
+ --Remove donation panel
+ for k,v in pairs(inst.ViewManager.Views) do
+ if type(k) == "table" and k.Title == "Donate!" then
+ k:SetVisible(false)
+ end
+ end
+
+ local fileuri = "game/data/" .. filename
+ print("Trying to open","game/data/" .. filename)
+
+ --Open up this npc's file
+ inst:OpenFile(fileuri,function(succ,res,view)
+ view:Select()
+ end)
+
+ --Set our file browser to the right place
+ for k,v in pairs(inst.ViewManager.Views) do
+ if k.Title == "File Browser" then
+
+ k.FolderListView:SetPath(string.GetPathFromFilename(fileuri))
+ end
+ end
+
+ --Detour it's save to let the server know the file was saved.
+ local doc = inst.DocumentManager.DocumentsByUri[fileuri]
+ print("Doc was",doc)
+ doc:AddEventListener("Saved",function()
+ print("Saved",doc)
+ net.Start("edit_sendsave")
+ net.WriteString(doc.Uri)
+ net.WriteEntity(who)
+ net.SendToServer()
+ end)
+
+
+end
+
+function Reload_Gcompute(what)
+ print("attempting to refresh instances of ", what)
+ local inst = GCompute.IDE:GetInstance()
+ for k,v in pairs(inst.ViewManager.ViewsById) do
+ if v.Title == what then
+ v:CreateFileChangeNotificationBar ()
+ v.FileChangeNotificationBar:DispatchEvent("ReloadRequested")
+ local oldvisible = v.FileChangeNotificationBar.SetVisible
+ v.FileChangeNotificationBar.SetVisible = function(self,bool)
+ if bool then
+ self.SetVisible = oldvisible
+ end
+ end
+ break
+ end
+ end
+end
+
+net.Receive("edit_notify_file_changed",function()
+ local what = net.ReadString()
+ Reload_Gcompute(what)
+end)
+
+net.Receive("edit_confirmremove",function()
+ local who = net.ReadString()
+ local frame = vgui.Create( "DFrame" )
+ frame:SetSize( 300, 250 )
+ frame:Center()
+ frame:MakePopup()
+
+ local but = vgui.Create( "DButton", frame )
+ but:SetText( "\nYes, I want to remove\n" .. who .. "\n")
+ but:Dock(TOP)
+ but.DoClick = function() // A custom function run when clicked ( note the . instead of : )
+ net.Start("edit_removeconfirmed")
+ net.WriteString(who)
+ net.SendToServer()
+ frame:Close()
+ end
+
+ local but2 = vgui.Create("DButton",frame)
+ but2:SetText("\nNo I don't want to remove\n" .. who .. "\n")
+ but2:Dock(BOTTOM)
+ but2.DoClick = function() // A custom function run when clicked ( note the . instead of : )
+ net.Start("edit_removedeny")
+ net.WriteString(who)
+ net.SendToServer()
+ frame:Close()
+ end
+ but:SizeToContents()
+ but2:SizeToContents()
+end)
+
+net.Receive("edit_sendopen",function()
+
+ print('got request to edit someone')
+ local file = net.ReadString()
+ local who = net.ReadEntity()
+ print(file)
+ draw_edit_panel(file,who)
+end)
+
+hook.Add( "Think", "edit_type_tip", function()
+ local tr = LocalPlayer():GetEyeTrace()
+ local e = tr.Entity
+ if e.Entity then
+ AddWorldTip( nil, e:GetClass(), nil, tr.HitPos, e )
+ end
+
+
+end )
diff --git a/lua/entities/info_programmable_base/init.lua b/lua/entities/info_programmable_base/init.lua
new file mode 100644
index 0000000..9fed830
--- /dev/null
+++ b/lua/entities/info_programmable_base/init.lua
@@ -0,0 +1,148 @@
+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')
+
+local file_base = string.format("artery/maps/%s",game.GetMap())
+
+local hulls = {
+ [HULL_HUMAN] = {
+ Model = "models/props_phx/construct/metal_tubex2.mdl",
+ Angle = Angle(0,0,0)
+ },
+ [HULL_TINY] = {
+ Model = "models/props_phx/construct/metal_tube.mdl",
+ Angle = Angle(0,0,0)
+ }
+}
+
+function ENT:Initialize()
+ --The hull
+ local thull = hulls[self.edit_data.Size]
+ self:SetModel(thull.Model)
+ self:SetAngles(thull.Angle)
+
+ --The entity
+ self:PhysicsInit( SOLID_VPHYSICS ) -- Make us work with physics,
+ self:SetMoveType( MOVETYPE_VPHYSICS ) -- after all, gmod is a physics
+ self:SetSolid( SOLID_VPHYSICS ) -- Toolbox
+ --[[
+ local phys = self:GetPhysicsObject()
+ if (phys:IsValid()) then
+ phys:Wake()
+ end
+ ]]
+ self:SetCollisionGroup(COLLISION_GROUP_WORLD)
+ self:SetUseType(SIMPLE_USE)
+
+ print("My edit data:",self)
+ PrintTable(self.edit_data)
+
+ --The visual
+ local e = ents.Create("prop_dynamic")
+ e:SetPos(self:GetPos())
+ e:SetModel(self.edit_data.Model)
+ if self.edit_data.OnSpawn ~= nil then
+ self.edit_data.OnSpawn(self)
+ end
+ e:SetParent(self)
+ e:Spawn()
+
+ --Make sure the folder we want to be in exists
+ local grouping = self.edit_data.Type or ""
+ local folder = string.format("%s/%s",file_base,grouping)
+ if not file.Exists(folder,"DATA") then
+ print("Createing dir",folder)
+ file.CreateDir(folder)
+ end
+
+ --Make sure whoever made us has attached a file
+ if self.File == nil then
+ print("Createing a new file for",self)
+ local c = self:GetClass()
+ local ind = #ents.FindByClass(c)
+ print("Found",ind,"number of",c)
+ self.File = string.format("%s/%s_%d.txt",folder,c,ind)
+ end
+ assert(self.File, "A programmable entity was made without a file attached!")
+
+end
+
+hook.Add("OnPhysgunFreeze","programmable_freezer",function(wep,phys,ent,ply)
+ print("I want to do something to ",ent)
+ if ent.RefreshChangeables ~= nil then
+ ent:RefreshChangeables()
+ end
+end)
+
+util.AddNetworkString("edit_notify_file_changed")
+function ENT:notify_file_changed(what)
+ net.Start("edit_notify_file_changed")
+ net.WriteString(string.GetFileFromFilename(what))
+ net.Broadcast()
+end
+
+function ENT:RefreshChangeables()
+ print("I",self,"Want to refresh my changeables in",self.File)
+ local filetxt = file.Read(self.File,"DATA")
+ print("filetxt was",filetxt)
+ if not filetxt then return end --We haven't pressed e to generate the file yet
+ --Oh god this is one hell of a pattern. Just trust that it works I guess
+ local pos = self:GetPos()
+ local ang = self:GetAngles()
+ local ntxt = filetxt
+ :gsub("Vector[^\n]*%-%-@tagpos",string.format("Vector(%f,%f,%f), --@tagpos",pos.x,pos.y,pos.z))
+ :gsub("Angle[^\n]*%-%-@tagang",string.format("Angle(%f,%f,%f), --@tagang",ang.p,ang.y,ang.r))
+ print("Writeing to",self.File,ntxt)
+ file.Write(self.File,ntxt)
+ self:notify_file_changed(self.File)
+end
+
+util.AddNetworkString("edit_confirmremove")
+util.AddNetworkString("edit_removeconfirmed")
+util.AddNetworkString("edit_removedeny")
+function ENT:OnRemove()
+ net.Start("edit_confirmremove")
+ net.WriteString(self.File)
+ net.Send(Entity(1))
+ return false
+end
+net.Receive("edit_removeconfirmed",function()
+ local who = net.ReadString()
+ file.Delete(who)
+end)
+net.Receive("edit_removedeny",function()
+ local who = net.ReadString()
+ loadtownies()
+end)
+
+util.AddNetworkString("edit_sendopen")
+function ENT:Use( activator, caller )
+ --Open up the npc's file (create it if it dosen't already exist)
+ if not file.Exists(self.File, "DATA") then
+ file.Write(self.File,self.edit_data.get_default_code(self))
+ end
+
+ net.Start("edit_sendopen");
+ net.WriteString(self.File)
+ net.WriteEntity(self)
+ net.Send(caller)
+end
+
+util.AddNetworkString("edit_sendsave")
+net.Receive("edit_sendsave",function()
+ local file = net.ReadString()
+ local who = net.ReadEntity()
+ print("I got a save for", file, who)
+ who:OnSave()
+end)
+
+function ENT:OnSave()
+ print("On save was called!")
+end
+
+function ENT:Think()
+-- We don't need to think, we are just a prop after all!
+end
diff --git a/lua/entities/info_programmable_base/shared.lua b/lua/entities/info_programmable_base/shared.lua
new file mode 100644
index 0000000..86de4d2
--- /dev/null
+++ b/lua/entities/info_programmable_base/shared.lua
@@ -0,0 +1,12 @@
+if engine.ActiveGamemode() ~= "sandbox" then return end
+
+ENT.Base = "base_entity"
+
+ENT.PrintName= "Programmable base"
+ENT.Author= "Apickx"
+ENT.Contact= "cogarr.net"
+ENT.Purpose= "A base for other programmables"
+ENT.Instructions= "Don't use this probably!"
+ENT.Spawnable = false
+ENT.AdminSpawnable = false
+ENT.Category = "Artery"