FortManager.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.InstanceListManager;
  29. import com.l2jserver.gameserver.model.L2Clan;
  30. import com.l2jserver.gameserver.model.L2Object;
  31. import com.l2jserver.gameserver.model.entity.Fort;
  32. public final class FortManager implements InstanceListManager
  33. {
  34. protected static final Logger _log = Logger.getLogger(FortManager.class.getName());
  35. private List<Fort> _forts;
  36. public final int findNearestFortIndex(L2Object obj)
  37. {
  38. return findNearestFortIndex(obj, Long.MAX_VALUE);
  39. }
  40. public final int findNearestFortIndex(L2Object obj, long maxDistance)
  41. {
  42. int index = getFortIndex(obj);
  43. if (index < 0)
  44. {
  45. double distance;
  46. Fort fort;
  47. for (int i = 0; i < getForts().size(); i++)
  48. {
  49. fort = getForts().get(i);
  50. if (fort == null)
  51. {
  52. continue;
  53. }
  54. distance = fort.getDistance(obj);
  55. if (maxDistance > distance)
  56. {
  57. maxDistance = (long) distance;
  58. index = i;
  59. }
  60. }
  61. }
  62. return index;
  63. }
  64. public final Fort getFortById(int fortId)
  65. {
  66. for (Fort f : getForts())
  67. {
  68. if (f.getResidenceId() == fortId)
  69. {
  70. return f;
  71. }
  72. }
  73. return null;
  74. }
  75. public final Fort getFortByOwner(L2Clan clan)
  76. {
  77. for (Fort f : getForts())
  78. {
  79. if (f.getOwnerClan() == clan)
  80. {
  81. return f;
  82. }
  83. }
  84. return null;
  85. }
  86. public final Fort getFort(String name)
  87. {
  88. for (Fort f : getForts())
  89. {
  90. if (f.getName().equalsIgnoreCase(name.trim()))
  91. {
  92. return f;
  93. }
  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. {
  103. return f;
  104. }
  105. }
  106. return null;
  107. }
  108. public final Fort getFort(L2Object activeObject)
  109. {
  110. return getFort(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  111. }
  112. public final int getFortIndex(int fortId)
  113. {
  114. Fort fort;
  115. for (int i = 0; i < getForts().size(); i++)
  116. {
  117. fort = getForts().get(i);
  118. if ((fort != null) && (fort.getResidenceId() == fortId))
  119. {
  120. return i;
  121. }
  122. }
  123. return -1;
  124. }
  125. public final int getFortIndex(L2Object activeObject)
  126. {
  127. return getFortIndex(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  128. }
  129. public final int getFortIndex(int x, int y, int z)
  130. {
  131. Fort fort;
  132. for (int i = 0; i < getForts().size(); i++)
  133. {
  134. fort = getForts().get(i);
  135. if ((fort != null) && fort.checkIfInZone(x, y, z))
  136. {
  137. return i;
  138. }
  139. }
  140. return -1;
  141. }
  142. public final List<Fort> getForts()
  143. {
  144. if (_forts == null)
  145. {
  146. _forts = new FastList<>();
  147. }
  148. return _forts;
  149. }
  150. @Override
  151. public void loadInstances()
  152. {
  153. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  154. Statement s = con.createStatement();
  155. ResultSet rs = s.executeQuery("SELECT id FROM fort ORDER BY id"))
  156. {
  157. while (rs.next())
  158. {
  159. getForts().add(new Fort(rs.getInt("id")));
  160. }
  161. _log.info(getClass().getSimpleName() + ": Loaded: " + getForts().size() + " fortress");
  162. for (Fort fort : getForts())
  163. {
  164. fort.getSiege().getSiegeGuardManager().loadSiegeGuard();
  165. }
  166. }
  167. catch (Exception e)
  168. {
  169. _log.log(Level.WARNING, "Exception: loadFortData(): " + e.getMessage(), e);
  170. }
  171. }
  172. @Override
  173. public void updateReferences()
  174. {
  175. }
  176. @Override
  177. public void activateInstances()
  178. {
  179. for (final Fort fort : _forts)
  180. {
  181. fort.activateInstance();
  182. }
  183. }
  184. public static final FortManager getInstance()
  185. {
  186. return SingletonHolder._instance;
  187. }
  188. private static class SingletonHolder
  189. {
  190. protected static final FortManager _instance = new FortManager();
  191. }
  192. }