DoorTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.Map;
  23. import java.util.StringTokenizer;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastMap;
  26. import net.sf.l2j.Config;
  27. import net.sf.l2j.gameserver.idfactory.IdFactory;
  28. import net.sf.l2j.gameserver.instancemanager.ClanHallManager;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  30. import net.sf.l2j.gameserver.model.entity.ClanHall;
  31. import net.sf.l2j.gameserver.pathfinding.AbstractNodeLoc;
  32. import net.sf.l2j.gameserver.templates.L2CharTemplate;
  33. import net.sf.l2j.gameserver.templates.StatsSet;
  34. public class DoorTable
  35. {
  36. private static Logger _log = Logger.getLogger(DoorTable.class.getName());
  37. private Map<Integer,L2DoorInstance> _staticItems;
  38. private static DoorTable _instance;
  39. public static DoorTable getInstance()
  40. {
  41. if (_instance == null)
  42. _instance = new DoorTable();
  43. return _instance;
  44. }
  45. public DoorTable()
  46. {
  47. _staticItems = new FastMap<Integer,L2DoorInstance>();
  48. //parseData();
  49. }
  50. public void reloadAll()
  51. {
  52. respawn();
  53. }
  54. public void respawn()
  55. {
  56. // L2DoorInstance[] currentDoors = getDoors();
  57. _staticItems = null;
  58. _instance = null;
  59. _instance = new DoorTable();
  60. }
  61. public void parseData()
  62. {
  63. LineNumberReader lnr = null;
  64. try
  65. {
  66. File doorData = new File(Config.DATAPACK_ROOT, "data/door.csv");
  67. lnr = new LineNumberReader(new BufferedReader(new FileReader(doorData)));
  68. String line = null;
  69. _log.warning("Searching clan halls doors:");
  70. while ((line = lnr.readLine()) != null)
  71. {
  72. if (line.trim().length() == 0 || line.startsWith("#"))
  73. continue;
  74. L2DoorInstance door = parseList(line);
  75. _staticItems.put(door.getDoorId(), door);
  76. door.spawnMe(door.getX(), door.getY(),door.getZ());
  77. ClanHall clanhall = ClanHallManager.getInstance().getNearbyClanHall(door.getX(), door.getY(), 500);
  78. if (clanhall != null)
  79. {
  80. clanhall.getDoors().add(door);
  81. door.setClanHall(clanhall);
  82. if (Config.DEBUG)
  83. _log.warning("door "+door.getDoorName()+" attached to ch "+clanhall.getName());
  84. }
  85. }
  86. _log.config("DoorTable: Loaded " + _staticItems.size() + " Door Templates.");
  87. }
  88. catch (FileNotFoundException e)
  89. {
  90. _initialized = false;
  91. _log.warning("door.csv is missing in data folder");
  92. }
  93. catch (IOException e)
  94. {
  95. _initialized = false;
  96. _log.warning("error while creating door table " + e);
  97. }
  98. finally
  99. {
  100. try { lnr.close(); } catch (Exception e1) { /* ignore problems */ }
  101. }
  102. }
  103. public static L2DoorInstance parseList(String line)
  104. {
  105. StringTokenizer st = new StringTokenizer(line, ";");
  106. String name = st.nextToken();
  107. int id = Integer.parseInt(st.nextToken());
  108. int x = Integer.parseInt(st.nextToken());
  109. int y = Integer.parseInt(st.nextToken());
  110. int z = Integer.parseInt(st.nextToken());
  111. int rangeXMin = Integer.parseInt(st.nextToken());
  112. int rangeYMin = Integer.parseInt(st.nextToken());
  113. int rangeZMin = Integer.parseInt(st.nextToken());
  114. int rangeXMax = Integer.parseInt(st.nextToken());
  115. int rangeYMax = Integer.parseInt(st.nextToken());
  116. int rangeZMax = Integer.parseInt(st.nextToken());
  117. int hp = Integer.parseInt(st.nextToken());
  118. int pdef = Integer.parseInt(st.nextToken());
  119. int mdef = Integer.parseInt(st.nextToken());
  120. boolean unlockable = false;
  121. if (st.hasMoreTokens())
  122. unlockable = Boolean.parseBoolean(st.nextToken());
  123. if (rangeXMin > rangeXMax) _log.severe("Error in door data, ID:"+id);
  124. if (rangeYMin > rangeYMax) _log.severe("Error in door data, ID:"+id);
  125. if (rangeZMin > rangeZMax) _log.severe("Error in door data, ID:"+id);
  126. int collisionRadius; // (max) radius for movement checks
  127. if ((rangeXMax - rangeXMin) > (rangeYMax - rangeYMin))
  128. collisionRadius = rangeYMax - rangeYMin;
  129. else
  130. collisionRadius = rangeXMax - rangeXMin;
  131. StatsSet npcDat = new StatsSet();
  132. npcDat.set("npcId", id);
  133. npcDat.set("level", 0);
  134. npcDat.set("jClass", "door");
  135. npcDat.set("baseSTR", 0);
  136. npcDat.set("baseCON", 0);
  137. npcDat.set("baseDEX", 0);
  138. npcDat.set("baseINT", 0);
  139. npcDat.set("baseWIT", 0);
  140. npcDat.set("baseMEN", 0);
  141. npcDat.set("baseShldDef", 0);
  142. npcDat.set("baseShldRate", 0);
  143. npcDat.set("baseAccCombat", 38);
  144. npcDat.set("baseEvasRate", 38);
  145. npcDat.set("baseCritRate", 38);
  146. //npcDat.set("name", "");
  147. npcDat.set("collision_radius", collisionRadius);
  148. npcDat.set("collision_height", rangeZMax - rangeZMin);
  149. npcDat.set("sex", "male");
  150. npcDat.set("type", "");
  151. npcDat.set("baseAtkRange", 0);
  152. npcDat.set("baseMpMax", 0);
  153. npcDat.set("baseCpMax", 0);
  154. npcDat.set("rewardExp", 0);
  155. npcDat.set("rewardSp", 0);
  156. npcDat.set("basePAtk", 0);
  157. npcDat.set("baseMAtk", 0);
  158. npcDat.set("basePAtkSpd", 0);
  159. npcDat.set("aggroRange", 0);
  160. npcDat.set("baseMAtkSpd", 0);
  161. npcDat.set("rhand", 0);
  162. npcDat.set("lhand", 0);
  163. npcDat.set("armor", 0);
  164. npcDat.set("baseWalkSpd", 0);
  165. npcDat.set("baseRunSpd", 0);
  166. npcDat.set("name", name);
  167. npcDat.set("baseHpMax", hp);
  168. npcDat.set("baseHpReg", 3.e-3f);
  169. npcDat.set("baseMpReg", 3.e-3f);
  170. npcDat.set("basePDef", pdef);
  171. npcDat.set("baseMDef", mdef);
  172. L2CharTemplate template = new L2CharTemplate(npcDat);
  173. L2DoorInstance door = new L2DoorInstance(IdFactory.getInstance().getNextId(),template, id, name, unlockable);
  174. door.setRange(rangeXMin, rangeYMin, rangeZMin, rangeXMax, rangeYMax, rangeZMax);
  175. try
  176. {
  177. door.setMapRegion(MapRegionTable.getInstance().getMapRegion(x,y));
  178. }
  179. catch (Exception e)
  180. {
  181. _log.severe("Error in door data, ID:"+id);
  182. }
  183. door.setCurrentHpMp(door.getMaxHp(), door.getMaxMp());
  184. door.setOpen(1);
  185. door.setXYZInvisible(x,y,z);
  186. return door;
  187. }
  188. public boolean isInitialized()
  189. {
  190. return _initialized;
  191. }
  192. private boolean _initialized = true;
  193. public L2DoorInstance getDoor(Integer id)
  194. {
  195. return _staticItems.get(id);
  196. }
  197. public void putDoor(L2DoorInstance door)
  198. {
  199. _staticItems.put(door.getDoorId(), door);
  200. }
  201. public L2DoorInstance[] getDoors()
  202. {
  203. L2DoorInstance[] _allTemplates = _staticItems.values().toArray(new L2DoorInstance[_staticItems.size()]);
  204. return _allTemplates;
  205. }
  206. /**
  207. * Performs a check and sets up a scheduled task for
  208. * those doors that require auto opening/closing.
  209. */
  210. public void checkAutoOpen()
  211. {
  212. for (L2DoorInstance doorInst : getDoors())
  213. // Garden of Eva (every 7 minutes)
  214. if (doorInst.getDoorName().startsWith("goe"))
  215. doorInst.setAutoActionDelay(420000);
  216. // Tower of Insolence (every 5 minutes)
  217. else if (doorInst.getDoorName().startsWith("aden_tower"))
  218. doorInst.setAutoActionDelay(300000);
  219. /* TODO: check which are automatic
  220. // devils (every 5 minutes)
  221. else if (doorInst.getDoorName().startsWith("pirate_isle"))
  222. doorInst.setAutoActionDelay(300000);
  223. // Cruma Tower (every 20 minutes)
  224. else if (doorInst.getDoorName().startsWith("cruma"))
  225. doorInst.setAutoActionDelay(1200000);
  226. // Coral Garden Gate (every 15 minutes)
  227. else if (doorInst.getDoorName().startsWith("Coral_garden"))
  228. doorInst.setAutoActionDelay(900000);
  229. // Normil's cave (every 5 minutes)
  230. else if (doorInst.getDoorName().startsWith("Normils_cave"))
  231. doorInst.setAutoActionDelay(300000);
  232. // Normil's Garden (every 15 minutes)
  233. else if (doorInst.getDoorName().startsWith("Normils_garden"))
  234. doorInst.setAutoActionDelay(900000);
  235. */
  236. }
  237. public boolean checkIfDoorsBetween(AbstractNodeLoc start, AbstractNodeLoc end)
  238. {
  239. return checkIfDoorsBetween(start.getX(), start.getY(), start.getZ(), end.getX(), end.getY(), end.getZ());
  240. }
  241. public boolean checkIfDoorsBetween(int x, int y, int z, int tx, int ty, int tz)
  242. {
  243. int region;
  244. try
  245. {
  246. region = MapRegionTable.getInstance().getMapRegion(x,y);
  247. }
  248. catch (Exception e)
  249. {
  250. return false;
  251. }
  252. for (L2DoorInstance doorInst : getDoors())
  253. {
  254. if (doorInst.getMapRegion() != region)
  255. continue;
  256. if (doorInst.getXMax() == 0)
  257. continue;
  258. // line segment goes through box
  259. // heavy approximation disabling some shooting angles especially near 2-piece doors
  260. // but most calculations should stop short
  261. // phase 1, x
  262. if (x <= doorInst.getXMax() && tx >= doorInst.getXMin() || tx <= doorInst.getXMax() && x >= doorInst.getXMin())
  263. {
  264. //phase 2, y
  265. if (y <= doorInst.getYMax() && ty >= doorInst.getYMin() || ty <= doorInst.getYMax() && y >= doorInst.getYMin())
  266. {
  267. // phase 3, z (there's a small problem when the other is above/under door level..)
  268. if (z >= doorInst.getZMin() && z <= doorInst.getZMax() && tz >= doorInst.getZMin() && tz <= doorInst.getZMax())
  269. {
  270. if (!(doorInst.getCurrentHp() <= 0 || doorInst.getOpen() == 0))
  271. return true;
  272. }
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. }