To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/15.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.Vector;
  19
  20/**
  21 *
  22 * @author James Hamilton
  23 */
  24public class Ant extends MyObject implements Runnable {
  25 
  26    private Thread move;
  27    private int currentDirection = GridSquare.getRandomDirection();
  28    private Food food = null;
  29    
  30    private GridSquare lastGridSquare = null;
  31    
  32    /** Creates a new instance of Ant */
  33    public Ant() {
  34    }
  35    
  36    public Ant(Ant ant) {
  37         super(ant.getGridSquare()); 
  38    }
  39    
  40    public Ant(GridSquare gridSquare) {
  41        super(gridSquare);
  42         
  43      //  setGridSquare(gridSquare); 
  44    }
  45    
  46    private Vector<AntListener> listeners = new Vector<AntListener>();
  47    
  48    public void addAntListener(AntListener listener) {
  49        listeners.add(listener);
  50    }
  51    
  52    public void notifyListeners(AntEvent e) {
  53        for(AntListener listener : listeners)
  54            listener.antEventHandler(e);
  55    }
  56    
  57    public boolean isRoaming() {
  58        return !isOnAMission();
  59    }
  60    
  61    public boolean isOnAMission() {
  62        GridVector surroundingSquares = getGridSquare().getGridSquares(1, true);
  63        return surroundingSquares.getPheromoneStrength() > 0 ||
  64                surroundingSquares.getFoodStrength() > 0 ||
  65                carryingFood();
  66    }
  67  
  68    private synchronized void move_next(GridSquare square, boolean changeDirection) {
  69        if(changeDirection) currentDirection = direction_of(square);
  70        setGridSquare(square);
  71    }
  72    
  73    private synchronized void move_next(GridVector squares, boolean changeDirection) {
  74         System.out.println(squares.size());
  75//        if(squares.size() == 0) {
  76//            squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
  77//            changeDirection = true;
  78//        }
  79//        if(squares.size() == 0) return;
  80        
  81        GridSquare nextGridSquare = squares.getRandomGridSquare(true);
  82       
  83        move_next(nextGridSquare, changeDirection);
  84    }
  85            
  86    private synchronized void move_next(GridVector squares) {
  87        move_next(squares, false);
  88    } 
  89    
  90    private int direction_of(GridSquare gs) {
  91        return getGridSquare().getDirectionOf(gs);
  92    }
  93    
  94    private int direction_of(Nest nest) {
  95        return direction_of(nest.getGridSquare());
  96    }
  97    
  98    private GridVector choose_best_squares(GridVector squares) {
  99        if(squares.containsNest() && carryingFood()) {
 100            squares.clear();
 101            squares.add(Grid.getInstance().getNest().getGridSquare());
 102        }else if(squares.containsFood()) {
 103            squares = squares.getFoodGridSquares();
 104        }else if(squares.getFoodStrength() > 0) {
 105            squares = squares.getFoodGridSquares();
 106            Collections.sort(squares, GridSquare.FoodStrengthComparator);
 107        }else if(squares.getPheromoneStrength() > 0) {
 108            squares = squares.getPheromoneGridSquares();
 109            Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 110        }
 111        
 112        return squares;
 113        
 114    }
 115    
 116    public synchronized void move() {
 117        
 118        GridVector squares;
 119        GridSquare currentGridSquare = getGridSquare();
 120        
 121        if(getGridSquare().isNest()) {
 122            
 123            squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
 124          
 125            if(carryingFood()) {
 126                Grid.getInstance().getNest().addAnt(this);
 127              //  System.out.println("TEST");
 128            }else if(squares.size() > 0) {
 129                if(squares.containsFood()) {
 130                    setGridSquare(squares.firstElement());
 131                }else if(squares.getFoodStrength() > 0) {
 132                    Collections.sort(squares, GridSquare.FoodStrengthComparator);
 133                    setGridSquare(squares.getRandomGridSquare(true));   
 134                }else if(squares.getPheromoneStrength() > 0) {
 135                    squares = squares.getPheromoneGridSquares();
 136                    Collections.sort(squares, GridSquare.PheromoneStrengthComparator);
 137                    setGridSquare(squares.firstElement());//getRandomGridSquare(true));                    
 138                }else{
 139                    setGridSquare(squares.getRandomGridSquare()); 
 140                }
 141                currentDirection = currentGridSquare.getDirectionOf(getGridSquare());
 142            }
 143            
 144
 145            
 146        }else{
 147            if(carryingFood()) {
 148                currentDirection = getGridSquare().getDirectionOf(Grid.getInstance().getNest().getGridSquare());
 149            }    
 150            
 151//with direction+sort
 152            squares = getGridSquare().getGridSquaresInDirection(currentDirection).getFreeGridSquares();
 153      
 154            if(!carryingFood() && getGridSquare().getPheromoneStrength() > 0 && squares.getFoodStrength() == 0) {
 155                if(getGridSquare().getPheromone().getDirection() != -1) {
 156                    currentDirection = getGridSquare().getPheromone().getDirection();
 157                    squares = getGridSquare().getGridSquaresInDirection(currentDirection).getFreeGridSquares();
 158                }
 159            }
 160            
 161            if(carryingFood()) {
 162                squares = squares.getEmptyGridSquares();
 163            }else{
 164                squares = squares.getGridSquaresWithoutNest();
 165            }
 166            
 167            if(squares.size() == 0) {
 168                squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
 169                if(carryingFood()) squares = squares.getEmptyGridSquares();
 170                if(squares.size() > 0) currentDirection = getGridSquare().getDirectionOf(squares.getRandomGridSquare());
 171            }else{
 172                
 173                if(squares.containsFood() && !carryingFood()) {
 174                    squares = squares.getFoodGridSquares();
 175                    GridSquare gridSquare = squares.getRandomGridSquare();
 176                    carry(gridSquare.getFood());
 177                    setGridSquare(gridSquare);
 178                }else if(squares.getFoodStrength() > 0 && !carryingFood()) {
 179                    Collections.sort(squares, GridSquare.FoodStrengthComparator);
 180                    setGridSquare(squares.getRandomGridSquare(true));    //firstElement());//
 181                  //  if(currentGridSquare.getPheromone().getStrength() < getGridSquare().getPheromone().getStrength())
 182                        currentDirection = currentGridSquare.getDirectionOf(getGridSquare());
 183                }else if(squares.getPheromoneStrength() > 0 && !carryingFood()) {
 184                    squares = squares.getPheromoneGridSquares();
 185                    Collections.sort(squares,GridSquare.PheromoneStrengthComparator);//Collections.reverseOrder(GridSquare.PheromoneStrengthComparator));// 
 186                    setGridSquare( squares.firstElement());//squares.getRandomGridSquare(true)); //squares.firstElement());//        //better with random or first?          
 187
 188                    //    setGridSquare(squares.getRandomGridSquare());
 189 
 190                   
 191                    ////currentDirection = currentGridSquare.getDirectionOf(getGridSquare());                    
 192                }else{
 193                    if(carryingFood()) {
 194                        setGridSquare(squares.firstElement());
 195                    }else if(currentGridSquare.getPheromoneStrength() > 0 && squares.getPheromoneStrength() == 0) {
 196                        currentDirection = GridSquare.getOppositeDirection(currentDirection);
 197                    }else if(squares.size() == 3) {
 198                        setGridSquare(squares.getMiddleGridSquare());
 199                    }else{
 200                        setGridSquare(squares.getRandomGridSquare());
 201                    }
 202                }
 203                
 204
 205            }
 206           if(carryingFood()) {
 207              
 208               currentGridSquare.getPheromone().increaseStrength(GridSquare.getOppositeDirection(currentDirection));
 209                //currentGridSquare.getPheromone().setDirection(GridSquare.getOppositeDirection(getGridSquare().getDirectionOf(Grid.getInstance().getNest().getGridSquare())));
 210           }
 211        }
 212        
 213        /*
 214        
 215        boolean changeDirection = false;
 216        GridVector squares = getGridSquare().getGridSquares(1).getFreeGridSquares().getGridSquaresInDirection(getGridSquare(), currentDirection);
 217//        if(squares.size() == 0) {
 218//            squares = getGridSquare().getGridSquares(1);
 219//            changeDirection = true;
 220//        }
 221        GridSquare nextGridSquare = null;
 222        if(carryingFood()) {
 223
 224            
 225            squares = squares.getEmptyGridSquares();
 226            if(squares.containsNest() || getGridSquare().isNest()) {
 227                setGridSquare(Grid.getInstance().getNest().getGridSquare());
 228                Grid.getInstance().getNest().add(this);
 229               // nextGridSquare = Grid.getInstance().getNest().getGridSquare();
 230            }else if(squares.size() > 0) {
 231                currentDirection = direction_of(Grid.getInstance().getNest());
 232                nextGridSquare = squares.getMiddleGridSquare();//getRandomGridSquare();//firstElement();//getMiddleGridSquare();
 233                
 234            }
 235            
 236        }else if (!carryingFood()) {
 237           
 238            if(getGridSquare().isNest()) {
 239                squares = getGridSquare().getGridSquares(1);
 240                if(squares.containsFood()) {
 241                    nextGridSquare = squares.getFoodGridSquares().getRandomGridSquare(true);
 242                }else if(squares.getFoodStrength() > 0) {
 243                    nextGridSquare = squares.getFoodStrengthGridSquares().getRandomGridSquare(true);
 244                }else if(squares.getPheromoneStrength() > 0) {
 245                    nextGridSquare = squares.getPheromoneGridSquares().getRandomGridSquare(true);
 246                }else{
 247                    nextGridSquare = squares.getRandomGridSquare();
 248                }
 249                currentDirection = direction_of(nextGridSquare);
 250                
 251            }else if(squares.containsFood()) {
 252                nextGridSquare = squares.getFoodGridSquares().firstElement();
 253                carry(nextGridSquare.getFood());
 254                currentDirection = direction_of(Grid.getInstance().getNest());
 255            }else if(squares.size() > 0) {
 256                nextGridSquare = squares.getMiddleGridSquare();//getRandomGridSquare();//getMiddleGridSquare();
 257            }else{
 258                
 259                squares = getGridSquare().getGridSquares(1).getFreeGridSquares();
 260                if(squares.size() > 0) {
 261                    nextGridSquare = squares.getRandomGridSquare();//getMiddleGridSquare();//getRandomGridSquare();//getMiddleGridSquare();
 262                    currentDirection = direction_of(nextGridSquare);
 263                }
 264            }
 265            
 266        }
 267        //if(changeDirection) currentDirection = direction_of(nextGridSquare);
 268        if(nextGridSquare != null) setGridSquare(nextGridSquare);
 269   */
 270        
 271        
 272/*
 273        GridSquare currentGridSquare = getGridSquare();
 274        
 275        GridVector surroundingSquares = currentGridSquare.getGridSquares(1).getFreeGridSquares().getGridSquaresInDirection(currentGridSquare, currentDirection);
 276        
 277        GridSquare nextGridSquare =  currentGridSquare.getGridSquare(currentDirection);
 278    
 279        if(currentGridSquare.isNest()) {
 280            surroundingSquares = currentGridSquare.getGridSquares(1).getFreeGridSquares();
 281            if(surroundingSquares.getPheromoneStrength() > 0)
 282                surroundingSquares = surroundingSquares.getPheromoneGridSquares();
 283               //System.out.println(surroundingSquares.size());     
 284               
 285        }      
 286        
 287        if(surroundingSquares.isEmpty()) {
 288            
 289             surroundingSquares = currentGridSquare.getGridSquares(1).getFreeGridSquares();
 290     
 291            
 292        }
 293            if(surroundingSquares.getPheromoneStrength() > 0 && !carryingFood())
 294                surroundingSquares = surroundingSquares.getPheromoneGridSquares();    
 295        
 296        if(nextGridSquare == null || nextGridSquare.containsObstacle() 
 297
 298        || (carryingFood() && nextGridSquare.containsFood()) 
 299        ) {
 300            
 301            if(surroundingSquares.isEmpty())
 302                surroundingSquares = currentGridSquare.getGridSquares(1).getFreeGridSquares();
 303            
 304            try {
 305                nextGridSquare = surroundingSquares.getEmptyGridSquares().getRandomGridSquare();
 306              //  currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 307            }catch (Exception e) {
 308                //no where to move - wait
 309            }
 310            
 311        }else if(carryingFood()) { // && !nextGridSquare.containsFood()
 312            
 313            if(currentGridSquare.isNest()) {
 314                Grid.getInstance().getNest().addAnt(this);
 315//            }else if( surroundingSquares.getFoodStrength() > 0) {
 316//                surroundingSquares.getGridSquaresInDirection(currentGridSquare,currentGridSquare.getDirectionOf(Grid.getInstance().getNest().getGridSquare()));
 317//                 Collections.sort(surroundingSquares, Collections.reverseOrder());
 318//                nextGridSquare = surroundingSquares.getRandomGridSquare(true);
 319            }else{
 320                
 321//                if(surroundingSquares.getFoodStrength() != 0) {
 322//                    Collections.sort(surroundingSquares, Collections.reverseOrder()); 
 323//                    nextGridSquare = surroundingSquares.firstElement();
 324//                }else{
 325                    nextGridSquare = currentGridSquare.getGridSquare(currentGridSquare.getDirectionOf(Grid.getInstance().getNest().getGridSquare()));
 326                     currentDirection = currentGridSquare.getDirectionOf(Grid.getInstance().getNest().getGridSquare());
 327//                }
 328            }
 329            
 330        }else if(surroundingSquares.containsFood()) { // && !carryingFood()
 331            
 332            nextGridSquare = surroundingSquares.getFoodGridSquares().getRandomGridSquare(true);//.firstElement();
 333            carry(nextGridSquare.getFood());
 334          
 335        }else if( surroundingSquares.getFoodStrength() > 0) {
 336            
 337            Collections.sort(surroundingSquares);
 338            nextGridSquare = surroundingSquares.getRandomGridSquare(true);//.firstElement();
 339            currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 340        }else if(surroundingSquares.getPheromoneStrength() > 0) {
 341//            if(nextGridSquare.getPheromone().getStrength() >= currentGridSquare.getPheromone().getStrength()) {
 342//                nextGridSquare = currentGridSquare.getGridSquare(currentDirection);
 343//            }else{
 344                
 345                Collections.sort(surroundingSquares, GridSquare.PheromoneStrengthComparator);
 346                
 347                //nextGridSquare = surroundingSquares.firstElement();
 348                
 349                if(currentGridSquare.isNest()) {
 350                    nextGridSquare = surroundingSquares.firstElement();
 351                 
 352                }else
 353                    nextGridSquare = surroundingSquares.getRandomGridSquare(true);
 354                 
 355//            }
 356            currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 357        }
 358
 359        if(!currentGridSquare.equals(nextGridSquare) && carryingFood()) {
 360
 361                currentGridSquare.getPheromone().increaseStrength();
 362
 363        }
 364        
 365        if(nextGridSquare == null || surroundingSquares.size() == 0) {
 366            nextGridSquare = currentGridSquare;
 367           currentDirection = GridSquare.getRandomDirection(); 
 368            
 369   //         surroundingSquares = currentGridSquare.getGridSquares(1).getFreeGridSquares();
 370     //       nextGridSquare = surroundingSquares.getRandomGridSquare();
 371     //   currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 372           
 373        }else if(carryingFood() && nextGridSquare.isNest()) {
 374           // nextGridSquare = currentGridSquare;
 375            
 376             Grid.getInstance().getNest().addAnt(this);
 377        
 378        }else
 379        if(nextGridSquare.containsAnt() || nextGridSquare.containsObstacle()) {
 380                Collections.sort(surroundingSquares, GridSquare.PheromoneStrengthComparator);
 381                nextGridSquare = surroundingSquares.firstElement();//.getRandomGridSquare(true);
 382
 383        }else      
 384        if((carryingFood() && nextGridSquare.containsFood())) {// || nextGridSquare.containsObstacle()) { // || nextGridSquare.containsAnt()) { //(carryingFood() && nextGridSquare.containsAnt())) {
 385               nextGridSquare = currentGridSquare;
 386              // currentDirection = GridSquare.getRandomDirection();    
 387               
 388        }
 389        
 390
 391        
 392
 393
 394        setGridSquare(nextGridSquare);
 395    currentDirection = currentGridSquare.getDirectionOf(nextGridSquare);
 396                        
 397
 398 */       
 399
 400    }
 401   
 402    public void carry(Food food) {
 403      //  System.out.println("Carrying...");
 404       // Grid.getInstance().printStats();
 405       
 406        try {
 407            notifyListeners(new PickedUpFoodAntEvent(this));
 408            this.setFood(food);
 409            food.getGridSquare().recalculateFoodStrength(false);
 410            food.getGridSquare().setObject(null);
 411            food.setGridSquare(null);
 412        }catch (RuntimeException e) {
 413           // System.out.println("Already gone!");
 414        }
 415    }
 416    
 417    public boolean carryingFood() {
 418        return getFood() != null;
 419    }
 420    
 421    public void run() {
 422        //Call the move method while the Thread is running. And sleep for a time based on iSpeed.
 423        while(move != null) {
 424            try {
 425                move();
 426          
 427                move.sleep(200);
 428            }catch (InterruptedException e) {}
 429        }
 430    }
 431    
 432    /**
 433     * Start or stop the rectangle moving.
 434     * @param start true to start, false to stop.
 435     */
 436    public void start(boolean start) {
 437        if(start) start(); else stop();
 438    }
 439    
 440    /**
 441     * Start the rectangle moving.
 442     */
 443    public void start() {
 444        //Create and start a new Thread.
 445        move = new Thread(this);
 446      //  move.setDaemon(true);
 447        move.start();
 448    }
 449    
 450    /**
 451     * Stop the rectangle moving.
 452     */
 453    public void stop() {
 454        move = null;
 455    }
 456
 457    
 458    public Color getColor() {
 459        if(food == null) 
 460            return Color.red;//new Color((int)Math.random() * 255 + 100, (int)Math.random() * 255+100, (int)Math.random() * 255+100);//Color.red;
 461        else
 462            return Color.YELLOW;
 463    }
 464
 465    public Food getFood() {
 466        return food;
 467    }
 468
 469    public void setFood(Food food) {
 470        this.food = food;
 471    }
 472    
 473    public String toString() {
 474        return "Ant" + getGridSquare().getGridSquares(1).getGridSquaresInDirection(getGridSquare(), currentDirection).getFreeGridSquares() + ";direction: " + currentDirection;
 475    }
 476    
 477    public void draw(Graphics g) {
 478        super.draw(g);
 479
 480    
 481    }
 482}
 483

· Ant.java ends ·

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