TvTEvent.java 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. else
  303. return "TvT Event: Event has ended with both teams tying.";
  304. }
  305. // Set state REWARDING so nobody can point anymore
  306. setState(EventState.REWARDING);
  307. // Get team which has more points
  308. TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
  309. rewardTeam(team);
  310. return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
  311. }
  312. private static void rewardTeam(TvTEventTeam team)
  313. {
  314. // Iterate over all participated player instances of the winning team
  315. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  316. {
  317. // Check for nullpointer
  318. if (playerInstance == null)
  319. {
  320. continue;
  321. }
  322. SystemMessage systemMessage = null;
  323. // Iterate over all tvt event rewards
  324. for (int[] reward : Config.TVT_EVENT_REWARDS)
  325. {
  326. PcInventory inv = playerInstance.getInventory();
  327. // Check for stackable item, non stackabe items need to be added one by one
  328. if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  329. {
  330. inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
  331. if (reward[1] > 1)
  332. {
  333. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  334. systemMessage.addItemName(reward[0]);
  335. systemMessage.addItemNumber(reward[1]);
  336. }
  337. else
  338. {
  339. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  340. systemMessage.addItemName(reward[0]);
  341. }
  342. playerInstance.sendPacket(systemMessage);
  343. }
  344. else
  345. {
  346. for (int i = 0; i < reward[1]; ++i)
  347. {
  348. inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
  349. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  350. systemMessage.addItemName(reward[0]);
  351. playerInstance.sendPacket(systemMessage);
  352. }
  353. }
  354. }
  355. StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
  356. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  357. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  358. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"Reward.htm"));
  359. playerInstance.sendPacket(statusUpdate);
  360. playerInstance.sendPacket(npcHtmlMessage);
  361. }
  362. }
  363. /**
  364. * Stops the TvTEvent fight<br>
  365. * 1. Set state EventState.INACTIVATING<br>
  366. * 2. Remove tvt npc from world<br>
  367. * 3. Open doors specified in configs<br>
  368. * 4. Teleport all participants back to participation npc location<br>
  369. * 5. Teams cleaning<br>
  370. * 6. Set state EventState.INACTIVE<br>
  371. */
  372. public static void stopFight()
  373. {
  374. // Set state INACTIVATING
  375. setState(EventState.INACTIVATING);
  376. //Unspawn event npc
  377. unSpawnNpc();
  378. // Opens all doors specified in configs for tvt
  379. openDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  380. // Closes all doors specified in Configs for tvt
  381. closeDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  382. // Iterate over all teams
  383. for (TvTEventTeam team : _teams)
  384. {
  385. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  386. {
  387. // Check for nullpointer
  388. if (playerInstance != null)
  389. {
  390. new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false, false);
  391. }
  392. }
  393. }
  394. // Cleanup of teams
  395. _teams[0].cleanMe();
  396. _teams[1].cleanMe();
  397. // Set state INACTIVE
  398. setState(EventState.INACTIVE);
  399. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  400. }
  401. /**
  402. * Adds a player to a TvTEvent team<br>
  403. * 1. Calculate the id of the team in which the player should be added<br>
  404. * 2. Add the player to the calculated team<br><br>
  405. *
  406. * @param playerInstance as L2PcInstance<br>
  407. * @return boolean: true if success, otherwise false<br>
  408. */
  409. public static synchronized boolean addParticipant(L2PcInstance playerInstance)
  410. {
  411. // Check for nullpoitner
  412. if (playerInstance == null)
  413. {
  414. return false;
  415. }
  416. byte teamId = 0;
  417. // Check to which team the player should be added
  418. if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
  419. {
  420. teamId = (byte) (Rnd.get(2));
  421. }
  422. else
  423. {
  424. teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
  425. }
  426. return _teams[teamId].addPlayer(playerInstance);
  427. }
  428. /**
  429. * Removes a TvTEvent player from it's team<br>
  430. * 1. Get team id of the player<br>
  431. * 2. Remove player from it's team<br><br>
  432. *
  433. * @param playerName as String<br>
  434. * @return boolean: true if success, otherwise false<br>
  435. */
  436. public static boolean removeParticipant(int playerObjectId)
  437. {
  438. // Get the teamId of the player
  439. byte teamId = getParticipantTeamId(playerObjectId);
  440. // Check if the player is participant
  441. if (teamId != -1)
  442. {
  443. // Remove the player from team
  444. _teams[teamId].removePlayer(playerObjectId);
  445. return true;
  446. }
  447. return false;
  448. }
  449. public static boolean needParticipationFee()
  450. {
  451. return Config.TVT_EVENT_PARTICIPATION_FEE[0] != 0 && Config.TVT_EVENT_PARTICIPATION_FEE[1] != 0;
  452. }
  453. public static boolean hasParticipationFee(L2PcInstance playerInstance)
  454. {
  455. return playerInstance.getInventory().getInventoryItemCount(Config.TVT_EVENT_PARTICIPATION_FEE[0], -1) >= Config.TVT_EVENT_PARTICIPATION_FEE[1];
  456. }
  457. public static boolean payParticipationFee(L2PcInstance playerInstance)
  458. {
  459. return playerInstance.destroyItemByItemId("TvT Participation Fee", Config.TVT_EVENT_PARTICIPATION_FEE[0], Config.TVT_EVENT_PARTICIPATION_FEE[1], _lastNpcSpawn, true);
  460. }
  461. public static String getParticipationFee()
  462. {
  463. int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
  464. int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
  465. if (itemId == 0 || itemNum == 0)
  466. return "-";
  467. return StringUtil.concat(String.valueOf(itemNum), " ", ItemTable.getInstance().getTemplate(itemId).getName());
  468. }
  469. /**
  470. * Send a SystemMessage to all participated players<br>
  471. * 1. Send the message to all players of team number one<br>
  472. * 2. Send the message to all players of team number two<br><br>
  473. *
  474. * @param message as String<br>
  475. */
  476. public static void sysMsgToAllParticipants(String message)
  477. {
  478. for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
  479. {
  480. if (playerInstance != null)
  481. {
  482. playerInstance.sendMessage(message);
  483. }
  484. }
  485. for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
  486. {
  487. if (playerInstance != null)
  488. {
  489. playerInstance.sendMessage(message);
  490. }
  491. }
  492. }
  493. /**
  494. * Close doors specified in configs
  495. */
  496. private static void closeDoors(List<Integer> doors)
  497. {
  498. for (int doorId : doors)
  499. {
  500. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  501. if (doorInstance != null)
  502. {
  503. doorInstance.closeMe();
  504. }
  505. }
  506. }
  507. /**
  508. * Open doors specified in configs
  509. */
  510. private static void openDoors(List<Integer> doors)
  511. {
  512. for (int doorId : doors)
  513. {
  514. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  515. if (doorInstance != null)
  516. {
  517. doorInstance.openMe();
  518. }
  519. }
  520. }
  521. /**
  522. * UnSpawns the TvTEvent npc
  523. */
  524. private static void unSpawnNpc()
  525. {
  526. // Delete the npc
  527. _lastNpcSpawn.deleteMe();
  528. SpawnTable.getInstance().deleteSpawn(_lastNpcSpawn.getSpawn(), false);
  529. // Stop respawning of the npc
  530. _npcSpawn.stopRespawn();
  531. _npcSpawn = null;
  532. _lastNpcSpawn = null;
  533. }
  534. /**
  535. * Called when a player logs in<br><br>
  536. *
  537. * @param playerInstance as L2PcInstance<br>
  538. */
  539. public static void onLogin(L2PcInstance playerInstance)
  540. {
  541. if (playerInstance == null || (!isStarting() && !isStarted()))
  542. {
  543. return;
  544. }
  545. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  546. if (teamId == -1)
  547. {
  548. return;
  549. }
  550. _teams[teamId].addPlayer(playerInstance);
  551. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
  552. }
  553. /**
  554. * Called when a player logs out<br><br>
  555. *
  556. * @param playerInstance as L2PcInstance<br>
  557. */
  558. public static void onLogout(L2PcInstance playerInstance)
  559. {
  560. if (playerInstance != null && (isStarting() || isStarted() || isParticipating()))
  561. {
  562. if (removeParticipant(playerInstance.getObjectId()))
  563. playerInstance.setXYZInvisible(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)-50,
  564. Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)-50,
  565. Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  566. }
  567. }
  568. /**
  569. * Called on every bypass by npc of type L2TvTEventNpc<br>
  570. * Needs synchronization cause of the max player check<br><br>
  571. *
  572. * @param command as String<br>
  573. * @param playerInstance as L2PcInstance<br>
  574. */
  575. public static synchronized void onBypass(String command, L2PcInstance playerInstance)
  576. {
  577. if (playerInstance == null || !isParticipating())
  578. return;
  579. final String htmContent;
  580. if (command.equals("tvt_event_participation"))
  581. {
  582. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  583. int playerLevel = playerInstance.getLevel();
  584. if (playerInstance.isCursedWeaponEquipped())
  585. {
  586. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"CursedWeaponEquipped.htm");
  587. if (htmContent != null)
  588. npcHtmlMessage.setHtml(htmContent);
  589. }
  590. else if (OlympiadManager.getInstance().isRegistered(playerInstance))
  591. {
  592. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"Olympiad.htm");
  593. if (htmContent != null)
  594. npcHtmlMessage.setHtml(htmContent);
  595. }
  596. else if (playerInstance.getKarma() > 0)
  597. {
  598. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"Karma.htm");
  599. if (htmContent != null)
  600. npcHtmlMessage.setHtml(htmContent);
  601. }
  602. else if (playerLevel < Config.TVT_EVENT_MIN_LVL || playerLevel > Config.TVT_EVENT_MAX_LVL)
  603. {
  604. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"Level.htm");
  605. if (htmContent != null)
  606. {
  607. npcHtmlMessage.setHtml(htmContent);
  608. npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
  609. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
  610. }
  611. }
  612. else if (_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS && _teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS)
  613. {
  614. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"TeamsFull.htm");
  615. if (htmContent != null)
  616. {
  617. npcHtmlMessage.setHtml(htmContent);
  618. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
  619. }
  620. }
  621. else if (Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0
  622. && !AntiFeedManager.getInstance().tryAddPlayer(AntiFeedManager.TVT_ID, playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP))
  623. {
  624. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"IPRestriction.htm");
  625. if (htmContent != null)
  626. {
  627. npcHtmlMessage.setHtml(htmContent);
  628. npcHtmlMessage.replace("%max%", String.valueOf(AntiFeedManager.getInstance().getLimit(playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP)));
  629. }
  630. }
  631. else if (needParticipationFee() && !hasParticipationFee(playerInstance))
  632. {
  633. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"ParticipationFee.htm");
  634. if (htmContent != null)
  635. {
  636. npcHtmlMessage.setHtml(htmContent);
  637. npcHtmlMessage.replace("%fee%", getParticipationFee());
  638. }
  639. }
  640. else if (addParticipant(playerInstance))
  641. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"Registered.htm"));
  642. else
  643. return;
  644. playerInstance.sendPacket(npcHtmlMessage);
  645. }
  646. else if (command.equals("tvt_event_remove_participation"))
  647. {
  648. removeParticipant(playerInstance.getObjectId());
  649. if (Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0)
  650. AntiFeedManager.getInstance().removePlayer(AntiFeedManager.TVT_ID, playerInstance);
  651. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  652. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath+"Unregistered.htm"));
  653. playerInstance.sendPacket(npcHtmlMessage);
  654. }
  655. }
  656. /**
  657. * Called on every onAction in L2PcIstance<br><br>
  658. *
  659. * @param playerName as String<br>
  660. * @param targetPlayerName as String<br>
  661. * @return boolean: true if player is allowed to target, otherwise false<br>
  662. */
  663. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  664. {
  665. if (playerInstance == null || !isStarted())
  666. {
  667. return true;
  668. }
  669. if (playerInstance.isGM())
  670. {
  671. return true;
  672. }
  673. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  674. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  675. if ((playerTeamId != -1 && targetedPlayerTeamId == -1) || (playerTeamId == -1 && targetedPlayerTeamId != -1))
  676. {
  677. return false;
  678. }
  679. if (playerTeamId != -1 && targetedPlayerTeamId != -1 && playerTeamId == targetedPlayerTeamId && playerInstance.getObjectId() != targetedPlayerObjectId && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  680. {
  681. return false;
  682. }
  683. return true;
  684. }
  685. /**
  686. * Called on every scroll use<br><br>
  687. *
  688. * @param playerName as String<br>
  689. * @return boolean: true if player is allowed to use scroll, otherwise false<br>
  690. */
  691. public static boolean onScrollUse(int playerObjectId)
  692. {
  693. if (!isStarted())
  694. return true;
  695. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  696. return false;
  697. return true;
  698. }
  699. /**
  700. * Called on every potion use<br><br>
  701. *
  702. * @param playerName as String<br>
  703. * @return boolean: true if player is allowed to use potions, otherwise false<br>
  704. */
  705. public static boolean onPotionUse(int playerObjectId)
  706. {
  707. if (!isStarted())
  708. return true;
  709. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  710. return false;
  711. return true;
  712. }
  713. /**
  714. * Called on every escape use(thanks to nbd)<br><br>
  715. *
  716. * @param playerName as String<br>
  717. * @return boolean: true if player is not in tvt event, otherwise false<br>
  718. */
  719. public static boolean onEscapeUse(int playerObjectId)
  720. {
  721. if (!isStarted())
  722. {
  723. return true;
  724. }
  725. if (isPlayerParticipant(playerObjectId))
  726. {
  727. return false;
  728. }
  729. return true;
  730. }
  731. /**
  732. * Called on every summon item use<br><br>
  733. *
  734. * @param playerName as String<br>
  735. * @return boolean: true if player is allowed to summon by item, otherwise false<br>
  736. */
  737. public static boolean onItemSummon(int playerObjectId)
  738. {
  739. if (!isStarted())
  740. {
  741. return true;
  742. }
  743. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  744. {
  745. return false;
  746. }
  747. return true;
  748. }
  749. /**
  750. * Is called when a player is killed<br><br>
  751. *
  752. * @param killerCharacter as L2Character<br>
  753. * @param killedPlayerInstance as L2PcInstance<br>
  754. */
  755. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  756. {
  757. if (killedPlayerInstance == null || !isStarted())
  758. {
  759. return;
  760. }
  761. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  762. if (killedTeamId == -1)
  763. {
  764. return;
  765. }
  766. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
  767. if (killerCharacter == null)
  768. {
  769. return;
  770. }
  771. L2PcInstance killerPlayerInstance = null;
  772. if (killerCharacter instanceof L2PetInstance || killerCharacter instanceof L2SummonInstance)
  773. {
  774. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  775. if (killerPlayerInstance == null)
  776. {
  777. return;
  778. }
  779. }
  780. else if (killerCharacter instanceof L2PcInstance)
  781. {
  782. killerPlayerInstance = (L2PcInstance) killerCharacter;
  783. }
  784. else
  785. {
  786. return;
  787. }
  788. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  789. if (killerTeamId != -1 && killedTeamId != -1 && killerTeamId != killedTeamId)
  790. {
  791. TvTEventTeam killerTeam = _teams[killerTeamId];
  792. killerTeam.increasePoints();
  793. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  794. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  795. {
  796. if (playerInstance != null)
  797. {
  798. playerInstance.sendPacket(cs);
  799. }
  800. }
  801. }
  802. }
  803. /**
  804. * Called on Appearing packet received (player finished teleporting)<br><br>
  805. *
  806. * @param L2PcInstance playerInstance
  807. */
  808. public static void onTeleported(L2PcInstance playerInstance)
  809. {
  810. if (!isStarted() || playerInstance == null || !isPlayerParticipant(playerInstance.getObjectId()))
  811. return;
  812. if (playerInstance.isMageClass())
  813. {
  814. if (Config.TVT_EVENT_MAGE_BUFFS != null && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
  815. {
  816. for (int i : Config.TVT_EVENT_MAGE_BUFFS.keys())
  817. {
  818. L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_MAGE_BUFFS.get(i));
  819. if (skill != null)
  820. skill.getEffects(playerInstance, playerInstance);
  821. }
  822. }
  823. }
  824. else
  825. {
  826. if (Config.TVT_EVENT_FIGHTER_BUFFS != null && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
  827. {
  828. for (int i : Config.TVT_EVENT_FIGHTER_BUFFS.keys())
  829. {
  830. L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_FIGHTER_BUFFS.get(i));
  831. if (skill != null)
  832. skill.getEffects(playerInstance, playerInstance);
  833. }
  834. }
  835. }
  836. }
  837. /*
  838. * Return true if player valid for skill
  839. */
  840. public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, L2Skill skill)
  841. {
  842. if (!isStarted())
  843. return true;
  844. // TvT is started
  845. final int sourcePlayerId = source.getObjectId();
  846. final int targetPlayerId = target.getObjectId();
  847. final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
  848. final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
  849. // both players not participating
  850. if (!isSourceParticipant && !isTargetParticipant)
  851. return true;
  852. // one player not participating
  853. if (!(isSourceParticipant && isTargetParticipant))
  854. return false;
  855. // players in the different teams ?
  856. if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
  857. {
  858. if (!skill.isOffensive())
  859. return false;
  860. }
  861. return true;
  862. }
  863. /**
  864. * Sets the TvTEvent state<br><br>
  865. *
  866. * @param state as EventState<br>
  867. */
  868. private static void setState(EventState state)
  869. {
  870. synchronized (_state)
  871. {
  872. _state = state;
  873. }
  874. }
  875. /**
  876. * Is TvTEvent inactive?<br><br>
  877. *
  878. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  879. */
  880. public static boolean isInactive()
  881. {
  882. boolean isInactive;
  883. synchronized (_state)
  884. {
  885. isInactive = _state == EventState.INACTIVE;
  886. }
  887. return isInactive;
  888. }
  889. /**
  890. * Is TvTEvent in inactivating?<br><br>
  891. *
  892. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  893. */
  894. public static boolean isInactivating()
  895. {
  896. boolean isInactivating;
  897. synchronized (_state)
  898. {
  899. isInactivating = _state == EventState.INACTIVATING;
  900. }
  901. return isInactivating;
  902. }
  903. /**
  904. * Is TvTEvent in participation?<br><br>
  905. *
  906. * @return boolean: true if event is in participation progress, otherwise false<br>
  907. */
  908. public static boolean isParticipating()
  909. {
  910. boolean isParticipating;
  911. synchronized (_state)
  912. {
  913. isParticipating = _state == EventState.PARTICIPATING;
  914. }
  915. return isParticipating;
  916. }
  917. /**
  918. * Is TvTEvent starting?<br><br>
  919. *
  920. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  921. */
  922. public static boolean isStarting()
  923. {
  924. boolean isStarting;
  925. synchronized (_state)
  926. {
  927. isStarting = _state == EventState.STARTING;
  928. }
  929. return isStarting;
  930. }
  931. /**
  932. * Is TvTEvent started?<br><br>
  933. *
  934. * @return boolean: true if event is started, otherwise false<br>
  935. */
  936. public static boolean isStarted()
  937. {
  938. boolean isStarted;
  939. synchronized (_state)
  940. {
  941. isStarted = _state == EventState.STARTED;
  942. }
  943. return isStarted;
  944. }
  945. /**
  946. * Is TvTEvent rewadrding?<br><br>
  947. *
  948. * @return boolean: true if event is currently rewarding, otherwise false<br>
  949. */
  950. public static boolean isRewarding()
  951. {
  952. boolean isRewarding;
  953. synchronized (_state)
  954. {
  955. isRewarding = _state == EventState.REWARDING;
  956. }
  957. return isRewarding;
  958. }
  959. /**
  960. * Returns the team id of a player, if player is not participant it returns -1<br><br>
  961. *
  962. * @param playerName as String<br>
  963. * @return byte: team name of the given playerName, if not in event -1<br>
  964. */
  965. public static byte getParticipantTeamId(int playerObjectId)
  966. {
  967. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  968. }
  969. /**
  970. * Returns the team of a player, if player is not participant it returns null <br><br>
  971. *
  972. * @param player objectId as Integer<br>
  973. * @return TvTEventTeam: team of the given playerObjectId, if not in event null <br>
  974. */
  975. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  976. {
  977. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  978. }
  979. /**
  980. * Returns the enemy team of a player, if player is not participant it returns null <br><br>
  981. *
  982. * @param player objectId as Integer<br>
  983. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null <br>
  984. */
  985. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  986. {
  987. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  988. }
  989. /**
  990. * Returns the team coordinates in which the player is in, if player is not in a team return null<br><br>
  991. *
  992. * @param playerName as String<br>
  993. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  994. */
  995. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  996. {
  997. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  998. }
  999. /**
  1000. * Is given player participant of the event?<br><br>
  1001. *
  1002. * @param playerName as String<br>
  1003. * @return boolean: true if player is participant, ohterwise false<br>
  1004. */
  1005. public static boolean isPlayerParticipant(int playerObjectId)
  1006. {
  1007. if (!isParticipating() && !isStarting() && !isStarted())
  1008. {
  1009. return false;
  1010. }
  1011. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  1012. }
  1013. /**
  1014. * Returns participated player count<br><br>
  1015. *
  1016. * @return int: amount of players registered in the event<br>
  1017. */
  1018. public static int getParticipatedPlayersCount()
  1019. {
  1020. if (!isParticipating() && !isStarting() && !isStarted())
  1021. {
  1022. return 0;
  1023. }
  1024. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  1025. }
  1026. /**
  1027. * Returns teams names<br><br>
  1028. *
  1029. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1030. */
  1031. public static String[] getTeamNames()
  1032. {
  1033. return new String[]
  1034. {
  1035. _teams[0].getName(), _teams[1].getName()
  1036. };
  1037. }
  1038. /**
  1039. * Returns player count of both teams<br><br>
  1040. *
  1041. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1042. */
  1043. public static int[] getTeamsPlayerCounts()
  1044. {
  1045. return new int[]
  1046. {
  1047. _teams[0].getParticipatedPlayerCount(), _teams[1].getParticipatedPlayerCount()
  1048. };
  1049. }
  1050. /**
  1051. * Returns points count of both teams
  1052. *
  1053. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1054. */
  1055. public static int[] getTeamsPoints()
  1056. {
  1057. return new int[]
  1058. {
  1059. _teams[0].getPoints(), _teams[1].getPoints()
  1060. };
  1061. }
  1062. public static int getTvTEventInstance()
  1063. {
  1064. return _TvTEventInstance;
  1065. }
  1066. }