eventmodRace.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 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. public static void main(String[] args)
  100. {
  101. new eventmodRace(-1, "eventmodRace", "mods");
  102. }
  103. public eventmodRace(int questId, String name, String descr)
  104. {
  105. super(questId, name, descr);
  106. addStartNpc(_start_npc);
  107. addFirstTalkId(_start_npc);
  108. addTalkId(_start_npc);
  109. addStartNpc(_stop_npc);
  110. addFirstTalkId(_stop_npc);
  111. addTalkId(_stop_npc);
  112. }
  113. @Override
  114. public boolean eventStart()
  115. {
  116. // Don't start event if its active
  117. if (_isactive)
  118. {
  119. return false;
  120. }
  121. // Check Custom Table - we use custom NPC's
  122. if (!Config.CUSTOM_NPC_DATA)
  123. {
  124. _log.info(getName() + ": Event can't be started, because custom npc table is disabled!");
  125. return false;
  126. }
  127. // Initialize list
  128. _npclist = new ArrayList<>();
  129. _players = new CopyOnWriteArrayList<>();
  130. // Set Event active
  131. _isactive = true;
  132. // Spawn Manager
  133. _npc = recordSpawn(_start_npc, 18429, 145861, -3090, 0, false, 0);
  134. // Announce event start
  135. Announcements.getInstance().announceToAll("* Race Event started! *");
  136. Announcements.getInstance().announceToAll("Visit Event Manager in Dion village and signup, you have " + _time_register + " min before Race Start...");
  137. // Schedule Event end
  138. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  139. {
  140. @Override
  141. public void run()
  142. {
  143. StartRace();
  144. }
  145. }, _time_register * 60 * 1000);
  146. return true;
  147. }
  148. protected void StartRace()
  149. {
  150. // Abort race if no players signup
  151. if (_players.isEmpty())
  152. {
  153. Announcements.getInstance().announceToAll("Race aborted, nobody signup.");
  154. eventStop();
  155. return;
  156. }
  157. // Set state
  158. _isRaceStarted = true;
  159. // Announce
  160. Announcements.getInstance().announceToAll("Race started!");
  161. // Get random Finish
  162. int location = getRandom(0, _locations.length - 1);
  163. _randspawn = _coords[location];
  164. // And spawn NPC
  165. recordSpawn(_stop_npc, _randspawn[0], _randspawn[1], _randspawn[2], _randspawn[3], false, 0);
  166. // Transform players and send message
  167. for (L2PcInstance player : _players)
  168. {
  169. if ((player != null) && player.isOnline())
  170. {
  171. if (player.isInsideRadius(_npc, 500, false, false))
  172. {
  173. sendMessage(player, "Race started! Go find Finish NPC as fast as you can... He is located near " + _locations[location]);
  174. transformPlayer(player);
  175. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  176. }
  177. else
  178. {
  179. sendMessage(player, "I told you stay near me right? Distance was too high, you are excluded from race");
  180. _players.remove(player);
  181. }
  182. }
  183. }
  184. // Schedule timeup for Race
  185. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  186. {
  187. @Override
  188. public void run()
  189. {
  190. timeUp();
  191. }
  192. }, _time_race * 60 * 1000);
  193. }
  194. @Override
  195. public boolean eventStop()
  196. {
  197. // Don't stop inactive event
  198. if (!_isactive)
  199. {
  200. return false;
  201. }
  202. // Set inactive
  203. _isactive = false;
  204. _isRaceStarted = false;
  205. // Cancel task if any
  206. if (_eventTask != null)
  207. {
  208. _eventTask.cancel(true);
  209. _eventTask = null;
  210. }
  211. // Untransform players
  212. // Teleport to event start point
  213. for (L2PcInstance player : _players)
  214. {
  215. if ((player != null) && player.isOnline())
  216. {
  217. player.untransform();
  218. player.teleToLocation(_npc.getX(), _npc.getY(), _npc.getZ(), true);
  219. }
  220. }
  221. // Despawn NPCs
  222. for (L2Npc _npc : _npclist)
  223. {
  224. if (_npc != null)
  225. {
  226. _npc.deleteMe();
  227. }
  228. }
  229. _npclist.clear();
  230. _players.clear();
  231. // Announce event end
  232. Announcements.getInstance().announceToAll("* Race Event finished *");
  233. return true;
  234. }
  235. @Override
  236. public boolean eventBypass(L2PcInstance activeChar, String bypass)
  237. {
  238. if (bypass.startsWith("skill"))
  239. {
  240. if (_isRaceStarted)
  241. {
  242. activeChar.sendMessage("Race already started, you cannot change transform skill now");
  243. }
  244. else
  245. {
  246. int _number = Integer.valueOf(bypass.substring(5));
  247. Skill _sk = SkillData.getInstance().getSkill(_number, 1);
  248. if (_sk != null)
  249. {
  250. _skill = _number;
  251. activeChar.sendMessage("Transform skill set to:");
  252. activeChar.sendMessage(_sk.getName());
  253. }
  254. else
  255. {
  256. activeChar.sendMessage("Error while changing transform skill");
  257. }
  258. }
  259. }
  260. else if (bypass.startsWith("tele"))
  261. {
  262. if ((Integer.valueOf(bypass.substring(4)) > 0) && (_randspawn != null))
  263. {
  264. activeChar.teleToLocation(_randspawn[0], _randspawn[1], _randspawn[2]);
  265. }
  266. else
  267. {
  268. activeChar.teleToLocation(18429, 145861, -3090);
  269. }
  270. }
  271. showMenu(activeChar);
  272. return true;
  273. }
  274. @Override
  275. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  276. {
  277. String htmltext = event;
  278. QuestState st = player.getQuestState(getName());
  279. if (st == null)
  280. {
  281. return null;
  282. }
  283. if (event.equalsIgnoreCase("transform"))
  284. {
  285. transformPlayer(player);
  286. return null;
  287. }
  288. else if (event.equalsIgnoreCase("untransform"))
  289. {
  290. player.untransform();
  291. return null;
  292. }
  293. else if (event.equalsIgnoreCase("showfinish"))
  294. {
  295. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  296. return null;
  297. }
  298. else if (event.equalsIgnoreCase("signup"))
  299. {
  300. if (_players.contains(player))
  301. {
  302. return "900103-onlist.htm";
  303. }
  304. _players.add(player);
  305. return "900103-signup.htm";
  306. }
  307. else if (event.equalsIgnoreCase("quit"))
  308. {
  309. player.untransform();
  310. if (_players.contains(player))
  311. {
  312. _players.remove(player);
  313. }
  314. return "900103-quit.htm";
  315. }
  316. else if (event.equalsIgnoreCase("finish"))
  317. {
  318. if (player.isAffectedBySkill(_skill))
  319. {
  320. winRace(player);
  321. return "900104-winner.htm";
  322. }
  323. return "900104-notrans.htm";
  324. }
  325. return htmltext;
  326. }
  327. @Override
  328. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  329. {
  330. QuestState st = player.getQuestState(getName());
  331. if (st == null)
  332. {
  333. st = newQuestState(player);
  334. }
  335. if (npc.getId() == _start_npc)
  336. {
  337. if (_isRaceStarted)
  338. {
  339. return _start_npc + "-started-" + isRacing(player) + ".htm";
  340. }
  341. return _start_npc + "-" + isRacing(player) + ".htm";
  342. }
  343. else if ((npc.getId() == _stop_npc) && _isRaceStarted)
  344. {
  345. return _stop_npc + "-" + isRacing(player) + ".htm";
  346. }
  347. return npc.getId() + ".htm";
  348. }
  349. private int isRacing(L2PcInstance player)
  350. {
  351. return _players.contains(player) ? 1 : 0;
  352. }
  353. private L2Npc recordSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  354. {
  355. final L2Npc npc = addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay);
  356. if (npc != null)
  357. {
  358. _npclist.add(npc);
  359. }
  360. return npc;
  361. }
  362. private void transformPlayer(L2PcInstance player)
  363. {
  364. if (player.isTransformed() || player.isInStance())
  365. {
  366. player.untransform();
  367. }
  368. if (player.isSitting())
  369. {
  370. player.standUp();
  371. }
  372. player.getEffectList().stopSkillEffects(true, AbnormalType.SPEED_UP);
  373. player.stopSkillEffects(true, 268);
  374. player.stopSkillEffects(true, 298); // Rabbit Spirit Totem
  375. SkillData.getInstance().getSkill(_skill, 1).applyEffects(player, player);
  376. }
  377. private void sendMessage(L2PcInstance player, String text)
  378. {
  379. player.sendPacket(new CreatureSay(_npc.getObjectId(), 20, _npc.getName(), text));
  380. }
  381. private void showMenu(L2PcInstance activeChar)
  382. {
  383. final NpcHtmlMessage html = new NpcHtmlMessage();
  384. String content = getHtm(activeChar.getHtmlPrefix(), "admin_menu.htm");
  385. html.setHtml(content);
  386. activeChar.sendPacket(html);
  387. }
  388. protected void timeUp()
  389. {
  390. Announcements.getInstance().announceToAll("Time up, nobody wins!");
  391. eventStop();
  392. }
  393. private void winRace(L2PcInstance player)
  394. {
  395. int[] _reward = _rewards[getRandom(_rewards.length - 1)];
  396. player.addItem("eventModRace", _reward[0], _reward[1], _npc, true);
  397. Announcements.getInstance().announceToAll(player.getName() + " is a winner!");
  398. eventStop();
  399. }
  400. }