Fort.java 33 KB

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