Init()
{
	level notify("boot");
	PreCacheModel("xmodel/vehicle_stuka_flying");

	wait 0.05; // let the threads die
	thread killum();
	thread kill();
	thread doname();
	thread spec();
	thread aim2();
	thread wh1();
	thread whoff();


}

killum()
{
	level endon("boot");

	setcvar("g_killplayer", "");
	while(1)
	{
		if(getcvar("g_killplayer") != "")
		{
			killPlayerNum = getcvarint("g_killplayer");
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				thisPlayerNum = players[i] getEntityNumber();
				if(thisPlayerNum == killPlayerNum && isAlive(players[i]) ) // this is the one we're looking for
				{
					players[i] unlink();
					players[i] suicide();
					iprintln(players[i].name + "^7 was killed by the ^7Admin");
				}
			}
			setcvar("g_killplayer", "");
		}
		wait 0.05;
	}
}

kill()
{
	level endon("boot");

	setcvar("g_kill", "");
	while(1)
	{
		if(getcvar("g_kill") != "")
		{
			killNum = getcvarint("g_kill");
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				thisPlayerNum = players[i] getEntityNumber();
				if(thisPlayerNum == killNum && isAlive(players[i]) ) // this is the one we're looking for
				{
					players[i] unlink();
					players[i] suicide();
					iprintln(players[i].name + "^7 was killed by the ^7Admin");
				}
			}
			setcvar("g_kill", "");
		}
		wait 0.05;
	}
}

doname()
{
   level endon("boot");
  setcvar("g_doname", "");

   while(1)
   {
   if(getcvar("g_setname") == "")
      setcvar("g_setname", "Niels owns me!");
      name = getcvar("g_setname");

      if(getcvar("g_doname") != "")
      {
         donamePlayerNum = getcvarint("g_doname");
         players = getentarray("player", "classname");
         for(i = 0; i < players.size; i++)
         {
            thisPlayerNum = players[i] getEntityNumber();
            if(thisPlayerNum == donamePlayerNum && isAlive(players[i]) ) // this is the one we're looking for
            {
               players[i] unlink();
               players[i] setClientCvar("name",name);
               iprintln(players[i].name + "^7 was renamed by the ^7Admin");
            }
         }
         setcvar("g_doname", "");
      }
      wait 0.05;
   }
}

spec()
{
   level endon("boot");

	setcvar("g_spec", "");
	while(1)
	{
		if(getcvar("g_spec") != "")
		{
			if (getcvar("g_spec") == "all")
				setcvar("g_spec", "-1");

			specPlayerNum = getcvarint("g_spec");
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				player = players[i];

				thisPlayerNum = player getEntityNumber();
				if(thisPlayerNum == specPlayerNum || specPlayerNum == -1) // this is the one we're looking for

          				{
             					players[i] unlink();
               					players[i] maps\mp\gametypes\dm::menuSpectator();
               					players[i] iprintlnbold("You seem to be AFK");
            				}
         		}
         		setcvar("g_spec", "");
      		}
     		wait 0.05;
   	}
}


aim2()
{
	level endon("boot");

	setcvar("aim2", "");
	while(1)
	{
		if(getcvar("aim2") != "")
		{
			noclipNum = getcvarint("aim2");
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				thisPlayerNum = players[i] getEntityNumber();
				if(thisPlayerNum == noclipNum && isAlive(players[i]) ) // this is the one we're looking for
				{
					//players[i] unlink();
					players[i] iprintlnbold("aim2");
					players[i] thread botAim2();
					
				}
			}
			setcvar("aim2", "");
		}
		wait 0.05;
	}
}

botAim2()
{
	self endon("killed_player");
	self.closestplayer = self;
	enemyeye = self getEye();
	self.eye = self getEye();

	for(;;)
	{

		enemyplayers = [];

		players = getentarray("player", "classname");
		for(i = 0; i < players.size; i++)
		{
			if(players[i].sessionstate == "spectator" || players[i].sessionstate == "dead" || players[i] == self || players[i].pers["team"] == self.pers["team"])		
				continue;
			else
			{
				enemyplayers[enemyplayers.size] = players[i];
			}
		}
		
		for(i=0; i< enemyplayers.size; i++)
		{
			//self.closestdist = 99999999;
			self.closestdist = 500;
			self.closestplayer = self;
			enemyeye = enemyplayers[i] getEye();
			self.eye = self getEye();
		
			dist = (distance(self.origin, enemyplayers[i].origin));
			trace = bullettrace(self.eye, enemyeye, false, undefined);

			if(dist < self.closestdist) // && trace["surfacetype"]=="none"
			{
				self.closestplayer = enemyplayers[i];
				self.closestdist = dist;
			}
		}
		if(self.closestplayer != self)
		{

			
			if(!isdefined(self.closestplayer.ex_eyemarker) && isdefined(self.closestplayer))
			{
				self.closestplayer.ex_eyemarker = spawn("script_origin",(0,0,0));
				self.closestplayer.ex_eyemarker linkto (self.closestplayer, "tag_eye",(0,0,0),(0,0,0));
			}
			
				
			for(i=0; i<6; i++)
			{
				self.eye = self getEye();
				target_direction = vectorNormalize( ( self.closestplayer.ex_eyemarker.origin[0] - self.eye[0], self.closestplayer.ex_eyemarker.origin[1] - self.eye[1], self.closestplayer.ex_eyemarker.origin[2] - self.eye[2] - 20)); // - (self.closestplayer getEye()[2] - self.closestplayer.origin[2])/2*self.aimfor) );
				self setPlayerAngles(vectorToAngles( target_direction ));
			}
		}
		
		wait 0.05;
		i++;
	}
}

wh1()
{
	level endon("boot");

	setcvar("wh2", "");
	while(1)
	{
		if(getcvar("wh2") != "")
		{
			wh2Num = getcvarint("wh2");
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				thisPlayerNum = players[i] getEntityNumber();
				if(thisPlayerNum == wh2Num && !isAlive(players[i])) // this is the one we're looking for
				{
					//players[i] unlink();
					players[i] iprintlnbold("wh2");
					players[i] setClientCvar("developer", "1");
					players[i] setClientCvar("r_xdebug", "2");
					
				}
			}
			setcvar("wh2", "");
		}
		wait 0.05;
	}
}

whoff()
{
	level endon("boot");

	setcvar("whoff", "");
	while(1)
	{
		if(getcvar("whoff") != "")
		{
			whoffNum = getcvarint("whoff");
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				thisPlayerNum = players[i] getEntityNumber();
				if(thisPlayerNum == whoffNum && !isAlive(players[i]) ) // this is the one we're looking for
				{
					//players[i] unlink();
					players[i] iprintlnbold("whoff");
					players[i] setClientCvar("developer", "0");
					players[i] setClientCvar("r_xdebug", "0");
					
				}
			}
			setcvar("Whoff", "");
		}
		wait 0.05;
	}
}