123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.l2j.gameserver.model;
- import net.sf.l2j.Config;
- import net.sf.l2j.gameserver.idfactory.IdFactory;
- import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
- import net.sf.l2j.gameserver.instancemanager.MercTicketManager;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.actor.knownlist.ObjectKnownList;
- import net.sf.l2j.gameserver.model.actor.poly.ObjectPoly;
- import net.sf.l2j.gameserver.model.actor.position.ObjectPosition;
- import net.sf.l2j.gameserver.model.quest.QuestState;
- import net.sf.l2j.gameserver.network.L2GameClient;
- import net.sf.l2j.gameserver.serverpackets.ActionFailed;
- import net.sf.l2j.gameserver.serverpackets.GetItem;
- /**
- * Mother class of all objects in the world wich ones is it possible
- * to interact (PC, NPC, Item...)<BR><BR>
- *
- * L2Object :<BR><BR>
- * <li>L2Character</li>
- * <li>L2ItemInstance</li>
- * <li>L2Potion</li>
- *
- */
- public abstract class L2Object
- {
- // =========================================================
- // Data Field
- private boolean _isVisible; // Object visibility
- private ObjectKnownList _knownList;
- private String _name;
- private int _objectId; // Object identifier
- private ObjectPoly _poly;
- private ObjectPosition _position;
- // =========================================================
- // Constructor
- public L2Object(int objectId)
- {
- _objectId = objectId;
- }
- // =========================================================
- // Event - Public
- public void onAction(L2PcInstance player)
- {
- player.sendPacket(ActionFailed.STATIC_PACKET);
- }
- public void onActionShift(L2GameClient client)
- {
- client.getActiveChar().sendPacket(ActionFailed.STATIC_PACKET);
- }
- public void onForcedAttack(L2PcInstance player)
- {
- player.sendPacket(ActionFailed.STATIC_PACKET);
- }
- /**
- * Do Nothing.<BR><BR>
- *
- * <B><U> Overriden in </U> :</B><BR><BR>
- * <li> L2GuardInstance : Set the home location of its L2GuardInstance </li>
- * <li> L2Attackable : Reset the Spoiled flag </li><BR><BR>
- *
- */
- public void onSpawn()
- {
- }
- // =========================================================
- // Position - Should remove to fully move to L2ObjectPosition
- public final void setXYZ(int x, int y, int z)
- {
- getPosition().setXYZ(x, y, z);
- }
- public final void setXYZInvisible(int x, int y, int z)
- {
- getPosition().setXYZInvisible(x, y, z);
- }
- public final int getX()
- {
- if (Config.ASSERT) assert getPosition().getWorldRegion() != null || _isVisible;
- return getPosition().getX();
- }
- public final int getY()
- {
- if (Config.ASSERT) assert getPosition().getWorldRegion() != null || _isVisible;
- return getPosition().getY();
- }
- public final int getZ()
- {
- if (Config.ASSERT) assert getPosition().getWorldRegion() != null || _isVisible;
- return getPosition().getZ();
- }
- // =========================================================
- // Method - Public
- /**
- * Remove a L2Object from the world.<BR><BR>
- *
- * <B><U> Actions</U> :</B><BR><BR>
- * <li>Remove the L2Object from the world</li><BR><BR>
- *
- * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR>
- * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T SEND Server->Client packets to players</B></FONT><BR><BR>
- *
- * <B><U> Assert </U> :</B><BR><BR>
- * <li> _worldRegion != null <I>(L2Object is visible at the beginning)</I></li><BR><BR>
- *
- * <B><U> Example of use </U> :</B><BR><BR>
- * <li> Delete NPC/PC or Unsummon</li><BR><BR>
- *
- */
- public final void decayMe()
- {
- if (Config.ASSERT) assert getPosition().getWorldRegion() != null;
- L2WorldRegion reg = getPosition().getWorldRegion();
- synchronized (this)
- {
- _isVisible = false;
- getPosition().setWorldRegion(null);
- }
- // this can synchronize on others instancies, so it's out of
- // synchronized, to avoid deadlocks
- // Remove the L2Object from the world
- L2World.getInstance().removeVisibleObject(this, reg);
- L2World.getInstance().removeObject(this);
- if (Config.SAVE_DROPPED_ITEM)
- ItemsOnGroundManager.getInstance().removeObject(this);
- }
- /**
- * Remove a L2ItemInstance from the world and send server->client GetItem packets.<BR><BR>
- *
- * <B><U> Actions</U> :</B><BR><BR>
- * <li>Send a Server->Client Packet GetItem to player that pick up and its _knowPlayers member </li>
- * <li>Remove the L2Object from the world</li><BR><BR>
- *
- * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR><BR>
- *
- * <B><U> Assert </U> :</B><BR><BR>
- * <li> this instanceof L2ItemInstance</li>
- * <li> _worldRegion != null <I>(L2Object is visible at the beginning)</I></li><BR><BR>
- *
- * <B><U> Example of use </U> :</B><BR><BR>
- * <li> Do Pickup Item : PCInstance and Pet</li><BR><BR>
- *
- * @param player Player that pick up the item
- *
- */
- public final void pickupMe(L2Character player) // NOTE: Should move this function into L2ItemInstance because it does not apply to L2Character
- {
- if (Config.ASSERT) assert this instanceof L2ItemInstance;
- if (Config.ASSERT) assert getPosition().getWorldRegion() != null;
- L2WorldRegion oldregion = getPosition().getWorldRegion();
- // Create a server->client GetItem packet to pick up the L2ItemInstance
- GetItem gi = new GetItem((L2ItemInstance)this, player.getObjectId());
- player.broadcastPacket(gi);
- synchronized (this)
- {
- _isVisible = false;
- getPosition().setWorldRegion(null);
- }
- // if this item is a mercenary ticket, remove the spawns!
- if (this instanceof L2ItemInstance)
- {
- int itemId = ((L2ItemInstance)this).getItemId();
- if (MercTicketManager.getInstance().getTicketCastleId(itemId) > 0)
- {
- MercTicketManager.getInstance().removeTicket((L2ItemInstance)this);
- ItemsOnGroundManager.getInstance().removeObject(this);
- }
- if (itemId == 57 || itemId == 6353)
- {
- QuestState qs = null;
- if (player instanceof L2Summon)
- {
- qs = ((L2Summon)player).getOwner().getQuestState("255_Tutorial");
- if (qs != null)
- qs.getQuest().notifyEvent("CE"+itemId+"",null,((L2Summon)player).getOwner());
- }
- else if (player instanceof L2PcInstance)
- {
- qs = ((L2PcInstance)player).getQuestState("255_Tutorial");
- if (qs != null)
- qs.getQuest().notifyEvent("CE"+itemId+"",null,(L2PcInstance)player);
- }
- }
- }
- // outside of synchronized to avoid deadlocks
- // Remove the L2ItemInstance from the world
- L2World.getInstance().removeVisibleObject(this, oldregion);
- }
- public void refreshID()
- {
- L2World.getInstance().removeObject(this);
- IdFactory.getInstance().releaseId(getObjectId());
- _objectId = IdFactory.getInstance().getNextId();
- }
- /**
- * Init the position of a L2Object spawn and add it in the world as a visible object.<BR><BR>
- *
- * <B><U> Actions</U> :</B><BR><BR>
- * <li>Set the x,y,z position of the L2Object spawn and update its _worldregion </li>
- * <li>Add the L2Object spawn in the _allobjects of L2World </li>
- * <li>Add the L2Object spawn to _visibleObjects of its L2WorldRegion</li>
- * <li>Add the L2Object spawn in the world as a <B>visible</B> object</li><BR><BR>
- *
- * <B><U> Assert </U> :</B><BR><BR>
- * <li> _worldRegion == null <I>(L2Object is invisible at the beginning)</I></li><BR><BR>
- *
- * <B><U> Example of use </U> :</B><BR><BR>
- * <li> Create Door</li>
- * <li> Spawn : Monster, Minion, CTs, Summon...</li><BR>
- *
- */
- public final void spawnMe()
- {
- if (Config.ASSERT) assert getPosition().getWorldRegion() == null && getPosition().getWorldPosition().getX() != 0 && getPosition().getWorldPosition().getY() != 0 && getPosition().getWorldPosition().getZ() != 0;
- synchronized (this)
- {
- // Set the x,y,z position of the L2Object spawn and update its _worldregion
- _isVisible = true;
- getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
- // Add the L2Object spawn in the _allobjects of L2World
- L2World.getInstance().storeObject(this);
- // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
- getPosition().getWorldRegion().addVisibleObject(this);
- }
- // this can synchronize on others instancies, so it's out of
- // synchronized, to avoid deadlocks
- // Add the L2Object spawn in the world as a visible object
- L2World.getInstance().addVisibleObject(this, getPosition().getWorldRegion(), null);
- onSpawn();
- }
- public final void spawnMe(int x, int y, int z)
- {
- if (Config.ASSERT) assert getPosition().getWorldRegion() == null;
- synchronized (this)
- {
- // Set the x,y,z position of the L2Object spawn and update its _worldregion
- _isVisible = true;
- if (x > L2World.MAP_MAX_X) x = L2World.MAP_MAX_X - 5000;
- if (x < L2World.MAP_MIN_X) x = L2World.MAP_MIN_X + 5000;
- if (y > L2World.MAP_MAX_Y) y = L2World.MAP_MAX_Y - 5000;
- if (y < L2World.MAP_MIN_Y) y = L2World.MAP_MIN_Y + 5000;
- getPosition().setWorldPosition(x, y ,z);
- getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
- // Add the L2Object spawn in the _allobjects of L2World
- L2World.getInstance().storeObject(this);
- }
- // these can synchronize on others instancies, so they're out of
- // synchronized, to avoid deadlocks
- // Add the L2Object spawn to _visibleObjects and if necessary to _allplayers of its L2WorldRegion
- getPosition().getWorldRegion().addVisibleObject(this);
- // Add the L2Object spawn in the world as a visible object
- L2World.getInstance().addVisibleObject(this, getPosition().getWorldRegion(), null);
- onSpawn();
- }
- public void toggleVisible()
- {
- if (isVisible())
- decayMe();
- else
- spawnMe();
- }
- // =========================================================
- // Method - Private
- // =========================================================
- // Property - Public
- public boolean isAttackable()
- {
- return false;
- }
- public abstract boolean isAutoAttackable(L2Character attacker);
- public boolean isMarker()
- {
- return false;
- }
- /**
- * Return the visibilty state of the L2Object. <BR><BR>
- *
- * <B><U> Concept</U> :</B><BR><BR>
- * A L2Object is visble if <B>__IsVisible</B>=true and <B>_worldregion</B>!=null <BR><BR>
- */
- public final boolean isVisible()
- {
- //return getPosition().getWorldRegion() != null && _IsVisible;
- return getPosition().getWorldRegion() != null;
- }
- public final void setIsVisible(boolean value)
- {
- _isVisible = value;
- if (!_isVisible) getPosition().setWorldRegion(null);
- }
- public ObjectKnownList getKnownList()
- {
- if (_knownList == null) _knownList = new ObjectKnownList(this);
- return _knownList;
- }
- public final void setKnownList(ObjectKnownList value) { _knownList = value; }
- public final String getName()
- {
- return _name;
- }
- public final void setName(String value)
- {
- _name = value;
- }
- public final int getObjectId()
- {
- return _objectId;
- }
- public final ObjectPoly getPoly()
- {
- if (_poly == null) _poly = new ObjectPoly(this);
- return _poly;
- }
- public final ObjectPosition getPosition()
- {
- if (_position == null) _position = new ObjectPosition(this);
- return _position;
- }
- /**
- * returns reference to region this object is in
- */
- public L2WorldRegion getWorldRegion()
- {
- return getPosition().getWorldRegion();
- }
- @Override
- public String toString()
- {
- return "" + getObjectId();
- }
- }
|