SiegeGuardManager.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 final Castle _castle;
  33. private final 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. {
  47. return;
  48. }
  49. addSiegeGuard(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  50. }
  51. /**
  52. * Add guard.
  53. * @param x
  54. * @param y
  55. * @param z
  56. * @param heading
  57. * @param npcId
  58. */
  59. public void addSiegeGuard(int x, int y, int z, int heading, int npcId)
  60. {
  61. saveSiegeGuard(x, y, z, heading, npcId, 0);
  62. }
  63. /**
  64. * Hire merc.
  65. * @param activeChar
  66. * @param npcId
  67. */
  68. public void hireMerc(L2PcInstance activeChar, int npcId)
  69. {
  70. if (activeChar == null)
  71. {
  72. return;
  73. }
  74. hireMerc(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  75. }
  76. /**
  77. * Hire merc.
  78. * @param x
  79. * @param y
  80. * @param z
  81. * @param heading
  82. * @param npcId
  83. */
  84. public void hireMerc(int x, int y, int z, int heading, int npcId)
  85. {
  86. saveSiegeGuard(x, y, z, heading, npcId, 1);
  87. }
  88. /**
  89. * Remove a single mercenary, identified by the npcId and location. Presumably, this is used when a castle lord picks up a previously dropped ticket
  90. * @param npcId
  91. * @param x
  92. * @param y
  93. * @param z
  94. */
  95. public void removeMerc(int npcId, int x, int y, int z)
  96. {
  97. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  98. {
  99. PreparedStatement statement = con.prepareStatement("Delete From castle_siege_guards Where npcId = ? And x = ? AND y = ? AND z = ? AND isHired = 1");
  100. statement.setInt(1, npcId);
  101. statement.setInt(2, x);
  102. statement.setInt(3, y);
  103. statement.setInt(4, z);
  104. statement.execute();
  105. statement.close();
  106. }
  107. catch (Exception e)
  108. {
  109. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting hired siege guard at " + x + ',' + y + ',' + z + ": " + e.getMessage(), e);
  110. }
  111. }
  112. /**
  113. * Remove mercs.
  114. */
  115. public void removeMercs()
  116. {
  117. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  118. {
  119. PreparedStatement statement = con.prepareStatement("Delete From castle_siege_guards Where castleId = ? And isHired = 1");
  120. statement.setInt(1, getCastle().getCastleId());
  121. statement.execute();
  122. statement.close();
  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.getCastleId());
  137. boolean isHired = (getCastle().getOwnerId() > 0) ? true : false;
  138. loadSiegeGuard();
  139. for (L2Spawn spawn : getSiegeGuardSpawn())
  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 : getSiegeGuardSpawn())
  166. {
  167. if (spawn == null)
  168. {
  169. continue;
  170. }
  171. spawn.stopRespawn();
  172. spawn.getLastSpawn().doDie(spawn.getLastSpawn());
  173. }
  174. getSiegeGuardSpawn().clear();
  175. }
  176. /**
  177. * Load guards.
  178. */
  179. private void loadSiegeGuard()
  180. {
  181. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  182. {
  183. PreparedStatement statement = con.prepareStatement("SELECT * FROM castle_siege_guards Where castleId = ? And isHired = ?");
  184. statement.setInt(1, getCastle().getCastleId());
  185. if (getCastle().getOwnerId() > 0)
  186. {
  187. statement.setInt(2, 1);
  188. }
  189. else
  190. {
  191. statement.setInt(2, 0);
  192. }
  193. ResultSet rs = statement.executeQuery();
  194. L2Spawn spawn1;
  195. L2NpcTemplate template1;
  196. while (rs.next())
  197. {
  198. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  199. if (template1 != null)
  200. {
  201. spawn1 = new L2Spawn(template1);
  202. spawn1.setAmount(1);
  203. spawn1.setLocx(rs.getInt("x"));
  204. spawn1.setLocy(rs.getInt("y"));
  205. spawn1.setLocz(rs.getInt("z"));
  206. spawn1.setHeading(rs.getInt("heading"));
  207. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  208. spawn1.setLocation(0);
  209. _siegeGuardSpawn.add(spawn1);
  210. }
  211. else
  212. {
  213. _log.warning(getClass().getSimpleName() + ": Missing npc data in npc table for id: " + rs.getInt("npcId"));
  214. }
  215. }
  216. rs.close();
  217. statement.close();
  218. }
  219. catch (Exception e)
  220. {
  221. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error loading siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  222. }
  223. }
  224. /**
  225. * Save guards.
  226. * @param x
  227. * @param y
  228. * @param z
  229. * @param heading
  230. * @param npcId
  231. * @param isHire
  232. */
  233. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  234. {
  235. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  236. {
  237. PreparedStatement statement = con.prepareStatement("Insert Into castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)");
  238. statement.setInt(1, getCastle().getCastleId());
  239. statement.setInt(2, npcId);
  240. statement.setInt(3, x);
  241. statement.setInt(4, y);
  242. statement.setInt(5, z);
  243. statement.setInt(6, heading);
  244. statement.setInt(7, (isHire == 1 ? 0 : 600));
  245. statement.setInt(8, isHire);
  246. statement.execute();
  247. statement.close();
  248. }
  249. catch (Exception e)
  250. {
  251. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error adding siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  252. }
  253. }
  254. public final Castle getCastle()
  255. {
  256. return _castle;
  257. }
  258. public final List<L2Spawn> getSiegeGuardSpawn()
  259. {
  260. return _siegeGuardSpawn;
  261. }
  262. }