Skip to content

Commit

Permalink
Bot crouching support.
Browse files Browse the repository at this point in the history
  • Loading branch information
splewis committed May 31, 2017
1 parent d39e7b9 commit 5f935b0
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 102 deletions.
54 changes: 36 additions & 18 deletions scripting/practicemode.sp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ bool g_IsPMBot[MAXPLAYERS + 1];
float g_BotSpawnOrigin[MAXPLAYERS + 1][3];
float g_BotSpawnAngles[MAXPLAYERS + 1][3];
char g_BotSpawnWeapon[MAXPLAYERS + 1][64];
bool g_BotCrouching[MAXPLAYERS + 1];

#define PLAYER_HEIGHT 72.0
#define CLASS_LENGTH 64
Expand Down Expand Up @@ -234,8 +235,10 @@ public void OnPluginStart() {

// Bot commands
RegConsoleCmd("sm_bot", Command_Bot);
RegConsoleCmd("sm_crouchbot", Command_CrouchBot);
RegConsoleCmd("sm_botplace", Command_BotPlace);
RegConsoleCmd("sm_boost", Command_Boost);
RegConsoleCmd("sm_crouchboost", Command_CrouchBoost);
RegConsoleCmd("sm_removebot", Command_RemoveBot);

PM_AddChatAlias(".back", "sm_grenadeback");
Expand All @@ -260,11 +263,16 @@ public void OnPluginStart() {
PM_AddChatAlias(".timer", "sm_time");
PM_AddChatAlias(".time", "sm_time");
PM_AddChatAlias(".timer2", "sm_time2");
PM_AddChatAlias(".boost", "sm_boost");

PM_AddChatAlias(".bot", "sm_bot");
PM_AddChatAlias(".crouchbot", "sm_crouchbot");
PM_AddChatAlias(".cbot", "sm_crouchbot");
PM_AddChatAlias(".botplace", "sm_botplace");
PM_AddChatAlias(".bot2", "sm_botplace");
PM_AddChatAlias(".boost", "sm_boost");
PM_AddChatAlias(".crouchboost", "sm_crouchboost");
PM_AddChatAlias(".cboost", "sm_crouchboost");

PM_AddChatAlias(".removebot", "sm_removebot");
PM_AddChatAlias(".kickbot", "sm_removebot");
PM_AddChatAlias(".clearbot", "sm_removebot");
Expand Down Expand Up @@ -591,27 +599,37 @@ public void GetColor(ClientColor c, int array[4]) {
public Action OnPlayerRunCmd(int client, int& buttons, int& impulse, float vel[3], float angles[3],
int& weapon, int& subtype, int& cmdnum, int& tickcount, int& seed,
int mouse[2]) {
if (!g_InPracticeMode) {
return Plugin_Continue;
}

if (IsPMBot(client)) {
if (g_BotCrouching[client]) {
buttons |= IN_DUCK;
} else {
buttons &= ~IN_DUCK;
}
return Plugin_Continue;
}

if (!IsPlayer(client)) {
return Plugin_Continue;
}

if (g_InPracticeMode) {
bool moving = MovingButtons(buttons);

if (g_RunningTimeCommand[client] && g_TimerType[client] == TimerType_Movement) {
if (g_RunningLiveTimeCommand[client]) {
// The movement timer is already running; stop it.
if (!moving && GetEntityFlags(client) & FL_ONGROUND) {
g_RunningTimeCommand[client] = false;
g_RunningLiveTimeCommand[client] = false;
StopClientTimer(client);
}
} else {
// We're pending a movement timer start.
if (moving) {
g_RunningLiveTimeCommand[client] = true;
StartClientTimer(client);
}
bool moving = MovingButtons(buttons);
if (g_RunningTimeCommand[client] && g_TimerType[client] == TimerType_Movement) {
if (g_RunningLiveTimeCommand[client]) {
// The movement timer is already running; stop it.
if (!moving && GetEntityFlags(client) & FL_ONGROUND) {
g_RunningTimeCommand[client] = false;
g_RunningLiveTimeCommand[client] = false;
StopClientTimer(client);
}
} else {
// We're pending a movement timer start.
if (moving) {
g_RunningLiveTimeCommand[client] = true;
StartClientTimer(client);
}
}
}
Expand Down
134 changes: 133 additions & 1 deletion scripting/practicemode/bots.sp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public int CreateBot(int client) {
stock int CreateBot(int client, bool forceCrouch=false) {
int bot = GetClientBot(client);
if (bot <= 0) {
char name[64];
Expand All @@ -7,6 +7,7 @@ public int CreateBot(int client) {
bot = CreateFakeClient(name);
g_BotOwned[client] = bot;
g_IsPMBot[bot] = true;
PM_Message(client, "Created bot, use .nobot to remove it.");
}
if (bot <= 0) {
PM_Message(client, "Failed to create bot :(");
Expand All @@ -15,6 +16,10 @@ public int CreateBot(int client) {

int botTeam = GetClientTeam(client) == CS_TEAM_CT ? CS_TEAM_T : CS_TEAM_CT;
ChangeClientTeam(bot, botTeam);

bool clientCrouching = (GetEntityFlags(client) & FL_DUCKING != 0);
g_BotCrouching[bot] = forceCrouch || clientCrouching;

CS_RespawnPlayer(bot);
return bot;
}
Expand Down Expand Up @@ -45,3 +50,130 @@ void GiveBotParams(int bot) {
GivePlayerItem(bot, g_BotSpawnWeapon[bot]);
TeleportEntity(bot, g_BotSpawnOrigin[bot], g_BotSpawnAngles[bot], NULL_VECTOR);
}

// Commands.

public Action Command_Bot(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

int bot = CreateBot(client);
if (bot <= 0) {
return Plugin_Handled;
}

GetClientAbsOrigin(client, g_BotSpawnOrigin[bot]);
GetClientEyeAngles(client, g_BotSpawnAngles[bot]);
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);

SetEntityMoveType(client, MOVETYPE_NOCLIP);
return Plugin_Handled;
}

public Action Command_CrouchBot(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

int bot = CreateBot(client, true);
if (bot <= 0) {
return Plugin_Handled;
}

GetClientAbsOrigin(client, g_BotSpawnOrigin[bot]);
GetClientEyeAngles(client, g_BotSpawnAngles[bot]);
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);

SetEntityMoveType(client, MOVETYPE_NOCLIP);
return Plugin_Handled;
}

public Action Command_BotPlace(int client, int args) {
// Based on Franc1sco's bot_spawner plugin:
// https://github.com/Franc1sco/BotSpawner/blob/master/bot_spawner.sp
int bot = CreateBot(client);
if (bot <= 0) {
return Plugin_Handled;
}

float start[3], angle[3], end[3], normal[3];
GetClientEyePosition(client, start);
GetClientEyeAngles(client, angle);

TR_TraceRayFilter(start, angle, MASK_SOLID, RayType_Infinite, RayDontHitSelf, client);
if (TR_DidHit(INVALID_HANDLE)) {
TR_GetEndPosition(end, INVALID_HANDLE);
TR_GetPlaneNormal(INVALID_HANDLE, normal);
GetVectorAngles(normal, normal);
normal[0] += 90.0;

g_BotSpawnOrigin[bot] = end;
g_BotSpawnAngles[bot] = normal;
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);
}

return Plugin_Handled;
}

public Action Command_Boost(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

int bot = CreateBot(client);
if (bot <= 0) {
return Plugin_Handled;
}

float origin[3];
GetClientAbsOrigin(client, origin);
g_BotSpawnOrigin[bot] = origin;

GetClientEyeAngles(client, g_BotSpawnAngles[bot]);
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);

origin[2] += PLAYER_HEIGHT;
TeleportEntity(client, origin, NULL_VECTOR, NULL_VECTOR);
return Plugin_Handled;
}

public Action Command_CrouchBoost(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

int bot = CreateBot(client, true);
if (bot <= 0) {
return Plugin_Handled;
}

float origin[3];
GetClientAbsOrigin(client, origin);
g_BotSpawnOrigin[bot] = origin;

GetClientEyeAngles(client, g_BotSpawnAngles[bot]);
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);

origin[2] += PLAYER_HEIGHT;
TeleportEntity(client, origin, NULL_VECTOR, NULL_VECTOR);
return Plugin_Handled;
}

public bool RayDontHitSelf(int entity, int contentsMask, any data) {
return entity != data;
}

public Action Command_RemoveBot(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

KickClientBot(client);
return Plugin_Handled;
}
83 changes: 0 additions & 83 deletions scripting/practicemode/commands.sp
Original file line number Diff line number Diff line change
Expand Up @@ -696,86 +696,3 @@ public Action Timer_ResetTimescale(Handle timer) {
}
return Plugin_Handled;
}

public Action Command_Bot(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

int bot = CreateBot(client);
if (bot <= 0) {
return Plugin_Handled;
}

GetClientAbsOrigin(client, g_BotSpawnOrigin[bot]);
GetClientEyeAngles(client, g_BotSpawnAngles[bot]);
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);

SetEntityMoveType(client, MOVETYPE_NOCLIP);
return Plugin_Handled;
}

public Action Command_BotPlace(int client, int args) {
// Based on Franc1sco's bot_spawner plugin:
// https://github.com/Franc1sco/BotSpawner/blob/master/bot_spawner.sp
int bot = CreateBot(client);
if (bot <= 0) {
return Plugin_Handled;
}

float start[3], angle[3], end[3], normal[3];
GetClientEyePosition(client, start);
GetClientEyeAngles(client, angle);

TR_TraceRayFilter(start, angle, MASK_SOLID, RayType_Infinite, RayDontHitSelf, client);
if (TR_DidHit(INVALID_HANDLE)) {
TR_GetEndPosition(end, INVALID_HANDLE);
TR_GetPlaneNormal(INVALID_HANDLE, normal);
GetVectorAngles(normal, normal);
normal[0] += 90.0;

g_BotSpawnOrigin[bot] = end;
g_BotSpawnAngles[bot] = normal;
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);
}

return Plugin_Handled;
}

public Action Command_Boost(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

int bot = CreateBot(client);
if (bot <= 0) {
return Plugin_Handled;
}

float origin[3];
GetClientAbsOrigin(client, origin);
g_BotSpawnOrigin[bot] = origin;

GetClientEyeAngles(client, g_BotSpawnAngles[bot]);
GetClientWeapon(client, g_BotSpawnWeapon[bot], CLASS_LENGTH);
GiveBotParams(bot);

origin[2] += PLAYER_HEIGHT;
TeleportEntity(client, origin, NULL_VECTOR, NULL_VECTOR);
return Plugin_Handled;
}

public bool RayDontHitSelf(int entity, int contentsMask, any data) {
return entity != data;
}

public Action Command_RemoveBot(int client, int args) {
if (!g_InPracticeMode) {
return Plugin_Handled;
}

KickClientBot(client);
return Plugin_Handled;
}

0 comments on commit 5f935b0

Please sign in to comment.