TvTEvent.java 31 KB

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