This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathxms_bots.sp
More file actions
413 lines (334 loc) · 10.5 KB
/
xms_bots.sp
File metadata and controls
413 lines (334 loc) · 10.5 KB
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
#pragma semicolon 1
#pragma newdecls required
#define PLUGIN_VERSION "1.3"
#define PLUGIN_URL "www.hl2dm.community"
#define PLUGIN_UPDATE "http://raw.githubusercontent.com/utharper/sourcemod-hl2dm/master/addons/sourcemod/xms_bots.upd"
public Plugin myinfo = {
name = "XMS - Bot Controller",
version = PLUGIN_VERSION,
description = "RCBot2 controller for XMS servers",
// currently only supports a single bot -- automatically leaves when player count exceeds 1
author = "harper",
url = PLUGIN_URL
};
/**************************************************************
* INCLUDES
*************************************************************/
#include <sourcemod>
#include <morecolors>
#include <smlib>
#undef REQUIRE_PLUGIN
#include <updater>
#define REQUIRE_PLUGIN
#include <jhl2dm>
#include <xms>
/**************************************************************
* GLOBAL VARS
*************************************************************/
int giBotClient;
int giState;
int giJoinDelay;
int giLeaveDelay;
bool gbEnabled;
bool gbContinue;
bool gbSourceTV;
char gsBotName[MAX_NAME_LENGTH];
char gsKnownMaps[8][MAX_MAP_LENGTH] =
{
// the bot is familiar with these maps and shouldn't get stuck. (just stock maps for now)
"dm_lockdown",
"dm_overwatch",
"dm_powerhouse",
"dm_resistance",
"dm_runoff",
"dm_steamlab",
"dm_underpass",
"halls3"
};
/**************************************************************/
public void OnPluginStart()
{
LoadTranslations("xms_bot.phrases.txt");
CreateConVar("xms_bots_version", PLUGIN_VERSION, _, FCVAR_NOTIFY);
CreateTimer(1.0, T_CheckBots, _, TIMER_REPEAT);
HookEvent("player_death", Event_PlayerDeath, EventHookMode_Post);
if (LibraryExists("updater")) {
Updater_AddPlugin(PLUGIN_UPDATE);
}
}
public void OnLibraryAdded(const char[] sName)
{
if (StrEqual(sName, "updater")) {
Updater_AddPlugin(PLUGIN_UPDATE);
}
}
public void OnAllPluginsLoaded()
{
gbEnabled = BotsAvailable();
}
public void OnMapStart()
{
gbEnabled = BotsAvailable();
gbSourceTV = FindConVar("tv_enable").BoolValue;
}
public void OnMapEnd()
{
giBotClient = 0;
}
public void OnClientPutInServer(int iClient)
{
if (gbEnabled && IsClientInGame(iClient) && IsFakeClient(iClient) && !IsClientSourceTV(iClient))
{
giBotClient = iClient;
GetClientName(giBotClient, gsBotName, sizeof(gsBotName));
CreateTimer(2.0, T_BotAnnounce, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action OnClientSayCommand(int iClient, const char[] sCommand, const char[] sArgs)
{
if (!iClient || giBotClient <= 0 || !IsClientInGame(giBotClient)) {
return Plugin_Continue;
}
if (StrContains(sArgs, "!") != 0 && StrContains(sArgs, "/") != 0 && !CommandExists(sArgs))
{
// not a command
if (StrContains(sArgs, gsBotName, false) != -1 || Math_GetRandomInt(1, 2) == 2)
{
// Reply to 50% of chat, or if bot name is said
CreateTimer(1.0, T_BotResponse, TIMER_FLAG_NO_MAPCHANGE);
}
}
return Plugin_Continue;
}
public void OnGamestateChanged(int iNewState, int iOldState)
{
if (iNewState == GAME_OVER)
{
if (giBotClient > 0 && IsClientInGame(giBotClient))
{
char sText[MAX_SAY_LENGTH];
Format(sText, sizeof(sText), "xms_bot_end%i", Math_GetRandomInt(1, 2));
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
}
}
else if (iOldState == GAME_CHANGING)
{
gbContinue = (giBotClient > 0 && IsClientInGame(giBotClient));
}
giState = iNewState;
}
public Action T_CheckBots(Handle hTimer)
{
if (gbEnabled && (GetTimeElapsed() >= giJoinDelay || gbContinue))
{
int iTotal = GetClientCount2(true, true) - view_as<int>(gbSourceTV);
int iPlayers = GetClientCount2(true, false);
int iConnecting = GetClientCount2(false, false) - iPlayers;
if (iPlayers == 1)
{
if (!iConnecting && giBotClient == 0)
{
giBotClient = -1;
CreateTimer(1.0, T_BotAdd, _, TIMER_FLAG_NO_MAPCHANGE);
}
}
else if (giBotClient)
{
CreateTimer(view_as<float>(clamp(giLeaveDelay, 0, 999)), T_BotRemove, _, TIMER_FLAG_NO_MAPCHANGE);
}
if (iTotal > iPlayers + 1)
{
LogMessage("More bots spawned than expected, kicking..");
ServerCommand("rcbotd kickbot");
giBotClient = FindBotClient();
}
}
return Plugin_Continue;
}
public Action T_BotAdd(Handle hTimer)
{
if (GetClientCount2(true, false) == 1 && giState == GAME_DEFAULT) {
ServerCommand("rcbotd addbot");
}
return Plugin_Handled;
}
public Action T_BotAnnounce(Handle hTimer)
{
static int iTimer;
static int iRan;
if (giBotClient > 0 && IsClientInGame(giBotClient))
{
char sText[MAX_SAY_LENGTH];
iRan = Math_GetRandomIntNot(1, 3, iRan);
if (gbContinue)
{
bool bKnownMap;
char sMap[MAX_MAP_LENGTH];
GetCurrentMap(sMap, sizeof(sMap));
for (int i = 0; i < sizeof(gsKnownMaps); i++)
{
if (StrEqual(sMap, gsKnownMaps[i])) {
bKnownMap = true;
}
}
Format(sText, sizeof(sText), "xms_bot_%sknownmap%i", bKnownMap ? "" : "un", iRan);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
return Plugin_Stop;
}
else
{
if (iTimer == 0)
{
Format(sText, sizeof(sText), "xms_bot_greet%i", iRan);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
}
else if (iTimer >= 2)
{
Format(sText, sizeof(sText), "xms_bot_play%i", iRan);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
iTimer = 0;
return Plugin_Stop;
}
iTimer++;
}
}
return Plugin_Continue;
}
public Action T_BotRemove(Handle hTimer)
{
static int iRan;
if (GetClientCount2(true, false) != 1 && giState == GAME_DEFAULT)
{
if (giBotClient > 0 && IsClientInGame(giBotClient))
{
char sText[MAX_SAY_LENGTH];
iRan = Math_GetRandomIntNot(1, 3, iRan);
Format(sText, sizeof(sText), "xms_bot_quit%i", iRan);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
ServerCommand("rcbotd kickbot");
giBotClient = 0;
}
}
return Plugin_Handled;
}
public Action T_BotTaunt(Handle hTimer)
{
static int iText;
if (giBotClient > 0 && IsClientInGame(giBotClient))
{
char sText[MAX_SAY_LENGTH];
iText = Math_GetRandomIntNot(1, 6, iText);
Format(sText, sizeof(sText), "xms_bot_taunt%i", iText);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
}
return Plugin_Stop;
}
public Action T_BotDeath(Handle hTimer, bool bSuicide)
{
static int iText;
if (giBotClient > 0 && IsClientInGame(giBotClient))
{
char sText[MAX_SAY_LENGTH];
iText = Math_GetRandomIntNot(1, bSuicide ? 3 : 6, iText);
Format(sText, sizeof(sText), "xms_bot_%s%i", bSuicide ? "suicide" : "death", iText);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
}
return Plugin_Stop;
}
public Action T_BotResponse(Handle hTimer)
{
static int iText;
if (giBotClient > 0 && IsClientInGame(giBotClient))
{
char sText[MAX_SAY_LENGTH];
iText = Math_GetRandomIntNot(1, 7, iText);
Format(sText, sizeof(sText), "xms_bot_response%i", iText);
Format(sText, sizeof(sText), "%T", sText, LANG_SERVER);
MC_PrintToChatAllFrom(giBotClient, false, sText);
}
return Plugin_Stop;
}
public Action Event_PlayerDeath(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
if (!gbEnabled || !giBotClient) {
return Plugin_Continue;
}
bool bBotAttacker = (giBotClient == GetClientOfUserId(GetEventInt(hEvent, "attacker")));
bool bBotVictim = (giBotClient == GetClientOfUserId(GetEventInt(hEvent, "userid")));
if (bBotAttacker && bBotVictim)
{
// suicide
CreateTimer(1.0, T_BotDeath, true, TIMER_FLAG_NO_MAPCHANGE);
}
else if (bBotAttacker)
{
// player killed by bot, 33% chance to taunt
if (Math_GetRandomInt(1, 3) == 1) {
CreateTimer(1.0, T_BotTaunt, TIMER_FLAG_NO_MAPCHANGE);
}
}
else if (bBotVictim)
{
// bot killed by player, 33% chance to comment
if (Math_GetRandomInt(1, 3) == 1) {
CreateTimer(1.0, T_BotDeath, false, TIMER_FLAG_NO_MAPCHANGE);
}
}
return Plugin_Continue;
}
bool BotsAvailable()
{
static bool bBotSupport;
char sCurrentMode [MAX_MODE_LENGTH];
char sSupportedModes[192];
char sCommand [16];
if (!bBotSupport)
{
ServerCommandEx(sCommand, sizeof(sCommand), "rcbotd");
if (!(StrContains(sCommand, "Unknown command") == 0)) {
bBotSupport = true;
}
else {
LogError("RCBot2 not running");
}
}
if (bBotSupport)
{
GetGamemode(sCurrentMode, sizeof(sCurrentMode));
if (GetConfigString(sSupportedModes, sizeof(sSupportedModes), "Gamemodes", "Bots"))
{
giJoinDelay = GetConfigInt("JoinDelay", "Bots");
giLeaveDelay = GetConfigInt("QuitDelay", "Bots");
if (IsItemInList(sCurrentMode, sSupportedModes)) {
return true;
}
}
}
return false;
}
int FindBotClient()
{
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientConnected(i) && IsClientInGame(i) && IsFakeClient(i) && !IsClientSourceTV(i)) {
return i;
}
}
return 0;
}
int Math_GetRandomIntNot(int iMin, int iMax, int iNot)
{
int i;
do {
i = Math_GetRandomInt(iMin, iMax);
}
while (i == iNot);
return i;
}