Calculator.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.stats;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.skills.Skill;
  24. import com.l2jserver.gameserver.model.stats.functions.AbstractFunction;
  25. /**
  26. * A calculator is created to manage and dynamically calculate the effect of a character property (ex : MAX_HP, REGENERATE_HP_RATE...).<br>
  27. * In fact, each calculator is a table of Func object in which each Func represents a mathematical function:<br>
  28. * FuncAtkAccuracy -> Math.sqrt(_player.getDEX())*6+_player.getLevel()<br>
  29. * When the calc method of a calculator is launched, each mathematical function is called according to its priority <B>_order</B>.<br>
  30. * Indeed, Func with lowest priority order is executed first and Funcs with the same order are executed in unspecified order.<br>
  31. * The result of the calculation is stored in the value property of an Env class instance.<br>
  32. * Method addFunc and removeFunc permit to add and remove a Func object from a Calculator.
  33. */
  34. public final class Calculator
  35. {
  36. /** Empty Func table definition */
  37. private static final AbstractFunction[] EMPTY_FUNCS = new AbstractFunction[0];
  38. /** Table of Func object */
  39. private AbstractFunction[] _functions;
  40. /**
  41. * Constructor of Calculator (Init value : emptyFuncs).
  42. */
  43. public Calculator()
  44. {
  45. _functions = EMPTY_FUNCS;
  46. }
  47. /**
  48. * Constructor of Calculator (Init value : Calculator c).
  49. * @param c
  50. */
  51. public Calculator(Calculator c)
  52. {
  53. _functions = c._functions;
  54. }
  55. /**
  56. * Check if 2 calculators are equals.
  57. * @param c1
  58. * @param c2
  59. * @return
  60. */
  61. public static boolean equalsCals(Calculator c1, Calculator c2)
  62. {
  63. if (c1 == c2)
  64. {
  65. return true;
  66. }
  67. if ((c1 == null) || (c2 == null))
  68. {
  69. return false;
  70. }
  71. AbstractFunction[] funcs1 = c1._functions;
  72. AbstractFunction[] funcs2 = c2._functions;
  73. if (funcs1 == funcs2)
  74. {
  75. return true;
  76. }
  77. if (funcs1.length != funcs2.length)
  78. {
  79. return false;
  80. }
  81. if (funcs1.length == 0)
  82. {
  83. return true;
  84. }
  85. for (int i = 0; i < funcs1.length; i++)
  86. {
  87. if (funcs1[i] != funcs2[i])
  88. {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * Return the number of Funcs in the Calculator.
  96. * @return
  97. */
  98. public int size()
  99. {
  100. return _functions.length;
  101. }
  102. /**
  103. * Adds a function to the Calculator.
  104. * @param function the function
  105. */
  106. public synchronized void addFunc(AbstractFunction function)
  107. {
  108. AbstractFunction[] funcs = _functions;
  109. AbstractFunction[] tmp = new AbstractFunction[funcs.length + 1];
  110. final int order = function.getOrder();
  111. int i;
  112. for (i = 0; (i < funcs.length) && (order >= funcs[i].getOrder()); i++)
  113. {
  114. tmp[i] = funcs[i];
  115. }
  116. tmp[i] = function;
  117. for (; i < funcs.length; i++)
  118. {
  119. tmp[i + 1] = funcs[i];
  120. }
  121. _functions = tmp;
  122. }
  123. /**
  124. * Removes a function from the Calculator.
  125. * @param function the function
  126. */
  127. public synchronized void removeFunc(AbstractFunction function)
  128. {
  129. AbstractFunction[] funcs = _functions;
  130. AbstractFunction[] tmp = new AbstractFunction[funcs.length - 1];
  131. int i;
  132. for (i = 0; (i < (funcs.length - 1)) && (function != funcs[i]); i++)
  133. {
  134. tmp[i] = funcs[i];
  135. }
  136. if (i == funcs.length)
  137. {
  138. return;
  139. }
  140. for (i++; i < funcs.length; i++)
  141. {
  142. tmp[i - 1] = funcs[i];
  143. }
  144. if (tmp.length == 0)
  145. {
  146. _functions = EMPTY_FUNCS;
  147. }
  148. else
  149. {
  150. _functions = tmp;
  151. }
  152. }
  153. /**
  154. * Remove each Func with the specified owner of the Calculator.
  155. * @param owner the owner
  156. * @return a list of modified stats
  157. */
  158. public synchronized List<Stats> removeOwner(Object owner)
  159. {
  160. List<Stats> modifiedStats = new ArrayList<>();
  161. for (AbstractFunction func : _functions)
  162. {
  163. if (func.getFuncOwner() == owner)
  164. {
  165. modifiedStats.add(func.getStat());
  166. removeFunc(func);
  167. }
  168. }
  169. return modifiedStats;
  170. }
  171. /**
  172. * Run each function of the Calculator.
  173. * @param caster the caster
  174. * @param target the target
  175. * @param skill the skill
  176. * @param initVal the initial value
  177. * @return the calculated value
  178. */
  179. public double calc(L2Character caster, L2Character target, Skill skill, double initVal)
  180. {
  181. double value = initVal;
  182. for (AbstractFunction func : _functions)
  183. {
  184. value = func.calc(caster, target, skill, value);
  185. }
  186. return value;
  187. }
  188. /**
  189. * Get array of all function, dont use for add/remove
  190. * @return
  191. */
  192. public AbstractFunction[] getFunctions()
  193. {
  194. return _functions;
  195. }
  196. }