TownManager.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.util.logging.Logger;
  17. import javolution.util.FastList;
  18. import net.sf.l2j.gameserver.datatables.MapRegionTable;
  19. import net.sf.l2j.gameserver.model.L2Object;
  20. import net.sf.l2j.gameserver.model.entity.Castle;
  21. import net.sf.l2j.gameserver.model.zone.type.L2TownZone;
  22. public class TownManager
  23. {
  24. private static final Logger _log = Logger.getLogger(TownManager.class.getName());
  25. // =========================================================
  26. private static TownManager _instance;
  27. public static final TownManager getInstance()
  28. {
  29. if (_instance == null)
  30. {
  31. _log.info("Initializing TownManager");
  32. _instance = new TownManager();
  33. }
  34. return _instance;
  35. }
  36. // =========================================================
  37. // =========================================================
  38. // Data Field
  39. private FastList<L2TownZone> _towns;
  40. // =========================================================
  41. // Constructor
  42. public TownManager()
  43. {
  44. }
  45. // =========================================================
  46. // Property - Public
  47. public void addTown(L2TownZone arena)
  48. {
  49. if (_towns == null)
  50. _towns = new FastList<L2TownZone>();
  51. _towns.add(arena);
  52. }
  53. public final L2TownZone getClosestTown(L2Object activeObject)
  54. {
  55. switch (MapRegionTable.getInstance().getMapRegion(activeObject.getPosition().getX(), activeObject.getPosition().getY()))
  56. {
  57. case 0:
  58. return getTown(2); // TI
  59. case 1:
  60. return getTown(3); // Elven
  61. case 2:
  62. return getTown(1); // DE
  63. case 3:
  64. return getTown(4); // Orc
  65. case 4:
  66. return getTown(6); // Dwarven
  67. case 5:
  68. return getTown(7); // Gludio
  69. case 6:
  70. return getTown(5); // Gludin
  71. case 7:
  72. return getTown(8); // Dion
  73. case 8:
  74. return getTown(9); // Giran
  75. case 9:
  76. return getTown(10); // Oren
  77. case 10:
  78. return getTown(12); // Aden
  79. case 11:
  80. return getTown(11); // HV
  81. case 12:
  82. return getTown(9); // Giran Harbour
  83. case 13:
  84. return getTown(15); // Heine
  85. case 14:
  86. return getTown(14); // Rune
  87. case 15:
  88. return getTown(13); // Goddard
  89. case 16:
  90. return getTown(17); // Schuttgart
  91. case 17:
  92. return getTown(16); // Floran
  93. case 18:
  94. return getTown(19); //Primeval Isle
  95. case 19:
  96. return getTown(20); //Kamael Village
  97. case 20:
  98. return getTown(21); //South of Wastelands Camp
  99. case 21:
  100. return getTown(22); //Fantasy Island
  101. }
  102. return getTown(16); // Default to floran
  103. }
  104. public final boolean townHasCastleInSiege(int townId)
  105. {
  106. //int[] castleidarray = {0,0,0,0,0,0,0,1,2,3,4,0,5,0,0,6,0};
  107. int[] castleidarray = {0,0,0,0,0,0,0,1,2,3,4,0,5,7,8,6,0,9,0,0,0,0,0};
  108. int castleIndex= castleidarray[townId] ;
  109. if ( castleIndex > 0 )
  110. {
  111. Castle castle = CastleManager.getInstance().getCastles().get(CastleManager.getInstance().getCastleIndex(castleIndex));
  112. if (castle != null)
  113. return castle.getSiege().getIsInProgress();
  114. }
  115. return false;
  116. }
  117. public final boolean townHasCastleInSiege(int x, int y)
  118. {
  119. int curtown= (MapRegionTable.getInstance().getMapRegion(x, y));
  120. //int[] castleidarray = {0,0,0,0,0,1,0,2,3,4,5,0,0,6,0,0,0,0};
  121. int[] castleidarray = {0,0,0,0,0,1,0,2,3,4,5,0,0,6,8,7,9,0,0,0,0,0};
  122. //find an instance of the castle for this town.
  123. int castleIndex = castleidarray[curtown];
  124. if ( castleIndex > 0 )
  125. {
  126. Castle castle = CastleManager.getInstance().getCastles().get(CastleManager.getInstance().getCastleIndex(castleIndex));
  127. if (castle != null)
  128. return castle.getSiege().getIsInProgress();
  129. }
  130. return false;
  131. }
  132. public final L2TownZone getTown(int townId)
  133. {
  134. for (L2TownZone temp : _towns)
  135. if (temp.getTownId() == townId) return temp;
  136. return null;
  137. }
  138. /**
  139. * Returns the town at that position (if any)
  140. * @param x
  141. * @param y
  142. * @param z
  143. * @return
  144. */
  145. public final L2TownZone getTown(int x, int y, int z)
  146. {
  147. for (L2TownZone temp : _towns)
  148. if (temp.isInsideZone(x, y, z)) return temp;
  149. return null;
  150. }
  151. }