2
0

DoorTable.java 7.7 KB

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