projects-jenz/AutismBotIngame/scripting/autism_bot_info.sp

317 lines
8.8 KiB
SourcePawn

#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.3"
#define generic_length 256
#define rows_entry_cap 6
#include <sourcemod>
#include <sdktools>
#include <socket>
//#pragma newdecls required
int present = 0;
int targethuman = 0;
int stuckcounterx = 0;
int stuckcountery = 0;
//socket for bot input
Handle global_socket;
//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;
public Plugin myinfo =
{
name = "coordinates for the bot",
author = PLUGIN_AUTHOR,
description = "hello ",
version = PLUGIN_VERSION,
url = ""
};
public void OnPluginStart()
{
//talking
RegConsoleCmd("sm_autism", cmd_talk, "talking to the bot through java application");
//hooks
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
//socket otherwise declare in public OnConfigsExecuted(){}
Handle socket = SocketCreate(SOCKET_UDP, OnSocketError);
SocketSetOption(socket, SocketReuseAddr, 1);
SocketBind(socket, "127.0.0.1", 48475);
connect(socket);
global_socket = socket;
}
public Action cmd_talk(int client, int args)
{
char msg[generic_length];
char info[generic_length];
GetCmdArgString(info, sizeof(info));
if (strlen(info) == 0)
{
PrintToChat(client, "Add a message to the command if autism bot is ingame and running on discord");
return Plugin_Handled;
}
Format(msg, sizeof(msg), "clientmessage: %s", info);
send_socket_msg(msg, strlen(msg));
return Plugin_Handled;
}
public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
{
targethuman = 0;
}
public void OnMapStart()
{
CreateTimer(0.2, recursive_pressing, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
CreateTimer(0.5, socket_send_msg, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
CreateTimer(10.0, bot_check_connect, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
public bool TraceEntityFilterPlayer(int entity, int contentsMask)
{
return (entity > GetMaxClients() || !entity);
}
public void send_socket_msg(char[] query_msg, int len)
{
if (global_socket != INVALID_HANDLE && SocketIsConnected(global_socket))
SocketSendTo(global_socket, query_msg, len, "127.0.0.1", 48477); //udp
}
public Action bot_check_connect(Handle timer, any data)
{
if (!IsValidClient(present) && GetClientCount(false) < 63)
{
char msg[generic_length];
//PrintToChatAll("sending UDP message...");
Format(msg, sizeof(msg), "connect to ze");
send_socket_msg(msg, strlen(msg));
}
return Plugin_Continue;
}
public Action socket_send_msg(Handle timer, any data)
{
if (IsValidClient(present) && IsPlayerAlive(present))
{
char message[generic_length * 7];
int iterator = 0;
int stuckcap = 12;
float flVel[3];
int stuckX = 0;
int stuckY = 0;
GetEntPropVector(present, Prop_Data, "m_vecAbsVelocity", flVel);
float mincapvelocity = 40.0;
if (flVel[0] < mincapvelocity)
{
if (stuckcounterx >= stuckcap)
{
stuckX = 1;
stuckcounterx = 0;
}
stuckcounterx++;
}
if (flVel[1] < mincapvelocity)
{
if (stuckcountery >= stuckcap)
{
stuckY = 1;
stuckcountery = 0;
}
stuckcountery++;
}
while (iterator < row_counter)
{
char row[generic_length];
if (xyz[iterator][0] != 0 || xyz[iterator][1] != 0 || xyz[iterator][2] != 0)
{
Format(row, sizeof(row), "keyinput: %s clientangles: %f %f %f xyz: %f %f %f hunt_or_mimic: %i stuckX: %i stuckY: %i \n", 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(message, sizeof(message), row);
Format(keyinput[iterator], sizeof(keyinput), "");
for (int i = 0; i < 3; i++)
{
clientangles[iterator][i] = 0.0;
xyz[iterator][i] = 0.0;
}
}
iterator++;
}
row_counter = 0;
send_socket_msg(message, strlen(message));
}
return Plugin_Continue;
}
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)
{
//2 = autismo is zm and should follow closest moving zm
targeteam = 2;
}
else
{
//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];
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;
}
}
if (IsValidClient(targethuman))
GetClientAbsOrigin(targethuman, target_human_original_coord);
}
if (IsValidClient(targethuman))
{
if (!hunt_or_mimic)
{
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;
}
int keys = GetClientButtons(targethuman);
if (keys & IN_FORWARD)
Format(keyinput[row_counter], sizeof(keyinput[]), "-back; wait 5; +forward; wait 5; ");
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]);
GetClientAbsAngles(targethuman, clientangles[row_counter]);
//PrintToChatAll("targethuman: %N", targethuman);
}
}
if (row_counter < rows_entry_cap - 1)
row_counter++;
return Plugin_Continue;
}
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]
char auth[50];
GetClientAuthId(client, AuthId_Engine, auth, sizeof(auth));
if (StrEqual("[U:1:120378081]", auth, false))
{
present = client;
bot_send_connected_msg();
}
else if (StrEqual("STEAM_0:1:60189040", auth, false))
{
present = client;
bot_send_connected_msg();
}
}
public OnSocketError(Handle socket, const int errorType, const int errorNum, any args)
{
LogError("[MR] Socket error: %d (errno %d)", errorType, errorNum);
CreateTimer(120.0, TimerConnect, socket, TIMER_HNDL_CLOSE);
}
stock void connect(Handle socket)
{
if (!SocketIsConnected(socket))
SocketConnect(socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "127.0.0.1", 48475);
}
public void OnClientDisconnect(int client)
{
if (present == client)
{
present = 0;
}
}
public void bot_send_connected_msg()
{
char msg[generic_length];
Format(msg, sizeof(msg), "autismo connected");
send_socket_msg(msg, strlen(msg));
}
//Socket callback
public OnSocketConnected(Handle socket, any arg)
{
}
//manage message
public OnSocketReceive(Handle socket, char[] receiveData, const dataSize, any hFile)
{
//PrintToChatAll("receiveData: %s", receiveData);
}
public OnSocketDisconnected(Handle socket, any arg)
{
CreateTimer(120.0, TimerConnect, socket, TIMER_HNDL_CLOSE);
}
public Action TimerConnect(Handle timer, any arg)
{
connect(arg);
return Plugin_Handled;
}