Fort.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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.model.entity;
  16. import gnu.trove.TIntIntHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.Calendar;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.concurrent.ScheduledFuture;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.FortUpdater;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.datatables.ClanTable;
  31. import com.l2jserver.gameserver.datatables.DoorTable;
  32. import com.l2jserver.gameserver.datatables.NpcTable;
  33. import com.l2jserver.gameserver.datatables.ResidentialSkillTable;
  34. import com.l2jserver.gameserver.datatables.SpawnTable;
  35. import com.l2jserver.gameserver.datatables.StaticObjects;
  36. import com.l2jserver.gameserver.instancemanager.FortManager;
  37. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  38. import com.l2jserver.gameserver.model.L2Clan;
  39. import com.l2jserver.gameserver.model.L2Object;
  40. import com.l2jserver.gameserver.model.L2Skill;
  41. import com.l2jserver.gameserver.model.L2Spawn;
  42. import com.l2jserver.gameserver.model.L2World;
  43. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  44. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  46. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  47. import com.l2jserver.gameserver.model.zone.type.L2FortZone;
  48. import com.l2jserver.gameserver.network.SystemMessageId;
  49. import com.l2jserver.gameserver.network.serverpackets.PlaySound;
  50. import com.l2jserver.gameserver.network.serverpackets.PledgeShowInfoUpdate;
  51. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  52. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  53. import javolution.util.FastList;
  54. import javolution.util.FastMap;
  55. public class Fort
  56. {
  57. protected static final Logger _log = Logger.getLogger(Fort.class.getName());
  58. // =========================================================
  59. // Data Field
  60. private int _fortId = 0;
  61. private List<L2DoorInstance> _doors = new FastList<L2DoorInstance>();
  62. private L2StaticObjectInstance _flagPole = null;
  63. private List<String> _doorDefault = new FastList<String>();
  64. private List<String> _flagPoleStats = new FastList<String>();
  65. private String _name = "";
  66. private FortSiege _siege = null;
  67. private Calendar _siegeDate;
  68. private Calendar _lastOwnedTime;
  69. private L2FortZone _zone;
  70. private L2Clan _fortOwner = null;
  71. private int _fortType = 0;
  72. private int _state = 0;
  73. private int _castleId = 0;
  74. private int _blood = 0;
  75. private FastMap<Integer, FortFunction> _function;
  76. private FastList<L2Skill> _residentialSkills = new FastList<L2Skill>();
  77. private ScheduledFuture<?> _FortUpdater;
  78. // Spawn Data
  79. protected FastList<L2Spawn> _siegeNpcs = new FastList<L2Spawn>();
  80. protected FastList<L2Spawn> _npcCommanders = new FastList<L2Spawn>();
  81. protected FastList<L2Spawn> _specialEnvoys = new FastList<L2Spawn>();
  82. private TIntIntHashMap _envoyCastles = new TIntIntHashMap(2);
  83. /** Fortress Functions */
  84. public static final int FUNC_TELEPORT = 1;
  85. public static final int FUNC_RESTORE_HP = 2;
  86. public static final int FUNC_RESTORE_MP = 3;
  87. public static final int FUNC_RESTORE_EXP = 4;
  88. public static final int FUNC_SUPPORT = 5;
  89. public class FortFunction
  90. {
  91. private int _type;
  92. private int _lvl;
  93. protected int _fee;
  94. protected int _tempFee;
  95. private long _rate;
  96. private long _endDate;
  97. protected boolean _inDebt;
  98. public boolean _cwh;
  99. public FortFunction(int type, int lvl, int lease, int tempLease, long rate, long time, boolean cwh)
  100. {
  101. _type = type;
  102. _lvl = lvl;
  103. _fee = lease;
  104. _tempFee = tempLease;
  105. _rate = rate;
  106. _endDate = time;
  107. initializeTask(cwh);
  108. }
  109. public int getType()
  110. {
  111. return _type;
  112. }
  113. public int getLvl()
  114. {
  115. return _lvl;
  116. }
  117. public int getLease()
  118. {
  119. return _fee;
  120. }
  121. public long getRate()
  122. {
  123. return _rate;
  124. }
  125. public long getEndTime()
  126. {
  127. return _endDate;
  128. }
  129. public void setLvl(int lvl)
  130. {
  131. _lvl = lvl;
  132. }
  133. public void setLease(int lease)
  134. {
  135. _fee = lease;
  136. }
  137. public void setEndTime(long time)
  138. {
  139. _endDate = time;
  140. }
  141. private void initializeTask(boolean cwh)
  142. {
  143. if (getOwnerClan() == null)
  144. return;
  145. long currentTime = System.currentTimeMillis();
  146. if (_endDate > currentTime)
  147. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), _endDate - currentTime);
  148. else
  149. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), 0);
  150. }
  151. private class FunctionTask implements Runnable
  152. {
  153. public FunctionTask(boolean cwh)
  154. {
  155. _cwh = cwh;
  156. }
  157. public void run()
  158. {
  159. try
  160. {
  161. if (getOwnerClan() == null)
  162. return;
  163. if (getOwnerClan().getWarehouse().getAdena() >= _fee || !_cwh)
  164. {
  165. int fee = _fee;
  166. boolean newfc = true;
  167. if (getEndTime() == 0 || getEndTime() == -1)
  168. {
  169. if (getEndTime() == -1)
  170. {
  171. newfc = false;
  172. fee = _tempFee;
  173. }
  174. }
  175. else
  176. newfc = false;
  177. setEndTime(System.currentTimeMillis() + getRate());
  178. dbSave(newfc);
  179. if (_cwh)
  180. {
  181. getOwnerClan().getWarehouse().destroyItemByItemId("CS_function_fee", 57, fee, null, null);
  182. if (Config.DEBUG)
  183. _log.warning("deducted " + fee + " adena from " + getName() + " owner's cwh for function id : " + getType());
  184. }
  185. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(true), getRate());
  186. }
  187. else
  188. removeFunction(getType());
  189. }
  190. catch (Throwable t)
  191. {
  192. }
  193. }
  194. }
  195. public void dbSave(boolean newFunction)
  196. {
  197. Connection con = null;
  198. try
  199. {
  200. PreparedStatement statement;
  201. con = L2DatabaseFactory.getInstance().getConnection();
  202. if (newFunction)
  203. {
  204. statement = con.prepareStatement("INSERT INTO fort_functions (fort_id, type, lvl, lease, rate, endTime) VALUES (?,?,?,?,?,?)");
  205. statement.setInt(1, getFortId());
  206. statement.setInt(2, getType());
  207. statement.setInt(3, getLvl());
  208. statement.setInt(4, getLease());
  209. statement.setLong(5, getRate());
  210. statement.setLong(6, getEndTime());
  211. }
  212. else
  213. {
  214. statement = con.prepareStatement("UPDATE fort_functions SET lvl=?, lease=?, endTime=? WHERE fort_id=? AND type=?");
  215. statement.setInt(1, getLvl());
  216. statement.setInt(2, getLease());
  217. statement.setLong(3, getEndTime());
  218. statement.setInt(4, getFortId());
  219. statement.setInt(5, getType());
  220. }
  221. statement.execute();
  222. statement.close();
  223. }
  224. catch (Exception e)
  225. {
  226. _log.log(Level.SEVERE, "Exception: Fort.updateFunctions(int type, int lvl, int lease, long rate, long time, boolean addNew): "
  227. + e.getMessage(), e);
  228. }
  229. finally
  230. {
  231. try
  232. {
  233. con.close();
  234. }
  235. catch (Exception e)
  236. {
  237. e.printStackTrace();
  238. }
  239. }
  240. }
  241. }
  242. // =========================================================
  243. // Constructor
  244. public Fort(int fortId)
  245. {
  246. _fortId = fortId;
  247. load();
  248. loadDoor();
  249. loadFlagPoles();
  250. _function = new FastMap<Integer, FortFunction>();
  251. _residentialSkills = ResidentialSkillTable.getInstance().getSkills(fortId);
  252. if (getOwnerClan() != null)
  253. {
  254. setVisibleFlag(true);
  255. loadFunctions();
  256. }
  257. initNpcs(); // load and spawn npcs (Always spawned)
  258. initSiegeNpcs(); // load suspicious merchants (Despawned 10mins before siege)
  259. spawnNpcs(_siegeNpcs);// spawn suspicious merchants
  260. initNpcCommanders(); // npc Commanders (not monsters) (Spawned during siege)
  261. spawnNpcs(_npcCommanders); // spawn npc Commanders
  262. initSpecialEnvoys(); // envoys from castles (Spawned after fort taken)
  263. if (getOwnerClan() != null && getFortState() == 0)
  264. {
  265. spawnNpcs(_specialEnvoys);
  266. ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleSpecialEnvoysDeSpawn(this), 1 * 60 * 60 * 1000); // Prepare 1hr task for special envoys despawn
  267. }
  268. }
  269. /** Return function with id */
  270. public FortFunction getFunction(int type)
  271. {
  272. if (_function.get(type) != null)
  273. return _function.get(type);
  274. return null;
  275. }
  276. public class ScheduleSpecialEnvoysDeSpawn implements Runnable
  277. {
  278. private Fort _fortInst;
  279. public ScheduleSpecialEnvoysDeSpawn(Fort pFort)
  280. {
  281. _fortInst = pFort;
  282. }
  283. public void run()
  284. {
  285. try
  286. {
  287. // if state not decided, change state to indenpendent
  288. if (_fortInst.getFortState() == 0)
  289. _fortInst.setFortState(1, 0);
  290. _fortInst.despawnNpcs(_specialEnvoys);
  291. }
  292. catch (Exception e)
  293. {
  294. _log.warning("Exception: ScheduleSpecialEnvoysSpawn() for Fort: " + _fortInst.getName() + " " + e.getMessage());
  295. e.printStackTrace();
  296. }
  297. }
  298. }
  299. // =========================================================
  300. // Method - Public
  301. public void EndOfSiege(L2Clan clan)
  302. {
  303. ThreadPoolManager.getInstance().scheduleGeneral(new endFortressSiege(this, clan), 1000);
  304. }
  305. public void Engrave(L2Clan clan)
  306. {
  307. setOwner(clan, true);
  308. }
  309. /**
  310. * Move non clan members off fort area and to nearest town.<BR><BR>
  311. */
  312. public void banishForeigners()
  313. {
  314. getZone().banishForeigners(getOwnerClan());
  315. }
  316. /**
  317. * Return true if object is inside the zone
  318. */
  319. public boolean checkIfInZone(int x, int y, int z)
  320. {
  321. return getZone().isInsideZone(x, y, z);
  322. }
  323. public L2FortZone getZone()
  324. {
  325. if (_zone == null)
  326. {
  327. for (L2ZoneType zone : ZoneManager.getInstance().getAllZones())
  328. {
  329. if (zone instanceof L2FortZone && ((L2FortZone) zone).getFortId() == getFortId())
  330. {
  331. _zone = (L2FortZone) zone;
  332. break;
  333. }
  334. }
  335. }
  336. return _zone;
  337. }
  338. /**
  339. * Get the objects distance to this fort
  340. * @param object
  341. * @return
  342. */
  343. public double getDistance(L2Object obj)
  344. {
  345. return getZone().getDistanceToZone(obj);
  346. }
  347. public void closeDoor(L2PcInstance activeChar, int doorId)
  348. {
  349. openCloseDoor(activeChar, doorId, false);
  350. }
  351. public void openDoor(L2PcInstance activeChar, int doorId)
  352. {
  353. openCloseDoor(activeChar, doorId, true);
  354. }
  355. public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
  356. {
  357. if (activeChar.getClan() != getOwnerClan())
  358. return;
  359. L2DoorInstance door = getDoor(doorId);
  360. if (door != null)
  361. {
  362. if (open)
  363. door.openMe();
  364. else
  365. door.closeMe();
  366. }
  367. }
  368. // This method is used to begin removing all fort upgrades
  369. public void removeUpgrade()
  370. {
  371. removeDoorUpgrade();
  372. }
  373. /**
  374. * This method will set owner for Fort
  375. * @param clan
  376. * @param updateClanPoints
  377. */
  378. public boolean setOwner(L2Clan clan, boolean updateClansReputation)
  379. {
  380. if (updateClansReputation)
  381. {
  382. // update reputation first
  383. updateClansReputation(clan, false);
  384. }
  385. // Remove old owner
  386. if (getOwnerClan() != null && (clan != null && clan != getOwnerClan()))
  387. {
  388. updateClansReputation(getOwnerClan(), true);
  389. try
  390. {
  391. L2PcInstance oldleader = getOwnerClan().getLeader().getPlayerInstance();
  392. if (oldleader != null)
  393. {
  394. if (oldleader.getMountType() == 2)
  395. oldleader.dismount();
  396. }
  397. }
  398. catch (Exception e)
  399. {
  400. e.printStackTrace();
  401. }
  402. removeOwner(true);
  403. }
  404. setFortState(0, 0); // initialize fort state
  405. // if clan already have castle, don't store him in fortress
  406. if (clan.getHasCastle() > 0)
  407. {
  408. getSiege().announceToPlayer(new SystemMessage(SystemMessageId.NPCS_RECAPTURED_FORTRESS));
  409. return false;
  410. }
  411. else
  412. {
  413. spawnNpcs(_specialEnvoys);
  414. ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleSpecialEnvoysDeSpawn(this), 1 * 60 * 60 * 1000); // Prepare 1hr task for special envoys despawn
  415. // if clan have already fortress, remove it
  416. if (clan.getHasFort() > 0)
  417. FortManager.getInstance().getFortByOwner(clan).removeOwner(true);
  418. setBloodOathReward(0);
  419. setOwnerClan(clan);
  420. updateOwnerInDB(); // Update in database
  421. if (getSiege().getIsInProgress()) // If siege in progress
  422. getSiege().endSiege();
  423. for (L2PcInstance member : clan.getOnlineMembers(0))
  424. {
  425. giveResidentialSkills(member);
  426. member.sendSkillList();
  427. }
  428. return true;
  429. }
  430. }
  431. public void removeOwner(boolean updateDB)
  432. {
  433. L2Clan clan = getOwnerClan();
  434. if (clan != null)
  435. {
  436. for (L2PcInstance member : clan.getOnlineMembers(0))
  437. {
  438. removeResidentialSkills(member);
  439. member.sendSkillList();
  440. }
  441. clan.setHasFort(0);
  442. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  443. setOwnerClan(null);
  444. setBloodOathReward(0);
  445. if (updateDB)
  446. updateOwnerInDB();
  447. }
  448. }
  449. public void setBloodOathReward(int val)
  450. {
  451. _blood = val;
  452. Connection con = null;
  453. try
  454. {
  455. con = L2DatabaseFactory.getInstance().getConnection();
  456. PreparedStatement statement;
  457. statement = con.prepareStatement("UPDATE fort SET blood=? WHERE id = ?");
  458. statement.setInt(1, _blood);
  459. statement.setInt(2, getFortId());
  460. statement.execute();
  461. statement.close();
  462. }
  463. catch (Exception e)
  464. {
  465. _log.warning("Exception: setBloodOathReward(int val): " + e.getMessage());
  466. e.printStackTrace();
  467. }
  468. finally
  469. {
  470. try
  471. {
  472. con.close();
  473. }
  474. catch (Exception e)
  475. {
  476. _log.warning("" + e.getMessage());
  477. e.printStackTrace();
  478. }
  479. }
  480. }
  481. public int getBloodOathReward()
  482. {
  483. return _blood;
  484. }
  485. /**
  486. * Show or hide flag inside flagpole<BR><BR>
  487. */
  488. public void setVisibleFlag(boolean val)
  489. {
  490. L2StaticObjectInstance flagPole = getFlagPole();
  491. if (flagPole != null)
  492. flagPole.setMeshIndex(val ? 1 : 0);
  493. }
  494. /**
  495. * Respawn all doors on fort grounds<BR><BR>
  496. */
  497. public void resetDoors()
  498. {
  499. for (int i = 0; i < getDoors().size(); i++)
  500. {
  501. L2DoorInstance door = getDoors().get(i);
  502. if (door.getOpen())
  503. door.closeMe();
  504. if (door.getCurrentHp() <= 0)
  505. door.doRevive();
  506. if (door.getCurrentHp() < door.getMaxHp())
  507. door.setCurrentHp(door.getMaxHp());
  508. }
  509. loadDoorUpgrade(); // Check for any upgrade the doors may have
  510. }
  511. // This method upgrade door
  512. public void upgradeDoor(int doorId, int hp, int pDef, int mDef)
  513. {
  514. L2DoorInstance door = getDoor(doorId);
  515. if (door == null)
  516. return;
  517. if (door != null && door.getDoorId() == doorId)
  518. {
  519. door.setCurrentHp(door.getMaxHp() + hp);
  520. saveDoorUpgrade(doorId, hp, pDef, mDef);
  521. return;
  522. }
  523. }
  524. // =========================================================
  525. // Method - Private
  526. // This method loads fort
  527. private void load()
  528. {
  529. Connection con = null;
  530. try
  531. {
  532. PreparedStatement statement;
  533. ResultSet rs;
  534. con = L2DatabaseFactory.getInstance().getConnection();
  535. statement = con.prepareStatement("SELECT * FROM fort WHERE id = ?");
  536. statement.setInt(1, getFortId());
  537. rs = statement.executeQuery();
  538. int ownerId = 0;
  539. while (rs.next())
  540. {
  541. _name = rs.getString("name");
  542. _siegeDate = Calendar.getInstance();
  543. _lastOwnedTime = Calendar.getInstance();
  544. _siegeDate.setTimeInMillis(rs.getLong("siegeDate"));
  545. _lastOwnedTime.setTimeInMillis(rs.getLong("lastOwnedTime"));
  546. ownerId = rs.getInt("owner");
  547. _fortType = rs.getInt("fortType");
  548. _state = rs.getInt("state");
  549. _castleId = rs.getInt("castleId");
  550. _blood = rs.getInt("blood");
  551. }
  552. rs.close();
  553. statement.close();
  554. if (ownerId > 0)
  555. {
  556. L2Clan clan = ClanTable.getInstance().getClan(ownerId); // Try to find clan instance
  557. clan.setHasFort(getFortId());
  558. setOwnerClan(clan);
  559. int runCount = getOwnedTime() / (Config.FS_BLOOD_OATH_FRQ * 60);
  560. long initial = System.currentTimeMillis() - _lastOwnedTime.getTimeInMillis();
  561. while (initial > (Config.FS_BLOOD_OATH_FRQ * 60000))
  562. initial -= (Config.FS_BLOOD_OATH_FRQ * 60000);
  563. initial = (Config.FS_BLOOD_OATH_FRQ * 60000) - initial;
  564. _FortUpdater = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new FortUpdater(this, clan, runCount), initial, Config.FS_BLOOD_OATH_FRQ * 60000); // Schedule owner tasks to start running
  565. }
  566. else
  567. setOwnerClan(null);
  568. }
  569. catch (Exception e)
  570. {
  571. _log.warning("Exception: loadFortData(): " + e.getMessage());
  572. e.printStackTrace();
  573. }
  574. finally
  575. {
  576. try
  577. {
  578. con.close();
  579. }
  580. catch (Exception e)
  581. {
  582. _log.warning("" + e.getMessage());
  583. e.printStackTrace();
  584. }
  585. }
  586. }
  587. /** Load All Functions */
  588. private void loadFunctions()
  589. {
  590. Connection con = null;
  591. try
  592. {
  593. PreparedStatement statement;
  594. ResultSet rs;
  595. con = L2DatabaseFactory.getInstance().getConnection();
  596. statement = con.prepareStatement("SELECT * FROM fort_functions WHERE fort_id = ?");
  597. statement.setInt(1, getFortId());
  598. rs = statement.executeQuery();
  599. while (rs.next())
  600. {
  601. _function.put(rs.getInt("type"), new FortFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
  602. }
  603. statement.close();
  604. }
  605. catch (Exception e)
  606. {
  607. _log.log(Level.SEVERE, "Exception: Fort.loadFunctions(): " + e.getMessage(), e);
  608. }
  609. finally
  610. {
  611. try
  612. {
  613. con.close();
  614. }
  615. catch (Exception e)
  616. {
  617. }
  618. }
  619. }
  620. /** Remove function In List and in DB */
  621. public void removeFunction(int functionType)
  622. {
  623. _function.remove(functionType);
  624. Connection con = null;
  625. try
  626. {
  627. PreparedStatement statement;
  628. con = L2DatabaseFactory.getInstance().getConnection();
  629. statement = con.prepareStatement("DELETE FROM fort_functions WHERE fort_id=? AND type=?");
  630. statement.setInt(1, getFortId());
  631. statement.setInt(2, functionType);
  632. statement.execute();
  633. statement.close();
  634. }
  635. catch (Exception e)
  636. {
  637. _log.log(Level.SEVERE, "Exception: Fort.removeFunctions(int functionType): " + e.getMessage(), e);
  638. }
  639. finally
  640. {
  641. try
  642. {
  643. con.close();
  644. }
  645. catch (Exception e)
  646. {
  647. }
  648. }
  649. }
  650. public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
  651. {
  652. if (player == null)
  653. return false;
  654. if (Config.DEBUG)
  655. _log.warning("Called Fort.updateFunctions(int type, int lvl, int lease, long rate, boolean addNew) Owner : " + getOwnerClan());
  656. if (lease > 0)
  657. if (!player.destroyItemByItemId("Consume", 57, lease, null, true))
  658. return false;
  659. if (addNew)
  660. {
  661. _function.put(type, new FortFunction(type, lvl, lease, 0, rate, 0, false));
  662. }
  663. else
  664. {
  665. if (lvl == 0 && lease == 0)
  666. removeFunction(type);
  667. else
  668. {
  669. int diffLease = lease - _function.get(type).getLease();
  670. if (Config.DEBUG)
  671. _log.warning("Called Fort.updateFunctions diffLease : " + diffLease);
  672. if (diffLease > 0)
  673. {
  674. _function.remove(type);
  675. _function.put(type, new FortFunction(type, lvl, lease, 0, rate, -1, false));
  676. }
  677. else
  678. {
  679. _function.get(type).setLease(lease);
  680. _function.get(type).setLvl(lvl);
  681. _function.get(type).dbSave(false);
  682. }
  683. }
  684. }
  685. return true;
  686. }
  687. public void activateInstance()
  688. {
  689. for (final L2DoorInstance door : _doors)
  690. {
  691. door.spawnMe(door.getX(), door.getY(), door.getZ());
  692. }
  693. }
  694. // This method loads fort door data from database
  695. private void loadDoor()
  696. {
  697. Connection con = null;
  698. try
  699. {
  700. con = L2DatabaseFactory.getInstance().getConnection();
  701. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_staticobjects WHERE fortId = ? AND objectType = ?");
  702. statement.setInt(1, getFortId());
  703. statement.setInt(2, 0);
  704. ResultSet rs = statement.executeQuery();
  705. while (rs.next())
  706. {
  707. // Create list of the door default for use when respawning dead doors
  708. _doorDefault.add(rs.getString("name") + ";" + rs.getInt("id") + ";" + rs.getInt("x") + ";" + rs.getInt("y") + ";"
  709. + rs.getInt("z") + ";" + rs.getInt("range_xmin") + ";" + rs.getInt("range_ymin") + ";" + rs.getInt("range_zmin")
  710. + ";" + rs.getInt("range_xmax") + ";" + rs.getInt("range_ymax") + ";" + rs.getInt("range_zmax") + ";"
  711. + rs.getInt("hp") + ";" + rs.getInt("pDef") + ";" + rs.getInt("mDef") + ";" + rs.getBoolean("openType") + ";"
  712. + rs.getBoolean("commanderDoor"));
  713. L2DoorInstance door;
  714. _doors.add(door = DoorTable.parseList(_doorDefault.get(_doorDefault.size() - 1), true));
  715. DoorTable.getInstance().putDoor(door);
  716. }
  717. rs.close();
  718. statement.close();
  719. }
  720. catch (Exception e)
  721. {
  722. _log.warning("Exception: loadFortDoor(): " + e.getMessage());
  723. e.printStackTrace();
  724. }
  725. finally
  726. {
  727. try
  728. {
  729. con.close();
  730. }
  731. catch (Exception e)
  732. {
  733. _log.warning("" + e.getMessage());
  734. e.printStackTrace();
  735. }
  736. }
  737. }
  738. // This method loads fort flagpoles data from database
  739. private void loadFlagPoles()
  740. {
  741. Connection con = null;
  742. try
  743. {
  744. con = L2DatabaseFactory.getInstance().getConnection();
  745. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_staticobjects WHERE fortId = ? AND objectType = ?");
  746. statement.setInt(1, getFortId());
  747. statement.setInt(2, 1);
  748. ResultSet rs = statement.executeQuery();
  749. while (rs.next())
  750. {
  751. //Create list of the door default for use when respawning dead doors
  752. _flagPoleStats.add(rs.getString("name") + ";" + rs.getInt("id") + ";" + rs.getInt("x") + ";" + rs.getInt("y") + ";"
  753. + rs.getInt("z") + ";3;none;0;0");
  754. _flagPole = StaticObjects.parse(_flagPoleStats.get(_flagPoleStats.size() - 1));
  755. StaticObjects.getInstance().putObject(_flagPole);
  756. }
  757. rs.close();
  758. statement.close();
  759. }
  760. catch (Exception e)
  761. {
  762. _log.warning("Exception: loadFlagPoles(): " + e.getMessage());
  763. e.printStackTrace();
  764. }
  765. finally
  766. {
  767. try
  768. {
  769. con.close();
  770. }
  771. catch (Exception e)
  772. {
  773. _log.warning("" + e.getMessage());
  774. e.printStackTrace();
  775. }
  776. }
  777. }
  778. // This method loads fort door upgrade data from database
  779. private void loadDoorUpgrade()
  780. {
  781. Connection con = null;
  782. try
  783. {
  784. con = L2DatabaseFactory.getInstance().getConnection();
  785. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_doorupgrade WHERE doorId IN (SELECT Id FROM fort_staticobjects WHERE fortId = ? AND objectType = ?)");
  786. statement.setInt(1, getFortId());
  787. statement.setInt(2, 0);
  788. ResultSet rs = statement.executeQuery();
  789. while (rs.next())
  790. {
  791. upgradeDoor(rs.getInt("id"), rs.getInt("hp"), rs.getInt("pDef"), rs.getInt("mDef"));
  792. }
  793. rs.close();
  794. statement.close();
  795. }
  796. catch (Exception e)
  797. {
  798. _log.warning("Exception: loadFortDoorUpgrade(): " + e.getMessage());
  799. e.printStackTrace();
  800. }
  801. finally
  802. {
  803. try
  804. {
  805. con.close();
  806. }
  807. catch (Exception e)
  808. {
  809. _log.warning("" + e.getMessage());
  810. e.printStackTrace();
  811. }
  812. }
  813. }
  814. private void removeDoorUpgrade()
  815. {
  816. Connection con = null;
  817. try
  818. {
  819. con = L2DatabaseFactory.getInstance().getConnection();
  820. PreparedStatement statement = con.prepareStatement("DELETE FROM fort_doorupgrade WHERE doorId IN (SELECT id FROM fort_staticobjects WHERE fortId=? AND objectType = ?)");
  821. statement.setInt(1, getFortId());
  822. statement.setInt(2, 0);
  823. statement.execute();
  824. statement.close();
  825. }
  826. catch (Exception e)
  827. {
  828. _log.warning("Exception: removeDoorUpgrade(): " + e.getMessage());
  829. e.printStackTrace();
  830. }
  831. finally
  832. {
  833. try
  834. {
  835. con.close();
  836. }
  837. catch (Exception e)
  838. {
  839. _log.warning("" + e.getMessage());
  840. e.printStackTrace();
  841. }
  842. }
  843. }
  844. private void saveDoorUpgrade(int doorId, int hp, int pDef, int mDef)
  845. {
  846. Connection con = null;
  847. try
  848. {
  849. con = L2DatabaseFactory.getInstance().getConnection();
  850. PreparedStatement statement = con.prepareStatement("INSERT INTO fort_doorupgrade (doorId, hp, pDef, mDef) VALUES (?,?,?,?)");
  851. statement.setInt(1, doorId);
  852. statement.setInt(2, hp);
  853. statement.setInt(3, pDef);
  854. statement.setInt(4, mDef);
  855. statement.execute();
  856. statement.close();
  857. }
  858. catch (Exception e)
  859. {
  860. _log.warning("Exception: saveDoorUpgrade(int doorId, int hp, int pDef, int mDef): " + e.getMessage());
  861. e.printStackTrace();
  862. }
  863. finally
  864. {
  865. try
  866. {
  867. con.close();
  868. }
  869. catch (Exception e)
  870. {
  871. _log.warning("" + e.getMessage());
  872. e.printStackTrace();
  873. }
  874. }
  875. }
  876. private void updateOwnerInDB()
  877. {
  878. L2Clan clan = getOwnerClan();
  879. int clanId = 0;
  880. if (clan != null)
  881. {
  882. clanId = clan.getClanId();
  883. _lastOwnedTime.setTimeInMillis(System.currentTimeMillis());
  884. }
  885. else
  886. _lastOwnedTime.setTimeInMillis(0);
  887. Connection con = null;
  888. try
  889. {
  890. con = L2DatabaseFactory.getInstance().getConnection();
  891. PreparedStatement statement;
  892. statement = con.prepareStatement("UPDATE fort SET owner=?,lastOwnedTime=?,state=?,castleId=?,blood=? WHERE id = ?");
  893. statement.setInt(1, clanId);
  894. statement.setLong(2, _lastOwnedTime.getTimeInMillis());
  895. statement.setInt(3, 0);
  896. statement.setInt(4, 0);
  897. statement.setInt(5, getBloodOathReward());
  898. statement.setInt(6, getFortId());
  899. statement.execute();
  900. statement.close();
  901. // ============================================================================
  902. // Announce to clan memebers
  903. if (clan != null)
  904. {
  905. clan.setHasFort(getFortId()); // Set has fort flag for new owner
  906. SystemMessage sm;
  907. sm = new SystemMessage(SystemMessageId.S1_CLAN_IS_VICTORIOUS_IN_THE_FORTRESS_BATTLE_OF_S2);
  908. sm.addString(clan.getName());
  909. sm.addFortId(getFortId());
  910. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  911. for (L2PcInstance player : pls)
  912. {
  913. player.sendPacket(sm);
  914. }
  915. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  916. clan.broadcastToOnlineMembers(new PlaySound(1, "Siege_Victory", 0, 0, 0, 0, 0));
  917. if (_FortUpdater != null)
  918. _FortUpdater.cancel(false);
  919. _FortUpdater = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new FortUpdater(this, clan, 0), Config.FS_BLOOD_OATH_FRQ * 60000, Config.FS_BLOOD_OATH_FRQ * 60000); // Schedule owner tasks to start running
  920. }
  921. else
  922. {
  923. _FortUpdater.cancel(false);
  924. _FortUpdater = null;
  925. }
  926. }
  927. catch (Exception e)
  928. {
  929. _log.warning("Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage());
  930. e.printStackTrace();
  931. }
  932. finally
  933. {
  934. try
  935. {
  936. con.close();
  937. }
  938. catch (Exception e)
  939. {
  940. _log.warning("" + e.getMessage());
  941. e.printStackTrace();
  942. }
  943. }
  944. }
  945. public final int getFortId()
  946. {
  947. return _fortId;
  948. }
  949. public final L2Clan getOwnerClan()
  950. {
  951. return _fortOwner;
  952. }
  953. public final void setOwnerClan(L2Clan clan)
  954. {
  955. setVisibleFlag(clan != null ? true : false);
  956. _fortOwner = clan;
  957. }
  958. public final L2DoorInstance getDoor(int doorId)
  959. {
  960. if (doorId <= 0)
  961. return null;
  962. for (L2DoorInstance door : getDoors())
  963. {
  964. if (door.getDoorId() == doorId)
  965. return door;
  966. }
  967. return null;
  968. }
  969. public final List<L2DoorInstance> getDoors()
  970. {
  971. return _doors;
  972. }
  973. public final L2StaticObjectInstance getFlagPole()
  974. {
  975. return _flagPole;
  976. }
  977. public final FortSiege getSiege()
  978. {
  979. if (_siege == null)
  980. _siege = new FortSiege(new Fort[] { this });
  981. return _siege;
  982. }
  983. public final Calendar getSiegeDate()
  984. {
  985. return _siegeDate;
  986. }
  987. public final void setSiegeDate(Calendar siegeDate)
  988. {
  989. _siegeDate = siegeDate;
  990. }
  991. public final int getOwnedTime()
  992. {
  993. if (_lastOwnedTime.getTimeInMillis() == 0)
  994. return 0;
  995. return (int) ((System.currentTimeMillis() - _lastOwnedTime.getTimeInMillis()) / 1000);
  996. }
  997. public final String getName()
  998. {
  999. return _name;
  1000. }
  1001. public void updateClansReputation(L2Clan owner, boolean removePoints)
  1002. {
  1003. if (owner != null)
  1004. {
  1005. if (removePoints)
  1006. owner.takeReputationScore(Config.LOOSE_FORT_POINTS, true);
  1007. else
  1008. owner.addReputationScore(Config.TAKE_FORT_POINTS, true);
  1009. }
  1010. }
  1011. private class endFortressSiege implements Runnable
  1012. {
  1013. private Fort _f;
  1014. private L2Clan _clan;
  1015. public endFortressSiege(Fort f, L2Clan clan)
  1016. {
  1017. _f = f;
  1018. _clan = clan;
  1019. }
  1020. public void run()
  1021. {
  1022. try
  1023. {
  1024. _f.Engrave(_clan);
  1025. }
  1026. catch (Exception e)
  1027. {
  1028. _log.warning("Exception in endFortressSiege " + e.getMessage());
  1029. e.printStackTrace();
  1030. }
  1031. }
  1032. }
  1033. /**
  1034. * @return Returns state of fortress.<BR><BR>
  1035. * 0 - not decided yet<BR>
  1036. * 1 - independent<BR>
  1037. * 2 - contracted with castle<BR>
  1038. */
  1039. public final int getFortState()
  1040. {
  1041. return _state;
  1042. }
  1043. /**
  1044. * @param State.<BR>
  1045. * 0 - not decided yet<BR>
  1046. * 1 - independent<BR>
  1047. * 2 - contracted with castle<BR><BR>
  1048. * @param CastleId.<BR>
  1049. * set Castle Id for contracted fort
  1050. */
  1051. public final void setFortState(int state, int castleId)
  1052. {
  1053. _state = state;
  1054. _castleId = castleId;
  1055. Connection con = null;
  1056. try
  1057. {
  1058. con = L2DatabaseFactory.getInstance().getConnection();
  1059. PreparedStatement statement;
  1060. statement = con.prepareStatement("UPDATE fort SET state=?,castleId=? WHERE id = ?");
  1061. statement.setInt(1, getFortState());
  1062. statement.setInt(2, getCastleId());
  1063. statement.setInt(3, getFortId());
  1064. statement.execute();
  1065. statement.close();
  1066. }
  1067. catch (Exception e)
  1068. {
  1069. _log.warning("Exception: setFortState(int state, int castleId): " + e.getMessage());
  1070. e.printStackTrace();
  1071. }
  1072. finally
  1073. {
  1074. try
  1075. {
  1076. con.close();
  1077. }
  1078. catch (Exception e)
  1079. {
  1080. _log.warning("" + e.getMessage());
  1081. e.printStackTrace();
  1082. }
  1083. }
  1084. }
  1085. /**
  1086. * @return Returns Castle Id of fortress contracted with castle.
  1087. */
  1088. public final int getCastleId()
  1089. {
  1090. return _castleId;
  1091. }
  1092. /**
  1093. * @return Returns fortress type.<BR><BR>
  1094. * 0 - small (3 commanders) <BR>
  1095. * 1 - big (4 commanders + control room)
  1096. */
  1097. public final int getFortType()
  1098. {
  1099. return _fortType;
  1100. }
  1101. public final int getCastleIdFromEnvoy(int npcId)
  1102. {
  1103. return _envoyCastles.get(npcId);
  1104. }
  1105. /**
  1106. * @return Returns amount of barracks.
  1107. */
  1108. public final int getFortSize()
  1109. {
  1110. return getFortType() == 0 ? 3 : 5;
  1111. }
  1112. public void spawnNpcs(FastList<L2Spawn> spawnList)
  1113. {
  1114. for (L2Spawn spawnDat : spawnList)
  1115. {
  1116. spawnDat.doSpawn();
  1117. spawnDat.startRespawn();
  1118. }
  1119. }
  1120. public void despawnNpcs(FastList<L2Spawn> spawnList)
  1121. {
  1122. for (L2Spawn spawnDat : spawnList)
  1123. {
  1124. spawnDat.stopRespawn();
  1125. spawnDat.getLastSpawn().deleteMe();
  1126. }
  1127. }
  1128. /** FIXME: deleted
  1129. public void spawnSuspiciousMerchant()
  1130. {
  1131. for (L2Spawn spawnDat : _siegeNpcs)
  1132. {
  1133. spawnDat.doSpawn();
  1134. spawnDat.startRespawn();
  1135. }
  1136. }
  1137. public void despawnSuspiciousMerchant()
  1138. {
  1139. for (L2Spawn spawnDat : _siegeNpcs)
  1140. {
  1141. spawnDat.stopRespawn();
  1142. spawnDat.getLastSpawn().deleteMe();
  1143. }
  1144. }
  1145. public void spawnNpcCommanders()
  1146. {
  1147. for (L2Spawn spawnDat : _npcCommanders)
  1148. {
  1149. spawnDat.doSpawn();
  1150. spawnDat.startRespawn();
  1151. }
  1152. }
  1153. public void despawnNpcCommanders()
  1154. {
  1155. for (L2Spawn spawnDat : _npcCommanders)
  1156. {
  1157. spawnDat.stopRespawn();
  1158. spawnDat.getLastSpawn().deleteMe();
  1159. }
  1160. }
  1161. public void spawnSpecialEnvoys()
  1162. {
  1163. for (L2Spawn spawnDat : _specialEnvoys)
  1164. {
  1165. spawnDat.doSpawn();
  1166. spawnDat.startRespawn();
  1167. }
  1168. }
  1169. public void despawnSpecialEnvoys()
  1170. {
  1171. for (L2Spawn spawnDat : _specialEnvoys)
  1172. {
  1173. spawnDat.stopRespawn();
  1174. spawnDat.getLastSpawn().deleteMe();
  1175. }
  1176. }
  1177. **/
  1178. private void initNpcs()
  1179. {
  1180. Connection con = null;
  1181. try
  1182. {
  1183. con = L2DatabaseFactory.getInstance().getConnection();
  1184. PreparedStatement statement = con.prepareStatement("SELECT * FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ");
  1185. statement.setInt(1, getFortId());
  1186. statement.setInt(2, 0);
  1187. ResultSet rset = statement.executeQuery();
  1188. L2Spawn spawnDat;
  1189. L2NpcTemplate template;
  1190. while (rset.next())
  1191. {
  1192. template = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
  1193. if (template != null)
  1194. {
  1195. spawnDat = new L2Spawn(template);
  1196. spawnDat.setAmount(1);
  1197. spawnDat.setLocx(rset.getInt("x"));
  1198. spawnDat.setLocy(rset.getInt("y"));
  1199. spawnDat.setLocz(rset.getInt("z"));
  1200. spawnDat.setHeading(rset.getInt("heading"));
  1201. spawnDat.setRespawnDelay(60);
  1202. SpawnTable.getInstance().addNewSpawn(spawnDat, false);
  1203. spawnDat.doSpawn();
  1204. spawnDat.startRespawn();
  1205. }
  1206. else
  1207. {
  1208. _log.warning("Fort " + getFortId() + " initNpcs: Data missing in NPC table for ID: " + rset.getInt("npcId") + ".");
  1209. }
  1210. }
  1211. rset.close();
  1212. statement.close();
  1213. }
  1214. catch (Exception e)
  1215. {
  1216. // problem with initializing spawn, go to next one
  1217. _log.warning("Fort " + getFortId() + " initNpcs: Spawn could not be initialized: " + e.getMessage());
  1218. e.printStackTrace();
  1219. }
  1220. finally
  1221. {
  1222. try
  1223. {
  1224. con.close();
  1225. }
  1226. catch (Exception e)
  1227. {
  1228. _log.warning("" + e.getMessage());
  1229. e.printStackTrace();
  1230. }
  1231. }
  1232. }
  1233. private void initSiegeNpcs()
  1234. {
  1235. Connection con = null;
  1236. _siegeNpcs.clear();
  1237. try
  1238. {
  1239. con = L2DatabaseFactory.getInstance().getConnection();
  1240. PreparedStatement statement = con.prepareStatement("SELECT id, npcId, x, y, z, heading FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id");
  1241. statement.setInt(1, getFortId());
  1242. statement.setInt(2, 2);
  1243. ResultSet rset = statement.executeQuery();
  1244. L2Spawn spawnDat;
  1245. L2NpcTemplate template;
  1246. while (rset.next())
  1247. {
  1248. template = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
  1249. if (template != null)
  1250. {
  1251. spawnDat = new L2Spawn(template);
  1252. spawnDat.setAmount(1);
  1253. spawnDat.setLocx(rset.getInt("x"));
  1254. spawnDat.setLocy(rset.getInt("y"));
  1255. spawnDat.setLocz(rset.getInt("z"));
  1256. spawnDat.setHeading(rset.getInt("heading"));
  1257. spawnDat.setRespawnDelay(60);
  1258. _siegeNpcs.add(spawnDat);
  1259. }
  1260. else
  1261. {
  1262. _log.warning("Fort " + getFortId() + " initSiegeNpcs: Data missing in NPC table for ID: " + rset.getInt("npcId") + ".");
  1263. }
  1264. }
  1265. rset.close();
  1266. statement.close();
  1267. }
  1268. catch (Exception e)
  1269. {
  1270. // problem with initializing spawn, go to next one
  1271. _log.warning("Fort " + getFortId() + " initSiegeNpcs: Spawn could not be initialized: " + e.getMessage());
  1272. e.printStackTrace();
  1273. }
  1274. finally
  1275. {
  1276. try
  1277. {
  1278. con.close();
  1279. }
  1280. catch (Exception e)
  1281. {
  1282. _log.warning("" + e.getMessage());
  1283. e.printStackTrace();
  1284. }
  1285. }
  1286. }
  1287. private void initNpcCommanders()
  1288. {
  1289. Connection con = null;
  1290. _npcCommanders.clear();
  1291. try
  1292. {
  1293. con = L2DatabaseFactory.getInstance().getConnection();
  1294. PreparedStatement statement = con.prepareStatement("SELECT id, npcId, x, y, z, heading FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id");
  1295. statement.setInt(1, getFortId());
  1296. statement.setInt(2, 1);
  1297. ResultSet rset = statement.executeQuery();
  1298. L2Spawn spawnDat;
  1299. L2NpcTemplate template;
  1300. while (rset.next())
  1301. {
  1302. template = NpcTable.getInstance().getTemplate(rset.getInt("npcId"));
  1303. if (template != null)
  1304. {
  1305. spawnDat = new L2Spawn(template);
  1306. spawnDat.setAmount(1);
  1307. spawnDat.setLocx(rset.getInt("x"));
  1308. spawnDat.setLocy(rset.getInt("y"));
  1309. spawnDat.setLocz(rset.getInt("z"));
  1310. spawnDat.setHeading(rset.getInt("heading"));
  1311. spawnDat.setRespawnDelay(60);
  1312. _npcCommanders.add(spawnDat);
  1313. }
  1314. else
  1315. {
  1316. _log.warning("Fort " + getFortId() + " initNpcCommanders: Data missing in NPC table for ID: " + rset.getInt("npcId")
  1317. + ".");
  1318. }
  1319. }
  1320. rset.close();
  1321. statement.close();
  1322. }
  1323. catch (Exception e)
  1324. {
  1325. // problem with initializing spawn, go to next one
  1326. _log.warning("Fort " + getFortId() + " initNpcCommanders: Spawn could not be initialized: " + e.getMessage());
  1327. e.printStackTrace();
  1328. }
  1329. finally
  1330. {
  1331. try
  1332. {
  1333. con.close();
  1334. }
  1335. catch (Exception e)
  1336. {
  1337. _log.warning("" + e.getMessage());
  1338. e.printStackTrace();
  1339. }
  1340. }
  1341. }
  1342. private void initSpecialEnvoys()
  1343. {
  1344. Connection con = null;
  1345. _specialEnvoys.clear();
  1346. _envoyCastles.clear();
  1347. try
  1348. {
  1349. con = L2DatabaseFactory.getInstance().getConnection();
  1350. PreparedStatement statement = con.prepareStatement("SELECT id, npcId, x, y, z, heading, castleId FROM fort_spawnlist WHERE fortId = ? AND spawnType = ? ORDER BY id");
  1351. statement.setInt(1, getFortId());
  1352. statement.setInt(2, 3);
  1353. ResultSet rset = statement.executeQuery();
  1354. L2Spawn spawnDat;
  1355. L2NpcTemplate template;
  1356. while (rset.next())
  1357. {
  1358. int castleId = rset.getInt("castleId");
  1359. int npcId = rset.getInt("npcId");
  1360. template = NpcTable.getInstance().getTemplate(npcId);
  1361. if (template != null)
  1362. {
  1363. spawnDat = new L2Spawn(template);
  1364. spawnDat.setAmount(1);
  1365. spawnDat.setLocx(rset.getInt("x"));
  1366. spawnDat.setLocy(rset.getInt("y"));
  1367. spawnDat.setLocz(rset.getInt("z"));
  1368. spawnDat.setHeading(rset.getInt("heading"));
  1369. spawnDat.setRespawnDelay(60);
  1370. _specialEnvoys.add(spawnDat);
  1371. _envoyCastles.put(npcId, castleId);
  1372. }
  1373. else
  1374. {
  1375. _log.warning("Fort " + getFortId() + " initSpecialEnvoys: Data missing in NPC table for ID: " + rset.getInt("npcId")
  1376. + ".");
  1377. }
  1378. }
  1379. rset.close();
  1380. statement.close();
  1381. }
  1382. catch (Exception e)
  1383. {
  1384. // problem with initializing spawn, go to next one
  1385. _log.warning("Fort " + getFortId() + " initSpecialEnvoys: Spawn could not be initialized: " + e.getMessage());
  1386. e.printStackTrace();
  1387. }
  1388. finally
  1389. {
  1390. try
  1391. {
  1392. con.close();
  1393. }
  1394. catch (Exception e)
  1395. {
  1396. _log.warning("" + e.getMessage());
  1397. e.printStackTrace();
  1398. }
  1399. }
  1400. }
  1401. public FastList<L2Skill> getResidentialSkills()
  1402. {
  1403. return _residentialSkills;
  1404. }
  1405. public void giveResidentialSkills(L2PcInstance player)
  1406. {
  1407. if (_residentialSkills != null && !_residentialSkills.isEmpty())
  1408. {
  1409. for (L2Skill sk : _residentialSkills)
  1410. player.addSkill(sk, false);
  1411. }
  1412. }
  1413. public void removeResidentialSkills(L2PcInstance player)
  1414. {
  1415. if (_residentialSkills != null && !_residentialSkills.isEmpty())
  1416. {
  1417. for (L2Skill sk : _residentialSkills)
  1418. player.removeSkill(sk, false);
  1419. }
  1420. }
  1421. }