-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathentity.lua
More file actions
712 lines (489 loc) · 16.2 KB
/
entity.lua
File metadata and controls
712 lines (489 loc) · 16.2 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
local meta = FindMetaTable( "Entity" )
-- Return if there's nothing to add on to
if ( !meta ) then return end
function meta:SetSpawnFlags( flags )
self:SetKeyValue( "spawnflags", flags )
end
function meta:AddSpawnFlags( flags )
self:SetKeyValue( "spawnflags", bit.bor( self:GetSpawnFlags(), flags ) )
end
function meta:RemoveSpawnFlags( flags )
self:SetKeyValue( "spawnflags", bit.band( self:GetSpawnFlags(), bit.bnot( flags ) ) )
end
function meta:GetShouldPlayPickupSound()
return self.m_bPlayPickupSound or false
end
function meta:SetShouldPlayPickupSound( bPlaySound )
self.m_bPlayPickupSound = tobool( bPlaySound ) or false
end
--
-- Entity index accessor. This used to be done in engine, but it's done in Lua now because it's faster
--
function meta:__index( key )
--
-- Search the metatable. We can do this without dipping into C, so we do it first.
--
local val = meta[ key ]
if ( val != nil ) then return val end
--
-- Search the entity table
--
local tab = meta.GetTable( self )
if ( tab ) then
local tabval = tab[ key ]
if ( tabval != nil ) then return tabval end
end
--
-- Legacy: sometimes use self.Owner to get the owner.. so lets carry on supporting that stupidness
-- This needs to be retired, just like self.Entity was.
--
if ( key == "Owner" ) then return meta.GetOwner( self ) end
return nil
end
--[[---------------------------------------------------------
Name: Short cut to add entities to the table
-----------------------------------------------------------]]
function meta:GetVar( name, default )
local Val = self:GetTable()[ name ]
if ( Val == nil ) then return default end
return Val
end
if ( SERVER ) then
function meta:SetCreator( ply --[[= NULL]] )
if ( ply == nil ) then
ply = NULL
elseif ( !isentity( ply ) ) then
error( "bad argument #1 to 'SetCreator' (Entity expected, got " .. type( ply ) .. ")", 2 )
end
self.m_PlayerCreator = ply
end
function meta:GetCreator()
return self.m_PlayerCreator or NULL
end
end
--[[---------------------------------------------------------
Name: Returns true if the entity has constraints attached to it
-----------------------------------------------------------]]
function meta:IsConstrained()
if ( CLIENT ) then return self:GetNWBool( "IsConstrained" ) end
local c = self:GetTable().Constraints
local bIsConstrained = false
if ( c ) then
for k, v in pairs( c ) do
if ( IsValid( v ) ) then bIsConstrained = true break end
c[ k ] = nil
end
end
self:SetNWBool( "IsConstrained", bIsConstrained )
return bIsConstrained
end
--[[---------------------------------------------------------
Name: Short cut to set tables on the entity table
-----------------------------------------------------------]]
function meta:SetVar( name, value )
self:GetTable()[ name ] = value
end
--[[---------------------------------------------------------
Name: CallOnRemove
Desc: Call this function when this entity dies.
Calls the function like Function( <entity>, <optional args> )
-----------------------------------------------------------]]
function meta:CallOnRemove( name, func, ... )
local mytable = self:GetTable()
mytable.OnDieFunctions = mytable.OnDieFunctions or {}
mytable.OnDieFunctions[ name ] = { Name = name, Function = func, Args = { ... } }
end
--[[---------------------------------------------------------
Name: RemoveCallOnRemove
Desc: Removes the named hook
-----------------------------------------------------------]]
function meta:RemoveCallOnRemove( name )
local mytable = self:GetTable()
mytable.OnDieFunctions = mytable.OnDieFunctions or {}
mytable.OnDieFunctions[ name ] = nil
end
--[[---------------------------------------------------------
Simple mechanism for calling the die functions.
-----------------------------------------------------------]]
local function DoDieFunction( ent )
if ( !ent.OnDieFunctions ) then return end
for name, data in pairs( ent.OnDieFunctions ) do
-- Functions aren't saved - so this could be nil if we loaded a game.
if ( data && data.Function ) then
ProtectedCall( data.Function, ent, unpack( data.Args ) )
end
end
end
hook.Add( "EntityRemoved", "DoDieFunction", DoDieFunction )
function meta:PhysWake()
local phys = self:GetPhysicsObject()
if ( !IsValid( phys ) ) then return end
phys:Wake()
end
-- This makes these a bit faster
function meta:GetColor()
return Color( self:GetColor4Part() )
end
function meta:SetColor( col )
-- Backwards compatibility
if ( !col ) then
return self:SetColor4Part( 255, 255, 255, 255 )
end
self:SetColor4Part( col.r, col.g, col.b, col.a )
end
function meta:GetChildBones( bone )
local bonecount = self:GetBoneCount()
if ( bonecount == 0 or bonecount < bone ) then return end
local bones = {}
for k = 0, bonecount - 1 do
if ( self:GetBoneParent( k ) != bone ) then continue end
table.insert( bones, k )
end
return bones
end
function DTVar_ReceiveProxyGL( ent, name, id, val )
if ( ent.CallDTVarProxies ) then
ent:CallDTVarProxies( name, id, val )
end
end
function meta:InstallDataTable()
self.dt = {}
local typetable = {}
local datatable = {}
local keytable = {}
local dtmeta = {}
local editing = {}
dtmeta.__index = function ( ent, key )
local dt = datatable[ key ]
if ( dt == nil ) then return end
return dt.GetFunc( self, dt.index, key )
end
dtmeta.__newindex = function( ent, key, value )
local dt = datatable[ key ]
if ( dt == nil ) then return end
dt.SetFunc( self, dt.index, value )
end
local function FindUnusedIndex( typename )
local tbl = typetable[ typename ]
if ( !tbl ) then return 0 end
for i = 0, 31 do
if ( !tbl[i] ) then return i end
end
end
self.IsDTVarSlotUsed = function( ent, typename, index )
local tbl = typetable[ typename ]
if ( !tbl or !tbl[index] ) then return false end
return true
end
self.DTVar = function( ent, typename, index, name )
if ( isstring( index ) && !name ) then
name = index
index = FindUnusedIndex( typename )
elseif ( !index && isstring( name ) ) then
index = FindUnusedIndex( typename )
end
local SetFunc = ent[ "SetDT" .. typename ]
local GetFunc = ent[ "GetDT" .. typename ]
if ( !SetFunc or !GetFunc ) then
MsgN( "Couldn't addvar ", name, " - type ", typename, " is invalid!" )
return
end
local data = {
index = index,
name = name,
SetFunc = SetFunc,
GetFunc = GetFunc,
typename = typename,
Notify = {}
}
typetable[ typename ] = typetable[ typename ] or {}
typetable[ typename ][ index ] = data
datatable[ name ] = data
return data
end
--
-- Access to the editing table
--
self.GetEditingData = function()
return editing
end
--
-- Adds an editable variable.
--
self.SetupEditing = function( ent, name, keyname, data )
if ( !data ) then return end
if ( !data.title ) then data.title = name end
editing[ keyname ] = data
end
self.SetupKeyValue = function( ent, keyname, kvtype, setfunc, getfunc, other_data )
keyname = keyname:lower()
keytable[ keyname ] = {
KeyName = keyname,
Set = setfunc,
Get = getfunc,
Type = kvtype
}
if ( other_data ) then
table.Merge( keytable[ keyname ], other_data )
end
end
local CallProxies = function( ent, tbl, name, oldval, newval )
for i = 1, #tbl do
tbl[ i ]( ent, name, oldval, newval )
end
end
self.CallDTVarProxies = function( ent, typename, index, newVal )
local t = typetable[ typename ] && typetable[ typename ][ index ] or nil
if ( t ) then
CallProxies( ent, t.Notify, t.name, t.GetFunc( ent, index ), newVal )
end
end
self.NetworkVar = function( ent, typename, index, name, other_data )
if ( isstring( index ) && ( istable( name ) or !name ) ) then
other_data = name
name = index
index = FindUnusedIndex( typename )
elseif ( !index && isstring( name ) ) then
index = FindUnusedIndex( typename )
end
local t = ent.DTVar( ent, typename, index, name )
-- Some addons call these on the entity table, and that used to work, so we keep that
ent[ "Set" .. name ] = function( selfent, value )
CallProxies( ent, t.Notify, name, t.GetFunc( ent, index ), value )
t.SetFunc( ent, index, value )
end
ent[ "Get" .. name ] = function( selfent )
return t.GetFunc( ent, index )
end
if ( !other_data ) then return end
-- This KeyName stuff is absolutely unnecessary, there's absolutely no reason for it to exist
-- But we cannot remove it now because dupes will break. It should've used the "name" variable
if ( other_data.KeyName ) then
ent:SetupKeyValue( other_data.KeyName, typename, ent[ "Set" .. name ], ent[ "Get" .. name ], other_data )
ent:SetupEditing( name, other_data.KeyName, other_data.Edit )
end
end
--
-- Add a function that gets called when the variable changes
-- Note: this doesn't work on the client yet - which drastically reduces its usefulness.
--
self.NetworkVarNotify = function( ent, name, func )
if ( !datatable[ name ] ) then error( "calling NetworkVarNotify on missing network var " .. name ) end
table.insert( datatable[ name ].Notify, func )
end
--
-- Create an accessor of an element. This is mainly so you can use spare
-- network vars (vectors, angles) to network single floats.
--
self.NetworkVarElement = function( ent, typename, index, element, name, other_data )
if ( isstring( index ) && isstring( element ) ) then
other_data = name
name = element
element = index
index = FindUnusedIndex( typename )
elseif ( !index && isstring( name ) ) then
index = FindUnusedIndex( typename )
end
local t = ent.DTVar( ent, typename, index, name )
t.element = element
ent[ "Set" .. name ] = function( selfent, value )
local old = t.GetFunc( selfent, index )
old[ element ] = value
t.SetFunc( selfent, index, old )
end
ent[ "Get" .. name ] = function( selfent )
return t.GetFunc( selfent, index )[ element ]
end
if ( !other_data ) then return end
-- This KeyName stuff is absolutely unnecessary, there's absolutely no reason for it to exist
-- But we cannot remove it now because dupes will break. It should've used the "name" variable
if ( other_data.KeyName ) then
ent:SetupKeyValue( other_data.KeyName, "float", ent[ "Set" .. name ], ent[ "Get" .. name ], other_data )
ent:SetupEditing( name, other_data.KeyName, other_data.Edit )
end
end
self.SetNetworkKeyValue = function( ent, key, value )
key = key:lower()
local k = keytable[ key ]
if ( !k ) then return end
local v = util.StringToType( value, k.Type )
if ( v == nil ) then return end
k.Set( ent, v )
return true
end
self.SetNetworkVarsFromMapInput = function( ent, name, data )
name = name:lower()
if ( !string.StartsWith( name, "set" ) ) then return end
name = string.sub( name, 4 )
if ( name == "" ) then return end
-- Only allow setting variables that were marked as editable!
local k = keytable[ name ]
if ( !k || !k.Edit ) then return end
local v = util.StringToType( data, k.Type )
if ( v == nil ) then return end
-- Special case for colors. FGD (maps) use color255 format, while colors in this system use unit vectors.
if ( k.Edit.type == "VectorColor" ) then
v = v / 255
end
k.Set( ent, v )
return true
end
self.GetNetworkKeyValue = function( ent, key )
key = key:lower()
local k = keytable[ key ]
if ( !k ) then return end
return k.Get( ent )
end
--
-- Called by the duplicator system to get the network vars
--
self.GetNetworkVars = function( ent )
local dt = {}
for k, v in pairs( datatable ) do
-- Don't try to save entities (yet?)
if ( v.typename == "Entity" ) then continue end
if ( v.element ) then
dt[ k ] = v.GetFunc( ent, v.index )[ v.element ]
else
dt[ k ] = v.GetFunc( ent, v.index )
end
end
--
-- If there's nothing in our table - then return nil.
--
if ( table.IsEmpty( dt ) ) then return nil end
return dt
end
--
-- Called by the duplicator system to restore from network vars
--
self.RestoreNetworkVars = function( ent, tab )
if ( !tab ) then return end
-- Loop this entities data table
for k, v in pairs( datatable ) do
-- If it contains this entry
if ( tab[ k ] == nil ) then continue end
-- Support old saves/dupes with incorrectly saved data
if ( v.element && ( isangle( tab[ k ] ) or isvector( tab[ k ] ) ) ) then
tab[ k ] = tab[ k ][ v.element ]
end
-- Set it.
if ( ent[ "Set" .. k ] ) then
ent[ "Set" .. k ]( ent, tab[ k ] )
else
v.SetFunc( ent, v.index, tab[ k ] )
end
end
end
setmetatable( self.dt, dtmeta )
--
-- In sandbox the client can edit certain values on certain entities
-- we implement this here incase any other gamemodes want to use it
-- although it is of course deactivated by default.
--
--
-- This function takes a keyname and a value - both strings.
--
--
-- Called serverside it will set the value.
--
self.EditValue = function( ent, variable, value )
if ( !isstring( variable ) ) then return end
if ( !isstring( value ) ) then return end
--
-- It can be called clientside to send a message to the server
-- to request a change of value.
--
if ( CLIENT ) then
net.Start( "editvariable" )
net.WriteEntity( ent )
net.WriteString( variable )
net.WriteString( value )
net.SendToServer()
end
--
-- Called serverside it simply changes the value
--
if ( SERVER ) then
ent:SetNetworkKeyValue( variable, value )
end
end
end
if ( SERVER ) then
util.AddNetworkString( "editvariable" )
net.Receive( "editvariable", function( len, client )
local ent = net.ReadEntity()
if ( !IsValid( ent ) ) then return end
if ( !isfunction( ent.GetEditingData ) ) then return end
if ( ent.AdminOnly && !( client:IsAdmin() or game.SinglePlayer() ) ) then return end
local key = net.ReadString()
-- Is this key in our edit table?
local editor = ent:GetEditingData()[ key ]
if ( !istable( editor ) ) then return end
local val = net.ReadString()
hook.Run( "VariableEdited", ent, client, key, val, editor )
end )
function meta:GetUnFreezable()
return self.m_bUnFreezable or false
end
function meta:SetUnFreezable( bFreeze )
self.m_bUnFreezable = tobool( bFreeze ) or false
end
end
--
-- Networked var proxies
--
function meta:SetNetworked2VarProxy( name, func )
if ( !self.NWVarProxies ) then
self.NWVarProxies = {}
end
self.NWVarProxies[ name ] = func
end
function meta:GetNetworked2VarProxy( name )
if ( self.NWVarProxies ) then
local func = self.NWVarProxies[ name ]
if ( isfunction( func ) ) then
return func
end
end
return nil
end
meta.SetNW2VarProxy = meta.SetNetworked2VarProxy
meta.GetNW2VarProxy = meta.GetNetworked2VarProxy
hook.Add( "EntityNetworkedVarChanged", "NetworkedVars", function( ent, name, oldValue, newValue )
if ( ent.NWVarProxies ) then
local func = ent.NWVarProxies[ name ]
if ( isfunction( func ) ) then
func( ent, name, oldValue, newValue )
end
end
end )
--
-- Vehicle Extensions
--
local vehicle = FindMetaTable( "Vehicle" )
--
-- We steal some DT slots by default for vehicles
-- to control the third person view. You should use
-- these functions if you want to play with them because
-- they might eventually be moved into the engine - so manually
-- editing the DT values will stop working.
--
function vehicle:SetVehicleClass( s )
self:SetDTString( 3, s )
end
function vehicle:GetVehicleClass()
return self:GetDTString( 3 )
end
function vehicle:SetThirdPersonMode( b )
self:SetDTBool( 3, b )
end
function vehicle:GetThirdPersonMode()
return self:GetDTBool( 3 )
end
function vehicle:SetCameraDistance( dist )
self:SetDTFloat( 3, dist )
end
function vehicle:GetCameraDistance()
return self:GetDTFloat( 3 )
end