ZoneManager.java 13 KB

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