To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.0/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(1,1);
  70            return Grid.getInstance().getGridSquare(x, y);
  71     //   return null;
  72        }catch (Exception e) {
  73            System.out.println(direction +": " + getCoordinateXY() +" - " + e);
  74            return null;
  75        }
  76    }
  77    
  78    public GridVector getGridSquaresInDirection(int direction) {
  79        GridVector v = new GridVector();
  80        v.add(getGridSquare(direction));
  81        v.add(getGridSquare(getPreviousDirection(direction)));
  82        v.add(getGridSquare(getNextDirection(direction)));
  83        return v;
  84    }
  85    
  86    public GridSquare getGridSquare() {
  87        return getGridSquare(GridSquare.getRandomDirection());
  88    }
  89
  90    public static int getRandomDirection() {
  91        return (int)(Math.random() * LAST_DIRECTION) + 1;
  92    }
  93    
  94    public static int getOppositeDirection(int direction) {
  95        switch(direction) {
  96            case EAST:  return WEST;
  97            case NORTH: return SOUTH; 
  98            case WEST:  return EAST;
  99            case SOUTH: return NORTH;
 100            case SOUTHEAST: return NORTHWEST;
 101            case SOUTHWEST: return NORTHEAST;
 102            case NORTHEAST: return SOUTHWEST;
 103            case NORTHWEST: return SOUTHEAST;                
 104        }
 105        return -1;
 106    }
 107  
 108    public GridVector getGridSquares(int radius) {
 109        return getGridSquares(radius, false);
 110    }
 111    
 112    public GridVector getGridSquares(int radius, boolean center) {
 113       GridVector squares = new GridVector();
 114        Point p = getCoordinateXY();
 115       
 116        for(int x = -radius; x <= radius; x++) {
 117            
 118            int z;
 119            
 120         
 121                z = (int)(Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2)));
 122 
 123            for(int y = -z; y <= z; y++) {
 124
 125                try {
 126                    if(x == 0 && y == 0 && !center) continue;
 127                //    if(!(Grid.getInstance().getGridSquare(p.x + x, p.y + y).getObject() instanceof Obstacle))
 128                        squares.add(Grid.getInstance().getGridSquare(p.x + x, p.y + y));
 129                    
 130
 131                } catch (Exception ex) {
 132                    
 133                }
 134                
 135            }
 136            
 137        }
 138
 139        if(radius == 1) {
 140            try {
 141
 142                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y - 1));
 143                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y + 1));
 144                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y - 1));
 145                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y + 1));
 146                
 147            } catch (Exception ex) {
 148                
 149            }
 150
 151        } 
 152        
 153        return squares;        
 154    }   
 155    
 156    public int getDirectionOf(GridSquare gs) {
 157        Point thisP = getCoordinateXY();
 158        Point otherP = gs.getCoordinateXY();
 159        
 160        int x = otherP.x - thisP.x;
 161        int y = otherP.y - thisP.y;
 162        
 163        if(otherP.x == thisP.x && otherP.y < thisP.y) {
 164            return NORTH;
 165        }else if(otherP.x == thisP.x && otherP.y > thisP.y) {
 166            return SOUTH;
 167        }else if(otherP.y == thisP.y && otherP.x < thisP.x) {
 168            return WEST;
 169        }else if(otherP.y == thisP.y && otherP.x > thisP.x) {
 170            return EAST;
 171//        }else if(otherP.x == thisP.x && otherP.y > thisP.y) {
 172//            return EAST;
 173        }else if(otherP.x < thisP.x && otherP.y < thisP.y) {
 174            return NORTHWEST;
 175        }else if(otherP.x > thisP.x && otherP.y < thisP.y) {
 176            return NORTHEAST;
 177        }else if(otherP.x > thisP.x && otherP.y > thisP.y) {
 178           return SOUTHEAST;
 179        }else if(otherP.x < thisP.x && otherP.y > thisP.y) {
 180            return SOUTHWEST;
 181        }else{
 182            System.out.println("thisP: " + thisP + ", otherP:" + otherP);
 183            
 184        }
 185        
 186        return -1;
 187    }
 188    
 189    public Vector<GridSquare> getEmptyGridSquares(int radius) {
 190        Vector<GridSquare> squares = new Vector<GridSquare>();
 191        Point p = getCoordinateXY();
 192       
 193        for(int x = -radius; x <= radius; x++) {
 194            
 195            int z;
 196            
 197         
 198                z = (int)(Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2)));
 199 
 200            for(int y = -z; y <= z; y++) {
 201
 202                try {
 203                        
 204                    if(Grid.getInstance().getGridSquare(p.x + x, p.y + y).isEmpty())
 205                        squares.add(Grid.getInstance().getGridSquare(p.x + x, p.y + y));
 206                    
 207
 208                } catch (Exception ex) {
 209                    
 210                }
 211                
 212            }
 213            
 214        }
 215
 216        if(radius == 1) {
 217            try {
 218
 219                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y - 1));
 220                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y + 1));
 221                squares.add(Grid.getInstance().getGridSquare(p.x + 1, p.y - 1));
 222                squares.add(Grid.getInstance().getGridSquare(p.x - 1, p.y + 1));
 223                
 224            } catch (Exception ex) {
 225                
 226            }
 227
 228        } 
 229        
 230        return squares;        
 231    }  
 232    
 233    public static int getNextDirection(int direction) {
 234        //return direction == LAST_DIRECTION ? FIRST_DIRECTION : direction + 1;
 235        
 236        int newDirection = direction == LAST_DIRECTION ? FIRST_DIRECTION : direction + 1;
 237
 238        return newDirection;
 239    }
 240
 241    public static int getPreviousDirection(int direction) {
 242        //return direction == LAST_DIRECTION ? FIRST_DIRECTION : direction + 1;
 243        
 244        int newDirection = direction == FIRST_DIRECTION ? LAST_DIRECTION : direction - 1;
 245
 246        return newDirection;
 247    }
 248    
 249    public void draw(Graphics g) {
 250          Graphics2D g2d = (Graphics2D)g;
 251          
 252         int green = (int)(foodStrength * 75) < 255 ? (int)(foodStrength * 75) : 255;
 253         
 254         
 255         Color old = g2d.getColor();  
 256        if(foodStrength >0 && green >= 0) {
 257          
 258
 259            
 260            
 261           //System.out.println(green);
 262       
 263              
 264            g2d.setColor( new Color(0, green, 0));
 265
 266            g2d.fill(this);
 267
 268            g2d.draw(this);
 269            g2d.setColor(old);
 270        }
 271//         
 272//         if(empty() && pheromone == null && foodStrength == 0.0) {
 273//         
 274//
 275//            g2d.setColor( Color.black);
 276//
 277//            g2d.fill(this);
 278//
 279//            g2d.draw(this);
 280//                      
 281//        }
 282//         
 283          
 284        if(pheromone != null && pheromone.getStrength() != 0) {
 285            pheromone.draw(g);
 286        }
 287        
 288        if(object != null) {
 289            object.draw(g);
 290        }
 291        
 292
 293         g2d.setColor(old);  
 294
 295         
 296         /*      
 297        Graphics2D g2d = (Graphics2D)g;
 298        
 299        g2d.setColor( empty() ? Color.black : object.getColor());
 300  
 301        g2d.fill(this);
 302       
 303        g2d.draw(this);*/
 304    }
 305
 306    public boolean empty() {
 307        return object == null && getAnt() == null;
 308        //return getAnt() == null && getFood() == null;
 309    }
 310
 311    public MyObject getObject() {
 312        return object;
 313    }
 314    
 315    public void setObject(MyObject o) {
 316        if(o instanceof Pheromone) {
 317            setPheromone((Pheromone)o);
 318            return;
 319        }
 320        
 321 //       if(o instanceof Ant && ((Ant)o).carryingFood()){  return; }
 322        if(!isNest() && !(object instanceof Obstacle))
 323            object = o;
 324        
 325        if(o instanceof Obstacle) {
 326            setFoodStrength(0);
 327            getPheromone().setStrength(0);
 328        }
 329    }
 330    
 331    public Ant getAnt() {
 332       return object instanceof Ant ? (Ant)object : null;
 333       // return ant;
 334    }
 335    
 336    public boolean containsAnt() {
 337        return !empty() && object instanceof Ant;
 338        
 339     //   return getAnt() != null;
 340    }
 341
 342    public void setAnt(Ant ant) {
 343        this.object = ant;
 344        this.ant = ant;
 345    }
 346
 347    public Food getFood() {
 348        return object instanceof Food ? (Food)object : null;
 349        //return food;
 350    }
 351    
 352    public Obstacle getObstacle() {
 353        return object instanceof Obstacle ? (Obstacle)object : null;
 354        //return food;
 355    }
 356    
 357    public boolean containsFood() {
 358        return getFood() != null;
 359        //return getFood() != null;
 360    }
 361    
 362    public boolean containsObstacle() {
 363        return getObstacle() != null;
 364    }
 365    
 366    public void setFood(Food food) {
 367     //   if(food != null) setFoodStrength(100);
 368       // else setFoodStrength(0);
 369        this.object = food;
 370    }
 371    
 372    public boolean isNest() {
 373        return object != null && object instanceof Nest;
 374    }
 375
 376    public Pheromone getPheromone() {
 377        if(pheromone == null) pheromone = new Pheromone(this);
 378        return pheromone;
 379    }
 380    
 381    public double getPheromoneStrength() {
 382        return pheromone == null ? 0 : getPheromone().getStrength();
 383    }
 384    
 385    public void setPheromone(Pheromone pheromone) {
 386        this.pheromone = pheromone;
 387    }
 388
 389    public synchronized double getFoodStrength() {
 390
 391//        GridVector surroundingSquares = this.getGridSquares(2);
 392//        
 393//        int count =0;
 394//        for(GridSquare gs : surroundingSquares) 
 395//            if(gs.containsFood()) count++;
 396//        
 397//        if(count == 0) foodStrength = 0;
 398        
 399    //     if(java.lang.Double.compare(foodStrength, 0) <0 || containsObstacle()) { foodStrength = 0; }
 400        return foodStrength;
 401    }
 402
 403    public void recalculateFoodStrength() {
 404        
 405        recalculateFoodStrength(true);
 406    }
 407    
 408    public void recalculateFoodStrength(boolean add) {
 409 
 410        if(getFood() == null) return;
 411
 412        Point thisP = getCoordinateXY();
 413
 414        int radius = 15;
 415
 416        GridVector surroundingSquares = this.getGridSquares(radius);
 417                
 418        for(GridSquare gs : surroundingSquares) {      
 419            
 420                Point p = gs.getCoordinateXY();
 421
 422                double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
 423
 424                double newStrength = gs.getFoodStrength();
 425                
 426                if(add) {
 427                     newStrength += 1 / c2;
 428                }else{
 429                    newStrength -= (1 / c2) + 0.0000001;
 430                   
 431                }
 432
 433                gs.setFoodStrength(newStrength);            
 434            
 435        }    
 436        
 437        
 438        
 439//        Point thisP = getCoordinateXY();
 440//        Vector<GridSquare> gridSquares = Grid.getInstance().getGridSquaresWithFood();
 441//        
 442//     
 443//        
 444//        for(GridSquare gs : gridSquares) {
 445//                if(gs.equals(this)) continue;
 446//                Point p = gs.getCoordinateXY();
 447//
 448//                double c2 = Math.pow(p.x - thisP.x, 2) + Math.pow(p.y - thisP.y, 2);
 449//              //  System.out.println(1/c2);
 450//                if(Math.sqrt(c2) <= 10)
 451//                    this.setFoodStrength(this.getFoodStrength() + 1 / c2);            
 452//            
 453//        }
 454    }
 455
 456    
 457    public synchronized void setFoodStrength(double foodStrength) {
 458
 459//       System.out.print("Setting food strength for " + this.getCoordinateXY().x +
 460//                            ", " + this.getCoordinateXY().y + " from " + foodStrength + " to ");
 461// 
 462        
 463      //  if(java.lang.Double.compare(foodStrength, 0.000009) < 0 || containsObstacle()) { foodStrength = 0; }
 464        if(foodStrength < 0) foodStrength = 0;
 465        this.foodStrength = foodStrength;
 466//        System.out.println(foodStrength + ".");
 467    }
 468
 469    public int compareTo(GridSquare gs) {
 470        return FoodStrengthComparator.compare(this, gs);
 471    }
 472    
 473    public static Comparator<GridSquare> FoodStrengthComparator = new Comparator<GridSquare>() {
 474        public int compare(GridSquare gs1, GridSquare gs2) {
 475          //  int pheromoneStrength = gs1.getPheromone() != null ? gs1.getPheromone().getStrength() : 0;
 476            return gs1.getFoodStrength() > gs2.getFoodStrength() ? -1 : 1;
 477        }
 478    };   
 479
 480    public static Comparator<GridSquare> PheromoneStrengthComparator = new Comparator<GridSquare>() {
 481        public int compare(GridSquare gs1, GridSquare gs2) {
 482            //return gs1.getPheromone().getStrength() > gs2.getPheromone().getStrength() ? -1 : 1;
 483            double pheromoneStrength1 = gs1.getPheromone() != null ? gs1.getPheromone().getStrength() : 0;
 484            double pheromoneStrength2 = gs2.getPheromone() != null ? gs2.getPheromone().getStrength() : 0;
 485            //return pheromoneStrength1 > pheromoneStrength2 ? -1 : 1;
 486           // return (int)pheromoneStrength1 - pheromoneStrength2;
 487            return java.lang.Double.compare(pheromoneStrength1, pheromoneStrength2);
 488        }
 489    }; 
 490    
 491    public String toString() {
 492        Point p = this.getCoordinateXY();
 493        String s = "Grid Square - x: " + p.x + ", y: " + p.y + "\n";
 494        s = s.concat("FoodStrength: " + getFoodStrength());
 495        if(getPheromoneStrength() > 0) s = s.concat("PheromoneStrength: " + this.getPheromone().getStrength() + ", direction: " + getPheromone().getDirection());
 496        s = s.concat("Object: " + getObject());
 497        return s;
 498    }
 499    
 500
 501}
 502

· GridSquare.java ends ·

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