FortSiegeGuardManager.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.logging.Logger;
  20. import com.l2jserver.L2DatabaseFactory;
  21. import com.l2jserver.gameserver.datatables.NpcTable;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  23. import com.l2jserver.gameserver.model.actor.instance.L2FortBallistaInstance;
  24. import com.l2jserver.gameserver.model.entity.Fort;
  25. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  26. import javolution.util.FastList;
  27. import javolution.util.FastMap;
  28. public class FortSiegeGuardManager
  29. {
  30. private static final Logger _log = Logger.getLogger(FortSiegeGuardManager.class.getName());
  31. private Fort _fort;
  32. protected FastMap<Integer, FastList<L2Spawn>> _siegeGuards = new FastMap<Integer, FastList<L2Spawn>>();
  33. protected FastList<L2Spawn> _siegeGuardsSpawns;
  34. public FortSiegeGuardManager(Fort fort)
  35. {
  36. _fort = fort;
  37. }
  38. /**
  39. * Spawn guards.<BR><BR>
  40. */
  41. public void spawnSiegeGuard()
  42. {
  43. try
  44. {
  45. FastList<L2Spawn> monsterList = getSiegeGuardSpawn().get(getFort().getFortId());
  46. if (monsterList != null)
  47. {
  48. for (L2Spawn spawnDat : monsterList)
  49. {
  50. spawnDat.doSpawn();
  51. if (spawnDat.getLastSpawn() instanceof L2FortBallistaInstance)
  52. spawnDat.stopRespawn();
  53. else
  54. spawnDat.startRespawn();
  55. }
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. _log.warning("Error spawning siege guards for fort " + getFort().getName() + ":" + e.getMessage());
  61. e.printStackTrace();
  62. }
  63. }
  64. /**
  65. * Unspawn guards.<BR><BR>
  66. */
  67. public void unspawnSiegeGuard()
  68. {
  69. try
  70. {
  71. FastList<L2Spawn> monsterList = getSiegeGuardSpawn().get(getFort().getFortId());
  72. if (monsterList != null)
  73. {
  74. for (L2Spawn spawnDat : monsterList)
  75. {
  76. spawnDat.stopRespawn();
  77. if (spawnDat.getLastSpawn() != null)
  78. spawnDat.getLastSpawn().doDie(spawnDat.getLastSpawn());
  79. }
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. _log.warning("Error unspawning siege guards for fort " + getFort().getName() + ":" + e.getMessage());
  85. e.printStackTrace();
  86. }
  87. }
  88. /**
  89. * Load guards.<BR><BR>
  90. */
  91. void loadSiegeGuard()
  92. {
  93. _siegeGuards.clear();
  94. Connection con = null;
  95. try
  96. {
  97. con = L2DatabaseFactory.getInstance().getConnection();
  98. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_siege_guards Where fortId = ? ");
  99. statement.setInt(1, getFort().getFortId());
  100. ResultSet rs = statement.executeQuery();
  101. L2Spawn spawn1;
  102. L2NpcTemplate template1;
  103. _siegeGuardsSpawns = new FastList<L2Spawn>();
  104. while (rs.next())
  105. {
  106. int fortId = rs.getInt("fortId");
  107. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  108. if (template1 != null)
  109. {
  110. spawn1 = new L2Spawn(template1);
  111. spawn1.setId(rs.getInt("id"));
  112. spawn1.setAmount(1);
  113. spawn1.setLocx(rs.getInt("x"));
  114. spawn1.setLocy(rs.getInt("y"));
  115. spawn1.setLocz(rs.getInt("z"));
  116. spawn1.setHeading(rs.getInt("heading"));
  117. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  118. spawn1.setLocation(0);
  119. _siegeGuardsSpawns.add(spawn1);
  120. }
  121. else
  122. {
  123. _log.warning("Missing npc data in npc table for id: " + rs.getInt("npcId"));
  124. }
  125. _siegeGuards.put(fortId, _siegeGuardsSpawns);
  126. }
  127. rs.close();
  128. statement.close();
  129. }
  130. catch (Exception e1)
  131. {
  132. _log.warning("Error loading siege guard for fort " + getFort().getName() + ":" + e1);
  133. e1.printStackTrace();
  134. }
  135. finally
  136. {
  137. try
  138. {
  139. con.close();
  140. }
  141. catch (Exception e)
  142. {
  143. _log.warning("" + e.getMessage());
  144. e.printStackTrace();
  145. }
  146. }
  147. }
  148. public final Fort getFort()
  149. {
  150. return _fort;
  151. }
  152. public final FastMap<Integer, FastList<L2Spawn>> getSiegeGuardSpawn()
  153. {
  154. return _siegeGuards;
  155. }
  156. }