2
0

FortSiegeManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. java.sql.Connection con = null;
  110. boolean register = false;
  111. try
  112. {
  113. con = L2DatabaseFactory.getInstance().getConnection();
  114. PreparedStatement statement = con.prepareStatement("SELECT clan_id FROM fortsiege_clans where clan_id=? and fort_id=?");
  115. statement.setInt(1, clan.getClanId());
  116. statement.setInt(2, fortid);
  117. ResultSet rs = statement.executeQuery();
  118. while (rs.next())
  119. {
  120. register = true;
  121. break;
  122. }
  123. rs.close();
  124. statement.close();
  125. }
  126. catch (Exception e)
  127. {
  128. _log.warning("Exception: checkIsRegistered(): " + e.getMessage());
  129. e.printStackTrace();
  130. }
  131. finally
  132. {
  133. try { con.close(); } catch (Exception e) {}
  134. }
  135. return register;
  136. }
  137. public final void removeSiegeSkills(L2PcInstance character)
  138. {
  139. character.removeSkill(SkillTable.getInstance().getInfo(246, 1));
  140. character.removeSkill(SkillTable.getInstance().getInfo(247, 1));
  141. }
  142. // =========================================================
  143. // Method - Private
  144. private final void load()
  145. {
  146. try {
  147. InputStream is = new FileInputStream(new File(Config.FORTSIEGE_CONFIGURATION_FILE));
  148. Properties siegeSettings = new Properties();
  149. siegeSettings.load(is);
  150. is.close();
  151. // Siege setting
  152. _attackerMaxClans = Integer.decode(siegeSettings.getProperty("AttackerMaxClans", "500"));
  153. _attackerRespawnDelay = Integer.decode(siegeSettings.getProperty("AttackerRespawn", "30000"));
  154. _controlTowerLosePenalty = Integer.decode(siegeSettings.getProperty("CTLossPenalty", "20000"));
  155. _defenderMaxClans = Integer.decode(siegeSettings.getProperty("DefenderMaxClans", "500"));
  156. _defenderRespawnDelay = Integer.decode(siegeSettings.getProperty("DefenderRespawn", "20000"));
  157. _flagMaxCount = Integer.decode(siegeSettings.getProperty("MaxFlags", "1"));
  158. _siegeClanMinLevel = Integer.decode(siegeSettings.getProperty("SiegeClanMinLevel", "4"));
  159. _siegeLength = Integer.decode(siegeSettings.getProperty("SiegeLength", "120"));
  160. // Siege spawns settings
  161. _commanderSpawnList = new FastMap<Integer,FastList<SiegeSpawn>>();
  162. _flagList = new FastMap<Integer,FastList<CombatFlag>>();
  163. for (Fort fort: FortManager.getInstance().getForts())
  164. {
  165. FastList<SiegeSpawn> _commanderSpawns = new FastList<SiegeSpawn>();
  166. FastList<CombatFlag> _flagSpawns = new FastList<CombatFlag>();
  167. for (int i=1; i<5; i++)
  168. {
  169. String _spawnParams = siegeSettings.getProperty(fort.getName() + "Commander" + Integer.toString(i), "");
  170. if (_spawnParams.length() == 0) break;
  171. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  172. try
  173. {
  174. int x = Integer.parseInt(st.nextToken());
  175. int y = Integer.parseInt(st.nextToken());
  176. int z = Integer.parseInt(st.nextToken());
  177. int heading = Integer.parseInt(st.nextToken());
  178. int npc_id = Integer.parseInt(st.nextToken());
  179. _commanderSpawns.add(new SiegeSpawn(fort.getFortId(),x,y,z,heading,npc_id));
  180. }
  181. catch (Exception e)
  182. {
  183. _log.warning("Error while loading commander(s) for "+fort.getName()+" fort.");
  184. }
  185. }
  186. _commanderSpawnList.put(fort.getFortId(), _commanderSpawns);
  187. for (int i=1; i<4; i++)
  188. {
  189. String _spawnParams = siegeSettings.getProperty(fort.getName() + "Flag" + Integer.toString(i), "");
  190. if (_spawnParams.length() == 0) break;
  191. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  192. try
  193. {
  194. int x = Integer.parseInt(st.nextToken());
  195. int y = Integer.parseInt(st.nextToken());
  196. int z = Integer.parseInt(st.nextToken());
  197. int flag_id = Integer.parseInt(st.nextToken());
  198. _flagSpawns.add(new CombatFlag(fort.getFortId(),x,y,z,0,flag_id));
  199. }
  200. catch (Exception e)
  201. {
  202. _log.warning("Error while loading flag(s) for "+fort.getName()+" fort.");
  203. }
  204. }
  205. _flagList.put(fort.getFortId(), _flagSpawns);
  206. }
  207. } catch (Exception e) {
  208. //_initialized = false;
  209. System.err.println("Error while loading fortsiege data.");
  210. e.printStackTrace();
  211. }
  212. }
  213. // =========================================================
  214. // Property - Public
  215. public final FastList<SiegeSpawn> getCommanderSpawnList(int _fortId)
  216. {
  217. if (_commanderSpawnList.containsKey(_fortId))
  218. return _commanderSpawnList.get(_fortId);
  219. else
  220. return null;
  221. }
  222. public final FastList<CombatFlag> getFlagList(int _fortId)
  223. {
  224. if (_flagList.containsKey(_fortId))
  225. return _flagList.get(_fortId);
  226. else
  227. return null;
  228. }
  229. public final int getAttackerMaxClans() { return _attackerMaxClans; }
  230. public final int getAttackerRespawnDelay() { return _attackerRespawnDelay; }
  231. public final int getControlTowerLosePenalty() { return _controlTowerLosePenalty; }
  232. public final int getDefenderMaxClans() { return _defenderMaxClans; }
  233. public final int getDefenderRespawnDelay() { return (_defenderRespawnDelay); }
  234. public final int getFlagMaxCount() { return _flagMaxCount; }
  235. public final FortSiege getSiege(L2Object activeObject) { return getSiege(activeObject.getX(), activeObject.getY(), activeObject.getZ()); }
  236. public final FortSiege getSiege(int x, int y, int z)
  237. {
  238. for (Fort fort: FortManager.getInstance().getForts())
  239. if (fort.getSiege().checkIfInZone(x, y, z)) return fort.getSiege();
  240. return null;
  241. }
  242. public final int getSiegeClanMinLevel() { return _siegeClanMinLevel; }
  243. public final int getSiegeLength() { return _siegeLength; }
  244. public final List<FortSiege> getSieges()
  245. {
  246. if (_sieges == null)
  247. _sieges = new FastList<FortSiege>();
  248. return _sieges;
  249. }
  250. public final void addSiege(FortSiege fortSiege)
  251. {
  252. if (_sieges == null)
  253. _sieges = new FastList<FortSiege>();
  254. _sieges.add(fortSiege);
  255. }
  256. public boolean isCombat(int itemId)
  257. {
  258. return ( itemId == 9819);
  259. }
  260. public void activateCombatFlag(L2PcInstance player, L2ItemInstance item)
  261. {
  262. if (!checkIfCanPickup(player))
  263. return;
  264. Fort fort = FortManager.getInstance().getFort(player);
  265. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  266. for ( CombatFlag cf : fcf)
  267. {
  268. if ( cf.itemInstance == item)
  269. {
  270. cf.activate(player, item);
  271. }
  272. }
  273. }
  274. public boolean checkIfCanPickup(L2PcInstance player)
  275. {
  276. // Cannot own 2 combat flag
  277. if (player.isCombatFlagEquipped())
  278. {
  279. player.sendMessage("You already have the combat flag");
  280. return false;
  281. }
  282. // here check if is siege is in progress
  283. // here check if is siege is attacker
  284. Fort fort = FortManager.getInstance().getFort(player);
  285. if (fort == null || fort.getFortId() <= 0)
  286. {
  287. player.sendMessage("You must be on fort ground to pickup Combat Flag");
  288. return false;
  289. }
  290. else if (!fort.getSiege().getIsInProgress())
  291. {
  292. player.sendMessage("You can only pickup Combat Flag during a siege.");
  293. return false;
  294. }
  295. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  296. {
  297. player.sendMessage("You must be an attacker to pickup Combat Flag");
  298. return false;
  299. }
  300. return true;
  301. }
  302. public void dropCombatFlag(L2PcInstance player)
  303. {
  304. System.out.println("Player obj " + player.getObjectId());
  305. Fort fort = FortManager.getInstance().getFort(player);
  306. System.out.println("Fort " + fort.getName());
  307. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  308. System.out.println("fast list size " + fcf.size());
  309. for ( CombatFlag cf : fcf)
  310. {
  311. if ( cf.playerId == player.getObjectId())
  312. {
  313. System.out.println("found cf ");
  314. cf.dropIt();
  315. cf.spawnMe();
  316. }
  317. }
  318. }
  319. public class SiegeSpawn
  320. {
  321. Location _location;
  322. private int _npcId;
  323. private int _heading;
  324. private int _fortId;
  325. private int _hp;
  326. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id)
  327. {
  328. _fortId = fort_id;
  329. _location = new Location(x,y,z,heading);
  330. _heading = heading;
  331. _npcId = npc_id;
  332. }
  333. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id, int hp)
  334. {
  335. _fortId = fort_id;
  336. _location = new Location(x,y,z,heading);
  337. _heading = heading;
  338. _npcId = npc_id;
  339. _hp = hp;
  340. }
  341. public int getFortId()
  342. {
  343. return _fortId;
  344. }
  345. public int getNpcId()
  346. {
  347. return _npcId;
  348. }
  349. public int getHeading()
  350. {
  351. return _heading;
  352. }
  353. public int getHp()
  354. {
  355. return _hp;
  356. }
  357. public Location getLocation()
  358. {
  359. return _location;
  360. }
  361. }
  362. }