ZoneManager.java 16 KB

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