FortSiegeManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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.CombatFlag;
  31. import net.sf.l2j.gameserver.model.L2Character;
  32. import net.sf.l2j.gameserver.model.L2Clan;
  33. import net.sf.l2j.gameserver.model.L2ItemInstance;
  34. import net.sf.l2j.gameserver.model.L2Object;
  35. import net.sf.l2j.gameserver.model.Location;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  37. import net.sf.l2j.gameserver.model.entity.Fort;
  38. import net.sf.l2j.gameserver.model.entity.FortSiege;
  39. public class FortSiegeManager
  40. {
  41. private static final Logger _log = Logger.getLogger(FortSiegeManager.class.getName());
  42. // =========================================================
  43. private static FortSiegeManager _instance;
  44. public static final FortSiegeManager getInstance()
  45. {
  46. if (_instance == null)
  47. {
  48. _log.info("Initializing SiegeManager");
  49. _instance = new FortSiegeManager();
  50. _instance.load();
  51. }
  52. return _instance;
  53. }
  54. // =========================================================
  55. // Data Field
  56. private int _attackerMaxClans = 500; // Max number of clans
  57. private int _attackerRespawnDelay = 20000; // Time in ms. Changeable in siege.config
  58. private int _defenderMaxClans = 500; // Max number of clans
  59. private int _defenderRespawnDelay = 10000; // Time in ms. Changeable in siege.config
  60. // Fort Siege settings
  61. private FastMap<Integer,FastList<SiegeSpawn>> _commanderSpawnList;
  62. private FastMap<Integer,FastList<CombatFlag>> _flagList;
  63. private int _controlTowerLosePenalty = 20000; // Time in ms. Changeable in siege.config
  64. private int _flagMaxCount = 1; // Changeable in siege.config
  65. private int _siegeClanMinLevel = 4; // Changeable in siege.config
  66. private int _siegeLength = 120; // Time in minute. Changeable in siege.config
  67. private List<FortSiege> _sieges;
  68. // =========================================================
  69. // Constructor
  70. private FortSiegeManager()
  71. {
  72. }
  73. // =========================================================
  74. // Method - Public
  75. public final void addSiegeSkills(L2PcInstance character)
  76. {
  77. character.addSkill(SkillTable.getInstance().getInfo(246, 1), false);
  78. character.addSkill(SkillTable.getInstance().getInfo(247, 1), false);
  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. Fort fort = FortManager.getInstance().getFort(player);
  90. if (fort == null || fort.getFortId() <= 0)
  91. text = "You must be on fort ground to summon this";
  92. else if (!fort.getSiege().getIsInProgress())
  93. text = "You can only summon this during a siege.";
  94. else if (player.getClanId() != 0 && fort.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 fort<BR><BR>
  104. * @param clan The L2Clan of the player
  105. */
  106. public final boolean checkIsRegistered(L2Clan clan, int fortid)
  107. {
  108. if (clan == null) return false;
  109. if (clan.getHasFort() > 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 fortsiege_clans where clan_id=? and fort_id=?");
  116. statement.setInt(1, clan.getClanId());
  117. statement.setInt(2, fortid);
  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. character.removeSkill(SkillTable.getInstance().getInfo(246, 1));
  141. character.removeSkill(SkillTable.getInstance().getInfo(247, 1));
  142. }
  143. // =========================================================
  144. // Method - Private
  145. private final void load()
  146. {
  147. try {
  148. InputStream is = new FileInputStream(new File(Config.FORTSIEGE_CONFIGURATION_FILE));
  149. Properties siegeSettings = new Properties();
  150. siegeSettings.load(is);
  151. is.close();
  152. // Siege setting
  153. _attackerMaxClans = Integer.decode(siegeSettings.getProperty("AttackerMaxClans", "500"));
  154. _attackerRespawnDelay = Integer.decode(siegeSettings.getProperty("AttackerRespawn", "30000"));
  155. _controlTowerLosePenalty = Integer.decode(siegeSettings.getProperty("CTLossPenalty", "20000"));
  156. _defenderMaxClans = Integer.decode(siegeSettings.getProperty("DefenderMaxClans", "500"));
  157. _defenderRespawnDelay = Integer.decode(siegeSettings.getProperty("DefenderRespawn", "20000"));
  158. _flagMaxCount = Integer.decode(siegeSettings.getProperty("MaxFlags", "1"));
  159. _siegeClanMinLevel = Integer.decode(siegeSettings.getProperty("SiegeClanMinLevel", "4"));
  160. _siegeLength = Integer.decode(siegeSettings.getProperty("SiegeLength", "120"));
  161. // Siege spawns settings
  162. _commanderSpawnList = new FastMap<Integer,FastList<SiegeSpawn>>();
  163. _flagList = new FastMap<Integer,FastList<CombatFlag>>();
  164. for (Fort fort: FortManager.getInstance().getForts())
  165. {
  166. FastList<SiegeSpawn> _commanderSpawns = new FastList<SiegeSpawn>();
  167. FastList<CombatFlag> _flagSpawns = new FastList<CombatFlag>();
  168. for (int i=1; i<5; i++)
  169. {
  170. String _spawnParams = siegeSettings.getProperty(fort.getName() + "Commander" + Integer.toString(i), "");
  171. if (_spawnParams.length() == 0) break;
  172. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  173. try
  174. {
  175. int x = Integer.parseInt(st.nextToken());
  176. int y = Integer.parseInt(st.nextToken());
  177. int z = Integer.parseInt(st.nextToken());
  178. int heading = Integer.parseInt(st.nextToken());
  179. int npc_id = Integer.parseInt(st.nextToken());
  180. _commanderSpawns.add(new SiegeSpawn(fort.getFortId(),x,y,z,heading,npc_id));
  181. }
  182. catch (Exception e)
  183. {
  184. _log.warning("Error while loading commander(s) for "+fort.getName()+" fort.");
  185. }
  186. }
  187. _commanderSpawnList.put(fort.getFortId(), _commanderSpawns);
  188. for (int i=1; i<4; i++)
  189. {
  190. String _spawnParams = siegeSettings.getProperty(fort.getName() + "Flag" + Integer.toString(i), "");
  191. if (_spawnParams.length() == 0) break;
  192. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  193. try
  194. {
  195. int x = Integer.parseInt(st.nextToken());
  196. int y = Integer.parseInt(st.nextToken());
  197. int z = Integer.parseInt(st.nextToken());
  198. int flag_id = Integer.parseInt(st.nextToken());
  199. _flagSpawns.add(new CombatFlag(fort.getFortId(),x,y,z,0,flag_id));
  200. }
  201. catch (Exception e)
  202. {
  203. _log.warning("Error while loading flag(s) for "+fort.getName()+" fort.");
  204. }
  205. }
  206. _flagList.put(fort.getFortId(), _flagSpawns);
  207. }
  208. } catch (Exception e) {
  209. //_initialized = false;
  210. System.err.println("Error while loading fortsiege data.");
  211. e.printStackTrace();
  212. }
  213. }
  214. // =========================================================
  215. // Property - Public
  216. public final FastList<SiegeSpawn> getCommanderSpawnList(int _fortId)
  217. {
  218. if (_commanderSpawnList.containsKey(_fortId))
  219. return _commanderSpawnList.get(_fortId);
  220. else
  221. return null;
  222. }
  223. public final FastList<CombatFlag> getFlagList(int _fortId)
  224. {
  225. if (_flagList.containsKey(_fortId))
  226. return _flagList.get(_fortId);
  227. else
  228. return null;
  229. }
  230. public final int getAttackerMaxClans() { return _attackerMaxClans; }
  231. public final int getAttackerRespawnDelay() { return _attackerRespawnDelay; }
  232. public final int getControlTowerLosePenalty() { return _controlTowerLosePenalty; }
  233. public final int getDefenderMaxClans() { return _defenderMaxClans; }
  234. public final int getDefenderRespawnDelay() { return (_defenderRespawnDelay); }
  235. public final int getFlagMaxCount() { return _flagMaxCount; }
  236. public final FortSiege getSiege(L2Object activeObject) { return getSiege(activeObject.getX(), activeObject.getY(), activeObject.getZ()); }
  237. public final FortSiege getSiege(int x, int y, int z)
  238. {
  239. for (Fort fort: FortManager.getInstance().getForts())
  240. if (fort.getSiege().checkIfInZone(x, y, z)) return fort.getSiege();
  241. return null;
  242. }
  243. public final int getSiegeClanMinLevel() { return _siegeClanMinLevel; }
  244. public final int getSiegeLength() { return _siegeLength; }
  245. public final List<FortSiege> getSieges()
  246. {
  247. if (_sieges == null)
  248. _sieges = new FastList<FortSiege>();
  249. return _sieges;
  250. }
  251. public final void addSiege(FortSiege fortSiege)
  252. {
  253. if (_sieges == null)
  254. _sieges = new FastList<FortSiege>();
  255. _sieges.add(fortSiege);
  256. }
  257. public boolean isCombat(int itemId)
  258. {
  259. return ( itemId == 9819);
  260. }
  261. public void activateCombatFlag(L2PcInstance player, L2ItemInstance item)
  262. {
  263. if (!checkIfCanPickup(player))
  264. return;
  265. Fort fort = FortManager.getInstance().getFort(player);
  266. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  267. for ( CombatFlag cf : fcf)
  268. {
  269. if ( cf.itemInstance == item)
  270. {
  271. cf.activate(player, item);
  272. }
  273. }
  274. }
  275. public boolean checkIfCanPickup(L2PcInstance player)
  276. {
  277. // Cannot own 2 combat flag
  278. if (player.isCombatFlagEquipped())
  279. {
  280. player.sendMessage("You already have the combat flag");
  281. return false;
  282. }
  283. // here check if is siege is in progress
  284. // here check if is siege is attacker
  285. Fort fort = FortManager.getInstance().getFort(player);
  286. if (fort == null || fort.getFortId() <= 0)
  287. {
  288. player.sendMessage("You must be on fort ground to pickup Combat Flag");
  289. return false;
  290. }
  291. else if (!fort.getSiege().getIsInProgress())
  292. {
  293. player.sendMessage("You can only pickup Combat Flag during a siege.");
  294. return false;
  295. }
  296. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  297. {
  298. player.sendMessage("You must be an attacker to pickup Combat Flag");
  299. return false;
  300. }
  301. return true;
  302. }
  303. public void dropCombatFlag(L2PcInstance player)
  304. {
  305. System.out.println("Player obj " + player.getObjectId());
  306. Fort fort = FortManager.getInstance().getFort(player);
  307. System.out.println("Fort " + fort.getName());
  308. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  309. System.out.println("fast list size " + fcf.size());
  310. for ( CombatFlag cf : fcf)
  311. {
  312. if ( cf.playerId == player.getObjectId())
  313. {
  314. System.out.println("found cf ");
  315. cf.dropIt();
  316. cf.spawnMe();
  317. }
  318. }
  319. }
  320. public class SiegeSpawn
  321. {
  322. Location _location;
  323. private int _npcId;
  324. private int _heading;
  325. private int _fortId;
  326. private int _hp;
  327. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id)
  328. {
  329. _fortId = fort_id;
  330. _location = new Location(x,y,z,heading);
  331. _heading = heading;
  332. _npcId = npc_id;
  333. }
  334. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id, int hp)
  335. {
  336. _fortId = fort_id;
  337. _location = new Location(x,y,z,heading);
  338. _heading = heading;
  339. _npcId = npc_id;
  340. _hp = hp;
  341. }
  342. public int getFortId()
  343. {
  344. return _fortId;
  345. }
  346. public int getNpcId()
  347. {
  348. return _npcId;
  349. }
  350. public int getHeading()
  351. {
  352. return _heading;
  353. }
  354. public int getHp()
  355. {
  356. return _hp;
  357. }
  358. public Location getLocation()
  359. {
  360. return _location;
  361. }
  362. }
  363. }