TerritoryWarManager.java 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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 com.l2jserver.gameserver.instancemanager;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.InputStream;
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.util.Calendar;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.ScheduledFuture;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastList;
  30. import javolution.util.FastMap;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.Announcements;
  34. import com.l2jserver.gameserver.ThreadPoolManager;
  35. import com.l2jserver.gameserver.datatables.ClanTable;
  36. import com.l2jserver.gameserver.datatables.NpcTable;
  37. import com.l2jserver.gameserver.datatables.SkillTable;
  38. import com.l2jserver.gameserver.datatables.SkillTreesData;
  39. import com.l2jserver.gameserver.model.L2Clan;
  40. import com.l2jserver.gameserver.model.L2SiegeClan;
  41. import com.l2jserver.gameserver.model.L2Skill;
  42. import com.l2jserver.gameserver.model.L2SkillLearn;
  43. import com.l2jserver.gameserver.model.L2Spawn;
  44. import com.l2jserver.gameserver.model.L2World;
  45. import com.l2jserver.gameserver.model.Location;
  46. import com.l2jserver.gameserver.model.TerritoryWard;
  47. import com.l2jserver.gameserver.model.actor.L2Npc;
  48. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  49. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  50. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  51. import com.l2jserver.gameserver.model.entity.Castle;
  52. import com.l2jserver.gameserver.model.entity.Fort;
  53. import com.l2jserver.gameserver.model.entity.Siegable;
  54. import com.l2jserver.gameserver.model.quest.Quest;
  55. import com.l2jserver.gameserver.network.SystemMessageId;
  56. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  57. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  58. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  59. import com.l2jserver.gameserver.util.Util;
  60. import com.l2jserver.util.L2Properties;
  61. public class TerritoryWarManager implements Siegable
  62. {
  63. private static final Logger _log = Logger.getLogger(TerritoryWarManager.class.getName());
  64. public static final TerritoryWarManager getInstance()
  65. {
  66. return SingletonHolder._instance;
  67. }
  68. // =========================================================
  69. // Data Field
  70. public static String qn = "TerritoryWarSuperClass";
  71. public static int DEFENDERMAXCLANS; // Max number of clans
  72. public static int DEFENDERMAXPLAYERS; // Max number of individual player
  73. public static int CLANMINLEVEL;
  74. public static int PLAYERMINLEVEL;
  75. public static int MINTWBADGEFORNOBLESS;
  76. public static int MINTWBADGEFORSTRIDERS;
  77. public static int MINTWBADGEFORBIGSTRIDER;
  78. public static Long WARLENGTH;
  79. public static boolean PLAYER_WITH_WARD_CAN_BE_KILLED_IN_PEACEZONE;
  80. public static boolean SPAWN_WARDS_WHEN_TW_IS_NOT_IN_PROGRESS;
  81. public static boolean RETURN_WARDS_WHEN_TW_STARTS;
  82. public final Map<Integer,Integer> TERRITORY_ITEM_IDS;
  83. // Territory War settings
  84. private FastMap<Integer, FastList<L2Clan>> _registeredClans;
  85. private FastMap<Integer, FastList<Integer>> _registeredMercenaries;
  86. private FastMap<Integer, Territory> _territoryList;
  87. private FastList<Integer> _disguisedPlayers;
  88. private FastList<TerritoryWard> _territoryWards;
  89. private FastMap<L2Clan, L2SiegeFlagInstance> _clanFlags;
  90. private Map<Integer,Integer[]> _participantPoints = new FastMap<Integer,Integer[]>();
  91. private Calendar _startTWDate = Calendar.getInstance();
  92. private boolean _isRegistrationOver = true;
  93. private boolean _isTWChannelOpen = false;
  94. private boolean _isTWInProgress = false;
  95. protected ScheduledFuture<?> _scheduledStartTWTask = null;
  96. protected ScheduledFuture<?> _scheduledEndTWTask = null;
  97. protected ScheduledFuture<?> _scheduledRewardOnlineTask = null;
  98. // =========================================================
  99. // Constructor
  100. private TerritoryWarManager()
  101. {
  102. _log.info("Initializing TerritoryWarManager");
  103. // init lists
  104. _registeredClans = new FastMap<Integer, FastList<L2Clan>>();
  105. _registeredMercenaries = new FastMap<Integer, FastList<Integer>>();
  106. _territoryList = new FastMap<Integer, Territory>();
  107. _territoryWards = new FastList<TerritoryWard>();
  108. _clanFlags = new FastMap<L2Clan, L2SiegeFlagInstance>();
  109. _disguisedPlayers = new FastList<Integer>();
  110. TERRITORY_ITEM_IDS = new FastMap<Integer,Integer>();
  111. // Constant data
  112. TERRITORY_ITEM_IDS.put(81, 13757);
  113. TERRITORY_ITEM_IDS.put(82, 13758);
  114. TERRITORY_ITEM_IDS.put(83, 13759);
  115. TERRITORY_ITEM_IDS.put(84, 13760);
  116. TERRITORY_ITEM_IDS.put(85, 13761);
  117. TERRITORY_ITEM_IDS.put(86, 13762);
  118. TERRITORY_ITEM_IDS.put(87, 13763);
  119. TERRITORY_ITEM_IDS.put(88, 13764);
  120. TERRITORY_ITEM_IDS.put(89, 13765);
  121. // load data from database
  122. load();
  123. }
  124. // =========================================================
  125. // Method - Public
  126. public int getRegisteredTerritoryId(L2PcInstance player)
  127. {
  128. if (player == null || !_isTWChannelOpen || player.getLevel() < PLAYERMINLEVEL)
  129. return 0;
  130. if (player.getClan() != null)
  131. {
  132. if (player.getClan().getHasCastle() > 0)
  133. return player.getClan().getHasCastle() + 80;
  134. for(int cId:_registeredClans.keySet())
  135. if (_registeredClans.get(cId).contains(player.getClan()))
  136. return cId + 80;
  137. }
  138. for(int cId:_registeredMercenaries.keySet())
  139. if (_registeredMercenaries.get(cId).contains(player.getObjectId()))
  140. return cId + 80;
  141. return 0;
  142. }
  143. public boolean isAllyField(L2PcInstance player, int fieldId)
  144. {
  145. if (player == null || player.getSiegeSide() == 0)
  146. return false;
  147. else if ((player.getSiegeSide() - 80) == fieldId)
  148. return true;
  149. else if (fieldId > 100 && _territoryList.containsKey((player.getSiegeSide() - 80))
  150. && _territoryList.get((player.getSiegeSide() - 80)).getFortId() == fieldId)
  151. return true;
  152. return false;
  153. }
  154. /**
  155. * @param castleId
  156. * @param clan The L2Clan of the player
  157. * @return true if the clan is registered
  158. */
  159. public final boolean checkIsRegistered(int castleId, L2Clan clan)
  160. {
  161. if (clan == null)
  162. return false;
  163. if (clan.getHasCastle() > 0)
  164. return (castleId == -1 ? true : (clan.getHasCastle() == castleId));
  165. if (castleId == -1)
  166. {
  167. for(int cId:_registeredClans.keySet())
  168. if (_registeredClans.get(cId).contains(clan))
  169. return true;
  170. return false;
  171. }
  172. return _registeredClans.get(castleId).contains(clan);
  173. }
  174. /**
  175. * @param castleId
  176. * @param objId
  177. * @return true if the player is registered
  178. */
  179. public final boolean checkIsRegistered(int castleId, int objId)
  180. {
  181. if (castleId == -1)
  182. {
  183. for(int cId:_registeredMercenaries.keySet())
  184. if (_registeredMercenaries.get(cId).contains(objId))
  185. return true;
  186. return false;
  187. }
  188. return _registeredMercenaries.get(castleId).contains(objId);
  189. }
  190. public Territory getTerritory(int castleId)
  191. {
  192. return _territoryList.get(castleId);
  193. }
  194. public FastList<Territory> getAllTerritories()
  195. {
  196. FastList<Territory> ret = new FastList<Territory>();
  197. for (Territory t : _territoryList.values())
  198. if (t.getOwnerClan() != null)
  199. ret.add(t);
  200. return ret;
  201. }
  202. public Collection<L2Clan> getRegisteredClans(int castleId)
  203. {
  204. return _registeredClans.get(castleId);
  205. }
  206. public void addDisguisedPlayer(int playerObjId)
  207. {
  208. _disguisedPlayers.add(playerObjId);
  209. }
  210. public boolean isDisguised(int playerObjId)
  211. {
  212. return _disguisedPlayers.contains(playerObjId);
  213. }
  214. public Collection<Integer> getRegisteredMercenaries(int castleId)
  215. {
  216. return _registeredMercenaries.get(castleId);
  217. }
  218. public long getTWStartTimeInMillis()
  219. {
  220. return _startTWDate.getTimeInMillis();
  221. }
  222. public Calendar getTWStart()
  223. {
  224. return _startTWDate;
  225. }
  226. public void setTWStartTimeInMillis(long time)
  227. {
  228. _startTWDate.setTimeInMillis(time);
  229. if (_isTWInProgress)
  230. {
  231. if (_scheduledEndTWTask != null)
  232. _scheduledEndTWTask.cancel(false);
  233. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), 1000);
  234. }
  235. else
  236. {
  237. if (_scheduledStartTWTask != null)
  238. _scheduledStartTWTask.cancel(false);
  239. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), 1000);
  240. }
  241. }
  242. public boolean isTWChannelOpen()
  243. {
  244. return _isTWChannelOpen;
  245. }
  246. public void registerClan(int castleId, L2Clan clan)
  247. {
  248. if (clan == null || (_registeredClans.get(castleId) != null && _registeredClans.get(castleId).contains(clan)))
  249. return;
  250. else if (_registeredClans.get(castleId) == null)
  251. _registeredClans.put(castleId, new FastList<L2Clan>());
  252. _registeredClans.get(castleId).add(clan);
  253. changeRegistration(castleId, clan.getClanId(), false);
  254. }
  255. public void registerMerc(int castleId, L2PcInstance player)
  256. {
  257. if (player == null || player.getLevel() < PLAYERMINLEVEL
  258. || (_registeredMercenaries.get(castleId) != null && _registeredMercenaries.get(castleId).contains(player.getObjectId())))
  259. return;
  260. else if (_registeredMercenaries.get(castleId) == null)
  261. _registeredMercenaries.put(castleId, new FastList<Integer>());
  262. _registeredMercenaries.get(castleId).add(player.getObjectId());
  263. changeRegistration(castleId, player.getObjectId(), false);
  264. }
  265. public void removeClan(int castleId, L2Clan clan)
  266. {
  267. if (clan == null)
  268. return;
  269. else if (_registeredClans.get(castleId) != null && _registeredClans.get(castleId).contains(clan))
  270. {
  271. _registeredClans.get(castleId).remove(clan);
  272. changeRegistration(castleId, clan.getClanId(), true);
  273. }
  274. }
  275. public void removeMerc(int castleId, L2PcInstance player)
  276. {
  277. if (player == null)
  278. return;
  279. else if (_registeredMercenaries.get(castleId) != null && _registeredMercenaries.get(castleId).contains(player.getObjectId()))
  280. {
  281. _registeredMercenaries.get(castleId).remove(_registeredMercenaries.get(castleId).indexOf(player.getObjectId()));
  282. changeRegistration(castleId, player.getObjectId(), true);
  283. }
  284. }
  285. public boolean getIsRegistrationOver()
  286. {
  287. return _isRegistrationOver;
  288. }
  289. public boolean isTWInProgress()
  290. {
  291. return _isTWInProgress;
  292. }
  293. public void territoryCatapultDestroyed(int castleId)
  294. {
  295. if (_territoryList.get(castleId) != null)
  296. _territoryList.get(castleId).changeNPCsSpawn(2, false);
  297. for (L2DoorInstance door : CastleManager.getInstance().getCastleById(castleId).getDoors())
  298. door.openMe();
  299. }
  300. public L2Npc addTerritoryWard(int territoryId, int newOwnerId, int oldOwnerId, boolean broadcastMessage)
  301. {
  302. L2Npc ret = null;
  303. if (_territoryList.get(newOwnerId) != null)
  304. {
  305. Territory terNew = _territoryList.get(newOwnerId);
  306. TerritoryNPCSpawn ward = terNew.getFreeWardSpawnPlace();
  307. if (ward != null)
  308. {
  309. ward._npcId = territoryId;
  310. ret = spawnNPC(36491 + territoryId, ward.getLocation());
  311. ward.setNPC(ret);
  312. if (!isTWInProgress() && !SPAWN_WARDS_WHEN_TW_IS_NOT_IN_PROGRESS)
  313. ret.decayMe();
  314. if (terNew.getOwnerClan() != null && terNew.getOwnedWardIds().contains(newOwnerId + 80))
  315. {
  316. for(int wardId : terNew.getOwnedWardIds())
  317. {
  318. final FastList<L2SkillLearn> residentialSkills = SkillTreesData.getInstance().getAvailableResidentialSkills(wardId);
  319. for (L2SkillLearn s : residentialSkills)
  320. {
  321. final L2Skill sk = SkillTable.getInstance().getInfo(s.getSkillId(), s.getSkillLevel());
  322. if (sk != null)
  323. {
  324. for (L2PcInstance member : terNew.getOwnerClan().getOnlineMembers(0))
  325. {
  326. if (!member.isInOlympiadMode())
  327. {
  328. member.addSkill(sk, false);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. if (_territoryList.containsKey(oldOwnerId))
  337. {
  338. Territory terOld = _territoryList.get(oldOwnerId);
  339. terOld.removeWard(territoryId);
  340. updateTerritoryData(terOld);
  341. updateTerritoryData(terNew);
  342. if (broadcastMessage)
  343. {
  344. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_S1_HAS_SUCCEDED_IN_CAPTURING_S2_TERRITORY_WARD);
  345. sm.addString(terNew.getOwnerClan().getName());
  346. sm.addString(ward.getNpc().getName().replaceAll(" Ward", ""));
  347. announceToParticipants(sm, 135000, 13500);
  348. }
  349. if (terOld.getOwnerClan() != null)
  350. {
  351. final FastList<L2SkillLearn> territorySkills = SkillTreesData.getInstance().getAvailableResidentialSkills(territoryId);
  352. for (L2SkillLearn s : territorySkills)
  353. {
  354. final L2Skill sk = SkillTable.getInstance().getInfo(s.getSkillId(), s.getSkillLevel());
  355. if (sk != null)
  356. {
  357. for (L2PcInstance member : terOld.getOwnerClan().getOnlineMembers(0))
  358. {
  359. member.removeSkill(sk, false);
  360. }
  361. }
  362. }
  363. if (!terOld.getOwnedWardIds().isEmpty() && !terOld.getOwnedWardIds().contains(oldOwnerId + 80))
  364. {
  365. for(int wardId : terOld.getOwnedWardIds())
  366. {
  367. final FastList<L2SkillLearn> wardSkills = SkillTreesData.getInstance().getAvailableResidentialSkills(wardId);
  368. for (L2SkillLearn s : wardSkills)
  369. {
  370. final L2Skill sk = SkillTable.getInstance().getInfo(s.getSkillId(), s.getSkillLevel());
  371. if (sk != null)
  372. {
  373. for (L2PcInstance member : terOld.getOwnerClan().getOnlineMembers(0))
  374. {
  375. member.removeSkill(sk, false);
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. else
  385. _log.warning("TerritoryWarManager: Missing territory for new Ward owner: " + newOwnerId + ";" + territoryId);
  386. return ret;
  387. }
  388. public L2SiegeFlagInstance getHQForClan(L2Clan clan)
  389. {
  390. if (clan.getHasCastle() > 0)
  391. return _territoryList.get(clan.getHasCastle()).getHQ();
  392. return null;
  393. }
  394. public L2SiegeFlagInstance getHQForTerritory(int territoryId)
  395. {
  396. if (_territoryList.containsKey(territoryId - 80))
  397. return _territoryList.get(territoryId - 80).getHQ();
  398. return null;
  399. }
  400. public void setHQForClan(L2Clan clan, L2SiegeFlagInstance hq)
  401. {
  402. if (clan.getHasCastle() > 0)
  403. _territoryList.get(clan.getHasCastle()).setHQ(hq);
  404. }
  405. public void addClanFlag(L2Clan clan, L2SiegeFlagInstance flag)
  406. {
  407. _clanFlags.put(clan, flag);
  408. }
  409. public boolean isClanHasFlag(L2Clan clan)
  410. {
  411. return _clanFlags.containsKey(clan);
  412. }
  413. public L2SiegeFlagInstance getFlagForClan(L2Clan clan)
  414. {
  415. if (_clanFlags.containsKey(clan))
  416. return _clanFlags.get(clan);
  417. return null;
  418. }
  419. public void removeClanFlag(L2Clan clan)
  420. {
  421. _clanFlags.remove(clan);
  422. }
  423. public List<TerritoryWard> getAllTerritoryWards()
  424. {
  425. return _territoryWards;
  426. }
  427. public TerritoryWard getTerritoryWardForOwner(int castleId)
  428. {
  429. for(TerritoryWard twWard : _territoryWards)
  430. if (twWard.getTerritoryId() == castleId)
  431. return twWard;
  432. return null;
  433. }
  434. public TerritoryWard getTerritoryWard(int territoryId)
  435. {
  436. for(TerritoryWard twWard : _territoryWards)
  437. if (twWard.getTerritoryId() == territoryId)
  438. return twWard;
  439. return null;
  440. }
  441. public TerritoryWard getTerritoryWard(L2PcInstance player)
  442. {
  443. for(TerritoryWard twWard : _territoryWards)
  444. if (twWard.playerId == player.getObjectId())
  445. return twWard;
  446. return null;
  447. }
  448. public void dropCombatFlag(L2PcInstance player, boolean isKilled, boolean isSpawnBack)
  449. {
  450. for(TerritoryWard twWard : _territoryWards)
  451. if (twWard.playerId == player.getObjectId())
  452. {
  453. twWard.dropIt();
  454. if (isTWInProgress())
  455. {
  456. if (isKilled)
  457. twWard.spawnMe();
  458. else if (isSpawnBack)
  459. twWard.spawnBack();
  460. else
  461. for(TerritoryNPCSpawn wardSpawn : _territoryList.get(twWard.getOwnerCastleId()).getOwnedWard())
  462. if (wardSpawn.getNpcId() == twWard.getTerritoryId())
  463. {
  464. wardSpawn.setNPC(wardSpawn.getNpc().getSpawn().doSpawn());
  465. twWard.unSpawnMe();
  466. twWard.setNpc(wardSpawn.getNpc());
  467. }
  468. }
  469. if (isKilled)
  470. {
  471. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_CHAR_THAT_ACQUIRED_S1_WARD_HAS_BEEN_KILLED);
  472. sm.addString(twWard.getNpc().getName().replaceAll(" Ward", ""));
  473. announceToParticipants(sm, 0, 0);
  474. }
  475. }
  476. }
  477. public void giveTWQuestPoint(L2PcInstance player)
  478. {
  479. if (!_participantPoints.containsKey(player.getObjectId()))
  480. _participantPoints.put(player.getObjectId(), new Integer[]{player.getSiegeSide(),0,0,0,0,0,0});
  481. _participantPoints.get(player.getObjectId())[2]++;
  482. }
  483. public void giveTWPoint(L2PcInstance killer, int victimSide, int type)
  484. {
  485. if (victimSide == 0)
  486. return;
  487. if (killer.getParty() != null && type < 5)
  488. for(L2PcInstance pl : killer.getParty().getPartyMembers())
  489. {
  490. if (pl.getSiegeSide() == victimSide || pl.getSiegeSide() == 0 || !Util.checkIfInRange(2000, killer, pl, false))
  491. continue;
  492. else if (!_participantPoints.containsKey(pl.getObjectId()))
  493. _participantPoints.put(pl.getObjectId(), new Integer[]{pl.getSiegeSide(),0,0,0,0,0,0});
  494. _participantPoints.get(pl.getObjectId())[type]++;
  495. }
  496. else
  497. {
  498. if (killer.getSiegeSide() == victimSide || killer.getSiegeSide() == 0)
  499. return;
  500. else if (!_participantPoints.containsKey(killer.getObjectId()))
  501. _participantPoints.put(killer.getObjectId(), new Integer[]{killer.getSiegeSide(),0,0,0,0,0,0});
  502. _participantPoints.get(killer.getObjectId())[type]++;
  503. }
  504. }
  505. public int[] calcReward(L2PcInstance player)
  506. {
  507. if (_participantPoints.containsKey(player.getObjectId()))
  508. {
  509. int[] reward = new int[2];
  510. Integer[] temp = _participantPoints.get(player.getObjectId());
  511. reward[0] = temp[0];
  512. reward[1] = 0;
  513. // badges for being online. if char was not online at least 10 mins
  514. // than he cant get rewards(also this will handle that player already get his/her rewards)
  515. if (temp[6] < 10)
  516. return reward;
  517. reward[1] += (temp[6] > 70 ? 7 : (int)(temp[6] * 0.1));
  518. // badges for player Quests
  519. reward[1] += temp[2] * 7;
  520. // badges for player Kills
  521. if (temp[1] < 50)
  522. reward[1] += temp[1] * 0.1;
  523. else if (temp[1] < 120)
  524. reward[1] += (5 + (temp[1] - 50) / 14);
  525. else
  526. reward[1] += 10;
  527. // badges for territory npcs
  528. reward[1] += temp[3];
  529. // badges for territory catapults
  530. reward[1] += temp[4] * 2;
  531. // badges for territory Wards
  532. reward[1] += (temp[5] > 0 ? 5 : 0);
  533. // badges for territory quest done
  534. reward[1] += Math.min(_territoryList.get(temp[0] - 80).getQuestDone()[0], 10);
  535. reward[1] += _territoryList.get(temp[0] - 80).getQuestDone()[1];
  536. reward[1] += _territoryList.get(temp[0] - 80).getOwnedWardIds().size();
  537. return reward;
  538. }
  539. return new int[]{0,0};
  540. }
  541. public void debugReward(L2PcInstance player)
  542. {
  543. player.sendMessage("Registred TerrId: " + player.getSiegeSide());
  544. if (_participantPoints.containsKey(player.getObjectId()))
  545. {
  546. Integer[] temp = _participantPoints.get(player.getObjectId());
  547. player.sendMessage("TerrId: " + temp[0]);
  548. player.sendMessage("PcKill: " + temp[1]);
  549. player.sendMessage("PcQuests: " + temp[2]);
  550. player.sendMessage("npcKill: " + temp[3]);
  551. player.sendMessage("CatatKill: " + temp[4]);
  552. player.sendMessage("WardKill: " + temp[5]);
  553. player.sendMessage("onlineTime: " + temp[6]);
  554. }
  555. else
  556. player.sendMessage("No points for you!");
  557. if (_territoryList.containsKey(player.getSiegeSide() - 80))
  558. {
  559. player.sendMessage("Your Territory's jobs:");
  560. player.sendMessage("npcKill: " + _territoryList.get(player.getSiegeSide() - 80).getQuestDone()[0]);
  561. player.sendMessage("WardCaptured: " + _territoryList.get(player.getSiegeSide() - 80).getQuestDone()[1]);
  562. }
  563. }
  564. public void resetReward(L2PcInstance player)
  565. {
  566. if (_participantPoints.containsKey(player.getObjectId()))
  567. {
  568. _participantPoints.get(player.getObjectId())[6] = 0;
  569. }
  570. }
  571. // =========================================================
  572. // Method - Private
  573. public L2Npc spawnNPC(int npcId, Location loc)
  574. {
  575. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  576. if (template != null)
  577. {
  578. L2Spawn spawnDat;
  579. try
  580. {
  581. spawnDat = new L2Spawn(template);
  582. spawnDat.setAmount(1);
  583. spawnDat.setLocx(loc.getX());
  584. spawnDat.setLocy(loc.getY());
  585. spawnDat.setLocz(loc.getZ());
  586. spawnDat.setHeading(loc.getHeading());
  587. spawnDat.stopRespawn();
  588. return spawnDat.spawnOne(false);
  589. }
  590. catch (Exception e)
  591. {
  592. _log.log(Level.WARNING, "Territory War Manager: " + e.getMessage(), e);
  593. }
  594. }
  595. else
  596. {
  597. _log.warning("Territory War Manager: Data missing in NPC table for ID: " + npcId + ".");
  598. }
  599. return null;
  600. }
  601. private void changeRegistration(int castleId, int objId, boolean delete)
  602. {
  603. Connection con = null;
  604. try
  605. {
  606. con = L2DatabaseFactory.getInstance().getConnection();
  607. PreparedStatement statement;
  608. if (delete)
  609. statement = con.prepareStatement("DELETE FROM territory_registrations WHERE castleId=? and registeredId=?");
  610. else
  611. statement = con.prepareStatement("INSERT INTO territory_registrations (castleId, registeredId) values (?,?)");
  612. statement.setInt(1, castleId);
  613. statement.setInt(2, objId);
  614. statement.execute();
  615. statement.close();
  616. }
  617. catch (Exception e)
  618. {
  619. _log.log(Level.WARNING, "Exception: Territory War registration: " + e.getMessage(), e);
  620. }
  621. finally
  622. {
  623. L2DatabaseFactory.close(con);
  624. }
  625. }
  626. private void updateTerritoryData(Territory ter)
  627. {
  628. Connection con = null;
  629. try
  630. {
  631. con = L2DatabaseFactory.getInstance().getConnection();
  632. PreparedStatement statement = con.prepareStatement("UPDATE territories SET ownedWardIds=? WHERE territoryId=?");
  633. StringBuilder wardList = new StringBuilder();
  634. for (int i : ter.getOwnedWardIds())
  635. wardList.append(i + ";");
  636. statement.setString(1, wardList.toString());
  637. statement.setInt(2, ter.getTerritoryId());
  638. statement.execute();
  639. statement.close();
  640. }
  641. catch (Exception e)
  642. {
  643. _log.log(Level.WARNING, "Exception: Territory Data update: " + e.getMessage(), e);
  644. }
  645. finally
  646. {
  647. L2DatabaseFactory.close(con);
  648. }
  649. }
  650. private final void load()
  651. {
  652. InputStream is = null;
  653. try
  654. {
  655. is = new FileInputStream(new File(Config.TW_CONFIGURATION_FILE));
  656. L2Properties territoryWarSettings = new L2Properties();
  657. territoryWarSettings.load(is);
  658. // Siege setting
  659. DEFENDERMAXCLANS = Integer.decode(territoryWarSettings.getProperty("DefenderMaxClans", "500"));
  660. DEFENDERMAXPLAYERS = Integer.decode(territoryWarSettings.getProperty("DefenderMaxPlayers", "500"));
  661. CLANMINLEVEL = Integer.decode(territoryWarSettings.getProperty("ClanMinLevel", "0"));
  662. PLAYERMINLEVEL = Integer.decode(territoryWarSettings.getProperty("PlayerMinLevel", "40"));
  663. WARLENGTH = Long.decode(territoryWarSettings.getProperty("WarLength", "120")) * 60000;
  664. PLAYER_WITH_WARD_CAN_BE_KILLED_IN_PEACEZONE = Boolean.parseBoolean(territoryWarSettings.getProperty("PlayerWithWardCanBeKilledInPeaceZone", "False"));
  665. SPAWN_WARDS_WHEN_TW_IS_NOT_IN_PROGRESS = Boolean.parseBoolean(territoryWarSettings.getProperty("SpawnWardsWhenTWIsNotInProgress", "False"));
  666. RETURN_WARDS_WHEN_TW_STARTS = Boolean.parseBoolean(territoryWarSettings.getProperty("ReturnWardsWhenTWStarts", "False"));
  667. MINTWBADGEFORNOBLESS = Integer.decode(territoryWarSettings.getProperty("MinTerritoryBadgeForNobless", "100"));
  668. MINTWBADGEFORSTRIDERS = Integer.decode(territoryWarSettings.getProperty("MinTerritoryBadgeForStriders", "50"));
  669. MINTWBADGEFORBIGSTRIDER = Integer.decode(territoryWarSettings.getProperty("MinTerritoryBadgeForBigStrider", "80"));
  670. Connection con = null;
  671. try
  672. {
  673. con = L2DatabaseFactory.getInstance().getConnection();
  674. PreparedStatement statement = con.prepareStatement("SELECT * FROM territory_spawnlist");
  675. ResultSet rs = statement.executeQuery();
  676. while (rs.next())
  677. {
  678. int castleId = rs.getInt("castleId");
  679. int npcId = rs.getInt("npcId");
  680. Location loc = new Location(rs.getInt("x"),rs.getInt("y"),rs.getInt("z"),rs.getInt("heading"));
  681. int spawnType = rs.getInt("spawnType");
  682. if (!_territoryList.containsKey(castleId))
  683. _territoryList.put(castleId, new Territory(castleId));
  684. switch(spawnType)
  685. {
  686. case 0: // town npcs
  687. case 1: // fortress npcs
  688. case 2: // castle npcs
  689. _territoryList.get(castleId).getSpawnList().add(new TerritoryNPCSpawn(castleId, loc, npcId, spawnType, null));
  690. break;
  691. case 3: // ward spawns
  692. _territoryList.get(castleId).addWardSpawnPlace(loc);
  693. break;
  694. default:
  695. _log.warning("Territory War Manager: Unknown npc type for " + rs.getInt("id"));
  696. }
  697. }
  698. rs.close();
  699. statement.close();
  700. }
  701. catch (Exception e)
  702. {
  703. _log.log(Level.WARNING, "Territory War Manager: SpawnList error: " + e.getMessage(), e);
  704. }
  705. finally
  706. {
  707. L2DatabaseFactory.close(con);
  708. }
  709. try
  710. {
  711. con = L2DatabaseFactory.getInstance().getConnection();
  712. PreparedStatement statement = con.prepareStatement("SELECT * FROM territories");
  713. ResultSet rs = statement.executeQuery();
  714. while (rs.next())
  715. {
  716. int castleId = rs.getInt("castleId");
  717. int fortId = rs.getInt("fortId");
  718. String ownedWardIds = rs.getString("OwnedWardIds");
  719. Territory t = _territoryList.get(castleId);
  720. if (t != null)
  721. {
  722. t._fortId = fortId;
  723. if (CastleManager.getInstance().getCastleById(castleId).getOwnerId() > 0)
  724. {
  725. t.setOwnerClan(ClanTable.getInstance().getClan(CastleManager.getInstance().getCastleById(castleId).getOwnerId()));
  726. t.changeNPCsSpawn(0, true);
  727. }
  728. if (!ownedWardIds.isEmpty())
  729. {
  730. for(String wardId:ownedWardIds.split(";"))
  731. if (Integer.parseInt(wardId) > 0)
  732. addTerritoryWard(Integer.parseInt(wardId), castleId, 0, false);
  733. }
  734. }
  735. }
  736. rs.close();
  737. statement.close();
  738. }
  739. catch (Exception e)
  740. {
  741. _log.log(Level.WARNING, "Territory War Manager: territory list error(): " + e.getMessage(), e);
  742. }
  743. finally
  744. {
  745. L2DatabaseFactory.close(con);
  746. }
  747. try
  748. {
  749. con = L2DatabaseFactory.getInstance().getConnection();
  750. PreparedStatement statement = con.prepareStatement("SELECT * FROM territory_registrations");
  751. ResultSet rs = statement.executeQuery();
  752. while (rs.next())
  753. {
  754. int castleId = rs.getInt("castleId");
  755. int registeredId = rs.getInt("registeredId");
  756. if (ClanTable.getInstance().getClan(registeredId) != null)
  757. {
  758. if (_registeredClans.get(castleId) == null)
  759. _registeredClans.put(castleId, new FastList<L2Clan>());
  760. _registeredClans.get(castleId).add(ClanTable.getInstance().getClan(registeredId));
  761. }
  762. else
  763. {
  764. if (_registeredMercenaries.get(castleId) == null)
  765. _registeredMercenaries.put(castleId, new FastList<Integer>());
  766. _registeredMercenaries.get(castleId).add(registeredId);
  767. }
  768. }
  769. rs.close();
  770. statement.close();
  771. }
  772. catch (Exception e)
  773. {
  774. _log.log(Level.WARNING, "Territory War Manager: registration list error: " + e.getMessage(), e);
  775. }
  776. finally
  777. {
  778. L2DatabaseFactory.close(con);
  779. }
  780. }
  781. catch (Exception e)
  782. {
  783. //_initialized = false;
  784. _log.log(Level.WARNING, "Error while loading Territory War Manager! " + e.getMessage(), e);
  785. }
  786. finally
  787. {
  788. try
  789. {
  790. is.close();
  791. }
  792. catch (Exception e)
  793. {
  794. }
  795. }
  796. }
  797. private void startTerritoryWar()
  798. {
  799. if (_territoryList == null)
  800. {
  801. _log.warning("TerritoryWarManager: TerritoryList is NULL!");
  802. return;
  803. }
  804. FastList<Territory> activeTerritoryList = new FastList<Territory>();
  805. for(Territory t : _territoryList.values())
  806. {
  807. Castle castle = CastleManager.getInstance().getCastleById(t.getCastleId());
  808. if (castle != null)
  809. {
  810. if (castle.getOwnerId() > 0)
  811. activeTerritoryList.add(t);
  812. }
  813. else
  814. _log.warning("TerritoryWarManager: Castle missing! CastleId: " + t.getCastleId());
  815. }
  816. if (activeTerritoryList.size() < 2)
  817. return;
  818. _isTWInProgress = true;
  819. if (!updatePlayerTWStateFlags(false))
  820. return;
  821. // teleportPlayer(Siege.TeleportWhoType.Attacker, MapRegionTable.TeleportWhereType.Town); // Teleport to the closest town
  822. for(Territory t : activeTerritoryList)
  823. {
  824. Castle castle = CastleManager.getInstance().getCastleById(t.getCastleId());
  825. Fort fort = FortManager.getInstance().getFortById(t.getFortId());
  826. // spawnControlTower(t.getCastleId()); // Spawn control tower
  827. if (castle != null)
  828. {
  829. t.changeNPCsSpawn(2, true);
  830. castle.spawnDoor(); // Spawn door
  831. castle.getZone().setSiegeInstance(this);
  832. castle.getZone().setIsActive(true);
  833. castle.getZone().updateZoneStatusForCharactersInside();
  834. }
  835. else
  836. _log.warning("TerritoryWarManager: Castle missing! CastleId: " + t.getCastleId());
  837. if (fort != null)
  838. {
  839. t.changeNPCsSpawn(1, true);
  840. fort.resetDoors(); // Spawn door
  841. fort.getZone().setSiegeInstance(this);
  842. fort.getZone().setIsActive(true);
  843. fort.getZone().updateZoneStatusForCharactersInside();
  844. }
  845. else
  846. _log.warning("TerritoryWarManager: Fort missing! FortId: " + t.getFortId());
  847. for(TerritoryNPCSpawn ward : t.getOwnedWard())
  848. if (ward.getNpc() != null && t.getOwnerClan() != null)
  849. {
  850. if (!ward.getNpc().isVisible())
  851. ward.setNPC(ward.getNpc().getSpawn().doSpawn());
  852. _territoryWards.add(new TerritoryWard(ward.getNpcId(), ward.getLocation().getX(), ward.getLocation().getY(), ward.getLocation().getZ(), 0, ward.getNpcId() + 13479, t.getCastleId(), ward.getNpc()));
  853. }
  854. t.getQuestDone()[0] = 0; // killed npc
  855. t.getQuestDone()[1] = 0; // captured wards
  856. }
  857. _participantPoints.clear();
  858. if (RETURN_WARDS_WHEN_TW_STARTS)
  859. for(TerritoryWard ward : _territoryWards)
  860. if (ward.getOwnerCastleId() != ward.getTerritoryId() - 80)
  861. {
  862. ward.unSpawnMe();
  863. ward.setNpc(addTerritoryWard(ward.getTerritoryId(), ward.getTerritoryId() - 80, ward.getOwnerCastleId(), false));
  864. ward.setOwnerCastleId(ward.getTerritoryId() - 80);
  865. }
  866. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.TERRITORY_WAR_HAS_BEGUN);
  867. Announcements.getInstance().announceToAll(sm);
  868. }
  869. private void endTerritoryWar()
  870. {
  871. _isTWInProgress = false;
  872. if (_territoryList == null)
  873. {
  874. _log.warning("TerritoryWarManager: TerritoryList is NULL!");
  875. return;
  876. }
  877. FastList<Territory> activeTerritoryList = new FastList<Territory>();
  878. for(Territory t : _territoryList.values())
  879. {
  880. Castle castle = CastleManager.getInstance().getCastleById(t.getCastleId());
  881. if (castle != null)
  882. {
  883. if (castle.getOwnerId() > 0)
  884. activeTerritoryList.add(t);
  885. }
  886. else
  887. _log.warning("TerritoryWarManager: Castle missing! CastleId: " + t.getCastleId());
  888. }
  889. if (activeTerritoryList.size() < 2)
  890. return;
  891. if (!updatePlayerTWStateFlags(true))
  892. return;
  893. if (_territoryWards != null)
  894. {
  895. for(TerritoryWard twWard : _territoryWards)
  896. twWard.unSpawnMe();
  897. _territoryWards.clear();
  898. }
  899. // teleportPlayer(Siege.TeleportWhoType.Attacker, MapRegionTable.TeleportWhereType.Town); // Teleport to the closest town
  900. for(Territory t : activeTerritoryList)
  901. {
  902. Castle castle = CastleManager.getInstance().getCastleById(t.getCastleId());
  903. Fort fort = FortManager.getInstance().getFortById(t.getFortId());
  904. if (castle != null)
  905. {
  906. castle.spawnDoor();
  907. t.changeNPCsSpawn(2, false);
  908. castle.getZone().setIsActive(false);
  909. castle.getZone().updateZoneStatusForCharactersInside();
  910. castle.getZone().setSiegeInstance(null);
  911. }
  912. else
  913. _log.warning("TerritoryWarManager: Castle missing! CastleId: " + t.getCastleId());
  914. if (fort != null)
  915. {
  916. t.changeNPCsSpawn(1, false);
  917. fort.getZone().setIsActive(false);
  918. fort.getZone().updateZoneStatusForCharactersInside();
  919. fort.getZone().setSiegeInstance(null);
  920. }
  921. else
  922. _log.warning("TerritoryWarManager: Fort missing! FortId: " + t.getFortId());
  923. if (t.getHQ() != null)
  924. t.getHQ().deleteMe();
  925. for(TerritoryNPCSpawn ward : t.getOwnedWard())
  926. if (ward.getNpc() != null)
  927. {
  928. if (!ward.getNpc().isVisible() && SPAWN_WARDS_WHEN_TW_IS_NOT_IN_PROGRESS)
  929. ward.setNPC(ward.getNpc().getSpawn().doSpawn());
  930. else if (ward.getNpc().isVisible() && !SPAWN_WARDS_WHEN_TW_IS_NOT_IN_PROGRESS)
  931. ward.getNpc().decayMe();
  932. }
  933. }
  934. for(L2SiegeFlagInstance flag : _clanFlags.values())
  935. flag.deleteMe();
  936. _clanFlags.clear();
  937. for(Integer castleId:_registeredClans.keySet())
  938. for(L2Clan clan:_registeredClans.get(castleId))
  939. changeRegistration(castleId, clan.getClanId(), true);
  940. for(Integer castleId:_registeredMercenaries.keySet())
  941. for(Integer pl_objId:_registeredMercenaries.get(castleId))
  942. changeRegistration(castleId, pl_objId, true);
  943. // change next TW date
  944. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.TERRITORY_WAR_HAS_ENDED);
  945. Announcements.getInstance().announceToAll(sm);
  946. }
  947. private boolean updatePlayerTWStateFlags(boolean clear)
  948. {
  949. Quest twQuest = QuestManager.getInstance().getQuest(qn);
  950. if (twQuest == null)
  951. {
  952. _log.warning("TerritoryWarManager: missing main Quest!");
  953. return false;
  954. }
  955. for(int castleId : _registeredClans.keySet())
  956. for(L2Clan clan : _registeredClans.get(castleId))
  957. for(L2PcInstance player : clan.getOnlineMembers(0))
  958. {
  959. if (player == null)
  960. continue;
  961. if (clear)
  962. {
  963. player.setSiegeState((byte) 0);
  964. if (!_isTWChannelOpen)
  965. player.setSiegeSide(0);
  966. }
  967. else
  968. {
  969. if (player.getLevel() < PLAYERMINLEVEL || player.getClassId().level() < 2)
  970. continue;
  971. if (_isTWInProgress)
  972. {
  973. player.setSiegeState((byte) 1);
  974. }
  975. player.setSiegeSide(80 + castleId);
  976. }
  977. player.broadcastUserInfo();
  978. }
  979. for(int castleId : _registeredMercenaries.keySet())
  980. for(int objId : _registeredMercenaries.get(castleId))
  981. {
  982. L2PcInstance player = L2World.getInstance().getPlayer(objId);
  983. if (player == null)
  984. continue;
  985. if (clear)
  986. {
  987. player.setSiegeState((byte) 0);
  988. if (!_isTWChannelOpen)
  989. player.setSiegeSide(0);
  990. }
  991. else
  992. {
  993. if (_isTWInProgress)
  994. {
  995. player.setSiegeState((byte) 1);
  996. }
  997. player.setSiegeSide(80 + castleId);
  998. }
  999. player.broadcastUserInfo();
  1000. }
  1001. for(Territory terr : _territoryList.values())
  1002. if (terr.getOwnerClan() != null)
  1003. for(L2PcInstance player : terr.getOwnerClan().getOnlineMembers(0))
  1004. {
  1005. if (player == null)
  1006. continue;
  1007. if (clear)
  1008. {
  1009. player.setSiegeState((byte) 0);
  1010. if (!_isTWChannelOpen)
  1011. player.setSiegeSide(0);
  1012. }
  1013. else
  1014. {
  1015. if (player.getLevel() < PLAYERMINLEVEL || player.getClassId().level() < 2)
  1016. continue;
  1017. if (_isTWInProgress)
  1018. {
  1019. player.setSiegeState((byte) 1);
  1020. }
  1021. player.setSiegeSide(80 + terr.getCastleId());
  1022. }
  1023. player.broadcastUserInfo();
  1024. }
  1025. twQuest.setOnEnterWorld(_isTWInProgress);
  1026. return true;
  1027. }
  1028. private class RewardOnlineParticipants implements Runnable
  1029. {
  1030. public RewardOnlineParticipants()
  1031. {
  1032. }
  1033. @Override
  1034. public void run()
  1035. {
  1036. if (isTWInProgress())
  1037. {
  1038. for(L2PcInstance player : L2World.getInstance().getAllPlayersArray())
  1039. if (player != null && player.getSiegeSide() > 0)
  1040. giveTWPoint(player, 1000, 6);
  1041. }
  1042. else
  1043. _scheduledRewardOnlineTask.cancel(false);
  1044. }
  1045. }
  1046. private class ScheduleStartTWTask implements Runnable
  1047. {
  1048. public ScheduleStartTWTask()
  1049. {
  1050. }
  1051. @Override
  1052. public void run()
  1053. {
  1054. _scheduledStartTWTask.cancel(false);
  1055. try
  1056. {
  1057. long timeRemaining = _startTWDate.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
  1058. if (timeRemaining > 7200000)
  1059. {
  1060. _isRegistrationOver = false;
  1061. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), timeRemaining - 7200000); // Prepare task for 2h before TW start to end registration
  1062. }
  1063. else if ((timeRemaining <= 7200000) && (timeRemaining > 1200000))
  1064. {
  1065. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_TERRITORY_WAR_REGISTERING_PERIOD_ENDED);
  1066. Announcements.getInstance().announceToAll(sm);
  1067. _isRegistrationOver = true;
  1068. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), timeRemaining - 1200000); // Prepare task for 20 mins left before TW start.
  1069. }
  1070. else if ((timeRemaining <= 1200000) && (timeRemaining > 600000))
  1071. {
  1072. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.TERRITORY_WAR_BEGINS_IN_20_MINUTES);
  1073. Announcements.getInstance().announceToAll(sm);
  1074. _isTWChannelOpen = true;
  1075. _isRegistrationOver = true;
  1076. updatePlayerTWStateFlags(false);
  1077. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), timeRemaining - 600000); // Prepare task for 10 mins left before TW start.
  1078. }
  1079. else if ((timeRemaining <= 600000) && (timeRemaining > 300000))
  1080. {
  1081. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.TERRITORY_WAR_BEGINS_IN_10_MINUTES);
  1082. Announcements.getInstance().announceToAll(sm);
  1083. _isTWChannelOpen = true;
  1084. _isRegistrationOver = true;
  1085. updatePlayerTWStateFlags(false);
  1086. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), timeRemaining - 300000); // Prepare task for 5 mins left before TW start.
  1087. }
  1088. else if ((timeRemaining <= 300000) && (timeRemaining > 60000))
  1089. {
  1090. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.TERRITORY_WAR_BEGINS_IN_5_MINUTES);
  1091. Announcements.getInstance().announceToAll(sm);
  1092. _isTWChannelOpen = true;
  1093. _isRegistrationOver = true;
  1094. updatePlayerTWStateFlags(false);
  1095. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), timeRemaining - 60000); // Prepare task for 1 min left before TW start.
  1096. }
  1097. else if ((timeRemaining <= 60000) && (timeRemaining > 0))
  1098. {
  1099. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.TERRITORY_WAR_BEGINS_IN_1_MINUTE);
  1100. Announcements.getInstance().announceToAll(sm);
  1101. _isTWChannelOpen = true;
  1102. _isRegistrationOver = true;
  1103. updatePlayerTWStateFlags(false);
  1104. _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), timeRemaining); // Prepare task for TW start.
  1105. }
  1106. else if ((timeRemaining + WARLENGTH) > 0)
  1107. {
  1108. _isTWChannelOpen = true;
  1109. _isRegistrationOver = true;
  1110. startTerritoryWar();
  1111. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), 1000); // Prepare task for TW end.
  1112. _scheduledRewardOnlineTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new RewardOnlineParticipants(), 60000, 60000);
  1113. }
  1114. }
  1115. catch (Exception e)
  1116. {
  1117. _log.log(Level.SEVERE, "", e);
  1118. }
  1119. }
  1120. }
  1121. private class ScheduleEndTWTask implements Runnable
  1122. {
  1123. public ScheduleEndTWTask()
  1124. {
  1125. }
  1126. @Override
  1127. public void run()
  1128. {
  1129. try
  1130. {
  1131. _scheduledEndTWTask.cancel(false);
  1132. long timeRemaining = _startTWDate.getTimeInMillis() + WARLENGTH - Calendar.getInstance().getTimeInMillis();
  1133. if (timeRemaining > 3600000)
  1134. {
  1135. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_TERRITORY_WAR_WILL_END_IN_S1_HOURS);
  1136. sm.addNumber(2);
  1137. announceToParticipants(sm, 0, 0);
  1138. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), timeRemaining - 3600000); // Prepare task for 1 hr left.
  1139. }
  1140. else if ((timeRemaining <= 3600000) && (timeRemaining > 600000))
  1141. {
  1142. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_TERRITORY_WAR_WILL_END_IN_S1_MINUTES);
  1143. sm.addNumber(Math.round(timeRemaining / 60000));
  1144. announceToParticipants(sm, 0, 0);
  1145. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), timeRemaining - 600000); // Prepare task for 10 minute left.
  1146. }
  1147. else if ((timeRemaining <= 600000) && (timeRemaining > 300000))
  1148. {
  1149. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_TERRITORY_WAR_WILL_END_IN_S1_MINUTES);
  1150. sm.addNumber(Math.round(timeRemaining / 60000));
  1151. announceToParticipants(sm, 0, 0);
  1152. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), timeRemaining - 300000); // Prepare task for 5 minute left.
  1153. }
  1154. else if ((timeRemaining <= 300000) && (timeRemaining > 10000))
  1155. {
  1156. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.THE_TERRITORY_WAR_WILL_END_IN_S1_MINUTES);
  1157. sm.addNumber(Math.round(timeRemaining / 60000));
  1158. announceToParticipants(sm, 0, 0);
  1159. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), timeRemaining - 10000); // Prepare task for 10 seconds count down
  1160. }
  1161. else if ((timeRemaining <= 10000) && (timeRemaining > 0))
  1162. {
  1163. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_SECONDS_TO_THE_END_OF_TERRITORY_WAR);
  1164. sm.addNumber(Math.round(timeRemaining / 1000));
  1165. announceToParticipants(sm, 0, 0);
  1166. _scheduledEndTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleEndTWTask(), timeRemaining); // Prepare task for second count down
  1167. }
  1168. else
  1169. {
  1170. endTerritoryWar();
  1171. // _scheduledStartTWTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleStartTWTask(), 1000);
  1172. ThreadPoolManager.getInstance().scheduleGeneral(new closeTerritoryChannelTask(), 600000);
  1173. }
  1174. }
  1175. catch (Exception e)
  1176. {
  1177. _log.log(Level.SEVERE, "", e);
  1178. }
  1179. }
  1180. }
  1181. private class closeTerritoryChannelTask implements Runnable
  1182. {
  1183. public closeTerritoryChannelTask()
  1184. {
  1185. }
  1186. @Override
  1187. public void run()
  1188. {
  1189. _isTWChannelOpen = false;
  1190. _disguisedPlayers.clear();
  1191. updatePlayerTWStateFlags(true);
  1192. }
  1193. }
  1194. public void announceToParticipants(L2GameServerPacket sm, int exp, int sp)
  1195. {
  1196. // broadcast to clan members
  1197. for(Territory ter : _territoryList.values())
  1198. if (ter.getOwnerClan() != null)
  1199. for (L2PcInstance member : ter.getOwnerClan().getOnlineMembers(0))
  1200. {
  1201. member.sendPacket(sm);
  1202. if (exp > 0 || sp > 0)
  1203. member.addExpAndSp(exp, sp);
  1204. }
  1205. for(FastList<L2Clan> list:_registeredClans.values())
  1206. for(L2Clan c:list)
  1207. for (L2PcInstance member : c.getOnlineMembers(0))
  1208. {
  1209. member.sendPacket(sm);
  1210. if (exp > 0 || sp > 0)
  1211. member.addExpAndSp(exp, sp);
  1212. }
  1213. // broadcast to mercenaries
  1214. for(FastList<Integer> list:_registeredMercenaries.values())
  1215. for(int objId:list)
  1216. {
  1217. L2PcInstance player = L2World.getInstance().getPlayer(objId);
  1218. if (player != null && (player.getClan() == null || !checkIsRegistered(-1, player.getClan())))
  1219. {
  1220. player.sendPacket(sm);
  1221. if (exp > 0 || sp > 0)
  1222. player.addExpAndSp(exp, sp);
  1223. }
  1224. }
  1225. }
  1226. // =========================================================
  1227. // Property - Public
  1228. public static class TerritoryNPCSpawn
  1229. {
  1230. private Location _location;
  1231. private int _npcId;
  1232. private int _castleId;
  1233. private int _type;
  1234. private L2Npc _npc;
  1235. public TerritoryNPCSpawn(int castle_id, Location loc, int npc_id, int type, L2Npc npc)
  1236. {
  1237. _castleId = castle_id;
  1238. _location = loc;
  1239. _npcId = npc_id;
  1240. _type = type;
  1241. _npc = npc;
  1242. }
  1243. public int getCastleId()
  1244. {
  1245. return _castleId;
  1246. }
  1247. public int getNpcId()
  1248. {
  1249. return _npcId;
  1250. }
  1251. public int getType()
  1252. {
  1253. return _type;
  1254. }
  1255. public void setNPC(L2Npc npc)
  1256. {
  1257. if (_npc != null)
  1258. _npc.deleteMe();
  1259. _npc = npc;
  1260. }
  1261. public L2Npc getNpc()
  1262. {
  1263. return _npc;
  1264. }
  1265. public Location getLocation()
  1266. {
  1267. return _location;
  1268. }
  1269. }
  1270. public class Territory
  1271. {
  1272. private final int _territoryId;
  1273. private final int _castleId; // territory Castle
  1274. private int _fortId; // territory Fortress
  1275. private L2Clan _ownerClan;
  1276. private FastList<TerritoryNPCSpawn> _spawnList;
  1277. private TerritoryNPCSpawn[] _territoryWardSpawnPlaces;
  1278. private boolean _isInProgress = false;
  1279. private L2SiegeFlagInstance _territoryHQ = null;
  1280. private int[] _questDone;
  1281. public Territory(int castleId)
  1282. {
  1283. _castleId = castleId;
  1284. _territoryId = castleId + 80;
  1285. _spawnList = new FastList<TerritoryNPCSpawn>();
  1286. _territoryWardSpawnPlaces = new TerritoryNPCSpawn[9];
  1287. _questDone = new int[2];
  1288. }
  1289. private void addWardSpawnPlace(Location loc)
  1290. {
  1291. for(int i = 0; i < _territoryWardSpawnPlaces.length; i++)
  1292. if (_territoryWardSpawnPlaces[i] == null)
  1293. {
  1294. _territoryWardSpawnPlaces[i] = new TerritoryNPCSpawn(_castleId, loc, 0, 4, null);
  1295. return;
  1296. }
  1297. }
  1298. private TerritoryNPCSpawn getFreeWardSpawnPlace()
  1299. {
  1300. for(int i = 0; i < _territoryWardSpawnPlaces.length; i++)
  1301. if (_territoryWardSpawnPlaces[i] != null && _territoryWardSpawnPlaces[i].getNpc() == null)
  1302. return _territoryWardSpawnPlaces[i];
  1303. _log.log(Level.WARNING, "TerritoryWarManager: no free Ward spawn found for territory: " + _territoryId);
  1304. for(int i = 0; i < _territoryWardSpawnPlaces.length; i++)
  1305. if (_territoryWardSpawnPlaces[i] == null)
  1306. _log.log(Level.WARNING, "TerritoryWarManager: territory ward spawn place " + i + " is null!");
  1307. else if (_territoryWardSpawnPlaces[i].getNpc() != null)
  1308. _log.log(Level.WARNING, "TerritoryWarManager: territory ward spawn place " + i + " has npc name: " + _territoryWardSpawnPlaces[i].getNpc().getName());
  1309. else
  1310. _log.log(Level.WARNING, "TerritoryWarManager: territory ward spawn place " + i + " is empty!");
  1311. return null;
  1312. }
  1313. public FastList<TerritoryNPCSpawn> getSpawnList()
  1314. {
  1315. return _spawnList;
  1316. }
  1317. private void changeNPCsSpawn(int type, boolean isSpawn)
  1318. {
  1319. if (type < 0 || type > 3)
  1320. {
  1321. _log.log(Level.WARNING, "TerritoryWarManager: wrong type(" + type + ") for NPCs spawn change!");
  1322. return;
  1323. }
  1324. for(TerritoryNPCSpawn twSpawn : _spawnList)
  1325. {
  1326. if (twSpawn.getType() != type)
  1327. continue;
  1328. if (isSpawn)
  1329. twSpawn.setNPC(spawnNPC(twSpawn.getNpcId(), twSpawn.getLocation()));
  1330. else
  1331. {
  1332. L2Npc npc = twSpawn.getNpc();
  1333. if (npc != null && !npc.isDead())
  1334. npc.deleteMe();
  1335. twSpawn.setNPC(null);
  1336. }
  1337. }
  1338. }
  1339. private void removeWard(int wardId)
  1340. {
  1341. for(TerritoryNPCSpawn wardSpawn : _territoryWardSpawnPlaces)
  1342. if (wardSpawn.getNpcId() == wardId)
  1343. {
  1344. wardSpawn.getNpc().deleteMe();
  1345. wardSpawn.setNPC(null);
  1346. wardSpawn._npcId = 0;
  1347. return;
  1348. }
  1349. _log.log(Level.WARNING, "TerritoryWarManager: cant delete wardId: " + wardId + " for territory: " + _territoryId);
  1350. }
  1351. public int getTerritoryId()
  1352. {
  1353. return _territoryId;
  1354. }
  1355. public int getCastleId()
  1356. {
  1357. return _castleId;
  1358. }
  1359. public int getFortId()
  1360. {
  1361. return _fortId;
  1362. }
  1363. public L2Clan getOwnerClan()
  1364. {
  1365. return _ownerClan;
  1366. }
  1367. public void setOwnerClan(L2Clan newOwner)
  1368. {
  1369. _ownerClan = newOwner;
  1370. }
  1371. public void setHQ(L2SiegeFlagInstance hq)
  1372. {
  1373. _territoryHQ = hq;
  1374. }
  1375. public L2SiegeFlagInstance getHQ()
  1376. {
  1377. return _territoryHQ;
  1378. }
  1379. public TerritoryNPCSpawn[] getOwnedWard()
  1380. {
  1381. return _territoryWardSpawnPlaces;
  1382. }
  1383. public int[] getQuestDone()
  1384. {
  1385. return _questDone;
  1386. }
  1387. public FastList<Integer> getOwnedWardIds()
  1388. {
  1389. FastList<Integer> ret = new FastList<Integer>();
  1390. for(TerritoryNPCSpawn wardSpawn : _territoryWardSpawnPlaces)
  1391. if (wardSpawn.getNpcId() > 0)
  1392. ret.add(wardSpawn.getNpcId());
  1393. return ret;
  1394. }
  1395. public boolean getIsInProgress()
  1396. {
  1397. return _isInProgress;
  1398. }
  1399. public void setIsInProgress(boolean val)
  1400. {
  1401. _isInProgress = val;
  1402. }
  1403. }
  1404. @SuppressWarnings("synthetic-access")
  1405. private static class SingletonHolder
  1406. {
  1407. protected static final TerritoryWarManager _instance = new TerritoryWarManager();
  1408. }
  1409. @Override
  1410. public void startSiege()
  1411. {
  1412. // TODO Auto-generated method stub
  1413. throw new UnsupportedOperationException();
  1414. }
  1415. @Override
  1416. public void endSiege()
  1417. {
  1418. // TODO Auto-generated method stub
  1419. throw new UnsupportedOperationException();
  1420. }
  1421. @Override
  1422. public L2SiegeClan getAttackerClan(int clanId)
  1423. {
  1424. throw new UnsupportedOperationException();
  1425. }
  1426. @Override
  1427. public L2SiegeClan getAttackerClan(L2Clan clan)
  1428. {
  1429. // TODO Auto-generated method stub
  1430. throw new UnsupportedOperationException();
  1431. }
  1432. @Override
  1433. public List<L2SiegeClan> getAttackerClans()
  1434. {
  1435. // TODO Auto-generated method stub
  1436. throw new UnsupportedOperationException();
  1437. }
  1438. @Override
  1439. public List<L2PcInstance> getAttackersInZone()
  1440. {
  1441. // TODO Auto-generated method stub
  1442. throw new UnsupportedOperationException();
  1443. }
  1444. @Override
  1445. public boolean checkIsAttacker(L2Clan clan)
  1446. {
  1447. // TODO Auto-generated method stub
  1448. throw new UnsupportedOperationException();
  1449. }
  1450. @Override
  1451. public L2SiegeClan getDefenderClan(int clanId)
  1452. {
  1453. // TODO Auto-generated method stub
  1454. throw new UnsupportedOperationException();
  1455. }
  1456. @Override
  1457. public L2SiegeClan getDefenderClan(L2Clan clan)
  1458. {
  1459. // TODO Auto-generated method stub
  1460. throw new UnsupportedOperationException();
  1461. }
  1462. @Override
  1463. public List<L2SiegeClan> getDefenderClans()
  1464. {
  1465. // TODO Auto-generated method stub
  1466. throw new UnsupportedOperationException();
  1467. }
  1468. @Override
  1469. public boolean checkIsDefender(L2Clan clan)
  1470. {
  1471. // TODO Auto-generated method stub
  1472. throw new UnsupportedOperationException();
  1473. }
  1474. @Override
  1475. public List<L2Npc> getFlag(L2Clan clan)
  1476. {
  1477. // TODO Auto-generated method stub
  1478. throw new UnsupportedOperationException();
  1479. }
  1480. @Override
  1481. public Calendar getSiegeDate()
  1482. {
  1483. // TODO Auto-generated method stub
  1484. throw new UnsupportedOperationException();
  1485. }
  1486. @Override
  1487. public boolean giveFame()
  1488. {
  1489. return true;
  1490. }
  1491. @Override
  1492. public int getFameFrequency()
  1493. {
  1494. return Config.CASTLE_ZONE_FAME_TASK_FREQUENCY;
  1495. }
  1496. @Override
  1497. public int getFameAmount()
  1498. {
  1499. return Config.CASTLE_ZONE_FAME_AQUIRE_POINTS;
  1500. }
  1501. @Override
  1502. public void updateSiege()
  1503. {
  1504. }
  1505. }