eventmodRace.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (C) 2004-2014 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package mods.eventmodRace;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.concurrent.CopyOnWriteArrayList;
  23. import java.util.concurrent.ScheduledFuture;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.Announcements;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.datatables.SkillData;
  28. import com.l2jserver.gameserver.model.actor.L2Npc;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.quest.Event;
  31. import com.l2jserver.gameserver.model.quest.QuestState;
  32. import com.l2jserver.gameserver.model.skills.AbnormalType;
  33. import com.l2jserver.gameserver.model.skills.Skill;
  34. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  35. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  36. /**
  37. * @author Gnacik
  38. */
  39. public final class eventmodRace extends Event
  40. {
  41. // Event NPC's list
  42. private List<L2Npc> _npclist;
  43. // Npc
  44. private L2Npc _npc;
  45. // Player list
  46. private List<L2PcInstance> _players;
  47. // Event Task
  48. ScheduledFuture<?> _eventTask = null;
  49. // Event state
  50. private static boolean _isactive = false;
  51. // Race state
  52. private static boolean _isRaceStarted = false;
  53. // 5 min for register
  54. private static final int _time_register = 5;
  55. // 5 min for race
  56. private static final int _time_race = 10;
  57. // NPC's
  58. private static final int _start_npc = 900103;
  59. private static final int _stop_npc = 900104;
  60. // Skills (Frog by default)
  61. private static int _skill = 6201;
  62. // We must keep second NPC spawn for radar
  63. private static int[] _randspawn = null;
  64. // Locations
  65. private static final String[] _locations =
  66. {
  67. "Heretic catacomb enterance",
  68. "Dion castle bridge",
  69. "Floran village enterance",
  70. "Floran fort gate"
  71. };
  72. // @formatter:off
  73. private static final int[][] _coords =
  74. {
  75. // x, y, z, heading
  76. { 39177, 144345, -3650, 0 },
  77. { 22294, 155892, -2950, 0 },
  78. { 16537, 169937, -3500, 0 },
  79. { 7644, 150898, -2890, 0 }
  80. };
  81. private static final int[][] _rewards =
  82. {
  83. { 6622, 2 }, // Giant's Codex
  84. { 9625, 2 }, // Giant's Codex -
  85. { 9626, 2 }, // Giant's Codex -
  86. { 9627, 2 }, // Giant's Codex -
  87. { 9546, 5 }, // Attr stones
  88. { 9547, 5 },
  89. { 9548, 5 },
  90. { 9549, 5 },
  91. { 9550, 5 },
  92. { 9551, 5 },
  93. { 9574, 3 }, // Mid-Grade Life Stone: level 80
  94. { 9575, 2 }, // High-Grade Life Stone: level 80
  95. { 9576, 1 }, // Top-Grade Life Stone: level 80
  96. { 20034,1 } // Revita pop
  97. };
  98. // @formatter:on
  99. private eventmodRace()
  100. {
  101. super(-1, eventmodRace.class.getSimpleName(), "mods");
  102. addStartNpc(_start_npc);
  103. addFirstTalkId(_start_npc);
  104. addTalkId(_start_npc);
  105. addStartNpc(_stop_npc);
  106. addFirstTalkId(_stop_npc);
  107. addTalkId(_stop_npc);
  108. }
  109. @Override
  110. public boolean eventStart()
  111. {
  112. // Don't start event if its active
  113. if (_isactive)
  114. {
  115. return false;
  116. }
  117. // Check Custom Table - we use custom NPC's
  118. if (!Config.CUSTOM_NPC_DATA)
  119. {
  120. _log.info(getName() + ": Event can't be started, because custom npc table is disabled!");
  121. return false;
  122. }
  123. // Initialize list
  124. _npclist = new ArrayList<>();
  125. _players = new CopyOnWriteArrayList<>();
  126. // Set Event active
  127. _isactive = true;
  128. // Spawn Manager
  129. _npc = recordSpawn(_start_npc, 18429, 145861, -3090, 0, false, 0);
  130. // Announce event start
  131. Announcements.getInstance().announceToAll("* Race Event started! *");
  132. Announcements.getInstance().announceToAll("Visit Event Manager in Dion village and signup, you have " + _time_register + " min before Race Start...");
  133. // Schedule Event end
  134. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  135. {
  136. @Override
  137. public void run()
  138. {
  139. StartRace();
  140. }
  141. }, _time_register * 60 * 1000);
  142. return true;
  143. }
  144. protected void StartRace()
  145. {
  146. // Abort race if no players signup
  147. if (_players.isEmpty())
  148. {
  149. Announcements.getInstance().announceToAll("Race aborted, nobody signup.");
  150. eventStop();
  151. return;
  152. }
  153. // Set state
  154. _isRaceStarted = true;
  155. // Announce
  156. Announcements.getInstance().announceToAll("Race started!");
  157. // Get random Finish
  158. int location = getRandom(0, _locations.length - 1);
  159. _randspawn = _coords[location];
  160. // And spawn NPC
  161. recordSpawn(_stop_npc, _randspawn[0], _randspawn[1], _randspawn[2], _randspawn[3], false, 0);
  162. // Transform players and send message
  163. for (L2PcInstance player : _players)
  164. {
  165. if ((player != null) && player.isOnline())
  166. {
  167. if (player.isInsideRadius(_npc, 500, false, false))
  168. {
  169. sendMessage(player, "Race started! Go find Finish NPC as fast as you can... He is located near " + _locations[location]);
  170. transformPlayer(player);
  171. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  172. }
  173. else
  174. {
  175. sendMessage(player, "I told you stay near me right? Distance was too high, you are excluded from race");
  176. _players.remove(player);
  177. }
  178. }
  179. }
  180. // Schedule timeup for Race
  181. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  182. {
  183. @Override
  184. public void run()
  185. {
  186. timeUp();
  187. }
  188. }, _time_race * 60 * 1000);
  189. }
  190. @Override
  191. public boolean eventStop()
  192. {
  193. // Don't stop inactive event
  194. if (!_isactive)
  195. {
  196. return false;
  197. }
  198. // Set inactive
  199. _isactive = false;
  200. _isRaceStarted = false;
  201. // Cancel task if any
  202. if (_eventTask != null)
  203. {
  204. _eventTask.cancel(true);
  205. _eventTask = null;
  206. }
  207. // Untransform players
  208. // Teleport to event start point
  209. for (L2PcInstance player : _players)
  210. {
  211. if ((player != null) && player.isOnline())
  212. {
  213. player.untransform();
  214. player.teleToLocation(_npc.getX(), _npc.getY(), _npc.getZ(), true);
  215. }
  216. }
  217. // Despawn NPCs
  218. for (L2Npc _npc : _npclist)
  219. {
  220. if (_npc != null)
  221. {
  222. _npc.deleteMe();
  223. }
  224. }
  225. _npclist.clear();
  226. _players.clear();
  227. // Announce event end
  228. Announcements.getInstance().announceToAll("* Race Event finished *");
  229. return true;
  230. }
  231. @Override
  232. public boolean eventBypass(L2PcInstance activeChar, String bypass)
  233. {
  234. if (bypass.startsWith("skill"))
  235. {
  236. if (_isRaceStarted)
  237. {
  238. activeChar.sendMessage("Race already started, you cannot change transform skill now");
  239. }
  240. else
  241. {
  242. int _number = Integer.valueOf(bypass.substring(5));
  243. Skill _sk = SkillData.getInstance().getSkill(_number, 1);
  244. if (_sk != null)
  245. {
  246. _skill = _number;
  247. activeChar.sendMessage("Transform skill set to:");
  248. activeChar.sendMessage(_sk.getName());
  249. }
  250. else
  251. {
  252. activeChar.sendMessage("Error while changing transform skill");
  253. }
  254. }
  255. }
  256. else if (bypass.startsWith("tele"))
  257. {
  258. if ((Integer.valueOf(bypass.substring(4)) > 0) && (_randspawn != null))
  259. {
  260. activeChar.teleToLocation(_randspawn[0], _randspawn[1], _randspawn[2]);
  261. }
  262. else
  263. {
  264. activeChar.teleToLocation(18429, 145861, -3090);
  265. }
  266. }
  267. showMenu(activeChar);
  268. return true;
  269. }
  270. @Override
  271. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  272. {
  273. String htmltext = event;
  274. QuestState st = player.getQuestState(getName());
  275. if (st == null)
  276. {
  277. return null;
  278. }
  279. if (event.equalsIgnoreCase("transform"))
  280. {
  281. transformPlayer(player);
  282. return null;
  283. }
  284. else if (event.equalsIgnoreCase("untransform"))
  285. {
  286. player.untransform();
  287. return null;
  288. }
  289. else if (event.equalsIgnoreCase("showfinish"))
  290. {
  291. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  292. return null;
  293. }
  294. else if (event.equalsIgnoreCase("signup"))
  295. {
  296. if (_players.contains(player))
  297. {
  298. return "900103-onlist.htm";
  299. }
  300. _players.add(player);
  301. return "900103-signup.htm";
  302. }
  303. else if (event.equalsIgnoreCase("quit"))
  304. {
  305. player.untransform();
  306. if (_players.contains(player))
  307. {
  308. _players.remove(player);
  309. }
  310. return "900103-quit.htm";
  311. }
  312. else if (event.equalsIgnoreCase("finish"))
  313. {
  314. if (player.isAffectedBySkill(_skill))
  315. {
  316. winRace(player);
  317. return "900104-winner.htm";
  318. }
  319. return "900104-notrans.htm";
  320. }
  321. return htmltext;
  322. }
  323. @Override
  324. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  325. {
  326. QuestState st = player.getQuestState(getName());
  327. if (st == null)
  328. {
  329. st = newQuestState(player);
  330. }
  331. if (npc.getId() == _start_npc)
  332. {
  333. if (_isRaceStarted)
  334. {
  335. return _start_npc + "-started-" + isRacing(player) + ".htm";
  336. }
  337. return _start_npc + "-" + isRacing(player) + ".htm";
  338. }
  339. else if ((npc.getId() == _stop_npc) && _isRaceStarted)
  340. {
  341. return _stop_npc + "-" + isRacing(player) + ".htm";
  342. }
  343. return npc.getId() + ".htm";
  344. }
  345. private int isRacing(L2PcInstance player)
  346. {
  347. return _players.contains(player) ? 1 : 0;
  348. }
  349. private L2Npc recordSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  350. {
  351. final L2Npc npc = addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay);
  352. if (npc != null)
  353. {
  354. _npclist.add(npc);
  355. }
  356. return npc;
  357. }
  358. private void transformPlayer(L2PcInstance player)
  359. {
  360. if (player.isTransformed() || player.isInStance())
  361. {
  362. player.untransform();
  363. }
  364. if (player.isSitting())
  365. {
  366. player.standUp();
  367. }
  368. player.getEffectList().stopSkillEffects(true, AbnormalType.SPEED_UP);
  369. player.stopSkillEffects(true, 268);
  370. player.stopSkillEffects(true, 298); // Rabbit Spirit Totem
  371. SkillData.getInstance().getSkill(_skill, 1).applyEffects(player, player);
  372. }
  373. private void sendMessage(L2PcInstance player, String text)
  374. {
  375. player.sendPacket(new CreatureSay(_npc.getObjectId(), 20, _npc.getName(), text));
  376. }
  377. private void showMenu(L2PcInstance activeChar)
  378. {
  379. final NpcHtmlMessage html = new NpcHtmlMessage();
  380. String content = getHtm(activeChar.getHtmlPrefix(), "admin_menu.htm");
  381. html.setHtml(content);
  382. activeChar.sendPacket(html);
  383. }
  384. protected void timeUp()
  385. {
  386. Announcements.getInstance().announceToAll("Time up, nobody wins!");
  387. eventStop();
  388. }
  389. private void winRace(L2PcInstance player)
  390. {
  391. int[] _reward = _rewards[getRandom(_rewards.length - 1)];
  392. player.addItem("eventModRace", _reward[0], _reward[1], _npc, true);
  393. Announcements.getInstance().announceToAll(player.getName() + " is a winner!");
  394. eventStop();
  395. }
  396. public static void main(String[] args)
  397. {
  398. new eventmodRace();
  399. }
  400. }