DoorTable.java 7.8 KB

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