ZoneData.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.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.instancemanager.ArenaManager;
  26. import net.sf.l2j.gameserver.instancemanager.FishingZoneManager;
  27. import net.sf.l2j.gameserver.instancemanager.GrandBossManager;
  28. import net.sf.l2j.gameserver.instancemanager.OlympiadStadiaManager;
  29. import net.sf.l2j.gameserver.instancemanager.TownManager;
  30. import net.sf.l2j.gameserver.model.L2World;
  31. import net.sf.l2j.gameserver.model.L2WorldRegion;
  32. import net.sf.l2j.gameserver.model.zone.L2ZoneType;
  33. import net.sf.l2j.gameserver.model.zone.form.ZoneCuboid;
  34. import net.sf.l2j.gameserver.model.zone.form.ZoneNPoly;
  35. import net.sf.l2j.gameserver.model.zone.type.L2ArenaZone;
  36. import net.sf.l2j.gameserver.model.zone.type.L2BigheadZone;
  37. import net.sf.l2j.gameserver.model.zone.type.L2BossZone;
  38. import net.sf.l2j.gameserver.model.zone.type.L2CastleTeleportZone;
  39. import net.sf.l2j.gameserver.model.zone.type.L2CastleZone;
  40. import net.sf.l2j.gameserver.model.zone.type.L2ClanHallZone;
  41. import net.sf.l2j.gameserver.model.zone.type.L2DamageZone;
  42. import net.sf.l2j.gameserver.model.zone.type.L2DerbyTrackZone;
  43. import net.sf.l2j.gameserver.model.zone.type.L2FishingZone;
  44. import net.sf.l2j.gameserver.model.zone.type.L2FortZone;
  45. import net.sf.l2j.gameserver.model.zone.type.L2JailZone;
  46. import net.sf.l2j.gameserver.model.zone.type.L2MotherTreeZone;
  47. import net.sf.l2j.gameserver.model.zone.type.L2NoLandingZone;
  48. import net.sf.l2j.gameserver.model.zone.type.L2OlympiadStadiumZone;
  49. import net.sf.l2j.gameserver.model.zone.type.L2PeaceZone;
  50. import net.sf.l2j.gameserver.model.zone.type.L2TownZone;
  51. import net.sf.l2j.gameserver.model.zone.type.L2WaterZone;
  52. import org.w3c.dom.Document;
  53. import org.w3c.dom.NamedNodeMap;
  54. import org.w3c.dom.Node;
  55. /**
  56. * This class manages the augmentation data and can also create new augmentations.
  57. *
  58. * @author durgus
  59. */
  60. public class ZoneData
  61. {
  62. private static final Logger _log = Logger.getLogger(ZoneData.class.getName());
  63. // =========================================================
  64. private static ZoneData _instance;
  65. public static final ZoneData getInstance()
  66. {
  67. if (_instance == null)
  68. {
  69. _instance = new ZoneData();
  70. }
  71. return _instance;
  72. }
  73. // =========================================================
  74. // Data Field
  75. // =========================================================
  76. // Constructor
  77. public ZoneData()
  78. {
  79. _log.info("Loading zones...");
  80. load();
  81. }
  82. // =========================================================
  83. // Method - Private
  84. private final void load()
  85. {
  86. java.sql.Connection con = null;
  87. int zoneCount = 0;
  88. // Get the world regions
  89. L2WorldRegion[][] worldRegions = L2World.getInstance().getAllWorldRegions();
  90. // Load the zone xml
  91. try
  92. {
  93. // Get a sql connection here
  94. con = L2DatabaseFactory.getInstance().getConnection();
  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("CastleTeleportZone"))
  135. temp = new L2CastleTeleportZone(zoneId);
  136. else if (zoneType.equals("FortZone"))
  137. temp = new L2FortZone(zoneId);
  138. else if (zoneType.equals("DamageZone"))
  139. temp = new L2DamageZone(zoneId);
  140. else if (zoneType.equals("Arena"))
  141. temp = new L2ArenaZone(zoneId);
  142. else if (zoneType.equals("MotherTree"))
  143. temp = new L2MotherTreeZone(zoneId);
  144. else if (zoneType.equals("BigheadZone"))
  145. temp = new L2BigheadZone(zoneId);
  146. else if (zoneType.equals("NoLandingZone"))
  147. temp = new L2NoLandingZone(zoneId);
  148. else if (zoneType.equals("JailZone"))
  149. temp = new L2JailZone(zoneId);
  150. else if (zoneType.equals("DerbyTrackZone"))
  151. temp = new L2DerbyTrackZone(zoneId);
  152. else if (zoneType.equals("BossZone"))
  153. temp = new L2BossZone(zoneId);
  154. else if (zoneType.equals("WaterZone"))
  155. temp = new L2WaterZone(zoneId);
  156. // Check for unknown type
  157. if (temp == null)
  158. {
  159. _log.warning("ZoneData: No such zone type: "+zoneType);
  160. continue;
  161. }
  162. // Get the zone shape from sql
  163. try
  164. {
  165. PreparedStatement statement = null;
  166. // Set the correct query
  167. statement = con.prepareStatement("SELECT x,y FROM zone_vertices WHERE id=? ORDER BY 'order' ASC ");
  168. statement.setInt(1, zoneId);
  169. ResultSet rset = statement.executeQuery();
  170. // Create this zone. Parsing for cuboids is a bit different than for other polygons
  171. // cuboids need exactly 2 points to be defined. Other polygons need at least 3 (one per vertex)
  172. if (zoneShape.equals("Cuboid"))
  173. {
  174. int[] x = {0,0};
  175. int[] y = {0,0};
  176. boolean successfulLoad = true;
  177. for (int i=0;i<2; i++)
  178. {
  179. if ( rset.next() )
  180. {
  181. x[i] = rset.getInt("x");
  182. y[i] = rset.getInt("y");
  183. }
  184. else
  185. {
  186. _log.warning("ZoneData: Missing cuboid vertex in sql data for zone: "+zoneId);
  187. rset.close();
  188. statement.close();
  189. successfulLoad = false;
  190. break;
  191. }
  192. }
  193. if (successfulLoad)
  194. temp.setZone(new ZoneCuboid(x[0],x[1], y[0],y[1],minZ,maxZ));
  195. else
  196. continue;
  197. }
  198. else if (zoneShape.equals("NPoly"))
  199. {
  200. FastList<Integer> fl_x = new FastList<Integer>(), fl_y = new FastList<Integer>();
  201. // Load the rest
  202. while (rset.next())
  203. {
  204. fl_x.add(rset.getInt("x"));
  205. fl_y.add(rset.getInt("y"));
  206. }
  207. // An nPoly needs to have at least 3 vertices
  208. if ((fl_x.size() == fl_y.size()) && (fl_x.size() > 2))
  209. {
  210. // Create arrays
  211. int[] aX = new int[fl_x.size()];
  212. int[] aY = new int[fl_y.size()];
  213. // This runs only at server startup so dont complain :>
  214. for (int i=0; i < fl_x.size(); i++) { aX[i] = fl_x.get(i); aY[i] = fl_y.get(i); }
  215. // Create the zone
  216. temp.setZone(new ZoneNPoly(aX, aY, minZ, maxZ));
  217. }
  218. else
  219. {
  220. _log.warning("ZoneData: Bad sql data for zone: "+zoneId);
  221. rset.close();
  222. statement.close();
  223. continue;
  224. }
  225. }
  226. else
  227. {
  228. _log.warning("ZoneData: Unknown shape: "+zoneShape);
  229. rset.close();
  230. statement.close();
  231. continue;
  232. }
  233. rset.close();
  234. statement.close();
  235. }
  236. catch (Exception e)
  237. {
  238. _log.warning("ZoneData: Failed to load zone coordinates: " + e);
  239. }
  240. // Check for aditional parameters
  241. for (Node cd=d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  242. {
  243. if ("stat".equalsIgnoreCase(cd.getNodeName()))
  244. {
  245. attrs = cd.getAttributes();
  246. String name = attrs.getNamedItem("name").getNodeValue();
  247. String val = attrs.getNamedItem("val").getNodeValue();
  248. temp.setParameter(name, val);
  249. }
  250. }
  251. // Skip checks for fishing zones & add to fishing zone manager
  252. if (temp instanceof L2FishingZone)
  253. {
  254. FishingZoneManager.getInstance().addFishingZone((L2FishingZone)temp);
  255. continue;
  256. }
  257. // Register the zone into any world region it intersects with...
  258. // currently 11136 test for each zone :>
  259. int ax,ay,bx,by;
  260. for (int x=0; x < worldRegions.length; x++)
  261. {
  262. for (int y=0; y < worldRegions[x].length; y++)
  263. {
  264. ax = (x-L2World.OFFSET_X) << L2World.SHIFT_BY;
  265. bx = ((x+1)-L2World.OFFSET_X) << L2World.SHIFT_BY;
  266. ay = (y-L2World.OFFSET_Y) << L2World.SHIFT_BY;
  267. by = ((y+1)-L2World.OFFSET_Y) << L2World.SHIFT_BY;
  268. if (temp.getZone().intersectsRectangle(ax, bx, ay, by))
  269. {
  270. if (Config.DEBUG)
  271. {
  272. _log.info("Zone ("+zoneId+") added to: "+x+" "+y);
  273. }
  274. worldRegions[x][y].addZone(temp);
  275. }
  276. }
  277. }
  278. // Special managers for arenas, towns...
  279. if (temp instanceof L2ArenaZone)
  280. ArenaManager.getInstance().addArena((L2ArenaZone)temp);
  281. else if (temp instanceof L2TownZone)
  282. TownManager.getInstance().addTown((L2TownZone)temp);
  283. else if (temp instanceof L2OlympiadStadiumZone)
  284. OlympiadStadiaManager.getInstance().addStadium((L2OlympiadStadiumZone)temp);
  285. else if (temp instanceof L2BossZone)
  286. GrandBossManager.getInstance().addZone((L2BossZone) temp);
  287. // Increase the counter
  288. zoneCount++;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. catch (Exception e)
  295. {
  296. _log.log(Level.SEVERE, "Error while loading zones.", e);
  297. return ;
  298. }
  299. finally
  300. {
  301. try
  302. {
  303. con.close();
  304. }
  305. catch (Exception e)
  306. {
  307. }
  308. }
  309. GrandBossManager.getInstance().initZones();
  310. _log.info("Done: loaded "+zoneCount+" zones.");
  311. }
  312. }