FortSiegeManager.java 12 KB

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