//---------------------------------------------------------------------------
// AI Name Generator v1.1
// Takes strings below to piece together a name
// Created by Keen - http://radiantage.com
// Overrides server settings on bots, gives them custom skins, creates male,
// female, bioderm, and actual bot bots. They will have a random skill level,
// and will be randomly aggressive.

$RandomBotGenName[0] = "Demon";
$RandomBotGenName[1] = "Zabutom";
$RandomBotGenName[2] = "Azarok";
$RandomBotGenName[3] = "Murkhofud";
$RandomBotGenName[4] = "Zhek";
$RandomBotGenName[5] = "Yellowfang";
$RandomBotGenName[6] = "Hrreshig";
$RandomBotGenName[7] = "Marakh";
$RandomBotGenName[8] = "Kolkhris";
$RandomBotGenName[9] = "Ribshatter";
$RandomBotGenName[10] = "Chol";
$RandomBotGenName[11] = "Gerekh";
$RandomBotGenName[12] = "Suntalon";
$RandomBotGenName[13] = "Omakhros";
$RandomBotGenName[14] = "Arhakhral";
$RandomBotGenName[15] = "Lunatic";
$RandomBotGenName[16] = "Stoneskin";
$RandomBotGenName[17] = "Zhor";
$RandomBotGenName[18] = "Khan";
$RandomBotGenName[19] = "Morax";
$RandomBotGenName[20] = "Mrazhurr";
$RandomBotGenName[21] = "Skullcrusher";
$RandomBotGenName[22] = "Boneboiler";
$RandomBotGenName[23] = "Murder Beast";
$RandomBotGenName[24] = "Zura";
$RandomBotGenName[25] = "Durakh Zon";
$RandomBotGenName[26] = "Mormoghast";
$RandomBotGenName[27] = "Devilclaw";
$RandomBotGenName[28] = "Scarred-by-Plasma";
$RandomBotGenName[29] = "Ku Annorkh";
$RandomBotGenName[30] = "Tribekiller";
$RandomBotGenName[31] = "Bluefang";
$RandomBotGenName[32] = "Dreadbite";
$RandomBotGenName[33] = "Skarjett";
$RandomBotGenName[34] = "Irokhirr";
$RandomBotGenName[35] = "Mercury";
$RandomBotGenName[36] = "Liverthief";
$RandomBotGenName[37] = "Tumoz Agarh";
$RandomBotGenName[38] = "Malevolox";
$RandomBotGenName[39] = "Monkeykiller";
$RandomBotGenName[40] = "Mordhul Nom";
$RandomBotGenName[41] = "Octogonapus";
$RandomBotGenName[42] = "Poring";
$RandomBotGenName[43] = "Alex";
$RandomBotGenName[44] = "Terminator";
$RandomBotGenName[45] = "Tuborg";
$RandomBotGenName[46] = "wragmuffin";
$RandomBotGenName[47] = "Nite";
$RandomBotGenName[48] = "Death Strike";
$RandomBotGenName[49] = "Keen Machine";
$RandomBotGenName[50] = "Weebl";
$RandomBotGenName[51] = "Candlejack";
$RandomBotGenName[52] = "Lemming";
$RandomBotGenName[53] = "Nebulon";
$RandomBotGenName[53] = "Trogdor";
$RandomBotGenName[54] = "Fishbait";
$RandomBotGenName[55] = "Skulker";
$RandomBotGenName[56] = "Dogstar";
$RandomBotGenName[57] = "Bonehead";
$RandomBotGenName[58] = "Torus";
$RandomBotGenName[59] = "Glitter";
$RandomBotGenName[60] = "Ironbreath";
$RandomBotGenName[61] = "Jetsam";
$RandomBotGenName[62] = "Retch";
$RandomBotGenName[63] = "Hotfoot";
$RandomBotGenName[64] = "Zigzag";
$RandomBotGenName[65] = "Troglodyte";
$RandomBotGenName[66] = "Simrionic";
$RandomBotGenName[67] = "Cathode Kiss";
$RandomBotGenName[68] = "Demonshriek";
$RandomBotGenName[69] = "Terrapin";
$RandomBotGenName[70] = "Perilous";
$RandomBotGenName[71] = "Nova-9";
$RandomBotGenName[72] = "Hotspur";
$RandomBotGenName[73] = "DustWitch";
$RandomBotGenName[74] = "Mohican";
$RandomBotGenName[75] = "Punch";
$RandomBotGenName[76] = "Mameluke";
$RandomBotGenName[77] = "Sorrow";
$RandomBotGenName[78] = "Neon Blossom";
$RandomBotGenName[79] = "Shiver";
$RandomBotGenName[80] = "GLaDOS";
$RandomBotGenName[81] = "DuKat";
$RandomBotGenName[82] = "Inu";
$RandomBotGenName[83] = "Elwood";
$RandomBotGenName[84] = "GLaDOS";
$RandomBotGenName[85] = "Chopper D";
$RandomBotGenName[86] = "Negromancer";
$RandomBotGenName[87] = "Cow";
$RandomBotGenName[88] = "Chicken";
$RandomBotGenName[89] = "Shazbot";
$RandomBotGenName[90] = "Solid";
$RandomBotGenName[91] = "Deepy";
$RandomBotGenName[92] = "DuoFire";
$RandomBotGenName[93] = "Primus";
$RandomBotGenName[94] = "Jazz";
$RandomBotGenName[95] = "Darkness";
$RandomBotGenName[96] = "Kool-Aid";
$RandomBotGenName[97] = "Datachild";
$RandomBotGenName[98] = "Drakkar";
$RandomBotGenName[99] = "Atlas";
$RandomBotGenName[100] = "Monkey";
$RandomBotGenName[101] = "Ningyo";
$RandomBotGenName[102] = "Smiley";
$RandomBotGenName[103] = "Duster";
$RandomBotGenName[104] = "Sam";
$RandomBotGenName[105] = "Altec";
$RandomBotGenName[106] = "Phong";
$RandomBotGenName[107] = "Slash";
$RandomBotGenName[108] = "Zen";
$RandomBotGenName[109] = "Neko";
$RandomBotGenName[110] = "Hydrox";
$RandomBotGenName[111] = "Zalza";

$RandomBotGenNameCount = 112;

$RandomBotGenPrefix[0] = "Dr.";
$RandomBotGenPrefix[1] = "Private";
$RandomBotGenPrefix[2] = "Lt.";
$RandomBotGenPrefix[3] = "Major";
$RandomBotGenPrefix[4] = "General";
$RandomBotGenPrefix[5] = "Flaymaster";
$RandomBotGenPrefix[6] = "Red";
$RandomBotGenPrefix[7] = "Blood Drinker";
$RandomBotGenPrefix[8] = "Gul";
$RandomBotGenPrefix[9] = "Commodore";
$RandomBotGenPrefix[10] = "Terrible";
$RandomBotGenPrefix[11] = "Horrible";
$RandomBotGenPrefix[12] = "Overlord";
$RandomBotGenPrefix[13] = "Guardian";
$RandomBotGenPrefix[14] = "Stonefist";
$RandomBotGenPrefix[15] = "Maverick";
$RandomBotGenPrefix[16] = "Chainbreaker";
$RandomBotGenPrefix[17] = "Plagued";
$RandomBotGenPrefix[18] = "Obsidian";
$RandomBotGenPrefix[19] = "Chief";
$RandomBotGenPrefix[20] = "Officer";
$RandomBotGenPrefix[21] = "Mayor";
$RandomBotGenPrefix[22] = "Violent";
$RandomBotGenPrefix[23] = "Deep One";
$RandomBotGenPrefix[24] = "Destroyer";
$RandomBotGenPrefix[25] = "Wrecker";
$RandomBotGenPrefix[26] = "Slayer";
$RandomBotGenPrefix[27] = "Sgt.";
$RandomBotGenPrefix[28] = "Admiral";
$RandomBotGenPrefix[29] = "Seeker";
$RandomBotGenPrefix[30] = "Slicer";
$RandomBotGenPrefix[31] = "Vanguard";
$RandomBotGenPrefix[32] = "Heretic";
$RandomBotGenPrefix[33] = "Velomancer";
$RandomBotGenPrefix[34] = "Widowmaker";
$RandomBotGenPrefix[35] = "Devourer";
$RandomBotGenPrefix[36] = "Backbreaker";
$RandomBotGenPrefix[37] = "Manslayer";
$RandomBotGenPrefix[38] = "Rockthrower";
$RandomBotGenPrefix[39] = "Anklebiter";
$RandomBotGenPrefix[39] = "Technician";
$RandomBotGenPrefix[40] = "Oracle";
$RandomBotGenPrefix[41] = "Master";
$RandomBotGenPrefix[42] = "Defender";
$RandomBotGenPrefix[43] = "Symmetriad";
$RandomBotGenPrefix[44] = "Brain Slapper";
$RandomBotGenPrefix[45] = "Terrorist";
$RandomBotGenPrefix[46] = "Elemental";
$RandomBotGenPrefix[47] = "Corporal";

$RandomBotGenPrefixCount = 48;

$RandomBotGenSuffix[0] = "the Strong";
$RandomBotGenSuffix[1] = "the Invincible";
$RandomBotGenSuffix[2] = "of Steel";
$RandomBotGenSuffix[3] = "of Stone";
$RandomBotGenSuffix[4] = "of Fire";
$RandomBotGenSuffix[5] = "of the Bear";
$RandomBotGenSuffix[6] = "of the Tiger";
$RandomBotGenSuffix[7] = "the Broken";
$RandomBotGenSuffix[8] = "Jenkins";
$RandomBotGenSuffix[9] = "the Outcast";
$RandomBotGenSuffix[10] = "the Stinger";
$RandomBotGenSuffix[11] = "the Proctologist";
$RandomBotGenSuffix[12] = "the Cannon";
$RandomBotGenSuffix[13] = "the Alpha";
$RandomBotGenSuffix[14] = "the Maverick";
$RandomBotGenSuffix[15] = "of Potatoes";
$RandomBotGenSuffix[16] = "of Bombs";
$RandomBotGenSuffix[17] = "of Knives";
$RandomBotGenSuffix[18] = "of Blades";
$RandomBotGenSuffix[19] = "the Rotten";
$RandomBotGenSuffix[20] = "the Outlaw";
$RandomBotGenSuffix[21] = "the Stryker";
$RandomBotGenSuffix[22] = "the Flayer";
$RandomBotGenSuffix[23] = "Cosby";
$RandomBotGenSuffix[24] = "Bot";
$RandomBotGenSuffix[25] = "the Raven";
$RandomBotGenSuffix[26] = "the Mighty";
$RandomBotGenSuffix[27] = "the Brave";
$RandomBotGenSuffix[28] = "the Coward";
$RandomBotGenSuffix[29] = "the Mind-Taker";
$RandomBotGenSuffix[30] = "the Corruptor";
$RandomBotGenSuffix[31] = "the Hallowed";
$RandomBotGenSuffix[32] = "the Risen";
$RandomBotGenSuffix[33] = "of Champions";
$RandomBotGenSuffix[34] = "the Legend";
$RandomBotGenSuffix[35] = "Payne";
$RandomBotGenSuffix[36] = "the Wise";
$RandomBotGenSuffix[37] = "of Ice";
$RandomBotGenSuffix[38] = "the Honored";
$RandomBotGenSuffix[39] = "of Glory";
$RandomBotGenSuffix[40] = "the Dark";
$RandomBotGenSuffix[41] = "the Burninator";
$RandomBotGenSuffix[42] = "the Golden";
$RandomBotGenSuffix[43] = "Deathwind";
$RandomBotGenSuffix[44] = "Evenkill";
$RandomBotGenSuffix[45] = "Oddkill";
$RandomBotGenSuffix[46] = "the Omega";
$RandomBotGenSuffix[47] = "the Insane";
$RandomBotGenSuffix[48] = "the Juicer";
$RandomBotGenSuffix[49] = "Griffin";
$RandomBotGenSuffix[50] = "the Boss";
$RandomBotGenSuffix[51] = "Rogers";
$RandomBotGenSuffix[52] = "Seuss";
$RandomBotGenSuffix[53] = "Bond";
$RandomBotGenSuffix[54] = "Skywalker";
$RandomBotGenSuffix[55] = "Einstien";
$RandomBotGenSuffix[56] = "McDonald";
$RandomBotGenSuffix[57] = "O'Brien";
$RandomBotGenSuffix[58] = "Hawking";
$RandomBotGenSuffix[59] = "the Toothfairy";
$RandomBotGenSuffix[60] = "Clinton";

$RandomBotGenSuffixCount = 61;

// Generate name
function AIGenerateRandomName()
{
     %prefix = getRandom(1);
     %suffix = getRandom(1);
     %name = "";
     %tries = 0;
          
     if(%prefix)
          %name = $RandomBotGenPrefix[getRandom($RandomBotGenPrefixCount)];

     %rnd = getRandom($RandomBotGenNameCount);
     
     // Make sure we don't have any duplicates
     if($RandomBotNameAllocated[%rnd])
     {
          // Choose another unused bot name until we've found a free one...
          // If this fails (runs out of tries) it will be inevitable and use the
          // last generated one
          while($RandomBotNameAllocated[%rnd] && (%tries < $RandomBotGenNameCount))
          {
               %rnd = getRandom($RandomBotGenNameCount);
               %tries++;
          }

          // If we do overflow, just start over          
          if(%tries == $RandomBotGenNameCount)
               AIClearUsedNamePool();
     }

     // Mark this name as taken
     $RandomBotNameAllocated[%rnd] = true;
          
     %mainName = $RandomBotGenName[%rnd];
     
     // Sanity check - somehow name is blank, this solves the problem although 
     // potentially counter-productive...
     if(%mainName $= "")
          %mainName = $RandomBotGenName[getRandom($RandomBotGenNameCount)];
     
     %name = %name $= "" ? %mainName : %name SPC %mainName;

     if(%suffix)
          %name = %name SPC $RandomBotGenSuffix[getRandom($RandomBotGenSuffixCount)];
     
     return %name;
}

function AIClearUsedNamePool()
{
     %count = 0;
     
     while(%count < $RandomBotGenNameCount)
     {
          $RandomBotNameAllocated[%count] = false;
          %count++;
     }
}

function AIConnection::onAIConnect(%client, %name, %team, %skill, %offense, %voice, %voicePitch)
{
   if(%skill $= "")
      %skill = getRandom();

   %name = AIGenerateRandomName();

   // Sex/Race defaults
   %client.race = getRandom(1) ? "Human" : "Bioderm";

   if(%client.race $= "Human")
        %client.sex = getRandom(1) ? "Male" : "Female";
   else
        %client.sex = "Male";

   %client.armor = getRandom(1) ? "Light" : "Medium";

   if(%client.race $= "Human" && %client.sex $= "Male")
        %voice = getRandom(1) ? "Bot1" : "Male"@getRandom(1,5);
   else if(%client.race $= "Human" && %client.sex $= "Female")
        %voice = "Fem"@getRandom(1,5);
   else
        %voice = "Derm"@getRandom(1,3);

   %client.voice = %voice;
   %client.voiceTag = addTaggedString(%voice);

//   if (%voicePitch $= "" || %voicePitch < 0.5 || %voicePitch > 2.0)
//      %voicePitch = 1.0;
   %voicePitch = 1 - ((getRandom(20) - 10)/100);
   %client.voicePitch = %voicePitch;

   %client.name = addTaggedString( "\cp\c9" @ %name @ "\co" );
   %client.nameBase = %name;

   echo(%client.name);
   echo("CADD: " @ %client @ " " @ %client.getAddress());
   $HostGamePlayerCount++;

   //set the initial team - Game.assignClientTeam() should be called later on...
   %client.team = %team;

   if(%voice $= "Bot1")
        %skin = %client.team == 1 ? "basebot" : "basebbot";
   else if(%client.race $= "Human")
        %skin = %client.team == 1 ? "beagle" : "swolf";
   else
        %skin = %client.team == 1 ? "base" : "baseb";

//   if ( %client.team & 1 )
   %client.skin = addTaggedString(%skin);

	//setup the target for use with the sensor net, etc...
   %client.target = allocClientTarget(%client, %client.name, %client.skin, %client.voiceTag, '_ClientConnection', 0, 0, %client.voicePitch);

   if($currentMissionType $= "SinglePlayer")
		messageAllExcept(%client, -1, 'MsgClientJoin', "", %name, %client, %client.target, true);
   else
	   messageAllExcept(%client, -1, 'MsgClientJoin', '\c1%1 joined the game.', %name, %client, %client.target, true);

	//assign the skill
	%client.setSkillLevel(%skill);

	//assign the affinity
   %client.offense = getRandom(1) == 1 ? true : false;

   //clear any flags
   %client.stop(); // this will clear the players move state
   %client.clearStep();
   %client.lastDamageClient = -1;
   %client.lastDamageTurret = -1;
   %client.setEngageTarget(-1);
   %client.setTargetObject(-1);
   %client.objective = "";

	//clear the defaulttasks flag
	%client.defaultTasksAdded = false;

	//if the mission is already running, spawn the bot
   if ($missionRunning)
      %client.startMission();
}

function RndAIAssignClientTeam(%game, %client, %respawn)
{
//error("DefaultGame::assignClientTeam");
   // this function is overwritten in non-team mission types (e.g. DM)
   // so these lines won't do anything
   //if(!%game.numTeams)
   //{
   //   setTargetSkin(%client.target, %client.skin);
   //   return;
   //}

   //  camera is responsible for creating a player
   //  - counts the number of players per team
   //  - puts this player on the least player count team
   //  - sets the client's skin to the servers default

   %numPlayers = ClientGroup.getCount();
   for(%i = 0; %i <= %game.numTeams; %i++)
      %numTeamPlayers[%i] = 0;

   for(%i = 0; %i < %numPlayers; %i = %i + 1)
   {
      %cl = ClientGroup.getObject(%i);
      if(%cl != %client)
         %numTeamPlayers[%cl.team]++;
   }
   %leastPlayers = %numTeamPlayers[1];
   %leastTeam = 1;
   for(%i = 2; %i <= %game.numTeams; %i++)
   {
      if( (%numTeamPlayers[%i] < %leastPlayers) ||
         ( (%numTeamPlayers[%i] == %leastPlayers) &&
         ($teamScore[%i] < $teamScore[%leastTeam] ) ))
      {
         %leastTeam = %i;
         %leastPlayers = %numTeamPlayers[%i];
      }
   }

   %client.team = %leastTeam;
   %client.lastTeam = %team;

   // Assign the team skin: - disabled here so bots don't get weird skins on mission change - keen
//   if ( %client.isAIControlled() )
//   {
//      if ( %leastTeam & 1 )
//      {
//         %client.skin = addTaggedString( "basebot" );
//         setTargetSkin( %client.target, 'basebot' );
//      }
//      else
//      {
//         %client.skin = addTaggedString( "basebbot" );
//         setTargetSkin( %client.target, 'basebbot' );
//      }
//   }
//   else
      setTargetSkin( %client.target, %game.getTeamSkin(%client.team) );
      //setTargetSkin( %client.target, %client.skin );

   // might as well standardize the messages
   //messageAllExcept( %client, -1, 'MsgClientJoinTeam', '\c1%1 joined %2.', %client.name, $teamName[%leastTeam], %client, %leastTeam );
   //messageClient( %client, 'MsgClientJoinTeam', '\c1You joined the %2 team.', $client.name, $teamName[%client.team], %client, %client.team );
   messageAllExcept( %client, -1, 'MsgClientJoinTeam', '\c1%1 joined %2.', %client.name, %game.getTeamName(%client.team), %client, %client.team );
   messageClient( %client, 'MsgClientJoinTeam', '\c1You joined the %2 team.', %client.name, %game.getTeamName(%client.team), %client, %client.team );

   updateCanListenState( %client );

   logEcho(%client.nameBase@" (cl "@%client@") joined team "@%client.team);
}

package RndAINameGen {

function DefaultGame::assignClientTeam(%game, %client, %respawn)
{
     if(%client.isAIControlled())
     {
          RndAIAssignClientTeam(%game, %client, %respawn);
          return;
     }
     
     Parent::assignClientTeam(%game, %client, %respawn);
}

};

activatePackage("RndAINameGen");
