Race.java 11 KB

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