/*
	TeleTo AdminMod plugin by Zero3Cool
	www.zerosplugins.1go.dk
	zero3cool@zerosplayground.dk / zero3cool@hotmail.com
 
	Changelog:
 
	[1.0] - [1.1]:
	[*] Fixed a bug causing the plugin to teleport yourself to yourself when trying to teleport a specific player only.
	[*] Fixed a minor bug causing the plugin to try teleporting a player even if there was an error.
	[*] Fixed a bug causing the plugin to search for playerinfo in slot 0, and slot maxplayers+1 (Which is impossible).
	[*] Made various changed to the source (none affecting functionality).
*/
#include <core>
#include <string>
#include <admin>
#include <adminlib>
 
new STRING_VERSION[] = "1.1";
new NextX;
new NextY;
new NextZ;
new DestX;
new DestY;
new DestZ;
 
public admin_teleto(HLCommand,HLData,HLUserName,UserIndex)
	{
	new Data[MAX_DATA_LENGTH];
	new DestTarget[MAX_DATA_LENGTH];
	new TeleTarget[MAX_DATA_LENGTH];
	new DestName[MAX_DATA_LENGTH];
	new i;
	new PlayerStatus;
	new maxplayers = maxplayercount();
	new Dead;
	new CurrentTarget[MAX_NAME_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Command[MAX_DATA_LENGTH];
	new SlotNumber = 0;
	new Team;
 
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLCommand,Command,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_DATA_LENGTH);
 
	strbreak(Data,DestTarget,TeleTarget,MAX_DATA_LENGTH);
 
	if (check_user(DestTarget)==0)
		{
		selfmessage("Destination Target Invalid.");
		return PLUGIN_HANDLED;
		}
	get_username(DestTarget,DestName,MAX_NAME_LENGTH);
	get_userorigin(DestName,DestX,DestY,DestZ);
	if ((strcmp(TeleTarget,"all")==0) || (strcmp(TeleTarget,"team1")==0) || (strcmp(TeleTarget,"team2")==0) || (strcmp(TeleTarget,"team3")==0) || (strcmp(TeleTarget,"team4")==0))
		{
			for (i=1;i<=maxplayers;i++)
			{
			PlayerStatus = playerinfo(i,CurrentTarget,MAX_NAME_LENGTH,_,_,Team,Dead);
			if (PlayerStatus != 1 || Dead == 1 || strcmp(CurrentTarget,DestName) == 0)
				{
				continue;
				}
			if (strcmp(TeleTarget,"team1")== 0 && Team != 1)
				{
				continue;
				}
			if (strcmp(TeleTarget,"team2")== 0 && Team != 2)
				{
				continue;
				}
			if (strcmp(TeleTarget,"team3")== 0 && Team != 3)
				{
				continue;
				}
			if (strcmp(TeleTarget,"team4")== 0 && Team != 4)
				{
				continue;
				}
			SlotNumber++;
			GetSlot(SlotNumber);
			teleport(CurrentTarget,NextX,NextY,NextZ);
			}
		}
	else
		{
		if (check_user(TeleTarget)==0)
			{
			selfmessage("Target Invalid.");
			return PLUGIN_HANDLED;
			}
		get_username(TeleTarget,CurrentTarget,MAX_NAME_LENGTH);
		get_userindex(CurrentTarget,UserIndex);
		PlayerStatus = playerinfo(UserIndex,CurrentTarget,MAX_NAME_LENGTH,_,_,Team,Dead);
		if (PlayerStatus != 1 || Dead == 1)
			{
			selfmessage("Error: Player is either not connected or dead");
			return PLUGIN_HANDLED;
			}
		teleport(TeleTarget,DestX+48,DestY,DestZ);
		}
	say_command(User,Command,Data);
	return PLUGIN_HANDLED;
	}
 
GetSlot(Slot)
	{
	/*
		I couldnt figure out a more smart way to calculate the next position. There probably is,
		but would it matter when there only can be 32 players max. in a HL game?
	*/
	new X = DestX;
	new Y = DestY;
	new Z = DestZ;
	if (Slot > 8)
		{
		Z = Z + 48;
		}
	if (Slot > 17)
		{
		Z = Z + 48;
		}
	if (Slot > 26)
		{
		Z = Z + 48;
		}
	if (Slot == 9 || Slot == 18 || Slot == 27)
		{
		X = X;
		Y = Y;
		}
	if (Slot == 1 || Slot == 10 || Slot == 19 || Slot == 28)
		{
		X = X + 48;
		Y = Y;
		}
	if (Slot == 2 || Slot == 11 || Slot == 20 || Slot == 29)
		{
		X = X + 48;
		Y = Y + 48;
		}
	if (Slot == 3 || Slot == 12 || Slot == 21 || Slot == 30)
		{
		X = X;
		Y = Y + 48;
		}
	if (Slot == 4 || Slot == 13 || Slot == 22 || Slot == 31)
		{
		X = X - 48;
		Y = Y + 48;
		}
	if (Slot == 5 || Slot == 14 || Slot == 23)
		{
		X = X - 48;
		Y = Y;
		}
	if (Slot == 6 || Slot == 15 || Slot == 24)
		{
		X = X - 48;
		Y = Y - 48;
		}
	if (Slot == 7 || Slot == 16 || Slot == 25)
		{
		X = X;
		Y = Y - 48;
		}
	if (Slot == 8 || Slot == 17 || Slot == 26)
		{
		X = X + 48;
		Y = Y - 48;
		}
	NextX = X;
	NextY = Y;
	NextZ = Z;
	}
 
public plugin_init()
	{
	plugin_registerinfo("Teleto plugin","Allows smart teleporting without messing around with the XYZ's",STRING_VERSION);
	plugin_registercmd("admin_teleto","admin_teleto",8192,"admin_teleto <Target> <all|team1|team2|team3|team4|Target> : Teleports <all|teamX|target> around the position of <target>.");
	return PLUGIN_CONTINUE;
	}