SiegeGuardManager.java 8.1 KB

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