SiegeGuardManager.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.datatables.NpcTable;
  24. import com.l2jserver.gameserver.model.L2Spawn;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.entity.Castle;
  27. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  28. import javolution.util.FastList;
  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.<BR><BR>
  46. */
  47. public void addSiegeGuard(L2PcInstance activeChar, int npcId)
  48. {
  49. if (activeChar == null)
  50. return;
  51. addSiegeGuard(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  52. }
  53. /**
  54. * Add guard.<BR><BR>
  55. */
  56. public void addSiegeGuard(int x, int y, int z, int heading, int npcId)
  57. {
  58. saveSiegeGuard(x, y, z, heading, npcId, 0);
  59. }
  60. /**
  61. * Hire merc.<BR><BR>
  62. */
  63. public void hireMerc(L2PcInstance activeChar, int npcId)
  64. {
  65. if (activeChar == null)
  66. return;
  67. hireMerc(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  68. }
  69. /**
  70. * Hire merc.<BR><BR>
  71. */
  72. public void hireMerc(int x, int y, int z, int heading, int npcId)
  73. {
  74. saveSiegeGuard(x, y, z, heading, npcId, 1);
  75. }
  76. /**
  77. * Remove a single mercenary, identified by the npcId and location.
  78. * Presumably, this is used when a castle lord picks up a previously dropped ticket
  79. */
  80. public void removeMerc(int npcId, int x, int y, int z)
  81. {
  82. Connection con = null;
  83. try
  84. {
  85. con = L2DatabaseFactory.getInstance().getConnection();
  86. PreparedStatement statement = con.prepareStatement("Delete From castle_siege_guards Where npcId = ? And x = ? AND y = ? AND z = ? AND isHired = 1");
  87. statement.setInt(1, npcId);
  88. statement.setInt(2, x);
  89. statement.setInt(3, y);
  90. statement.setInt(4, z);
  91. statement.execute();
  92. statement.close();
  93. }
  94. catch (Exception e)
  95. {
  96. _log.log(Level.WARNING, "Error deleting hired siege guard at " + x + ',' + y + ',' + z + ": " + e.getMessage(), e);
  97. }
  98. finally
  99. {
  100. try
  101. {
  102. con.close();
  103. }
  104. catch (Exception e)
  105. {
  106. }
  107. }
  108. }
  109. /**
  110. * Remove mercs.<BR><BR>
  111. */
  112. public void removeMercs()
  113. {
  114. Connection con = null;
  115. try
  116. {
  117. con = L2DatabaseFactory.getInstance().getConnection();
  118. PreparedStatement statement = con.prepareStatement("Delete From castle_siege_guards Where castleId = ? And isHired = 1");
  119. statement.setInt(1, getCastle().getCastleId());
  120. statement.execute();
  121. statement.close();
  122. }
  123. catch (Exception e)
  124. {
  125. _log.log(Level.WARNING, "Error deleting hired siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  126. }
  127. finally
  128. {
  129. try
  130. {
  131. con.close();
  132. }
  133. catch (Exception e)
  134. {
  135. }
  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. // Method - Private
  183. /**
  184. * Load guards.<BR><BR>
  185. */
  186. private void loadSiegeGuard()
  187. {
  188. Connection con = null;
  189. try
  190. {
  191. con = L2DatabaseFactory.getInstance().getConnection();
  192. PreparedStatement statement = con.prepareStatement("SELECT * FROM castle_siege_guards Where castleId = ? And isHired = ?");
  193. statement.setInt(1, getCastle().getCastleId());
  194. if (getCastle().getOwnerId() > 0) // If castle is owned by a clan, then don't spawn default guards
  195. statement.setInt(2, 1);
  196. else
  197. statement.setInt(2, 0);
  198. ResultSet rs = statement.executeQuery();
  199. L2Spawn spawn1;
  200. L2NpcTemplate template1;
  201. while (rs.next())
  202. {
  203. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  204. if (template1 != null)
  205. {
  206. spawn1 = new L2Spawn(template1);
  207. spawn1.setId(rs.getInt("id"));
  208. spawn1.setAmount(1);
  209. spawn1.setLocx(rs.getInt("x"));
  210. spawn1.setLocy(rs.getInt("y"));
  211. spawn1.setLocz(rs.getInt("z"));
  212. spawn1.setHeading(rs.getInt("heading"));
  213. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  214. spawn1.setLocation(0);
  215. _siegeGuardSpawn.add(spawn1);
  216. }
  217. else
  218. {
  219. _log.warning("Missing npc data in npc table for id: " + rs.getInt("npcId"));
  220. }
  221. }
  222. statement.close();
  223. }
  224. catch (Exception e)
  225. {
  226. _log.log(Level.WARNING, "Error loading siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  227. }
  228. finally
  229. {
  230. try
  231. {
  232. con.close();
  233. }
  234. catch (Exception e)
  235. {
  236. }
  237. }
  238. }
  239. /**
  240. * Save guards.<BR><BR>
  241. */
  242. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  243. {
  244. Connection con = null;
  245. try
  246. {
  247. con = L2DatabaseFactory.getInstance().getConnection();
  248. PreparedStatement statement = con.prepareStatement("Insert Into castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)");
  249. statement.setInt(1, getCastle().getCastleId());
  250. statement.setInt(2, npcId);
  251. statement.setInt(3, x);
  252. statement.setInt(4, y);
  253. statement.setInt(5, z);
  254. statement.setInt(6, heading);
  255. if (isHire == 1)
  256. statement.setInt(7, 0);
  257. else
  258. statement.setInt(7, 600);
  259. statement.setInt(8, isHire);
  260. statement.execute();
  261. statement.close();
  262. }
  263. catch (Exception e)
  264. {
  265. _log.log(Level.WARNING, "Error adding siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  266. }
  267. finally
  268. {
  269. try
  270. {
  271. con.close();
  272. }
  273. catch (Exception e)
  274. {
  275. }
  276. }
  277. }
  278. // =========================================================
  279. // Proeprty
  280. public final Castle getCastle()
  281. {
  282. return _castle;
  283. }
  284. public final List<L2Spawn> getSiegeGuardSpawn()
  285. {
  286. return _siegeGuardSpawn;
  287. }
  288. }