SiegeGuardManager.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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().getCastleId());
  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.getCastleId());
  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)
  170. {
  171. continue;
  172. }
  173. spawn.stopRespawn();
  174. spawn.getLastSpawn().doDie(spawn.getLastSpawn());
  175. }
  176. getSiegeGuardSpawn().clear();
  177. }
  178. /**
  179. * Load guards.
  180. */
  181. private void loadSiegeGuard()
  182. {
  183. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  184. PreparedStatement ps = con.prepareStatement("SELECT * FROM castle_siege_guards Where castleId = ? And isHired = ?"))
  185. {
  186. ps.setInt(1, getCastle().getCastleId());
  187. if (getCastle().getOwnerId() > 0)
  188. {
  189. ps.setInt(2, 1);
  190. }
  191. else
  192. {
  193. ps.setInt(2, 0);
  194. }
  195. try (ResultSet rs = ps.executeQuery())
  196. {
  197. L2Spawn spawn1;
  198. L2NpcTemplate template1;
  199. while (rs.next())
  200. {
  201. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  202. if (template1 != null)
  203. {
  204. spawn1 = new L2Spawn(template1);
  205. spawn1.setAmount(1);
  206. spawn1.setX(rs.getInt("x"));
  207. spawn1.setY(rs.getInt("y"));
  208. spawn1.setZ(rs.getInt("z"));
  209. spawn1.setHeading(rs.getInt("heading"));
  210. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  211. spawn1.setLocationId(0);
  212. _siegeGuardSpawn.add(spawn1);
  213. }
  214. else
  215. {
  216. _log.warning(getClass().getSimpleName() + ": Missing npc data in npc table for id: " + rs.getInt("npcId"));
  217. }
  218. }
  219. }
  220. }
  221. catch (Exception e)
  222. {
  223. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error loading siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  224. }
  225. }
  226. /**
  227. * Save guards.
  228. * @param x
  229. * @param y
  230. * @param z
  231. * @param heading
  232. * @param npcId
  233. * @param isHire
  234. */
  235. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  236. {
  237. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  238. PreparedStatement statement = con.prepareStatement("Insert Into castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)"))
  239. {
  240. statement.setInt(1, getCastle().getCastleId());
  241. statement.setInt(2, npcId);
  242. statement.setInt(3, x);
  243. statement.setInt(4, y);
  244. statement.setInt(5, z);
  245. statement.setInt(6, heading);
  246. statement.setInt(7, (isHire == 1 ? 0 : 600));
  247. statement.setInt(8, isHire);
  248. statement.execute();
  249. }
  250. catch (Exception e)
  251. {
  252. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error adding siege guard for castle " + getCastle().getName() + ": " + e.getMessage(), e);
  253. }
  254. }
  255. public final Castle getCastle()
  256. {
  257. return _castle;
  258. }
  259. public final List<L2Spawn> getSiegeGuardSpawn()
  260. {
  261. return _siegeGuardSpawn;
  262. }
  263. }