L2Object.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model;
  20. import java.util.Map;
  21. import javolution.util.FastMap;
  22. import com.l2jserver.gameserver.enums.InstanceType;
  23. import com.l2jserver.gameserver.enums.ShotType;
  24. import com.l2jserver.gameserver.handler.ActionHandler;
  25. import com.l2jserver.gameserver.handler.ActionShiftHandler;
  26. import com.l2jserver.gameserver.handler.IActionHandler;
  27. import com.l2jserver.gameserver.idfactory.IdFactory;
  28. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.L2Npc;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.model.actor.knownlist.ObjectKnownList;
  33. import com.l2jserver.gameserver.model.actor.poly.ObjectPoly;
  34. import com.l2jserver.gameserver.model.entity.Instance;
  35. import com.l2jserver.gameserver.model.interfaces.IDecayable;
  36. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  37. import com.l2jserver.gameserver.model.interfaces.ILocational;
  38. import com.l2jserver.gameserver.model.interfaces.INamable;
  39. import com.l2jserver.gameserver.model.interfaces.ISpawnable;
  40. import com.l2jserver.gameserver.model.interfaces.IUniqueId;
  41. import com.l2jserver.gameserver.model.zone.ZoneId;
  42. import com.l2jserver.gameserver.network.SystemMessageId;
  43. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  44. import com.l2jserver.gameserver.network.serverpackets.ExSendUIEvent;
  45. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  46. import com.l2jserver.gameserver.util.Point3D;
  47. /**
  48. * Base class for all interactive objects.
  49. * @author unknown
  50. */
  51. public abstract class L2Object extends Point3D implements IIdentifiable, INamable, ISpawnable, IUniqueId, IDecayable
  52. {
  53. private boolean _isVisible;
  54. private ObjectKnownList _knownList;
  55. private String _name;
  56. private int _objectId;
  57. private L2WorldRegion _worldRegion;
  58. private InstanceType _instanceType = null;
  59. private volatile Map<String, Object> _scripts;
  60. public L2Object(int objectId)
  61. {
  62. super(0, 0, 0);
  63. setInstanceType(InstanceType.L2Object);
  64. _objectId = objectId;
  65. initKnownList();
  66. }
  67. protected final void setInstanceType(InstanceType i)
  68. {
  69. _instanceType = i;
  70. }
  71. public final InstanceType getInstanceType()
  72. {
  73. return _instanceType;
  74. }
  75. public final boolean isInstanceType(InstanceType i)
  76. {
  77. return _instanceType.isType(i);
  78. }
  79. public final boolean isInstanceTypes(InstanceType... i)
  80. {
  81. return _instanceType.isTypes(i);
  82. }
  83. public final void onAction(L2PcInstance player)
  84. {
  85. onAction(player, true);
  86. }
  87. public void onAction(L2PcInstance player, boolean interact)
  88. {
  89. IActionHandler handler = ActionHandler.getInstance().getHandler(getInstanceType());
  90. if (handler != null)
  91. {
  92. handler.action(player, this, interact);
  93. }
  94. player.sendPacket(ActionFailed.STATIC_PACKET);
  95. }
  96. public void onActionShift(L2PcInstance player)
  97. {
  98. IActionHandler handler = ActionShiftHandler.getInstance().getHandler(getInstanceType());
  99. if (handler != null)
  100. {
  101. handler.action(player, this, true);
  102. }
  103. player.sendPacket(ActionFailed.STATIC_PACKET);
  104. }
  105. public void onForcedAttack(L2PcInstance player)
  106. {
  107. player.sendPacket(ActionFailed.STATIC_PACKET);
  108. }
  109. public void onSpawn()
  110. {
  111. }
  112. /**
  113. * UnAfraid: TODO: Add listener here.
  114. * @param instanceId The id of the instance zone the object is in - id 0 is global
  115. */
  116. @Override
  117. public void setInstanceId(int instanceId)
  118. {
  119. if ((instanceId < 0) || (getInstanceId() == instanceId))
  120. {
  121. return;
  122. }
  123. Instance oldI = InstanceManager.getInstance().getInstance(getInstanceId());
  124. Instance newI = InstanceManager.getInstance().getInstance(instanceId);
  125. if (newI == null)
  126. {
  127. return;
  128. }
  129. if (isPlayer())
  130. {
  131. final L2PcInstance player = getActingPlayer();
  132. if ((getInstanceId() > 0) && (oldI != null))
  133. {
  134. oldI.removePlayer(getObjectId());
  135. if (oldI.isShowTimer())
  136. {
  137. sendInstanceUpdate(oldI, true);
  138. }
  139. }
  140. if (instanceId > 0)
  141. {
  142. newI.addPlayer(getObjectId());
  143. if (newI.isShowTimer())
  144. {
  145. sendInstanceUpdate(newI, false);
  146. }
  147. }
  148. if (player.hasSummon())
  149. {
  150. player.getSummon().setInstanceId(instanceId);
  151. }
  152. }
  153. else if (isNpc())
  154. {
  155. final L2Npc npc = (L2Npc) this;
  156. if ((getInstanceId() > 0) && (oldI != null))
  157. {
  158. oldI.removeNpc(npc);
  159. }
  160. if (instanceId > 0)
  161. {
  162. newI.addNpc(npc);
  163. }
  164. }
  165. super.setInstanceId(instanceId);
  166. if (_isVisible && (_knownList != null))
  167. {
  168. // We don't want some ugly looking disappear/appear effects, so don't update
  169. // the knownlist here, but players usually enter instancezones through teleporting
  170. // and the teleport will do the revalidation for us.
  171. if (!isPlayer())
  172. {
  173. decayMe();
  174. spawnMe();
  175. }
  176. }
  177. }
  178. private final void sendInstanceUpdate(Instance instance, boolean hide)
  179. {
  180. final int startTime = (int) ((System.currentTimeMillis() - instance.getInstanceStartTime()) / 1000);
  181. final int endTime = (int) ((instance.getInstanceEndTime() - instance.getInstanceStartTime()) / 1000);
  182. if (instance.isTimerIncrease())
  183. {
  184. sendPacket(new ExSendUIEvent(getActingPlayer(), hide, true, startTime, endTime, instance.getTimerText()));
  185. }
  186. else
  187. {
  188. sendPacket(new ExSendUIEvent(getActingPlayer(), hide, false, endTime - startTime, 0, instance.getTimerText()));
  189. }
  190. }
  191. @Override
  192. public boolean decayMe()
  193. {
  194. assert getWorldRegion() != null;
  195. L2WorldRegion reg = getWorldRegion();
  196. synchronized (this)
  197. {
  198. _isVisible = false;
  199. setWorldRegion(null);
  200. }
  201. // this can synchronize on others instances, so it's out of
  202. // synchronized, to avoid deadlocks
  203. // Remove the L2Object from the world
  204. L2World.getInstance().removeVisibleObject(this, reg);
  205. L2World.getInstance().removeObject(this);
  206. return true;
  207. }
  208. public void refreshID()
  209. {
  210. L2World.getInstance().removeObject(this);
  211. IdFactory.getInstance().releaseId(getObjectId());
  212. _objectId = IdFactory.getInstance().getNextId();
  213. }
  214. @Override
  215. public final boolean spawnMe()
  216. {
  217. assert (getWorldRegion() == null) && (getWorldPosition().getX() != 0) && (getWorldPosition().getY() != 0) && (getWorldPosition().getZ() != 0);
  218. synchronized (this)
  219. {
  220. // Set the x,y,z position of the L2Object spawn and update its _worldregion
  221. _isVisible = true;
  222. setWorldRegion(L2World.getInstance().getRegion(getWorldPosition()));
  223. // Add the L2Object spawn in the _allobjects of L2World
  224. L2World.getInstance().storeObject(this);
  225. // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  226. getWorldRegion().addVisibleObject(this);
  227. }
  228. // this can synchronize on others instances, so it's out of synchronized, to avoid deadlocks
  229. // Add the L2Object spawn in the world as a visible object
  230. L2World.getInstance().addVisibleObject(this, getWorldRegion());
  231. onSpawn();
  232. return true;
  233. }
  234. public final void spawnMe(int x, int y, int z)
  235. {
  236. assert getWorldRegion() == null;
  237. synchronized (this)
  238. {
  239. // Set the x,y,z position of the L2Object spawn and update its _worldregion
  240. _isVisible = true;
  241. if (x > L2World.MAP_MAX_X)
  242. {
  243. x = L2World.MAP_MAX_X - 5000;
  244. }
  245. if (x < L2World.MAP_MIN_X)
  246. {
  247. x = L2World.MAP_MIN_X + 5000;
  248. }
  249. if (y > L2World.MAP_MAX_Y)
  250. {
  251. y = L2World.MAP_MAX_Y - 5000;
  252. }
  253. if (y < L2World.MAP_MIN_Y)
  254. {
  255. y = L2World.MAP_MIN_Y + 5000;
  256. }
  257. setXYZ(x, y, z);
  258. setWorldRegion(L2World.getInstance().getRegion(getWorldPosition()));
  259. // Add the L2Object spawn in the _allobjects of L2World
  260. }
  261. L2World.getInstance().storeObject(this);
  262. // these can synchronize on others instances, so they're out of
  263. // synchronized, to avoid deadlocks
  264. // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  265. getWorldRegion().addVisibleObject(this);
  266. // Add the L2Object spawn in the world as a visible object
  267. L2World.getInstance().addVisibleObject(this, getWorldRegion());
  268. onSpawn();
  269. }
  270. public boolean isAttackable()
  271. {
  272. return false;
  273. }
  274. public abstract boolean isAutoAttackable(L2Character attacker);
  275. public final boolean isVisible()
  276. {
  277. return getWorldRegion() != null;
  278. }
  279. public final void setIsVisible(boolean value)
  280. {
  281. _isVisible = value;
  282. if (!_isVisible)
  283. {
  284. setWorldRegion(null);
  285. }
  286. }
  287. public void toggleVisible()
  288. {
  289. if (isVisible())
  290. {
  291. decayMe();
  292. }
  293. else
  294. {
  295. spawnMe();
  296. }
  297. }
  298. public ObjectKnownList getKnownList()
  299. {
  300. return _knownList;
  301. }
  302. public void initKnownList()
  303. {
  304. _knownList = new ObjectKnownList(this);
  305. }
  306. public final void setKnownList(ObjectKnownList value)
  307. {
  308. _knownList = value;
  309. }
  310. @Override
  311. public final String getName()
  312. {
  313. return _name;
  314. }
  315. public void setName(String value)
  316. {
  317. _name = value;
  318. }
  319. @Override
  320. public final int getObjectId()
  321. {
  322. return _objectId;
  323. }
  324. public final ObjectPoly getPoly()
  325. {
  326. final ObjectPoly poly = getScript(ObjectPoly.class);
  327. return (poly == null) ? addScript(new ObjectPoly(this)) : poly;
  328. }
  329. public abstract void sendInfo(L2PcInstance activeChar);
  330. public void sendPacket(L2GameServerPacket mov)
  331. {
  332. }
  333. public void sendPacket(SystemMessageId id)
  334. {
  335. }
  336. public L2PcInstance getActingPlayer()
  337. {
  338. return null;
  339. }
  340. /**
  341. * @return {@code true} if object is instance of L2PcInstance
  342. */
  343. public boolean isPlayer()
  344. {
  345. return false;
  346. }
  347. /**
  348. * @return {@code true} if object is instance of L2Playable
  349. */
  350. public boolean isPlayable()
  351. {
  352. return false;
  353. }
  354. /**
  355. * @return {@code true} if object is instance of L2Summon
  356. */
  357. public boolean isSummon()
  358. {
  359. return false;
  360. }
  361. /**
  362. * @return {@code true} if object is instance of L2PetInstance
  363. */
  364. public boolean isPet()
  365. {
  366. return false;
  367. }
  368. /**
  369. * @return {@code true} if object is instance of L2ServitorInstance
  370. */
  371. public boolean isServitor()
  372. {
  373. return false;
  374. }
  375. /**
  376. * @return {@code true} if object is instance of L2Attackable
  377. */
  378. public boolean isCharacter()
  379. {
  380. return false;
  381. }
  382. /**
  383. * @return {@code true} if object is instance of L2DoorInstance
  384. */
  385. public boolean isDoor()
  386. {
  387. return false;
  388. }
  389. /**
  390. * @return {@code true} if object is instance of L2Npc
  391. */
  392. public boolean isNpc()
  393. {
  394. return false;
  395. }
  396. /**
  397. * @return {@code true} if object is instance of L2Attackable
  398. */
  399. public boolean isL2Attackable()
  400. {
  401. return false;
  402. }
  403. /**
  404. * @return {@code true} if object is instance of L2MonsterInstance
  405. */
  406. public boolean isMonster()
  407. {
  408. return false;
  409. }
  410. /**
  411. * @return {@code true} if object is instance of L2TrapInstance
  412. */
  413. public boolean isTrap()
  414. {
  415. return false;
  416. }
  417. /**
  418. * @return {@code true} if object is instance of L2ItemInstance
  419. */
  420. public boolean isItem()
  421. {
  422. return false;
  423. }
  424. /**
  425. * @return {@code true} if object Npc Walker or Vehicle
  426. */
  427. public boolean isWalker()
  428. {
  429. return false;
  430. }
  431. /**
  432. * @return {@code true} if object Can be targeted
  433. */
  434. public boolean isTargetable()
  435. {
  436. return true;
  437. }
  438. /**
  439. * Check if the object is in the given zone Id.
  440. * @param zone the zone Id to check
  441. * @return {@code true} if the object is in that zone Id
  442. */
  443. public boolean isInsideZone(ZoneId zone)
  444. {
  445. return false;
  446. }
  447. /**
  448. * Check if current object has charged shot.
  449. * @param type of the shot to be checked.
  450. * @return {@code true} if the object has charged shot
  451. */
  452. public boolean isChargedShot(ShotType type)
  453. {
  454. return false;
  455. }
  456. /**
  457. * Charging shot into the current object.
  458. * @param type of the shot to be charged.
  459. * @param charged
  460. */
  461. public void setChargedShot(ShotType type, boolean charged)
  462. {
  463. }
  464. /**
  465. * Try to recharge a shot.
  466. * @param physical skill are using Soul shots.
  467. * @param magical skill are using Spirit shots.
  468. */
  469. public void rechargeShots(boolean physical, boolean magical)
  470. {
  471. }
  472. /**
  473. * @param <T>
  474. * @param script
  475. * @return
  476. */
  477. public final <T> T addScript(T script)
  478. {
  479. if (_scripts == null)
  480. {
  481. // Double-checked locking
  482. synchronized (this)
  483. {
  484. if (_scripts == null)
  485. {
  486. _scripts = new FastMap<String, Object>().shared();
  487. }
  488. }
  489. }
  490. _scripts.put(script.getClass().getName(), script);
  491. return script;
  492. }
  493. /**
  494. * @param <T>
  495. * @param script
  496. * @return
  497. */
  498. @SuppressWarnings("unchecked")
  499. public final <T> T removeScript(Class<T> script)
  500. {
  501. if (_scripts == null)
  502. {
  503. return null;
  504. }
  505. return (T) _scripts.remove(script.getName());
  506. }
  507. /**
  508. * @param <T>
  509. * @param script
  510. * @return
  511. */
  512. @SuppressWarnings("unchecked")
  513. public final <T> T getScript(Class<T> script)
  514. {
  515. if (_scripts == null)
  516. {
  517. return null;
  518. }
  519. return (T) _scripts.get(script.getName());
  520. }
  521. public void removeStatusListener(L2Character object)
  522. {
  523. }
  524. @Override
  525. public final void setXYZ(int x, int y, int z)
  526. {
  527. assert getWorldRegion() != null;
  528. super.setXYZ(x, y, z);
  529. try
  530. {
  531. if (L2World.getInstance().getRegion(getWorldPosition()) != getWorldRegion())
  532. {
  533. updateWorldRegion();
  534. }
  535. }
  536. catch (Exception e)
  537. {
  538. badCoords();
  539. }
  540. }
  541. protected void badCoords()
  542. {
  543. if (isCharacter())
  544. {
  545. this.decayMe();
  546. }
  547. else if (isPlayer())
  548. {
  549. ((L2Character) this).teleToLocation(new Location(0, 0, 0), false);
  550. ((L2Character) this).sendMessage("Error with your coords, Please ask a GM for help!");
  551. }
  552. }
  553. public final void setXYZInvisible(int x, int y, int z)
  554. {
  555. assert getWorldRegion() == null;
  556. if (x > L2World.MAP_MAX_X)
  557. {
  558. x = L2World.MAP_MAX_X - 5000;
  559. }
  560. if (x < L2World.MAP_MIN_X)
  561. {
  562. x = L2World.MAP_MIN_X + 5000;
  563. }
  564. if (y > L2World.MAP_MAX_Y)
  565. {
  566. y = L2World.MAP_MAX_Y - 5000;
  567. }
  568. if (y < L2World.MAP_MIN_Y)
  569. {
  570. y = L2World.MAP_MIN_Y + 5000;
  571. }
  572. setXYZ(x, y, z);
  573. this.setIsVisible(false);
  574. }
  575. public final void setLocationInvisible(Location loc)
  576. {
  577. setXYZInvisible(loc.getX(), loc.getY(), loc.getZ());
  578. }
  579. public void updateWorldRegion()
  580. {
  581. if (!this.isVisible())
  582. {
  583. return;
  584. }
  585. L2WorldRegion newRegion = L2World.getInstance().getRegion(getWorldPosition());
  586. if (newRegion != getWorldRegion())
  587. {
  588. getWorldRegion().removeVisibleObject(this);
  589. setWorldRegion(newRegion);
  590. // Add the L2Oject spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  591. getWorldRegion().addVisibleObject(this);
  592. }
  593. }
  594. public final void setWorldPosition(Point3D newPosition)
  595. {
  596. setXYZ(newPosition.getX(), newPosition.getY(), newPosition.getZ());
  597. }
  598. public final L2WorldRegion getWorldRegion()
  599. {
  600. return _worldRegion;
  601. }
  602. public void setWorldRegion(L2WorldRegion value)
  603. {
  604. if ((getWorldRegion() != null) && isCharacter()) // confirm revalidation of old region's zones
  605. {
  606. if (value != null)
  607. {
  608. getWorldRegion().revalidateZones((L2Character) this); // at world region change
  609. }
  610. else
  611. {
  612. getWorldRegion().removeFromZones((L2Character) this); // at world region change
  613. }
  614. }
  615. _worldRegion = value;
  616. }
  617. /**
  618. * Calculates distance between this L2Object and given x, y , z.
  619. * @param x - X coordinate.
  620. * @param y - Y coordinate.
  621. * @param z - Z coordinate.
  622. * @param includeZAxis - If set to true, Z coordinate will be included.
  623. * @param squared - If set to true, distance returned will be squared.
  624. * @return {@code double} - Distance between object and given x, y , z.
  625. */
  626. public double calculateDistance(int x, int y, int z, boolean includeZAxis, boolean squared)
  627. {
  628. final double distance = Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + (includeZAxis ? Math.pow(z - getZ(), 2) : 0);
  629. return (squared) ? distance : Math.sqrt(distance);
  630. }
  631. /**
  632. * Calculates distance between this L2Object and given location.
  633. * @param loc - Location on map.
  634. * @param includeZAxis - If set to true, Z coordinate will be included.
  635. * @param squared - If set to true, distance returned will be squared.
  636. * @return {@code double} - Distance between object and given location.
  637. */
  638. public double calculateDistance(ILocational loc, boolean includeZAxis, boolean squared)
  639. {
  640. return calculateDistance(loc.getX(), loc.getY(), loc.getZ(), includeZAxis, squared);
  641. }
  642. @Override
  643. public String toString()
  644. {
  645. return (getClass().getSimpleName() + ":" + getName() + "[" + getObjectId() + "]");
  646. }
  647. }