TvTEvent.java 32 KB

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