eventmodRace.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.actor.L2Npc;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.effects.L2Effect;
  27. import com.l2jserver.gameserver.model.quest.Event;
  28. import com.l2jserver.gameserver.model.quest.Quest;
  29. import com.l2jserver.gameserver.model.quest.QuestState;
  30. import com.l2jserver.gameserver.model.skills.L2Skill;
  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. public class eventmodRace extends Event
  38. {
  39. // Event NPC's list
  40. private List<L2Npc> _npclist;
  41. // Npc
  42. private L2Npc _npc;
  43. // Player list
  44. private List<L2PcInstance> _players;
  45. // Event Task
  46. ScheduledFuture<?> _eventTask = null;
  47. // Event state
  48. private static boolean _isactive = false;
  49. // Race state
  50. private static boolean _isRaceStarted = false;
  51. // 5 min for register
  52. private static final int _time_register = 5;
  53. // 5 min for race
  54. private static final int _time_race = 10;
  55. // NPC's
  56. private static final int _start_npc = 900103;
  57. private static final int _stop_npc = 900104;
  58. // Skills (Frog by default)
  59. private static int _skill = 6201;
  60. // We must keep second NPC spawn for radar
  61. private static int[] _randspawn = null;
  62. // Locations
  63. private static final String[] _locations = {
  64. "Heretic catacomb enterance",
  65. "Dion castle bridge",
  66. "Floran village enterance",
  67. "Floran fort gate"
  68. };
  69. private static final int[][] _coords = {
  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. { 6622, 2 }, // Giant's Codex
  78. { 9625, 2 }, // Giant's Codex -
  79. { 9626, 2 }, // Giant's Codex -
  80. { 9627, 2 }, // Giant's Codex -
  81. { 9546, 5 }, // Attr stones
  82. { 9547, 5 },
  83. { 9548, 5 },
  84. { 9549, 5 },
  85. { 9550, 5 },
  86. { 9551, 5 },
  87. { 9574, 3 }, // Mid-Grade Life Stone: level 80
  88. { 9575, 2 }, // High-Grade Life Stone: level 80
  89. { 9576, 1 }, // Top-Grade Life Stone: level 80
  90. { 20034,1 } // Revita pop
  91. };
  92. public static void main(String[] args)
  93. {
  94. new eventmodRace(-1, "eventmodRace", "mods");
  95. }
  96. public eventmodRace(int questId, String name, String descr)
  97. {
  98. super(questId, name, descr);
  99. addStartNpc(_start_npc);
  100. addFirstTalkId(_start_npc);
  101. addTalkId(_start_npc);
  102. addStartNpc(_stop_npc);
  103. addFirstTalkId(_stop_npc);
  104. addTalkId(_stop_npc);
  105. }
  106. @Override
  107. public boolean eventStart()
  108. {
  109. // Don't start event if its active
  110. if(_isactive)
  111. return false;
  112. // Check Custom Table - we use custom NPC's
  113. if (!Config.CUSTOM_NPC_TABLE)
  114. return false;
  115. // Initialize list
  116. _npclist = new FastList<L2Npc>();
  117. _players = new FastList<L2PcInstance>();
  118. // Set Event active
  119. _isactive = true;
  120. // Spawn Manager
  121. _npc = recordSpawn(_start_npc, 18429, 145861, -3090, 0, false, 0);
  122. // Announce event start
  123. Announcements.getInstance().announceToAll("* Race Event started! *");
  124. Announcements.getInstance().announceToAll("Visit Event Manager in Dion village and signup, you have "+_time_register+" min before Race Start...");
  125. // Schedule Event end
  126. _eventTask = ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
  127. {
  128. @Override
  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. @Override
  176. public void run()
  177. {
  178. timeUp();
  179. }
  180. }, _time_race*60*1000);
  181. }
  182. @Override
  183. public boolean eventStop()
  184. {
  185. // Don't stop inactive event
  186. if(!_isactive)
  187. return false;
  188. // Set inactive
  189. _isactive = false;
  190. _isRaceStarted = false;
  191. // Cancel task if any
  192. if (_eventTask != null)
  193. {
  194. _eventTask.cancel(true);
  195. _eventTask = null;
  196. }
  197. // Untransform players
  198. // Teleport to event start point
  199. if (!_players.isEmpty())
  200. {
  201. for (L2PcInstance player : _players)
  202. {
  203. if (player != null && player.isOnline())
  204. {
  205. player.untransform();
  206. player.teleToLocation(_npc.getX(), _npc.getY(), _npc.getZ(), true);
  207. }
  208. }
  209. }
  210. // Despawn Npc's
  211. if(!_npclist.isEmpty())
  212. {
  213. for (L2Npc _npc : _npclist)
  214. if (_npc != null)
  215. _npc.deleteMe();
  216. }
  217. _npclist.clear();
  218. _players.clear();
  219. // Announce event end
  220. Announcements.getInstance().announceToAll("* Race Event finished *");
  221. return true;
  222. }
  223. @Override
  224. public boolean eventBypass(L2PcInstance activeChar, String bypass)
  225. {
  226. if (bypass.startsWith("skill"))
  227. {
  228. if (_isRaceStarted)
  229. {
  230. activeChar.sendMessage("Race already started, you cannot change transform skill now");
  231. }
  232. else
  233. {
  234. int _number = Integer.valueOf(bypass.substring(5));
  235. L2Skill _sk = SkillTable.getInstance().getInfo(_number, 1);
  236. if(_sk != null)
  237. {
  238. _skill = _number;
  239. activeChar.sendMessage("Transform skill set to:");
  240. activeChar.sendMessage(_sk.getName());
  241. }
  242. else
  243. {
  244. activeChar.sendMessage("Error while changing transform skill");
  245. }
  246. }
  247. }
  248. else if (bypass.startsWith("tele"))
  249. {
  250. if(Integer.valueOf(bypass.substring(4)) > 0 && _randspawn != null)
  251. activeChar.teleToLocation(_randspawn[0], _randspawn[1], _randspawn[2]);
  252. else
  253. activeChar.teleToLocation(18429, 145861, -3090);
  254. }
  255. showMenu(activeChar);
  256. return true;
  257. }
  258. @Override
  259. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  260. {
  261. String htmltext = event;
  262. QuestState st = player.getQuestState(getName());
  263. if (st == null)
  264. return null;
  265. if (event.equalsIgnoreCase("transform"))
  266. {
  267. transformPlayer(player);
  268. return null;
  269. }
  270. else if (event.equalsIgnoreCase("untransform"))
  271. {
  272. player.untransform();
  273. return null;
  274. }
  275. else if (event.equalsIgnoreCase("showfinish"))
  276. {
  277. player.getRadar().addMarker(_randspawn[0], _randspawn[1], _randspawn[2]);
  278. return null;
  279. }
  280. else if (event.equalsIgnoreCase("signup"))
  281. {
  282. if (_players.contains(player))
  283. return "900103-onlist.htm";
  284. _players.add(player);
  285. return "900103-signup.htm";
  286. }
  287. else if (event.equalsIgnoreCase("quit"))
  288. {
  289. player.untransform();
  290. if (_players.contains(player))
  291. _players.remove(player);
  292. return "900103-quit.htm";
  293. }
  294. else if (event.equalsIgnoreCase("finish"))
  295. {
  296. if (player.getFirstEffect(_skill) != null)
  297. {
  298. winRace(player);
  299. return "900104-winner.htm";
  300. }
  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. {
  318. return _start_npc+"-started-"+isRacing(player)+".htm";
  319. }
  320. return _start_npc+"-"+isRacing(player)+".htm";
  321. }
  322. else if (npc.getNpcId() == _stop_npc && _isRaceStarted)
  323. {
  324. return _stop_npc+"-"+isRacing(player)+".htm";
  325. }
  326. return npc.getNpcId()+".htm";
  327. }
  328. private int isRacing(L2PcInstance player)
  329. {
  330. if (_players.isEmpty())
  331. return 0;
  332. if (_players.contains(player))
  333. return 1;
  334. return 0;
  335. }
  336. private L2Npc recordSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
  337. {
  338. L2Npc _tmp = addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay);
  339. if(_tmp != null)
  340. _npclist.add(_tmp);
  341. return _tmp;
  342. }
  343. private void transformPlayer(L2PcInstance player)
  344. {
  345. if (player.isTransformed() || player.isInStance())
  346. player.untransform();
  347. if (player.isSitting())
  348. player.standUp();
  349. for (L2Effect e : player.getAllEffects())
  350. {
  351. if (e.getAbnormalType().equalsIgnoreCase("speed_up"))
  352. e.exit();
  353. if (e.getSkill() != null && (
  354. e.getSkill().getId() == 268 || // Song of Wind
  355. e.getSkill().getId() == 298)) // Rabbit Spirit Totem
  356. e.exit();
  357. }
  358. SkillTable.getInstance().getInfo(_skill, 1).getEffects(player, player);
  359. }
  360. private void sendMessage(L2PcInstance player, String text)
  361. {
  362. player.sendPacket(new CreatureSay(_npc.getObjectId(), 20, _npc.getName(), text));
  363. }
  364. private void showMenu(L2PcInstance activeChar)
  365. {
  366. NpcHtmlMessage html = new NpcHtmlMessage(0);
  367. String content = getHtm(activeChar.getHtmlPrefix(), "admin_menu.htm");
  368. html.setHtml(content);
  369. activeChar.sendPacket(html);
  370. }
  371. private void timeUp()
  372. {
  373. Announcements.getInstance().announceToAll("Time up, nobody wins!");
  374. eventStop();
  375. }
  376. private void winRace(L2PcInstance player)
  377. {
  378. int[] _reward = _rewards[Rnd.get(_rewards.length-1)];
  379. player.addItem("eventModRace", _reward[0], _reward[1], _npc, true);
  380. Announcements.getInstance().announceToAll(player.getName()+" is a winner!");
  381. eventStop();
  382. }
  383. }