TvTEvent.java 33 KB

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