Fort.java 37 KB

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