SiegeManager.java 12 KB

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