package net.sf.l2j.gameserver.model.actor.instance;
import java.util.concurrent.Future;
import net.sf.l2j.commons.pool.ThreadPool;
import net.sf.l2j.commons.random.Rnd;
import net.sf.l2j.gameserver.model.actor.Npc;
import net.sf.l2j.gameserver.model.actor.Player;
import net.sf.l2j.gameserver.model.actor.template.NpcTemplate;
import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
import net.sf.l2j.gameserver.model.location.Location;
import net.sf.l2j.gameserver.network.serverpackets.PetDelete;
/**
* @author maxi5
*/
public class Agathion extends Npc
{
private Player owner;
private Future<?> _followTask;
private ItemInstance item;
public Agathion(int objectId, NpcTemplate template, Player owner)
{
super(objectId, template);
// Set the magical circle animation.
setShowSummonAnimation(true);
// Set the Player owner.
this.owner = owner;
}
@Override
public void onSpawn()
{
disableCoreAi(true);
setSpawnLocation(getPosition());
followToOwner();
super.onSpawn();
}
public void unSummon(Player owner)
{
// Abort attack, cast and move.
abortAll(true);
if (item != null)
item.setAgathion(owner, false);
owner.sendPacket(new PetDelete(2, getObjectId()));
owner.setAgathion(null);
if (owner.getSummon() != null)
owner.getSummon().sendInfo(owner);
owner.broadcastUserInfo();
if (_followTask != null)
_followTask.cancel(true);
decayMe();
deleteMe();
super.deleteMe();
}
public void followToOwner()
{
if (owner == null)
return;
setTarget(owner);
if (_followTask == null)
_followTask = ThreadPool.scheduleAtFixedRate(new Follow(this), 1000, 1000);
getPosition().setHeading(owner.getHeading());
int rnd = Rnd.get(-15, +15);
Location toMoveLoc = new Location(owner.getX(), owner.getY(), owner.getZ());
if (!isIn2DRadius(owner, 2000))
{
teleportTo(owner.getPosition().clone(), rnd);
return;
}
if (!isIn2DRadius(owner, 50) && !isMoving())
{
getMove().maybeMoveToLocation(toMoveLoc, rnd, true, false);
return;
}
if (isIn2DRadius(owner, 30))
{
getMove().stop();
getAI().thinkIdle();
return;
}
getMove().maybeMoveToLocation(toMoveLoc, rnd, true, false);
}
@Override
public boolean isInMyTerritory()
{
return false;
}
private static class Follow implements Runnable
{
private final Agathion agathion;
protected Follow(Agathion aga)
{
agathion = aga;
}
@Override
public void run()
{
agathion.followToOwner();
}
}
public void setAgathionItem(ItemInstance item)
{
this.item = item;
}
public Player getOwner()
{
return owner;
}
}