DoorTable.java 11 KB

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