SiegeManager.java 13 KB

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