-
Notifications
You must be signed in to change notification settings - Fork 78
/
sv_player_ext.lua
1946 lines (1546 loc) · 50.5 KB
/
sv_player_ext.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
---
-- @class Player
-- serverside extensions to player table
local net = net
local table = table
local pairs = pairs
local IsValid = IsValid
local hook = hook
local playerGetAll = player.GetAll
local plymeta = FindMetaTable("Player")
if not plymeta then
ErrorNoHaltWithStack("FAILED TO FIND PLAYER TABLE")
return
end
local soundWeaponPickup = Sound("items/ammo_pickup.wav")
util.AddNetworkString("StartDrowning")
util.AddNetworkString("TTT2TargetPlayer")
util.AddNetworkString("TTT2SetPlayerReady")
util.AddNetworkString("TTT2NotifyPlayerReadyOnClients")
util.AddNetworkString("TTT2SetRevivalReason")
util.AddNetworkString("TTT2RevivalStopped")
util.AddNetworkString("TTT2RevivalUpdate_IsReviving")
util.AddNetworkString("TTT2RevivalUpdate_RevivalBlockMode")
util.AddNetworkString("TTT2RevivalUpdate_RevivalStartTime")
util.AddNetworkString("TTT2RevivalUpdate_RevivalDuration")
---
-- Sets whether a @{Player} is spectating the own ragdoll
-- @param boolean s
-- @realm server
function plymeta:SetRagdollSpec(s)
if s then
self.spec_ragdoll_start = CurTime()
end
self.spec_ragdoll = s
end
---
-- Returns whether a @{Player} is spectating the own ragdoll
-- @return boolean
-- @realm server
function plymeta:GetRagdollSpec()
return self.spec_ragdoll
end
---
-- Sets a @{Player} to be a forced spectator (not just a dead terrorist)
-- @param boolean state
-- @realm server
function plymeta:SetForceSpec(state)
self.force_spec = state -- compatibility with other addons
self:SetNWBool("force_spec", state)
end
-- Karma
---
-- The base/start karma is determined once per round and determines the @{Player}'s
-- damage penalty. It is networked and shown on clients.
-- @param number k
-- @realm server
function plymeta:SetBaseKarma(k)
self:SetNWFloat("karma", k)
end
---
-- @accessor number The live karma starts equal to the base karma, but is updated "live" as the
-- player damages/kills others. When another player damages/kills this one, the
-- live karma is used to determine his karma penalty.
-- @realm server
AccessorFunc(plymeta, "live_karma", "LiveKarma", FORCE_NUMBER)
---
-- @accessor number The damage factor scales how much damage the player deals, so if it is .9
-- then the player only deals 90% of his original damage.
-- @realm server
AccessorFunc(plymeta, "dmg_factor", "DamageFactor", FORCE_NUMBER)
---
-- @accessor boolean If a player does not damage team members in a round, he has a "clean" round
-- and gets a bonus for it.
-- @realm server
AccessorFunc(plymeta, "clean_round", "CleanRound", FORCE_BOOL)
---
-- Initializes the KARMA for the given @{Player}
-- @realm server
function plymeta:InitKarma()
KARMA.InitPlayer(self)
end
-- Equipment credits
---
-- Sets the amount of credits
-- @param number amt
-- @realm server
function plymeta:SetCredits(amt)
self.equipment_credits = amt
self:SendCredits()
end
---
-- Adds an amount of credits
-- @param number amt
-- @realm server
function plymeta:AddCredits(amt)
self:SetCredits(self:GetCredits() + amt)
end
---
-- Removes an amount of credits
-- @param number amt
-- @realm server
function plymeta:SubtractCredits(amt)
local tmp = self:GetCredits() - amt
if tmp < 0 then
tmp = 0
end
self:SetCredits(tmp)
end
---
-- Sets the default amount of credits
-- @realm server
function plymeta:SetDefaultCredits()
---
-- @realm server
if hook.Run("TTT2SetDefaultCredits", self) then
return
end
if not self:IsShopper() then
self:SetCredits(0)
return
end
local rd = self:GetSubRoleData()
local name = "ttt_" .. rd.abbr .. "_credits_starting"
if self:GetTeam() ~= TEAM_TRAITOR then
self:SetCredits(math.ceil(ConVarExists(name) and GetConVar(name):GetFloat() or 0))
return
end
local c = ConVarExists(name) and GetConVar(name):GetFloat() or 0
---
-- @realm server
self:SetCredits(math.ceil(hook.Run("TTT2ModifyDefaultTraitorCredits", self, c) or c))
end
---
-- Syncs the amount of credits with the @{Player}
-- @realm server
function plymeta:SendCredits()
net.Start("TTT_Credits")
net.WriteUInt(self:GetCredits(), 8)
net.Send(self)
end
-- Equipment items
---
-- Gives a specific @{ITEM} (if possible)
-- @param string className
-- @return ITEM|nil
-- @realm server
-- @internal
function plymeta:AddEquipmentItem(className)
local item = items.GetStored(className)
if not item or item.limited and self:HasEquipmentItem(className) then
return
end
self.equipmentItems = self.equipmentItems or {}
self.equipmentItems[#self.equipmentItems + 1] = className
item:Equip(self)
self:SendEquipment(EQUIPITEMS_ADD, className)
return item
end
---
-- Removes a specific @{ITEM}
-- @param string className
-- @realm server
function plymeta:RemoveEquipmentItem(className)
if not self:HasEquipmentItem(className) then
return
end
local item = items.GetStored(className)
if item and isfunction(item.Reset) then
item:Reset(self)
end
local equipItems = self:GetEquipmentItems()
for k = 1, #equipItems do
if equipItems[k] == className then
table.remove(self.equipmentItems, k)
break
end
end
self:SendEquipment(EQUIPITEMS_REMOVE, className)
end
---
-- Removes a specific @{Weapon}
-- @param string cls
-- @realm server
plymeta.RemoveEquipmentWeapon = plymeta.StripWeapon
---
-- Syncs the server stored equipment with the @{Player}
-- @note We do this instead of an NW var in order to limit the info to just this ply
-- @param number mode The mode to determine how the Equipment is handled on the client
-- @param string itemName The name of the item to send, can be 'nil' if mode is EQUIPITEMS_RESET
-- @realm server
function plymeta:SendEquipment(mode, itemName)
if not mode then
ErrorNoHaltWithStack(
"[TTT2] Define an EQUIPITEMS_mode for plymeta:SendEquipment(mode, itemName) to work.\n"
)
return
end
net.Start("TTT_Equipment")
net.WriteUInt(mode, 2)
if mode ~= EQUIPITEMS_RESET then
net.WriteString(itemName)
end
net.Send(self)
end
---
-- Resets the equipment of a @{Player}
-- @realm server
function plymeta:ResetEquipment()
local equipItems = self:GetEquipmentItems()
for i = 1, #equipItems do
local item = items.GetStored(equipItems[i])
if item and isfunction(item.Reset) then
item:Reset(self)
end
end
self.equipmentItems = {}
self:SendEquipment(EQUIPITEMS_RESET)
end
---
-- Sends the list of bought @{ITEM}s and @{Weapon}s to the @{Player}
-- @realm server
function plymeta:SendBought()
local bought = self.bought
-- Send all as string, even though equipment are numbers, for simplicity
net.Start("TTT_Bought")
net.WriteUInt(#bought, 8)
for i = 1, #bought do
net.WriteString(bought[i])
end
net.Send(self)
end
local function ttt_resend_bought(ply)
if not IsValid(ply) then
return
end
ply:SendBought()
end
concommand.Add("ttt_resend_bought", ttt_resend_bought)
---
-- Resets the bought list of a @{Player}
-- @realm server
function plymeta:ResetBought()
self.bought = {}
self:SendBought()
end
---
-- Adds an @{ITEM} or a @{Weapon} into the bought list of a @{Player}
-- @note This will disable another purchase of the same equipment
-- if this equipment is limited
-- @param string equipmentName
-- @realm server
-- @see Player:RemoveBought
function plymeta:AddBought(equipmentName)
self.bought = self.bought or {}
self.bought[#self.bought + 1] = tostring(equipmentName)
shop.SetEquipmentBought(self, equipmentName)
shop.SetEquipmentGlobalBought(equipmentName)
shop.SetEquipmentTeamBought(self, equipmentName)
self:SendBought()
end
---
-- Removes an @{ITEM} or a @{Weapon} from the bought list of a @{Player}
-- @note This will enable another purchase of the same equipment
-- if this equipment is limited
-- @param string equipmentName
-- @realm server
-- @see Player:AddBought
function plymeta:RemoveBought(equipmentName)
local key
self.bought = self.bought or {}
for k = 1, #self.bought do
if self.bought[k] ~= tostring(equipmentName) then
continue
end
key = k
break
end
if key then
table.remove(self.bought, key)
self:SendBought()
end
end
---
-- Strips player of all equipment
-- @realm server
function plymeta:StripAll()
-- standard stuff
self:StripAmmo()
self:StripWeapons()
-- our stuff
self:ResetEquipment()
self:SetCredits(0)
end
---
-- Sets all flags (force_spec, etc) to their default
-- @realm server
function plymeta:ResetStatus()
self:SetRole(ROLE_NONE) -- this will update the team automatically
self:SetRagdollSpec(false)
self:SetForceSpec(false)
self:ResetRoundFlags()
end
---
-- Sets round-based misc flags to default position. Called at PlayerSpawn.
-- @realm server
function plymeta:ResetRoundFlags()
self:ResetEquipment()
self:SetCredits(0)
self:ResetBought()
-- equipment stuff
self.bomb_wire = nil
self.radar_charge = 0
self.decoy = nil
timer.Remove("give_equipment" .. self:UniqueID())
-- corpse
self:TTT2NETSetBool("body_found", false)
self.kills = {}
self.dying_wep = nil
self.was_headshot = false
-- communication
self.mute_team = -1
local winTms = roles.GetWinTeams()
for i = 1, #winTms do
self[winTms[i] .. "_gvoice"] = false
end
self:SetNWBool("disguised", false)
-- karma
self:SetCleanRound(true)
self:Freeze(false)
-- armor
self:ResetArmor()
end
---
-- Give a @{Weapon} to a @{Player}.
-- @note If the initial attempt fails due to heisenbugs in
-- the map, keep trying until the @{Player} has moved to a better spot where it
-- does work.
-- @param string cls @{Weapon}'s class
-- @param function callback Will be called if weapon was given successfully,
-- takes the @{Player}, cls and created @{Weapon} as parameters, can be nil
-- @realm server
function plymeta:GiveEquipmentWeapon(cls, callback)
if not cls then
return
end
if not self:CanCarryWeapon(weapons.GetStored(cls)) then
return
end
-- Referring to players by SteamID64 because a player may disconnect while his
-- unique timer still runs, in which case we want to be able to stop it. For
-- that we need its name, and hence his SteamID64.
local tmr = "give_equipment" .. self:UniqueID()
if not IsValid(self) or not self:Alive() then
timer.Remove(tmr)
return
end
-- giving attempt, will fail if we're in a crazy spot in the map or perhaps
-- other glitchy cases
local w = self:Give(cls)
if not IsValid(w) or not self:HasWeapon(cls) then
if not timer.Exists(tmr) then
local slf = self
timer.Create(tmr, 1, 0, function()
if not IsValid(slf) then
return
end
slf:GiveEquipmentWeapon(cls, callback)
end)
end
-- we will be retrying
else
-- can stop retrying, if we were
timer.Remove(tmr)
if isfunction(callback) then
-- basically a delayed/asynchronous return, necessary due to the timers
callback(self, cls, w)
end
end
end
---
-- Gives an @{ITEM} to a @{Player} and returns whether it was successful
-- @param string cls
-- @return ITEM|nil
-- @realm server
function plymeta:GiveEquipmentItem(cls)
if not cls then
return
end
local item = items.GetStored(cls)
if not item or item.limited and self:HasEquipmentItem(cls) then
return
end
return self:AddEquipmentItem(cls)
end
---
-- Forced specs and latejoin specs should not get points
-- @return boolean
-- @realm server
function plymeta:ShouldScore()
if self:GetForceSpec() then
return false
elseif self:IsSpec() and self:Alive() then
return false
else
return true
end
end
---
-- Adds a kill into the kill list
-- @param Player victim
-- @realm server
function plymeta:RecordKill(victim)
if not IsValid(victim) then
return
end
self.kills = self.kills or {}
self.kills[#self.kills + 1] = victim:SteamID64()
end
---
-- This is doing nothing, it's just a function to avoid incompatibility
-- @note Please remove this call and use the TTTPlayerSpeedModifier hook in both CLIENT and SERVER states
-- @param any slowed
-- @deprecated
-- @realm server
function plymeta:SetSpeed(slowed)
ErrorNoHaltWithStack(
"Player:SetSpeed(slowed) is deprecated - please remove this call and use the TTTPlayerSpeedModifier hook in both CLIENT and SERVER states"
)
end
---
-- Resets the last words
-- @realm server
function plymeta:ResetLastWords()
--if not IsValid(self) then return end -- timers are dangerous things
self.last_words_id = nil
end
---
-- Sends the last words based on the DamageInfo
-- @param CTakeDamageInfo dmginfo
-- @realm server
function plymeta:SendLastWords(dmginfo)
-- Use a pseudo unique id to prevent people from abusing the concmd
self.last_words_id = math.floor(CurTime() + math.random(500))
-- See if the damage was interesting
local dtype = KILL_NORMAL
if dmginfo:GetAttacker() == self or dmginfo:GetInflictor() == self then
dtype = KILL_SUICIDE
elseif dmginfo:IsDamageType(DMG_BURN) then
dtype = KILL_BURN
elseif dmginfo:IsFallDamage() then
dtype = KILL_FALL
end
self.death_type = dtype
net.Start("TTT_InterruptChat")
net.WriteUInt(self.last_words_id, 32)
net.Send(self)
-- any longer than this and you're out of luck
local ply = self
timer.Simple(2, function()
if not IsValid(ply) then
return
end
ply:ResetLastWords()
end)
end
---
-- Resets the view
-- @realm server
function plymeta:ResetViewRoll()
local ang = self:EyeAngles()
if ang.r == 0 then
return
end
ang.r = 0
self:SetEyeAngles(ang)
end
---
-- Checks whether a @{Player} is able to spawn
-- @return boolean
-- @realm server
function plymeta:ShouldSpawn()
-- do not spawn players who have not been through initspawn
if not self:IsSpec() and not self:IsTerror() then
return false
end
-- do not spawn forced specs
if self:IsSpec() and self:GetForceSpec() then
return false
end
return true
end
---
-- Preps a player for a new round, spawning them if they should.
-- @param boolean deadOnly If deadOnly is true, only spawns if player is dead, else just makes sure he is healed
-- @return boolean Returns true if player is spawned
-- @realm server
function plymeta:SpawnForRound(deadOnly)
---
-- @realm server
hook.Run("PlayerSetModel", self)
---
-- @realm server
hook.Run("TTTPlayerSetColor", self)
-- wrong alive status and not a willing spec who unforced after prep started
-- (and will therefore be "alive")
if deadOnly and self:Alive() and not self:IsSpec() then
-- if the player does not need respawn, make sure he has full health
self:SetHealth(self:GetMaxHealth())
return false
end
if not self:ShouldSpawn() then
return false
end
-- reset propspec state that they may have gotten during prep
PROPSPEC.Clear(self)
-- respawn anyone else
if self:Team() == TEAM_SPEC then
self:UnSpectate()
end
self:StripAll()
self:SetTeam(TEAM_TERROR)
self:Spawn()
-- set spawn position
local spawnPoint = plyspawn.GetRandomSafePlayerSpawnPoint(self)
if not spawnPoint then
return false
end
self:SetPos(spawnPoint.pos)
self:SetAngles(spawnPoint.ang)
-- tell caller that we spawned
return true
end
---
-- This is called on the first spawn to set the default vars
-- @realm server
function plymeta:InitialSpawn()
self.has_spawned = false
-- The team the player spawns on depends on the round state
self:SetTeam(gameloop.GetRoundState() == ROUND_PREP and TEAM_TERROR or TEAM_SPEC)
-- Change some gmod defaults
self:SetCanZoom(false)
self:SetJumpPower(160)
self:SetCrouchedWalkSpeed(0.3)
self:SetRunSpeed(220)
self:SetWalkSpeed(220)
self:SetMaxSpeed(220)
self:ResetStatus()
-- Start off with clean, full karma (unless it can and should be loaded)
self:InitKarma()
-- We never have weapons here, but this inits our equipment state
self:StripAll()
-- Initialize role weights
roleselection.InitializeRoleWeights(self)
-- set spawn position
local spawnPoint = plyspawn.GetRandomSafePlayerSpawnPoint(self)
if not spawnPoint then
return
end
self:SetPos(spawnPoint.pos)
self:SetAngles(spawnPoint.ang)
end
---
-- Kicks and bans a player
-- @param number length time of the ban
-- @param string reason
-- @realm server
function plymeta:KickBan(length, reason)
-- see admin.lua
PerformKickBan(self, length, reason)
end
local oldSpectate = plymeta.Spectate
---
-- Forces a @{Player} to the spectation modus
-- @param number type
-- @realm server
function plymeta:Spectate(type)
oldSpectate(self, type)
-- NPCs should never see spectators. A workaround for the fact that gmod NPCs
-- do not ignore them by default.
self:SetNoTarget(true)
if type == OBS_MODE_ROAMING then
self:SetMoveType(MOVETYPE_NOCLIP)
end
end
local oldSpectateEntity = plymeta.SpectateEntity
---
-- Forces a @{Player} to spectate an @{Entity}
-- @param Entity ent
-- @realm server
function plymeta:SpectateEntity(ent)
oldSpectateEntity(self, ent)
if IsValid(ent) and ent:IsPlayer() then
self:SetupHands(ent)
end
end
local oldUnSpectate = plymeta.UnSpectate
---
-- Unspectates a @{Player}
-- @realm server
function plymeta:UnSpectate()
oldUnSpectate(self)
self:SetNoTarget(false)
end
---
-- @accessor table A table containing the weights to use when selecting roles, if enabled.
-- @realm server
AccessorFunc(plymeta, "role_weights", "RoleWeightTable")
---
-- Returns whether a @{Player} is able to select a specific @{ROLE}
-- @param ROLE roleData
-- @param number choice_count
-- @param number role_count
-- @return boolean
-- @realm server
function plymeta:CanSelectRole(roleData, choice_count, role_count)
-- if there aren't enough players anymore to have a greater role variety
if choice_count <= role_count then
return true
end
-- or the player has enough karma
local minKarmaCVar = GetConVar("ttt_" .. roleData.name .. "_karma_min")
local minKarma = minKarmaCVar and minKarmaCVar:GetInt() or 0
if KARMA.cv.enabled:GetBool() and self:GetBaseKarma() > minKarma then
return true
end
-- or if the randomness decides
if math.random(3) == 2 then
return true
end
return false
end
---
-- Tries to find the corpse of a player. Returns nil if none was found.
-- @return Entity Returns the ragdoll entity
-- @realm server
function plymeta:FindCorpse()
local ragdolls = ents.FindByClass("prop_ragdoll")
for i = 1, #ragdolls do
local rag = ragdolls[i]
if
not rag:IsPlayerRagdoll()
or not CORPSE.IsRealPlayerCorpse(rag)
or CORPSE.GetPlayerSID64(rag) ~= self:SteamID64()
then
continue
end
return rag
end
end
-- Handles all stuff needed if the revival failed
local function OnReviveFailed(ply, failMessage)
if isfunction(ply.OnReviveFailedCallback) then
ply.OnReviveFailedCallback(ply, failMessage)
ply.OnReviveFailedCallback = nil
else
LANG.Msg(ply, failMessage, nil, MSG_MSTACK_WARN)
end
net.Start("TTT2RevivalStopped")
net.Send(ply)
end
---
-- Revives a @{Player}
-- @param[default=3] number delay The delay of the revive
-- @param[opt] function OnRevive The @{function} that should be run if the @{Player} revives
-- @param[opt] function DoCheck An additional checking @{function}
-- @param[default=false] boolean needsCorpse Whether the dead @{Player} @{CORPSE} is needed
-- @param[default=REVIVAL_BLOCK_NONE] number blockRound Stops the round from ending if this is set to someting other than 0
-- @param[opt] function OnFail This @{function} is called if the revive fails
-- @param[opt] Vector spawnPos The position where the player should be spawned, accounts for minor obstacles
-- @param[opt] Angle spawnEyeAngle The eye angles of the revived players
-- @realm server
function plymeta:Revive(
delay,
OnRevive,
DoCheck,
needsCorpse,
blockRound,
OnFail,
spawnPos,
spawnEyeAngle
)
if self:IsReviving() then
return
end
local name = "TTT2RevivePlayer" .. self:EntIndex()
delay = delay or 3
-- compatible mode for block round
if isbool(blockRound) then
ErrorNoHaltWithStack("[DEPRECATION WARNING]: You should use the REVIVAL_BLOCK enum here.")
end
if blockRound == nil or blockRound == false then
blockRound = REVIVAL_BLOCK_NONE
elseif blockRound == true then
blockRound = REVIVAL_BLOCK_AS_ALIVE
end
self:SetReviving(true)
self:SetRevivalBlockMode(blockRound)
self:SetRevivalStartTime(CurTime())
self:SetRevivalDuration(delay)
self.OnReviveFailedCallback = OnFail
timer.Create(name, delay, 1, function()
if not IsValid(self) then
return
end
self:SetReviving(false)
self:SetRevivalBlockMode(REVIVAL_BLOCK_NONE)
self:SendRevivalReason(nil)
if not isfunction(DoCheck) or DoCheck(self) then
local corpse = self:FindCorpse()
if needsCorpse and (not IsValid(corpse) or corpse:IsOnFire()) then
OnReviveFailed(self, "message_revival_failed_missing_body")
return
end
self:SetMaxHealth(100)
self:SetHealth(100)
self:SpawnForRound(true)
if not spawnPos and IsValid(corpse) then
spawnPos = corpse:GetPos()
spawnEyeAngle = Angle(0, corpse:GetAngles().y, 0)
end
spawnPos = spawnPos or self:GetDeathPosition()
spawnPos = plyspawn.MakeSpawnPointSafe(self, spawnPos)
if not spawnPos then
local spawnPoint = plyspawn.GetRandomSafePlayerSpawnPoint(self)
if not spawnPoint then
OnReviveFailed(self, "message_revival_failed")
return
end
spawnPos = spawnPoint.pos
spawnEyeAngle = spawnPoint.ang
end
self:SetPos(spawnPos)
self:SetEyeAngles(spawnEyeAngle or Angle(0, 0, 0))
---
-- @realm server
hook.Run("PlayerLoadout", self, true)
self:SetCredits(CORPSE.GetCredits(corpse, 0))
self:SelectWeapon("weapon_zm_improvised")
if IsValid(corpse) then
corpse:Remove()
end
DamageLog("TTT2Revive: " .. self:Nick() .. " has been respawned.")
if isfunction(OnRevive) then
OnRevive(self)
end
else
OnReviveFailed(self, "message_revival_failed")
end
self.OnReviveFailedCallback = nil
end)
end
---
-- Cancel the ongoing revival process.
-- @param[default="message_revival_canceled"] string failMessage The fail message that should be displayed for the client
-- @param[opt] boolean silent If silent is true, no sound and text will be displayed
-- @realm server
function plymeta:CancelRevival(failMessage, silent)
if not self:IsReviving() then
return
end
self:SetReviving(false)
self:SetRevivalBlockMode(REVIVAL_BLOCK_NONE)
self:SendRevivalReason(nil)
timer.Remove("TTT2RevivePlayer" .. self:EntIndex())
if silent then
return
end
OnReviveFailed(self, failMessage or "message_revival_canceled")
end
---
-- Sets the revival state.
-- @param[default=false] boolean isReviving The reviving state
-- @internal
-- @realm server
function plymeta:SetReviving(isReviving)
isReviving = isReviving or false
if self.isReviving == isReviving then
return
end
self.isReviving = isReviving
net.Start("TTT2RevivalUpdate_IsReviving")
net.WritePlayer(self)
net.WriteBool(self.isReviving)
net.Broadcast()
end
---
-- Sets the blocking revival state.
-- @param[default=REVIVAL_BLOCK_NONE] number revivalBlockMode The blocking revival state
-- @internal
-- @realm server
function plymeta:SetRevivalBlockMode(revivalBlockMode)
revivalBlockMode = revivalBlockMode or REVIVAL_BLOCK_NONE