L2Object.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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;
  16. import com.l2jserver.Config;
  17. import com.l2jserver.gameserver.idfactory.IdFactory;
  18. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  19. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.actor.L2Npc;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.actor.knownlist.ObjectKnownList;
  24. import com.l2jserver.gameserver.model.actor.poly.ObjectPoly;
  25. import com.l2jserver.gameserver.model.actor.position.ObjectPosition;
  26. import com.l2jserver.gameserver.model.entity.Instance;
  27. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  28. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  29. /**
  30. * Mother class of all objects in the world wich ones is it possible
  31. * to interact (PC, NPC, Item...)<BR><BR>
  32. *
  33. * L2Object :<BR><BR>
  34. * <li>L2Character</li>
  35. * <li>L2ItemInstance</li>
  36. * <li>L2Potion</li>
  37. *
  38. */
  39. public abstract class L2Object
  40. {
  41. // =========================================================
  42. // Data Field
  43. private boolean _isVisible; // Object visibility
  44. private ObjectKnownList _knownList;
  45. private String _name;
  46. private int _objectId; // Object identifier
  47. private ObjectPoly _poly;
  48. private ObjectPosition _position;
  49. private int _instanceId = 0;
  50. private InstanceType _instanceType = null;
  51. // =========================================================
  52. // Constructor
  53. public L2Object(int objectId)
  54. {
  55. setInstanceType(InstanceType.L2Object);
  56. _objectId = objectId;
  57. initKnownList();
  58. initPosition();
  59. }
  60. public static enum InstanceType
  61. {
  62. L2Object(null),
  63. L2Character(L2Object),
  64. L2Npc(L2Character),
  65. L2Playable(L2Character),
  66. L2Summon(L2Playable),
  67. L2Decoy(L2Character),
  68. L2Trap(L2Character),
  69. L2PcInstance(L2Playable),
  70. L2NpcInstance(L2Npc),
  71. L2MerchantInstance(L2NpcInstance),
  72. L2WarehouseInstance(L2NpcInstance),
  73. L2StaticObjectInstance(L2Character),
  74. L2DoorInstance(L2Character),
  75. L2NpcWalkerInstance(L2Npc),
  76. L2EffectPointInstance(L2Npc),
  77. // Summons, Pets, Decoys and Traps
  78. L2SummonInstance(L2Summon),
  79. L2SiegeSummonInstance(L2SummonInstance),
  80. L2MerchantSummonInstance(L2SummonInstance),
  81. L2PetInstance(L2Summon),
  82. L2BabyPetInstance(L2PetInstance),
  83. L2DecoyInstance(L2Decoy),
  84. L2TrapInstance(L2Trap),
  85. // Attackable
  86. L2Attackable(L2Npc),
  87. L2GuardInstance(L2Attackable),
  88. L2MonsterInstance(L2Attackable),
  89. L2ChestInstance(L2MonsterInstance),
  90. L2ControllableMobInstance(L2MonsterInstance),
  91. L2FeedableBeastInstance(L2MonsterInstance),
  92. L2TamedBeastInstance(L2FeedableBeastInstance),
  93. L2FriendlyMobInstance(L2Attackable),
  94. L2PenaltyMonsterInstance(L2MonsterInstance),
  95. L2RiftInvaderInstance(L2MonsterInstance),
  96. L2MinionInstance(L2MonsterInstance),
  97. L2RaidBossInstance(L2MonsterInstance),
  98. L2GrandBossInstance(L2RaidBossInstance),
  99. // FlyMobs
  100. L2FlyNpcInstance(L2NpcInstance),
  101. L2FlyMonsterInstance(L2MonsterInstance),
  102. L2FlyMinionInstance(L2MinionInstance),
  103. L2FlyRaidBossInstance(L2RaidBossInstance),
  104. // Sepulchers
  105. L2SepulcherNpcInstance(L2NpcInstance),
  106. L2SepulcherMonsterInstance(L2MonsterInstance),
  107. // Festival
  108. L2FestivalGiudeInstance(L2Npc),
  109. L2FestivalMonsterInstance(L2MonsterInstance),
  110. // Ships and controllers
  111. L2BoatInstance(L2Character),
  112. L2AirShipInstance(L2Character),
  113. L2AirShipControllerInstance(L2NpcInstance),
  114. // Siege
  115. L2DefenderInstance(L2Attackable),
  116. L2ArtefactInstance(L2NpcInstance),
  117. L2ControlTowerInstance(L2Npc),
  118. L2FlameTowerInstance(L2Npc),
  119. L2SiegeFlagInstance(L2Npc),
  120. L2SiegeNpcInstance(L2Npc),
  121. // Fort Siege
  122. L2FortBallistaInstance(L2Npc),
  123. L2FortCommanderInstance(L2DefenderInstance),
  124. // Castle NPCs
  125. L2CastleBlacksmithInstance(L2NpcInstance),
  126. L2CastleChamberlainInstance(L2MerchantInstance),
  127. L2CastleMagicianInstance(L2NpcInstance),
  128. L2CastleTeleporterInstance(L2Npc),
  129. L2CastleWarehouseInstance(L2WarehouseInstance),
  130. L2MercManagerInstance(L2MerchantInstance),
  131. // Fort NPCs
  132. L2FortEnvoyInstance(L2Npc),
  133. L2FortLogisticsInstance(L2MerchantInstance),
  134. L2FortManagerInstance(L2MerchantInstance),
  135. L2FortSiegeNpcInstance(L2NpcWalkerInstance),
  136. L2FortSupportCaptainInstance(L2MerchantInstance),
  137. // Seven Signs
  138. L2CabaleBufferInstance(L2Npc),
  139. L2SignsPriestInstance(L2Npc),
  140. L2DawnPriestInstance(L2SignsPriestInstance),
  141. L2DuskPriestInstance(L2SignsPriestInstance),
  142. L2DungeonGatekeeperInstance(L2Npc),
  143. // City NPCs
  144. L2AdventurerInstance(L2NpcInstance),
  145. L2AuctioneerInstance(L2Npc),
  146. L2ClanHallManagerInstance(L2MerchantInstance),
  147. L2ClanTraderInstance(L2Npc),
  148. L2FameManagerInstance(L2Npc),
  149. L2FishermanInstance(L2MerchantInstance),
  150. L2ManorManagerInstance(L2MerchantInstance),
  151. L2MercenaryManagerInstance(L2Npc),
  152. L2ObservationInstance(L2Npc),
  153. L2OlympiadManagerInstance(L2Npc),
  154. L2PetManagerInstance(L2MerchantInstance),
  155. L2RaceManagerInstance(L2Npc),
  156. L2SymbolMakerInstance(L2Npc),
  157. L2TeleporterInstance(L2Npc),
  158. L2TownPetInstance(L2Npc),
  159. L2TrainerInstance(L2NpcInstance),
  160. L2TransformManagerInstance(L2MerchantInstance),
  161. L2VillageMasterInstance(L2NpcInstance),
  162. L2WyvernManagerInstance(L2NpcInstance),
  163. L2XmassTreeInstance(L2NpcInstance),
  164. // Doormens
  165. L2DoormenInstance(L2NpcInstance),
  166. L2CastleDoormenInstance(L2DoormenInstance),
  167. L2FortDoormenInstance(L2DoormenInstance),
  168. L2ClanHallDoormenInstance(L2DoormenInstance),
  169. // Custom
  170. L2ClassMasterInstance(L2NpcInstance),
  171. L2NpcBufferInstance(L2Npc),
  172. L2TvTEventNpcInstance(L2Npc),
  173. L2WeddingManagerInstance(L2Npc);
  174. private final InstanceType _parent;
  175. private final long _typeL;
  176. private final long _typeH;
  177. private final long _maskL;
  178. private final long _maskH;
  179. private InstanceType(InstanceType parent)
  180. {
  181. _parent = parent;
  182. final int high = this.ordinal() - (Long.SIZE - 1);
  183. if (high < 0)
  184. {
  185. _typeL = 1L << this.ordinal();
  186. _typeH = 0;
  187. }
  188. else
  189. {
  190. _typeL = 0;
  191. _typeH = 1L << high;
  192. }
  193. if (_typeL < 0 || _typeH < 0)
  194. throw new Error("Too many instance types, failed to load " + this.name());
  195. if (parent != null)
  196. {
  197. _maskL = _typeL | parent._maskL;
  198. _maskH = _typeH | parent._maskH;
  199. }
  200. else
  201. {
  202. _maskL = _typeL;
  203. _maskH = _typeH;
  204. }
  205. }
  206. public final InstanceType getParent()
  207. {
  208. return _parent;
  209. }
  210. public final boolean isType(InstanceType it)
  211. {
  212. return (_maskL & it._typeL) > 0 || (_maskH & it._typeH) > 0;
  213. }
  214. public final boolean isTypes(InstanceType... it)
  215. {
  216. for (InstanceType i : it)
  217. {
  218. if (isType(i))
  219. return true;
  220. }
  221. return false;
  222. }
  223. }
  224. protected final void setInstanceType(InstanceType i)
  225. {
  226. _instanceType = i;
  227. }
  228. public final InstanceType getInstanceType()
  229. {
  230. return _instanceType;
  231. }
  232. public final boolean isInstanceType(InstanceType i)
  233. {
  234. return _instanceType.isType(i);
  235. }
  236. public final boolean isInstanceTypes(InstanceType... i)
  237. {
  238. return _instanceType.isTypes(i);
  239. }
  240. // =========================================================
  241. // Event - Public
  242. public final void onAction(L2PcInstance player)
  243. {
  244. onAction(player, true);
  245. }
  246. public void onAction(L2PcInstance player, boolean interact)
  247. {
  248. player.sendPacket(ActionFailed.STATIC_PACKET);
  249. }
  250. public void onActionShift(L2PcInstance player)
  251. {
  252. player.sendPacket(ActionFailed.STATIC_PACKET);
  253. }
  254. public void onForcedAttack(L2PcInstance player)
  255. {
  256. player.sendPacket(ActionFailed.STATIC_PACKET);
  257. }
  258. /**
  259. * Do Nothing.<BR><BR>
  260. *
  261. * <B><U> Overridden in </U> :</B><BR><BR>
  262. * <li> L2GuardInstance : Set the home location of its L2GuardInstance </li>
  263. * <li> L2Attackable : Reset the Spoiled flag </li><BR><BR>
  264. *
  265. */
  266. public void onSpawn()
  267. {
  268. }
  269. // =========================================================
  270. // Position - Should remove to fully move to L2ObjectPosition
  271. public final void setXYZ(int x, int y, int z)
  272. {
  273. getPosition().setXYZ(x, y, z);
  274. }
  275. public final void setXYZInvisible(int x, int y, int z)
  276. {
  277. getPosition().setXYZInvisible(x, y, z);
  278. }
  279. public final int getX()
  280. {
  281. assert getPosition().getWorldRegion() != null || _isVisible;
  282. return getPosition().getX();
  283. }
  284. /**
  285. * @return The id of the instance zone the object is in - id 0 is global
  286. * since everything like dropped items, mobs, players can be in a instanciated area, it must be in l2object
  287. */
  288. public int getInstanceId()
  289. {
  290. return _instanceId;
  291. }
  292. /**
  293. * @param instanceId The id of the instance zone the object is in - id 0 is global
  294. */
  295. public void setInstanceId(int instanceId)
  296. {
  297. if (_instanceId == instanceId)
  298. return;
  299. Instance oldI = InstanceManager.getInstance().getInstance(_instanceId);
  300. Instance newI = InstanceManager.getInstance().getInstance(instanceId);
  301. if (newI == null)
  302. return;
  303. if (this instanceof L2PcInstance)
  304. {
  305. if (_instanceId > 0 && oldI != null)
  306. oldI.removePlayer(getObjectId());
  307. if (instanceId > 0)
  308. newI.addPlayer(getObjectId());
  309. if (((L2PcInstance)this).getPet() != null)
  310. ((L2PcInstance)this).getPet().setInstanceId(instanceId);
  311. }
  312. else if (this instanceof L2Npc)
  313. {
  314. if (_instanceId > 0 && oldI != null)
  315. oldI.removeNpc(((L2Npc)this));
  316. if (instanceId > 0)
  317. newI.addNpc(((L2Npc)this));
  318. }
  319. _instanceId = instanceId;
  320. // If we change it for visible objects, me must clear & revalidate knownlists
  321. if (_isVisible && _knownList != null)
  322. {
  323. if (this instanceof L2PcInstance)
  324. {
  325. // We don't want some ugly looking disappear/appear effects, so don't update
  326. // the knownlist here, but players usually enter instancezones through teleporting
  327. // and the teleport will do the revalidation for us.
  328. }
  329. else
  330. {
  331. decayMe();
  332. spawnMe();
  333. }
  334. }
  335. }
  336. public final int getY()
  337. {
  338. assert getPosition().getWorldRegion() != null || _isVisible;
  339. return getPosition().getY();
  340. }
  341. public final int getZ()
  342. {
  343. assert getPosition().getWorldRegion() != null || _isVisible;
  344. return getPosition().getZ();
  345. }
  346. // =========================================================
  347. // Method - Public
  348. /**
  349. * Remove a L2Object from the world.<BR><BR>
  350. *
  351. * <B><U> Actions</U> :</B><BR><BR>
  352. * <li>Remove the L2Object from the world</li><BR><BR>
  353. *
  354. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR>
  355. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packets to players</B></FONT><BR><BR>
  356. *
  357. * <B><U> Assert </U> :</B><BR><BR>
  358. * <li> _worldRegion != null <I>(L2Object is visible at the beginning)</I></li><BR><BR>
  359. *
  360. * <B><U> Example of use </U> :</B><BR><BR>
  361. * <li> Delete NPC/PC or Unsummon</li><BR><BR>
  362. *
  363. */
  364. public final void decayMe()
  365. {
  366. assert getPosition().getWorldRegion() != null;
  367. L2WorldRegion reg = getPosition().getWorldRegion();
  368. synchronized (this)
  369. {
  370. _isVisible = false;
  371. getPosition().setWorldRegion(null);
  372. }
  373. // this can synchronize on others instancies, so it's out of
  374. // synchronized, to avoid deadlocks
  375. // Remove the L2Object from the world
  376. L2World.getInstance().removeVisibleObject(this, reg);
  377. L2World.getInstance().removeObject(this);
  378. if (Config.SAVE_DROPPED_ITEM)
  379. ItemsOnGroundManager.getInstance().removeObject(this);
  380. }
  381. public void refreshID()
  382. {
  383. L2World.getInstance().removeObject(this);
  384. IdFactory.getInstance().releaseId(getObjectId());
  385. _objectId = IdFactory.getInstance().getNextId();
  386. }
  387. /**
  388. * Init the position of a L2Object spawn and add it in the world as a visible object.<BR><BR>
  389. *
  390. * <B><U> Actions</U> :</B><BR><BR>
  391. * <li>Set the x,y,z position of the L2Object spawn and update its _worldregion </li>
  392. * <li>Add the L2Object spawn in the _allobjects of L2World </li>
  393. * <li>Add the L2Object spawn to _visibleObjects of its L2WorldRegion</li>
  394. * <li>Add the L2Object spawn in the world as a <B>visible</B> object</li><BR><BR>
  395. *
  396. * <B><U> Assert </U> :</B><BR><BR>
  397. * <li> _worldRegion == null <I>(L2Object is invisible at the beginning)</I></li><BR><BR>
  398. *
  399. * <B><U> Example of use </U> :</B><BR><BR>
  400. * <li> Create Door</li>
  401. * <li> Spawn : Monster, Minion, CTs, Summon...</li><BR>
  402. *
  403. */
  404. public final void spawnMe()
  405. {
  406. assert getPosition().getWorldRegion() == null && getPosition().getWorldPosition().getX() != 0 && getPosition().getWorldPosition().getY() != 0 && getPosition().getWorldPosition().getZ() != 0;
  407. synchronized (this)
  408. {
  409. // Set the x,y,z position of the L2Object spawn and update its _worldregion
  410. _isVisible = true;
  411. getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
  412. // Add the L2Object spawn in the _allobjects of L2World
  413. L2World.getInstance().storeObject(this);
  414. // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  415. getPosition().getWorldRegion().addVisibleObject(this);
  416. }
  417. // this can synchronize on others instancies, so it's out of
  418. // synchronized, to avoid deadlocks
  419. // Add the L2Object spawn in the world as a visible object
  420. L2World.getInstance().addVisibleObject(this, getPosition().getWorldRegion());
  421. onSpawn();
  422. }
  423. public final void spawnMe(int x, int y, int z)
  424. {
  425. assert getPosition().getWorldRegion() == null;
  426. synchronized (this)
  427. {
  428. // Set the x,y,z position of the L2Object spawn and update its _worldregion
  429. _isVisible = true;
  430. if (x > L2World.MAP_MAX_X) x = L2World.MAP_MAX_X - 5000;
  431. if (x < L2World.MAP_MIN_X) x = L2World.MAP_MIN_X + 5000;
  432. if (y > L2World.MAP_MAX_Y) y = L2World.MAP_MAX_Y - 5000;
  433. if (y < L2World.MAP_MIN_Y) y = L2World.MAP_MIN_Y + 5000;
  434. getPosition().setWorldPosition(x, y ,z);
  435. getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
  436. // Add the L2Object spawn in the _allobjects of L2World
  437. }
  438. L2World.getInstance().storeObject(this);
  439. // these can synchronize on others instancies, so they're out of
  440. // synchronized, to avoid deadlocks
  441. // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
  442. getPosition().getWorldRegion().addVisibleObject(this);
  443. // Add the L2Object spawn in the world as a visible object
  444. L2World.getInstance().addVisibleObject(this, getPosition().getWorldRegion());
  445. onSpawn();
  446. }
  447. public void toggleVisible()
  448. {
  449. if (isVisible())
  450. decayMe();
  451. else
  452. spawnMe();
  453. }
  454. // =========================================================
  455. // Method - Private
  456. // =========================================================
  457. // Property - Public
  458. public boolean isAttackable()
  459. {
  460. return false;
  461. }
  462. public abstract boolean isAutoAttackable(L2Character attacker);
  463. public boolean isMarker()
  464. {
  465. return false;
  466. }
  467. /**
  468. * Return the visibilty state of the L2Object. <BR><BR>
  469. *
  470. * <B><U> Concept</U> :</B><BR><BR>
  471. * A L2Object is visble if <B>__IsVisible</B>=true and <B>_worldregion</B>!=null <BR><BR>
  472. */
  473. public final boolean isVisible()
  474. {
  475. //return getPosition().getWorldRegion() != null && _IsVisible;
  476. return getPosition().getWorldRegion() != null;
  477. }
  478. public final void setIsVisible(boolean value)
  479. {
  480. _isVisible = value;
  481. if (!_isVisible) getPosition().setWorldRegion(null);
  482. }
  483. public ObjectKnownList getKnownList()
  484. {
  485. return _knownList;
  486. }
  487. /**
  488. * Initializes the KnownList of the L2Object,
  489. * is overwritten in classes that require a different knownlist Type.
  490. *
  491. * Removes the need for instanceof checks.
  492. */
  493. public void initKnownList()
  494. {
  495. _knownList = new ObjectKnownList(this);
  496. }
  497. public final void setKnownList(ObjectKnownList value)
  498. {
  499. _knownList = value;
  500. }
  501. public final String getName()
  502. {
  503. return _name;
  504. }
  505. public final void setName(String value)
  506. {
  507. _name = value;
  508. }
  509. public final int getObjectId()
  510. {
  511. return _objectId;
  512. }
  513. public final ObjectPoly getPoly()
  514. {
  515. if (_poly == null) _poly = new ObjectPoly(this);
  516. return _poly;
  517. }
  518. public ObjectPosition getPosition()
  519. {
  520. return _position;
  521. }
  522. /**
  523. * Initializes the Position class of the L2Object,
  524. * is overwritten in classes that require a different position Type.
  525. *
  526. * Removes the need for instanceof checks.
  527. */
  528. public void initPosition()
  529. {
  530. _position = new ObjectPosition(this);
  531. }
  532. public final void setObjectPosition(ObjectPosition value)
  533. {
  534. _position = value;
  535. }
  536. /**
  537. * returns reference to region this object is in
  538. */
  539. public L2WorldRegion getWorldRegion()
  540. {
  541. return getPosition().getWorldRegion();
  542. }
  543. public L2PcInstance getActingPlayer()
  544. {
  545. return null;
  546. }
  547. /**
  548. * Sends the Server->Client info packet for the object.<br><br>
  549. * Is Overridden in:
  550. * <li>L2AirShipInstance</li>
  551. * <li>L2BoatInstance</li>
  552. * <li>L2DoorInstance</li>
  553. * <li>L2PcInstance</li>
  554. * <li>L2StaticObjectInstance</li>
  555. * <li>L2Decoy</li>
  556. * <li>L2Npc</li>
  557. * <li>L2Summon</li>
  558. * <li>L2Trap</li>
  559. * <li>L2ItemInstance</li>
  560. */
  561. public void sendInfo(L2PcInstance activeChar)
  562. {
  563. }
  564. @Override
  565. public String toString()
  566. {
  567. return (getClass().getSimpleName() + ":"+getName()+"[" + getObjectId() + "]");
  568. }
  569. /**
  570. * Not Implemented.<BR><BR>
  571. *
  572. * <B><U> Overridden in </U> :</B><BR><BR>
  573. * <li> L2PcInstance</li><BR><BR>
  574. */
  575. public void sendPacket(L2GameServerPacket mov)
  576. {
  577. // default implementation
  578. }
  579. }