FortSiegeManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.Level;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastList;
  28. import javolution.util.FastMap;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.L2DatabaseFactory;
  31. import com.l2jserver.gameserver.datatables.SkillTable;
  32. import com.l2jserver.gameserver.model.CombatFlag;
  33. import com.l2jserver.gameserver.model.L2Clan;
  34. import com.l2jserver.gameserver.model.L2Object;
  35. import com.l2jserver.gameserver.model.Location;
  36. import com.l2jserver.gameserver.model.actor.L2Character;
  37. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  38. import com.l2jserver.gameserver.model.entity.Fort;
  39. import com.l2jserver.gameserver.model.entity.FortSiege;
  40. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  41. import com.l2jserver.gameserver.network.SystemMessageId;
  42. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  43. public class FortSiegeManager
  44. {
  45. private static final Logger _log = Logger.getLogger(FortSiegeManager.class.getName());
  46. public static final FortSiegeManager getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. private int _attackerMaxClans = 500; // Max number of clans
  51. // Fort Siege settings
  52. private FastMap<Integer, FastList<SiegeSpawn>> _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. L2PcInstance player = (L2PcInstance) activeChar;
  82. 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.getClanId());
  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. 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<SiegeSpawn> _commanderSpawns = new FastList<>();
  167. FastList<CombatFlag> _flagSpawns = new FastList<>();
  168. for (int i = 1; i < 5; i++)
  169. {
  170. String _spawnParams = siegeSettings.getProperty(fort.getName().replace(" ", "") + "Commander" + i, "");
  171. if (_spawnParams.isEmpty())
  172. {
  173. break;
  174. }
  175. 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 SiegeSpawn(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. String _spawnParams = siegeSettings.getProperty(fort.getName().replace(" ", "") + "Flag" + i, "");
  194. if (_spawnParams.isEmpty())
  195. {
  196. break;
  197. }
  198. 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<SiegeSpawn> 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. Fort fort = FortManager.getInstance().getFort(player);
  297. 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. SystemMessage sm;
  310. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_FORTRESS_BATTLE_OF_S1_HAS_FINISHED);
  311. sm.addItemName(9819);
  312. // Cannot own 2 combat flag
  313. if (player.isCombatFlagEquipped())
  314. {
  315. player.sendPacket(sm);
  316. return false;
  317. }
  318. // here check if is siege is in progress
  319. // here check if is siege is attacker
  320. Fort fort = FortManager.getInstance().getFort(player);
  321. if ((fort == null) || (fort.getFortId() <= 0))
  322. {
  323. player.sendPacket(sm);
  324. return false;
  325. }
  326. else if (!fort.getSiege().getIsInProgress())
  327. {
  328. player.sendPacket(sm);
  329. return false;
  330. }
  331. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  332. {
  333. player.sendPacket(sm);
  334. return false;
  335. }
  336. return true;
  337. }
  338. public void dropCombatFlag(L2PcInstance player, int fortId)
  339. {
  340. Fort fort = FortManager.getInstance().getFortById(fortId);
  341. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  342. for (CombatFlag cf : fcf)
  343. {
  344. if (cf.getPlayerObjectId() == player.getObjectId())
  345. {
  346. cf.dropIt();
  347. if (fort.getSiege().getIsInProgress())
  348. {
  349. cf.spawnMe();
  350. }
  351. }
  352. }
  353. }
  354. public static class SiegeSpawn
  355. {
  356. Location _location;
  357. private final int _npcId;
  358. private final int _heading;
  359. private final int _fortId;
  360. private final int _id;
  361. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id, int id)
  362. {
  363. _fortId = fort_id;
  364. _location = new Location(x, y, z, heading);
  365. _heading = heading;
  366. _npcId = npc_id;
  367. _id = id;
  368. }
  369. public int getFortId()
  370. {
  371. return _fortId;
  372. }
  373. public int getNpcId()
  374. {
  375. return _npcId;
  376. }
  377. public int getHeading()
  378. {
  379. return _heading;
  380. }
  381. public int getId()
  382. {
  383. return _id;
  384. }
  385. public Location getLocation()
  386. {
  387. return _location;
  388. }
  389. }
  390. private static class SingletonHolder
  391. {
  392. protected static final FortSiegeManager _instance = new FortSiegeManager();
  393. }
  394. }