eventmodRace.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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.instancemanager.QuestManager;
  24. import com.l2jserver.gameserver.model.L2Effect;
  25. import com.l2jserver.gameserver.model.L2Skill;
  26. import com.l2jserver.gameserver.model.actor.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.quest.Event;
  29. import com.l2jserver.gameserver.model.quest.Quest;
  30. import com.l2jserver.gameserver.model.quest.QuestState;
  31. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  32. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  33. import com.l2jserver.util.Rnd;
  34. /**
  35. ** @author Gnacik
  36. **
  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. "Heretic catacomb enterance",
  66. "Dion castle bridge",
  67. "Floran village enterance",
  68. "Floran fort gate"
  69. };
  70. private static final int[][] _coords = {
  71. // x, y, z, heading
  72. { 39177, 144345, -3650, 0 },
  73. { 22294, 155892, -2950, 0 },
  74. { 16537, 169937, -3500, 0 },
  75. { 7644, 150898, -2890, 0 }
  76. };
  77. private static final int[][] _rewards = {
  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. public static void main(String[] args)
  94. {
  95. new eventmodRace(-1, "eventmodRace", "mods");
  96. }
  97. public eventmodRace(int questId, String name, String descr)
  98. {
  99. super(questId, name, descr);
  100. addStartNpc(_start_npc);
  101. addFirstTalkId(_start_npc);
  102. addTalkId(_start_npc);
  103. addStartNpc(_stop_npc);
  104. addFirstTalkId(_stop_npc);
  105. addTalkId(_stop_npc);
  106. }
  107. @Override
  108. public boolean eventStart()
  109. {
  110. // Don't start event if its active
  111. if(_isactive)
  112. return false;
  113. // Check Custom Table - we use custom NPC's
  114. if (!Config.CUSTOM_NPC_TABLE)
  115. return false;
  116. // Initialize list
  117. _npclist = new FastList<L2Npc>();
  118. _players = new FastList<L2PcInstance>();
  119. // Set Event active
  120. _isactive = true;
  121. // Spawn Manager
  122. _npc = recordSpawn(_start_npc, 18429, 145861, -3090, 0, false, 0);
  123. // Announce event start
  124. Announcements.getInstance().announceToAll("* Race Event started! *");
  125. Announcements.getInstance().announceToAll("Visit Event Manager in Dion village and signup, you have "+_time_register+" min before Race Start...");
  126. // Schedule Event end
  127. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  128. {
  129. public void run()
  130. {
  131. StartRace();
  132. }
  133. }, _time_register*60*1000);
  134. return true;
  135. }
  136. private void StartRace()
  137. {
  138. // Abort race if no players signup
  139. if (_players.isEmpty())
  140. {
  141. Announcements.getInstance().announceToAll("Race aborted, nobody signup.");
  142. eventStop();
  143. return;
  144. }
  145. // Set state
  146. _isRaceStarted = true;
  147. // Announce
  148. Announcements.getInstance().announceToAll("Race started!");
  149. // Get random Finish
  150. int location = Rnd.get(0, _locations.length-1);
  151. _randspawn = _coords[location];
  152. // And spawn NPC
  153. recordSpawn(_stop_npc, _randspawn[0], _randspawn[1], _randspawn[2], _randspawn[3], false, 0);
  154. // Transform players and send message
  155. for (L2PcInstance player : _players)
  156. {
  157. if (player != null && player.isOnline())
  158. {
  159. if (player.isInsideRadius(_npc, 500, false, false))
  160. {
  161. sendMessage(player, "Race started! Go find Finish NPC as fast as you can... He is located near "+_locations[location]);
  162. transformPlayer(player);
  163. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  164. }
  165. else
  166. {
  167. sendMessage(player, "I told you stay near me right? Distance was too high, you are excluded from race");
  168. _players.remove(player);
  169. }
  170. }
  171. }
  172. // Schedule timeup for Race
  173. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  174. {
  175. public void run()
  176. {
  177. timeUp();
  178. }
  179. }, _time_race*60*1000);
  180. }
  181. @Override
  182. public boolean eventStop()
  183. {
  184. // Don't stop inactive event
  185. if(!_isactive)
  186. return false;
  187. // Set inactive
  188. _isactive = false;
  189. _isRaceStarted = false;
  190. // Cancel task if any
  191. if (_eventTask != null)
  192. {
  193. _eventTask.cancel(true);
  194. _eventTask = null;
  195. }
  196. // Untransform players
  197. // Teleport to event start point
  198. if (!_players.isEmpty())
  199. {
  200. for (L2PcInstance player : _players)
  201. {
  202. if (player != null && player.isOnline())
  203. {
  204. player.untransform();
  205. player.teleToLocation(_npc.getX(), _npc.getY(), _npc.getZ(), true);
  206. }
  207. }
  208. }
  209. // Despawn Npc's
  210. if(!_npclist.isEmpty())
  211. {
  212. for (L2Npc _npc : _npclist)
  213. if (_npc != null)
  214. _npc.deleteMe();
  215. }
  216. _npclist.clear();
  217. _players.clear();
  218. // Announce event end
  219. Announcements.getInstance().announceToAll("* Race Event finished *");
  220. return true;
  221. }
  222. @Override
  223. public boolean eventBypass(L2PcInstance activeChar, String bypass)
  224. {
  225. if (bypass.startsWith("skill"))
  226. {
  227. if (_isRaceStarted)
  228. {
  229. activeChar.sendMessage("Race already started, you cannot change transform skill now");
  230. }
  231. else
  232. {
  233. int _number = Integer.valueOf(bypass.substring(5));
  234. L2Skill _sk = SkillTable.getInstance().getInfo(_number, 1);
  235. if(_sk != null)
  236. {
  237. _skill = _number;
  238. activeChar.sendMessage("Transform skill set to:");
  239. activeChar.sendMessage(_sk.getName());
  240. }
  241. else
  242. {
  243. activeChar.sendMessage("Error while changing transform skill");
  244. }
  245. }
  246. }
  247. else if (bypass.startsWith("tele"))
  248. {
  249. if(Integer.valueOf(bypass.substring(4)) > 0 && _randspawn != null)
  250. activeChar.teleToLocation(_randspawn[0], _randspawn[1], _randspawn[2]);
  251. else
  252. activeChar.teleToLocation(18429, 145861, -3090);
  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 = player.getQuestState(getName());
  262. if (st == null)
  263. return null;
  264. if (event.equalsIgnoreCase("transform"))
  265. {
  266. transformPlayer(player);
  267. return null;
  268. }
  269. else if (event.equalsIgnoreCase("untransform"))
  270. {
  271. player.untransform();
  272. return null;
  273. }
  274. else if (event.equalsIgnoreCase("showfinish"))
  275. {
  276. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  277. return null;
  278. }
  279. else if (event.equalsIgnoreCase("signup"))
  280. {
  281. if (_players.contains(player))
  282. return "900103-onlist.htm";
  283. _players.add(player);
  284. return "900103-signup.htm";
  285. }
  286. else if (event.equalsIgnoreCase("quit"))
  287. {
  288. player.untransform();
  289. if (_players.contains(player))
  290. _players.remove(player);
  291. return "900103-quit.htm";
  292. }
  293. else if (event.equalsIgnoreCase("finish"))
  294. {
  295. if (player.getFirstEffect(_skill) != null)
  296. {
  297. winRace(player);
  298. return "900104-winner.htm";
  299. }
  300. else
  301. return "900104-notrans.htm";
  302. }
  303. return htmltext;
  304. }
  305. @Override
  306. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  307. {
  308. QuestState st = player.getQuestState(getName());
  309. if (st == null)
  310. {
  311. Quest q = QuestManager.getInstance().getQuest(getName());
  312. st = q.newQuestState(player);
  313. }
  314. if (npc.getNpcId() == _start_npc)
  315. {
  316. if (_isRaceStarted)
  317. return _start_npc+"-started-"+isRacing(player)+".htm";
  318. else
  319. return _start_npc+"-"+isRacing(player)+".htm";
  320. }
  321. else if (npc.getNpcId() == _stop_npc && _isRaceStarted)
  322. {
  323. return _stop_npc+"-"+isRacing(player)+".htm";
  324. }
  325. return npc.getNpcId()+".htm";
  326. }
  327. private int isRacing(L2PcInstance player)
  328. {
  329. if (_players.isEmpty())
  330. return 0;
  331. if (_players.contains(player))
  332. return 1;
  333. return 0;
  334. }
  335. private L2Npc recordSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  336. {
  337. L2Npc _tmp = addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay);
  338. if(_tmp != null)
  339. _npclist.add(_tmp);
  340. return _tmp;
  341. }
  342. private void transformPlayer(L2PcInstance player)
  343. {
  344. if (player.isTransformed() || player.isInStance())
  345. player.untransform();
  346. if (player.isSitting())
  347. player.standUp();
  348. for (L2Effect e : player.getAllEffects())
  349. {
  350. if (e.getAbnormalType().equalsIgnoreCase("speed_up"))
  351. e.exit();
  352. if (e.getSkill() != null && (
  353. e.getSkill().getId() == 268 || // Song of Wind
  354. e.getSkill().getId() == 298)) // Rabbit Spirit Totem
  355. e.exit();
  356. }
  357. SkillTable.getInstance().getInfo(_skill, 1).getEffects(player, player);
  358. }
  359. private void sendMessage(L2PcInstance player, String text)
  360. {
  361. player.sendPacket(new CreatureSay(_npc.getObjectId(), 20, _npc.getName(), text));
  362. }
  363. private void showMenu(L2PcInstance activeChar)
  364. {
  365. NpcHtmlMessage html = new NpcHtmlMessage(0);
  366. String content = getHtm(activeChar.getHtmlPrefix(), "admin_menu.htm");
  367. html.setHtml(content);
  368. activeChar.sendPacket(html);
  369. }
  370. private void timeUp()
  371. {
  372. Announcements.getInstance().announceToAll("Time up, nobody wins!");
  373. eventStop();
  374. }
  375. private void winRace(L2PcInstance player)
  376. {
  377. int[] _reward = _rewards[Rnd.get(_rewards.length-1)];
  378. player.addItem("eventModRace", _reward[0], _reward[1], _npc, true);
  379. Announcements.getInstance().announceToAll(player.getName()+" is a winner!");
  380. eventStop();
  381. }
  382. }