ZoneManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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.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.Collection;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.L2DatabaseFactory;
  31. import com.l2jserver.gameserver.model.L2Object;
  32. import com.l2jserver.gameserver.model.L2World;
  33. import com.l2jserver.gameserver.model.L2WorldRegion;
  34. import com.l2jserver.gameserver.model.actor.L2Character;
  35. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  36. import com.l2jserver.gameserver.model.zone.form.*;
  37. import com.l2jserver.gameserver.model.zone.type.*;
  38. /**
  39. * This class manages the zones
  40. *
  41. * @author durgus
  42. */
  43. public class ZoneManager
  44. {
  45. private static final Logger _log = Logger.getLogger(ZoneManager.class.getName());
  46. private final FastMap<Integer, L2ZoneType> _zones = new FastMap<Integer,L2ZoneType>();
  47. public static final ZoneManager getInstance()
  48. {
  49. return SingletonHolder._instance;
  50. }
  51. // =========================================================
  52. // Data Field
  53. // =========================================================
  54. // Constructor
  55. private ZoneManager()
  56. {
  57. load();
  58. }
  59. public void reload()
  60. {
  61. // int zoneCount = 0;
  62. // Get the world regions
  63. int count = 0;
  64. L2WorldRegion[][] worldRegions = L2World.getInstance().getAllWorldRegions();
  65. for (int x = 0; x < worldRegions.length; x++)
  66. {
  67. for (int y = 0; y < worldRegions[x].length; y++)
  68. {
  69. worldRegions[x][y].getZones().clear();
  70. count++;
  71. }
  72. }
  73. GrandBossManager.getInstance().getZones().clear();
  74. _log.info("Removed zones in " + count + " regions.");
  75. // Load the zones
  76. load();
  77. }
  78. // =========================================================
  79. // Method - Private
  80. private final void load()
  81. {
  82. _log.info("Loading zones...");
  83. Connection con = null;
  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. else if (zoneType.equals("ScriptZone"))
  162. temp = new L2ScriptZone(zoneId);
  163. else if (zoneType.equals("PaganZone"))
  164. temp = new L2PaganZone(zoneId);
  165. else if (zoneType.equals("NoHqZone"))
  166. temp = new L2NoHqZone(zoneId);
  167. else if (zoneType.equals("NoSummonZone"))
  168. temp = new L2NoSummonFriendZone(zoneId);
  169. // Check for unknown type
  170. if (temp == null)
  171. {
  172. _log.warning("ZoneData: No such zone type: " + zoneType);
  173. continue;
  174. }
  175. // Get the zone shape from sql
  176. try
  177. {
  178. PreparedStatement statement = null;
  179. // Set the correct query
  180. statement = con.prepareStatement("SELECT x,y FROM zone_vertices WHERE id=? ORDER BY 'order' ASC ");
  181. statement.setInt(1, zoneId);
  182. ResultSet rset = statement.executeQuery();
  183. // Create this zone. Parsing for cuboids is a
  184. // bit different than for other polygons
  185. // cuboids need exactly 2 points to be defined.
  186. // Other polygons need at least 3 (one per
  187. // vertex)
  188. if (zoneShape.equalsIgnoreCase("Cuboid"))
  189. {
  190. int[] x = { 0, 0 };
  191. int[] y = { 0, 0 };
  192. boolean successfulLoad = true;
  193. for (int i = 0; i < 2; i++)
  194. {
  195. if (rset.next())
  196. {
  197. x[i] = rset.getInt("x");
  198. y[i] = rset.getInt("y");
  199. }
  200. else
  201. {
  202. _log.warning("ZoneData: Missing cuboid vertex in sql data for zone: " + zoneId);
  203. rset.close();
  204. statement.close();
  205. successfulLoad = false;
  206. break;
  207. }
  208. }
  209. if (successfulLoad)
  210. temp.setZone(new ZoneCuboid(x[0], x[1], y[0], y[1], minZ, maxZ));
  211. else
  212. continue;
  213. }
  214. else if (zoneShape.equalsIgnoreCase("NPoly"))
  215. {
  216. FastList<Integer> fl_x = new FastList<Integer>(), fl_y = new FastList<Integer>();
  217. // Load the rest
  218. while (rset.next())
  219. {
  220. fl_x.add(rset.getInt("x"));
  221. fl_y.add(rset.getInt("y"));
  222. }
  223. // An nPoly needs to have at least 3
  224. // vertices
  225. if ((fl_x.size() == fl_y.size()) && (fl_x.size() > 2))
  226. {
  227. // Create arrays
  228. int[] aX = new int[fl_x.size()];
  229. int[] aY = new int[fl_y.size()];
  230. // This runs only at server startup so
  231. // dont complain :>
  232. for (int i = 0; i < fl_x.size(); i++)
  233. {
  234. aX[i] = fl_x.get(i);
  235. aY[i] = fl_y.get(i);
  236. }
  237. // Create the zone
  238. temp.setZone(new ZoneNPoly(aX, aY, minZ, maxZ));
  239. }
  240. else
  241. {
  242. _log.warning("ZoneData: Bad sql data for zone: " + zoneId);
  243. rset.close();
  244. statement.close();
  245. continue;
  246. }
  247. }
  248. else if (zoneShape.equalsIgnoreCase("Cylinder"))
  249. {
  250. // A Cylinder zone requires a centre point
  251. // at x,y and a radius
  252. int zoneRad = Integer.parseInt(attrs.getNamedItem("rad").getNodeValue());
  253. if (rset.next() && zoneRad > 0)
  254. {
  255. int zoneX = rset.getInt("x");
  256. int zoneY = rset.getInt("y");
  257. // create the zone
  258. temp.setZone(new ZoneCylinder(zoneX, zoneY, minZ, maxZ, zoneRad));
  259. }
  260. else
  261. {
  262. _log.warning("ZoneData: Bad sql data for zone: " + zoneId);
  263. rset.close();
  264. statement.close();
  265. continue;
  266. }
  267. }
  268. else
  269. {
  270. _log.warning("ZoneData: Unknown shape: " + zoneShape);
  271. rset.close();
  272. statement.close();
  273. continue;
  274. }
  275. rset.close();
  276. statement.close();
  277. }
  278. catch (Exception e)
  279. {
  280. _log.warning("ZoneData: Failed to load zone coordinates: " + e);
  281. }
  282. // Check for aditional parameters
  283. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  284. {
  285. if ("stat".equalsIgnoreCase(cd.getNodeName()))
  286. {
  287. attrs = cd.getAttributes();
  288. String name = attrs.getNamedItem("name").getNodeValue();
  289. String val = attrs.getNamedItem("val").getNodeValue();
  290. temp.setParameter(name, val);
  291. }
  292. }
  293. addZone(zoneId, temp);
  294. // Register the zone into any world region it
  295. // intersects with...
  296. // currently 11136 test for each zone :>
  297. int ax, ay, bx, by;
  298. for (int x = 0; x < worldRegions.length; x++)
  299. {
  300. for (int y = 0; y < worldRegions[x].length; y++)
  301. {
  302. ax = (x - L2World.OFFSET_X) << L2World.SHIFT_BY;
  303. bx = ((x + 1) - L2World.OFFSET_X) << L2World.SHIFT_BY;
  304. ay = (y - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  305. by = ((y + 1) - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  306. if (temp.getZone().intersectsRectangle(ax, bx, ay, by))
  307. {
  308. if (Config.DEBUG)
  309. {
  310. _log.info("Zone (" + zoneId + ") added to: " + x + " " + y);
  311. }
  312. worldRegions[x][y].addZone(temp);
  313. }
  314. }
  315. }
  316. // Special managers for granbosses...
  317. if (temp instanceof L2BossZone)
  318. GrandBossManager.getInstance().addZone((L2BossZone) temp);
  319. }
  320. }
  321. }
  322. }
  323. }
  324. catch (Exception e)
  325. {
  326. _log.log(Level.SEVERE, "Error while loading zones.", e);
  327. return;
  328. }
  329. finally
  330. {
  331. try
  332. {
  333. con.close();
  334. }
  335. catch (Exception e)
  336. {
  337. }
  338. }
  339. _log.info("Done: loaded " + _zones.size() + " zones.");
  340. }
  341. /**
  342. * Add new zone
  343. *
  344. * @param zone
  345. */
  346. public void addZone(Integer id,L2ZoneType zone)
  347. {
  348. _zones.put(id, zone);
  349. }
  350. /**
  351. * Returns all zones registered with the ZoneManager.
  352. * To minimise iteration processing retrieve zones from L2WorldRegion for a specific location instead.
  353. * @return zones
  354. */
  355. public Collection<L2ZoneType> getAllZones()
  356. {
  357. return _zones.values();
  358. }
  359. public L2ZoneType getZoneById( int id)
  360. {
  361. return _zones.get(id);
  362. }
  363. /**
  364. * Returns all zones from where the object is located
  365. *
  366. * @param object
  367. * @return zones
  368. */
  369. public FastList<L2ZoneType> getZones(L2Object object)
  370. {
  371. return getZones(object.getX(), object.getY(), object.getZ());
  372. }
  373. /**
  374. * Returns zone from where the object is located by type
  375. *
  376. * @param object
  377. * @param type
  378. * @return zone
  379. */
  380. public L2ZoneType getZone(L2Object object, Class<? extends L2ZoneType> type)
  381. {
  382. return getZone(object.getX(), object.getY(), object.getZ(), type);
  383. }
  384. /**
  385. * Returns all zones from given coordinates (plane)
  386. *
  387. * @param x
  388. * @param y
  389. * @return zones
  390. */
  391. public FastList<L2ZoneType> getZones(int x, int y)
  392. {
  393. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  394. FastList<L2ZoneType> temp = new FastList<L2ZoneType>();
  395. for (L2ZoneType zone : region.getZones())
  396. {
  397. if (zone.isInsideZone(x, y))
  398. temp.add(zone);
  399. }
  400. return temp;
  401. }
  402. /**
  403. * Returns all zones from given coordinates
  404. *
  405. * @param x
  406. * @param y
  407. * @param z
  408. * @return zones
  409. */
  410. public FastList<L2ZoneType> getZones(int x, int y, int z)
  411. {
  412. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  413. FastList<L2ZoneType> temp = new FastList<L2ZoneType>();
  414. for (L2ZoneType zone : region.getZones())
  415. {
  416. if (zone.isInsideZone(x, y, z))
  417. temp.add(zone);
  418. }
  419. return temp;
  420. }
  421. /**
  422. * Returns zone from given coordinates
  423. *
  424. * @param x
  425. * @param y
  426. * @param z
  427. * @param type
  428. * @return zone
  429. */
  430. public L2ZoneType getZone(int x, int y, int z, Class<? extends L2ZoneType> type)
  431. {
  432. L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  433. for (L2ZoneType zone : region.getZones())
  434. {
  435. if (zone.isInsideZone(x, y, z) && zone.getClass().equals(type))
  436. return zone;
  437. }
  438. return null;
  439. }
  440. public final L2ArenaZone getArena(L2Character character)
  441. {
  442. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  443. {
  444. if (temp instanceof L2ArenaZone && temp.isCharacterInZone(character))
  445. return ((L2ArenaZone) temp);
  446. }
  447. return null;
  448. }
  449. public final L2OlympiadStadiumZone getOlympiadStadium(L2Character character)
  450. {
  451. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  452. {
  453. if (temp instanceof L2OlympiadStadiumZone && temp.isCharacterInZone(character))
  454. return ((L2OlympiadStadiumZone) temp);
  455. }
  456. return null;
  457. }
  458. @SuppressWarnings("synthetic-access")
  459. private static class SingletonHolder
  460. {
  461. protected static final ZoneManager _instance = new ZoneManager();
  462. }
  463. }