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

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.automaton.*;

import gecco.server.core.UnitEvent;

/**
 *  Description of the Class
 *
 */
public class AutomatonImpl extends Automaton {

    /**
     *  Description of the Field
     */
    final static String FIRE = "FIRE";


    /**
     *  Gets the UnitEventForCurrentState attribute of the AutomatonImpl object
     *
     * @param  unitType  Description of Parameter
     * @return           The UnitEventForCurrentState value
     */
    public UnitEvent getUnitEventForCurrentState(String unitType) {
        if (getState() != 2) {
            return null;
        }
        return new UnitEvent("FIRE", 10);
    }


    /**
     *  Description of the Method
     *
     * @param  initialState  Description of Parameter
     */
    public void initialize(int initialState) {
        super.initialize(initialState);
        setColor(initialState);
    }


    /**
     *  Description of the Method
     *
     * @param  neighourStates  Description of Parameter
     * @return                 Description of the Returned Value
     */
    public AutomatonReturn update(int[][] neighourStates) {
        switch (getState()) {
            case 1:
                // Icke-brinnande träd...
                if (checkNeighbourstatesForState(neighourStates, 2)) {
                    if (Math.random() < 0.75) {
                        setState(2);
                        setColor(2);
                    }
                    return new AutomatonReturnQueueSelfAndNeighbours(2.0, 2.0, new UnitEvent("FIRE", 10));
                }
                break;
            case 2:
                // Brinnande träd...
                // 10% slh att vi brunnit färdigt...
                if (Math.random() > 0.1) {
                    // Har inte brunnit klart, kasta in grannar och själv i kön...
                    return fireReturn(neighourStates);
                }
                setState(3);
                setColor(3);
                break;
            case 3:
                // Nedbränt träd...
                // Do nothing...
                break;
            case 4:
                // Berg
                break;
            case 5:
                // Mark
                break;
            case 6:
                // Vatten
                break;
            default:
            // Strange...
        }
        return new AutomatonReturnNoAction();
    }


    /**
     *  Description of the Method
     *
     * @param  event  Description of Parameter
     * @return        Description of the Returned Value
     */
    public AutomatonReturn handleEvent(AutomatonEvent event) {
        if (event.getEventName().equals(FIRE)) {
            // DET BRINNER! YEEEEHHHAAAAA!!!
            if (getState() == 1) {
                // Jag är ett träd och börjar nu brinna!
                setColor(2);
                setState(2);
                return new AutomatonReturnQueueSelfAndNeighbours(3.0, 2.0, new UnitEvent("FIRE", 10));
            }
        }
        return new AutomatonReturnNoAction();
    }


    /**
     *  Sets the Color attribute of the AutomatonImpl object
     *
     * @param  state  The new Color value
     */
    private void setColor(int state) {
        switch (state) {
            case 1:
                // Icke-brinnande träd...
                setCurrentColor(10, 170, 10);
                break;
            case 2:
                // Brinnande träd...
                setCurrentColor(250, 90, 0);
                break;
            case 3:
                // Nedbränt träd...
                setCurrentColor(20, 45, 20);
                break;
            case 4:
                // Berg
                setCurrentColor(180, 180, 180);
                break;
            case 5:
                // Mark
                setCurrentColor(120, 100, 75);
                break;
            case 6:
                // Vatten
                setCurrentColor(30, 30, 200);
                break;
            default:
                // Odefinierat
                setCurrentColor(200, 0, 0);
                break;
        }
    }


    /**
     *  Description of the Method
     *
     * @param  neighbourStates  Description of Parameter
     * @return                  Description of the Returned Value
     */
    private AutomatonReturn fireReturn(int[][] neighbourStates) {
        // Kasta in alla grannar som inte redan brinner, och kasta in sig själv
        // för uppdatering om en liten stund.
        double[][] theArray = new double[3][3];
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                int nState = neighbourStates[x][y];
                if (nState == 1) {
                    // Icke-brinnande träd, ska börja brinna
                    theArray[x][y] = 1.0 + Math.random() * 2;
                } else {
                    // Ska ej börja brinna
                    theArray[x][y] = -1.0;
                }
            }
        }
        // Se till att automaten själv kastas in i kön
        theArray[1][1] = 1.5 + Math.random() * 2;
        return new AutomatonReturnQueueSelective(theArray, new UnitEvent("FIRE", 10));
    }


    /**
     *  Description of the Method
     *
     * @param  neighbourStates  Description of Parameter
     * @param  checkState       Description of Parameter
     * @return                  Description of the Returned Value
     */
    private boolean checkNeighbourstatesForState(int[][] neighbourStates, int checkState) {
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                if (neighbourStates[x][y] == checkState) {
                    return true;
                }
            }
        }
        return false;
    }
}
