/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.game.testgame;

import gecco.server.core.*;

/**
 *  Description of the Class
 *
 */
public class AttackAction extends Action {

    /**
     *  Description of the Field
     */
    private final int NONE_BEHAVIOUR = 0;
    /**
     *  Description of the Field
     */
    private final int ATTACKING_BEHAVIOUR = 1;
    /**
     *  Description of the Field
     */
    private final int MOVEMENT_BEHAVIOUR = 2;
    /**
     *  Description of the Field
     */
    private double addX;
    /**
     *  Description of the Field
     */
    private double addY;
    /**
     *  Description of the Field
     */
    private double stepLength;
    /**
     *  Description of the Field
     */
    private int attackFactor;
    /**
     *  Description of the Field
     */
    private int behaviourState = NONE_BEHAVIOUR;

    /**
     *  Description of the Field
     */
    private double attackRadiusSquared;


    /**
     *  Constructor for the AttackAction object
     */
    public AttackAction() {
        super();
    }


    // Override superclass methods

    /**
     *  Description of the Method
     *
     * @param  _actionHandle  Description of Parameter
     * @param  _unitHandle    Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle) {
        throw new IllegalArgumentException("This action must be instanciated with a point as an argument.");
    }


    /**
     *  Description of the Method
     *
     * @param  _actionHandle   Description of Parameter
     * @param  _unitHandle     Description of Parameter
     * @param  _argUnitHandle  Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle, int _argUnitHandle) {
        super.initiate(_actionHandle, _unitHandle, _argUnitHandle);
        // Set the attack factor and stepLength depending on which unittype
        // that is attacking!
        if (getUnit().getUnitType().getName().endsWith("Helicopter")) {
            stepLength = 2.0;
            attackFactor = 1;
            attackRadiusSquared = 35 * 35;
        } else if (getUnit().getUnitType().getName().endsWith("Tank")) {
            stepLength = 0.9;
            attackFactor = 3;
            attackRadiusSquared = 25 * 25;
        } else {
            // Unknown, set default values
            stepLength = 1;
            attackFactor = 3;
        }
    }


    /**
     *  Description of the Method
     *
     * @param  _actionHandle  Description of Parameter
     * @param  _unitHandle    Description of Parameter
     * @param  _argX          Description of Parameter
     * @param  _argY          Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle, double _argX, double _argY) {
        throw new IllegalArgumentException("This action must be instanciated with a unit as an argument.");
    }


    /**
     *  Description of the Method
     *
     * @return    Description of the Returned Value
     */
    public ActionReturn checkPoint() {

        if (getArgumentUnit().isDestroyed()) {
            return new ActionReturnCompleted("Attack successful! Target destroyed!");
        }

        String unitMessage = null;

        double curX = getUnit().getX();
        double curY = getUnit().getY();
        double enemyX = getArgumentUnit().getX();
        double enemyY = getArgumentUnit().getY();

        // Check if we're inside the attack radius!
        double dx = enemyX - curX;
        double dy = enemyY - curY;
        double dist = dx * dx + dy * dy;

        // Check if unit is visible
        if (!canSeePosition(enemyX, enemyY)) {
            return new ActionReturnError("Target lost, aborting attack!");
        } else if (dx * dx + dy * dy <= attackRadiusSquared) {
            // YES! ATTACK!
            sendEventToUnit(getArgumentUnit(), "ATTACK", 10);
            if (behaviourState != ATTACKING_BEHAVIOUR) {
                unitMessage = "Target is withing range! Attacking!";
                behaviourState = ATTACKING_BEHAVIOUR;
            }
        } else {
            // Move towards unit
            // Check if there is enough fuel to move!
            if (behaviourState != MOVEMENT_BEHAVIOUR) {
                unitMessage = "Target is too far away range! Closing in!";
                behaviourState = MOVEMENT_BEHAVIOUR;
            }
            double fuel = getUnit().getDoubleProperty("Fuel");
            if (fuel < 10) {
                if (getUnit().getUnitType().getName().endsWith("Helicopter")) {
                    getUnit().addBlockedAction("Fly");
                } else {
                    getUnit().addBlockedAction("Move");
                }
                return new ActionReturnError("Unit out of fuel - cannot move towards target. Attack aborted!");
            }
            fuel -= 1;
            getUnit().setProperty("Fuel", fuel);

            double sqrt = Math.sqrt(dist);
            addX = dx * stepLength / sqrt;
            addY = dy * stepLength / sqrt;

            curX = curX + addX;
            curY = curY + addY;

            // See if we're moving over an automaton in a fire state!
            // (If the attacking unit isn't a helicopter!)
            if (!getUnit().getUnitType().getName().endsWith("Helicopter") &&
                    getAutomatonState((int) curX, (int) curY) == 2) {
                // FIRE! DECREASE HEALTH!
                double health = getUnit().getDoubleProperty("Health");
                getUnit().setProperty("Health", health - 5);
            }

            // Move! Set coordinates, we're not done yet, requeue!
            getUnit().setPosition(curX, curY);
        }

        return new ActionReturnRequeue(0.3, unitMessage);
    }
}
