SiegeGuardManager.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. PreparedStatement ps = con.prepareStatement("Delete From castle_siege_guards Where npcId = ? And x = ? AND y = ? AND z = ? AND isHired = 1"))
  99. {
  100. ps.setInt(1, npcId);
  101. ps.setInt(2, x);
  102. ps.setInt(3, y);
  103. ps.setInt(4, z);
  104. ps.execute();
  105. }
  106. catch (Exception e)
  107. {
  108. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting hired siege guard at " + x + ',' + y + ',' + z + ": " + e.getMessage(), e);
  109. }
  110. }
  111. /**
  112. * Remove mercs.
  113. */
  114. public void removeMercs()
  115. {
  116. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  117. PreparedStatement ps = con.prepareStatement("Delete From castle_siege_guards Where castleId = ? And isHired = 1"))
  118. {
  119. ps.setInt(1, getCastle().getCastleId());
  120. ps.execute();
  121. }
  122. catch (Exception e)
  123. {
  124. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting hired siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  125. }
  126. }
  127. /**
  128. * Spawn guards.
  129. */
  130. public void spawnSiegeGuard()
  131. {
  132. try
  133. {
  134. int hiredCount = 0, hiredMax = MercTicketManager.getInstance().getMaxAllowedMerc(_castle.getCastleId());
  135. boolean isHired = (getCastle().getOwnerId() > 0) ? true : false;
  136. loadSiegeGuard();
  137. for (L2Spawn spawn : getSiegeGuardSpawn())
  138. {
  139. if (spawn != null)
  140. {
  141. spawn.init();
  142. if (isHired)
  143. {
  144. spawn.stopRespawn();
  145. if (++hiredCount > hiredMax)
  146. {
  147. return;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error spawning siege guards for castle " + getCastle().getName(), e);
  156. }
  157. }
  158. /**
  159. * Unspawn guards.
  160. */
  161. public void unspawnSiegeGuard()
  162. {
  163. for (L2Spawn spawn : getSiegeGuardSpawn())
  164. {
  165. if (spawn == null)
  166. {
  167. continue;
  168. }
  169. spawn.stopRespawn();
  170. spawn.getLastSpawn().doDie(spawn.getLastSpawn());
  171. }
  172. getSiegeGuardSpawn().clear();
  173. }
  174. /**
  175. * Load guards.
  176. */
  177. private void loadSiegeGuard()
  178. {
  179. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  180. PreparedStatement ps = con.prepareStatement("SELECT * FROM castle_siege_guards Where castleId = ? And isHired = ?"))
  181. {
  182. ps.setInt(1, getCastle().getCastleId());
  183. if (getCastle().getOwnerId() > 0)
  184. {
  185. ps.setInt(2, 1);
  186. }
  187. else
  188. {
  189. ps.setInt(2, 0);
  190. }
  191. try (ResultSet rs = ps.executeQuery())
  192. {
  193. L2Spawn spawn1;
  194. L2NpcTemplate template1;
  195. while (rs.next())
  196. {
  197. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  198. if (template1 != null)
  199. {
  200. spawn1 = new L2Spawn(template1);
  201. spawn1.setAmount(1);
  202. spawn1.setLocx(rs.getInt("x"));
  203. spawn1.setLocy(rs.getInt("y"));
  204. spawn1.setLocz(rs.getInt("z"));
  205. spawn1.setHeading(rs.getInt("heading"));
  206. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  207. spawn1.setLocation(0);
  208. _siegeGuardSpawn.add(spawn1);
  209. }
  210. else
  211. {
  212. _log.warning(getClass().getSimpleName() + ": Missing npc data in npc table for id: " + rs.getInt("npcId"));
  213. }
  214. }
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error loading siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  220. }
  221. }
  222. /**
  223. * Save guards.
  224. * @param x
  225. * @param y
  226. * @param z
  227. * @param heading
  228. * @param npcId
  229. * @param isHire
  230. */
  231. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  232. {
  233. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  234. PreparedStatement statement = con.prepareStatement("Insert Into castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)"))
  235. {
  236. statement.setInt(1, getCastle().getCastleId());
  237. statement.setInt(2, npcId);
  238. statement.setInt(3, x);
  239. statement.setInt(4, y);
  240. statement.setInt(5, z);
  241. statement.setInt(6, heading);
  242. statement.setInt(7, (isHire == 1 ? 0 : 600));
  243. statement.setInt(8, isHire);
  244. statement.execute();
  245. }
  246. catch (Exception e)
  247. {
  248. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error adding siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  249. }
  250. }
  251. public final Castle getCastle()
  252. {
  253. return _castle;
  254. }
  255. public final List<L2Spawn> getSiegeGuardSpawn()
  256. {
  257. return _siegeGuardSpawn;
  258. }
  259. }