To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/16.0/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 synchronized void move() {
  67        
  68        GridVector squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
  69        GridSquare currentGridSquare = getGridSquare();
  70        GridSquare nextGridSquare = null;
  71        
  72        if(getGridSquare().isNest()) {
  73
  74            if(carryingFood()) {
  75                Grid.getInstance().getNest().addAnt(this);
  76            }else{
  77                
  78                if(squares.containsFood()) {
  79                    //contains food
  80                    squares = squares.getFoodGridSquares();
  81                    nextGridSquare = squares.getRandomGridSquare();
  82                    
  83                }else if(squares.getFoodStrength() > 0) {
  84                    //if there is any food around
  85                    squares = squares.getFoodStrengthGridSquares();
  86                    
  87                    Collections.sort(squares, GridSquare.FoodStrengthComparator);
  88                    
  89                    nextGridSquare = squares.getRandomGridSquare(true);
  90                    
  91                }else if(squares.getPheromoneStrength(PheromoneParticle.TOWARD_NEST) > 0) {
  92                    //if there is any pheromone pointing away from the nest
  93                    
  94                    squares = squares.getPheromoneGridSquares();
  95                    
  96                    Collections.sort(squares,GridSquare.PheromoneStrengthComparator);
  97                    
  98                    nextGridSquare = squares.getRandomGridSquare(true);//.firstElement();
  99                    
 100                }else if(squares.getPheromoneStrength(PheromoneParticle.AWAY_FROM_NEST) > 0) {
 101                    //if there is any pheromone pointing away from the nest
 102                    
 103                   // Collections.sort(squares,GridSquare.PheromoneStrengthComparator);
 104                    
 105                   // nextGridSquare = squares.firstElement();
 106                    nextGridSquare = squares.getRandomGridSquare();
 107                    
 108                }else{
 109                    //empty squares        
 110                    
 111                    nextGridSquare = squares.getRandomGridSquare();
 112                }
 113                
 114         
 115                setGridSquare(nextGridSquare, true);
 116                
 117            }
 118            
 119            
 120        }else{
 121            //if outside nest
 122            
 123            //only look in front
 124            squares = getGridSquare().getGridSquaresInDirection(currentDirection).getFreeGridSquares(); 
 125           
 126            if(carryingFood()) {
 127                //if carrying food, ignore other food.
 128                squares = squares.getEmptyGridSquares();
 129            }            
 130            
 131            if(squares.size() == 0) {
 132                //if no squares in front, look behind. turn around if so.  
 133                squares = getGridSquare().getGridSquaresInDirection(GridSquare.getOppositeDirection(currentDirection)).getFreeGridSquares(); 
 134                if(squares.size() > 0) currentDirection = GridSquare.getOppositeDirection(currentDirection);
 135            }
 136            
 137            if(squares.size() == 0) {
 138                //if no squares behind either, look in all directions.
 139                squares = getGridSquare().getGridSquares(1).getFreeGridSquares();  
 140            }
 141  
 142            if(carryingFood()) {
 143                //if carrying food, ignore other food.
 144                squares = squares.getEmptyGridSquares();
 145            }   
 146            
 147            if(squares.size() > 0) {
 148            
 149                if(squares.containsNest()) {
 150                    nextGridSquare = Grid.getInstance().getNest().getGridSquare();
 151                }else if(squares.containsFood() && !carryingFood()) {
 152                    //contains food
 153                    
 154                    squares = squares.getFoodGridSquares();
 155                    
 156                    nextGridSquare = squares.getRandomGridSquare();
 157                    
 158                    carry(nextGridSquare.getFood());
 159                   
 160                }else if(squares.getFoodStrength() > 0 && !carryingFood()) {
 161                    //food nearby
 162                    squares = squares.getFoodStrengthGridSquares();
 163                    
 164                    Collections.sort(squares, GridSquare.FoodStrengthComparator);
 165                    
 166                    nextGridSquare = squares.getRandomGridSquare(true);
 167                    
 168                    currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 169                    
 170                }else if(squares.getPheromoneStrength() > 0) {
 171                    //contains pheromone
 172                    
 173                    if(carryingFood()) {
 174                        //if carrying food - follow trail back to nest.
 175                        
 176                        //trails toward nest are better, own trail even better
 177                        
 178                       
 179                        
 180                        if(squares.getPheromoneStrength(PheromoneParticle.TOWARD_NEST) > 0) {
 181                            if(squares.getPheromoneGridSquares(this).size() > 0) {
 182                                 squares = squares.getPheromoneGridSquares(this);
 183                            }else{
 184                                squares = squares.getPheromoneGridSquares(PheromoneParticle.TOWARD_NEST);
 185                            }
 186                            
 187                            Collections.sort(squares,GridSquare.PheromoneStrengthComparator);
 188                            nextGridSquare = squares.firstElement();
 189                        }else if(squares.getPheromoneStrength(PheromoneParticle.AWAY_FROM_NEST) > 0) {
 190                            squares = squares.getPheromoneGridSquares(PheromoneParticle.AWAY_FROM_NEST);
 191                            
 192                            Collections.sort(squares,GridSquare.PheromoneStrengthComparator);       
 193                            nextGridSquare = squares.firstElement();                            
 194                        }
 195                        currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 196                    }else{
 197                        //if not carrying food - follow trail away from nest.
 198                        if(squares.getPheromoneStrength(PheromoneParticle.AWAY_FROM_NEST) > 0) {
 199                            //squares = squares.getPheromoneGridSquares(PheromoneParticle.AWAY_FROM_NEST);
 200                            
 201                            //Collections.sort(squares,GridSquare.PheromoneStrengthComparator);       
 202                            //nextGridSquare = squares.getRandomGridSquare();//.firstElement();
 203                            nextGridSquare = squares.getRandomGridSquare();
 204                            currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 205                        }else{
 206                            nextGridSquare = squares.getMiddleGridSquare();
 207                        }
 208                    }
 209                    
 210                    
 211            
 212                }else{
 213                    //empty squares        
 214                    if(squares.size() == 3) {
 215                        //pick middle
 216                        nextGridSquare = squares.getMiddleGridSquare();
 217                    }else{
 218                        nextGridSquare = squares.getRandomGridSquare();
 219                    }
 220                }
 221                
 222                
 223                if(carryingFood()) {
 224
 225                     currentGridSquare.getPheromone().increaseStrength(this, PheromoneParticle.TOWARD_NEST);               
 226
 227                }else{
 228
 229
 230                    currentGridSquare.getPheromone().increaseStrength(this, PheromoneParticle.AWAY_FROM_NEST);   
 231
 232                }
 233                
 234                if(nextGridSquare != null) setGridSquare(nextGridSquare, false);
 235                
 236            }else{
 237                //no where to move. wait.
 238                
 239                //turn around
 240                currentDirection = GridSquare.getOppositeDirection(currentDirection);
 241            }
 242            
 243            //move, keep direction
 244           
 245        }
 246        
 247        
 248        
 249        /*
 250        if(getGridSquare().isNest()) {
 251            
 252            squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
 253          
 254            if(carryingFood()) {
 255                Grid.getInstance().getNest().addAnt(this);
 256              //  System.out.println("TEST");
 257            }else if(squares.size() > 0) {
 258                if(squares.containsFood()) {
 259                    setGridSquare(squares.firstElement());
 260                }else if(squares.getFoodStrength() > 0) {
 261                    Collections.sort(squares, GridSquare.FoodStrengthComparator);
 262                    setGridSquare(squares.getRandomGridSquare(true));   
 263                }else if(squares.getPheromoneStrength() > 0) {
 264                    GridVector towardNest = squares.getPheromoneGridSquares(PheromoneParticle.TOWARD_NEST);
 265                   // GridVector awayFromNest = squares.getPheromoneGridSquares(PheromoneParticle.AWAY_FROM_NEST);
 266                    
 267           //         if(towardNest.size() > awayFromNest.size()) {
 268                        squares = towardNest;
 269               //     }else{
 270                 //       squares = towardNest;
 271                //    }
 272                    
 273                    if(squares.size() > 0) setGridSquare(squares.firstElement());
 274                    else{
 275                        setGridSquare(getGridSquare().getGridSquares(1).getFreeGridSquares().getRandomGridSquare());
 276                    }
 277                    
 278          
 279                }else{
 280                    setGridSquare(squares.getRandomGridSquare()); 
 281                }
 282                currentDirection = currentGridSquare.getDirectionOf(getGridSquare());
 283            }
 284            
 285
 286            
 287        }else{
 288            if(carryingFood()) {
 289             //   currentDirection = getGridSquare().getDirectionOf(Grid.getInstance().getNest().getGridSquare());
 290            }    
 291            
 292//with direction+sort
 293            squares = getGridSquare().getGridSquaresInDirection(currentDirection).getFreeGridSquares();
 294     
 295       
 296            
 297            if(carryingFood()) {
 298                squares = squares.getEmptyGridSquares();
 299            }else{
 300                squares = squares.getGridSquaresWithoutNest();
 301            }
 302            
 303            if(squares.size() == 0) {
 304                try {
 305                //    setGridSquare(getPreviousGridSquare());
 306                }catch (NullPointerException e) {}
 307                setPreviousGridSquare(null);
 308                
 309                squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
 310                if(carryingFood()) squares = squares.getEmptyGridSquares();
 311                if(squares.size() > 0) currentDirection = getGridSquare().getDirectionOf(squares.getRandomGridSquare());
 312            }else{
 313                
 314                if(squares.containsFood() && !carryingFood()) {
 315                    squares = squares.getFoodGridSquares();
 316                    GridSquare gridSquare = squares.getRandomGridSquare();
 317                    carry(gridSquare.getFood());
 318                    setGridSquare(gridSquare);
 319                }else if(squares.getFoodStrength() > 0 && !carryingFood()) {
 320                    Collections.sort(squares, GridSquare.FoodStrengthComparator);
 321                    setGridSquare(squares.getRandomGridSquare(true));    //firstElement());//
 322                  //  if(currentGridSquare.getPheromone().getStrength() < getGridSquare().getPheromone().getStrength())
 323                        currentDirection = currentGridSquare.getDirectionOf(getGridSquare());
 324               }else if(squares.getPheromoneStrength() > 0) {
 325                    if(carryingFood()) {
 326                        
 327                        GridSquare square = squares.getGridSquare(this);
 328                        
 329                        if(square != null) {
 330                            currentDirection = getGridSquare().getDirectionOf(square);
 331                            setGridSquare(square);
 332                        }
 333                        
 334                    }else{
 335                        squares = squares.getPheromoneGridSquares(PheromoneParticle.AWAY_FROM_NEST);
 336                        squares = squares.getPheromoneGridSquares(this, false);
 337                        GridSquare square = null;
 338                        if(squares.size() > 0) square = squares.getRandomGridSquare();
 339                        
 340                        if(square != null) {
 341                            currentDirection = getGridSquare().getDirectionOf(square);
 342                            setGridSquare(square);
 343                        }else{
 344                            
 345                           setGridSquare(getGridSquare().getGridSquares(1).getFreeGridSquares().getRandomGridSquare());
 346                        }           
 347                        
 348                 
 349                    }
 350                }else{
 351             
 352                    
 353                    if(squares.size() == 3) {
 354                        setGridSquare(squares.getMiddleGridSquare());
 355                    }else{
 356                        setGridSquare(squares.getRandomGridSquare());
 357                    }
 358                }
 359                
 360
 361            }
 362           if(carryingFood()) {
 363              
 364               currentGridSquare.getPheromone().increaseStrength(this, PheromoneParticle.TOWARD_NEST);//GridSquare.getOppositeDirection(currentDirection));
 365               
 366              // currentGridSquare.getPheromone().setDirection(Pheromone.TOWARD_NEST);
 367                //currentGridSquare.getPheromone().setDirection(GridSquare.getOppositeDirection(getGridSquare().getDirectionOf(Grid.getInstance().getNest().getGridSquare())));
 368           }else{
 369                currentGridSquare.getPheromone().increaseStrength(this, PheromoneParticle.AWAY_FROM_NEST);
 370             //    currentGridSquare.getPheromone().setDirection(Pheromone.AWAY_FROM_NEST);
 371           }
 372        }
 373        */
 374
 375
 376    }
 377   
 378    public void carry(Food food) {
 379      //  System.out.println("Carrying...");
 380       // Grid.getInstance().printStats();
 381       
 382        try {
 383            notifyListeners(new PickedUpFoodAntEvent(this));
 384            this.setFood(food);
 385            food.getGridSquare().recalculateFoodStrength(false);
 386            food.getGridSquare().setObject(null);
 387            
 388            turnBack();
 389            setGridSquare(getPreviousGridSquare());
 390            
 391            food.setGridSquare(null);
 392            
 393
 394        }catch (RuntimeException e) {
 395           // System.out.println("Already gone!");
 396        }
 397    }
 398    
 399    public boolean carryingFood() {
 400        return getFood() != null;
 401    }
 402    
 403    public void turnBack() {
 404            if(getPreviousGridSquare() != null) {
 405                currentDirection = getGridSquare().getDirectionOf(getPreviousGridSquare());
 406            }        
 407    }
 408    
 409    public void run() {
 410        //Call the move method while the Thread is running. And sleep for a time based on iSpeed.
 411        while(move != null) {
 412            try {
 413                move();
 414          
 415                move.sleep(200);
 416            }catch (InterruptedException e) {}
 417        }
 418    }
 419    
 420    /**
 421     * Start or stop the rectangle moving.
 422     * @param start true to start, false to stop.
 423     */
 424    public void start(boolean start) {
 425        if(start) start(); else stop();
 426    }
 427    
 428    /**
 429     * Start the rectangle moving.
 430     */
 431    public void start() {
 432        //Create and start a new Thread.
 433        move = new Thread(this);
 434      //  move.setDaemon(true);
 435        move.start();
 436    }
 437    
 438    /**
 439     * Stop the rectangle moving.
 440     */
 441    public void stop() {
 442        move = null;
 443    }
 444
 445    
 446    public Color getColor() {
 447        if(food == null) 
 448            return color;//Color.red;//new Color((int)Math.random() * 255 + 100, (int)Math.random() * 255+100, (int)Math.random() * 255+100);//Color.red;
 449        else
 450            return Color.YELLOW;
 451    }
 452
 453    public Food getFood() {
 454        return food;
 455    }
 456
 457    public void setFood(Food food) {
 458        this.food = food;
 459    }
 460    
 461    public String toString() {
 462        return "Ant" + getGridSquare().getGridSquares(1).getGridSquaresInDirection(getGridSquare(), currentDirection).getFreeGridSquares() + ";direction: " + currentDirection;
 463    }
 464    
 465    public void draw(Graphics g) {
 466        super.draw(g);
 467
 468    
 469    }
 470
 471    public GridSquare getPreviousGridSquare() {
 472        return previousGridSquare;
 473    }
 474
 475    public void setPreviousGridSquare(GridSquare previousGridSquare) {
 476        this.previousGridSquare = previousGridSquare;
 477    }
 478 
 479    public void setGridSquare(GridSquare gridSquare, boolean setDirection) {
 480        if(setDirection) 
 481               currentDirection = getGridSquare().getDirectionOf(gridSquare);
 482        
 483        setPreviousGridSquare(getGridSquare());
 484        super.setGridSquare(gridSquare);
 485    }
 486    
 487    public void setGridSquare(GridSquare gridSquare) {
 488            setGridSquare(gridSquare, false);
 489    }
 490}
 491

· Ant.java ends ·

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