L2Vehicle.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 com.l2jserver.gameserver.model.actor;
  16. import java.util.Collection;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.logging.Level;
  20. import javolution.util.FastList;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.GameTimeController;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.ai.CtrlIntention;
  25. import com.l2jserver.gameserver.ai.L2CharacterAI;
  26. import com.l2jserver.gameserver.datatables.MapRegionTable;
  27. import com.l2jserver.gameserver.model.L2CharPosition;
  28. import com.l2jserver.gameserver.model.L2ItemInstance;
  29. import com.l2jserver.gameserver.model.L2World;
  30. import com.l2jserver.gameserver.model.L2WorldRegion;
  31. import com.l2jserver.gameserver.model.Location;
  32. import com.l2jserver.gameserver.model.VehiclePathPoint;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.actor.knownlist.VehicleKnownList;
  35. import com.l2jserver.gameserver.model.actor.stat.VehicleStat;
  36. import com.l2jserver.gameserver.network.SystemMessageId;
  37. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  38. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  39. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  40. import com.l2jserver.gameserver.templates.chars.L2CharTemplate;
  41. import com.l2jserver.gameserver.templates.item.L2Weapon;
  42. import com.l2jserver.gameserver.util.Util;
  43. /**
  44. *
  45. * @author DS
  46. *
  47. */
  48. public abstract class L2Vehicle extends L2Character
  49. {
  50. protected int _dockId = 0;
  51. protected final FastList<L2PcInstance> _passengers = new FastList<L2PcInstance>();
  52. protected Location _oustLoc = null;
  53. private Runnable _engine = null;
  54. protected VehiclePathPoint[] _currentPath = null;
  55. protected int _runState = 0;
  56. public L2Vehicle(int objectId, L2CharTemplate template)
  57. {
  58. super(objectId, template);
  59. setInstanceType(InstanceType.L2Vehicle);
  60. setIsFlying(true);
  61. }
  62. public boolean isBoat()
  63. {
  64. return false;
  65. }
  66. public boolean isAirShip()
  67. {
  68. return false;
  69. }
  70. public boolean canBeControlled()
  71. {
  72. return _engine == null;
  73. }
  74. public void registerEngine(Runnable r)
  75. {
  76. _engine = r;
  77. }
  78. public void runEngine(int delay)
  79. {
  80. if (_engine != null)
  81. ThreadPoolManager.getInstance().scheduleGeneral(_engine, delay);
  82. }
  83. public void executePath(VehiclePathPoint[] path)
  84. {
  85. _runState = 0;
  86. _currentPath = path;
  87. if (_currentPath != null && _currentPath.length > 0)
  88. {
  89. final VehiclePathPoint point = _currentPath[0];
  90. if (point.moveSpeed > 0)
  91. getStat().setMoveSpeed(point.moveSpeed);
  92. if (point.rotationSpeed > 0)
  93. getStat().setRotationSpeed(point.rotationSpeed);
  94. getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(point.x, point.y, point.z, 0));
  95. return;
  96. }
  97. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  98. }
  99. @Override
  100. public boolean moveToNextRoutePoint()
  101. {
  102. _move = null;
  103. if (_currentPath != null)
  104. {
  105. _runState++;
  106. if (_runState < _currentPath.length)
  107. {
  108. final VehiclePathPoint point = _currentPath[_runState];
  109. if (!isMovementDisabled())
  110. {
  111. if (point.moveSpeed == 0)
  112. {
  113. teleToLocation(point.x, point.y, point.z, point.rotationSpeed, false);
  114. _currentPath = null;
  115. }
  116. else
  117. {
  118. if (point.moveSpeed > 0)
  119. getStat().setMoveSpeed(point.moveSpeed);
  120. if (point.rotationSpeed > 0)
  121. getStat().setRotationSpeed(point.rotationSpeed);
  122. MoveData m = new MoveData();
  123. m.disregardingGeodata = false;
  124. m.onGeodataPathIndex = -1;
  125. m._xDestination = point.x;
  126. m._yDestination = point.y;
  127. m._zDestination = point.z;
  128. m._heading = 0;
  129. final double dx = point.x - getX();
  130. final double dy = point.y - getY();
  131. final double distance = Math.sqrt(dx*dx + dy*dy);
  132. if (distance > 1) // vertical movement heading check
  133. setHeading(Util.calculateHeadingFrom(getX(), getY(), point.x, point.y));
  134. m._moveStartTime = GameTimeController.getGameTicks();
  135. _move = m;
  136. GameTimeController.getInstance().registerMovingObject(this);
  137. return true;
  138. }
  139. }
  140. }
  141. else
  142. _currentPath = null;
  143. }
  144. runEngine(10);
  145. return false;
  146. }
  147. @Override
  148. public void initKnownList()
  149. {
  150. setKnownList(new VehicleKnownList(this));
  151. }
  152. @Override
  153. public VehicleStat getStat()
  154. {
  155. return (VehicleStat)super.getStat();
  156. }
  157. @Override
  158. public void initCharStat()
  159. {
  160. setStat(new VehicleStat(this));
  161. }
  162. public boolean isInDock()
  163. {
  164. return _dockId > 0;
  165. }
  166. public int getDockId()
  167. {
  168. return _dockId;
  169. }
  170. public void setInDock(int d)
  171. {
  172. _dockId = d;
  173. }
  174. public void setOustLoc(Location loc)
  175. {
  176. _oustLoc = loc;
  177. }
  178. public Location getOustLoc()
  179. {
  180. return _oustLoc != null ? _oustLoc : MapRegionTable.getInstance().getTeleToLocation(this, MapRegionTable.TeleportWhereType.Town);
  181. }
  182. public void oustPlayers()
  183. {
  184. L2PcInstance player;
  185. // Use iterator because oustPlayer will try to remove player from _passengers
  186. final Iterator<L2PcInstance> iter = _passengers.iterator();
  187. while (iter.hasNext())
  188. {
  189. player = iter.next();
  190. iter.remove();
  191. if (player != null)
  192. oustPlayer(player);
  193. }
  194. }
  195. public void oustPlayer(L2PcInstance player)
  196. {
  197. player.setVehicle(null);
  198. player.setInVehiclePosition(null);
  199. removePassenger(player);
  200. }
  201. public boolean addPassenger(L2PcInstance player)
  202. {
  203. if (player == null || _passengers.contains(player))
  204. return false;
  205. // already in other vehicle
  206. if (player.getVehicle() != null && player.getVehicle() != this)
  207. return false;
  208. _passengers.add(player);
  209. return true;
  210. }
  211. public void removePassenger(L2PcInstance player)
  212. {
  213. try
  214. {
  215. _passengers.remove(player);
  216. }
  217. catch (Exception e)
  218. {}
  219. }
  220. public boolean isEmpty()
  221. {
  222. return _passengers.isEmpty();
  223. }
  224. public List<L2PcInstance> getPassengers()
  225. {
  226. return _passengers;
  227. }
  228. public void broadcastToPassengers(L2GameServerPacket sm)
  229. {
  230. for (L2PcInstance player : _passengers)
  231. {
  232. if (player != null)
  233. player.sendPacket(sm);
  234. }
  235. }
  236. /**
  237. * Consume ticket(s) and teleport player from boat if no correct ticket
  238. * @param itemId Ticket itemId
  239. * @param count Ticket count
  240. * @param oustX
  241. * @param oustY
  242. * @param oustZ
  243. */
  244. public void payForRide(int itemId, int count, int oustX, int oustY, int oustZ)
  245. {
  246. final Collection<L2PcInstance> passengers = getKnownList().getKnownPlayersInRadius(1000);
  247. if (passengers != null && !passengers.isEmpty())
  248. {
  249. L2ItemInstance ticket;
  250. InventoryUpdate iu;
  251. for (L2PcInstance player : passengers)
  252. {
  253. if (player == null)
  254. continue;
  255. if (player.isInBoat() && player.getBoat() == this)
  256. {
  257. if (itemId > 0)
  258. {
  259. ticket = player.getInventory().getItemByItemId(itemId);
  260. if (ticket == null || player.getInventory().destroyItem("Boat", ticket, count, player, this) == null)
  261. {
  262. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_CORRECT_BOAT_TICKET));
  263. player.teleToLocation(oustX, oustY, oustZ, true);
  264. continue;
  265. }
  266. iu = new InventoryUpdate();
  267. iu.addModifiedItem(ticket);
  268. player.sendPacket(iu);
  269. }
  270. addPassenger(player);
  271. }
  272. }
  273. }
  274. }
  275. @Override
  276. public boolean updatePosition(int gameTicks)
  277. {
  278. final boolean result = super.updatePosition(gameTicks);
  279. for (L2PcInstance player : _passengers)
  280. {
  281. if (player != null && player.getVehicle() == this)
  282. {
  283. player.getPosition().setXYZ(getX(), getY(), getZ());
  284. player.revalidateZone(false);
  285. }
  286. }
  287. return result;
  288. }
  289. @Override
  290. public void teleToLocation(int x, int y, int z, int heading, boolean allowRandomOffset)
  291. {
  292. if (isMoving())
  293. stopMove(null, false);
  294. setIsTeleporting(true);
  295. getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  296. for (L2PcInstance player : _passengers)
  297. {
  298. if (player != null)
  299. player.teleToLocation(x, y, z);
  300. }
  301. decayMe();
  302. setXYZ(x, y, z);
  303. // temporary fix for heading on teleports
  304. if (heading != 0)
  305. getPosition().setHeading(heading);
  306. onTeleported();
  307. revalidateZone(true);
  308. }
  309. @Override
  310. public void stopMove(L2CharPosition pos, boolean updateKnownObjects)
  311. {
  312. _move = null;
  313. if (pos != null)
  314. {
  315. setXYZ(pos.x, pos.y, pos.z);
  316. setHeading(pos.heading);
  317. revalidateZone(true);
  318. }
  319. if (Config.MOVE_BASED_KNOWNLIST && updateKnownObjects)
  320. this.getKnownList().findObjects();
  321. }
  322. @Override
  323. public void deleteMe()
  324. {
  325. _engine = null;
  326. try
  327. {
  328. if (isMoving())
  329. stopMove(null);
  330. }
  331. catch (Exception e)
  332. {
  333. _log.log(Level.SEVERE, "Failed stopMove().", e);
  334. }
  335. try
  336. {
  337. oustPlayers();
  338. }
  339. catch (Exception e)
  340. {
  341. _log.log(Level.SEVERE, "Failed oustPlayers().", e);
  342. }
  343. final L2WorldRegion oldRegion = getWorldRegion();
  344. try
  345. {
  346. decayMe();
  347. }
  348. catch (Exception e)
  349. {
  350. _log.log(Level.SEVERE, "Failed decayMe().", e);
  351. }
  352. if (oldRegion != null)
  353. oldRegion.removeFromZones(this);
  354. try
  355. {
  356. getKnownList().removeAllKnownObjects();
  357. }
  358. catch (Exception e)
  359. {
  360. _log.log(Level.SEVERE, "Failed cleaning knownlist.", e);
  361. }
  362. // Remove L2Object object from _allObjects of L2World
  363. L2World.getInstance().removeObject(this);
  364. super.deleteMe();
  365. }
  366. @Override
  367. public void updateAbnormalEffect()
  368. {
  369. }
  370. @Override
  371. public L2ItemInstance getActiveWeaponInstance()
  372. {
  373. return null;
  374. }
  375. @Override
  376. public L2Weapon getActiveWeaponItem()
  377. {
  378. return null;
  379. }
  380. @Override
  381. public L2ItemInstance getSecondaryWeaponInstance()
  382. {
  383. return null;
  384. }
  385. @Override
  386. public L2Weapon getSecondaryWeaponItem()
  387. {
  388. return null;
  389. }
  390. @Override
  391. public int getLevel()
  392. {
  393. return 0;
  394. }
  395. @Override
  396. public boolean isAutoAttackable(L2Character attacker)
  397. {
  398. return false;
  399. }
  400. @Override
  401. public void setAI(L2CharacterAI newAI)
  402. {
  403. if (_ai == null)
  404. _ai = newAI;
  405. }
  406. public class AIAccessor extends L2Character.AIAccessor
  407. {
  408. @Override
  409. public void detachAI()
  410. {}
  411. }
  412. }