L2Event.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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.io.BufferedInputStream;
  17. import java.io.BufferedReader;
  18. import java.io.DataInputStream;
  19. import java.io.FileInputStream;
  20. import java.io.InputStreamReader;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import javolution.util.FastMap;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.gameserver.cache.HtmCache;
  30. import com.l2jserver.gameserver.datatables.NpcTable;
  31. import com.l2jserver.gameserver.datatables.SpawnTable;
  32. import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
  33. import com.l2jserver.gameserver.model.L2Spawn;
  34. import com.l2jserver.gameserver.model.L2World;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  37. import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  38. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  39. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  40. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  41. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  42. import com.l2jserver.gameserver.util.PlayerEventStatus;
  43. import com.l2jserver.util.ValueSortMap;
  44. /**
  45. * @since $Revision: 1.3.4.1 $ $Date: 2005/03/27 15:29:32 $
  46. * This ancient thingie got reworked by Nik at $Date: 2011/05/17 21:51:39 $
  47. * Yeah, for 6 years no one bothered reworking this buggy event engine.
  48. */
  49. public class L2Event
  50. {
  51. protected static final Logger _log = Logger.getLogger(L2Event.class.getName());
  52. public static EventState eventState = EventState.OFF;
  53. public static String _eventName = "";
  54. public static String _eventCreator = "";
  55. public static String _eventInfo = "";
  56. public static int _teamsNumber = 0;
  57. public static final Map<Integer, String> _teamNames = new FastMap<>();
  58. public static final List<L2PcInstance> _registeredPlayers = new FastList<>();
  59. public static final Map<Integer, FastList<L2PcInstance>> _teams = new FastMap<>();
  60. public static int _npcId = 0;
  61. //public static final List<L2Npc> _npcs = new FastList<L2Npc>();
  62. private static final Map<L2PcInstance, PlayerEventStatus> _connectionLossData = new FastMap<>();
  63. public enum EventState
  64. {
  65. OFF, // Not running
  66. STANDBY, // Waiting for participants to register
  67. ON // Registration is over and the event has started.
  68. }
  69. /**
  70. *
  71. * @param player
  72. * @return The team ID where the player is in, or -1 if player is null or team not found.
  73. */
  74. public static int getPlayerTeamId(L2PcInstance player)
  75. {
  76. if (player == null)
  77. return -1;
  78. for (Entry<Integer, FastList<L2PcInstance>> team : _teams.entrySet())
  79. {
  80. if (team.getValue().contains(player))
  81. return team.getKey();
  82. }
  83. return -1;
  84. }
  85. public static List<L2PcInstance> getTopNKillers(int n)
  86. {
  87. Map<L2PcInstance, Integer> tmp = new FastMap<>();
  88. for (FastList<L2PcInstance> teamList : _teams.values())
  89. {
  90. for (L2PcInstance player : teamList)
  91. {
  92. if (player.getEventStatus() == null)
  93. continue;
  94. tmp.put(player, player.getEventStatus().kills.size());
  95. }
  96. }
  97. ValueSortMap.sortMapByValue(tmp, false);
  98. // If the map size is less than "n", n will be as much as the map size
  99. if (tmp.size() <= n)
  100. {
  101. List<L2PcInstance> toReturn = new FastList<>();
  102. toReturn.addAll(tmp.keySet());
  103. return toReturn;
  104. }
  105. List<L2PcInstance> toReturn = new FastList<>();
  106. toReturn.addAll(tmp.keySet());
  107. return toReturn.subList(1, n);
  108. }
  109. public static void showEventHtml(L2PcInstance player, String objectid)
  110. {//TODO: work on this
  111. if (eventState == EventState.STANDBY)
  112. {
  113. try
  114. {
  115. final String htmContent;
  116. NpcHtmlMessage html = new NpcHtmlMessage(5);
  117. if (_registeredPlayers.contains(player))
  118. htmContent = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/mods/EventEngine/Participating.htm");
  119. else
  120. htmContent = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/mods/EventEngine/Participation.htm");
  121. if (htmContent != null)
  122. html.setHtml(htmContent);
  123. html.replace("%objectId%", objectid); // Yeah, we need this.
  124. html.replace("%eventName%", _eventName);
  125. html.replace("%eventCreator%", _eventCreator);
  126. html.replace("%eventInfo%", _eventInfo);
  127. player.sendPacket(html);
  128. }
  129. catch (Exception e)
  130. {
  131. _log.log(Level.WARNING, "Exception on showEventHtml(): " + e.getMessage(), e);
  132. }
  133. }
  134. }
  135. /**
  136. * Spawns an event participation NPC near the player.
  137. * The npc id used to spawning is L2Event._npcId
  138. * @param target
  139. */
  140. public static void spawnEventNpc(L2PcInstance target)
  141. {
  142. L2NpcTemplate template = NpcTable.getInstance().getTemplate(_npcId);
  143. try
  144. {
  145. L2Spawn spawn = new L2Spawn(template);
  146. spawn.setLocx(target.getX() + 50);
  147. spawn.setLocy(target.getY() + 50);
  148. spawn.setLocz(target.getZ());
  149. spawn.setAmount(1);
  150. spawn.setHeading(target.getHeading());
  151. spawn.stopRespawn();
  152. SpawnTable.getInstance().addNewSpawn(spawn, false);
  153. spawn.init();
  154. spawn.getLastSpawn().setCurrentHp(999999999);
  155. spawn.getLastSpawn().setTitle(_eventName);
  156. spawn.getLastSpawn().isEventMob = true;
  157. //spawn.getLastSpawn().decayMe();
  158. //spawn.getLastSpawn().spawnMe(spawn.getLastSpawn().getX(), spawn.getLastSpawn().getY(), spawn.getLastSpawn().getZ());
  159. spawn.getLastSpawn().broadcastPacket(new MagicSkillUse(spawn.getLastSpawn(), spawn.getLastSpawn(), 1034, 1, 1, 1));
  160. //_npcs.add(spawn.getLastSpawn());
  161. }
  162. catch (Exception e)
  163. {
  164. _log.log(Level.WARNING, "Exception on spawn(): " + e.getMessage(), e);
  165. }
  166. }
  167. public static void unspawnEventNpcs()
  168. {
  169. //Its a little rough, but for sure it will remove every damn event NPC.
  170. for (L2Spawn spawn : SpawnTable.getInstance().getSpawnTable())
  171. {
  172. if (spawn.getLastSpawn() != null && spawn.getLastSpawn().isEventMob)
  173. {
  174. spawn.getLastSpawn().deleteMe();
  175. spawn.stopRespawn();
  176. SpawnTable.getInstance().deleteSpawn(spawn, false);
  177. }
  178. }
  179. //for (L2Npc npc : _npcs)
  180. // npc.deleteMe();
  181. }
  182. /**
  183. * @param player
  184. * @return False: If player is null, his event status is null or the event state is off.
  185. * True: if the player is inside the _registeredPlayers list while the event state is STANDBY.
  186. * If the event state is ON, it will check if the player is inside in one of the teams.
  187. */
  188. public static boolean isParticipant(L2PcInstance player)
  189. {
  190. if (player == null || player.getEventStatus() == null)
  191. return false;
  192. switch (eventState)
  193. {
  194. case OFF:
  195. return false;
  196. case STANDBY:
  197. return _registeredPlayers.contains(player);
  198. case ON:
  199. for (FastList<L2PcInstance> teamList : _teams.values())
  200. {
  201. if (teamList.contains(player))
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. /**
  208. *
  209. * Adds the player to the list of participants.
  210. * If the event state is NOT STANDBY, the player wont be registered.
  211. * @param player
  212. */
  213. public static void registerPlayer(L2PcInstance player)
  214. {
  215. if (eventState != EventState.STANDBY)
  216. {
  217. player.sendMessage("The registration period for this event is over.");
  218. return;
  219. }
  220. if (Config.L2JMOD_DUALBOX_CHECK_MAX_L2EVENT_PARTICIPANTS_PER_IP == 0 || AntiFeedManager.getInstance().tryAddPlayer(AntiFeedManager.L2EVENT_ID, player, Config.L2JMOD_DUALBOX_CHECK_MAX_L2EVENT_PARTICIPANTS_PER_IP))
  221. _registeredPlayers.add(player);
  222. else
  223. {
  224. player.sendMessage("You have reached the maximum allowed participants per IP.");
  225. return;
  226. }
  227. }
  228. /**
  229. *
  230. * Removes the player from the participating players and the teams and restores
  231. * his init stats before he registered at the event (loc, pvp, pk, title etc)
  232. * @param player
  233. */
  234. public static void removeAndResetPlayer(L2PcInstance player)
  235. {
  236. try
  237. {
  238. if (isParticipant(player))
  239. {
  240. if (player.isDead())
  241. {
  242. player.restoreExp(100.0);
  243. player.doRevive();
  244. player.setCurrentHpMp(player.getMaxHp(), player.getMaxMp());
  245. player.setCurrentCp(player.getMaxCp());
  246. }
  247. player.getPoly().setPolyInfo(null, "1");
  248. player.decayMe();
  249. player.spawnMe(player.getX(), player.getY(), player.getZ());
  250. CharInfo info1 = new CharInfo(player);
  251. player.broadcastPacket(info1);
  252. UserInfo info2 = new UserInfo(player);
  253. player.sendPacket(info2);
  254. player.broadcastPacket(new ExBrExtraUserInfo(player));
  255. player.stopTransformation(true);
  256. }
  257. if (player.getEventStatus() != null)
  258. player.getEventStatus().restoreInits();
  259. player.setEventStatus(null);
  260. _registeredPlayers.remove(player);
  261. int teamId = getPlayerTeamId(player);
  262. if (_teams.containsKey(teamId))
  263. _teams.get(teamId).remove(player);
  264. }
  265. catch (Exception e)
  266. {
  267. _log.log(Level.WARNING, "Error at unregisterAndResetPlayer in the event:" + e.getMessage(), e);
  268. }
  269. }
  270. /**
  271. * The player's event status will be saved at _connectionLossData
  272. * @param player
  273. */
  274. public static void savePlayerEventStatus(L2PcInstance player)
  275. {
  276. _connectionLossData.put(player, player.getEventStatus());
  277. }
  278. /**
  279. * If _connectionLossData contains the player, it will restore the player's event status.
  280. * Also it will remove the player from the _connectionLossData.
  281. * @param player
  282. */
  283. public static void restorePlayerEventStatus(L2PcInstance player)
  284. {
  285. if (_connectionLossData.containsKey(player))
  286. {
  287. player.setEventStatus(_connectionLossData.get(player));
  288. _connectionLossData.remove(player);
  289. }
  290. }
  291. /**
  292. * If the event is ON or STANDBY, it will not start.
  293. * Sets the event state to STANDBY and spawns registration NPCs
  294. * @return a string with information if the event participation has been successfully started or not.
  295. */
  296. public static String startEventParticipation()
  297. {
  298. try
  299. {
  300. switch (eventState)
  301. {
  302. case ON:
  303. return "Cannot start event, it is already on.";
  304. case STANDBY:
  305. return "Cannot start event, it is on standby mode.";
  306. case OFF: // Event is off, so no problem turning it on.
  307. eventState = EventState.STANDBY;
  308. break;
  309. }
  310. // Register the event at AntiFeedManager and clean it for just in case if the event is already registered.
  311. AntiFeedManager.getInstance().registerEvent(AntiFeedManager.L2EVENT_ID);
  312. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  313. // Just in case
  314. unspawnEventNpcs();
  315. _registeredPlayers.clear();
  316. //_npcs.clear();
  317. if (NpcTable.getInstance().getTemplate(_npcId) == null)
  318. return "Cannot start event, invalid npc id.";
  319. DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(Config.DATAPACK_ROOT+"data/events/" + _eventName)));
  320. BufferedReader inbr = new BufferedReader(new InputStreamReader(in));
  321. _eventCreator = inbr.readLine();
  322. _eventInfo = inbr.readLine();
  323. List<L2PcInstance> temp = new FastList<>();
  324. for (L2PcInstance player : L2World.getInstance().getAllPlayersArray())
  325. {
  326. if (!player.isOnline()) // Offline shops?
  327. continue;
  328. if (!temp.contains(player))
  329. {
  330. spawnEventNpc(player);
  331. temp.add(player);
  332. }
  333. for (L2PcInstance playertemp : player.getKnownList().getKnownPlayers().values())
  334. {
  335. if ((Math.abs(playertemp.getX() - player.getX()) < 1000) && (Math.abs(playertemp.getY() - player.getY()) < 1000) && (Math.abs(playertemp.getZ() - player.getZ()) < 1000))
  336. temp.add(playertemp);
  337. }
  338. }
  339. }
  340. catch (Exception e)
  341. {
  342. _log.warning("L2Event: " + e.getMessage());
  343. return "Cannot start event participation, an error has occured.";
  344. }
  345. return "The event participation has been successfully started.";
  346. }
  347. /**
  348. * If the event is ON or OFF, it will not start.
  349. * Sets the event state to ON, creates the teams,
  350. * adds the registered players ordered by level at the teams
  351. * and adds a new event status to the players.
  352. * @return a string with information if the event has been successfully started or not.
  353. */
  354. public static String startEvent()
  355. {
  356. try
  357. {
  358. switch (eventState)
  359. {
  360. case ON:
  361. return "Cannot start event, it is already on.";
  362. case STANDBY:
  363. eventState = EventState.ON;
  364. break;
  365. case OFF: // Event is off, so no problem turning it on.
  366. return "Cannot start event, it is off. Participation start is required.";
  367. }
  368. // Clean the things we will use, just in case.
  369. unspawnEventNpcs();
  370. _teams.clear();
  371. _connectionLossData.clear();
  372. // Insert empty lists at _teams.
  373. for (int i = 0; i < _teamsNumber; i++)
  374. _teams.put(i + 1, new FastList<L2PcInstance>());
  375. int i = 0;
  376. while (!_registeredPlayers.isEmpty())
  377. {
  378. //Get the player with the biggest level
  379. int max = 0;
  380. L2PcInstance biggestLvlPlayer = null;
  381. for (L2PcInstance player : _registeredPlayers)
  382. {
  383. if (player == null)
  384. continue;
  385. if (max < player.getLevel())
  386. {
  387. max = player.getLevel();
  388. biggestLvlPlayer = player;
  389. }
  390. }
  391. if (biggestLvlPlayer == null)
  392. continue;
  393. _registeredPlayers.remove(biggestLvlPlayer);
  394. _teams.get(i + 1).add(biggestLvlPlayer);
  395. biggestLvlPlayer.setEventStatus();
  396. i = (i + 1) % _teamsNumber;
  397. }
  398. }
  399. catch (Exception e)
  400. {
  401. _log.warning("L2Event: " + e.getMessage());
  402. return "Cannot start event, an error has occured.";
  403. }
  404. return "The event has been successfully started.";
  405. }
  406. /**
  407. * If the event state is OFF, it will not finish.
  408. * Sets the event state to OFF, unregisters and resets the players,
  409. * unspawns and clers the event NPCs, clears the teams, registered players,
  410. * connection loss data, sets the teams number to 0, sets the event name to empty.
  411. * @return a string with information if the event has been successfully stopped or not.
  412. */
  413. public static String finishEvent()
  414. {
  415. switch (eventState)
  416. {
  417. case OFF:
  418. return "Cannot finish event, it is already off.";
  419. case STANDBY:
  420. for (L2PcInstance player : _registeredPlayers)
  421. removeAndResetPlayer(player);
  422. unspawnEventNpcs();
  423. //_npcs.clear();
  424. _registeredPlayers.clear();
  425. _teams.clear();
  426. _connectionLossData.clear();
  427. _teamsNumber = 0;
  428. _eventName = "";
  429. eventState = EventState.OFF;
  430. return "The event has been stopped at STANDBY mode, all players unregistered and all event npcs unspawned.";
  431. case ON:
  432. for (FastList<L2PcInstance> teamList : _teams.values())
  433. {
  434. for (L2PcInstance player : teamList)
  435. removeAndResetPlayer(player);
  436. }
  437. eventState = EventState.OFF;
  438. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  439. unspawnEventNpcs(); // Just in case
  440. //_npcs.clear();
  441. _registeredPlayers.clear();
  442. _teams.clear();
  443. _connectionLossData.clear();
  444. _teamsNumber = 0;
  445. _eventName = "";
  446. _npcId = 0;
  447. _eventCreator = "";
  448. _eventInfo = "";
  449. return "The event has been stopped, all players unregistered and all event npcs unspawned.";
  450. }
  451. return "The event has been successfully finished.";
  452. }
  453. }