eventmodRace.java 11 KB

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