projects-jenz/AutismBotIngame/scripting/autism_bot_info.sp

402 lines
14 KiB
SourcePawn
Raw Normal View History

2020-01-23 23:15:26 +01:00
#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "jenz"
2020-06-09 23:34:49 +02:00
#define PLUGIN_VERSION "1.2"
#define generic_length 256
2020-06-09 23:34:49 +02:00
#define rows_entry_cap 10
2020-01-23 23:15:26 +01:00
#include <sourcemod>
#include <sdktools>
#pragma newdecls required
2020-02-28 22:00:22 +01:00
int present = 0;
2020-02-09 00:26:05 +01:00
int targethuman = 0;
int stuckcounterx = 0;
int stuckcountery = 0;
2020-01-23 23:15:26 +01:00
2020-06-09 23:34:49 +02:00
//bot input
int row_counter = 0;
char keyinput[rows_entry_cap][generic_length];
float clientangles[rows_entry_cap][3];
float xyz[rows_entry_cap][3];
float target_human_original_coord[3];
bool hunt_or_mimic;
2020-01-23 23:15:26 +01:00
public Plugin myinfo =
{
name = "coordinates for the bot",
author = PLUGIN_AUTHOR,
description = "hello ",
version = PLUGIN_VERSION,
url = ""
};
2020-02-28 22:00:22 +01:00
public void OnClientDisconnect(int client)
2020-01-23 23:15:26 +01:00
{
2020-02-28 22:00:22 +01:00
if (present == client)
{
2020-02-28 22:00:22 +01:00
present = 0;
mysql_enable_disable_connected(0);
}
2020-01-23 23:15:26 +01:00
}
2020-02-28 22:00:22 +01:00
public void OnPluginStart()
2020-01-23 23:15:26 +01:00
{
//talking
RegConsoleCmd("sm_autism", cmd_talk, "talking to the bot through java application");
2020-02-28 22:00:22 +01:00
//hooks
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
HookEvent("player_team", event_playerteam, EventHookMode_PostNoCopy);
//mysql
sql_create_table();
2020-01-23 23:15:26 +01:00
}
public Action cmd_talk(int client, int args)
{
char info[generic_length];
GetCmdArgString(info, sizeof(info));
2020-05-06 01:24:15 +02:00
if (strlen(info) == 0)
{
2020-06-09 23:34:49 +02:00
PrintToChat(client, "Add a message to the command if autism bot is ingame and running on discord");
2020-05-06 01:24:15 +02:00
return Plugin_Handled;
}
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
return Plugin_Handled;
2020-06-09 23:34:49 +02:00
char query_start[generic_length * 2];
int size2 = 2 * strlen(info) + 1;
char[] sEscapedName = new char[size2 + 1];
SQL_EscapeString(database_connection, info, sEscapedName, size2 + 1);
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`chatting` (`chatmessage`, `responsemessage`) VALUES ('%s', '')", sEscapedName);
//PrintToChat(client, "sEscapedName: %s", query_start);
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
return Plugin_Handled;
}
public Action event_playerteam(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if (client == present)
{
//PrintToChatAll("called event_playerteam");
int team = event.GetInt("team");
//PrintToChatAll("team: %i", team);
if (team == 2 || team == 3)
mysql_bot_not_spec();
}
}
2020-02-28 22:00:22 +01:00
public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
2020-01-23 23:15:26 +01:00
{
2020-02-28 22:00:22 +01:00
targethuman = 0;
int server_port = GetConVarInt(FindConVar("hostport"));
if (server_port == 27015)
{
mysql_update_playercount();
}
hunt_or_mimic = false;
if (present)
mysql_clean_movement_input();
2020-01-23 23:15:26 +01:00
}
2020-02-28 22:00:22 +01:00
public void OnMapStart()
2020-02-09 00:26:05 +01:00
{
sql_create_table();
2020-06-09 23:34:49 +02:00
CreateTimer(0.2, recursive_pressing, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
CreateTimer(2.0, mysql_send_pressing, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
2020-02-09 00:26:05 +01:00
}
2020-02-28 22:00:22 +01:00
public bool TraceEntityFilterPlayer(int entity, int contentsMask)
2020-02-09 00:26:05 +01:00
{
2020-02-28 22:00:22 +01:00
return (entity > GetMaxClients() || !entity);
2020-02-09 00:26:05 +01:00
}
2020-06-09 23:34:49 +02:00
public Action mysql_send_pressing(Handle timer, any data)
{
if (present && IsPlayerAlive(present))
mysql_send_input();
}
2020-02-28 22:00:22 +01:00
public Action recursive_pressing(Handle timer, any data)
{
if (present && IsPlayerAlive(present))
{
float present_bot_coords[3];
GetClientAbsOrigin(present, present_bot_coords);
int targeteam = 0;
if (GetClientTeam(present) != 3)
{
2020-06-09 23:34:49 +02:00
//2 = autismo is zm and should follow closest moving zm
targeteam = 2;
}
else
{
2020-06-09 23:34:49 +02:00
//3 = autismo is human and should follow closest moving ct
targeteam = 3;
}
if (!IsValidClient(targethuman) || GetClientTeam(targethuman) != targeteam || !IsPlayerAlive(targethuman))
{
hunt_or_mimic = false;
float lowest_distance = 1000000.0;
for (int i = 1; i <= MaxClients; i++)
if (IsValidClient(i) && IsPlayerAlive(i) && GetClientTeam(i) == targeteam && i != present)
{
float flVel[3];
2020-06-09 23:34:49 +02:00
float minvelocity = 40.0;
GetEntPropVector(i, Prop_Data, "m_vecAbsVelocity", flVel);
if (flVel[0] < minvelocity && flVel[1] < minvelocity)
continue;
float pos[3];
GetClientAbsOrigin(i, pos);
float dx = present_bot_coords[0] - pos[0];
float dy = present_bot_coords[1] - pos[1];
float dz = FloatAbs(present_bot_coords[2] - pos[2]);
float dist = SquareRoot(dx*dx + dy*dy + dz*dz);
if (dist < lowest_distance)
{
lowest_distance = dist;
targethuman = i;
}
}
2020-06-09 23:34:49 +02:00
if (IsValidClient(targethuman))
GetClientAbsOrigin(targethuman, target_human_original_coord);
}
2020-02-28 22:00:22 +01:00
if (IsValidClient(targethuman))
2020-01-23 23:15:26 +01:00
{
2020-06-09 23:34:49 +02:00
if (!hunt_or_mimic)
2020-02-28 22:00:22 +01:00
{
2020-06-09 23:34:49 +02:00
int distance_limit = 50;
float dx = present_bot_coords[0] - target_human_original_coord[0];
float dy = present_bot_coords[1] - target_human_original_coord[1];
float dz = FloatAbs(present_bot_coords[2] - target_human_original_coord[2]);
float dist = SquareRoot(dx*dx + dy*dy + dz*dz);
//PrintToChatAll("dist: %f", dist);
if (dist < distance_limit)
hunt_or_mimic = true;
xyz[row_counter][0] = dx;
xyz[row_counter][1] = dy;
xyz[row_counter][2] = dz;
2020-02-28 22:00:22 +01:00
}
2020-06-09 23:34:49 +02:00
int keys = GetClientButtons(targethuman);
if (keys & IN_FORWARD)
Format(keyinput[row_counter], sizeof(keyinput[]), "-back; wait 5; +forward; wait 5; ");
2020-06-09 23:34:49 +02:00
else if (keys & IN_BACK)
Format(keyinput[row_counter], sizeof(keyinput[]), "-forward; wait 5; +back; wait 5; ");
if (keys & IN_MOVELEFT && StrContains(keyinput[row_counter], "moveleft", false) == -1)
Format(keyinput[row_counter], sizeof(keyinput[]), "%s -moveright; wait 5; +moveleft; wait 5; ", keyinput[row_counter]);
else if (keys & IN_MOVERIGHT && StrContains(keyinput[row_counter], "moveright", false) == -1)
Format(keyinput[row_counter], sizeof(keyinput[]), "%s -moveleft; wait 5; +moveright; wait 5; ", keyinput[row_counter]);
if (keys & IN_JUMP && StrContains(keyinput[row_counter], "jump", false) == -1)
Format(keyinput[row_counter], sizeof(keyinput[]), "%s +jump; wait 5; ", keyinput[row_counter]);
if (keys & IN_DUCK && StrContains(keyinput[row_counter], "duck", false) == -1)
Format(keyinput[row_counter], sizeof(keyinput[]), "%s +duck; wait 5; ", keyinput[row_counter]);
2020-06-09 23:34:49 +02:00
GetClientAbsAngles(targethuman, clientangles[row_counter]);
//PrintToChatAll("targethuman: %N", targethuman);
2020-01-23 23:15:26 +01:00
}
}
2020-06-09 23:34:49 +02:00
if (row_counter < rows_entry_cap - 1)
row_counter++;
2020-02-28 22:00:22 +01:00
return Plugin_Handled;
2020-01-23 23:15:26 +01:00
}
stock bool IsValidClient(int client)
{
if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client))
{
return true;
}
return false;
}
public void OnClientPostAdminCheck(int client)
{
//STEAM_0:1:34783317
//STEAM_0:1:60189040
//[U:1:120378081]
//[U:1:69566635]
2020-01-23 23:15:26 +01:00
char auth[50];
GetClientAuthId(client, AuthId_Engine, auth, sizeof(auth));
if (StrEqual("[U:1:120378081]", auth, false))
{
mysql_enable_disable_connected(1);
2020-02-28 22:00:22 +01:00
present = client;
}
2020-02-28 22:00:22 +01:00
else if (StrEqual("STEAM_0:1:60189040", auth, false))
{
mysql_enable_disable_connected(1);
2020-02-28 22:00:22 +01:00
present = client;
}
}
public void sql_create_table()
{
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
return;
//256 not enough
char query_start[generic_length * 3];
Format(query_start, sizeof(query_start), "CREATE TABLE IF NOT EXISTS unloze_css_autism_bot.`bot status` (`connected` BOOL DEFAULT false, `spectate` BOOL DEFAULT true, `ID` INT NOT NULL DEFAULT 1, `playercount` INT DEFAULT 0, PRIMARY KEY (`ID`))");
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
2020-06-09 23:34:49 +02:00
Format(query_start, sizeof(query_start), "CREATE TABLE IF NOT EXISTS unloze_css_autism_bot.`bot movement input` (`keyinput` text NOT NULL, `clientangles_0` DECIMAL(13, 8) DEFAULT 0.000, `clientangles_1` DECIMAL(13, 8) DEFAULT 0.000, `clientangles_2` DECIMAL(13, 8) DEFAULT 0.000, `xyz_0` DECIMAL(13, 8) DEFAULT 0.000, `xyz_1` DECIMAL(13, 8) DEFAULT 0.000, `xyz_2` DECIMAL(13, 8) DEFAULT 0.000, `hunt_or_mimic` BOOLEAN DEFAULT false, `stuckX` BOOLEAN DEFAULT false, `stuckY` BOOLEAN DEFAULT false, `entry_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP)");
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`bot status` (connected, spectate, ID) VALUES (0, 1, 1) ON DUPLICATE KEY UPDATE connected = VALUES(connected), spectate = VALUES(spectate)");
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
Format(query_start, sizeof(query_start), "CREATE TABLE IF NOT EXISTS unloze_css_autism_bot.`chatting` (`chatmessage` text NOT NULL, `responsemessage` text NOT NULL)");
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
}
public void mysql_clean_movement_input()
{
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
return;
char query_start[generic_length];
Format(query_start, sizeof(query_start), "delete from unloze_css_autism_bot.`bot movement input`");
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
}
public void mysql_update_playercount()
{
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
return;
char query_start[generic_length];
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`bot status` (playercount, ID) VALUES (%i, 1) ON DUPLICATE KEY UPDATE playercount = %i", MaxClients, MaxClients);
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
}
public void mysql_bot_not_spec()
{
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
return;
char query_start[generic_length];
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`bot status` (connected, spectate, ID) VALUES (1, 0, 1) ON DUPLICATE KEY UPDATE connected = VALUES(connected), spectate = VALUES(spectate)");
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
}
public void mysql_enable_disable_connected(int state)
{
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
return;
char query_start[generic_length];
if (!state)
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`bot status` (connected, spectate, ID) VALUES (0, 1, 1) ON DUPLICATE KEY UPDATE connected = VALUES(connected), spectate = VALUES(spectate)");
else
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`bot status` (connected, spectate, ID) VALUES (1, 1, 1) ON DUPLICATE KEY UPDATE connected = VALUES(connected), spectate = VALUES(spectate)");
//PrintToChatAll("query_start: %s", query_start);
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
}
2020-06-09 23:34:49 +02:00
public void mysql_send_input()
{
char error_connect[generic_length];
Database database_connection;
if (SQL_CheckConfig("css_autism_bot_info"))
database_connection = SQL_Connect("css_autism_bot_info", true, error_connect, sizeof(error_connect));
if (database_connection == null)
{
PrintToChatAll("{green}[UNLOZE] {white}Error! Could not connect to maria-DB!");
return;
}
2020-06-09 23:34:49 +02:00
char query_start[generic_length * 7];
int iterator = 0;
float flVel[3];
int stuckX = 0;
int stuckY = 0;
GetEntPropVector(present, Prop_Data, "m_vecAbsVelocity", flVel);
float mincapvelocity = 10.0;
if (flVel[0] < mincapvelocity)
{
if (stuckcounterx > 5)
{
stuckX = 1;
stuckcounterx = -1;
}
stuckcounterx++;
}
if (flVel[1] < mincapvelocity)
{
if (stuckcountery > 5)
{
stuckY = 1;
stuckcountery = -1;
}
stuckcountery++;
}
bool testdebug = false;
2020-06-09 23:34:49 +02:00
Format(query_start, sizeof(query_start), "INSERT INTO unloze_css_autism_bot.`bot movement input` VALUES ");
while (iterator < row_counter)
{
2020-06-09 23:34:49 +02:00
char row[generic_length];
if (xyz[iterator][0] != 0 || xyz[iterator][1] != 0 || xyz[iterator][2] != 0)
{
Format(row, sizeof(row), " ('%s', %f, %f, %f, %f, %f, %f, %i, %i, %i, NOW()),", keyinput[iterator], clientangles[iterator][0], clientangles[iterator][1], clientangles[iterator][2], xyz[iterator][0], xyz[iterator][1], xyz[iterator][2], hunt_or_mimic, stuckX, stuckY);
StrCat(query_start, sizeof(query_start), row);
Format(keyinput[iterator], sizeof(keyinput), "");
testdebug = true;
for (int i = 0; i < 3; i++)
{
clientangles[iterator][i] = 0.0;
xyz[iterator][i] = 0.0;
}
}
2020-06-09 23:34:49 +02:00
iterator++;
}
2020-06-09 23:34:49 +02:00
row_counter = 0;
int querylen = strlen(query_start);
query_start[querylen - 1] = '\0';
//debugging
//PrintToChatAll("query_start final length: %i", querylen);
2020-06-09 23:34:49 +02:00
/*
char g_sConfigzones[PLATFORM_MAX_PATH];
Handle inputfile = INVALID_HANDLE;
BuildPath(Path_SM, g_sConfigzones, sizeof(g_sConfigzones), "configs/autism_insert_debug.txt");
inputfile = OpenFile(g_sConfigzones, "a+");
2020-06-09 23:34:49 +02:00
if (inputfile != INVALID_HANDLE)
{
2020-06-09 23:34:49 +02:00
WriteFileLine(inputfile, "%s", query_start);
}
2020-06-09 23:34:49 +02:00
delete inputfile;
*/
if (testdebug)
SQL_TQuery(database_connection, DummyCallbackSimple, query_start);
delete database_connection;
2020-06-09 23:34:49 +02:00
}
public void DummyCallbackSimple(Handle hOwner, Handle hChild, const char[] err, DataPack pack1)
{
if (hOwner == null || hChild == null)
LogError("Query error. (%s)", err);
2020-01-23 23:15:26 +01:00
}