FortManager.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import net.sf.l2j.L2DatabaseFactory;
  22. import net.sf.l2j.gameserver.model.L2Clan;
  23. import net.sf.l2j.gameserver.model.L2Object;
  24. import net.sf.l2j.gameserver.model.entity.Fort;
  25. public class FortManager
  26. {
  27. protected static final Logger _log = Logger.getLogger(FortManager.class.getName());
  28. // =========================================================
  29. private static FortManager _instance;
  30. public static final FortManager getInstance()
  31. {
  32. if (_instance == null)
  33. {
  34. _log.info("Initializing FortManager");
  35. _instance = new FortManager();
  36. _instance.load();
  37. }
  38. return _instance;
  39. }
  40. // =========================================================
  41. // Data Field
  42. private List<Fort> _forts;
  43. // =========================================================
  44. // Constructor
  45. public FortManager() {}
  46. // =========================================================
  47. // Method - Public
  48. public final int findNearestFortIndex(L2Object obj)
  49. {
  50. int index = getFortIndex(obj);
  51. if (index < 0)
  52. {
  53. double closestDistance = 99999999;
  54. double distance;
  55. Fort fort;
  56. for (int i = 0; i < getForts().size(); i++)
  57. {
  58. fort = getForts().get(i);
  59. if (fort == null) continue;
  60. distance = fort.getDistance(obj);
  61. if (closestDistance > distance)
  62. {
  63. closestDistance = distance;
  64. index = i;
  65. }
  66. }
  67. }
  68. return index;
  69. }
  70. // =========================================================
  71. // Method - Private
  72. private final void load()
  73. {
  74. java.sql.Connection con = null;
  75. try
  76. {
  77. PreparedStatement statement;
  78. ResultSet rs;
  79. con = L2DatabaseFactory.getInstance().getConnection();
  80. statement = con.prepareStatement("Select id from fort order by id");
  81. rs = statement.executeQuery();
  82. while (rs.next())
  83. {
  84. getForts().add(new Fort(rs.getInt("id")));
  85. }
  86. rs.close();
  87. statement.close();
  88. _log.info("Loaded: " + getForts().size() + " fortress");
  89. }
  90. catch (Exception e)
  91. {
  92. _log.warning("Exception: loadFortData(): " + e.getMessage());
  93. e.printStackTrace();
  94. }
  95. finally
  96. {
  97. try
  98. {
  99. con.close();
  100. }
  101. catch (Exception e)
  102. {}
  103. }
  104. }
  105. // =========================================================
  106. // Property - Public
  107. public final Fort getFortById(int fortId)
  108. {
  109. for (Fort f : getForts())
  110. {
  111. if (f.getFortId() == fortId)
  112. return f;
  113. }
  114. return null;
  115. }
  116. public final Fort getFortByOwner(L2Clan clan)
  117. {
  118. for (Fort f : getForts())
  119. {
  120. if (f.getOwnerId() == clan.getClanId())
  121. return f;
  122. }
  123. return null;
  124. }
  125. public final Fort getFort(String name)
  126. {
  127. for (Fort f : getForts())
  128. {
  129. if (f.getName().equalsIgnoreCase(name.trim()))
  130. return f;
  131. }
  132. return null;
  133. }
  134. public final Fort getFort(int x, int y, int z)
  135. {
  136. for (Fort f : getForts())
  137. {
  138. if (f.checkIfInZone(x, y, z))
  139. return f;
  140. }
  141. return null;
  142. }
  143. public final Fort getFort(L2Object activeObject) { return getFort(activeObject.getX(), activeObject.getY(), activeObject.getZ()); }
  144. public final int getFortIndex(int fortId)
  145. {
  146. Fort fort;
  147. for (int i = 0; i < getForts().size(); i++)
  148. {
  149. fort = getForts().get(i);
  150. if (fort != null && fort.getFortId() == fortId) return i;
  151. }
  152. return -1;
  153. }
  154. public final int getFortIndex(L2Object activeObject)
  155. {
  156. return getFortIndex(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  157. }
  158. public final int getFortIndex(int x, int y, int z)
  159. {
  160. Fort fort;
  161. for (int i = 0; i < getForts().size(); i++)
  162. {
  163. fort = getForts().get(i);
  164. if (fort != null && fort.checkIfInZone(x, y, z)) return i;
  165. }
  166. return -1;
  167. }
  168. public final List<Fort> getForts()
  169. {
  170. if (_forts == null) _forts = new FastList<Fort>();
  171. return _forts;
  172. }
  173. }