-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathHud.cpp
More file actions
975 lines (845 loc) · 28.9 KB
/
Copy pathHud.cpp
File metadata and controls
975 lines (845 loc) · 28.9 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
#include "Hud.hpp"
#include "Features/Demo/GhostEntity.hpp"
#include "Features/Session.hpp"
#include "Features/Timer/PauseTimer.hpp"
#include "Features/EntityList.hpp"
#include "Modules/Client.hpp"
#include "Modules/Engine.hpp"
#include "Modules/EngineDemoPlayer.hpp"
#include "Modules/Scheme.hpp"
#include "Modules/Server.hpp"
#include "Modules/Surface.hpp"
#include "Modules/VGui.hpp"
#include "Event.hpp"
#include "Variable.hpp"
#include "InputHud.hpp"
#include "VphysHud.hpp"
#include "Imgui/Hud/ImguiHuds.hpp"
#include <algorithm>
#include <cstdio>
#include <map>
#include <optional>
Variable sar_hud_spacing("sar_hud_spacing", "1", 0, "Spacing between elements of HUD.\n", FCVAR_DONTRECORD);
Variable sar_hud_x("sar_hud_x", "2", 0, "X padding of HUD.\n", FCVAR_DONTRECORD);
Variable sar_hud_y("sar_hud_y", "2", 0, "Y padding of HUD.\n", FCVAR_DONTRECORD);
Variable sar_hud_font_index("sar_hud_font_index", "0", 0, "Font index of HUD.\n", FCVAR_DONTRECORD);
Variable sar_hud_font_color("sar_hud_font_color", "255 255 255 255", "RGBA font color of HUD.\n", FCVAR_DONTRECORD);
Variable sar_hud_align("sar_hud_align", "0", 0, "Alignment of HUD. (0 = left, 1 = center, 2 = right).\n", FCVAR_DONTRECORD);
Variable sar_hud_precision("sar_hud_precision", "2", 0, "Precision of HUD numbers.\n");
Variable sar_hud_velocity_precision("sar_hud_velocity_precision", "2", 0, "Precision of velocity HUD numbers.\n");
Variable sar_hud_shorthand("sar_hud_shorthand", "0", "Whether to hide the text part of HUD elements.\n", FCVAR_DONTRECORD);
Variable sar_hud_rainbow("sar_hud_rainbow", "-1", -1, 1, "Enables the rainbow HUD mode. -1 = default, 0 = disable, 1 = enable.\n", FCVAR_DONTRECORD);
static bool g_rainbow = false;
ON_INIT {
time_t t = time(NULL);
struct tm *ltime = localtime(&t);
int month = ltime->tm_mon + 1;
if (month == 6) { // June
if (Math::RandomNumber(1, 50) == 1) {
g_rainbow = true;
}
}
}
static Color g_rainbow_color;
ON_EVENT(FRAME) {
switch (sar_hud_rainbow.GetInt()) {
case 0:
g_rainbow = false;
break;
case 1:
g_rainbow = true;
break;
}
if (g_rainbow) {
int host, server, client;
engine->GetTicks(host, server, client);
int hue = (host * 3) % 360; // 2 secs for full cycle
g_rainbow_color = Utils::HSVToRGB(hue, 50, 100);
}
}
bool g_hudPrecisionWarn = true;
CON_COMMAND(sar_hud_precision_disable_warning, "sar_hud_precision_disable_warning {lp|reset} - Disables the warning about high precision values in HUD.\n") {
if (args.ArgC() > 2) {
return console->Print(sar_hud_precision_disable_warning.ThisPtr()->m_pszHelpString);
}
if (args.ArgC() == 1) {
if (g_hudPrecisionWarn) return console->Print("HUD precision above 2 is only allowed in Least Portals. Run \"sar_hud_precision_disable_warning lp\" to confirm.\n");
return console->Print(sar_hud_precision_disable_warning.ThisPtr()->m_pszHelpString);
}
if (!strcmp(args[1], "reset")) {
g_hudPrecisionWarn = true;
return;
} else if (!strcmp(args[1], "lp")) {
g_hudPrecisionWarn = false;
return;
} else {
return console->Print(sar_hud_precision_disable_warning.ThisPtr()->m_pszHelpString);
}
}
ON_EVENT_P(CONFIG_EXEC, -1) {
g_hudPrecisionWarn = true;
}
static inline int getPrecision(bool velocity = false) {
int p = velocity ? sar_hud_velocity_precision.GetInt() : sar_hud_precision.GetInt();
if (p < 0) p = 0;
if (!sv_cheats.GetBool()) {
const int max = velocity ? 2 : 6;
if (p > max) p = max;
}
return p;
}
BaseHud::BaseHud(int type, bool drawSecondSplitScreen, int version)
: type(type)
, drawSecondSplitScreen(drawSecondSplitScreen)
, version(version) {
}
bool BaseHud::ShouldDraw() {
if (engine->IsForcingNoRendering()) {
return false;
}
for (auto& h : g_imguiHuds) {
if (strcmp(h->GetHandle(), "showpos") == 0) {
if (h->Enabled() && h->ShouldDraw()) {
return false;
}
}
}
if (engine->demoplayer->IsPlaying() || engine->IsOrange()) {
return this->type & HudType_InGame;
}
if (!engine->hoststate->m_activeGame) {
return this->type & HudType_Menu;
}
if (pauseTimer->IsActive()) {
return this->type & HudType_Paused;
}
if (session->isRunning) {
return this->type & HudType_InGame;
}
return this->type & HudType_LoadingScreen;
}
std::vector<Hud *> &Hud::GetList() {
static std::vector<Hud *> list;
return list;
}
Hud::Hud(int type, bool drawSecondSplitScreen, int version)
: BaseHud(type, drawSecondSplitScreen, version) {
Hud::GetList().push_back(this);
}
/*
Converting one component of the position into a number.
Possible inputs:
- aliases: names which have certain percentage value assigned,
- percentage: position taking size of the screen into consideration,
- normal: pixel offset from top left corner. negative value changes the corner.
*/
float Hud::PositionFromString(const char *str, bool isX) {
float value = 0;
bool isPercent = false;
// checking if string is an alias
const char *aliases[] = {"top", "left", "begin", "middle", "center", "bottom", "right", "end"};
const char aliasTypes[] = {'y', 'x', 'a', 'a', 'a', 'y', 'x', 'a'};
const float aliasValues[] = {0, 0, 0, 50, 50, 100, 100, 100};
bool isAlias = false;
for (int i = 0; i < 8; i++) {
if ((isX && aliasTypes[i] == 'y') || (!isX && aliasTypes[i] == 'x')) continue;
if (strcmp(str, aliases[i]) == 0) {
value = aliasValues[i];
isPercent = true;
isAlias = true;
break;
}
}
// doing normal string to number conversion
int strLen = strlen(str);
if (!isAlias && strLen>0) {
sscanf(str, "%f", &value);
isPercent = str[strLen - 1] == '%';
}
//converting value into actual position
int xScreen, yScreen;
engine->GetScreenSize(nullptr, xScreen, yScreen);
int xHud, yHud;
GetCurrentSize(xHud, yHud);
int screenSize = (isX) ? xScreen : yScreen;
int hudSize = (isX) ? xHud : yHud;
float pos = value;
if (isPercent) {
pos = (screenSize - hudSize) * value * 0.01;
} else if (pos < 0) {
pos = screenSize + value - hudSize;
}
return pos;
}
void HudContext::DrawElement(const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
char data[128];
vsnprintf(data, sizeof(data), fmt, argptr);
va_end(argptr);
// cut off to the first ": " if shorthand is on
if (sar_hud_shorthand.GetBool()) {
char *colon = strstr(data, ": ");
if (colon) strcpy(data, colon + 2);
}
int align = sar_hud_align.GetInt();
int width = surface->GetFontLength(this->font, "%s", data);
int offset = !align ? 0 : align == 1 ? width / 2 : width;
surface->DrawTxt(font, this->xPadding - offset, this->yPadding + this->elements * (this->fontSize + this->spacing), this->textColor, data);
++this->elements;
if (width > this->maxWidth) this->maxWidth = width;
}
void HudContext::Reset(int slot) {
this->slot = slot;
this->elements = 0;
this->group.fill(0);
this->xPadding = sar_hud_x.GetInt();
if (sar_hud_y.GetInt() < 28) {
this->yPadding = (g_drawImgui ? 28 : sar_hud_y.GetInt());
} else {
this->yPadding = sar_hud_y.GetInt();
}
this->spacing = sar_hud_spacing.GetInt();
this->maxWidth = 0;
this->font = scheme->GetFontByID(sar_hud_font_index.GetInt());
this->fontSize = surface->GetFontHeight(font);
int r, g, b, a;
sscanf(sar_hud_font_color.GetString(), "%i%i%i%i", &r, &g, &b, &a);
if (r == 255 && g == 255 && b == 255 && a == 255 && g_rainbow) {
this->textColor = g_rainbow_color;
} else {
this->textColor = Color(r, g, b, a);
}
}
std::vector<HudElement *> &HudElement::GetList() {
static std::vector<HudElement *> list;
return list;
}
HudElement::HudElement(Variable *variable, int type, bool drawSecondSplitScreen, int version)
: BaseHud(type, drawSecondSplitScreen, version)
, orderIndex(-1)
, variable(variable) {
HudElement::GetList().push_back(this);
}
HudElement::HudElement(Variable *variable, _PaintCallback callback, int type, bool drawSecondSplitScreen, int version)
: HudElement(variable, type, drawSecondSplitScreen, version) {
this->callbackDefault = callback;
}
HudElement::HudElement(const char *name, _PaintCallback callback, int type, bool drawSecondSplitScreen, int version)
: HudElement(nullptr, type, drawSecondSplitScreen, version) {
this->name = name;
this->callbackDefault = callback;
}
HudModeElement::HudModeElement(Variable *variable, _PaintCallbackMode callback, int type, bool drawSecondSplitScreen, int version)
: HudElement(variable, type, drawSecondSplitScreen, version) {
this->callbackMode = callback;
}
HudStringElement::HudStringElement(Variable *variable, _PaintCallbackString callback, int type, bool drawSecondSplitScreen, int version)
: HudElement(variable, type, drawSecondSplitScreen, version) {
this->callbackString = callback;
}
// Default order
std::vector<std::string> elementOrder = {
"fps",
"tastick",
"groundframes",
"text",
"position",
"angles",
"portal_angles",
"portal_angles_2",
"velocity",
"velang",
"groundspeed",
"session",
"last_session",
"sum",
"timer",
"avg",
"cps",
"pause_timer",
"demo",
"jumps",
"portals",
"tbeam",
"tbeam_count",
"steps",
"jump",
"jump_peak",
"velocity_peak",
"frame",
"last_frame",
"inspection",
"implicit_step_speed"
};
void HudElement::IndexAll() {
auto elements = HudElement::GetList();
auto index = 0;
for (const auto &name : elementOrder) {
auto element = std::find_if(elements.begin(), elements.end(), [name](HudElement *element) {
return Utils::ICompare(element->ElementName() + 8, name);
});
if (element != elements.end()) {
(*element)->orderIndex = index;
} else {
console->Warning("HUD name %s is not in element list!\n", name.c_str());
}
++index;
}
}
// Commands
DECL_AUTO_COMMAND_COMPLETION(sar_hud_order_top, (elementOrder))
DECL_AUTO_COMMAND_COMPLETION(sar_hud_order_bottom, (elementOrder))
CON_COMMAND_F_COMPLETION(sar_hud_order_top, "sar_hud_order_top <name> - orders hud element to top\n", FCVAR_DONTRECORD, AUTOCOMPLETION_FUNCTION(sar_hud_order_top)) {
if (args.ArgC() != 2) {
return console->Print(sar_hud_order_top.ThisPtr()->m_pszHelpString);
}
auto elements = &vgui->elements;
auto name = std::string("sar_hud_") + std::string(args[1]);
auto result = std::find_if(elements->begin(), elements->end(), [name](const HudElement *a) {
if (Utils::ICompare(a->ElementName(), name)) {
return true;
}
return false;
});
if (result == elements->end()) {
return console->Print("Unknown HUD element name!\n");
}
auto element = *result;
elements->erase(result);
elements->insert(elements->begin(), element);
console->Print("Moved HUD element %s to top.\n", args[1]);
}
CON_COMMAND_F_COMPLETION(sar_hud_order_bottom, "sar_hud_order_bottom <name> - orders hud element to bottom\n", FCVAR_DONTRECORD, AUTOCOMPLETION_FUNCTION(sar_hud_order_bottom)) {
if (args.ArgC() != 2) {
return console->Print(sar_hud_order_bottom.ThisPtr()->m_pszHelpString);
}
auto elements = &vgui->elements;
auto name = std::string("sar_hud_") + std::string(args[1]);
auto result = std::find_if(elements->begin(), elements->end(), [name](const HudElement *a) {
if (Utils::ICompare(a->ElementName(), name)) {
return true;
}
return false;
});
if (result == elements->end()) {
return console->Print("Unknown HUD element name!\n");
}
auto element = *result;
elements->erase(result);
elements->insert(elements->end(), element);
console->Print("Moved HUD element %s to bottom.\n", args[1]);
}
CON_COMMAND_F(sar_hud_order_reset, "sar_hud_order_reset - resets order of hud elements\n", FCVAR_DONTRECORD) {
std::sort(vgui->elements.begin(), vgui->elements.end(), [](const HudElement *a, const HudElement *b) {
return a->orderIndex < b->orderIndex;
});
console->Print("Reset default HUD element order!\n");
}
// HUD
struct TextComponent {
std::optional<Color> color;
std::string text;
};
struct TextLine {
bool draw = true;
std::optional<Color> defaultColor;
std::vector<TextComponent> components;
};
static std::map<long, TextLine> sar_hud_text_vals;
HUD_ELEMENT2_NO_DISABLE(text, HudType_InGame | HudType_Paused | HudType_Menu | HudType_LoadingScreen) {
for (auto &t : sar_hud_text_vals) {
int x = ctx->xPadding;
int y = ctx->yPadding + ctx->elements * (ctx->fontSize + ctx->spacing);
int totalPixLen = 0;
for (auto &c : t.second.components) totalPixLen += surface->GetFontLength(ctx->font, "%s", c.text.c_str());
int align = sar_hud_align.GetInt();
int offset = !align ? 0 : align == 1 ? totalPixLen / 2 : totalPixLen;
if (t.second.draw) {
for (auto &c : t.second.components) {
Color color = c.color ? *c.color : t.second.defaultColor ? *t.second.defaultColor : ctx->textColor;
int pixLen = surface->GetFontLength(ctx->font, "%s", c.text.c_str());
surface->DrawTxt(ctx->font, x - offset, y, color, "%s", c.text.c_str());
x += pixLen;
}
if (t.second.components.size() > 0) {
++ctx->elements;
}
int width = x - ctx->xPadding;
if (width > ctx->maxWidth) ctx->maxWidth = width;
}
}
}
// autocomplete function used for "setpos" commands.
DECL_DECLARE_AUTOCOMPLETION_FUNCTION(HudSetPos) {
//separate command from parameters
std::string partialStr = partial;
std::string cmdStr, matchStr;
auto cmdSpace = partialStr.find(' ');
if (cmdSpace != std::string::npos) {
cmdStr = partialStr.substr(0, cmdSpace + 1);
matchStr = partialStr.substr(cmdSpace + 1);
}
const char *cmd = cmdStr.c_str();
static auto items = std::vector<std::string>();
items.clear();
// do the autocomplete here
auto space = matchStr.find(' ');
std::string prefix = ""; // already typed part
std::string main = matchStr; // currently autocompleted part
bool isX = false;
if (space != std::string::npos) {
// autocompleting second (x) component
isX = true;
prefix = matchStr.substr(0, space + 1);
main = matchStr.substr(space + 1);
}
// double space, disable autocomplete
if (isX && prefix.length() <= 1) {
return 0;
}
const char *aliases[] = {"top", "left", "begin", "middle", "center", "bottom", "right", "end"};
const char aliasTypes[] = {'y', 'x', 'a', 'a', 'a', 'y', 'x', 'a'};
for (int i = 0; i < 8; i++) {
if ((isX && aliasTypes[i] == 'y') || (!isX && aliasTypes[i] == 'x')) continue;
if (std::strstr(aliases[i], main.c_str())) {
items.push_back(prefix + aliases[i]);
}
}
bool isnum = true;
for (char l : main) {
if ((l < '0' || l > '9') && l != '-' && l != '+' && l != '.') {
isnum = false;
break;
}
}
if (isnum && main.length() > 0) {
items.push_back(prefix + main);
items.push_back(prefix + main + "%");
}
auto count = 0;
for (auto &item : items) {
std::strcpy(commands[count++], (std::string(cmd) + item).c_str());
}
return count;
}
long parseIdx(const char *idxStr) {
char *end;
long idx = std::strtol(idxStr, &end, 10);
if (*end != 0 || end == idxStr) {
return -1;
}
return idx;
}
CON_COMMAND_F(sar_hud_set_text, "sar_hud_set_text <id> <text>... - sets and shows the nth text value in the HUD\n", FCVAR_DONTRECORD) {
if (args.ArgC() < 3) {
console->Print(sar_hud_set_text.ThisPtr()->m_pszHelpString);
return;
}
long idx = parseIdx(args[1]);
if (idx == -1) {
console->Print(sar_hud_set_text.ThisPtr()->m_pszHelpString);
return;
}
const char *txt = Utils::ArgContinuation(args, 2);
std::optional<Color> curColor;
std::string component = "";
std::vector<TextComponent> components;
while (*txt) {
if (*txt == '#') {
++txt;
if (*txt == '#') {
component += '#';
++txt;
continue;
} else {
unsigned r, g, b;
int end = -1;
if (sscanf(txt, "%2x%2x%2x%n", &r, &g, &b, &end) == 3 && end == 6) {
txt += 6;
if (!curColor || curColor->r != r || curColor->g != g || curColor->b != b) {
components.push_back({curColor, component});
curColor = Color{(uint8_t)r, (uint8_t)g, (uint8_t)b, 255};
component = "";
}
continue;
}
if (*txt == 'r') {
txt++;
components.push_back({curColor, component});
curColor.reset();
component = "";
continue;
}
component += '#';
continue;
}
}
component += *txt;
++txt;
}
if (component.size() != 0) {
components.push_back({curColor, component});
}
sar_hud_text_vals[idx].components = components;
}
CON_COMMAND_F(sar_hud_set_text_color, "sar_hud_set_text_color <id> [color] - sets the color of the nth text value in the HUD. Reset by not giving color.\n", FCVAR_DONTRECORD) {
if (args.ArgC() < 2 || args.ArgC() > 3) {
console->Print(sar_hud_set_text_color.ThisPtr()->m_pszHelpString);
return;
}
long idx = parseIdx(args[1]);
if (idx == -1) {
console->Print(sar_hud_set_text_color.ThisPtr()->m_pszHelpString);
return;
}
if (args.ArgC() == 2) {
sar_hud_text_vals[idx].defaultColor.reset();
} else {
auto col = Utils::GetColor(args[2]);
if (!col) return console->Print("Invalid color string '%s'\n", args[2]);
sar_hud_text_vals[idx].defaultColor = *col;
}
}
CON_COMMAND_F(sar_hud_hide_text, "sar_hud_hide_text <id|all> - hides the nth text value in the HUD\n", FCVAR_DONTRECORD) {
if (args.ArgC() < 2) {
console->Print(sar_hud_hide_text.ThisPtr()->m_pszHelpString);
return;
}
if (!strcmp(args[1], "all")) {
for (auto &t : sar_hud_text_vals) {
t.second.draw = false;
}
return;
}
long idx = parseIdx(args[1]);
if (idx == -1) {
console->Print(sar_hud_hide_text.ThisPtr()->m_pszHelpString);
return;
}
sar_hud_text_vals[idx].draw = false;
}
CON_COMMAND_F(sar_hud_show_text, "sar_hud_show_text <id|all> - shows the nth text value in the HUD\n", FCVAR_DONTRECORD) {
if (args.ArgC() < 2) {
console->Print(sar_hud_show_text.ThisPtr()->m_pszHelpString);
return;
}
if (!strcmp(args[1], "all")) {
for (auto &t : sar_hud_text_vals) {
t.second.draw = true;
}
return;
}
long idx = parseIdx(args[1]);
if (idx == -1) {
console->Print(sar_hud_show_text.ThisPtr()->m_pszHelpString);
return;
}
sar_hud_text_vals[idx].draw = true;
}
CON_COMMAND_F(sar_hud_toggle_text, "sar_hud_toggle_text <id> - toggles the nth text value in the HUD\n", FCVAR_DONTRECORD) {
if (args.ArgC() < 2) {
console->Print(sar_hud_toggle_text.ThisPtr()->m_pszHelpString);
return;
}
long idx = parseIdx(args[1]);
if (idx == -1) {
console->Print(sar_hud_toggle_text.ThisPtr()->m_pszHelpString);
return;
}
sar_hud_text_vals[idx].draw = !sar_hud_text_vals[idx].draw;
}
HUD_ELEMENT_MODE2(position, "0", 0, 2,
"Draws absolute position of the client.\n"
"0 = Default,\n"
"1 = Player position,\n"
"2 = Camera (shoot) position.\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = client->GetPlayer(ctx->slot + 1);
if (player) {
auto pos = client->GetAbsOrigin(player);
if (mode >= 2) {
pos = pos + client->GetViewOffset(player) + client->GetPortalLocal(player).m_vEyeOffset;
}
int p = getPrecision();
ctx->DrawElement("pos: %.*f %.*f %.*f", p, pos.x, p, pos.y, p, pos.z);
} else {
ctx->DrawElement("pos: -");
}
}
HUD_ELEMENT_MODE2(angles, "0", 0, 4,
"Draws absolute view angles of the client.\n"
"0 = Default,\n"
"1 = XY,\n"
"2 = XYZ,\n"
"3 = X,\n"
"4 = Y.\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
// When we're orange (and not splitscreen), for some fucking reason,
// the *engine* thinks we're slot 0, but everything else thinks
// we're slot 1
auto ang = engine->GetAngles(engine->IsOrange() ? 0 : ctx->slot);
int p = getPrecision();
if (mode == 1) {
ctx->DrawElement("ang: %.*f %.*f", p, ang.x, p, ang.y);
} else if (mode == 2) {
ctx->DrawElement("ang: %.*f %.*f %.*f", p, ang.x, p, ang.y, p, ang.z);
} else if (mode == 3) {
ctx->DrawElement("ang: %.*f", p, ang.x);
} else {
ctx->DrawElement("ang: %.*f", p, ang.y);
}
}
HUD_ELEMENT_MODE2(velocity, "0", 0, 5,
"Draws velocity of the client.\n"
"0 = Default,\n"
"1 = X, Y, Z\n"
"2 = X:Y\n"
"3 = X:Y, Z\n"
"4 = X:Y:Z\n"
"5 = X, Y, X:Y, Z\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = client->GetPlayer(ctx->slot + 1);
if (player) {
int p = getPrecision(true);
auto vel = client->GetLocalVelocity(player);
switch (mode) {
case 1:
ctx->DrawElement("vel: %.*f %.*f %.*f", p, vel.x, p, vel.y, p, vel.z);
break;
case 2:
ctx->DrawElement("vel: %.*f", p, vel.Length2D());
break;
case 3:
ctx->DrawElement("vel: %.*f %.*f", p, vel.Length2D(), p, vel.z);
break;
case 4:
ctx->DrawElement("vel: %.*f", p, vel.Length());
break;
case 5:
ctx->DrawElement("vel: %.*f %.*f (%.*f) %.*f", p, vel.x, p, vel.y, p, vel.Length2D(), p, vel.z);
break;
}
} else {
ctx->DrawElement("vel: -");
}
}
HUD_ELEMENT_MODE2(eyeoffset, "0", 0, 2,
"Draws player's eye offset.\n"
"0 = Default,\n"
"1 = eye offset including precision error,\n"
"2 = raw eye offset.\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = client->GetPlayer(ctx->slot + 1);
if (player) {
auto eyepos = client->GetPortalLocal(player).m_vEyeOffset;
if (mode == 1) {
auto viewOffset = client->GetAbsOrigin(player) + client->GetViewOffset(player);
auto realPos = viewOffset + eyepos;
eyepos = realPos - viewOffset;
}
int p = getPrecision();
ctx->DrawElement("eyeoff: %.*f %.*f %.*f", p, eyepos.x, p, eyepos.y, p, eyepos.z);
} else {
ctx->DrawElement("eyeoff: -");
}
}
HUD_ELEMENT_MODE2(velang, "0", 0, 2,
"Draw the angle of the player's horizontal velocity vector.\n"
"0 = Default,\n"
"1 = yaw,\n"
"2 = pitch yaw.\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = client->GetPlayer(ctx->slot + 1);
if (player) {
auto vel = client->GetLocalVelocity(player);
float pitch = -RAD2DEG(atan2(vel.z, vel.Length2D()));
float yaw = RAD2DEG(atan2(vel.y, vel.x));
if (mode == 1) {
ctx->DrawElement("velang: %.*f", getPrecision(true), yaw);
} else {
ctx->DrawElement("velang: %.*f %.*f", getPrecision(true), pitch, getPrecision(true), yaw);
}
} else {
ctx->DrawElement("velang: -");
}
}
HUD_ELEMENT_MODE2(groundspeed, "0", 0, 2, "Draw the speed of the player upon leaving the ground.\n"
"0 = Default\n"
"1 = Groundspeed\n"
"2 = Groundspeed (Gain)\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
static float speeds[2];
static float drawSpeeds[2];
static bool groundeds[2];
static float lastSpeeds[2] = {0};
auto player = client->GetPlayer(ctx->slot + 1);
if (!player) {
groundeds[ctx->slot] = false;
drawSpeeds[ctx->slot] = 0.0f;
ctx->DrawElement("groundspeed: -");
return;
}
if (player->ground_entity()) {
groundeds[ctx->slot] = true;
speeds[ctx->slot] = client->GetLocalVelocity(player).Length();
} else if (groundeds[ctx->slot]) {
groundeds[ctx->slot] = false;
lastSpeeds[ctx->slot] = drawSpeeds[ctx->slot];
drawSpeeds[ctx->slot] = speeds[ctx->slot];
}
if (mode == 2) {
ctx->DrawElement("groundspeed: %.*f (%.*f)", getPrecision(true), drawSpeeds[ctx->slot], getPrecision(true), drawSpeeds[ctx->slot] - lastSpeeds[ctx->slot]);
} else {
ctx->DrawElement("groundspeed: %.*f", getPrecision(true), drawSpeeds[ctx->slot]);
}
}
QAngle g_bluePortalAngles[2];
QAngle g_orangePortalAngles[2];
HUD_ELEMENT_MODE2(portal_angles, "0", 0, 2, "Draw the camera angles of the last primary portal shot.\n", HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto ang = g_bluePortalAngles[engine->IsOrange() ? 0 : ctx->slot];
int p = getPrecision();
if (mode == 1) {
ctx->DrawElement("portal ang: %.*f %.*f", p, ang.x, p, ang.y);
} else {
ctx->DrawElement("portal ang: %.*f %.*f %.*f", p, ang.x, p, ang.y, p, ang.z);
}
}
HUD_ELEMENT_MODE2(portal_angles_2, "0", 0, 2, "Draw the camera angles of the last secondary portal shot.\n", HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto ang = g_orangePortalAngles[engine->IsOrange() ? 0 : ctx->slot];
int p = getPrecision();
if (mode == 1) {
ctx->DrawElement("portal ang 2: %.*f %.*f", p, ang.x, p, ang.y);
} else {
ctx->DrawElement("portal ang 2: %.*f %.*f %.*f", p, ang.x, p, ang.y, p, ang.z);
}
}
HUD_ELEMENT_MODE2(duckstate, "0", 0, 2,
"Draw the state of player ducking.\n"
"1 - shows either ducked or standing state\n"
"2 - shows detailed report (requires sv_cheats)\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen)
{
auto player = client->GetPlayer(ctx->slot + 1);
if (!player) {
ctx->DrawElement("duckstate: -");
return;
}
bool ducked = player->ducked();
if (mode == 1 || !sv_cheats.GetBool()) {
ctx->DrawElement("duckstate: %s", ducked ? "ducked" : "standing");
} else {
bool holdingDuck = (inputHud.GetButtonBits(ctx->slot) & IN_DUCK);
bool ducking = player->field<bool>("m_bDucking");
bool inDuckJump = player->field<bool>("m_bInDuckJump");
int duckTimeMsecs = player->field<int>("m_nDuckTimeMsecs");
int duckJumpTimeMsecs = player->field<int>("m_nDuckJumpTimeMsecs");
bool duckedInAir = client->GetPortalLocal(player).m_bDuckedInAir;
Vector viewOffset = client->GetViewOffset(player);
std::string duckState = "standing";
//recovering from duck jump. standing, but can't crouch until interpolation is over
if (!ducking && !ducked && duckJumpTimeMsecs > 0) {
int remaining = (200 - (1000 - duckJumpTimeMsecs)) / 16 + 1;
duckState += " (duck jump recovery, " + std::to_string(remaining) + " ticks left)";
}
if (ducking) {
duckState = "ducking";
int length = 400;
if (ducked || !holdingDuck) {
duckState = "unducking";
length = 200;
}
int remaining = (length - (1000 - duckTimeMsecs)) / 16 + 1;
duckState += " (" + std::to_string(remaining) + " ticks left)";
if (duckedInAir) duckState = "air " + duckState;
} else if (ducked) {
if (holdingDuck && inDuckJump && (duckJumpTimeMsecs > 0 || viewOffset.z > 28.0)) {
duckState = "quantum crouch";
if (duckedInAir) duckState += " (low variant)";
} else if (inDuckJump && duckJumpTimeMsecs == 0) {
duckState = "forced duck";
} else {
duckState = "ducked";
}
}
ctx->DrawElement("duckstate: %s", duckState.c_str());
}
}
HUD_ELEMENT2(tbeam, "0", "Draw the name of the funnel player is currently in (requires sv_cheats).\n", HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = server->GetPlayer(ctx->slot + 1);
if (!player || !sv_cheats.GetBool()) {
ctx->DrawElement("tbeam: -");
return;
}
auto portalLocal = server->GetPortalLocal(player);
void* tbeamHandle = reinterpret_cast<void*>(portalLocal.m_hTractorBeam);
if (!tbeamHandle || (uint32_t)tbeamHandle == (unsigned)Offsets::INVALID_EHANDLE_INDEX) {
ctx->DrawElement("tbeam: none");
} else {
ctx->DrawElement("tbeam: [0x%08X]", (uint32_t)tbeamHandle);
}
}
HUD_ELEMENT2(tbeam_count, "0", "Draw the player's funnel count (requires sv_cheats).\n", HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = server->GetPlayer(ctx->slot + 1);
if (!player || !sv_cheats.GetBool()) {
ctx->DrawElement("tbeam count: -");
return;
}
auto portalLocal = server->GetPortalLocal(player);
ctx->DrawElement("tbeam count: %d", portalLocal.m_nTractorBeamCount);
}
HUD_ELEMENT_MODE2(ent_slot_serial, "0", 0, 4096,
"Draw the serial number of given entity slot.\n",
HudType_InGame | HudType_Paused | HudType_LoadingScreen | HudType_Menu) {
if (!sv_cheats.GetBool()) {
ctx->DrawElement("ent slot - serial: -");
return;
}
int serial = entityList->GetEntityInfoByIndex(mode)->m_SerialNumber;
ctx->DrawElement("ent slot %d serial: %d", mode, serial);
}
HUD_ELEMENT2(implicit_step_speed, "0", "Draw the player's implicit vertical step speed (requires sv_cheats).\n", HudType_InGame | HudType_Paused | HudType_LoadingScreen) {
auto player = server->GetPlayer(ctx->slot + 1);
if (!player || !sv_cheats.GetBool()) {
ctx->DrawElement("implicit step speed: -");
return;
}
float implicitStepSpeed = player->fieldOff<float>("m_flUseKeyCooldownTime", 8);
ctx->DrawElement("implicit step speed: %f", implicitStepSpeed);
}
HUD_ELEMENT_MODE2(fps, "0", 0, 2, "Show fps (frames per second) on the SAR hud.\n1 - Show fps\n2 - Show fps with fps cap\n", HudType_InGame | HudType_Paused | HudType_Menu) {
if (mode == 1) {
ctx->DrawElement("fps: %.0f", g_cur_fps);
} else if (mode == 2) {
ctx->DrawElement("fps: %.0f/%i", g_cur_fps, fps_max.GetInt());
}
}
CON_COMMAND_F(sar_pip_align, "sar_pip_align <top|center|bottom> <left|center|right> - aligns the remote view.\n", FCVAR_DONTRECORD) {
if (args.ArgC() != 3) {
return console->Print(sar_pip_align.ThisPtr()->m_pszHelpString);
}
int sw, sh;
engine->GetScreenSize(nullptr, sw, sh);
float scale = ss_pipscale.GetFloat();
float w = sw * scale, h = sh * scale, x, y;
if (!strcasecmp(args[1], "top")) {
y = sh - h - 25;
} else if (!strcasecmp(args[1], "center")) {
y = (sh - h) / 2;
} else if (!strcasecmp(args[1], "bottom")) {
y = 25;
} else {
return console->Print(sar_pip_align.ThisPtr()->m_pszHelpString);
}
if (!strcasecmp(args[2], "left")) {
x = sw - w - 25;
} else if (!strcasecmp(args[2], "center")) {
x = (sw - w) / 2;
} else if (!strcasecmp(args[2], "right")) {
x = 25;
} else {
return console->Print(sar_pip_align.ThisPtr()->m_pszHelpString);
}
Variable("ss_pip_right_offset").SetValue(x);
Variable("ss_pip_bottom_offset").SetValue(y);
}