Fort.java 38 KB

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