DoorTable.java 11 KB

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