SiegeGuardManager.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.datatables.NpcTable;
  29. import com.l2jserver.gameserver.model.L2Spawn;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  32. import com.l2jserver.gameserver.model.entity.Castle;
  33. public final class SiegeGuardManager
  34. {
  35. private static Logger _log = Logger.getLogger(SiegeGuardManager.class.getName());
  36. private final Castle _castle;
  37. private final List<L2Spawn> _siegeGuardSpawn = new FastList<>();
  38. public SiegeGuardManager(Castle castle)
  39. {
  40. _castle = castle;
  41. }
  42. /**
  43. * Add guard.
  44. * @param activeChar
  45. * @param npcId
  46. */
  47. public void addSiegeGuard(L2PcInstance activeChar, int npcId)
  48. {
  49. if (activeChar == null)
  50. {
  51. return;
  52. }
  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. {
  76. return;
  77. }
  78. hireMerc(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId);
  79. }
  80. /**
  81. * Hire merc.
  82. * @param x
  83. * @param y
  84. * @param z
  85. * @param heading
  86. * @param npcId
  87. */
  88. public void hireMerc(int x, int y, int z, int heading, int npcId)
  89. {
  90. saveSiegeGuard(x, y, z, heading, npcId, 1);
  91. }
  92. /**
  93. * Remove a single mercenary, identified by the npcId and location. Presumably, this is used when a castle lord picks up a previously dropped ticket
  94. * @param npcId
  95. * @param x
  96. * @param y
  97. * @param z
  98. */
  99. public void removeMerc(int npcId, int x, int y, int z)
  100. {
  101. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  102. PreparedStatement ps = con.prepareStatement("Delete From castle_siege_guards Where npcId = ? And x = ? AND y = ? AND z = ? AND isHired = 1"))
  103. {
  104. ps.setInt(1, npcId);
  105. ps.setInt(2, x);
  106. ps.setInt(3, y);
  107. ps.setInt(4, z);
  108. ps.execute();
  109. }
  110. catch (Exception e)
  111. {
  112. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting hired siege guard at " + x + ',' + y + ',' + z + ": " + e.getMessage(), e);
  113. }
  114. }
  115. /**
  116. * Remove mercs.
  117. */
  118. public void removeMercs()
  119. {
  120. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  121. PreparedStatement ps = con.prepareStatement("Delete From castle_siege_guards Where castleId = ? And isHired = 1"))
  122. {
  123. ps.setInt(1, getCastle().getResidenceId());
  124. ps.execute();
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting hired siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  129. }
  130. }
  131. /**
  132. * Spawn guards.
  133. */
  134. public void spawnSiegeGuard()
  135. {
  136. try
  137. {
  138. int hiredCount = 0, hiredMax = MercTicketManager.getInstance().getMaxAllowedMerc(_castle.getResidenceId());
  139. boolean isHired = (getCastle().getOwnerId() > 0) ? true : false;
  140. loadSiegeGuard();
  141. for (L2Spawn spawn : getSiegeGuardSpawn())
  142. {
  143. if (spawn != null)
  144. {
  145. spawn.init();
  146. if (isHired)
  147. {
  148. spawn.stopRespawn();
  149. if (++hiredCount > hiredMax)
  150. {
  151. return;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error spawning siege guards for castle " + getCastle().getName(), e);
  160. }
  161. }
  162. /**
  163. * Unspawn guards.
  164. */
  165. public void unspawnSiegeGuard()
  166. {
  167. for (L2Spawn spawn : getSiegeGuardSpawn())
  168. {
  169. if ((spawn != null) && (spawn.getLastSpawn() != null))
  170. {
  171. spawn.stopRespawn();
  172. spawn.getLastSpawn().doDie(spawn.getLastSpawn());
  173. }
  174. }
  175. getSiegeGuardSpawn().clear();
  176. }
  177. /**
  178. * Load guards.
  179. */
  180. private void loadSiegeGuard()
  181. {
  182. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  183. PreparedStatement ps = con.prepareStatement("SELECT * FROM castle_siege_guards Where castleId = ? And isHired = ?"))
  184. {
  185. ps.setInt(1, getCastle().getResidenceId());
  186. if (getCastle().getOwnerId() > 0)
  187. {
  188. ps.setInt(2, 1);
  189. }
  190. else
  191. {
  192. ps.setInt(2, 0);
  193. }
  194. try (ResultSet rs = ps.executeQuery())
  195. {
  196. L2Spawn spawn1;
  197. L2NpcTemplate template1;
  198. while (rs.next())
  199. {
  200. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  201. if (template1 != null)
  202. {
  203. spawn1 = new L2Spawn(template1);
  204. spawn1.setAmount(1);
  205. spawn1.setX(rs.getInt("x"));
  206. spawn1.setY(rs.getInt("y"));
  207. spawn1.setZ(rs.getInt("z"));
  208. spawn1.setHeading(rs.getInt("heading"));
  209. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  210. spawn1.setLocationId(0);
  211. _siegeGuardSpawn.add(spawn1);
  212. }
  213. else
  214. {
  215. _log.warning(getClass().getSimpleName() + ": Missing npc data in npc table for id: " + rs.getInt("npcId"));
  216. }
  217. }
  218. }
  219. }
  220. catch (Exception e)
  221. {
  222. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error loading siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  223. }
  224. }
  225. /**
  226. * Save guards.
  227. * @param x
  228. * @param y
  229. * @param z
  230. * @param heading
  231. * @param npcId
  232. * @param isHire
  233. */
  234. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  235. {
  236. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  237. PreparedStatement statement = con.prepareStatement("Insert Into castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)"))
  238. {
  239. statement.setInt(1, getCastle().getResidenceId());
  240. statement.setInt(2, npcId);
  241. statement.setInt(3, x);
  242. statement.setInt(4, y);
  243. statement.setInt(5, z);
  244. statement.setInt(6, heading);
  245. statement.setInt(7, (isHire == 1 ? 0 : 600));
  246. statement.setInt(8, isHire);
  247. statement.execute();
  248. }
  249. catch (Exception e)
  250. {
  251. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error adding siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  252. }
  253. }
  254. public final Castle getCastle()
  255. {
  256. return _castle;
  257. }
  258. public final List<L2Spawn> getSiegeGuardSpawn()
  259. {
  260. return _siegeGuardSpawn;
  261. }
  262. }