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