FortSiegeManager.java 11 KB

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