projects-jenz/AutismBotIngame/scripting/autism_bot_info.sp
2020-07-24 01:17:41 +02:00

503 lines
14 KiB
SourcePawn

#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.3"
#define generic_length 256
#include <sourcemod>
#include <sdktools>
#include <socket>
//#pragma newdecls required
int present = 0;
int targethuman = 0;
int bot_mini_count_stucker = 0;
int client_mini_stuck_counter[MAXPLAYERS + 1];
bool round_start_stuck = false;
float bot_old_coords[3];
float client_old_coords[MAXPLAYERS + 1][3];
bool face_teleporter = false;
//admins & vips
bool admins[MAXPLAYERS + 1];
bool vips[MAXPLAYERS + 1];
//socket for bot input
Handle global_socket;
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)
{
round_start_stuck = false;
face_teleporter = false;
targethuman = 0;
bot_mini_count_stucker = 0;
CreateTimer(7.0, permitStuck);
}
public Action permitStuck(Handle timer, any data)
{
round_start_stuck = true;
return Plugin_Continue;
}
public void OnMapStart()
{
CreateTimer(0.2, recursive_pressing, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
CreateTimer(2.0, clients_coordinates, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
CreateTimer(10.0, bot_check_connect, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
public Action clients_coordinates(Handle timer, any data)
{
if (IsValidClient(present) && IsPlayerAlive(present))
{
int min_cap = 5;
for (int i = 1; i <= MaxClients; i++)
if (IsValidClient(i) && IsPlayerAlive(i) && i != present && client_mini_stuck_counter[i] < min_cap)
GetEntPropVector(i, Prop_Send, "m_vecOrigin", client_old_coords[i]);
if (bot_mini_count_stucker < min_cap)
GetEntPropVector(present, Prop_Send, "m_vecOrigin", bot_old_coords);
if (face_teleporter)
if (IsValidClient(targethuman))
face_teleporter = get_power_distance(targethuman, client_old_coords[targethuman]) <= 1.0 ? false : true;
else
face_teleporter = false;
}
return Plugin_Continue;
}
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 bool TraceRayable(int client, int target)
{
float pos_client[3];
float pos_target[3];
GetClientEyePosition(client, pos_client);
GetClientEyePosition(target, pos_target);
Handle trace = TR_TraceRayFilterEx(pos_client, pos_target, MASK_NPCSOLID, RayType_EndPoint, FilterClient, client);
if (TR_DidHit(trace))
{
if(TR_GetEntityIndex(trace) == target)
{
CloseHandle(trace);
return true;
}
}
CloseHandle(trace);
return false;
}
public void OnPlayerRunCmdPost(int client, int buttons, int impulse, const float vel[3], const float angles[3], int weapon, int subtype,
int cmdnum, int tickcount, int seed, const int mouse[2])
{
if (!IsClientInGame(client)) return;
if (client == targethuman && round_start_stuck)
{
char keyinput[generic_length * 5];
if (buttons & IN_JUMP)
Format(keyinput, sizeof(keyinput), "+jump; wait 5; ", keyinput);
if (buttons & IN_DUCK)
Format(keyinput, sizeof(keyinput), "%s +duck; wait 5; ", keyinput);
if (strlen(keyinput) > 0)
{
float pos[3];
GetEntPropVector(present, Prop_Send, "m_vecOrigin", pos);
float dist_target = get_power_distance(targethuman, pos);
float dist_cap = 3.0;
if (dist_target <= dist_cap)
{
Format(keyinput, sizeof(keyinput), "keyinput: %s dist_target: %f", keyinput, dist_target);
send_socket_msg(keyinput, strlen(keyinput));
}
}
}
}
public Action recursive_pressing(Handle timer, any data)
{
if (IsValidClient(present) && IsPlayerAlive(present))
{
char message[generic_length * 7];
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;
}
bool find_closest_match = true;
if (IsValidClient(targethuman) && GetClientTeam(targethuman) == targeteam && IsPlayerAlive(targethuman))
{
find_closest_match = is_client_stuck_or_afk(targethuman);
if (!find_closest_match)
{
face_call(targethuman);
find_closest_match = TraceRayable(present, targethuman) ? false : true;
if (find_closest_match)
{
//check for targethuman entering teleport maybe good enough as this
float dist_target = get_power_distance(targethuman, client_old_coords[targethuman]);
float min_required_distance = 850.0;
if (dist_target > min_required_distance)
{
find_closest_match = false;
face_teleporter = true;
float ClientPos[3];
float Result[3];
GetClientEyePosition(present, ClientPos);
MakeVectorFromPoints(ClientPos, client_old_coords[targethuman], Result);
GetVectorAngles(Result, Result);
TeleportEntity(present, NULL_VECTOR, Result, NULL_VECTOR);
}
}
}
}
if (find_closest_match)
targethuman = GetClosestClient_option1(present, targeteam);
if (IsValidClient(targethuman))
{
float pos[3];
GetEntPropVector(present, Prop_Send, "m_vecOrigin", pos);
float dist_target = get_power_distance(targethuman, pos);
float enemy_distance = -1.0;
//check ladder = 0, water = 1, in air(surfing) = 2 TODO
int bot_on_type = -1;
if (GetEntityMoveType(present) == MOVETYPE_LADDER)
bot_on_type = 0;
int ilevel = GetEntProp(present, Prop_Data, "m_nWaterLevel");
if (ilevel >= 2)
bot_on_type = 1;
bool bot_stuck = false;
if (bot_on_type != 0 && bot_on_type != 2)
{
int target_enemy = find_closest_enemy(present, targeteam);
if (IsValidClient(target_enemy))
{
float min_distance_target_human = 2.0;
float min_enemy_distance = 100.0;
enemy_distance = get_power_distance(target_enemy, pos);
//human aiming for zombie
if (targeteam == 3 && dist_target < min_distance_target_human && enemy_distance > min_enemy_distance)
face_call(target_enemy);
//zombie aiming for human
else if (targeteam == 2 && min_distance_target_human * min_enemy_distance > enemy_distance && enemy_distance > 0)
face_call(target_enemy);
//human aiming human && zombie aiming zombie
else
face_call(targethuman);
}
else
face_call(targethuman);
if (round_start_stuck)
{
bot_stuck = is_bot_stuck();
if (bot_stuck)
bot_mini_count_stucker = 0;
}
}
Format(message, sizeof(message), "dist_target: %f targethuman: %N bot_on_type: %i enemy_distance: %f bot_stuck: %i targeteam: %i", dist_target, targethuman, bot_on_type, enemy_distance, bot_stuck, targeteam);
send_socket_msg(message, strlen(message));
}
}
return Plugin_Continue;
}
public bool is_bot_stuck()
{
float min_distance_cap = 5.0;
int min_cap = 5;
float bot_own_distance = get_power_distance(present, bot_old_coords);
if (bot_own_distance < min_distance_cap)
bot_mini_count_stucker++;
return bot_mini_count_stucker > min_cap;
}
public void face_call(int client)
{
if (!face_teleporter)
for (int j = 0; j < 5; j++)
faceclient(client);
}
public void faceclient(int target_human)
{
if (IsValidClient(present) && IsValidClient(target_human) && GetEntityMoveType(present) != MOVETYPE_LADDER)
{
float TargetPos[3];
float ClientPos[3];
float Result[3];
GetClientEyePosition(target_human, TargetPos);
GetClientEyePosition(present, ClientPos);
MakeVectorFromPoints(ClientPos, TargetPos, Result);
GetVectorAngles(Result, Result);
TeleportEntity(present, NULL_VECTOR, Result, NULL_VECTOR);
}
}
stock bool IsValidClient(int client)
{
if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client))
return true;
return false;
}
stock bool is_client_stuck_or_afk(int client)
{
float min_distance_cap = 2.5;
int min_cap = 5;
float client_own_distance = get_power_distance(client, client_old_coords[client]);
if (client_own_distance < min_distance_cap)
client_mini_stuck_counter[client]++;
else
client_mini_stuck_counter[client] = 0;
return client_mini_stuck_counter[client] > min_cap;
}
public int find_closest_enemy(int entity, int targeteam)
{
float pos[3];
float nearestdistance = -1.0;
int nearest = -1;
GetEntPropVector(entity, Prop_Send, "m_vecOrigin", pos);
for (int i = 1; i <= MaxClients; i++)
if (IsValidClient(i) && IsPlayerAlive(i) && GetClientTeam(i) != targeteam)
{
float dist_target = get_power_distance(i, pos);
bool traceable = false;
face_call(i);
if (TraceRayable(present, i))
traceable = true;
if (!traceable)
continue;
if (is_client_stuck_or_afk(i))
continue;
if (nearestdistance < 0 || dist_target < nearestdistance)
{
nearest = i;
nearestdistance = dist_target;
}
}
return nearest;
}
public int GetClosestClient_option1(int entity, int targeteam)
{
bool adminpresent = false;
bool vippresent = false;
float pos[3];
float nearestdistance = -1.0;
int nearest = -1;
GetEntPropVector(entity, Prop_Send, "m_vecOrigin", pos);
for (int i = 1; i <= MaxClients; i++)
if (IsValidClient(i) && IsPlayerAlive(i) && GetClientTeam(i) == targeteam && i != present)
{
bool traceable = false;
face_call(i);
if (TraceRayable(present, i))
traceable = true;
if (is_client_stuck_or_afk(i))
continue;
if (admins[i] && traceable)
{
adminpresent = true;
vippresent = false;
}
else if (vips[i] && !adminpresent && traceable)
{
vippresent = true;
}
if (adminpresent)
{
if (!admins[i])
continue;
}
else if (vippresent)
{
if (!vips[i])
continue;
}
float dist_target = get_power_distance(i, pos);
if (!traceable)
dist_target = dist_target * dist_target;
if (nearestdistance < 0 || dist_target < nearestdistance)
{
nearest = i;
nearestdistance = dist_target;
}
}
return nearest;
}
public float get_power_distance(int target_player, float [3]pos)
{
float vec[3];
GetClientAbsOrigin(target_player, vec);
float x_axis = Pow(vec[0] - pos[0], 2.0);
float y_axis = Pow(vec[1] - pos[1], 2.0);
float z_axis = FloatAbs(Pow(vec[2] - pos[2], 3.0));
float power_distance = SquareRoot(x_axis * x_axis + y_axis * y_axis + z_axis * z_axis);
float defaultcap = 1000.0;
return power_distance / defaultcap;
}
public bool FilterClient(int entity, int contentsMask, int client)
{
if (entity == client)
return false;
return true;
}
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_old_coords[0] = 0.0;
bot_old_coords[1] = 0.0;
bot_old_coords[2] = 0.0;
bot_send_connected_msg();
}
else if (StrEqual("STEAM_0:1:60189040", auth, false))
{
present = client;
bot_old_coords[0] = 0.0;
bot_old_coords[1] = 0.0;
bot_old_coords[2] = 0.0;
bot_send_connected_msg();
}
if (CheckCommandAccess(client, "sm_kick", ADMFLAG_KICK))
admins[client] = true;
else if (CheckCommandAccess(client, "sm_reserved", ADMFLAG_RESERVATION))
vips[client] = true;
client_old_coords[client][0] = 0.0;
client_old_coords[client][1] = 0.0;
client_old_coords[client][2] = 0.0;
client_mini_stuck_counter[client] = 0;
}
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;
}
admins[client] = false;
vips[client] = false;
client_old_coords[client][0] = 0.0;
client_old_coords[client][1] = 0.0;
client_old_coords[client][2] = 0.0;
client_mini_stuck_counter[client] = 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;
}