Fort.java 37 KB

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