DoorTable.java 12 KB

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