FortManager.java 4.9 KB

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