forked from fdxx/l4d2_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4d2_afk_commands.sp
267 lines (223 loc) · 6.12 KB
/
l4d2_afk_commands.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
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <left4dhooks>
#include <multicolors>
#define VERSION "3.1"
#define IDLE_DEFAULT 0
#define IDLE_BLOCK 1
#define IDLE_NO_LIMIT 2
ConVar g_cvAfkDelay, g_cvIdleType;
Handle g_hSDK_GoAwayFromKeyboard;
float g_fAfkDelay;
int g_iIdleType;
public Plugin myinfo =
{
name = "L4D2 AFK Commands",
author = "fdxx",
version = VERSION,
}
public void OnPluginStart()
{
Init();
CreateConVar("l4d2_afk_commands_version", VERSION, "version", FCVAR_NOTIFY | FCVAR_DONTRECORD);
g_cvAfkDelay = CreateConVar("l4d2_afk_commands_afk_delay", "3.0", "How long to delay change to spectator. 0.0=Disabled.", FCVAR_NONE);
g_cvIdleType = CreateConVar("l4d2_afk_commands_idle_type", "1", "0=Default, 1=Block, 2=No limit.");
OnConVarChanged(null, "", "");
g_cvAfkDelay.AddChangeHook(OnConVarChanged);
g_cvIdleType.AddChangeHook(OnConVarChanged);
AddCommandListener(GoAfk_CmdListener, "go_away_from_keyboard");
RegConsoleCmd("sm_afk", Cmd_JoinSpectate);
RegConsoleCmd("sm_away", Cmd_JoinSpectate);
RegConsoleCmd("sm_idle", Cmd_JoinSpectate);
RegConsoleCmd("sm_spectate", Cmd_JoinSpectate);
RegConsoleCmd("sm_spectators", Cmd_JoinSpectate);
RegConsoleCmd("sm_joinspectators", Cmd_JoinSpectate);
RegConsoleCmd("sm_jointeam1", Cmd_JoinSpectate);
RegConsoleCmd("sm_survivors", Cmd_JoinSurvivor);
RegConsoleCmd("sm_sur", Cmd_JoinSurvivor);
RegConsoleCmd("sm_join", Cmd_JoinSurvivor);
RegConsoleCmd("sm_jg", Cmd_JoinSurvivor);
RegConsoleCmd("sm_jiaru", Cmd_JoinSurvivor);
RegConsoleCmd("sm_jointeam2", Cmd_JoinSurvivor);
RegConsoleCmd("sm_jr", Cmd_JoinSurvivor);
RegConsoleCmd("sm_kill", Cmd_KillSelf);
RegConsoleCmd("sm_zs", Cmd_KillSelf);
//AutoExecConfig(true, "l4d2_afk_commands");
}
void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_fAfkDelay = g_cvAfkDelay.FloatValue;
g_iIdleType = g_cvIdleType.IntValue;
}
void Init()
{
char sBuffer[128];
strcopy(sBuffer, sizeof(sBuffer), "l4d2_afk_commands");
GameData hGameData = new GameData(sBuffer);
if (hGameData == null)
SetFailState("Failed to load \"%s.txt\" gamedata.", sBuffer);
// void CTerrorPlayer::GoAwayFromKeyboard(void)
strcopy(sBuffer, sizeof(sBuffer), "CTerrorPlayer::GoAwayFromKeyboard");
StartPrepSDKCall(SDKCall_Player);
PrepSDKCall_SetFromConf(hGameData, SDKConf_Signature, sBuffer);
g_hSDK_GoAwayFromKeyboard = EndPrepSDKCall();
if(g_hSDK_GoAwayFromKeyboard == null)
SetFailState("Failed to create SDKCall: %s", sBuffer);
delete hGameData;
}
Action GoAfk_CmdListener(int client, const char[] command, int argc)
{
switch (g_iIdleType)
{
case IDLE_BLOCK:
{
if (IsRealClient(client) && GetClientTeam(client) != 1)
{
PrintHintText(client, "闲置请用 !away 命令");
return Plugin_Handled;
}
}
case IDLE_NO_LIMIT:
{
if (IsRealClient(client) && GetClientTeam(client) == 2 && IsPlayerAlive(client))
{
// By default, it cannot be idle when there is only 1 person.
SDKCall(g_hSDK_GoAwayFromKeyboard, client);
return Plugin_Handled;
}
}
}
return Plugin_Continue;
}
Action Cmd_JoinSpectate(int client, int args)
{
if (IsRealClient(client) && GetClientTeam(client) != 1)
{
if (g_fAfkDelay >= 0.1)
{
CreateTimer(g_fAfkDelay, JoinSpectate_Timer, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
PrintHintText(client, "%.1f 秒后进入旁观状态", g_fAfkDelay);
}
else
{
ChangeClientTeam(client, 1);
CPrintToChatAll("{blue}[提示] {olive}%N {default}进入了旁观状态.", client);
}
}
return Plugin_Handled;
}
Action JoinSpectate_Timer(Handle timer, int userid)
{
int client = GetClientOfUserId(userid);
if (IsRealClient(client) && GetClientTeam(client) != 1)
{
ChangeClientTeam(client, 1);
CPrintToChatAll("{blue}[提示] {olive}%N {default}进入了旁观状态.", client);
}
return Plugin_Continue;
}
Action Cmd_JoinSurvivor(int client, int args)
{
if (IsRealClient(client) && GetClientTeam(client) != 2)
{
if (IsPlayerIdle(client))
{
L4D_TakeOverBot(client);
return Plugin_Handled;
}
int bot = GetSurBot();
if (bot > 0)
{
ChangeClientTeam(client, 0);
L4D_SetHumanSpec(bot, client);
L4D_TakeOverBot(client);
}
else PrintHintText(client, "暂无幸存者BOT供接管");
}
return Plugin_Handled;
}
Action Cmd_KillSelf(int client, int args)
{
if (IsRealClient(client))
{
switch (GetClientTeam(client))
{
case 2, 3:
{
if (IsPlayerAlive(client))
{
ForcePlayerSuicide(client);
}
}
}
}
return Plugin_Handled;
}
bool IsRealClient(int client)
{
return (client > 0 && client <= MaxClients && IsClientInGame(client) && !IsFakeClient(client));
}
int GetSurBot()
{
int bot;
ArrayList aAliveBots = new ArrayList();
ArrayList aDeadBots = new ArrayList();
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i) && GetClientTeam(i) == 2 && IsFakeClient(i) && !HasIdlePlayer(i))
{
if (IsPlayerAlive(i))
aAliveBots.Push(i);
else
aDeadBots.Push(i);
}
}
if (aAliveBots.Length > 0)
{
bot = aAliveBots.Get(GetRandomIntEx(0, aAliveBots.Length - 1));
}
else if (aDeadBots.Length > 0)
{
bot = aDeadBots.Get(GetRandomIntEx(0, aDeadBots.Length - 1));
}
delete aAliveBots;
delete aDeadBots;
return bot;
}
int GetRandomIntEx(int min, int max)
{
return GetURandomInt() % (max - min + 1) + min;
}
bool IsPlayerIdle(int player)
{
int offset;
char sNetClass[12];
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i) && GetClientTeam(i) == 2 && IsFakeClient(i) && IsPlayerAlive(i))
{
if (!GetEntityNetClass(i, sNetClass, sizeof(sNetClass)))
continue;
offset = FindSendPropInfo(sNetClass, "m_humanSpectatorUserID");
if (offset > 0 && GetClientOfUserId(GetEntData(i, offset)) == player)
return true;
}
}
return false;
}
bool HasIdlePlayer(int bot)
{
char sNetClass[12];
int offset, player;
GetEntityNetClass(bot, sNetClass, sizeof(sNetClass));
offset = FindSendPropInfo(sNetClass, "m_humanSpectatorUserID");
if (offset > 0)
{
player = GetClientOfUserId(GetEntData(bot, offset));
if (player > 0 && IsClientConnected(player) && !IsFakeClient(player))
return true;
}
return false;
}