-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCapture-Lib.nut
More file actions
8572 lines (7394 loc) · 295 KB
/
PCapture-Lib.nut
File metadata and controls
8572 lines (7394 loc) · 295 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
/*+--------------------------------------------------------------------------------+
| PCapture Vscripts Library |
+---------------------------------------------------------------------------------+
| Author: |
| One-of-a-Kind - laVashik |
+---------------------------------------------------------------------------------+
| pcapture-lib.nut |
| The main file in the library. initializes required parts of the library |
| GitHud repo: https://github.com/LaVashikk/PCapture-LIB |
+----------------------------------------------------------------------------------+ */
local version = "PCapture-Lib 4.0 Release Candidate"
local rootScope = getroottable()
// `Self` must be in any case, even if the script is run directly by the interpreter
if (!("self" in this)) {
self <- Entities.First()
} else {
getroottable()["self"] <- self
}
if("LIB_VERSION" in getroottable() && version.find("Debug") == null) {
printl("\n")
dev.warning("PCapture-Lib already initialized.")
if(LIB_VERSION != version) {
printl("\n======================== WARNING ========================")
printl("Attempting to initialize different versions of the PCapture-Lib library!")
macros.fprint("Inited version \"{}\" != \"{}\"", LIB_VERSION, version)
printl("==========================================================\n")
}
return
}
/*
* Global Tables Initialization:
*
* This section initializes the fundamental global tables required for the PCapture Library.
* These tables are essential for ensuring the correct functioning
* of the library's various components during runtime.
*/
::pcapEntityCache <- {}
::EntitiesScopes <- {}
::TracePlusIgnoreEnts <- {}
/*+--------------------------------------------------------------------------------+
| PCapture Vscripts Library |
+----------------------------------------------------------------------------------+
| Author: |
| Data Structure Maestro - laVashik ^v^ |
+----------------------------------------------------------------------------------+
| The Improved Data Types module, offering enhanced versions of standard VScripts|
| data structures like arrays, lists, and trees for efficient data management. |
+----------------------------------------------------------------------------------+ */
::pcapEntity <- class {
CBaseEntity = null;
/*
* Constructor for the entity object.
*
* @param {CBaseEntity} entity - The entity object.
*/
constructor(entity) {
if (!entity || !entity.IsValid()) throw("pcapEntity: invalid entity");
if(typeof entity == "pcapEntity")
entity = entity.CBaseEntity
this.CBaseEntity = entity
EntitiesScopes[this.CBaseEntity] <- {}
entity.ValidateScriptScope()
entity.GetScriptScope().selfEx <- this // todo: no docs about it
}
/*
* Sets the angles of the entity.
*
* @param {Vector} angles - The angle vector.
*/
function SetAngles2(angles) {
this.CBaseEntity.SetAngles(angles.x, angles.y, angles.z)
}
// Destroys the entity.
function Destroy(fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.Destroy, fireDelay, null, this)
if(!this.CBaseEntity || !this.CBaseEntity.IsValid()) return
this.CBaseEntity.Destroy()
this.CBaseEntity = null
}
/*
* Kills the entity.
*
* @param {number} fireDelay - Delay in seconds before applying.
*/
function Kill(fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.Kill, fireDelay, null, this)
if(!this.CBaseEntity || !this.CBaseEntity.IsValid()) return
EntFireByHandle(this.CBaseEntity, "kill")
this.CBaseEntity = null
}
/*
* Dissolves the entity using an env_entity_dissolver entity, creating a visual effect of the entity dissolving away.
*
* This method utilizes an env_entity_dissolver entity to create the dissolve effect. If the entity doesn't have a targetname,
* a unique targetname is assigned to it before initiating the dissolve process.
*/
function Dissolve(fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.Dissolve, fireDelay, null, this)
if(!dissolver || !dissolver.IsValid()) throw("The entity 'dissolver' is missing, the Dissolve method will not work.")
if(this.GetName() == "")
this.SetUniqueName("targetname")
for(local entity; entity = Entities.FindByName(entity, this.GetName()); ) if(!macros.IsEqual(this, entity)) {
this.SetUniqueName()
break
}
dissolver.SetKeyValue("target", this.GetName())
EntFireByHandle(dissolver, "dissolve")
this.SetUserData("Dissolved", true)
}
/*
* Checks if the entity is valid.
*
* @returns {bool} - True if the entity is valid, false otherwise.
*/
function IsValid() {
return this.CBaseEntity && this.CBaseEntity.IsValid() && !this.GetUserData("Dissolved")
}
/*
* Checks if this entity is the player entity.
*
* @returns {bool} - True if this entity is the player, false otherwise.
*/
function IsPlayer() {
return this.CBaseEntity.GetClassname() == "player"
}
/*
* Gets the eye position of the player entity.
*
* @returns {Vector|null} - The eye position as a Vector, or null if the entity is not a player.
*
* This method retrieves the position from which the player's view is rendered. It is important to note that this method only works with player entities.
* For non-player entities, it returns GetCenter.
*/
function EyePosition() {
if(this.IsPlayer()) return this.CBaseEntity.EyePosition()
return this.GetCenter()
}
/*
* Gets the eye angles of the player entity.
*
* @returns {Vector|null} - The eye angles as a Vector representing pitch, yaw, and roll, or null if the entity is not a player.
*
* This method retrieves the orientation of the player's view. It is important to note that this method only works with player entities.
* For non-player entities, it returns GetAngles.
*/
function EyeAngles() {
if(this.IsPlayer()) return this.GetUserData("Eye").GetAngles()
return this.GetAngles()
}
/*
* Gets the forward vector from the player entity's eye position, indicating the direction the player is looking.
*
* @returns {Vector|null} - The forward vector as a Vector, or null if the entity is not a player.
*
* This method retrieves the direction in which the player is facing. It is important to note that this method only works with player entities.
* For non-player entities, it returns GetForwardVector.
*/
function EyeForwardVector() {
if(this.IsPlayer()) return this.GetUserData("Eye").GetForwardVector()
return this.GetForwardVector()
}
/*
* Sets the key-value pair of the entity.
*
* @param {string} key - The key of the key-value pair.
* @param {int|float|Vector|string} value - The value of the key-value pair.
*/
function SetKeyValue(key, value, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetKeyValue, fireDelay, [key, value], this)
this.SetUserData(key, value)
switch (typeof value) {
case "integer":
return this.CBaseEntity.__KeyValueFromInt(key, value);
case "float":
return this.CBaseEntity.__KeyValueFromFloat(key, value);
case "Vector":
return this.CBaseEntity.__KeyValueFromVector(key, value);
case "string":
return this.CBaseEntity.__KeyValueFromString(key, value);
}
return this.CBaseEntity.__KeyValueFromString(key, value.tostring());
}
/*
* Connects an output of the entity to a target entity and input, allowing for parameter overrides, delays, and limited trigger counts.
*
* @param {string} outputName - The name of the output to connect.
* @param {string|CBaseEntity|pcapEntity} target - The target entity or its targetname to connect the output to.
* @param {string} input - The name of the input on the target entity to connect to.
* @param {string} param - An optional parameter override for the input. (default: "")
* @param {number} delay - An optional delay in seconds before triggering the input. (default: 0)
* @param {number} fires - The number of times the output should fire before becoming inactive. -1 indicates unlimited fires. (default: -1)
*
* This method provides a way to establish connections between entities, allowing them to trigger actions on each other based on outputs and inputs.
*/
function AddOutput(outputName, target, input, param = "", delay = 0, fires = -1) {
if(typeof target == "instance" || typeof target == "pcapEntity")
target = target.GetName()
this.SetKeyValue(outputName, target + "\x001B" + input + "\x001B" + param + "\x001B" + delay + "\x001B" + fires)
}
/*
* Connects an output of the entity to a script function or string, allowing for delays and limited trigger counts.
*
* @param {string} outputName - The name of the output to connect.
* @param {string|function} script - The VScripts code or function name to execute when the output is triggered.
* @param {number} delay - An optional delay in seconds before executing the script. (default: 0)
* @param {number} fires - The number of times the output should fire before becoming inactive. -1 indicates unlimited fires. (default: -1)
*
* This method provides a way to connect entity outputs directly to script code, allowing for custom actions to be performed when the output is triggered.
*/
function ConnectOutputEx(outputName, script, delay = 0, fires = -1) {
if(typeof script == "function") {
local funcName = script.getinfos().name
local src = script.getinfos().src
if(!funcName || src == "unnamedbuffer") { // If scripts is an anonymous function, assign it a unique name and add it to the root scope
funcName = UniqueString("OutputFunc")
getroottable()[funcName] <- script
}
script = funcName + "()"
} else {
return this.ConnectOutput(outputName, script)
}
this.AddOutput(outputName, "!self", "RunScriptCode", script, delay, fires)
}
/*
* Connects a script function to an input of the entity, allowing external code to react to input events.
*
* @param {string} inputName - The name of the input to connect to.
* @param {function} closure - The function to execute when the input is triggered.
*
* This function enables the association of custom logic with specific entity inputs, facilitating communication and interaction between entities and external scripts.
*/
function SetInputHook(inputName, closure) {
if(this.ValidateScriptScope() == false) return
this.GetScriptScope()["Input" + inputName] <- closure
// This is a quick hack to address a bug where the game sometimes calls `Inputname` instead of `InputName` (Valve, why?)
this.GetScriptScope()["Input" + inputName.tolower()] <- closure
}
/*
* Plays a sound on the entity.
*
* @param {string} soundName - The name of the sound to play.
* @param {number} fireDelay - An optional delay in seconds before playing the sound. (default: 0)
* @param {string} eventName - The name of the event to schedule the sound playback under. (default: "global")
*
* This function is an enhanced version of the built-in EmitSound method, allowing for delayed sound playback
* and cancellation using ScheduleEvent.Cancel.
*/
function EmitSound(soundName, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.EmitSound, fireDelay, [soundName], this)
this.CBaseEntity.EmitSound(soundName)
}
/*
* Plays a sound on the entity with advanced options, including volume control and looping.
*
* @param {string} soundName - The name of the sound to play.
* @param {number} volume - The volume of the sound (0-10). (default: 10)
* @param {boolean} looped - Whether the sound should loop. (default: false)
* @param {number} fireDelay - An optional delay in seconds before playing the sound. (default: 0)
* @param {string} eventName - The name of the event to schedule the sound playback under. (default: "global")
*
* This function provides more control over sound playback compared to EmitSound. It allows you to adjust the volume,
* loop sounds that are not inherently loopable, and stop the sound playback using StopSoundEx.
*/
function EmitSoundEx(soundName, volume = 10, looped = false, fireDelay = 0, eventName = "global") { // add volume and loop flag
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.EmitSoundEx, fireDelay, [soundName], this)
// for updating volume if sound active
local soundEnt = this.GetUserData(soundName)
if(soundEnt && soundEnt.IsValid())
return soundEnt.SetAbsOrigin(this.GetOrigin() + Vector(0, 0, 3000 - volume * 300))
// SO FKING CURSED WORKAROUND EVER!
local soundEnt = entLib.CreateProp("prop_physics", Vector(), ALWAYS_PRECACHED_MODEL)
soundEnt.SetAbsOrigin(this.GetOrigin() + Vector(0, 0, 3000 - volume * 300))
soundEnt.SetParent(this)
soundEnt.SetKeyValue("rendermode", 5)
soundEnt.SetAlpha(0)
local soundTime = macros.GetSoundDuration(soundName)
this.SetUserData(soundName, soundEnt)
soundEnt.EmitSound(soundName)
if(!looped) return soundEnt.Destroy(soundTime)
ScheduleEvent.AddInterval(soundEnt, soundEnt.EmitSound, soundTime, soundTime, [soundName], soundEnt) // :>
}
/*
* Stops a sound played using EmitSoundEx.
*
* @param {string} soundName - The name of the sound to stop.
* @param {number} fireDelay - An optional delay in seconds before stopping the sound. (default: 0)
* @param {string} eventName - The name of the event to schedule the sound stop under. (default: "global")
*
* This function allows you to interrupt sounds that were started using EmitSoundEx. It ensures the sound is stopped
* and the associated entity is cleaned up.
*/
function StopSoundEx(soundName, fireDelay = 0, eventName = "global") {
if(fireDelay != 0) {
return ScheduleEvent.Add(eventName, this.StopSoundEx, fireDelay, [soundName], this)
}
local soundEnt = this.GetUserData(soundName)
if(!soundEnt || !soundEnt.IsValid()) return
ScheduleEvent.TryCancel(soundEnt)
soundEnt.SetOrigin(Vector(0, 0, 10000))
soundEnt.Destroy(0.04)
}
/*
* Sets the targetname of the entity.
*
* @param {string} name - The targetname to be set.
*/
function SetName(name, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetName, fireDelay, [name], this)
this.SetKeyValue("targetname", name)
}
/*
* Sets a unique targetname for the entity using the provided prefix.
*
* @param {string} prefix - The prefix to be used for the unique targetname. (optional, default="u")
*/
function SetUniqueName(prefix = "pcapEnt", fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetUniqueName, fireDelay, [prefix], this)
this.SetKeyValue("targetname", UniqueString(prefix))
}
/*
* Sets the parent of the entity.
*
* @param {string|CBaseEntity|pcapEntity} parent - The parent entity object or its targetname.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetParent(parentEnt, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetParent, fireDelay, [parentEnt], this)
if(typeof parentEnt != "string") {
local Pent = entLib.FromEntity(parentEnt)
local hasUniqueName = true
if(Pent.GetName() == "")
Pent.SetUniqueName("parent")
for(local entity; entity = Entities.FindByName(entity, Pent.GetName()); ) if(!macros.IsEqual(Pent, entity)) {
Pent.SetUniqueName("parent")
break
}
parentEnt = Pent.GetName()
}
EntFireByHandle(this.CBaseEntity, "SetParent", parentEnt)
}
/*
* Gets a list of all DIRECT children of this entity.
* This function is not recursive and will not find grandchildren.
*
* @returns {List} A List containing all direct child pcaptEntity objects.
*/
function GetChildren() {
local childrenList = List()
local child = this.CBaseEntity.FirstMoveChild();
while(child) {
childrenList.append(entLib.FromEntity(child));
child = child.NextMovePeer();
}
return childrenList;
}
/*
* Recursively finds all descendants (children, grandchildren, etc.) of this entity.
* Performs a depth-first search of the entity hierarchy.
*
* @returns {List} A List containing all descendant pcaptEntity objects.
*/
function GetAllChildrenRecursively() {
local descendantsList = List();
_findDescendantsRecursive(this.CBaseEntity, descendantsList);
return descendantsList;
}
/*
* Sets the collision of the entity.
*
* @param {number} solidType - The type of collision.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetCollision(solidType, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetCollision, fireDelay, [solidType], this)
EntFireByHandle(this.CBaseEntity, "SetSolidtype", solidType.tostring())
this.SetUserData("Solidtype", solidType)
}
/*
* Sets the collision group of the entity.
*
* @param {number} collisionGroup - The collision group.
*/
function SetCollisionGroup(collisionGroup, fireDelay = 0, eventName = "global") {
this.SetKeyValue("CollisionGroup", collisionGroup, fireDelay, eventName)
}
/*
* Starts playing the specified animation.
*
* @param {string} animationName - The name of the animation to play.
* @param {number} fireDelay - Delay in seconds before starting the animation.
*/
function SetAnimation(animationName, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetAnimation, fireDelay, [animationName], this)
EntFireByHandle(this.CBaseEntity, "SetAnimation", animationName)
this.SetUserData("animation", animationName)
}
/*
* Sets the alpha (opacity) of the entity.
*
* @param {number} opacity - The alpha value (0-255).
* @param {number} fireDelay - Delay in seconds before applying.
* Note: Don't forget to set "rendermode" to 5 for alpha to work.
*/
function SetAlpha(opacity, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetAlpha, fireDelay, [opacity], this)
EntFireByHandle(this.CBaseEntity, "Alpha", opacity.tostring())
this.SetUserData("alpha", opacity)
}
/*
* Sets the color of the entity.
*
* @param {string|Vector} colorValue - The color value as a string (e.g., "255 0 0") or a Vector.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetColor(colorValue, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetColor, fireDelay, [colorValue], this)
if(typeof colorValue == "Vector")
colorValue = macros.VecToStr(colorValue)
EntFireByHandle(this.CBaseEntity, "Color", colorValue)
this.SetUserData("color", colorValue)
}
/*
* Sets the skin of the entity.
*
* @param {number} skin - The skin number.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetSkin(skin, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetSkin, fireDelay, [skin], this)
EntFireByHandle(this.CBaseEntity, "skin", skin.tostring())
this.SetUserData("skin", skin)
}
/*
* Sets whether the entity should be drawn or not.
*
* @param {boolean} isEnabled - True to enable drawing, false to disable.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetDrawEnabled(isEnabled, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetDrawEnabled, fireDelay, [isEnabled], this)
local action = isEnabled ? "EnableDraw" : "DisableDraw"
EntFireByHandle(this.CBaseEntity, action)
this.SetUserData("IsDraw", isEnabled)
}
function Disable(fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.Disable, fireDelay, null, this)
EntFireByHandle(this.CBaseEntity, "Disable")
this.SetTraceIgnore(true)
}
function Enable(fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.Enable, fireDelay, null, this)
EntFireByHandle(this.CBaseEntity, "Enable")
this.SetTraceIgnore(false)
}
/*
* Checks if the entity is set to be drawn.
*
* @returns {boolean} - True if the entity is set to be drawn, false otherwise.
*/
function IsDrawEnabled() {
return this.GetUserData("IsDraw")
}
/*
* Sets whether the entity should be ignored during traces.
*
* @param {boolean} isEnabled - True to ignore the entity during traces, false otherwise.
*/
function SetTraceIgnore(isEnabled, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetTraceIgnore, fireDelay, [isEnabled], this)
this.SetUserData("TracePlusIgnore", isEnabled)
TracePlusIgnoreEnts[this.CBaseEntity] <- isEnabled
}
function IsTraceIgnored() {
return this.GetUserData("TracePlusIgnore")
}
/*
* Sets the spawnflags for the entity.
*
* @param {number} flag - The spawnflags value to set.
*/
function SetSpawnflags(flag, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetSpawnflags, fireDelay, [flag], this)
this.SetKeyValue("SpawnFlags", flag)
}
/*
* Sets the scale of the entity's model.
*
* @param {number} scaleValue - The scale value.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetModelScale(scaleValue, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetModelScale, fireDelay, [scaleValue], this)
EntFireByHandle(this.CBaseEntity, "AddOutput", "ModelScale " + scaleValue)
this.SetUserData("ModelScale", scaleValue)
// hack for entity update
EntFireByHandle(this.CBaseEntity, "SetBodyGroup", "1"); EntFireByHandle(this.CBaseEntity, "SetBodyGroup", "0", 0.02)
}
/*
* Gets the current model scale of the entity.
*
* @returns {number} - The current model scale value.
*/
function GetModelScale() {
local res = this.GetUserData("ModelScale")
return res ? res : 1
}
/*
* Sets the absolute center of the entity in world space.
* This will override any parent-relative positioning.
*
* @param {Vector} vector - The desired absolute center vector in world coordinates.
*/
function SetAbsCenter(vector) {
local offset = this.CBaseEntity.GetCenter() - this.CBaseEntity.GetOrigin()
this.CBaseEntity.SetAbsOrigin( vector - offset )
}
/*
* Sets the center of the entity.
*
* @param {Vector} vector - The center vector.
*/
function SetCenter(vector) {
local offset = this.CBaseEntity.GetCenter() - this.CBaseEntity.GetOrigin()
this.CBaseEntity.SetOrigin( vector - offset )
}
/*
* Sets a context value for the entity.
*
* @param {string} name - The name of the context value.
* @param {any} value - The value to set.
* @param {number} fireDelay - Delay in seconds before applying.
*/
function SetContext(name, value, fireDelay = 0, eventName = "global") {
if(fireDelay != 0)
return ScheduleEvent.Add(eventName, this.SetContext, fireDelay, [name, value], this)
EntFireByHandle(this.CBaseEntity, "AddContext", (name + ":" + value))
this.SetUserData(name, value)
}
/*
* Stores an arbitrary value associated with the entity.
*
* @param {string} name - The name of the value.
* @param {any} value - The value to store.
*/
function SetUserData(name, value) {
EntitiesScopes[this.CBaseEntity][name.tolower()] <- value
}
/*
* Gets a stored user data value.
*
* @param {string} name - The name of the value to get.
* @returns {any} - The stored value, or null if not found.
*/
function GetUserData(name) {
name = name.tolower()
if(name in EntitiesScopes[this.CBaseEntity])
return EntitiesScopes[this.CBaseEntity][name]
return null
}
/*
* Calculates the local coordinates (position and angles) of this entity relative to its move parent.
* If the entity has no parent, its world coordinates are returned.
*
* @returns {table} A table containing the local coordinates: { pos = <Vector>, ang = <Vector> }.
*/
function GetLocalCoords() {
local pParent = this.CBaseEntity.GetMoveParent();
if (!pParent) {
// If there's no parent, local coordinates are equal to world coordinates.
return { pos = this.GetOrigin(), ang = this.GetAngles() };
}
// Get the world positions and angles of both entities
local childWorldPos = this.GetOrigin();
local parentWorldPos = pParent.GetOrigin();
local childWorldAng = this.GetAngles();
local parentWorldAng = pParent.GetAngles();
// Calculate the offset vector in world coordinates
local worldOffset = childWorldPos - parentWorldPos;
// "Unrotate" the world offset vector by the parent's angles to get the local position
local localPos = math.vector.unrotate(worldOffset, parentWorldAng);
local localAng = childWorldAng - parentWorldAng;
return { pos = localPos, ang = localAng };
}
/*
* A function that sets the entity's absolute origin in world space using teleportation.
* This function bypasses interpolation, making the position change instantaneous.
*
* @param {Vector} desiredAbsVec - The desired absolute position in world coordinates.
*/
function SetAbsOrigin(desiredAbsVec) {
local pParent = this.CBaseEntity.GetMoveParent()
// If there is no parent, local coordinates are equivalent to absolute coordinates.
if (!pParent) {
return this.CBaseEntity.SetOrigin(desiredAbsVec)
}
// We have parent, so we need to convert the desired ABSOLUTE coordinates into LOCAL coordinates.
local parentWorldPos = pParent.GetOrigin()
local parentWorldAng = pParent.GetAngles()
// Calculate the offset vector from the parent to the desired point in world coordinates.
local worldOffsetVector = desiredAbsVec - parentWorldPos
// Transform the world offset vector into a local one.
local localPos = math.vector.unrotate(worldOffsetVector, parentWorldAng)
this.SetOrigin(localPos)
}
/*
* Sets the entity's absolute origin using the native engine method.
* Unlike the custom SetAbsOrigin, this method handles attachment parents correctly.
* However, it causes client-side interpolation (visual sliding) when moving the entity.
*
* @param {Vector} desiredAbsVec - The desired absolute position in world coordinates.
*/
function SetAbsOriginNative(desiredAbsVec) {
this.CBaseEntity.SetAbsOrigin(desiredAbsVec)
}
/*
* Checks if the bounding box of an entity is a cube (all sides have equal length).
*
* @returns {boolean} - True if the bounding box is a cube, false otherwise.
*/
function IsSquareBbox() {
// Get the minimum and maximum coordinates of the bounding box
local mins = this.GetBoundingMins();
local maxs = this.GetBoundingMaxs();
// Calculate the size of the bounding box along each axis
local sizeX = abs(maxs.x - mins.x)
local sizeY = abs(maxs.y - mins.y)
local sizeZ = abs(maxs.z - mins.z)
// Check if the sizes are equal along all axes
return sizeX == sizeY && sizeY == sizeZ;
}
//! TODO ADD TO DOCS
function GetBoundingCenter() {
return (this.CBaseEntity.GetBoundingMaxs() - this.CBaseEntity.GetBoundingMins()) * 0.5
}
/*
* Returns the axis-aligned bounding box (AABB) of the entity.
*
* @returns {table} - The minimum bounds, maximum bounds, and center of the entity.
*/
function GetAABB() {
return {
min = this.CreateAABB(0),
center = this.CreateAABB(4),
max = this.CreateAABB(7)
}
}
/*
* Gets the index of the entity.
*
* @returns {int} - The index of the entity.
*/
function GetIndex() {
return this.CBaseEntity.entindex()
}
/*
* Gets the value of the key-value pair of the entity.
* Note: only works if you used the SetKeyValue function!
*
* @param {string} key - The key of the key-value pair.
* @returns {any} - The value of the key-value pair.
*/
function GetKeyValue(key) {
local value = this.GetUserData(key)
if(value != null)
return value
switch(key) {
case "model":
return this.GetModelName()
break
case "health":
return this.GetHealth()
break
case "targetname":
return this.GetName()
break
}
return null
}
/*
* Gets the spawnflags for the entity.
* Note: only works if you used the SetKeyValue function!
*
* @returns {int|null} - The spawnflags value.
*/
function GetSpawnflags() {
return this.GetUserData("spawnflags")
}
/*
* Gets the alpha/opacity value of the entity.
* Note: only works if you used the SetKeyValue function!
*
* @returns {int|null} - The alpha value.
*/
function GetAlpha() {
local alpha = this.GetUserData("alpha")
return alpha != null ? alpha : 255
}
/*
* Gets the color value of the entity.
* Note: only works if you used the SetKeyValue function!
*
* @returns {string|null} - The color value.
*/
function GetColor() {
local color = this.GetUserData("color")
return color ? color : "255 255 255"
}
/*
* Gets the skin of the entity.
* Note: only works if you used the SetKeyValue function!
*
* @returns {number|null} - The skin number.
*/
function GetSkin() {
local skin = this.GetUserData("skin")
return skin ? skin : 0
}
/*
* Gets the prefix of the entity name.
*
* @returns {string} - The prefix of the entity name.
*/
function GetNamePrefix() {
return macros.GetPrefix(this.GetName())
}
/*
* Gets the postfix of the entity name.
*
* @returns {string} - The postfix of the entity name.
*/
function GetNamePostfix() {
return macros.GetPostfix(this.GetName())
}
/*
* Retrieves the partner instance of the entity, prioritizing the actual partner entity if available, and falling back to user-stored partner data if necessary.
*
* @returns {Entity|null} - The partner entity, or null if no partner is found.
*
* This method first attempts to retrieve the actual partner entity using the `GetPartnerInstance` method of the underlying CBaseEntity.
* If no partner entity is found, it then checks for a partner stored in the entity's user data under the key "partner". This can be useful in situations where the partner relationship is not directly established through entity properties but is managed through custom logic.
*/
function GetPartnerInstance() {
if(this.CBaseEntity instanceof CLinkedPortalDoor)
return entLib.FromEntity(this.CBaseEntity.GetPartnerInstance())
return FindPartnerForPropPortal(this)
}
/*
* Gets the oriented bounding box of the entity.
*
* @param {number} stat - 0 for min bounds, 7 for max bounds, 4 for center bounds.
* @returns {Vector} - The bounds vector.
*/
function CreateAABB(stat) {
local angles = this.GetAngles()
if(stat == 4)
angles = Vector(45, 45, 45)
local all_vertex = this.GetBBoxPoints()
local x = array(8)
local y = array(8)
local z = array(8)
foreach(idx, vec in all_vertex) {
x[idx] = vec.x
y[idx] = vec.y
z[idx] = vec.z
}
x.sort(); y.sort(); z.sort()
local result
if(stat == 4) { // centered
result = ( Vector(x[7], y[7], z[7]) - Vector(x[0], y[0], z[0]) ) * 0.5
}
else {
result = Vector(x[stat], y[stat], z[stat])
}
return result
}
/*
* Gets the 8 vertices of the axis-aligned bounding box.
*
* @returns {Array<Vector>} - The 8 vertices of the bounding box.
*/
function GetBBoxPoints() {
local max = this.GetBoundingMaxs();
local min = this.GetBoundingMins();
local angles = this.GetAngles()
local getVertex = macros.GetVertex
return [
getVertex(max, min, min, angles), // 0 - Right-Bottom-Front
getVertex(max, max, min, angles), // 1 - Right-Top-Front
getVertex(min, max, min, angles), // 2 - Left-Top-Front
getVertex(min, min, min, angles), // 3 - Left-Bottom-Front
getVertex(min, min, max, angles), // 4 - Left-Bottom-Back
getVertex(min, max, max, angles), // 5 - Left-Top-Back
getVertex(max, max, max, angles), // 6 - Right-Top-Back
getVertex(max, min, max, angles), // 7 - Right-Bottom-Back
]
}
/*
* Gets the faces of the entity's bounding box as an array of triangle vertices.
*
* @returns {array} - An array of 12 Vector triplets, where each triplet represents the three vertices of a triangle face.
*/
function GetBBoxFaces() {
local vertices = this.GetBBoxPoints()
local getTriangle = macros.GetTriangle
return [
/* Bottom face triangles */
getTriangle(vertices[0], vertices[3], vertices[4]), // Face 0: Right-Bottom-Front, Left-Bottom-Front, Left-Bottom-Back
getTriangle(vertices[0], vertices[4], vertices[7]), // Face 1: Right-Bottom-Front, Left-Bottom-Back, Right-Bottom-Back
/* Top face triangles */
getTriangle(vertices[1], vertices[2], vertices[5]), // Face 2: Right-Top-Front, Left-Top-Front, Left-Top-Back
getTriangle(vertices[1], vertices[5], vertices[6]), // Face 3: Right-Top-Front, Left-Top-Back, Right-Top-Back
/* Left face triangles */
getTriangle(vertices[3], vertices[2], vertices[5]), // Face 4: Left-Bottom-Front, Left-Top-Front, Left-Top-Back
getTriangle(vertices[3], vertices[5], vertices[4]), // Face 5: Left-Bottom-Front, Left-Top-Back, Left-Bottom-Back
/* Right face triangles */
getTriangle(vertices[0], vertices[1], vertices[6]), // Face 6: Right-Bottom-Front, Right-Top-Front, Right-Top-Back
getTriangle(vertices[0], vertices[6], vertices[7]), // Face 7: Right-Bottom-Front, Right-Top-Back, Right-Bottom-Back
/* Front face triangles */
getTriangle(vertices[0], vertices[1], vertices[2]), // Face 8: Right-Bottom-Front, Right-Top-Front, Left-Top-Front
getTriangle(vertices[0], vertices[2], vertices[3]), // Face 9: Right-Bottom-Front, Left-Top-Front, Left-Bottom-Front
/* Back face triangles */
getTriangle(vertices[7], vertices[6], vertices[5]), // Face 10: Right-Bottom-Back, Right-Top-Back, Left-Top-Back
getTriangle(vertices[7], vertices[5], vertices[4]) // Face 11: Right-Bottom-Back, Left-Top-Back, Left-Bottom-Back
]
}
/*