-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal-tsurf-replay-viewer.sp
338 lines (267 loc) · 9.59 KB
/
local-tsurf-replay-viewer.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
//local tricksurf replay viewer
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#define MAX_REPLAY_LENGTH 100000
#define TICK_RATE 100.0
#define REPLAY_DIRECTORY "data/trickreplay"
enum struct ReplayData {
float origin[3];
float angles[2];
int buttons;
}
ReplayData g_ReplayData[MAX_REPLAY_LENGTH];
int g_ReplayLength = 0;
int g_ReplayIndex = 0;
bool g_IsPlayingReplay = false;
bool g_IsPaused = false;
int g_ReplayBot = -1;
float g_LastReplayTime = 0.0;
public void OnPluginStart() {
RegConsoleCmd("sm_loadreplay", Command_LoadReplay);
RegConsoleCmd("sm_startreplay", Command_StartReplay);
RegConsoleCmd("sm_stopreplay", Command_StopReplay);
RegConsoleCmd("sm_pausereplay", Command_PauseReplay);
RegConsoleCmd("sm_resumereplay", Command_ResumeReplay);
RegConsoleCmd("sm_resetreplay", Command_ResetReplay);
RegConsoleCmd("sm_listreplays", Command_ListReplays);
// Disable bot quota
SetConVarInt(FindConVar("bot_quota"), 0);
SetConVarInt(FindConVar("bot_join_after_player"), 0);
// Disable team balancing
SetConVarInt(FindConVar("mp_autoteambalance"), 0);
SetConVarInt(FindConVar("mp_limitteams"), 0);
// Create replay directory if it doesn't exist
char replayDir[PLATFORM_MAX_PATH];
BuildPath(Path_SM, replayDir, sizeof(replayDir), REPLAY_DIRECTORY);
if (!DirectoryExists(replayDir)) {
if (!CreateDirectory(replayDir, 511)) {
LogError("Failed to create directory: %s", replayDir);
}
}
}
public void OnMapStart() {
// Precache bot model
PrecacheModel("models/player/ct_urban.mdl", true);
}
public Action Command_LoadReplay(int client, int args) {
if (args < 1) {
ReplyToCommand(client, "Usage: sm_loadreplay <file>");
return Plugin_Handled;
}
char replayFile[PLATFORM_MAX_PATH];
GetCmdArg(1, replayFile, sizeof(replayFile));
// Check for potential buffer overflow
if (strlen(replayFile) >= PLATFORM_MAX_PATH - strlen(REPLAY_DIRECTORY) - 2) {
ReplyToCommand(client, "Replay file name is too long.");
return Plugin_Handled;
}
char filePath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, filePath, sizeof(filePath), "%s/%s", REPLAY_DIRECTORY, replayFile);
if (!LoadReplayFile(filePath)) {
ReplyToCommand(client, "Failed to load replay file: %s", replayFile);
LogError("Failed to load replay file: %s", filePath);
return Plugin_Handled;
}
ReplyToCommand(client, "Replay file loaded successfully. Length: %d", g_ReplayLength);
return Plugin_Handled;
}
public Action Command_StartReplay(int client, int args) {
if (g_ReplayLength == 0) {
ReplyToCommand(client, "No replay file loaded.");
return Plugin_Handled;
}
if (g_IsPlayingReplay) {
ReplyToCommand(client, "Replay is already playing.");
return Plugin_Handled;
}
g_ReplayIndex = 0;
g_IsPlayingReplay = true;
g_IsPaused = false;
g_LastReplayTime = GetEngineTime();
g_ReplayBot = CreateFakeClient("Replay Bot");
if (g_ReplayBot == 0) {
g_ReplayBot = -1; // Ensure g_ReplayBot is reset
ReplyToCommand(client, "Failed to create replay bot.");
LogError("Failed to create replay bot.");
g_IsPlayingReplay = false;
return Plugin_Handled;
}
ChangeClientTeam(g_ReplayBot, 2); // Set bot team to Terrorists (2)
CS_RespawnPlayer(g_ReplayBot); // Respawn the bot
SetEntProp(g_ReplayBot, Prop_Send, "m_iHealth", 100); // Set bot health
SetEntProp(g_ReplayBot, Prop_Data, "m_takedamage", 0, 1); // Disable bot damage
// Set bot model
SetEntityModel(g_ReplayBot, "models/player/ct_urban.mdl");
// Disable round restart
SetConVarInt(FindConVar("mp_round_restart_delay"), 0);
ReplyToCommand(client, "Replay started.");
return Plugin_Handled;
}
public Action Command_StopReplay(int client, int args) {
if (!g_IsPlayingReplay) {
ReplyToCommand(client, "No replay is currently playing.");
return Plugin_Handled;
}
g_IsPlayingReplay = false;
g_IsPaused = false;
if (g_ReplayBot != -1 && IsClientInGame(g_ReplayBot)) {
KickClient(g_ReplayBot);
g_ReplayBot = -1;
}
// Restore round restart delay
SetConVarInt(FindConVar("mp_round_restart_delay"), 7);
ReplyToCommand(client, "Replay stopped.");
return Plugin_Handled;
}
public Action Command_PauseReplay(int client, int args) {
if (!g_IsPlayingReplay) {
ReplyToCommand(client, "No replay is currently playing.");
return Plugin_Handled;
}
if (g_IsPaused) {
ReplyToCommand(client, "Replay is already paused.");
return Plugin_Handled;
}
g_IsPaused = true;
ReplyToCommand(client, "Replay paused.");
return Plugin_Handled;
}
public Action Command_ResumeReplay(int client, int args) {
if (!g_IsPlayingReplay) {
ReplyToCommand(client, "No replay is currently playing.");
return Plugin_Handled;
}
if (!g_IsPaused) {
ReplyToCommand(client, "Replay is not paused.");
return Plugin_Handled;
}
g_IsPaused = false;
g_LastReplayTime = GetEngineTime();
ReplyToCommand(client, "Replay resumed.");
return Plugin_Handled;
}
public Action Command_ResetReplay(int client, int args) {
if (!g_IsPlayingReplay) {
ReplyToCommand(client, "No replay is currently playing.");
return Plugin_Handled;
}
g_ReplayIndex = 0;
g_LastReplayTime = GetEngineTime();
ReplyToCommand(client, "Replay reset.");
return Plugin_Handled;
}
public Action Command_ListReplays(int client, int args) {
char replayDir[PLATFORM_MAX_PATH];
BuildPath(Path_SM, replayDir, sizeof(replayDir), REPLAY_DIRECTORY);
DirectoryListing dir = OpenDirectory(replayDir);
if (dir == null) {
ReplyToCommand(client, "Failed to open replay directory.");
LogError("Failed to open replay directory: %s", replayDir);
return Plugin_Handled;
}
char filename[PLATFORM_MAX_PATH];
FileType fileType;
int count = 0;
while (dir.GetNext(filename, sizeof(filename), fileType)) {
if (fileType == FileType_File) {
ReplyToCommand(client, "%d. %s", ++count, filename);
}
}
delete dir;
if (count == 0) {
ReplyToCommand(client, "No replay files found.");
}
return Plugin_Handled;
}
public void OnGameFrame() {
if (!g_IsPlayingReplay || g_IsPaused || g_ReplayBot == -1 || !IsClientInGame(g_ReplayBot)) {
return;
}
float currentTime = GetEngineTime();
float frameInterval = 1.0 / TICK_RATE;
float timeSinceLastUpdate = currentTime - g_LastReplayTime;
if (timeSinceLastUpdate < frameInterval) {
return;
}
g_LastReplayTime += frameInterval; // Increment by the expected interval
if (g_ReplayIndex >= g_ReplayLength - 1) {
g_ReplayIndex = 0; // Loop the replay
} else {
g_ReplayIndex++;
}
ReplayData CurrentData;
CurrentData = g_ReplayData[g_ReplayIndex];
// Set entity position and eye angles
float newOrigin[3];
newOrigin[0] = CurrentData.origin[0];
newOrigin[1] = CurrentData.origin[1];
newOrigin[2] = CurrentData.origin[2];
float newAngles[3];
newAngles[0] = CurrentData.angles[0];
newAngles[1] = CurrentData.angles[1];
newAngles[2] = 0.0; // Roll is usually 0 in CS:S
// Apply replay data, currently not working for eye angles
TeleportEntity(g_ReplayBot, newOrigin, newAngles, NULL_VECTOR);
}
public Action OnBotTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype) {
if (victim == g_ReplayBot) {
return Plugin_Handled;
}
return Plugin_Continue;
}
bool LoadReplayFile(const char[] filePath) {
g_ReplayLength = 0;
File fileHandle = OpenFile(filePath, "r");
if (fileHandle == null) {
LogError("Failed to open replay file: %s", filePath);
return false;
}
char line[256];
while (fileHandle.ReadLine(line, sizeof(line))) {
if (g_ReplayLength >= MAX_REPLAY_LENGTH) {
LogError("Replay file exceeds maximum allowed length: %d", MAX_REPLAY_LENGTH);
break;
}
if (strlen(line) == 0) {
continue;
}
char parts[6][32];
int numParts = ExplodeString(line, "|", parts, sizeof(parts), sizeof(parts[]));
if (numParts != 6) {
LogError("Invalid replay data format: %s", line);
continue;
}
float originX, originY, originZ, pitchAngle, yawAngle;
int buttons;
if (!StringToFloatEx(parts[0], originX) ||
!StringToFloatEx(parts[1], originY) ||
!StringToFloatEx(parts[2], originZ) ||
!StringToFloatEx(parts[3], pitchAngle) ||
!StringToFloatEx(parts[4], yawAngle) ||
!StringToIntEx(parts[5], buttons)) {
LogError("Failed to parse replay data: %s", line);
continue;
}
g_ReplayData[g_ReplayLength].origin[0] = originX;
g_ReplayData[g_ReplayLength].origin[1] = originY;
g_ReplayData[g_ReplayLength].origin[2] = originZ;
g_ReplayData[g_ReplayLength].angles[0] = pitchAngle;
g_ReplayData[g_ReplayLength].angles[1] = yawAngle;
g_ReplayData[g_ReplayLength].buttons = buttons;
g_ReplayLength++;
}
if (fileHandle.EndOfFile() && g_ReplayLength == 0) {
LogError("Replay file is empty or corrupted: %s", filePath);
}
delete fileHandle;
return true;
}
bool DirectoryExists(const char[] path) {
Handle dir = OpenDirectory(path);
if (dir == null) {
return false;
}
CloseHandle(dir);
return true;
}