SiegeManager.java 11 KB

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