SiegeGuardManager.java 9.2 KB

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