HitConditionBonus.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.datatables;
  16. import java.io.File;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.NamedNodeMap;
  19. import org.w3c.dom.Node;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.GameTimeController;
  22. import com.l2jserver.gameserver.engines.DocumentParser;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. /**
  25. * This class load, holds and calculates the hit condition bonuses.
  26. * @author Nik
  27. */
  28. public final class HitConditionBonus extends DocumentParser
  29. {
  30. private int frontBonus = 0;
  31. private int sideBonus = 0;
  32. private int backBonus = 0;
  33. private int highBonus = 0;
  34. private int lowBonus = 0;
  35. private int darkBonus = 0;
  36. private int rainBonus = 0;
  37. private HitConditionBonus()
  38. {
  39. parseFile(new File(Config.DATAPACK_ROOT, "data/stats/hitConditionBonus.xml"));
  40. _log.info(getClass().getSimpleName() + ": Loaded Hit Condition bonuses.");
  41. if (Config.DEBUG)
  42. {
  43. _log.info(getClass().getSimpleName() + ": Front bonus: " + frontBonus);
  44. _log.info(getClass().getSimpleName() + ": Side bonus: " + sideBonus);
  45. _log.info(getClass().getSimpleName() + ": Back bonus: " + backBonus);
  46. _log.info(getClass().getSimpleName() + ": High bonus: " + highBonus);
  47. _log.info(getClass().getSimpleName() + ": Low bonus: " + lowBonus);
  48. _log.info(getClass().getSimpleName() + ": Dark bonus: " + darkBonus);
  49. _log.info(getClass().getSimpleName() + ": Rain bonus: " + rainBonus);
  50. }
  51. }
  52. @Override
  53. protected void parseDocument(Document doc)
  54. {
  55. final Node n = doc.getFirstChild();
  56. NamedNodeMap attrs;
  57. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  58. {
  59. attrs = d.getAttributes();
  60. switch (d.getNodeName())
  61. {
  62. case "front":
  63. frontBonus = parseInt(attrs, "val");
  64. break;
  65. case "side":
  66. sideBonus = parseInt(attrs, "val");
  67. break;
  68. case "back":
  69. backBonus = parseInt(attrs, "val");
  70. break;
  71. case "high":
  72. highBonus = parseInt(attrs, "val");
  73. break;
  74. case "low":
  75. lowBonus = parseInt(attrs, "val");
  76. break;
  77. case "dark":
  78. darkBonus = parseInt(attrs, "val");
  79. break;
  80. case "rain":
  81. rainBonus = parseInt(attrs, "val");
  82. break;
  83. }
  84. }
  85. }
  86. /**
  87. * @param attacker the attacking character.
  88. * @param target the attacked character.
  89. * @return the bonus of the attacker against the target.
  90. */
  91. public double getConditionBonus(L2Character attacker, L2Character target)
  92. {
  93. double mod = 100;
  94. // Get high or low bonus
  95. if ((attacker.getZ() - target.getZ()) > 50)
  96. {
  97. mod += highBonus;
  98. }
  99. else if ((attacker.getZ() - target.getZ()) < -50)
  100. {
  101. mod += lowBonus;
  102. }
  103. // Get weather bonus
  104. if (GameTimeController.getInstance().isNowNight())
  105. {
  106. mod += darkBonus;
  107. // else if () No rain support yet.
  108. // chance += hitConditionBonus.rainBonus;
  109. }
  110. // Get side bonus
  111. if (attacker.isBehindTarget())
  112. {
  113. mod += backBonus;
  114. }
  115. else if (attacker.isInFrontOfTarget())
  116. {
  117. mod += frontBonus;
  118. }
  119. else
  120. {
  121. mod += sideBonus;
  122. }
  123. // If (mod / 100) is less than 0, return 0, because we can't lower more than 100%.
  124. return Math.max(mod / 100, 0);
  125. }
  126. public static HitConditionBonus getInstance()
  127. {
  128. return SingletonHolder._instance;
  129. }
  130. @SuppressWarnings("synthetic-access")
  131. private static class SingletonHolder
  132. {
  133. protected static final HitConditionBonus _instance = new HitConditionBonus();
  134. }
  135. }