FortManager.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import javolution.util.FastMap;
  23. import net.sf.l2j.L2DatabaseFactory;
  24. import net.sf.l2j.gameserver.ThreadPoolManager;
  25. import net.sf.l2j.gameserver.datatables.NpcTable;
  26. import net.sf.l2j.gameserver.datatables.SpawnTable;
  27. import net.sf.l2j.gameserver.model.L2Clan;
  28. import net.sf.l2j.gameserver.model.L2Object;
  29. import net.sf.l2j.gameserver.model.L2Spawn;
  30. import net.sf.l2j.gameserver.model.entity.Fort;
  31. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  32. public class FortManager
  33. {
  34. protected static final Logger _log = Logger.getLogger(FortManager.class.getName());
  35. // =========================================================
  36. private static FortManager _instance;
  37. protected FastMap<Integer, Integer> _envoyCastles = new FastMap<Integer, Integer>();
  38. protected FastMap<Integer, FastList<L2Spawn>> _npcCommanders = new FastMap<Integer, FastList<L2Spawn>>();
  39. protected FastMap<Integer, FastList<L2Spawn>> _siegeNpcs = new FastMap<Integer, FastList<L2Spawn>>();
  40. protected FastMap<Integer, FastList<L2Spawn>> _specialEnvoys = new FastMap<Integer, FastList<L2Spawn>>();
  41. protected FastList<L2Spawn> _npcCommandersSpawns;
  42. protected FastList<L2Spawn> _siegeNpcsSpawns;
  43. protected FastList<L2Spawn> _specialEnvoysSpawns;
  44. protected int _respawnTime;
  45. public static final FortManager getInstance()
  46. {
  47. if (_instance == null)
  48. {
  49. _log.info("Initializing FortManager");
  50. _instance = new FortManager();
  51. _instance.load();
  52. }
  53. return _instance;
  54. }
  55. // =========================================================
  56. // Data Field
  57. private Fort _fort;
  58. private List<Fort> _forts;
  59. // =========================================================
  60. // Constructor
  61. public FortManager() {}
  62. public FortManager(Fort fort)
  63. {
  64. _fort = fort;
  65. initNpcs(); // load and spawn npcs
  66. initSiegeNpcs(); // load suspicious merchants
  67. spawnSuspiciousMerchant();// spawn suspicious merchants
  68. initNpcCommanders(); // npc Commanders (not monsters)
  69. spawnNpcCommanders(); // spawn npc Commanders
  70. initSpecialEnvoys(); // envoys from castles
  71. if (_fort.getOwnerClan() != null && _fort.getFortState() == 0)
  72. {
  73. spawnSpecialEnvoys();
  74. ThreadPoolManager.getInstance().scheduleGeneral(_fort.new ScheduleSpecialEnvoysDeSpawn(_fort), 1*60*60*1000); // Prepare 1hr task for special envoys despawn
  75. }
  76. }
  77. // =========================================================
  78. // Method - Public
  79. public final int findNearestFortIndex(L2Object obj)
  80. {
  81. int index = getFortIndex(obj);
  82. if (index < 0)
  83. {
  84. double closestDistance = 99999999;
  85. double distance;
  86. Fort fort;
  87. for (int i = 0; i < getForts().size(); i++)
  88. {
  89. fort = getForts().get(i);
  90. if (fort == null) continue;
  91. distance = fort.getDistance(obj);
  92. if (closestDistance > distance)
  93. {
  94. closestDistance = distance;
  95. index = i;
  96. }
  97. }
  98. }
  99. return index;
  100. }
  101. // =========================================================
  102. // Method - Private
  103. private final void load()
  104. {
  105. java.sql.Connection con = null;
  106. try
  107. {
  108. PreparedStatement statement;
  109. ResultSet rs;
  110. con = L2DatabaseFactory.getInstance().getConnection();
  111. statement = con.prepareStatement("Select id from fort order by id");
  112. rs = statement.executeQuery();
  113. while (rs.next())
  114. {
  115. getForts().add(new Fort(rs.getInt("id")));
  116. }
  117. rs.close();
  118. statement.close();
  119. _log.info("Loaded: " + getForts().size() + " fortress");
  120. for (Fort fort : getForts())
  121. {
  122. fort.getSiege().getSiegeGuardManager().loadSiegeGuard();
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. _log.warning("Exception: loadFortData(): " + e.getMessage());
  128. e.printStackTrace();
  129. }
  130. finally
  131. {
  132. try
  133. {
  134. con.close();
  135. }
  136. catch (Exception e)
  137. {
  138. _log.warning(""+e.getMessage());
  139. e.printStackTrace();
  140. }
  141. }
  142. }
  143. // =========================================================
  144. // Property - Public
  145. public final Fort getFortById(int fortId)
  146. {
  147. for (Fort f : getForts())
  148. {
  149. if (f.getFortId() == fortId)
  150. return f;
  151. }
  152. return null;
  153. }
  154. public final Fort getFortByOwner(L2Clan clan)
  155. {
  156. for (Fort f : getForts())
  157. {
  158. if (f.getOwnerClan() == clan)
  159. return f;
  160. }
  161. return null;
  162. }
  163. public final Fort getFort(String name)
  164. {
  165. for (Fort f : getForts())
  166. {
  167. if (f.getName().equalsIgnoreCase(name.trim()))
  168. return f;
  169. }
  170. return null;
  171. }
  172. public final Fort getFort(int x, int y, int z)
  173. {
  174. for (Fort f : getForts())
  175. {
  176. if (f.checkIfInZone(x, y, z))
  177. return f;
  178. }
  179. return null;
  180. }
  181. public final Fort getFort(L2Object activeObject) { return getFort(activeObject.getX(), activeObject.getY(), activeObject.getZ()); }
  182. public final int getFortIndex(int fortId)
  183. {
  184. Fort fort;
  185. for (int i = 0; i < getForts().size(); i++)
  186. {
  187. fort = getForts().get(i);
  188. if (fort != null && fort.getFortId() == fortId) return i;
  189. }
  190. return -1;
  191. }
  192. public final int getFortIndex(L2Object activeObject)
  193. {
  194. return getFortIndex(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  195. }
  196. public final int getFortIndex(int x, int y, int z)
  197. {
  198. Fort fort;
  199. for (int i = 0; i < getForts().size(); i++)
  200. {
  201. fort = getForts().get(i);
  202. if (fort != null && fort.checkIfInZone(x, y, z)) return i;
  203. }
  204. return -1;
  205. }
  206. public final List<Fort> getForts()
  207. {
  208. if (_forts == null) _forts = new FastList<Fort>();
  209. return _forts;
  210. }
  211. public final Fort getFort()
  212. {
  213. return _fort;
  214. }
  215. private void initNpcs()
  216. {
  217. Connection con = null;
  218. try
  219. {
  220. con = L2DatabaseFactory.getInstance().getConnection();
  221. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_spawnlist Where fortId = ? and spawnType = ? ");
  222. statement.setInt(1, getFort().getFortId());
  223. statement.setInt(2, 0);
  224. ResultSet rset = statement.executeQuery();
  225. L2Spawn spawnDat;
  226. L2NpcTemplate template1;
  227. while (rset.next())
  228. {
  229. template1 = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
  230. if (template1 != null)
  231. {
  232. spawnDat = new L2Spawn(template1);
  233. spawnDat.setAmount(1);
  234. spawnDat.setLocx(rset.getInt("x"));
  235. spawnDat.setLocy(rset.getInt("y"));
  236. spawnDat.setLocz(rset.getInt("z"));
  237. spawnDat.setHeading(rset.getInt("heading"));
  238. spawnDat.setRespawnDelay(60);
  239. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  240. spawnDat.doSpawn();
  241. spawnDat.startRespawn();
  242. }
  243. else
  244. {
  245. _log.warning("FortManager.initNpcs: Data missing in NPC table for ID: "
  246. + rset.getInt("npcId") + ".");
  247. }
  248. }
  249. rset.close();
  250. statement.close();
  251. }
  252. catch (Exception e)
  253. {
  254. // problem with initializing spawn, go to next one
  255. _log.warning("FortManager.initNpcs: Spawn could not be initialized: "+ e.getMessage());
  256. e.printStackTrace();
  257. }
  258. finally
  259. {
  260. try
  261. {
  262. con.close();
  263. }
  264. catch (Exception e)
  265. {
  266. _log.warning(""+e.getMessage());
  267. e.printStackTrace();
  268. }
  269. }
  270. }
  271. private void initNpcCommanders()
  272. {
  273. Connection con = null;
  274. _npcCommanders.clear();
  275. try
  276. {
  277. con = L2DatabaseFactory.getInstance().getConnection();
  278. PreparedStatement statement1 = con.prepareStatement("SELECT Distinct fortId FROM fort_spawnlist Where spawnType = ? ORDER BY fortId");
  279. statement1.setInt(1, 1);
  280. ResultSet rset1 = statement1.executeQuery();
  281. while (rset1.next())
  282. {
  283. int fortId = rset1.getInt("fortId");
  284. PreparedStatement statement2 = con.prepareStatement("SELECT id, npcId, x, y, z, heading FROM fort_spawnlist Where fortId = ? and spawnType = ? ORDER BY id");
  285. statement2.setInt(1, getFort().getFortId());
  286. statement2.setInt(2, 1);
  287. ResultSet rset2 = statement2.executeQuery();
  288. L2Spawn spawnDat;
  289. L2NpcTemplate template1;
  290. _npcCommandersSpawns = new FastList<L2Spawn>();
  291. while (rset2.next())
  292. {
  293. template1 = NpcTable.getInstance().getTemplate(rset2.getInt("npcId"));
  294. if (template1 != null)
  295. {
  296. spawnDat = new L2Spawn(template1);
  297. spawnDat.setAmount(1);
  298. spawnDat.setLocx(rset2.getInt("x"));
  299. spawnDat.setLocy(rset2.getInt("y"));
  300. spawnDat.setLocz(rset2.getInt("z"));
  301. spawnDat.setHeading(rset2.getInt("heading"));
  302. spawnDat.setRespawnDelay(60);
  303. _npcCommandersSpawns.add(spawnDat);
  304. }
  305. else
  306. {
  307. _log.warning("FortManager.initNpcCommanders: Data missing in NPC table for ID: "
  308. + rset2.getInt("npcId") + ".");
  309. }
  310. }
  311. rset2.close();
  312. statement2.close();
  313. _npcCommanders.put(fortId, _npcCommandersSpawns);
  314. }
  315. rset1.close();
  316. statement1.close();
  317. }
  318. catch (Exception e)
  319. {
  320. // problem with initializing spawn, go to next one
  321. _log.warning("FortManager.initNpcCommanders: Spawn could not be initialized: "
  322. + e.getMessage());
  323. e.printStackTrace();
  324. }
  325. finally
  326. {
  327. try
  328. {
  329. con.close();
  330. }
  331. catch (Exception e)
  332. {
  333. _log.warning(""+e.getMessage());
  334. e.printStackTrace();
  335. }
  336. }
  337. }
  338. private void initSiegeNpcs()
  339. {
  340. Connection con = null;
  341. _siegeNpcs.clear();
  342. try
  343. {
  344. con = L2DatabaseFactory.getInstance().getConnection();
  345. PreparedStatement statement1 = con.prepareStatement("SELECT Distinct fortId FROM fort_spawnlist Where spawnType = ? ORDER BY fortId");
  346. statement1.setInt(1, 2);
  347. ResultSet rset1 = statement1.executeQuery();
  348. while (rset1.next())
  349. {
  350. int fortId = rset1.getInt("fortId");
  351. PreparedStatement statement2 = con.prepareStatement("SELECT id, npcId, x, y, z, heading FROM fort_spawnlist Where fortId = ? and spawnType = ? ORDER BY id");
  352. statement2.setInt(1, getFort().getFortId());
  353. statement2.setInt(2, 2);
  354. ResultSet rset2 = statement2.executeQuery();
  355. L2Spawn spawnDat;
  356. L2NpcTemplate template1;
  357. _siegeNpcsSpawns = new FastList<L2Spawn>();
  358. while (rset2.next())
  359. {
  360. template1 = NpcTable.getInstance().getTemplate(rset2.getInt("npcId"));
  361. if (template1 != null)
  362. {
  363. spawnDat = new L2Spawn(template1);
  364. spawnDat.setAmount(1);
  365. spawnDat.setLocx(rset2.getInt("x"));
  366. spawnDat.setLocy(rset2.getInt("y"));
  367. spawnDat.setLocz(rset2.getInt("z"));
  368. spawnDat.setHeading(rset2.getInt("heading"));
  369. spawnDat.setRespawnDelay(60);
  370. _siegeNpcsSpawns.add(spawnDat);
  371. }
  372. else
  373. {
  374. _log.warning("FortManager.initSiegeNpcs: Data missing in NPC table for ID: "
  375. + rset2.getInt("npcId") + ".");
  376. }
  377. }
  378. rset2.close();
  379. statement2.close();
  380. _siegeNpcs.put(fortId, _siegeNpcsSpawns);
  381. }
  382. rset1.close();
  383. statement1.close();
  384. }
  385. catch (Exception e)
  386. {
  387. // problem with initializing spawn, go to next one
  388. _log.warning("FortManager.initSiegeNpcs: Spawn could not be initialized: "
  389. + e.getMessage());
  390. e.printStackTrace();
  391. }
  392. finally
  393. {
  394. try
  395. {
  396. con.close();
  397. }
  398. catch (Exception e)
  399. {
  400. _log.warning(""+e.getMessage());
  401. e.printStackTrace();
  402. }
  403. }
  404. }
  405. private void initSpecialEnvoys()
  406. {
  407. Connection con = null;
  408. _specialEnvoys.clear();
  409. _envoyCastles.clear();
  410. try
  411. {
  412. con = L2DatabaseFactory.getInstance().getConnection();
  413. PreparedStatement statement1 = con.prepareStatement("SELECT Distinct fortId FROM fort_spawnlist Where spawnType = ? ORDER BY fortId");
  414. statement1.setInt(1, 3);
  415. ResultSet rset1 = statement1.executeQuery();
  416. while (rset1.next())
  417. {
  418. int fortId = rset1.getInt("fortId");
  419. PreparedStatement statement2 = con.prepareStatement("SELECT id, npcId, x, y, z, heading, castleId FROM fort_spawnlist Where fortId = ? and spawnType = ? ORDER BY id");
  420. statement2.setInt(1, getFort().getFortId());
  421. statement2.setInt(2, 3);
  422. ResultSet rset2 = statement2.executeQuery();
  423. L2Spawn spawnDat;
  424. L2NpcTemplate template1;
  425. _specialEnvoysSpawns = new FastList<L2Spawn>();
  426. while (rset2.next())
  427. {
  428. int castleId = rset2.getInt("castleId");
  429. int npcId = rset2.getInt("npcId");
  430. template1 = NpcTable.getInstance().getTemplate(npcId);
  431. if (template1 != null)
  432. {
  433. spawnDat = new L2Spawn(template1);
  434. spawnDat.setAmount(1);
  435. spawnDat.setLocx(rset2.getInt("x"));
  436. spawnDat.setLocy(rset2.getInt("y"));
  437. spawnDat.setLocz(rset2.getInt("z"));
  438. spawnDat.setHeading(rset2.getInt("heading"));
  439. spawnDat.setRespawnDelay(60);
  440. _specialEnvoysSpawns.add(spawnDat);
  441. _envoyCastles.put(npcId, castleId);
  442. }
  443. else
  444. {
  445. _log.warning("FortManager.initSpecialEnvoys: Data missing in NPC table for ID: "
  446. + rset2.getInt("npcId") + ".");
  447. }
  448. }
  449. rset2.close();
  450. statement2.close();
  451. _specialEnvoys.put(fortId, _specialEnvoysSpawns);
  452. }
  453. rset1.close();
  454. statement1.close();
  455. }
  456. catch (Exception e)
  457. {
  458. // problem with initializing spawn, go to next one
  459. _log.warning("FortManager.initSpecialEnvoys: Spawn could not be initialized: "
  460. + e.getMessage());
  461. e.printStackTrace();
  462. }
  463. finally
  464. {
  465. try
  466. {
  467. con.close();
  468. }
  469. catch (Exception e)
  470. {
  471. _log.warning(""+e.getMessage());
  472. e.printStackTrace();
  473. }
  474. }
  475. }
  476. public void spawnNpcCommanders()
  477. {
  478. FastList<L2Spawn> monsterList = _npcCommanders.get(getFort().getFortId());
  479. if (monsterList != null)
  480. {
  481. for (L2Spawn spawnDat : monsterList)
  482. {
  483. spawnDat.doSpawn();
  484. spawnDat.startRespawn();
  485. }
  486. }
  487. }
  488. public void despawnNpcCommanders()
  489. {
  490. FastList<L2Spawn> monsterList = _npcCommanders.get(getFort().getFortId());
  491. if (monsterList != null)
  492. {
  493. for (L2Spawn spawnDat : monsterList)
  494. {
  495. spawnDat.stopRespawn();
  496. spawnDat.getLastSpawn().deleteMe();
  497. }
  498. }
  499. }
  500. public void spawnSuspiciousMerchant()
  501. {
  502. FastList<L2Spawn> monsterList = _siegeNpcs.get(getFort().getFortId());
  503. if (monsterList != null)
  504. {
  505. for (L2Spawn spawnDat : monsterList)
  506. {
  507. spawnDat.doSpawn();
  508. spawnDat.startRespawn();
  509. }
  510. }
  511. }
  512. public void despawnSuspiciousMerchant()
  513. {
  514. FastList<L2Spawn> monsterList = _siegeNpcs.get(getFort().getFortId());
  515. if (monsterList != null)
  516. {
  517. for (L2Spawn spawnDat : monsterList)
  518. {
  519. spawnDat.stopRespawn();
  520. spawnDat.getLastSpawn().deleteMe();
  521. }
  522. }
  523. }
  524. public void spawnSpecialEnvoys()
  525. {
  526. FastList<L2Spawn> monsterList = _specialEnvoys.get(getFort().getFortId());
  527. if (monsterList != null)
  528. {
  529. for (L2Spawn spawnDat : monsterList)
  530. {
  531. spawnDat.doSpawn();
  532. spawnDat.startRespawn();
  533. }
  534. }
  535. }
  536. public void despawnSpecialEnvoys()
  537. {
  538. FastList<L2Spawn> monsterList = _specialEnvoys.get(getFort().getFortId());
  539. if (monsterList != null)
  540. {
  541. for (L2Spawn spawnDat : monsterList)
  542. {
  543. spawnDat.stopRespawn();
  544. spawnDat.getLastSpawn().deleteMe();
  545. }
  546. }
  547. }
  548. public int getEnvoyCastle(int npcId)
  549. {
  550. return _envoyCastles.get(npcId);
  551. }
  552. }