FortSiegeGuardManager.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.Fort;
  26. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  27. public class FortSiegeGuardManager {
  28. private static final Logger _log = Logger.getLogger(SiegeGuardManager.class.getName());
  29. // =========================================================
  30. // Data Field
  31. private Fort _fort;
  32. private List<L2Spawn> _siegeGuardSpawn = new FastList<L2Spawn>();
  33. // =========================================================
  34. // Constructor
  35. public FortSiegeGuardManager(Fort fort)
  36. {
  37. _fort = fort;
  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 fort 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 fort_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 fort_siege_guards Where fortId = ? And isHired = 1");
  108. statement.setInt(1, getFort().getFortId());
  109. statement.execute();
  110. statement.close();
  111. }
  112. catch (Exception e1)
  113. {
  114. _log.warning("Error deleting hired siege guard for fort " + getFort().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(_fort.getFortId());
  130. boolean isHired = (getFort().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 fort " + getFort().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 fort_siege_guards Where fortId = ? ");
  177. statement.setInt(1, getFort().getFortId());
  178. ResultSet rs = statement.executeQuery();
  179. L2Spawn spawn1;
  180. L2NpcTemplate template1;
  181. while (rs.next())
  182. {
  183. template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId"));
  184. if (template1 != null)
  185. {
  186. spawn1 = new L2Spawn(template1);
  187. spawn1.setId(rs.getInt("id"));
  188. spawn1.setAmount(1);
  189. spawn1.setLocx(rs.getInt("x"));
  190. spawn1.setLocy(rs.getInt("y"));
  191. spawn1.setLocz(rs.getInt("z"));
  192. spawn1.setHeading(rs.getInt("heading"));
  193. spawn1.setRespawnDelay(rs.getInt("respawnDelay"));
  194. spawn1.setLocation(0);
  195. _siegeGuardSpawn.add(spawn1);
  196. }
  197. else
  198. {
  199. _log.warning("Missing npc data in npc table for id: " + rs.getInt("npcId"));
  200. }
  201. }
  202. rs.close();
  203. statement.close();
  204. }
  205. catch (Exception e1)
  206. {
  207. _log.warning("Error loading siege guard for fort " + getFort().getName() + ":" + e1);
  208. }
  209. finally
  210. {
  211. try { con.close(); } catch (Exception e) {}
  212. }
  213. }
  214. /**
  215. * Save guards.<BR><BR>
  216. */
  217. private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire)
  218. {
  219. java.sql.Connection con = null;
  220. try
  221. {
  222. con = L2DatabaseFactory.getInstance().getConnection();
  223. PreparedStatement statement = con.prepareStatement("Insert Into fort_siege_guards (fortId, npcId, x, y, z, heading, respawnDelay, isHired) Values (?, ?, ?, ?, ?, ?, ?, ?)");
  224. statement.setInt(1, getFort().getFortId());
  225. statement.setInt(2, npcId);
  226. statement.setInt(3, x);
  227. statement.setInt(4, y);
  228. statement.setInt(5, z);
  229. statement.setInt(6, heading);
  230. if (isHire == 1)
  231. statement.setInt(7, 0);
  232. else
  233. statement.setInt(7, 600);
  234. statement.setInt(8, isHire);
  235. statement.execute();
  236. statement.close();
  237. }
  238. catch (Exception e1)
  239. {
  240. _log.warning("Error adding siege guard for fort " + getFort().getName() + ":" + e1);
  241. }
  242. finally
  243. {
  244. try { con.close(); } catch (Exception e) {}
  245. }
  246. }
  247. // =========================================================
  248. // Proeprty
  249. public final Fort getFort()
  250. {
  251. return _fort;
  252. }
  253. public final List<L2Spawn> getSiegeGuardSpawn()
  254. {
  255. return _siegeGuardSpawn;
  256. }
  257. }