FortSiegeManager.java 12 KB

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