To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.1/src/ants/GridSquare.java. This is a spam-protection measure; sorry for the inconvenience.

· GridSquare.java ·

   1/*
   2 * GridSquare.java
   3 *
   4 * Created on 07 October 2006, 01:37
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package ants;
  11
  12import java.awt.Color;
  13import java.awt.Graphics;
  14import java.awt.Graphics2D;
  15import java.awt.Point;
  16import java.awt.Rectangle;
  17import java.util.Comparator;
  18import java.util.Vector;
  19import java.lang.Double;
  20
  21/**
  22 *
  23 * @author James Hamilton
  24 */
  25public class GridSquare extends Rectangle implements Comparable<GridSquare> {
  26    
  27    public static final int SIZE = 4;
  28    
  29    private MyObject object = null;
  30    private Ant ant = null;
  31    private Pheromone pheromone = null;
  32    
  33    private double foodStrength = 0;
  34
  35    
  36    public static final int
  37        NORTH = 1,  NORTHEAST = 2, EAST = 3, SOUTH = 4, SOUTHEAST = 5, WEST = 6, NORTHWEST = 7, SOUTHWEST = 8;
  38    
  39    public static final int LAST_DIRECTION = SOUTHWEST;
  40    public static final int FIRST_DIRECTION = NORTH;
  41    
  42    /** Creates a new instance of GridSquare */
  43    public GridSquare() {
  44        setSize(SIZE, SIZE);
  45        //pheromone = new Pheromone(this);
  46    }
  47
  48    public Point getCoordinateXY() {
  49        return new Point(x / SIZE, y / SIZE);
  50    }
  51    
  52    public GridSquare getGridSquare(int direction) {
  53        try {
  54            Point p = getCoordinateXY();
  55            int x = -1, y = -1;
  56
  57            switch(direction) {
  58                case EAST:  x = p.x + 1; y = p.y; break;
  59                case NORTH: x = p.x; y = p.y - 1; break; 
  60                case WEST:  x = p.x - 1; y = p.y; break;
  61                case SOUTH: x = p.x; y = p.y + 1; break;
  62                case SOUTHEAST: x = p.x + 1; y = p.y + 1; break;
  63                case SOUTHWEST: x = p.x - 1; y = p.y + 1; break;
  64                case NORTHEAST: x = p.x + 1; y = p.y - 1; break;
  65                case NORTHWEST: x = p.x - 1; y = p.y - 1; break;                
  66            }
  67        
  68     
  69            return Grid.getInstance().getGridSquare(x, y);
  70        }catch (Exception e) {
  71            System.out.println(direction +": " + getCoordinateXY() +" - " + e);
  72            return null;
  73        }
  74    }
  75    
  76    public GridVector getGridSquaresInDirection(int direction) {
  77        GridVector v = new GridVector();
  78        v.add(getGridSquare(direction));
  79        v.add(getGridSquare(getPreviousDirection(direction)));
  80        v.add(getGridSquare(getNextDirection(direction)));
  81        return v;
  82    }
  83    
  84    public GridSquare getGridSquare() {
  85        return getGridSquare(GridSquare.getRandomDirection());
  86    }
  87
  88    public static int getRandomDirection() {
  89        return (int)(Math.random() * LAST_DIRECTION) + 1;
  90    }
  91    
  92    public static int getOppositeDirection(int direction) {
  93        switch(direction) {
  94            case EAST:  return WEST;
  95            case NORTH: return SOUTH; 
  96            case WEST:  return EAST;
  97            case SOUTH: return NORTH;
  98            case SOUTHEAST: return NORTHWEST;
  99            case SOUTHWEST: return NORTHEAST;
 100            case NORTHEAST: return SOUTHWEST;
 101            case NORTHWEST: return SOUTHEAST;                
 102        }
 103        return -1;
 104    }
 105  
 106    public GridVector getGridSquares(int radius) {
 107        return getGridSquares(radius, false);
 108    }
 109    
 110    public GridVector getGridSquares(int radius, boolean center) {
 111       GridVector squares = new GridVector();
 112        Point p = getCoordinateXY();
 113       
 114        for(int x = -radius; x <= radius; x++) {
 115            
 116            int z;
 117            
 118         
 119                z = (int)(Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2)));
 120 
 121            for(int y = -z; y <= z; y++) {
 122
 123                try {
 124                    if(x == 0 && y == 0 && !center) continue;
 125                        squares.add(Grid.getInstance().getGridSquare(p.x + x, p.y + y));
 126                    
 127
 128                } catch (Exception ex) {
 129                    
 130                }
 131                
 132            }
 133            
 134        }
 135
 136        if(radius == 1) {
 137            try {
 138
 139                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y - 1));
 140                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y + 1));
 141                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y - 1));
 142                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y + 1));
 143                
 144            } catch (Exception ex) {
 145                
 146            }
 147
 148        } 
 149        
 150        return squares;        
 151    }   
 152    
 153    public int getDirectionOf(GridSquare gs) {
 154        Point thisP = getCoordinateXY();
 155        Point otherP = gs.getCoordinateXY();
 156        
 157        int x = otherP.x - thisP.x;
 158        int y = otherP.y - thisP.y;
 159        
 160        if(otherP.x == thisP.x && otherP.y < thisP.y) {
 161            return NORTH;
 162        }else if(otherP.x == thisP.x && otherP.y > thisP.y) {
 163            return SOUTH;
 164        }else if(otherP.y == thisP.y && otherP.x < thisP.x) {
 165            return WEST;
 166        }else if(otherP.y == thisP.y && otherP.x > thisP.x) {
 167            return EAST;
 168//        }else if(otherP.x == thisP.x && otherP.y > thisP.y) {
 169//            return EAST;
 170        }else if(otherP.x < thisP.x && otherP.y < thisP.y) {
 171            return NORTHWEST;
 172        }else if(otherP.x > thisP.x && otherP.y < thisP.y) {
 173            return NORTHEAST;
 174        }else if(otherP.x > thisP.x && otherP.y > thisP.y) {
 175           return SOUTHEAST;
 176        }else if(otherP.x < thisP.x && otherP.y > thisP.y) {
 177            return SOUTHWEST;
 178        }else{
 179            System.out.println("thisP: " + thisP + ", otherP:" + otherP);
 180            
 181        }
 182        
 183        return -1;
 184    }
 185    
 186    public Vector<GridSquare> getEmptyGridSquares(int radius) {
 187        Vector<GridSquare> squares = new Vector<GridSquare>();
 188        Point p = getCoordinateXY();
 189       
 190        for(int x = -radius; x <= radius; x++) {
 191            
 192            int z;
 193            
 194         
 195                z = (int)(Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2)));
 196 
 197            for(int y = -z; y <= z; y++) {
 198
 199                try {
 200                        
 201                    if(Grid.getInstance().getGridSquare(p.x + x, p.y + y).isEmpty())
 202                        squares.add(Grid.getInstance().getGridSquare(p.x + x, p.y + y));
 203                    
 204
 205                } catch (Exception ex) {
 206                    
 207                }
 208                
 209            }
 210            
 211        }
 212
 213        if(radius == 1) {
 214            try {
 215
 216                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y - 1));
 217                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y + 1));
 218                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y - 1));
 219                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y + 1));
 220                
 221            } catch (Exception ex) {
 222                
 223            }
 224
 225        } 
 226        
 227        return squares;        
 228    }  
 229    
 230    public static int getNextDirection(int direction) {
 231        
 232        int newDirection = direction == LAST_DIRECTION ? FIRST_DIRECTION : direction + 1;
 233
 234        return newDirection;
 235    }
 236
 237    public static int getPreviousDirection(int direction) {
 238        
 239        int newDirection = direction == FIRST_DIRECTION ? LAST_DIRECTION : direction - 1;
 240
 241        return newDirection;
 242    }
 243    
 244    public void draw(Graphics g) {
 245          Graphics2D g2d = (Graphics2D)g;
 246          
 247         int green = (int)(foodStrength * 75) < 255 ? (int)(foodStrength * 75) : 255;
 248         
 249         
 250         Color old = g2d.getColor();  
 251        if(foodStrength >0 && green >= 0) {
 252          
 253            g2d.setColor( new Color(0, green, 0));
 254
 255            g2d.fill(this);
 256
 257            g2d.draw(this);
 258            g2d.setColor(old);
 259        }
 260          
 261        if(pheromone != null && pheromone.getStrength() != 0) {
 262            pheromone.draw(g);
 263        }
 264        
 265        if(object != null) {
 266            object.draw(g);
 267        }
 268        
 269
 270         g2d.setColor(old);  
 271
 272    }
 273
 274    public boolean empty() {
 275        return object == null && getAnt() == null;
 276    }
 277
 278    public MyObject getObject() {
 279        return object;
 280    }
 281    
 282    public void setObject(MyObject o) {
 283        if(o instanceof Pheromone) {
 284            setPheromone((Pheromone)o);
 285            return;
 286        }
 287        
 288        if(!isNest() && !(object instanceof Obstacle))
 289            object = o;
 290        
 291        if(o instanceof Obstacle) {
 292            setFoodStrength(0);
 293            getPheromone().setStrength(0);
 294        }
 295    }
 296    
 297    public Ant getAnt() {
 298       return object instanceof Ant ? (Ant)object : null;
 299    }
 300    
 301    public boolean containsAnt() {
 302        return !empty() && object instanceof Ant;
 303    }
 304
 305    public void setAnt(Ant ant) {
 306        this.object = ant;
 307        this.ant = ant;
 308    }
 309
 310    public Food getFood() {
 311        return object instanceof Food ? (Food)object : null;
 312    }
 313    
 314    public Obstacle getObstacle() {
 315        return object instanceof Obstacle ? (Obstacle)object : null;
 316    }
 317    
 318    public boolean containsFood() {
 319        return getFood() != null;
 320    }
 321    
 322    public boolean containsObstacle() {
 323        return getObstacle() != null;
 324    }
 325    
 326    public void setFood(Food food) {
 327            this.object = food;
 328    }
 329    
 330    public boolean isNest() {
 331        return object != null && object instanceof Nest;
 332    }
 333
 334    public Pheromone getPheromone() {
 335        if(pheromone == null) pheromone = new Pheromone(this);
 336        return pheromone;
 337    }
 338    
 339    public double getPheromoneStrength() {
 340        return pheromone == null ? 0 : getPheromone().getStrength();
 341    }
 342    
 343    public void setPheromone(Pheromone pheromone) {
 344        this.pheromone = pheromone;
 345    }
 346
 347    public synchronized double getFoodStrength() {
 348        return foodStrength;
 349    }
 350
 351    public void recalculateFoodStrength() {
 352        
 353        recalculateFoodStrength(true);
 354    }
 355    
 356    public void recalculateFoodStrength(boolean add) {
 357 
 358        if(getFood() == null) return;
 359
 360        Point thisP = getCoordinateXY();
 361
 362        int radius = 15;
 363
 364        GridVector surroundingSquares = this.getGridSquares(radius);
 365                
 366        for(GridSquare gs : surroundingSquares) {      
 367            
 368                Point p = gs.getCoordinateXY();
 369
 370                double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
 371
 372                double newStrength = gs.getFoodStrength();
 373                
 374                if(add) {
 375                     newStrength += 1 / c2;
 376                }else{
 377                    newStrength -= (1 / c2) + 0.0000001;
 378                   
 379                }
 380
 381                gs.setFoodStrength(newStrength);            
 382            
 383        }    
 384        
 385        
 386    }
 387
 388    
 389    public synchronized void setFoodStrength(double foodStrength) {
 390
 391        if(foodStrength < 0) foodStrength = 0;
 392        this.foodStrength = foodStrength;
 393    }
 394
 395    public int compareTo(GridSquare gs) {
 396        return FoodStrengthComparator.compare(this, gs);
 397    }
 398    
 399    public static Comparator<GridSquare> FoodStrengthComparator = new Comparator<GridSquare>() {
 400        public int compare(GridSquare gs1, GridSquare gs2) {
 401            return gs1.getFoodStrength() > gs2.getFoodStrength() ? -1 : 1;
 402        }
 403    };   
 404
 405    public static Comparator<GridSquare> PheromoneStrengthComparator = new Comparator<GridSquare>() {
 406        public int compare(GridSquare gs1, GridSquare gs2) {
 407            double pheromoneStrength1 = gs1.getPheromone() != null ? gs1.getPheromone().getStrength() : 0;
 408            double pheromoneStrength2 = gs2.getPheromone() != null ? gs2.getPheromone().getStrength() : 0;
 409            return java.lang.Double.compare(pheromoneStrength1, pheromoneStrength2);
 410        }
 411    }; 
 412    
 413    public String toString() {
 414        Point p = this.getCoordinateXY();
 415        String s = "";
 416        s += getPheromone().toString();
 417        return s;
 418    }
 419    
 420
 421}
 422

· GridSquare.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.