-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectate.lua
1115 lines (947 loc) · 36.2 KB
/
spectate.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
local camera_x_position = 5
local camera_y_position = 300
local camera_width = 500
local camera_height = 300
local fullscreen_width, fullscreen_height = draw.GetScreenSize()
-- Constants
local MAX_KILLFEED_ENTRIES = 8
-- Camera control variables
local camera_position = Vector3(0, 0, 0)
local camera_angles = EulerAngles(0, 0, 0)
local own_view_angles = EulerAngles(0, 0, 0)
local camera_speed = 10
local target_player = nil
local current_enemy_index = 1
local visited_players = {}
local first_person_mode = false
local fullscreen_mode = false
local free_camera = false
local last_key_press = 0
local key_delay = 0.2
local MOUSE_SENSITIVITY = 0.06
local last_killer = nil
local persistent_fullscreen = false
local death_time = 0
local current_wave_start = 0
local has_spawned_once = false
local is_in_game = false
local stored_class = nil
local spectate_locked = false
local current_all_player_index = 1
local friendly_player_index = 1
-- Material variables
local materials_initialized = false
local windowed_texture = nil
local windowed_material = nil
local fullscreen_texture = nil
local fullscreen_material = nil
local invisibleMaterial = nil
local class_icon_materials = {}
-- Killfeed variables
local killfeed_deaths = {}
-- HUD fonts
local title_font = draw.CreateFont("Tahoma", 12, 800, FONTFLAG_OUTLINE)
local hud_font = draw.CreateFont("TF2 BUILD", 30, 800, FONTFLAG_OUTLINE)
local killfeed_font = draw.CreateFont("TF2 BUILD", 24, 800, FONTFLAG_OUTLINE)
-- Constants
local DEATH_TIME = 2.0
local TRAVEL_TIME = 0.4
local FREEZE_TIME = 4.0
local DEFAULT_WAVE_TIME = 10.0
local BASE_DELAY = TRAVEL_TIME + FREEZE_TIME
local TOTAL_BASE_DELAY = DEATH_TIME + BASE_DELAY
-- Static variables
local lockedWaveTime = nil
local lastDeathTime = nil
local lastDebugTime = 0
--[[
There is a static base respawn time, which is based on a static death time of 2 seconds + the time for the freeze frame (0.4 travel time + 4.0 freeze time).
On top of this base, the non-scaled respawn wave time is added, which is 10 seconds by default, and can be changed per team by an input which happens
upon capturing a control point, which adds or subtracts from a team's base respawn wave time. Then, the game checks the time for the next respawn wave,
and compares it to the time for the base respawn time. If the base respawn time occurs after the next respawn wave, the scaled respawn wave time is added
on top of the next respawn wave to get the wave after the next, and so on until a respawn wave is found that occurs after the base respawn time.
The scaled respawn wave time is the non-scaled respawn wave time, except with the following extra logic: if the respawn wave time is above 5 seconds,
then scale it by a number between 0.25 and 1.0, linearly scaled by the number of players (1 to 8). Then this value is capped to a maximum of 5.
This logic happens during any PvP game, tournament mode or not. There are a few exceptions outside of normal PvP play, like in between rounds during Competitive Mode,
you respawn with your static base respawn time, and in pre-game for tournament mode, there are no respawn times.
]]--
local function ScaleWaveTime(waveTime, playerCount)
if waveTime <= 5.0 then
return waveTime
end
local scale = 0.25 + (math.min(playerCount, 8) - 1) * (0.75 / 7)
return math.min(waveTime * scale, 5.0)
end
local function GetRespawnTime()
local currentTime = globals.CurTime()
if not lastDeathTime or currentTime - lastDeathTime > 15.0 then
lockedWaveTime = nil
lastDeathTime = currentTime
lastDebugTime = 0
end
local baseRespawnTime = lastDeathTime + TOTAL_BASE_DELAY
local resources = entities.GetPlayerResources()
if not resources then return 0 end
local waveTable = resources:GetPropDataTableFloat("m_flNextRespawnTime")
if not waveTable then return 0 end
if lockedWaveTime and lockedWaveTime > currentTime then
if currentTime - lastDebugTime >= 1.0 then
print(string.format("Time to respawn: %.1f", lockedWaveTime - currentTime))
lastDebugTime = currentTime
end
return lockedWaveTime - currentTime
end
local futureWaves = {}
local seenWaves = {}
for _, waveTime in pairs(waveTable) do
local roundedTime = math.floor(waveTime * 100) / 100
if waveTime > currentTime and waveTime ~= 0 and not seenWaves[roundedTime] then
table.insert(futureWaves, waveTime)
seenWaves[roundedTime] = true
end
end
table.sort(futureWaves)
print("Current time:", currentTime)
print("Base respawn time:", baseRespawnTime)
print("Available waves:")
for i, wave in ipairs(futureWaves) do
print(string.format("Wave %d: %.2f (in %.2f seconds)",
i, wave, wave - currentTime))
end
if #futureWaves == 0 then
return TOTAL_BASE_DELAY
end
local nextWave = futureWaves[1]
if baseRespawnTime > nextWave then
-- Get scaled wave time
local playerCount = 0
for i = 1, 32 do
if resources:GetPropInt("m_bConnected", i) == 1 then
playerCount = playerCount + 1
end
end
local fullWaveTime = DEFAULT_WAVE_TIME
if fullWaveTime > 5.0 then
local scale = 0.25 + (math.min(playerCount, 8) - 1) * (0.75 / 7)
fullWaveTime = math.min(fullWaveTime * scale, 5.0)
end
-- Calculate where we should be after adding the wave time
local targetTime = nextWave + fullWaveTime
-- Find the next wave after this point
local targetWave = nil
for _, wave in ipairs(futureWaves) do
if wave > targetTime then
targetWave = wave
break
end
end
-- If we didn't find a suitable wave, add another wave time
if not targetWave then
targetWave = futureWaves[#futureWaves] + fullWaveTime
end
lockedWaveTime = targetWave
else
-- Base respawn is before next wave, use next available wave
lockedWaveTime = nextWave
end
print(string.format("Selected wave: %.2f (in %.2f seconds)",
lockedWaveTime, lockedWaveTime - currentTime))
lastDebugTime = currentTime
return lockedWaveTime - currentTime
end
-- Cleanup state
local function CleanupState()
killfeed_deaths = {}
camera_position = Vector3(0, 0, 0)
camera_angles = EulerAngles(0, 0, 0)
own_view_angles = EulerAngles(0, 0, 0)
target_player = nil
current_enemy_index = 1
visited_players = {}
first_person_mode = false
free_camera = false
fullscreen_mode = persistent_fullscreen
last_killer = nil
death_time = 0
current_wave_start = 0
end
-- Clean up function for materials and textures
local function CleanupMaterials()
if windowed_texture then
windowed_texture = nil
end
if fullscreen_texture then
fullscreen_texture = nil
end
windowed_material = nil
fullscreen_material = nil
invisibleMaterial = nil
class_icon_materials = {}
materials_initialized = false
end
local function InitializeAllMaterials()
-- Clean up any existing materials first
CleanupMaterials()
-- Create windowed mode materials
local windowed_texture_name = "camTexture_windowed"
windowed_texture = materials.CreateTextureRenderTarget(windowed_texture_name, camera_width, camera_height)
if not windowed_texture then
print("Failed to create windowed texture")
return false
end
windowed_material = materials.Create("camMaterial_windowed", string.format([[
UnlitGeneric
{
$basetexture "%s"
$ignorez 1
$nofog 1
}
]], windowed_texture_name))
-- Create fullscreen mode materials
local fullscreen_texture_name = "camTexture_fullscreen"
fullscreen_texture = materials.CreateTextureRenderTarget(fullscreen_texture_name, fullscreen_width, fullscreen_height)
if not fullscreen_texture then
print("Failed to create fullscreen texture")
return false
end
fullscreen_material = materials.Create("camMaterial_fullscreen", string.format([[
UnlitGeneric
{
$basetexture "%s"
$ignorez 1
$nofog 1
}
]], fullscreen_texture_name))
invisibleMaterial = materials.Create("invisible_material", [[
VertexLitGeneric
{
$basetexture "vgui/white"
$no_draw 1
}
]])
-- Initialize class icons
local class_icons = {
[1] = "hud/leaderboard_class_scout",
[2] = "hud/leaderboard_class_sniper",
[3] = "hud/leaderboard_class_soldier",
[4] = "hud/leaderboard_class_demo",
[5] = "hud/leaderboard_class_medic",
[6] = "hud/leaderboard_class_heavy",
[7] = "hud/leaderboard_class_pyro",
[8] = "hud/leaderboard_class_spy",
[9] = "hud/leaderboard_class_engineer"
}
for class_id, icon_path in pairs(class_icons) do
local material_name = string.format("class_icon_material_%d", class_id)
class_icon_materials[class_id] = materials.Create(material_name, string.format([[
UnlitGeneric
{
$basetexture "%s"
$translucent 1
$ignorez 1
$nofog 1
}
]], icon_path))
if not class_icon_materials[class_id] then
print(string.format("Failed to create material for class %d", class_id))
return false
end
end
materials_initialized = true
return true
end
local function draw_crosshair(x, y, r, g, b, a)
local size = 6
draw.Color(r, g, b, a)
draw.Line(x, y-size/2 - 10, x, y+size/2 - 10)
draw.Line(x-size/2 - 10, y, x+size/2 - 10, y)
draw.Line(x+size/2 + 10, y, x-size/2 + 10, y)
draw.Line(x, y+size/2 + 10, x, y-size/2 + 10)
end
local function IsAttachedToTargetPlayer(entity)
if not target_player or not entity then return false end
local moveChild = target_player:GetMoveChild()
while moveChild do
if moveChild == entity then return true end
moveChild = moveChild:GetMovePeer()
end
return false
end
local function GetEnemyPlayers()
local enemy_players = {}
local local_player = entities.GetLocalPlayer()
if not local_player then return enemy_players end
local players = entities.FindByClass("CTFPlayer")
for _, player in pairs(players) do
if player and player:IsValid() and player:IsAlive() and
not player:IsDormant() and
player:GetTeamNumber() ~= local_player:GetTeamNumber() then
table.insert(enemy_players, player)
end
end
return enemy_players
end
local function CycleNextEnemy()
local enemies = GetEnemyPlayers()
if #enemies == 0 then
target_player = nil
first_person_mode = false
free_camera = false
visited_players = {}
return
end
local available_enemies = {}
for _, player in ipairs(enemies) do
local already_visited = false
for _, visited in ipairs(visited_players) do
if visited == player then
already_visited = true
break
end
end
if not already_visited then
table.insert(available_enemies, player)
end
end
if #available_enemies == 0 then
visited_players = {}
available_enemies = enemies
end
for _, player in ipairs(available_enemies) do
if player and player:IsValid() and player:IsAlive() and not player:IsDormant() then
target_player = player
table.insert(visited_players, player)
break
end
end
if target_player then
print("Now spectating: " .. target_player:GetName() .. " (" .. #visited_players .. "/" .. #enemies .. " visited)")
end
end
-- Function to get all alive players
local function GetAllPlayers()
local all_players = {}
local players = entities.FindByClass("CTFPlayer")
for _, player in pairs(players) do
if player and player:IsValid() and player:IsAlive() and not player:IsDormant() then
table.insert(all_players, player)
end
end
return all_players
end
-- Function to get friendly players
local function GetFriendlyPlayers()
local friendly_players = {}
local local_player = entities.GetLocalPlayer()
if not local_player then return friendly_players end
local players = entities.FindByClass("CTFPlayer")
for _, player in pairs(players) do
if player and player:IsValid() and player:IsAlive() and
not player:IsDormant() and
player:GetTeamNumber() == local_player:GetTeamNumber() then
table.insert(friendly_players, player)
end
end
return friendly_players
end
-- Function to cycle through all players
local function CycleAllPlayers(forward)
local all_players = GetAllPlayers()
if #all_players == 0 then
target_player = nil
first_person_mode = false
free_camera = false
return
end
if forward then
current_all_player_index = current_all_player_index + 1
if current_all_player_index > #all_players then
current_all_player_index = 1
end
else
current_all_player_index = current_all_player_index - 1
if current_all_player_index < 1 then
current_all_player_index = #all_players
end
end
target_player = all_players[current_all_player_index]
if target_player then
print("Now spectating: " .. target_player:GetName())
end
end
-- Function to cycle through friendly players
local function CycleFriendlyPlayers()
local friendly_players = GetFriendlyPlayers()
if #friendly_players == 0 then
target_player = nil
first_person_mode = false
free_camera = false
return
end
friendly_player_index = friendly_player_index + 1
if friendly_player_index > #friendly_players then
friendly_player_index = 1
end
target_player = friendly_players[friendly_player_index]
if target_player then
print("Now spectating friendly: " .. target_player:GetName())
end
end
-- Function to store current class
local function StoreCurrentClass()
local local_player = entities.GetLocalPlayer()
if local_player then
stored_class = local_player:GetPropInt("m_iClass")
end
end
-- Function to toggle spectate lock
local function ToggleSpectateLock()
if not spectate_locked then
StoreCurrentClass()
client.Command("menuopen", true)
spectate_locked = true
else
if stored_class then
client.Command("join_class " .. stored_class, true)
end
spectate_locked = false
end
end
local function HandleMovement()
local forward = Vector3(0, 0, 0)
local right = Vector3(0, 0, 0)
local up = Vector3(0, 0, 0)
if input.IsButtonDown(KEY_W) then
forward = forward + camera_angles:Forward() * camera_speed
end
if input.IsButtonDown(KEY_S) then
forward = forward - camera_angles:Forward() * camera_speed
end
if input.IsButtonDown(KEY_D) then
right = right + camera_angles:Right() * camera_speed
end
if input.IsButtonDown(KEY_A) then
right = right - camera_angles:Right() * camera_speed
end
if input.IsButtonDown(KEY_Q) then
up.z = up.z + camera_speed
end
if input.IsButtonDown(KEY_E) then
up.z = up.z - camera_speed
end
return forward + right + up
end
local function SafeGetTextSize(text)
if not text or text == "" then
return 0, 0
end
return draw.GetTextSize(text)
end
local function HandleKillfeedEvent(event)
if event:GetName() == "player_death" then
local victim = entities.GetByUserID(event:GetInt("userid"))
local attacker_id = event:GetInt("attacker")
local attacker = nil
if attacker_id and attacker_id > 0 then
attacker = entities.GetByUserID(attacker_id)
end
local assister = nil
local assister_id = event:GetInt("assister")
if assister_id and assister_id > 0 then
assister = entities.GetByUserID(assister_id)
end
local local_player = entities.GetLocalPlayer()
if victim and local_player and victim:GetIndex() == local_player:GetIndex() and attacker then
last_killer = attacker
end
if not victim then return end
local current_tick = globals.TickCount()
local hud_deathnotice_time = client.GetConVar("hud_deathnotice_time")
killfeed_deaths[#killfeed_deaths+1] = {
victim = victim,
attacker = attacker,
assister = assister,
tick_to_disappear = current_tick + (hud_deathnotice_time * 66 * 2)
}
while #killfeed_deaths > MAX_KILLFEED_ENTRIES do
table.remove(killfeed_deaths, 1)
end
elseif event:GetName() == "game_newmap" then
-- Reset state on map change
has_spawned_once = false
is_in_game = false
CleanupState()
elseif event:GetName() == "teamplay_round_start" then
is_in_game = true
elseif event:GetName() == "teamplay_game_over" or
event:GetName() == "tf_game_over" then
is_in_game = false
elseif event:GetName() == "team_control_point_captured" then
-- Reset our target wave when a point is captured
targetWaveTime = nil
current_wave_start = globals.CurTime()
end
end
local function DrawKillfeed()
if not fullscreen_mode then return end
local current_tick = globals.TickCount()
for i = #killfeed_deaths, 1, -1 do
if killfeed_deaths[i].tick_to_disappear <= current_tick then
table.remove(killfeed_deaths, i)
end
end
local lastHeight = 5
local local_player = entities.GetLocalPlayer()
local team_colors = {
[2] = {255, 64, 64, 255},
[3] = {153, 204, 255, 255}
}
local function GetColoredPlayerText(player)
if not player or not player:IsValid() then
return {text = "Unknown", color = {255, 255, 255, 255}}
end
local name = player:GetName()
if not name or name == "" then
return {text = "Unknown", color = {255, 255, 255, 255}}
end
if (local_player and player:GetIndex() == local_player:GetIndex()) or
(target_player and player:GetIndex() == target_player:GetIndex()) then
return {text = name, color = {255, 255, 255, 255}}
else
local team_color = team_colors[player:GetTeamNumber()] or {255, 255, 255, 255}
return {text = name, color = team_color}
end
end
for pos, death in ipairs(killfeed_deaths) do
if not death.victim then goto continue end
local victim_info = GetColoredPlayerText(death.victim)
local died_alone = death.attacker == death.victim
local map_death = not death.attacker or not death.attacker:IsValid()
draw.SetFont(killfeed_font)
local full_text
local components = {}
if map_death or died_alone then
full_text = string.format("%s died a horrible death :(", victim_info.text)
components = {{text = full_text, color = victim_info.color}}
else
local attacker_info = GetColoredPlayerText(death.attacker)
components = {
{text = attacker_info.text, color = attacker_info.color}
}
if death.assister and death.assister:IsValid() and death.assister:GetName() then
local assister_info = GetColoredPlayerText(death.assister)
table.insert(components, {text = " + ", color = {255, 255, 255, 255}})
table.insert(components, {text = assister_info.text, color = assister_info.color})
end
table.insert(components, {text = " → ", color = {255, 255, 255, 255}})
table.insert(components, {text = victim_info.text, color = victim_info.color})
full_text = ""
for _, component in ipairs(components) do
full_text = full_text .. component.text
end
end
local textwidth, textheight = SafeGetTextSize(full_text)
if textwidth == 0 or textheight == 0 then goto continue end
local x1 = fullscreen_width - textwidth - 30
local y = lastHeight + textheight
local current_x = x1
for _, component in ipairs(components) do
draw.Color(component.color[1], component.color[2], component.color[3], component.color[4])
draw.TextShadow(current_x, y, component.text)
current_x = current_x + SafeGetTextSize(component.text)
end
lastHeight = lastHeight + textheight + 10
::continue::
end
end
-- Make sure to set death_time when player dies
callbacks.Register("FireGameEvent", function(event)
if event:GetName() == "player_death" then
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return end
local victim = entities.GetByUserID(event:GetInt("userid"))
if victim and localPlayer:GetIndex() == victim:GetIndex() then
death_time = globals.CurTime()
current_wave_start = 0 -- Reset current wave
end
end
end)
-- Track when points are captured to reset wave timing
local function OnGameEvent(event)
if event:GetName() == "team_control_point_captured" then
-- Reset our target wave when a point is captured
targetWaveTime = nil
current_wave_start = globals.CurTime()
end
end
callbacks.Register("FireGameEvent", "point_capture_hook", OnGameEvent)
-- Update DrawSpectatorHUD
local function DrawSpectatorHUD()
if not fullscreen_mode then return end
-- Draw respawn timer or infinite spectate text
draw.SetFont(hud_font)
draw.Color(255, 255, 255, 255)
local topText
if spectate_locked then
topText = "Infinite Spectate"
else
local time = GetRespawnTime()
if time > 0 then
topText = string.format("Respawning in: %.1f", time)
end
end
if topText then
local textW, textH = draw.GetTextSize(topText)
-- Draw semi-transparent background
draw.Color(0, 0, 0, 150)
draw.FilledRectFade(
math.floor(fullscreen_width/2 - textW/2 - 20),
35,
math.floor(fullscreen_width/2 + textW/2 + 20),
45 + textH,
100,
50,
true
)
-- Draw text
draw.Color(255, 255, 255, 255)
draw.TextShadow(
math.floor(fullscreen_width/2 - textW/2),
40,
topText
)
end
if not target_player or free_camera then return end
local health = target_player:GetHealth()
if not health then return end
local maxHealth = target_player:GetMaxHealth()
if not maxHealth then return end
local playerName = target_player:GetName()
if not playerName then return end
local healthColor = {
r = math.floor(255 * (1 - (health / maxHealth))),
g = math.floor(255 * (health / maxHealth)),
b = 0
}
local crosshairColor = {
r = 0,
g = 255,
b = 0
}
-- Get team colors
local team_colors = {
[2] = {r = 255, g = 64, b = 64}, -- RED
[3] = {r = 153, g = 204, b = 255} -- BLU
}
local team_color = team_colors[target_player:GetTeamNumber()] or {r = 255, g = 255, b = 255}
draw.SetFont(hud_font)
-- Draw name with team-colored background and class icon
local nameW, textH = draw.GetTextSize(playerName)
local nameBgPadding = 20
local iconSize = 32
local nameY = math.floor(fullscreen_height - 140)
local totalWidth = nameW + iconSize + 10 -- 10 pixels padding between icon and name
-- Draw semi-transparent team-colored background (extended for icon)
draw.Color(team_color.r, team_color.g, team_color.b, 100)
draw.FilledRectFade(
math.floor(fullscreen_width/2 - totalWidth/2 - nameBgPadding),
nameY - 5,
math.floor(fullscreen_width/2 + totalWidth/2 + nameBgPadding),
nameY + math.max(textH, iconSize) + 5,
100,
50,
true
)
-- Draw name text (shifted right to make room for icon)
draw.Color(255, 255, 255, 255)
draw.TextShadow(
math.floor(fullscreen_width/2 - totalWidth/2 + iconSize + 10),
nameY,
playerName
)
-- Draw class icon using our custom materials
local playerClass = target_player:GetPropInt("m_iClass")
local classIcon = class_icon_materials[playerClass]
if classIcon then
draw.Color(255, 255, 255, 255)
local iconX = math.floor(fullscreen_width/2 - totalWidth/2)
local iconY = math.floor(nameY + textH/2 - iconSize/2)
render.DrawScreenSpaceRectangle(
classIcon,
iconX,
iconY,
math.floor(iconSize),
math.floor(iconSize),
0, 0,
32, 32,
32, 32
)
end
-- Draw health with darker background
local healthText = string.format("%d HP", health)
local healthW, healthH = draw.GetTextSize(healthText)
local healthY = math.floor(fullscreen_height - 100)
-- Draw semi-transparent background for health
draw.Color(0, 0, 0, 150)
draw.FilledRectFade(
math.floor(fullscreen_width/2 - healthW/2 - nameBgPadding),
healthY - 5,
math.floor(fullscreen_width/2 + healthW/2 + nameBgPadding),
healthY + healthH + 5,
100,
50,
true
)
-- Draw health text
draw.Color(healthColor.r, healthColor.g, healthColor.b, 255)
draw.TextShadow(math.floor(fullscreen_width/2 - healthW/2), healthY, healthText)
if first_person_mode then
draw_crosshair(fullscreen_width/2, fullscreen_height/2, crosshairColor.r, crosshairColor.g, crosshairColor.b, 255)
end
end
local function HandleCameraControls()
local current_time = globals.RealTime()
-- Add Caps Lock check for cycling friendly players
if input.IsButtonPressed(KEY_CAPSLOCK) and current_time - last_key_press > key_delay then
CycleFriendlyPlayers()
last_key_press = current_time
free_camera = false
end
-- Add Mouse1 and Mouse2 checks for cycling all players
if input.IsButtonPressed(MOUSE_LEFT) and current_time - last_key_press > key_delay then
CycleAllPlayers(true)
last_key_press = current_time
free_camera = false
end
if input.IsButtonPressed(MOUSE_RIGHT) and current_time - last_key_press > key_delay then
CycleAllPlayers(false)
last_key_press = current_time
free_camera = false
end
-- Add Shift check for spectate lock
if input.IsButtonPressed(KEY_LSHIFT) and current_time - last_key_press > key_delay then
ToggleSpectateLock()
last_key_press = current_time
end
if input.IsButtonPressed(KEY_LCONTROL) and current_time - last_key_press > key_delay then
persistent_fullscreen = not persistent_fullscreen
fullscreen_mode = persistent_fullscreen
last_key_press = current_time
end
if input.IsButtonPressed(KEY_TAB) and current_time - last_key_press > key_delay then
if not target_player or not target_player:IsValid() or not target_player:IsAlive() or target_player:IsDormant() then
visited_players = {}
end
CycleNextEnemy()
last_key_press = current_time
free_camera = false
end
if input.IsButtonPressed(KEY_SPACE) and target_player and current_time - last_key_press > key_delay then
first_person_mode = not first_person_mode
free_camera = false
last_key_press = current_time
end
if not target_player then
camera_angles = own_view_angles
camera_position = camera_position + HandleMovement()
else
if not first_person_mode then
local moving = input.IsButtonDown(KEY_W) or input.IsButtonDown(KEY_A) or
input.IsButtonDown(KEY_S) or input.IsButtonDown(KEY_D) or
input.IsButtonDown(KEY_Q) or input.IsButtonDown(KEY_E)
if moving and not free_camera then
free_camera = true
own_view_angles = camera_angles
end
end
if first_person_mode then
free_camera = false
camera_position = target_player:GetAbsOrigin() + target_player:GetPropVector("localdata", "m_vecViewOffset[0]")
local pitch = target_player:GetPropFloat("tfnonlocaldata", "m_angEyeAngles[0]") or 0
local yaw = target_player:GetPropFloat("tfnonlocaldata", "m_angEyeAngles[1]") or 0
camera_angles = EulerAngles(pitch, yaw, 0)
local forward_offset = 16.5
local upward_offset = 12
local forward_vector = camera_angles:Forward()
camera_position = camera_position + forward_vector * forward_offset
camera_position = camera_position + Vector3(0, 0, upward_offset)
else
camera_angles = own_view_angles
if free_camera then
camera_position = camera_position + HandleMovement()
else
camera_position = target_player:GetAbsOrigin() + Vector3(0, 0, 64) - camera_angles:Forward() * 100
end
end
end
end
callbacks.Register("DrawModel", function(ctx)
if not target_player or not first_person_mode or not invisibleMaterial then return end
local ent = ctx:GetEntity()
if not ent then return end
if ent == target_player or IsAttachedToTargetPlayer(ent) then
ctx:ForcedMaterialOverride(invisibleMaterial)
end
end)
callbacks.Register("CreateMove", function(cmd)
local localPlayer = entities.GetLocalPlayer()
if localPlayer and localPlayer:IsAlive() then
has_spawned_once = true
end
if first_person_mode then return end
-- Allow mouse movement in free cam or when in third person with target
if free_camera or (target_player and not first_person_mode) then
local mouse_x = -cmd.mousedx * MOUSE_SENSITIVITY
local mouse_y = cmd.mousedy * MOUSE_SENSITIVITY
own_view_angles.y = own_view_angles.y + mouse_x
own_view_angles.x = math.max(-89, math.min(89, own_view_angles.x + mouse_y))
end
end)
callbacks.Register("PostRenderView", function(view)
if engine.Con_IsVisible() or engine.IsGameUIVisible() then
return
end
if not materials_initialized or not windowed_material or not fullscreen_material then
if not InitializeAllMaterials() then
return
end
end
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return end
if localPlayer:IsAlive() then
has_spawned_once = true
is_in_game = true
camera_position = Vector3(0, 0, 0)
CleanupState()
return
end
-- Only show spectator window if we've spawned before and are actually in-game
if not has_spawned_once or not is_in_game then return end
local current_texture = persistent_fullscreen and fullscreen_texture or windowed_texture
local current_material = persistent_fullscreen and fullscreen_material or windowed_material
if not current_texture or not current_material then return end
if camera_position == Vector3(0, 0, 0) then
camera_position = localPlayer:GetAbsOrigin() + Vector3(0, 0, 64)
own_view_angles = engine.GetViewAngles()
if last_killer and last_killer:IsValid() and last_killer:IsAlive() and not last_killer:IsDormant() then
target_player = last_killer
first_person_mode = true
free_camera = false
else
CycleNextEnemy()
first_person_mode = true
free_camera = false
end
last_killer = nil
fullscreen_mode = persistent_fullscreen
end
-- Clean up invalid target player
if target_player and (not target_player:IsValid() or not target_player:IsAlive() or target_player:IsDormant()) then
visited_players = {}
target_player = nil
CycleNextEnemy()
end
HandleCameraControls()
local customView = view
customView.origin = camera_position
customView.angles = camera_angles