L2Object.java 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * Copyright (C) 2004-2014 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 java.util.concurrent.atomic.AtomicInteger;
  22. import javolution.util.FastMap;
  23. import com.l2jserver.gameserver.enums.InstanceType;
  24. import com.l2jserver.gameserver.enums.ShotType;
  25. import com.l2jserver.gameserver.handler.ActionHandler;
  26. import com.l2jserver.gameserver.handler.ActionShiftHandler;
  27. import com.l2jserver.gameserver.handler.IActionHandler;
  28. import com.l2jserver.gameserver.handler.IActionShiftHandler;
  29. import com.l2jserver.gameserver.idfactory.IdFactory;
  30. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.L2Npc;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.actor.knownlist.ObjectKnownList;
  35. import com.l2jserver.gameserver.model.actor.poly.ObjectPoly;
  36. import com.l2jserver.gameserver.model.entity.Instance;
  37. import com.l2jserver.gameserver.model.events.ListenersContainer;
  38. import com.l2jserver.gameserver.model.interfaces.IDecayable;
  39. import com.l2jserver.gameserver.model.interfaces.IIdentifiable;
  40. import com.l2jserver.gameserver.model.interfaces.ILocational;
  41. import com.l2jserver.gameserver.model.interfaces.INamable;
  42. import com.l2jserver.gameserver.model.interfaces.IPositionable;
  43. import com.l2jserver.gameserver.model.interfaces.ISpawnable;
  44. import com.l2jserver.gameserver.model.interfaces.IUniqueId;
  45. import com.l2jserver.gameserver.model.zone.ZoneId;
  46. import com.l2jserver.gameserver.network.SystemMessageId;
  47. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  48. import com.l2jserver.gameserver.network.serverpackets.DeleteObject;
  49. import com.l2jserver.gameserver.network.serverpackets.ExSendUIEvent;
  50. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  51. import com.l2jserver.gameserver.util.Util;
  52. /**
  53. * Base class for all interactive objects.
  54. */
  55. public abstract class L2Object extends ListenersContainer implements IIdentifiable, INamable, ISpawnable, IUniqueId, IDecayable, IPositionable
  56. {
  57. /** Name */
  58. private String _name;
  59. /** Object ID */
  60. private int _objectId;
  61. /** World Region */
  62. private L2WorldRegion _worldRegion;
  63. /** Instance type */
  64. private InstanceType _instanceType = null;
  65. private volatile Map<String, Object> _scripts;
  66. /** X coordinate */
  67. private final AtomicInteger _x = new AtomicInteger(0);
  68. /** Y coordinate */
  69. private final AtomicInteger _y = new AtomicInteger(0);
  70. /** Z coordinate */
  71. private final AtomicInteger _z = new AtomicInteger(0);
  72. /** Orientation */
  73. private final AtomicInteger _heading = new AtomicInteger(0);
  74. /** Instance id of object. 0 - Global */
  75. private final AtomicInteger _instanceId = new AtomicInteger(0);
  76. private boolean _isVisible;
  77. private boolean _isInvisible;
  78. private ObjectKnownList _knownList;
  79. public L2Object(int objectId)
  80. {
  81. setInstanceType(InstanceType.L2Object);
  82. _objectId = objectId;
  83. initKnownList();
  84. }
  85. /**
  86. * Gets the instance type of object.
  87. * @return the instance type
  88. */
  89. public final InstanceType getInstanceType()
  90. {
  91. return _instanceType;
  92. }
  93. /**
  94. * Sets the instance type.
  95. * @param newInstanceType the instance type to set
  96. */
  97. protected final void setInstanceType(InstanceType newInstanceType)
  98. {
  99. _instanceType = newInstanceType;
  100. }
  101. /**
  102. * Verifies if object is of any given instance types.
  103. * @param instanceTypes the instance types to verify
  104. * @return {@code true} if object is of any given instance types, {@code false} otherwise
  105. */
  106. public final boolean isInstanceTypes(InstanceType... instanceTypes)
  107. {
  108. return _instanceType.isTypes(instanceTypes);
  109. }
  110. public final void onAction(L2PcInstance player)
  111. {
  112. onAction(player, true);
  113. }
  114. public void onAction(L2PcInstance player, boolean interact)
  115. {
  116. IActionHandler handler = ActionHandler.getInstance().getHandler(getInstanceType());
  117. if (handler != null)
  118. {
  119. handler.action(player, this, interact);
  120. }
  121. player.sendPacket(ActionFailed.STATIC_PACKET);
  122. }
  123. public void onActionShift(L2PcInstance player)
  124. {
  125. IActionShiftHandler handler = ActionShiftHandler.getInstance().getHandler(getInstanceType());
  126. if (handler != null)
  127. {
  128. handler.action(player, this, true);
  129. }
  130. player.sendPacket(ActionFailed.STATIC_PACKET);
  131. }
  132. public void onForcedAttack(L2PcInstance player)
  133. {
  134. player.sendPacket(ActionFailed.STATIC_PACKET);
  135. }
  136. public void onSpawn()
  137. {
  138. }
  139. @Override
  140. public boolean decayMe()
  141. {
  142. assert getWorldRegion() != null;
  143. L2WorldRegion reg = getWorldRegion();
  144. synchronized (this)
  145. {
  146. _isVisible = false;
  147. setWorldRegion(null);
  148. }
  149. // this can synchronize on others instances, so it's out of
  150. // synchronized, to avoid deadlocks
  151. // Remove the L2Object from the world
  152. L2World.getInstance().removeVisibleObject(this, reg);
  153. L2World.getInstance().removeObject(this);
  154. return true;
  155. }
  156. public void refreshID()
  157. {
  158. L2World.getInstance().removeObject(this);
  159. IdFactory.getInstance().releaseId(getObjectId());
  160. _objectId = IdFactory.getInstance().getNextId();
  161. }
  162. @Override
  163. public final boolean spawnMe()
  164. {
  165. assert (getWorldRegion() == null) && (getLocation().getX() != 0) && (getLocation().getY() != 0) && (getLocation().getZ() != 0);
  166. synchronized (this)
  167. {
  168. // Set the x,y,z position of the L2Object spawn and update its _worldregion
  169. _isVisible = true;
  170. setWorldRegion(L2World.getInstance().getRegion(getLocation()));
  171. // Add the L2Object spawn in the _allobjects of L2World
  172. L2World.getInstance().storeObject(this);
  173. // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  174. getWorldRegion().addVisibleObject(this);
  175. }
  176. // this can synchronize on others instances, so it's out of synchronized, to avoid deadlocks
  177. // Add the L2Object spawn in the world as a visible object
  178. L2World.getInstance().addVisibleObject(this, getWorldRegion());
  179. onSpawn();
  180. return true;
  181. }
  182. public final void spawnMe(int x, int y, int z)
  183. {
  184. assert getWorldRegion() == null;
  185. synchronized (this)
  186. {
  187. // Set the x,y,z position of the L2Object spawn and update its _worldregion
  188. _isVisible = true;
  189. if (x > L2World.MAP_MAX_X)
  190. {
  191. x = L2World.MAP_MAX_X - 5000;
  192. }
  193. if (x < L2World.MAP_MIN_X)
  194. {
  195. x = L2World.MAP_MIN_X + 5000;
  196. }
  197. if (y > L2World.MAP_MAX_Y)
  198. {
  199. y = L2World.MAP_MAX_Y - 5000;
  200. }
  201. if (y < L2World.MAP_MIN_Y)
  202. {
  203. y = L2World.MAP_MIN_Y + 5000;
  204. }
  205. setXYZ(x, y, z);
  206. setWorldRegion(L2World.getInstance().getRegion(getLocation()));
  207. // Add the L2Object spawn in the _allobjects of L2World
  208. }
  209. L2World.getInstance().storeObject(this);
  210. // these can synchronize on others instances, so they're out of
  211. // synchronized, to avoid deadlocks
  212. // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  213. getWorldRegion().addVisibleObject(this);
  214. // Add the L2Object spawn in the world as a visible object
  215. L2World.getInstance().addVisibleObject(this, getWorldRegion());
  216. onSpawn();
  217. }
  218. /**
  219. * Verify if object can be attacked.
  220. * @return {@code true} if object can be attacked, {@code false} otherwise
  221. */
  222. public boolean canBeAttacked()
  223. {
  224. return false;
  225. }
  226. public abstract boolean isAutoAttackable(L2Character attacker);
  227. public final boolean isVisible()
  228. {
  229. return getWorldRegion() != null;
  230. }
  231. public final void setIsVisible(boolean value)
  232. {
  233. _isVisible = value;
  234. if (!_isVisible)
  235. {
  236. setWorldRegion(null);
  237. }
  238. }
  239. public void toggleVisible()
  240. {
  241. if (isVisible())
  242. {
  243. decayMe();
  244. }
  245. else
  246. {
  247. spawnMe();
  248. }
  249. }
  250. public ObjectKnownList getKnownList()
  251. {
  252. return _knownList;
  253. }
  254. public void initKnownList()
  255. {
  256. _knownList = new ObjectKnownList(this);
  257. }
  258. public final void setKnownList(ObjectKnownList value)
  259. {
  260. _knownList = value;
  261. }
  262. @Override
  263. public String getName()
  264. {
  265. return _name;
  266. }
  267. public void setName(String value)
  268. {
  269. _name = value;
  270. }
  271. @Override
  272. public final int getObjectId()
  273. {
  274. return _objectId;
  275. }
  276. public final ObjectPoly getPoly()
  277. {
  278. final ObjectPoly poly = getScript(ObjectPoly.class);
  279. return (poly == null) ? addScript(new ObjectPoly(this)) : poly;
  280. }
  281. public abstract void sendInfo(L2PcInstance activeChar);
  282. public void sendPacket(L2GameServerPacket mov)
  283. {
  284. }
  285. public void sendPacket(SystemMessageId id)
  286. {
  287. }
  288. public L2PcInstance getActingPlayer()
  289. {
  290. return null;
  291. }
  292. /**
  293. * Verify if object is instance of L2Attackable.
  294. * @return {@code true} if object is instance of L2Attackable, {@code false} otherwise
  295. */
  296. public boolean isAttackable()
  297. {
  298. return false;
  299. }
  300. /**
  301. * Verify if object is instance of L2Character.
  302. * @return {@code true} if object is instance of L2Character, {@code false} otherwise
  303. */
  304. public boolean isCharacter()
  305. {
  306. return false;
  307. }
  308. /**
  309. * Verify if object is instance of L2DoorInstance.
  310. * @return {@code true} if object is instance of L2DoorInstance, {@code false} otherwise
  311. */
  312. public boolean isDoor()
  313. {
  314. return false;
  315. }
  316. /**
  317. * Verify if object is instance of L2MonsterInstance.
  318. * @return {@code true} if object is instance of L2MonsterInstance, {@code false} otherwise
  319. */
  320. public boolean isMonster()
  321. {
  322. return false;
  323. }
  324. /**
  325. * Verify if object is instance of L2Npc.
  326. * @return {@code true} if object is instance of L2Npc, {@code false} otherwise
  327. */
  328. public boolean isNpc()
  329. {
  330. return false;
  331. }
  332. /**
  333. * Verify if object is instance of L2PetInstance.
  334. * @return {@code true} if object is instance of L2PetInstance, {@code false} otherwise
  335. */
  336. public boolean isPet()
  337. {
  338. return false;
  339. }
  340. /**
  341. * Verify if object is instance of L2PcInstance.
  342. * @return {@code true} if object is instance of L2PcInstance, {@code false} otherwise
  343. */
  344. public boolean isPlayer()
  345. {
  346. return false;
  347. }
  348. /**
  349. * Verify if object is instance of L2Playable.
  350. * @return {@code true} if object is instance of L2Playable, {@code false} otherwise
  351. */
  352. public boolean isPlayable()
  353. {
  354. return false;
  355. }
  356. /**
  357. * Verify if object is instance of L2ServitorInstance.
  358. * @return {@code true} if object is instance of L2ServitorInstance, {@code false} otherwise
  359. */
  360. public boolean isServitor()
  361. {
  362. return false;
  363. }
  364. /**
  365. * Verify if object is instance of L2Summon.
  366. * @return {@code true} if object is instance of L2Summon, {@code false} otherwise
  367. */
  368. public boolean isSummon()
  369. {
  370. return false;
  371. }
  372. /**
  373. * Verify if object is instance of L2TrapInstance.
  374. * @return {@code true} if object is instance of L2TrapInstance, {@code false} otherwise
  375. */
  376. public boolean isTrap()
  377. {
  378. return false;
  379. }
  380. /**
  381. * Verify if object is instance of L2ItemInstance.
  382. * @return {@code true} if object is instance of L2ItemInstance, {@code false} otherwise
  383. */
  384. public boolean isItem()
  385. {
  386. return false;
  387. }
  388. /**
  389. * Verifies if the object is a walker NPC.
  390. * @return {@code true} if object is a walker NPC, {@code false} otherwise
  391. */
  392. public boolean isWalker()
  393. {
  394. return false;
  395. }
  396. /**
  397. * Verifies if this object is a vehicle.
  398. * @return {@code true} if object is Vehicle, {@code false} otherwise
  399. */
  400. public boolean isVehicle()
  401. {
  402. return false;
  403. }
  404. /**
  405. * @return {@code true} if object Can be targeted
  406. */
  407. public boolean isTargetable()
  408. {
  409. return true;
  410. }
  411. /**
  412. * Check if the object is in the given zone Id.
  413. * @param zone the zone Id to check
  414. * @return {@code true} if the object is in that zone Id
  415. */
  416. public boolean isInsideZone(ZoneId zone)
  417. {
  418. return false;
  419. }
  420. /**
  421. * Check if current object has charged shot.
  422. * @param type of the shot to be checked.
  423. * @return {@code true} if the object has charged shot
  424. */
  425. public boolean isChargedShot(ShotType type)
  426. {
  427. return false;
  428. }
  429. /**
  430. * Charging shot into the current object.
  431. * @param type of the shot to be charged.
  432. * @param charged
  433. */
  434. public void setChargedShot(ShotType type, boolean charged)
  435. {
  436. }
  437. /**
  438. * Try to recharge a shot.
  439. * @param physical skill are using Soul shots.
  440. * @param magical skill are using Spirit shots.
  441. */
  442. public void rechargeShots(boolean physical, boolean magical)
  443. {
  444. }
  445. /**
  446. * @param <T>
  447. * @param script
  448. * @return
  449. */
  450. public final <T> T addScript(T script)
  451. {
  452. if (_scripts == null)
  453. {
  454. // Double-checked locking
  455. synchronized (this)
  456. {
  457. if (_scripts == null)
  458. {
  459. _scripts = new FastMap<String, Object>().shared();
  460. }
  461. }
  462. }
  463. _scripts.put(script.getClass().getName(), script);
  464. return script;
  465. }
  466. /**
  467. * @param <T>
  468. * @param script
  469. * @return
  470. */
  471. @SuppressWarnings("unchecked")
  472. public final <T> T removeScript(Class<T> script)
  473. {
  474. if (_scripts == null)
  475. {
  476. return null;
  477. }
  478. return (T) _scripts.remove(script.getName());
  479. }
  480. /**
  481. * @param <T>
  482. * @param script
  483. * @return
  484. */
  485. @SuppressWarnings("unchecked")
  486. public final <T> T getScript(Class<T> script)
  487. {
  488. if (_scripts == null)
  489. {
  490. return null;
  491. }
  492. return (T) _scripts.get(script.getName());
  493. }
  494. public void removeStatusListener(L2Character object)
  495. {
  496. }
  497. protected void badCoords()
  498. {
  499. if (isCharacter())
  500. {
  501. decayMe();
  502. }
  503. else if (isPlayer())
  504. {
  505. ((L2Character) this).teleToLocation(new Location(0, 0, 0), false);
  506. ((L2Character) this).sendMessage("Error with your coords, Please ask a GM for help!");
  507. }
  508. }
  509. public final void setXYZInvisible(int x, int y, int z)
  510. {
  511. assert getWorldRegion() == null;
  512. if (x > L2World.MAP_MAX_X)
  513. {
  514. x = L2World.MAP_MAX_X - 5000;
  515. }
  516. if (x < L2World.MAP_MIN_X)
  517. {
  518. x = L2World.MAP_MIN_X + 5000;
  519. }
  520. if (y > L2World.MAP_MAX_Y)
  521. {
  522. y = L2World.MAP_MAX_Y - 5000;
  523. }
  524. if (y < L2World.MAP_MIN_Y)
  525. {
  526. y = L2World.MAP_MIN_Y + 5000;
  527. }
  528. setXYZ(x, y, z);
  529. setIsVisible(false);
  530. }
  531. public final void setLocationInvisible(ILocational loc)
  532. {
  533. setXYZInvisible(loc.getX(), loc.getY(), loc.getZ());
  534. }
  535. public void updateWorldRegion()
  536. {
  537. if (!isVisible())
  538. {
  539. return;
  540. }
  541. L2WorldRegion newRegion = L2World.getInstance().getRegion(getLocation());
  542. if (newRegion != getWorldRegion())
  543. {
  544. getWorldRegion().removeVisibleObject(this);
  545. setWorldRegion(newRegion);
  546. // Add the L2Oject spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  547. getWorldRegion().addVisibleObject(this);
  548. }
  549. }
  550. public final L2WorldRegion getWorldRegion()
  551. {
  552. return _worldRegion;
  553. }
  554. public void setWorldRegion(L2WorldRegion value)
  555. {
  556. if ((getWorldRegion() != null) && isCharacter()) // confirm revalidation of old region's zones
  557. {
  558. if (value != null)
  559. {
  560. getWorldRegion().revalidateZones((L2Character) this); // at world region change
  561. }
  562. else
  563. {
  564. getWorldRegion().removeFromZones((L2Character) this); // at world region change
  565. }
  566. }
  567. _worldRegion = value;
  568. }
  569. /**
  570. * Gets the X coordinate.
  571. * @return the X coordinate
  572. */
  573. @Override
  574. public int getX()
  575. {
  576. return _x.get();
  577. }
  578. /**
  579. * Gets the Y coordinate.
  580. * @return the Y coordinate
  581. */
  582. @Override
  583. public int getY()
  584. {
  585. return _y.get();
  586. }
  587. /**
  588. * Gets the Z coordinate.
  589. * @return the Z coordinate
  590. */
  591. @Override
  592. public int getZ()
  593. {
  594. return _z.get();
  595. }
  596. /**
  597. * Gets the heading.
  598. * @return the heading
  599. */
  600. @Override
  601. public int getHeading()
  602. {
  603. return _heading.get();
  604. }
  605. /**
  606. * Gets the instance ID.
  607. * @return the instance ID
  608. */
  609. @Override
  610. public int getInstanceId()
  611. {
  612. return _instanceId.get();
  613. }
  614. /**
  615. * Gets the location object.
  616. * @return the location object
  617. */
  618. @Override
  619. public Location getLocation()
  620. {
  621. return new Location(getX(), getY(), getZ(), getHeading(), getInstanceId());
  622. }
  623. /**
  624. * Sets the X coordinate
  625. * @param newX the X coordinate
  626. */
  627. @Override
  628. public void setX(int newX)
  629. {
  630. _x.set(newX);
  631. }
  632. /**
  633. * Sets the Y coordinate
  634. * @param newY the Y coordinate
  635. */
  636. @Override
  637. public void setY(int newY)
  638. {
  639. _y.set(newY);
  640. }
  641. /**
  642. * Sets the Z coordinate
  643. * @param newZ the Z coordinate
  644. */
  645. @Override
  646. public void setZ(int newZ)
  647. {
  648. _z.set(newZ);
  649. }
  650. /**
  651. * Sets the x, y, z coordinate.
  652. * @param newX the X coordinate
  653. * @param newY the Y coordinate
  654. * @param newZ the Z coordinate
  655. */
  656. @Override
  657. public final void setXYZ(int newX, int newY, int newZ)
  658. {
  659. assert getWorldRegion() != null;
  660. setX(newX);
  661. setY(newY);
  662. setZ(newZ);
  663. try
  664. {
  665. if (L2World.getInstance().getRegion(getLocation()) != getWorldRegion())
  666. {
  667. updateWorldRegion();
  668. }
  669. }
  670. catch (Exception e)
  671. {
  672. badCoords();
  673. }
  674. }
  675. /**
  676. * Sets the x, y, z coordinate.
  677. * @param loc the location object
  678. */
  679. @Override
  680. public void setXYZ(ILocational loc)
  681. {
  682. setXYZ(loc.getX(), loc.getY(), loc.getZ());
  683. }
  684. /**
  685. * Sets heading of object.
  686. * @param newHeading the new heading
  687. */
  688. @Override
  689. public void setHeading(int newHeading)
  690. {
  691. _heading.set(newHeading);
  692. }
  693. /**
  694. * Sets the instance ID of object.<br>
  695. * 0 - Global<br>
  696. * TODO: Add listener here.
  697. * @param instanceId the ID of the instance
  698. */
  699. @Override
  700. public void setInstanceId(int instanceId)
  701. {
  702. if ((instanceId < 0) || (getInstanceId() == instanceId))
  703. {
  704. return;
  705. }
  706. Instance oldI = InstanceManager.getInstance().getInstance(getInstanceId());
  707. Instance newI = InstanceManager.getInstance().getInstance(instanceId);
  708. if (newI == null)
  709. {
  710. return;
  711. }
  712. if (isPlayer())
  713. {
  714. final L2PcInstance player = getActingPlayer();
  715. if ((getInstanceId() > 0) && (oldI != null))
  716. {
  717. oldI.removePlayer(getObjectId());
  718. if (oldI.isShowTimer())
  719. {
  720. sendInstanceUpdate(oldI, true);
  721. }
  722. }
  723. if (instanceId > 0)
  724. {
  725. newI.addPlayer(getObjectId());
  726. if (newI.isShowTimer())
  727. {
  728. sendInstanceUpdate(newI, false);
  729. }
  730. }
  731. if (player.hasSummon())
  732. {
  733. player.getSummon().setInstanceId(instanceId);
  734. }
  735. }
  736. else if (isNpc())
  737. {
  738. final L2Npc npc = (L2Npc) this;
  739. if ((getInstanceId() > 0) && (oldI != null))
  740. {
  741. oldI.removeNpc(npc);
  742. }
  743. if (instanceId > 0)
  744. {
  745. newI.addNpc(npc);
  746. }
  747. }
  748. _instanceId.set(instanceId);
  749. if (_isVisible && (_knownList != null))
  750. {
  751. // We don't want some ugly looking disappear/appear effects, so don't update
  752. // the knownlist here, but players usually enter instancezones through teleporting
  753. // and the teleport will do the revalidation for us.
  754. if (!isPlayer())
  755. {
  756. decayMe();
  757. spawnMe();
  758. }
  759. }
  760. }
  761. /**
  762. * Sets location of object.
  763. * @param loc the location object
  764. */
  765. @Override
  766. public void setLocation(Location loc)
  767. {
  768. _x.set(loc.getX());
  769. _y.set(loc.getY());
  770. _z.set(loc.getZ());
  771. _heading.set(loc.getHeading());
  772. _instanceId.set(loc.getInstanceId());
  773. }
  774. /**
  775. * Calculates distance between this L2Object and given x, y , z.
  776. * @param x the X coordinate
  777. * @param y the Y coordinate
  778. * @param z the Z coordinate
  779. * @param includeZAxis if {@code true} Z axis will be included
  780. * @param squared if {@code true} return will be squared
  781. * @return distance between object and given x, y, z.
  782. */
  783. public final double calculateDistance(int x, int y, int z, boolean includeZAxis, boolean squared)
  784. {
  785. final double distance = Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + (includeZAxis ? Math.pow(z - getZ(), 2) : 0);
  786. return (squared) ? distance : Math.sqrt(distance);
  787. }
  788. /**
  789. * Calculates distance between this L2Object and given location.
  790. * @param loc the location object
  791. * @param includeZAxis if {@code true} Z axis will be included
  792. * @param squared if {@code true} return will be squared
  793. * @return distance between object and given location.
  794. */
  795. public final double calculateDistance(ILocational loc, boolean includeZAxis, boolean squared)
  796. {
  797. return calculateDistance(loc.getX(), loc.getY(), loc.getZ(), includeZAxis, squared);
  798. }
  799. /**
  800. * Calculates the angle in degrees from this object to the given object.<br>
  801. * The return value can be described as how much this object has to turn<br>
  802. * to have the given object directly in front of it.
  803. * @param target the object to which to calculate the angle
  804. * @return the angle this object has to turn to have the given object in front of it
  805. */
  806. public final double calculateDirectionTo(ILocational target)
  807. {
  808. int heading = Util.calculateHeadingFrom(this, target) - getHeading();
  809. if (heading < 0)
  810. {
  811. heading = 65535 + heading;
  812. }
  813. return Util.convertHeadingToDegree(heading);
  814. }
  815. /**
  816. * Sends an instance update for player.
  817. * @param instance the instance to update
  818. * @param hide if {@code true} hide the player
  819. */
  820. private final void sendInstanceUpdate(Instance instance, boolean hide)
  821. {
  822. final int startTime = (int) ((System.currentTimeMillis() - instance.getInstanceStartTime()) / 1000);
  823. final int endTime = (int) ((instance.getInstanceEndTime() - instance.getInstanceStartTime()) / 1000);
  824. if (instance.isTimerIncrease())
  825. {
  826. sendPacket(new ExSendUIEvent(getActingPlayer(), hide, true, startTime, endTime, instance.getTimerText()));
  827. }
  828. else
  829. {
  830. sendPacket(new ExSendUIEvent(getActingPlayer(), hide, false, endTime - startTime, 0, instance.getTimerText()));
  831. }
  832. }
  833. /**
  834. * @return {@code true} if this object is invisible, {@code false} otherwise.
  835. */
  836. public boolean isInvisible()
  837. {
  838. return _isInvisible;
  839. }
  840. /**
  841. * Sets this object as invisible or not
  842. * @param invis
  843. */
  844. public void setInvisible(boolean invis)
  845. {
  846. _isInvisible = invis;
  847. if (invis)
  848. {
  849. final DeleteObject deletePacket = new DeleteObject(this);
  850. for (L2Object obj : getKnownList().getKnownObjects().values())
  851. {
  852. if ((obj != null) && obj.isPlayer())
  853. {
  854. final L2PcInstance player = obj.getActingPlayer();
  855. if (!isVisibleFor(player))
  856. {
  857. obj.sendPacket(deletePacket);
  858. }
  859. }
  860. }
  861. }
  862. // Broadcast information regarding the object to those which are suppose to see.
  863. broadcastInfo();
  864. }
  865. /**
  866. * @param player
  867. * @return {@code true} if player can see an invisible object if it's invisible, {@code false} otherwise.
  868. */
  869. public boolean isVisibleFor(L2PcInstance player)
  870. {
  871. return !isInvisible() || player.canOverrideCond(PcCondOverride.SEE_ALL_PLAYERS);
  872. }
  873. /**
  874. * Broadcasts describing info to known players.
  875. */
  876. public void broadcastInfo()
  877. {
  878. for (L2Object obj : getKnownList().getKnownObjects().values())
  879. {
  880. if ((obj != null) && obj.isPlayer() && isVisibleFor(obj.getActingPlayer()))
  881. {
  882. sendInfo(obj.getActingPlayer());
  883. }
  884. }
  885. }
  886. @Override
  887. public boolean equals(Object obj)
  888. {
  889. return ((obj instanceof L2Object) && (((L2Object) obj).getObjectId() == getObjectId()));
  890. }
  891. @Override
  892. public String toString()
  893. {
  894. return (getClass().getSimpleName() + ":" + getName() + "[" + getObjectId() + "]");
  895. }
  896. }