/* CVS: $Id: GameBoard.java,v 1.20 2001/03/16 11:20:57 gvijf Exp $ */

package evolution;

import java.awt.Graphics;
import java.util.*;
import java.lang.*;

import evolution.lands.SquareOfLand;
import evolution.lands.IllegalPlacementException;
import evolution.constructions.Construction;
import evolution.resources.*;

/**
 * The GameBoard keeps tracks of all the SquareOfLand objects.
 */
public class GameBoard {

    /**
     * Create a new random gameboard.
     */
    public static GameBoard create(int width, int height) {
        GameBoard gameBoard = new GameBoard(width, height);
        for(int x = 0; x < width; x++) {
            for(int y = 0; y < height; y++) {
	            gameBoard.squares[x][y] = new SquareOfLand(x, y, false);
            }
        }
        // create an explored square in the middle of the gameboard
		gameBoard.squares[width/2][height/2] = new SquareOfLand(width/2, height/2, true);
        return gameBoard;
    }

    /**
     * Create a new *empty* gameboard with the given size.
     */
    protected GameBoard(int width, int height) {
        this.width = width;
        this.height = height;
        squares = new SquareOfLand[width][height];
        this.constructionList = new ArrayList();
    }

    /**
     * Place a human on the given spot.
     * @exception IllegalPlacementException If the human is not allowed
     * to stand on this piece of land.
     */
    public void place(int x, int y, Human human) throws IllegalPlacementException {
        squares[x][y].place(human);
    }

    /**
     * Get the square at the given position.
     */
    public SquareOfLand getSquare(int x, int y) {
        return squares[x][y];
    }

    /**
     * Get the surrounding squares in the range of a certain position.
     * @param explored: only add explored squares if this is true.
     */
    public List getSurroundingSquares(SquareOfLand sq, int range, boolean explored) {
        List list = new ArrayList();
        int i1 = Math.max(sq.getX() - range, 0);
        int i2 = Math.min(sq.getX() + range, World.getInst().getWidth() - 1);
        int j1 = Math.max(0, sq.getY() - range);
        int j2 = Math.min(sq.getY() + range, World.getInst().getHeight() - 1);
         for(int i = i1; i <= i2; i++) {
            for(int j = j1; j <= j2; j++) {
                if (explored && getSquare(i, j).isExplored())
	 	            list.add(getSquare(i, j));
                else list.add(getSquare(i, j));
            }
         }
         list.remove(sq);
         return list;
    }

    /**
     * Get the width of the gameboard.
     */
    public int getWidth() {
        return width;
    }

    /**
     * Get the height of the gameboard.
     */
    public int getHeight() {
        return height;
    }

    /**
     * Get the constructionlist of the gameboard.
     */
    public List getConstructionList() {
        return constructionList;
    }

    /**
     * Return the maximum energy production of all the constructions on the gameboard.
     */
    /*public double maxEnergyProduction() {
        List list = getConstructionList();
        Iterator it = list.iterator();
        double result = 0;
        while (it.hasNext()) {
            Construction con = (Construction) it.next();
            List energyproducers = World.getInst().getProducersNames("Energy");
            result = result + con.getMaxEnergyProduction(energyproducers);
        }
        return result;
    } */


    /**
     * The width of the gameboard
     */
    private int width;

    /**
     * The height of the gameboard
     */
    private int height;

    /**
     * @associates <{evolution.lands.SquareOfLand}> 
     * @link aggregation
     */
    private SquareOfLand[][] squares;

    /**
     * A list of all the completed constructions the squaresOfLand of this Gameboard contain.
     */
    private List constructionList;
}
