forked from wiremod/wire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.lua
More file actions
1398 lines (1097 loc) · 45.5 KB
/
entity.lua
File metadata and controls
1398 lines (1097 loc) · 45.5 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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local wire_expression2_entity_trails_max = CreateConVar("wire_expression2_entity_trails_max", 30, FCVAR_ARCHIVE, "Max amount of trails a player can make. 0 - to disable trails. (Limit is shared between E2s of a player)", 0)
registerType("entity", "e", nil,
nil,
function(self,output) return output or NULL end,
nil,
function(v)
return not isentity(v)
end
)
--[[******************************************************************************]]
-- import some e2lib functions
local validPhysics = E2Lib.validPhysics
local getOwner = E2Lib.getOwner
local isOwner = E2Lib.isOwner
local newE2Table = E2Lib.newE2Table
local canProperty = WireLib.CanProperty
local canEditVariable = WireLib.CanEditVariable
local sun = ents.FindByClass("env_sun")[1] -- used for sunDirection()
local trailedEntsAmount = {};
hook.Add("InitPostEntity","sunent",function()
sun = ents.FindByClass("env_sun")[1]
timer.Simple(0,function() -- make sure we have a sun first
hook.Remove("InitPostEntity","sunent")
end ) -- then remove this. we don't need it anymore.
end)
registerCallback("e2lib_replace_function", function(funcname, func, oldfunc)
if funcname == "isOwner" then
isOwner = func
elseif funcname == "getOwner" then
getOwner = func
elseif funcname == "IsValid" then
IsValid = func
elseif funcname == "validPhysics" then
validPhysics = func
end
end)
-- faster access to some math library functions
local atan2 = math.atan2
local asin = math.asin
local Clamp = math.Clamp
local rad2deg = 180 / math.pi
--[[******************************************************************************]]
local function checkOwner(self)
return IsValid(self.player)
end
--[[******************************************************************************]]
-- Functions using operators
__e2setcost(5) -- temporary
--[[******************************************************************************]]
e2function number operator_is(entity this)
return IsValid(this) and 1 or 0
end
--[[******************************************************************************]]
e2function entity entity(id)
local ent = ents.GetByIndex(id)
return IsValid(ent) and ent or nil
end
e2function number entity:id()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:EntIndex()
end
e2function number entity:creationID()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetCreationID()
end
e2function number entity:creationTime()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetCreationTime()
end
--[[******************************************************************************]]
-- Functions getting string
e2function entity noentity()
return NULL
end
e2function entity world()
return game.GetWorld()
end
e2function string entity:name()
if not IsValid(this) then return self:throw("Invalid entity!", "") end
return this:GetName() or ""
end
[nodiscard]
e2function string entity:getClass()
if not IsValid(this) then return self:throw("Invalid entity!", "") end
return this:GetClass()
end
[deprecated = "Use getClass"]
e2function string entity:type() = e2function string entity:getClass()
e2function string entity:model()
if not IsValid(this) then return self:throw("Invalid entity!", "") end
return this:GetModel() or ""
end
e2function entity entity:owner()
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
return getOwner(self, this)
end
__e2setcost(100)
e2function array entities()
local entities = ents.GetAll()
self.prf = self.prf + #entities / 2
return entities
end
__e2setcost(20)
e2function table entity:keyvalues()
local ret = newE2Table() -- default table
if not IsValid(this) then return self:throw("Invalid entity!", ret) end
local keyvalues = this:GetKeyValues()
local size = 0
for k,v in pairs( keyvalues ) do
size = size + 1
ret.s[k] = v
ret.stypes[k] = string.lower(type(v)[1]) -- i swear there's a more elegant solution to this but whatever.
end
ret.size = size
return ret
end
e2function number entity:setEditProperty(string key, string value)
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", 0) end
if not this.Editable then return self:throw("Tried to edit non-editable entity!", 0) end
if not canProperty(self.player, this, "editentity") then return self:throw("Gamemode disallowed editing this entity!", 0) end
key = key:lower()
local edit = this:GetEditingData()[key]
if not edit then return self:throw("Property '" .. key .. "' does not exist on entity!", 0) end
if not canEditVariable(this, self.player, key, value, edit) then return self:throw("Server disallowed editing this property!", 0) end
return this:SetNetworkKeyValue(key, value) and 1 or 0
end
e2function string entity:getEditProperty(string key)
if not IsValid(this) then return self:throw("Invalid entity!", "") end
if not this.Editable then return self:throw("Tried to access non-editable entity!", "") end
-- GetNetworkKeyValue lowercases the key for us
return tostring(this:GetNetworkKeyValue(key) or "")
end
__e2setcost(30)
e2function table entity:getEditData()
local ret = newE2Table()
if not IsValid(this) then return self:throw("Invalid entity!", ret) end
if not this.Editable then return self:throw("Tried to access non-editable entity!", ret) end
local i = 0
for k, v in pairs(this:GetEditingData()) do
ret.s[k] = v.type -- Note that this is the DProperty rowType and not a valid E2 type
ret.stypes[k] = "s"
i = i + 1
end
ret.size = i
return ret
end
__e2setcost(1)
e2function number entity:isEditable()
return (IsValid(this) or self:throw("Invalid entity!")) and this.Editable and 1 or 0
end
__e2setcost(5) -- temporary
--[[******************************************************************************]]
-- Functions getting vector
e2function vector entity:pos()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:GetPos()
end
e2function vector entity:forward()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:GetForward()
end
e2function vector entity:right()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:GetRight()
end
e2function vector entity:up()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:GetUp()
end
e2function vector entity:vel()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:GetVelocity()
end
e2function vector entity:velL()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:WorldToLocal(this:GetVelocity() + this:GetPos())
end
[nodiscard]
e2function vector entity:velAtPoint(vector worldPosition)
local physobj = this:GetPhysicsObject()
if not IsValid(this) or not IsValid(physobj) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return physobj:GetVelocityAtPoint(worldPosition)
end
e2function angle entity:angVel()
if not validPhysics(this) then return self:throw("Invalid entity!", Angle(0, 0, 0)) end
local phys = this:GetPhysicsObject()
local vec = phys:GetAngleVelocity()
return Angle(vec.y, vec.z, vec.x)
end
--- Returns a vector describing rotation axis, magnitude and sense given as the vector's direction, magnitude and orientation.
e2function vector entity:angVelVector()
if not validPhysics(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
local phys = this:GetPhysicsObject()
return phys:GetAngleVelocity()
end
--- Specific to env_sun because Source is dum. Use this to trace towards the sun or something.
e2function vector sunDirection()
if not IsValid(sun) then return Vector(0, 0, 0) end
return sun:GetKeyValues().sun_dir
end
--[[******************************************************************************]]
-- Functions using vector getting vector
__e2setcost(15)
e2function vector entity:toWorld(vector localPosition)
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:LocalToWorld(localPosition)
end
e2function vector entity:toLocal(vector worldPosition)
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:WorldToLocal(worldPosition)
end
e2function vector entity:toWorldAxis(vector localAxis)
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:LocalToWorld(localAxis)-this:GetPos()
end
e2function vector entity:toLocalAxis(vector worldAxis)
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:WorldToLocal(worldAxis+this:GetPos())
end
--- Transforms from an angle local to <this> to a world angle.
e2function angle entity:toWorld(angle localAngle)
if not IsValid(this) then return self:throw("Invalid entity!", Angle(0, 0, 0)) end
return this:LocalToWorldAngles(localAngle)
end
--- Transforms from a world angle to an angle local to <this>.
e2function angle entity:toLocal(angle worldAngle)
if not IsValid(this) then return self:throw("Invalid entity!", Angle(0, 0, 0)) end
return this:WorldToLocalAngles(worldAngle)
end
--[[******************************************************************************]]
-- Functions getting number
__e2setcost(5)
e2function number entity:health()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:Health()
end
e2function number entity:maxHealth()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetMaxHealth()
end
e2function number entity:radius()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:BoundingRadius()
end
-- original bearing & elevation thanks to Gwahir
--- Returns the bearing (yaw) from <this> to <pos>
__e2setcost(15)
e2function number entity:bearing(vector pos)
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
pos = this:WorldToLocal(pos)
return rad2deg * -atan2(pos.y, pos.x)
end
--- Returns the elevation (pitch) from <this> to <pos>
e2function number entity:elevation(vector pos)
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
pos = this:WorldToLocal(pos)
local len = pos:Length()
if len < 0 then return 0 end
return rad2deg*asin(pos.z / len)
end
--- Returns the elevation (pitch) and bearing (yaw) from <this> to <pos>
e2function angle entity:heading(vector pos)
if not IsValid(this) then return self:throw("Invalid entity!", Angle(0, 0, 0)) end
pos = this:WorldToLocal(pos)
-- bearing
local bearing = rad2deg*-atan2(pos.y, pos.x)
-- elevation
local len = pos:Length()--sqrt(x*x + y*y + z*z)
if len < 0 then return Angle(0, bearing, 0) end
local elevation = rad2deg * asin(pos.z / len)
return Angle(elevation, bearing, 0)
end
__e2setcost(10)
e2function number entity:mass()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
local phys = this:GetPhysicsObject()
if not phys:IsValid() then return self:throw("Invalid physics object!", 0) end
return phys:GetMass()
end
e2function vector entity:massCenter()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
local phys = this:GetPhysicsObject()
if not phys:IsValid() then return self:throw("Invalid physics object!", Vector(0, 0, 0)) end
return this:LocalToWorld(phys:GetMassCenter())
end
e2function vector entity:massCenterL()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
local phys = this:GetPhysicsObject()
if not phys:IsValid() then return self:throw("Invalid physics object!", Vector(0, 0, 0)) end
return phys:GetMassCenter()
end
e2function void setMass(mass)
if not validPhysics(self.entity) then return self:throw("Invalid entity!", nil) end
if WireLib.isnan( mass ) then mass = 50000 end
local mass = Clamp(mass, 0.001, 50000)
local phys = self.entity:GetPhysicsObject()
phys:SetMass(mass)
duplicator.StoreEntityModifier(self.entity, "mass", { Mass = mass })
end
e2function void entity:setMass(mass)
if not validPhysics(this) then return self:throw("Invalid physics object!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this prop!", nil) end
if this:IsPlayer() then return self:throw("You cannot set the mass of a player") end
if WireLib.isnan( mass ) then mass = 50000 end
local mass = Clamp(mass, 0.001, 50000)
local phys = this:GetPhysicsObject()
phys:SetMass(mass)
duplicator.StoreEntityModifier(this, "mass", { Mass = mass })
end
e2function number entity:volume()
if not validPhysics(this) then return self:throw("Invalid physics object!", 0) end
local phys = this:GetPhysicsObject()
return phys:GetVolume() or 0
end
e2function number entity:surfaceArea()
if not validPhysics(this) then return self:throw("Invalid physics object!", 0) end
local phys = this:GetPhysicsObject()
return phys:GetSurfaceArea() or 0
end
e2function number entity:stress()
if not validPhysics(this) then return self:throw("Invalid physics object!", 0) end
local phys = this:GetPhysicsObject()
return phys:GetStress() or 0
end
local ids = {
["EnergyAbsorbed"] = "n",
["FrictionCoefficient"] = "n",
["NormalForce"] = "n",
["Normal"] = "v",
["ContactPoint"] = "v",
}
e2function table entity:frictionSnapshot()
local ret = newE2Table() -- default table
if not validPhysics(this) then return self:throw("Invalid physics object!", ret) end
local events = this:GetPhysicsObject():GetFrictionSnapshot()
for i, event in ipairs(events) do
local data = newE2Table()
local size = 0
for k, v in pairs(event) do
if ids[k] then
data.s[k] = v
data.stypes[k] = ids[k]
size = size + 1
end
end
data.size = size
ret.n[i] = data
ret.ntypes[i] = "t"
end
ret.size = #events
self.prf = self.prf + ret.size * 50
return ret
end
--[[******************************************************************************]]
-- Functions getting boolean/number
e2function number entity:isPlayer()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsPlayer() then return 1 else return 0 end
end
e2function number entity:isNPC()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsNPC() then return 1 else return 0 end
end
e2function number entity:isNextBot()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsNextBot() then return 1 else return 0 end
end
e2function number entity:isRagdoll()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsRagdoll() then return 1 else return 0 end
end
e2function number entity:isVehicle()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsVehicle() then return 1 else return 0 end
end
e2function number entity:isWorld()
if not isentity(this) then return self:throw("Invalid entity!", 0) end
if this:IsWorld() then return 1 else return 0 end
end
e2function number entity:isOnGround()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsOnGround() then return 1 else return 0 end
end
e2function number entity:isUnderWater()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:WaterLevel() > 0 then return 1 else return 0 end
end
e2function number entity:isValid()
if IsValid(this) then return 1 else return 0 end
end
--- Returns 1 if <this> has valid physics. Note: Players do not.
e2function number entity:isValidPhysics()
if E2Lib.validPhysics(this) then return 1 else return 0 end
end
--[[******************************************************************************]]
-- Functions getting angles
e2function angle entity:angles()
if not IsValid(this) then return self:throw("Invalid entity!", Angle(0, 0, 0)) end
local ang = this:GetAngles()
return Angle(ang.p, ang.y, ang.r)
end
--[[******************************************************************************]]
e2function string entity:getMaterial()
if not IsValid(this) then return self:throw("Invalid entity!", "") end
return this:GetMaterial() or ""
end
e2function string entity:getSubMaterial(index)
if not IsValid(this) then return self:throw("Invalid entity!", "") end
return this:GetSubMaterial(index-1) or ""
end
e2function number entity:getModelRadius()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetModelRadius() or 0
end
e2function number entity:getModelScale()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetModelScale()
end
__e2setcost(20)
e2function array entity:getMaterials()
if not IsValid(this) then return self:throw("Invalid entity!", {}) end
return this:GetMaterials()
end
__e2setcost(10)
e2function void entity:setMaterial(string material)
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
E2Lib.setMaterial(this, material)
end
e2function void entity:setSubMaterial(index, string material)
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
E2Lib.setSubMaterial(this, index-1, material)
end
--- Gets <this>'s current skin number.
e2function number entity:getSkin()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetSkin()
end
--- Sets <this>'s skin number.
e2function void entity:setSkin(skinIndex)
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
this:SetSkin(skinIndex)
end
--- Gets <this>'s number of skins.
e2function number entity:getSkinCount()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:SkinCount()
end
--- Sets <this>'s bodygroup.
e2function void entity:setBodygroup(bgrp_id, bgrp_subid)
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
this:SetBodygroup(bgrp_id, bgrp_subid)
end
--- Gets <this>'s bodygroup number.
e2function number entity:getBodygroup(bgrp_id)
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetBodygroup(bgrp_id)
end
--- Gets <this>'s bodygroup count.
e2function number entity:getBodygroups(bgrp_id)
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
return this:GetBodygroupCount(bgrp_id)
end
E2Lib.registerConstant("BLOOD_DONT_BLEED", DONT_BLEED)
E2Lib.registerConstant("BLOOD_COLOR_RED", BLOOD_COLOR_RED)
E2Lib.registerConstant("BLOOD_COLOR_YELLOW", BLOOD_COLOR_YELLOW)
E2Lib.registerConstant("BLOOD_COLOR_GREEN", BLOOD_COLOR_GREEN)
E2Lib.registerConstant("BLOOD_COLOR_MECH", BLOOD_COLOR_MECH)
E2Lib.registerConstant("BLOOD_COLOR_ANTLION", BLOOD_COLOR_ANTLION)
E2Lib.registerConstant("BLOOD_COLOR_ZOMBIE", BLOOD_COLOR_ZOMBIE)
E2Lib.registerConstant("BLOOD_COLOR_ANTLION_WORKER", BLOOD_COLOR_ANTLION_WORKER)
e2function void entity:setBloodColor(number bloodcolor)
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
this:SetBloodColor(math.Clamp(bloodcolor, -1, 6))
end
e2function number entity:getBloodColor()
if not IsValid(this) then return self:throw("Invalid entity!", -1) end
return this:GetBloodColor() or -1
end
--[[******************************************************************************]]
e2function number entity:isPlayerHolding()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsPlayerHolding() then return 1 else return 0 end
end
e2function number entity:isOnFire()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsOnFire() then return 1 else return 0 end
end
e2function number entity:isWeapon()
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
if this:IsWeapon() then return 1 else return 0 end
end
e2function number entity:isFrozen()
if not validPhysics(this) then return self:throw("Invalid entity!", 0) end
local phys = this:GetPhysicsObject()
if phys:IsMoveable() then return 0 else return 1 end
end
e2function number entity:isAsleep()
if not validPhysics(this) then return self:throw("Invalid entity!", 0) end
local phys = this:GetPhysicsObject()
if phys:IsAsleep() then return 1 else return 0 end
end
e2function number entity:isGravityEnabled()
if not validPhysics(this) then return self:throw("Invalid entity!", 0) end
local phys = this:GetPhysicsObject()
if phys:IsGravityEnabled() then return 1 else return 0 end
end
e2function number entity:isPenetrating()
if not validPhysics(this) then return self:throw("Invalid entity!", 0) end
local phys = this:GetPhysicsObject()
if phys:IsPenetrating() then return 1 else return 0 end
end
--[[******************************************************************************]]
__e2setcost(30) -- temporary
local clamp = WireLib.clampForce
e2function void entity:applyForce(vector force)
if not validPhysics(this) then return self:throw("Invalid physics object!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
force = clamp(force)
local phys = this:GetPhysicsObject()
phys:ApplyForceCenter(force)
end
e2function void entity:applyOffsetForce(vector force, vector position)
if not validPhysics(this) then return self:throw("Invalid physics object!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
force = clamp(force)
position = clamp(position)
local phys = this:GetPhysicsObject()
phys:ApplyForceOffset(force, position)
end
e2function void entity:applyAngForce(angle angForce)
if not validPhysics(this) then return self:throw("Invalid physics object!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
if angForce:IsZero() then return end
angForce = clamp(angForce)
local phys = this:GetPhysicsObject()
-- assign vectors
local up = this:GetUp()
local left = this:GetRight() * -1
local forward = this:GetForward()
-- apply pitch force
if angForce[1] ~= 0 then
local pitch = up * (angForce[1] * 0.5)
phys:ApplyForceOffset( forward, pitch )
phys:ApplyForceOffset( forward * -1, pitch * -1 )
end
-- apply yaw force
if angForce[2] ~= 0 then
local yaw = forward * (angForce[2] * 0.5)
phys:ApplyForceOffset( left, yaw )
phys:ApplyForceOffset( left * -1, yaw * -1 )
end
-- apply roll force
if angForce[3] ~= 0 then
local roll = left * (angForce[3] * 0.5)
phys:ApplyForceOffset( up, roll )
phys:ApplyForceOffset( up * -1, roll * -1 )
end
end
--- Applies torque according to a local torque vector, with magnitude and sense given by the vector's direction, magnitude and orientation.
e2function void entity:applyTorque(vector torque)
if not validPhysics(this) then return self:throw("Invalid physics object!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
if torque:IsZero() then return end
local phys = this:GetPhysicsObject()
-- Convert torque from local to world axis
torque = phys:LocalToWorldVector( clamp(torque) )
-- Convert rad*in^2 to deg*m^2
phys:ApplyTorqueCenter( torque * (180 / math.pi / 39.3701^2) )
end
e2function vector entity:inertia()
if not validPhysics(this) then return self:throw("Invalid physics object!", Vector(0, 0, 0)) end
return this:GetPhysicsObject():GetInertia()
end
--[[******************************************************************************]]
__e2setcost(10) -- temporary
e2function void entity:lockPod(lock)
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
if lock ~= 0 then
this:Fire("Lock", "", 0)
else
this:Fire("Unlock", "", 0)
end
end
e2function void entity:killPod()
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
local ply = this:GetDriver()
if IsValid(ply) and ply:IsPlayer() and ply:Alive() then ply:Kill() end
end
e2function void entity:ejectPod()
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
local ply = this:GetDriver()
if IsValid(ply) then ply:ExitVehicle() end
end
e2function void entity:podStripWeapons()
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end
local ply = this:GetDriver()
if IsValid(ply) and next(ply:GetWeapons()) ~= nil then
ply:StripWeapons()
ply:ChatPrint("Your weapons have been stripped!")
end
end
--[[******************************************************************************]]
__e2setcost(10)
e2function vector entity:boxSize()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:OBBMaxs() - this:OBBMins()
end
e2function vector entity:boxCenter()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:OBBCenter()
end
-- Same as using E:toWorld(E:boxCenter()) in E2, but since Lua runs faster, this is more efficient.
e2function vector entity:boxCenterW()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:LocalToWorld(this:OBBCenter())
end
e2function vector entity:boxMax()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:OBBMaxs()
end
e2function vector entity:boxMin()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
return this:OBBMins()
end
--[[******************************************************************************]]
-- Returns the entity's (min) axis-aligned bounding box
e2function vector entity:aabbMin()
if not IsValid(this) or not IsValid(this:GetPhysicsObject()) then return self:throw("Invalid physics object!", Vector(0, 0, 0)) end
local ret, _ = this:GetPhysicsObject():GetAABB()
return ret or Vector(0, 0, 0)
end
-- Returns the entity's (max) axis-aligned bounding box
e2function vector entity:aabbMax()
if not IsValid(this) or not IsValid(this:GetPhysicsObject()) then return self:throw("Invalid physics object!", Vector(0, 0, 0)) end
local _, ret = this:GetPhysicsObject():GetAABB()
return ret or Vector(0, 0, 0)
end
-- Returns the entity's axis-aligned bounding box size
e2function vector entity:aabbSize()
if not IsValid(this) or not IsValid(this:GetPhysicsObject()) then return self:throw("Invalid physics object!", Vector(0, 0, 0)) end
local ret, ret2 = this:GetPhysicsObject():GetAABB()
ret = ret or Vector(0,0,0)
ret2 = ret2 or Vector(0,0,0)
return ret2 - ret
end
--[[******************************************************************************]]
-- Returns the rotated entity's min world-axis-aligned bounding box corner
e2function vector entity:aabbWorldMin()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
local ret, _ = this:WorldSpaceAABB()
return ret or Vector(0, 0, 0)
end
-- Returns the rotated entity's max world-axis-aligned bounding box corner
e2function vector entity:aabbWorldMax()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
local _, ret = this:WorldSpaceAABB()
return ret or Vector(0, 0, 0)
end
-- Returns the rotated entity's world-axis-aligned bounding box size
e2function vector entity:aabbWorldSize()
if not IsValid(this) then return self:throw("Invalid entity!", Vector(0, 0, 0)) end
local ret, ret2 = this:WorldSpaceAABB()
ret = ret or Vector(0,0,0)
ret2 = ret2 or Vector(0,0,0)
return ret2 - ret
end
--[[******************************************************************************]]
hook.Add("PlayerLeaveVehicle","Exp2RunOnLeaveVehicle",function(ply, vehicle)
E2Lib.triggerEvent("playerLeftVehicle", { ply, vehicle } )
end)
hook.Add("PlayerEnteredVehicle","Exp2RunOnEnteredVehicle", function(ply, vehicle)
E2Lib.triggerEvent("playerEnteredVehicle", { ply, vehicle })
end)
__e2setcost(5)
e2function entity entity:driver()
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
return this:GetDriver()
end
e2function entity entity:passenger()
if not IsValid(this) or not this:IsVehicle() then return self:throw("Invalid vehicle!", nil) end
return this:GetPassenger(0)
end
--- Returns <ent> formatted as a string. Returns "<code>(null)</code>" for invalid entities.
e2function string toString(entity ent)
if not IsValid(ent) then return "(null)" end
return tostring(ent)
end
e2function string entity:toString() = e2function string toString(entity ent)
E2Lib.registerEvent("playerLeftVehicle", {
{ "Player", "e" },
{ "Vehicle", "e" },
})
E2Lib.registerEvent("playerEnteredVehicle", {
{ "Player", "e" },
{ "Vehicle", "e" },
})
--[[******************************************************************************]]
hook.Add("PhysgunPickup", "Exp2RunOnPhysgunPickup", function(ply, entity)
E2Lib.triggerEvent("playerPhysgunPickup", { ply, entity })
end)
hook.Add("PhysgunDrop", "Exp2RunOnPhysgunDrop", function(ply, entity)
E2Lib.triggerEvent("playerPhysgunDrop", { ply, entity })
end)
E2Lib.registerEvent("playerPhysgunPickup", {
{ "Player", "e" },
{ "Entity", "e" },
})
E2Lib.registerEvent("playerPhysgunDrop", {
{ "Player", "e" },
{ "Entity", "e" },
})
hook.Add("GravGunOnPickedUp", "Exp2RunOnGravGunPickup", function(ply, entity)
E2Lib.triggerEvent("playerGravGunPickup", { ply, entity })
end)
hook.Add("GravGunOnDropped", "Exp2RunOnGravGunDrop", function(ply, entity)
E2Lib.triggerEvent("playerGravGunDrop", { ply, entity })
end)
E2Lib.registerEvent("playerGravGunPickup", {
{ "Player", "e" },
{ "Entity", "e" },
})
E2Lib.registerEvent("playerGravGunDrop", {
{ "Player", "e" },
{ "Entity", "e" },
})
hook.Add("OnPlayerPhysicsPickup", "Exp2RunOnPhysicsPickup", function(ply, entity)
E2Lib.triggerEvent("playerPhysicsPickup", { ply, entity })
end)
hook.Add("OnPlayerPhysicsDrop", "Exp2RunPhysicsDrop", function(ply, entity, thrown)
E2Lib.triggerEvent("playerPhysicsDrop", { ply, entity, thrown == true and 1 or 0 })
end)
E2Lib.registerEvent("playerPhysicsPickup", {
{ "Player", "e" },
{ "Entity", "e" },
})
E2Lib.registerEvent("playerPhysicsDrop", {
{ "Player", "e" },
{ "Entity", "e" },
{ "Thrown", "n" }
})
--[[******************************************************************************]]
local function composedata(startSize, endSize, length, material, color, alpha)
if string.find(material, '"', 1, true) then return nil end
endSize = math.Clamp( endSize, 0, 128 )
startSize = math.Clamp( startSize, 0, 128 )
return {
Color = Color( color[1], color[2], color[3], alpha ),
Length = length,
StartSize = startSize,
EndSize = endSize,
Material = material,
}
end
local function validateCanTrail(self, ent)
if not checkOwner(self) then return end
if not IsValid(ent) then return self:throw("Invalid entity!", nil) end
if not isOwner(self, ent) then return self:throw("You do not own this entity!", nil) end
end
local function removeTrail(self, ent)
local PlayerSteamID = self.player:SteamID()
if not trailedEntsAmount[PlayerSteamID] then return end
if not trailedEntsAmount[PlayerSteamID][ent] then return end
trailedEntsAmount[PlayerSteamID][ent] = nil
duplicator.EntityModifiers.trail(self.player, ent, nil)
end
local function setTrail(self, ent, Data)
local PlayerSteamID = self.player:SteamID()
if not trailedEntsAmount[PlayerSteamID] then trailedEntsAmount[PlayerSteamID] = {} end
-- Removing a trail (just in case)
if Data == nil then
removeTrail(self, ent)
return
end
-- Setting a trail
if wire_expression2_entity_trails_max:GetInt() < table.Count(trailedEntsAmount[PlayerSteamID])+1 then return self:throw("Trails limit reached!", nil) end
trailedEntsAmount[PlayerSteamID][ent] = true
ent:CallOnRemove("wire_expression2_trail_remove", function(ent)
trailedEntsAmount[PlayerSteamID][ent] = nil
end)