SiegeManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 com.l2jserver.Config;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.datatables.SkillTable;
  31. import com.l2jserver.gameserver.model.L2Clan;
  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.Castle;
  37. import com.l2jserver.gameserver.model.entity.Siege;
  38. import com.l2jserver.gameserver.model.skills.L2Skill;
  39. import gnu.trove.map.hash.TIntObjectHashMap;
  40. public class SiegeManager
  41. {
  42. private static final Logger _log = Logger.getLogger(SiegeManager.class.getName());
  43. private int _attackerMaxClans = 500; // Max number of clans
  44. private int _attackerRespawnDelay = 0; // Time in ms. Changeable in siege.config
  45. private int _defenderMaxClans = 500; // Max number of clans
  46. private TIntObjectHashMap<FastList<SiegeSpawn>> _artefactSpawnList;
  47. private TIntObjectHashMap<FastList<SiegeSpawn>> _controlTowerSpawnList;
  48. private TIntObjectHashMap<FastList<SiegeSpawn>> _flameTowerSpawnList;
  49. private int _flagMaxCount = 1; // Changeable in siege.config
  50. private int _siegeClanMinLevel = 5; // Changeable in siege.config
  51. private int _siegeLength = 120; // Time in minute. Changeable in siege.config
  52. private int _bloodAllianceReward = 0; // Number of Blood Alliance items reward for successful castle defending
  53. protected SiegeManager()
  54. {
  55. load();
  56. }
  57. public final void addSiegeSkills(L2PcInstance character)
  58. {
  59. for (L2Skill sk : SkillTable.getInstance().getSiegeSkills(character.isNoble(), character.getClan().getCastleId() > 0))
  60. {
  61. character.addSkill(sk, false);
  62. }
  63. }
  64. /**
  65. * @param activeChar The L2Character of the character can summon
  66. * @param isCheckOnly
  67. * @return true if character summon
  68. */
  69. public final boolean checkIfOkToSummon(L2Character activeChar, boolean isCheckOnly)
  70. {
  71. if (!(activeChar instanceof L2PcInstance))
  72. {
  73. return false;
  74. }
  75. String text = "";
  76. L2PcInstance player = (L2PcInstance) activeChar;
  77. Castle castle = CastleManager.getInstance().getCastle(player);
  78. if ((castle == null) || (castle.getCastleId() <= 0))
  79. {
  80. text = "You must be on castle ground to summon this";
  81. }
  82. else if (!castle.getSiege().getIsInProgress())
  83. {
  84. text = "You can only summon this during a siege.";
  85. }
  86. else if ((player.getClanId() != 0) && (castle.getSiege().getAttackerClan(player.getClanId()) == null))
  87. {
  88. text = "You can only summon this as a registered attacker.";
  89. }
  90. else
  91. {
  92. return true;
  93. }
  94. if (!isCheckOnly)
  95. {
  96. player.sendMessage(text);
  97. }
  98. return false;
  99. }
  100. /**
  101. * @param clan The L2Clan of the player
  102. * @param castleid
  103. * @return true if the clan is registered or owner of a castle
  104. */
  105. public final boolean checkIsRegistered(L2Clan clan, int castleid)
  106. {
  107. if (clan == null)
  108. {
  109. return false;
  110. }
  111. if (clan.getCastleId() > 0)
  112. {
  113. return true;
  114. }
  115. boolean register = false;
  116. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  117. PreparedStatement statement = con.prepareStatement("SELECT clan_id FROM siege_clans where clan_id=? and castle_id=?"))
  118. {
  119. statement.setInt(1, clan.getClanId());
  120. statement.setInt(2, castleid);
  121. try (ResultSet rs = statement.executeQuery())
  122. {
  123. while (rs.next())
  124. {
  125. register = true;
  126. break;
  127. }
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. _log.log(Level.WARNING, getClass().getSimpleName() + ": Exception: checkIsRegistered(): " + e.getMessage(), e);
  133. }
  134. return register;
  135. }
  136. public final void removeSiegeSkills(L2PcInstance character)
  137. {
  138. for (L2Skill sk : SkillTable.getInstance().getSiegeSkills(character.isNoble(), character.getClan().getCastleId() > 0))
  139. {
  140. character.removeSkill(sk);
  141. }
  142. }
  143. private final void load()
  144. {
  145. Properties siegeSettings = new Properties();
  146. final File file = new File(Config.SIEGE_CONFIGURATION_FILE);
  147. try (InputStream is = new FileInputStream(file))
  148. {
  149. siegeSettings.load(is);
  150. }
  151. catch (Exception e)
  152. {
  153. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while loading Territory War Manager settings!", e);
  154. }
  155. // Siege setting
  156. _attackerMaxClans = Integer.decode(siegeSettings.getProperty("AttackerMaxClans", "500"));
  157. _attackerRespawnDelay = Integer.decode(siegeSettings.getProperty("AttackerRespawn", "0"));
  158. _defenderMaxClans = Integer.decode(siegeSettings.getProperty("DefenderMaxClans", "500"));
  159. _flagMaxCount = Integer.decode(siegeSettings.getProperty("MaxFlags", "1"));
  160. _siegeClanMinLevel = Integer.decode(siegeSettings.getProperty("SiegeClanMinLevel", "5"));
  161. _siegeLength = Integer.decode(siegeSettings.getProperty("SiegeLength", "120"));
  162. _bloodAllianceReward = Integer.decode(siegeSettings.getProperty("BloodAllianceReward", "0"));
  163. // Siege spawns settings
  164. _controlTowerSpawnList = new TIntObjectHashMap<>();
  165. _artefactSpawnList = new TIntObjectHashMap<>();
  166. _flameTowerSpawnList = new TIntObjectHashMap<>();
  167. for (Castle castle : CastleManager.getInstance().getCastles())
  168. {
  169. FastList<SiegeSpawn> _controlTowersSpawns = new FastList<>();
  170. for (int i = 1; i < 0xFF; i++)
  171. {
  172. String _spawnParams = siegeSettings.getProperty(castle.getName() + "ControlTower" + i, "");
  173. if (_spawnParams.isEmpty())
  174. {
  175. break;
  176. }
  177. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  178. try
  179. {
  180. int x = Integer.parseInt(st.nextToken());
  181. int y = Integer.parseInt(st.nextToken());
  182. int z = Integer.parseInt(st.nextToken());
  183. int npc_id = Integer.parseInt(st.nextToken());
  184. int hp = Integer.parseInt(st.nextToken());
  185. _controlTowersSpawns.add(new SiegeSpawn(castle.getCastleId(), x, y, z, 0, npc_id, hp));
  186. }
  187. catch (Exception e)
  188. {
  189. _log.warning(getClass().getSimpleName() + ": Error while loading control tower(s) for " + castle.getName() + " castle.");
  190. }
  191. }
  192. FastList<SiegeSpawn> _flameTowersSpawns = new FastList<>();
  193. for (int i = 1; i < 0xFF; i++)
  194. {
  195. String _spawnParams = siegeSettings.getProperty(castle.getName() + "FlameTower" + i, "");
  196. if (_spawnParams.isEmpty())
  197. {
  198. break;
  199. }
  200. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  201. try
  202. {
  203. int x = Integer.parseInt(st.nextToken());
  204. int y = Integer.parseInt(st.nextToken());
  205. int z = Integer.parseInt(st.nextToken());
  206. int npc_id = Integer.parseInt(st.nextToken());
  207. int hp = Integer.parseInt(st.nextToken());
  208. _flameTowersSpawns.add(new SiegeSpawn(castle.getCastleId(), x, y, z, 0, npc_id, hp));
  209. }
  210. catch (Exception e)
  211. {
  212. _log.warning(getClass().getSimpleName() + ": Error while loading artefact(s) for " + castle.getName() + " castle.");
  213. }
  214. }
  215. FastList<SiegeSpawn> _artefactSpawns = new FastList<>();
  216. for (int i = 1; i < 0xFF; i++)
  217. {
  218. String _spawnParams = siegeSettings.getProperty(castle.getName() + "Artefact" + i, "");
  219. if (_spawnParams.isEmpty())
  220. {
  221. break;
  222. }
  223. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  224. try
  225. {
  226. int x = Integer.parseInt(st.nextToken());
  227. int y = Integer.parseInt(st.nextToken());
  228. int z = Integer.parseInt(st.nextToken());
  229. int heading = Integer.parseInt(st.nextToken());
  230. int npc_id = Integer.parseInt(st.nextToken());
  231. _artefactSpawns.add(new SiegeSpawn(castle.getCastleId(), x, y, z, heading, npc_id));
  232. }
  233. catch (Exception e)
  234. {
  235. _log.warning(getClass().getSimpleName() + ": Error while loading artefact(s) for " + castle.getName() + " castle.");
  236. }
  237. }
  238. MercTicketManager.MERCS_MAX_PER_CASTLE[castle.getCastleId() - 1] = Integer.parseInt(siegeSettings.getProperty(castle.getName() + "MaxMercenaries", Integer.toString(MercTicketManager.MERCS_MAX_PER_CASTLE[castle.getCastleId() - 1])).trim());
  239. _controlTowerSpawnList.put(castle.getCastleId(), _controlTowersSpawns);
  240. _artefactSpawnList.put(castle.getCastleId(), _artefactSpawns);
  241. _flameTowerSpawnList.put(castle.getCastleId(), _flameTowersSpawns);
  242. }
  243. }
  244. public final FastList<SiegeSpawn> getArtefactSpawnList(int _castleId)
  245. {
  246. return _artefactSpawnList.get(_castleId);
  247. }
  248. public final FastList<SiegeSpawn> getControlTowerSpawnList(int _castleId)
  249. {
  250. return _controlTowerSpawnList.get(_castleId);
  251. }
  252. public final FastList<SiegeSpawn> getFlameTowerSpawnList(int _castleId)
  253. {
  254. return _flameTowerSpawnList.get(_castleId);
  255. }
  256. public final int getAttackerMaxClans()
  257. {
  258. return _attackerMaxClans;
  259. }
  260. public final int getAttackerRespawnDelay()
  261. {
  262. return _attackerRespawnDelay;
  263. }
  264. public final int getDefenderMaxClans()
  265. {
  266. return _defenderMaxClans;
  267. }
  268. public final int getFlagMaxCount()
  269. {
  270. return _flagMaxCount;
  271. }
  272. public final Siege getSiege(L2Object activeObject)
  273. {
  274. return getSiege(activeObject.getX(), activeObject.getY(), activeObject.getZ());
  275. }
  276. public final Siege getSiege(int x, int y, int z)
  277. {
  278. for (Castle castle : CastleManager.getInstance().getCastles())
  279. {
  280. if (castle.getSiege().checkIfInZone(x, y, z))
  281. {
  282. return castle.getSiege();
  283. }
  284. }
  285. return null;
  286. }
  287. public final int getSiegeClanMinLevel()
  288. {
  289. return _siegeClanMinLevel;
  290. }
  291. public final int getSiegeLength()
  292. {
  293. return _siegeLength;
  294. }
  295. public final int getBloodAllianceReward()
  296. {
  297. return _bloodAllianceReward;
  298. }
  299. public final List<Siege> getSieges()
  300. {
  301. FastList<Siege> sieges = new FastList<>();
  302. for (Castle castle : CastleManager.getInstance().getCastles())
  303. {
  304. sieges.add(castle.getSiege());
  305. }
  306. return sieges;
  307. }
  308. public static class SiegeSpawn
  309. {
  310. Location _location;
  311. private final int _npcId;
  312. private final int _heading;
  313. private final int _castleId;
  314. private int _hp;
  315. public SiegeSpawn(int castle_id, int x, int y, int z, int heading, int npc_id)
  316. {
  317. _castleId = castle_id;
  318. _location = new Location(x, y, z, heading);
  319. _heading = heading;
  320. _npcId = npc_id;
  321. }
  322. public SiegeSpawn(int castle_id, int x, int y, int z, int heading, int npc_id, int hp)
  323. {
  324. _castleId = castle_id;
  325. _location = new Location(x, y, z, heading);
  326. _heading = heading;
  327. _npcId = npc_id;
  328. _hp = hp;
  329. }
  330. public int getCastleId()
  331. {
  332. return _castleId;
  333. }
  334. public int getNpcId()
  335. {
  336. return _npcId;
  337. }
  338. public int getHeading()
  339. {
  340. return _heading;
  341. }
  342. public int getHp()
  343. {
  344. return _hp;
  345. }
  346. public Location getLocation()
  347. {
  348. return _location;
  349. }
  350. }
  351. public static final SiegeManager getInstance()
  352. {
  353. return SingletonHolder._instance;
  354. }
  355. private static class SingletonHolder
  356. {
  357. protected static final SiegeManager _instance = new SiegeManager();
  358. }
  359. }