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