2
0

SiegeGuardManager.java 7.7 KB

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