AirShipController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 gracia.vehicles;
  20. import java.util.concurrent.Future;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.instancemanager.AirShipManager;
  25. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  26. import com.l2jserver.gameserver.model.ClanPrivilege;
  27. import com.l2jserver.gameserver.model.Location;
  28. import com.l2jserver.gameserver.model.VehiclePathPoint;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Npc;
  31. import com.l2jserver.gameserver.model.actor.instance.L2AirShipInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2ControllableAirShipInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.quest.Quest;
  35. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  36. import com.l2jserver.gameserver.model.zone.type.L2ScriptZone;
  37. import com.l2jserver.gameserver.network.NpcStringId;
  38. import com.l2jserver.gameserver.network.SystemMessageId;
  39. import com.l2jserver.gameserver.network.clientpackets.Say2;
  40. import com.l2jserver.gameserver.network.serverpackets.NpcSay;
  41. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  42. public abstract class AirShipController extends Quest
  43. {
  44. protected final class DecayTask implements Runnable
  45. {
  46. @Override
  47. public void run()
  48. {
  49. if (_dockedShip != null)
  50. {
  51. _dockedShip.deleteMe();
  52. }
  53. }
  54. }
  55. protected final class DepartTask implements Runnable
  56. {
  57. @Override
  58. public void run()
  59. {
  60. if ((_dockedShip != null) && _dockedShip.isInDock() && !_dockedShip.isMoving())
  61. {
  62. if (_departPath != null)
  63. {
  64. _dockedShip.executePath(_departPath);
  65. }
  66. else
  67. {
  68. _dockedShip.deleteMe();
  69. }
  70. }
  71. }
  72. }
  73. public static final Logger _log = Logger.getLogger(AirShipController.class.getName());
  74. protected int _dockZone = 0;
  75. protected int _shipSpawnX = 0;
  76. protected int _shipSpawnY = 0;
  77. protected int _shipSpawnZ = 0;
  78. protected int _shipHeading = 0;
  79. protected Location _oustLoc = null;
  80. protected int _locationId = 0;
  81. protected VehiclePathPoint[] _arrivalPath = null;
  82. protected VehiclePathPoint[] _departPath = null;
  83. protected VehiclePathPoint[][] _teleportsTable = null;
  84. protected int[] _fuelTable = null;
  85. protected int _movieId = 0;
  86. protected boolean _isBusy = false;
  87. protected L2ControllableAirShipInstance _dockedShip = null;
  88. private final Runnable _decayTask = new DecayTask();
  89. private final Runnable _departTask = new DepartTask();
  90. private Future<?> _departSchedule = null;
  91. private NpcSay _arrivalMessage = null;
  92. private static final int DEPART_INTERVAL = 300000; // 5 min
  93. private static final int LICENSE = 13559;
  94. private static final int STARSTONE = 13277;
  95. private static final int SUMMON_COST = 5;
  96. private static final SystemMessage SM_NEED_MORE = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_NEED_MORE_S1).addItemName(STARSTONE);
  97. public AirShipController(int questId, String name, String descr)
  98. {
  99. super(questId, name, descr);
  100. }
  101. @Override
  102. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  103. {
  104. if ("summon".equalsIgnoreCase(event))
  105. {
  106. if (_dockedShip != null)
  107. {
  108. if (_dockedShip.isOwner(player))
  109. {
  110. player.sendPacket(SystemMessageId.THE_AIRSHIP_IS_ALREADY_EXISTS);
  111. }
  112. return null;
  113. }
  114. if (_isBusy)
  115. {
  116. player.sendPacket(SystemMessageId.ANOTHER_AIRSHIP_ALREADY_SUMMONED);
  117. return null;
  118. }
  119. if (!player.hasClanPrivilege(ClanPrivilege.CL_SUMMON_AIRSHIP))
  120. {
  121. player.sendPacket(SystemMessageId.THE_AIRSHIP_NO_PRIVILEGES);
  122. return null;
  123. }
  124. int ownerId = player.getClanId();
  125. if (!AirShipManager.getInstance().hasAirShipLicense(ownerId))
  126. {
  127. player.sendPacket(SystemMessageId.THE_AIRSHIP_NEED_LICENSE_TO_SUMMON);
  128. return null;
  129. }
  130. if (AirShipManager.getInstance().hasAirShip(ownerId))
  131. {
  132. player.sendPacket(SystemMessageId.THE_AIRSHIP_ALREADY_USED);
  133. return null;
  134. }
  135. if (!player.destroyItemByItemId("AirShipSummon", STARSTONE, SUMMON_COST, npc, true))
  136. {
  137. player.sendPacket(SM_NEED_MORE);
  138. return null;
  139. }
  140. _isBusy = true;
  141. final L2AirShipInstance ship = AirShipManager.getInstance().getNewAirShip(_shipSpawnX, _shipSpawnY, _shipSpawnZ, _shipHeading, ownerId);
  142. if (ship != null)
  143. {
  144. if (_arrivalPath != null)
  145. {
  146. ship.executePath(_arrivalPath);
  147. }
  148. if (_arrivalMessage == null)
  149. {
  150. _arrivalMessage = new NpcSay(npc.getObjectId(), Say2.NPC_SHOUT, npc.getId(), NpcStringId.THE_AIRSHIP_HAS_BEEN_SUMMONED_IT_WILL_AUTOMATICALLY_DEPART_IN_5_MINUTES);
  151. }
  152. npc.broadcastPacket(_arrivalMessage);
  153. }
  154. else
  155. {
  156. _isBusy = false;
  157. }
  158. return null;
  159. }
  160. else if ("board".equalsIgnoreCase(event))
  161. {
  162. if (player.isTransformed())
  163. {
  164. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_TRANSFORMED);
  165. return null;
  166. }
  167. else if (player.isParalyzed())
  168. {
  169. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_PETRIFIED);
  170. return null;
  171. }
  172. else if (player.isDead() || player.isFakeDeath())
  173. {
  174. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_DEAD);
  175. return null;
  176. }
  177. else if (player.isFishing())
  178. {
  179. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_FISHING);
  180. return null;
  181. }
  182. else if (player.isInCombat())
  183. {
  184. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_IN_BATTLE);
  185. return null;
  186. }
  187. else if (player.isInDuel())
  188. {
  189. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_IN_A_DUEL);
  190. return null;
  191. }
  192. else if (player.isSitting())
  193. {
  194. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_SITTING);
  195. return null;
  196. }
  197. else if (player.isCastingNow())
  198. {
  199. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_CASTING);
  200. return null;
  201. }
  202. else if (player.isCursedWeaponEquipped())
  203. {
  204. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_A_CURSED_WEAPON_IS_EQUIPPED);
  205. return null;
  206. }
  207. else if (player.isCombatFlagEquipped())
  208. {
  209. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_HOLDING_A_FLAG);
  210. return null;
  211. }
  212. else if (player.hasSummon() || player.isMounted())
  213. {
  214. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_A_PET_OR_A_SERVITOR_IS_SUMMONED);
  215. return null;
  216. }
  217. else if (player.isFlyingMounted())
  218. {
  219. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_NOT_MEET_REQUEIREMENTS);
  220. return null;
  221. }
  222. if (_dockedShip != null)
  223. {
  224. _dockedShip.addPassenger(player);
  225. }
  226. return null;
  227. }
  228. else if ("register".equalsIgnoreCase(event))
  229. {
  230. if ((player.getClan() == null) || (player.getClan().getLevel() < 5))
  231. {
  232. player.sendPacket(SystemMessageId.THE_AIRSHIP_NEED_CLANLVL_5_TO_SUMMON);
  233. return null;
  234. }
  235. if (!player.isClanLeader())
  236. {
  237. player.sendPacket(SystemMessageId.THE_AIRSHIP_NO_PRIVILEGES);
  238. return null;
  239. }
  240. final int ownerId = player.getClanId();
  241. if (AirShipManager.getInstance().hasAirShipLicense(ownerId))
  242. {
  243. player.sendPacket(SystemMessageId.THE_AIRSHIP_SUMMON_LICENSE_ALREADY_ACQUIRED);
  244. return null;
  245. }
  246. if (!player.destroyItemByItemId("AirShipLicense", LICENSE, 1, npc, true))
  247. {
  248. player.sendPacket(SM_NEED_MORE);
  249. return null;
  250. }
  251. AirShipManager.getInstance().registerLicense(ownerId);
  252. player.sendPacket(SystemMessageId.THE_AIRSHIP_SUMMON_LICENSE_ENTERED);
  253. return null;
  254. }
  255. else
  256. {
  257. return event;
  258. }
  259. }
  260. @Override
  261. public String onEnterZone(L2Character character, L2ZoneType zone)
  262. {
  263. if (character instanceof L2ControllableAirShipInstance)
  264. {
  265. if (_dockedShip == null)
  266. {
  267. _dockedShip = (L2ControllableAirShipInstance) character;
  268. _dockedShip.setInDock(_dockZone);
  269. _dockedShip.setOustLoc(_oustLoc);
  270. // Ship is not empty - display movie to passengers and dock
  271. if (!_dockedShip.isEmpty())
  272. {
  273. if (_movieId != 0)
  274. {
  275. for (L2PcInstance passenger : _dockedShip.getPassengers())
  276. {
  277. if (passenger != null)
  278. {
  279. passenger.showQuestMovie(_movieId);
  280. }
  281. }
  282. }
  283. ThreadPoolManager.getInstance().scheduleGeneral(_decayTask, 1000);
  284. }
  285. else
  286. {
  287. _departSchedule = ThreadPoolManager.getInstance().scheduleGeneral(_departTask, DEPART_INTERVAL);
  288. }
  289. }
  290. }
  291. return null;
  292. }
  293. @Override
  294. public String onExitZone(L2Character character, L2ZoneType zone)
  295. {
  296. if (character instanceof L2ControllableAirShipInstance)
  297. {
  298. if (character.equals(_dockedShip))
  299. {
  300. if (_departSchedule != null)
  301. {
  302. _departSchedule.cancel(false);
  303. _departSchedule = null;
  304. }
  305. _dockedShip.setInDock(0);
  306. _dockedShip = null;
  307. _isBusy = false;
  308. }
  309. }
  310. return null;
  311. }
  312. @Override
  313. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  314. {
  315. return npc.getId() + ".htm";
  316. }
  317. protected void validityCheck()
  318. {
  319. L2ScriptZone zone = ZoneManager.getInstance().getZoneById(_dockZone, L2ScriptZone.class);
  320. if (zone == null)
  321. {
  322. _log.log(Level.WARNING, getName() + ": Invalid zone " + _dockZone + ", controller disabled");
  323. _isBusy = true;
  324. return;
  325. }
  326. VehiclePathPoint p;
  327. if (_arrivalPath != null)
  328. {
  329. if (_arrivalPath.length == 0)
  330. {
  331. _log.log(Level.WARNING, getName() + ": Zero arrival path length.");
  332. _arrivalPath = null;
  333. }
  334. else
  335. {
  336. p = _arrivalPath[_arrivalPath.length - 1];
  337. if (!zone.isInsideZone(p.getLocation()))
  338. {
  339. _log.log(Level.WARNING, getName() + ": Arrival path finish point (" + p.getX() + "," + p.getY() + "," + p.getZ() + ") not in zone " + _dockZone);
  340. _arrivalPath = null;
  341. }
  342. }
  343. }
  344. if (_arrivalPath == null)
  345. {
  346. if (!ZoneManager.getInstance().getZoneById(_dockZone, L2ScriptZone.class).isInsideZone(_shipSpawnX, _shipSpawnY, _shipSpawnZ))
  347. {
  348. _log.log(Level.WARNING, getName() + ": Arrival path is null and spawn point not in zone " + _dockZone + ", controller disabled");
  349. _isBusy = true;
  350. return;
  351. }
  352. }
  353. if (_departPath != null)
  354. {
  355. if (_departPath.length == 0)
  356. {
  357. _log.log(Level.WARNING, getName() + ": Zero depart path length.");
  358. _departPath = null;
  359. }
  360. else
  361. {
  362. p = _departPath[_departPath.length - 1];
  363. if (zone.isInsideZone(p.getLocation()))
  364. {
  365. _log.log(Level.WARNING, getName() + ": Departure path finish point (" + p.getX() + "," + p.getY() + "," + p.getZ() + ") in zone " + _dockZone);
  366. _departPath = null;
  367. }
  368. }
  369. }
  370. if (_teleportsTable != null)
  371. {
  372. if (_fuelTable == null)
  373. {
  374. _log.log(Level.WARNING, getName() + ": Fuel consumption not defined.");
  375. }
  376. else
  377. {
  378. if (_teleportsTable.length != _fuelTable.length)
  379. {
  380. _log.log(Level.WARNING, getName() + ": Fuel consumption not match teleport list.");
  381. }
  382. else
  383. {
  384. AirShipManager.getInstance().registerAirShipTeleportList(_dockZone, _locationId, _teleportsTable, _fuelTable);
  385. }
  386. }
  387. }
  388. }
  389. }