#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.00"

#include <sourcemod>
#include <sdktools>

#pragma newdecls required

int present = 0;
int targethuman = 0;

public Plugin myinfo = 
{
	name = "coordinates for the bot",
	author = PLUGIN_AUTHOR,
	description = "hello ",
	version = PLUGIN_VERSION,
	url = ""
};

public void OnClientDisconnect(int client)
{
	if (present == client)
		present = 0;
}

public void OnPluginStart()
{
	//hooks
	HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
}

public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
{
	targethuman = 0;
}

public void OnMapStart()
{
	CreateTimer(0.3, recursive_pressing, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public bool TraceEntityFilterPlayer(int entity, int contentsMask)
{
	return (entity > GetMaxClients() || !entity);
}

public Action recursive_pressing(Handle timer, any data)
{
	//PrintToChatAll("present: %N", present);
	if (present)
	{
		float client_coord[3];
		float xyz[3];
		float lowest_distance = 1000000.0;
		float pos_client[3];
		GetClientAbsOrigin(present, pos_client);
		for (int i = 1; i <= MaxClients; i++)
			if (IsValidClient(i) && IsPlayerAlive(i) && GetClientTeam(i) == 3 && i != present)
			{
				float pos_i[3];
				GetClientAbsOrigin(i, pos_i);
				float dx = pos_client[0] - pos_i[0];
				float dy = pos_client[1] - pos_i[1];
				float dz = FloatAbs(pos_client[2] - pos_i[2]);
				float dist = SquareRoot(dx*dx + dy*dy + dz*dz);
				//PrintToChatAll("dist: %f", dist);
				if (dist < lowest_distance)
				{
					lowest_distance = dist;
					targethuman = i;
					client_coord = pos_i;
					xyz[0] = dx;
					xyz[1] = dy;
					xyz[2] = dz;
				}
			}
		if (IsValidClient(targethuman))
		{
			//PrintToChatAll("targethuman: %N", targethuman);
			float clientangles[3];
			GetClientAbsAngles(targethuman, clientangles);
			int keys = GetClientButtons(targethuman);
			char keyinput[256];
			if (keys & IN_FORWARD)
			{
				StrCat(keyinput, sizeof(keyinput), "forward+");
			}
			if (keys & IN_BACK)
			{
				StrCat(keyinput, sizeof(keyinput), "back+");
			}
			if (keys & IN_MOVELEFT)
			{
				StrCat(keyinput, sizeof(keyinput), "moveleft+");
			}
			if (keys & IN_MOVERIGHT)
			{
				StrCat(keyinput, sizeof(keyinput), "moveright+");
			}
			if (keys & IN_JUMP)
			{
				StrCat(keyinput, sizeof(keyinput), "jump+");
			}
			//PrintToChatAll("keyinput: %s", keyinput);
			//PrintToChatAll("xyz[0], xyz[1], xyz[2]: %f %f %f", xyz[0], xyz[1], xyz[2]);
			//PrintToChatAll("lowest_distance: %f", lowest_distance);
			ServerCommand("sm_psay #[U:1:120378081] movement_input_specific:%s", keyinput);
			ServerCommand("sm_psay #[U:1:120378081] ct_eye_angles_:%f+%f+%f", clientangles[0], clientangles[1], clientangles[2]);
			ServerCommand("sm_psay #[U:1:120378081] player_origin_diff:%f+%f+%f", xyz[0], xyz[1], xyz[2]);
			ServerCommand("sm_psay #[U:1:120378081] player_origin_position:%f+%f+%f", client_coord[0], client_coord[1], client_coord[2]);
			ServerCommand("sm_psay #[U:1:120378081] player_lowest_distance:%f", lowest_distance);
		}
	}
	return Plugin_Handled;
}

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;
	else if (StrEqual("STEAM_0:1:60189040", auth, false))
		present = client;
}