TvTEvent.java 34 KB

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