TvTEvent.java 36 KB

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