-
Notifications
You must be signed in to change notification settings - Fork 45
/
sh_entity.lua
1893 lines (1625 loc) · 54.6 KB
/
sh_entity.lua
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
--[[
© 2015 CloudSixteen.com do not share, re-distribute or modify
without permission of its author (kurozael@gmail.com).
Clockwork was created by Conna Wiles (also known as kurozael.)
http://cloudsixteen.com/license/clockwork.html
--]]
local Clockwork = Clockwork;
local GetViewEntity = GetViewEntity;
local EffectData = EffectData;
local tostring = tostring;
local IsValid = IsValid;
local CurTime = CurTime;
local Color = Color;
local Angle = Angle;
local type = type;
local string = string;
local timer = timer;
local table = table;
local math = math;
local ents = ents;
local util = util;
--[[
@codebase Shared
@details A library adding additional functionality to entities.
--]]
Clockwork.entity = Clockwork.kernel:NewLibrary("Entity");
if (CLIENT) then
--[[
@codebase Client
@details A function to get a weapon's muzzle position from its viewmodel.
@param Entity The player's current weapon.
@param Entity The player's attachment on the weapon.
@returns Vector The coordinates of weapon's muzzle position.
@returns Angle The angle of the weapon's muzzle position.
--]]
function Clockwork.entity:GetMuzzlePos(weapon, attachment)
if (!IsValid(weapon)) then
return vector_origin, Angle(0, 0, 0);
end;
local origin = weapon:GetPos();
local angle = weapon:GetAngles();
if (weapon:IsWeapon() and weapon:IsCarriedByLocalPlayer()) then
local owner = weapon:GetOwner();
if (IsValid(owner) and GetViewEntity() == owner) then
local viewmodel = owner:GetViewModel();
if (IsValid(viewmodel)) then
weapon = viewmodel;
end;
end;
end;
local attachment = weapon:GetAttachment(attachment or 1);
if (!attachment) then
return origin, angle;
end;
return attachment.Pos, attachment.Ang;
end;
else
--[[
@codebase Server
@details A function to get all door entities which are stored from server start.
@return Table A list of all door entities.
--]]
function Clockwork.entity:GetDoorEntities()
return self.DoorEntities or {};
end;
end;
--[[
@codebase Shared
@details A function to check if an entity is a door or not by seeing if its name includes "door" or if the entity is one of the following classes: "func_door", "func_door_rotating", "prop_door_rotating", "func_movelinear"
@param Entity The entity being check as a door.
@returns Bool Whether the entity is a door or not.
--]]
function Clockwork.entity:IsDoor(entity)
if (IsValid(entity)) then
local class = entity:GetClass();
local model = entity:GetModel();
if (class and model) then
class = string.lower(class);
model = string.lower(model);
if (class == "func_door"
or class == "func_door_rotating"
or class == "prop_door_rotating"
or (class == "prop_dynamic" and string.find(model, "door"))
or class == "func_movelinear") then
return true;
end;
end;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity is decaying by being in the process of fading out and being removed.
@param Entity The entity being checked as decaying or not.
@returns Bool Whether or not the entity is decaying.
--]]
function Clockwork.entity:IsDecaying(entity)
return entity.cwIsDecaying;
end;
--[[
@codebase Shared
@details A function to get a door entity's partners which are doors that open along with it.
@param Entity The entity to get the door partners from.
@return Table A list of partners on the door entity.
--]]
function Clockwork.entity:GetDoorPartners(entity)
local doorPartners = {entity};
local doorEntities = ents.FindByClass(entity:GetClass());
local doorAngles = entity:GetAngles();
local doorModel = entity:GetModel();
local doorSkin = entity:GetSkin();
local doorPos = entity:GetPos();
for k, v in pairs(doorEntities) do
if (entity != v and v:GetModel() == doorModel
and v:GetSkin() == doorSkin) then
local tempPosition = v:GetPos();
local distance = tempPosition:Distance(doorPos);
if (distance >= 90 and distance <= 100
and v:GetAngles() != doorAngles) then
if (math.floor(tempPosition.z) == math.floor(doorPos.z)) then
doorPartners[#doorPartners + 1] = v;
end;
end;
end;
end;
return doorPartners;
end;
--[[
@codebase Shared
@details A function to check if an entity is in a specified box area.
@param Entity The entity to check if it's in a box.
@param Int Minimum position to check in.
@param Int Maximum position to check in.
@returns Bool Whether or not an entity is within the defined box.
--]]
function Clockwork.entity:IsInBox(entity, minimum, maximum)
local position = entity:GetPos();
if (entity:IsPlayer() or entity:IsNPC()) then
position = entity:GetShootPos();
end;
if ((position.x >= math.min(minimum.x, maximum.x) and position.x <= math.max(minimum.x, maximum.x))
and (position.y >= math.min(minimum.y, maximum.y) and position.y <= math.max(minimum.y, maximum.y))
and (position.z >= math.min(minimum.z, maximum.z) and position.z <= math.max(minimum.z, maximum.z))) then
return true;
else
return false;
end;
end;
--[[
@codebase Shared
@details A function to get a ragdoll entity's pelvis position.
@param Entity The entity to get the pelvis position from.
@returns Vector Coordinates of the entity's pelvis.
--]]
function Clockwork.entity:GetPelvisPosition(entity)
local position = entity:GetPos();
local physBone = entity:LookupBone("ValveBiped.Bip01_Pelvis");
if (physBone) then
local bonePosition = entity:GetBonePosition(physBone);
if (bonePosition) then
position = bonePosition;
end;
end;
return position;
end;
--[[
@codebase Shared
@details A function to get whether an entity can see a position from its own position.
@param Entity The entity that's being checked on if it can see a position.
@param Vector Location being checked if the entity can see it.
@param Int Optional: Variance around the position being checked.
@param Entity Optional: Entities that should be ignored when checking if an entity can see a position.
@returns Bool Whether or not the entity can see a position.
--]]
function Clockwork.entity:CanSeePosition(entity, position, iAllowance, tIgnoreEnts)
local trace = {};
trace.mask = CONTENTS_SOLID + CONTENTS_MOVEABLE + CONTENTS_OPAQUE + CONTENTS_DEBRIS + CONTENTS_HITBOX + CONTENTS_MONSTER;
trace.start = entity:LocalToWorld(entity:OBBCenter());
trace.endpos = position;
trace.filter = {entity};
if (tIgnoreEnts) then
if (type(tIgnoreEnts) == "table") then
table.Add(trace.filter, tIgnoreEnts);
else
table.Add(trace.filter, ents.GetAll());
end;
end;
trace = util.TraceLine(trace);
if (trace.Fraction >= (iAllowance or 0.75)) then
return true;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity can see an NPC from its own position.
@param Entity The entity that's being checked on if it can see an NPC.
@param Entity The NPC being check if it can be seen.
@param Int Optional: Variance around the position being checked.
@param Entity Optional: Entities that should be ignored when checking if an entity can see an NPC.
@returns Bool Whether or not the entity can see an NPC.
--]]
function Clockwork.entity:CanSeeNPC(entity, target, iAllowance, tIgnoreEnts)
local trace = {};
trace.mask = CONTENTS_SOLID + CONTENTS_MOVEABLE + CONTENTS_OPAQUE + CONTENTS_DEBRIS + CONTENTS_HITBOX + CONTENTS_MONSTER;
trace.start = entity:LocalToWorld(entity:OBBCenter());
trace.endpos = target:GetShootPos();
trace.filter = {entity, target};
if (tIgnoreEnts) then
if (type(tIgnoreEnts) == "table") then
table.Add(trace.filter, tIgnoreEnts);
else
table.Add(trace.filter, ents.GetAll());
end;
end;
trace = util.TraceLine(trace);
if (trace.Fraction >= (iAllowance or 0.75)) then
return true;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity can see a player from its own position.
@param Entity The entity that's being checked on if it can see a player.
@param Entity The player being checked if it can be seen.
@param Int Optional: Variance around the position being checked.
@param Entity Optional: Entities that should be ignored when checking if an entity can see a player.
@returns Bool Whether or not the entity can see a player.
--]]
function Clockwork.entity:CanSeePlayer(entity, target, iAllowance, tIgnoreEnts)
if (target:GetEyeTraceNoCursor().Entity == entity) then
return true;
else
local trace = {};
trace.mask = CONTENTS_SOLID + CONTENTS_MOVEABLE + CONTENTS_OPAQUE + CONTENTS_DEBRIS + CONTENTS_HITBOX + CONTENTS_MONSTER;
trace.start = entity:LocalToWorld(entity:OBBCenter());
trace.endpos = target:GetShootPos();
trace.filter = {entity, target};
if (tIgnoreEnts) then
if (type(tIgnoreEnts) == "table") then
table.Add(trace.filter, tIgnoreEnts);
else
table.Add(trace.filter, ents.GetAll());
end;
end;
trace = util.TraceLine(trace);
if (trace.Fraction >= (iAllowance or 0.75)) then
return true;
end;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity can see an entity from its own position.
@param Entity The entity that's being checked on if it can see another entity.
@param Entity The entity being checked if it can be seen.
@param Int Optional: Variance around the entity being checked.
@param Entity Optional: Entities that should be ignored when checking if an entity can see another entity.
@returns Bool Whether or not the entity can see another entity.
--]]
function Clockwork.entity:CanSeeEntity(entity, target, iAllowance, tIgnoreEnts)
local trace = {};
trace.mask = CONTENTS_SOLID + CONTENTS_MOVEABLE + CONTENTS_OPAQUE + CONTENTS_DEBRIS + CONTENTS_HITBOX + CONTENTS_MONSTER;
trace.start = entity:LocalToWorld(entity:OBBCenter());
trace.endpos = target:LocalToWorld(target:OBBCenter());
trace.filter = {entity, target};
if (tIgnoreEnts) then
if (type(tIgnoreEnts) == "table") then
table.Add(trace.filter, tIgnoreEnts);
else
table.Add(trace.filter, ents.GetAll());
end;
end;
trace = util.TraceLine(trace);
if (trace.Fraction >= (iAllowance or 0.75)) then
return true;
end;
end;
--[[
@codebase Shared
@details A function to get whether a door is unownable by a player.
@param Entity Door being checked as unownable.
@returns Bool Whether or not the door is unownable.
--]]
function Clockwork.entity:IsDoorUnownable(entity)
return entity:GetNetworkedBool("Unownable");
end;
--[[
@codebase Shared
@details A function to get whether a door is false.
@param Entity Door being check as false.
@returns Bool Whether or not the door is false or not.
--]]
function Clockwork.entity:IsDoorFalse(entity)
return self:IsDoorUnownable(entity) and self:GetDoorName(entity) == "false";
end;
--[[
@codebase Shared
@details A function to get whether a door is hidden.
@param Entity Door being checked as hidden.
@returns Bool Whether or not the door is hidden or not.
--]]
function Clockwork.entity:IsDoorHidden(entity)
return self:IsDoorUnownable(entity) and self:GetDoorName(entity) == "hidden";
end;
--[[
@codebase Shared
@details A function to get a door's name.
@param Entity Door getting its name checked.
@returns String Name of the door.
--]]
function Clockwork.entity:GetDoorName(entity)
return entity:GetNetworkedString("Name");
end;
--[[
@codebase Shared
@details A function to get the text being displayed on a door.
@param Entity Door getting its text from.
@returns String Text being displayed on the door.
--]]
function Clockwork.entity:GetDoorText(entity)
return entity:GetNetworkedString("Text");
end;
--[[
@codebase Shared
@details A function to get whether an entity is a player in ragdoll form.
@param Entity The entity getting checked as a player ragdoll.
@returns Bool Whether or not the entity is a player ragdoll.
--]]
function Clockwork.entity:IsPlayerRagdoll(entity)
local player = entity:GetNetworkedEntity("Player");
if (IsValid(player)) then
if (player:GetRagdollEntity() == entity) then
return true;
end;
end;
end;
--[[
@codebase Shared
@details A function to get a player from an entity.
@param Entity The entity getting the player from.
@returns Entity The player from the entity.
--]]
function Clockwork.entity:GetPlayer(entity)
local player = entity:GetNetworkedEntity("Player");
if (IsValid(player)) then
return player;
elseif (entity:IsPlayer()) then
return entity;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity is interactable.
@param Entity The entity being checked if it is interactable.
@returns Bool Whether or not the entity is interactable.
--]]
function Clockwork.entity:IsInteractable(entity)
local class = entity:GetClass();
if (string.find(class, "prop_")) then
if (entity:HasSpawnFlags(SF_PHYSPROP_MOTIONDISABLED) or entity:HasSpawnFlags(SF_PHYSPROP_PREVENT_PICKUP)) then
return false;
end;
end;
if (entity:IsNPC() or entity:IsPlayer() or string.find(class, "prop_dynamic")) then
return false;
end;
if (class == "func_physbox" and entity:HasSpawnFlags(SF_PHYSBOX_MOTIONDISABLED)) then
return false;
end
if (class != "func_physbox" and string.find(class, "func_")) then
return false;
end;
if (self:IsDoor(entity)) then
return false;
end;
return true;
end;
--[[
@codebase Shared
@details A function to get whether an entity is a physics entity.
@param Entity The entity being checked if it is a physics entity.
@return Bool Whether or not the entity is a physicas entity.
--]]
function Clockwork.entity:IsPhysicsEntity(entity)
local class = string.lower(entity:GetClass());
if (class == "prop_physicsmultiplayer" or class == "prop_physics") then
return true;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity is a pod.
@param Entity The entity being checked if it is a pod entity.
@returns Bool Whether or not the entity is a pod entity.
--]]
function Clockwork.entity:IsPodEntity(entity)
local entityModel = string.lower(entity:GetModel());
if (string.find(entityModel, "prisoner")) then
return true;
end;
end;
--[[
@codebase Shared
@details A function to get whether an entity is a chair.
@param Entity The Entity being checked if it is a chair entity.
@returns Bool Whether or not the entity is a chair entity.
--]]
function Clockwork.entity:IsChairEntity(entity)
if (entity:GetModel()) then
local entityModel = string.lower(entity:GetModel());
if (string.find(entityModel, "chair") or string.find(entityModel, "seat")) then
return true;
end;
end;
end;
if (CLIENT) then
--[[
@codebase Client
@details A function to get whether an item's data has been fetched.
@param Entity The entity being checked if the item data has been fetched.
@returns Bool Whether or not the item data has been fetched.
--]]
function Clockwork.entity:HasFetchedItemData(entity)
return (entity.cwFetchedItemData == true);
end;
--[[
@codebase Client
@details A function to fetch the entity's item table.
@param Entity The entity getting the item table from.
@returns Table Contains the entity's item table.
--]]
function Clockwork.entity:FetchItemTable(entity)
return entity.cwItemTable;
end;
--[[
@codebase Client
@details A function to fetch the entity's item data.
@param Entity The entity getting the item data from.
--]]
function Clockwork.entity:FetchItemData(entity)
local curTime = CurTime();
if (!entity.m_iNextFetchItemData) then
entity.m_iNextFetchItemData = 0;
end;
if (curTime > entity.m_iNextFetchItemData) then
entity.m_iNextFetchItemData = curTime + 4;
if (entity:IsVehicle()) then
Clockwork.datastream:Start("FetchItemData", entity:EntIndex());
else
Clockwork.datastream:Start("FetchItemData", entity);
end;
end;
end;
Clockwork.datastream:Hook("FetchItemData", function(data)
if (type(data.entity) == "number") then
data.entity = ents.GetByIndex(data.entity);
end;
if (IsValid(data.entity)) then
data.entity.cwFetchedItemData = true;
data.entity.cwItemTable = Clockwork.item:CreateInstance(
data.definition.index, data.definition.itemID, data.definition.data
);
end;
end);
else
Clockwork.datastream:Hook("FetchItemData", function(player, data)
local entity = data;
if (type(data) == "number") then
entity = ents.GetByIndex(data);
end;
if (!IsValid(entity)) then return; end;
--[[
Find out what the entity's item table is
by trying a couple of common methods.
--]]
local itemTable = entity.cwItemTable;
if (entity.GetItemTable) then
itemTable = entity:GetItemTable();
end;
if (itemTable) then
local definition = Clockwork.item:GetDefinition(itemTable, true);
if (entity:IsVehicle()) then
data = entity:EntIndex();
end;
Clockwork.datastream:Start(player, "FetchItemData", {
definition = definition,
entity = data
});
end;
end);
--[[
@codebase Server
@details A function to dissolve an entity using a Source effect.
@param Entity The entity that will be dissolved.
@param String Dissolving effect to be applied to the entity.
@param Int Optional: Time until the entity is removed.
@param Entity Optional: The entity that is set as the dissolved entity's attacker.
@returns Entity Reference to the entity making the dissolving effects.
--]]
function Clockwork.entity:Dissolve(entity, dissolveType, iRemoveDelay, attacker)
local dissolver = ents.Create("env_entity_dissolver");
local oldName = entity:GetName();
if (!oldName or oldName == "") then
entity:SetName("dissolve_"..entity:EntIndex());
end;
dissolver:SetKeyValue("dissolvetype", dissolveType);
dissolver:SetKeyValue("magnitude", 0);
dissolver:SetPos(entity:GetPos());
if (IsValid(attacker)) then
dissolver:SetPhysicsAttacker(attacker);
end;
dissolver:Spawn();
--[[ Dissolve the entity now using Fire commands. --]]
dissolver:Fire("Dissolve", entity:GetName(), 0);
dissolver:Fire("Kill", "", 0.1);
dissolver:Remove();
--[[ Give the entity its old name back! --]]
entity:SetName(oldName);
if (iRemoveDelay) then
timer.Simple(iRemoveDelay, function()
if (IsValid(entity)) then
entity:Remove();
end;
end);
end;
return dissolver;
end;
--[[
@codebase Server
@details A function to temporarily set a door's speed to fast.
@param Entity The door getting its speed set to fast.
--]]
function Clockwork.entity:SetDoorSpeedFast(entity)
local curTime = CurTime();
local iSpeed = entity:GetSaveTable().speed;
if (Clockwork.entity:IsDoor(entity) and iSpeed
and (!entity.cwNextDoorSpeed or curTime >= entity.cwNextDoorSpeed)) then
entity:Fire("SetSpeed", tostring(iSpeed * 3), 0);
timer.Simple(1, function()
if (IsValid(entity)) then
entity:Fire("SetSpeed", tostring(iSpeed), 0);
end;
end);
entity.cwNextDoorSpeed = curTime + 2;
end;
end;
--[[
@codebase Server
@details A function to blast down a door off its hinges.
@param Entity The door being blasted off its hinges.
@param Int The blast strength that should be applied to the door.
@param Entity The entity that is blasting the door off its hinges.
--]]
function Clockwork.entity:BlastDownDoor(entity, force, attacker)
entity.cwIsBustedDown = true;
entity:SetNotSolid(true);
entity:DrawShadow(false);
entity:SetNoDraw(true);
entity:EmitSound("physics/wood/wood_box_impact_hard3.wav");
entity:Fire("Unlock", "", 0);
if (IsValid(entity.cwCombineLock)) then
entity.cwCombineLock:Explode();
entity.cwCombineLock:Remove();
end;
if (IsValid(entity.cwBreachEnt)) then
entity.cwBreachEnt:BreachEntity();
end;
local fakeDoor = ents.Create("prop_physics");
fakeDoor:SetCollisionGroup(COLLISION_GROUP_WORLD);
fakeDoor:SetAngles(entity:GetAngles());
fakeDoor:SetModel(entity:GetModel());
fakeDoor:SetSkin(entity:GetSkin());
fakeDoor:SetPos(entity:GetPos());
fakeDoor:Spawn();
Clockwork.entity:Decay(fakeDoor, 300);
Clockwork.kernel:CreateTimer("ResetDoor"..entity:EntIndex(), 300, 1, function()
if (IsValid(door)) then
entity.cwIsBustedDown = nil;
entity:SetNotSolid(false);
entity:DrawShadow(true);
entity:SetNoDraw(false);
end;
end);
local physicsObject = fakeDoor:GetPhysicsObject();
if (!IsValid(physicsObject)) then return; end;
if (IsValid(attacker)) then
local position = entity:GetPos() - attacker:GetPos();
position:Normalize();
force = position * 10000;
end;
if (force) then
physicsObject:ApplyForceCenter(force);
end;
end;
end;
--[[
@codebase Shared
@details A function to get an entity's door state.
@returns Table The state of the door.
--]]
function Clockwork.entity:GetDoorState(entity)
return entity:GetSaveTable().m_eDoorState or DOOR_STATE_CLOSED;
end;
--[[
@codebase Shared
@details A function to get whether a door is locked.
@returns Bool Whether or not the door is locked.
--]]
function Clockwork.entity:IsDoorLocked(entity)
return (entity:GetSaveTable().m_bLocked == true);
end;
if (SERVER) then
--[[
@codebase Server
@details A function to open a door.
@param Entity The door to be opened.
@param Int Delay until the door should be opened.
@param Bool Whether or not the door should be unlocked.
@param Bool Whether or not a sound should be played for unlocking the door.
@param Vector Postition that the info_target is created at.
@param Float Not implemented.
--]]
function Clockwork.entity:OpenDoor(entity, delay, bUnlock, bSound, origin, fSpeed)
if (self:IsDoor(entity)) then
if (bUnlock) then
entity:Fire("Unlock", "", delay);
delay = delay + 0.025;
if (bSound) then
entity:EmitSound("physics/wood/wood_box_impact_hard3.wav");
end;
end;
if (entity:GetClass() == "prop_dynamic") then
entity:Fire("SetAnimation", "open", delay);
entity:Fire("SetAnimation", "close", delay + 5);
elseif (origin and string.lower(entity:GetClass()) == "prop_door_rotating") then
local target = ents.Create("info_target");
target:SetName(tostring(target));
target:SetPos(origin);
target:Spawn();
entity:Fire("OpenAwayFrom", tostring(target), delay);
timer.Simple(delay + 1, function()
if (IsValid(target)) then
target:Remove();
end;
end);
else
entity:Fire("Open", "", delay);
end;
end;
end;
--[[
@codebase Server
@details A function to bash in a door entity.
@param Entity The door being bashed in.
@param Entity The entity bashing in the door.
--]]
function Clockwork.entity:BashInDoor(entity, eBasher)
local curTime = CurTime();
if (self:GetDoorState(entity) != DOOR_STATE_CLOSED) then
return;
end;
local oldCollisionGroup = entity:GetCollisionGroup();
Clockwork.entity:SetDoorSpeedFast(entity);
Clockwork.entity:OpenDoor(
entity, 0, nil, nil, eBasher:GetPos()
);
entity:EmitSound("physics/wood/wood_box_impact_hard3.wav");
entity:SetCollisionGroup(COLLISION_GROUP_WEAPON);
entity.cwNextBashDoor = curTime + 3;
Clockwork.entity:ReturnCollisionGroup(
entity, oldCollisionGroup
);
end;
--[[
@codebase Server
@details A function to make an entity safe.
@param Entity The entity being made safe.
@param Bool Whether or not the entity should be safe from physguns.
@param Table List of tools that can't be used on the entity.
@param Bool Whether or not the entity should be frozen.
--]]
function Clockwork.entity:MakeSafe(entity, bPhysgunProtect, tToolProtect, bFreezeEntity)
if (bPhysgunProtect) then
entity.PhysgunDisabled = true;
end;
if (tToolProtect) then
entity.CanTool = function(entity, player, trace, tool)
if (type(tToolProtect) == "table") then
return !table.HasValue(tToolProtect, tool);
else
return false;
end;
end;
end;
if (bFreezeEntity) then
if (IsValid(entity:GetPhysicsObject())) then
entity:GetPhysicsObject():EnableMotion(false);
end;
end;
end;
--[[
@codebase Server
@details A function to statue a ragdoll.
@param Entity The entity being set as a statue.
@param Int How much force a bone can take before the weld breaks.
--]]
function Clockwork.entity:StatueRagdoll(entity, forceLimit)
local bones = entity:GetPhysicsObjectCount()
if (!entity.cwStatueInfo) then
entity.cwStatueInfo = {
Welds = {}
};
end;
if (!forceLimit) then
forceLimit = 0;
end;
for bone = 1, bones do
local boneOne = bone - 1;
local boneTwo = bones - bone;
if (!entity.cwStatueInfo.Welds[boneTwo]) then
local constraintOne = constraint.Weld(entity, entity, boneOne, boneTwo, forceLimit);
if (constraintOne) then
entity.cwStatueInfo.Welds[boneOne] = constraintOne
end;
end;
local constraintTwo = constraint.Weld(entity, entity, boneOne, 0, forceLimit);
if (constraintTwo) then
entity.cwStatueInfo.Welds[boneOne + bones] = constraintTwo;
end;
local effectData = EffectData();
effectData:SetScale(1);
effectData:SetOrigin(entity:GetPhysicsObjectNum(boneOne):GetPos());
effectData:SetMagnitude(1);
util.Effect("GlassImpact", effectData, true, true);
end;
end;
--[[
@codebase Server
@details A function to drop items and cash.
@param Table The inventory being dropped.
@param Int How much cash to drop.
@param Vector Where the items and cash should drop.
@param Entity The owner of the items and cash being dropped.
--]]
function Clockwork.entity:DropItemsAndCash(inventory, cash, position, entity)
if (!Clockwork.inventory:IsEmpty(inventory)) then
for k, v in pairs(inventory) do
for k2, v2 in pairs(v) do
local itemEntity = self:CreateItem(nil, v2, position);
if (IsValid(itemEntity) and IsValid(entity)) then
self:CopyOwner(entity, itemEntity);
end;
end;
end;
end;
if (cash and cash > 0) then
self:CreateCash(nil, cash, position);
end;
end;
--[[
@codebase Server
@details A function to make an entity into a ragdoll.
@param Entity The entity being made in to a ragdoll.
@param Int The amount of force applied to the entity upon being made in to a ragdoll.
@param Int What the entity's velocity should be forcibly set to.
@param Angle What the entity's angles should be set to.
--]]
function Clockwork.entity:MakeIntoRagdoll(entity, force, overrideVelocity, overrideAngles)
local velocity = entity:GetVelocity() * 1.5;
local ragdoll = ents.Create("prop_ragdoll");
if (overrideVelocity) then
velocity = overrideVelocity;
end;
if (overrideAngles) then
ragdoll:SetAngles(overrideAngles);
else
ragdoll:SetAngles(entity:GetAngles());
end;
ragdoll:SetMaterial(entity:GetMaterial());
ragdoll:SetAngles(entity:GetAngles() - Angle(0, -45, 0));
ragdoll:SetColor(entity:GetColor());
ragdoll:SetModel(entity:GetModel());
ragdoll:SetSkin(entity:GetSkin());
ragdoll:SetPos(entity:GetPos());
ragdoll:Spawn();
if (IsValid(ragdoll)) then
local headIndex = ragdoll:LookupBone("ValveBiped.Bip01_Head1");
for i = 1, ragdoll:GetPhysicsObjectCount() do
local physicsObject = ragdoll:GetPhysicsObjectNum(i);
local boneIndex = ragdoll:TranslatePhysBoneToBone(i);
local position, angle = entity:GetBonePosition(boneIndex);
if (IsValid(physicsObject)) then
physicsObject:SetPos(position);
physicsObject:SetAngles(angle);
if (boneIndex == headIndex) then
physicsObject:SetVelocity(velocity * 2);
else
physicsObject:SetVelocity(velocity);
end;
if (force) then
if (boneIndex == headIndex) then
physicsObject:ApplyForceCenter(force * 2);
else
physicsObject:ApplyForceCenter(force);
end;
end;
end;
end;
end;
if (entity:IsOnFire()) then
ragdoll:Ignite(8, 0);
end;
return ragdoll;
end;
--[[
@codebase Server
@details A function to get whether a door is unsellable.
@param Entity The door being checked as unsellable.
@returns Bool Whether or not the door is unsellable.
--]]
function Clockwork.entity:IsDoorUnsellable(door)
return door.unsellable;
end;
--[[
@codebase Server
@details A function to set a door's parent.
@param Entity The door being set as a child.
@param Entity The door being set as the parent for the child.
--]]
function Clockwork.entity:SetDoorParent(door, parent)
if (self:IsDoor(door)) then
for k, v in pairs(self:GetDoorChildren(door)) do
if (IsValid(v)) then
self:SetDoorParent(v, false);
end;
end;
if (IsValid(door.doorParent)) then
if (door.doorParent.doorChildren) then
door.doorParent.doorChildren[door] = nil;
end;
end;
if (IsValid(parent) and self:IsDoor(parent)) then
if (parent.doorChildren) then
parent.doorChildren[door] = door;
else
parent.doorChildren = { [door] = door };
end;
door.doorParent = parent;
else
door.doorParent = nil;
end;