Guest User

qq

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