summaryrefslogtreecommitdiff
path: root/lua/entities/info_programmable_base/init.lua
blob: 9fed8301724b88ec56b22529c4fd47be16bc518f (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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