FortManager.java 4.9 KB

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