View Single Post
Re: an idea of selecting weapons to buy
Old
  (#6)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Re: an idea of selecting weapons to buy - 29-12-2004

RB is open source, but since we like to paste code here is mine:

Code:
void BotBuyStuff (cBot * pBot)
{
 int money = pBot->bot_money; // Money
 int team = pBot->iTeam; // Team
 int buy_weapon = -1;
 
 // What to buy?
 if (pBot->buy_primary == true)
 {
  // Buy primary
  bool bBuyOther = true;
  // Personality related:
  // Check if we can buy our favorite weapon
  if (pBot->ipFavoPriWeapon > -1)
  {
   if (FUNC_BotHasWeapon (pBot, pBot->ipFavoPriWeapon) == false)
   {
	if (GoodWeaponForTeam (pBot->ipFavoPriWeapon, pBot->iTeam))
	 if (PriceWeapon (pBot->ipFavoPriWeapon) <= money)
	 {
	  // Buy favorite weapon
	  buy_weapon = pBot->ipFavoPriWeapon;
	  bBuyOther=false;
	 }	  
	 else
	 {
	  // We do here something to 'save' for our favorite weapon
	  if (RANDOM_LONG (0, 100) < pBot->ipSaveForWeapon)
	  {  // 31.08.04 Frashman forgotten brace
	   pBot->buy_primary = false;
	   bBuyOther = false;
	   pBot->buy_secondary = false; // don't buy a secondary
	   return; // get out of function, don't buy anything
	  }
	 }
   }
   else
   {
	pBot->buy_primary=false; // already have our favorite weapon
	bBuyOther=false;
	return; // get out of function, go buy a secondary weapon or something?
   }
  }
  // Normal buy code
  if (bBuyOther)
  {
   // Find weapon we can buy in the list of weapons
   for (int i = 0; i < MAX_WEAPONS; i++)
   {
	// 31.08.04 Frashman Filter Out all except PRIMARY and SHIELD 
	// SHIELD is used as primary weapon
	if ((UTIL_GiveWeaponType (weapons_table[i].iId) != PRIMARY) 
	 && (UTIL_GiveWeaponType (weapons_table[i].iId) !=SHIELD))	 
	 continue;
	if (GoodWeaponForTeam (weapons_table[i].iId, team) == false)
	 continue;
	if (weapons_table[i].price <= money)
	{
	 if (pBot->iPrimaryWeapon > -1)  // has a primary weapon
	  if (weapons_table[ListIdWeapon(pBot->iPrimaryWeapon)].priority >= 
	   weapons_table[i].priority)
	   continue;
	 if (buy_weapon == -1)
	  buy_weapon = weapons_table[i].iId;
	 else
	 {
	  if (RANDOM_LONG (0, 100) < weapons_table[i].priority)
	   buy_weapon = weapons_table[i].iId; // randomly buy a different weapon
	 }
	 // 30.8.04 Frashman fixed from 150 to 100
	 if (RANDOM_LONG (0, 100) < weapons_table[i].priority)
	  break;
	}
   }
  } // Buy a primary weapon

  if (buy_weapon != -1)
  {
   pBot->buy_primary = false;
   // depending on amount of money we have left buy a secondary weapon
   int iMoneyLeft = money - PriceWeapon (buy_weapon);
   // TODO: this should be dependant on something else... not only money
   // 01.09.04 Frashman if buyed a Shield, try to buy a good Pistol
   if (iMoneyLeft >= 600)
	if ((RANDOM_LONG (0, 100) < 15) || (pBot->iPrimaryWeapon == CS_WEAPON_SHIELD))
	 pBot->buy_secondary = true;
  }
 }
 else if (pBot->buy_secondary == true)
 {
  // Buy secondary
  bool bBuyOther = true;
  // Personality related:
  // Check if we can buy our favorite weapon
  if (pBot->ipFavoSecWeapon > -1)
  {
   if (FUNC_BotHasWeapon (pBot, pBot->ipFavoSecWeapon) == false)
   {
	if (GoodWeaponForTeam (pBot->ipFavoSecWeapon, pBot->iTeam))
	 if (PriceWeapon (pBot->ipFavoSecWeapon) < money)
	 {
	  // Buy favorite weapon
	  buy_weapon = pBot->ipFavoPriWeapon;
	  bBuyOther=false;
	 }	  
	 else
	 {
	  // We do here something to 'save' for our favorite weapon
	  if (RANDOM_LONG (0, 100) < pBot->ipSaveForWeapon)
	  {  // 31.08.04 Frashman forgotten brace
	   bBuyOther = false;
	   pBot->buy_secondary = false; // don't buy a secondary
	   return; // get out of function, don't buy anything
	  }
	 }
   }
   else
   {
	bBuyOther=false;
	return; // get out of function, go buy a secondary weapon or something?
   }
  }
	// Normal buy code
	if (bBuyOther)
	{	
	 // Buy secondary
	 // Find weapon we can buy in the list of weapons
	 for (int i = 0; i < MAX_WEAPONS; i++)
	 {
	  // When enough money and the priority is high enough..
	  // Filter out Secondary and Grenades
	  if (UTIL_GiveWeaponType (weapons_table[i].iId) != SECONDARY)
	   continue;
	  if (GoodWeaponForTeam (weapons_table[i].iId, team) == false)
	   continue;
	  if (weapons_table[i].price <= money)
	  {
	   if (pBot->iSecondaryWeapon > -1) {
		int index = weapons_table[pBot->iSecondaryWeapon].iIdIndex;
		// 31.08.04 Frashman > corrected to >= ,
		// else the bot will buy another weapon with the same priority
		if (weapons_table[index].priority >= weapons_table[i].priority)
		 continue;
	   }
	   if (buy_weapon == -1)
		buy_weapon = weapons_table[i].iId;
	   else
	   {
		if (RANDOM_LONG (0, 100) < weapons_table[i].priority)
		 buy_weapon = weapons_table[i].iId;
	   }
	   if (RANDOM_LONG (0, 100) < weapons_table[i].priority)
		break;
	  }
	 }
	}
	if (buy_weapon != -1)
	 pBot->buy_secondary = false;
 }
 else if (pBot->buy_ammo_primary == true)
 {
  // Buy primary ammo
  BuyWeapon (pBot, "6", NULL);
  pBot->buy_ammo_primary = false;
  return;
 }
 else if (pBot->buy_ammo_secondary == true)
 {
  // Buy secondary ammo
  BuyWeapon (pBot, "7", NULL);
  pBot->buy_ammo_secondary = false;
  return;
 }
 else if (pBot->buy_defusekit)
 {
  if (money >= 200)
  {
   buy_weapon = CS_DEFUSEKIT;
   pBot->buy_defusekit = false;
  }
 }
 else if (pBot->buy_armor)
 {
  if (money < 1000 && money >= 650)
  {
   // Buy light armor
   buy_weapon = CS_WEAPON_ARMOR_LIGHT;
  }
  else if (money >= 1000)
  {
   // Buy heavy armor
   buy_weapon = CS_WEAPON_ARMOR_HEAVY;
  }
  pBot->buy_armor = false;
 }
 else if (pBot->buy_grenade)
 {
  // Buy grenade
  if (money >= weapons_table[ListIdWeapon (CS_WEAPON_HEGRENADE)].price)   
   buy_weapon = CS_WEAPON_HEGRENADE;
  pBot->buy_grenade = false;
 }
 else if (pBot->buy_flashbang > 0)
 {
  // Buy flashbang
  if (money >= weapons_table[ListIdWeapon (CS_WEAPON_FLASHBANG)].price) 
  {
   buy_weapon = CS_WEAPON_FLASHBANG;
   pBot->buy_flashbang--;
  }
  else
   pBot->buy_flashbang = 0; // do not buy				 
 }
 else if (pBot->buy_smokegrenade)  //31.08.04 Frashman added Smoke Grenade support
 {
  // Buy SmokeGrenade
  if (money >= weapons_table[ListIdWeapon (CS_WEAPON_SMOKEGRENADE)].price)   
   buy_weapon = CS_WEAPON_SMOKEGRENADE;
  pBot->buy_smokegrenade = false;
 }
 if (buy_weapon != -1)
 {
  // Buy...	   
  // TODO
  // FRASHMAN 30.08.04 haven't changed the cs 1.5 buycode, maybe there are also errors
  // CS 1.5 only
  if (counterstrike == 0)
  {
   switch (buy_weapon)
   {
   case CS_WEAPON_AK47:	BuyWeapon (pBot, "4", "1"); break;
   case CS_WEAPON_DEAGLE:  BuyWeapon (pBot, "1", "3"); break;
   case CS_WEAPON_P228:	BuyWeapon (pBot, "1", "4"); break;
   case CS_WEAPON_SG552:   BuyWeapon (pBot, "4", "2"); break;
   case CS_WEAPON_SG550:   BuyWeapon (pBot, "4", "8"); break;
   case CS_WEAPON_SCOUT:   BuyWeapon (pBot, "4", "5"); break;
   case CS_WEAPON_AWP:	 BuyWeapon (pBot, "4", "6"); break;
   case CS_WEAPON_MP5NAVY: BuyWeapon (pBot, "3", "1"); break;
   case CS_WEAPON_UMP45:   BuyWeapon (pBot, "3", "5"); break;
   case CS_WEAPON_ELITE: BuyWeapon (pBot, "1", "5"); break; // T only
   case CS_WEAPON_MAC10: BuyWeapon (pBot, "3", "4"); break; // T only
   case CS_WEAPON_AUG: BuyWeapon (pBot, "4", "4"); break; // CT Only
   case CS_WEAPON_FIVESEVEN: BuyWeapon(pBot, "1", "6"); break; // CT only
   case CS_WEAPON_M4A1: BuyWeapon(pBot, "4", "3"); break; // CT Only
   case CS_WEAPON_TMP: BuyWeapon(pBot, "3", "2"); break; // CT only
   case CS_WEAPON_HEGRENADE: BuyWeapon(pBot, "8", "4"); break;
   case CS_WEAPON_XM1014: BuyWeapon(pBot, "2", "2"); break;
   case CS_WEAPON_SMOKEGRENADE: BuyWeapon(pBot, "8", "5"); break;
   case CS_WEAPON_USP: BuyWeapon(pBot, "1", "1"); break;
   case CS_WEAPON_GLOCK18: BuyWeapon(pBot, "1", "2"); break;
   case CS_WEAPON_M249: BuyWeapon(pBot, "5", "1"); break;
   case CS_WEAPON_M3: BuyWeapon(pBot, "2", "1"); break;
   case CS_WEAPON_G3SG1: BuyWeapon (pBot, "4", "7"); break;
   case CS_WEAPON_FLASHBANG: BuyWeapon (pBot, "8", "3"); break;
   case CS_WEAPON_P90: BuyWeapon (pBot, "3", "3"); break;
	// Armor
   case CS_WEAPON_ARMOR_LIGHT: BuyWeapon (pBot, "8", "1"); break;
   case CS_WEAPON_ARMOR_HEAVY: BuyWeapon (pBot, "8", "2"); break;
	// Defuse kit
   case CS_DEFUSEKIT: BuyWeapon (pBot, "8", "6"); break;
   }
  }

  // CS 1.6 only
  if (counterstrike == 1)
  {  // FRASHMAN 30/08/04: redone switch block, it was full of errors
   switch (buy_weapon)
   {
	//Pistols
   case CS_WEAPON_GLOCK18: BuyWeapon (pBot, "1", "1"); break;
   case CS_WEAPON_USP:	 BuyWeapon (pBot, "1", "2"); break;
   case CS_WEAPON_P228:	BuyWeapon (pBot, "1", "3"); break;
   case CS_WEAPON_DEAGLE:  BuyWeapon (pBot, "1", "4"); break;
   case CS_WEAPON_ELITE:   BuyWeapon (pBot, "1", "5"); break;
	//ShotGUNS
   case CS_WEAPON_M3:   BuyWeapon (pBot, "2", "1"); break;
   case CS_WEAPON_XM1014:  BuyWeapon (pBot, "2", "2"); break;
	//SMG
   case CS_WEAPON_MAC10:   BuyWeapon (pBot, "3", "1"); break;
   case CS_WEAPON_TMP:   BuyWeapon (pBot, "3", "1"); break;
   case CS_WEAPON_MP5NAVY: BuyWeapon (pBot, "3", "2"); break;
   case CS_WEAPON_UMP45:   BuyWeapon (pBot, "3", "3"); break;
   case CS_WEAPON_P90:   BuyWeapon (pBot, "3", "4"); break;
	//rifles
   case CS_WEAPON_GALIL:   BuyWeapon (pBot, "4", "1"); break;
   case CS_WEAPON_FAMAS:   BuyWeapon (pBot, "4", "1"); break;
   case CS_WEAPON_AK47:	BuyWeapon (pBot, "4", "2"); break;
   case CS_WEAPON_M4A1:	BuyWeapon (pBot, "4", "3"); break;
   case CS_WEAPON_SG552:   BuyWeapon (pBot, "4", "4"); break;
   case CS_WEAPON_AUG:   BuyWeapon (pBot, "4", "4"); break;
   case CS_WEAPON_SG550:   BuyWeapon (pBot, "4", "5"); break;
   case CS_WEAPON_G3SG1:   BuyWeapon (pBot, "4", "6"); break;
	//machinegun
   case CS_WEAPON_M249:   BuyWeapon (pBot, "5", "1"); break;
	// equipment
   case CS_WEAPON_ARMOR_LIGHT:   BuyWeapon (pBot, "8", "1"); break;
   case CS_WEAPON_ARMOR_HEAVY:   BuyWeapon (pBot, "8", "2"); break;
   case CS_WEAPON_FLASHBANG:	BuyWeapon (pBot, "8", "3"); break;
   case CS_WEAPON_HEGRENADE:	BuyWeapon (pBot, "8", "4"); break;
   case CS_WEAPON_SMOKEGRENADE:   BuyWeapon (pBot, "8", "5"); break;
   case CS_WEAPON_SHIELD:	BuyWeapon (pBot, "8", "8"); break;
   
   case CS_DEFUSEKIT:   BuyWeapon (pBot, "8", "6"); break;
   }
   // This differs per team
   // FRASHMAN 30/08/04: all into one ifthen block
   if (pBot->iTeam == 2) // counter
   {
	switch(buy_weapon)
	{
	case CS_WEAPON_SCOUT: BuyWeapon(pBot,"4","2"); break;
	case CS_WEAPON_AWP: BuyWeapon(pBot,"4","6"); break;
	 //whats about nightvision? BuyWeapon (pBot, "8", "7") 
	}	 
   }
   else  // terror
   {
	switch(buy_weapon)
	{
	case CS_WEAPON_SCOUT: BuyWeapon(pBot,"4","3"); break;
	case CS_WEAPON_AWP: BuyWeapon(pBot,"4","5"); break;
	 //whats about nightvision? BuyWeapon (pBot, "8", "6") 
	}	 
   }
  }  // end of cs 1.6 part
 }	// We actually gonna buy this weapon
}
its personality related (favorite weapon, etc)
its cs 1.5/1.6 compatible
its not _as and _es compatible (maps)
it has some sort of scoring system
its output relies on the ini parsed information from buytable.ini

piece of buytable.ini

Quote:
#======
#CS 1.6
#======
; Counter-Terrorist weapon; not as good as Galil
[FAMAS]
Price=2250
Priority=15
; Terrorist Weapon; pretty accurate
[GALIL]
Price=2000
Priority=30


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote