ZoneManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.instancemanager;
  16. import java.io.File;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javolution.util.FastList;
  24. import net.sf.l2j.Config;
  25. import net.sf.l2j.L2DatabaseFactory;
  26. import net.sf.l2j.gameserver.model.L2Object;
  27. import net.sf.l2j.gameserver.model.L2World;
  28. import net.sf.l2j.gameserver.model.L2WorldRegion;
  29. import net.sf.l2j.gameserver.model.actor.L2Character;
  30. import net.sf.l2j.gameserver.model.zone.L2ZoneType;
  31. import net.sf.l2j.gameserver.model.zone.form.*;
  32. import net.sf.l2j.gameserver.model.zone.type.*;
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.NamedNodeMap;
  35. import org.w3c.dom.Node;
  36. /**
  37. * This class manages the augmentation data and can also create new
  38. * augmentations.
  39. *
  40. * @author durgus
  41. */
  42. public class ZoneManager
  43. {
  44. private static final Logger _log = Logger.getLogger(ZoneManager.class.getName());
  45. private final FastList<L2ZoneType> _zones = new FastList<L2ZoneType>();
  46. public static final ZoneManager getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. // =========================================================
  51. // Data Field
  52. // =========================================================
  53. // Constructor
  54. private ZoneManager()
  55. {
  56. load();
  57. }
  58. public void reload()
  59. {
  60. // int zoneCount = 0;
  61. // Get the world regions
  62. int count = 0;
  63. L2WorldRegion[][] worldRegions = L2World.getInstance().getAllWorldRegions();
  64. for (int x = 0; x < worldRegions.length; x++)
  65. {
  66. for (int y = 0; y < worldRegions[x].length; y++)
  67. {
  68. worldRegions[x][y].getZones().clear();
  69. count++;
  70. }
  71. }
  72. GrandBossManager.getInstance().getZones().clear();
  73. _log.info("Removed zones in " + count + " regions.");
  74. // Load the zones
  75. load();
  76. }
  77. // =========================================================
  78. // Method - Private
  79. private final void load()
  80. {
  81. _log.info("Loading zones...");
  82. Connection con = null;
  83. int zoneCount = 0;
  84. _zones.clear();
  85. // Get the world regions
  86. L2WorldRegion[][] worldRegions = L2World.getInstance().getAllWorldRegions();
  87. // Load the zone xml
  88. try
  89. {
  90. // Get a sql connection here
  91. con = L2DatabaseFactory.getInstance().getConnection();
  92. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  93. factory.setValidating(false);
  94. factory.setIgnoringComments(true);
  95. File file = new File(Config.DATAPACK_ROOT + "/data/zones/zone.xml");
  96. if (!file.exists())
  97. {
  98. if (Config.DEBUG)
  99. _log.info("The zone.xml file is missing.");
  100. return;
  101. }
  102. Document doc = factory.newDocumentBuilder().parse(file);
  103. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  104. {
  105. if ("list".equalsIgnoreCase(n.getNodeName()))
  106. {
  107. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  108. {
  109. if ("zone".equalsIgnoreCase(d.getNodeName()))
  110. {
  111. NamedNodeMap attrs = d.getAttributes();
  112. int zoneId = Integer.parseInt(attrs.getNamedItem("id").getNodeValue());
  113. int minZ = Integer.parseInt(attrs.getNamedItem("minZ").getNodeValue());
  114. int maxZ = Integer.parseInt(attrs.getNamedItem("maxZ").getNodeValue());
  115. String zoneType = attrs.getNamedItem("type").getNodeValue();
  116. String zoneShape = attrs.getNamedItem("shape").getNodeValue();
  117. // Create the zone
  118. L2ZoneType temp = null;
  119. if (zoneType.equals("FishingZone"))
  120. temp = new L2FishingZone(zoneId);
  121. else if (zoneType.equals("ClanHallZone"))
  122. temp = new L2ClanHallZone(zoneId);
  123. else if (zoneType.equals("PeaceZone"))
  124. temp = new L2PeaceZone(zoneId);
  125. else if (zoneType.equals("Town"))
  126. temp = new L2TownZone(zoneId);
  127. else if (zoneType.equals("OlympiadStadium"))
  128. temp = new L2OlympiadStadiumZone(zoneId);
  129. else if (zoneType.equals("CastleZone"))
  130. temp = new L2CastleZone(zoneId);
  131. else if (zoneType.equals("CastleTeleportZone"))
  132. temp = new L2CastleTeleportZone(zoneId);
  133. else if (zoneType.equals("FortZone"))
  134. temp = new L2FortZone(zoneId);
  135. else if (zoneType.equals("DamageZone"))
  136. temp = new L2DamageZone(zoneId);
  137. else if (zoneType.equals("PoisonZone"))
  138. temp = new L2PoisonZone(zoneId);
  139. else if (zoneType.equals("SwampZone"))
  140. temp = new L2SwampZone(zoneId);
  141. else if (zoneType.equals("Arena"))
  142. temp = new L2ArenaZone(zoneId);
  143. else if (zoneType.equals("MotherTree"))
  144. temp = new L2MotherTreeZone(zoneId);
  145. else if (zoneType.equals("BigheadZone"))
  146. temp = new L2BigheadZone(zoneId);
  147. else if (zoneType.equals("LandingZone"))
  148. temp = new L2LandingZone(zoneId);
  149. else if (zoneType.equals("NoLandingZone"))
  150. temp = new L2NoLandingZone(zoneId);
  151. else if (zoneType.equals("JailZone"))
  152. temp = new L2JailZone(zoneId);
  153. else if (zoneType.equals("DerbyTrackZone"))
  154. temp = new L2DerbyTrackZone(zoneId);
  155. else if (zoneType.equals("BossZone"))
  156. temp = new L2BossZone(zoneId);
  157. else if (zoneType.equals("WaterZone"))
  158. temp = new L2WaterZone(zoneId);
  159. else if (zoneType.equals("NoStoreZone"))
  160. temp = new L2NoStoreZone(zoneId);
  161. // Check for unknown type
  162. if (temp == null)
  163. {
  164. _log.warning("ZoneData: No such zone type: " + zoneType);
  165. continue;
  166. }
  167. // Get the zone shape from sql
  168. try
  169. {
  170. PreparedStatement statement = null;
  171. // Set the correct query
  172. statement = con.prepareStatement("SELECT x,y FROM zone_vertices WHERE id=? ORDER BY 'order' ASC ");
  173. statement.setInt(1, zoneId);
  174. ResultSet rset = statement.executeQuery();
  175. // Create this zone. Parsing for cuboids is a
  176. // bit different than for other polygons
  177. // cuboids need exactly 2 points to be defined.
  178. // Other polygons need at least 3 (one per
  179. // vertex)
  180. if (zoneShape.equalsIgnoreCase("Cuboid"))
  181. {
  182. int[] x = { 0, 0 };
  183. int[] y = { 0, 0 };
  184. boolean successfulLoad = true;
  185. for (int i = 0; i < 2; i++)
  186. {
  187. if (rset.next())
  188. {
  189. x[i] = rset.getInt("x");
  190. y[i] = rset.getInt("y");
  191. }
  192. else
  193. {
  194. _log.warning("ZoneData: Missing cuboid vertex in sql data for zone: " + zoneId);
  195. rset.close();
  196. statement.close();
  197. successfulLoad = false;
  198. break;
  199. }
  200. }
  201. if (successfulLoad)
  202. temp.setZone(new ZoneCuboid(x[0], x[1], y[0], y[1], minZ, maxZ));
  203. else
  204. continue;
  205. }
  206. else if (zoneShape.equalsIgnoreCase("NPoly"))
  207. {
  208. FastList<Integer> fl_x = new FastList<Integer>(), fl_y = new FastList<Integer>();
  209. // Load the rest
  210. while (rset.next())
  211. {
  212. fl_x.add(rset.getInt("x"));
  213. fl_y.add(rset.getInt("y"));
  214. }
  215. // An nPoly needs to have at least 3
  216. // vertices
  217. if ((fl_x.size() == fl_y.size()) && (fl_x.size() > 2))
  218. {
  219. // Create arrays
  220. int[] aX = new int[fl_x.size()];
  221. int[] aY = new int[fl_y.size()];
  222. // This runs only at server startup so
  223. // dont complain :>
  224. for (int i = 0; i < fl_x.size(); i++)
  225. {
  226. aX[i] = fl_x.get(i);
  227. aY[i] = fl_y.get(i);
  228. }
  229. // Create the zone
  230. temp.setZone(new ZoneNPoly(aX, aY, minZ, maxZ));
  231. }
  232. else
  233. {
  234. _log.warning("ZoneData: Bad sql data for zone: " + zoneId);
  235. rset.close();
  236. statement.close();
  237. continue;
  238. }
  239. }
  240. else if (zoneShape.equalsIgnoreCase("Cylinder"))
  241. {
  242. // A Cylinder zone requires a centre point
  243. // at x,y and a radius
  244. int zoneRad = Integer.parseInt(attrs.getNamedItem("rad").getNodeValue());
  245. if (rset.next() && zoneRad > 0)
  246. {
  247. int zoneX = rset.getInt("x");
  248. int zoneY = rset.getInt("y");
  249. // create the zone
  250. temp.setZone(new ZoneCylinder(zoneX, zoneY, minZ, maxZ, zoneRad));
  251. }
  252. else
  253. {
  254. _log.warning("ZoneData: Bad sql data for zone: " + zoneId);
  255. rset.close();
  256. statement.close();
  257. continue;
  258. }
  259. }
  260. else
  261. {
  262. _log.warning("ZoneData: Unknown shape: " + zoneShape);
  263. rset.close();
  264. statement.close();
  265. continue;
  266. }
  267. rset.close();
  268. statement.close();
  269. }
  270. catch (Exception e)
  271. {
  272. _log.warning("ZoneData: Failed to load zone coordinates: " + e);
  273. }
  274. // Check for aditional parameters
  275. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  276. {
  277. if ("stat".equalsIgnoreCase(cd.getNodeName()))
  278. {
  279. attrs = cd.getAttributes();
  280. String name = attrs.getNamedItem("name").getNodeValue();
  281. String val = attrs.getNamedItem("val").getNodeValue();
  282. temp.setParameter(name, val);
  283. }
  284. }
  285. addZone(temp);
  286. // Register the zone into any world region it
  287. // intersects with...
  288. // currently 11136 test for each zone :>
  289. int ax, ay, bx, by;
  290. for (int x = 0; x < worldRegions.length; x++)
  291. {
  292. for (int y = 0; y < worldRegions[x].length; y++)
  293. {
  294. ax = (x - L2World.OFFSET_X) << L2World.SHIFT_BY;
  295. bx = ((x + 1) - L2World.OFFSET_X) << L2World.SHIFT_BY;
  296. ay = (y - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  297. by = ((y + 1) - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  298. if (temp.getZone().intersectsRectangle(ax, bx, ay, by))
  299. {
  300. if (Config.DEBUG)
  301. {
  302. _log.info("Zone (" + zoneId + ") added to: " + x + " " + y);
  303. }
  304. worldRegions[x][y].addZone(temp);
  305. }
  306. }
  307. }
  308. // Special managers for granbosses...
  309. if (temp instanceof L2BossZone)
  310. GrandBossManager.getInstance().addZone((L2BossZone) temp);
  311. // Increase the counter
  312. zoneCount++;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. catch (Exception e)
  319. {
  320. _log.log(Level.SEVERE, "Error while loading zones.", e);
  321. return;
  322. }
  323. finally
  324. {
  325. try
  326. {
  327. con.close();
  328. }
  329. catch (Exception e)
  330. {
  331. }
  332. }
  333. _log.info("Done: loaded " + zoneCount + " zones.");
  334. }
  335. /**
  336. * Add new zone
  337. *
  338. * @param zone
  339. */
  340. public void addZone(L2ZoneType zone)
  341. {
  342. _zones.add(zone);
  343. }
  344. /**
  345. * Returns all zones registered with the ZoneManager.
  346. * To minimise iteration processing retrieve zones from L2WorldRegion for a specific location instead.
  347. * @return zones
  348. */
  349. public FastList<L2ZoneType> getAllZones()
  350. {
  351. return _zones;
  352. }
  353. /**
  354. * Returns all zones from where the object is located
  355. *
  356. * @param object
  357. * @return zones
  358. */
  359. public FastList<L2ZoneType> getZones(L2Object object)
  360. {
  361. return getZones(object.getX(), object.getY(), object.getZ());
  362. }
  363. /**
  364. * Returns all zones from given coordinates (plane)
  365. *
  366. * @param x
  367. * @param y
  368. * @return zones
  369. */
  370. public FastList<L2ZoneType> getZones(int x, int y)
  371. {
  372. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  373. FastList<L2ZoneType> temp = new FastList<L2ZoneType>();
  374. for (L2ZoneType zone : region.getZones())
  375. {
  376. if (zone.isInsideZone(x, y))
  377. temp.add(zone);
  378. }
  379. return temp;
  380. }
  381. /**
  382. * Returns all zones from given coordinates
  383. *
  384. * @param x
  385. * @param y
  386. * @param z
  387. * @return zones
  388. */
  389. public FastList<L2ZoneType> getZones(int x, int y, int z)
  390. {
  391. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  392. FastList<L2ZoneType> temp = new FastList<L2ZoneType>();
  393. for (L2ZoneType zone : region.getZones())
  394. {
  395. if (zone.isInsideZone(x, y, z))
  396. temp.add(zone);
  397. }
  398. return temp;
  399. }
  400. public final L2ArenaZone getArena(L2Character character)
  401. {
  402. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  403. {
  404. if (temp instanceof L2ArenaZone && temp.isCharacterInZone(character))
  405. return ((L2ArenaZone) temp);
  406. }
  407. return null;
  408. }
  409. public final L2OlympiadStadiumZone getOlympiadStadium(L2Character character)
  410. {
  411. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  412. {
  413. if (temp instanceof L2OlympiadStadiumZone && temp.isCharacterInZone(character))
  414. return ((L2OlympiadStadiumZone) temp);
  415. }
  416. return null;
  417. }
  418. @SuppressWarnings("synthetic-access")
  419. private static class SingletonHolder
  420. {
  421. protected static final ZoneManager _instance = new ZoneManager();
  422. }
  423. }