-
Notifications
You must be signed in to change notification settings - Fork 9
/
L4D2_DefibUsingBots.sp
333 lines (299 loc) · 9.18 KB
/
L4D2_DefibUsingBots.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
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#define PLUGIN_VERSION "1.7"
#define STARTSHOOTING 1
#define STOPSHOOTING 0
static shoot[MAXPLAYERS + 1] = 0;
public Plugin:myinfo =
{
name = "机器人使用电击器",
author = "DeathChaos25",
description = "Allows bots to use Defibrillators in L4D2",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=261566"
};
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
decl String:s_GameFolder[32];
GetGameFolderName(s_GameFolder, sizeof(s_GameFolder));
if (!StrEqual(s_GameFolder, "left4dead2", false))
{
strcopy(error, err_max, "This plugin is for Left 4 Dead 2 Only!");
return APLRes_Failure;
}
return APLRes_Success;
}
public OnPluginStart()
{
CreateConVar("sm_defib_bots_version", PLUGIN_VERSION, "Defib Bots Version", FCVAR_SPONLY | FCVAR_REPLICATED | FCVAR_NOTIFY);
CreateTimer(1.0, BotsDefib, _, TIMER_REPEAT);
HookEvent("defibrillator_used_fail", Event_DefibFailed);
HookEvent("defibrillator_interrupted", Event_DefibFailed);
HookEvent("defibrillator_used", Event_DefibUsed);
RegAdminCmd("sm_resetbots", ResetSurvivorAI, ADMFLAG_ROOT, "Completely Resets Survivor AI");
RegAdminCmd("sm_regroup", RegroupBots, ADMFLAG_ROOT, "Commands Survivor Bots to move to your location");
RegAdminCmd("sm_attack", AttackSI, ADMFLAG_ROOT, "Orders Survivor Bots to Attack the SI you are aiming at");
RegAdminCmd("sm_retreat", RunAway, ADMFLAG_ROOT, "Orders Survivor Bots to retreat from the SI you are aiming at");
}
public Action:BotsDefib(Handle:timer)
{
if (!IsServerProcessing())
{
return Plugin_Continue;
}
decl Float:Origin[3], Float:TOrigin[3];
new i = -1;
while ((i = FindEntityByClassname(i, "survivor_death_model")) != INVALID_ENT_REFERENCE)
{
GetEntPropVector(i, Prop_Send, "m_vecOrigin", Origin);
for (new j = 1; j <= MaxClients; j++)
{
if (IsSurvivor(j) && IsFakeClient(j) && IsPlayerAlive(j) && !IsIncapacitated(j) && !IsPlayerHeld(j) && !IsAssistNeeded() && ClientHasFewThreats(j))
{
GetEntPropVector(j, Prop_Send, "m_vecOrigin", TOrigin);
new Float:distance = GetVectorDistance(TOrigin, Origin);
new String:defib[32];
if (IsValidEdict(GetPlayerWeaponSlot(j, 3)))
{
GetEdictClassname(GetPlayerWeaponSlot(j, 3), defib, sizeof(defib));
if (distance > 100 && distance < 800)
{
if (StrEqual(defib, "weapon_defibrillator"))
{
ScriptCommand(j, "script", "CommandABot({cmd=1,pos=Vector(%f,%f,%f),bot=GetPlayerFromUserID(%i)})", Origin[0], Origin[1], Origin[2], GetClientUserId(j));
break;
}
}
else if (distance < 100)
{
if (StrEqual(defib, "weapon_defibrillator") && ChangePlayerWeaponSlot(j, 3))
{
decl Float:EyePos[3], Float:AimOnDeadSurvivor[3], Float:AimAngles[3];
GetClientEyePosition(j, EyePos);
MakeVectorFromPoints(EyePos, Origin, AimOnDeadSurvivor);
GetVectorAngles(AimOnDeadSurvivor, AimAngles);
TeleportEntity(j, NULL_VECTOR, AimAngles, NULL_VECTOR);
CreateTimer(0.2, AllowDefib, GetClientUserId(j), TIMER_FLAG_NO_MAPCHANGE);
break;
}
}
}
}
}
break;
}
return Plugin_Continue;
}
// Bot AI Manipulations Functions
public Action:RegroupBots(client, args)
{
decl Float:Origin[3];
GetClientAbsOrigin(client, Origin);
for (new i = 1; i <= MaxClients; i++)
{
if (IsSurvivor(i) && IsPlayerAlive(i) && !IsIncapacitated(i) && !IsAssistNeeded() && ClientHasFewThreats(i))
{
ScriptCommand(i, "script", "CommandABot({cmd=1,pos=Vector(%f,%f,%f),bot=GetPlayerFromUserID(%i)})", Origin[0], Origin[1], Origin[2], GetClientUserId(i));
}
}
}
public Action:AttackSI(client, args)
{
if (!IsSurvivor(client))
return;
new target = GetClientAimTarget(client);
if (!IsInfected(target))
return;
for (new i = 1; i <= MaxClients; i++)
{
if (IsSurvivor(i) && IsPlayerAlive(i) && !IsIncapacitated(i) && !IsAssistNeeded())
{
ScriptCommand(i, "script", "CommandABot({cmd=0,bot=GetPlayerFromUserID(%i),target=GetPlayerFromUserID(%i)})", GetClientUserId(i), GetClientUserId(target));
}
}
}
public Action:RunAway(client, args)
{
if (!IsSurvivor(client))
return;
new target = GetClientAimTarget(client);
if (!IsInfected(target))
return;
for (new i = 1; i <= MaxClients; i++)
{
if (IsSurvivor(i) && IsPlayerAlive(i) && !IsIncapacitated(i) && !IsAssistNeeded())
{
ScriptCommand(i, "script", "CommandABot({cmd=2,bot=GetPlayerFromUserID(%i),target=GetPlayerFromUserID(%i)})", GetClientUserId(i), GetClientUserId(target));
}
}
}
// Functions to allow the Bot to use Defibs
public Action:AllowDefib(Handle:Timer, any:userid)
{
new client = GetClientOfUserId(userid);
shoot[client] = STARTSHOOTING;
}
public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
{
if (IsSurvivor(client) && IsPlayerAlive(client) && IsFakeClient(client))
{
new defib = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if (IsValidEntity(defib))
{
decl String:classname[128];
GetEntityClassname(defib, classname, sizeof(classname));
if (defib == GetPlayerWeaponSlot(client, 3) && StrEqual(classname, "weapon_defibrillator"))
{
if (shoot[client] == STARTSHOOTING)
{
buttons |= IN_ATTACK;
}
else if (shoot[client] == STOPSHOOTING)
{
buttons &= ~IN_ATTACK;
}
}
}
}
return Plugin_Continue;
}
// Event Hooks so we can reset bots and prevent them from being stuck in the trying to defib state
public Action:Event_DefibUsed(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if (IsSurvivor(client) && IsFakeClient(client))
{
shoot[client] = STOPSHOOTING;
CreateTimer(0.4, ResetBotAI, GetClientUserId(client));
}
}
public Action:Event_DefibFailed(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if (IsSurvivor(client) && IsFakeClient(client))
{
shoot[client] = STOPSHOOTING;
CreateTimer(6.0, ResetBotAI, GetClientUserId(client));
}
}
public Action:ResetSurvivorAI(client, args)
{
for (new i = 1; i <= MaxClients; i++)
{
if (IsSurvivor(i) && IsPlayerAlive(i) && !IsIncapacitated(i) && !IsAssistNeeded() && ClientHasFewThreats(i))
{
ScriptCommand(i, "script", "CommandABot({cmd=3,bot=GetPlayerFromUserID(%i)})", GetClientUserId(i));
}
}
}
public Action:ResetBotAI(Handle:Timer, any:userid)
{
new client = GetClientOfUserId(userid);
if (IsSurvivor(client) && IsFakeClient(client))
{
shoot[client] = STOPSHOOTING;
ScriptCommand(client, "script", "CommandABot({cmd=3,bot=GetPlayerFromUserID(%i)})", GetClientUserId(client));
}
}
// Stock functions, bools, etc
stock bool:IsIncapacitated(client)
{
if (GetEntProp(client, Prop_Send, "m_isIncapacitated", 1) > 0)
return true;
return false;
}
stock bool:ChangePlayerWeaponSlot(Client, weaponslot)
{
new iWeapon = GetPlayerWeaponSlot(Client, weaponslot);
if (iWeapon > MaxClients)
{
new String:weapon[32];
if (IsValidEdict(GetEdictClassname(GetPlayerWeaponSlot(Client, 3), weapon, sizeof(weapon))))
{
if (StrEqual(weapon, "weapon_defibrillator"))
{
FakeClientCommand(Client, "use weapon_defibrillator");
return true;
}
}
}
return false;
}
stock bool:IsPlayerHeld(client)
{
new jockey = GetEntPropEnt(client, Prop_Send, "m_jockeyAttacker");
new charger = GetEntPropEnt(client, Prop_Send, "m_pummelAttacker");
new hunter = GetEntPropEnt(client, Prop_Send, "m_pounceAttacker");
new smoker = GetEntPropEnt(client, Prop_Send, "m_tongueOwner");
if (jockey > 0 || charger > 0 || hunter > 0 || smoker > 0)
{
return true;
}
return false;
}
stock bool:IsAssistNeeded()
{
for (new i = 1; i <= MaxClients; i++)
{
if (IsSurvivor(i))
{
if (IsIncapacitated(i) || IsPlayerHeld(i))
{
return true;
}
}
}
return false;
}
stock bool:IsDefibOwned(defib)
{
for (new i = 1; i <= MaxClients; i++)
{
if (IsSurvivor(i))
{
if (GetPlayerWeaponSlot(i, 3) == defib)
{
return true;
}
}
}
return false;
}
stock bool:ClientHasFewThreats(client)
{
if (IsSurvivor(client))
{
new threats = GetEntProp(client, Prop_Send, "m_hasVisibleThreats");
if (threats <= 0)
{
return true;
}
}
return false;
}
stock ScriptCommand(client, const String:command[], const String:arguments[], any:...)
{
new String:vscript[PLATFORM_MAX_PATH];
VFormat(vscript, sizeof(vscript), arguments, 4);
new flags = GetCommandFlags(command);
SetCommandFlags(command, flags^FCVAR_CHEAT);
FakeClientCommand(client, "%s %s", command, vscript);
SetCommandFlags(command, flags | FCVAR_CHEAT);
}
stock bool:IsSurvivor(client)
{
if (client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2)
{
return true;
}
return false;
}
stock bool:IsInfected(client)
{
if (client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3)
{
return true;
}
return false;
}