Castle.java 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 net.sf.l2j.gameserver.model.entity;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.Calendar;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import javolution.util.FastMap;
  24. import net.sf.l2j.Config;
  25. import net.sf.l2j.L2DatabaseFactory;
  26. import net.sf.l2j.gameserver.Announcements;
  27. import net.sf.l2j.gameserver.CastleUpdater;
  28. import net.sf.l2j.gameserver.SevenSigns;
  29. import net.sf.l2j.gameserver.ThreadPoolManager;
  30. import net.sf.l2j.gameserver.datatables.ClanTable;
  31. import net.sf.l2j.gameserver.datatables.DoorTable;
  32. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  33. import net.sf.l2j.gameserver.instancemanager.CastleManorManager;
  34. import net.sf.l2j.gameserver.instancemanager.CastleManorManager.CropProcure;
  35. import net.sf.l2j.gameserver.instancemanager.CastleManorManager.SeedProduction;
  36. import net.sf.l2j.gameserver.model.L2Clan;
  37. import net.sf.l2j.gameserver.model.L2Manor;
  38. import net.sf.l2j.gameserver.model.L2Object;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  41. import net.sf.l2j.gameserver.model.zone.type.L2CastleTeleportZone;
  42. import net.sf.l2j.gameserver.model.zone.type.L2CastleZone;
  43. import net.sf.l2j.gameserver.serverpackets.PlaySound;
  44. import net.sf.l2j.gameserver.serverpackets.PledgeShowInfoUpdate;
  45. public class Castle
  46. {
  47. protected static final Logger _log = Logger.getLogger(Castle.class.getName());
  48. // =========================================================
  49. // Data Field
  50. private FastList<CropProcure> _procure = new FastList<CropProcure>();
  51. private FastList<SeedProduction> _production = new FastList<SeedProduction>();
  52. private FastList<CropProcure> _procureNext = new FastList<CropProcure>();
  53. private FastList<SeedProduction> _productionNext = new FastList<SeedProduction>();
  54. private boolean _isNextPeriodApproved = false;
  55. private static final String CASTLE_MANOR_DELETE_PRODUCTION =
  56. "DELETE FROM castle_manor_production WHERE castle_id=?;";
  57. private static final String CASTLE_MANOR_DELETE_PRODUCTION_PERIOD =
  58. "DELETE FROM castle_manor_production WHERE castle_id=? AND period=?;";
  59. private static final String CASTLE_MANOR_DELETE_PROCURE =
  60. "DELETE FROM castle_manor_procure WHERE castle_id=?;";
  61. private static final String CASTLE_MANOR_DELETE_PROCURE_PERIOD =
  62. "DELETE FROM castle_manor_procure WHERE castle_id=? AND period=?;";
  63. private static final String CASTLE_UPDATE_CROP =
  64. "UPDATE castle_manor_procure SET can_buy=? WHERE crop_id=? AND castle_id=? AND period=?";
  65. private static final String CASTLE_UPDATE_SEED =
  66. "UPDATE castle_manor_production SET can_produce=? WHERE seed_id=? AND castle_id=? AND period=?";
  67. // =========================================================
  68. // Data Field
  69. private int _castleId = 0;
  70. private List<L2DoorInstance> _doors = new FastList<L2DoorInstance>();
  71. private List<String> _doorDefault = new FastList<String>();
  72. private String _name = "";
  73. private int _ownerId = 0;
  74. private Siege _siege = null;
  75. private Calendar _siegeDate;
  76. private int _siegeDayOfWeek = 7; // Default to saturday
  77. private int _siegeHourOfDay = 20; // Default to 8 pm server time
  78. private int _taxPercent = 0;
  79. private double _taxRate = 0;
  80. private int _treasury = 0;
  81. private L2CastleZone _zone;
  82. private L2CastleTeleportZone _teleZone;
  83. private L2Clan _formerOwner = null;
  84. private int _nbArtifact = 1;
  85. private Map<Integer, Integer> _engrave = new FastMap<Integer, Integer>();
  86. // =========================================================
  87. // Constructor
  88. public Castle(int castleId)
  89. {
  90. _castleId = castleId;
  91. if(_castleId == 7 || castleId == 9) // Goddard and Schuttgart
  92. _nbArtifact = 2;
  93. load();
  94. loadDoor();
  95. }
  96. // =========================================================
  97. // Method - Public
  98. public void Engrave(L2Clan clan, int objId)
  99. {
  100. _engrave.put(objId, clan.getClanId());
  101. if (_engrave.size() == _nbArtifact)
  102. {
  103. boolean rst = true;
  104. for (int id : _engrave.values())
  105. {
  106. if (id != clan.getClanId())
  107. rst = false;
  108. }
  109. if(rst)
  110. {
  111. _engrave.clear();
  112. setOwner(clan);
  113. }
  114. else
  115. getSiege().announceToPlayer("Clan " + clan.getName() + " has finished to engrave one of the rulers.", true);
  116. }
  117. else
  118. getSiege().announceToPlayer("Clan " + clan.getName() + " has finished to engrave one of the rulers.", true);
  119. }
  120. // This method add to the treasury
  121. /** Add amount to castle instance's treasury (warehouse). */
  122. public void addToTreasury(int amount)
  123. {
  124. // check if owned
  125. if (getOwnerId() <= 0)
  126. {
  127. return;
  128. }
  129. if (_name.equalsIgnoreCase("Schuttgart") || _name.equalsIgnoreCase("Goddard"))
  130. {
  131. Castle rune = CastleManager.getInstance().getCastle("rune");
  132. if (rune != null )
  133. {
  134. int runeTax = (int)(amount * rune.getTaxRate());
  135. if (rune.getOwnerId() > 0 ) rune.addToTreasury(runeTax);
  136. amount -= runeTax;
  137. }
  138. }
  139. if (!_name.equalsIgnoreCase("aden") && !_name.equalsIgnoreCase("Rune") && !_name.equalsIgnoreCase("Schuttgart") && !_name.equalsIgnoreCase("Goddard")) // If current castle instance is not Aden, Rune, Goddard or Schuttgart.
  140. {
  141. Castle aden = CastleManager.getInstance().getCastle("aden");
  142. if (aden != null)
  143. {
  144. int adenTax = (int)(amount * aden.getTaxRate()); // Find out what Aden gets from the current castle instance's income
  145. if (aden.getOwnerId() > 0) aden.addToTreasury(adenTax); // Only bother to really add the tax to the treasury if not npc owned
  146. amount -= adenTax; // Subtract Aden's income from current castle instance's income
  147. }
  148. }
  149. addToTreasuryNoTax(amount);
  150. }
  151. /** Add amount to castle instance's treasury (warehouse), no tax paying. */
  152. public boolean addToTreasuryNoTax(int amount)
  153. {
  154. if (getOwnerId() <= 0) return false;
  155. if (amount < 0) {
  156. amount *= -1;
  157. if (_treasury < amount) return false;
  158. _treasury -= amount;
  159. } else {
  160. if ((long)_treasury + amount > Integer.MAX_VALUE) _treasury = Integer.MAX_VALUE;
  161. else _treasury += amount;
  162. }
  163. java.sql.Connection con = null;
  164. try
  165. {
  166. con = L2DatabaseFactory.getInstance().getConnection();
  167. PreparedStatement statement = con.prepareStatement("Update castle set treasury = ? where id = ?");
  168. statement.setInt(1, getTreasury());
  169. statement.setInt(2, getCastleId());
  170. statement.execute();
  171. statement.close();
  172. }
  173. catch (Exception e) {}
  174. finally {try { con.close(); } catch (Exception e) {}}
  175. return true;
  176. }
  177. /**
  178. * Move non clan members off castle area and to nearest town.<BR><BR>
  179. */
  180. public void banishForeigners()
  181. {
  182. _zone.banishForeigners(getOwnerId());
  183. }
  184. /**
  185. * Return true if object is inside the zone
  186. */
  187. public boolean checkIfInZone(int x, int y, int z)
  188. {
  189. return _zone.isInsideZone(x, y, z);
  190. }
  191. /**
  192. * Sets this castles zone
  193. * @param zone
  194. */
  195. public void setZone(L2CastleZone zone)
  196. {
  197. _zone = zone;
  198. }
  199. public L2CastleZone getZone()
  200. {
  201. return _zone;
  202. }
  203. public void setTeleZone(L2CastleTeleportZone zone)
  204. {
  205. _teleZone = zone;
  206. }
  207. public L2CastleTeleportZone getTeleZone()
  208. {
  209. return _teleZone;
  210. }
  211. public void oustAllPlayers()
  212. {
  213. _teleZone.oustAllPlayers();
  214. }
  215. /**
  216. * Get the objects distance to this castle
  217. * @param obj
  218. * @return
  219. */
  220. public double getDistance(L2Object obj)
  221. {
  222. return _zone.getDistanceToZone(obj);
  223. }
  224. public void closeDoor(L2PcInstance activeChar, int doorId)
  225. {
  226. openCloseDoor(activeChar, doorId, false);
  227. }
  228. public void openDoor(L2PcInstance activeChar, int doorId)
  229. {
  230. openCloseDoor(activeChar, doorId, true);
  231. }
  232. public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
  233. {
  234. if (activeChar.getClanId() != getOwnerId())
  235. return;
  236. L2DoorInstance door = getDoor(doorId);
  237. if (door != null)
  238. {
  239. if (open)
  240. door.openMe();
  241. else
  242. door.closeMe();
  243. }
  244. }
  245. // This method is used to begin removing all castle upgrades
  246. public void removeUpgrade()
  247. {
  248. removeDoorUpgrade();
  249. }
  250. // This method updates the castle tax rate
  251. public void setOwner(L2Clan clan)
  252. {
  253. // Remove old owner
  254. if (getOwnerId() > 0 && (clan == null || clan.getClanId() != getOwnerId()))
  255. {
  256. L2Clan oldOwner = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  257. if (oldOwner != null)
  258. {
  259. if (_formerOwner == null)
  260. {
  261. _formerOwner = oldOwner;
  262. if (Config.REMOVE_CASTLE_CIRCLETS)
  263. {
  264. CastleManager.getInstance().removeCirclet(_formerOwner,getCastleId());
  265. }
  266. }
  267. oldOwner.setHasCastle(0); // Unset has castle flag for old owner
  268. Announcements.getInstance().announceToAll(oldOwner.getName() + " has lost " + getName() + " castle!");
  269. }
  270. }
  271. updateOwnerInDB(clan); // Update in database
  272. if (getSiege().getIsInProgress()) // If siege in progress
  273. getSiege().midVictory(); // Mid victory phase of siege
  274. updateClansReputation();
  275. }
  276. public void removeOwner(L2Clan clan)
  277. {
  278. if (clan != null)
  279. {
  280. _formerOwner = clan;
  281. if (Config.REMOVE_CASTLE_CIRCLETS)
  282. {
  283. CastleManager.getInstance().removeCirclet(_formerOwner,getCastleId());
  284. }
  285. clan.setHasCastle(0);
  286. Announcements.getInstance().announceToAll(clan.getName() + " has lost " +getName() + " castle");
  287. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  288. }
  289. updateOwnerInDB(null);
  290. if (getSiege().getIsInProgress())
  291. getSiege().midVictory();
  292. updateClansReputation();
  293. }
  294. // This method updates the castle tax rate
  295. public void setTaxPercent(L2PcInstance activeChar, int taxPercent)
  296. {
  297. int maxTax;
  298. switch(SevenSigns.getInstance().getSealOwner(SevenSigns.SEAL_STRIFE))
  299. {
  300. case SevenSigns.CABAL_DAWN:
  301. maxTax = 25;
  302. break;
  303. case SevenSigns.CABAL_DUSK:
  304. maxTax = 5;
  305. break;
  306. default: // no owner
  307. maxTax = 15;
  308. }
  309. if (taxPercent < 0 || taxPercent > maxTax)
  310. {
  311. activeChar.sendMessage("Tax value must be between 0 and "+maxTax+".");
  312. return;
  313. }
  314. setTaxPercent(taxPercent);
  315. activeChar.sendMessage(getName() + " castle tax changed to " + taxPercent + "%.");
  316. }
  317. public void setTaxPercent(int taxPercent)
  318. {
  319. _taxPercent = taxPercent;
  320. _taxRate = _taxPercent / 100.0;
  321. java.sql.Connection con = null;
  322. try
  323. {
  324. con = L2DatabaseFactory.getInstance().getConnection();
  325. PreparedStatement statement = con.prepareStatement("Update castle set taxPercent = ? where id = ?");
  326. statement.setInt(1, taxPercent);
  327. statement.setInt(2, getCastleId());
  328. statement.execute();
  329. statement.close();
  330. }
  331. catch (Exception e) {}
  332. finally {try { con.close(); } catch (Exception e) {}}
  333. }
  334. /**
  335. * Respawn all doors on castle grounds<BR><BR>
  336. */
  337. public void spawnDoor()
  338. {
  339. spawnDoor(false);
  340. }
  341. /**
  342. * Respawn all doors on castle grounds<BR><BR>
  343. */
  344. public void spawnDoor(boolean isDoorWeak)
  345. {
  346. for (int i = 0; i < getDoors().size(); i++)
  347. {
  348. L2DoorInstance door = getDoors().get(i);
  349. if (door.getCurrentHp() <= 0)
  350. {
  351. door.decayMe(); // Kill current if not killed already
  352. door = DoorTable.parseList(_doorDefault.get(i));
  353. if (isDoorWeak)
  354. door.setCurrentHp(door.getMaxHp() / 2);
  355. door.spawnMe(door.getX(), door.getY(),door.getZ());
  356. getDoors().set(i, door);
  357. }
  358. else if (door.getOpen() == 0)
  359. door.closeMe();
  360. }
  361. loadDoorUpgrade(); // Check for any upgrade the doors may have
  362. }
  363. // This method upgrade door
  364. public void upgradeDoor(int doorId, int hp, int pDef, int mDef)
  365. {
  366. L2DoorInstance door = getDoor(doorId);
  367. if (door == null)
  368. return;
  369. if (door != null && door.getDoorId() == doorId)
  370. {
  371. door.setCurrentHp(door.getMaxHp() + hp);
  372. saveDoorUpgrade(doorId, hp, pDef, mDef);
  373. return;
  374. }
  375. }
  376. // =========================================================
  377. // Method - Private
  378. // This method loads castle
  379. private void load()
  380. {
  381. java.sql.Connection con = null;
  382. try
  383. {
  384. PreparedStatement statement;
  385. ResultSet rs;
  386. con = L2DatabaseFactory.getInstance().getConnection();
  387. statement = con.prepareStatement("Select * from castle where id = ?");
  388. statement.setInt(1, getCastleId());
  389. rs = statement.executeQuery();
  390. while (rs.next())
  391. {
  392. _name = rs.getString("name");
  393. //_OwnerId = rs.getInt("ownerId");
  394. _siegeDate = Calendar.getInstance();
  395. _siegeDate.setTimeInMillis(rs.getLong("siegeDate"));
  396. _siegeDayOfWeek = rs.getInt("siegeDayOfWeek");
  397. if (_siegeDayOfWeek < 1 || _siegeDayOfWeek > 7)
  398. _siegeDayOfWeek = 7;
  399. _siegeHourOfDay = rs.getInt("siegeHourOfDay");
  400. if (_siegeHourOfDay < 0 || _siegeHourOfDay > 23)
  401. _siegeHourOfDay = 20;
  402. _taxPercent = rs.getInt("taxPercent");
  403. _treasury = rs.getInt("treasury");
  404. }
  405. statement.close();
  406. _taxRate = _taxPercent / 100.0;
  407. statement = con.prepareStatement("Select clan_id from clan_data where hasCastle = ?");
  408. statement.setInt(1, getCastleId());
  409. rs = statement.executeQuery();
  410. while (rs.next())
  411. {
  412. _ownerId = rs.getInt("clan_id");
  413. }
  414. if (getOwnerId() > 0)
  415. {
  416. L2Clan clan = ClanTable.getInstance().getClan(getOwnerId()); // Try to find clan instance
  417. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  418. }
  419. statement.close();
  420. }
  421. catch (Exception e)
  422. {
  423. _log.warning("Exception: loadCastleData(): " + e.getMessage());
  424. e.printStackTrace();
  425. }
  426. finally {try { con.close(); } catch (Exception e) {}}
  427. }
  428. // This method loads castle door data from database
  429. private void loadDoor()
  430. {
  431. java.sql.Connection con = null;
  432. try
  433. {
  434. con = L2DatabaseFactory.getInstance().getConnection();
  435. PreparedStatement statement = con.prepareStatement("Select * from castle_door where castleId = ?");
  436. statement.setInt(1, getCastleId());
  437. ResultSet rs = statement.executeQuery();
  438. while (rs.next())
  439. {
  440. // Create list of the door default for use when respawning dead doors
  441. _doorDefault.add(rs.getString("name")
  442. + ";" + rs.getInt("id")
  443. + ";" + rs.getInt("x")
  444. + ";" + rs.getInt("y")
  445. + ";" + rs.getInt("z")
  446. + ";" + rs.getInt("range_xmin")
  447. + ";" + rs.getInt("range_ymin")
  448. + ";" + rs.getInt("range_zmin")
  449. + ";" + rs.getInt("range_xmax")
  450. + ";" + rs.getInt("range_ymax")
  451. + ";" + rs.getInt("range_zmax")
  452. + ";" + rs.getInt("hp")
  453. + ";" + rs.getInt("pDef")
  454. + ";" + rs.getInt("mDef"));
  455. L2DoorInstance door = DoorTable.parseList(_doorDefault.get(_doorDefault.size() - 1));
  456. door.spawnMe(door.getX(), door.getY(),door.getZ());
  457. _doors.add(door);
  458. DoorTable.getInstance().putDoor(door);
  459. }
  460. statement.close();
  461. }
  462. catch (Exception e)
  463. {
  464. _log.warning("Exception: loadCastleDoor(): " + e.getMessage());
  465. e.printStackTrace();
  466. }
  467. finally {try { con.close(); } catch (Exception e) {}}
  468. }
  469. // This method loads castle door upgrade data from database
  470. private void loadDoorUpgrade()
  471. {
  472. java.sql.Connection con = null;
  473. try
  474. {
  475. con = L2DatabaseFactory.getInstance().getConnection();
  476. PreparedStatement statement = con.prepareStatement("Select * from castle_doorupgrade where doorId in (Select Id from castle_door where castleId = ?)");
  477. statement.setInt(1, getCastleId());
  478. ResultSet rs = statement.executeQuery();
  479. while (rs.next())
  480. {
  481. upgradeDoor(rs.getInt("id"), rs.getInt("hp"), rs.getInt("pDef"), rs.getInt("mDef"));
  482. }
  483. statement.close();
  484. }
  485. catch (Exception e)
  486. {
  487. _log.warning("Exception: loadCastleDoorUpgrade(): " + e.getMessage());
  488. e.printStackTrace();
  489. }
  490. finally {try { con.close(); } catch (Exception e) {}}
  491. }
  492. private void removeDoorUpgrade()
  493. {
  494. java.sql.Connection con = null;
  495. try
  496. {
  497. con = L2DatabaseFactory.getInstance().getConnection();
  498. PreparedStatement statement = con.prepareStatement("delete from castle_doorupgrade where doorId in (select id from castle_door where castleId=?)");
  499. statement.setInt(1, getCastleId());
  500. statement.execute();
  501. statement.close();
  502. }
  503. catch (Exception e)
  504. {
  505. _log.warning("Exception: removeDoorUpgrade(): " + e.getMessage());
  506. e.printStackTrace();
  507. }
  508. finally {try { con.close(); } catch (Exception e) {}}
  509. }
  510. private void saveDoorUpgrade(int doorId, int hp, int pDef, int mDef)
  511. {
  512. java.sql.Connection con = null;
  513. try
  514. {
  515. con = L2DatabaseFactory.getInstance().getConnection();
  516. PreparedStatement statement = con.prepareStatement("INSERT INTO castle_doorupgrade (doorId, hp, pDef, mDef) values (?,?,?,?)");
  517. statement.setInt(1, doorId);
  518. statement.setInt(2, hp);
  519. statement.setInt(3, pDef);
  520. statement.setInt(4, mDef);
  521. statement.execute();
  522. statement.close();
  523. }
  524. catch (Exception e)
  525. {
  526. _log.warning("Exception: saveDoorUpgrade(int doorId, int hp, int pDef, int mDef): " + e.getMessage());
  527. e.printStackTrace();
  528. }
  529. finally
  530. {
  531. try { con.close(); } catch (Exception e) {}
  532. }
  533. }
  534. private void updateOwnerInDB(L2Clan clan)
  535. {
  536. if (clan != null)
  537. _ownerId = clan.getClanId(); // Update owner id property
  538. else
  539. _ownerId = 0; // Remove owner
  540. java.sql.Connection con = null;
  541. try
  542. {
  543. con = L2DatabaseFactory.getInstance().getConnection();
  544. PreparedStatement statement;
  545. // ============================================================================
  546. // NEED TO REMOVE HAS CASTLE FLAG FROM CLAN_DATA
  547. // SHOULD BE CHECKED FROM CASTLE TABLE
  548. statement = con.prepareStatement("UPDATE clan_data SET hasCastle=0 WHERE hasCastle=?");
  549. statement.setInt(1, getCastleId());
  550. statement.execute();
  551. statement.close();
  552. statement = con.prepareStatement("UPDATE clan_data SET hasCastle=? WHERE clan_id=?");
  553. statement.setInt(1, getCastleId());
  554. statement.setInt(2, getOwnerId());
  555. statement.execute();
  556. statement.close();
  557. // ============================================================================
  558. // Announce to clan memebers
  559. if (clan != null)
  560. {
  561. clan.setHasCastle(getCastleId()); // Set has castle flag for new owner
  562. Announcements.getInstance().announceToAll(clan.getName() + " has taken " + getName() + " castle!");
  563. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  564. clan.broadcastToOnlineMembers(new PlaySound(1, "Siege_Victory", 0, 0, 0, 0, 0));
  565. ThreadPoolManager.getInstance().scheduleGeneral(new CastleUpdater(clan, 1), 3600000); // Schedule owner tasks to start running
  566. }
  567. }
  568. catch (Exception e)
  569. {
  570. _log.warning("Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage());
  571. e.printStackTrace();
  572. }
  573. finally
  574. {
  575. try { con.close(); } catch (Exception e) {}
  576. }
  577. }
  578. // =========================================================
  579. // Property
  580. public final int getCastleId()
  581. {
  582. return _castleId;
  583. }
  584. public final L2DoorInstance getDoor(int doorId)
  585. {
  586. if (doorId <= 0)
  587. return null;
  588. for (int i = 0; i < getDoors().size(); i++)
  589. {
  590. L2DoorInstance door = getDoors().get(i);
  591. if (door.getDoorId() == doorId)
  592. return door;
  593. }
  594. return null;
  595. }
  596. public final List<L2DoorInstance> getDoors()
  597. {
  598. return _doors;
  599. }
  600. public final String getName()
  601. {
  602. return _name;
  603. }
  604. public final int getOwnerId()
  605. {
  606. return _ownerId;
  607. }
  608. public final Siege getSiege()
  609. {
  610. if (_siege == null) _siege = new Siege(new Castle[] {this});
  611. return _siege;
  612. }
  613. public final Calendar getSiegeDate() { return _siegeDate; }
  614. public final int getSiegeDayOfWeek() { return _siegeDayOfWeek; }
  615. public final int getSiegeHourOfDay() { return _siegeHourOfDay; }
  616. public final int getTaxPercent()
  617. {
  618. return _taxPercent;
  619. }
  620. public final double getTaxRate()
  621. {
  622. return _taxRate;
  623. }
  624. public final int getTreasury()
  625. {
  626. return _treasury;
  627. }
  628. public FastList<SeedProduction> getSeedProduction(int period)
  629. {
  630. return (period == CastleManorManager.PERIOD_CURRENT ? _production : _productionNext);
  631. }
  632. public FastList<CropProcure> getCropProcure(int period)
  633. {
  634. return (period == CastleManorManager.PERIOD_CURRENT ? _procure : _procureNext);
  635. }
  636. public void setSeedProduction(FastList<SeedProduction> seed, int period)
  637. {
  638. if (period == CastleManorManager.PERIOD_CURRENT)
  639. _production = seed;
  640. else
  641. _productionNext = seed;
  642. }
  643. public void setCropProcure(FastList<CropProcure> crop, int period)
  644. {
  645. if (period == CastleManorManager.PERIOD_CURRENT)
  646. _procure = crop;
  647. else
  648. _procureNext = crop;
  649. }
  650. public synchronized SeedProduction getSeed(int seedId, int period)
  651. {
  652. for (SeedProduction seed : getSeedProduction(period))
  653. {
  654. if (seed.getId() == seedId)
  655. {
  656. return seed;
  657. }
  658. }
  659. return null;
  660. }
  661. public synchronized CropProcure getCrop(int cropId, int period)
  662. {
  663. for (CropProcure crop : getCropProcure(period) )
  664. {
  665. if (crop.getId() == cropId)
  666. {
  667. return crop;
  668. }
  669. }
  670. return null;
  671. }
  672. public int getManorCost (int period)
  673. {
  674. FastList<CropProcure> procure;
  675. FastList<SeedProduction> production;
  676. if (period == CastleManorManager.PERIOD_CURRENT)
  677. {
  678. procure = _procure;
  679. production = _production;
  680. } else {
  681. procure = _procureNext;
  682. production = _productionNext;
  683. }
  684. int total = 0;
  685. if (production != null)
  686. {
  687. for (SeedProduction seed : production)
  688. {
  689. total += L2Manor.getInstance().getSeedBuyPrice(seed.getId()) * seed.getStartProduce();
  690. }
  691. }
  692. if (procure != null)
  693. {
  694. for (CropProcure crop: procure)
  695. {
  696. total += crop.getPrice() * crop.getStartAmount();
  697. }
  698. }
  699. return total;
  700. }
  701. //save manor production data
  702. public void saveSeedData()
  703. {
  704. java.sql.Connection con = null;
  705. PreparedStatement statement;
  706. try
  707. {
  708. con = L2DatabaseFactory.getInstance().getConnection();
  709. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION);
  710. statement.setInt(1, getCastleId());
  711. statement.execute();
  712. statement.close();
  713. if (_production != null)
  714. {
  715. int count = 0;
  716. String query = "INSERT INTO castle_manor_production VALUES ";
  717. String values[] = new String[_production.size()];
  718. for(SeedProduction s : _production)
  719. {
  720. values[count] = "("+getCastleId()+","+s.getId()+","+s.getCanProduce()+","+s.getStartProduce()+","+s.getPrice()+","+CastleManorManager.PERIOD_CURRENT+")";
  721. count++;
  722. }
  723. if (values.length > 0)
  724. {
  725. query += values[0];
  726. for (int i=1; i<values.length; i++)
  727. {
  728. query += "," + values[i];
  729. }
  730. statement = con.prepareStatement(query);
  731. statement.execute();
  732. statement.close();
  733. }
  734. }
  735. if (_productionNext != null)
  736. {
  737. int count = 0;
  738. String query = "INSERT INTO castle_manor_production VALUES ";
  739. String values[] = new String[_productionNext.size()];
  740. for(SeedProduction s : _productionNext)
  741. {
  742. values[count] = "("+getCastleId()+","+s.getId()+","+s.getCanProduce()+","+s.getStartProduce()+","+s.getPrice()+","+CastleManorManager.PERIOD_NEXT+")";
  743. count++;
  744. }
  745. if (values.length > 0)
  746. {
  747. query += values[0];
  748. for (int i=1;i<values.length;i++)
  749. {
  750. query += "," + values[i];
  751. }
  752. statement = con.prepareStatement(query);
  753. statement.execute();
  754. statement.close();
  755. }
  756. }
  757. } catch (Exception e)
  758. {
  759. _log.info("Error adding seed production data for castle " + getName() +": " + e.getMessage());
  760. } finally {
  761. try { con.close(); } catch (Exception e) {}
  762. }
  763. }
  764. //save manor production data for specified period
  765. public void saveSeedData(int period)
  766. {
  767. java.sql.Connection con = null;
  768. PreparedStatement statement;
  769. try
  770. {
  771. con = L2DatabaseFactory.getInstance().getConnection();
  772. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PRODUCTION_PERIOD);
  773. statement.setInt(1, getCastleId());
  774. statement.setInt(2, period);
  775. statement.execute();
  776. statement.close();
  777. FastList<SeedProduction> prod = null;
  778. prod = getSeedProduction(period);
  779. if (prod != null)
  780. {
  781. int count = 0;
  782. String query = "INSERT INTO castle_manor_production VALUES ";
  783. String values[] = new String[prod.size()];
  784. for(SeedProduction s : prod)
  785. {
  786. values[count] = "("+getCastleId()+","+s.getId()+","+s.getCanProduce()+","+s.getStartProduce()+","+s.getPrice()+","+period+")";
  787. count++;
  788. }
  789. if (values.length > 0)
  790. {
  791. query += values[0];
  792. for (int i=1;i<values.length;i++)
  793. {
  794. query += "," + values[i];
  795. }
  796. statement = con.prepareStatement(query);
  797. statement.execute();
  798. statement.close();
  799. }
  800. }
  801. } catch (Exception e)
  802. {
  803. _log.info("Error adding seed production data for castle " + getName() +": " + e.getMessage());
  804. } finally {
  805. try { con.close(); } catch (Exception e) {}
  806. }
  807. }
  808. //save crop procure data
  809. public void saveCropData()
  810. {
  811. java.sql.Connection con = null;
  812. PreparedStatement statement;
  813. try
  814. {
  815. con = L2DatabaseFactory.getInstance().getConnection();
  816. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE);
  817. statement.setInt(1, getCastleId());
  818. statement.execute();
  819. statement.close();
  820. if (_procure != null)
  821. {
  822. int count = 0;
  823. String query = "INSERT INTO castle_manor_procure VALUES ";
  824. String values[] = new String[_procure.size()];
  825. for (CropProcure cp : _procure)
  826. {
  827. values[count] = "("+getCastleId()+","+cp.getId()+","+cp.getAmount()+","+cp.getStartAmount()+","+cp.getPrice()+","+cp.getReward()+","+CastleManorManager.PERIOD_CURRENT+")";
  828. count++;
  829. }
  830. if (values.length > 0)
  831. {
  832. query += values[0];
  833. for (int i=1;i<values.length;i++)
  834. {
  835. query += "," + values[i];
  836. }
  837. statement = con.prepareStatement(query);
  838. statement.execute();
  839. statement.close();
  840. }
  841. }
  842. if (_procureNext != null)
  843. {
  844. int count = 0;
  845. String query = "INSERT INTO castle_manor_procure VALUES ";
  846. String values[] = new String[_procureNext.size()];
  847. for (CropProcure cp : _procureNext)
  848. {
  849. values[count] = "("+getCastleId()+","+cp.getId()+","+cp.getAmount()+","+cp.getStartAmount()+","+cp.getPrice()+","+cp.getReward()+","+CastleManorManager.PERIOD_NEXT+")";
  850. count++;
  851. }
  852. if (values.length > 0)
  853. {
  854. query += values[0];
  855. for (int i=1;i<values.length;i++)
  856. {
  857. query += "," + values[i];
  858. }
  859. statement = con.prepareStatement(query);
  860. statement.execute();
  861. statement.close();
  862. }
  863. }
  864. } catch (Exception e) {
  865. _log.info("Error adding crop data for castle " + getName() +": " + e.getMessage());
  866. } finally {
  867. try {
  868. con.close();
  869. } catch (Exception e) { }
  870. }
  871. }
  872. // save crop procure data for specified period
  873. public void saveCropData(int period) {
  874. java.sql.Connection con = null;
  875. PreparedStatement statement;
  876. try
  877. {
  878. con = L2DatabaseFactory.getInstance().getConnection();
  879. statement = con.prepareStatement(CASTLE_MANOR_DELETE_PROCURE_PERIOD);
  880. statement.setInt(1, getCastleId());
  881. statement.setInt(2, period);
  882. statement.execute();
  883. statement.close();
  884. FastList<CropProcure> proc = null;
  885. proc = getCropProcure(period);
  886. if (proc != null)
  887. {
  888. int count = 0;
  889. String query = "INSERT INTO castle_manor_procure VALUES ";
  890. String values[] = new String[proc.size()];
  891. for (CropProcure cp : proc)
  892. {
  893. values[count] = "("+getCastleId()+","+cp.getId()+","+cp.getAmount()+","+cp.getStartAmount()+","+cp.getPrice()+","+cp.getReward()+","+period+")";
  894. count++;
  895. }
  896. if (values.length > 0)
  897. {
  898. query += values[0];
  899. for (int i=1;i<values.length;i++)
  900. {
  901. query += "," + values[i];
  902. }
  903. statement = con.prepareStatement(query);
  904. statement.execute();
  905. statement.close();
  906. }
  907. }
  908. } catch (Exception e) {
  909. _log.info("Error adding crop data for castle " + getName() +": " + e.getMessage());
  910. } finally {
  911. try {
  912. con.close();
  913. } catch (Exception e) { }
  914. }
  915. }
  916. public void updateCrop (int cropId, int amount, int period)
  917. {
  918. java.sql.Connection con = null;
  919. PreparedStatement statement;
  920. try
  921. {
  922. con = L2DatabaseFactory.getInstance().getConnection();
  923. statement = con.prepareStatement(CASTLE_UPDATE_CROP);
  924. statement.setInt(1, amount);
  925. statement.setInt(2, cropId);
  926. statement.setInt(3, getCastleId());
  927. statement.setInt(4, period);
  928. statement.execute();
  929. statement.close();
  930. } catch (Exception e) {
  931. _log.info("Error adding crop data for castle " + getName() +": " + e.getMessage());
  932. } finally {
  933. try {
  934. con.close();
  935. } catch (Exception e) { }
  936. }
  937. }
  938. public void updateSeed (int seedId, int amount, int period)
  939. {
  940. java.sql.Connection con = null;
  941. PreparedStatement statement;
  942. try
  943. {
  944. con = L2DatabaseFactory.getInstance().getConnection();
  945. statement = con.prepareStatement(CASTLE_UPDATE_SEED);
  946. statement.setInt(1, amount);
  947. statement.setInt(2, seedId);
  948. statement.setInt(3, getCastleId());
  949. statement.setInt(4, period);
  950. statement.execute();
  951. statement.close();
  952. } catch (Exception e) {
  953. _log.info("Error adding seed production data for castle " + getName() +": " + e.getMessage());
  954. } finally {
  955. try {
  956. con.close();
  957. } catch (Exception e) { }
  958. }
  959. }
  960. public boolean isNextPeriodApproved()
  961. {
  962. return _isNextPeriodApproved;
  963. }
  964. public void setNextPeriodApproved(boolean val)
  965. {
  966. _isNextPeriodApproved = val;
  967. }
  968. public void updateClansReputation()
  969. {
  970. if (_formerOwner != null )
  971. {
  972. if (_formerOwner != ClanTable.getInstance().getClan(getOwnerId()))
  973. {
  974. int maxreward = Math.max(0,_formerOwner.getReputationScore());
  975. _formerOwner.setReputationScore(_formerOwner.getReputationScore()-1000, true);
  976. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  977. if (owner != null)
  978. {
  979. owner.setReputationScore(owner.getReputationScore()+Math.min(1000,maxreward), true);
  980. owner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(owner));
  981. }
  982. }
  983. else
  984. _formerOwner.setReputationScore(_formerOwner.getReputationScore()+500, true);
  985. _formerOwner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(_formerOwner));
  986. }
  987. else
  988. {
  989. L2Clan owner = ClanTable.getInstance().getClan(getOwnerId());
  990. if (owner != null)
  991. {
  992. owner.setReputationScore(owner.getReputationScore()+1000, true);
  993. owner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(owner));
  994. }
  995. }
  996. }
  997. }