/*
 * Copyright (C) 2004-2013 L2J DataPack
 * 
 * This file is part of L2J DataPack.
 * 
 * L2J DataPack 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.
 * 
 * L2J DataPack 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 handlers.effecthandlers;

import com.l2jserver.Config;
import com.l2jserver.gameserver.GeoData;
import com.l2jserver.gameserver.ai.CtrlIntention;
import com.l2jserver.gameserver.model.Location;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.effects.EffectTemplate;
import com.l2jserver.gameserver.model.effects.L2Effect;
import com.l2jserver.gameserver.model.effects.L2EffectType;
import com.l2jserver.gameserver.model.stats.Env;
import com.l2jserver.gameserver.network.serverpackets.FlyToLocation;
import com.l2jserver.gameserver.network.serverpackets.FlyToLocation.FlyType;
import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
import com.l2jserver.gameserver.util.Util;

/**
 * This class handles warp effects, disappear and quickly turn up in a near location. If geodata enabled and an object is between initial and final point, flight is stopped just before colliding with object. Flight course and radius are set as skill properties (flyCourse and flyRadius): <li>Fly
 * Radius means the distance between starting point and final point, it must be an integer.</li> <li>Fly Course means the movement direction: imagine a compass above player's head, making north player's heading. So if fly course is 180, player will go backwards (good for blink, e.g.). By the way, if
 * flyCourse = 360 or 0, player will be moved in in front of him. <br>
 * <br>
 * If target is effector, put in XML self = "1". This will make _actor = getEffector(). This, combined with target type, allows more complex actions like flying target's backwards or player's backwards.<br>
 * <br>
 * @author House
 */
public class Warp extends L2Effect
{
	public Warp(Env env, EffectTemplate template)
	{
		super(env, template);
	}
	
	@Override
	public L2EffectType getEffectType()
	{
		return L2EffectType.WARP;
	}
	
	@Override
	public boolean onStart()
	{
		final L2Character actor = isSelfEffect() ? getEffector() : getEffected();
		
		/*
		 * if (_actor.isMovementDisabled()) This is removed because in retail you can use Warp while movement is disabled. return false;
		 */
		
		// TODO: check if this AI intention is retail-like. This stops player's
		// previous movement
		actor.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
		actor.abortAttack();
		actor.abortCast();
		
		final int t = actor.getHeading();
		final int u = actor.getX();
		final int v = actor.getY();
		final int w = actor.getZ();
		
		final double angle = Math.toRadians(180 + Util.convertHeadingToDegree(t) + getSkill().getFlyCourse());
		final double radius = getSkill().getFlyRadius();
		
		int x = u + (int) (Math.cos(angle) * radius);
		int y = v + (int) (Math.sin(angle) * radius);
		int z = w;
		
		if (Config.GEODATA > 0)
		{
			Location destiny = GeoData.getInstance().moveCheck(u, v, w, x, y, z, actor.getInstanceId());
			x = destiny.getX();
			y = destiny.getY();
			z = destiny.getZ();
		}
		
		actor.setXYZ(x, y, z);
		actor.broadcastPacket(new ValidateLocation(actor));
		actor.broadcastPacket(new FlyToLocation(actor.getObjectId(), u, v, w, x, y, z, FlyType.DUMMY));
		return true;
	}
	
	@Override
	public boolean onActionTime()
	{
		return false;
	}
}
