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
|
/*
Mapping API - Triggers
Implements trigger related features.
*/
static float lastTrigMultiTouchTime[MAXPLAYERS + 1];
static float lastTrigTeleTouchTime[MAXPLAYERS + 1];
static float lastTouchGroundOrLadderTime[MAXPLAYERS + 1];
static int lastTouchSingleBhopEntRef[MAXPLAYERS + 1];
static ArrayList lastTouchSequentialBhopEntRefs[MAXPLAYERS + 1];
static int triggerTouchCount[MAXPLAYERS + 1];
static int antiCpTriggerTouchCount[MAXPLAYERS + 1];
static int antiPauseTriggerTouchCount[MAXPLAYERS + 1];
static int antiJumpstatTriggerTouchCount[MAXPLAYERS + 1];
static int mapMappingApiVersion = GOKZ_MAPPING_API_VERSION_NONE;
static int bhopTouchCount[MAXPLAYERS + 1];
static bool jumpedThisTick[MAXPLAYERS + 1];
static float jumpOrigin[MAXPLAYERS + 1][3];
static float jumpVelocity[MAXPLAYERS + 1][3];
static ArrayList triggerTouchList[MAXPLAYERS + 1]; // arraylist of TouchedTrigger that the player is currently touching. this array won't ever get long (unless the mapper does something weird).
static StringMap triggerTouchCounts[MAXPLAYERS + 1]; // stringmap of int touch counts with key being a string of the entity reference.
static StringMap antiBhopTriggers; // stringmap of AntiBhopTrigger with key being a string of the m_iHammerID entprop.
static StringMap teleportTriggers; // stringmap of TeleportTrigger with key being a string of the m_iHammerID entprop.
static StringMap timerButtonTriggers; // stringmap of legacy timer zone triggers with key being a string of the m_iHammerID entprop.
static ArrayList parseErrorStrings;
// =====[ PUBLIC ]=====
bool BhopTriggersJustTouched(int client)
{
// NOTE: This is slightly incorrect since we touch triggers in the air, but
// it doesn't matter since we can't checkpoint in the air.
if (bhopTouchCount[client] > 0)
{
return true;
}
// GetEngineTime return changes between calls. We only call it once at the beginning.
float engineTime = GetEngineTime();
// If the player touches a teleport trigger, increase the delay required
if (engineTime - lastTouchGroundOrLadderTime[client] < GOKZ_MULT_NO_CHECKPOINT_TIME // Just touched ground or ladder
&& engineTime - lastTrigMultiTouchTime[client] < GOKZ_MULT_NO_CHECKPOINT_TIME // Just touched trigger_multiple
|| engineTime - lastTrigTeleTouchTime[client] < GOKZ_BHOP_NO_CHECKPOINT_TIME) // Just touched trigger_teleport
{
return true;
}
return Movement_GetMovetype(client) == MOVETYPE_LADDER
&& triggerTouchCount[client] > 0
&& engineTime - lastTrigTeleTouchTime[client] < GOKZ_LADDER_NO_CHECKPOINT_TIME;
}
bool AntiCpTriggerIsTouched(int client)
{
return antiCpTriggerTouchCount[client] > 0;
}
bool AntiPauseTriggerIsTouched(int client)
{
return antiPauseTriggerTouchCount[client] > 0;
}
void PushMappingApiError(char[] format, any ...)
{
char error[GOKZ_MAX_MAPTRIGGERS_ERROR_LENGTH];
VFormat(error, sizeof(error), format, 2);
parseErrorStrings.PushString(error);
}
TriggerType GetTriggerType(char[] targetName)
{
TriggerType result = TriggerType_Invalid;
if (StrEqual(targetName, GOKZ_ANTI_BHOP_TRIGGER_NAME))
{
result = TriggerType_Antibhop;
}
else if (StrEqual(targetName, GOKZ_TELEPORT_TRIGGER_NAME))
{
result = TriggerType_Teleport;
}
return result;
}
bool IsBhopTrigger(TeleportType type)
{
return type == TeleportType_MultiBhop
|| type == TeleportType_SingleBhop
|| type == TeleportType_SequentialBhop;
}
bool IsTimerButtonTrigger(int entity, TimerButtonTrigger trigger)
{
char hammerID[32];
bool gotHammerID = GetEntityHammerIDString(entity, hammerID, sizeof(hammerID));
if (gotHammerID && timerButtonTriggers.GetArray(hammerID, trigger, sizeof(trigger)))
{
return true;
}
return false;
}
// =====[ EVENTS ]=====
void OnPluginStart_MapTriggers()
{
parseErrorStrings = new ArrayList(ByteCountToCells(GOKZ_MAX_MAPTRIGGERS_ERROR_LENGTH));
antiBhopTriggers = new StringMap();
teleportTriggers = new StringMap();
timerButtonTriggers = new StringMap();
}
void OnMapStart_MapTriggers()
{
parseErrorStrings.Clear();
antiBhopTriggers.Clear();
teleportTriggers.Clear();
timerButtonTriggers.Clear();
mapMappingApiVersion = GOKZ_MAPPING_API_VERSION_NONE;
EntlumpParse(antiBhopTriggers, teleportTriggers, timerButtonTriggers, mapMappingApiVersion);
if (mapMappingApiVersion > GOKZ_MAPPING_API_VERSION)
{
SetFailState("Map's mapping api version is too big! Maximum supported version is %i, but map has %i. If you're not on the latest GOKZ version, then update!",
GOKZ_MAPPING_API_VERSION, mapMappingApiVersion);
}
}
void OnClientPutInServer_MapTriggers(int client)
{
triggerTouchCount[client] = 0;
antiCpTriggerTouchCount[client] = 0;
antiPauseTriggerTouchCount[client] = 0;
antiJumpstatTriggerTouchCount[client] = 0;
if (triggerTouchList[client] == null)
{
triggerTouchList[client] = new ArrayList(sizeof(TouchedTrigger));
}
else
{
triggerTouchList[client].Clear();
}
if (triggerTouchCounts[client] == null)
{
triggerTouchCounts[client] = new StringMap();
}
else
{
triggerTouchCounts[client].Clear();
}
bhopTouchCount[client] = 0;
if (lastTouchSequentialBhopEntRefs[client] == null)
{
lastTouchSequentialBhopEntRefs[client] = new ArrayList();
}
else
{
lastTouchSequentialBhopEntRefs[client].Clear();
}
}
void OnPlayerRunCmd_MapTriggers(int client, int &buttons)
{
int flags = GetEntityFlags(client);
MoveType moveType = GetEntityMoveType(client);
// if the player isn't touching any bhop triggers on ground/a ladder, then
// reset the singlebhop and sequential bhop state.
if ((flags & FL_ONGROUND || moveType == MOVETYPE_LADDER)
&& bhopTouchCount[client] == 0)
{
ResetBhopState(client);
}
if (antiJumpstatTriggerTouchCount[client] > 0)
{
if (GetFeatureStatus(FeatureType_Native, "GOKZ_JS_InvalidateJump") == FeatureStatus_Available)
{
GOKZ_JS_InvalidateJump(client);
}
}
// Check if we're touching any triggers and act accordingly.
// NOTE: Read through the touch list in reverse order, so some
// trigger behaviours will be better. Trust me!
int triggerTouchListLength = triggerTouchList[client].Length;
for (int i = triggerTouchListLength - 1; i >= 0; i--)
{
TouchedTrigger touched;
triggerTouchList[client].GetArray(i, touched);
if (touched.triggerType == TriggerType_Antibhop)
{
TouchAntibhopTrigger(client, touched, buttons, flags);
}
else if (touched.triggerType == TriggerType_Teleport)
{
// Sometimes due to lag or whatever, the player can be
// teleported twice by the same trigger. This fixes that.
if (TouchTeleportTrigger(client, touched, flags))
{
RemoveTriggerFromTouchList(client, EntRefToEntIndex(touched.entRef));
i--;
triggerTouchListLength--;
}
}
}
jumpedThisTick[client] = false;
}
void OnPlayerSpawn_MapTriggers(int client)
{
// Print trigger errors every time a player spawns so that
// mappers and testers can very easily spot mistakes in names
// and get them fixed asap.
if (parseErrorStrings.Length > 0)
{
char errStart[] = "ERROR: Errors detected when trying to load triggers!";
CPrintToChat(client, "{red}%s", errStart);
PrintToConsole(client, "\n%s", errStart);
int length = parseErrorStrings.Length;
for (int err = 0; err < length; err++)
{
char error[GOKZ_MAX_MAPTRIGGERS_ERROR_LENGTH];
parseErrorStrings.GetString(err, error, sizeof(error));
CPrintToChat(client, "{red}%s", error);
PrintToConsole(client, error);
}
CPrintToChat(client, "{red}If the errors get clipped off in the chat, then look in your developer console!\n");
}
}
public void OnPlayerJump_Triggers(int client)
{
jumpedThisTick[client] = true;
GetClientAbsOrigin(client, jumpOrigin[client]);
Movement_GetVelocity(client, jumpVelocity[client]);
}
void OnEntitySpawned_MapTriggers(int entity)
{
char classname[32];
GetEntityClassname(entity, classname, sizeof(classname));
char name[64];
GetEntityName(entity, name, sizeof(name));
bool triggerMultiple = StrEqual("trigger_multiple", classname);
if (triggerMultiple)
{
char hammerID[32];
bool gotHammerID = GetEntityHammerIDString(entity, hammerID, sizeof(hammerID));
if (StrEqual(GOKZ_TELEPORT_TRIGGER_NAME, name))
{
TeleportTrigger teleportTrigger;
if (gotHammerID && teleportTriggers.GetArray(hammerID, teleportTrigger, sizeof(teleportTrigger)))
{
HookSingleEntityOutput(entity, "OnStartTouch", OnTeleportTrigTouchStart_MapTriggers);
HookSingleEntityOutput(entity, "OnEndTouch", OnTeleportTrigTouchEnd_MapTriggers);
}
else
{
PushMappingApiError("ERROR: Couldn't match teleport trigger's Hammer ID %s with any Hammer ID from the map.", hammerID);
}
}
else if (StrEqual(GOKZ_ANTI_BHOP_TRIGGER_NAME, name))
{
AntiBhopTrigger antiBhopTrigger;
if (gotHammerID && antiBhopTriggers.GetArray(hammerID, antiBhopTrigger, sizeof(antiBhopTrigger)))
{
HookSingleEntityOutput(entity, "OnStartTouch", OnAntiBhopTrigTouchStart_MapTriggers);
HookSingleEntityOutput(entity, "OnEndTouch", OnAntiBhopTrigTouchEnd_MapTriggers);
}
else
{
PushMappingApiError("ERROR: Couldn't match antibhop trigger's Hammer ID %s with any Hammer ID from the map.", hammerID);
}
}
else if (StrEqual(GOKZ_BHOP_RESET_TRIGGER_NAME, name))
{
HookSingleEntityOutput(entity, "OnStartTouch", OnBhopResetTouchStart_MapTriggers);
}
else if (StrEqual(GOKZ_ANTI_CP_TRIGGER_NAME, name, false))
{
HookSingleEntityOutput(entity, "OnStartTouch", OnAntiCpTrigTouchStart_MapTriggers);
HookSingleEntityOutput(entity, "OnEndTouch", OnAntiCpTrigTouchEnd_MapTriggers);
}
else if (StrEqual(GOKZ_ANTI_PAUSE_TRIGGER_NAME, name, false))
{
HookSingleEntityOutput(entity, "OnStartTouch", OnAntiPauseTrigTouchStart_MapTriggers);
HookSingleEntityOutput(entity, "OnEndTouch", OnAntiPauseTrigTouchEnd_MapTriggers);
}
else if (StrEqual(GOKZ_ANTI_JUMPSTAT_TRIGGER_NAME, name, false))
{
HookSingleEntityOutput(entity, "OnStartTouch", OnAntiJumpstatTrigTouchStart_MapTriggers);
HookSingleEntityOutput(entity, "OnEndTouch", OnAntiJumpstatTrigTouchEnd_MapTriggers);
}
else
{
// NOTE: SDKHook touch hooks bypass trigger filters. We want that only with
// non mapping api triggers because it prevents checkpointing on bhop blocks.
SDKHook(entity, SDKHook_StartTouchPost, OnTrigMultTouchStart_MapTriggers);
SDKHook(entity, SDKHook_EndTouchPost, OnTrigMultTouchEnd_MapTriggers);
}
}
else if (StrEqual("trigger_teleport", classname))
{
SDKHook(entity, SDKHook_StartTouchPost, OnTrigTeleTouchStart_MapTriggers);
SDKHook(entity, SDKHook_EndTouchPost, OnTrigTeleTouchEnd_MapTriggers);
}
}
public void OnAntiBhopTrigTouchStart_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
int touchCount = IncrementTriggerTouchCount(other, entity);
if (touchCount <= 0)
{
// The trigger has fired a matching endtouch output before
// the starttouch output, so ignore it.
return;
}
if (jumpedThisTick[other])
{
TeleportEntity(other, jumpOrigin[other], NULL_VECTOR, jumpVelocity[other]);
}
AddTriggerToTouchList(other, entity, TriggerType_Antibhop);
}
public void OnAntiBhopTrigTouchEnd_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
DecrementTriggerTouchCount(other, entity);
RemoveTriggerFromTouchList(other, entity);
}
public void OnTeleportTrigTouchStart_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
int touchCount = IncrementTriggerTouchCount(other, entity);
if (touchCount <= 0)
{
// The trigger has fired a matching endtouch output before
// the starttouch output, so ignore it.
return;
}
char key[32];
GetEntityHammerIDString(entity, key, sizeof(key));
TeleportTrigger trigger;
if (teleportTriggers.GetArray(key, trigger, sizeof(trigger))
&& IsBhopTrigger(trigger.type))
{
bhopTouchCount[other]++;
}
AddTriggerToTouchList(other, entity, TriggerType_Teleport);
}
public void OnTeleportTrigTouchEnd_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
DecrementTriggerTouchCount(other, entity);
char key[32];
GetEntityHammerIDString(entity, key, sizeof(key));
TeleportTrigger trigger;
if (teleportTriggers.GetArray(key, trigger, sizeof(trigger))
&& IsBhopTrigger(trigger.type))
{
bhopTouchCount[other]--;
}
RemoveTriggerFromTouchList(other, entity);
}
public void OnBhopResetTouchStart_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
ResetBhopState(other);
}
public void OnAntiCpTrigTouchStart_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
antiCpTriggerTouchCount[other]++;
}
public void OnAntiCpTrigTouchEnd_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
antiCpTriggerTouchCount[other]--;
}
public void OnAntiPauseTrigTouchStart_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
antiPauseTriggerTouchCount[other]++;
}
public void OnAntiPauseTrigTouchEnd_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
antiPauseTriggerTouchCount[other]--;
}
public void OnAntiJumpstatTrigTouchStart_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
antiJumpstatTriggerTouchCount[other]++;
}
public void OnAntiJumpstatTrigTouchEnd_MapTriggers(const char[] output, int entity, int other, float delay)
{
if (!IsValidClient(other))
{
return;
}
antiJumpstatTriggerTouchCount[other]--;
}
public void OnTrigMultTouchStart_MapTriggers(int entity, int other)
{
if (!IsValidClient(other))
{
return;
}
lastTrigMultiTouchTime[other] = GetEngineTime();
triggerTouchCount[other]++;
}
public void OnTrigMultTouchEnd_MapTriggers(int entity, int other)
{
if (!IsValidClient(other))
{
return;
}
triggerTouchCount[other]--;
}
public void OnTrigTeleTouchStart_MapTriggers(int entity, int other)
{
if (!IsValidClient(other))
{
return;
}
lastTrigTeleTouchTime[other] = GetEngineTime();
triggerTouchCount[other]++;
}
public void OnTrigTeleTouchEnd_MapTriggers(int entity, int other)
{
if (!IsValidClient(other))
{
return;
}
triggerTouchCount[other]--;
}
void OnStartTouchGround_MapTriggers(int client)
{
lastTouchGroundOrLadderTime[client] = GetEngineTime();
for (int i = 0; i < triggerTouchList[client].Length; i++)
{
TouchedTrigger touched;
triggerTouchList[client].GetArray(i, touched);
// set the touched tick to the tick that the player touches the ground.
touched.groundTouchTick = gI_TickCount[client];
triggerTouchList[client].SetArray(i, touched);
}
}
void OnStopTouchGround_MapTriggers(int client)
{
for (int i = 0; i < triggerTouchList[client].Length; i++)
{
TouchedTrigger touched;
triggerTouchList[client].GetArray(i, touched);
if (touched.triggerType == TriggerType_Teleport)
{
char key[32];
GetEntityHammerIDString(touched.entRef, key, sizeof(key));
TeleportTrigger trigger;
// set last touched triggers for single and sequential bhop.
if (teleportTriggers.GetArray(key, trigger, sizeof(trigger))
&& IsBhopTrigger(trigger.type))
{
if (trigger.type == TeleportType_SequentialBhop)
{
lastTouchSequentialBhopEntRefs[client].Push(touched.entRef);
}
// NOTE: For singlebhops, we don't care which type of bhop we last touched, because
// otherwise jumping back and forth between a multibhop and a singlebhop wouldn't work.
if (i == 0 && IsBhopTrigger(trigger.type))
{
// We only want to set this once in this loop.
lastTouchSingleBhopEntRef[client] = touched.entRef;
}
}
}
}
}
void OnChangeMovetype_MapTriggers(int client, MoveType newMovetype)
{
if (newMovetype == MOVETYPE_LADDER)
{
lastTouchGroundOrLadderTime[client] = GetEngineTime();
}
}
// =====[ PRIVATE ]=====
static void AddTriggerToTouchList(int client, int trigger, TriggerType triggerType)
{
int triggerEntRef = EntIndexToEntRef(trigger);
TouchedTrigger touched;
touched.triggerType = triggerType;
touched.entRef = triggerEntRef;
touched.startTouchTick = gI_TickCount[client];
touched.groundTouchTick = -1;
if (GetEntityFlags(client) & FL_ONGROUND)
{
touched.groundTouchTick = gI_TickCount[client];
}
triggerTouchList[client].PushArray(touched);
}
static void RemoveTriggerFromTouchList(int client, int trigger)
{
int triggerEntRef = EntIndexToEntRef(trigger);
for (int i = 0; i < triggerTouchList[client].Length; i++)
{
TouchedTrigger touched;
triggerTouchList[client].GetArray(i, touched);
if (touched.entRef == triggerEntRef)
{
triggerTouchList[client].Erase(i);
break;
}
}
}
static int IncrementTriggerTouchCount(int client, int trigger)
{
int entref = EntIndexToEntRef(trigger);
char szEntref[64];
FormatEx(szEntref, sizeof(szEntref), "%i", entref);
int value = 0;
triggerTouchCounts[client].GetValue(szEntref, value);
value += 1;
triggerTouchCounts[client].SetValue(szEntref, value);
return value;
}
static void DecrementTriggerTouchCount(int client, int trigger)
{
int entref = EntIndexToEntRef(trigger);
char szEntref[64];
FormatEx(szEntref, sizeof(szEntref), "%i", entref);
int value = 0;
triggerTouchCounts[client].GetValue(szEntref, value);
value -= 1;
triggerTouchCounts[client].SetValue(szEntref, value);
}
static void TouchAntibhopTrigger(int client, TouchedTrigger touched, int &newButtons, int flags)
{
if (!(flags & FL_ONGROUND))
{
// Disable jump when the player is in the air.
// This is a very simple way to fix jumpbugging antibhop triggers.
newButtons &= ~IN_JUMP;
return;
}
if (touched.groundTouchTick == -1)
{
// The player hasn't touched the ground inside this trigger yet.
return;
}
char key[32];
GetEntityHammerIDString(touched.entRef, key, sizeof(key));
AntiBhopTrigger trigger;
if (antiBhopTriggers.GetArray(key, trigger, sizeof(trigger)))
{
float touchTime = CalculateGroundTouchTime(client, touched);
if (trigger.time == 0.0 || touchTime <= trigger.time)
{
// disable jump
newButtons &= ~IN_JUMP;
}
}
}
static bool TouchTeleportTrigger(int client, TouchedTrigger touched, int flags)
{
bool shouldTeleport = false;
char key[32];
GetEntityHammerIDString(touched.entRef, key, sizeof(key));
TeleportTrigger trigger;
if (!teleportTriggers.GetArray(key, trigger, sizeof(trigger)))
{
// Couldn't get the teleport trigger from the trigger array for some reason.
return shouldTeleport;
}
bool isBhopTrigger = IsBhopTrigger(trigger.type);
// NOTE: Player hasn't touched the ground inside this trigger yet.
if (touched.groundTouchTick == -1 && isBhopTrigger)
{
return shouldTeleport;
}
float destOrigin[3];
float destAngles[3];
bool gotDestOrigin;
bool gotDestAngles;
int destinationEnt = GetTeleportDestinationAndOrientation(trigger.tpDestination, destOrigin, destAngles, gotDestOrigin, gotDestAngles);
float triggerOrigin[3];
bool gotTriggerOrigin = GetEntityAbsOrigin(touched.entRef, triggerOrigin);
// NOTE: We only use the trigger's origin if we're using a relative destination, so if
// we're not using a relative destination and don't have it, then it's fine.
if (!IsValidEntity(destinationEnt) || !gotDestOrigin
|| (!gotTriggerOrigin && trigger.relativeDestination))
{
PrintToConsole(client, "[KZ] Invalid teleport destination \"%s\" on trigger with hammerID %i.", trigger.tpDestination, trigger.hammerID);
return shouldTeleport;
}
// NOTE: Find out if we should actually teleport.
if (isBhopTrigger && (flags & FL_ONGROUND))
{
float touchTime = CalculateGroundTouchTime(client, touched);
if (touchTime > trigger.delay)
{
shouldTeleport = true;
}
else if (trigger.type == TeleportType_SingleBhop)
{
shouldTeleport = lastTouchSingleBhopEntRef[client] == touched.entRef;
}
else if (trigger.type == TeleportType_SequentialBhop)
{
int length = lastTouchSequentialBhopEntRefs[client].Length;
for (int j = 0; j < length; j++)
{
int entRef = lastTouchSequentialBhopEntRefs[client].Get(j);
if (entRef == touched.entRef)
{
shouldTeleport = true;
break;
}
}
}
}
else if (trigger.type == TeleportType_Normal)
{
float touchTime = CalculateStartTouchTime(client, touched);
shouldTeleport = touchTime > trigger.delay || (trigger.delay == 0.0);
}
if (!shouldTeleport)
{
return shouldTeleport;
}
bool shouldReorientPlayer = trigger.reorientPlayer
&& gotDestAngles && (destAngles[1] != 0.0);
float zAxis[3];
zAxis = view_as<float>({0.0, 0.0, 1.0});
// NOTE: Work out finalOrigin.
float finalOrigin[3];
if (trigger.relativeDestination)
{
float playerOrigin[3];
Movement_GetOrigin(client, playerOrigin);
float playerOffsetFromTrigger[3];
SubtractVectors(playerOrigin, triggerOrigin, playerOffsetFromTrigger);
if (shouldReorientPlayer)
{
// NOTE: rotate player offset by the destination trigger's yaw.
RotateVectorAxis(playerOffsetFromTrigger, zAxis, DegToRad(destAngles[1]), playerOffsetFromTrigger);
}
AddVectors(destOrigin, playerOffsetFromTrigger, finalOrigin);
}
else
{
finalOrigin = destOrigin;
}
// NOTE: Work out finalPlayerAngles.
float finalPlayerAngles[3];
Movement_GetEyeAngles(client, finalPlayerAngles);
if (shouldReorientPlayer)
{
finalPlayerAngles[1] -= destAngles[1];
float velocity[3];
Movement_GetVelocity(client, velocity);
// NOTE: rotate velocity by the destination trigger's yaw.
RotateVectorAxis(velocity, zAxis, DegToRad(destAngles[1]), velocity);
Movement_SetVelocity(client, velocity);
}
else if (!trigger.reorientPlayer && trigger.useDestAngles)
{
finalPlayerAngles = destAngles;
}
if (shouldTeleport)
{
TeleportPlayer(client, finalOrigin, finalPlayerAngles, gotDestAngles && trigger.useDestAngles, trigger.resetSpeed);
}
return shouldTeleport;
}
static float CalculateGroundTouchTime(int client, TouchedTrigger touched)
{
float result = float(gI_TickCount[client] - touched.groundTouchTick) * GetTickInterval();
return result;
}
static float CalculateStartTouchTime(int client, TouchedTrigger touched)
{
float result = float(gI_TickCount[client] - touched.startTouchTick) * GetTickInterval();
return result;
}
static void ResetBhopState(int client)
{
lastTouchSingleBhopEntRef[client] = INVALID_ENT_REFERENCE;
lastTouchSequentialBhopEntRefs[client].Clear();
}
static bool GetEntityHammerIDString(int entity, char[] buffer, int maxLength)
{
if (!IsValidEntity(entity))
{
return false;
}
if (!HasEntProp(entity, Prop_Data, "m_iHammerID"))
{
return false;
}
int hammerID = GetEntProp(entity, Prop_Data, "m_iHammerID");
IntToString(hammerID, buffer, maxLength);
return true;
}
// NOTE: returns an entity reference (possibly invalid).
static int GetTeleportDestinationAndOrientation(char[] targetName, float origin[3], float angles[3] = NULL_VECTOR, bool &gotOrigin = false, bool &gotAngles = false)
{
// NOTE: We're not caching the teleport destination because it could change.
int destination = GOKZFindEntityByName(targetName, .ignorePlayers = true);
if (!IsValidEntity(destination))
{
return destination;
}
gotOrigin = GetEntityAbsOrigin(destination, origin);
if (HasEntProp(destination, Prop_Data, "m_angAbsRotation"))
{
GetEntPropVector(destination, Prop_Data, "m_angAbsRotation", angles);
gotAngles = true;
}
else
{
gotAngles = false;
}
return destination;
}
|