-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbotmimic_fix.sp
2141 lines (1805 loc) · 62.6 KB
/
botmimic_fix.sp
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
/**
* Bot Mimic - Record your movments and have bots playing it back.
* by Peace-Maker
* visit http://wcfan.de
*
* Changelog:
* 2.0 - 22.07.2013: Released rewrite
* 2.0.1 - 01.08.2013: Actually made DHooks an optional dependency.
* 2.1 - 02.10.2014: Added bookmarks and pausing/resuming while recording. Fixed crashes and problems with CS:GO.
*/
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <sdkhooks>
#include <smlib>
#include <botmimic_fix>
#undef REQUIRE_EXTENSIONS
#include <dhooks>
#pragma newdecls required
#define PLUGIN_VERSION "1.0"
#define BM_MAGIC 0xdeadbeef
// New in 0x02: bookmarkCount and bookmarks list
#define BINARY_FORMAT_VERSION 0x02
#define DEFAULT_RECORD_FOLDER "data/botmimic/"
// Flags set in FramInfo.additionalFields to inform, that there's more info afterwards.
#define ADDITIONAL_FIELD_TELEPORTED_ORIGIN (1<<0)
#define ADDITIONAL_FIELD_TELEPORTED_ANGLES (1<<1)
#define ADDITIONAL_FIELD_TELEPORTED_VELOCITY (1<<2)
enum struct FrameInfo {
int playerButtons;
int playerImpulse;
float actualVelocity[3];
float predictedVelocity[3];
float predictedAngles[2]; // Ignore roll
CSWeaponID newWeapon;
int playerSubtype;
int playerSeed;
int additionalFields; // see ADDITIONAL_FIELD_* defines
}
#define AT_ORIGIN 0
#define AT_ANGLES 1
#define AT_VELOCITY 2
#define AT_FLAGS 3
enum struct AdditionalTeleport {
float atOrigin[3];
float atAngles[3];
float atVelocity[3];
int atFlags;
}
enum struct FileHeader {
int FH_binaryFormatVersion;
int FH_recordEndTime;
char FH_recordName[MAX_RECORD_NAME_LENGTH];
int FH_tickCount;
int FH_bookmarkCount;
float FH_initialPosition[3];
float FH_initialAngles[3];
ArrayList FH_bookmarks;
ArrayList FH_frames;
}
enum struct Bookmarks {
int BKM_frame;
int BKM_additionalTeleportTick;
char BKM_name[MAX_BOOKMARK_NAME_LENGTH];
}
// Used to fire the OnPlayerMimicBookmark effciently during playback
enum struct BookmarkWhileMimicing {
int BWM_frame; // The frame this bookmark was saved in
int BWM_index; // The index into the FH_bookmarks array in the fileheader for the corresponding bookmark (to get the name)
}
// Where did he start recording. The bot is teleported to this position on replay.
float g_fInitialPosition[MAXPLAYERS+1][3];
float g_fInitialAngles[MAXPLAYERS+1][3];
// Array of frames
ArrayList g_hRecording[MAXPLAYERS+1];
ArrayList g_hRecordingAdditionalTeleport[MAXPLAYERS+1];
ArrayList g_hRecordingBookmarks[MAXPLAYERS+1];
int g_iCurrentAdditionalTeleportIndex[MAXPLAYERS+1];
// Is the recording currently paused?
bool g_bRecordingPaused[MAXPLAYERS+1];
bool g_bMimicingPaused[MAXPLAYERS+1];
bool g_bSaveFullSnapshot[MAXPLAYERS+1];
// How many calls to OnPlayerRunCmd were recorded?
int g_iRecordedTicks[MAXPLAYERS+1];
// What's the last active weapon
int g_iRecordPreviousWeapon[MAXPLAYERS+1];
// Count ticks till we save the position again
int g_iOriginSnapshotInterval[MAXPLAYERS+1];
// The name of this recording
char g_sRecordName[MAXPLAYERS+1][MAX_RECORD_NAME_LENGTH];
char g_sRecordPath[MAXPLAYERS+1][PLATFORM_MAX_PATH];
char g_sRecordCategory[MAXPLAYERS+1][PLATFORM_MAX_PATH];
char g_sRecordSubDir[MAXPLAYERS+1][PLATFORM_MAX_PATH];
bool g_bForceMove[MAXPLAYERS+1]; // ignore pause once
#define WEAPONS_SLOTS_MAX 5
int g_iWeaponCache[MAXPLAYERS+1][WEAPONS_SLOTS_MAX];
StringMap g_hLoadedRecords;
StringMap g_hLoadedRecordsAdditionalTeleport;
StringMap g_hLoadedRecordsCategory;
ArrayList g_hSortedRecordList;
ArrayList g_hSortedCategoryList;
ArrayList g_hBotMimicsRecord[MAXPLAYERS+1] = {null,...};
int g_iBotMimicTick[MAXPLAYERS+1] = {0,...};
int g_iBotMimicRecordTickCount[MAXPLAYERS+1] = {0,...};
int g_iBotActiveWeapon[MAXPLAYERS+1] = {-1,...};
char g_BotActiveWeaponName[MAXPLAYERS+1][32];
bool g_bBotSwitchedWeapon[MAXPLAYERS+1];
bool g_bValidTeleportCall[MAXPLAYERS+1];
BookmarkWhileMimicing g_iBotMimicNextBookmarkTick[MAXPLAYERS+1];
Handle g_hfwdOnStartRecording;
Handle g_hfwdOnRecordingPauseStateChanged;
Handle g_hfwdOnRecordingBookmarkSaved;
Handle g_hfwdOnStopRecording;
Handle g_hfwdOnRecordSaved;
Handle g_hfwdOnRecordDeleted;
Handle g_hfwdOnPlayerStartsMimicing;
Handle g_hfwdOnPlayerStopsMimicing;
Handle g_hfwdOnPlayerMimicLoops;
Handle g_hfwdOnPlayerMimicBookmark;
// DHooks
Handle g_hTeleport;
ConVar g_hCVOriginSnapshotInterval;
ConVar g_hCVRespawnOnDeath;
public Plugin myinfo =
{
name = "Bot Mimic-CSGOWiki",
author = "CarOL(based on Jannik \"Peace-Maker\" Hartung)",
description = "Bots mimic your movements!",
version = PLUGIN_VERSION,
url = "https://github.com/hx-w/simple-practicemode"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
RegPluginLibrary("botmimic_fix");
CreateNative("BotMimicFix_StartRecording", StartRecording);
CreateNative("BotMimicFix_PauseRecording", PauseRecording);
CreateNative("BotMimicFix_ResumeRecording", ResumeRecording);
CreateNative("BotMimicFix_IsRecordingPaused", IsRecordingPaused);
CreateNative("BotMimicFix_StopRecording", StopRecording);
CreateNative("BotMimicFix_SaveBookmark", SaveBookmark);
CreateNative("BotMimicFix_DeleteRecord", DeleteRecord);
CreateNative("BotMimicFix_IsPlayerRecording", IsPlayerRecording);
CreateNative("BotMimicFix_IsPlayerMimicing", IsPlayerMimicing);
CreateNative("BotMimicFix_GetRecordPlayerMimics", GetRecordPlayerMimics);
CreateNative("BotMimicFix_PlayRecordFromFile", PlayRecordFromFile);
CreateNative("BotMimicFix_PlayRecordByName", PlayRecordByName);
CreateNative("BotMimicFix_ResetPlayback", ResetPlayback);
CreateNative("BotMimicFix_GoToBookmark", GoToBookmark);
CreateNative("BotMimicFix_StopPlayerMimic", StopPlayerMimic);
CreateNative("BotMimicFix_GetFileHeaders", GetFileHeaders);
CreateNative("BotMimicFix_ChangeRecordName", ChangeRecordName);
CreateNative("BotMimicFix_GetLoadedRecordCategoryList", GetLoadedRecordCategoryList);
CreateNative("BotMimicFix_GetLoadedRecordList", GetLoadedRecordList);
CreateNative("BotMimicFix_GetFileCategory", GetFileCategory);
CreateNative("BotMimicFix_GetRecordBookmarks", GetRecordBookmarks);
CreateNative("BotMimicFix_FastForwardPlayback", FastForwardPlayback);
CreateNative("BotMimicFix_RewindPlayback", RewindPlayback);
CreateNative("BotMimicFix_GetPlaybackPercentage", GetPlaybackPercentage);
CreateNative("BotMimicFix_PauseMimicing", PauseMimicing);
CreateNative("BotMimicFix_ResumeMimicing", ResumeMimicing);
g_hfwdOnStartRecording = CreateGlobalForward("BotMimicFix_OnStartRecording", ET_Hook, Param_Cell, Param_String, Param_String, Param_String, Param_String);
g_hfwdOnRecordingPauseStateChanged = CreateGlobalForward("BotMimicFix_OnRecordingPauseStateChanged", ET_Ignore, Param_Cell, Param_Cell);
g_hfwdOnRecordingBookmarkSaved = CreateGlobalForward("BotMimicFix_OnRecordingBookmarkSaved", ET_Ignore, Param_Cell, Param_String);
g_hfwdOnStopRecording = CreateGlobalForward("BotMimicFix_OnStopRecording", ET_Hook, Param_Cell, Param_String, Param_String, Param_String, Param_String, Param_CellByRef);
g_hfwdOnRecordSaved = CreateGlobalForward("BotMimicFix_OnRecordSaved", ET_Ignore, Param_Cell, Param_String, Param_String, Param_String, Param_String);
g_hfwdOnRecordDeleted = CreateGlobalForward("BotMimicFix_OnRecordDeleted", ET_Ignore, Param_String, Param_String, Param_String);
g_hfwdOnPlayerStartsMimicing = CreateGlobalForward("BotMimicFix_OnPlayerStartsMimicing", ET_Hook, Param_Cell, Param_String, Param_String, Param_String);
g_hfwdOnPlayerStopsMimicing = CreateGlobalForward("BotMimicFix_OnPlayerStopsMimicing", ET_Ignore, Param_Cell, Param_String, Param_String, Param_String);
g_hfwdOnPlayerMimicLoops = CreateGlobalForward("BotMimicFix_OnPlayerMimicLoops", ET_Ignore, Param_Cell);
g_hfwdOnPlayerMimicBookmark = CreateGlobalForward("BotMimicFix_OnPlayerMimicBookmark", ET_Ignore, Param_Cell, Param_String);
}
public void OnPluginStart()
{
ConVar hVersion = CreateConVar("sm_botmimic_version", PLUGIN_VERSION, "Bot Mimic version", FCVAR_NOTIFY|FCVAR_DONTRECORD);
if(hVersion != null)
{
hVersion.SetString(PLUGIN_VERSION);
hVersion.AddChangeHook(ConVar_VersionChanged);
}
// Save the position of clients every 10000 ticks
// This is to avoid bots getting stuck in walls due to slightly lower jumps, if they don't touch the ground.
g_hCVOriginSnapshotInterval = CreateConVar("sm_botmimic_snapshotinterval", "10000", "Save the position of clients every x ticks. This is to avoid bots getting stuck in walls during a long playback and lots of jumps.", _, true, 0.0);
g_hCVRespawnOnDeath = CreateConVar("sm_botmimic_respawnondeath", "1", "Respawn the bot when he dies during playback?", _, true, 0.0, true, 1.0);
AutoExecConfig();
// Maps path to .rec -> record enum
g_hLoadedRecords = new StringMap();
g_hLoadedRecordsAdditionalTeleport = new StringMap();
// Maps path to .rec -> record category
g_hLoadedRecordsCategory = new StringMap();
// Save all paths to .rec files in the trie sorted by time
g_hSortedRecordList = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
g_hSortedCategoryList = new ArrayList(ByteCountToCells(64));
HookEvent("player_spawn", Event_OnPlayerSpawn);
HookEvent("player_death", Event_OnPlayerDeath);
if(LibraryExists("dhooks"))
{
OnLibraryAdded("dhooks");
}
}
public void ConVar_VersionChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
convar.SetString(PLUGIN_VERSION);
}
/**
* Public forwards
*/
public void OnLibraryAdded(const char[] name)
{
if(StrEqual(name, "dhooks") && g_hTeleport == null)
{
// Optionally setup a hook on CBaseEntity::Teleport to keep track of sudden place changes
Handle hGameData = LoadGameConfigFile("sdktools.games");
if(hGameData == null)
return;
int iOffset = GameConfGetOffset(hGameData, "Teleport");
delete hGameData;
if(iOffset == -1)
return;
g_hTeleport = DHookCreate(iOffset, HookType_Entity, ReturnType_Void, ThisPointer_CBaseEntity, DHooks_OnTeleport);
if(g_hTeleport == null)
return;
DHookAddParam(g_hTeleport, HookParamType_VectorPtr);
DHookAddParam(g_hTeleport, HookParamType_ObjectPtr);
DHookAddParam(g_hTeleport, HookParamType_VectorPtr);
if(GetEngineVersion() == Engine_CSGO)
DHookAddParam(g_hTeleport, HookParamType_Bool);
for(int i=1;i<=MaxClients;i++)
{
if(IsClientInGame(i))
OnClientPutInServer(i);
}
}
}
public void OnLibraryRemoved(const char[] name)
{
if(StrEqual(name, "dhooks"))
{
g_hTeleport = null;
}
}
public void OnMapStart()
{
// Clear old records for old map
int iSize = g_hSortedRecordList.Length;
char sPath[PLATFORM_MAX_PATH];
FileHeader iFileHeader;
Handle hAdditionalTeleport;
for(int i=0;i<iSize;i++)
{
g_hSortedRecordList.GetString(i, sPath, sizeof(sPath));
if (!g_hLoadedRecords.GetArray(sPath, iFileHeader, sizeof(FileHeader)))
{
LogError("Internal state error. %s was in the sorted list, but not in the actual storage.", sPath);
continue;
}
if(iFileHeader.FH_frames != null)
delete iFileHeader.FH_frames;
if(iFileHeader.FH_bookmarks != null)
delete iFileHeader.FH_bookmarks;
if(g_hLoadedRecordsAdditionalTeleport.GetValue(sPath, hAdditionalTeleport))
delete hAdditionalTeleport;
}
g_hLoadedRecords.Clear();
g_hLoadedRecordsAdditionalTeleport.Clear();
g_hLoadedRecordsCategory.Clear();
g_hSortedRecordList.Clear();
g_hSortedCategoryList.Clear();
// Create our record directory
BuildPath(Path_SM, sPath, sizeof(sPath), DEFAULT_RECORD_FOLDER);
if(!DirExists(sPath))
CreateDirectory(sPath, 511);
// Check for categories
DirectoryListing hDir = OpenDirectory(sPath);
if(hDir == null)
return;
char sFile[64];
FileType fileType;
while(hDir.GetNext(sFile, sizeof(sFile), fileType))
{
switch(fileType)
{
// Check all directories for records on this map
case FileType_Directory:
{
// INFINITE RECURSION ANYONE?
if(StrEqual(sFile, ".") || StrEqual(sFile, ".."))
continue;
BuildPath(Path_SM, sPath, sizeof(sPath), "%s%s", DEFAULT_RECORD_FOLDER, sFile);
ParseRecordsInDirectory(sPath, sFile, false);
}
}
}
delete hDir;
}
public void OnClientPutInServer(int client)
{
if(g_hTeleport != null)
DHookEntity(g_hTeleport, false, client);
}
public void OnClientDisconnect(int client)
{
if(g_hRecording[client] != null)
BotMimicFix_StopRecording(client);
if(g_hBotMimicsRecord[client] != null)
BotMimicFix_StopPlayerMimic(client);
}
public void OnPlayerRunCmdPost(int client, int buttons, int impulse, const float vel[3], const float angles[3], int weapon, int subtype, int cmdnum, int tickcount, int seed, const int mouse[2])
{
// Client isn't recording or recording is paused.
if(g_hRecording[client] == null || g_bRecordingPaused[client])
return;
FrameInfo iFrame;
iFrame.playerButtons = buttons;
iFrame.playerImpulse = impulse;
float vVel[3];
Entity_GetAbsVelocity(client, vVel);
iFrame.actualVelocity = vVel;
iFrame.predictedVelocity = vel;
Array_Copy(angles, iFrame.predictedAngles, 2);
iFrame.newWeapon = CSWeapon_NONE;
iFrame.playerSubtype = subtype;
iFrame.playerSeed = seed;
// Save the origin, angles and velocity in this frame.
if(g_bSaveFullSnapshot[client])
{
AdditionalTeleport iAT;
float fBuffer[3];
GetClientAbsOrigin(client, fBuffer);
Array_Copy(fBuffer, iAT.atOrigin, 3);
GetClientEyeAngles(client, fBuffer);
Array_Copy(fBuffer, iAT.atAngles, 3);
Entity_GetAbsVelocity(client, fBuffer);
Array_Copy(fBuffer, iAT.atVelocity, 3);
iAT.atFlags = ADDITIONAL_FIELD_TELEPORTED_ORIGIN|ADDITIONAL_FIELD_TELEPORTED_ANGLES|ADDITIONAL_FIELD_TELEPORTED_VELOCITY;
g_hRecordingAdditionalTeleport[client].PushArray(iAT, sizeof(AdditionalTeleport));
g_bSaveFullSnapshot[client] = false;
}
else
{
// Save the current position
int iInterval = g_hCVOriginSnapshotInterval.IntValue;
if(iInterval > 0 && g_iOriginSnapshotInterval[client] > iInterval)
{
AdditionalTeleport iAT;
float origin[3];
GetClientAbsOrigin(client, origin);
Array_Copy(origin, iAT.atOrigin, 3);
iAT.atFlags |= ADDITIONAL_FIELD_TELEPORTED_ORIGIN;
g_hRecordingAdditionalTeleport[client].PushArray(iAT, sizeof(AdditionalTeleport));
g_iOriginSnapshotInterval[client] = 0;
}
}
g_iOriginSnapshotInterval[client]++;
// Check for additional Teleports
if(g_hRecordingAdditionalTeleport[client].Length > g_iCurrentAdditionalTeleportIndex[client])
{
AdditionalTeleport iAT;
g_hRecordingAdditionalTeleport[client].GetArray(g_iCurrentAdditionalTeleportIndex[client], iAT, sizeof(AdditionalTeleport));
// Remember, we were teleported this frame!
iFrame.additionalFields |= iAT.atFlags;
g_iCurrentAdditionalTeleportIndex[client]++;
}
int iNewWeapon = -1;
// Did he change his weapon?
if(weapon)
{
iNewWeapon = weapon;
}
// Picked up a new one?
else
{
int iWeapon = Client_GetActiveWeapon(client);
// He's holding a weapon and
if(iWeapon != -1 &&
// we just started recording. Always save the first weapon!
(g_iRecordedTicks[client] == 0 ||
// This is a new weapon, he didn't held before.
g_iRecordPreviousWeapon[client] != iWeapon))
{
iNewWeapon = iWeapon;
}
}
if(iNewWeapon != -1)
{
// Save it
if(IsValidEntity(iNewWeapon) && IsValidEdict(iNewWeapon))
{
g_iRecordPreviousWeapon[client] = iNewWeapon;
char sClassName[64];
GetEdictClassname(iNewWeapon, sClassName, sizeof(sClassName));
ReplaceString(sClassName, sizeof(sClassName), "weapon_", "", false);
char sWeaponAlias[64];
CS_GetTranslatedWeaponAlias(sClassName, sWeaponAlias, sizeof(sWeaponAlias));
CSWeaponID weaponId = CS_AliasToWeaponID(sWeaponAlias);
iFrame.newWeapon = weaponId;
}
}
g_hRecording[client].PushArray(iFrame, sizeof(FrameInfo));
g_iRecordedTicks[client]++;
}
public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon, int &subtype, int &cmdnum, int &tickcount, int &seed, int mouse[2])
{
// Bot is mimicing something
if (g_hBotMimicsRecord[client] == null)
return Plugin_Continue;
// Is this a valid living bot?
if (!IsPlayerAlive(client) || GetClientTeam(client) < CS_TEAM_T)
return Plugin_Continue;
if (g_iBotMimicTick[client] >= g_iBotMimicRecordTickCount[client])
{
PrintCenterText(client, "<font color='#87CEFA'>回放结束");
g_iBotMimicTick[client] = 0;
g_iCurrentAdditionalTeleportIndex[client] = 0;
BotMimicFix_StopPlayerMimic(client); // for once
return Plugin_Continue;
}
if (g_bMimicingPaused[client] && !g_bForceMove[client]) {
buttons = 0;
angles[0] = 0.0;
angles[1] = 0.0;
angles[2] = 0.0;
return Plugin_Continue;
}
// float percent = 100 * float(g_iBotMimicTick[client]) / g_iBotMimicRecordTickCount[client];
// PrintCenterText(client, "回放进度:<font color='#0CED26'>%.1f<font color='#ffffff'>\%", percent);
FrameInfo iFrame;
g_hBotMimicsRecord[client].GetArray(g_iBotMimicTick[client], iFrame, sizeof(FrameInfo));
if (!g_bBotSwitchedWeapon[client]) {
buttons = iFrame.playerButtons;
impulse = iFrame.playerImpulse;
}
Array_Copy(iFrame.predictedVelocity, vel, 3);
Array_Copy(iFrame.predictedAngles, angles, 2);
subtype = iFrame.playerSubtype;
seed = iFrame.playerSeed;
weapon = 0;
float fActualVelocity[3];
Array_Copy(iFrame.actualVelocity, fActualVelocity, 3);
// We're supposed to teleport stuff?
if(iFrame.additionalFields & (ADDITIONAL_FIELD_TELEPORTED_ORIGIN|ADDITIONAL_FIELD_TELEPORTED_ANGLES|ADDITIONAL_FIELD_TELEPORTED_VELOCITY))
{
g_bForceMove[client] = false;
AdditionalTeleport iAT;
ArrayList hAdditionalTeleport;
char sPath[PLATFORM_MAX_PATH];
GetFileFromFrameHandle(g_hBotMimicsRecord[client], sPath, sizeof(sPath));
g_hLoadedRecordsAdditionalTeleport.GetValue(sPath, hAdditionalTeleport);
hAdditionalTeleport.GetArray(g_iCurrentAdditionalTeleportIndex[client], iAT, sizeof(AdditionalTeleport));
float fOrigin[3], fAngles[3], fVelocity[3];
Array_Copy(iAT.atOrigin, fOrigin, 3);
Array_Copy(iAT.atAngles, fAngles, 3);
Array_Copy(iAT.atVelocity, fVelocity, 3);
// The next call to Teleport is ok.
g_bValidTeleportCall[client] = true;
// THATS STUPID!
// Only pass the arguments, if they were set..
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_ORIGIN)
{
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_ANGLES)
{
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_VELOCITY)
TeleportEntity(client, fOrigin, fAngles, fVelocity);
else
TeleportEntity(client, fOrigin, fAngles, NULL_VECTOR);
}
else
{
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_VELOCITY)
TeleportEntity(client, fOrigin, NULL_VECTOR, fVelocity);
else
TeleportEntity(client, fOrigin, NULL_VECTOR, NULL_VECTOR);
}
}
else
{
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_ANGLES)
{
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_VELOCITY)
TeleportEntity(client, NULL_VECTOR, fAngles, fVelocity);
else
TeleportEntity(client, NULL_VECTOR, fAngles, NULL_VECTOR);
}
else
{
if(iAT.atFlags & ADDITIONAL_FIELD_TELEPORTED_VELOCITY)
TeleportEntity(client, NULL_VECTOR, NULL_VECTOR, fVelocity);
}
}
g_iCurrentAdditionalTeleportIndex[client]++;
}
// This is the first tick. Teleport him to the initial position
if(g_iBotMimicTick[client] == 0)
{
g_bValidTeleportCall[client] = true;
TeleportEntity(client, g_fInitialPosition[client], g_fInitialAngles[client], fActualVelocity);
// Client_RemoveAllWeapons(client);
Call_StartForward(g_hfwdOnPlayerMimicLoops);
Call_PushCell(client);
Call_Finish();
}
else
{
g_bValidTeleportCall[client] = true;
TeleportEntity(client, NULL_VECTOR, angles, fActualVelocity);
}
if (g_bBotSwitchedWeapon[client]) {
FakeClientCommand(client, "use %s", g_BotActiveWeaponName[client]);
g_bBotSwitchedWeapon[client] = false;
}
if(iFrame.newWeapon != CSWeapon_NONE)
{
char sAlias[64];
CS_WeaponIDToAlias(iFrame.newWeapon, sAlias, sizeof(sAlias));
Format(sAlias, sizeof(sAlias), "weapon_%s", sAlias);
strcopy(g_BotActiveWeaponName[client], sizeof(g_BotActiveWeaponName), sAlias);
g_bBotSwitchedWeapon[client] = true;
// CreateTimer(0.1, SwitchWeaponTimer, client);
if (g_iBotMimicTick[client] == 0 || !HasWeapon(client, sAlias)) {
char weaponCmd[32];
Format(weaponCmd, sizeof(weaponCmd), "give %s", sAlias);
FakeClientCommand(client, weaponCmd);
}
}
// See if there's a bookmark on this tick
if(g_iBotMimicTick[client] == g_iBotMimicNextBookmarkTick[client].BWM_frame)
{
// Get the file header of the current playing record.
char sPath[PLATFORM_MAX_PATH];
GetFileFromFrameHandle(g_hBotMimicsRecord[client], sPath, sizeof(sPath));
FileHeader iFileHeader;
g_hLoadedRecords.GetArray(sPath, iFileHeader, sizeof(FileHeader));
Bookmarks iBookmark;
iFileHeader.FH_bookmarks.GetArray(g_iBotMimicNextBookmarkTick[client].BWM_index, iBookmark, sizeof(Bookmarks));
// Cache the next tick in which we should fire the forward.
UpdateNextBookmarkTick(client);
// Call the forward
Call_StartForward(g_hfwdOnPlayerMimicBookmark);
Call_PushCell(client);
Call_PushString(iBookmark.BKM_name);
Call_Finish();
}
g_iBotMimicTick[client]++;
return Plugin_Changed;
}
// public Action SwitchWeaponTimer(Handle timer, int client) {
// FakeClientCommand(client, "use %s", g_BotActiveWeaponName[client]);
// g_bBotSwitchedWeapon[client] = false;
// return Plugin_Handled;
// }
/**
* Event Callbacks
*/
public void Event_OnPlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if(!client)
return;
// Restart moving on spawn!
if(g_hBotMimicsRecord[client] != null)
{
g_iBotMimicTick[client] = 0;
g_iCurrentAdditionalTeleportIndex[client] = 0;
}
}
public void Event_OnPlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if(!client)
return;
// This one has been recording currently
if(g_hRecording[client] != null)
{
BotMimicFix_StopRecording(client, true);
}
// This bot has been playing one
else if(g_hBotMimicsRecord[client] != null)
{
// Respawn the bot after death!
g_iBotMimicTick[client] = 0;
g_iCurrentAdditionalTeleportIndex[client] = 0;
if(g_hCVRespawnOnDeath.BoolValue && GetClientTeam(client) >= CS_TEAM_T)
BotMimicFix_StopPlayerMimic(client);
// CreateTimer(1.0, Timer_DelayedRespawn, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
}
/**
* Timer Callbacks
*/
// public Action Timer_DelayedRespawn(Handle timer, any userid)
// {
// int client = GetClientOfUserId(userid);
// if(!client)
// return Plugin_Stop;
// if(g_hBotMimicsRecord[client] != null && IsClientInGame(client) && !IsPlayerAlive(client) && IsFakeClient(client) && GetClientTeam(client) >= CS_TEAM_T)
// CS_RespawnPlayer(client);
// return Plugin_Stop;
// }
/**
* SDKHooks Callbacks
*/
// Don't allow mimicing players any other weapon than the one recorded!!
public Action Hook_WeaponCanSwitchTo(int client, int weapon)
{
if(g_hBotMimicsRecord[client] == null)
return Plugin_Continue;
if(g_iBotActiveWeapon[client] != weapon)
{
return Plugin_Stop;
}
return Plugin_Continue;
}
/**
* DHooks Callbacks
*/
public MRESReturn DHooks_OnTeleport(int client, Handle hParams)
{
// This one is currently mimicing something.
if(g_hBotMimicsRecord[client] != null)
{
// We didn't allow that teleporting. STOP THAT.
if(!g_bValidTeleportCall[client])
return MRES_Supercede;
g_bValidTeleportCall[client] = false;
return MRES_Ignored;
}
// Don't care if he's not recording.
if(g_hRecording[client] == null)
return MRES_Ignored;
float origin[3], angles[3], velocity[3];
bool bOriginNull = DHookIsNullParam(hParams, 1);
bool bAnglesNull = DHookIsNullParam(hParams, 2);
bool bVelocityNull = DHookIsNullParam(hParams, 3);
if(!bOriginNull)
DHookGetParamVector(hParams, 1, origin);
if(!bAnglesNull)
{
for(int i=0;i<3;i++)
angles[i] = DHookGetParamObjectPtrVar(hParams, 2, i*4, ObjectValueType_Float);
}
if(!bVelocityNull)
DHookGetParamVector(hParams, 3, velocity);
if(bOriginNull && bAnglesNull && bVelocityNull)
return MRES_Ignored;
AdditionalTeleport iAT;
Array_Copy(origin, iAT.atOrigin, 3);
Array_Copy(angles, iAT.atAngles, 3);
Array_Copy(velocity, iAT.atVelocity, 3);
// Remember,
if(!bOriginNull)
iAT.atFlags |= ADDITIONAL_FIELD_TELEPORTED_ORIGIN;
if(!bAnglesNull)
iAT.atFlags |= ADDITIONAL_FIELD_TELEPORTED_ANGLES;
if(!bVelocityNull)
iAT.atFlags |= ADDITIONAL_FIELD_TELEPORTED_VELOCITY;
g_hRecordingAdditionalTeleport[client].PushArray(iAT, sizeof(AdditionalTeleport));
return MRES_Ignored;
}
/**
* Natives
*/
public int StartRecording(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if(client < 1 || client > MaxClients || !IsClientInGame(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Bad player index %d", client);
return;
}
if(g_hRecording[client] != null)
{
ThrowNativeError(SP_ERROR_NATIVE, "Player is already recording.");
return;
}
if(g_hBotMimicsRecord[client] != null)
{
ThrowNativeError(SP_ERROR_NATIVE, "Player is currently mimicing another record.");
return;
}
g_hRecording[client] = new ArrayList(sizeof(FrameInfo));
g_hRecordingAdditionalTeleport[client] = new ArrayList(sizeof(AdditionalTeleport));
g_hRecordingBookmarks[client] = new ArrayList(sizeof(Bookmarks));
GetClientAbsOrigin(client, g_fInitialPosition[client]);
GetClientEyeAngles(client, g_fInitialAngles[client]);
g_iRecordedTicks[client] = 0;
g_iOriginSnapshotInterval[client] = 0;
GetNativeString(2, g_sRecordName[client], MAX_RECORD_NAME_LENGTH);
GetNativeString(3, g_sRecordCategory[client], PLATFORM_MAX_PATH);
GetNativeString(4, g_sRecordSubDir[client], PLATFORM_MAX_PATH);
if(g_sRecordCategory[client][0] == '\0')
strcopy(g_sRecordCategory[client], sizeof(g_sRecordCategory[]), DEFAULT_CATEGORY);
// Path:
// data/botmimic/%CATEGORY%/map_name/%SUBDIR%/record.rec
// subdir can be omitted, default category is "default"
// All demos reside in the default path (data/botmimic)
BuildPath(Path_SM, g_sRecordPath[client], PLATFORM_MAX_PATH, "%s%s", DEFAULT_RECORD_FOLDER, g_sRecordCategory[client]);
// Remove trailing slashes
if(g_sRecordPath[client][strlen(g_sRecordPath[client])-1] == '\\' ||
g_sRecordPath[client][strlen(g_sRecordPath[client])-1] == '/')
g_sRecordPath[client][strlen(g_sRecordPath[client])-1] = '\0';
Action result;
Call_StartForward(g_hfwdOnStartRecording);
Call_PushCell(client);
Call_PushString(g_sRecordName[client]);
Call_PushString(g_sRecordCategory[client]);
Call_PushString(g_sRecordSubDir[client]);
Call_PushString(g_sRecordPath[client]);
Call_Finish(result);
if(result >= Plugin_Handled)
BotMimicFix_StopRecording(client, false);
}
public int PauseRecording(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if(client < 1 || client > MaxClients || !IsClientInGame(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Bad player index %d", client);
return;
}
if(g_hRecording[client] == null)
{
ThrowNativeError(SP_ERROR_NATIVE, "Player is not recording.");
return;
}
if(g_bRecordingPaused[client])
{
ThrowNativeError(SP_ERROR_NATIVE, "Recording is already paused.");
return;
}
g_bRecordingPaused[client] = true;
Call_StartForward(g_hfwdOnRecordingPauseStateChanged);
Call_PushCell(client);
Call_PushCell(true);
Call_Finish();
}
public int ResumeRecording(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if(client < 1 || client > MaxClients || !IsClientInGame(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Bad player index %d", client);
return;
}
if(g_hRecording[client] == null)
{
ThrowNativeError(SP_ERROR_NATIVE, "Player is not recording.");
return;
}
if(!g_bRecordingPaused[client])
{
ThrowNativeError(SP_ERROR_NATIVE, "Recording is not paused.");
return;
}
// Save the new full position, angles and velocity.
g_bSaveFullSnapshot[client] = true;
g_bRecordingPaused[client] = false;
Call_StartForward(g_hfwdOnRecordingPauseStateChanged);
Call_PushCell(client);
Call_PushCell(false);
Call_Finish();
}
public int IsRecordingPaused(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if(client < 1 || client > MaxClients || !IsClientInGame(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Bad player index %d", client);
return false;
}
if(g_hRecording[client] == null)
{
ThrowNativeError(SP_ERROR_NATIVE, "Player is not recording.");
return false;
}
return g_bRecordingPaused[client];
}
public int StopRecording(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
if(client < 1 || client > MaxClients || !IsClientInGame(client))
{
ThrowNativeError(SP_ERROR_NATIVE, "Bad player index %d", client);
return;
}
// Not recording..
if(g_hRecording[client] == null)
{
ThrowNativeError(SP_ERROR_NATIVE, "Player is not recording.");
return;
}
bool save = GetNativeCell(2);
Action result;
Call_StartForward(g_hfwdOnStopRecording);
Call_PushCell(client);
Call_PushString(g_sRecordName[client]);
Call_PushString(g_sRecordCategory[client]);
Call_PushString(g_sRecordSubDir[client]);
Call_PushString(g_sRecordPath[client]);
Call_PushCellRef(save);
Call_Finish(result);
// Don't stop recording?
if(result >= Plugin_Handled)
return;
if(save)
{
int iEndTime = GetTime();
char sMapName[64], sPath[PLATFORM_MAX_PATH];
GetCurrentMap(sMapName, sizeof(sMapName));
// Check if the default record folder exists?
BuildPath(Path_SM, sPath, sizeof(sPath), DEFAULT_RECORD_FOLDER);
// Remove trailing slashes
if(sPath[strlen(sPath)-1] == '\\' || sPath[strlen(sPath)-1] == '/')
sPath[strlen(sPath)-1] = '\0';
if(!CheckCreateDirectory(sPath, 511))
return;
// Check if the category folder exists?
BuildPath(Path_SM, sPath, sizeof(sPath), "%s%s", DEFAULT_RECORD_FOLDER, g_sRecordCategory[client]);
if(!CheckCreateDirectory(sPath, 511))
return;
// Check, if there is a folder for this map already
Format(sPath, sizeof(sPath), "%s/%s", g_sRecordPath[client], sMapName);
if(!CheckCreateDirectory(sPath, 511))
return;
// Check if the subdirectory exists
if(g_sRecordSubDir[client][0] != '\0')
{
Format(sPath, sizeof(sPath), "%s/%s", sPath, g_sRecordSubDir[client]);
if(!CheckCreateDirectory(sPath, 511))
return;
}
char filename[16];
GetNativeString(3, filename, sizeof(filename));
if (strlen(filename)) {
Format(sPath, sizeof(sPath), "%s/%s.rec", sPath, filename);
}
else {
Format(sPath, sizeof(sPath), "%s/%d.rec", sPath, iEndTime);
}
// Add to our loaded record list
FileHeader iHeader;
iHeader.FH_binaryFormatVersion = BINARY_FORMAT_VERSION;
iHeader.FH_recordEndTime = iEndTime;
iHeader.FH_tickCount = g_hRecording[client].Length;
strcopy(iHeader.FH_recordName, MAX_RECORD_NAME_LENGTH, g_sRecordName[client]);
Array_Copy(g_fInitialPosition[client], iHeader.FH_initialPosition, 3);
Array_Copy(g_fInitialAngles[client], iHeader.FH_initialAngles, 3);
iHeader.FH_frames = g_hRecording[client];
if (g_hRecordingBookmarks[client].Length > 0)
{