TvTEvent.java 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.entity;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.cache.HtmCache;
  29. import com.l2jserver.gameserver.data.xml.impl.DoorData;
  30. import com.l2jserver.gameserver.datatables.ItemTable;
  31. import com.l2jserver.gameserver.datatables.SkillData;
  32. import com.l2jserver.gameserver.datatables.SpawnTable;
  33. import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
  34. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  35. import com.l2jserver.gameserver.model.L2Spawn;
  36. import com.l2jserver.gameserver.model.L2World;
  37. import com.l2jserver.gameserver.model.actor.L2Character;
  38. import com.l2jserver.gameserver.model.actor.L2Npc;
  39. import com.l2jserver.gameserver.model.actor.L2Summon;
  40. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  41. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  42. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  43. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  44. import com.l2jserver.gameserver.model.events.EventDispatcher;
  45. import com.l2jserver.gameserver.model.events.impl.events.OnTvTEventFinish;
  46. import com.l2jserver.gameserver.model.events.impl.events.OnTvTEventKill;
  47. import com.l2jserver.gameserver.model.events.impl.events.OnTvTEventRegistrationStart;
  48. import com.l2jserver.gameserver.model.events.impl.events.OnTvTEventStart;
  49. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  50. import com.l2jserver.gameserver.model.skills.Skill;
  51. import com.l2jserver.gameserver.network.SystemMessageId;
  52. import com.l2jserver.gameserver.network.clientpackets.Say2;
  53. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  54. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  55. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  56. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  57. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  58. import com.l2jserver.util.Rnd;
  59. import com.l2jserver.util.StringUtil;
  60. /**
  61. * @author HorridoJoho
  62. */
  63. public class TvTEvent
  64. {
  65. enum EventState
  66. {
  67. INACTIVE,
  68. INACTIVATING,
  69. PARTICIPATING,
  70. STARTING,
  71. STARTED,
  72. REWARDING
  73. }
  74. protected static final Logger _log = Logger.getLogger(TvTEvent.class.getName());
  75. /** html path **/
  76. private static final String htmlPath = "data/scripts/custom/events/TvT/TvTManager/";
  77. /** The teams of the TvTEvent<br> */
  78. private static TvTEventTeam[] _teams = new TvTEventTeam[2];
  79. /** The state of the TvTEvent<br> */
  80. private static EventState _state = EventState.INACTIVE;
  81. /** The spawn of the participation npc<br> */
  82. private static L2Spawn _npcSpawn = null;
  83. /** the npc instance of the participation npc<br> */
  84. private static L2Npc _lastNpcSpawn = null;
  85. /** Instance id<br> */
  86. private static int _TvTEventInstance = 0;
  87. /**
  88. * No instance of this class!<br>
  89. */
  90. private TvTEvent()
  91. {
  92. }
  93. /**
  94. * Teams initializing<br>
  95. */
  96. public static void init()
  97. {
  98. AntiFeedManager.getInstance().registerEvent(AntiFeedManager.TVT_ID);
  99. _teams[0] = new TvTEventTeam(Config.TVT_EVENT_TEAM_1_NAME, Config.TVT_EVENT_TEAM_1_COORDINATES);
  100. _teams[1] = new TvTEventTeam(Config.TVT_EVENT_TEAM_2_NAME, Config.TVT_EVENT_TEAM_2_COORDINATES);
  101. }
  102. /**
  103. * Starts the participation of the TvTEvent<br>
  104. * 1. Get L2NpcTemplate by Config.TVT_EVENT_PARTICIPATION_NPC_ID<br>
  105. * 2. Try to spawn a new npc of it<br>
  106. * <br>
  107. * @return boolean: true if success, otherwise false<br>
  108. */
  109. public static boolean startParticipation()
  110. {
  111. try
  112. {
  113. _npcSpawn = new L2Spawn(Config.TVT_EVENT_PARTICIPATION_NPC_ID);
  114. _npcSpawn.setX(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0]);
  115. _npcSpawn.setY(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1]);
  116. _npcSpawn.setZ(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  117. _npcSpawn.setAmount(1);
  118. _npcSpawn.setHeading(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[3]);
  119. _npcSpawn.setRespawnDelay(1);
  120. // later no need to delete spawn from db, we don't store it (false)
  121. SpawnTable.getInstance().addNewSpawn(_npcSpawn, false);
  122. _npcSpawn.init();
  123. _lastNpcSpawn = _npcSpawn.getLastSpawn();
  124. _lastNpcSpawn.setCurrentHp(_lastNpcSpawn.getMaxHp());
  125. _lastNpcSpawn.setTitle("TvT Event Participation");
  126. _lastNpcSpawn.isAggressive();
  127. _lastNpcSpawn.decayMe();
  128. _lastNpcSpawn.spawnMe(_npcSpawn.getLastSpawn().getX(), _npcSpawn.getLastSpawn().getY(), _npcSpawn.getLastSpawn().getZ());
  129. _lastNpcSpawn.broadcastPacket(new MagicSkillUse(_lastNpcSpawn, _lastNpcSpawn, 1034, 1, 1, 1));
  130. }
  131. catch (Exception e)
  132. {
  133. _log.log(Level.WARNING, "TvTEventEngine[TvTEvent.startParticipation()]: exception: " + e.getMessage(), e);
  134. return false;
  135. }
  136. setState(EventState.PARTICIPATING);
  137. EventDispatcher.getInstance().notifyEventAsync(new OnTvTEventRegistrationStart());
  138. return true;
  139. }
  140. private static int highestLevelPcInstanceOf(Map<Integer, L2PcInstance> players)
  141. {
  142. int maxLevel = Integer.MIN_VALUE, maxLevelId = -1;
  143. for (L2PcInstance player : players.values())
  144. {
  145. if (player.getLevel() >= maxLevel)
  146. {
  147. maxLevel = player.getLevel();
  148. maxLevelId = player.getObjectId();
  149. }
  150. }
  151. return maxLevelId;
  152. }
  153. /**
  154. * Starts the TvTEvent fight<br>
  155. * 1. Set state EventState.STARTING<br>
  156. * 2. Close doors specified in configs<br>
  157. * 3. Abort if not enought participants(return false)<br>
  158. * 4. Set state EventState.STARTED<br>
  159. * 5. Teleport all participants to team spot<br>
  160. * <br>
  161. * @return boolean: true if success, otherwise false<br>
  162. */
  163. public static boolean startFight()
  164. {
  165. // Set state to STARTING
  166. setState(EventState.STARTING);
  167. // Randomize and balance team distribution
  168. Map<Integer, L2PcInstance> allParticipants = new FastMap<>();
  169. allParticipants.putAll(_teams[0].getParticipatedPlayers());
  170. allParticipants.putAll(_teams[1].getParticipatedPlayers());
  171. _teams[0].cleanMe();
  172. _teams[1].cleanMe();
  173. L2PcInstance player;
  174. Iterator<L2PcInstance> iter;
  175. if (needParticipationFee())
  176. {
  177. iter = allParticipants.values().iterator();
  178. while (iter.hasNext())
  179. {
  180. player = iter.next();
  181. if (!hasParticipationFee(player))
  182. {
  183. iter.remove();
  184. }
  185. }
  186. }
  187. int balance[] =
  188. {
  189. 0,
  190. 0
  191. }, priority = 0, highestLevelPlayerId;
  192. L2PcInstance highestLevelPlayer;
  193. // TODO: allParticipants should be sorted by level instead of using highestLevelPcInstanceOf for every fetch
  194. while (!allParticipants.isEmpty())
  195. {
  196. // Priority team gets one player
  197. highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
  198. highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
  199. allParticipants.remove(highestLevelPlayerId);
  200. _teams[priority].addPlayer(highestLevelPlayer);
  201. balance[priority] += highestLevelPlayer.getLevel();
  202. // Exiting if no more players
  203. if (allParticipants.isEmpty())
  204. {
  205. break;
  206. }
  207. // The other team gets one player
  208. // TODO: Code not dry
  209. priority = 1 - priority;
  210. highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
  211. highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
  212. allParticipants.remove(highestLevelPlayerId);
  213. _teams[priority].addPlayer(highestLevelPlayer);
  214. balance[priority] += highestLevelPlayer.getLevel();
  215. // Recalculating priority
  216. priority = balance[0] > balance[1] ? 1 : 0;
  217. }
  218. // Check for enought participants
  219. if ((_teams[0].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS) || (_teams[1].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS))
  220. {
  221. // Set state INACTIVE
  222. setState(EventState.INACTIVE);
  223. // Cleanup of teams
  224. _teams[0].cleanMe();
  225. _teams[1].cleanMe();
  226. // Unspawn the event NPC
  227. unSpawnNpc();
  228. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  229. return false;
  230. }
  231. if (needParticipationFee())
  232. {
  233. iter = _teams[0].getParticipatedPlayers().values().iterator();
  234. while (iter.hasNext())
  235. {
  236. player = iter.next();
  237. if (!payParticipationFee(player))
  238. {
  239. iter.remove();
  240. }
  241. }
  242. iter = _teams[1].getParticipatedPlayers().values().iterator();
  243. while (iter.hasNext())
  244. {
  245. player = iter.next();
  246. if (!payParticipationFee(player))
  247. {
  248. iter.remove();
  249. }
  250. }
  251. }
  252. if (Config.TVT_EVENT_IN_INSTANCE)
  253. {
  254. try
  255. {
  256. _TvTEventInstance = InstanceManager.getInstance().createDynamicInstance(Config.TVT_EVENT_INSTANCE_FILE);
  257. InstanceManager.getInstance().getInstance(_TvTEventInstance).setAllowSummon(false);
  258. InstanceManager.getInstance().getInstance(_TvTEventInstance).setPvPInstance(true);
  259. InstanceManager.getInstance().getInstance(_TvTEventInstance).setEmptyDestroyTime((Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY * 1000) + 60000L);
  260. }
  261. catch (Exception e)
  262. {
  263. _TvTEventInstance = 0;
  264. _log.log(Level.WARNING, "TvTEventEngine[TvTEvent.createDynamicInstance]: exception: " + e.getMessage(), e);
  265. }
  266. }
  267. // Opens all doors specified in configs for tvt
  268. openDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  269. // Closes all doors specified in configs for tvt
  270. closeDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  271. // Set state STARTED
  272. setState(EventState.STARTED);
  273. // Iterate over all teams
  274. for (TvTEventTeam team : _teams)
  275. {
  276. // Iterate over all participated player instances in this team
  277. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  278. {
  279. if (playerInstance != null)
  280. {
  281. // Disable player revival.
  282. playerInstance.setCanRevive(false);
  283. // Teleporter implements Runnable and starts itself
  284. new TvTEventTeleporter(playerInstance, team.getCoordinates(), false, false);
  285. }
  286. }
  287. }
  288. // Notify to scripts.
  289. EventDispatcher.getInstance().notifyEventAsync(new OnTvTEventStart());
  290. return true;
  291. }
  292. /**
  293. * Calculates the TvTEvent reward<br>
  294. * 1. If both teams are at a tie(points equals), send it as system message to all participants, if one of the teams have 0 participants left online abort rewarding<br>
  295. * 2. Wait till teams are not at a tie anymore<br>
  296. * 3. Set state EvcentState.REWARDING<br>
  297. * 4. Reward team with more points<br>
  298. * 5. Show win html to wining team participants<br>
  299. * <br>
  300. * @return String: winning team name<br>
  301. */
  302. public static String calculateRewards()
  303. {
  304. if (_teams[0].getPoints() == _teams[1].getPoints())
  305. {
  306. // Check if one of the teams have no more players left
  307. if ((_teams[0].getParticipatedPlayerCount() == 0) || (_teams[1].getParticipatedPlayerCount() == 0))
  308. {
  309. // set state to rewarding
  310. setState(EventState.REWARDING);
  311. // return here, the fight can't be completed
  312. return "TvT Event: Event has ended. No team won due to inactivity!";
  313. }
  314. // Both teams have equals points
  315. sysMsgToAllParticipants("TvT Event: Event has ended, both teams have tied.");
  316. if (Config.TVT_REWARD_TEAM_TIE)
  317. {
  318. rewardTeam(_teams[0]);
  319. rewardTeam(_teams[1]);
  320. return "TvT Event: Event has ended with both teams tying.";
  321. }
  322. return "TvT Event: Event has ended with both teams tying.";
  323. }
  324. // Set state REWARDING so nobody can point anymore
  325. setState(EventState.REWARDING);
  326. // Get team which has more points
  327. TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
  328. rewardTeam(team);
  329. // Notify to scripts.
  330. EventDispatcher.getInstance().notifyEventAsync(new OnTvTEventFinish());
  331. return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
  332. }
  333. private static void rewardTeam(TvTEventTeam team)
  334. {
  335. // Iterate over all participated player instances of the winning team
  336. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  337. {
  338. // Check for nullpointer
  339. if (playerInstance == null)
  340. {
  341. continue;
  342. }
  343. SystemMessage systemMessage = null;
  344. // Iterate over all tvt event rewards
  345. for (int[] reward : Config.TVT_EVENT_REWARDS)
  346. {
  347. PcInventory inv = playerInstance.getInventory();
  348. // Check for stackable item, non stackabe items need to be added one by one
  349. if (ItemTable.getInstance().getTemplate(reward[0]).isStackable())
  350. {
  351. inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
  352. if (reward[1] > 1)
  353. {
  354. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  355. systemMessage.addItemName(reward[0]);
  356. systemMessage.addLong(reward[1]);
  357. }
  358. else
  359. {
  360. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  361. systemMessage.addItemName(reward[0]);
  362. }
  363. playerInstance.sendPacket(systemMessage);
  364. }
  365. else
  366. {
  367. for (int i = 0; i < reward[1]; ++i)
  368. {
  369. inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
  370. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  371. systemMessage.addItemName(reward[0]);
  372. playerInstance.sendPacket(systemMessage);
  373. }
  374. }
  375. }
  376. StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
  377. final NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage();
  378. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  379. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Reward.html"));
  380. playerInstance.sendPacket(statusUpdate);
  381. playerInstance.sendPacket(npcHtmlMessage);
  382. }
  383. }
  384. /**
  385. * Stops the TvTEvent fight<br>
  386. * 1. Set state EventState.INACTIVATING<br>
  387. * 2. Remove tvt npc from world<br>
  388. * 3. Open doors specified in configs<br>
  389. * 4. Teleport all participants back to participation npc location<br>
  390. * 5. Teams cleaning<br>
  391. * 6. Set state EventState.INACTIVE<br>
  392. */
  393. public static void stopFight()
  394. {
  395. // Set state INACTIVATING
  396. setState(EventState.INACTIVATING);
  397. // Unspawn event npc
  398. unSpawnNpc();
  399. // Opens all doors specified in configs for tvt
  400. openDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  401. // Closes all doors specified in Configs for tvt
  402. closeDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  403. // Iterate over all teams
  404. for (TvTEventTeam team : _teams)
  405. {
  406. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  407. {
  408. // Check for nullpointer
  409. if (playerInstance != null)
  410. {
  411. // Enable player revival.
  412. playerInstance.setCanRevive(true);
  413. // Teleport back.
  414. new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false, false);
  415. }
  416. }
  417. }
  418. // Cleanup of teams
  419. _teams[0].cleanMe();
  420. _teams[1].cleanMe();
  421. // Set state INACTIVE
  422. setState(EventState.INACTIVE);
  423. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  424. }
  425. /**
  426. * Adds a player to a TvTEvent team<br>
  427. * 1. Calculate the id of the team in which the player should be added<br>
  428. * 2. Add the player to the calculated team<br>
  429. * <br>
  430. * @param playerInstance as L2PcInstance<br>
  431. * @return boolean: true if success, otherwise false<br>
  432. */
  433. public static synchronized boolean addParticipant(L2PcInstance playerInstance)
  434. {
  435. // Check for nullpoitner
  436. if (playerInstance == null)
  437. {
  438. return false;
  439. }
  440. byte teamId = 0;
  441. // Check to which team the player should be added
  442. if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
  443. {
  444. teamId = (byte) (Rnd.get(2));
  445. }
  446. else
  447. {
  448. teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
  449. }
  450. playerInstance.addEventListener(new TvTEventListener(playerInstance));
  451. return _teams[teamId].addPlayer(playerInstance);
  452. }
  453. /**
  454. * Removes a TvTEvent player from it's team<br>
  455. * 1. Get team id of the player<br>
  456. * 2. Remove player from it's team<br>
  457. * <br>
  458. * @param playerObjectId
  459. * @return boolean: true if success, otherwise false
  460. */
  461. public static boolean removeParticipant(int playerObjectId)
  462. {
  463. // Get the teamId of the player
  464. byte teamId = getParticipantTeamId(playerObjectId);
  465. // Check if the player is participant
  466. if (teamId != -1)
  467. {
  468. // Remove the player from team
  469. _teams[teamId].removePlayer(playerObjectId);
  470. final L2PcInstance player = L2World.getInstance().getPlayer(playerObjectId);
  471. if (player != null)
  472. {
  473. player.removeEventListener(TvTEventListener.class);
  474. }
  475. return true;
  476. }
  477. return false;
  478. }
  479. public static boolean needParticipationFee()
  480. {
  481. return (Config.TVT_EVENT_PARTICIPATION_FEE[0] != 0) && (Config.TVT_EVENT_PARTICIPATION_FEE[1] != 0);
  482. }
  483. public static boolean hasParticipationFee(L2PcInstance playerInstance)
  484. {
  485. return playerInstance.getInventory().getInventoryItemCount(Config.TVT_EVENT_PARTICIPATION_FEE[0], -1) >= Config.TVT_EVENT_PARTICIPATION_FEE[1];
  486. }
  487. public static boolean payParticipationFee(L2PcInstance playerInstance)
  488. {
  489. return playerInstance.destroyItemByItemId("TvT Participation Fee", Config.TVT_EVENT_PARTICIPATION_FEE[0], Config.TVT_EVENT_PARTICIPATION_FEE[1], _lastNpcSpawn, true);
  490. }
  491. public static String getParticipationFee()
  492. {
  493. int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
  494. int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
  495. if ((itemId == 0) || (itemNum == 0))
  496. {
  497. return "-";
  498. }
  499. return StringUtil.concat(String.valueOf(itemNum), " ", ItemTable.getInstance().getTemplate(itemId).getName());
  500. }
  501. /**
  502. * Send a SystemMessage to all participated players<br>
  503. * 1. Send the message to all players of team number one<br>
  504. * 2. Send the message to all players of team number two<br>
  505. * <br>
  506. * @param message as String<br>
  507. */
  508. public static void sysMsgToAllParticipants(String message)
  509. {
  510. for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
  511. {
  512. if (playerInstance != null)
  513. {
  514. playerInstance.sendMessage(message);
  515. }
  516. }
  517. for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
  518. {
  519. if (playerInstance != null)
  520. {
  521. playerInstance.sendMessage(message);
  522. }
  523. }
  524. }
  525. private static L2DoorInstance getDoor(int doorId)
  526. {
  527. L2DoorInstance door = null;
  528. if (_TvTEventInstance <= 0)
  529. {
  530. door = DoorData.getInstance().getDoor(doorId);
  531. }
  532. else
  533. {
  534. final Instance inst = InstanceManager.getInstance().getInstance(_TvTEventInstance);
  535. if (inst != null)
  536. {
  537. door = inst.getDoor(doorId);
  538. }
  539. }
  540. return door;
  541. }
  542. /**
  543. * Close doors specified in configs
  544. * @param doors
  545. */
  546. private static void closeDoors(List<Integer> doors)
  547. {
  548. for (int doorId : doors)
  549. {
  550. final L2DoorInstance doorInstance = getDoor(doorId);
  551. if (doorInstance != null)
  552. {
  553. doorInstance.closeMe();
  554. }
  555. }
  556. }
  557. /**
  558. * Open doors specified in configs
  559. * @param doors
  560. */
  561. private static void openDoors(List<Integer> doors)
  562. {
  563. for (int doorId : doors)
  564. {
  565. final L2DoorInstance doorInstance = getDoor(doorId);
  566. if (doorInstance != null)
  567. {
  568. doorInstance.openMe();
  569. }
  570. }
  571. }
  572. /**
  573. * UnSpawns the TvTEvent npc
  574. */
  575. private static void unSpawnNpc()
  576. {
  577. // Delete the npc
  578. _lastNpcSpawn.deleteMe();
  579. SpawnTable.getInstance().deleteSpawn(_lastNpcSpawn.getSpawn(), false);
  580. // Stop respawning of the npc
  581. _npcSpawn.stopRespawn();
  582. _npcSpawn = null;
  583. _lastNpcSpawn = null;
  584. }
  585. /**
  586. * Called when a player logs in<br>
  587. * <br>
  588. * @param playerInstance as L2PcInstance<br>
  589. */
  590. public static void onLogin(L2PcInstance playerInstance)
  591. {
  592. if ((playerInstance == null) || (!isStarting() && !isStarted()))
  593. {
  594. return;
  595. }
  596. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  597. if (teamId == -1)
  598. {
  599. return;
  600. }
  601. _teams[teamId].addPlayer(playerInstance);
  602. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
  603. }
  604. /**
  605. * Called when a player logs out<br>
  606. * <br>
  607. * @param playerInstance as L2PcInstance<br>
  608. */
  609. public static void onLogout(L2PcInstance playerInstance)
  610. {
  611. if ((playerInstance != null) && (isStarting() || isStarted() || isParticipating()))
  612. {
  613. if (removeParticipant(playerInstance.getObjectId()))
  614. {
  615. playerInstance.setXYZInvisible((Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)) - 50, (Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)) - 50, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  616. }
  617. }
  618. }
  619. /**
  620. * Called on every onAction in L2PcIstance<br>
  621. * <br>
  622. * @param playerInstance
  623. * @param targetedPlayerObjectId
  624. * @return boolean: true if player is allowed to target, otherwise false
  625. */
  626. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  627. {
  628. if ((playerInstance == null) || !isStarted())
  629. {
  630. return true;
  631. }
  632. if (playerInstance.isGM())
  633. {
  634. return true;
  635. }
  636. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  637. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  638. if (((playerTeamId != -1) && (targetedPlayerTeamId == -1)) || ((playerTeamId == -1) && (targetedPlayerTeamId != -1)))
  639. {
  640. return false;
  641. }
  642. if ((playerTeamId != -1) && (targetedPlayerTeamId != -1) && (playerTeamId == targetedPlayerTeamId) && (playerInstance.getObjectId() != targetedPlayerObjectId) && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  643. {
  644. return false;
  645. }
  646. return true;
  647. }
  648. /**
  649. * Called on every scroll use<br>
  650. * <br>
  651. * @param playerObjectId
  652. * @return boolean: true if player is allowed to use scroll, otherwise false
  653. */
  654. public static boolean onScrollUse(int playerObjectId)
  655. {
  656. if (!isStarted())
  657. {
  658. return true;
  659. }
  660. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  661. {
  662. return false;
  663. }
  664. return true;
  665. }
  666. /**
  667. * Called on every potion use
  668. * @param playerObjectId
  669. * @return boolean: true if player is allowed to use potions, otherwise false
  670. */
  671. public static boolean onPotionUse(int playerObjectId)
  672. {
  673. if (!isStarted())
  674. {
  675. return true;
  676. }
  677. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  678. {
  679. return false;
  680. }
  681. return true;
  682. }
  683. /**
  684. * Called on every escape use(thanks to nbd)
  685. * @param playerObjectId
  686. * @return boolean: true if player is not in tvt event, otherwise false
  687. */
  688. public static boolean onEscapeUse(int playerObjectId)
  689. {
  690. if (!isStarted())
  691. {
  692. return true;
  693. }
  694. if (isPlayerParticipant(playerObjectId))
  695. {
  696. return false;
  697. }
  698. return true;
  699. }
  700. /**
  701. * Called on every summon item use
  702. * @param playerObjectId
  703. * @return boolean: true if player is allowed to summon by item, otherwise false
  704. */
  705. public static boolean onItemSummon(int playerObjectId)
  706. {
  707. if (!isStarted())
  708. {
  709. return true;
  710. }
  711. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  712. {
  713. return false;
  714. }
  715. return true;
  716. }
  717. /**
  718. * Is called when a player is killed<br>
  719. * <br>
  720. * @param killerCharacter as L2Character<br>
  721. * @param killedPlayerInstance as L2PcInstance<br>
  722. */
  723. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  724. {
  725. if ((killedPlayerInstance == null) || !isStarted())
  726. {
  727. return;
  728. }
  729. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  730. if (killedTeamId == -1)
  731. {
  732. return;
  733. }
  734. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
  735. if (killerCharacter == null)
  736. {
  737. return;
  738. }
  739. L2PcInstance killerPlayerInstance = null;
  740. if ((killerCharacter instanceof L2PetInstance) || (killerCharacter instanceof L2ServitorInstance))
  741. {
  742. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  743. if (killerPlayerInstance == null)
  744. {
  745. return;
  746. }
  747. }
  748. else if (killerCharacter instanceof L2PcInstance)
  749. {
  750. killerPlayerInstance = (L2PcInstance) killerCharacter;
  751. }
  752. else
  753. {
  754. return;
  755. }
  756. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  757. if ((killerTeamId != -1) && (killedTeamId != -1) && (killerTeamId != killedTeamId))
  758. {
  759. TvTEventTeam killerTeam = _teams[killerTeamId];
  760. killerTeam.increasePoints();
  761. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  762. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  763. {
  764. if (playerInstance != null)
  765. {
  766. playerInstance.sendPacket(cs);
  767. }
  768. }
  769. // Notify to scripts.
  770. EventDispatcher.getInstance().notifyEventAsync(new OnTvTEventKill(killerPlayerInstance, killedPlayerInstance, killerTeam));
  771. }
  772. }
  773. /**
  774. * Called on Appearing packet received (player finished teleporting)
  775. * @param playerInstance
  776. */
  777. public static void onTeleported(L2PcInstance playerInstance)
  778. {
  779. if (!isStarted() || (playerInstance == null) || !isPlayerParticipant(playerInstance.getObjectId()))
  780. {
  781. return;
  782. }
  783. if (playerInstance.isMageClass())
  784. {
  785. if ((Config.TVT_EVENT_MAGE_BUFFS != null) && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
  786. {
  787. for (Entry<Integer, Integer> e : Config.TVT_EVENT_MAGE_BUFFS.entrySet())
  788. {
  789. Skill skill = SkillData.getInstance().getSkill(e.getKey(), e.getValue());
  790. if (skill != null)
  791. {
  792. skill.applyEffects(playerInstance, playerInstance);
  793. }
  794. }
  795. }
  796. }
  797. else
  798. {
  799. if ((Config.TVT_EVENT_FIGHTER_BUFFS != null) && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
  800. {
  801. for (Entry<Integer, Integer> e : Config.TVT_EVENT_FIGHTER_BUFFS.entrySet())
  802. {
  803. Skill skill = SkillData.getInstance().getSkill(e.getKey(), e.getValue());
  804. if (skill != null)
  805. {
  806. skill.applyEffects(playerInstance, playerInstance);
  807. }
  808. }
  809. }
  810. }
  811. }
  812. /**
  813. * @param source
  814. * @param target
  815. * @param skill
  816. * @return true if player valid for skill
  817. */
  818. public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, Skill skill)
  819. {
  820. if (!isStarted())
  821. {
  822. return true;
  823. }
  824. // TvT is started
  825. final int sourcePlayerId = source.getObjectId();
  826. final int targetPlayerId = target.getObjectId();
  827. final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
  828. final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
  829. // both players not participating
  830. if (!isSourceParticipant && !isTargetParticipant)
  831. {
  832. return true;
  833. }
  834. // one player not participating
  835. if (!(isSourceParticipant && isTargetParticipant))
  836. {
  837. return false;
  838. }
  839. // players in the different teams ?
  840. if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
  841. {
  842. if (!skill.isBad())
  843. {
  844. return false;
  845. }
  846. }
  847. return true;
  848. }
  849. /**
  850. * Sets the TvTEvent state<br>
  851. * <br>
  852. * @param state as EventState<br>
  853. */
  854. private static void setState(EventState state)
  855. {
  856. synchronized (_state)
  857. {
  858. _state = state;
  859. }
  860. }
  861. /**
  862. * Is TvTEvent inactive?<br>
  863. * <br>
  864. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  865. */
  866. public static boolean isInactive()
  867. {
  868. boolean isInactive;
  869. synchronized (_state)
  870. {
  871. isInactive = _state == EventState.INACTIVE;
  872. }
  873. return isInactive;
  874. }
  875. /**
  876. * Is TvTEvent in inactivating?<br>
  877. * <br>
  878. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  879. */
  880. public static boolean isInactivating()
  881. {
  882. boolean isInactivating;
  883. synchronized (_state)
  884. {
  885. isInactivating = _state == EventState.INACTIVATING;
  886. }
  887. return isInactivating;
  888. }
  889. /**
  890. * Is TvTEvent in participation?<br>
  891. * <br>
  892. * @return boolean: true if event is in participation progress, otherwise false<br>
  893. */
  894. public static boolean isParticipating()
  895. {
  896. boolean isParticipating;
  897. synchronized (_state)
  898. {
  899. isParticipating = _state == EventState.PARTICIPATING;
  900. }
  901. return isParticipating;
  902. }
  903. /**
  904. * Is TvTEvent starting?<br>
  905. * <br>
  906. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  907. */
  908. public static boolean isStarting()
  909. {
  910. boolean isStarting;
  911. synchronized (_state)
  912. {
  913. isStarting = _state == EventState.STARTING;
  914. }
  915. return isStarting;
  916. }
  917. /**
  918. * Is TvTEvent started?<br>
  919. * <br>
  920. * @return boolean: true if event is started, otherwise false<br>
  921. */
  922. public static boolean isStarted()
  923. {
  924. boolean isStarted;
  925. synchronized (_state)
  926. {
  927. isStarted = _state == EventState.STARTED;
  928. }
  929. return isStarted;
  930. }
  931. /**
  932. * Is TvTEvent rewarding?<br>
  933. * <br>
  934. * @return boolean: true if event is currently rewarding, otherwise false<br>
  935. */
  936. public static boolean isRewarding()
  937. {
  938. boolean isRewarding;
  939. synchronized (_state)
  940. {
  941. isRewarding = _state == EventState.REWARDING;
  942. }
  943. return isRewarding;
  944. }
  945. /**
  946. * Returns the team id of a player, if player is not participant it returns -1
  947. * @param playerObjectId
  948. * @return byte: team name of the given playerName, if not in event -1
  949. */
  950. public static byte getParticipantTeamId(int playerObjectId)
  951. {
  952. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  953. }
  954. /**
  955. * Returns the team of a player, if player is not participant it returns null
  956. * @param playerObjectId
  957. * @return TvTEventTeam: team of the given playerObjectId, if not in event null
  958. */
  959. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  960. {
  961. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  962. }
  963. /**
  964. * Returns the enemy team of a player, if player is not participant it returns null
  965. * @param playerObjectId
  966. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null
  967. */
  968. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  969. {
  970. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  971. }
  972. /**
  973. * Returns the team coordinates in which the player is in, if player is not in a team return null
  974. * @param playerObjectId
  975. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2
  976. */
  977. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  978. {
  979. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  980. }
  981. /**
  982. * Is given player participant of the event?
  983. * @param playerObjectId
  984. * @return boolean: true if player is participant, ohterwise false
  985. */
  986. public static boolean isPlayerParticipant(int playerObjectId)
  987. {
  988. if (!isParticipating() && !isStarting() && !isStarted())
  989. {
  990. return false;
  991. }
  992. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  993. }
  994. /**
  995. * Returns participated player count<br>
  996. * <br>
  997. * @return int: amount of players registered in the event<br>
  998. */
  999. public static int getParticipatedPlayersCount()
  1000. {
  1001. if (!isParticipating() && !isStarting() && !isStarted())
  1002. {
  1003. return 0;
  1004. }
  1005. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  1006. }
  1007. /**
  1008. * Returns teams names<br>
  1009. * <br>
  1010. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1011. */
  1012. public static String[] getTeamNames()
  1013. {
  1014. return new String[]
  1015. {
  1016. _teams[0].getName(),
  1017. _teams[1].getName()
  1018. };
  1019. }
  1020. /**
  1021. * Returns player count of both teams<br>
  1022. * <br>
  1023. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1024. */
  1025. public static int[] getTeamsPlayerCounts()
  1026. {
  1027. return new int[]
  1028. {
  1029. _teams[0].getParticipatedPlayerCount(),
  1030. _teams[1].getParticipatedPlayerCount()
  1031. };
  1032. }
  1033. /**
  1034. * Returns points count of both teams
  1035. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1036. */
  1037. public static int[] getTeamsPoints()
  1038. {
  1039. return new int[]
  1040. {
  1041. _teams[0].getPoints(),
  1042. _teams[1].getPoints()
  1043. };
  1044. }
  1045. public static int getTvTEventInstance()
  1046. {
  1047. return _TvTEventInstance;
  1048. }
  1049. }