FortManager.java 4.7 KB

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