SiegeManager.java 12 KB

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