2
0

SiegeManager.java 12 KB

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