-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathl4d2_AI_HardSI.sp
261 lines (218 loc) · 9.43 KB
/
l4d2_AI_HardSI.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
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <smlib>
#include <left4downtown>
#include "includes/hardcoop_util.sp"
#include "modules/AI_Smoker.sp"
#include "modules/AI_Boomer.sp"
#include "modules/AI_Hunter.sp"
#include "modules/AI_Spitter.sp"
#include "modules/AI_Charger.sp"
#include "modules/AI_Jockey.sp"
#include "modules/AI_Tank.sp"
#include "modules/AI_Witch.sp"
new Handle:hCvarAssaultReminderInterval;
new bool:bHasBeenShoved[MAXPLAYERS]; // shoving resets SI movement
public Plugin:myinfo =
{
name = "特感 AI 加强",
author = "Breezy",
description = "Improves the AI behaviour of special infected",
version = "1.0",
url = "github.com/breezyplease"
};
public OnPluginStart() {
// Cvars
hCvarAssaultReminderInterval = CreateConVar( "ai_assault_reminder_interval", "2", "Frequency(sec) at which the 'nb_assault' command is fired to make SI attack" );
// Event hooks
HookEvent("player_spawn", InitialiseSpecialInfected, EventHookMode_Pre);
HookEvent("ability_use", OnAbilityUse, EventHookMode_Pre);
HookEvent("player_shoved", OnPlayerShoved, EventHookMode_Pre);
HookEvent("player_jump", OnPlayerJump, EventHookMode_Pre);
// Load modules
Smoker_OnModuleStart();
Hunter_OnModuleStart();
Spitter_OnModuleStart();
Boomer_OnModuleStart();
Charger_OnModuleStart();
Jockey_OnModuleStart();
Tank_OnModuleStart();
Witch_OnModuleStart();
AutoExecConfig(true, "l4d2_ai_hardsi");
}
public OnPluginEnd() {
// Unload modules
Smoker_OnModuleEnd();
Hunter_OnModuleEnd();
Spitter_OnModuleEnd();
Boomer_OnModuleEnd();
Charger_OnModuleEnd();
Jockey_OnModuleEnd();
Tank_OnModuleEnd();
Witch_OnModuleEnd();
}
/***********************************************************************************************************************************************************************************
KEEP SI AGGRESSIVE
***********************************************************************************************************************************************************************************/
public Action:L4D_OnFirstSurvivorLeftSafeArea(firstSurvivor) {
CreateTimer( float(GetConVarInt(hCvarAssaultReminderInterval)), Timer_ForceInfectedAssault, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE );
}
public Action:Timer_ForceInfectedAssault( Handle:timer ) {
CheatCommand("nb_assault");
}
/***********************************************************************************************************************************************************************************
SI MOVEMENT
***********************************************************************************************************************************************************************************/
// Modify SI movement
public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon) {
if( IsBotInfected(client) && IsPlayerAlive(client) ) { // bots continue to trigger this callback for a few seconds after death
new botInfected = client;
switch( L4D2_Infected:GetInfectedClass(botInfected) ) {
case (L4D2Infected_Hunter): {
if( !bHasBeenShoved[botInfected] ) return Hunter_OnPlayerRunCmd( botInfected, buttons, impulse, vel, angles, weapon );
}
case (L4D2Infected_Charger): {
return Charger_OnPlayerRunCmd( botInfected, buttons, impulse, vel, angles, weapon );
}
case (L4D2Infected_Jockey): {
return Jockey_OnPlayerRunCmd( botInfected, buttons, impulse, vel, angles, weapon, bHasBeenShoved[botInfected] );
}
case (L4D2Infected_Tank): {
return Tank_OnPlayerRunCmd( botInfected, buttons, impulse, vel, angles, weapon );
}
default: {
return Plugin_Continue;
}
}
}
return Plugin_Continue;
}
/***********************************************************************************************************************************************************************************
EVENT HOOKS
***********************************************************************************************************************************************************************************/
// Initialise relevant module flags for SI when they spawn
public Action:InitialiseSpecialInfected(Handle:event, String:name[], bool:dontBroadcast) {
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if( IsBotInfected(client) ) {
new botInfected = client;
bHasBeenShoved[client] = false;
// Process for SI class
switch( L4D2_Infected:GetInfectedClass(botInfected) ) {
case (L4D2Infected_Hunter): {
return Hunter_OnSpawn(botInfected);
}
case (L4D2Infected_Charger): {
return Charger_OnSpawn(botInfected);
}
case (L4D2Infected_Jockey): {
return Jockey_OnSpawn(botInfected);
}
default: {
return Plugin_Handled;
}
}
}
return Plugin_Handled;
}
// Modify hunter lunges and block smokers/spitters from fleeing after using their ability
public Action:OnAbilityUse(Handle:event, String:name[], bool:dontBroadcast) {
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if( IsBotInfected(client) ) {
new bot = client;
bHasBeenShoved[bot] = false; // Reset shove status
// Process for different SI
new String:abilityName[32];
GetEventString(event, "ability", abilityName, sizeof(abilityName));
if( StrEqual(abilityName, "ability_lunge") ) {
return Hunter_OnPounce(bot);
} else if( StrEqual(abilityName, "ability_charge") ) {
Charger_OnCharge(bot);
} else if( StrEqual(abilityName, "ability_spit") ) { // stop smokers and spitters running away
CreateTimer(0.5, Timer_Suicide, any:client, TIMER_FLAG_NO_MAPCHANGE);
}
}
return Plugin_Handled;
}
public Action:OnTongueRelease(Handle:event, String:name[], bool:dontBroadcast) {
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if( IsBotInfected(client) ) {
CreateTimer(0.5, Timer_Suicide, any:client, TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action:Timer_Suicide(Handle:timer, any:client) {
ForcePlayerSuicide(client);
return Plugin_Handled;
}
// Pause behaviour modification when shoved
public Action:OnPlayerShoved(Handle:event, String:name[], bool:dontBroadcast) {
new shovedPlayer = GetClientOfUserId(GetEventInt(event, "userid"));
if( IsBotInfected(shovedPlayer) ) {
bHasBeenShoved[shovedPlayer] = true;
if( L4D2_Infected:GetInfectedClass(shovedPlayer) == L4D2Infected_Jockey ) {
Jockey_OnShoved(shovedPlayer);
}
}
return Plugin_Continue;
}
// Re-enable forced hopping when a shoved jockey leaps again naturally
public Action:OnPlayerJump(Handle:event, String:name[], bool:dontBroadcast) {
new jumpingPlayer = GetClientOfUserId(GetEventInt(event, "userid"));
if( IsBotInfected(jumpingPlayer) ) {
bHasBeenShoved[jumpingPlayer] = false;
}
}
/***********************************************************************************************************************************************************************************
TRACKING SURVIVORS' AIM
***********************************************************************************************************************************************************************************/
/**
Determines whether an attacking SI is being watched by the survivor
@return: true if the survivor's crosshair is within the specified radius
@param attacker: the client number of the attacking SI
@param offsetThreshold: the radius(degrees) of the cone of detection around the straight line from the attacked survivor to the SI
**/
bool:IsTargetWatchingAttacker( attacker, offsetThreshold ) {
new bool:isWatching = true;
if( GetClientTeam(attacker) == 3 && IsPlayerAlive(attacker) ) { // SI continue to hold on to their targets for a few seconds after death
new target = GetClientAimTarget(attacker);
if( IsSurvivor(target) ) {
new aimOffset = RoundToNearest(GetPlayerAimOffset(target, attacker));
if( aimOffset <= offsetThreshold ) {
isWatching = true;
} else {
isWatching = false;
}
}
}
return isWatching;
}
/**
Calculates how much a player's aim is off another player
@return: aim offset in degrees
@attacker: considers this player's eye angles
@target: considers this player's position
Adapted from code written by Guren with help from Javalia
**/
Float:GetPlayerAimOffset( attacker, target ) {
if( !IsClientConnected(attacker) || !IsClientInGame(attacker) || !IsPlayerAlive(attacker) )
ThrowError("Client is not Alive.");
if(!IsClientConnected(target) || !IsClientInGame(target) || !IsPlayerAlive(target) )
ThrowError("Target is not Alive.");
decl Float:attackerPos[3], Float:targetPos[3];
decl Float:aimVector[3], Float:directVector[3];
decl Float:resultAngle;
// Get the unit vector representing the attacker's aim
GetClientEyeAngles(attacker, aimVector);
aimVector[0] = aimVector[2] = 0.0; // Restrict pitch and roll, consider yaw only (angles on horizontal plane)
GetAngleVectors(aimVector, aimVector, NULL_VECTOR, NULL_VECTOR); // extract the forward vector[3]
NormalizeVector(aimVector, aimVector); // convert into unit vector
// Get the unit vector representing the vector between target and attacker
GetClientAbsOrigin(target, targetPos);
GetClientAbsOrigin(attacker, attackerPos);
attackerPos[2] = targetPos[2] = 0.0; // Restrict to XY coordinates
MakeVectorFromPoints(attackerPos, targetPos, directVector);
NormalizeVector(directVector, directVector);
// Calculate the angle between the two unit vectors
resultAngle = RadToDeg(ArcCosine(GetVectorDotProduct(aimVector, directVector)));
return resultAngle;
}