FortSiegeManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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.instancemanager;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.InputStream;
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.util.List;
  23. import java.util.Properties;
  24. import java.util.StringTokenizer;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.datatables.SkillTable;
  29. import com.l2jserver.gameserver.model.CombatFlag;
  30. import com.l2jserver.gameserver.model.L2Clan;
  31. import com.l2jserver.gameserver.model.L2ItemInstance;
  32. import com.l2jserver.gameserver.model.L2Object;
  33. import com.l2jserver.gameserver.model.Location;
  34. import com.l2jserver.gameserver.model.actor.L2Character;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.entity.Fort;
  37. import com.l2jserver.gameserver.model.entity.FortSiege;
  38. import com.l2jserver.gameserver.network.SystemMessageId;
  39. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  40. import javolution.util.FastList;
  41. import javolution.util.FastMap;
  42. public class FortSiegeManager
  43. {
  44. private static final Logger _log = Logger.getLogger(FortSiegeManager.class.getName());
  45. public static final FortSiegeManager getInstance()
  46. {
  47. return SingletonHolder._instance;
  48. }
  49. // =========================================================
  50. // Data Field
  51. private int _attackerMaxClans = 500; // Max number of clans
  52. // Fort Siege settings
  53. private FastMap<Integer, FastList<SiegeSpawn>> _commanderSpawnList;
  54. private FastMap<Integer, FastList<CombatFlag>> _flagList;
  55. private int _flagMaxCount = 1; // Changeable in fortsiege.properties
  56. private int _siegeClanMinLevel = 4; // Changeable in fortsiege.properties
  57. private int _siegeLength = 60; // Time in minute. Changeable in fortsiege.properties
  58. private int _countDownLength = 10; // Time in minute. Changeable in fortsiege.properties
  59. private int _suspiciousMerchantRespawnDelay = 180; // Time in minute. Changeable in fortsiege.properties
  60. private List<FortSiege> _sieges;
  61. // =========================================================
  62. // Constructor
  63. private FortSiegeManager()
  64. {
  65. _log.info("Initializing FortSiegeManager");
  66. load();
  67. }
  68. // =========================================================
  69. // Method - Public
  70. public final void addSiegeSkills(L2PcInstance character)
  71. {
  72. character.addSkill(SkillTable.FrequentSkill.SEAL_OF_RULER.getSkill(), false);
  73. character.addSkill(SkillTable.FrequentSkill.BUILD_HEADQUARTERS.getSkill(), false);
  74. }
  75. /**
  76. * Return true if character summon<BR><BR>
  77. * @param activeChar The L2Character of the character can summon
  78. */
  79. public final boolean checkIfOkToSummon(L2Character activeChar, boolean isCheckOnly)
  80. {
  81. if (!(activeChar instanceof L2PcInstance))
  82. return false;
  83. String text = "";
  84. L2PcInstance player = (L2PcInstance) activeChar;
  85. Fort fort = FortManager.getInstance().getFort(player);
  86. if (fort == null || fort.getFortId() <= 0)
  87. text = "You must be on fort ground to summon this";
  88. else if (!fort.getSiege().getIsInProgress())
  89. text = "You can only summon this during a siege.";
  90. else if (player.getClanId() != 0 && fort.getSiege().getAttackerClan(player.getClanId()) == null)
  91. text = "You can only summon this as a registered attacker.";
  92. else
  93. return true;
  94. if (!isCheckOnly)
  95. player.sendMessage(text);
  96. return false;
  97. }
  98. /**
  99. * Return true if the clan is registered or owner of a fort<BR><BR>
  100. * @param clan The L2Clan of the player
  101. */
  102. public final boolean checkIsRegistered(L2Clan clan, int fortid)
  103. {
  104. if (clan == null)
  105. return false;
  106. Connection con = null;
  107. boolean register = false;
  108. try
  109. {
  110. con = L2DatabaseFactory.getInstance().getConnection();
  111. PreparedStatement statement = con.prepareStatement("SELECT clan_id FROM fortsiege_clans where clan_id=? and fort_id=?");
  112. statement.setInt(1, clan.getClanId());
  113. statement.setInt(2, fortid);
  114. ResultSet rs = statement.executeQuery();
  115. while (rs.next())
  116. {
  117. register = true;
  118. break;
  119. }
  120. rs.close();
  121. statement.close();
  122. }
  123. catch (Exception e)
  124. {
  125. _log.warning("Exception: checkIsRegistered(): " + e.getMessage());
  126. e.printStackTrace();
  127. }
  128. finally
  129. {
  130. try
  131. {
  132. con.close();
  133. }
  134. catch (Exception e)
  135. {
  136. _log.warning("" + e.getMessage());
  137. e.printStackTrace();
  138. }
  139. }
  140. return register;
  141. }
  142. public final void removeSiegeSkills(L2PcInstance character)
  143. {
  144. character.removeSkill(SkillTable.FrequentSkill.SEAL_OF_RULER.getSkill());
  145. character.removeSkill(SkillTable.FrequentSkill.BUILD_HEADQUARTERS.getSkill());
  146. }
  147. // =========================================================
  148. // Method - Private
  149. private final void load()
  150. {
  151. InputStream is = null;
  152. try
  153. {
  154. is = new FileInputStream(new File(Config.FORTSIEGE_CONFIGURATION_FILE));
  155. Properties siegeSettings = new Properties();
  156. siegeSettings.load(is);
  157. // Siege setting
  158. _attackerMaxClans = Integer.decode(siegeSettings.getProperty("AttackerMaxClans", "500"));
  159. _flagMaxCount = Integer.decode(siegeSettings.getProperty("MaxFlags", "1"));
  160. _siegeClanMinLevel = Integer.decode(siegeSettings.getProperty("SiegeClanMinLevel", "4"));
  161. _siegeLength = Integer.decode(siegeSettings.getProperty("SiegeLength", "60"));
  162. _countDownLength = Integer.decode(siegeSettings.getProperty("CountDownLength", "10"));
  163. _suspiciousMerchantRespawnDelay = Integer.decode(siegeSettings.getProperty("SuspiciousMerchantRespawnDelay", "180"));
  164. // Siege spawns settings
  165. _commanderSpawnList = new FastMap<Integer, FastList<SiegeSpawn>>();
  166. _flagList = new FastMap<Integer, FastList<CombatFlag>>();
  167. for (Fort fort : FortManager.getInstance().getForts())
  168. {
  169. FastList<SiegeSpawn> _commanderSpawns = new FastList<SiegeSpawn>();
  170. FastList<CombatFlag> _flagSpawns = new FastList<CombatFlag>();
  171. for (int i = 1; i < 5; i++)
  172. {
  173. String _spawnParams = siegeSettings.getProperty(fort.getName().replace(" ", "") + "Commander" + i, "");
  174. if (_spawnParams.length() == 0)
  175. break;
  176. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  177. try
  178. {
  179. int x = Integer.parseInt(st.nextToken());
  180. int y = Integer.parseInt(st.nextToken());
  181. int z = Integer.parseInt(st.nextToken());
  182. int heading = Integer.parseInt(st.nextToken());
  183. int npc_id = Integer.parseInt(st.nextToken());
  184. _commanderSpawns.add(new SiegeSpawn(fort.getFortId(), x, y, z, heading, npc_id, i));
  185. }
  186. catch (Exception e)
  187. {
  188. _log.warning("Error while loading commander(s) for " + fort.getName() + " fort.");
  189. }
  190. }
  191. _commanderSpawnList.put(fort.getFortId(), _commanderSpawns);
  192. for (int i = 1; i < 4; i++)
  193. {
  194. String _spawnParams = siegeSettings.getProperty(fort.getName().replace(" ", "") + "Flag" + i, "");
  195. if (_spawnParams.length() == 0)
  196. break;
  197. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  198. try
  199. {
  200. int x = Integer.parseInt(st.nextToken());
  201. int y = Integer.parseInt(st.nextToken());
  202. int z = Integer.parseInt(st.nextToken());
  203. int flag_id = Integer.parseInt(st.nextToken());
  204. _flagSpawns.add(new CombatFlag(fort.getFortId(), x, y, z, 0, flag_id));
  205. }
  206. catch (Exception e)
  207. {
  208. _log.warning("Error while loading flag(s) for " + fort.getName() + " fort.");
  209. }
  210. }
  211. _flagList.put(fort.getFortId(), _flagSpawns);
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. //_initialized = false;
  217. _log.warning("Error while loading fortsiege data." + e.getMessage());
  218. e.printStackTrace();
  219. }
  220. finally
  221. {
  222. try
  223. {
  224. is.close();
  225. }
  226. catch (Exception e)
  227. {
  228. _log.warning("" + e.getMessage());
  229. e.printStackTrace();
  230. }
  231. }
  232. }
  233. // =========================================================
  234. // Property - Public
  235. public final FastList<SiegeSpawn> getCommanderSpawnList(int _fortId)
  236. {
  237. if (_commanderSpawnList.containsKey(_fortId))
  238. {
  239. return _commanderSpawnList.get(_fortId);
  240. }
  241. else
  242. {
  243. return null;
  244. }
  245. }
  246. public final FastList<CombatFlag> getFlagList(int _fortId)
  247. {
  248. if (_flagList.containsKey(_fortId))
  249. return _flagList.get(_fortId);
  250. else
  251. return null;
  252. }
  253. public final int getAttackerMaxClans()
  254. {
  255. return _attackerMaxClans;
  256. }
  257. public final int getFlagMaxCount()
  258. {
  259. return _flagMaxCount;
  260. }
  261. public final int getSuspiciousMerchantRespawnDelay()
  262. {
  263. return _suspiciousMerchantRespawnDelay;
  264. }
  265. public final FortSiege getSiege(L2Object activeObject)
  266. {
  267. return getSiege(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  268. }
  269. public final FortSiege getSiege(int x, int y, int z)
  270. {
  271. for (Fort fort : FortManager.getInstance().getForts())
  272. if (fort.getSiege().checkIfInZone(x, y, z))
  273. return fort.getSiege();
  274. return null;
  275. }
  276. public final int getSiegeClanMinLevel()
  277. {
  278. return _siegeClanMinLevel;
  279. }
  280. public final int getSiegeLength()
  281. {
  282. return _siegeLength;
  283. }
  284. public final int getCountDownLength()
  285. {
  286. return _countDownLength;
  287. }
  288. public final List<FortSiege> getSieges()
  289. {
  290. if (_sieges == null)
  291. _sieges = new FastList<FortSiege>();
  292. return _sieges;
  293. }
  294. public final void addSiege(FortSiege fortSiege)
  295. {
  296. if (_sieges == null)
  297. _sieges = new FastList<FortSiege>();
  298. _sieges.add(fortSiege);
  299. }
  300. public boolean isCombat(int itemId)
  301. {
  302. return (itemId == 9819);
  303. }
  304. public boolean activateCombatFlag(L2PcInstance player, L2ItemInstance item)
  305. {
  306. if (!checkIfCanPickup(player))
  307. return false;
  308. Fort fort = FortManager.getInstance().getFort(player);
  309. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  310. for (CombatFlag cf : fcf)
  311. {
  312. if (cf.itemInstance == item)
  313. {
  314. cf.activate(player, item);
  315. }
  316. }
  317. return true;
  318. }
  319. public boolean checkIfCanPickup(L2PcInstance player)
  320. {
  321. SystemMessage sm;
  322. sm = new SystemMessage(SystemMessageId.THE_FORTRESS_BATTLE_OF_S1_HAS_FINISHED);
  323. sm.addItemName(9819);
  324. // Cannot own 2 combat flag
  325. if (player.isCombatFlagEquipped())
  326. {
  327. player.sendPacket(sm);
  328. return false;
  329. }
  330. // here check if is siege is in progress
  331. // here check if is siege is attacker
  332. Fort fort = FortManager.getInstance().getFort(player);
  333. if (fort == null || fort.getFortId() <= 0)
  334. {
  335. player.sendPacket(sm);
  336. return false;
  337. }
  338. else if (!fort.getSiege().getIsInProgress())
  339. {
  340. player.sendPacket(sm);
  341. return false;
  342. }
  343. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  344. {
  345. player.sendPacket(sm);
  346. return false;
  347. }
  348. return true;
  349. }
  350. public void dropCombatFlag(L2PcInstance player)
  351. {
  352. Fort fort = FortManager.getInstance().getFort(player);
  353. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  354. for (CombatFlag cf : fcf)
  355. {
  356. if (cf.playerId == player.getObjectId())
  357. {
  358. cf.dropIt();
  359. if (fort.getSiege().getIsInProgress())
  360. cf.spawnMe();
  361. }
  362. }
  363. }
  364. public class SiegeSpawn
  365. {
  366. Location _location;
  367. private int _npcId;
  368. private int _heading;
  369. private int _fortId;
  370. private int _id;
  371. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id, int id)
  372. {
  373. _fortId = fort_id;
  374. _location = new Location(x, y, z, heading);
  375. _heading = heading;
  376. _npcId = npc_id;
  377. _id = id;
  378. }
  379. public int getFortId()
  380. {
  381. return _fortId;
  382. }
  383. public int getNpcId()
  384. {
  385. return _npcId;
  386. }
  387. public int getHeading()
  388. {
  389. return _heading;
  390. }
  391. public int getId()
  392. {
  393. return _id;
  394. }
  395. public Location getLocation()
  396. {
  397. return _location;
  398. }
  399. }
  400. @SuppressWarnings("synthetic-access")
  401. private static class SingletonHolder
  402. {
  403. protected static final FortSiegeManager _instance = new FortSiegeManager();
  404. }
  405. }