SiegeGuardManager.java 7.7 KB

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