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

· Pheromone.java ·

   1/*
   2 * Pheromone.java
   3 *
   4 * Created on 15 October 2006, 23:31
   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.Point;
  14
  15/**
  16 *
  17 * @author James Hamilton
  18 */
  19public class Pheromone extends MyObject implements Runnable {
  20    
  21    private double strength = 0;
  22    private Thread move;
  23    private int rate = 1000;
  24    private boolean go = true;
  25    private int maxStrength = 3;
  26    
  27    private int direction = -1;
  28    
  29    /** Creates a new instance of Pheromone */
  30    public Pheromone() {
  31        setStrength(0);
  32                    move = new Thread(this);
  33            move.setDaemon(true);
  34            move.start();
  35    }
  36    
  37    public Pheromone(GridSquare gs) {
  38        super(gs);
  39        move = new Thread(this);
  40        move.setDaemon(true);
  41        move.start();
  42        setStrength(0);
  43
  44    }
  45    
  46    public void run() {
  47        while(go) {
  48            
  49            try {
  50                 if(strength> 0.0)  strength -= (strength * 0.1);
  51                 if(strength <= 0.0001) { strength = 0; direction = -1;}
  52                    move.sleep(rate);//strength * 1000);
  53                    
  54           //      if(strength > 0)System.out.println(strength);
  55                    //System.out.println(getGridSquare() + ": " + strength);
  56// if(strength < 0.01) strength =0;
  57//                   if(strength <= 0) {
  58//                        stop();
  59//                   }
  60                
  61            }catch (InterruptedException e) {
  62                System.out.println(e);
  63            }
  64            
  65        }
  66    }
  67    
  68    public void increaseStrength(int direction) {
  69        strength += 0.5;
  70        Point thisP = getGridSquare().getCoordinateXY();
  71        setDirection(direction);
  72        GridVector squares = getGridSquare().getGridSquares(5);
  73        for(GridSquare s : squares) {
  74             Point p = s.getCoordinateXY();
  75             double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
  76             
  77             s.getPheromone().increaseStrength(1/(c2*500));
  78            if( s.getPheromone().getDirection() == -1) s.getPheromone().setDirection(s.getDirectionOf(this.getGridSquare()));
  79        }  
  80        
  81        if(strength > maxStrength) strength = maxStrength;
  82    }
  83    
  84    public void increaseStrength() {
  85       
  86        strength += 0.5;
  87        Point thisP = getGridSquare().getCoordinateXY();
  88        
  89        GridVector squares = getGridSquare().getGridSquares(5);
  90        for(GridSquare s : squares) {
  91             Point p = s.getCoordinateXY();
  92             double c2 = Math.pow( thisP.x - p.x , 2) + Math.pow( thisP.y - p.y , 2);
  93             
  94             s.getPheromone().increaseStrength(1/(c2*500));
  95        }
  96   /*     
  97          GridVector s = getGridSquare().getGridSquares(1);
  98            for(GridSquare gs: s)
  99                gs.getPheromone().increaseStrength(0.01);
 100            s = getGridSquare().getGridSquares(2);
 101            for(GridSquare gs: s)
 102                gs.getPheromone().increaseStrength(0.01);   
 103s = getGridSquare().getGridSquares(3);
 104            for(GridSquare gs: s)
 105                gs.getPheromone().increaseStrength(0.01);   
 106s = getGridSquare().getGridSquares(4);
 107            for(GridSquare gs: s)
 108                gs.getPheromone().increaseStrength(0.01);   
 109*/
 110            
 111         if(strength > maxStrength) strength = maxStrength;
 112//                if(strength > 0) {
 113//            
 114//            
 115//            start();
 116//        }else{
 117//            stop();
 118//        }
 119    }
 120
 121    public void increaseStrength(double n) {
 122       
 123        strength += n;
 124         if(strength > maxStrength) strength = maxStrength;
 125//                if(strength > 0) {
 126//            start();
 127//        }else{
 128//            stop();
 129//        }
 130    }    
 131    
 132    public Color getColor() {
 133        if(!go && strength == 0) return Color.black;
 134        java.lang.Double blue = new java.lang.Double(((double)strength) * 10 + 100 < 255 ? ((double)strength) * 10 + 100 : 255);
 135      
 136        return new Color(0, 0, blue.intValue());
 137    }
 138
 139    public double getStrength() {
 140        
 141        return strength;
 142    }
 143
 144    public void setStrength(int strength) {
 145        if(strength > 10) strength = 10;
 146        this.strength = strength;
 147        
 148        
 149        
 150//        if(strength > 0) {
 151//            start();
 152//        }else{
 153//            stop();
 154//        }
 155        
 156    }
 157    
 158    public void start() {
 159        go = true;
 160        if(move == null) {
 161                            move = new Thread(this);
 162            move.setDaemon(true);
 163            move.start();
 164        }
 165            
 166    }
 167    
 168    public void stop() {
 169        go = false;
 170        strength = 0;
 171        //move = null;
 172    }
 173
 174    public int getDirection() {
 175        return direction;
 176    }
 177
 178    public void setDirection(int direction) {
 179       
 180        this.direction = direction;
 181    }
 182}
 183

· Pheromone.java ends ·

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