2
0

L2Object.java 22 KB

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