Calculator.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * @param c
  47. */
  48. public Calculator(Calculator c)
  49. {
  50. _functions = c._functions;
  51. }
  52. /**
  53. * Check if 2 calculators are equals.<BR><BR>
  54. * @param c1
  55. * @param c2
  56. * @return
  57. */
  58. public static boolean equalsCals(Calculator c1, Calculator c2)
  59. {
  60. if (c1 == c2)
  61. return true;
  62. if (c1 == null || c2 == null)
  63. return false;
  64. Func[] funcs1 = c1._functions;
  65. Func[] funcs2 = c2._functions;
  66. if (funcs1 == funcs2)
  67. return true;
  68. if (funcs1.length != funcs2.length)
  69. return false;
  70. if (funcs1.length == 0)
  71. return true;
  72. for (int i=0; i < funcs1.length; i++)
  73. {
  74. if (funcs1[i] != funcs2[i])
  75. return false;
  76. }
  77. return true;
  78. }
  79. /**
  80. * Return the number of Funcs in the Calculator.<BR><BR>
  81. * @return
  82. */
  83. public int size()
  84. {
  85. return _functions.length;
  86. }
  87. /**
  88. * Add a Func to the Calculator.<BR><BR>
  89. * @param f
  90. */
  91. public synchronized void addFunc(Func f)
  92. {
  93. Func[] funcs = _functions;
  94. Func[] tmp = new Func[funcs.length+1];
  95. final int order = f.order;
  96. int i;
  97. for (i=0; i < funcs.length && order >= funcs[i].order; i++)
  98. tmp[i] = funcs[i];
  99. tmp[i] = f;
  100. for (; i < funcs.length; i++)
  101. tmp[i+1] = funcs[i];
  102. _functions = tmp;
  103. }
  104. /**
  105. * Remove a Func from the Calculator.<BR><BR>
  106. * @param f
  107. */
  108. public synchronized void removeFunc(Func f)
  109. {
  110. Func[] funcs = _functions;
  111. Func[] tmp = new Func[funcs.length-1];
  112. int i;
  113. for (i=0; i < funcs.length && f != funcs[i]; i++)
  114. tmp[i] = funcs[i];
  115. if (i == funcs.length)
  116. return;
  117. for (i++; i < funcs.length; i++)
  118. tmp[i-1] = funcs[i];
  119. if (tmp.length == 0)
  120. _functions = _emptyFuncs;
  121. else
  122. _functions = tmp;
  123. }
  124. /**
  125. * Remove each Func with the specified owner of the Calculator.<BR><BR>
  126. * @param owner
  127. * @return
  128. */
  129. public synchronized FastList<Stats> removeOwner(Object owner)
  130. {
  131. FastList<Stats> modifiedStats = new FastList<Stats>();
  132. for (Func func: _functions)
  133. {
  134. if (func.funcOwner == owner)
  135. {
  136. modifiedStats.add(func.stat);
  137. removeFunc(func);
  138. }
  139. }
  140. return modifiedStats;
  141. }
  142. /**
  143. * Run each Func of the Calculator.<BR><BR>
  144. * @param env
  145. */
  146. public void calc(Env env)
  147. {
  148. for (Func func: _functions)
  149. func.calc(env);
  150. }
  151. }