DoorTable.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.HashMap;
  19. import java.util.HashSet;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.logging.Logger;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.gameserver.engines.DocumentParser;
  26. import com.l2jserver.gameserver.idfactory.IdFactory;
  27. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  28. import com.l2jserver.gameserver.instancemanager.MapRegionManager;
  29. import com.l2jserver.gameserver.model.StatsSet;
  30. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  31. import com.l2jserver.gameserver.model.actor.templates.L2DoorTemplate;
  32. import com.l2jserver.gameserver.pathfinding.AbstractNodeLoc;
  33. public class DoorTable extends DocumentParser
  34. {
  35. private static final Logger _log = Logger.getLogger(DoorTable.class.getName());
  36. private final Map<Integer, L2DoorInstance> _doors = new HashMap<>();
  37. private static final Map<String, Set<Integer>> _groups = new HashMap<>();
  38. private final Map<Integer, Collection<L2DoorInstance>> _regions = new HashMap<>();
  39. protected DoorTable()
  40. {
  41. load();
  42. }
  43. @Override
  44. public void load()
  45. {
  46. parseDatapackFile("data/doorData.xml");
  47. }
  48. public void reloadAll()
  49. {
  50. _doors.clear();
  51. _groups.clear();
  52. _regions.clear();
  53. load();
  54. }
  55. @Override
  56. protected void parseDocument()
  57. {
  58. NamedNodeMap attrs;
  59. Node att;
  60. StatsSet set;
  61. for (Node a = getCurrentDocument().getFirstChild(); a != null; a = a.getNextSibling())
  62. {
  63. if ("list".equalsIgnoreCase(a.getNodeName()))
  64. {
  65. for (Node b = a.getFirstChild(); b != null; b = b.getNextSibling())
  66. {
  67. if ("door".equalsIgnoreCase(b.getNodeName()))
  68. {
  69. attrs = b.getAttributes();
  70. set = new StatsSet();
  71. set.set("baseHpMax", 1); // Avoid doors without HP value created dead due to default value 0 in L2CharTemplate
  72. for (int i = 0; i < attrs.getLength(); i++)
  73. {
  74. att = attrs.item(i);
  75. set.set(att.getNodeName(), att.getNodeValue());
  76. }
  77. makeDoor(set);
  78. }
  79. }
  80. }
  81. }
  82. _log.info("DoorTable: Loaded " + _doors.size() + " Door Templates for " + _regions.size() + " regions.");
  83. }
  84. public void insertCollisionData(StatsSet set)
  85. {
  86. int posX, posY, nodeX, nodeY, height;
  87. height = set.getInteger("height");
  88. String[] pos = set.getString("node1").split(",");
  89. nodeX = Integer.parseInt(pos[0]);
  90. nodeY = Integer.parseInt(pos[1]);
  91. pos = set.getString("node2").split(",");
  92. posX = Integer.parseInt(pos[0]);
  93. posY = Integer.parseInt(pos[1]);
  94. int collisionRadius; // (max) radius for movement checks
  95. collisionRadius = Math.min(Math.abs(nodeX - posX), Math.abs(nodeY - posY));
  96. if (collisionRadius < 20)
  97. {
  98. collisionRadius = 20;
  99. }
  100. set.set("collision_radius", collisionRadius);
  101. set.set("collision_height", height);
  102. }
  103. /**
  104. * @param set
  105. */
  106. private void makeDoor(StatsSet set)
  107. {
  108. insertCollisionData(set);
  109. L2DoorTemplate template = new L2DoorTemplate(set);
  110. L2DoorInstance door = new L2DoorInstance(IdFactory.getInstance().getNextId(), template, set);
  111. door.setCurrentHp(door.getMaxHp());
  112. door.spawnMe(template.posX, template.posY, template.posZ);
  113. putDoor(door, MapRegionManager.getInstance().getMapRegionLocId(door.getX(), door.getY()));
  114. }
  115. public L2DoorTemplate getDoorTemplate(int doorId)
  116. {
  117. return _doors.get(doorId).getTemplate();
  118. }
  119. public L2DoorInstance getDoor(int doorId)
  120. {
  121. return _doors.get(doorId);
  122. }
  123. public void putDoor(L2DoorInstance door, int region)
  124. {
  125. _doors.put(door.getDoorId(), door);
  126. if (_regions.containsKey(region))
  127. {
  128. _regions.get(region).add(door);
  129. }
  130. else
  131. {
  132. final ArrayList<L2DoorInstance> list = new ArrayList<>();
  133. list.add(door);
  134. _regions.put(region, list);
  135. }
  136. }
  137. public static void addDoorGroup(String groupName, int doorId)
  138. {
  139. Set<Integer> set = _groups.get(groupName);
  140. if (set == null)
  141. {
  142. set = new HashSet<>();
  143. set.add(doorId);
  144. _groups.put(groupName, set);
  145. }
  146. else
  147. {
  148. set.add(doorId);
  149. }
  150. }
  151. public static Set<Integer> getDoorsByGroup(String groupName)
  152. {
  153. return _groups.get(groupName);
  154. }
  155. public Collection<L2DoorInstance> getDoors()
  156. {
  157. return _doors.values();
  158. }
  159. public boolean checkIfDoorsBetween(AbstractNodeLoc start, AbstractNodeLoc end, int instanceId)
  160. {
  161. return checkIfDoorsBetween(start.getX(), start.getY(), start.getZ(), end.getX(), end.getY(), end.getZ(), instanceId);
  162. }
  163. public boolean checkIfDoorsBetween(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  164. {
  165. return checkIfDoorsBetween(x, y, z, tx, ty, tz, instanceId, false);
  166. }
  167. /**
  168. * GodKratos: TODO: remove GeoData checks from door table and convert door nodes to Geo zones
  169. * @param x
  170. * @param y
  171. * @param z
  172. * @param tx
  173. * @param ty
  174. * @param tz
  175. * @param instanceId
  176. * @param doubleFaceCheck
  177. * @return {@code boolean}
  178. */
  179. public boolean checkIfDoorsBetween(int x, int y, int z, int tx, int ty, int tz, int instanceId, boolean doubleFaceCheck)
  180. {
  181. Collection<L2DoorInstance> allDoors;
  182. if ((instanceId > 0) && (InstanceManager.getInstance().getInstance(instanceId) != null))
  183. {
  184. allDoors = InstanceManager.getInstance().getInstance(instanceId).getDoors();
  185. }
  186. else
  187. {
  188. allDoors = _regions.get(MapRegionManager.getInstance().getMapRegionLocId(x, y));
  189. }
  190. if (allDoors == null)
  191. {
  192. return false;
  193. }
  194. for (L2DoorInstance doorInst : allDoors)
  195. {
  196. // check dead and open
  197. if (doorInst.isDead() || doorInst.getOpen() || !doorInst.checkCollision() || (doorInst.getX(0) == 0))
  198. {
  199. continue;
  200. }
  201. boolean intersectFace = false;
  202. for (int i = 0; i < 4; i++)
  203. {
  204. int j = (i + 1) < 4 ? i + 1 : 0;
  205. // lower part of the multiplier fraction, if it is 0 we avoid an error and also know that the lines are parallel
  206. int denominator = ((ty - y) * (doorInst.getX(i) - doorInst.getX(j))) - ((tx - x) * (doorInst.getY(i) - doorInst.getY(j)));
  207. if (denominator == 0)
  208. {
  209. continue;
  210. }
  211. // multipliers to the equations of the lines. If they are lower than 0 or bigger than 1, we know that segments don't intersect
  212. float multiplier1 = (float) (((doorInst.getX(j) - doorInst.getX(i)) * (y - doorInst.getY(i))) - ((doorInst.getY(j) - doorInst.getY(i)) * (x - doorInst.getX(i)))) / denominator;
  213. float multiplier2 = (float) (((tx - x) * (y - doorInst.getY(i))) - ((ty - y) * (x - doorInst.getX(i)))) / denominator;
  214. if ((multiplier1 >= 0) && (multiplier1 <= 1) && (multiplier2 >= 0) && (multiplier2 <= 1))
  215. {
  216. int intersectZ = Math.round(z + (multiplier1 * (tz - z)));
  217. // now checking if the resulting point is between door's min and max z
  218. if ((intersectZ > doorInst.getZMin()) && (intersectZ < doorInst.getZMax()))
  219. {
  220. if (!doubleFaceCheck || intersectFace)
  221. {
  222. return true;
  223. }
  224. intersectFace = true;
  225. }
  226. }
  227. }
  228. }
  229. return false;
  230. }
  231. public static DoorTable getInstance()
  232. {
  233. return SingletonHolder._instance;
  234. }
  235. private static class SingletonHolder
  236. {
  237. protected static final DoorTable _instance = new DoorTable();
  238. }
  239. }