Advertisement
dalvorsn

Untitled

Apr 3rd, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. bool WeaponDistance::useWeapon(Player* player, Item* item, Creature* target) const
  2. {
  3.     int32_t modifier = playerWeaponCheck(player, target);
  4.     if(!modifier)
  5.         return false;
  6.  
  7.     int32_t chance = hitChance;
  8.     if(chance == -1)
  9.     {
  10.         //hit chance is based on distance to target and distance skill
  11.         const Position& playerPos = player->getPosition();
  12.         const Position& targetPos = target->getPosition();
  13.  
  14.         uint32_t distance = std::max(std::abs(playerPos.x - targetPos.x), std::abs(
  15.             playerPos.y - targetPos.y)), skill = player->getSkill(SKILL_DIST, SKILL_LEVEL);
  16.         if(maxHitChance == 75)
  17.         {
  18.             //chance for one-handed weapons
  19.             switch(distance)
  20.             {
  21.                 case 1:
  22.                     chance = (uint32_t)((float)std::min(skill, (uint32_t)74)) + 1;
  23.                     break;
  24.                 case 2:
  25.                     chance = (uint32_t)((float)2.4 * std::min(skill, (uint32_t)28)) + 8;
  26.                     break;
  27.                 case 3:
  28.                     chance = (uint32_t)((float)1.55 * std::min(skill, (uint32_t)45)) + 6;
  29.                     break;
  30.                 case 4:
  31.                     chance = (uint32_t)((float)1.25 * std::min(skill, (uint32_t)58)) + 3;
  32.                     break;
  33.                 case 5:
  34.                     chance = (uint32_t)((float)std::min(skill, (uint32_t)74)) + 1;
  35.                     break;
  36.                 case 6:
  37.                     chance = (uint32_t)((float)0.8 * std::min(skill, (uint32_t)90)) + 3;
  38.                     break;
  39.                 case 7:
  40.                     chance = (uint32_t)((float)0.7 * std::min(skill, (uint32_t)104)) + 2;
  41.                     break;
  42.                 default:
  43.                     chance = hitChance;
  44.                     break;
  45.             }
  46.         }
  47.         else if(maxHitChance == 90)
  48.         {
  49.             //formula for two-handed weapons
  50.             switch(distance)
  51.             {
  52.                 case 1:
  53.                     chance = (uint32_t)((float)1.2 * std::min(skill, (uint32_t)74)) + 1;
  54.                     break;
  55.                 case 2:
  56.                     chance = (uint32_t)((float)3.2 * std::min(skill, (uint32_t)28));
  57.                     break;
  58.                 case 3:
  59.                     chance = (uint32_t)((float)2.0 * std::min(skill, (uint32_t)45));
  60.                     break;
  61.                 case 4:
  62.                     chance = (uint32_t)((float)1.55 * std::min(skill, (uint32_t)58));
  63.                     break;
  64.                 case 5:
  65.                     chance = (uint32_t)((float)1.2 * std::min(skill, (uint32_t)74)) + 1;
  66.                     break;
  67.                 case 6:
  68.                     chance = (uint32_t)((float)1.0 * std::min(skill, (uint32_t)90));
  69.                     break;
  70.                 case 7:
  71.                     chance = (uint32_t)((float)1.0 * std::min(skill, (uint32_t)90));
  72.                     break;
  73.                 default:
  74.                     chance = hitChance;
  75.                     break;
  76.             }
  77.         }
  78.         else if(maxHitChance == 100)
  79.         {
  80.             switch(distance)
  81.             {
  82.                 case 1:
  83.                     chance = (uint32_t)((float)1.35 * std::min(skill, (uint32_t)73)) + 1;
  84.                     break;
  85.                 case 2:
  86.                     chance = (uint32_t)((float)3.2 * std::min(skill, (uint32_t)30)) + 4;
  87.                     break;
  88.                 case 3:
  89.                     chance = (uint32_t)((float)2.05 * std::min(skill, (uint32_t)48)) + 2;
  90.                     break;
  91.                 case 4:
  92.                     chance = (uint32_t)((float)1.5 * std::min(skill, (uint32_t)65)) + 2;
  93.                     break;
  94.                 case 5:
  95.                     chance = (uint32_t)((float)1.35 * std::min(skill, (uint32_t)73)) + 1;
  96.                     break;
  97.                 case 6:
  98.                     chance = (uint32_t)((float)1.2 * std::min(skill, (uint32_t)87)) - 4;
  99.                     break;
  100.                 case 7:
  101.                     chance = (uint32_t)((float)1.1 * std::min(skill, (uint32_t)90)) + 1;
  102.                     break;
  103.                 default:
  104.                     chance = hitChance;
  105.                     break;
  106.             }
  107.         }
  108.         else
  109.             chance = maxHitChance;
  110.     }
  111.  
  112.     if(item->getWeaponType() == WEAPON_AMMO)
  113.     {
  114.         Item* bow = player->getWeapon(true);
  115.         if(bow && bow->getHitChance() != -1)
  116.             chance += bow->getHitChance();
  117.     }
  118.  
  119.     if(random_range(1, 100) > chance)
  120.     {
  121.         //we failed attack, miss!
  122.         Tile* destTile = target->getTile();
  123.         if(!Position::areInRange<1,1,0>(player->getPosition(), target->getPosition()))
  124.         {
  125.             std::vector<std::pair<int32_t, int32_t> > destList;
  126.             destList.push_back(std::make_pair(-1, -1));
  127.             destList.push_back(std::make_pair(-1, 0));
  128.             destList.push_back(std::make_pair(-1, 1));
  129.             destList.push_back(std::make_pair(0, -1));
  130.             destList.push_back(std::make_pair(0, 0));
  131.             destList.push_back(std::make_pair(0, 1));
  132.             destList.push_back(std::make_pair(1, -1));
  133.             destList.push_back(std::make_pair(1, 0));
  134.             destList.push_back(std::make_pair(1, 1));
  135.  
  136.             std::random_shuffle(destList.begin(), destList.end());
  137.             Position destPos = target->getPosition();
  138.  
  139.             Tile* tmpTile = NULL;
  140.             for(std::vector<std::pair<int32_t, int32_t> >::iterator it = destList.begin(); it != destList.end(); ++it)
  141.             {
  142.                 if((tmpTile = g_game.getTile(destPos.x + it->first, destPos.y + it->second, destPos.z))
  143.                     && !tmpTile->hasProperty(IMMOVABLEBLOCKSOLID) && tmpTile->ground)
  144.                 {
  145.                     destTile = tmpTile;
  146.                     break;
  147.                 }
  148.             }
  149.         }
  150.  
  151.         Weapon::internalUseWeapon(player, item, destTile);
  152.     }
  153.     else
  154.         Weapon::internalUseWeapon(player, item, target, modifier);
  155.  
  156.     return true;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement