2
0

SiegeManager.java 13 KB

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