TvTEvent.java 36 KB

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