eventmodRace.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 mods.eventmodRace;
  16. import java.util.List;
  17. import java.util.concurrent.ScheduledFuture;
  18. import javolution.util.FastList;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.Announcements;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. import com.l2jserver.gameserver.datatables.SkillTable;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.effects.L2Effect;
  26. import com.l2jserver.gameserver.model.quest.Event;
  27. import com.l2jserver.gameserver.model.quest.QuestState;
  28. import com.l2jserver.gameserver.model.skills.L2Skill;
  29. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  30. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  31. /**
  32. * @author Gnacik
  33. */
  34. public class eventmodRace extends Event
  35. {
  36. // Event NPC's list
  37. private List<L2Npc> _npclist;
  38. // Npc
  39. private L2Npc _npc;
  40. // Player list
  41. private List<L2PcInstance> _players;
  42. // Event Task
  43. ScheduledFuture<?> _eventTask = null;
  44. // Event state
  45. private static boolean _isactive = false;
  46. // Race state
  47. private static boolean _isRaceStarted = false;
  48. // 5 min for register
  49. private static final int _time_register = 5;
  50. // 5 min for race
  51. private static final int _time_race = 10;
  52. // NPC's
  53. private static final int _start_npc = 900103;
  54. private static final int _stop_npc = 900104;
  55. // Skills (Frog by default)
  56. private static int _skill = 6201;
  57. // We must keep second NPC spawn for radar
  58. private static int[] _randspawn = null;
  59. // Locations
  60. private static final String[] _locations =
  61. {
  62. "Heretic catacomb enterance",
  63. "Dion castle bridge",
  64. "Floran village enterance",
  65. "Floran fort gate"
  66. };
  67. // @formatter:off
  68. private static final int[][] _coords =
  69. {
  70. // x, y, z, heading
  71. { 39177, 144345, -3650, 0 },
  72. { 22294, 155892, -2950, 0 },
  73. { 16537, 169937, -3500, 0 },
  74. { 7644, 150898, -2890, 0 }
  75. };
  76. private static final int[][] _rewards =
  77. {
  78. { 6622, 2 }, // Giant's Codex
  79. { 9625, 2 }, // Giant's Codex -
  80. { 9626, 2 }, // Giant's Codex -
  81. { 9627, 2 }, // Giant's Codex -
  82. { 9546, 5 }, // Attr stones
  83. { 9547, 5 },
  84. { 9548, 5 },
  85. { 9549, 5 },
  86. { 9550, 5 },
  87. { 9551, 5 },
  88. { 9574, 3 }, // Mid-Grade Life Stone: level 80
  89. { 9575, 2 }, // High-Grade Life Stone: level 80
  90. { 9576, 1 }, // Top-Grade Life Stone: level 80
  91. { 20034,1 } // Revita pop
  92. };
  93. // @formatter:on
  94. public static void main(String[] args)
  95. {
  96. new eventmodRace(-1, "eventmodRace", "mods");
  97. }
  98. public eventmodRace(int questId, String name, String descr)
  99. {
  100. super(questId, name, descr);
  101. addStartNpc(_start_npc);
  102. addFirstTalkId(_start_npc);
  103. addTalkId(_start_npc);
  104. addStartNpc(_stop_npc);
  105. addFirstTalkId(_stop_npc);
  106. addTalkId(_stop_npc);
  107. }
  108. @Override
  109. public boolean eventStart()
  110. {
  111. // Don't start event if its active
  112. if (_isactive)
  113. {
  114. return false;
  115. }
  116. // Check Custom Table - we use custom NPC's
  117. if (!Config.CUSTOM_NPC_TABLE)
  118. {
  119. return false;
  120. }
  121. // Initialize list
  122. _npclist = new FastList<>();
  123. _players = new FastList<>();
  124. // Set Event active
  125. _isactive = true;
  126. // Spawn Manager
  127. _npc = recordSpawn(_start_npc, 18429, 145861, -3090, 0, false, 0);
  128. // Announce event start
  129. Announcements.getInstance().announceToAll("* Race Event started! *");
  130. Announcements.getInstance().announceToAll("Visit Event Manager in Dion village and signup, you have " + _time_register + " min before Race Start...");
  131. // Schedule Event end
  132. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  133. {
  134. @Override
  135. public void run()
  136. {
  137. StartRace();
  138. }
  139. }, _time_register * 60 * 1000);
  140. return true;
  141. }
  142. protected void StartRace()
  143. {
  144. // Abort race if no players signup
  145. if (_players.isEmpty())
  146. {
  147. Announcements.getInstance().announceToAll("Race aborted, nobody signup.");
  148. eventStop();
  149. return;
  150. }
  151. // Set state
  152. _isRaceStarted = true;
  153. // Announce
  154. Announcements.getInstance().announceToAll("Race started!");
  155. // Get random Finish
  156. int location = getRandom(0, _locations.length - 1);
  157. _randspawn = _coords[location];
  158. // And spawn NPC
  159. recordSpawn(_stop_npc, _randspawn[0], _randspawn[1], _randspawn[2], _randspawn[3], false, 0);
  160. // Transform players and send message
  161. for (L2PcInstance player : _players)
  162. {
  163. if ((player != null) && player.isOnline())
  164. {
  165. if (player.isInsideRadius(_npc, 500, false, false))
  166. {
  167. sendMessage(player, "Race started! Go find Finish NPC as fast as you can... He is located near " + _locations[location]);
  168. transformPlayer(player);
  169. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  170. }
  171. else
  172. {
  173. sendMessage(player, "I told you stay near me right? Distance was too high, you are excluded from race");
  174. _players.remove(player);
  175. }
  176. }
  177. }
  178. // Schedule timeup for Race
  179. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  180. {
  181. @Override
  182. public void run()
  183. {
  184. timeUp();
  185. }
  186. }, _time_race * 60 * 1000);
  187. }
  188. @Override
  189. public boolean eventStop()
  190. {
  191. // Don't stop inactive event
  192. if (!_isactive)
  193. {
  194. return false;
  195. }
  196. // Set inactive
  197. _isactive = false;
  198. _isRaceStarted = false;
  199. // Cancel task if any
  200. if (_eventTask != null)
  201. {
  202. _eventTask.cancel(true);
  203. _eventTask = null;
  204. }
  205. // Untransform players
  206. // Teleport to event start point
  207. if (!_players.isEmpty())
  208. {
  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. }
  218. // Despawn Npc's
  219. if (!_npclist.isEmpty())
  220. {
  221. for (L2Npc _npc : _npclist)
  222. {
  223. if (_npc != null)
  224. {
  225. _npc.deleteMe();
  226. }
  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. L2Skill _sk = SkillTable.getInstance().getInfo(_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.getFirstEffect(_skill) != null)
  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.getNpcId() == _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.getNpcId() == _stop_npc) && _isRaceStarted)
  344. {
  345. return _stop_npc + "-" + isRacing(player) + ".htm";
  346. }
  347. return npc.getNpcId() + ".htm";
  348. }
  349. private int isRacing(L2PcInstance player)
  350. {
  351. if (_players.isEmpty())
  352. {
  353. return 0;
  354. }
  355. if (_players.contains(player))
  356. {
  357. return 1;
  358. }
  359. return 0;
  360. }
  361. private L2Npc recordSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  362. {
  363. L2Npc _tmp = addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay);
  364. if (_tmp != null)
  365. {
  366. _npclist.add(_tmp);
  367. }
  368. return _tmp;
  369. }
  370. private void transformPlayer(L2PcInstance player)
  371. {
  372. if (player.isTransformed() || player.isInStance())
  373. {
  374. player.untransform();
  375. }
  376. if (player.isSitting())
  377. {
  378. player.standUp();
  379. }
  380. for (L2Effect e : player.getAllEffects())
  381. {
  382. if (e.getAbnormalType().equalsIgnoreCase("speed_up"))
  383. {
  384. e.exit();
  385. }
  386. if ((e.getSkill() != null) && ((e.getSkill().getId() == 268) || // Song of Wind
  387. (e.getSkill().getId() == 298)))
  388. {
  389. e.exit();
  390. }
  391. }
  392. SkillTable.getInstance().getInfo(_skill, 1).getEffects(player, player);
  393. }
  394. private void sendMessage(L2PcInstance player, String text)
  395. {
  396. player.sendPacket(new CreatureSay(_npc.getObjectId(), 20, _npc.getName(), text));
  397. }
  398. private void showMenu(L2PcInstance activeChar)
  399. {
  400. NpcHtmlMessage html = new NpcHtmlMessage(0);
  401. String content = getHtm(activeChar.getHtmlPrefix(), "admin_menu.htm");
  402. html.setHtml(content);
  403. activeChar.sendPacket(html);
  404. }
  405. protected void timeUp()
  406. {
  407. Announcements.getInstance().announceToAll("Time up, nobody wins!");
  408. eventStop();
  409. }
  410. private void winRace(L2PcInstance player)
  411. {
  412. int[] _reward = _rewards[getRandom(_rewards.length - 1)];
  413. player.addItem("eventModRace", _reward[0], _reward[1], _npc, true);
  414. Announcements.getInstance().announceToAll(player.getName() + " is a winner!");
  415. eventStop();
  416. }
  417. }