SiegeGuardManager.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.datatables.NpcTable;
  25. import com.l2jserver.gameserver.model.L2Spawn;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  28. import com.l2jserver.gameserver.model.entity.Castle;
  29. public class SiegeGuardManager
  30. {
  31. private static Logger _log = Logger.getLogger(SiegeGuardManager.class.getName());
  32. private Castle _castle;
  33. private List<L2Spawn> _siegeGuardSpawn = new FastList<>();
  34. public SiegeGuardManager(Castle castle)
  35. {
  36. _castle = castle;
  37. }
  38. /**
  39. * Add guard.
  40. * @param activeChar
  41. * @param npcId
  42. */
  43. public void addSiegeGuard(L2PcInstance activeChar, int npcId)
  44. {
  45. if (activeChar == null)
  46. return;
  47. addSiegeGuard(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  48. }
  49. /**
  50. * Add guard.
  51. * @param x
  52. * @param y
  53. * @param z
  54. * @param heading
  55. * @param npcId
  56. */
  57. public void addSiegeGuard(int x, int y, int z, int heading, int npcId)
  58. {
  59. saveSiegeGuard(x, y, z, heading, npcId, 0);
  60. }
  61. /**
  62. * Hire merc.
  63. * @param activeChar
  64. * @param npcId
  65. */
  66. public void hireMerc(L2PcInstance activeChar, int npcId)
  67. {
  68. if (activeChar == null)
  69. return;
  70. hireMerc(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  71. }
  72. /**
  73. * Hire merc.
  74. * @param x
  75. * @param y
  76. * @param z
  77. * @param heading
  78. * @param npcId
  79. */
  80. public void hireMerc(int x, int y, int z, int heading, int npcId)
  81. {
  82. saveSiegeGuard(x, y, z, heading, npcId, 1);
  83. }
  84. /**
  85. * Remove a single mercenary, identified by the npcId and location.
  86. * Presumably, this is used when a castle lord picks up a previously dropped ticket
  87. * @param npcId
  88. * @param x
  89. * @param y
  90. * @param z
  91. */
  92. public void removeMerc(int npcId, int x, int y, int z)
  93. {
  94. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  95. {
  96. PreparedStatement statement = con.prepareStatement("Delete From castle_siege_guards Where npcId = ? And x = ? AND y = ? AND z = ? AND isHired = 1");
  97. statement.setInt(1, npcId);
  98. statement.setInt(2, x);
  99. statement.setInt(3, y);
  100. statement.setInt(4, z);
  101. statement.execute();
  102. statement.close();
  103. }
  104. catch (Exception e)
  105. {
  106. _log.log(Level.WARNING, "Error deleting hired siege guard at " + x + ',' + y + ',' + z + ": " + e.getMessage(), e);
  107. }
  108. }
  109. /**
  110. * Remove mercs.<BR><BR>
  111. */
  112. public void removeMercs()
  113. {
  114. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  115. {
  116. PreparedStatement statement = con.prepareStatement("Delete From castle_siege_guards Where castleId = ? And isHired = 1");
  117. statement.setInt(1, getCastle().getCastleId());
  118. statement.execute();
  119. statement.close();
  120. }
  121. catch (Exception e)
  122. {
  123. _log.log(Level.WARNING, "Error deleting hired siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  124. }
  125. }
  126. /**
  127. * Spawn guards.<BR><BR>
  128. */
  129. public void spawnSiegeGuard()
  130. {
  131. try
  132. {
  133. int hiredCount = 0, hiredMax = MercTicketManager.getInstance().getMaxAllowedMerc(_castle.getCastleId());
  134. boolean isHired = (getCastle().getOwnerId() > 0) ? true : false;
  135. loadSiegeGuard();
  136. for (L2Spawn spawn : getSiegeGuardSpawn())
  137. {
  138. if (spawn != null)
  139. {
  140. spawn.init();
  141. if (isHired)
  142. {
  143. spawn.stopRespawn();
  144. if (++hiredCount > hiredMax)
  145. return;
  146. }
  147. }
  148. }
  149. }
  150. catch (Exception e)
  151. {
  152. _log.log(Level.SEVERE, "Error spawning siege guards for castle " + getCastle().getName(), e);
  153. }
  154. }
  155. /**
  156. * Unspawn guards.<BR><BR>
  157. */
  158. public void unspawnSiegeGuard()
  159. {
  160. for (L2Spawn spawn : getSiegeGuardSpawn())
  161. {
  162. if (spawn == null)
  163. continue;
  164. spawn.stopRespawn();
  165. spawn.getLastSpawn().doDie(spawn.getLastSpawn());
  166. }
  167. getSiegeGuardSpawn().clear();
  168. }
  169. /**
  170. * Load guards.<BR><BR>
  171. */
  172. private void loadSiegeGuard()
  173. {
  174. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  175. {
  176. PreparedStatement statement = con.prepareStatement("SELECT * FROM castle_siege_guards Where castleId = ? And isHired = ?");
  177. statement.setInt(1, getCastle().getCastleId());
  178. if (getCastle().getOwnerId() > 0) // If castle is owned by a clan, then don't spawn default guards
  179. statement.setInt(2, 1);
  180. else
  181. statement.setInt(2, 0);
  182. ResultSet rs = statement.executeQuery();
  183. L2Spawn spawn1;
  184. L2NpcTemplate template1;
  185. while (rs.next())
  186. {
  187. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  188. if (template1 != null)
  189. {
  190. spawn1 = new L2Spawn(template1);
  191. spawn1.setAmount(1);
  192. spawn1.setLocx(rs.getInt("x"));
  193. spawn1.setLocy(rs.getInt("y"));
  194. spawn1.setLocz(rs.getInt("z"));
  195. spawn1.setHeading(rs.getInt("heading"));
  196. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  197. spawn1.setLocation(0);
  198. _siegeGuardSpawn.add(spawn1);
  199. }
  200. else
  201. {
  202. _log.warning("Missing npc data in npc table for id: " + rs.getInt("npcId"));
  203. }
  204. }
  205. rs.close();
  206. statement.close();
  207. }
  208. catch (Exception e)
  209. {
  210. _log.log(Level.WARNING, "Error loading siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  211. }
  212. }
  213. /**
  214. * Save guards.<BR><BR>
  215. * @param x
  216. * @param y
  217. * @param z
  218. * @param heading
  219. * @param npcId
  220. * @param isHire
  221. */
  222. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  223. {
  224. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  225. {
  226. PreparedStatement statement = con.prepareStatement("Insert Into castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)");
  227. statement.setInt(1, getCastle().getCastleId());
  228. statement.setInt(2, npcId);
  229. statement.setInt(3, x);
  230. statement.setInt(4, y);
  231. statement.setInt(5, z);
  232. statement.setInt(6, heading);
  233. statement.setInt(7, (isHire == 1 ? 0 : 600));
  234. statement.setInt(8, isHire);
  235. statement.execute();
  236. statement.close();
  237. }
  238. catch (Exception e)
  239. {
  240. _log.log(Level.WARNING, "Error adding siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  241. }
  242. }
  243. public final Castle getCastle()
  244. {
  245. return _castle;
  246. }
  247. public final List<L2Spawn> getSiegeGuardSpawn()
  248. {
  249. return _siegeGuardSpawn;
  250. }
  251. }