Advertisement
Chiddix

Accuracy formulae

Jul 30th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1.     /**
  2.      * Calculates the chance to hit out of 1.0
  3.      * @param opponent The entity being attacked.
  4.      * @return The chance to hit.
  5.      */
  6.     public double chanceToHit(Entity opponent) {       
  7.         /*
  8.          * Calculate the entity's base attack value.
  9.          */
  10.         int attackBonus = entity.getCombat().getBonus(entity.getCombat().getAttackType().getAttackBonus());
  11.        
  12.         if (attackBonus < 1) {
  13.             attackBonus = 1;
  14.         }
  15.        
  16.         int attackLevel = entity.getSkills().getLevel(Skills.ATTACK);
  17.        
  18.         switch (entity.getCombat().getAttackStyle()) {
  19.         case ACCURATE:
  20.             attackLevel += 3;
  21.             break;
  22.         case CONTROLLED:
  23.             attackLevel += 1;
  24.             break;
  25.         default:
  26.             break;
  27.         }
  28.        
  29.         double attackCalculation = attackBonus * attackLevel;
  30.        
  31.         /*
  32.          * Calculate the opponent's base defence value.
  33.          */    
  34.         int defenceBonus = opponent.getCombat().getBonus(entity.getCombat().getAttackType().getDefenceBonus());
  35.        
  36.         if (defenceBonus < 1) {
  37.             defenceBonus = 1;
  38.         }
  39.        
  40.         int defenceLevel = opponent.getSkills().getLevel(Skills.DEFENCE);
  41.        
  42.         switch (opponent.getCombat().getAttackStyle()) {
  43.         case DEFENSIVE:
  44.             defenceLevel += 3;
  45.             break;
  46.         case CONTROLLED:
  47.             defenceLevel += 1;
  48.             break;
  49.         default:
  50.             break;
  51.         }
  52.        
  53.         double defenceCalculation = defenceBonus * defenceLevel;
  54.        
  55.         final double MAGIC_NUMBER = 133;
  56.        
  57.         double chanceToHit = MAGIC_NUMBER * (attackCalculation / defenceCalculation);
  58.        
  59.         if (chanceToHit > 1.0) {
  60.             chanceToHit = 1.0;
  61.         }
  62.        
  63.         // TODO: prayer modifiers, verac effect
  64.        
  65.         return chanceToHit;
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement