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. import net.sf.l2j.gameserver.network.SystemMessageId;
  40. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  41. public class FortSiegeManager
  42. {
  43. private static final Logger _log = Logger.getLogger(FortSiegeManager.class.getName());
  44. // =========================================================
  45. private static FortSiegeManager _instance;
  46. public static final FortSiegeManager getInstance()
  47. {
  48. if (_instance == null)
  49. {
  50. _log.info("Initializing FortSiegeManager");
  51. _instance = new FortSiegeManager();
  52. _instance.load();
  53. }
  54. return _instance;
  55. }
  56. // =========================================================
  57. // Data Field
  58. private int _attackerMaxClans = 500; // Max number of clans
  59. // Fort Siege settings
  60. private FastMap<Integer,FastList<SiegeSpawn>> _commanderSpawnList;
  61. private FastMap<Integer,FastList<CombatFlag>> _flagList;
  62. private int _flagMaxCount = 1; // Changeable in fortsiege.properties
  63. private int _siegeClanMinLevel = 4; // Changeable in fortsiege.properties
  64. private int _siegeLength = 60; // Time in minute. Changeable in fortsiege.properties
  65. private int _countDownLength = 10; // Time in minute. Changeable in fortsiege.properties
  66. private int _suspiciousMerchantRespawnDelay = 180; // Time in minute. Changeable in fortsiege.properties
  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 {
  134. con.close();
  135. }
  136. catch (Exception e)
  137. {
  138. _log.warning(""+e.getMessage());
  139. e.printStackTrace();
  140. }
  141. }
  142. return register;
  143. }
  144. public final void removeSiegeSkills(L2PcInstance character)
  145. {
  146. character.removeSkill(SkillTable.getInstance().getInfo(246, 1));
  147. character.removeSkill(SkillTable.getInstance().getInfo(247, 1));
  148. }
  149. // =========================================================
  150. // Method - Private
  151. private final void load()
  152. {
  153. InputStream is = null;
  154. try {
  155. is = new FileInputStream(new File(Config.FORTSIEGE_CONFIGURATION_FILE));
  156. Properties siegeSettings = new Properties();
  157. siegeSettings.load(is);
  158. // Siege setting
  159. _attackerMaxClans = Integer.decode(siegeSettings.getProperty("AttackerMaxClans", "500"));
  160. _flagMaxCount = Integer.decode(siegeSettings.getProperty("MaxFlags", "1"));
  161. _siegeClanMinLevel = Integer.decode(siegeSettings.getProperty("SiegeClanMinLevel", "4"));
  162. _siegeLength = Integer.decode(siegeSettings.getProperty("SiegeLength", "60"));
  163. _countDownLength = Integer.decode(siegeSettings.getProperty("CountDownLength", "10"));
  164. _suspiciousMerchantRespawnDelay = Integer.decode(siegeSettings.getProperty("SuspiciousMerchantRespawnDelay", "180"));
  165. // Siege spawns settings
  166. _commanderSpawnList = new FastMap<Integer,FastList<SiegeSpawn>>();
  167. _flagList = new FastMap<Integer,FastList<CombatFlag>>();
  168. for (Fort fort: FortManager.getInstance().getForts())
  169. {
  170. FastList<SiegeSpawn> _commanderSpawns = new FastList<SiegeSpawn>();
  171. FastList<CombatFlag> _flagSpawns = new FastList<CombatFlag>();
  172. for (int i=1; i<5; i++)
  173. {
  174. String _spawnParams = siegeSettings.getProperty(fort.getName() + "Commander" + Integer.toString(i), "");
  175. if (_spawnParams.length() == 0) break;
  176. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  177. try
  178. {
  179. int x = Integer.parseInt(st.nextToken());
  180. int y = Integer.parseInt(st.nextToken());
  181. int z = Integer.parseInt(st.nextToken());
  182. int heading = Integer.parseInt(st.nextToken());
  183. int npc_id = Integer.parseInt(st.nextToken());
  184. _commanderSpawns.add(new SiegeSpawn(fort.getFortId(),x,y,z,heading,npc_id,i));
  185. }
  186. catch (Exception e)
  187. {
  188. _log.warning("Error while loading commander(s) for "+fort.getName()+" fort.");
  189. }
  190. }
  191. _commanderSpawnList.put(fort.getFortId(), _commanderSpawns);
  192. for (int i=1; i<4; i++)
  193. {
  194. String _spawnParams = siegeSettings.getProperty(fort.getName() + "Flag" + Integer.toString(i), "");
  195. if (_spawnParams.length() == 0) break;
  196. StringTokenizer st = new StringTokenizer(_spawnParams.trim(), ",");
  197. try
  198. {
  199. int x = Integer.parseInt(st.nextToken());
  200. int y = Integer.parseInt(st.nextToken());
  201. int z = Integer.parseInt(st.nextToken());
  202. int flag_id = Integer.parseInt(st.nextToken());
  203. _flagSpawns.add(new CombatFlag(fort.getFortId(),x,y,z,0,flag_id));
  204. }
  205. catch (Exception e)
  206. {
  207. _log.warning("Error while loading flag(s) for "+fort.getName()+" fort.");
  208. }
  209. }
  210. _flagList.put(fort.getFortId(), _flagSpawns);
  211. }
  212. }
  213. catch (Exception e)
  214. {
  215. //_initialized = false;
  216. _log.warning("Error while loading fortsiege data."+e.getMessage());
  217. e.printStackTrace();
  218. }
  219. finally
  220. {
  221. try
  222. {
  223. is.close();
  224. }
  225. catch (Exception e)
  226. {
  227. _log.warning(""+e.getMessage());
  228. e.printStackTrace();
  229. }
  230. }
  231. }
  232. // =========================================================
  233. // Property - Public
  234. public final FastList<SiegeSpawn> getCommanderSpawnList(int _fortId)
  235. {
  236. if (_commanderSpawnList.containsKey(_fortId))
  237. {
  238. return _commanderSpawnList.get(_fortId);
  239. }
  240. else
  241. {
  242. return null;
  243. }
  244. }
  245. public final FastList<CombatFlag> getFlagList(int _fortId)
  246. {
  247. if (_flagList.containsKey(_fortId))
  248. return _flagList.get(_fortId);
  249. else
  250. return null;
  251. }
  252. public final int getAttackerMaxClans() { return _attackerMaxClans; }
  253. public final int getFlagMaxCount() { return _flagMaxCount; }
  254. public final int getSuspiciousMerchantRespawnDelay() { return _suspiciousMerchantRespawnDelay; }
  255. public final FortSiege getSiege(L2Object activeObject) { return getSiege(activeObject.getX(), activeObject.getY(), activeObject.getZ()); }
  256. public final FortSiege getSiege(int x, int y, int z)
  257. {
  258. for (Fort fort: FortManager.getInstance().getForts())
  259. if (fort.getSiege().checkIfInZone(x, y, z)) return fort.getSiege();
  260. return null;
  261. }
  262. public final int getSiegeClanMinLevel() { return _siegeClanMinLevel; }
  263. public final int getSiegeLength() { return _siegeLength; }
  264. public final int getCountDownLength() { return _countDownLength; }
  265. public final List<FortSiege> getSieges()
  266. {
  267. if (_sieges == null)
  268. _sieges = new FastList<FortSiege>();
  269. return _sieges;
  270. }
  271. public final void addSiege(FortSiege fortSiege)
  272. {
  273. if (_sieges == null)
  274. _sieges = new FastList<FortSiege>();
  275. _sieges.add(fortSiege);
  276. }
  277. public boolean isCombat(int itemId)
  278. {
  279. return ( itemId == 9819);
  280. }
  281. public boolean activateCombatFlag(L2PcInstance player, L2ItemInstance item)
  282. {
  283. if (!checkIfCanPickup(player))
  284. return false;
  285. Fort fort = FortManager.getInstance().getFort(player);
  286. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  287. for ( CombatFlag cf : fcf)
  288. {
  289. if ( cf.itemInstance == item)
  290. {
  291. cf.activate(player, item);
  292. }
  293. }
  294. return true;
  295. }
  296. public boolean checkIfCanPickup(L2PcInstance player)
  297. {
  298. SystemMessage sm;
  299. sm = new SystemMessage(SystemMessageId.THE_FORTRESS_BATTLE_OF_S1_HAS_FINISHED);
  300. sm.addItemName(9819);
  301. // Cannot own 2 combat flag
  302. if (player.isCombatFlagEquipped())
  303. {
  304. player.sendPacket(sm);
  305. return false;
  306. }
  307. // here check if is siege is in progress
  308. // here check if is siege is attacker
  309. Fort fort = FortManager.getInstance().getFort(player);
  310. if (fort == null || fort.getFortId() <= 0)
  311. {
  312. player.sendPacket(sm);
  313. return false;
  314. }
  315. else if (!fort.getSiege().getIsInProgress())
  316. {
  317. player.sendPacket(sm);
  318. return false;
  319. }
  320. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  321. {
  322. player.sendPacket(sm);
  323. return false;
  324. }
  325. return true;
  326. }
  327. public void dropCombatFlag(L2PcInstance player)
  328. {
  329. Fort fort = FortManager.getInstance().getFort(player);
  330. FastList<CombatFlag> fcf = _flagList.get(fort.getFortId());
  331. for ( CombatFlag cf : fcf)
  332. {
  333. if ( cf.playerId == player.getObjectId())
  334. {
  335. cf.dropIt();
  336. if (fort.getSiege().getIsInProgress())
  337. cf.spawnMe();
  338. }
  339. }
  340. }
  341. public class SiegeSpawn
  342. {
  343. Location _location;
  344. private int _npcId;
  345. private int _heading;
  346. private int _fortId;
  347. private int _id;
  348. public SiegeSpawn(int fort_id, int x, int y, int z, int heading, int npc_id, int id)
  349. {
  350. _fortId = fort_id;
  351. _location = new Location(x,y,z,heading);
  352. _heading = heading;
  353. _npcId = npc_id;
  354. _id = id;
  355. }
  356. public int getFortId()
  357. {
  358. return _fortId;
  359. }
  360. public int getNpcId()
  361. {
  362. return _npcId;
  363. }
  364. public int getHeading()
  365. {
  366. return _heading;
  367. }
  368. public int getId()
  369. {
  370. return _id;
  371. }
  372. public Location getLocation()
  373. {
  374. return _location;
  375. }
  376. }
  377. }