-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathconvert.lua
More file actions
281 lines (204 loc) · 5.91 KB
/
Copy pathconvert.lua
File metadata and controls
281 lines (204 loc) · 5.91 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- This part is for converting between map and wire
-- Per type converting functions for
-- converting from map inputs to wire outputs. (String to Value)
local function isEqualList(_, listA, listB)
if listA == listB then
return true
end
if not listA or not listB then
return false
end
if #listA ~= #listB then
return false
end
for i, va in ipairs(listA) do
local vb = listB[i]
if va ~= vb then
return false
end
end
return true
end
local function isEqualGeneric(_, varA, varB)
return varA == varB
end
local function isEqualEntity(_, entA, entB)
if not IsValid(entA) or not IsValid(entB) then
return false
end
return entA == entB
end
local g_supportedTypesById = {
[0] = { -- Number, default
wireType = "NORMAL",
toHammer = function(_, wireValue)
wireValue = tonumber(wireValue or 0) or 0
return string.format("%.20g", wireValue)
end,
toWire = function(_, hammerValue)
return tonumber(hammerValue or 0) or 0
end,
wireIsEqual = function(_, wireValueA, wireValueB)
wireValueA = tonumber(wireValueA)
wireValueB = tonumber(wireValueB)
if not wireValueA then
return false
end
if not wireValueB then
return false
end
return wireValueA == wireValueB
end
},
[1] = { -- Number, toggle
wireType = "NORMAL",
-- Wire Input: Trigger Hammer output only if true. Does not pass the input value from Wire to Hammer.
-- Wire Output: Toggle the Wire Output when triggered by Hammer. Does not pass the input value from Hammer or Wire.
isToggle = true,
toHammer = function(_, wireValue) -- Return a boolean, 0 = false, 1 = true, useful for toggling. It triggers Hammer output only if true.
return tobool(wireValue)
end,
toWire = function(_, hammerValue) -- Is being switched between 0 and 1 each call from Hammer input.
return tobool(hammerValue) and 1 or 0
end,
wireIsEqual = isEqualGeneric,
},
[2] = { -- String
wireType = "STRING",
toHammer = function(_, wireValue)
return tostring(wireValue or "")
end,
toWire = function(_, hammerValue)
return hammerValue or ""
end,
wireIsEqual = isEqualGeneric,
},
[3] = { -- 2D Vector
wireType = "VECTOR2",
toHammer = function(_, wireValue)
wireValue = wireValue or {0, 0}
local x = tonumber(wireValue[1] or 0) or 0
local y = tonumber(wireValue[2] or 0) or 0
return string.format("%.20g %.20g", x, y)
end,
toWire = function(_, hammerValue)
local x, y = unpack(string.Explode(" ", hammerValue or ""))
x = tonumber(x or 0) or 0
y = tonumber(y or 0) or 0
return {x, y}
end,
wireIsEqual = isEqualList,
},
[4] = { -- 3D Vector
wireType = "VECTOR",
toHammer = function(_, wireValue)
wireValue = wireValue or Vector(0, 0, 0)
local x = tonumber(wireValue.x or 0) or 0
local y = tonumber(wireValue.y or 0) or 0
local z = tonumber(wireValue.z or 0) or 0
return string.format("%.20g %.20g %.20g", x, y, z)
end,
toWire = function(_, hammerValue)
local x, y, z = unpack(string.Explode(" ", hammerValue or ""))
x = tonumber(x or 0) or 0
y = tonumber(y or 0) or 0
z = tonumber(z or 0) or 0
return Vector(x, y, z)
end,
wireIsEqual = isEqualGeneric,
},
[5] = { -- 4D Vector
wireType = "VECTOR4",
toHammer = function(_, wireValue)
val = val or {0, 0, 0, 0}
local x = tonumber(val[1] or 0) or 0
local y = tonumber(val[2] or 0) or 0
local z = tonumber(val[3] or 0) or 0
local w = tonumber(val[4] or 0) or 0
return string.format("%.20g %.20g %.20g %.20g", x, y, z, w)
end,
toWire = function(_, hammerValue)
local x, y, z, w = unpack(string.Explode(" ", hammerValue or ""))
x = tonumber(x or 0) or 0
y = tonumber(y or 0) or 0
z = tonumber(z or 0) or 0
w = tonumber(w or 0) or 0
return {x, y, z, w}
end,
wireIsEqual = isEqualList,
},
[6] = { -- Angle
wireType = "ANGLE",
toHammer = function(_, wireValue)
wireValue = wireValue or Angle(0, 0, 0)
local p = tonumber(wireValue.p or 0) or 0
local y = tonumber(wireValue.y or 0) or 0
local r = tonumber(wireValue.r or 0) or 0
return string.format("%.20g %.20g %.20g", p, y, r)
end,
toWire = function(_, hammerValue)
local p, y, r = unpack(string.Explode(" ", hammerValue or ""))
p = tonumber(p or 0) or 0
y = tonumber(y or 0) or 0
r = tonumber(r or 0) or 0
return Angle(p, y, r)
end,
wireIsEqual = isEqualGeneric,
},
[7] = { -- Entity
wireType = "ENTITY",
toHammer = function(_, wireValue)
if not IsValid(wireValue) then return "0" end
return tostring(wireValue:EntIndex())
end,
toWire = function(self, hammerValue)
local id = tonumber(hammerValue or 0)
if id ~= nil then
if id == 0 then
return game.GetWorld()
end
return ents.GetByIndex(id)
end
return self:GetFirstEntityByTargetnameOrClass(hammerValue) or NULL
end,
wireIsEqual = isEqualEntity,
},
[8] = { -- Array/Table
wireType = "ARRAY",
toHammer = function(_, wireValue)
return table.concat(wireValue or {}, " ")
end,
toWire = function(_, hammerValue)
return string.Explode(" ", hammerValue or "")
end,
wireIsEqual = isEqualList,
},
}
function ENT:GetMapToWireConverter(typeId)
local typeData = g_supportedTypesById[typeId or 0] or g_supportedTypesById[0]
if not typeData then
return
end
return typeData.toWire, typeData.isToggle or false
end
function ENT:GetWireToMapConverter(typeId)
local typeData = g_supportedTypesById[typeId or 0] or g_supportedTypesById[0]
if not typeData then
return
end
return typeData.toHammer, typeData.isToggle or false
end
function ENT:IsEqualWireValue(typeId, wireValueA, wireValueB)
local typeData = g_supportedTypesById[typeId or 0] or g_supportedTypesById[0]
if not typeData then
return false
end
return typeData.wireIsEqual(self, wireValueA, wireValueB)
end
function ENT:GetWireTypenameByTypeId(typeId)
local typeData = g_supportedTypesById[typeId or 0] or g_supportedTypesById[0]
if not typeData then
return
end
return typeData.wireType
end