TvTEvent.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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.SkillTable;
  26. import net.sf.l2j.gameserver.datatables.SpawnTable;
  27. import net.sf.l2j.gameserver.instancemanager.InstanceManager;
  28. import net.sf.l2j.gameserver.model.L2Skill;
  29. import net.sf.l2j.gameserver.model.L2Spawn;
  30. import net.sf.l2j.gameserver.model.actor.L2Character;
  31. import net.sf.l2j.gameserver.model.actor.L2Npc;
  32. import net.sf.l2j.gameserver.model.actor.L2Summon;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
  37. import net.sf.l2j.gameserver.model.itemcontainer.PcInventory;
  38. import net.sf.l2j.gameserver.model.olympiad.Olympiad;
  39. import net.sf.l2j.gameserver.network.SystemMessageId;
  40. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  41. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  42. import net.sf.l2j.gameserver.network.serverpackets.MagicSkillUse;
  43. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  44. import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
  45. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  46. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  47. import net.sf.l2j.gameserver.util.StringUtil;
  48. import net.sf.l2j.util.Rnd;
  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(0);
  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(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(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. // Stop respawningof the npc
  548. _npcSpawn.stopRespawn();
  549. _npcSpawn = null;
  550. _lastNpcSpawn = null;
  551. }
  552. /**
  553. * Called when a player logs in<br><br>
  554. *
  555. * @param playerInstance as L2PcInstance<br>
  556. */
  557. public static void onLogin(L2PcInstance playerInstance)
  558. {
  559. if (playerInstance == null || (!isStarting() && !isStarted()))
  560. {
  561. return;
  562. }
  563. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  564. if (teamId == -1)
  565. {
  566. return;
  567. }
  568. _teams[teamId].addPlayer(playerInstance);
  569. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
  570. }
  571. /**
  572. * Called when a player logs out<br><br>
  573. *
  574. * @param playerInstance as L2PcInstance<br>
  575. */
  576. public static void onLogout(L2PcInstance playerInstance)
  577. {
  578. if (playerInstance != null && (isStarting() || isStarted() || isParticipating()))
  579. {
  580. if (removeParticipant(playerInstance.getObjectId()))
  581. playerInstance.setXYZ(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)-50,
  582. Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)-50,
  583. Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  584. }
  585. }
  586. /**
  587. * Called on every bypass by npc of type L2TvTEventNpc<br>
  588. * Needs synchronization cause of the max player check<br><br>
  589. *
  590. * @param command as String<br>
  591. * @param playerInstance as L2PcInstance<br>
  592. */
  593. public static synchronized void onBypass(String command, L2PcInstance playerInstance)
  594. {
  595. if (playerInstance == null || !isParticipating())
  596. return;
  597. final String htmContent;
  598. if (command.equals("tvt_event_participation"))
  599. {
  600. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  601. int playerLevel = playerInstance.getLevel();
  602. if (playerInstance.isCursedWeaponEquipped())
  603. {
  604. htmContent = HtmCache.getInstance().getHtm(htmlPath+"CursedWeaponEquipped.htm");
  605. if (htmContent != null)
  606. npcHtmlMessage.setHtml(htmContent);
  607. }
  608. else if (Olympiad.getInstance().isRegistered(playerInstance))
  609. {
  610. htmContent = HtmCache.getInstance().getHtm(htmlPath+"Olympiad.htm");
  611. if (htmContent != null)
  612. npcHtmlMessage.setHtml(htmContent);
  613. }
  614. else if (playerInstance.getKarma() > 0)
  615. {
  616. htmContent = HtmCache.getInstance().getHtm(htmlPath+"Karma.htm");
  617. if (htmContent != null)
  618. npcHtmlMessage.setHtml(htmContent);
  619. }
  620. else if (playerLevel < Config.TVT_EVENT_MIN_LVL || playerLevel > Config.TVT_EVENT_MAX_LVL)
  621. {
  622. htmContent = HtmCache.getInstance().getHtm(htmlPath+"Level.htm");
  623. if (htmContent != null)
  624. {
  625. npcHtmlMessage.setHtml(htmContent);
  626. npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
  627. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
  628. }
  629. }
  630. else if (_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS && _teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS)
  631. {
  632. htmContent = HtmCache.getInstance().getHtm(htmlPath+"TeamsFull.htm");
  633. if (htmContent != null)
  634. {
  635. npcHtmlMessage.setHtml(htmContent);
  636. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
  637. }
  638. }
  639. else if (!payParticipationFee(playerInstance))
  640. {
  641. htmContent = HtmCache.getInstance().getHtm(htmlPath+"ParticipationFee.htm");
  642. if (htmContent != null)
  643. {
  644. npcHtmlMessage.setHtml(htmContent);
  645. npcHtmlMessage.replace("%fee%", getParticipationFee());
  646. }
  647. }
  648. else if (addParticipant(playerInstance))
  649. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Registered.htm"));
  650. else
  651. return;
  652. playerInstance.sendPacket(npcHtmlMessage);
  653. }
  654. else if (command.equals("tvt_event_remove_participation"))
  655. {
  656. removeParticipant(playerInstance.getObjectId());
  657. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  658. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Unregistered.htm"));
  659. playerInstance.sendPacket(npcHtmlMessage);
  660. }
  661. }
  662. /**
  663. * Called on every onAction in L2PcIstance<br><br>
  664. *
  665. * @param playerName as String<br>
  666. * @param targetPlayerName as String<br>
  667. * @return boolean: true if player is allowed to target, otherwise false<br>
  668. */
  669. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  670. {
  671. if (playerInstance == null || !isStarted())
  672. {
  673. return true;
  674. }
  675. if (playerInstance.isGM())
  676. {
  677. return true;
  678. }
  679. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  680. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  681. if ((playerTeamId != -1 && targetedPlayerTeamId == -1) || (playerTeamId == -1 && targetedPlayerTeamId != -1))
  682. {
  683. return false;
  684. }
  685. if (playerTeamId != -1 && targetedPlayerTeamId != -1 && playerTeamId == targetedPlayerTeamId && playerInstance.getObjectId() != targetedPlayerObjectId && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  686. {
  687. return false;
  688. }
  689. return true;
  690. }
  691. /**
  692. * Called on every scroll use<br><br>
  693. *
  694. * @param playerName as String<br>
  695. * @return boolean: true if player is allowed to use scroll, otherwise false<br>
  696. */
  697. public static boolean onScrollUse(int playerObjectId)
  698. {
  699. if (!isStarted())
  700. return true;
  701. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  702. return false;
  703. return true;
  704. }
  705. /**
  706. * Called on every potion use<br><br>
  707. *
  708. * @param playerName as String<br>
  709. * @return boolean: true if player is allowed to use potions, otherwise false<br>
  710. */
  711. public static boolean onPotionUse(int playerObjectId)
  712. {
  713. if (!isStarted())
  714. return true;
  715. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  716. return false;
  717. return true;
  718. }
  719. /**
  720. * Called on every escape use(thanks to nbd)<br><br>
  721. *
  722. * @param playerName as String<br>
  723. * @return boolean: true if player is not in tvt event, otherwise false<br>
  724. */
  725. public static boolean onEscapeUse(int playerObjectId)
  726. {
  727. if (!isStarted())
  728. {
  729. return true;
  730. }
  731. if (isPlayerParticipant(playerObjectId))
  732. {
  733. return false;
  734. }
  735. return true;
  736. }
  737. /**
  738. * Called on every summon item use<br><br>
  739. *
  740. * @param playerName as String<br>
  741. * @return boolean: true if player is allowed to summon by item, otherwise false<br>
  742. */
  743. public static boolean onItemSummon(int playerObjectId)
  744. {
  745. if (!isStarted())
  746. {
  747. return true;
  748. }
  749. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  750. {
  751. return false;
  752. }
  753. return true;
  754. }
  755. /**
  756. * Is called when a player is killed<br><br>
  757. *
  758. * @param killerCharacter as L2Character<br>
  759. * @param killedPlayerInstance as L2PcInstance<br>
  760. */
  761. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  762. {
  763. if (killedPlayerInstance == null || !isStarted())
  764. {
  765. return;
  766. }
  767. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  768. if (killedTeamId == -1)
  769. {
  770. return;
  771. }
  772. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
  773. if (killerCharacter == null)
  774. {
  775. return;
  776. }
  777. L2PcInstance killerPlayerInstance = null;
  778. if (killerCharacter instanceof L2PetInstance || killerCharacter instanceof L2SummonInstance)
  779. {
  780. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  781. if (killerPlayerInstance == null)
  782. {
  783. return;
  784. }
  785. }
  786. else if (killerCharacter instanceof L2PcInstance)
  787. {
  788. killerPlayerInstance = (L2PcInstance) killerCharacter;
  789. }
  790. else
  791. {
  792. return;
  793. }
  794. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  795. if (killerTeamId != -1 && killedTeamId != -1 && killerTeamId != killedTeamId)
  796. {
  797. TvTEventTeam killerTeam = _teams[killerTeamId];
  798. killerTeam.increasePoints();
  799. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  800. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  801. {
  802. if (playerInstance != null)
  803. {
  804. playerInstance.sendPacket(cs);
  805. }
  806. }
  807. }
  808. }
  809. /**
  810. * Called on Appearing packet received (player finished teleporting)<br><br>
  811. *
  812. * @param L2PcInstance playerInstance
  813. */
  814. public static void onTeleported(L2PcInstance playerInstance)
  815. {
  816. if (!isStarted() || playerInstance == null || !isPlayerParticipant(playerInstance.getObjectId()))
  817. return;
  818. if (playerInstance.isMageClass())
  819. {
  820. if (Config.TVT_EVENT_MAGE_BUFFS != null && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
  821. {
  822. for (int i : Config.TVT_EVENT_MAGE_BUFFS.keySet())
  823. {
  824. L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_MAGE_BUFFS.get(i));
  825. if (skill != null)
  826. skill.getEffects(playerInstance, playerInstance);
  827. }
  828. }
  829. }
  830. else
  831. {
  832. if (Config.TVT_EVENT_FIGHTER_BUFFS != null && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
  833. {
  834. for (int i : Config.TVT_EVENT_FIGHTER_BUFFS.keySet())
  835. {
  836. L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_FIGHTER_BUFFS.get(i));
  837. if (skill != null)
  838. skill.getEffects(playerInstance, playerInstance);
  839. }
  840. }
  841. }
  842. }
  843. /*
  844. * Return true if player valid for skill
  845. */
  846. public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, L2Skill skill)
  847. {
  848. if (!isStarted())
  849. return true;
  850. // TvT is started
  851. final int sourcePlayerId = source.getObjectId();
  852. final int targetPlayerId = target.getObjectId();
  853. final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
  854. final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
  855. // both players not participating
  856. if (!isSourceParticipant && !isTargetParticipant)
  857. return true;
  858. // one player not participating
  859. if (!(isSourceParticipant && isTargetParticipant))
  860. return false;
  861. // players in the different teams ?
  862. if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
  863. {
  864. if (!skill.isOffensive())
  865. return false;
  866. }
  867. return true;
  868. }
  869. /**
  870. * Sets the TvTEvent state<br><br>
  871. *
  872. * @param state as EventState<br>
  873. */
  874. private static void setState(EventState state)
  875. {
  876. synchronized (_state)
  877. {
  878. _state = state;
  879. }
  880. }
  881. /**
  882. * Is TvTEvent inactive?<br><br>
  883. *
  884. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  885. */
  886. public static boolean isInactive()
  887. {
  888. boolean isInactive;
  889. synchronized (_state)
  890. {
  891. isInactive = _state == EventState.INACTIVE;
  892. }
  893. return isInactive;
  894. }
  895. /**
  896. * Is TvTEvent in inactivating?<br><br>
  897. *
  898. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  899. */
  900. public static boolean isInactivating()
  901. {
  902. boolean isInactivating;
  903. synchronized (_state)
  904. {
  905. isInactivating = _state == EventState.INACTIVATING;
  906. }
  907. return isInactivating;
  908. }
  909. /**
  910. * Is TvTEvent in participation?<br><br>
  911. *
  912. * @return boolean: true if event is in participation progress, otherwise false<br>
  913. */
  914. public static boolean isParticipating()
  915. {
  916. boolean isParticipating;
  917. synchronized (_state)
  918. {
  919. isParticipating = _state == EventState.PARTICIPATING;
  920. }
  921. return isParticipating;
  922. }
  923. /**
  924. * Is TvTEvent starting?<br><br>
  925. *
  926. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  927. */
  928. public static boolean isStarting()
  929. {
  930. boolean isStarting;
  931. synchronized (_state)
  932. {
  933. isStarting = _state == EventState.STARTING;
  934. }
  935. return isStarting;
  936. }
  937. /**
  938. * Is TvTEvent started?<br><br>
  939. *
  940. * @return boolean: true if event is started, otherwise false<br>
  941. */
  942. public static boolean isStarted()
  943. {
  944. boolean isStarted;
  945. synchronized (_state)
  946. {
  947. isStarted = _state == EventState.STARTED;
  948. }
  949. return isStarted;
  950. }
  951. /**
  952. * Is TvTEvent rewadrding?<br><br>
  953. *
  954. * @return boolean: true if event is currently rewarding, otherwise false<br>
  955. */
  956. public static boolean isRewarding()
  957. {
  958. boolean isRewarding;
  959. synchronized (_state)
  960. {
  961. isRewarding = _state == EventState.REWARDING;
  962. }
  963. return isRewarding;
  964. }
  965. /**
  966. * Returns the team id of a player, if player is not participant it returns -1<br><br>
  967. *
  968. * @param playerName as String<br>
  969. * @return byte: team name of the given playerName, if not in event -1<br>
  970. */
  971. public static byte getParticipantTeamId(int playerObjectId)
  972. {
  973. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  974. }
  975. /**
  976. * Returns the team of a player, if player is not participant it returns null <br><br>
  977. *
  978. * @param player objectId as Integer<br>
  979. * @return TvTEventTeam: team of the given playerObjectId, if not in event null <br>
  980. */
  981. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  982. {
  983. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  984. }
  985. /**
  986. * Returns the enemy team of a player, if player is not participant it returns null <br><br>
  987. *
  988. * @param player objectId as Integer<br>
  989. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null <br>
  990. */
  991. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  992. {
  993. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  994. }
  995. /**
  996. * Returns the team coordinates in which the player is in, if player is not in a team return null<br><br>
  997. *
  998. * @param playerName as String<br>
  999. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1000. */
  1001. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  1002. {
  1003. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  1004. }
  1005. /**
  1006. * Is given player participant of the event?<br><br>
  1007. *
  1008. * @param playerName as String<br>
  1009. * @return boolean: true if player is participant, ohterwise false<br>
  1010. */
  1011. public static boolean isPlayerParticipant(int playerObjectId)
  1012. {
  1013. if (!isParticipating() && !isStarting() && !isStarted())
  1014. {
  1015. return false;
  1016. }
  1017. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  1018. }
  1019. /**
  1020. * Returns participated player count<br><br>
  1021. *
  1022. * @return int: amount of players registered in the event<br>
  1023. */
  1024. public static int getParticipatedPlayersCount()
  1025. {
  1026. if (!isParticipating() && !isStarting() && !isStarted())
  1027. {
  1028. return 0;
  1029. }
  1030. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  1031. }
  1032. /**
  1033. * Returns teams names<br><br>
  1034. *
  1035. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1036. */
  1037. public static String[] getTeamNames()
  1038. {
  1039. return new String[]
  1040. {
  1041. _teams[0].getName(), _teams[1].getName()
  1042. };
  1043. }
  1044. /**
  1045. * Returns player count of both teams<br><br>
  1046. *
  1047. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1048. */
  1049. public static int[] getTeamsPlayerCounts()
  1050. {
  1051. return new int[]
  1052. {
  1053. _teams[0].getParticipatedPlayerCount(), _teams[1].getParticipatedPlayerCount()
  1054. };
  1055. }
  1056. /**
  1057. * Returns points count of both teams
  1058. *
  1059. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1060. */
  1061. public static int[] getTeamsPoints()
  1062. {
  1063. return new int[]
  1064. {
  1065. _teams[0].getPoints(), _teams[1].getPoints()
  1066. };
  1067. }
  1068. public static int getTvTEventInstance()
  1069. {
  1070. return _TvTEventInstance;
  1071. }
  1072. }