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