DoorTable.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.TIntObjectHashMap;
  17. import java.io.BufferedReader;
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileReader;
  21. import java.io.IOException;
  22. import java.io.LineNumberReader;
  23. import java.util.ArrayList;
  24. import java.util.StringTokenizer;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.idfactory.IdFactory;
  29. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  30. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  31. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  32. import com.l2jserver.gameserver.model.entity.ClanHall;
  33. import com.l2jserver.gameserver.pathfinding.AbstractNodeLoc;
  34. import com.l2jserver.gameserver.templates.StatsSet;
  35. import com.l2jserver.gameserver.templates.chars.L2CharTemplate;
  36. public class DoorTable
  37. {
  38. private static final Logger _log = Logger.getLogger(DoorTable.class.getName());
  39. private final TIntObjectHashMap<L2DoorInstance> _staticItems;
  40. private final TIntObjectHashMap<ArrayList<L2DoorInstance>> _regions;
  41. public static DoorTable getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. private DoorTable()
  46. {
  47. _staticItems = new TIntObjectHashMap<L2DoorInstance>();
  48. _regions = new TIntObjectHashMap<ArrayList<L2DoorInstance>>();
  49. parseData();
  50. onStart();
  51. }
  52. public void reloadAll()
  53. {
  54. respawn();
  55. }
  56. public void respawn()
  57. {
  58. _staticItems.clear();
  59. _regions.clear();
  60. parseData();
  61. }
  62. public void parseData()
  63. {
  64. LineNumberReader lnr = null;
  65. try
  66. {
  67. File doorData = new File(Config.DATAPACK_ROOT, "data/door.csv");
  68. lnr = new LineNumberReader(new BufferedReader(new FileReader(doorData)));
  69. String line = null;
  70. _log.info("Searching clan halls doors:");
  71. while ((line = lnr.readLine()) != null)
  72. {
  73. if (line.trim().length() == 0 || line.startsWith("#"))
  74. continue;
  75. L2DoorInstance door = parseList(line, false);
  76. putDoor(door);
  77. door.spawnMe(door.getX(), door.getY(), door.getZ());
  78. ClanHall clanhall = ClanHallManager.getInstance().getNearbyClanHall(door.getX(), door.getY(), 500);
  79. if (clanhall != null)
  80. {
  81. clanhall.getDoors().add(door);
  82. door.setClanHall(clanhall);
  83. if (Config.DEBUG)
  84. _log.info("door " + door.getDoorName() + " attached to ch " + clanhall.getName());
  85. }
  86. }
  87. _log.info("DoorTable: Loaded " + _staticItems.size() + " Door Templates for " + _regions.size() + " regions.");
  88. }
  89. catch (FileNotFoundException e)
  90. {
  91. _log.warning("door.csv is missing in data folder");
  92. }
  93. catch (IOException e)
  94. {
  95. _log.log(Level.WARNING, "Error while creating door table " + e.getMessage(), e);
  96. }
  97. finally
  98. {
  99. try
  100. {
  101. lnr.close();
  102. }
  103. catch (Exception e1)
  104. { /* ignore problems */
  105. }
  106. }
  107. }
  108. /**
  109. * Parses door list.
  110. *
  111. * @param line string
  112. * @param commanderDoor whether the door is commander door (fortress)
  113. *
  114. * @return created door instance
  115. */
  116. public static L2DoorInstance parseList(final String line, final boolean commanderDoor)
  117. {
  118. StringTokenizer st = new StringTokenizer(line, ";");
  119. L2DoorInstance door = null;
  120. try
  121. {
  122. String name = st.nextToken();
  123. int id = Integer.parseInt(st.nextToken());
  124. int x = Integer.parseInt(st.nextToken());
  125. int y = Integer.parseInt(st.nextToken());
  126. int z = Integer.parseInt(st.nextToken());
  127. int rangeXMin = Integer.parseInt(st.nextToken());
  128. int rangeYMin = Integer.parseInt(st.nextToken());
  129. int rangeZMin = Integer.parseInt(st.nextToken());
  130. int rangeXMax = Integer.parseInt(st.nextToken());
  131. int rangeYMax = Integer.parseInt(st.nextToken());
  132. int rangeZMax = Integer.parseInt(st.nextToken());
  133. int hp = Integer.parseInt(st.nextToken());
  134. int pdef = Integer.parseInt(st.nextToken());
  135. int mdef = Integer.parseInt(st.nextToken());
  136. int emitter = Integer.parseInt(st.nextToken());
  137. boolean unlockable = false;
  138. if (st.hasMoreTokens())
  139. unlockable = Boolean.parseBoolean(st.nextToken());
  140. boolean startOpen = false;
  141. if (st.hasMoreTokens())
  142. startOpen = Boolean.parseBoolean(st.nextToken());
  143. boolean targetable = true;
  144. if (st.hasMoreTokens())
  145. targetable = Boolean.parseBoolean(st.nextToken());
  146. if (rangeXMin > rangeXMax)
  147. _log.severe("Error in door data, XMin > XMax, ID:" + id);
  148. if (rangeYMin > rangeYMax)
  149. _log.severe("Error in door data, YMin > YMax, ID:" + id);
  150. if (rangeZMin > rangeZMax)
  151. _log.severe("Error in door data, ZMin > ZMax, ID:" + id);
  152. int collisionRadius; // (max) radius for movement checks
  153. if ((rangeXMax - rangeXMin) > (rangeYMax - rangeYMin))
  154. collisionRadius = rangeYMax - rangeYMin;
  155. else
  156. collisionRadius = rangeXMax - rangeXMin;
  157. StatsSet npcDat = new StatsSet();
  158. npcDat.set("npcId", id);
  159. npcDat.set("level", 0);
  160. npcDat.set("jClass", "door");
  161. npcDat.set("baseSTR", 0);
  162. npcDat.set("baseCON", 0);
  163. npcDat.set("baseDEX", 0);
  164. npcDat.set("baseINT", 0);
  165. npcDat.set("baseWIT", 0);
  166. npcDat.set("baseMEN", 0);
  167. npcDat.set("baseShldDef", 0);
  168. npcDat.set("baseShldRate", 0);
  169. npcDat.set("baseAccCombat", 38);
  170. npcDat.set("baseEvasRate", 38);
  171. npcDat.set("baseCritRate", 38);
  172. //npcDat.set("name", "");
  173. npcDat.set("collision_radius", collisionRadius);
  174. npcDat.set("collision_height", rangeZMax - rangeZMin);
  175. npcDat.set("sex", "male");
  176. npcDat.set("type", "");
  177. npcDat.set("baseAtkRange", 0);
  178. npcDat.set("baseMpMax", 0);
  179. npcDat.set("baseCpMax", 0);
  180. npcDat.set("rewardExp", 0);
  181. npcDat.set("rewardSp", 0);
  182. npcDat.set("basePAtk", 0);
  183. npcDat.set("baseMAtk", 0);
  184. npcDat.set("basePAtkSpd", 0);
  185. npcDat.set("aggroRange", 0);
  186. npcDat.set("baseMAtkSpd", 0);
  187. npcDat.set("rhand", 0);
  188. npcDat.set("lhand", 0);
  189. npcDat.set("armor", 0);
  190. npcDat.set("baseWalkSpd", 0);
  191. npcDat.set("baseRunSpd", 0);
  192. npcDat.set("name", name);
  193. npcDat.set("baseHpMax", hp);
  194. npcDat.set("baseHpReg", 3.e-3f);
  195. npcDat.set("baseMpReg", 3.e-3f);
  196. npcDat.set("basePDef", pdef);
  197. npcDat.set("baseMDef", mdef);
  198. L2CharTemplate template = new L2CharTemplate(npcDat);
  199. door = new L2DoorInstance(IdFactory.getInstance().getNextId(), template, id, name, unlockable);
  200. door.setRange(rangeXMin, rangeYMin, rangeZMin, rangeXMax, rangeYMax, rangeZMax);
  201. door.setCurrentHpMp(door.getMaxHp(), door.getMaxMp());
  202. door.setXYZInvisible(x, y, z);
  203. door.setMapRegion(MapRegionTable.getInstance().getMapRegion(x, y));
  204. door.setEmitter(emitter);
  205. door.setTargetable(targetable);
  206. if (commanderDoor)
  207. door.setIsCommanderDoor(startOpen);
  208. else
  209. door.setOpen(startOpen);
  210. }
  211. catch (Exception e)
  212. {
  213. _log.severe("Error in door data at line: " + line);
  214. }
  215. return door;
  216. }
  217. public L2DoorInstance getDoor(Integer id)
  218. {
  219. return _staticItems.get(id);
  220. }
  221. public void putDoor(L2DoorInstance door)
  222. {
  223. _staticItems.put(door.getDoorId(), door);
  224. if (_regions.contains(door.getMapRegion()))
  225. _regions.get(door.getMapRegion()).add(door);
  226. else
  227. {
  228. final ArrayList<L2DoorInstance> region = new ArrayList<L2DoorInstance>();
  229. region.add(door);
  230. _regions.put(door.getMapRegion(), region);
  231. }
  232. }
  233. public L2DoorInstance[] getDoors()
  234. {
  235. L2DoorInstance[] _allTemplates = _staticItems.getValues(new L2DoorInstance[_staticItems.size()]);
  236. return _allTemplates;
  237. }
  238. /**
  239. * Performs a check and sets up a scheduled task for
  240. * those doors that require auto opening/closing.
  241. */
  242. public void checkAutoOpen()
  243. {
  244. for (L2DoorInstance doorInst : getDoors())
  245. // Garden of Eva (every 7 minutes)
  246. if (doorInst.getDoorName().startsWith("goe"))
  247. doorInst.setAutoActionDelay(420000);
  248. // Tower of Insolence (every 5 minutes)
  249. else if (doorInst.getDoorName().startsWith("aden_tower"))
  250. doorInst.setAutoActionDelay(300000);
  251. /* TODO: check which are automatic
  252. // devils (every 5 minutes)
  253. else if (doorInst.getDoorName().startsWith("pirate_isle"))
  254. doorInst.setAutoActionDelay(300000);
  255. // Cruma Tower (every 20 minutes)
  256. else if (doorInst.getDoorName().startsWith("cruma"))
  257. doorInst.setAutoActionDelay(1200000);
  258. */
  259. }
  260. public boolean checkIfDoorsBetween(AbstractNodeLoc start, AbstractNodeLoc end, int instanceId)
  261. {
  262. return checkIfDoorsBetween(start.getX(), start.getY(), start.getZ(), end.getX(), end.getY(), end.getZ(), instanceId);
  263. }
  264. public boolean checkIfDoorsBetween(int x, int y, int z, int tx, int ty, int tz, int instanceId)
  265. {
  266. ArrayList<L2DoorInstance> allDoors;
  267. if (instanceId > 0 && InstanceManager.getInstance().getInstance(instanceId) != null)
  268. allDoors = InstanceManager.getInstance().getInstance(instanceId).getDoors();
  269. else
  270. allDoors = _regions.get(MapRegionTable.getInstance().getMapRegion(x, y));
  271. if (allDoors == null)
  272. return false;
  273. for (L2DoorInstance doorInst : allDoors)
  274. {
  275. if (doorInst.getXMax() == 0)
  276. continue;
  277. // line segment goes through box
  278. // first basic checks to stop most calculations short
  279. // phase 1, x
  280. if ((x <= doorInst.getXMax() && tx >= doorInst.getXMin())
  281. || (tx <= doorInst.getXMax() && x >= doorInst.getXMin()))
  282. {
  283. //phase 2, y
  284. if ((y <= doorInst.getYMax() && ty >= doorInst.getYMin())
  285. || (ty <= doorInst.getYMax() && y >= doorInst.getYMin()))
  286. {
  287. // phase 3, basically only z remains but now we calculate it with another formula (by rage)
  288. // in some cases the direct line check (only) in the beginning isn't sufficient,
  289. // when char z changes a lot along the path
  290. if (doorInst.getCurrentHp() > 0 && !doorInst.getOpen())
  291. {
  292. int px1 = doorInst.getXMin();
  293. int py1 = doorInst.getYMin();
  294. int pz1 = doorInst.getZMin();
  295. int px2 = doorInst.getXMax();
  296. int py2 = doorInst.getYMax();
  297. int pz2 = doorInst.getZMax();
  298. int l = tx - x;
  299. int m = ty - y;
  300. int n = tz - z;
  301. int dk;
  302. if ((dk = (doorInst.getA() * l + doorInst.getB() * m + doorInst.getC() * n)) == 0)
  303. continue; // Parallel
  304. float p = (float) (doorInst.getA() * x + doorInst.getB() * y + doorInst.getC() * z + doorInst.getD()) / (float) dk;
  305. int fx = (int) (x - l * p);
  306. int fy = (int) (y - m * p);
  307. int fz = (int) (z - n * p);
  308. if ((Math.min(x, tx) <= fx && fx <= Math.max(x, tx)) && (Math.min(y, ty) <= fy && fy <= Math.max(y, ty))
  309. && (Math.min(z, tz) <= fz && fz <= Math.max(z, tz)))
  310. {
  311. if (((fx >= px1 && fx <= px2) || (fx >= px2 && fx <= px1))
  312. && ((fy >= py1 && fy <= py2) || (fy >= py2 && fy <= py1))
  313. && ((fz >= pz1 && fz <= pz2) || (fz >= pz2 && fz <= pz1)))
  314. return true; // Door between
  315. }
  316. }
  317. }
  318. }
  319. }
  320. return false;
  321. }
  322. private void onStart()
  323. {
  324. try
  325. {
  326. checkAutoOpen();
  327. }
  328. catch (NullPointerException e)
  329. {
  330. _log.log(Level.WARNING, "There are errors in your Door.csv file. Update door.csv", e);
  331. }
  332. }
  333. @SuppressWarnings("synthetic-access")
  334. private static class SingletonHolder
  335. {
  336. protected static final DoorTable _instance = new DoorTable();
  337. }
  338. }