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

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.oresund;

import gecco.server.core.*;

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

    /**
     *  Description of the Field
     */
    private final static String WATER_EVENT_STRING = "WATER";
    /**
     *  Description of the Field
     */
    private final static String VOLUME_EVENT_STRING = "VOLUME";
    /**
     *  Description of the Field
     */
    private final static int BURNING_TREE_STATE = 2;
    /**
     *  Description of the Field
     */
    private final static int SURVIVOR = 2;

    /**
     *  Description of the Field
     */
    private final static int VOLUME_AMOUNT = 1;

    /**
     *  Description of the Field
     */
    private final static int stepLength = 10;


    /**
     *  Constructor for the Rescue object
     */
    public Rescue() {
        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) {
        super.initiate(_actionHandle, _unitHandle);
    }


    /**
     *  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) {
        throw new IllegalArgumentException("This action must be instanciated without any argument.");
    }


    /**
     *  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 without any argument.");
    }


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

        // Current coordinates are to be found in the Unit-class
        // getArgumentX(),getArgumentY() are the destination coordinates
        // Property FUEL holds the amount of fuel left (Max 100, min 0)

        // Check if there is enough volume.
        double volume = getUnit().getDoubleProperty("Volume");
        if (volume < 1) {
            getUnit().addBlockedAction("Rescue");
            return new ActionReturnError("Unit out of space. ");
        }


        double curX = getUnit().getX();
        double curY = getUnit().getY();

        String unitMessage = "No one here!";
        for (int i = -stepLength; i < stepLength; i++) {
            for (int j = -stepLength; j < stepLength; j++) {

                //Get the current State for the automaton.
                try {
                    int state = getAutomatonState((int) curX + i, (int) curY + j);
                    //Survivors here
                    if (state == SURVIVOR) {
                        volume = getUnit().getDoubleProperty("Volume");
                        if (volume < 1) {
                            getUnit().addBlockedAction("Rescue");
                            return new ActionReturnError("Unit out of space. ");
                        }
                        getUnit().setProperty("Volume", (volume - VOLUME_AMOUNT));
                        sendEventToAutomaton((int) curX + i, (int) curY + j, "Rescue", VOLUME_AMOUNT);
                        unitMessage = "Rescue sucessful";
                    }
                } catch (IllegalArgumentException e) {
                    // Thrown if we try to fetch the state for a non-existing automaton!
                }
            }
        }

        return new ActionReturnCompleted(unitMessage);
    }
}
