AirShipController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (C) 2004-2013 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 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.L2Clan;
  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_ALREADY_EXISTS = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_IS_ALREADY_EXISTS);
  97. private static final SystemMessage SM_ALREADY_SUMMONED = SystemMessage.getSystemMessage(SystemMessageId.ANOTHER_AIRSHIP_ALREADY_SUMMONED);
  98. private static final SystemMessage SM_NEED_LICENSE = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_NEED_LICENSE_TO_SUMMON);
  99. private static final SystemMessage SM_NEED_CLANLVL5 = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_NEED_CLANLVL_5_TO_SUMMON);
  100. private static final SystemMessage SM_NO_PRIVS = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_NO_PRIVILEGES);
  101. private static final SystemMessage SM_ALREADY_USED = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_ALREADY_USED);
  102. private static final SystemMessage SM_LICENSE_ALREADY_ACQUIRED = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_SUMMON_LICENSE_ALREADY_ACQUIRED);
  103. private static final SystemMessage SM_LICENSE_ENTERED = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_SUMMON_LICENSE_ENTERED);
  104. private static final SystemMessage SM_NEED_MORE = SystemMessage.getSystemMessage(SystemMessageId.THE_AIRSHIP_NEED_MORE_S1).addItemName(STARSTONE);
  105. public AirShipController(int questId, String name, String descr)
  106. {
  107. super(questId, name, descr);
  108. }
  109. @Override
  110. public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  111. {
  112. if ("summon".equalsIgnoreCase(event))
  113. {
  114. if (_dockedShip != null)
  115. {
  116. if (_dockedShip.isOwner(player))
  117. {
  118. player.sendPacket(SM_ALREADY_EXISTS);
  119. }
  120. return null;
  121. }
  122. if (_isBusy)
  123. {
  124. player.sendPacket(SM_ALREADY_SUMMONED);
  125. return null;
  126. }
  127. if ((player.getClanPrivileges() & L2Clan.CP_CL_SUMMON_AIRSHIP) != L2Clan.CP_CL_SUMMON_AIRSHIP)
  128. {
  129. player.sendPacket(SM_NO_PRIVS);
  130. return null;
  131. }
  132. int ownerId = player.getClanId();
  133. if (!AirShipManager.getInstance().hasAirShipLicense(ownerId))
  134. {
  135. player.sendPacket(SM_NEED_LICENSE);
  136. return null;
  137. }
  138. if (AirShipManager.getInstance().hasAirShip(ownerId))
  139. {
  140. player.sendPacket(SM_ALREADY_USED);
  141. return null;
  142. }
  143. if (!player.destroyItemByItemId("AirShipSummon", STARSTONE, SUMMON_COST, npc, true))
  144. {
  145. player.sendPacket(SM_NEED_MORE);
  146. return null;
  147. }
  148. _isBusy = true;
  149. final L2AirShipInstance ship = AirShipManager.getInstance().getNewAirShip(_shipSpawnX, _shipSpawnY, _shipSpawnZ, _shipHeading, ownerId);
  150. if (ship != null)
  151. {
  152. if (_arrivalPath != null)
  153. {
  154. ship.executePath(_arrivalPath);
  155. }
  156. if (_arrivalMessage == null)
  157. {
  158. _arrivalMessage = new NpcSay(npc.getObjectId(), Say2.NPC_SHOUT, npc.getNpcId(), NpcStringId.THE_AIRSHIP_HAS_BEEN_SUMMONED_IT_WILL_AUTOMATICALLY_DEPART_IN_5_MINUTES);
  159. }
  160. npc.broadcastPacket(_arrivalMessage);
  161. }
  162. else
  163. {
  164. _isBusy = false;
  165. }
  166. return null;
  167. }
  168. else if ("board".equalsIgnoreCase(event))
  169. {
  170. if (player.isTransformed())
  171. {
  172. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_TRANSFORMED);
  173. return null;
  174. }
  175. else if (player.isParalyzed())
  176. {
  177. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_PETRIFIED);
  178. return null;
  179. }
  180. else if (player.isDead() || player.isFakeDeath())
  181. {
  182. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_DEAD);
  183. return null;
  184. }
  185. else if (player.isFishing())
  186. {
  187. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_FISHING);
  188. return null;
  189. }
  190. else if (player.isInCombat())
  191. {
  192. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_IN_BATTLE);
  193. return null;
  194. }
  195. else if (player.isInDuel())
  196. {
  197. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_IN_A_DUEL);
  198. return null;
  199. }
  200. else if (player.isSitting())
  201. {
  202. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_SITTING);
  203. return null;
  204. }
  205. else if (player.isCastingNow())
  206. {
  207. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_CASTING);
  208. return null;
  209. }
  210. else if (player.isCursedWeaponEquipped())
  211. {
  212. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_A_CURSED_WEAPON_IS_EQUIPPED);
  213. return null;
  214. }
  215. else if (player.isCombatFlagEquipped())
  216. {
  217. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_HOLDING_A_FLAG);
  218. return null;
  219. }
  220. else if (player.hasSummon() || player.isMounted())
  221. {
  222. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_AN_AIRSHIP_WHILE_A_PET_OR_A_SERVITOR_IS_SUMMONED);
  223. return null;
  224. }
  225. else if (player.isFlyingMounted())
  226. {
  227. player.sendPacket(SystemMessageId.YOU_CANNOT_BOARD_NOT_MEET_REQUEIREMENTS);
  228. return null;
  229. }
  230. if (_dockedShip != null)
  231. {
  232. _dockedShip.addPassenger(player);
  233. }
  234. return null;
  235. }
  236. else if ("register".equalsIgnoreCase(event))
  237. {
  238. if ((player.getClan() == null) || (player.getClan().getLevel() < 5))
  239. {
  240. player.sendPacket(SM_NEED_CLANLVL5);
  241. return null;
  242. }
  243. if (!player.isClanLeader())
  244. {
  245. player.sendPacket(SM_NO_PRIVS);
  246. return null;
  247. }
  248. final int ownerId = player.getClanId();
  249. if (AirShipManager.getInstance().hasAirShipLicense(ownerId))
  250. {
  251. player.sendPacket(SM_LICENSE_ALREADY_ACQUIRED);
  252. return null;
  253. }
  254. if (!player.destroyItemByItemId("AirShipLicense", LICENSE, 1, npc, true))
  255. {
  256. player.sendPacket(SM_NEED_MORE);
  257. return null;
  258. }
  259. AirShipManager.getInstance().registerLicense(ownerId);
  260. player.sendPacket(SM_LICENSE_ENTERED);
  261. return null;
  262. }
  263. else
  264. {
  265. return event;
  266. }
  267. }
  268. @Override
  269. public String onEnterZone(L2Character character, L2ZoneType zone)
  270. {
  271. if (character instanceof L2ControllableAirShipInstance)
  272. {
  273. if (_dockedShip == null)
  274. {
  275. _dockedShip = (L2ControllableAirShipInstance) character;
  276. _dockedShip.setInDock(_dockZone);
  277. _dockedShip.setOustLoc(_oustLoc);
  278. // Ship is not empty - display movie to passengers and dock
  279. if (!_dockedShip.isEmpty())
  280. {
  281. if (_movieId != 0)
  282. {
  283. for (L2PcInstance passenger : _dockedShip.getPassengers())
  284. {
  285. if (passenger != null)
  286. {
  287. passenger.showQuestMovie(_movieId);
  288. }
  289. }
  290. }
  291. ThreadPoolManager.getInstance().scheduleGeneral(_decayTask, 1000);
  292. }
  293. else
  294. {
  295. _departSchedule = ThreadPoolManager.getInstance().scheduleGeneral(_departTask, DEPART_INTERVAL);
  296. }
  297. }
  298. }
  299. return null;
  300. }
  301. @Override
  302. public String onExitZone(L2Character character, L2ZoneType zone)
  303. {
  304. if (character instanceof L2ControllableAirShipInstance)
  305. {
  306. if (character.equals(_dockedShip))
  307. {
  308. if (_departSchedule != null)
  309. {
  310. _departSchedule.cancel(false);
  311. _departSchedule = null;
  312. }
  313. _dockedShip.setInDock(0);
  314. _dockedShip = null;
  315. _isBusy = false;
  316. }
  317. }
  318. return null;
  319. }
  320. @Override
  321. public String onFirstTalk(L2Npc npc, L2PcInstance player)
  322. {
  323. return npc.getNpcId() + ".htm";
  324. }
  325. protected void validityCheck()
  326. {
  327. L2ScriptZone zone = ZoneManager.getInstance().getZoneById(_dockZone, L2ScriptZone.class);
  328. if (zone == null)
  329. {
  330. _log.log(Level.WARNING, getName() + ": Invalid zone " + _dockZone + ", controller disabled");
  331. _isBusy = true;
  332. return;
  333. }
  334. VehiclePathPoint p;
  335. if (_arrivalPath != null)
  336. {
  337. if (_arrivalPath.length == 0)
  338. {
  339. _log.log(Level.WARNING, getName() + ": Zero arrival path length.");
  340. _arrivalPath = null;
  341. }
  342. else
  343. {
  344. p = _arrivalPath[_arrivalPath.length - 1];
  345. if (!zone.isInsideZone(p.x, p.y, p.z))
  346. {
  347. _log.log(Level.WARNING, getName() + ": Arrival path finish point (" + p.x + "," + p.y + "," + p.z + ") not in zone " + _dockZone);
  348. _arrivalPath = null;
  349. }
  350. }
  351. }
  352. if (_arrivalPath == null)
  353. {
  354. if (!ZoneManager.getInstance().getZoneById(_dockZone, L2ScriptZone.class).isInsideZone(_shipSpawnX, _shipSpawnY, _shipSpawnZ))
  355. {
  356. _log.log(Level.WARNING, getName() + ": Arrival path is null and spawn point not in zone " + _dockZone + ", controller disabled");
  357. _isBusy = true;
  358. return;
  359. }
  360. }
  361. if (_departPath != null)
  362. {
  363. if (_departPath.length == 0)
  364. {
  365. _log.log(Level.WARNING, getName() + ": Zero depart path length.");
  366. _departPath = null;
  367. }
  368. else
  369. {
  370. p = _departPath[_departPath.length - 1];
  371. if (zone.isInsideZone(p.x, p.y, p.z))
  372. {
  373. _log.log(Level.WARNING, getName() + ": Departure path finish point (" + p.x + "," + p.y + "," + p.z + ") in zone " + _dockZone);
  374. _departPath = null;
  375. }
  376. }
  377. }
  378. if (_teleportsTable != null)
  379. {
  380. if (_fuelTable == null)
  381. {
  382. _log.log(Level.WARNING, getName() + ": Fuel consumption not defined.");
  383. }
  384. else
  385. {
  386. if (_teleportsTable.length != _fuelTable.length)
  387. {
  388. _log.log(Level.WARNING, getName() + ": Fuel consumption not match teleport list.");
  389. }
  390. else
  391. {
  392. AirShipManager.getInstance().registerAirShipTeleportList(_dockZone, _locationId, _teleportsTable, _fuelTable);
  393. }
  394. }
  395. }
  396. }
  397. }