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

· Ant.java ·

   1/*
   2 * Ant.java
   3 *
   4 * Created on 07 October 2006, 15:45
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package ants;
  11
  12import ants.event.AntEvent;
  13import ants.event.AntListener;
  14import ants.event.PickedUpFoodAntEvent;
  15import java.awt.Color;
  16import java.awt.Graphics;
  17import java.util.Collections;
  18import java.util.HashSet;
  19import java.util.Random;
  20import java.util.Vector;
  21
  22/**
  23 *
  24 * @author James Hamilton
  25 */
  26public class Ant extends MyObject implements Runnable {
  27 
  28    private Thread move;
  29    private int currentDirection = GridSquare.getRandomDirection();
  30    private Food food = null;
  31    
  32    private GridSquare lastGridSquare = null;
  33    private GridSquare previousGridSquare = null;
  34    private HashSet<GridSquare> dontVisit = new HashSet<GridSquare>();
  35    
  36    private Color color = Color.red;
  37    
  38    
  39    /** Creates a new instance of Ant */
  40    public Ant() {
  41    }
  42    
  43    public Ant(Ant ant) {
  44         super(ant.getGridSquare()); 
  45    }
  46    
  47    public Ant(GridSquare gridSquare) {
  48        super(gridSquare);
  49        Random generator = new Random();
  50         color = new Color(generator.nextInt( 255 ), generator.nextInt( 255 ),generator.nextInt( 255 ));
  51    
  52      //  setGridSquare(gridSquare); 
  53    }
  54    
  55    private Vector<AntListener> listeners = new Vector<AntListener>();
  56    
  57    public void addAntListener(AntListener listener) {
  58        listeners.add(listener);
  59    }
  60    
  61    public void notifyListeners(AntEvent e) {
  62        for(AntListener listener : listeners)
  63            listener.antEventHandler(e);
  64    }
  65   
  66    public boolean onTrail() {
  67        return getGridSquare().getPheromoneStrength() > 0;
  68    }
  69    
  70    public boolean onOwnTrail() {
  71        return getGridSquare().getPheromone().laidBy(this);
  72    }
  73    
  74
  75    public GridSquare getNextGridSquare() {
  76        
  77        GridVector squares;// = getGridSquare().getGridSquares(1).getFreeGridSquares();
  78        squares = getGridSquare().getGridSquaresInDirection(currentDirection).getFreeGridSquares(); 
  79        
  80        if(carryingFood()) squares = squares.getEmptyGridSquares();
  81        
  82        GridSquare currentGridSquare = getGridSquare();
  83        GridSquare nextGridSquare = null;
  84        
  85        if(getGridSquare().isNest()) {
  86            if(carryingFood()) {
  87                Grid.getInstance().getNest().addAnt(this);    
  88            }else{
  89                squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
  90                
  91                if(squares.getPheromoneStrength(PheromoneParticle.TOWARD_NEST) > 0) {
  92                    
  93                    squares = squares.getPheromoneGridSquares();
  94
  95                    GridVector towardsNest = squares.getPheromoneGridSquares(PheromoneParticle.TOWARD_NEST);
  96                    GridVector awayFromNest = squares.getPheromoneGridSquares(PheromoneParticle.AWAY_FROM_NEST);
  97
  98                
  99                        if(towardsNest.size() > 0) {
 100                            Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 101
 102                            nextGridSquare = squares.firstElement();                        
 103                        }
 104                        /*else if(awayFromNest.size() > 0) {
 105                             if(awayFromNest.getPheromoneGridSquares(this).size() > 0) {
 106                                squares = awayFromNest.getPheromoneGridSquares(this);
 107                            }else{
 108                                 squares = awayFromNest;
 109                            }
 110
 111                            Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 112
 113                            nextGridSquare = squares.firstElement();
 114                        }*/
 115                   
 116                }
 117                
 118                if(nextGridSquare == null) nextGridSquare = squares.getRandomGridSquare();
 119                currentDirection = getGridSquare().getDirectionOf(nextGridSquare);                               
 120            }
 121        }else{
 122           
 123            if(squares.size() == 0) {
 124                //if there is nowhere 2 go infront, look behind.
 125                currentDirection = GridSquare.getOppositeDirection(currentDirection);
 126                squares = getGridSquare().getGridSquaresInDirection(currentDirection).getFreeGridSquares(); 
 127            }
 128            
 129            boolean changeDirection = false;
 130            
 131            if(squares.size() == 0 || (onTrail() && carryingFood())) {
 132                //if there is nowhere 2 go behind, look look sidewards.
 133                squares = getGridSquare().getGridSquares(1).getFreeGridSquares();      
 134                changeDirection = true;
 135            }
 136            
 137            if(carryingFood() && squares.containsNest()) {
 138                nextGridSquare = Grid.getInstance().getNest().getGridSquare();
 139            }else if(!carryingFood() && squares.containsFood() ) {
 140                squares = squares.getFoodGridSquares();
 141                
 142                nextGridSquare = squares.getRandomGridSquare();
 143            }else if(!carryingFood() && squares.getFoodStrength() > 0) {
 144                squares = squares.getFoodStrengthGridSquares();
 145                
 146                Collections.sort(squares, GridSquare.FoodStrengthComparator);
 147                
 148                nextGridSquare = squares.firstElement();
 149            }else if(carryingFood() && squares.getPheromoneStrength() > 0) {
 150                squares = squares.getPheromoneGridSquares();
 151                
 152                GridVector towardsNest = squares.getPheromoneGridSquares(PheromoneParticle.TOWARD_NEST);
 153                GridVector awayFromNest = squares.getPheromoneGridSquares(PheromoneParticle.AWAY_FROM_NEST);
 154                
 155                if(carryingFood()) {
 156                    if(towardsNest.size() > 0) {
 157                        Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 158                        
 159                        nextGridSquare = squares.firstElement();                        
 160                    }else if(awayFromNest.size() > 0) {
 161                         if(awayFromNest.getPheromoneGridSquares(this).size() > 0) {
 162                            squares = awayFromNest.getPheromoneGridSquares(this);
 163                        }else{
 164                             squares = awayFromNest;
 165                        }
 166                         
 167                        Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 168                        
 169                        nextGridSquare = squares.firstElement();
 170                    }
 171                }
 172                
 173                Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 174                
 175                nextGridSquare = squares.getRandomGridSquare(true);                
 176                
 177                
 178            }else if(squares.size() == 3) {
 179                nextGridSquare = squares.getMiddleGridSquare();
 180            }else if(squares.size() > 0) {
 181                nextGridSquare = squares.getRandomGridSquare();
 182            }else{
 183                //dont move
 184            }
 185            
 186            if(changeDirection)
 187                currentDirection = getGridSquare().getDirectionOf(nextGridSquare);
 188            
 189        }
 190        
 191        return nextGridSquare;
 192    }
 193    
 194    public synchronized void move() {
 195        
 196       
 197           if(carryingFood()) {
 198
 199             getGridSquare().getPheromone().increaseStrength(this, PheromoneParticle.TOWARD_NEST);               
 200
 201        }else{
 202
 203
 204            getGridSquare().getPheromone().increaseStrength(this, PheromoneParticle.AWAY_FROM_NEST);   
 205
 206        }     
 207        GridVector squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
 208        GridSquare currentGridSquare = getGridSquare();
 209        GridSquare nextGridSquare = getNextGridSquare();
 210        
 211         
 212  
 213
 214
 215       
 216        
 217        if(nextGridSquare != null) {
 218            if(nextGridSquare.containsFood()) carry(nextGridSquare.getFood());
 219            setGridSquare(nextGridSquare, false);
 220        }
 221        
 222     
 223
 224
 225    }
 226   
 227    public void carry(Food food) {
 228      //  System.out.println("Carrying...");
 229       // Grid.getInstance().printStats();
 230       
 231        try {
 232            notifyListeners(new PickedUpFoodAntEvent(this));
 233            this.setFood(food);
 234            food.getGridSquare().recalculateFoodStrength(false);
 235            food.getGridSquare().setObject(null);
 236            
 237            turnBack();
 238            //setGridSquare(getPreviousGridSquare());
 239            
 240            food.setGridSquare(null);
 241            
 242
 243        }catch (RuntimeException e) {
 244           // System.out.println("Already gone!");
 245        }
 246    }
 247    
 248    public boolean carryingFood() {
 249        return getFood() != null;
 250    }
 251    
 252    public void turnBack() {
 253            if(getPreviousGridSquare() != null) {
 254                currentDirection = getGridSquare().getDirectionOf(getPreviousGridSquare());
 255            }        
 256    }
 257    
 258    public void run() {
 259        //Call the move method while the Thread is running. And sleep for a time based on iSpeed.
 260        while(move != null) {
 261            try {
 262                move();
 263          
 264                move.sleep(200);
 265            }catch (InterruptedException e) {}
 266        }
 267    }
 268    
 269    /**
 270     * Start or stop the ant moving.
 271     * @param start true to start, false to stop.
 272     */
 273    public void start(boolean start) {
 274        if(start) start(); else stop();
 275    }
 276    
 277    /**
 278     * Start the ant moving.
 279     */
 280    public void start() {
 281        //Create and start a new Thread.
 282        move = new Thread(this);
 283      //  move.setDaemon(true);
 284        move.start();
 285    }
 286    
 287    public void pause() {
 288        if(move == null) start();
 289        else stop();
 290    }
 291    
 292    /**
 293     * Stop the rectangle moving.
 294     */
 295    public void stop() {
 296        move = null;
 297    }
 298
 299    
 300    public Color getColor() {
 301        if(food == null) 
 302            return color;
 303        else
 304            return Color.YELLOW;
 305    }
 306
 307    public Food getFood() {
 308        return food;
 309    }
 310
 311    public void setFood(Food food) {
 312        this.food = food;
 313    }
 314    
 315    public String toString() {
 316        return "Ant" 
 317		+ getGridSquare().getGridSquares(1).getGridSquaresInDirection(
 318			getGridSquare(), currentDirection).getFreeGridSquares() 
 319		+ ";direction: " + currentDirection;
 320    }
 321    
 322    public void draw(Graphics g) {
 323        super.draw(g);
 324
 325    
 326    }
 327
 328    public GridSquare getPreviousGridSquare() {
 329        return previousGridSquare;
 330    }
 331
 332    public void setPreviousGridSquare(GridSquare previousGridSquare) {
 333        this.previousGridSquare = previousGridSquare;
 334    }
 335 
 336    public void setGridSquare(GridSquare gridSquare, boolean setDirection) {
 337        if(setDirection) 
 338               currentDirection = getGridSquare().getDirectionOf(gridSquare);
 339        
 340        setPreviousGridSquare(getGridSquare());
 341        super.setGridSquare(gridSquare);
 342    }
 343    
 344    public void setGridSquare(GridSquare gridSquare) {
 345            setGridSquare(gridSquare, false);
 346    }
 347}
 348

· Ant.java ends ·

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