Fort.java 39 KB

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