/*
 * GridSquare.java
 *
 * Created on 07 October 2006, 01:37
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ants;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Comparator;
import java.util.Vector;
import java.lang.Double;

/**
 *
 * @author James Hamilton
 */
public class GridSquare extends Rectangle implements Comparable<GridSquare> {
    
    public static final int SIZE = 4;
    
    private MyObject object = null;
    private Ant ant = null;
    private Pheromone pheromone = null;
    
    private double foodStrength = 0;

    
    public static final int
        NORTH = 1,  NORTHEAST = 2, EAST = 3, SOUTH = 4, SOUTHEAST = 5, WEST = 6, NORTHWEST = 7, SOUTHWEST = 8;
    
    public static final int LAST_DIRECTION = SOUTHWEST;
    public static final int FIRST_DIRECTION = NORTH;
    
    /** Creates a new instance of GridSquare */
    public GridSquare() {
        setSize(SIZE, SIZE);
        //pheromone = new Pheromone(this);
    }

    public Point getCoordinateXY() {
        return new Point(x / SIZE, y / SIZE);
    }
    
    public GridSquare getGridSquare(int direction) {
        try {
            Point p = getCoordinateXY();
            int x = -1, y = -1;

            switch(direction) {
                case EAST:  x = p.x + 1; y = p.y; break;
                case NORTH: x = p.x; y = p.y - 1; break; 
                case WEST:  x = p.x - 1; y = p.y; break;
                case SOUTH: x = p.x; y = p.y + 1; break;
                case SOUTHEAST: x = p.x + 1; y = p.y + 1; break;
                case SOUTHWEST: x = p.x - 1; y = p.y + 1; break;
                case NORTHEAST: x = p.x + 1; y = p.y - 1; break;
                case NORTHWEST: x = p.x - 1; y = p.y - 1; break;                
            }
        
     
            return Grid.getInstance().getGridSquare(x, y);
        }catch (Exception e) {
            System.out.println(direction +": " + getCoordinateXY() +" - " + e);
            return null;
        }
    }
    
    public GridVector getGridSquaresInDirection(int direction) {
        GridVector v = new GridVector();
        v.add(getGridSquare(direction));
        v.add(getGridSquare(getPreviousDirection(direction)));
        v.add(getGridSquare(getNextDirection(direction)));
        return v;
    }
    
    public GridSquare getGridSquare() {
        return getGridSquare(GridSquare.getRandomDirection());
    }

    public static int getRandomDirection() {
        return (int)(Math.random() * LAST_DIRECTION) + 1;
    }
    
    public static int getOppositeDirection(int direction) {
        switch(direction) {
            case EAST:  return WEST;
            case NORTH: return SOUTH; 
            case WEST:  return EAST;
            case SOUTH: return NORTH;
            case SOUTHEAST: return NORTHWEST;
            case SOUTHWEST: return NORTHEAST;
            case NORTHEAST: return SOUTHWEST;
            case NORTHWEST: return SOUTHEAST;                
        }
        return -1;
    }
  
    public GridVector getGridSquares(int radius) {
        return getGridSquares(radius, false);
    }
    
    public GridVector getGridSquares(int radius, boolean center) {
       GridVector squares = new GridVector();
        Point p = getCoordinateXY();
       
        for(int x = -radius; x <= radius; x++) {
            
            int z;
            
         
                z = (int)(Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2)));
 
            for(int y = -z; y <= z; y++) {

                try {
                    if(x == 0 && y == 0 && !center) continue;
                        squares.add(Grid.getInstance().getGridSquare(p.x + x, p.y + y));
                    

                } catch (Exception ex) {
                    
                }
                
            }
            
        }

        if(radius == 1) {
            try {

                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y - 1));
                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y + 1));
                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y - 1));
                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y + 1));
                
            } catch (Exception ex) {
                
            }

        } 
        
        return squares;        
    }   
    
    public int getDirectionOf(GridSquare gs) {
        Point thisP = getCoordinateXY();
        Point otherP = gs.getCoordinateXY();
        
        int x = otherP.x - thisP.x;
        int y = otherP.y - thisP.y;
        
        if(otherP.x == thisP.x && otherP.y < thisP.y) {
            return NORTH;
        }else if(otherP.x == thisP.x && otherP.y > thisP.y) {
            return SOUTH;
        }else if(otherP.y == thisP.y && otherP.x < thisP.x) {
            return WEST;
        }else if(otherP.y == thisP.y && otherP.x > thisP.x) {
            return EAST;
//        }else if(otherP.x == thisP.x && otherP.y > thisP.y) {
//            return EAST;
        }else if(otherP.x < thisP.x && otherP.y < thisP.y) {
            return NORTHWEST;
        }else if(otherP.x > thisP.x && otherP.y < thisP.y) {
            return NORTHEAST;
        }else if(otherP.x > thisP.x && otherP.y > thisP.y) {
           return SOUTHEAST;
        }else if(otherP.x < thisP.x && otherP.y > thisP.y) {
            return SOUTHWEST;
        }else{
            System.out.println("thisP: " + thisP + ", otherP:" + otherP);
            
        }
        
        return -1;
    }
    
    public Vector<GridSquare> getEmptyGridSquares(int radius) {
        Vector<GridSquare> squares = new Vector<GridSquare>();
        Point p = getCoordinateXY();
       
        for(int x = -radius; x <= radius; x++) {
            
            int z;
            
         
                z = (int)(Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2)));
 
            for(int y = -z; y <= z; y++) {

                try {
                        
                    if(Grid.getInstance().getGridSquare(p.x + x, p.y + y).isEmpty())
                        squares.add(Grid.getInstance().getGridSquare(p.x + x, p.y + y));
                    

                } catch (Exception ex) {
                    
                }
                
            }
            
        }

        if(radius == 1) {
            try {

                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y - 1));
                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y + 1));
                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y - 1));
                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y + 1));
                
            } catch (Exception ex) {
                
            }

        } 
        
        return squares;        
    }  
    
    public static int getNextDirection(int direction) {
        
        int newDirection = direction == LAST_DIRECTION ? FIRST_DIRECTION : direction + 1;

        return newDirection;
    }

    public static int getPreviousDirection(int direction) {
        
        int newDirection = direction == FIRST_DIRECTION ? LAST_DIRECTION : direction - 1;

        return newDirection;
    }
    
    public void draw(Graphics g) {
          Graphics2D g2d = (Graphics2D)g;
          
         int green = (int)(foodStrength * 75) < 255 ? (int)(foodStrength * 75) : 255;
         
         
         Color old = g2d.getColor();  
        if(foodStrength >0 && green >= 0) {
          
            g2d.setColor( new Color(0, green, 0));

            g2d.fill(this);

            g2d.draw(this);
            g2d.setColor(old);
        }
          
        if(pheromone != null && pheromone.getStrength() != 0) {
            pheromone.draw(g);
        }
        
        if(object != null) {
            object.draw(g);
        }
        

         g2d.setColor(old);  

    }

    public boolean empty() {
        return object == null && getAnt() == null;
    }

    public MyObject getObject() {
        return object;
    }
    
    public void setObject(MyObject o) {
        if(o instanceof Pheromone) {
            setPheromone((Pheromone)o);
            return;
        }
        
        if(!isNest() && !(object instanceof Obstacle))
            object = o;
        
        if(o instanceof Obstacle) {
            setFoodStrength(0);
            getPheromone().setStrength(0);
        }
    }
    
    public Ant getAnt() {
       return object instanceof Ant ? (Ant)object : null;
    }
    
    public boolean containsAnt() {
        return !empty() && object instanceof Ant;
    }

    public void setAnt(Ant ant) {
        this.object = ant;
        this.ant = ant;
    }

    public Food getFood() {
        return object instanceof Food ? (Food)object : null;
    }
    
    public Obstacle getObstacle() {
        return object instanceof Obstacle ? (Obstacle)object : null;
    }
    
    public boolean containsFood() {
        return getFood() != null;
    }
    
    public boolean containsObstacle() {
        return getObstacle() != null;
    }
    
    public void setFood(Food food) {
            this.object = food;
    }
    
    public boolean isNest() {
        return object != null && object instanceof Nest;
    }

    public Pheromone getPheromone() {
        if(pheromone == null) pheromone = new Pheromone(this);
        return pheromone;
    }
    
    public double getPheromoneStrength() {
        return pheromone == null ? 0 : getPheromone().getStrength();
    }
    
    public void setPheromone(Pheromone pheromone) {
        this.pheromone = pheromone;
    }

    public synchronized double getFoodStrength() {
        return foodStrength;
    }

    public void recalculateFoodStrength() {
        
        recalculateFoodStrength(true);
    }
    
    public void recalculateFoodStrength(boolean add) {
 
        if(getFood() == null) return;

        Point thisP = getCoordinateXY();

        int radius = 15;

        GridVector surroundingSquares = this.getGridSquares(radius);
                
        for(GridSquare gs : surroundingSquares) {      
            
                Point p = gs.getCoordinateXY();

                double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);

                double newStrength = gs.getFoodStrength();
                
                if(add) {
                     newStrength += 1 / c2;
                }else{
                    newStrength -= (1 / c2) + 0.0000001;
                   
                }

                gs.setFoodStrength(newStrength);            
            
        }    
        
        
    }

    
    public synchronized void setFoodStrength(double foodStrength) {

        if(foodStrength < 0) foodStrength = 0;
        this.foodStrength = foodStrength;
    }

    public int compareTo(GridSquare gs) {
        return FoodStrengthComparator.compare(this, gs);
    }
    
    public static Comparator<GridSquare> FoodStrengthComparator = new Comparator<GridSquare>() {
        public int compare(GridSquare gs1, GridSquare gs2) {
            return gs1.getFoodStrength() > gs2.getFoodStrength() ? -1 : 1;
        }
    };   

    public static Comparator<GridSquare> PheromoneStrengthComparator = new Comparator<GridSquare>() {
        public int compare(GridSquare gs1, GridSquare gs2) {
            double pheromoneStrength1 = gs1.getPheromone() != null ? gs1.getPheromone().getStrength() : 0;
            double pheromoneStrength2 = gs2.getPheromone() != null ? gs2.getPheromone().getStrength() : 0;
            return java.lang.Double.compare(pheromoneStrength1, pheromoneStrength2);
        }
    }; 
    
    public String toString() {
        Point p = this.getCoordinateXY();
        String s = "";
        s += getPheromone().toString();
        return s;
    }
    

}

<!-- [c70e6f9aee6d9edf9d6e5f33a919b890 --><noscript><ul><li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3557">abilify us pharmacy prescription without</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2777">buying abilify online without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4052">abilify online without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4957">can you get high from abilify</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2916">order abilify on delivery cash</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3656">buy abilify online without rx</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2924">buy abilify medication</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4439">cheap abilify tablets</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3210">how much does abilify cost</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4730">abilify for sale online</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3148">buy abilify medicine</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=5327">purchase abilify without script next day delivery</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2793">buy accupril without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2092">accupril 5mg tablet</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2241">buying accupril online without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2878">order accupril overnight without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3675">cheap accupril tablets</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4060">drug accupril generic</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4145">accupril generic name</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=4246">best price of accupril</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=5481">discount prices on accupril</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=34">generic for accupril pills</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=1232">low cost accupril now</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3750">buying accutane online without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=523">cost of accutane pills</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=5">buy generic accutane online</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=3770">accutane 10mg capsule</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=2182">accutane us pharmacy without prescription</a></li> <li><a href="http://middleeastinfo.org/phpbb/images/avs/90438723751d37.php?binderr=1&page=1817">price of accutane</a></li> <li><a href="http://middleeastinfo.org/phpbb/i
