SiegeGuardManager.java 7.0 KB

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