L2Event.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.network.serverpackets.CharInfo;
  37. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  38. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  39. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  40. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  41. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  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<Integer, String>();
  58. public static final List<L2PcInstance> _registeredPlayers = new FastList<L2PcInstance>();
  59. public static final Map<Integer, FastList<L2PcInstance>> _teams = new FastMap<Integer, FastList<L2PcInstance>>();
  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<L2PcInstance, PlayerEventStatus>();
  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<L2PcInstance, Integer>();
  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<L2PcInstance>();
  102. toReturn.addAll(tmp.keySet());
  103. return toReturn;
  104. }
  105. List<L2PcInstance> toReturn = new FastList<L2PcInstance>();
  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. */
  139. public static void spawnEventNpc(L2PcInstance target)
  140. {
  141. L2NpcTemplate template = NpcTable.getInstance().getTemplate(_npcId);
  142. try
  143. {
  144. L2Spawn spawn = new L2Spawn(template);
  145. spawn.setLocx(target.getX() + 50);
  146. spawn.setLocy(target.getY() + 50);
  147. spawn.setLocz(target.getZ());
  148. spawn.setAmount(1);
  149. spawn.setHeading(target.getHeading());
  150. SpawnTable.getInstance().addNewSpawn(spawn, false);
  151. spawn.init();
  152. spawn.getLastSpawn().setCurrentHp(999999999);
  153. spawn.getLastSpawn().setTitle(_eventName);
  154. spawn.getLastSpawn().isEventMob = true;
  155. //spawn.getLastSpawn().decayMe();
  156. //spawn.getLastSpawn().spawnMe(spawn.getLastSpawn().getX(), spawn.getLastSpawn().getY(), spawn.getLastSpawn().getZ());
  157. spawn.getLastSpawn().broadcastPacket(new MagicSkillUse(spawn.getLastSpawn(), spawn.getLastSpawn(), 1034, 1, 1, 1));
  158. //_npcs.add(spawn.getLastSpawn());
  159. }
  160. catch (Exception e)
  161. {
  162. _log.log(Level.WARNING, "Exception on spawn(): " + e.getMessage(), e);
  163. }
  164. }
  165. public static void unspawnEventNpcs()
  166. {
  167. //Its a little rough, but for sure it will remove every damn event NPC.
  168. for (L2Spawn spawn : SpawnTable.getInstance().getSpawnTable())
  169. {
  170. if (spawn.getLastSpawn() != null && spawn.getLastSpawn().isEventMob)
  171. {
  172. spawn.getLastSpawn().deleteMe();
  173. spawn.stopRespawn();
  174. SpawnTable.getInstance().deleteSpawn(spawn, false);
  175. }
  176. }
  177. //for (L2Npc npc : _npcs)
  178. // npc.deleteMe();
  179. }
  180. /**
  181. *
  182. * @return False: If player is null, his event status is null or the event state is off.
  183. * True: if the player is inside the _registeredPlayers list while the event state is STANDBY.
  184. * If the event state is ON, it will check if the player is inside in one of the teams.
  185. */
  186. public static boolean isParticipant(L2PcInstance player)
  187. {
  188. if (player == null || player.getEventStatus() == null)
  189. return false;
  190. switch (eventState)
  191. {
  192. case OFF:
  193. return false;
  194. case STANDBY:
  195. return _registeredPlayers.contains(player);
  196. case ON:
  197. for (FastList<L2PcInstance> teamList : _teams.values())
  198. {
  199. if (teamList.contains(player))
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. /**
  206. *
  207. * Adds the player to the list of participants.
  208. * If the event state is NOT STANDBY, the player wont be registered.
  209. */
  210. public static void registerPlayer(L2PcInstance player)
  211. {
  212. if (eventState != EventState.STANDBY)
  213. {
  214. player.sendMessage("The registration period for this event is over.");
  215. return;
  216. }
  217. if (AntiFeedManager.getInstance().tryAddPlayer(AntiFeedManager.L2EVENT_ID, player, Config.L2JMOD_DUALBOX_CHECK_MAX_L2EVENT_PARTICIPANTS_PER_IP))
  218. _registeredPlayers.add(player);
  219. else
  220. {
  221. player.sendMessage("You have reached the maximum allowed participants per IP.");
  222. return;
  223. }
  224. }
  225. /**
  226. *
  227. * Removes the player from the participating players and the teams and restores
  228. * his init stats before he registered at the event (loc, pvp, pk, title etc)
  229. */
  230. public static void removeAndResetPlayer(L2PcInstance player)
  231. {
  232. try
  233. {
  234. if (isParticipant(player))
  235. {
  236. if (player.isDead())
  237. {
  238. player.restoreExp(100.0);
  239. player.doRevive();
  240. player.setCurrentHpMp(player.getMaxHp(), player.getMaxMp());
  241. player.setCurrentCp(player.getMaxCp());
  242. }
  243. player.getPoly().setPolyInfo(null, "1");
  244. player.decayMe();
  245. player.spawnMe(player.getX(), player.getY(), player.getZ());
  246. CharInfo info1 = new CharInfo(player);
  247. player.broadcastPacket(info1);
  248. UserInfo info2 = new UserInfo(player);
  249. player.sendPacket(info2);
  250. player.broadcastPacket(new ExBrExtraUserInfo(player));
  251. player.stopTransformation(true);
  252. }
  253. if (player.getEventStatus() != null)
  254. player.getEventStatus().restoreInits();
  255. player.setEventStatus(null);
  256. _registeredPlayers.remove(player);
  257. int teamId = getPlayerTeamId(player);
  258. if (_teams.containsKey(teamId))
  259. _teams.get(teamId).remove(player);
  260. }
  261. catch (Exception e)
  262. {
  263. _log.log(Level.WARNING, "Error at unregisterAndResetPlayer in the event:" + e.getMessage(), e);
  264. }
  265. }
  266. /**
  267. *
  268. * The player's event status will be saved at _connectionLossData
  269. */
  270. public static void savePlayerEventStatus(L2PcInstance player)
  271. {
  272. _connectionLossData.put(player, player.getEventStatus());
  273. }
  274. /**
  275. *
  276. * If _connectionLossData contains the player, it will restore the player's event status.
  277. * Also it will remove the player from the _connectionLossData.
  278. */
  279. public static void restorePlayerEventStatus(L2PcInstance player)
  280. {
  281. if (_connectionLossData.containsKey(player))
  282. {
  283. player.setEventStatus(_connectionLossData.get(player));
  284. _connectionLossData.remove(player);
  285. }
  286. }
  287. /**
  288. * If the event is ON or STANDBY, it will not start.
  289. * Sets the event state to STANDBY and spawns registration NPCs
  290. * @return a string with information if the event participation has been successfully started or not.
  291. */
  292. public static String startEventParticipation()
  293. {
  294. try
  295. {
  296. switch (eventState)
  297. {
  298. case ON:
  299. return "Cannot start event, it is already on.";
  300. case STANDBY:
  301. return "Cannot start event, it is on standby mode.";
  302. case OFF: // Event is off, so no problem turning it on.
  303. eventState = EventState.STANDBY;
  304. break;
  305. }
  306. // Register the event at AntiFeedManager and clean it for just in case if the event is already registered.
  307. AntiFeedManager.getInstance().registerEvent(AntiFeedManager.L2EVENT_ID);
  308. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  309. // Just in case
  310. unspawnEventNpcs();
  311. _registeredPlayers.clear();
  312. //_npcs.clear();
  313. if (NpcTable.getInstance().getTemplate(_npcId) == null)
  314. return "Cannot start event, invalid npc id.";
  315. DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("data/events/" + _eventName)));
  316. BufferedReader inbr = new BufferedReader(new InputStreamReader(in));
  317. _eventCreator = inbr.readLine();
  318. _eventInfo = inbr.readLine();
  319. List<L2PcInstance> temp = new FastList<L2PcInstance>();
  320. for (L2PcInstance player : L2World.getInstance().getAllPlayersArray())
  321. {
  322. if (!player.isOnline()) // Offline shops?
  323. continue;
  324. if (!temp.contains(player))
  325. {
  326. spawnEventNpc(player);
  327. temp.add(player);
  328. }
  329. for (L2PcInstance playertemp : player.getKnownList().getKnownPlayers().values())
  330. {
  331. if ((Math.abs(playertemp.getX() - player.getX()) < 1000) && (Math.abs(playertemp.getY() - player.getY()) < 1000) && (Math.abs(playertemp.getZ() - player.getZ()) < 1000))
  332. temp.add(playertemp);
  333. }
  334. }
  335. }
  336. catch (Exception e)
  337. {
  338. e.printStackTrace();
  339. return "Cannot start event participation, an error has occured.";
  340. }
  341. return "The event participation has been successfully started.";
  342. }
  343. /**
  344. * If the event is ON or OFF, it will not start.
  345. * Sets the event state to ON, creates the teams,
  346. * adds the registered players ordered by level at the teams
  347. * and adds a new event status to the players.
  348. * @return a string with information if the event has been successfully started or not.
  349. */
  350. public static String startEvent()
  351. {
  352. try
  353. {
  354. switch (eventState)
  355. {
  356. case ON:
  357. return "Cannot start event, it is already on.";
  358. case STANDBY:
  359. eventState = EventState.ON;
  360. break;
  361. case OFF: // Event is off, so no problem turning it on.
  362. return "Cannot start event, it is off. Participation start is required.";
  363. }
  364. // Clean the things we will use, just in case.
  365. unspawnEventNpcs();
  366. _teams.clear();
  367. _connectionLossData.clear();
  368. // Insert empty lists at _teams.
  369. for (int i = 0; i < _teamsNumber; i++)
  370. _teams.put(i + 1, new FastList<L2PcInstance>());
  371. int i = 0;
  372. while (!_registeredPlayers.isEmpty())
  373. {
  374. //Get the player with the biggest level
  375. int max = 0;
  376. L2PcInstance biggestLvlPlayer = null;
  377. for (L2PcInstance player : _registeredPlayers)
  378. {
  379. if (player == null)
  380. continue;
  381. if (max < player.getLevel())
  382. {
  383. max = player.getLevel();
  384. biggestLvlPlayer = player;
  385. }
  386. }
  387. if (biggestLvlPlayer == null)
  388. continue;
  389. _registeredPlayers.remove(biggestLvlPlayer);
  390. _teams.get(i + 1).add(biggestLvlPlayer);
  391. biggestLvlPlayer.setEventStatus();
  392. i = (i + 1) % _teamsNumber;
  393. }
  394. }
  395. catch (Exception e)
  396. {
  397. e.printStackTrace();
  398. return "Cannot start event, an error has occured.";
  399. }
  400. return "The event has been successfully started.";
  401. }
  402. /**
  403. * If the event state is OFF, it will not finish.
  404. * Sets the event state to OFF, unregisters and resets the players,
  405. * unspawns and clers the event NPCs, clears the teams, registered players,
  406. * connection loss data, sets the teams number to 0, sets the event name to empty.
  407. * @return a string with information if the event has been successfully stopped or not.
  408. */
  409. public static String finishEvent()
  410. {
  411. switch (eventState)
  412. {
  413. case OFF:
  414. return "Cannot finish event, it is already off.";
  415. case STANDBY:
  416. for (L2PcInstance player : _registeredPlayers)
  417. removeAndResetPlayer(player);
  418. unspawnEventNpcs();
  419. //_npcs.clear();
  420. _registeredPlayers.clear();
  421. _teams.clear();
  422. _connectionLossData.clear();
  423. _teamsNumber = 0;
  424. _eventName = "";
  425. eventState = EventState.OFF;
  426. return "The event has been stopped at STANDBY mode, all players unregistered and all event npcs unspawned.";
  427. case ON:
  428. for (FastList<L2PcInstance> teamList : _teams.values())
  429. {
  430. for (L2PcInstance player : teamList)
  431. removeAndResetPlayer(player);
  432. }
  433. eventState = EventState.OFF;
  434. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  435. unspawnEventNpcs(); // Just in case
  436. //_npcs.clear();
  437. _registeredPlayers.clear();
  438. _teams.clear();
  439. _connectionLossData.clear();
  440. _teamsNumber = 0;
  441. _eventName = "";
  442. _npcId = 0;
  443. _eventCreator = "";
  444. _eventInfo = "";
  445. return "The event has been stopped, all players unregistered and all event npcs unspawned.";
  446. }
  447. return "The event has been successfully finished.";
  448. }
  449. }