2
0

Calculator.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.skills;
  16. import javolution.util.FastList;
  17. import com.l2jserver.gameserver.skills.funcs.Func;
  18. /**
  19. * A calculator is created to manage and dynamically calculate the effect of a character property (ex : MAX_HP, REGENERATE_HP_RATE...).
  20. * In fact, each calculator is a table of Func object in which each Func represents a mathematic function : <BR><BR>
  21. *
  22. * FuncAtkAccuracy -> Math.sqrt(_player.getDEX())*6+_player.getLevel()<BR><BR>
  23. *
  24. * When the calc method of a calculator is launched, each mathematic function is called according to its priority <B>_order</B>.
  25. * Indeed, Func with lowest priority order is executed first and Funcs with the same order are executed in unspecified order.
  26. * The result of the calculation is stored in the value property of an Env class instance.<BR><BR>
  27. *
  28. * Method addFunc and removeFunc permit to add and remove a Func object from a Calculator.<BR><BR>
  29. *
  30. */
  31. public final class Calculator
  32. {
  33. /** Empty Func table definition */
  34. private static final Func[] _emptyFuncs = new Func[0];
  35. /** Table of Func object */
  36. private Func[] _functions;
  37. /**
  38. * Constructor of Calculator (Init value : emptyFuncs).<BR><BR>
  39. */
  40. public Calculator()
  41. {
  42. _functions = _emptyFuncs;
  43. }
  44. /**
  45. * Constructor of Calculator (Init value : Calculator c).<BR><BR>
  46. */
  47. public Calculator(Calculator c)
  48. {
  49. _functions = c._functions;
  50. }
  51. /**
  52. * Check if 2 calculators are equals.<BR><BR>
  53. */
  54. public static boolean equalsCals(Calculator c1, Calculator c2)
  55. {
  56. if (c1 == c2)
  57. return true;
  58. if (c1 == null || c2 == null)
  59. return false;
  60. Func[] funcs1 = c1._functions;
  61. Func[] funcs2 = c2._functions;
  62. if (funcs1 == funcs2)
  63. return true;
  64. if (funcs1.length != funcs2.length)
  65. return false;
  66. if (funcs1.length == 0)
  67. return true;
  68. for (int i=0; i < funcs1.length; i++)
  69. {
  70. if (funcs1[i] != funcs2[i])
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * Return the number of Funcs in the Calculator.<BR><BR>
  77. */
  78. public int size()
  79. {
  80. return _functions.length;
  81. }
  82. /**
  83. * Add a Func to the Calculator.<BR><BR>
  84. */
  85. public synchronized void addFunc(Func f)
  86. {
  87. Func[] funcs = _functions;
  88. Func[] tmp = new Func[funcs.length+1];
  89. final int order = f.order;
  90. int i;
  91. for (i=0; i < funcs.length && order >= funcs[i].order; i++)
  92. tmp[i] = funcs[i];
  93. tmp[i] = f;
  94. for (; i < funcs.length; i++)
  95. tmp[i+1] = funcs[i];
  96. _functions = tmp;
  97. }
  98. /**
  99. * Remove a Func from the Calculator.<BR><BR>
  100. */
  101. public synchronized void removeFunc(Func f)
  102. {
  103. Func[] funcs = _functions;
  104. Func[] tmp = new Func[funcs.length-1];
  105. int i;
  106. for (i=0; i < funcs.length && f != funcs[i]; i++)
  107. tmp[i] = funcs[i];
  108. if (i == funcs.length)
  109. return;
  110. for (i++; i < funcs.length; i++)
  111. tmp[i-1] = funcs[i];
  112. if (tmp.length == 0)
  113. _functions = _emptyFuncs;
  114. else
  115. _functions = tmp;
  116. }
  117. /**
  118. * Remove each Func with the specified owner of the Calculator.<BR><BR>
  119. */
  120. public synchronized FastList<Stats> removeOwner(Object owner)
  121. {
  122. FastList<Stats> modifiedStats = new FastList<Stats>();
  123. for (Func func: _functions)
  124. {
  125. if (func.funcOwner == owner)
  126. {
  127. modifiedStats.add(func.stat);
  128. removeFunc(func);
  129. }
  130. }
  131. return modifiedStats;
  132. }
  133. /**
  134. * Run each Func of the Calculator.<BR><BR>
  135. */
  136. public void calc(Env env)
  137. {
  138. for (Func func: _functions)
  139. func.calc(env);
  140. }
  141. }